blob: b5d259a43b56e7024e5b4b7a34e1c01f42182f5d [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
David Brownaac71112020-02-03 16:13:42 -07002 * SPDX-License-Identifier: Apache-2.0
3 *
4 * Copyright (c) 2017-2019 Linaro LTD
5 * Copyright (c) 2016-2019 JUUL Labs
6 * Copyright (c) 2019-2020 Arm Limited
7 *
8 * Original license:
9 *
Christopher Collins92ea77f2016-12-12 15:59:26 -080010 * Licensed to the Apache Software Foundation (ASF) under one
11 * or more contributor license agreements. See the NOTICE file
12 * distributed with this work for additional information
13 * regarding copyright ownership. The ASF licenses this file
14 * to you under the Apache License, Version 2.0 (the
15 * "License"); you may not use this file except in compliance
16 * with the License. You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing,
21 * software distributed under the License is distributed on an
22 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23 * KIND, either express or implied. See the License for the
24 * specific language governing permissions and limitations
25 * under the License.
26 */
27
Christopher Collins92ea77f2016-12-12 15:59:26 -080028#include <stddef.h>
David Vinczec3084132020-02-18 14:50:47 +010029#include <stdint.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080030#include <inttypes.h>
31#include <string.h>
32
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020033#include <flash_map_backend/flash_map_backend.h>
34
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include "bootutil/image.h"
David Browne629bf32017-04-24 13:37:33 -060036#include "bootutil/sha256.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080037#include "bootutil/sign_key.h"
David Vinczec3084132020-02-18 14:50:47 +010038#include "bootutil/security_cnt.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080039
Fabio Utzigba1fbe62017-07-21 14:01:20 -030040#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030041
Fabio Utzigba829042018-09-18 08:29:34 -030042#ifdef MCUBOOT_ENC_IMAGES
43#include "bootutil/enc_key.h"
44#endif
45#if defined(MCUBOOT_SIGN_RSA)
Christopher Collins92ea77f2016-12-12 15:59:26 -080046#include "mbedtls/rsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060047#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -030048#if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080049#include "mbedtls/ecdsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060050#endif
Arvin Farahmandf8240192020-05-05 11:43:52 -040051#if defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_SIGN_RSA) || \
52 defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080053#include "mbedtls/asn1.h"
Arvin Farahmandf8240192020-05-05 11:43:52 -040054#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080055
56#include "bootutil_priv.h"
57
58/*
59 * Compute SHA256 over the image.
60 */
61static int
Fabio Utzig10ee6482019-08-01 12:04:52 -030062bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
63 struct image_header *hdr, const struct flash_area *fap,
64 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *hash_result,
65 uint8_t *seed, int seed_len)
Christopher Collins92ea77f2016-12-12 15:59:26 -080066{
David Browne629bf32017-04-24 13:37:33 -060067 bootutil_sha256_context sha256_ctx;
Christopher Collins92ea77f2016-12-12 15:59:26 -080068 uint32_t blk_sz;
69 uint32_t size;
Fabio Utzig2fc80df2018-12-14 06:47:38 -020070 uint16_t hdr_size;
Christopher Collins92ea77f2016-12-12 15:59:26 -080071 uint32_t off;
72 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -030073 uint32_t blk_off;
Fabio Utzige52c08e2019-09-11 19:32:00 -030074 uint32_t tlv_off;
Christopher Collins92ea77f2016-12-12 15:59:26 -080075
Tamas Banfe031092020-09-10 17:32:39 +020076#if (BOOT_IMAGE_NUMBER == 1) || !defined(MCUBOOT_ENC_IMAGES) || \
77 defined(MCUBOOT_RAM_LOAD)
Fabio Utzig10ee6482019-08-01 12:04:52 -030078 (void)enc_state;
Fabio Utzigb0f04732019-07-31 09:49:19 -030079 (void)image_index;
Fabio Utzigc9621352019-08-08 12:15:51 -030080 (void)hdr_size;
Fabio Utzige52c08e2019-09-11 19:32:00 -030081 (void)blk_off;
82 (void)tlv_off;
Tamas Banfe031092020-09-10 17:32:39 +020083#ifdef MCUBOOT_RAM_LOAD
84 (void)blk_sz;
85 (void)off;
86 (void)rc;
87#endif
Fabio Utzigb0f04732019-07-31 09:49:19 -030088#endif
89
Fabio Utzigbc077932019-08-26 11:16:34 -030090#ifdef MCUBOOT_ENC_IMAGES
91 /* Encrypted images only exist in the secondary slot */
92 if (MUST_DECRYPT(fap, image_index, hdr) &&
93 !boot_enc_valid(enc_state, image_index, fap)) {
94 return -1;
95 }
96#endif
97
David Browne629bf32017-04-24 13:37:33 -060098 bootutil_sha256_init(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -080099
100 /* in some cases (split image) the hash is seeded with data from
101 * the loader image */
Fabio Utzig53986042017-12-12 14:57:07 -0200102 if (seed && (seed_len > 0)) {
David Browne629bf32017-04-24 13:37:33 -0600103 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800104 }
105
David Vinczee32483f2019-06-13 10:46:24 +0200106 /* Hash is computed over image header and image itself. */
Fabio Utzige52c08e2019-09-11 19:32:00 -0300107 size = hdr_size = hdr->ih_hdr_size;
108 size += hdr->ih_img_size;
109 tlv_off = size;
David Vinczee32483f2019-06-13 10:46:24 +0200110
Fabio Utzige52c08e2019-09-11 19:32:00 -0300111 /* If protected TLVs are present they are also hashed. */
112 size += hdr->ih_protect_tlv_size;
David Vinczee32483f2019-06-13 10:46:24 +0200113
Tamas Banfe031092020-09-10 17:32:39 +0200114#ifdef MCUBOOT_RAM_LOAD
115 bootutil_sha256_update(&sha256_ctx,(void*)(hdr->ih_load_addr), size);
116#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800117 for (off = 0; off < size; off += blk_sz) {
118 blk_sz = size - off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300119 if (blk_sz > tmp_buf_sz) {
120 blk_sz = tmp_buf_sz;
121 }
Fabio Utzigc21c2102019-09-10 10:10:42 -0300122#ifdef MCUBOOT_ENC_IMAGES
123 /* The only data that is encrypted in an image is the payload;
124 * both header and TLVs (when protected) are not.
125 */
126 if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) {
127 /* read only the header */
128 blk_sz = hdr_size - off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300129 }
130 if ((off < tlv_off) && ((off + blk_sz) > tlv_off)) {
131 /* read only up to the end of the image payload */
132 blk_sz = tlv_off - off;
Fabio Utzigc21c2102019-09-10 10:10:42 -0300133 }
134#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800135 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
136 if (rc) {
137 return rc;
138 }
Fabio Utzigba829042018-09-18 08:29:34 -0300139#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzigc21c2102019-09-10 10:10:42 -0300140 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzige52c08e2019-09-11 19:32:00 -0300141 /* Only payload is encrypted (area between header and TLVs) */
142 if (off >= hdr_size && off < tlv_off) {
Fabio Utzigc21c2102019-09-10 10:10:42 -0300143 blk_off = (off - hdr_size) & 0xf;
144 boot_encrypt(enc_state, image_index, fap, off - hdr_size,
145 blk_sz, blk_off, tmp_buf);
146 }
Fabio Utzigba829042018-09-18 08:29:34 -0300147 }
148#endif
David Browne629bf32017-04-24 13:37:33 -0600149 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800150 }
Tamas Banfe031092020-09-10 17:32:39 +0200151#endif /* MCUBOOT_RAM_LOAD */
David Browne629bf32017-04-24 13:37:33 -0600152 bootutil_sha256_finish(&sha256_ctx, hash_result);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800153
154 return 0;
155}
156
157/*
David Brown43cda332017-09-01 09:53:23 -0600158 * Currently, we only support being able to verify one type of
159 * signature, because there is a single verification function that we
160 * call. List the type of TLV we are expecting. If we aren't
161 * configured for any signature, don't define this macro.
162 */
Fabio Utzig48764842019-05-10 19:28:24 -0300163#if (defined(MCUBOOT_SIGN_RSA) + \
164 defined(MCUBOOT_SIGN_EC) + \
165 defined(MCUBOOT_SIGN_EC256) + \
166 defined(MCUBOOT_SIGN_ED25519)) > 1
Fabio Utzig3501c012019-05-13 15:07:25 -0700167#error "Only a single signature type is supported!"
168#endif
169
David Brown43cda332017-09-01 09:53:23 -0600170#if defined(MCUBOOT_SIGN_RSA)
Fabio Utzig3501c012019-05-13 15:07:25 -0700171# if MCUBOOT_SIGN_RSA_LEN == 2048
172# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
173# elif MCUBOOT_SIGN_RSA_LEN == 3072
174# define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS
175# else
176# error "Unsupported RSA signature length"
David Brown43cda332017-09-01 09:53:23 -0600177# endif
Fabio Utzig3501c012019-05-13 15:07:25 -0700178# define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8)
179# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) /* 2048 bits */
David Brown43cda332017-09-01 09:53:23 -0600180#elif defined(MCUBOOT_SIGN_EC)
181# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224
Fabio Utzig3501c012019-05-13 15:07:25 -0700182# define SIG_BUF_SIZE 128
David Browna3608262019-12-12 15:35:31 -0700183# define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */
David Brown43cda332017-09-01 09:53:23 -0600184#elif defined(MCUBOOT_SIGN_EC256)
185# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
Fabio Utzig3501c012019-05-13 15:07:25 -0700186# define SIG_BUF_SIZE 128
David Browna3608262019-12-12 15:35:31 -0700187# define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */
Fabio Utzig48764842019-05-10 19:28:24 -0300188#elif defined(MCUBOOT_SIGN_ED25519)
189# define EXPECTED_SIG_TLV IMAGE_TLV_ED25519
190# define SIG_BUF_SIZE 64
191# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE)
Fabio Utzig3501c012019-05-13 15:07:25 -0700192#else
193# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
David Brown43cda332017-09-01 09:53:23 -0600194#endif
195
196#ifdef EXPECTED_SIG_TLV
David Vincze03368b82020-04-01 12:53:53 +0200197#if !defined(MCUBOOT_HW_KEY)
David Brown43cda332017-09-01 09:53:23 -0600198static int
199bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
200{
201 bootutil_sha256_context sha256_ctx;
202 int i;
203 const struct bootutil_key *key;
204 uint8_t hash[32];
205
Fabio Utzig15c14672019-08-09 07:45:20 -0300206 if (keyhash_len > 32) {
207 return -1;
208 }
David Brown43cda332017-09-01 09:53:23 -0600209
210 for (i = 0; i < bootutil_key_cnt; i++) {
211 key = &bootutil_keys[i];
212 bootutil_sha256_init(&sha256_ctx);
213 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
214 bootutil_sha256_finish(&sha256_ctx, hash);
215 if (!memcmp(hash, keyhash, keyhash_len)) {
216 return i;
217 }
218 }
219 return -1;
220}
David Vincze03368b82020-04-01 12:53:53 +0200221#else
222extern unsigned int pub_key_len;
223static int
224bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
225{
226 bootutil_sha256_context sha256_ctx;
227 uint8_t hash[32];
228 uint8_t key_hash[32];
229 size_t key_hash_size = sizeof(key_hash);
230 int rc;
231
232 bootutil_sha256_init(&sha256_ctx);
233 bootutil_sha256_update(&sha256_ctx, key, key_len);
234 bootutil_sha256_finish(&sha256_ctx, hash);
235
236 rc = boot_retrieve_public_key_hash(image_index, key_hash, &key_hash_size);
237 if (rc) {
238 return rc;
239 }
240
241 if (!memcmp(hash, key_hash, key_hash_size)) {
242 bootutil_keys[0].key = key;
243 pub_key_len = key_len;
244 return 0;
245 }
246 return -1;
247}
248#endif /* !MCUBOOT_HW_KEY */
David Brown43cda332017-09-01 09:53:23 -0600249#endif
250
David Vinczec3084132020-02-18 14:50:47 +0100251#ifdef MCUBOOT_HW_ROLLBACK_PROT
252/**
253 * Reads the value of an image's security counter.
254 *
255 * @param hdr Pointer to the image header structure.
256 * @param fap Pointer to a description structure of the image's
257 * flash area.
258 * @param security_cnt Pointer to store the security counter value.
259 *
260 * @return 0 on success; nonzero on failure.
261 */
262int32_t
263bootutil_get_img_security_cnt(struct image_header *hdr,
264 const struct flash_area *fap,
265 uint32_t *img_security_cnt)
266{
267 struct image_tlv_iter it;
268 uint32_t off;
269 uint16_t len;
270 int32_t rc;
271
272 if ((hdr == NULL) ||
273 (fap == NULL) ||
274 (img_security_cnt == NULL)) {
275 /* Invalid parameter. */
276 return BOOT_EBADARGS;
277 }
278
279 /* The security counter TLV is in the protected part of the TLV area. */
280 if (hdr->ih_protect_tlv_size == 0) {
281 return BOOT_EBADIMAGE;
282 }
283
284 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_SEC_CNT, true);
285 if (rc) {
286 return rc;
287 }
288
289 /* Traverse through the protected TLV area to find
290 * the security counter TLV.
291 */
292
293 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
294 if (rc != 0) {
295 /* Security counter TLV has not been found. */
296 return -1;
297 }
298
299 if (len != sizeof(*img_security_cnt)) {
300 /* Security counter is not valid. */
301 return BOOT_EBADIMAGE;
302 }
303
Tamas Banfe031092020-09-10 17:32:39 +0200304 rc = LOAD_IMAGE_DATA(hdr, fap, off, img_security_cnt, len);
David Vinczec3084132020-02-18 14:50:47 +0100305 if (rc != 0) {
306 return BOOT_EFLASH;
307 }
308
309 return 0;
310}
311#endif /* MCUBOOT_HW_ROLLBACK_PROT */
312
David Brown43cda332017-09-01 09:53:23 -0600313/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800314 * Verify the integrity of the image.
315 * Return non-zero if image could not be validated/does not validate.
316 */
317int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300318bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
319 struct image_header *hdr, const struct flash_area *fap,
320 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *seed,
321 int seed_len, uint8_t *out_hash)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800322{
323 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300324 uint16_t len;
David Brownd13318a2019-12-04 17:28:40 -0700325 uint16_t type;
David Brown43cda332017-09-01 09:53:23 -0600326 int sha256_valid = 0;
327#ifdef EXPECTED_SIG_TLV
328 int valid_signature = 0;
329 int key_id = -1;
David Vincze03368b82020-04-01 12:53:53 +0200330#ifdef MCUBOOT_HW_KEY
331 /* Few extra bytes for encoding and for public exponent. */
332 uint8_t key_buf[SIG_BUF_SIZE + 24];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800333#endif
David Vincze03368b82020-04-01 12:53:53 +0200334#endif /* EXPECTED_SIG_TLV */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300335 struct image_tlv_iter it;
Fabio Utzig3501c012019-05-13 15:07:25 -0700336 uint8_t buf[SIG_BUF_SIZE];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800337 uint8_t hash[32];
338 int rc;
David Vinczec3084132020-02-18 14:50:47 +0100339#ifdef MCUBOOT_HW_ROLLBACK_PROT
340 uint32_t security_cnt = UINT32_MAX;
341 uint32_t img_security_cnt = 0;
342 int32_t security_counter_valid = 0;
343#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800344
Fabio Utzig10ee6482019-08-01 12:04:52 -0300345 rc = bootutil_img_hash(enc_state, image_index, hdr, fap, tmp_buf,
346 tmp_buf_sz, hash, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800347 if (rc) {
348 return rc;
349 }
350
351 if (out_hash) {
352 memcpy(out_hash, hash, 32);
353 }
354
Fabio Utzig61fd8882019-09-14 20:00:20 -0300355 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false);
David Brownf5b33d82017-09-01 10:58:27 -0600356 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300357 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600358 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800359
David Brown43cda332017-09-01 09:53:23 -0600360 /*
361 * Traverse through all of the TLVs, performing any checks we know
362 * and are able to do.
363 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300364 while (true) {
365 rc = bootutil_tlv_iter_next(&it, &off, &len, &type);
366 if (rc < 0) {
367 return -1;
368 } else if (rc > 0) {
369 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800370 }
David Brown43cda332017-09-01 09:53:23 -0600371
Fabio Utzig61fd8882019-09-14 20:00:20 -0300372 if (type == IMAGE_TLV_SHA256) {
David Brown43cda332017-09-01 09:53:23 -0600373 /*
374 * Verify the SHA256 image hash. This must always be
375 * present.
376 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300377 if (len != sizeof(hash)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800378 return -1;
379 }
Tamas Banfe031092020-09-10 17:32:39 +0200380 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, sizeof(hash));
David Brown43cda332017-09-01 09:53:23 -0600381 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300382 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800383 }
David Brown43cda332017-09-01 09:53:23 -0600384 if (memcmp(hash, buf, sizeof(hash))) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300385 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800386 }
David Brown43cda332017-09-01 09:53:23 -0600387
388 sha256_valid = 1;
389#ifdef EXPECTED_SIG_TLV
David Vincze03368b82020-04-01 12:53:53 +0200390#ifndef MCUBOOT_HW_KEY
Fabio Utzig61fd8882019-09-14 20:00:20 -0300391 } else if (type == IMAGE_TLV_KEYHASH) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800392 /*
David Brown43cda332017-09-01 09:53:23 -0600393 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800394 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300395 if (len > 32) {
David Brown43cda332017-09-01 09:53:23 -0600396 return -1;
397 }
Tamas Banfe031092020-09-10 17:32:39 +0200398 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600399 if (rc) {
400 return rc;
401 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300402 key_id = bootutil_find_key(buf, len);
David Brown43cda332017-09-01 09:53:23 -0600403 /*
404 * The key may not be found, which is acceptable. There
405 * can be multiple signatures, each preceded by a key.
406 */
David Vincze03368b82020-04-01 12:53:53 +0200407#else
408 } else if (type == IMAGE_TLV_PUBKEY) {
409 /*
410 * Determine which key we should be checking.
411 */
412 if (len > sizeof(key_buf)) {
413 return -1;
414 }
Tamas Banfe031092020-09-10 17:32:39 +0200415 rc = LOAD_IMAGE_DATA(hdr, fap, off, key_buf, len);
David Vincze03368b82020-04-01 12:53:53 +0200416 if (rc) {
417 return rc;
418 }
419 key_id = bootutil_find_key(image_index, key_buf, len);
420 /*
421 * The key may not be found, which is acceptable. There
422 * can be multiple signatures, each preceded by a key.
423 */
424#endif /* !MCUBOOT_HW_KEY */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300425 } else if (type == EXPECTED_SIG_TLV) {
David Brown43cda332017-09-01 09:53:23 -0600426 /* Ignore this signature if it is out of bounds. */
427 if (key_id < 0 || key_id >= bootutil_key_cnt) {
428 key_id = -1;
429 continue;
430 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300431 if (!EXPECTED_SIG_LEN(len) || len > sizeof(buf)) {
David Brown43cda332017-09-01 09:53:23 -0600432 return -1;
433 }
Tamas Banfe031092020-09-10 17:32:39 +0200434 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600435 if (rc) {
436 return -1;
437 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300438 rc = bootutil_verify_sig(hash, sizeof(hash), buf, len, key_id);
David Brown43cda332017-09-01 09:53:23 -0600439 if (rc == 0) {
440 valid_signature = 1;
441 }
442 key_id = -1;
David Vinczec3084132020-02-18 14:50:47 +0100443#endif /* EXPECTED_SIG_TLV */
444#ifdef MCUBOOT_HW_ROLLBACK_PROT
445 } else if (type == IMAGE_TLV_SEC_CNT) {
446 /*
447 * Verify the image's security counter.
448 * This must always be present.
449 */
450 if (len != sizeof(img_security_cnt)) {
451 /* Security counter is not valid. */
452 return -1;
453 }
454
Tamas Banfe031092020-09-10 17:32:39 +0200455 rc = LOAD_IMAGE_DATA(hdr, fap, off, &img_security_cnt, len);
David Vinczec3084132020-02-18 14:50:47 +0100456 if (rc) {
457 return rc;
458 }
459
460 rc = boot_nv_security_counter_get(image_index, &security_cnt);
461 if (rc) {
462 return rc;
463 }
464
465 /* Compare the new image's security counter value against the
466 * stored security counter value.
467 */
468 if (img_security_cnt < security_cnt) {
469 /* The image's security counter is not accepted. */
470 return -1;
471 }
472
473 /* The image's security counter has been successfully verified. */
474 security_counter_valid = 1;
475#endif /* MCUBOOT_HW_ROLLBACK_PROT */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800476 }
477 }
David Brown43cda332017-09-01 09:53:23 -0600478
479 if (!sha256_valid) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800480 return -1;
David Brown43cda332017-09-01 09:53:23 -0600481#ifdef EXPECTED_SIG_TLV
David Vinczec3084132020-02-18 14:50:47 +0100482 } else if (!valid_signature) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800483 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800484#endif
David Vinczec3084132020-02-18 14:50:47 +0100485#ifdef MCUBOOT_HW_ROLLBACK_PROT
486 } else if (!security_counter_valid) {
487 return -1;
488#endif
489 }
David Brown43cda332017-09-01 09:53:23 -0600490
Christopher Collins92ea77f2016-12-12 15:59:26 -0800491 return 0;
492}