blob: 4807b482226c5766c2fb9e07bd6357db1c087b22 [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
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300125 do
126 {
127#if defined(MCUBOOT_RAM_LOAD)
128#if defined(MCUBOOT_MULTI_MEMORY_LOAD)
129 if (IS_RAM_BOOTABLE(hdr))
130#endif /* MCUBOOT_MULTI_MEMORY_LOAD */
131 {
132 bootutil_sha256_update(
133 &sha256_ctx, (void *)(IMAGE_RAM_BASE + hdr->ih_load_addr), size);
134 break;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300135 }
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300136#endif /* MCUBOOT_RAM_LOAD */
137#if !(defined(MCUBOOT_RAM_LOAD) && !defined(MCUBOOT_MULTI_MEMORY_LOAD))
138 {
139 for (off = 0; off < size; off += blk_sz) {
140 blk_sz = size - off;
141 if (blk_sz > tmp_buf_sz) {
142 blk_sz = tmp_buf_sz;
143 }
Fabio Utzigc21c2102019-09-10 10:10:42 -0300144#ifdef MCUBOOT_ENC_IMAGES
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300145 /* The only data that is encrypted in an image is the payload;
146 * both header and TLVs (when protected) are not.
147 */
148 if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) {
149 /* read only the header */
150 blk_sz = hdr_size - off;
151 }
152 if ((off < tlv_off) && ((off + blk_sz) > tlv_off)) {
153 /* read only up to the end of the image payload */
154 blk_sz = tlv_off - off;
155 }
156#endif /* MCUBOOT_ENC_IMAGES */
157 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300158 if (rc) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300159 bootutil_sha256_drop(&sha256_ctx);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300160 return rc;
161 }
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300162#ifdef MCUBOOT_ENC_IMAGES
163 if (MUST_DECRYPT(fap, image_index, hdr)) {
164 /* Only payload is encrypted (area between header and TLVs) */
165 if (off >= hdr_size && off < tlv_off) {
166 blk_off = (off - hdr_size) & 0xf;
167#ifdef MCUBOOT_ENC_IMAGES_XIP
168 rc = bootutil_img_encrypt(enc_state, image_index, hdr, fap, off,
169 blk_sz, blk_off, tmp_buf);
170#else
171 rc = boot_encrypt(enc_state, image_index, fap, off - hdr_size,
172 blk_sz, blk_off, tmp_buf);
173#endif /* MCUBOOT_ENC_IMAGES_XIP */
174 if (rc) {
175 return rc;
176 }
177 }
178 }
179#endif /* MCUBOOT_ENC_IMAGES */
180 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Fabio Utzigc21c2102019-09-10 10:10:42 -0300181 }
Fabio Utzigba829042018-09-18 08:29:34 -0300182 }
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300183#endif /* !MCUBOOT_RAM_LOAD || MCUBOOT_MULTI_MEMORY_LOAD */
184 } while (false);
David Browne629bf32017-04-24 13:37:33 -0600185 bootutil_sha256_finish(&sha256_ctx, hash_result);
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900186 bootutil_sha256_drop(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800187
188 return 0;
189}
190
191/*
David Brown43cda332017-09-01 09:53:23 -0600192 * Currently, we only support being able to verify one type of
193 * signature, because there is a single verification function that we
194 * call. List the type of TLV we are expecting. If we aren't
195 * configured for any signature, don't define this macro.
196 */
Fabio Utzig48764842019-05-10 19:28:24 -0300197#if (defined(MCUBOOT_SIGN_RSA) + \
198 defined(MCUBOOT_SIGN_EC) + \
199 defined(MCUBOOT_SIGN_EC256) + \
200 defined(MCUBOOT_SIGN_ED25519)) > 1
Fabio Utzig3501c012019-05-13 15:07:25 -0700201#error "Only a single signature type is supported!"
202#endif
203
David Brown43cda332017-09-01 09:53:23 -0600204#if defined(MCUBOOT_SIGN_RSA)
Fabio Utzig3501c012019-05-13 15:07:25 -0700205# if MCUBOOT_SIGN_RSA_LEN == 2048
206# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
207# elif MCUBOOT_SIGN_RSA_LEN == 3072
208# define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS
209# else
210# error "Unsupported RSA signature length"
David Brown43cda332017-09-01 09:53:23 -0600211# endif
Fabio Utzig3501c012019-05-13 15:07:25 -0700212# define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8)
213# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) /* 2048 bits */
David Brown43cda332017-09-01 09:53:23 -0600214#elif defined(MCUBOOT_SIGN_EC)
215# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224
Fabio Utzig3501c012019-05-13 15:07:25 -0700216# define SIG_BUF_SIZE 128
David Browna3608262019-12-12 15:35:31 -0700217# define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */
David Brown43cda332017-09-01 09:53:23 -0600218#elif defined(MCUBOOT_SIGN_EC256)
219# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
Fabio Utzig3501c012019-05-13 15:07:25 -0700220# define SIG_BUF_SIZE 128
David Browna3608262019-12-12 15:35:31 -0700221# define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */
Fabio Utzig48764842019-05-10 19:28:24 -0300222#elif defined(MCUBOOT_SIGN_ED25519)
223# define EXPECTED_SIG_TLV IMAGE_TLV_ED25519
224# define SIG_BUF_SIZE 64
225# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE)
Fabio Utzig3501c012019-05-13 15:07:25 -0700226#else
227# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
David Brown43cda332017-09-01 09:53:23 -0600228#endif
229
230#ifdef EXPECTED_SIG_TLV
David Vincze03368b82020-04-01 12:53:53 +0200231#if !defined(MCUBOOT_HW_KEY)
David Brown43cda332017-09-01 09:53:23 -0600232static int
233bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
234{
235 bootutil_sha256_context sha256_ctx;
236 int i;
237 const struct bootutil_key *key;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300238 uint8_t hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
David Brown43cda332017-09-01 09:53:23 -0600239
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300240 if (keyhash_len != BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE) {
Fabio Utzig15c14672019-08-09 07:45:20 -0300241 return -1;
242 }
David Brown43cda332017-09-01 09:53:23 -0600243
244 for (i = 0; i < bootutil_key_cnt; i++) {
245 key = &bootutil_keys[i];
246 bootutil_sha256_init(&sha256_ctx);
247 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
248 bootutil_sha256_finish(&sha256_ctx, hash);
249 if (!memcmp(hash, keyhash, keyhash_len)) {
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900250 bootutil_sha256_drop(&sha256_ctx);
David Brown43cda332017-09-01 09:53:23 -0600251 return i;
252 }
253 }
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900254 bootutil_sha256_drop(&sha256_ctx);
David Brown43cda332017-09-01 09:53:23 -0600255 return -1;
256}
David Vincze03368b82020-04-01 12:53:53 +0200257#else
258extern unsigned int pub_key_len;
259static int
260bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
261{
262 bootutil_sha256_context sha256_ctx;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300263 uint8_t hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
264 uint8_t key_hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
David Vincze03368b82020-04-01 12:53:53 +0200265 size_t key_hash_size = sizeof(key_hash);
266 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100267 fih_int fih_rc;
David Vincze03368b82020-04-01 12:53:53 +0200268
269 bootutil_sha256_init(&sha256_ctx);
270 bootutil_sha256_update(&sha256_ctx, key, key_len);
271 bootutil_sha256_finish(&sha256_ctx, hash);
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900272 bootutil_sha256_drop(&sha256_ctx);
David Vincze03368b82020-04-01 12:53:53 +0200273
274 rc = boot_retrieve_public_key_hash(image_index, key_hash, &key_hash_size);
275 if (rc) {
276 return rc;
277 }
278
Raef Colese8fe6cf2020-05-26 13:07:40 +0100279 /* Adding hardening to avoid this potential attack:
280 * - Image is signed with an arbitrary key and the corresponding public
281 * key is added as a TLV field.
282 * - During public key validation (comparing against key-hash read from
283 * HW) a fault is injected to accept the public key as valid one.
284 */
285 FIH_CALL(boot_fih_memequal, fih_rc, hash, key_hash, key_hash_size);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300286 if (FIH_TRUE == fih_eq(fih_rc, FIH_SUCCESS)) {
David Vincze03368b82020-04-01 12:53:53 +0200287 bootutil_keys[0].key = key;
288 pub_key_len = key_len;
289 return 0;
290 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100291
David Vincze03368b82020-04-01 12:53:53 +0200292 return -1;
293}
294#endif /* !MCUBOOT_HW_KEY */
David Brown43cda332017-09-01 09:53:23 -0600295#endif
296
David Vinczec3084132020-02-18 14:50:47 +0100297#ifdef MCUBOOT_HW_ROLLBACK_PROT
298/**
299 * Reads the value of an image's security counter.
300 *
301 * @param hdr Pointer to the image header structure.
302 * @param fap Pointer to a description structure of the image's
303 * flash area.
304 * @param security_cnt Pointer to store the security counter value.
305 *
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300306 * @return FIH_SUCCESS on success; FIH_FAILURE on failure.
David Vinczec3084132020-02-18 14:50:47 +0100307 */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300308fih_int
David Vinczec3084132020-02-18 14:50:47 +0100309bootutil_get_img_security_cnt(struct image_header *hdr,
310 const struct flash_area *fap,
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300311 fih_uint *img_security_cnt)
David Vinczec3084132020-02-18 14:50:47 +0100312{
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300313 uint32_t img_sec_cnt = 0u;
314 uint32_t img_chk_cnt = 0u;
315 struct image_tlv_iter it = {0};
316 uint32_t off = 0u;
317 uint16_t len = 0u;
318 int32_t rc = -1;
319 fih_int fih_rc = FIH_FAILURE;
David Vinczec3084132020-02-18 14:50:47 +0100320
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300321 if ((NULL == hdr) ||
322 (NULL == fap) ||
323 (NULL == img_security_cnt)) {
David Vinczec3084132020-02-18 14:50:47 +0100324 /* Invalid parameter. */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300325 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100326 }
327
328 /* The security counter TLV is in the protected part of the TLV area. */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300329 if (0u == hdr->ih_protect_tlv_size) {
330 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100331 }
332
333 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_SEC_CNT, true);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300334 if (rc != 0) {
335 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100336 }
337
338 /* Traverse through the protected TLV area to find
339 * the security counter TLV.
340 */
341
342 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
343 if (rc != 0) {
344 /* Security counter TLV has not been found. */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300345 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100346 }
347
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300348 if (len != sizeof(img_sec_cnt)) {
David Vinczec3084132020-02-18 14:50:47 +0100349 /* Security counter is not valid. */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300350 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100351 }
352
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300353 rc = LOAD_IMAGE_DATA(hdr, fap, off, &img_sec_cnt, len);
David Vinczec3084132020-02-18 14:50:47 +0100354 if (rc != 0) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300355 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100356 }
357
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300358 *img_security_cnt = fih_uint_encode(img_sec_cnt);
359
360 rc = LOAD_IMAGE_DATA(hdr, fap, off, &img_chk_cnt, len);
361 if (rc != 0) {
362 goto out;
363 }
364
365 if (FIH_TRUE == fih_uint_eq(fih_uint_encode(img_chk_cnt),
366 *img_security_cnt)) {
367
368 if (img_sec_cnt == img_chk_cnt) {
369 fih_rc = FIH_SUCCESS;
370 }
371 }
372
373out:
374 FIH_RET(fih_rc);
David Vinczec3084132020-02-18 14:50:47 +0100375}
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300376#ifdef CYW20829
377/**
378 * Reads the content of an image's reprovisioning packet.
379 *
380 * @param hdr Pointer to the image header structure.
381 * @param fap Pointer to a description structure of the image's
382 * flash area.
383 * @param reprov_packet Pointer to store the reprovisioning packet.
384 *
385 * @return 0 on success; nonzero on failure.
386 */
387int32_t
388bootutil_get_img_reprov_packet(struct image_header *hdr,
389 const struct flash_area *fap,
390 uint8_t *reprov_packet)
391{
392 struct image_tlv_iter it;
393 uint32_t off;
394 uint16_t len;
395 int32_t rc;
396
397 if ((hdr == NULL) ||
398 (fap == NULL) ||
399 (reprov_packet == NULL)) {
400 /* Invalid parameter. */
401 return BOOT_EBADARGS;
402 }
403
404 /* The reprovisioning packet TLV is in the protected part of the TLV area. */
405 if (hdr->ih_protect_tlv_size == 0) {
406 return BOOT_EBADIMAGE;
407 }
408
409 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_PROV_PACK, true);
410 if (rc) {
411 return rc;
412 }
413
414 /* Traverse through the protected TLV area to find
415 * the reprovisioning apcket TLV.
416 */
417
418 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
419 if (rc != 0) {
420 /* Reprovisioning packet TLV has not been found. */
421 return -1;
422 }
423
424 if (len != REPROV_PACK_SIZE) {
425 /* Reprovisioning packet is not valid. */
426 return BOOT_EBADIMAGE;
427 }
428
429 rc = LOAD_IMAGE_DATA(hdr, fap, off, reprov_packet, len);
430 if (rc != 0) {
431 return BOOT_EFLASH;
432 }
433
434 return 0;
435}
436#endif /* CYW20289 */
David Vinczec3084132020-02-18 14:50:47 +0100437#endif /* MCUBOOT_HW_ROLLBACK_PROT */
438
David Brown43cda332017-09-01 09:53:23 -0600439/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800440 * Verify the integrity of the image.
441 * Return non-zero if image could not be validated/does not validate.
442 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100443fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300444bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
445 struct image_header *hdr, const struct flash_area *fap,
446 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *seed,
447 int seed_len, uint8_t *out_hash)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800448{
449 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300450 uint16_t len;
David Brownd13318a2019-12-04 17:28:40 -0700451 uint16_t type;
David Brown43cda332017-09-01 09:53:23 -0600452 int sha256_valid = 0;
453#ifdef EXPECTED_SIG_TLV
Raef Colese8fe6cf2020-05-26 13:07:40 +0100454 fih_int valid_signature = FIH_FAILURE;
David Brown43cda332017-09-01 09:53:23 -0600455 int key_id = -1;
David Vincze03368b82020-04-01 12:53:53 +0200456#ifdef MCUBOOT_HW_KEY
457 /* Few extra bytes for encoding and for public exponent. */
458 uint8_t key_buf[SIG_BUF_SIZE + 24];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800459#endif
David Vincze03368b82020-04-01 12:53:53 +0200460#endif /* EXPECTED_SIG_TLV */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300461 struct image_tlv_iter it;
Fabio Utzig3501c012019-05-13 15:07:25 -0700462 uint8_t buf[SIG_BUF_SIZE];
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300463 uint8_t hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100464 int rc = 0;
465 fih_int fih_rc = FIH_FAILURE;
David Vinczec3084132020-02-18 14:50:47 +0100466#ifdef MCUBOOT_HW_ROLLBACK_PROT
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300467 fih_uint security_cnt = FIH_UINT_MAX;
David Vinczec3084132020-02-18 14:50:47 +0100468 uint32_t img_security_cnt = 0;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300469 uint8_t reprov_packet[REPROV_PACK_SIZE];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100470 fih_int security_counter_valid = FIH_FAILURE;
Roman Okhrimenko25165622023-05-23 08:58:29 +0300471 #ifdef CYW20829
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300472 fih_uint extracted_img_cnt = FIH_UINT_MAX;
Roman Okhrimenko25165622023-05-23 08:58:29 +0300473 #endif /* CYW20829 */
474#endif /* MCUBOOT_HW_ROLLBACK_PROT */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800475
Fabio Utzig10ee6482019-08-01 12:04:52 -0300476 rc = bootutil_img_hash(enc_state, image_index, hdr, fap, tmp_buf,
477 tmp_buf_sz, hash, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800478 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100479 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800480 }
481
482 if (out_hash) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300483 (void)memcpy(out_hash, hash, BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800484 }
485
Fabio Utzig61fd8882019-09-14 20:00:20 -0300486 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false);
David Brownf5b33d82017-09-01 10:58:27 -0600487 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100488 goto out;
David Brownf5b33d82017-09-01 10:58:27 -0600489 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800490
David Brown43cda332017-09-01 09:53:23 -0600491 /*
492 * Traverse through all of the TLVs, performing any checks we know
493 * and are able to do.
494 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300495 while (true) {
496 rc = bootutil_tlv_iter_next(&it, &off, &len, &type);
497 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100498 goto out;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300499 } else if (rc > 0) {
500 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800501 }
David Brown43cda332017-09-01 09:53:23 -0600502
Fabio Utzig61fd8882019-09-14 20:00:20 -0300503 if (type == IMAGE_TLV_SHA256) {
David Brown43cda332017-09-01 09:53:23 -0600504 /*
505 * Verify the SHA256 image hash. This must always be
506 * present.
507 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300508 if (len != sizeof(hash)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100509 rc = -1;
510 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800511 }
Tamas Banfe031092020-09-10 17:32:39 +0200512 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, sizeof(hash));
David Brown43cda332017-09-01 09:53:23 -0600513 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100514 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800515 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100516
517 FIH_CALL(boot_fih_memequal, fih_rc, hash, buf, sizeof(hash));
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300518 if (fih_eq(fih_rc, FIH_SUCCESS) != FIH_TRUE) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100519 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800520 }
David Brown43cda332017-09-01 09:53:23 -0600521
522 sha256_valid = 1;
523#ifdef EXPECTED_SIG_TLV
David Vincze03368b82020-04-01 12:53:53 +0200524#ifndef MCUBOOT_HW_KEY
Fabio Utzig61fd8882019-09-14 20:00:20 -0300525 } else if (type == IMAGE_TLV_KEYHASH) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800526 /*
David Brown43cda332017-09-01 09:53:23 -0600527 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800528 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300529 if (len != BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100530 rc = -1;
531 goto out;
David Brown43cda332017-09-01 09:53:23 -0600532 }
Tamas Banfe031092020-09-10 17:32:39 +0200533 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600534 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100535 goto out;
David Brown43cda332017-09-01 09:53:23 -0600536 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300537 key_id = bootutil_find_key(buf, len);
David Brown43cda332017-09-01 09:53:23 -0600538 /*
539 * The key may not be found, which is acceptable. There
540 * can be multiple signatures, each preceded by a key.
541 */
David Vincze03368b82020-04-01 12:53:53 +0200542#else
543 } else if (type == IMAGE_TLV_PUBKEY) {
544 /*
545 * Determine which key we should be checking.
546 */
547 if (len > sizeof(key_buf)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100548 rc = -1;
549 goto out;
David Vincze03368b82020-04-01 12:53:53 +0200550 }
Tamas Banfe031092020-09-10 17:32:39 +0200551 rc = LOAD_IMAGE_DATA(hdr, fap, off, key_buf, len);
David Vincze03368b82020-04-01 12:53:53 +0200552 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100553 goto out;
David Vincze03368b82020-04-01 12:53:53 +0200554 }
555 key_id = bootutil_find_key(image_index, key_buf, len);
556 /*
557 * The key may not be found, which is acceptable. There
558 * can be multiple signatures, each preceded by a key.
559 */
560#endif /* !MCUBOOT_HW_KEY */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300561 } else if (type == EXPECTED_SIG_TLV) {
David Brown43cda332017-09-01 09:53:23 -0600562 /* Ignore this signature if it is out of bounds. */
563 if (key_id < 0 || key_id >= bootutil_key_cnt) {
564 key_id = -1;
565 continue;
566 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300567 if (!EXPECTED_SIG_LEN(len) || len > sizeof(buf)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100568 rc = -1;
569 goto out;
David Brown43cda332017-09-01 09:53:23 -0600570 }
Tamas Banfe031092020-09-10 17:32:39 +0200571 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600572 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100573 goto out;
David Brown43cda332017-09-01 09:53:23 -0600574 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100575 FIH_CALL(bootutil_verify_sig, valid_signature, hash, sizeof(hash),
576 buf, len, key_id);
David Brown43cda332017-09-01 09:53:23 -0600577 key_id = -1;
David Vinczec3084132020-02-18 14:50:47 +0100578#endif /* EXPECTED_SIG_TLV */
579#ifdef MCUBOOT_HW_ROLLBACK_PROT
580 } else if (type == IMAGE_TLV_SEC_CNT) {
581 /*
582 * Verify the image's security counter.
583 * This must always be present.
584 */
585 if (len != sizeof(img_security_cnt)) {
586 /* Security counter is not valid. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100587 rc = -1;
588 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100589 }
590
Tamas Banfe031092020-09-10 17:32:39 +0200591 rc = LOAD_IMAGE_DATA(hdr, fap, off, &img_security_cnt, len);
David Vinczec3084132020-02-18 14:50:47 +0100592 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100593 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100594 }
595
Raef Colese8fe6cf2020-05-26 13:07:40 +0100596 FIH_CALL(boot_nv_security_counter_get, fih_rc, image_index,
597 &security_cnt);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300598 if (fih_eq(fih_rc, FIH_SUCCESS) != FIH_TRUE) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100599 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100600 }
601
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300602 BOOT_LOG_DBG("NV Counter read from efuse = %u", fih_uint_decode(security_cnt));
603
Roman Okhrimenko25165622023-05-23 08:58:29 +0300604#ifdef CYW20829
605 BOOT_LOG_DBG("Full NV Counter read from image = 0x%X", img_security_cnt);
606
607 FIH_CALL(platform_security_counter_check_extract, fih_rc,
608 (uint32_t)image_index, fih_uint_encode(img_security_cnt), &extracted_img_cnt);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300609
610 if (fih_eq(fih_rc, FIH_SUCCESS) != FIH_TRUE) {
Roman Okhrimenko25165622023-05-23 08:58:29 +0300611 /* The image's security counter exceeds registered value for this image */
612 goto out;
613 }
614
615 img_security_cnt = fih_uint_decode(extracted_img_cnt);
616#endif /*CYW20829*/
617
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300618 BOOT_LOG_DBG("NV Counter read from image = %" PRIu32, img_security_cnt);
619
David Vinczec3084132020-02-18 14:50:47 +0100620 /* Compare the new image's security counter value against the
621 * stored security counter value.
622 */
Roman Okhrimenko25165622023-05-23 08:58:29 +0300623 fih_rc = fih_int_encode_zero_equality( (int32_t)(img_security_cnt <
624 fih_uint_decode(security_cnt)) );
625
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300626 if (fih_eq(fih_rc, FIH_SUCCESS) != FIH_TRUE) {
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200627 /* The image's security counter is not accepted. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100628 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100629 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300630#ifndef CYW20829
David Vinczec3084132020-02-18 14:50:47 +0100631 /* The image's security counter has been successfully verified. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100632 security_counter_valid = fih_rc;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300633 }
634#else
635 /* The image's security counter has been successfully verified. */
636 security_counter_valid = fih_int_encode(HW_ROLLBACK_CNT_VALID);
637 } else if (type == IMAGE_TLV_PROV_PACK) {
638
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300639 if (FIH_TRUE == fih_eq(security_counter_valid, fih_int_encode(HW_ROLLBACK_CNT_VALID))) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300640 /*
641 * Verify the image reprovisioning packet.
642 * This must always be present.
643 */
644 BOOT_LOG_INF("Prov packet length 0x51 TLV = %" PRIu16, len);
645
646 if (len != sizeof(reprov_packet)) {
647 /* Re-provisioning packet is not valid. */
648 rc = -1;
649 goto out;
650 }
651
652 rc = LOAD_IMAGE_DATA(hdr, fap, off, reprov_packet, len);
653 if (rc) {
654 goto out;
655 }
656
657 security_counter_valid = fih_int_encode(fih_int_decode(security_counter_valid) | REPROV_PACK_VALID);
658 }
659 else{
660 goto out;
661 }
662#endif /* CYW20829 */
David Vinczec3084132020-02-18 14:50:47 +0100663#endif /* MCUBOOT_HW_ROLLBACK_PROT */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800664 }
665 }
David Brown43cda332017-09-01 09:53:23 -0600666
Raef Colese8fe6cf2020-05-26 13:07:40 +0100667 rc = !sha256_valid;
668 if (rc) {
669 goto out;
670 }
David Brown43cda332017-09-01 09:53:23 -0600671#ifdef EXPECTED_SIG_TLV
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300672 fih_rc = FIH_FAILURE;
673 if (FIH_TRUE == fih_eq(valid_signature, FIH_SUCCESS)) {
674 fih_rc = valid_signature;
675 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800676#endif
David Vinczec3084132020-02-18 14:50:47 +0100677#ifdef MCUBOOT_HW_ROLLBACK_PROT
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300678#ifdef CYW20829
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300679 if (fih_eq(security_counter_valid, fih_int_encode(REPROV_PACK_VALID | HW_ROLLBACK_CNT_VALID)) != FIH_TRUE) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300680 BOOT_LOG_DBG("Reprovisioning packet TLV 0x51 is not present image = %d", image_index);
681#else
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300682 if (fih_eq(security_counter_valid, FIH_SUCCESS) != FIH_TRUE) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300683#endif /* CYW20829 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100684 rc = -1;
685 goto out;
686 }
David Vinczec3084132020-02-18 14:50:47 +0100687#endif
Raef Colese8fe6cf2020-05-26 13:07:40 +0100688
689out:
690 if (rc) {
691 fih_rc = fih_int_encode(rc);
David Vinczec3084132020-02-18 14:50:47 +0100692 }
David Brown43cda332017-09-01 09:53:23 -0600693
Raef Colese8fe6cf2020-05-26 13:07:40 +0100694 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800695}