Skip to contentSkip to conversation

tools: binman: Test signing an encrypted FIT with a preload header

PATCH In review yan wang 4 participants 39 messages Apr 2 – May 5

lore
Share & tools
lore

yan wang

author
#0 lore raw

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

From: Paul HENRYS <[email protected]>

Add a test to verify the preload header correctly signs an encrypted FIT. This test exercises the case where encryption uses random IVs that would change between mkimage calls.

---
Changes for v2:
- Rename test file as 351_pre_load_fit_encrypted.dts
- Update the commit message according to the remarks made

 tools/binman/ftest.py                         | 17 +++++
 .../test/351_pre_load_fit_encrypted.dts       | 63 +++++++++++++++++++
 2 files changed, 80 insertions(+)
 create mode 100644 tools/binman/test/351_pre_load_fit_encrypted.dts
tools/binman/ftest.py +17 -0
index a53e37f31b3..dcfe97f0444 100644
@@ -5894,6 +5894,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(
+            '351_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/351_pre_load_fit_encrypted.dts new file +63 -0

63 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 2/3 binman: Generate preload header and sign data only once

From: Paul HENRYS <[email protected]>

To optimize preload generation, generate the header and signatures only after all data has been collected. This avoids signing the data multiple times.

Since header_size is known upfront (from __init__), set the preload header size in ObtainContents() to avoid an extra packing pass when ProcessContentsUpdate() detects the size changed from 0 to header_size.

This reduces unnecessary repacking and signing operations.

---
Changes for v2:
- Set contents_size knowing header_size in ObtainContents()

 tools/binman/etype/pre_load.py | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
tools/binman/etype/pre_load.py +6 -6
index 00f1a896767..34a7bc1b985 100644
@@ -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())
+        """Only set the known size of the preload header. The data will be
+        generated later on in ProcessContents() when the data from every entries
+        can be retrieved.
+        """
+        self.contents_size = self.header_size
         return True
 
     def ProcessContents(self):
+        self.image = self.GetContents(True)
         data = self._CreateHeader()
         return self.ProcessContentsUpdate(data)
Signature · 1 line
2.43.0
Reviewed-by on v2 2/3
#2 lore raw

Re: [PATCH v2 2/3] binman: Generate preload header and sign data only once

Quoted 10 lines · yan wang

binman: Generate preload header and sign data only once

To optimize preload generation, generate the header and signatures only after all data has been collected. This avoids signing the data multiple times.

Since header_size is known upfront (from __init__), set the preload header size in ObtainContents() to avoid an extra packing pass when ProcessContentsUpdate() detects the size changed from 0 to header_size.

Quoted 6 lines

This reduces unnecessary repacking and signing operations.

tools/binman/etype/pre_load.py | 12 ++++++------

 1 file changed, 6 insertions(+), 6 deletions(-)
author
#3 lore raw

PATCH v2 3/3 binman: collection: Set build_done on referenced entries

In reply to yan wang

The collection etype uses phandles in the 'content' property to reference other entries. Mark each referenced entry with build_done to avoid rebuilding the same entry data multiple times.

This is important for cases where rebuilding may change the data content, e.g. due to timestamps or random IVs in encryption.

Refactor GetContentsByPhandle() to return both the entry object and its data.

---
Changes for v2:
- Refactor GetContentsByPhandle() to return both the entry object and its data

 tools/binman/etype/collection.py | 8 ++++++--
 tools/binman/etype/section.py    | 5 +++--
 2 files changed, 9 insertions(+), 4 deletions(-)
tools/binman/etype/collection.py +6 -2
index c532aafe3e7..5ea6344bd25 100644
@@ -45,12 +45,16 @@ class Entry_collection(Entry):
         self.Info('Getting contents, required=%s' % required)
         data = bytearray()
         for entry_phandle in self.content:
-            entry_data = self.section.GetContentsByPhandle(entry_phandle, self,
-                                                           required)
+            entry, entry_data = self.section.GetContentsByPhandle(
+                entry_phandle, self, required)
             if not required and entry_data is None:
                 self.Info('Contents not available yet')
                 # Data not available yet
                 return None
+
+            # Mark referenced entries as build_done to avoid rebuilding
+            entry.build_done = True
+
             data += entry_data
 
         self.Info('Returning contents size %x' % len(data))
tools/binman/etype/section.py +3 -2
index 6a26d687056..8530b7ee17f 100644
@@ -557,7 +557,8 @@ class Entry_section(Entry):
                 return None
 
         Returns:
-            data from associated entry (as a string), or None if not found
+            tuple: (entry, data) where entry is the Entry object and data is
+                from that entry (as a string), or (entry, None) if data not found
         """
         node = self._node.GetFdt().LookupPhandle(phandle)
         if not node:
@@ -565,7 +566,7 @@ class Entry_section(Entry):
         entry = self.FindEntryByNode(node)
         if not entry:
             source_entry.Raise("Cannot find entry for node '%s'" % node.name)
-        return entry.GetData(required)
+        return entry, entry.GetData(required)
 
     def LookupEntry(self, entries, sym_name, msg):
         """Look up the entry for a binman symbol
Signature · 1 line
2.43.0
Reviewed-by on v2 3/3
#4 lore raw

Re: [PATCH v2 3/3] binman: collection: Set build_done on referenced entries

Quoted 9 lines · yan wang

binman: collection: Set build_done on referenced entries

The collection etype uses phandles in the 'content' property to reference other entries. Mark each referenced entry with build_done to avoid rebuilding the same entry data multiple times.

This is important for cases where rebuilding may change the data content, e.g. due to timestamps or random IVs in encryption.

Quoted 8 lines

Refactor GetContentsByPhandle() to return both the entry object and its data.

tools/binman/etype/collection.py | 8 ++++++--

 tools/binman/etype/section.py    | 5 +++--
 2 files changed, 9 insertions(+), 4 deletions(-)
#5 lore raw
In reply to yan wang
Quoted 12 lines · yan wang

From: Paul HENRYS <[email protected]>

Add a test to verify the preload header correctly signs an encrypted FIT. This test exercises the case where encryption uses random IVs that would change between mkimage calls.

---
Changes for v2:
- Rename test file as 351_pre_load_fit_encrypted.dts
- Update the commit message according to the remarks made

Quoted 4 lines
 tools/binman/ftest.py                         | 17 +++++
 .../test/351_pre_load_fit_encrypted.dts       | 63 +++++++++++++++++++
 2 files changed, 80 insertions(+)
 create mode 100644 tools/binman/test/351_pre_load_fit_encrypted.dts

This should be against next, and we stopped using a numeric prefix on test files and instead put them in to appropriate directories there (so tools/binman/test/fit in this case), thanks.

Signature · 1 line
Tom
#6 lore raw

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

In reply to yan wang

Add a test to verify the preload header correctly signs an encrypted FIT. This test exercises the case where encryption uses random IVs that would change between mkimage calls.

---
Changes for v3:
- Rebase against 'next' branch
- Move test in tools/binman/test/fit without a numeric prefix
- Update encryption key path passed to _DoReadFileDtb()

 tools/binman/ftest.py                         | 21 +++++++
 .../test/fit/pre_load_fit_encrypted.dts       | 63 +++++++++++++++++++
 2 files changed, 84 insertions(+)
 create mode 100644 tools/binman/test/fit/pre_load_fit_encrypted.dts
tools/binman/ftest.py +21 -0
index ca5149ee654..301c7705837 100644
@@ -5895,6 +5895,27 @@ fdt         fdtmap                Extract the devicetree blob from the fdtmap
             data = self._DoReadFileDtb('security/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 = tools.read_file(self.TestFile("fit/aes256.bin"))
+        self._MakeInputFile("keys/aes256.bin", data)
+
+        keys_subdir = os.path.join(self._indir, "keys")
+        data = self._DoReadFileDtb(
+            'fit/pre_load_fit_encrypted.dts', entry_args=entry_args,
+            extra_indirs=[keys_subdir])[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/fit/pre_load_fit_encrypted.dts new file +63 -0

63 lines not on this page · raw message

Signature · 1 line
2.43.0
#7 lore raw

PATCH v3 2/3 binman: Generate preload header and sign data only once

To optimize preload generation, generate the header and signatures only after all data has been collected. This avoids signing the data multiple times.

Since header_size is known upfront (from __init__), set the preload header size in ObtainContents() to avoid an extra packing pass when ProcessContentsUpdate() detects the size changed from 0 to header_size.

This reduces unnecessary repacking and signing operations.

---
Changes for v3:
- Rebase against 'next' branch

 tools/binman/etype/pre_load.py | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
tools/binman/etype/pre_load.py +6 -6
index 00f1a896767..34a7bc1b985 100644
@@ -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())
+        """Only set the known size of the preload header. The data will be
+        generated later on in ProcessContents() when the data from every entries
+        can be retrieved.
+        """
+        self.contents_size = self.header_size
         return True
 
     def ProcessContents(self):
+        self.image = self.GetContents(True)
         data = self._CreateHeader()
         return self.ProcessContentsUpdate(data)
Signature · 1 line
2.43.0
#8 lore raw

PATCH v3 3/3 binman: collection: Set build_done on referenced entries

In reply to Paul HENRYS

From: yan wang <[email protected]>

The collection etype uses phandles in the 'content' property to reference other entries. Mark each referenced entry with build_done to avoid rebuilding the same entry data multiple times.

This is important for cases where rebuilding may change the data content, e.g. due to timestamps or random IVs in encryption.

Refactor GetContentsByPhandle() to return both the entry object and its data.

---
Changes for v3:
- Rebase against 'next' branch

 tools/binman/etype/collection.py | 8 ++++++--
 tools/binman/etype/section.py    | 5 +++--
 2 files changed, 9 insertions(+), 4 deletions(-)
tools/binman/etype/collection.py +6 -2
index c532aafe3e7..5ea6344bd25 100644
@@ -45,12 +45,16 @@ class Entry_collection(Entry):
         self.Info('Getting contents, required=%s' % required)
         data = bytearray()
         for entry_phandle in self.content:
-            entry_data = self.section.GetContentsByPhandle(entry_phandle, self,
-                                                           required)
+            entry, entry_data = self.section.GetContentsByPhandle(
+                entry_phandle, self, required)
             if not required and entry_data is None:
                 self.Info('Contents not available yet')
                 # Data not available yet
                 return None
+
+            # Mark referenced entries as build_done to avoid rebuilding
+            entry.build_done = True
+
             data += entry_data
 
         self.Info('Returning contents size %x' % len(data))
tools/binman/etype/section.py +3 -2
index 6a26d687056..8530b7ee17f 100644
@@ -557,7 +557,8 @@ class Entry_section(Entry):
                 return None
 
         Returns:
-            data from associated entry (as a string), or None if not found
+            tuple: (entry, data) where entry is the Entry object and data is
+                from that entry (as a string), or (entry, None) if data not found
         """
         node = self._node.GetFdt().LookupPhandle(phandle)
         if not node:
@@ -565,7 +566,7 @@ class Entry_section(Entry):
         entry = self.FindEntryByNode(node)
         if not entry:
             source_entry.Raise("Cannot find entry for node '%s'" % node.name)
-        return entry.GetData(required)
+        return entry, entry.GetData(required)
 
     def LookupEntry(self, entries, sym_name, msg):
         """Look up the entry for a binman symbol
Signature · 1 line
2.43.0
#9 lore raw
In reply to Paul HENRYS

Hi Tom,

Just after pushing the v3 I realized that this test would better go under tools/binman/test/security/ instead of tools/binman/test/fit/. I am going to push a v4 with this change. Sorry for the inconvenience.

Kind regards,
Paul

Quoted 117 lines · Paul HENRYS

Add a test to verify the preload header correctly signs an encrypted FIT. This test exercises the case where encryption uses random IVs that would change between mkimage calls.

---
Changes for v3:
- Rebase against 'next' branch
- Move test in tools/binman/test/fit without a numeric prefix
- Update encryption key path passed to _DoReadFileDtb()

  tools/binman/ftest.py                         | 21 +++++++
  .../test/fit/pre_load_fit_encrypted.dts       | 63 +++++++++++++++++++
  2 files changed, 84 insertions(+)

create mode 100644 tools/binman/test/fit/pre_load_fit_encrypted.dts

tools/binman/ftest.py +21 -0
index ca5149ee654..301c7705837 100644
@@ -5895,6 +5895,27 @@ fdt         fdtmap                Extract the devicetree blob from the fdtmap
              data = self._DoReadFileDtb('security/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 = tools.read_file(self.TestFile("fit/aes256.bin"))
+        self._MakeInputFile("keys/aes256.bin", data)
+
+        keys_subdir = os.path.join(self._indir, "keys")
+        data = self._DoReadFileDtb(
+            'fit/pre_load_fit_encrypted.dts', entry_args=entry_args,
+            extra_indirs=[keys_subdir])[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/fit/pre_load_fit_encrypted.dts new file +63 -0
new file mode 100644
index 00000000000..f5e9bf9426c
@@ -0,0 +1,63 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+/dts-v1/;
+
+/ {
+	#address-cells = <1>;
+	#size-cells = <1>;
+
+	binman {
+		pre-load {
+			content = <&image>;
+			algo-name = "sha256,rsa2048";
+			key-name = "dev.key";
+			header-size = <4096>;
+			version = <0x11223344>;
+		};
+
+		image: fit {
+			fit,encrypt;
+			description = "Test a FIT with encrypted data and signed with a preload";
+			#address-cells = <1>;
+
+			images {
+				u-boot {
+					description = "U-Boot";
+					type = "firmware";
+					arch = "arm64";
+					os = "U-Boot";
+					compression = "none";
+					load = <00000000>;
+					entry = <00000000>;
+					cipher {
+						algo = "aes256";
+						key-name-hint = "aes256";
+					};
+					u-boot-nodtb {
+					};
+				};
+				fdt-1 {
+					description = "Flattened Device Tree blob";
+					type = "flat_dt";
+					arch = "arm64";
+					compression = "none";
+					cipher {
+						algo = "aes256";
+						key-name-hint = "aes256";
+					};
+					u-boot-dtb {
+					};
+				};
+			};
+
+			configurations {
+				default = "conf-1";
+				conf-1 {
+					description = "Boot U-Boot with FDT blob";
+					firmware = "u-boot";
+					fdt = "fdt-1";
+				};
+			};
+		};
+	};
+};
#10 lore raw
Quoted 5 lines · Paul HENRYS

Hi Tom,

Just after pushing the v3 I realized that this test would better go under tools/binman/test/security/ instead of tools/binman/test/fit/. I am going to push a v4 with this change. Sorry for the inconvenience.

Thanks. It looks like there might be a v5, so please add a cover letter too, this will be used as part of merging the series.

Signature · 1 line
Tom
#11 lore raw

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

In reply to Paul HENRYS

Add a test to verify the preload header correctly signs an encrypted FIT. This test exercises the case where encryption uses random IVs that would change between mkimage calls.

---
Changes for v4:
- Move test in tools/binman/test/security

 tools/binman/ftest.py                         | 21 +++++++
 .../test/security/pre_load_fit_encrypted.dts  | 63 +++++++++++++++++++
 2 files changed, 84 insertions(+)
 create mode 100644 tools/binman/test/security/pre_load_fit_encrypted.dts
tools/binman/ftest.py +21 -0
index ca5149ee654..da8325f820a 100644
@@ -5895,6 +5895,27 @@ fdt         fdtmap                Extract the devicetree blob from the fdtmap
             data = self._DoReadFileDtb('security/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 = tools.read_file(self.TestFile("fit/aes256.bin"))
+        self._MakeInputFile("keys/aes256.bin", data)
+
+        keys_subdir = os.path.join(self._indir, "keys")
+        data = self._DoReadFileDtb(
+            'security/pre_load_fit_encrypted.dts', entry_args=entry_args,
+            extra_indirs=[keys_subdir])[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/security/pre_load_fit_encrypted.dts new file +63 -0

63 lines not on this page · raw message

Signature · 1 line
2.43.0
#12 lore raw

PATCH v4 2/3 binman: Generate preload header and sign data only once

To optimize preload generation, generate the header and signatures only after all data has been collected. This avoids signing the data multiple times.

Since header_size is known upfront (from __init__), set the preload header size in ObtainContents() to avoid an extra packing pass when ProcessContentsUpdate() detects the size changed from 0 to header_size.

This reduces unnecessary repacking and signing operations.

---
Changes for v4:
- No change

 tools/binman/etype/pre_load.py | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
tools/binman/etype/pre_load.py +6 -6
index 00f1a896767..34a7bc1b985 100644
@@ -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())
+        """Only set the known size of the preload header. The data will be
+        generated later on in ProcessContents() when the data from every entries
+        can be retrieved.
+        """
+        self.contents_size = self.header_size
         return True
 
     def ProcessContents(self):
+        self.image = self.GetContents(True)
         data = self._CreateHeader()
         return self.ProcessContentsUpdate(data)
Signature · 1 line
2.43.0
#13 lore raw

Re: [PATCH v4 2/3] binman: Generate preload header and sign data only once

Hi Paul,

Quoted 16 lines · Paul HENRYS

binman: Generate preload header and sign data only once

To optimize preload generation, generate the header and signatures only after all data has been collected. This avoids signing the data multiple times.

Since header_size is known upfront (from __init__), set the preload header size in ObtainContents() to avoid an extra packing pass when ProcessContentsUpdate() detects the size changed from 0 to header_size.

This reduces unnecessary repacking and signing operations.

tools/binman/etype/pre_load.py class Entry_pre_load(Entry_collection): · line 158

Quoted 7 lines
tools/binman/etype/pre_load.py +7 -0
@@ -152,14 +152,14 @@ class Entry_pre_load(Entry_collection):
+    def ObtainContents(self):
+        """Only set the known size of the preload header. The data will be
+        generated later on in ProcessContents() when the data from every entries
+        can be retrieved.
+        """
+        self.contents_size = self.header_size
+        return True

Setting contents_size without calling SetContents() is unusual. Most entry types provide placeholder data so GetData() returns something valid rather than None. While section.py handles None by substituting pad bytes, this feels like relying on a side-effect.

Please can you call SetContents() with placeholder data of header_size length instead, similar to image_header? That would be more consistent with other entry types.

Also the docstring should have the summary on the first line per Python conventions.

Regards,
Simon

#14 lore raw

PATCH v4 3/3 binman: collection: Set build_done on referenced entries

In reply to Paul HENRYS

From: yan wang <[email protected]>

The collection etype uses phandles in the 'content' property to reference other entries. Mark each referenced entry with build_done to avoid rebuilding the same entry data multiple times.

This is important for cases where rebuilding may change the data content, e.g. due to timestamps or random IVs in encryption.

Refactor GetContentsByPhandle() to return both the entry object and its data.

---
Changes for v4:
- No change

 tools/binman/etype/collection.py | 8 ++++++--
 tools/binman/etype/section.py    | 5 +++--
 2 files changed, 9 insertions(+), 4 deletions(-)
tools/binman/etype/collection.py +6 -2
index c532aafe3e7..5ea6344bd25 100644
@@ -45,12 +45,16 @@ class Entry_collection(Entry):
         self.Info('Getting contents, required=%s' % required)
         data = bytearray()
         for entry_phandle in self.content:
-            entry_data = self.section.GetContentsByPhandle(entry_phandle, self,
-                                                           required)
+            entry, entry_data = self.section.GetContentsByPhandle(
+                entry_phandle, self, required)
             if not required and entry_data is None:
                 self.Info('Contents not available yet')
                 # Data not available yet
                 return None
+
+            # Mark referenced entries as build_done to avoid rebuilding
+            entry.build_done = True
+
             data += entry_data
 
         self.Info('Returning contents size %x' % len(data))
tools/binman/etype/section.py +3 -2
index 6a26d687056..8530b7ee17f 100644
@@ -557,7 +557,8 @@ class Entry_section(Entry):
                 return None
 
         Returns:
-            data from associated entry (as a string), or None if not found
+            tuple: (entry, data) where entry is the Entry object and data is
+                from that entry (as a string), or (entry, None) if data not found
         """
         node = self._node.GetFdt().LookupPhandle(phandle)
         if not node:
@@ -565,7 +566,7 @@ class Entry_section(Entry):
         entry = self.FindEntryByNode(node)
         if not entry:
             source_entry.Raise("Cannot find entry for node '%s'" % node.name)
-        return entry.GetData(required)
+        return entry, entry.GetData(required)
 
     def LookupEntry(self, entries, sym_name, msg):
         """Look up the entry for a binman symbol
Signature · 1 line
2.43.0
Reviewed-by on v4 3/3
#15 lore raw

Re: [PATCH v4 3/3] binman: collection: Set build_done on referenced entries

Quoted 9 lines · Paul HENRYS

binman: collection: Set build_done on referenced entries

The collection etype uses phandles in the 'content' property to reference other entries. Mark each referenced entry with build_done to avoid rebuilding the same entry data multiple times.

This is important for cases where rebuilding may change the data content, e.g. due to timestamps or random IVs in encryption.

Quoted 8 lines

Refactor GetContentsByPhandle() to return both the entry object and its data.

tools/binman/etype/collection.py | 8 ++++++--

 tools/binman/etype/section.py    | 5 +++--
 2 files changed, 9 insertions(+), 4 deletions(-)
#16 lore raw

v5 cover binman: Fix preload signing with encrypted FIT

In reply to Paul HENRYS

This series improves the reliability and efficiency of binman preload header generation and test it against an encrypted FIT image signed with a preload header.

When a preload header references other entries (e.g. an encrypted FIT) through the collection etype, the referenced entries may be rebuilt multiple times during binman processing. This becomes problematic when the referenced entry produces non-deterministic output, such as FIT encryption using random IVs or timestamps, since rebuilding the entry changes the data.

This series ensures that referenced entries are built only once and that preload signing is performed after all data is collected. It also avoids unnecessary repacking or repeated signing operations by the preload.

The changes include:
* generate preload header placeholders in ObtainContents() and sign data only once in ProcessContentsUpdate()
* mark referenced entries as build_done in the collection etype to avoid rebuilding data
* add a functional test for signing an encrypted FIT with a preload header

Changes in v5:
- Generate preload placeholder in ObtainContents() to avoid unnecessary repacking
- Move functional test patch to the end of the series

Paul HENRYS (2):
binman: Generate preload header and sign data only once
tools: binman: Test signing an encrypted FIT with a preload header

Yan Wang (1):
binman: collection: Set build_done on referenced entries

 tools/binman/etype/collection.py              |  8 ++-
 tools/binman/etype/pre_load.py                |  9 +--
 tools/binman/etype/section.py                 |  5 +-
 tools/binman/ftest.py                         | 21 +++++++
 .../test/security/pre_load_fit_encrypted.dts  | 63 +++++++++++++++++++
 5 files changed, 96 insertions(+), 10 deletions(-)
 create mode 100644 tools/binman/test/security/pre_load_fit_encrypted.dts
Signature · 1 line
2.43.0
#17 lore raw

PATCH v5 1/3 binman: Generate preload header and sign data only once

To optimize preload generation, generate the header and signatures only after all data has been collected in ProcessContentsUpdate(). This avoids signing the data multiple times.

Since header_size is known upfront (from __init__), create a placeholder in ObtainContents() to avoid an extra packing pass when ProcessContentsUpdate() detects a size change.

This reduces unnecessary repacking and signing operations.

---

Changes in v5:
- Generate a placeholder in ObtainContents() to avoid unnecessary repack

 tools/binman/etype/pre_load.py | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
tools/binman/etype/pre_load.py +3 -6
index 00f1a896767..0d953cb258e 100644
@@ -152,14 +152,11 @@ 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())
+        """Create a placeholder for the header"""
+        self.SetContents(tools.get_bytes(0, self.header_size))
         return True
 
     def ProcessContents(self):
+        self.image = self.GetContents(True)
         data = self._CreateHeader()
         return self.ProcessContentsUpdate(data)
Signature · 1 line
2.43.0
Reviewed-by on v5 1/3
#18 lore raw

Re: [PATCH v5 1/3] binman: Generate preload header and sign data only once

Quoted 10 lines · Paul HENRYS

binman: Generate preload header and sign data only once

To optimize preload generation, generate the header and signatures only after all data has been collected in ProcessContentsUpdate(). This avoids signing the data multiple times.

Since header_size is known upfront (from __init__), create a placeholder in ObtainContents() to avoid an extra packing pass when ProcessContentsUpdate() detects a size change.

Quoted 6 lines

This reduces unnecessary repacking and signing operations.

tools/binman/etype/pre_load.py | 9 +++------

 1 file changed, 3 insertions(+), 6 deletions(-)
#19 lore raw

PATCH v5 2/3 binman: collection: Set build_done on referenced entries

In reply to Paul HENRYS

From: yan wang <[email protected]>

The collection etype uses phandles in the 'content' property to reference other entries. Mark each referenced entry with build_done to avoid rebuilding the same entry data multiple times.

This is important for cases where rebuilding may change the data content, e.g. due to timestamps or random IVs in encryption.

Refactor GetContentsByPhandle() to return both the entry object and its data.

---

Changes in v5:
- No changes

 tools/binman/etype/collection.py | 8 ++++++--
 tools/binman/etype/section.py    | 5 +++--
 2 files changed, 9 insertions(+), 4 deletions(-)
tools/binman/etype/collection.py +6 -2
index c532aafe3e7..5ea6344bd25 100644
@@ -45,12 +45,16 @@ class Entry_collection(Entry):
         self.Info('Getting contents, required=%s' % required)
         data = bytearray()
         for entry_phandle in self.content:
-            entry_data = self.section.GetContentsByPhandle(entry_phandle, self,
-                                                           required)
+            entry, entry_data = self.section.GetContentsByPhandle(
+                entry_phandle, self, required)
             if not required and entry_data is None:
                 self.Info('Contents not available yet')
                 # Data not available yet
                 return None
+
+            # Mark referenced entries as build_done to avoid rebuilding
+            entry.build_done = True
+
             data += entry_data
 
         self.Info('Returning contents size %x' % len(data))
tools/binman/etype/section.py +3 -2
index 6a26d687056..8530b7ee17f 100644
@@ -557,7 +557,8 @@ class Entry_section(Entry):
                 return None
 
         Returns:
-            data from associated entry (as a string), or None if not found
+            tuple: (entry, data) where entry is the Entry object and data is
+                from that entry (as a string), or (entry, None) if data not found
         """
         node = self._node.GetFdt().LookupPhandle(phandle)
         if not node:
@@ -565,7 +566,7 @@ class Entry_section(Entry):
         entry = self.FindEntryByNode(node)
         if not entry:
             source_entry.Raise("Cannot find entry for node '%s'" % node.name)
-        return entry.GetData(required)
+        return entry, entry.GetData(required)
 
     def LookupEntry(self, entries, sym_name, msg):
         """Look up the entry for a binman symbol
Signature · 1 line
2.43.0
#20 lore raw

Re: [PATCH v5 2/3] binman: collection: Set build_done on referenced entries

Hi Yan,

Quoted 20 lines · Paul HENRYS

binman: collection: Set build_done on referenced entries

The collection etype uses phandles in the 'content' property to reference other entries. Mark each referenced entry with build_done to avoid rebuilding the same entry data multiple times.

This is important for cases where rebuilding may change the data content, e.g. due to timestamps or random IVs in encryption.

Refactor GetContentsByPhandle() to return both the entry object and its data.

tools/binman/etype/collection.py | 8 ++++++--

 tools/binman/etype/section.py    | 5 +++--
 2 files changed, 9 insertions(+), 4 deletions(-)

tools/binman/etype/collection.py class Entry_collection(Entry): · line 46

Quoted 2 lines
tools/binman/etype/collection.py +2 -0
@@ -45,12 +45,16 @@ class Entry_collection(Entry):
+            # Mark referenced entries as build_done to avoid rebuilding
+            entry.build_done = True

Just a thought - have you tested this against testCollectionSection and testMkimageCollection?

An alternative would be to set build_done only when required=True, so it happens during ProcessContents() rather than ObtainContents():

if required:
entry.build_done = True

What do you think?

At some point we should try to come up with a better algorithm for when Binman rebuilds the contents of an entry.

Regards,
Simon

author
#21 lore raw

v6 cover binman: Fix preload signing with encrypted FIT

In reply to Paul HENRYS

This series improves the reliability and efficiency of binman preload header generation and test it against an encrypted FIT image signed with a preload header.

When a preload header references other entries (e.g. an encrypted FIT) through the collection etype, the referenced entries may be rebuilt multiple times during binman processing. This becomes problematic when the referenced entry produces non-deterministic output, such as FIT encryption using random IVs or timestamps, since rebuilding the entry changes the data.

This series ensures that referenced entries are built only once and that preload signing is performed after all data is collected. It also avoids unnecessary repacking or repeated signing operations by the preload.

The changes include:
* generate preload header placeholders in ObtainContents() and sign data only once in ProcessContentsUpdate()
* mark referenced entries as build_done in the collection etype to avoid rebuilding data
* add a functional test for signing an encrypted FIT with a preload header

Changes in v6:
- set build_done only when required=True, so it happens during ProcessContents() rather than ObtainContents()

Paul HENRYS (2):
binman: Generate preload header and sign data only once
tools: binman: Test signing an encrypted FIT with a preload header

yan wang (1):
binman: collection: Set build_done on referenced entries

 tools/binman/etype/collection.py              |  9 ++-
 tools/binman/etype/pre_load.py                |  9 +--
 tools/binman/etype/section.py                 |  5 +-
 tools/binman/ftest.py                         | 21 +++++++
 .../test/security/pre_load_fit_encrypted.dts  | 63 +++++++++++++++++++
 5 files changed, 97 insertions(+), 10 deletions(-)
 create mode 100644 tools/binman/test/security/pre_load_fit_encrypted.dts
Signature · 1 line
2.25.1
author
#22 lore raw

PATCH v6 1/3 binman: Generate preload header and sign data only once

From: Paul HENRYS <[email protected]>

To optimize preload generation, generate the header and signatures only after all data has been collected in ProcessContentsUpdate(). This avoids signing the data multiple times.

Since header_size is known upfront (from __init__), create a placeholder in ObtainContents() to avoid an extra packing pass when ProcessContentsUpdate() detects a size change.

This reduces unnecessary repacking and signing operations.

---

Changes in v6:
- No changes

 tools/binman/etype/pre_load.py | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
tools/binman/etype/pre_load.py +3 -6
index 00f1a896767..0d953cb258e 100644
@@ -152,14 +152,11 @@ 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())
+        """Create a placeholder for the header"""
+        self.SetContents(tools.get_bytes(0, self.header_size))
         return True
 
     def ProcessContents(self):
+        self.image = self.GetContents(True)
         data = self._CreateHeader()
         return self.ProcessContentsUpdate(data)
Signature · 1 line
2.25.1
Reviewed-by on v6 1/3
#23 lore raw

Re: [PATCH v6 1/3] binman: Generate preload header and sign data only once

Quoted 10 lines · Yan WANG

binman: Generate preload header and sign data only once

To optimize preload generation, generate the header and signatures only after all data has been collected in ProcessContentsUpdate(). This avoids signing the data multiple times.

Since header_size is known upfront (from __init__), create a placeholder in ObtainContents() to avoid an extra packing pass when ProcessContentsUpdate() detects a size change.

Quoted 6 lines

This reduces unnecessary repacking and signing operations.

tools/binman/etype/pre_load.py | 9 +++------

 1 file changed, 3 insertions(+), 6 deletions(-)
author
#24 lore raw

PATCH v6 2/3 binman: collection: Set build_done on referenced entries

In reply to Yan WANG

From: yan wang <[email protected]>

The collection etype uses phandles in the 'content' property to reference other entries. Mark each referenced entry with build_done to avoid rebuilding the same entry data multiple times.

This is important for cases where rebuilding may change the data content, e.g. due to timestamps or random IVs in encryption.

Refactor GetContentsByPhandle() to return both the entry object and its data.

---

Changes in v6:
- set build_done only when required=True, so it happens during ProcessContents() rather than ObtainContents()

 tools/binman/etype/collection.py | 9 +++++++--
 tools/binman/etype/section.py    | 5 +++--
 2 files changed, 10 insertions(+), 4 deletions(-)
tools/binman/etype/collection.py +7 -2
index c532aafe3e7..c0ada42e3b2 100644
@@ -45,12 +45,17 @@ class Entry_collection(Entry):
         self.Info('Getting contents, required=%s' % required)
         data = bytearray()
         for entry_phandle in self.content:
-            entry_data = self.section.GetContentsByPhandle(entry_phandle, self,
-                                                           required)
+            entry, entry_data = self.section.GetContentsByPhandle(
+                entry_phandle, self, required)
             if not required and entry_data is None:
                 self.Info('Contents not available yet')
                 # Data not available yet
                 return None
+
+            # Mark referenced entries as build_done to avoid rebuilding
+            if required:
+                entry.build_done = True
+
             data += entry_data
 
         self.Info('Returning contents size %x' % len(data))
tools/binman/etype/section.py +3 -2
index 6a26d687056..8530b7ee17f 100644
@@ -557,7 +557,8 @@ class Entry_section(Entry):
                 return None
 
         Returns:
-            data from associated entry (as a string), or None if not found
+            tuple: (entry, data) where entry is the Entry object and data is
+                from that entry (as a string), or (entry, None) if data not found
         """
         node = self._node.GetFdt().LookupPhandle(phandle)
         if not node:
@@ -565,7 +566,7 @@ class Entry_section(Entry):
         entry = self.FindEntryByNode(node)
         if not entry:
             source_entry.Raise("Cannot find entry for node '%s'" % node.name)
-        return entry.GetData(required)
+        return entry, entry.GetData(required)
 
     def LookupEntry(self, entries, sym_name, msg):
         """Look up the entry for a binman symbol
Signature · 1 line
2.25.1
#25 lore raw

Re: [PATCH v6 2/3] binman: collection: Set build_done on referenced entries

Hi Yan,

Quoted 20 lines · Yan WANG

binman: collection: Set build_done on referenced entries

The collection etype uses phandles in the 'content' property to reference other entries. Mark each referenced entry with build_done to avoid rebuilding the same entry data multiple times.

This is important for cases where rebuilding may change the data content, e.g. due to timestamps or random IVs in encryption.

Refactor GetContentsByPhandle() to return both the entry object and its data.

tools/binman/etype/collection.py | 9 +++++++--

 tools/binman/etype/section.py    | 5 +++--
 2 files changed, 10 insertions(+), 4 deletions(-)

tools/binman/etype/collection.py class Entry_collection(Entry): · line 47

Quoted 3 lines
tools/binman/etype/collection.py +3 -0
@@ -45,12 +45,17 @@ class Entry_collection(Entry):
+            # Mark referenced entries as build_done to avoid rebuilding
+            if required:
+                entry.build_done = True

I believe this should use mark_build_done() instead, as it recursively marks child entries in sections.

Regards,
Simon

author
#26 lore raw

v7 cover binman: Fix preload signing with encrypted FIT

In reply to Yan WANG

This series improves the reliability and efficiency of binman preload header generation and test it against an encrypted FIT image signed with a preload header.

When a preload header references other entries (e.g. an encrypted FIT) through the collection etype, the referenced entries may be rebuilt multiple times during binman processing. This becomes problematic when the referenced entry produces non-deterministic output, such as FIT encryption using random IVs or timestamps, since rebuilding the entry changes the data.

This series ensures that referenced entries are built only once and that preload signing is performed after all data is collected. It also avoids unnecessary repacking or repeated signing operations by the preload.

The changes include:
* generate preload header placeholders in ObtainContents() and sign data only once in ProcessContentsUpdate()
* mark referenced entries as build_done in the collection etype to avoid rebuilding data
* add a functional test for signing an encrypted FIT with a preload header

Changes in v7:
- use mark_build_done() to recursively mark child entries

Paul HENRYS (2):
binman: Generate preload header and sign data only once
tools: binman: Test signing an encrypted FIT with a preload header

yan wang (1):
binman: collection: Set build_done on referenced entries

 tools/binman/etype/collection.py              |  9 ++-
 tools/binman/etype/pre_load.py                |  9 +--
 tools/binman/etype/section.py                 |  5 +-
 tools/binman/ftest.py                         | 21 +++++++
 .../test/security/pre_load_fit_encrypted.dts  | 63 +++++++++++++++++++
 5 files changed, 97 insertions(+), 10 deletions(-)
 create mode 100644 tools/binman/test/security/pre_load_fit_encrypted.dts
Signature · 1 line
2.25.1
author
#27 lore raw

PATCH v7 1/3 binman: Generate preload header and sign data only once

From: Paul HENRYS <[email protected]>

To optimize preload generation, generate the header and signatures only after all data has been collected in ProcessContentsUpdate(). This avoids signing the data multiple times.

Since header_size is known upfront (from __init__), create a placeholder in ObtainContents() to avoid an extra packing pass when ProcessContentsUpdate() detects a size change.

This reduces unnecessary repacking and signing operations.

---

Changes in v7:
- No changes

 tools/binman/etype/pre_load.py | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
tools/binman/etype/pre_load.py +3 -6
index 00f1a896767..0d953cb258e 100644
@@ -152,14 +152,11 @@ 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())
+        """Create a placeholder for the header"""
+        self.SetContents(tools.get_bytes(0, self.header_size))
         return True
 
     def ProcessContents(self):
+        self.image = self.GetContents(True)
         data = self._CreateHeader()
         return self.ProcessContentsUpdate(data)
Signature · 1 line
2.25.1
Reviewed-by on v7 1/3
#28 lore raw

Re: [PATCH v7 1/3] binman: Generate preload header and sign data only once

Quoted 10 lines · Yan WANG

binman: Generate preload header and sign data only once

To optimize preload generation, generate the header and signatures only after all data has been collected in ProcessContentsUpdate(). This avoids signing the data multiple times.

Since header_size is known upfront (from __init__), create a placeholder in ObtainContents() to avoid an extra packing pass when ProcessContentsUpdate() detects a size change.

Quoted 6 lines

This reduces unnecessary repacking and signing operations.

tools/binman/etype/pre_load.py | 9 +++------

 1 file changed, 3 insertions(+), 6 deletions(-)
author
#29 lore raw

PATCH v7 2/3 binman: collection: Set build_done on referenced entries

In reply to Yan WANG

From: yan wang <[email protected]>

The collection etype uses phandles in the 'content' property to reference other entries. Mark each referenced entry with build_done to avoid rebuilding the same entry data multiple times.

This is important for cases where rebuilding may change the data content, e.g. due to timestamps or random IVs in encryption.

Refactor GetContentsByPhandle() to return both the entry object and its data.

---

Changes in v7:
- use mark_build_done() to recursively mark child entries

 tools/binman/etype/collection.py | 9 +++++++--
 tools/binman/etype/section.py    | 5 +++--
 2 files changed, 10 insertions(+), 4 deletions(-)
tools/binman/etype/collection.py +7 -2
index c532aafe3e7..866ea8dcc1e 100644
@@ -45,12 +45,17 @@ class Entry_collection(Entry):
         self.Info('Getting contents, required=%s' % required)
         data = bytearray()
         for entry_phandle in self.content:
-            entry_data = self.section.GetContentsByPhandle(entry_phandle, self,
-                                                           required)
+            entry, entry_data = self.section.GetContentsByPhandle(
+                entry_phandle, self, required)
             if not required and entry_data is None:
                 self.Info('Contents not available yet')
                 # Data not available yet
                 return None
+
+            # Mark referenced entries as build_done to avoid rebuilding
+            if required:
+                entry.mark_build_done()
+
             data += entry_data
 
         self.Info('Returning contents size %x' % len(data))
tools/binman/etype/section.py +3 -2
index 6a26d687056..8530b7ee17f 100644
@@ -557,7 +557,8 @@ class Entry_section(Entry):
                 return None
 
         Returns:
-            data from associated entry (as a string), or None if not found
+            tuple: (entry, data) where entry is the Entry object and data is
+                from that entry (as a string), or (entry, None) if data not found
         """
         node = self._node.GetFdt().LookupPhandle(phandle)
         if not node:
@@ -565,7 +566,7 @@ class Entry_section(Entry):
         entry = self.FindEntryByNode(node)
         if not entry:
             source_entry.Raise("Cannot find entry for node '%s'" % node.name)
-        return entry.GetData(required)
+        return entry, entry.GetData(required)
 
     def LookupEntry(self, entries, sym_name, msg):
         """Look up the entry for a binman symbol
Signature · 1 line
2.25.1
Reviewed-by on v7 2/3
#30 lore raw

Re: [PATCH v7 2/3] binman: collection: Set build_done on referenced entries

Quoted 9 lines · Yan WANG

binman: collection: Set build_done on referenced entries

The collection etype uses phandles in the 'content' property to reference other entries. Mark each referenced entry with build_done to avoid rebuilding the same entry data multiple times.

This is important for cases where rebuilding may change the data content, e.g. due to timestamps or random IVs in encryption.

Quoted 8 lines

Refactor GetContentsByPhandle() to return both the entry object and its data.

tools/binman/etype/collection.py | 9 +++++++--

 tools/binman/etype/section.py    | 5 +++--
 2 files changed, 10 insertions(+), 4 deletions(-)
author
#31 lore raw

PATCH v7 3/3 tools: binman: Test signing an encrypted FIT with a preload header

In reply to Yan WANG

From: Paul HENRYS <[email protected]>

Add a test to verify the preload header correctly signs an encrypted FIT. This test exercises the case where encryption uses random IVs that would change between mkimage calls.

---

Changes in v7:
- No changes

 tools/binman/ftest.py                         | 21 +++++++
 .../test/security/pre_load_fit_encrypted.dts  | 63 +++++++++++++++++++
 2 files changed, 84 insertions(+)
 create mode 100644 tools/binman/test/security/pre_load_fit_encrypted.dts
tools/binman/ftest.py +21 -0
index ca5149ee654..da8325f820a 100644
@@ -5895,6 +5895,27 @@ fdt         fdtmap                Extract the devicetree blob from the fdtmap
             data = self._DoReadFileDtb('security/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 = tools.read_file(self.TestFile("fit/aes256.bin"))
+        self._MakeInputFile("keys/aes256.bin", data)
+
+        keys_subdir = os.path.join(self._indir, "keys")
+        data = self._DoReadFileDtb(
+            'security/pre_load_fit_encrypted.dts', entry_args=entry_args,
+            extra_indirs=[keys_subdir])[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/security/pre_load_fit_encrypted.dts new file +63 -0

63 lines not on this page · raw message

Signature · 1 line
2.25.1
Reviewed-by on v7 3/3
#32 lore raw
Quoted 11 lines · Yan WANG

tools: binman: Test signing an encrypted FIT with a preload header

Add a test to verify the preload header correctly signs an encrypted FIT. This test exercises the case where encryption uses random IVs that would change between mkimage calls.

tools/binman/ftest.py | 21 ++++++++

 .../test/security/pre_load_fit_encrypted.dts       | 63 ++++++++++++++++++++++
 2 files changed, 84 insertions(+)
#33 lore raw

Re: [PATCH v7 0/3] binman: Fix preload signing with encrypted FIT

In reply to Yan WANG
Quoted 4 lines · Yan WANG

This series improves the reliability and efficiency of binman preload header generation and test it against an encrypted FIT image signed with a preload header.

Quoted 8 lines

When a preload header references other entries (e.g. an encrypted FIT) through the collection etype, the referenced entries may be rebuilt multiple times during binman processing. This becomes problematic when the referenced entry produces non-deterministic output, such as FIT encryption using random IVs or timestamps, since rebuilding the entry changes the data.

[...]

Applied to u-boot/master, thanks!

[1/3] binman: Generate preload header and sign data only once

      commit: 5006121b44a2d147e7e426c74537b3566407e853
[2/3] binman: collection: Set build_done on referenced entries
      commit: 9ff82a771c44a8cf38bd52b556f685ddbc6f1a55
[3/3] tools: binman: Test signing an encrypted FIT with a preload header
      commit: b20d69e5f57bee299380bb30112856e51d8f26b0
Signature · 1 line
Tom
author
#34 lore raw

PATCH v6 3/3 tools: binman: Test signing an encrypted FIT with a preload header

In reply to Yan WANG

From: Paul HENRYS <[email protected]>

Add a test to verify the preload header correctly signs an encrypted FIT. This test exercises the case where encryption uses random IVs that would change between mkimage calls.

---

Changes in v6:
- No changes

 tools/binman/ftest.py                         | 21 +++++++
 .../test/security/pre_load_fit_encrypted.dts  | 63 +++++++++++++++++++
 2 files changed, 84 insertions(+)
 create mode 100644 tools/binman/test/security/pre_load_fit_encrypted.dts
tools/binman/ftest.py +21 -0
index ca5149ee654..da8325f820a 100644
@@ -5895,6 +5895,27 @@ fdt         fdtmap                Extract the devicetree blob from the fdtmap
             data = self._DoReadFileDtb('security/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 = tools.read_file(self.TestFile("fit/aes256.bin"))
+        self._MakeInputFile("keys/aes256.bin", data)
+
+        keys_subdir = os.path.join(self._indir, "keys")
+        data = self._DoReadFileDtb(
+            'security/pre_load_fit_encrypted.dts', entry_args=entry_args,
+            extra_indirs=[keys_subdir])[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/security/pre_load_fit_encrypted.dts new file +63 -0

63 lines not on this page · raw message

Signature · 1 line
2.25.1
Reviewed-by on v6 3/3
#35 lore raw
Quoted 11 lines · Yan WANG

tools: binman: Test signing an encrypted FIT with a preload header

Add a test to verify the preload header correctly signs an encrypted FIT. This test exercises the case where encryption uses random IVs that would change between mkimage calls.

tools/binman/ftest.py | 21 ++++++++

 .../test/security/pre_load_fit_encrypted.dts       | 63 ++++++++++++++++++++++
 2 files changed, 84 insertions(+)
#36 lore raw

PATCH v5 3/3 tools: binman: Test signing an encrypted FIT with a preload header

In reply to Paul HENRYS

Add a test to verify the preload header correctly signs an encrypted FIT. This test exercises the case where encryption uses random IVs that would change between mkimage calls.

---

Changes in v5:
- No changes

 tools/binman/ftest.py                         | 21 +++++++
 .../test/security/pre_load_fit_encrypted.dts  | 63 +++++++++++++++++++
 2 files changed, 84 insertions(+)
 create mode 100644 tools/binman/test/security/pre_load_fit_encrypted.dts
tools/binman/ftest.py +21 -0
index ca5149ee654..da8325f820a 100644
@@ -5895,6 +5895,27 @@ fdt         fdtmap                Extract the devicetree blob from the fdtmap
             data = self._DoReadFileDtb('security/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 = tools.read_file(self.TestFile("fit/aes256.bin"))
+        self._MakeInputFile("keys/aes256.bin", data)
+
+        keys_subdir = os.path.join(self._indir, "keys")
+        data = self._DoReadFileDtb(
+            'security/pre_load_fit_encrypted.dts', entry_args=entry_args,
+            extra_indirs=[keys_subdir])[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/security/pre_load_fit_encrypted.dts new file +63 -0

63 lines not on this page · raw message

Signature · 1 line
2.43.0
Reviewed-by on v5 3/3
#37 lore raw
Quoted 11 lines · Paul HENRYS

tools: binman: Test signing an encrypted FIT with a preload header

Add a test to verify the preload header correctly signs an encrypted FIT. This test exercises the case where encryption uses random IVs that would change between mkimage calls.

tools/binman/ftest.py | 21 ++++++++

 .../test/security/pre_load_fit_encrypted.dts       | 63 ++++++++++++++++++++++
 2 files changed, 84 insertions(+)
#38 lore raw
In reply to Paul HENRYS

Hi Paul,

tools/binman/ftest.py fdt fdtmap Extract the devicetree blob from the fdtmap · line 5896

Quoted 12 lines · Paul HENRYS

tools: binman: Test signing an encrypted FIT with a preload header

Add a test to verify the preload header correctly signs an encrypted FIT. This test exercises the case where encryption uses random IVs that would change between mkimage calls.

tools/binman/ftest.py +2 -0
@@ -5895,6 +5895,27 @@ fdt         fdtmap                Extract the devicetree blob from the fdtmap
+    def testPreLoadEncryptedFit(self):
+        """Test an encrypted FIT image with a pre-load header"""

nit: for bisectability this should come last, since it relies on fixes in patches 2/3 and 3/3.

Regards,
Simon