blob: 0f9a9052d04cf56646df59122854b0e64e8ef880 [file] [log] [blame]
David Brown5e7c6dd2017-11-16 14:47:16 -07001"""
2RSA Key management
3"""
4
5from cryptography.hazmat.backends import default_backend
6from cryptography.hazmat.primitives import serialization
7from cryptography.hazmat.primitives.asymmetric import rsa
8from cryptography.hazmat.primitives.asymmetric.padding import PSS, MGF1
9from cryptography.hazmat.primitives.hashes import SHA256
10
11from .general import KeyClass
12
Fabio Utzig19fd79a2019-05-08 18:20:39 -030013
14# Sizes that bootutil will recognize
15RSA_KEY_SIZES = [2048, 3072]
16
17
David Brown5e7c6dd2017-11-16 14:47:16 -070018class RSAUsageError(Exception):
19 pass
20
Fabio Utzig19fd79a2019-05-08 18:20:39 -030021
22class RSAPublic(KeyClass):
David Brown5e7c6dd2017-11-16 14:47:16 -070023 """The public key can only do a few operations"""
24 def __init__(self, key):
25 self.key = key
26
Fabio Utzig19fd79a2019-05-08 18:20:39 -030027 def key_size(self):
28 return self.key.key_size
29
David Brown5e7c6dd2017-11-16 14:47:16 -070030 def shortname(self):
31 return "rsa"
32
33 def _unsupported(self, name):
34 raise RSAUsageError("Operation {} requires private key".format(name))
35
36 def _get_public(self):
37 return self.key
38
39 def get_public_bytes(self):
40 # The key embedded into MCUboot is in PKCS1 format.
41 return self._get_public().public_bytes(
42 encoding=serialization.Encoding.DER,
43 format=serialization.PublicFormat.PKCS1)
44
45 def export_private(self, path, passwd=None):
46 self._unsupported('export_private')
47
48 def export_public(self, path):
49 """Write the public key to the given file."""
50 pem = self._get_public().public_bytes(
51 encoding=serialization.Encoding.PEM,
52 format=serialization.PublicFormat.SubjectPublicKeyInfo)
53 with open(path, 'wb') as f:
54 f.write(pem)
55
56 def sig_type(self):
Fabio Utzig19fd79a2019-05-08 18:20:39 -030057 return "PKCS1_PSS_RSA{}_SHA256".format(self.key_size())
David Brown5e7c6dd2017-11-16 14:47:16 -070058
59 def sig_tlv(self):
Fabio Utzig19fd79a2019-05-08 18:20:39 -030060 return"RSA{}".format(self.key_size())
David Brown5e7c6dd2017-11-16 14:47:16 -070061
David Brown1d5bea12017-11-16 15:11:10 -070062 def sig_len(self):
Fabio Utzig19fd79a2019-05-08 18:20:39 -030063 return self.key_size() / 8
David Brown1d5bea12017-11-16 15:11:10 -070064
Fabio Utzig4a5477a2019-05-27 15:45:08 -030065 def verify(self, signature, payload):
66 k = self.key
67 if isinstance(self.key, rsa.RSAPrivateKey):
68 k = self.key.public_key()
69 return k.verify(signature=signature, data=payload,
70 padding=PSS(mgf=MGF1(SHA256()), salt_length=32),
71 algorithm=SHA256())
72
Fabio Utzig19fd79a2019-05-08 18:20:39 -030073
74class RSA(RSAPublic):
David Brown5e7c6dd2017-11-16 14:47:16 -070075 """
Fabio Utzig19fd79a2019-05-08 18:20:39 -030076 Wrapper around an RSA key, with imgtool support.
David Brown5e7c6dd2017-11-16 14:47:16 -070077 """
78
79 def __init__(self, key):
80 """The key should be a private key from cryptography"""
81 self.key = key
82
83 @staticmethod
Fabio Utzig19fd79a2019-05-08 18:20:39 -030084 def generate(key_size=2048):
85 if key_size not in RSA_KEY_SIZES:
86 raise RSAUsageError("Key size {} is not supported by MCUboot"
87 .format(key_size))
David Brown5e7c6dd2017-11-16 14:47:16 -070088 pk = rsa.generate_private_key(
89 public_exponent=65537,
Fabio Utzig19fd79a2019-05-08 18:20:39 -030090 key_size=key_size,
David Brown5e7c6dd2017-11-16 14:47:16 -070091 backend=default_backend())
Fabio Utzig19fd79a2019-05-08 18:20:39 -030092 return RSA(pk)
David Brown5e7c6dd2017-11-16 14:47:16 -070093
94 def _get_public(self):
95 return self.key.public_key()
96
97 def export_private(self, path, passwd=None):
Fabio Utzig19fd79a2019-05-08 18:20:39 -030098 """Write the private key to the given file, protecting it with the
99 optional password."""
David Brown5e7c6dd2017-11-16 14:47:16 -0700100 if passwd is None:
101 enc = serialization.NoEncryption()
102 else:
103 enc = serialization.BestAvailableEncryption(passwd)
104 pem = self.key.private_bytes(
105 encoding=serialization.Encoding.PEM,
106 format=serialization.PrivateFormat.PKCS8,
107 encryption_algorithm=enc)
108 with open(path, 'wb') as f:
109 f.write(pem)
110
111 def sign(self, payload):
David Brown20462a72017-11-21 14:28:51 -0700112 # The verification code only allows the salt length to be the
113 # same as the hash length, 32.
David Brown5e7c6dd2017-11-16 14:47:16 -0700114 return self.key.sign(
115 data=payload,
David Brown20462a72017-11-21 14:28:51 -0700116 padding=PSS(mgf=MGF1(SHA256()), salt_length=32),
David Brown5e7c6dd2017-11-16 14:47:16 -0700117 algorithm=SHA256())