blob: 07e0e8585f1d7fcd87cfcd4251675c1a591c44d9 [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
Roland Mikhel206b9142023-02-23 15:28:52 +01006 * Copyright (c) 2019-2023 Arm Limited
David Brownaac71112020-02-03 16:13:42 -07007 *
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"
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +090036#include "bootutil/crypto/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"
Raef Colese8fe6cf2020-05-26 13:07:40 +010039#include "bootutil/fault_injection_hardening.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080040
Fabio Utzigba1fbe62017-07-21 14:01:20 -030041#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030042
Fabio Utzigba829042018-09-18 08:29:34 -030043#ifdef MCUBOOT_ENC_IMAGES
44#include "bootutil/enc_key.h"
45#endif
46#if defined(MCUBOOT_SIGN_RSA)
Christopher Collins92ea77f2016-12-12 15:59:26 -080047#include "mbedtls/rsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060048#endif
Roland Mikhel206b9142023-02-23 15:28:52 +010049#if defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080050#include "mbedtls/ecdsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060051#endif
Arvin Farahmandf8240192020-05-05 11:43:52 -040052#if defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_SIGN_RSA) || \
Roland Mikhel206b9142023-02-23 15:28:52 +010053 defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080054#include "mbedtls/asn1.h"
Arvin Farahmandf8240192020-05-05 11:43:52 -040055#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080056
57#include "bootutil_priv.h"
58
59/*
60 * Compute SHA256 over the image.
61 */
62static int
Fabio Utzig10ee6482019-08-01 12:04:52 -030063bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
64 struct image_header *hdr, const struct flash_area *fap,
65 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *hash_result,
66 uint8_t *seed, int seed_len)
Christopher Collins92ea77f2016-12-12 15:59:26 -080067{
David Browne629bf32017-04-24 13:37:33 -060068 bootutil_sha256_context sha256_ctx;
Christopher Collins92ea77f2016-12-12 15:59:26 -080069 uint32_t blk_sz;
70 uint32_t size;
Fabio Utzig2fc80df2018-12-14 06:47:38 -020071 uint16_t hdr_size;
Christopher Collins92ea77f2016-12-12 15:59:26 -080072 uint32_t off;
73 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -030074 uint32_t blk_off;
Fabio Utzige52c08e2019-09-11 19:32:00 -030075 uint32_t tlv_off;
Christopher Collins92ea77f2016-12-12 15:59:26 -080076
Tamas Banfe031092020-09-10 17:32:39 +020077#if (BOOT_IMAGE_NUMBER == 1) || !defined(MCUBOOT_ENC_IMAGES) || \
78 defined(MCUBOOT_RAM_LOAD)
Fabio Utzig10ee6482019-08-01 12:04:52 -030079 (void)enc_state;
Fabio Utzigb0f04732019-07-31 09:49:19 -030080 (void)image_index;
Fabio Utzigc9621352019-08-08 12:15:51 -030081 (void)hdr_size;
Fabio Utzige52c08e2019-09-11 19:32:00 -030082 (void)blk_off;
83 (void)tlv_off;
Tamas Banfe031092020-09-10 17:32:39 +020084#ifdef MCUBOOT_RAM_LOAD
85 (void)blk_sz;
86 (void)off;
87 (void)rc;
David Brown122f9e72021-05-21 15:58:56 -060088 (void)fap;
89 (void)tmp_buf;
90 (void)tmp_buf_sz;
Tamas Banfe031092020-09-10 17:32:39 +020091#endif
Fabio Utzigb0f04732019-07-31 09:49:19 -030092#endif
93
Fabio Utzigbc077932019-08-26 11:16:34 -030094#ifdef MCUBOOT_ENC_IMAGES
95 /* Encrypted images only exist in the secondary slot */
96 if (MUST_DECRYPT(fap, image_index, hdr) &&
97 !boot_enc_valid(enc_state, image_index, fap)) {
98 return -1;
99 }
100#endif
101
David Browne629bf32017-04-24 13:37:33 -0600102 bootutil_sha256_init(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800103
104 /* in some cases (split image) the hash is seeded with data from
105 * the loader image */
Fabio Utzig53986042017-12-12 14:57:07 -0200106 if (seed && (seed_len > 0)) {
David Browne629bf32017-04-24 13:37:33 -0600107 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800108 }
109
David Vinczee32483f2019-06-13 10:46:24 +0200110 /* Hash is computed over image header and image itself. */
Fabio Utzige52c08e2019-09-11 19:32:00 -0300111 size = hdr_size = hdr->ih_hdr_size;
112 size += hdr->ih_img_size;
113 tlv_off = size;
David Vinczee32483f2019-06-13 10:46:24 +0200114
Fabio Utzige52c08e2019-09-11 19:32:00 -0300115 /* If protected TLVs are present they are also hashed. */
116 size += hdr->ih_protect_tlv_size;
David Vinczee32483f2019-06-13 10:46:24 +0200117
Tamas Banfe031092020-09-10 17:32:39 +0200118#ifdef MCUBOOT_RAM_LOAD
David Brown9bd7f902021-05-26 16:31:14 -0600119 bootutil_sha256_update(&sha256_ctx,
120 (void*)(IMAGE_RAM_BASE + hdr->ih_load_addr),
121 size);
Tamas Banfe031092020-09-10 17:32:39 +0200122#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800123 for (off = 0; off < size; off += blk_sz) {
124 blk_sz = size - off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300125 if (blk_sz > tmp_buf_sz) {
126 blk_sz = tmp_buf_sz;
127 }
Fabio Utzigc21c2102019-09-10 10:10:42 -0300128#ifdef MCUBOOT_ENC_IMAGES
129 /* The only data that is encrypted in an image is the payload;
130 * both header and TLVs (when protected) are not.
131 */
132 if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) {
133 /* read only the header */
134 blk_sz = hdr_size - off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300135 }
136 if ((off < tlv_off) && ((off + blk_sz) > tlv_off)) {
137 /* read only up to the end of the image payload */
138 blk_sz = tlv_off - off;
Fabio Utzigc21c2102019-09-10 10:10:42 -0300139 }
140#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800141 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
142 if (rc) {
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900143 bootutil_sha256_drop(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800144 return rc;
145 }
Fabio Utzigba829042018-09-18 08:29:34 -0300146#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzigc21c2102019-09-10 10:10:42 -0300147 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzige52c08e2019-09-11 19:32:00 -0300148 /* Only payload is encrypted (area between header and TLVs) */
149 if (off >= hdr_size && off < tlv_off) {
Fabio Utzigc21c2102019-09-10 10:10:42 -0300150 blk_off = (off - hdr_size) & 0xf;
151 boot_encrypt(enc_state, image_index, fap, off - hdr_size,
152 blk_sz, blk_off, tmp_buf);
153 }
Fabio Utzigba829042018-09-18 08:29:34 -0300154 }
155#endif
David Browne629bf32017-04-24 13:37:33 -0600156 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800157 }
Tamas Banfe031092020-09-10 17:32:39 +0200158#endif /* MCUBOOT_RAM_LOAD */
David Browne629bf32017-04-24 13:37:33 -0600159 bootutil_sha256_finish(&sha256_ctx, hash_result);
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900160 bootutil_sha256_drop(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800161
162 return 0;
163}
164
165/*
David Brown43cda332017-09-01 09:53:23 -0600166 * Currently, we only support being able to verify one type of
167 * signature, because there is a single verification function that we
168 * call. List the type of TLV we are expecting. If we aren't
169 * configured for any signature, don't define this macro.
170 */
Fabio Utzig48764842019-05-10 19:28:24 -0300171#if (defined(MCUBOOT_SIGN_RSA) + \
Fabio Utzig48764842019-05-10 19:28:24 -0300172 defined(MCUBOOT_SIGN_EC256) + \
173 defined(MCUBOOT_SIGN_ED25519)) > 1
Fabio Utzig3501c012019-05-13 15:07:25 -0700174#error "Only a single signature type is supported!"
175#endif
176
David Brown43cda332017-09-01 09:53:23 -0600177#if defined(MCUBOOT_SIGN_RSA)
Fabio Utzig3501c012019-05-13 15:07:25 -0700178# if MCUBOOT_SIGN_RSA_LEN == 2048
179# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
180# elif MCUBOOT_SIGN_RSA_LEN == 3072
181# define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS
182# else
183# error "Unsupported RSA signature length"
David Brown43cda332017-09-01 09:53:23 -0600184# endif
Fabio Utzig3501c012019-05-13 15:07:25 -0700185# define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8)
186# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) /* 2048 bits */
David Brown43cda332017-09-01 09:53:23 -0600187#elif defined(MCUBOOT_SIGN_EC256)
188# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
Fabio Utzig3501c012019-05-13 15:07:25 -0700189# define SIG_BUF_SIZE 128
David Browna3608262019-12-12 15:35:31 -0700190# define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */
Fabio Utzig48764842019-05-10 19:28:24 -0300191#elif defined(MCUBOOT_SIGN_ED25519)
192# define EXPECTED_SIG_TLV IMAGE_TLV_ED25519
193# define SIG_BUF_SIZE 64
194# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE)
Fabio Utzig3501c012019-05-13 15:07:25 -0700195#else
196# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
David Brown43cda332017-09-01 09:53:23 -0600197#endif
198
199#ifdef EXPECTED_SIG_TLV
David Vincze03368b82020-04-01 12:53:53 +0200200#if !defined(MCUBOOT_HW_KEY)
David Brown43cda332017-09-01 09:53:23 -0600201static int
202bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
203{
204 bootutil_sha256_context sha256_ctx;
205 int i;
206 const struct bootutil_key *key;
207 uint8_t hash[32];
208
Fabio Utzig15c14672019-08-09 07:45:20 -0300209 if (keyhash_len > 32) {
210 return -1;
211 }
David Brown43cda332017-09-01 09:53:23 -0600212
213 for (i = 0; i < bootutil_key_cnt; i++) {
214 key = &bootutil_keys[i];
215 bootutil_sha256_init(&sha256_ctx);
216 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
217 bootutil_sha256_finish(&sha256_ctx, hash);
218 if (!memcmp(hash, keyhash, keyhash_len)) {
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900219 bootutil_sha256_drop(&sha256_ctx);
David Brown43cda332017-09-01 09:53:23 -0600220 return i;
221 }
222 }
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900223 bootutil_sha256_drop(&sha256_ctx);
David Brown43cda332017-09-01 09:53:23 -0600224 return -1;
225}
David Vincze03368b82020-04-01 12:53:53 +0200226#else
227extern unsigned int pub_key_len;
228static int
229bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
230{
231 bootutil_sha256_context sha256_ctx;
232 uint8_t hash[32];
233 uint8_t key_hash[32];
234 size_t key_hash_size = sizeof(key_hash);
235 int rc;
Michael Grand5047f032022-11-24 16:49:56 +0100236 FIH_DECLARE(fih_rc, FIH_FAILURE);
David Vincze03368b82020-04-01 12:53:53 +0200237
238 bootutil_sha256_init(&sha256_ctx);
239 bootutil_sha256_update(&sha256_ctx, key, key_len);
240 bootutil_sha256_finish(&sha256_ctx, hash);
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900241 bootutil_sha256_drop(&sha256_ctx);
David Vincze03368b82020-04-01 12:53:53 +0200242
243 rc = boot_retrieve_public_key_hash(image_index, key_hash, &key_hash_size);
244 if (rc) {
Mark Horvath7ebf0e82021-02-19 09:55:35 +0100245 return -1;
David Vincze03368b82020-04-01 12:53:53 +0200246 }
247
Raef Colese8fe6cf2020-05-26 13:07:40 +0100248 /* Adding hardening to avoid this potential attack:
249 * - Image is signed with an arbitrary key and the corresponding public
250 * key is added as a TLV field.
251 * - During public key validation (comparing against key-hash read from
252 * HW) a fault is injected to accept the public key as valid one.
253 */
254 FIH_CALL(boot_fih_memequal, fih_rc, hash, key_hash, key_hash_size);
Michael Grand5047f032022-11-24 16:49:56 +0100255 if (FIH_EQ(fih_rc, FIH_SUCCESS)) {
David Vincze03368b82020-04-01 12:53:53 +0200256 bootutil_keys[0].key = key;
257 pub_key_len = key_len;
258 return 0;
259 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100260
David Vincze03368b82020-04-01 12:53:53 +0200261 return -1;
262}
263#endif /* !MCUBOOT_HW_KEY */
David Brown43cda332017-09-01 09:53:23 -0600264#endif
265
David Vinczec3084132020-02-18 14:50:47 +0100266/**
267 * Reads the value of an image's security counter.
268 *
269 * @param hdr Pointer to the image header structure.
270 * @param fap Pointer to a description structure of the image's
271 * flash area.
272 * @param security_cnt Pointer to store the security counter value.
273 *
274 * @return 0 on success; nonzero on failure.
275 */
276int32_t
277bootutil_get_img_security_cnt(struct image_header *hdr,
278 const struct flash_area *fap,
279 uint32_t *img_security_cnt)
280{
281 struct image_tlv_iter it;
282 uint32_t off;
283 uint16_t len;
284 int32_t rc;
285
286 if ((hdr == NULL) ||
287 (fap == NULL) ||
288 (img_security_cnt == NULL)) {
289 /* Invalid parameter. */
290 return BOOT_EBADARGS;
291 }
292
293 /* The security counter TLV is in the protected part of the TLV area. */
294 if (hdr->ih_protect_tlv_size == 0) {
295 return BOOT_EBADIMAGE;
296 }
297
298 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_SEC_CNT, true);
299 if (rc) {
300 return rc;
301 }
302
303 /* Traverse through the protected TLV area to find
304 * the security counter TLV.
305 */
306
307 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
308 if (rc != 0) {
309 /* Security counter TLV has not been found. */
310 return -1;
311 }
312
313 if (len != sizeof(*img_security_cnt)) {
314 /* Security counter is not valid. */
315 return BOOT_EBADIMAGE;
316 }
317
Tamas Banfe031092020-09-10 17:32:39 +0200318 rc = LOAD_IMAGE_DATA(hdr, fap, off, img_security_cnt, len);
David Vinczec3084132020-02-18 14:50:47 +0100319 if (rc != 0) {
320 return BOOT_EFLASH;
321 }
322
323 return 0;
324}
David Vinczec3084132020-02-18 14:50:47 +0100325
David Brown43cda332017-09-01 09:53:23 -0600326/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800327 * Verify the integrity of the image.
328 * Return non-zero if image could not be validated/does not validate.
329 */
Michael Grand5047f032022-11-24 16:49:56 +0100330fih_ret
Fabio Utzig10ee6482019-08-01 12:04:52 -0300331bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
332 struct image_header *hdr, const struct flash_area *fap,
333 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *seed,
334 int seed_len, uint8_t *out_hash)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800335{
336 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300337 uint16_t len;
David Brownd13318a2019-12-04 17:28:40 -0700338 uint16_t type;
David Brown43cda332017-09-01 09:53:23 -0600339 int sha256_valid = 0;
340#ifdef EXPECTED_SIG_TLV
Michael Grand5047f032022-11-24 16:49:56 +0100341 FIH_DECLARE(valid_signature, FIH_FAILURE);
David Brown43cda332017-09-01 09:53:23 -0600342 int key_id = -1;
David Vincze03368b82020-04-01 12:53:53 +0200343#ifdef MCUBOOT_HW_KEY
344 /* Few extra bytes for encoding and for public exponent. */
345 uint8_t key_buf[SIG_BUF_SIZE + 24];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800346#endif
David Vincze03368b82020-04-01 12:53:53 +0200347#endif /* EXPECTED_SIG_TLV */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300348 struct image_tlv_iter it;
Fabio Utzig3501c012019-05-13 15:07:25 -0700349 uint8_t buf[SIG_BUF_SIZE];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800350 uint8_t hash[32];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100351 int rc = 0;
Michael Grand5047f032022-11-24 16:49:56 +0100352 FIH_DECLARE(fih_rc, FIH_FAILURE);
David Vinczec3084132020-02-18 14:50:47 +0100353#ifdef MCUBOOT_HW_ROLLBACK_PROT
Raef Colese8fe6cf2020-05-26 13:07:40 +0100354 fih_int security_cnt = fih_int_encode(INT_MAX);
David Vinczec3084132020-02-18 14:50:47 +0100355 uint32_t img_security_cnt = 0;
Michael Grand5047f032022-11-24 16:49:56 +0100356 FIH_DECLARE(security_counter_valid, FIH_FAILURE);
David Vinczec3084132020-02-18 14:50:47 +0100357#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800358
Fabio Utzig10ee6482019-08-01 12:04:52 -0300359 rc = bootutil_img_hash(enc_state, image_index, hdr, fap, tmp_buf,
360 tmp_buf_sz, hash, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800361 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100362 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800363 }
364
365 if (out_hash) {
366 memcpy(out_hash, hash, 32);
367 }
368
Fabio Utzig61fd8882019-09-14 20:00:20 -0300369 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false);
David Brownf5b33d82017-09-01 10:58:27 -0600370 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100371 goto out;
David Brownf5b33d82017-09-01 10:58:27 -0600372 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800373
Andrzej Puzdrowski334b6a62022-01-21 16:59:18 +0100374 if (it.tlv_end > bootutil_max_image_size(fap)) {
375 rc = -1;
376 goto out;
377 }
378
David Brown43cda332017-09-01 09:53:23 -0600379 /*
380 * Traverse through all of the TLVs, performing any checks we know
381 * and are able to do.
382 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300383 while (true) {
384 rc = bootutil_tlv_iter_next(&it, &off, &len, &type);
385 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100386 goto out;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300387 } else if (rc > 0) {
388 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800389 }
David Brown43cda332017-09-01 09:53:23 -0600390
Fabio Utzig61fd8882019-09-14 20:00:20 -0300391 if (type == IMAGE_TLV_SHA256) {
David Brown43cda332017-09-01 09:53:23 -0600392 /*
393 * Verify the SHA256 image hash. This must always be
394 * present.
395 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300396 if (len != sizeof(hash)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100397 rc = -1;
398 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800399 }
Tamas Banfe031092020-09-10 17:32:39 +0200400 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, sizeof(hash));
David Brown43cda332017-09-01 09:53:23 -0600401 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100402 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800403 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100404
405 FIH_CALL(boot_fih_memequal, fih_rc, hash, buf, sizeof(hash));
Michael Grand5047f032022-11-24 16:49:56 +0100406 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
407 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100408 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800409 }
David Brown43cda332017-09-01 09:53:23 -0600410
411 sha256_valid = 1;
412#ifdef EXPECTED_SIG_TLV
David Vincze03368b82020-04-01 12:53:53 +0200413#ifndef MCUBOOT_HW_KEY
Fabio Utzig61fd8882019-09-14 20:00:20 -0300414 } else if (type == IMAGE_TLV_KEYHASH) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800415 /*
David Brown43cda332017-09-01 09:53:23 -0600416 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800417 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300418 if (len > 32) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100419 rc = -1;
420 goto out;
David Brown43cda332017-09-01 09:53:23 -0600421 }
Tamas Banfe031092020-09-10 17:32:39 +0200422 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600423 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100424 goto out;
David Brown43cda332017-09-01 09:53:23 -0600425 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300426 key_id = bootutil_find_key(buf, len);
David Brown43cda332017-09-01 09:53:23 -0600427 /*
428 * The key may not be found, which is acceptable. There
429 * can be multiple signatures, each preceded by a key.
430 */
David Vincze03368b82020-04-01 12:53:53 +0200431#else
432 } else if (type == IMAGE_TLV_PUBKEY) {
433 /*
434 * Determine which key we should be checking.
435 */
436 if (len > sizeof(key_buf)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100437 rc = -1;
438 goto out;
David Vincze03368b82020-04-01 12:53:53 +0200439 }
Tamas Banfe031092020-09-10 17:32:39 +0200440 rc = LOAD_IMAGE_DATA(hdr, fap, off, key_buf, len);
David Vincze03368b82020-04-01 12:53:53 +0200441 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100442 goto out;
David Vincze03368b82020-04-01 12:53:53 +0200443 }
444 key_id = bootutil_find_key(image_index, key_buf, len);
445 /*
446 * The key may not be found, which is acceptable. There
447 * can be multiple signatures, each preceded by a key.
448 */
449#endif /* !MCUBOOT_HW_KEY */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300450 } else if (type == EXPECTED_SIG_TLV) {
David Brown43cda332017-09-01 09:53:23 -0600451 /* Ignore this signature if it is out of bounds. */
452 if (key_id < 0 || key_id >= bootutil_key_cnt) {
453 key_id = -1;
454 continue;
455 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300456 if (!EXPECTED_SIG_LEN(len) || len > sizeof(buf)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100457 rc = -1;
458 goto out;
David Brown43cda332017-09-01 09:53:23 -0600459 }
Tamas Banfe031092020-09-10 17:32:39 +0200460 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600461 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100462 goto out;
David Brown43cda332017-09-01 09:53:23 -0600463 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100464 FIH_CALL(bootutil_verify_sig, valid_signature, hash, sizeof(hash),
465 buf, len, key_id);
David Brown43cda332017-09-01 09:53:23 -0600466 key_id = -1;
David Vinczec3084132020-02-18 14:50:47 +0100467#endif /* EXPECTED_SIG_TLV */
468#ifdef MCUBOOT_HW_ROLLBACK_PROT
469 } else if (type == IMAGE_TLV_SEC_CNT) {
470 /*
471 * Verify the image's security counter.
472 * This must always be present.
473 */
474 if (len != sizeof(img_security_cnt)) {
475 /* Security counter is not valid. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100476 rc = -1;
477 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100478 }
479
Tamas Banfe031092020-09-10 17:32:39 +0200480 rc = LOAD_IMAGE_DATA(hdr, fap, off, &img_security_cnt, len);
David Vinczec3084132020-02-18 14:50:47 +0100481 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100482 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100483 }
484
Raef Colese8fe6cf2020-05-26 13:07:40 +0100485 FIH_CALL(boot_nv_security_counter_get, fih_rc, image_index,
486 &security_cnt);
Michael Grand5047f032022-11-24 16:49:56 +0100487 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
488 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100489 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100490 }
491
492 /* Compare the new image's security counter value against the
493 * stored security counter value.
494 */
Michael Grand5047f032022-11-24 16:49:56 +0100495 fih_rc = fih_ret_encode_zero_equality(img_security_cnt <
Raef Colese8fe6cf2020-05-26 13:07:40 +0100496 fih_int_decode(security_cnt));
Michael Grand5047f032022-11-24 16:49:56 +0100497 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
498 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100499 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100500 }
501
502 /* The image's security counter has been successfully verified. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100503 security_counter_valid = fih_rc;
David Vinczec3084132020-02-18 14:50:47 +0100504#endif /* MCUBOOT_HW_ROLLBACK_PROT */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800505 }
506 }
David Brown43cda332017-09-01 09:53:23 -0600507
Raef Colese8fe6cf2020-05-26 13:07:40 +0100508 rc = !sha256_valid;
509 if (rc) {
510 goto out;
511 }
David Brown43cda332017-09-01 09:53:23 -0600512#ifdef EXPECTED_SIG_TLV
Michael Grand5047f032022-11-24 16:49:56 +0100513 FIH_SET(fih_rc, valid_signature);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800514#endif
David Vinczec3084132020-02-18 14:50:47 +0100515#ifdef MCUBOOT_HW_ROLLBACK_PROT
Michael Grand5047f032022-11-24 16:49:56 +0100516 if (FIH_NOT_EQ(security_counter_valid, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100517 rc = -1;
518 goto out;
519 }
David Vinczec3084132020-02-18 14:50:47 +0100520#endif
Raef Colese8fe6cf2020-05-26 13:07:40 +0100521
522out:
523 if (rc) {
Michael Grand5047f032022-11-24 16:49:56 +0100524 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczec3084132020-02-18 14:50:47 +0100525 }
David Brown43cda332017-09-01 09:53:23 -0600526
Raef Colese8fe6cf2020-05-26 13:07:40 +0100527 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800528}