blob: d4cbadee61304863cec46ff893c0fc31cba2f5fd [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)
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200129 if (IS_RAM_BOOTABLE(hdr) && IS_RAM_BOOT_STAGE())
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300130#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
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200230/* Complex result masks for bootutil_img_validate() */
231#define SET_MASK_IMAGE_TLV_SHA256 ((signed)0x0000002E)
232fih_int FIH_MASK_IMAGE_TLV_SHA256 = FIH_INT_INIT(
233 SET_MASK_IMAGE_TLV_SHA256);
234
235#define SET_MASK_SIG_TLV_EXPECTED ((signed)0x00007400)
236fih_int FIH_MASK_SIG_TLV_EXPECTED = FIH_INT_INIT(
237 SET_MASK_SIG_TLV_EXPECTED);
238
239#define SET_MASK_IMAGE_TLV_SEC_CNT ((signed)0x005C0000)
240fih_int FIH_MASK_IMAGE_TLV_SEC_CNT = FIH_INT_INIT(
241 SET_MASK_IMAGE_TLV_SEC_CNT);
242
243#define CHK_MASK_IMAGE_TLV_SHA256 SET_MASK_IMAGE_TLV_SHA256
244
245#if defined MCUBOOT_SKIP_VALIDATE_SECONDARY_SLOT
246 #if defined MCUBOOT_VALIDATE_PRIMARY_SLOT
247 #error Boot slot validation cannot be enabled if upgrade slot validation is disabled
248 #endif
249#endif
250
251#if defined(MCUBOOT_SIGN_RSA) || \
252 defined(MCUBOOT_SIGN_EC) || \
253 defined(MCUBOOT_SIGN_EC256) || \
254 defined(MCUBOOT_SIGN_ED25519)
255
256 #if defined MCUBOOT_SKIP_VALIDATE_SECONDARY_SLOT
257 #define CHK_MASK_SIG_TLV_EXPECTED ((signed)0)
258 #else
259 #define CHK_MASK_SIG_TLV_EXPECTED SET_MASK_SIG_TLV_EXPECTED
260 #endif /* MCUBOOT_SKIP_VALIDATE_SECONDARY_SLOT */
261#else
262 #define CHK_MASK_SIG_TLV_EXPECTED ((signed)0)
263#endif /* defined(MCUBOOT_SIGN_RSA) ||
264 defined(MCUBOOT_SIGN_EC) ||
265 defined(MCUBOOT_SIGN_EC256) ||
266 defined(MCUBOOT_SIGN_ED25519) */
267
268#ifdef MCUBOOT_HW_ROLLBACK_PROT
269 #define CHK_MASK_IMAGE_TLV_SEC_CNT SET_MASK_IMAGE_TLV_SEC_CNT
270#else
271 #define CHK_MASK_IMAGE_TLV_SEC_CNT ((signed)0)
272#endif /* MCUBOOT_HW_ROLLBACK_PROT */
273
274fih_int FIH_IMG_VALIDATE_COMPLEX_OK = FIH_INT_INIT( \
275 CHK_MASK_IMAGE_TLV_SHA256 | \
276 CHK_MASK_SIG_TLV_EXPECTED | \
277 CHK_MASK_IMAGE_TLV_SEC_CNT);
278
279#undef SET_MASK_IMAGE_TLV_SHA256
280#undef SET_MASK_SIG_TLV_EXPECTED
281#undef SET_MASK_IMAGE_TLV_SEC_CNT
282
283#undef CHK_MASK_IMAGE_TLV_SHA256
284#undef CHK_MASK_SIG_TLV_EXPECTED
285#undef CHK_MASK_IMAGE_TLV_SEC_CNT
286
David Brown43cda332017-09-01 09:53:23 -0600287#ifdef EXPECTED_SIG_TLV
David Vincze03368b82020-04-01 12:53:53 +0200288#if !defined(MCUBOOT_HW_KEY)
David Brown43cda332017-09-01 09:53:23 -0600289static int
290bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
291{
292 bootutil_sha256_context sha256_ctx;
293 int i;
294 const struct bootutil_key *key;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300295 uint8_t hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
David Brown43cda332017-09-01 09:53:23 -0600296
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300297 if (keyhash_len != BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE) {
Fabio Utzig15c14672019-08-09 07:45:20 -0300298 return -1;
299 }
David Brown43cda332017-09-01 09:53:23 -0600300
301 for (i = 0; i < bootutil_key_cnt; i++) {
302 key = &bootutil_keys[i];
303 bootutil_sha256_init(&sha256_ctx);
304 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
305 bootutil_sha256_finish(&sha256_ctx, hash);
306 if (!memcmp(hash, keyhash, keyhash_len)) {
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900307 bootutil_sha256_drop(&sha256_ctx);
David Brown43cda332017-09-01 09:53:23 -0600308 return i;
309 }
310 }
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900311 bootutil_sha256_drop(&sha256_ctx);
David Brown43cda332017-09-01 09:53:23 -0600312 return -1;
313}
David Vincze03368b82020-04-01 12:53:53 +0200314#else
315extern unsigned int pub_key_len;
316static int
317bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
318{
319 bootutil_sha256_context sha256_ctx;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300320 uint8_t hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
321 uint8_t key_hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
David Vincze03368b82020-04-01 12:53:53 +0200322 size_t key_hash_size = sizeof(key_hash);
323 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100324 fih_int fih_rc;
David Vincze03368b82020-04-01 12:53:53 +0200325
326 bootutil_sha256_init(&sha256_ctx);
327 bootutil_sha256_update(&sha256_ctx, key, key_len);
328 bootutil_sha256_finish(&sha256_ctx, hash);
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900329 bootutil_sha256_drop(&sha256_ctx);
David Vincze03368b82020-04-01 12:53:53 +0200330
331 rc = boot_retrieve_public_key_hash(image_index, key_hash, &key_hash_size);
332 if (rc) {
333 return rc;
334 }
335
Raef Colese8fe6cf2020-05-26 13:07:40 +0100336 /* Adding hardening to avoid this potential attack:
337 * - Image is signed with an arbitrary key and the corresponding public
338 * key is added as a TLV field.
339 * - During public key validation (comparing against key-hash read from
340 * HW) a fault is injected to accept the public key as valid one.
341 */
342 FIH_CALL(boot_fih_memequal, fih_rc, hash, key_hash, key_hash_size);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300343 if (FIH_TRUE == fih_eq(fih_rc, FIH_SUCCESS)) {
David Vincze03368b82020-04-01 12:53:53 +0200344 bootutil_keys[0].key = key;
345 pub_key_len = key_len;
346 return 0;
347 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100348
David Vincze03368b82020-04-01 12:53:53 +0200349 return -1;
350}
351#endif /* !MCUBOOT_HW_KEY */
David Brown43cda332017-09-01 09:53:23 -0600352#endif
353
David Vinczec3084132020-02-18 14:50:47 +0100354#ifdef MCUBOOT_HW_ROLLBACK_PROT
355/**
356 * Reads the value of an image's security counter.
357 *
358 * @param hdr Pointer to the image header structure.
359 * @param fap Pointer to a description structure of the image's
360 * flash area.
361 * @param security_cnt Pointer to store the security counter value.
362 *
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300363 * @return FIH_SUCCESS on success; FIH_FAILURE on failure.
David Vinczec3084132020-02-18 14:50:47 +0100364 */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300365fih_int
David Vinczec3084132020-02-18 14:50:47 +0100366bootutil_get_img_security_cnt(struct image_header *hdr,
367 const struct flash_area *fap,
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300368 fih_uint *img_security_cnt)
David Vinczec3084132020-02-18 14:50:47 +0100369{
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300370 uint32_t img_sec_cnt = 0u;
371 uint32_t img_chk_cnt = 0u;
372 struct image_tlv_iter it = {0};
373 uint32_t off = 0u;
374 uint16_t len = 0u;
375 int32_t rc = -1;
376 fih_int fih_rc = FIH_FAILURE;
David Vinczec3084132020-02-18 14:50:47 +0100377
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300378 if ((NULL == hdr) ||
379 (NULL == fap) ||
380 (NULL == img_security_cnt)) {
David Vinczec3084132020-02-18 14:50:47 +0100381 /* Invalid parameter. */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300382 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100383 }
384
385 /* The security counter TLV is in the protected part of the TLV area. */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300386 if (0u == hdr->ih_protect_tlv_size) {
387 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100388 }
389
390 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_SEC_CNT, true);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300391 if (rc != 0) {
392 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100393 }
394
395 /* Traverse through the protected TLV area to find
396 * the security counter TLV.
397 */
398
399 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
400 if (rc != 0) {
401 /* Security counter TLV has not been found. */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300402 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100403 }
404
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300405 if (len != sizeof(img_sec_cnt)) {
David Vinczec3084132020-02-18 14:50:47 +0100406 /* Security counter is not valid. */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300407 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100408 }
409
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300410 rc = LOAD_IMAGE_DATA(hdr, fap, off, &img_sec_cnt, len);
David Vinczec3084132020-02-18 14:50:47 +0100411 if (rc != 0) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300412 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100413 }
414
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300415 *img_security_cnt = fih_uint_encode(img_sec_cnt);
416
417 rc = LOAD_IMAGE_DATA(hdr, fap, off, &img_chk_cnt, len);
418 if (rc != 0) {
419 goto out;
420 }
421
422 if (FIH_TRUE == fih_uint_eq(fih_uint_encode(img_chk_cnt),
423 *img_security_cnt)) {
424
425 if (img_sec_cnt == img_chk_cnt) {
426 fih_rc = FIH_SUCCESS;
427 }
428 }
429
430out:
431 FIH_RET(fih_rc);
David Vinczec3084132020-02-18 14:50:47 +0100432}
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300433#ifdef CYW20829
434/**
435 * Reads the content of an image's reprovisioning packet.
436 *
437 * @param hdr Pointer to the image header structure.
438 * @param fap Pointer to a description structure of the image's
439 * flash area.
440 * @param reprov_packet Pointer to store the reprovisioning packet.
441 *
442 * @return 0 on success; nonzero on failure.
443 */
444int32_t
445bootutil_get_img_reprov_packet(struct image_header *hdr,
446 const struct flash_area *fap,
447 uint8_t *reprov_packet)
448{
449 struct image_tlv_iter it;
450 uint32_t off;
451 uint16_t len;
452 int32_t rc;
453
454 if ((hdr == NULL) ||
455 (fap == NULL) ||
456 (reprov_packet == NULL)) {
457 /* Invalid parameter. */
458 return BOOT_EBADARGS;
459 }
460
461 /* The reprovisioning packet TLV is in the protected part of the TLV area. */
462 if (hdr->ih_protect_tlv_size == 0) {
463 return BOOT_EBADIMAGE;
464 }
465
466 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_PROV_PACK, true);
467 if (rc) {
468 return rc;
469 }
470
471 /* Traverse through the protected TLV area to find
472 * the reprovisioning apcket TLV.
473 */
474
475 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
476 if (rc != 0) {
477 /* Reprovisioning packet TLV has not been found. */
478 return -1;
479 }
480
481 if (len != REPROV_PACK_SIZE) {
482 /* Reprovisioning packet is not valid. */
483 return BOOT_EBADIMAGE;
484 }
485
486 rc = LOAD_IMAGE_DATA(hdr, fap, off, reprov_packet, len);
487 if (rc != 0) {
488 return BOOT_EFLASH;
489 }
490
491 return 0;
492}
493#endif /* CYW20289 */
David Vinczec3084132020-02-18 14:50:47 +0100494#endif /* MCUBOOT_HW_ROLLBACK_PROT */
495
David Brown43cda332017-09-01 09:53:23 -0600496/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800497 * Verify the integrity of the image.
498 * Return non-zero if image could not be validated/does not validate.
499 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100500fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300501bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
502 struct image_header *hdr, const struct flash_area *fap,
503 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *seed,
504 int seed_len, uint8_t *out_hash)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800505{
506 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300507 uint16_t len;
David Brownd13318a2019-12-04 17:28:40 -0700508 uint16_t type;
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200509
David Brown43cda332017-09-01 09:53:23 -0600510#ifdef EXPECTED_SIG_TLV
Raef Colese8fe6cf2020-05-26 13:07:40 +0100511 fih_int valid_signature = FIH_FAILURE;
David Brown43cda332017-09-01 09:53:23 -0600512 int key_id = -1;
David Vincze03368b82020-04-01 12:53:53 +0200513#ifdef MCUBOOT_HW_KEY
514 /* Few extra bytes for encoding and for public exponent. */
515 uint8_t key_buf[SIG_BUF_SIZE + 24];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800516#endif
David Vincze03368b82020-04-01 12:53:53 +0200517#endif /* EXPECTED_SIG_TLV */
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200518
Fabio Utzig61fd8882019-09-14 20:00:20 -0300519 struct image_tlv_iter it;
Fabio Utzig3501c012019-05-13 15:07:25 -0700520 uint8_t buf[SIG_BUF_SIZE];
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300521 uint8_t hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100522 int rc = 0;
523 fih_int fih_rc = FIH_FAILURE;
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200524 /* fih_complex_result stores patterns of successful execution
525 * of required checks
526 */
527 fih_int fih_complex_result = FIH_INT_ZERO;
528
David Vinczec3084132020-02-18 14:50:47 +0100529#ifdef MCUBOOT_HW_ROLLBACK_PROT
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300530 fih_uint security_cnt = FIH_UINT_MAX;
David Vinczec3084132020-02-18 14:50:47 +0100531 uint32_t img_security_cnt = 0;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300532 uint8_t reprov_packet[REPROV_PACK_SIZE];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100533 fih_int security_counter_valid = FIH_FAILURE;
Roman Okhrimenko25165622023-05-23 08:58:29 +0300534 #ifdef CYW20829
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300535 fih_uint extracted_img_cnt = FIH_UINT_MAX;
Roman Okhrimenko25165622023-05-23 08:58:29 +0300536 #endif /* CYW20829 */
537#endif /* MCUBOOT_HW_ROLLBACK_PROT */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800538
Fabio Utzig10ee6482019-08-01 12:04:52 -0300539 rc = bootutil_img_hash(enc_state, image_index, hdr, fap, tmp_buf,
540 tmp_buf_sz, hash, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800541 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100542 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800543 }
544
545 if (out_hash) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300546 (void)memcpy(out_hash, hash, BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800547 }
548
Fabio Utzig61fd8882019-09-14 20:00:20 -0300549 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false);
David Brownf5b33d82017-09-01 10:58:27 -0600550 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100551 goto out;
David Brownf5b33d82017-09-01 10:58:27 -0600552 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800553
David Brown43cda332017-09-01 09:53:23 -0600554 /*
555 * Traverse through all of the TLVs, performing any checks we know
556 * and are able to do.
557 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300558 while (true) {
559 rc = bootutil_tlv_iter_next(&it, &off, &len, &type);
560 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100561 goto out;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300562 } else if (rc > 0) {
563 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800564 }
David Brown43cda332017-09-01 09:53:23 -0600565
Fabio Utzig61fd8882019-09-14 20:00:20 -0300566 if (type == IMAGE_TLV_SHA256) {
David Brown43cda332017-09-01 09:53:23 -0600567 /*
568 * Verify the SHA256 image hash. This must always be
569 * present.
570 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300571 if (len != sizeof(hash)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100572 rc = -1;
573 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800574 }
Tamas Banfe031092020-09-10 17:32:39 +0200575 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, sizeof(hash));
David Brown43cda332017-09-01 09:53:23 -0600576 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100577 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800578 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100579
580 FIH_CALL(boot_fih_memequal, fih_rc, hash, buf, sizeof(hash));
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200581
582 if (fih_eq(fih_rc, FIH_SUCCESS) == FIH_TRUE) {
583 /* Encode succesful completion pattern to complex_result */
584 fih_complex_result = fih_or(fih_complex_result,
585 FIH_MASK_IMAGE_TLV_SHA256);
586 }
587 else {
588 BOOT_LOG_DBG("IMAGE_TLV_SHA256 is invalid");
589 rc = -1;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100590 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800591 }
David Brown43cda332017-09-01 09:53:23 -0600592
David Brown43cda332017-09-01 09:53:23 -0600593#ifdef EXPECTED_SIG_TLV
David Vincze03368b82020-04-01 12:53:53 +0200594#ifndef MCUBOOT_HW_KEY
Fabio Utzig61fd8882019-09-14 20:00:20 -0300595 } else if (type == IMAGE_TLV_KEYHASH) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800596 /*
David Brown43cda332017-09-01 09:53:23 -0600597 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800598 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300599 if (len != BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100600 rc = -1;
601 goto out;
David Brown43cda332017-09-01 09:53:23 -0600602 }
Tamas Banfe031092020-09-10 17:32:39 +0200603 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600604 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100605 goto out;
David Brown43cda332017-09-01 09:53:23 -0600606 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300607 key_id = bootutil_find_key(buf, len);
David Brown43cda332017-09-01 09:53:23 -0600608 /*
609 * The key may not be found, which is acceptable. There
610 * can be multiple signatures, each preceded by a key.
611 */
David Vincze03368b82020-04-01 12:53:53 +0200612#else
613 } else if (type == IMAGE_TLV_PUBKEY) {
614 /*
615 * Determine which key we should be checking.
616 */
617 if (len > sizeof(key_buf)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100618 rc = -1;
619 goto out;
David Vincze03368b82020-04-01 12:53:53 +0200620 }
Tamas Banfe031092020-09-10 17:32:39 +0200621 rc = LOAD_IMAGE_DATA(hdr, fap, off, key_buf, len);
David Vincze03368b82020-04-01 12:53:53 +0200622 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100623 goto out;
David Vincze03368b82020-04-01 12:53:53 +0200624 }
625 key_id = bootutil_find_key(image_index, key_buf, len);
626 /*
627 * The key may not be found, which is acceptable. There
628 * can be multiple signatures, each preceded by a key.
629 */
630#endif /* !MCUBOOT_HW_KEY */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300631 } else if (type == EXPECTED_SIG_TLV) {
David Brown43cda332017-09-01 09:53:23 -0600632 /* Ignore this signature if it is out of bounds. */
633 if (key_id < 0 || key_id >= bootutil_key_cnt) {
634 key_id = -1;
635 continue;
636 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300637 if (!EXPECTED_SIG_LEN(len) || len > sizeof(buf)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100638 rc = -1;
639 goto out;
David Brown43cda332017-09-01 09:53:23 -0600640 }
Tamas Banfe031092020-09-10 17:32:39 +0200641 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600642 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100643 goto out;
David Brown43cda332017-09-01 09:53:23 -0600644 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100645 FIH_CALL(bootutil_verify_sig, valid_signature, hash, sizeof(hash),
646 buf, len, key_id);
David Brown43cda332017-09-01 09:53:23 -0600647 key_id = -1;
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200648
649 if (FIH_TRUE == fih_eq(FIH_SUCCESS, valid_signature)) {
650 /* Encode succesful completion pattern to complex_result */
651 fih_complex_result = fih_or(fih_complex_result,
652 FIH_MASK_SIG_TLV_EXPECTED);
653 } else {
654 BOOT_LOG_DBG("Invalid signature of bootable image %d",
655 image_index);
656 rc = -1;
657 goto out;
658 }
659
David Vinczec3084132020-02-18 14:50:47 +0100660#endif /* EXPECTED_SIG_TLV */
661#ifdef MCUBOOT_HW_ROLLBACK_PROT
662 } else if (type == IMAGE_TLV_SEC_CNT) {
663 /*
664 * Verify the image's security counter.
665 * This must always be present.
666 */
667 if (len != sizeof(img_security_cnt)) {
668 /* Security counter is not valid. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100669 rc = -1;
670 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100671 }
672
Tamas Banfe031092020-09-10 17:32:39 +0200673 rc = LOAD_IMAGE_DATA(hdr, fap, off, &img_security_cnt, len);
David Vinczec3084132020-02-18 14:50:47 +0100674 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100675 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100676 }
677
Raef Colese8fe6cf2020-05-26 13:07:40 +0100678 FIH_CALL(boot_nv_security_counter_get, fih_rc, image_index,
679 &security_cnt);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300680 if (fih_eq(fih_rc, FIH_SUCCESS) != FIH_TRUE) {
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200681 rc = -1;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100682 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100683 }
684
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300685 BOOT_LOG_DBG("NV Counter read from efuse = %u", fih_uint_decode(security_cnt));
686
Roman Okhrimenko25165622023-05-23 08:58:29 +0300687#ifdef CYW20829
688 BOOT_LOG_DBG("Full NV Counter read from image = 0x%X", img_security_cnt);
689
690 FIH_CALL(platform_security_counter_check_extract, fih_rc,
691 (uint32_t)image_index, fih_uint_encode(img_security_cnt), &extracted_img_cnt);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300692
693 if (fih_eq(fih_rc, FIH_SUCCESS) != FIH_TRUE) {
Roman Okhrimenko25165622023-05-23 08:58:29 +0300694 /* The image's security counter exceeds registered value for this image */
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200695 rc = -1;
Roman Okhrimenko25165622023-05-23 08:58:29 +0300696 goto out;
697 }
698
699 img_security_cnt = fih_uint_decode(extracted_img_cnt);
700#endif /*CYW20829*/
701
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300702 BOOT_LOG_DBG("NV Counter read from image = %" PRIu32, img_security_cnt);
703
David Vinczec3084132020-02-18 14:50:47 +0100704 /* Compare the new image's security counter value against the
705 * stored security counter value.
706 */
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200707 if (FIH_TRUE == fih_uint_ge(fih_uint_encode(img_security_cnt), security_cnt)) {
708 /* Encode succesful completion pattern to complex_result */
709 fih_complex_result = fih_or(fih_complex_result,
710 FIH_MASK_IMAGE_TLV_SEC_CNT);
711#ifdef CYW20829
712 /* The image's security counter has been successfully verified. */
713 security_counter_valid = fih_int_encode(HW_ROLLBACK_CNT_VALID);
714#endif
715 } else {
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200716 /* The image's security counter is not accepted. */
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200717 rc = -1;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100718 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100719 }
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200720
721#ifdef CYW20829
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300722 } else if (type == IMAGE_TLV_PROV_PACK) {
723
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300724 if (FIH_TRUE == fih_eq(security_counter_valid, fih_int_encode(HW_ROLLBACK_CNT_VALID))) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300725 /*
726 * Verify the image reprovisioning packet.
727 * This must always be present.
728 */
729 BOOT_LOG_INF("Prov packet length 0x51 TLV = %" PRIu16, len);
730
731 if (len != sizeof(reprov_packet)) {
732 /* Re-provisioning packet is not valid. */
733 rc = -1;
734 goto out;
735 }
736
737 rc = LOAD_IMAGE_DATA(hdr, fap, off, reprov_packet, len);
738 if (rc) {
739 goto out;
740 }
741
742 security_counter_valid = fih_int_encode(fih_int_decode(security_counter_valid) | REPROV_PACK_VALID);
743 }
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200744 else {
745 rc = -1;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300746 goto out;
747 }
748#endif /* CYW20829 */
David Vinczec3084132020-02-18 14:50:47 +0100749#endif /* MCUBOOT_HW_ROLLBACK_PROT */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800750 }
751 }
David Brown43cda332017-09-01 09:53:23 -0600752
David Vinczec3084132020-02-18 14:50:47 +0100753#ifdef MCUBOOT_HW_ROLLBACK_PROT
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300754#ifdef CYW20829
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300755 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 +0300756 BOOT_LOG_DBG("Reprovisioning packet TLV 0x51 is not present image = %d", image_index);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100757 rc = -1;
758 goto out;
759 }
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200760#endif /* CYW20829 */
761#endif /* MCUBOOT_HW_ROLLBACK_PROT */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100762
763out:
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200764 if (rc < 0) {
765 fih_complex_result = FIH_FAILURE;
David Vinczec3084132020-02-18 14:50:47 +0100766 }
David Brown43cda332017-09-01 09:53:23 -0600767
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200768 FIH_RET(fih_complex_result);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800769}