blob: 4ddbfc646ea9d024440095be7466990e63e11f86 [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
13class RSAUsageError(Exception):
14 pass
15
16class RSA2048Public(KeyClass):
17 """The public key can only do a few operations"""
18 def __init__(self, key):
19 self.key = key
20
21 def shortname(self):
22 return "rsa"
23
24 def _unsupported(self, name):
25 raise RSAUsageError("Operation {} requires private key".format(name))
26
27 def _get_public(self):
28 return self.key
29
30 def get_public_bytes(self):
31 # The key embedded into MCUboot is in PKCS1 format.
32 return self._get_public().public_bytes(
33 encoding=serialization.Encoding.DER,
34 format=serialization.PublicFormat.PKCS1)
35
36 def export_private(self, path, passwd=None):
37 self._unsupported('export_private')
38
39 def export_public(self, path):
40 """Write the public key to the given file."""
41 pem = self._get_public().public_bytes(
42 encoding=serialization.Encoding.PEM,
43 format=serialization.PublicFormat.SubjectPublicKeyInfo)
44 with open(path, 'wb') as f:
45 f.write(pem)
46
47 def sig_type(self):
48 return "PKCS1_PSS_RSA2048_SHA256"
49
50 def sig_tlv(self):
51 return "RSA2048"
52
David Brown1d5bea12017-11-16 15:11:10 -070053 def sig_len(self):
54 return 256
55
David Brown5e7c6dd2017-11-16 14:47:16 -070056class RSA2048(RSA2048Public):
57 """
58 Wrapper around an 2048-bit RSA key, with imgtool support.
59 """
60
61 def __init__(self, key):
62 """The key should be a private key from cryptography"""
63 self.key = key
64
65 @staticmethod
66 def generate():
67 pk = rsa.generate_private_key(
68 public_exponent=65537,
69 key_size=2048,
70 backend=default_backend())
71 return RSA2048(pk)
72
73 def _get_public(self):
74 return self.key.public_key()
75
76 def export_private(self, path, passwd=None):
77 """Write the private key to the given file, protecting it with the optional password."""
78 if passwd is None:
79 enc = serialization.NoEncryption()
80 else:
81 enc = serialization.BestAvailableEncryption(passwd)
82 pem = self.key.private_bytes(
83 encoding=serialization.Encoding.PEM,
84 format=serialization.PrivateFormat.PKCS8,
85 encryption_algorithm=enc)
86 with open(path, 'wb') as f:
87 f.write(pem)
88
89 def sign(self, payload):
David Brown20462a72017-11-21 14:28:51 -070090 # The verification code only allows the salt length to be the
91 # same as the hash length, 32.
David Brown5e7c6dd2017-11-16 14:47:16 -070092 return self.key.sign(
93 data=payload,
David Brown20462a72017-11-21 14:28:51 -070094 padding=PSS(mgf=MGF1(SHA256()), salt_length=32),
David Brown5e7c6dd2017-11-16 14:47:16 -070095 algorithm=SHA256())