Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1 | # Copyright 2017 Linaro Limited |
Tamas Ban | 861835c | 2019-05-13 08:59:38 +0100 | [diff] [blame^] | 2 | # Copyright (c) 2017-2019, Arm Limited. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | """ |
| 17 | Cryptographic key management for imgtool. |
| 18 | """ |
| 19 | |
Gabor Kertesz | 33e9b23 | 2018-09-12 15:38:41 +0200 | [diff] [blame] | 20 | from __future__ import print_function |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 21 | from Crypto.Hash import SHA256 |
| 22 | from Crypto.PublicKey import RSA |
| 23 | from Crypto.Signature import PKCS1_v1_5, PKCS1_PSS |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 24 | import hashlib |
| 25 | from pyasn1.type import namedtype, univ |
| 26 | from pyasn1.codec.der.encoder import encode |
| 27 | |
Tamas Ban | 861835c | 2019-05-13 08:59:38 +0100 | [diff] [blame^] | 28 | # Sizes that bootutil will recognize |
| 29 | RSA_KEY_SIZES = [2048, 3072] |
| 30 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 31 | # By default, we use RSA-PSS (PKCS 2.1). That can be overridden on |
| 32 | # the command line to support the older (less secure) PKCS1.5 |
| 33 | sign_rsa_pss = True |
| 34 | |
| 35 | AUTOGEN_MESSAGE = "/* Autogenerated by imgtool.py, do not edit. */" |
| 36 | |
Tamas Ban | 861835c | 2019-05-13 08:59:38 +0100 | [diff] [blame^] | 37 | class RSAUsageError(Exception): |
| 38 | pass |
| 39 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 40 | class RSAPublicKey(univ.Sequence): |
| 41 | componentType = namedtype.NamedTypes( |
| 42 | namedtype.NamedType('modulus', univ.Integer()), |
| 43 | namedtype.NamedType('publicExponent', univ.Integer())) |
| 44 | |
Tamas Ban | 861835c | 2019-05-13 08:59:38 +0100 | [diff] [blame^] | 45 | class RSAutil(): |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 46 | def __init__(self, key): |
Tamas Ban | 861835c | 2019-05-13 08:59:38 +0100 | [diff] [blame^] | 47 | """Construct an RSA key with the given key data""" |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 48 | self.key = key |
| 49 | |
Tamas Ban | 861835c | 2019-05-13 08:59:38 +0100 | [diff] [blame^] | 50 | def key_size(self): |
| 51 | return self.key.n.bit_length() |
| 52 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 53 | @staticmethod |
Tamas Ban | 861835c | 2019-05-13 08:59:38 +0100 | [diff] [blame^] | 54 | def generate(key_size=2048): |
| 55 | if key_size not in RSA_KEY_SIZES: |
| 56 | raise RSAUsageError("Key size {} is not supported by MCUboot" |
| 57 | .format(key_size)) |
| 58 | return RSAutil(RSA.generate(key_size)) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 59 | |
| 60 | def export_private(self, path): |
| 61 | with open(path, 'wb') as f: |
| 62 | f.write(self.key.exportKey('PEM')) |
| 63 | |
| 64 | def get_public_bytes(self): |
| 65 | node = RSAPublicKey() |
| 66 | node['modulus'] = self.key.n |
| 67 | node['publicExponent'] = self.key.e |
| 68 | return bytearray(encode(node)) |
| 69 | |
| 70 | def emit_c(self): |
| 71 | print(AUTOGEN_MESSAGE) |
| 72 | print("const unsigned char rsa_pub_key[] = {", end='') |
| 73 | encoded = self.get_public_bytes() |
| 74 | for count, b in enumerate(encoded): |
| 75 | if count % 8 == 0: |
| 76 | print("\n\t", end='') |
| 77 | else: |
| 78 | print(" ", end='') |
| 79 | print("0x{:02x},".format(b), end='') |
| 80 | print("\n};") |
| 81 | print("const unsigned int rsa_pub_key_len = {};".format(len(encoded))) |
| 82 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 83 | def sig_type(self): |
| 84 | """Return the type of this signature (as a string)""" |
| 85 | if sign_rsa_pss: |
Tamas Ban | 861835c | 2019-05-13 08:59:38 +0100 | [diff] [blame^] | 86 | return "PKCS1_PSS_RSA{}_SHA256".format(self.key_size()) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 87 | else: |
Tamas Ban | 861835c | 2019-05-13 08:59:38 +0100 | [diff] [blame^] | 88 | return "PKCS15_RSA{}_SHA256".format(self.key_size()) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 89 | |
| 90 | def sig_len(self): |
Tamas Ban | 861835c | 2019-05-13 08:59:38 +0100 | [diff] [blame^] | 91 | return 256 if self.key_size() == 2048 else 384 |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 92 | |
| 93 | def sig_tlv(self): |
Tamas Ban | 861835c | 2019-05-13 08:59:38 +0100 | [diff] [blame^] | 94 | return "RSA2048" if self.key_size() == 2048 else "RSA3072" |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 95 | |
| 96 | def sign(self, payload): |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 97 | converted_payload = bytes(payload) |
| 98 | sha = SHA256.new(converted_payload) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 99 | if sign_rsa_pss: |
| 100 | signer = PKCS1_PSS.new(self.key) |
| 101 | else: |
| 102 | signer = PKCS1_v1_5.new(self.key) |
| 103 | signature = signer.sign(sha) |
| 104 | assert len(signature) == self.sig_len() |
| 105 | return signature |
| 106 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 107 | def load(path): |
| 108 | with open(path, 'rb') as f: |
| 109 | pem = f.read() |
| 110 | try: |
| 111 | key = RSA.importKey(pem) |
Tamas Ban | 861835c | 2019-05-13 08:59:38 +0100 | [diff] [blame^] | 112 | return RSAutil(key) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 113 | except ValueError: |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 114 | raise Exception("Unsupported RSA key file") |