David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 1 | """ |
| 2 | Image signing and management. |
| 3 | """ |
| 4 | |
| 5 | from . import version as versmod |
| 6 | import hashlib |
| 7 | import struct |
| 8 | |
| 9 | IMAGE_MAGIC = 0x96f3b83c |
| 10 | IMAGE_HEADER_SIZE = 32 |
| 11 | |
| 12 | # Image header flags. |
| 13 | IMAGE_F = { |
| 14 | 'PIC': 0x0000001, |
| 15 | 'SHA256': 0x0000002, |
| 16 | 'PKCS15_RSA2048_SHA256': 0x0000004, |
| 17 | 'ECDSA224_SHA256': 0x0000008, |
| 18 | 'NON_BOOTABLE': 0x0000010, |
David Brown | 07916c3 | 2017-05-31 13:38:31 -0600 | [diff] [blame^] | 19 | 'ECDSA256_SHA256': 0x0000020, |
| 20 | 'PKCS1_PSS_RSA2048_SHA256': 0x0000040, } |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 21 | |
| 22 | TLV_VALUES = { |
| 23 | 'SHA256': 1, |
| 24 | 'RSA2048': 2, |
| 25 | 'ECDSA224': 3, |
| 26 | 'ECDSA256': 4, } |
| 27 | |
| 28 | TLV_HEADER_SIZE = 4 |
| 29 | |
| 30 | # Sizes of the image trailer, depending on image alignment. |
| 31 | trailer_sizes = { |
| 32 | 1: 402, |
| 33 | 2: 788, |
| 34 | 4: 1560, |
| 35 | 8: 3104, } |
| 36 | |
| 37 | boot_magic = bytes([ |
| 38 | 0x77, 0xc2, 0x95, 0xf3, |
| 39 | 0x60, 0xd2, 0xef, 0x7f, |
| 40 | 0x35, 0x52, 0x50, 0x0f, |
| 41 | 0x2c, 0xb6, 0x79, 0x80, ]) |
| 42 | |
| 43 | class TLV(): |
| 44 | def __init__(self): |
| 45 | self.buf = bytearray() |
| 46 | |
| 47 | def add(self, kind, payload): |
| 48 | """Add a TLV record. Kind should be a string found in TLV_VALUES above.""" |
| 49 | buf = struct.pack('<BBH', TLV_VALUES[kind], 0, len(payload)) |
| 50 | self.buf += buf |
| 51 | self.buf += payload |
| 52 | |
| 53 | def get(self): |
| 54 | return bytes(self.buf) |
| 55 | |
| 56 | class Image(): |
| 57 | @classmethod |
| 58 | def load(cls, path, **kwargs): |
| 59 | """Load an image from a given file""" |
| 60 | with open(path, 'rb') as f: |
| 61 | payload = f.read() |
| 62 | obj = cls(**kwargs) |
| 63 | obj.payload = payload |
| 64 | obj.check() |
| 65 | return obj |
| 66 | |
| 67 | def __init__(self, version=None, header_size=IMAGE_HEADER_SIZE, pad=0): |
| 68 | self.version = version or versmod.decode_version("0") |
| 69 | self.header_size = header_size or IMAGE_HEADER_SIZE |
| 70 | self.pad = pad |
| 71 | |
| 72 | def __repr__(self): |
| 73 | return "<Image version={}, header_size={}, pad={}, payloadlen=0x{:x}>".format( |
| 74 | self.version, |
| 75 | self.header_size, |
| 76 | self.pad, |
| 77 | len(self.payload)) |
| 78 | |
| 79 | def save(self, path): |
| 80 | with open(path, 'wb') as f: |
| 81 | f.write(self.payload) |
| 82 | |
| 83 | def check(self): |
| 84 | """Perform some sanity checking of the image.""" |
| 85 | # If there is a header requested, make sure that the image |
| 86 | # starts with all zeros. |
| 87 | if self.header_size > 0: |
| 88 | if any(v != 0 for v in self.payload[0:self.header_size]): |
| 89 | raise Exception("Padding requested, but image does not start with zeros") |
| 90 | |
| 91 | def sign(self, key): |
| 92 | self.add_header(key) |
| 93 | |
| 94 | tlv = TLV() |
| 95 | |
| 96 | # Note that ecdsa wants to do the hashing itself, which means |
| 97 | # we get to hash it twice. |
| 98 | sha = hashlib.sha256() |
| 99 | sha.update(self.payload) |
| 100 | digest = sha.digest() |
| 101 | |
| 102 | tlv.add('SHA256', digest) |
| 103 | |
| 104 | sig = key.sign(self.payload) |
| 105 | tlv.add(key.sig_tlv(), sig) |
| 106 | |
| 107 | self.payload += tlv.get() |
| 108 | |
| 109 | def add_header(self, key): |
| 110 | """Install the image header. |
| 111 | |
| 112 | The key is needed to know the type of signature, and |
| 113 | approximate the size of the signature.""" |
| 114 | |
| 115 | flags = IMAGE_F[key.sig_type()] |
| 116 | tlvsz = 0 |
| 117 | tlvsz += TLV_HEADER_SIZE + key.sig_len() |
| 118 | |
| 119 | flags |= IMAGE_F['SHA256'] |
| 120 | tlvsz += 4 + hashlib.sha256().digest_size |
| 121 | |
| 122 | fmt = ('<' + |
| 123 | # type ImageHdr struct { |
| 124 | 'I' + # Magic uint32 |
| 125 | 'H' + # TlvSz uint16 |
| 126 | 'B' + # KeyId uint8 |
| 127 | 'B' + # Pad1 uint8 |
| 128 | 'H' + # HdrSz uint16 |
| 129 | 'H' + # Pad2 uint16 |
| 130 | 'I' + # ImgSz uint32 |
| 131 | 'I' + # Flags uint32 |
| 132 | 'BBHI' + # Vers ImageVersion |
| 133 | 'I' # Pad3 uint32 |
| 134 | ) # } |
| 135 | assert struct.calcsize(fmt) == IMAGE_HEADER_SIZE |
| 136 | header = struct.pack(fmt, |
| 137 | IMAGE_MAGIC, |
| 138 | tlvsz, # TlvSz |
| 139 | 0, # KeyId (TODO: allow other ids) |
| 140 | 0, # Pad1 |
| 141 | self.header_size, |
| 142 | 0, # Pad2 |
| 143 | len(self.payload) - self.header_size, # ImageSz |
| 144 | flags, # Flags |
| 145 | self.version.major, |
| 146 | self.version.minor or 0, |
| 147 | self.version.revision or 0, |
| 148 | self.version.build or 0, |
| 149 | 0) # Pad3 |
| 150 | self.payload = bytearray(self.payload) |
| 151 | self.payload[:len(header)] = header |
| 152 | |
| 153 | def pad_to(self, size, align): |
| 154 | """Pad the image to the given size, with the given flash alignment.""" |
| 155 | tsize = trailer_sizes[align] |
| 156 | padding = size - (len(self.payload) + tsize) |
| 157 | if padding < 0: |
| 158 | msg = "Image size (0x{:x}) + trailer (0x{:x}) exceeds requested size 0x{:x}".format( |
| 159 | len(self.payload), tsize, size) |
| 160 | raise Exception(msg) |
| 161 | pbytes = b'\xff' * padding |
| 162 | pbytes += boot_magic |
| 163 | pbytes += b'\xff' * (tsize - len(boot_magic)) |
| 164 | self.payload += pbytes |