blob: ef6ebd0d2c00d5a0ec7ea6e7ab250f4787210396 [file] [log] [blame]
Fabio Utzig8101d1f2019-05-09 15:03:22 -03001"""
2Tests for ECDSA keys
3"""
4
David Brown79c4fcf2021-01-26 15:04:05 -07005# SPDX-License-Identifier: Apache-2.0
6
Fabio Utzig9560d772020-04-02 13:44:30 -03007import hashlib
Fabio Utzig8101d1f2019-05-09 15:03:22 -03008import io
9import os.path
10import sys
11import tempfile
12import unittest
13
14from cryptography.exceptions import InvalidSignature
15from cryptography.hazmat.primitives.asymmetric import ed25519
16
17sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
18
19from imgtool.keys import load, Ed25519, Ed25519UsageError
20
21
22class Ed25519KeyGeneration(unittest.TestCase):
23
24 def setUp(self):
25 self.test_dir = tempfile.TemporaryDirectory()
26
27 def tname(self, base):
28 return os.path.join(self.test_dir.name, base)
29
30 def tearDown(self):
31 self.test_dir.cleanup()
32
33 def test_keygen(self):
34 name1 = self.tname("keygen.pem")
35 k = Ed25519.generate()
36 k.export_private(name1, b'secret')
37
38 self.assertIsNone(load(name1))
39
40 k2 = load(name1, b'secret')
41
42 pubname = self.tname('keygen-pub.pem')
43 k2.export_public(pubname)
44 pk2 = load(pubname)
45
46 # We should be able to export the public key from the loaded
47 # public key, but not the private key.
48 pk2.export_public(self.tname('keygen-pub2.pem'))
49 self.assertRaises(Ed25519UsageError,
50 pk2.export_private, self.tname('keygen-priv2.pem'))
51
52 def test_emit(self):
53 """Basic sanity check on the code emitters."""
54 k = Ed25519.generate()
55
56 ccode = io.StringIO()
Fabio Utzig9560d772020-04-02 13:44:30 -030057 k.emit_c_public(ccode)
Fabio Utzig8101d1f2019-05-09 15:03:22 -030058 self.assertIn("ed25519_pub_key", ccode.getvalue())
59 self.assertIn("ed25519_pub_key_len", ccode.getvalue())
60
61 rustcode = io.StringIO()
Fabio Utzig9560d772020-04-02 13:44:30 -030062 k.emit_rust_public(rustcode)
Fabio Utzig8101d1f2019-05-09 15:03:22 -030063 self.assertIn("ED25519_PUB_KEY", rustcode.getvalue())
64
65 def test_emit_pub(self):
66 """Basic sanity check on the code emitters."""
67 pubname = self.tname("public.pem")
68 k = Ed25519.generate()
69 k.export_public(pubname)
70
71 k2 = load(pubname)
72
73 ccode = io.StringIO()
Fabio Utzig9560d772020-04-02 13:44:30 -030074 k2.emit_c_public(ccode)
Fabio Utzig8101d1f2019-05-09 15:03:22 -030075 self.assertIn("ed25519_pub_key", ccode.getvalue())
76 self.assertIn("ed25519_pub_key_len", ccode.getvalue())
77
78 rustcode = io.StringIO()
Fabio Utzig9560d772020-04-02 13:44:30 -030079 k2.emit_rust_public(rustcode)
Fabio Utzig8101d1f2019-05-09 15:03:22 -030080 self.assertIn("ED25519_PUB_KEY", rustcode.getvalue())
81
82 def test_sig(self):
83 k = Ed25519.generate()
84 buf = b'This is the message'
Fabio Utzig9560d772020-04-02 13:44:30 -030085 sha = hashlib.sha256()
86 sha.update(buf)
87 digest = sha.digest()
88 sig = k.sign_digest(digest)
Fabio Utzig8101d1f2019-05-09 15:03:22 -030089
90 # The code doesn't have any verification, so verify this
91 # manually.
Fabio Utzig9560d772020-04-02 13:44:30 -030092 k.key.public_key().verify(signature=sig, data=digest)
Fabio Utzig8101d1f2019-05-09 15:03:22 -030093
94 # Modify the message to make sure the signature fails.
Fabio Utzig9560d772020-04-02 13:44:30 -030095 sha = hashlib.sha256()
96 sha.update(b'This is thE message')
97 new_digest = sha.digest()
Fabio Utzig8101d1f2019-05-09 15:03:22 -030098 self.assertRaises(InvalidSignature,
99 k.key.public_key().verify,
100 signature=sig,
Fabio Utzig9560d772020-04-02 13:44:30 -0300101 data=new_digest)
Fabio Utzig8101d1f2019-05-09 15:03:22 -0300102
103
104if __name__ == '__main__':
105 unittest.main()