Skip to contentSkip to conversation

Binman: fix signing an encrypted FIT with a

PATCH In review yan wang 2 participants 4 messages Jun 2, 2025 – Jun 26, 2025

lore
Share & tools
lore

yan wang

author
#0 lore raw

cover Binman: fix signing an encrypted FIT with a

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 (1):
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/ftest.py                         | 17 +++++
 tools/binman/image.py                         | 10 +++
 .../test/336_pre_load_fit_encrypted.dts       | 63 +++++++++++++++++++
 3 files changed, 90 insertions(+)
 create mode 100644 tools/binman/test/336_pre_load_fit_encrypted.dts
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 1/2 tools: binman: Test signing an encrypted FIT with a preload header

From: 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
index 4670ca7d515..0ca04eda9ac 100644
@@ -5863,6 +5863,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
author
#2 lore raw

PATCH 2/2 binman: Fix signing an encryted FIT with a preload key

In reply to yan wang

At 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
index 24ce0af7c72..0118834b76a 100644
@@ -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
#3 lore raw
In reply to yan wang
Quoted 8 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.

Quoted 5 lines

During ProcessImage, how can one tell when exactly a fit is well generated, and stop the useless mkimage afterwards?

Paul HENRYS (1):
tools: binman: Test signing an encrypted FIT with a preload header

This fails CI with:
ERROR: binman.ftest.TestFunctional.testPreLoadEncryptedFit (subunit.RemotedTestCase) binman.ftest.TestFunctional.testPreLoadEncryptedFit
----------------------------------------------------------------------
testtools.testresult.real._StringException: TypeError: can't multiply sequence by non-int of ty pe 'NoneType'

Signature · 1 line
Tom