From 9560d773712e33c637f9e0afe49d1a4b097e7ce6 Mon Sep 17 00:00:00 2001 From: Fabio Utzig Date: Thu, 2 Apr 2020 13:44:30 -0300 Subject: [PATCH] imgtool: keys: fix tests Fix tests that were broken due to changes in key interfaces. Signed-off-by: Fabio Utzig --- scripts/imgtool/keys/ecdsa_test.py | 8 ++++---- scripts/imgtool/keys/ed25519_test.py | 21 ++++++++++++++------- scripts/imgtool/keys/rsa_test.py | 8 ++++---- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/scripts/imgtool/keys/ecdsa_test.py b/scripts/imgtool/keys/ecdsa_test.py index 8cb3220f..31fe0859 100644 --- a/scripts/imgtool/keys/ecdsa_test.py +++ b/scripts/imgtool/keys/ecdsa_test.py @@ -51,12 +51,12 @@ class EcKeyGeneration(unittest.TestCase): k = ECDSA256P1.generate() ccode = io.StringIO() - k.emit_c(ccode) + k.emit_c_public(ccode) self.assertIn("ecdsa_pub_key", ccode.getvalue()) self.assertIn("ecdsa_pub_key_len", ccode.getvalue()) rustcode = io.StringIO() - k.emit_rust(rustcode) + k.emit_rust_public(rustcode) self.assertIn("ECDSA_PUB_KEY", rustcode.getvalue()) def test_emit_pub(self): @@ -68,12 +68,12 @@ class EcKeyGeneration(unittest.TestCase): k2 = load(pubname) ccode = io.StringIO() - k2.emit_c(ccode) + k2.emit_c_public(ccode) self.assertIn("ecdsa_pub_key", ccode.getvalue()) self.assertIn("ecdsa_pub_key_len", ccode.getvalue()) rustcode = io.StringIO() - k2.emit_rust(rustcode) + k2.emit_rust_public(rustcode) self.assertIn("ECDSA_PUB_KEY", rustcode.getvalue()) def test_sig(self): diff --git a/scripts/imgtool/keys/ed25519_test.py b/scripts/imgtool/keys/ed25519_test.py index 6ed3656a..31f43fe9 100644 --- a/scripts/imgtool/keys/ed25519_test.py +++ b/scripts/imgtool/keys/ed25519_test.py @@ -2,6 +2,7 @@ Tests for ECDSA keys """ +import hashlib import io import os.path import sys @@ -51,12 +52,12 @@ class Ed25519KeyGeneration(unittest.TestCase): k = Ed25519.generate() ccode = io.StringIO() - k.emit_c(ccode) + k.emit_c_public(ccode) self.assertIn("ed25519_pub_key", ccode.getvalue()) self.assertIn("ed25519_pub_key_len", ccode.getvalue()) rustcode = io.StringIO() - k.emit_rust(rustcode) + k.emit_rust_public(rustcode) self.assertIn("ED25519_PUB_KEY", rustcode.getvalue()) def test_emit_pub(self): @@ -68,28 +69,34 @@ class Ed25519KeyGeneration(unittest.TestCase): k2 = load(pubname) ccode = io.StringIO() - k2.emit_c(ccode) + k2.emit_c_public(ccode) self.assertIn("ed25519_pub_key", ccode.getvalue()) self.assertIn("ed25519_pub_key_len", ccode.getvalue()) rustcode = io.StringIO() - k2.emit_rust(rustcode) + k2.emit_rust_public(rustcode) self.assertIn("ED25519_PUB_KEY", rustcode.getvalue()) def test_sig(self): k = Ed25519.generate() buf = b'This is the message' - sig = k.raw_sign(buf) + sha = hashlib.sha256() + sha.update(buf) + digest = sha.digest() + sig = k.sign_digest(digest) # The code doesn't have any verification, so verify this # manually. - k.key.public_key().verify(signature=sig, data=buf) + k.key.public_key().verify(signature=sig, data=digest) # Modify the message to make sure the signature fails. + sha = hashlib.sha256() + sha.update(b'This is thE message') + new_digest = sha.digest() self.assertRaises(InvalidSignature, k.key.public_key().verify, signature=sig, - data=b'This is thE message') + data=new_digest) if __name__ == '__main__': diff --git a/scripts/imgtool/keys/rsa_test.py b/scripts/imgtool/keys/rsa_test.py index b01635d5..b0afa835 100644 --- a/scripts/imgtool/keys/rsa_test.py +++ b/scripts/imgtool/keys/rsa_test.py @@ -62,12 +62,12 @@ class KeyGeneration(unittest.TestCase): k = RSA.generate(key_size=key_size) ccode = io.StringIO() - k.emit_c(ccode) + k.emit_c_public(ccode) self.assertIn("rsa_pub_key", ccode.getvalue()) self.assertIn("rsa_pub_key_len", ccode.getvalue()) rustcode = io.StringIO() - k.emit_rust(rustcode) + k.emit_rust_public(rustcode) self.assertIn("RSA_PUB_KEY", rustcode.getvalue()) def test_emit_pub(self): @@ -80,12 +80,12 @@ class KeyGeneration(unittest.TestCase): k2 = load(pubname) ccode = io.StringIO() - k2.emit_c(ccode) + k2.emit_c_public(ccode) self.assertIn("rsa_pub_key", ccode.getvalue()) self.assertIn("rsa_pub_key_len", ccode.getvalue()) rustcode = io.StringIO() - k2.emit_rust(rustcode) + k2.emit_rust_public(rustcode) self.assertIn("RSA_PUB_KEY", rustcode.getvalue()) def test_sig(self):