blob: 6ca7b109c92f1345a3038e0b447a2b3fde8e1d81 [file] [log] [blame]
Fabio Utzig8101d1f2019-05-09 15:03:22 -03001"""
Fabio Utzig67c59fa2020-04-02 13:09:15 -03002ED25519 key management
Fabio Utzig8101d1f2019-05-09 15:03:22 -03003"""
4
David Brown79c4fcf2021-01-26 15:04:05 -07005# SPDX-License-Identifier: Apache-2.0
6
Fabio Utzig8101d1f2019-05-09 15:03:22 -03007from cryptography.hazmat.backends import default_backend
8from cryptography.hazmat.primitives import serialization
9from cryptography.hazmat.primitives.asymmetric import ed25519
10
11from .general import KeyClass
12
13
14class Ed25519UsageError(Exception):
15 pass
16
17
18class Ed25519Public(KeyClass):
19 def __init__(self, key):
20 self.key = key
21
22 def shortname(self):
23 return "ed25519"
24
25 def _unsupported(self, name):
26 raise Ed25519UsageError("Operation {} requires private key".format(name))
27
28 def _get_public(self):
29 return self.key
30
31 def get_public_bytes(self):
32 # The key is embedded into MBUboot in "SubjectPublicKeyInfo" format
33 return self._get_public().public_bytes(
34 encoding=serialization.Encoding.DER,
35 format=serialization.PublicFormat.SubjectPublicKeyInfo)
36
Fabio Utzig8f289ba2023-01-09 21:01:55 -030037 def get_private_bytes(self, minimal, format):
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020038 self._unsupported('get_private_bytes')
39
Fabio Utzig8101d1f2019-05-09 15:03:22 -030040 def export_private(self, path, passwd=None):
41 self._unsupported('export_private')
42
43 def export_public(self, path):
44 """Write the public key to the given file."""
45 pem = self._get_public().public_bytes(
46 encoding=serialization.Encoding.PEM,
47 format=serialization.PublicFormat.SubjectPublicKeyInfo)
48 with open(path, 'wb') as f:
49 f.write(pem)
50
51 def sig_type(self):
52 return "ED25519"
53
54 def sig_tlv(self):
55 return "ED25519"
56
57 def sig_len(self):
58 return 64
59
60
61class Ed25519(Ed25519Public):
62 """
Fabio Utzig67c59fa2020-04-02 13:09:15 -030063 Wrapper around an ED25519 private key.
Fabio Utzig8101d1f2019-05-09 15:03:22 -030064 """
65
66 def __init__(self, key):
67 """key should be an instance of EllipticCurvePrivateKey"""
68 self.key = key
69
70 @staticmethod
71 def generate():
72 pk = ed25519.Ed25519PrivateKey.generate()
73 return Ed25519(pk)
74
75 def _get_public(self):
76 return self.key.public_key()
77
Fabio Utzig8f289ba2023-01-09 21:01:55 -030078 def get_private_bytes(self, minimal, format):
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +020079 raise Ed25519UsageError("Operation not supported with {} keys".format(
80 self.shortname()))
81
Fabio Utzig8101d1f2019-05-09 15:03:22 -030082 def export_private(self, path, passwd=None):
83 """
84 Write the private key to the given file, protecting it with the
85 optional password.
86 """
87 if passwd is None:
88 enc = serialization.NoEncryption()
89 else:
90 enc = serialization.BestAvailableEncryption(passwd)
91 pem = self.key.private_bytes(
92 encoding=serialization.Encoding.PEM,
93 format=serialization.PrivateFormat.PKCS8,
94 encryption_algorithm=enc)
95 with open(path, 'wb') as f:
96 f.write(pem)
97
98 def sign_digest(self, digest):
99 """Return the actual signature"""
100 return self.key.sign(data=digest)
101
102 def verify_digest(self, signature, digest):
103 """Verify that signature is valid for given digest"""
104 k = self.key
105 if isinstance(self.key, ed25519.Ed25519PrivateKey):
106 k = self.key.public_key()
107 return k.verify(signature=signature, data=digest)