All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] binman: pre_load: add sign-size parameter for partial image signing
@ 2026-11-20 15:36 Paul HENRYS
  2026-11-20 15:40 ` Tom Rini
  0 siblings, 1 reply; 4+ messages in thread
From: Paul HENRYS @ 2026-11-20 15:36 UTC (permalink / raw)
  To: u-boot; +Cc: alpernebiyasak, trini, philippe.reynes, paul.henrys_ext

Add an optional sign-size property to the pre-load entry type, allowing
users to sign only a leading portion of the image rather than the whole
thing. When set, only the first sign-size bytes are hashed and the
image_size field in the pre-load header is set accordingly.

This modification is particularly useful when signing FIT images whose
data are stored externally to the FIT and hashes are already
calculated and stored in the FIT header.

Assisted-by: Claude Sonnet 4.6
Signed-off-by: Paul HENRYS <paul.henrys_ext@softathome.com>
---
 tools/binman/etype/pre_load.py                | 46 ++++++++++++++++---
 tools/binman/ftest.py                         | 41 +++++++++++++++++
 .../pre_load_ecdsa_sign_size_too_large.dts    | 23 ++++++++++
 .../pre_load_rsa_sign_size_too_large.dts      | 23 ++++++++++
 .../test/security/pre_load_sign_size.dts      | 23 ++++++++++
 5 files changed, 150 insertions(+), 6 deletions(-)
 create mode 100644 tools/binman/test/security/pre_load_ecdsa_sign_size_too_large.dts
 create mode 100644 tools/binman/test/security/pre_load_rsa_sign_size_too_large.dts
 create mode 100644 tools/binman/test/security/pre_load_sign_size.dts

diff --git a/tools/binman/etype/pre_load.py b/tools/binman/etype/pre_load.py
index 7890c1c62a8..3612307d4ab 100644
--- a/tools/binman/etype/pre_load.py
+++ b/tools/binman/etype/pre_load.py
@@ -53,6 +53,11 @@ class Entry_pre_load(Entry_collection):
         - key-name: Filename of the private key to sign
         - header-size: Total size of the header
         - version: Version of the header
+        - sign-size: Number of bytes of the image to sign (default: sign the
+          entire image). When set, only the first sign-size bytes are hashed
+          and the header image_size field is set to this value. This allows
+          the runtime verifier to authenticate a leading portion of the image
+          while ignoring any trailing data.
 
     This entry creates a pre-load header that contains a global
     image signature.
@@ -72,6 +77,28 @@ class Entry_pre_load(Entry_collection):
                     version = <1>;
                 };
 
+                image: blob-ext {
+                    filename = "sandbox.itb";
+                };
+            };
+        };
+
+    To sign only the first 65536 bytes of the image::
+
+        binman {
+            image2 {
+                filename = "sandbox.bin";
+
+                pre-load {
+                    content = <&image>;
+                    algo-name = "sha256,rsa2048";
+                    padding-name = "pss";
+                    key-name = "private.pem";
+                    header-size = <4096>;
+                    version = <1>;
+                    sign-size = <65536>;
+                };
+
                 image: blob-ext {
                     filename = "sandbox.itb";
                 };
@@ -86,6 +113,7 @@ class Entry_pre_load(Entry_collection):
         self.key_name = fdt_util.GetString(self._node, 'key-name')
         self.header_size = fdt_util.GetInt(self._node, 'header-size')
         self.version = fdt_util.GetInt(self._node, 'version')
+        self.sign_size = fdt_util.GetInt(self._node, 'sign-size', 0)
 
     def ReadNode(self):
         super().ReadNode()
@@ -106,9 +134,12 @@ class Entry_pre_load(Entry_collection):
         if key.size_in_bytes() != RSAS[sign_name]:
             self.Raise("The key " + self.key_name + " doesn't have the expected size")
 
-        # Compute the hash
+        # Compute the hash (over the full image or only the first sign_size bytes)
+        sign_size = self.sign_size if self.sign_size else len(self.image)
+        if sign_size <= 0 or sign_size > len(self.image):
+            self.Raise(f"sign-size {self.sign_size} is invalid: must be between 1 and {len(self.image)}")
         hash_image = SHAS[hash_name].new()
-        hash_image.update(self.image)
+        hash_image.update(self.image[:sign_size])
 
         # Compute the signature
         if padding_name is None:
@@ -132,7 +163,7 @@ class Entry_pre_load(Entry_collection):
 
         version = self.version
         header_size = self.header_size
-        image_size = len(self.image)
+        image_size = sign_size
         ofs_img_sig = 64 + len(sig)
         flags = 0
         reserved0 = 0
@@ -164,9 +195,12 @@ class Entry_pre_load(Entry_collection):
         if key.pointQ.size_in_bytes() * 2 != ECDSAS[sign_name]:
             self.Raise("The key " + self.key_name + " doesn't have the expected size")
 
-        # Compute the hash
+        # Compute the hash (over the full image or only the first sign_size bytes)
+        sign_size = self.sign_size if self.sign_size else len(self.image)
+        if sign_size <= 0 or sign_size > len(self.image):
+            self.Raise(f"sign-size {self.sign_size} is invalid: must be between 1 and {len(self.image)}")
         hash_image = SHAS[hash_name].new()
-        hash_image.update(self.image)
+        hash_image.update(self.image[:sign_size])
 
         # Compute the signature
         signer = DSS.new(key, 'fips-186-3')
@@ -177,7 +211,7 @@ class Entry_pre_load(Entry_collection):
 
         version = self.version
         header_size = self.header_size
-        image_size = len(self.image)
+        image_size = sign_size
         ofs_img_sig = 64 + len(sig)
         flags = 0
         reserved0 = 0
diff --git a/tools/binman/ftest.py b/tools/binman/ftest.py
index 567ffa96b2b..90d0178ac9a 100644
--- a/tools/binman/ftest.py
+++ b/tools/binman/ftest.py
@@ -5850,6 +5850,36 @@ fdt         fdtmap                Extract the devicetree blob from the fdtmap
         self.assertEqual(PRE_LOAD_HDR_SIZE, data[8:8 + len(PRE_LOAD_HDR_SIZE)])
         self.assertEqual(is_signed, True)
 
+    def testPreLoadSignSize(self):
+        """Test a pre-load header that signs only part of the image"""
+        entry_args = {
+            'pre-load-key-path': os.path.join(self._binman_dir, 'test'),
+        }
+        data = self._DoReadFileDtb(
+            'security/pre_load_sign_size.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)])
+        # image_size in the header must reflect sign-size=4, not full image size
+        self.assertEqual((4).to_bytes(4, 'big'), data[12:16])
+        self.assertEqual(is_signed, True)
+
+    def testPreLoadSignSizeTooLarge(self):
+        """Test a pre-load header with sign-size larger than the image"""
+        entry_args = {
+            'pre-load-key-path': os.path.join(self._binman_dir, 'test'),
+        }
+        with self.assertRaises(ValueError) as exc:
+            self._DoReadFileDtb('security/pre_load_rsa_sign_size_too_large.dts',
+                                entry_args=entry_args)
+        self.assertIn('sign-size', str(exc.exception))
+        self.assertIn('invalid', str(exc.exception))
+
     def testPreLoadNoKey(self):
         """Test an image with a pre-load heade0r with missing key"""
         with self.assertRaises(FileNotFoundError) as exc:
@@ -5982,6 +6012,17 @@ fdt         fdtmap                Extract the devicetree blob from the fdtmap
         self.assertIn("Node '/binman/pre-load': The key ecdsa521.pem doesn't have the expected size",
                       str(exc.exception))
 
+    def testPreLoadEcdsaSignSizeTooLarge(self):
+        """Test a pre-load ecdsa header with sign-size larger than the image"""
+        entry_args = {
+            'pre-load-key-path': os.path.join(self._binman_dir, 'test'),
+        }
+        with self.assertRaises(ValueError) as exc:
+            self._DoReadFileDtb('security/pre_load_ecdsa_sign_size_too_large.dts',
+                                entry_args=entry_args)
+        self.assertIn('sign-size', str(exc.exception))
+        self.assertIn('invalid', str(exc.exception))
+
     def _CheckSafeUniqueNames(self, *images):
         """Check all entries of given images for unsafe unique names"""
         for image in images:
diff --git a/tools/binman/test/security/pre_load_ecdsa_sign_size_too_large.dts b/tools/binman/test/security/pre_load_ecdsa_sign_size_too_large.dts
new file mode 100644
index 00000000000..aefcf287104
--- /dev/null
+++ b/tools/binman/test/security/pre_load_ecdsa_sign_size_too_large.dts
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+/dts-v1/;
+
+/ {
+	#address-cells = <1>;
+	#size-cells = <1>;
+
+	binman {
+		pre-load {
+			content = <&image>;
+			algo-name = "sha256,ecdsa521";
+			key-name = "ecdsa521.pem";
+			header-size = <4096>;
+			version = <0x11223344>;
+			sign-size = <8>;
+		};
+
+		image: blob-ext {
+			filename = "refcode.bin";
+		};
+	};
+};
diff --git a/tools/binman/test/security/pre_load_rsa_sign_size_too_large.dts b/tools/binman/test/security/pre_load_rsa_sign_size_too_large.dts
new file mode 100644
index 00000000000..5b547c09aa6
--- /dev/null
+++ b/tools/binman/test/security/pre_load_rsa_sign_size_too_large.dts
@@ -0,0 +1,23 @@
+// 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>;
+			 sign-size = <8>;
+		};
+
+		image: blob-ext {
+			filename = "refcode.bin";
+		};
+	};
+};
diff --git a/tools/binman/test/security/pre_load_sign_size.dts b/tools/binman/test/security/pre_load_sign_size.dts
new file mode 100644
index 00000000000..fb99349323a
--- /dev/null
+++ b/tools/binman/test/security/pre_load_sign_size.dts
@@ -0,0 +1,23 @@
+// 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>;
+			 sign-size = <4>;
+		};
+
+		image: blob-ext {
+			filename = "refcode.bin";
+		};
+	};
+};
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-20 18:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 15:36 [PATCH] binman: pre_load: add sign-size parameter for partial image signing Paul HENRYS
2026-11-20 15:40 ` Tom Rini
2026-11-20 16:00   ` Paul HENRYS
2026-11-20 16:24     ` Tom Rini

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.