blob: 9983d4fd5cc3feb240d003ff02820c51eba29b0d [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
David Vinczee32483f2019-06-13 10:46:24 +020020/*
21 * Modifications are Copyright (c) 2019 Arm Limited.
22 */
23
Christopher Collins92ea77f2016-12-12 15:59:26 -080024#include <stddef.h>
25#include <inttypes.h>
26#include <string.h>
27
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020028#include <flash_map_backend/flash_map_backend.h>
29
Christopher Collins92ea77f2016-12-12 15:59:26 -080030#include "bootutil/image.h"
David Browne629bf32017-04-24 13:37:33 -060031#include "bootutil/sha256.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080032#include "bootutil/sign_key.h"
33
Fabio Utzigba1fbe62017-07-21 14:01:20 -030034#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030035
Fabio Utzigba829042018-09-18 08:29:34 -030036#ifdef MCUBOOT_ENC_IMAGES
37#include "bootutil/enc_key.h"
38#endif
39#if defined(MCUBOOT_SIGN_RSA)
Christopher Collins92ea77f2016-12-12 15:59:26 -080040#include "mbedtls/rsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060041#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -030042#if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080043#include "mbedtls/ecdsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060044#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080045#include "mbedtls/asn1.h"
46
47#include "bootutil_priv.h"
48
49/*
50 * Compute SHA256 over the image.
51 */
52static int
Fabio Utzig10ee6482019-08-01 12:04:52 -030053bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
54 struct image_header *hdr, const struct flash_area *fap,
55 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *hash_result,
56 uint8_t *seed, int seed_len)
Christopher Collins92ea77f2016-12-12 15:59:26 -080057{
David Browne629bf32017-04-24 13:37:33 -060058 bootutil_sha256_context sha256_ctx;
Christopher Collins92ea77f2016-12-12 15:59:26 -080059 uint32_t blk_sz;
60 uint32_t size;
Fabio Utzig2fc80df2018-12-14 06:47:38 -020061 uint16_t hdr_size;
Christopher Collins92ea77f2016-12-12 15:59:26 -080062 uint32_t off;
63 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -030064#ifdef MCUBOOT_ENC_IMAGES
65 uint32_t blk_off;
Fabio Utzigba829042018-09-18 08:29:34 -030066#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080067
Fabio Utzigb0f04732019-07-31 09:49:19 -030068#if (BOOT_IMAGE_NUMBER == 1) || !defined(MCUBOOT_ENC_IMAGES)
Fabio Utzig10ee6482019-08-01 12:04:52 -030069 (void)enc_state;
Fabio Utzigb0f04732019-07-31 09:49:19 -030070 (void)image_index;
Fabio Utzigc9621352019-08-08 12:15:51 -030071 (void)hdr_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -030072#endif
73
Fabio Utzigbc077932019-08-26 11:16:34 -030074#ifdef MCUBOOT_ENC_IMAGES
75 /* Encrypted images only exist in the secondary slot */
76 if (MUST_DECRYPT(fap, image_index, hdr) &&
77 !boot_enc_valid(enc_state, image_index, fap)) {
78 return -1;
79 }
80#endif
81
David Browne629bf32017-04-24 13:37:33 -060082 bootutil_sha256_init(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -080083
84 /* in some cases (split image) the hash is seeded with data from
85 * the loader image */
Fabio Utzig53986042017-12-12 14:57:07 -020086 if (seed && (seed_len > 0)) {
David Browne629bf32017-04-24 13:37:33 -060087 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -080088 }
89
David Vinczee32483f2019-06-13 10:46:24 +020090 /* Hash is computed over image header and image itself. */
Fabio Utzig2fc80df2018-12-14 06:47:38 -020091 hdr_size = hdr->ih_hdr_size;
Fabio Utzigc9621352019-08-08 12:15:51 -030092 size = BOOT_TLV_OFF(hdr);
David Vinczee32483f2019-06-13 10:46:24 +020093
94#if (MCUBOOT_IMAGE_NUMBER > 1)
95 /* If dependency TLVs are present then the TLV info header and the
96 * dependency TLVs are also protected and have to be included in the hash
97 * calculation.
98 */
99 if (hdr->ih_protect_tlv_size != 0) {
100 size += hdr->ih_protect_tlv_size;
101 }
102#endif
103
Christopher Collins92ea77f2016-12-12 15:59:26 -0800104 for (off = 0; off < size; off += blk_sz) {
105 blk_sz = size - off;
106 if (blk_sz > tmp_buf_sz) {
107 blk_sz = tmp_buf_sz;
108 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200109#ifdef MCUBOOT_ENC_IMAGES
110 /* Avoid reading header data together with other image data
111 * because the header is not encrypted, so maintain correct
112 * bounds when sending encrypted data to decrypt routine.
113 */
114 if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) {
115 blk_sz = hdr_size - off;
116 }
117#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800118 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
119 if (rc) {
120 return rc;
121 }
Fabio Utzigba829042018-09-18 08:29:34 -0300122#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzigbc077932019-08-26 11:16:34 -0300123 if (MUST_DECRYPT(fap, image_index, hdr) && off >= hdr_size) {
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200124 blk_off = (off - hdr_size) & 0xf;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300125 boot_encrypt(enc_state, image_index, fap, off - hdr_size, blk_sz,
126 blk_off, tmp_buf);
Fabio Utzigba829042018-09-18 08:29:34 -0300127 }
128#endif
David Browne629bf32017-04-24 13:37:33 -0600129 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800130 }
David Browne629bf32017-04-24 13:37:33 -0600131 bootutil_sha256_finish(&sha256_ctx, hash_result);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800132
133 return 0;
134}
135
136/*
David Brown43cda332017-09-01 09:53:23 -0600137 * Currently, we only support being able to verify one type of
138 * signature, because there is a single verification function that we
139 * call. List the type of TLV we are expecting. If we aren't
140 * configured for any signature, don't define this macro.
141 */
Fabio Utzig48764842019-05-10 19:28:24 -0300142#if (defined(MCUBOOT_SIGN_RSA) + \
143 defined(MCUBOOT_SIGN_EC) + \
144 defined(MCUBOOT_SIGN_EC256) + \
145 defined(MCUBOOT_SIGN_ED25519)) > 1
Fabio Utzig3501c012019-05-13 15:07:25 -0700146#error "Only a single signature type is supported!"
147#endif
148
David Brown43cda332017-09-01 09:53:23 -0600149#if defined(MCUBOOT_SIGN_RSA)
Fabio Utzig3501c012019-05-13 15:07:25 -0700150# if MCUBOOT_SIGN_RSA_LEN == 2048
151# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
152# elif MCUBOOT_SIGN_RSA_LEN == 3072
153# define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS
154# else
155# error "Unsupported RSA signature length"
David Brown43cda332017-09-01 09:53:23 -0600156# endif
Fabio Utzig3501c012019-05-13 15:07:25 -0700157# define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8)
158# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) /* 2048 bits */
David Brown43cda332017-09-01 09:53:23 -0600159#elif defined(MCUBOOT_SIGN_EC)
160# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224
Fabio Utzig3501c012019-05-13 15:07:25 -0700161# define SIG_BUF_SIZE 128
David Brown43cda332017-09-01 09:53:23 -0600162# define EXPECTED_SIG_LEN(x) ((x) >= 64) /* oids + 2 * 28 bytes */
David Brown43cda332017-09-01 09:53:23 -0600163#elif defined(MCUBOOT_SIGN_EC256)
164# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
Fabio Utzig3501c012019-05-13 15:07:25 -0700165# define SIG_BUF_SIZE 128
David Brown43cda332017-09-01 09:53:23 -0600166# define EXPECTED_SIG_LEN(x) ((x) >= 72) /* oids + 2 * 32 bytes */
Fabio Utzig48764842019-05-10 19:28:24 -0300167#elif defined(MCUBOOT_SIGN_ED25519)
168# define EXPECTED_SIG_TLV IMAGE_TLV_ED25519
169# define SIG_BUF_SIZE 64
170# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE)
Fabio Utzig3501c012019-05-13 15:07:25 -0700171#else
172# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
David Brown43cda332017-09-01 09:53:23 -0600173#endif
174
175#ifdef EXPECTED_SIG_TLV
176static int
177bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
178{
179 bootutil_sha256_context sha256_ctx;
180 int i;
181 const struct bootutil_key *key;
182 uint8_t hash[32];
183
Fabio Utzig15c14672019-08-09 07:45:20 -0300184 if (keyhash_len > 32) {
185 return -1;
186 }
David Brown43cda332017-09-01 09:53:23 -0600187
188 for (i = 0; i < bootutil_key_cnt; i++) {
189 key = &bootutil_keys[i];
190 bootutil_sha256_init(&sha256_ctx);
191 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
192 bootutil_sha256_finish(&sha256_ctx, hash);
193 if (!memcmp(hash, keyhash, keyhash_len)) {
194 return i;
195 }
196 }
197 return -1;
198}
199#endif
200
201/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800202 * Verify the integrity of the image.
203 * Return non-zero if image could not be validated/does not validate.
204 */
205int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300206bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
207 struct image_header *hdr, const struct flash_area *fap,
208 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *seed,
209 int seed_len, uint8_t *out_hash)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800210{
211 uint32_t off;
David Brown3eaa2a12017-09-01 11:19:03 -0600212 uint32_t end;
David Brown43cda332017-09-01 09:53:23 -0600213 int sha256_valid = 0;
214#ifdef EXPECTED_SIG_TLV
215 int valid_signature = 0;
216 int key_id = -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800217#endif
218 struct image_tlv tlv;
Fabio Utzig3501c012019-05-13 15:07:25 -0700219 uint8_t buf[SIG_BUF_SIZE];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800220 uint8_t hash[32];
221 int rc;
222
Fabio Utzig10ee6482019-08-01 12:04:52 -0300223 rc = bootutil_img_hash(enc_state, image_index, hdr, fap, tmp_buf,
224 tmp_buf_sz, hash, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800225 if (rc) {
226 return rc;
227 }
228
229 if (out_hash) {
230 memcpy(out_hash, hash, 32);
231 }
232
Fabio Utzig233af7d2019-08-26 12:06:16 -0300233 rc = boot_find_tlv_offs(hdr, fap, &off, &end);
David Brownf5b33d82017-09-01 10:58:27 -0600234 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300235 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600236 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800237
David Brown43cda332017-09-01 09:53:23 -0600238 /*
239 * Traverse through all of the TLVs, performing any checks we know
240 * and are able to do.
241 */
David Brown3eaa2a12017-09-01 11:19:03 -0600242 for (; off < end; off += sizeof(tlv) + tlv.it_len) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800243 rc = flash_area_read(fap, off, &tlv, sizeof tlv);
244 if (rc) {
245 return rc;
246 }
David Brown43cda332017-09-01 09:53:23 -0600247
Christopher Collins92ea77f2016-12-12 15:59:26 -0800248 if (tlv.it_type == IMAGE_TLV_SHA256) {
David Brown43cda332017-09-01 09:53:23 -0600249 /*
250 * Verify the SHA256 image hash. This must always be
251 * present.
252 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800253 if (tlv.it_len != sizeof(hash)) {
254 return -1;
255 }
David Brown43cda332017-09-01 09:53:23 -0600256 rc = flash_area_read(fap, off + sizeof(tlv), buf, sizeof hash);
257 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300258 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800259 }
David Brown43cda332017-09-01 09:53:23 -0600260 if (memcmp(hash, buf, sizeof(hash))) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300261 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800262 }
David Brown43cda332017-09-01 09:53:23 -0600263
264 sha256_valid = 1;
265#ifdef EXPECTED_SIG_TLV
266 } else if (tlv.it_type == IMAGE_TLV_KEYHASH) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800267 /*
David Brown43cda332017-09-01 09:53:23 -0600268 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800269 */
David Brown43cda332017-09-01 09:53:23 -0600270 if (tlv.it_len > 32) {
271 return -1;
272 }
273 rc = flash_area_read(fap, off + sizeof tlv, buf, tlv.it_len);
274 if (rc) {
275 return rc;
276 }
277 key_id = bootutil_find_key(buf, tlv.it_len);
278 /*
279 * The key may not be found, which is acceptable. There
280 * can be multiple signatures, each preceded by a key.
281 */
282 } else if (tlv.it_type == EXPECTED_SIG_TLV) {
283 /* Ignore this signature if it is out of bounds. */
284 if (key_id < 0 || key_id >= bootutil_key_cnt) {
285 key_id = -1;
286 continue;
287 }
288 if (!EXPECTED_SIG_LEN(tlv.it_len) || tlv.it_len > sizeof(buf)) {
289 return -1;
290 }
291 rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len);
292 if (rc) {
293 return -1;
294 }
295 rc = bootutil_verify_sig(hash, sizeof(hash), buf, tlv.it_len, key_id);
296 if (rc == 0) {
297 valid_signature = 1;
298 }
299 key_id = -1;
300#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800301 }
302 }
David Brown43cda332017-09-01 09:53:23 -0600303
304 if (!sha256_valid) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800305 return -1;
306 }
307
David Brown43cda332017-09-01 09:53:23 -0600308#ifdef EXPECTED_SIG_TLV
309 if (!valid_signature) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800310 return -1;
311 }
312#endif
David Brown43cda332017-09-01 09:53:23 -0600313
Christopher Collins92ea77f2016-12-12 15:59:26 -0800314 return 0;
315}