David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 1 | """ |
| 2 | RSA Key management |
| 3 | """ |
| 4 | |
| 5 | from cryptography.hazmat.backends import default_backend |
| 6 | from cryptography.hazmat.primitives import serialization |
| 7 | from cryptography.hazmat.primitives.asymmetric import rsa |
| 8 | from cryptography.hazmat.primitives.asymmetric.padding import PSS, MGF1 |
| 9 | from cryptography.hazmat.primitives.hashes import SHA256 |
| 10 | |
| 11 | from .general import KeyClass |
| 12 | |
| 13 | class RSAUsageError(Exception): |
| 14 | pass |
| 15 | |
| 16 | class 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 Brown | 1d5bea1 | 2017-11-16 15:11:10 -0700 | [diff] [blame^] | 53 | def sig_len(self): |
| 54 | return 256 |
| 55 | |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 56 | class 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): |
| 90 | return self.key.sign( |
| 91 | data=payload, |
| 92 | padding=PSS(mgf=MGF1(SHA256()), salt_length=PSS.MAX_LENGTH), |
| 93 | algorithm=SHA256()) |