David Brown | 1314bf3 | 2017-12-20 11:10:55 -0700 | [diff] [blame] | 1 | # Copyright 2017 Linaro Limited |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 15 | """ |
| 16 | Image signing and management. |
| 17 | """ |
| 18 | |
| 19 | from . import version as versmod |
| 20 | import hashlib |
| 21 | import struct |
| 22 | |
David Brown | 72e7a51 | 2017-09-01 11:08:23 -0600 | [diff] [blame] | 23 | IMAGE_MAGIC = 0x96f3b83d |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 24 | IMAGE_HEADER_SIZE = 32 |
| 25 | |
| 26 | # Image header flags. |
| 27 | IMAGE_F = { |
| 28 | 'PIC': 0x0000001, |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 29 | 'NON_BOOTABLE': 0x0000010, } |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 30 | |
| 31 | TLV_VALUES = { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 32 | 'KEYHASH': 0x01, |
David Brown | 27648b8 | 2017-08-31 10:40:29 -0600 | [diff] [blame] | 33 | 'SHA256': 0x10, |
| 34 | 'RSA2048': 0x20, |
| 35 | 'ECDSA224': 0x21, |
| 36 | 'ECDSA256': 0x22, } |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 37 | |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 38 | TLV_INFO_SIZE = 4 |
| 39 | TLV_INFO_MAGIC = 0x6907 |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 40 | TLV_HEADER_SIZE = 4 |
| 41 | |
Fabio Utzig | e08f087 | 2017-06-28 18:12:16 -0300 | [diff] [blame] | 42 | # Sizes of the image trailer, depending on flash write size. |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 43 | trailer_sizes = { |
Fabio Utzig | e08f087 | 2017-06-28 18:12:16 -0300 | [diff] [blame] | 44 | write_size: 128 * 3 * write_size + 8 * 2 + 16 |
| 45 | for write_size in [1, 2, 4, 8] |
| 46 | } |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 47 | |
| 48 | boot_magic = bytes([ |
| 49 | 0x77, 0xc2, 0x95, 0xf3, |
| 50 | 0x60, 0xd2, 0xef, 0x7f, |
| 51 | 0x35, 0x52, 0x50, 0x0f, |
| 52 | 0x2c, 0xb6, 0x79, 0x80, ]) |
| 53 | |
| 54 | class TLV(): |
| 55 | def __init__(self): |
| 56 | self.buf = bytearray() |
| 57 | |
| 58 | def add(self, kind, payload): |
| 59 | """Add a TLV record. Kind should be a string found in TLV_VALUES above.""" |
| 60 | buf = struct.pack('<BBH', TLV_VALUES[kind], 0, len(payload)) |
| 61 | self.buf += buf |
| 62 | self.buf += payload |
| 63 | |
| 64 | def get(self): |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 65 | header = struct.pack('<HH', TLV_INFO_MAGIC, TLV_INFO_SIZE + len(self.buf)) |
| 66 | return header + bytes(self.buf) |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 67 | |
| 68 | class Image(): |
| 69 | @classmethod |
David Brown | 2c21f71 | 2017-06-08 10:03:42 -0600 | [diff] [blame] | 70 | def load(cls, path, included_header=False, **kwargs): |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 71 | """Load an image from a given file""" |
| 72 | with open(path, 'rb') as f: |
| 73 | payload = f.read() |
| 74 | obj = cls(**kwargs) |
| 75 | obj.payload = payload |
David Brown | 2c21f71 | 2017-06-08 10:03:42 -0600 | [diff] [blame] | 76 | |
| 77 | # Add the image header if needed. |
| 78 | if not included_header and obj.header_size > 0: |
| 79 | obj.payload = (b'\000' * obj.header_size) + obj.payload |
| 80 | |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 81 | obj.check() |
| 82 | return obj |
| 83 | |
| 84 | def __init__(self, version=None, header_size=IMAGE_HEADER_SIZE, pad=0): |
| 85 | self.version = version or versmod.decode_version("0") |
| 86 | self.header_size = header_size or IMAGE_HEADER_SIZE |
| 87 | self.pad = pad |
| 88 | |
| 89 | def __repr__(self): |
| 90 | return "<Image version={}, header_size={}, pad={}, payloadlen=0x{:x}>".format( |
| 91 | self.version, |
| 92 | self.header_size, |
| 93 | self.pad, |
| 94 | len(self.payload)) |
| 95 | |
| 96 | def save(self, path): |
| 97 | with open(path, 'wb') as f: |
| 98 | f.write(self.payload) |
| 99 | |
| 100 | def check(self): |
| 101 | """Perform some sanity checking of the image.""" |
| 102 | # If there is a header requested, make sure that the image |
| 103 | # starts with all zeros. |
| 104 | if self.header_size > 0: |
| 105 | if any(v != 0 for v in self.payload[0:self.header_size]): |
| 106 | raise Exception("Padding requested, but image does not start with zeros") |
| 107 | |
| 108 | def sign(self, key): |
| 109 | self.add_header(key) |
| 110 | |
| 111 | tlv = TLV() |
| 112 | |
| 113 | # Note that ecdsa wants to do the hashing itself, which means |
| 114 | # we get to hash it twice. |
| 115 | sha = hashlib.sha256() |
| 116 | sha.update(self.payload) |
| 117 | digest = sha.digest() |
| 118 | |
| 119 | tlv.add('SHA256', digest) |
| 120 | |
David Brown | 0f0c6a8 | 2017-06-08 09:26:24 -0600 | [diff] [blame] | 121 | if key is not None: |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 122 | pub = key.get_public_bytes() |
| 123 | sha = hashlib.sha256() |
| 124 | sha.update(pub) |
| 125 | pubbytes = sha.digest() |
| 126 | tlv.add('KEYHASH', pubbytes) |
| 127 | |
David Brown | 47b77c5 | 2017-11-16 15:10:22 -0700 | [diff] [blame^] | 128 | sig = key.sign(bytes(self.payload)) |
David Brown | 0f0c6a8 | 2017-06-08 09:26:24 -0600 | [diff] [blame] | 129 | tlv.add(key.sig_tlv(), sig) |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 130 | |
| 131 | self.payload += tlv.get() |
| 132 | |
| 133 | def add_header(self, key): |
| 134 | """Install the image header. |
| 135 | |
| 136 | The key is needed to know the type of signature, and |
| 137 | approximate the size of the signature.""" |
| 138 | |
David Brown | 0f0c6a8 | 2017-06-08 09:26:24 -0600 | [diff] [blame] | 139 | flags = 0 |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 140 | tlvsz = 0 |
David Brown | 0f0c6a8 | 2017-06-08 09:26:24 -0600 | [diff] [blame] | 141 | if key is not None: |
David Brown | 0f0c6a8 | 2017-06-08 09:26:24 -0600 | [diff] [blame] | 142 | tlvsz += TLV_HEADER_SIZE + key.sig_len() |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 143 | |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 144 | tlvsz += 4 + hashlib.sha256().digest_size |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 145 | tlvsz += 4 + hashlib.sha256().digest_size |
| 146 | |
| 147 | fmt = ('<' + |
| 148 | # type ImageHdr struct { |
| 149 | 'I' + # Magic uint32 |
| 150 | 'H' + # TlvSz uint16 |
| 151 | 'B' + # KeyId uint8 |
| 152 | 'B' + # Pad1 uint8 |
| 153 | 'H' + # HdrSz uint16 |
| 154 | 'H' + # Pad2 uint16 |
| 155 | 'I' + # ImgSz uint32 |
| 156 | 'I' + # Flags uint32 |
| 157 | 'BBHI' + # Vers ImageVersion |
| 158 | 'I' # Pad3 uint32 |
| 159 | ) # } |
| 160 | assert struct.calcsize(fmt) == IMAGE_HEADER_SIZE |
| 161 | header = struct.pack(fmt, |
| 162 | IMAGE_MAGIC, |
| 163 | tlvsz, # TlvSz |
| 164 | 0, # KeyId (TODO: allow other ids) |
| 165 | 0, # Pad1 |
| 166 | self.header_size, |
| 167 | 0, # Pad2 |
| 168 | len(self.payload) - self.header_size, # ImageSz |
| 169 | flags, # Flags |
| 170 | self.version.major, |
| 171 | self.version.minor or 0, |
| 172 | self.version.revision or 0, |
| 173 | self.version.build or 0, |
| 174 | 0) # Pad3 |
| 175 | self.payload = bytearray(self.payload) |
| 176 | self.payload[:len(header)] = header |
| 177 | |
| 178 | def pad_to(self, size, align): |
| 179 | """Pad the image to the given size, with the given flash alignment.""" |
| 180 | tsize = trailer_sizes[align] |
| 181 | padding = size - (len(self.payload) + tsize) |
| 182 | if padding < 0: |
| 183 | msg = "Image size (0x{:x}) + trailer (0x{:x}) exceeds requested size 0x{:x}".format( |
| 184 | len(self.payload), tsize, size) |
| 185 | raise Exception(msg) |
Fabio Utzig | e08f087 | 2017-06-28 18:12:16 -0300 | [diff] [blame] | 186 | pbytes = b'\xff' * padding |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 187 | pbytes += b'\xff' * (tsize - len(boot_magic)) |
Fabio Utzig | e08f087 | 2017-06-28 18:12:16 -0300 | [diff] [blame] | 188 | pbytes += boot_magic |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 189 | self.payload += pbytes |