Skip to contentSkip to conversation

binman: pre_load: add sign-size parameter for partial image signing

PATCH Superseded Paul HENRYS 2 participants 5 messages Nov 20 – Nov 23

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 binman: pre_load: add sign-size parameter for partial image signing

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
---
 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
tools/binman/etype/pre_load.py +40 -6
index 7890c1c62a8..3612307d4ab 100644
@@ -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
tools/binman/ftest.py +41 -0

53 lines not on this page · raw message

tools/binman/test/security/pre_load_ecdsa_sign_size_too_large.dts new file +23 -0
new file mode 100644
index 00000000000..aefcf287104
@@ -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";
+		};
+	};
+};
tools/binman/test/security/pre_load_rsa_sign_size_too_large.dts new file +23 -0
new file mode 100644
index 00000000000..5b547c09aa6
@@ -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";
+		};
+	};
+};
tools/binman/test/security/pre_load_sign_size.dts new file +23 -0
new file mode 100644
index 00000000000..fb99349323a
@@ -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";
+		};
+	};
+};
Signature · 1 line
2.43.0
#1 lore raw
Quoted 10 lines · Paul HENRYS

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

Our AI policy is currently "Please don't", and I do owe and have been half drafting a longer write-up on that.

Quoted 17 lines
---
 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
tools/binman/etype/pre_load.py +0 -0
index 7890c1c62a8..3612307d4ab 100644

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

Quoted 8 lines
tools/binman/etype/pre_load.py +5 -0
@@ -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.

Why does this comment need to be almost 5x as long as all of the others for this function? Thanks.

Signature · 1 line
Tom
#2 lore raw

Dear Tom,

Quoted 6 lines · Tom Rini
Quoted 5 lines · Paul HENRYS

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.

Quoted 7 lines
Quoted 5 lines

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

Our AI policy is currently "Please don't", and I do owe and have been half drafting a longer write-up on that.

Does it mean, don't use AI or just don't add the prefix?
I saw some other commits with AI assisted information so I just added it as well. I just pushed another patch after reading your message. I will also correct that there.

Quoted 13 lines
Quoted 13 lines
---
  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

Quoted 15 lines

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

Quoted 13 lines
tools/binman/etype/pre_load.py +5 -0
index 7890c1c62a8..3612307d4ab 100644
@@ -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.

Why does this comment need to be almost 5x as long as all of the others for this function? Thanks.

Here, that is a typical abuse of the AI which is much better than me at writing English but more prolix... I missed that and will sum it up. Thx.

Quoted 1 line
#3 lore raw
Quoted 13 lines · Paul HENRYS

Dear Tom,

Quoted 10 lines · Tom Rini
Quoted 9 lines · Paul HENRYS

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.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Assisted-by Claude Sonnet 4.6

Our AI policy is currently "Please don't", and I do owe and have been half drafting a longer write-up on that.

Does it mean, don't use AI or just don't add the prefix?
I saw some other commits with AI assisted information so I just added it as well.
I just pushed another patch after reading your message. I will also correct that there.

That's part of what makes it harder to write up. Yes, it's "please don't use AI", and also yes, some people don't proactively disclose and of those some content is clear enough that no one would know if it wasn't disclosed.

Quoted 27 lines
Quoted 12 lines
Quoted 12 lines
---
  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

Quoted 15 lines

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

Quoted 13 lines
tools/binman/etype/pre_load.py +5 -0
index 7890c1c62a8..3612307d4ab 100644
@@ -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.

Why does this comment need to be almost 5x as long as all of the others for this function? Thanks.

Quoted 3 lines

Here, that is a typical abuse of the AI which is much better than me at writing English but more prolix...
I missed that and will sum it up. Thx.

I figured as much. And I really hate the notion that AI is better at writing, in part because I keep track of how often AI hallucinates (rather than questionable translation) over on my social media, of the few emails a day I throw at it, to see if my own understanding of them is correct.

Signature · 1 line
Tom
#4 lore raw

PATCH v2 1/1 binman: pre_load: add sign-size parameter for partial image signing

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.

---
Changes for v2:
- Refactore the code and only use self.sign_size
- Rewrite lengthy AI comments

 tools/binman/etype/pre_load.py                | 20 ++++++---
 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, 124 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
tools/binman/etype/pre_load.py +14 -6
index 7890c1c62a8..8d4442a38f5 100644
@@ -53,6 +53,7 @@ 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 (optional).
 
     This entry creates a pre-load header that contains a global
     image signature.
@@ -86,6 +87,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 +108,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)
+        self.sign_size = self.sign_size if self.sign_size else len(self.image)
+        if self.sign_size < 0 or self.sign_size > len(self.image):
+            self.Raise(f"sign-size {self.sign_size} is invalid: must be between 0 and {len(self.image)}")
         hash_image = SHAS[hash_name].new()
-        hash_image.update(self.image)
+        hash_image.update(self.image[:self.sign_size])
 
         # Compute the signature
         if padding_name is None:
@@ -132,7 +137,7 @@ class Entry_pre_load(Entry_collection):
 
         version = self.version
         header_size = self.header_size
-        image_size = len(self.image)
+        image_size = self.sign_size
         ofs_img_sig = 64 + len(sig)
         flags = 0
         reserved0 = 0
@@ -164,9 +169,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)
+        self.sign_size = self.sign_size if self.sign_size else len(self.image)
+        if self.sign_size < 0 or self.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[:self.sign_size])
 
         # Compute the signature
         signer = DSS.new(key, 'fips-186-3')
@@ -177,7 +185,7 @@ class Entry_pre_load(Entry_collection):
 
         version = self.version
         header_size = self.header_size
-        image_size = len(self.image)
+        image_size = self.sign_size
         ofs_img_sig = 64 + len(sig)
         flags = 0
         reserved0 = 0
tools/binman/ftest.py +41 -0

53 lines not on this page · raw message

tools/binman/test/security/pre_load_ecdsa_sign_size_too_large.dts new file +23 -0
new file mode 100644
index 00000000000..aefcf287104
@@ -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";
+		};
+	};
+};
tools/binman/test/security/pre_load_rsa_sign_size_too_large.dts new file +23 -0
new file mode 100644
index 00000000000..5b547c09aa6
@@ -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";
+		};
+	};
+};
tools/binman/test/security/pre_load_sign_size.dts new file +23 -0
new file mode 100644
index 00000000000..fb99349323a
@@ -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";
+		};
+	};
+};
Signature · 1 line
2.43.0