Binman: fix signing an encrypted FIT with a preload key
cover Binman: fix signing an encrypted FIT with a preload key
When running the test case testPreLoadEncryptedFit, mkimage has been called multiple times. Each call to Entry_fit's GetData falls into Entry_fit's BuildSectionData then mkimage is called. The last mkimage is called after the image has been signed with the preload key. As mkimage uses a random IV for encryption and the timestamps may differ, There is a mismatch between the previously calculated signature and the final fit included in the image.
During ProcessImage, how can one tell when exactly a fit is well generated, and stop the useless mkimage afterwards?
Paul HENRYS (2):
binman: Generate the preload header and sign the data only once
tools: binman: Test signing an encrypted FIT with a preload header
yan wang (1):
binman: Fix signing an encryted FIT with a preload key
tools/binman/etype/pre_load.py | 12 ++-- tools/binman/ftest.py | 17 +++++ tools/binman/image.py | 10 +++ .../test/336_pre_load_fit_encrypted.dts | 63 +++++++++++++++++++ 4 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 tools/binman/test/336_pre_load_fit_encrypted.dts
Signature · 1 line
2.25.1
PATCH 1/3 binman: Generate the preload header and sign the data only once
From: Paul HENRYS <[email protected]>
To optimize the preload generation, the header and signatures are only generated once all data have been collected. This avoids to sign multiple times.
--- tools/binman/etype/pre_load.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
tools/binman/etype/pre_load.py +6 -6
@@ -152,14 +152,14 @@ class Entry_pre_load(Entry_collection): return data + pad def ObtainContents(self): - """Obtain a placeholder for the header contents""" - # wait that the image is available - self.image = self.GetContents(False) - if self.image is None: - return False - self.SetContents(self._CreateHeader()) + """Do nothing here. + The data will be obtained and handled in ProcessContents + """ return True def ProcessContents(self): + self.image = self.GetContents(True) + if self.image is None: + return False data = self._CreateHeader() return self.ProcessContentsUpdate(data)
Signature · 1 line
2.25.1
PATCH 2/3 binman: Fix signing an encryted FIT with a preload key
In reply to yan wangAt the end of several passes of packing entries, a FIT is already generated. This is the FIT with which preload header has signed. Don't rerun the mkimage to generate a new FIT after the preload header has already signed the FIT. Because when encrypting the image with a random IV or if the timestamp in the FIT has changed, the signature will be different.
--- tools/binman/image.py | 10 ++++++++++ 1 file changed, 10 insertions(+)
tools/binman/image.py +10 -0
@@ -13,6 +13,7 @@ import re import sys from binman.entry import Entry +from binman.etype import fit from binman.etype import fdtmap from binman.etype import image_header from binman.etype import section
@@ -178,11 +179,20 @@ class Image(section.Entry_section): """Write symbol values into binary files for access at run time""" super().WriteSymbols(self) + def mark_fit_build_done(self): + """Mark Entry_fit as already built""" + entries = self.GetEntries() + if entries: + for entry in entries.values(): + if isinstance(entry, fit.Entry_fit): + entry.mark_build_done() + def BuildImage(self): """Write the image to a file""" fname = tools.get_output_filename(self._filename) tout.info("Writing image to '%s'" % fname) with open(fname, 'wb') as fd: + self.mark_fit_build_done() data = self.GetPaddedData() fd.write(data) tout.info("Wrote %#x bytes" % len(data))
Signature · 1 line
2.25.1
PATCH 3/3 tools: binman: Test signing an encrypted FIT with a preload header
In reply to yan wangFrom: Paul HENRYS <[email protected]>
This test is added to work on issue currently faced with binman. Binman calls multiple times mkimage and thus generates the FIT multiple times and the last call happens after the preload header has been generated. When encrypting the image with a random IV or if the timestamp in the FIT has changed between the 2 last calls of mkimage, the preload header would not sign the correct data leading to a corrupted image.
--- tools/binman/ftest.py | 17 +++++ .../test/336_pre_load_fit_encrypted.dts | 63 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 tools/binman/test/336_pre_load_fit_encrypted.dts
tools/binman/ftest.py +17 -0
@@ -5870,6 +5870,23 @@ fdt fdtmap Extract the devicetree blob from the fdtmap data = self._DoReadFileDtb('236_pre_load_invalid_key.dts', entry_args=entry_args) + def testPreLoadEncryptedFit(self): + """Test an encrypted FIT image with a pre-load header""" + entry_args = { + 'pre-load-key-path': os.path.join(self._binman_dir, 'test'), + } + data = self._DoReadFileDtb( + '336_pre_load_fit_encrypted.dts', entry_args=entry_args, + extra_indirs=[os.path.join(self._binman_dir, 'test')])[0] + + image_fname = tools.get_output_filename('image.bin') + is_signed = self._CheckPreload(image_fname, self.TestFile("dev.key")) + + self.assertEqual(PRE_LOAD_MAGIC, data[:len(PRE_LOAD_MAGIC)]) + self.assertEqual(PRE_LOAD_VERSION, data[4:4 + len(PRE_LOAD_VERSION)]) + self.assertEqual(PRE_LOAD_HDR_SIZE, data[8:8 + len(PRE_LOAD_HDR_SIZE)]) + self.assertEqual(is_signed, True) + def _CheckSafeUniqueNames(self, *images): """Check all entries of given images for unsafe unique names""" for image in images:
tools/binman/test/336_pre_load_fit_encrypted.dts new file +63 -0
63 lines not on this page · raw message
Signature · 1 line
2.25.1
Quoted 18 lines · yan wang
When running the test case testPreLoadEncryptedFit, mkimage has been called multiple times. Each call to Entry_fit's GetData falls into Entry_fit's BuildSectionData then mkimage is called. The last mkimage is called after the image has been signed with the preload key. As mkimage uses a random IV for encryption and the timestamps may differ, There is a mismatch between the previously calculated signature and the final fit included in the image.
During ProcessImage, how can one tell when exactly a fit is well generated, and stop the useless mkimage afterwards?
Paul HENRYS (2):
binman: Generate the preload header and sign the data only once
tools: binman: Test signing an encrypted FIT with a preload headeryan wang (1):
binman: Fix signing an encryted FIT with a preload key
Quoted 6 lines
tools/binman/etype/pre_load.py | 12 ++-- tools/binman/ftest.py | 17 +++++ tools/binman/image.py | 10 +++ .../test/336_pre_load_fit_encrypted.dts | 63 +++++++++++++++++++ 4 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 tools/binman/test/336_pre_load_fit_encrypted.dts
I had a small merge problem to resolve when applying this and maybe I didn't do it right because now CI fails:
https://source.denx.de/u-boot/u-boot/-/jobs/1198369
Please rebase, retest and repost, thanks!
Signature · 1 line
Tom