blob: 94af064ef17a5663e076babe9d7d58b28998e016 [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 Utzig19fd79a2019-05-08 18:20:39 -030065
66class RSA(RSAPublic):
David Brown5e7c6dd2017-11-16 14:47:16 -070067 """
Fabio Utzig19fd79a2019-05-08 18:20:39 -030068 Wrapper around an RSA key, with imgtool support.
David Brown5e7c6dd2017-11-16 14:47:16 -070069 """
70
71 def __init__(self, key):
72 """The key should be a private key from cryptography"""
73 self.key = key
74
75 @staticmethod
Fabio Utzig19fd79a2019-05-08 18:20:39 -030076 def generate(key_size=2048):
77 if key_size not in RSA_KEY_SIZES:
78 raise RSAUsageError("Key size {} is not supported by MCUboot"
79 .format(key_size))
David Brown5e7c6dd2017-11-16 14:47:16 -070080 pk = rsa.generate_private_key(
81 public_exponent=65537,
Fabio Utzig19fd79a2019-05-08 18:20:39 -030082 key_size=key_size,
David Brown5e7c6dd2017-11-16 14:47:16 -070083 backend=default_backend())
Fabio Utzig19fd79a2019-05-08 18:20:39 -030084 return RSA(pk)
David Brown5e7c6dd2017-11-16 14:47:16 -070085
86 def _get_public(self):
87 return self.key.public_key()
88
89 def export_private(self, path, passwd=None):
Fabio Utzig19fd79a2019-05-08 18:20:39 -030090 """Write the private key to the given file, protecting it with the
91 optional password."""
David Brown5e7c6dd2017-11-16 14:47:16 -070092 if passwd is None:
93 enc = serialization.NoEncryption()
94 else:
95 enc = serialization.BestAvailableEncryption(passwd)
96 pem = self.key.private_bytes(
97 encoding=serialization.Encoding.PEM,
98 format=serialization.PrivateFormat.PKCS8,
99 encryption_algorithm=enc)
100 with open(path, 'wb') as f:
101 f.write(pem)
102
103 def sign(self, payload):
David Brown20462a72017-11-21 14:28:51 -0700104 # The verification code only allows the salt length to be the
105 # same as the hash length, 32.
David Brown5e7c6dd2017-11-16 14:47:16 -0700106 return self.key.sign(
107 data=payload,
David Brown20462a72017-11-21 14:28:51 -0700108 padding=PSS(mgf=MGF1(SHA256()), salt_length=32),
David Brown5e7c6dd2017-11-16 14:47:16 -0700109 algorithm=SHA256())