blob: a53ccb469bb528e17576c591e76b8119f4575b3c [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
Roman Okhrimenko977b3752022-03-31 14:40:48 +03007 * Copyright (c) 2021 Infineon Technologies AG
David Brownaac71112020-02-03 16:13:42 -07008 *
9 * Original license:
10 *
Christopher Collins92ea77f2016-12-12 15:59:26 -080011 * Licensed to the Apache Software Foundation (ASF) under one
12 * or more contributor license agreements. See the NOTICE file
13 * distributed with this work for additional information
14 * regarding copyright ownership. The ASF licenses this file
15 * to you under the Apache License, Version 2.0 (the
16 * "License"); you may not use this file except in compliance
17 * with the License. You may obtain a copy of the License at
18 *
19 * http://www.apache.org/licenses/LICENSE-2.0
20 *
21 * Unless required by applicable law or agreed to in writing,
22 * software distributed under the License is distributed on an
23 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
24 * KIND, either express or implied. See the License for the
25 * specific language governing permissions and limitations
26 * under the License.
27 */
28
Christopher Collins92ea77f2016-12-12 15:59:26 -080029#include <stddef.h>
David Vinczec3084132020-02-18 14:50:47 +010030#include <stdint.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080031#include <inttypes.h>
32#include <string.h>
33
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020034#include <flash_map_backend/flash_map_backend.h>
35
Christopher Collins92ea77f2016-12-12 15:59:26 -080036#include "bootutil/image.h"
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +090037#include "bootutil/crypto/sha256.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080038#include "bootutil/sign_key.h"
David Vinczec3084132020-02-18 14:50:47 +010039#include "bootutil/security_cnt.h"
Raef Colese8fe6cf2020-05-26 13:07:40 +010040#include "bootutil/fault_injection_hardening.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080041
Roman Okhrimenko977b3752022-03-31 14:40:48 +030042#include "bootutil/bootutil_log.h"
43
Fabio Utzigba1fbe62017-07-21 14:01:20 -030044#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030045
Fabio Utzigba829042018-09-18 08:29:34 -030046#ifdef MCUBOOT_ENC_IMAGES
47#include "bootutil/enc_key.h"
48#endif
49#if defined(MCUBOOT_SIGN_RSA)
Christopher Collins92ea77f2016-12-12 15:59:26 -080050#include "mbedtls/rsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060051#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -030052#if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080053#include "mbedtls/ecdsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060054#endif
Arvin Farahmandf8240192020-05-05 11:43:52 -040055#if defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_SIGN_RSA) || \
56 defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080057#include "mbedtls/asn1.h"
Arvin Farahmandf8240192020-05-05 11:43:52 -040058#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080059
60#include "bootutil_priv.h"
61
Roman Okhrimenko25165622023-05-23 08:58:29 +030062#ifdef CYW20829
63#include "cy_security_cnt_platform.h"
64#endif
65
Christopher Collins92ea77f2016-12-12 15:59:26 -080066/*
67 * Compute SHA256 over the image.
68 */
69static int
Fabio Utzig10ee6482019-08-01 12:04:52 -030070bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
71 struct image_header *hdr, const struct flash_area *fap,
72 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *hash_result,
73 uint8_t *seed, int seed_len)
Christopher Collins92ea77f2016-12-12 15:59:26 -080074{
David Browne629bf32017-04-24 13:37:33 -060075 bootutil_sha256_context sha256_ctx;
Christopher Collins92ea77f2016-12-12 15:59:26 -080076 uint32_t blk_sz;
77 uint32_t size;
Fabio Utzig2fc80df2018-12-14 06:47:38 -020078 uint16_t hdr_size;
Christopher Collins92ea77f2016-12-12 15:59:26 -080079 uint32_t off;
80 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -030081 uint32_t blk_off;
Fabio Utzige52c08e2019-09-11 19:32:00 -030082 uint32_t tlv_off;
Christopher Collins92ea77f2016-12-12 15:59:26 -080083
Tamas Banfe031092020-09-10 17:32:39 +020084#if (BOOT_IMAGE_NUMBER == 1) || !defined(MCUBOOT_ENC_IMAGES) || \
85 defined(MCUBOOT_RAM_LOAD)
Fabio Utzig10ee6482019-08-01 12:04:52 -030086 (void)enc_state;
Fabio Utzigb0f04732019-07-31 09:49:19 -030087 (void)image_index;
Fabio Utzigc9621352019-08-08 12:15:51 -030088 (void)hdr_size;
Fabio Utzige52c08e2019-09-11 19:32:00 -030089 (void)blk_off;
90 (void)tlv_off;
Tamas Banfe031092020-09-10 17:32:39 +020091#ifdef MCUBOOT_RAM_LOAD
92 (void)blk_sz;
93 (void)off;
94 (void)rc;
Roman Okhrimenko977b3752022-03-31 14:40:48 +030095 (void)fap;
96 (void)tmp_buf;
97 (void)tmp_buf_sz;
Tamas Banfe031092020-09-10 17:32:39 +020098#endif
Fabio Utzigb0f04732019-07-31 09:49:19 -030099#endif
100
Fabio Utzigbc077932019-08-26 11:16:34 -0300101#ifdef MCUBOOT_ENC_IMAGES
102 /* Encrypted images only exist in the secondary slot */
103 if (MUST_DECRYPT(fap, image_index, hdr) &&
104 !boot_enc_valid(enc_state, image_index, fap)) {
105 return -1;
106 }
107#endif
108
David Browne629bf32017-04-24 13:37:33 -0600109 bootutil_sha256_init(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800110
111 /* in some cases (split image) the hash is seeded with data from
112 * the loader image */
Fabio Utzig53986042017-12-12 14:57:07 -0200113 if (seed && (seed_len > 0)) {
David Browne629bf32017-04-24 13:37:33 -0600114 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800115 }
116
David Vinczee32483f2019-06-13 10:46:24 +0200117 /* Hash is computed over image header and image itself. */
Fabio Utzige52c08e2019-09-11 19:32:00 -0300118 size = hdr_size = hdr->ih_hdr_size;
119 size += hdr->ih_img_size;
120 tlv_off = size;
David Vinczee32483f2019-06-13 10:46:24 +0200121
Fabio Utzige52c08e2019-09-11 19:32:00 -0300122 /* If protected TLVs are present they are also hashed. */
123 size += hdr->ih_protect_tlv_size;
David Vinczee32483f2019-06-13 10:46:24 +0200124
Tamas Banfe031092020-09-10 17:32:39 +0200125#ifdef MCUBOOT_RAM_LOAD
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300126 bootutil_sha256_update(&sha256_ctx,
127 (void*)(IMAGE_RAM_BASE + hdr->ih_load_addr),
128 size);
Tamas Banfe031092020-09-10 17:32:39 +0200129#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800130 for (off = 0; off < size; off += blk_sz) {
131 blk_sz = size - off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300132 if (blk_sz > tmp_buf_sz) {
133 blk_sz = tmp_buf_sz;
134 }
Fabio Utzigc21c2102019-09-10 10:10:42 -0300135#ifdef MCUBOOT_ENC_IMAGES
136 /* The only data that is encrypted in an image is the payload;
137 * both header and TLVs (when protected) are not.
138 */
139 if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) {
140 /* read only the header */
141 blk_sz = hdr_size - off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300142 }
143 if ((off < tlv_off) && ((off + blk_sz) > tlv_off)) {
144 /* read only up to the end of the image payload */
145 blk_sz = tlv_off - off;
Fabio Utzigc21c2102019-09-10 10:10:42 -0300146 }
147#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800148 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
149 if (rc) {
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900150 bootutil_sha256_drop(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800151 return rc;
152 }
Fabio Utzigba829042018-09-18 08:29:34 -0300153#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzigc21c2102019-09-10 10:10:42 -0300154 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzige52c08e2019-09-11 19:32:00 -0300155 /* Only payload is encrypted (area between header and TLVs) */
156 if (off >= hdr_size && off < tlv_off) {
Fabio Utzigc21c2102019-09-10 10:10:42 -0300157 blk_off = (off - hdr_size) & 0xf;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300158#ifdef MCUBOOT_ENC_IMAGES_XIP
159 rc = bootutil_img_encrypt(enc_state, image_index, hdr, fap, off,
Fabio Utzigc21c2102019-09-10 10:10:42 -0300160 blk_sz, blk_off, tmp_buf);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300161#else
162 rc = boot_encrypt(enc_state, image_index, fap, off - hdr_size,
163 blk_sz, blk_off, tmp_buf);
164#endif
165 if (rc) {
166 return rc;
167 }
Fabio Utzigc21c2102019-09-10 10:10:42 -0300168 }
Fabio Utzigba829042018-09-18 08:29:34 -0300169 }
170#endif
David Browne629bf32017-04-24 13:37:33 -0600171 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800172 }
Tamas Banfe031092020-09-10 17:32:39 +0200173#endif /* MCUBOOT_RAM_LOAD */
David Browne629bf32017-04-24 13:37:33 -0600174 bootutil_sha256_finish(&sha256_ctx, hash_result);
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900175 bootutil_sha256_drop(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800176
177 return 0;
178}
179
180/*
David Brown43cda332017-09-01 09:53:23 -0600181 * Currently, we only support being able to verify one type of
182 * signature, because there is a single verification function that we
183 * call. List the type of TLV we are expecting. If we aren't
184 * configured for any signature, don't define this macro.
185 */
Fabio Utzig48764842019-05-10 19:28:24 -0300186#if (defined(MCUBOOT_SIGN_RSA) + \
187 defined(MCUBOOT_SIGN_EC) + \
188 defined(MCUBOOT_SIGN_EC256) + \
189 defined(MCUBOOT_SIGN_ED25519)) > 1
Fabio Utzig3501c012019-05-13 15:07:25 -0700190#error "Only a single signature type is supported!"
191#endif
192
David Brown43cda332017-09-01 09:53:23 -0600193#if defined(MCUBOOT_SIGN_RSA)
Fabio Utzig3501c012019-05-13 15:07:25 -0700194# if MCUBOOT_SIGN_RSA_LEN == 2048
195# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
196# elif MCUBOOT_SIGN_RSA_LEN == 3072
197# define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS
198# else
199# error "Unsupported RSA signature length"
David Brown43cda332017-09-01 09:53:23 -0600200# endif
Fabio Utzig3501c012019-05-13 15:07:25 -0700201# define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8)
202# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) /* 2048 bits */
David Brown43cda332017-09-01 09:53:23 -0600203#elif defined(MCUBOOT_SIGN_EC)
204# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224
Fabio Utzig3501c012019-05-13 15:07:25 -0700205# define SIG_BUF_SIZE 128
David Browna3608262019-12-12 15:35:31 -0700206# define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */
David Brown43cda332017-09-01 09:53:23 -0600207#elif defined(MCUBOOT_SIGN_EC256)
208# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
Fabio Utzig3501c012019-05-13 15:07:25 -0700209# define SIG_BUF_SIZE 128
David Browna3608262019-12-12 15:35:31 -0700210# define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */
Fabio Utzig48764842019-05-10 19:28:24 -0300211#elif defined(MCUBOOT_SIGN_ED25519)
212# define EXPECTED_SIG_TLV IMAGE_TLV_ED25519
213# define SIG_BUF_SIZE 64
214# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE)
Fabio Utzig3501c012019-05-13 15:07:25 -0700215#else
216# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
David Brown43cda332017-09-01 09:53:23 -0600217#endif
218
219#ifdef EXPECTED_SIG_TLV
David Vincze03368b82020-04-01 12:53:53 +0200220#if !defined(MCUBOOT_HW_KEY)
David Brown43cda332017-09-01 09:53:23 -0600221static int
222bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
223{
224 bootutil_sha256_context sha256_ctx;
225 int i;
226 const struct bootutil_key *key;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300227 uint8_t hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
David Brown43cda332017-09-01 09:53:23 -0600228
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300229 if (keyhash_len != BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE) {
Fabio Utzig15c14672019-08-09 07:45:20 -0300230 return -1;
231 }
David Brown43cda332017-09-01 09:53:23 -0600232
233 for (i = 0; i < bootutil_key_cnt; i++) {
234 key = &bootutil_keys[i];
235 bootutil_sha256_init(&sha256_ctx);
236 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
237 bootutil_sha256_finish(&sha256_ctx, hash);
238 if (!memcmp(hash, keyhash, keyhash_len)) {
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900239 bootutil_sha256_drop(&sha256_ctx);
David Brown43cda332017-09-01 09:53:23 -0600240 return i;
241 }
242 }
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900243 bootutil_sha256_drop(&sha256_ctx);
David Brown43cda332017-09-01 09:53:23 -0600244 return -1;
245}
David Vincze03368b82020-04-01 12:53:53 +0200246#else
247extern unsigned int pub_key_len;
248static int
249bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
250{
251 bootutil_sha256_context sha256_ctx;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300252 uint8_t hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
253 uint8_t key_hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
David Vincze03368b82020-04-01 12:53:53 +0200254 size_t key_hash_size = sizeof(key_hash);
255 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100256 fih_int fih_rc;
David Vincze03368b82020-04-01 12:53:53 +0200257
258 bootutil_sha256_init(&sha256_ctx);
259 bootutil_sha256_update(&sha256_ctx, key, key_len);
260 bootutil_sha256_finish(&sha256_ctx, hash);
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900261 bootutil_sha256_drop(&sha256_ctx);
David Vincze03368b82020-04-01 12:53:53 +0200262
263 rc = boot_retrieve_public_key_hash(image_index, key_hash, &key_hash_size);
264 if (rc) {
265 return rc;
266 }
267
Raef Colese8fe6cf2020-05-26 13:07:40 +0100268 /* Adding hardening to avoid this potential attack:
269 * - Image is signed with an arbitrary key and the corresponding public
270 * key is added as a TLV field.
271 * - During public key validation (comparing against key-hash read from
272 * HW) a fault is injected to accept the public key as valid one.
273 */
274 FIH_CALL(boot_fih_memequal, fih_rc, hash, key_hash, key_hash_size);
275 if (fih_eq(fih_rc, FIH_SUCCESS)) {
David Vincze03368b82020-04-01 12:53:53 +0200276 bootutil_keys[0].key = key;
277 pub_key_len = key_len;
278 return 0;
279 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100280
David Vincze03368b82020-04-01 12:53:53 +0200281 return -1;
282}
283#endif /* !MCUBOOT_HW_KEY */
David Brown43cda332017-09-01 09:53:23 -0600284#endif
285
David Vinczec3084132020-02-18 14:50:47 +0100286#ifdef MCUBOOT_HW_ROLLBACK_PROT
287/**
288 * Reads the value of an image's security counter.
289 *
290 * @param hdr Pointer to the image header structure.
291 * @param fap Pointer to a description structure of the image's
292 * flash area.
293 * @param security_cnt Pointer to store the security counter value.
294 *
295 * @return 0 on success; nonzero on failure.
296 */
297int32_t
298bootutil_get_img_security_cnt(struct image_header *hdr,
299 const struct flash_area *fap,
300 uint32_t *img_security_cnt)
301{
302 struct image_tlv_iter it;
303 uint32_t off;
304 uint16_t len;
305 int32_t rc;
306
307 if ((hdr == NULL) ||
308 (fap == NULL) ||
309 (img_security_cnt == NULL)) {
310 /* Invalid parameter. */
311 return BOOT_EBADARGS;
312 }
313
314 /* The security counter TLV is in the protected part of the TLV area. */
315 if (hdr->ih_protect_tlv_size == 0) {
316 return BOOT_EBADIMAGE;
317 }
318
319 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_SEC_CNT, true);
320 if (rc) {
321 return rc;
322 }
323
324 /* Traverse through the protected TLV area to find
325 * the security counter TLV.
326 */
327
328 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
329 if (rc != 0) {
330 /* Security counter TLV has not been found. */
331 return -1;
332 }
333
334 if (len != sizeof(*img_security_cnt)) {
335 /* Security counter is not valid. */
336 return BOOT_EBADIMAGE;
337 }
338
Tamas Banfe031092020-09-10 17:32:39 +0200339 rc = LOAD_IMAGE_DATA(hdr, fap, off, img_security_cnt, len);
David Vinczec3084132020-02-18 14:50:47 +0100340 if (rc != 0) {
341 return BOOT_EFLASH;
342 }
343
344 return 0;
345}
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300346#ifdef CYW20829
347/**
348 * Reads the content of an image's reprovisioning packet.
349 *
350 * @param hdr Pointer to the image header structure.
351 * @param fap Pointer to a description structure of the image's
352 * flash area.
353 * @param reprov_packet Pointer to store the reprovisioning packet.
354 *
355 * @return 0 on success; nonzero on failure.
356 */
357int32_t
358bootutil_get_img_reprov_packet(struct image_header *hdr,
359 const struct flash_area *fap,
360 uint8_t *reprov_packet)
361{
362 struct image_tlv_iter it;
363 uint32_t off;
364 uint16_t len;
365 int32_t rc;
366
367 if ((hdr == NULL) ||
368 (fap == NULL) ||
369 (reprov_packet == NULL)) {
370 /* Invalid parameter. */
371 return BOOT_EBADARGS;
372 }
373
374 /* The reprovisioning packet TLV is in the protected part of the TLV area. */
375 if (hdr->ih_protect_tlv_size == 0) {
376 return BOOT_EBADIMAGE;
377 }
378
379 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_PROV_PACK, true);
380 if (rc) {
381 return rc;
382 }
383
384 /* Traverse through the protected TLV area to find
385 * the reprovisioning apcket TLV.
386 */
387
388 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
389 if (rc != 0) {
390 /* Reprovisioning packet TLV has not been found. */
391 return -1;
392 }
393
394 if (len != REPROV_PACK_SIZE) {
395 /* Reprovisioning packet is not valid. */
396 return BOOT_EBADIMAGE;
397 }
398
399 rc = LOAD_IMAGE_DATA(hdr, fap, off, reprov_packet, len);
400 if (rc != 0) {
401 return BOOT_EFLASH;
402 }
403
404 return 0;
405}
406#endif /* CYW20289 */
David Vinczec3084132020-02-18 14:50:47 +0100407#endif /* MCUBOOT_HW_ROLLBACK_PROT */
408
David Brown43cda332017-09-01 09:53:23 -0600409/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800410 * Verify the integrity of the image.
411 * Return non-zero if image could not be validated/does not validate.
412 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100413fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300414bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
415 struct image_header *hdr, const struct flash_area *fap,
416 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *seed,
417 int seed_len, uint8_t *out_hash)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800418{
419 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300420 uint16_t len;
David Brownd13318a2019-12-04 17:28:40 -0700421 uint16_t type;
David Brown43cda332017-09-01 09:53:23 -0600422 int sha256_valid = 0;
423#ifdef EXPECTED_SIG_TLV
Raef Colese8fe6cf2020-05-26 13:07:40 +0100424 fih_int valid_signature = FIH_FAILURE;
David Brown43cda332017-09-01 09:53:23 -0600425 int key_id = -1;
David Vincze03368b82020-04-01 12:53:53 +0200426#ifdef MCUBOOT_HW_KEY
427 /* Few extra bytes for encoding and for public exponent. */
428 uint8_t key_buf[SIG_BUF_SIZE + 24];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800429#endif
David Vincze03368b82020-04-01 12:53:53 +0200430#endif /* EXPECTED_SIG_TLV */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300431 struct image_tlv_iter it;
Fabio Utzig3501c012019-05-13 15:07:25 -0700432 uint8_t buf[SIG_BUF_SIZE];
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300433 uint8_t hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100434 int rc = 0;
435 fih_int fih_rc = FIH_FAILURE;
David Vinczec3084132020-02-18 14:50:47 +0100436#ifdef MCUBOOT_HW_ROLLBACK_PROT
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300437 fih_uint security_cnt = fih_uint_encode(UINT_MAX);
David Vinczec3084132020-02-18 14:50:47 +0100438 uint32_t img_security_cnt = 0;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300439 uint8_t reprov_packet[REPROV_PACK_SIZE];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100440 fih_int security_counter_valid = FIH_FAILURE;
Roman Okhrimenko25165622023-05-23 08:58:29 +0300441 #ifdef CYW20829
442 fih_uint extracted_img_cnt = fih_uint_encode(UINT_MAX);
443 #endif /* CYW20829 */
444#endif /* MCUBOOT_HW_ROLLBACK_PROT */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800445
Fabio Utzig10ee6482019-08-01 12:04:52 -0300446 rc = bootutil_img_hash(enc_state, image_index, hdr, fap, tmp_buf,
447 tmp_buf_sz, hash, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800448 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100449 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800450 }
451
452 if (out_hash) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300453 memcpy(out_hash, hash, BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800454 }
455
Fabio Utzig61fd8882019-09-14 20:00:20 -0300456 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false);
David Brownf5b33d82017-09-01 10:58:27 -0600457 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100458 goto out;
David Brownf5b33d82017-09-01 10:58:27 -0600459 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800460
David Brown43cda332017-09-01 09:53:23 -0600461 /*
462 * Traverse through all of the TLVs, performing any checks we know
463 * and are able to do.
464 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300465 while (true) {
466 rc = bootutil_tlv_iter_next(&it, &off, &len, &type);
467 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100468 goto out;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300469 } else if (rc > 0) {
470 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800471 }
David Brown43cda332017-09-01 09:53:23 -0600472
Fabio Utzig61fd8882019-09-14 20:00:20 -0300473 if (type == IMAGE_TLV_SHA256) {
David Brown43cda332017-09-01 09:53:23 -0600474 /*
475 * Verify the SHA256 image hash. This must always be
476 * present.
477 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300478 if (len != sizeof(hash)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100479 rc = -1;
480 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800481 }
Tamas Banfe031092020-09-10 17:32:39 +0200482 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, sizeof(hash));
David Brown43cda332017-09-01 09:53:23 -0600483 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100484 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800485 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100486
487 FIH_CALL(boot_fih_memequal, fih_rc, hash, buf, sizeof(hash));
488 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
489 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800490 }
David Brown43cda332017-09-01 09:53:23 -0600491
492 sha256_valid = 1;
493#ifdef EXPECTED_SIG_TLV
David Vincze03368b82020-04-01 12:53:53 +0200494#ifndef MCUBOOT_HW_KEY
Fabio Utzig61fd8882019-09-14 20:00:20 -0300495 } else if (type == IMAGE_TLV_KEYHASH) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800496 /*
David Brown43cda332017-09-01 09:53:23 -0600497 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800498 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300499 if (len != BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100500 rc = -1;
501 goto out;
David Brown43cda332017-09-01 09:53:23 -0600502 }
Tamas Banfe031092020-09-10 17:32:39 +0200503 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600504 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100505 goto out;
David Brown43cda332017-09-01 09:53:23 -0600506 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300507 key_id = bootutil_find_key(buf, len);
David Brown43cda332017-09-01 09:53:23 -0600508 /*
509 * The key may not be found, which is acceptable. There
510 * can be multiple signatures, each preceded by a key.
511 */
David Vincze03368b82020-04-01 12:53:53 +0200512#else
513 } else if (type == IMAGE_TLV_PUBKEY) {
514 /*
515 * Determine which key we should be checking.
516 */
517 if (len > sizeof(key_buf)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100518 rc = -1;
519 goto out;
David Vincze03368b82020-04-01 12:53:53 +0200520 }
Tamas Banfe031092020-09-10 17:32:39 +0200521 rc = LOAD_IMAGE_DATA(hdr, fap, off, key_buf, len);
David Vincze03368b82020-04-01 12:53:53 +0200522 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100523 goto out;
David Vincze03368b82020-04-01 12:53:53 +0200524 }
525 key_id = bootutil_find_key(image_index, key_buf, len);
526 /*
527 * The key may not be found, which is acceptable. There
528 * can be multiple signatures, each preceded by a key.
529 */
530#endif /* !MCUBOOT_HW_KEY */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300531 } else if (type == EXPECTED_SIG_TLV) {
David Brown43cda332017-09-01 09:53:23 -0600532 /* Ignore this signature if it is out of bounds. */
533 if (key_id < 0 || key_id >= bootutil_key_cnt) {
534 key_id = -1;
535 continue;
536 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300537 if (!EXPECTED_SIG_LEN(len) || len > sizeof(buf)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100538 rc = -1;
539 goto out;
David Brown43cda332017-09-01 09:53:23 -0600540 }
Tamas Banfe031092020-09-10 17:32:39 +0200541 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600542 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100543 goto out;
David Brown43cda332017-09-01 09:53:23 -0600544 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100545 FIH_CALL(bootutil_verify_sig, valid_signature, hash, sizeof(hash),
546 buf, len, key_id);
David Brown43cda332017-09-01 09:53:23 -0600547 key_id = -1;
David Vinczec3084132020-02-18 14:50:47 +0100548#endif /* EXPECTED_SIG_TLV */
549#ifdef MCUBOOT_HW_ROLLBACK_PROT
550 } else if (type == IMAGE_TLV_SEC_CNT) {
551 /*
552 * Verify the image's security counter.
553 * This must always be present.
554 */
555 if (len != sizeof(img_security_cnt)) {
556 /* Security counter is not valid. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100557 rc = -1;
558 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100559 }
560
Tamas Banfe031092020-09-10 17:32:39 +0200561 rc = LOAD_IMAGE_DATA(hdr, fap, off, &img_security_cnt, len);
David Vinczec3084132020-02-18 14:50:47 +0100562 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100563 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100564 }
565
Raef Colese8fe6cf2020-05-26 13:07:40 +0100566 FIH_CALL(boot_nv_security_counter_get, fih_rc, image_index,
567 &security_cnt);
568 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
569 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100570 }
571
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300572 BOOT_LOG_DBG("NV Counter read from efuse = %u", fih_uint_decode(security_cnt));
573
Roman Okhrimenko25165622023-05-23 08:58:29 +0300574#ifdef CYW20829
575 BOOT_LOG_DBG("Full NV Counter read from image = 0x%X", img_security_cnt);
576
577 FIH_CALL(platform_security_counter_check_extract, fih_rc,
578 (uint32_t)image_index, fih_uint_encode(img_security_cnt), &extracted_img_cnt);
579
580 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
581 /* The image's security counter exceeds registered value for this image */
582 goto out;
583 }
584
585 img_security_cnt = fih_uint_decode(extracted_img_cnt);
586#endif /*CYW20829*/
587
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300588 BOOT_LOG_DBG("NV Counter read from image = %" PRIu32, img_security_cnt);
589
David Vinczec3084132020-02-18 14:50:47 +0100590 /* Compare the new image's security counter value against the
591 * stored security counter value.
592 */
Roman Okhrimenko25165622023-05-23 08:58:29 +0300593 fih_rc = fih_int_encode_zero_equality( (int32_t)(img_security_cnt <
594 fih_uint_decode(security_cnt)) );
595
Raef Colese8fe6cf2020-05-26 13:07:40 +0100596 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200597 /* The image's security counter is not accepted. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100598 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100599 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300600#ifndef CYW20829
David Vinczec3084132020-02-18 14:50:47 +0100601 /* The image's security counter has been successfully verified. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100602 security_counter_valid = fih_rc;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300603 }
604#else
605 /* The image's security counter has been successfully verified. */
606 security_counter_valid = fih_int_encode(HW_ROLLBACK_CNT_VALID);
607 } else if (type == IMAGE_TLV_PROV_PACK) {
608
609 if (fih_eq(security_counter_valid, fih_int_encode(HW_ROLLBACK_CNT_VALID))) {
610 /*
611 * Verify the image reprovisioning packet.
612 * This must always be present.
613 */
614 BOOT_LOG_INF("Prov packet length 0x51 TLV = %" PRIu16, len);
615
616 if (len != sizeof(reprov_packet)) {
617 /* Re-provisioning packet is not valid. */
618 rc = -1;
619 goto out;
620 }
621
622 rc = LOAD_IMAGE_DATA(hdr, fap, off, reprov_packet, len);
623 if (rc) {
624 goto out;
625 }
626
627 security_counter_valid = fih_int_encode(fih_int_decode(security_counter_valid) | REPROV_PACK_VALID);
628 }
629 else{
630 goto out;
631 }
632#endif /* CYW20829 */
David Vinczec3084132020-02-18 14:50:47 +0100633#endif /* MCUBOOT_HW_ROLLBACK_PROT */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800634 }
635 }
David Brown43cda332017-09-01 09:53:23 -0600636
Raef Colese8fe6cf2020-05-26 13:07:40 +0100637 rc = !sha256_valid;
638 if (rc) {
639 goto out;
640 }
David Brown43cda332017-09-01 09:53:23 -0600641#ifdef EXPECTED_SIG_TLV
Raef Colese8fe6cf2020-05-26 13:07:40 +0100642 fih_rc = fih_int_encode_zero_equality(fih_not_eq(valid_signature,
643 FIH_SUCCESS));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800644#endif
David Vinczec3084132020-02-18 14:50:47 +0100645#ifdef MCUBOOT_HW_ROLLBACK_PROT
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300646#ifdef CYW20829
647 if (fih_not_eq(security_counter_valid, fih_int_encode(REPROV_PACK_VALID | HW_ROLLBACK_CNT_VALID))) {
648 BOOT_LOG_DBG("Reprovisioning packet TLV 0x51 is not present image = %d", image_index);
649#else
Raef Colese8fe6cf2020-05-26 13:07:40 +0100650 if (fih_not_eq(security_counter_valid, FIH_SUCCESS)) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300651#endif /* CYW20829 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100652 rc = -1;
653 goto out;
654 }
David Vinczec3084132020-02-18 14:50:47 +0100655#endif
Raef Colese8fe6cf2020-05-26 13:07:40 +0100656
657out:
658 if (rc) {
659 fih_rc = fih_int_encode(rc);
David Vinczec3084132020-02-18 14:50:47 +0100660 }
David Brown43cda332017-09-01 09:53:23 -0600661
Raef Colese8fe6cf2020-05-26 13:07:40 +0100662 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800663}