blob: a31cb3fbf570fadeb5880ce40ad33ac9230a4ad5 [file] [log] [blame]
Fabio Utzig960b4c52020-04-02 13:07:12 -03001"""
2X25519 key management
3"""
4
David Brown79c4fcf2021-01-26 15:04:05 -07005# SPDX-License-Identifier: Apache-2.0
6
Fabio Utzig960b4c52020-04-02 13:07:12 -03007from cryptography.hazmat.backends import default_backend
8from cryptography.hazmat.primitives import serialization
9from cryptography.hazmat.primitives.asymmetric import x25519
10
11from .general import KeyClass
12
13
14class X25519UsageError(Exception):
15 pass
16
17
18class X25519Public(KeyClass):
19 def __init__(self, key):
20 self.key = key
21
22 def shortname(self):
23 return "x25519"
24
25 def _unsupported(self, name):
26 raise X25519UsageError("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
37 def get_private_bytes(self, minimal):
38 self._unsupported('get_private_bytes')
39
40 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 "X25519"
53
54 def sig_tlv(self):
55 return "X25519"
56
57 def sig_len(self):
58 return 32
59
60
61class X25519(X25519Public):
62 """
63 Wrapper around an X25519 private key.
64 """
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 = x25519.X25519PrivateKey.generate()
73 return X25519(pk)
74
75 def _get_public(self):
76 return self.key.public_key()
77
78 def get_private_bytes(self, minimal):
Fabio Utzig4facd1b2020-04-02 13:17:38 -030079 return self.key.private_bytes(
80 encoding=serialization.Encoding.DER,
81 format=serialization.PrivateFormat.PKCS8,
82 encryption_algorithm=serialization.NoEncryption())
Fabio Utzig960b4c52020-04-02 13:07:12 -030083
84 def export_private(self, path, passwd=None):
85 """
86 Write the private key to the given file, protecting it with the
87 optional password.
88 """
89 if passwd is None:
90 enc = serialization.NoEncryption()
91 else:
92 enc = serialization.BestAvailableEncryption(passwd)
93 pem = self.key.private_bytes(
94 encoding=serialization.Encoding.PEM,
95 format=serialization.PrivateFormat.PKCS8,
96 encryption_algorithm=enc)
97 with open(path, 'wb') as f:
98 f.write(pem)
99
100 def sign_digest(self, digest):
101 """Return the actual signature"""
102 return self.key.sign(data=digest)
103
104 def verify_digest(self, signature, digest):
105 """Verify that signature is valid for given digest"""
106 k = self.key
107 if isinstance(self.key, x25519.X25519PrivateKey):
108 k = self.key.public_key()
109 return k.verify(signature=signature, data=digest)