Use integer division when calculating RSA and ECDSA signature sizes
and reject file-based signing without a key-name property.

Signed-off-by: yan wang <[email protected]>
---
Changes in v2:
- New patch, split out of v1 patch 3
- Use integer division for the RSA and ECDSA signature sizes
- Reject file-based signing when key-name is missing
- Add testPreLoadMissingKeyName and its devicetree (moved from v1 patch 5)

 tools/binman/etype/pre_load.py                | 16 +++++++++------
 tools/binman/ftest.py                         | 11 ++++++++++
 .../security/pre_load_missing_key_name.dts    | 20 +++++++++++++++++++
 3 files changed, 41 insertions(+), 6 deletions(-)
 create mode 100644 tools/binman/test/security/pre_load_missing_key_name.dts

diff --git a/tools/binman/etype/pre_load.py b/tools/binman/etype/pre_load.py
index 7890c1c62a8..49a63ecaafa 100644
--- a/tools/binman/etype/pre_load.py
+++ b/tools/binman/etype/pre_load.py
@@ -24,15 +24,15 @@ from Cryptodome.Signature import DSS
 PRE_LOAD_MAGIC = b'UBSH'
 
 RSAS = {
-    'rsa1024': 1024 / 8,
-    'rsa2048': 2048 / 8,
-    'rsa4096': 4096 / 8
+    'rsa1024': 1024 // 8,
+    'rsa2048': 2048 // 8,
+    'rsa4096': 4096 // 8
 }
 
 ECDSAS = {
-    'ecdsa256': 256 / 8 * 2,
-    'ecdsa384': 384 / 8 * 2,
-    'ecdsa521': (521 + 7) / 8 * 2
+    'ecdsa256': 256 // 8 * 2,
+    'ecdsa384': 384 // 8 * 2,
+    'ecdsa521': (521 + 7) // 8 * 2
 }
 
 SHAS = {
@@ -199,6 +199,10 @@ class Entry_pre_load(Entry_collection):
 
     def _CreateHeader(self):
         """Create a pre load header"""
+
+        # File-based path: dispatch on signing algorithm
+        if not self.key_name:
+            self.Raise('key-name is required for file-based signing')
         hash_name, sign_name = self.algo_name.split(',')
         padding_name = self.padding_name
         key_name = os.path.join(self.key_path, self.key_name)
diff --git a/tools/binman/ftest.py b/tools/binman/ftest.py
index 8caf275d466..d4522e905e0 100644
--- a/tools/binman/ftest.py
+++ b/tools/binman/ftest.py
@@ -5981,6 +5981,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 testPreLoadMissingKeyName(self):
+        """Test file-based signing with missing key-name property"""
+        entry_args = {
+            'pre-load-key-path': os.path.join(self._binman_dir, 'test'),
+        }
+        with self.assertRaises(ValueError) as exc:
+            self._DoReadFileDtb('security/pre_load_missing_key_name.dts',
+                                entry_args=entry_args)
+        self.assertIn('key-name is required for file-based signing',
+                      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_missing_key_name.dts 
b/tools/binman/test/security/pre_load_missing_key_name.dts
new file mode 100644
index 00000000000..262219fb6ab
--- /dev/null
+++ b/tools/binman/test/security/pre_load_missing_key_name.dts
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0+
+/dts-v1/;
+
+/ {
+       #address-cells = <1>;
+       #size-cells = <1>;
+
+       binman {
+               pre-load {
+                       content = <&image>;
+                       algo-name = "sha256,rsa2048";
+                       header-size = <4096>;
+                       version = <0x11223344>;
+               };
+
+               image: blob-ext {
+                       filename = "refcode.bin";
+               };
+       };
+};
-- 
2.43.0

Reply via email to