blob: e7cff909f6bd954e6c3040e6cfaeba07e775310d [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
62/*
63 * Compute SHA256 over the image.
64 */
65static int
Fabio Utzig10ee6482019-08-01 12:04:52 -030066bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
67 struct image_header *hdr, const struct flash_area *fap,
68 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *hash_result,
69 uint8_t *seed, int seed_len)
Christopher Collins92ea77f2016-12-12 15:59:26 -080070{
David Browne629bf32017-04-24 13:37:33 -060071 bootutil_sha256_context sha256_ctx;
Christopher Collins92ea77f2016-12-12 15:59:26 -080072 uint32_t blk_sz;
73 uint32_t size;
Fabio Utzig2fc80df2018-12-14 06:47:38 -020074 uint16_t hdr_size;
Christopher Collins92ea77f2016-12-12 15:59:26 -080075 uint32_t off;
76 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -030077 uint32_t blk_off;
Fabio Utzige52c08e2019-09-11 19:32:00 -030078 uint32_t tlv_off;
Christopher Collins92ea77f2016-12-12 15:59:26 -080079
Tamas Banfe031092020-09-10 17:32:39 +020080#if (BOOT_IMAGE_NUMBER == 1) || !defined(MCUBOOT_ENC_IMAGES) || \
81 defined(MCUBOOT_RAM_LOAD)
Fabio Utzig10ee6482019-08-01 12:04:52 -030082 (void)enc_state;
Fabio Utzigb0f04732019-07-31 09:49:19 -030083 (void)image_index;
Fabio Utzigc9621352019-08-08 12:15:51 -030084 (void)hdr_size;
Fabio Utzige52c08e2019-09-11 19:32:00 -030085 (void)blk_off;
86 (void)tlv_off;
Tamas Banfe031092020-09-10 17:32:39 +020087#ifdef MCUBOOT_RAM_LOAD
88 (void)blk_sz;
89 (void)off;
90 (void)rc;
Roman Okhrimenko977b3752022-03-31 14:40:48 +030091 (void)fap;
92 (void)tmp_buf;
93 (void)tmp_buf_sz;
Tamas Banfe031092020-09-10 17:32:39 +020094#endif
Fabio Utzigb0f04732019-07-31 09:49:19 -030095#endif
96
Fabio Utzigbc077932019-08-26 11:16:34 -030097#ifdef MCUBOOT_ENC_IMAGES
98 /* Encrypted images only exist in the secondary slot */
99 if (MUST_DECRYPT(fap, image_index, hdr) &&
100 !boot_enc_valid(enc_state, image_index, fap)) {
101 return -1;
102 }
103#endif
104
David Browne629bf32017-04-24 13:37:33 -0600105 bootutil_sha256_init(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800106
107 /* in some cases (split image) the hash is seeded with data from
108 * the loader image */
Fabio Utzig53986042017-12-12 14:57:07 -0200109 if (seed && (seed_len > 0)) {
David Browne629bf32017-04-24 13:37:33 -0600110 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800111 }
112
David Vinczee32483f2019-06-13 10:46:24 +0200113 /* Hash is computed over image header and image itself. */
Fabio Utzige52c08e2019-09-11 19:32:00 -0300114 size = hdr_size = hdr->ih_hdr_size;
115 size += hdr->ih_img_size;
116 tlv_off = size;
David Vinczee32483f2019-06-13 10:46:24 +0200117
Fabio Utzige52c08e2019-09-11 19:32:00 -0300118 /* If protected TLVs are present they are also hashed. */
119 size += hdr->ih_protect_tlv_size;
David Vinczee32483f2019-06-13 10:46:24 +0200120
Tamas Banfe031092020-09-10 17:32:39 +0200121#ifdef MCUBOOT_RAM_LOAD
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300122 bootutil_sha256_update(&sha256_ctx,
123 (void*)(IMAGE_RAM_BASE + hdr->ih_load_addr),
124 size);
Tamas Banfe031092020-09-10 17:32:39 +0200125#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800126 for (off = 0; off < size; off += blk_sz) {
127 blk_sz = size - off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300128 if (blk_sz > tmp_buf_sz) {
129 blk_sz = tmp_buf_sz;
130 }
Fabio Utzigc21c2102019-09-10 10:10:42 -0300131#ifdef MCUBOOT_ENC_IMAGES
132 /* The only data that is encrypted in an image is the payload;
133 * both header and TLVs (when protected) are not.
134 */
135 if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) {
136 /* read only the header */
137 blk_sz = hdr_size - off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300138 }
139 if ((off < tlv_off) && ((off + blk_sz) > tlv_off)) {
140 /* read only up to the end of the image payload */
141 blk_sz = tlv_off - off;
Fabio Utzigc21c2102019-09-10 10:10:42 -0300142 }
143#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800144 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
145 if (rc) {
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900146 bootutil_sha256_drop(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800147 return rc;
148 }
Fabio Utzigba829042018-09-18 08:29:34 -0300149#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzigc21c2102019-09-10 10:10:42 -0300150 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzige52c08e2019-09-11 19:32:00 -0300151 /* Only payload is encrypted (area between header and TLVs) */
152 if (off >= hdr_size && off < tlv_off) {
Fabio Utzigc21c2102019-09-10 10:10:42 -0300153 blk_off = (off - hdr_size) & 0xf;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300154#ifdef MCUBOOT_ENC_IMAGES_XIP
155 rc = bootutil_img_encrypt(enc_state, image_index, hdr, fap, off,
Fabio Utzigc21c2102019-09-10 10:10:42 -0300156 blk_sz, blk_off, tmp_buf);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300157#else
158 rc = boot_encrypt(enc_state, image_index, fap, off - hdr_size,
159 blk_sz, blk_off, tmp_buf);
160#endif
161 if (rc) {
162 return rc;
163 }
Fabio Utzigc21c2102019-09-10 10:10:42 -0300164 }
Fabio Utzigba829042018-09-18 08:29:34 -0300165 }
166#endif
David Browne629bf32017-04-24 13:37:33 -0600167 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800168 }
Tamas Banfe031092020-09-10 17:32:39 +0200169#endif /* MCUBOOT_RAM_LOAD */
David Browne629bf32017-04-24 13:37:33 -0600170 bootutil_sha256_finish(&sha256_ctx, hash_result);
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900171 bootutil_sha256_drop(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800172
173 return 0;
174}
175
176/*
David Brown43cda332017-09-01 09:53:23 -0600177 * Currently, we only support being able to verify one type of
178 * signature, because there is a single verification function that we
179 * call. List the type of TLV we are expecting. If we aren't
180 * configured for any signature, don't define this macro.
181 */
Fabio Utzig48764842019-05-10 19:28:24 -0300182#if (defined(MCUBOOT_SIGN_RSA) + \
183 defined(MCUBOOT_SIGN_EC) + \
184 defined(MCUBOOT_SIGN_EC256) + \
185 defined(MCUBOOT_SIGN_ED25519)) > 1
Fabio Utzig3501c012019-05-13 15:07:25 -0700186#error "Only a single signature type is supported!"
187#endif
188
David Brown43cda332017-09-01 09:53:23 -0600189#if defined(MCUBOOT_SIGN_RSA)
Fabio Utzig3501c012019-05-13 15:07:25 -0700190# if MCUBOOT_SIGN_RSA_LEN == 2048
191# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
192# elif MCUBOOT_SIGN_RSA_LEN == 3072
193# define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS
194# else
195# error "Unsupported RSA signature length"
David Brown43cda332017-09-01 09:53:23 -0600196# endif
Fabio Utzig3501c012019-05-13 15:07:25 -0700197# define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8)
198# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) /* 2048 bits */
David Brown43cda332017-09-01 09:53:23 -0600199#elif defined(MCUBOOT_SIGN_EC)
200# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224
Fabio Utzig3501c012019-05-13 15:07:25 -0700201# define SIG_BUF_SIZE 128
David Browna3608262019-12-12 15:35:31 -0700202# define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */
David Brown43cda332017-09-01 09:53:23 -0600203#elif defined(MCUBOOT_SIGN_EC256)
204# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
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 */
Fabio Utzig48764842019-05-10 19:28:24 -0300207#elif defined(MCUBOOT_SIGN_ED25519)
208# define EXPECTED_SIG_TLV IMAGE_TLV_ED25519
209# define SIG_BUF_SIZE 64
210# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE)
Fabio Utzig3501c012019-05-13 15:07:25 -0700211#else
212# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
David Brown43cda332017-09-01 09:53:23 -0600213#endif
214
215#ifdef EXPECTED_SIG_TLV
David Vincze03368b82020-04-01 12:53:53 +0200216#if !defined(MCUBOOT_HW_KEY)
David Brown43cda332017-09-01 09:53:23 -0600217static int
218bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
219{
220 bootutil_sha256_context sha256_ctx;
221 int i;
222 const struct bootutil_key *key;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300223 uint8_t hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
David Brown43cda332017-09-01 09:53:23 -0600224
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300225 if (keyhash_len != BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE) {
Fabio Utzig15c14672019-08-09 07:45:20 -0300226 return -1;
227 }
David Brown43cda332017-09-01 09:53:23 -0600228
229 for (i = 0; i < bootutil_key_cnt; i++) {
230 key = &bootutil_keys[i];
231 bootutil_sha256_init(&sha256_ctx);
232 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
233 bootutil_sha256_finish(&sha256_ctx, hash);
234 if (!memcmp(hash, keyhash, keyhash_len)) {
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900235 bootutil_sha256_drop(&sha256_ctx);
David Brown43cda332017-09-01 09:53:23 -0600236 return i;
237 }
238 }
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900239 bootutil_sha256_drop(&sha256_ctx);
David Brown43cda332017-09-01 09:53:23 -0600240 return -1;
241}
David Vincze03368b82020-04-01 12:53:53 +0200242#else
243extern unsigned int pub_key_len;
244static int
245bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
246{
247 bootutil_sha256_context sha256_ctx;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300248 uint8_t hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
249 uint8_t key_hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
David Vincze03368b82020-04-01 12:53:53 +0200250 size_t key_hash_size = sizeof(key_hash);
251 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100252 fih_int fih_rc;
David Vincze03368b82020-04-01 12:53:53 +0200253
254 bootutil_sha256_init(&sha256_ctx);
255 bootutil_sha256_update(&sha256_ctx, key, key_len);
256 bootutil_sha256_finish(&sha256_ctx, hash);
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900257 bootutil_sha256_drop(&sha256_ctx);
David Vincze03368b82020-04-01 12:53:53 +0200258
259 rc = boot_retrieve_public_key_hash(image_index, key_hash, &key_hash_size);
260 if (rc) {
261 return rc;
262 }
263
Raef Colese8fe6cf2020-05-26 13:07:40 +0100264 /* Adding hardening to avoid this potential attack:
265 * - Image is signed with an arbitrary key and the corresponding public
266 * key is added as a TLV field.
267 * - During public key validation (comparing against key-hash read from
268 * HW) a fault is injected to accept the public key as valid one.
269 */
270 FIH_CALL(boot_fih_memequal, fih_rc, hash, key_hash, key_hash_size);
271 if (fih_eq(fih_rc, FIH_SUCCESS)) {
David Vincze03368b82020-04-01 12:53:53 +0200272 bootutil_keys[0].key = key;
273 pub_key_len = key_len;
274 return 0;
275 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100276
David Vincze03368b82020-04-01 12:53:53 +0200277 return -1;
278}
279#endif /* !MCUBOOT_HW_KEY */
David Brown43cda332017-09-01 09:53:23 -0600280#endif
281
David Vinczec3084132020-02-18 14:50:47 +0100282#ifdef MCUBOOT_HW_ROLLBACK_PROT
283/**
284 * Reads the value of an image's security counter.
285 *
286 * @param hdr Pointer to the image header structure.
287 * @param fap Pointer to a description structure of the image's
288 * flash area.
289 * @param security_cnt Pointer to store the security counter value.
290 *
291 * @return 0 on success; nonzero on failure.
292 */
293int32_t
294bootutil_get_img_security_cnt(struct image_header *hdr,
295 const struct flash_area *fap,
296 uint32_t *img_security_cnt)
297{
298 struct image_tlv_iter it;
299 uint32_t off;
300 uint16_t len;
301 int32_t rc;
302
303 if ((hdr == NULL) ||
304 (fap == NULL) ||
305 (img_security_cnt == NULL)) {
306 /* Invalid parameter. */
307 return BOOT_EBADARGS;
308 }
309
310 /* The security counter TLV is in the protected part of the TLV area. */
311 if (hdr->ih_protect_tlv_size == 0) {
312 return BOOT_EBADIMAGE;
313 }
314
315 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_SEC_CNT, true);
316 if (rc) {
317 return rc;
318 }
319
320 /* Traverse through the protected TLV area to find
321 * the security counter TLV.
322 */
323
324 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
325 if (rc != 0) {
326 /* Security counter TLV has not been found. */
327 return -1;
328 }
329
330 if (len != sizeof(*img_security_cnt)) {
331 /* Security counter is not valid. */
332 return BOOT_EBADIMAGE;
333 }
334
Tamas Banfe031092020-09-10 17:32:39 +0200335 rc = LOAD_IMAGE_DATA(hdr, fap, off, img_security_cnt, len);
David Vinczec3084132020-02-18 14:50:47 +0100336 if (rc != 0) {
337 return BOOT_EFLASH;
338 }
339
340 return 0;
341}
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300342#ifdef CYW20829
343/**
344 * Reads the content of an image's reprovisioning packet.
345 *
346 * @param hdr Pointer to the image header structure.
347 * @param fap Pointer to a description structure of the image's
348 * flash area.
349 * @param reprov_packet Pointer to store the reprovisioning packet.
350 *
351 * @return 0 on success; nonzero on failure.
352 */
353int32_t
354bootutil_get_img_reprov_packet(struct image_header *hdr,
355 const struct flash_area *fap,
356 uint8_t *reprov_packet)
357{
358 struct image_tlv_iter it;
359 uint32_t off;
360 uint16_t len;
361 int32_t rc;
362
363 if ((hdr == NULL) ||
364 (fap == NULL) ||
365 (reprov_packet == NULL)) {
366 /* Invalid parameter. */
367 return BOOT_EBADARGS;
368 }
369
370 /* The reprovisioning packet TLV is in the protected part of the TLV area. */
371 if (hdr->ih_protect_tlv_size == 0) {
372 return BOOT_EBADIMAGE;
373 }
374
375 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_PROV_PACK, true);
376 if (rc) {
377 return rc;
378 }
379
380 /* Traverse through the protected TLV area to find
381 * the reprovisioning apcket TLV.
382 */
383
384 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
385 if (rc != 0) {
386 /* Reprovisioning packet TLV has not been found. */
387 return -1;
388 }
389
390 if (len != REPROV_PACK_SIZE) {
391 /* Reprovisioning packet is not valid. */
392 return BOOT_EBADIMAGE;
393 }
394
395 rc = LOAD_IMAGE_DATA(hdr, fap, off, reprov_packet, len);
396 if (rc != 0) {
397 return BOOT_EFLASH;
398 }
399
400 return 0;
401}
402#endif /* CYW20289 */
David Vinczec3084132020-02-18 14:50:47 +0100403#endif /* MCUBOOT_HW_ROLLBACK_PROT */
404
David Brown43cda332017-09-01 09:53:23 -0600405/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800406 * Verify the integrity of the image.
407 * Return non-zero if image could not be validated/does not validate.
408 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100409fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300410bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
411 struct image_header *hdr, const struct flash_area *fap,
412 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *seed,
413 int seed_len, uint8_t *out_hash)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800414{
415 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300416 uint16_t len;
David Brownd13318a2019-12-04 17:28:40 -0700417 uint16_t type;
David Brown43cda332017-09-01 09:53:23 -0600418 int sha256_valid = 0;
419#ifdef EXPECTED_SIG_TLV
Raef Colese8fe6cf2020-05-26 13:07:40 +0100420 fih_int valid_signature = FIH_FAILURE;
David Brown43cda332017-09-01 09:53:23 -0600421 int key_id = -1;
David Vincze03368b82020-04-01 12:53:53 +0200422#ifdef MCUBOOT_HW_KEY
423 /* Few extra bytes for encoding and for public exponent. */
424 uint8_t key_buf[SIG_BUF_SIZE + 24];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800425#endif
David Vincze03368b82020-04-01 12:53:53 +0200426#endif /* EXPECTED_SIG_TLV */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300427 struct image_tlv_iter it;
Fabio Utzig3501c012019-05-13 15:07:25 -0700428 uint8_t buf[SIG_BUF_SIZE];
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300429 uint8_t hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100430 int rc = 0;
431 fih_int fih_rc = FIH_FAILURE;
David Vinczec3084132020-02-18 14:50:47 +0100432#ifdef MCUBOOT_HW_ROLLBACK_PROT
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300433 fih_uint security_cnt = fih_uint_encode(UINT_MAX);
David Vinczec3084132020-02-18 14:50:47 +0100434 uint32_t img_security_cnt = 0;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300435 uint8_t reprov_packet[REPROV_PACK_SIZE];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100436 fih_int security_counter_valid = FIH_FAILURE;
David Vinczec3084132020-02-18 14:50:47 +0100437#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800438
Fabio Utzig10ee6482019-08-01 12:04:52 -0300439 rc = bootutil_img_hash(enc_state, image_index, hdr, fap, tmp_buf,
440 tmp_buf_sz, hash, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800441 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100442 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800443 }
444
445 if (out_hash) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300446 memcpy(out_hash, hash, BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800447 }
448
Fabio Utzig61fd8882019-09-14 20:00:20 -0300449 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false);
David Brownf5b33d82017-09-01 10:58:27 -0600450 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100451 goto out;
David Brownf5b33d82017-09-01 10:58:27 -0600452 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800453
David Brown43cda332017-09-01 09:53:23 -0600454 /*
455 * Traverse through all of the TLVs, performing any checks we know
456 * and are able to do.
457 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300458 while (true) {
459 rc = bootutil_tlv_iter_next(&it, &off, &len, &type);
460 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100461 goto out;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300462 } else if (rc > 0) {
463 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800464 }
David Brown43cda332017-09-01 09:53:23 -0600465
Fabio Utzig61fd8882019-09-14 20:00:20 -0300466 if (type == IMAGE_TLV_SHA256) {
David Brown43cda332017-09-01 09:53:23 -0600467 /*
468 * Verify the SHA256 image hash. This must always be
469 * present.
470 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300471 if (len != sizeof(hash)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100472 rc = -1;
473 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800474 }
Tamas Banfe031092020-09-10 17:32:39 +0200475 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, sizeof(hash));
David Brown43cda332017-09-01 09:53:23 -0600476 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100477 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800478 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100479
480 FIH_CALL(boot_fih_memequal, fih_rc, hash, buf, sizeof(hash));
481 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
482 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800483 }
David Brown43cda332017-09-01 09:53:23 -0600484
485 sha256_valid = 1;
486#ifdef EXPECTED_SIG_TLV
David Vincze03368b82020-04-01 12:53:53 +0200487#ifndef MCUBOOT_HW_KEY
Fabio Utzig61fd8882019-09-14 20:00:20 -0300488 } else if (type == IMAGE_TLV_KEYHASH) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800489 /*
David Brown43cda332017-09-01 09:53:23 -0600490 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800491 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300492 if (len != BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100493 rc = -1;
494 goto out;
David Brown43cda332017-09-01 09:53:23 -0600495 }
Tamas Banfe031092020-09-10 17:32:39 +0200496 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600497 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100498 goto out;
David Brown43cda332017-09-01 09:53:23 -0600499 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300500 key_id = bootutil_find_key(buf, len);
David Brown43cda332017-09-01 09:53:23 -0600501 /*
502 * The key may not be found, which is acceptable. There
503 * can be multiple signatures, each preceded by a key.
504 */
David Vincze03368b82020-04-01 12:53:53 +0200505#else
506 } else if (type == IMAGE_TLV_PUBKEY) {
507 /*
508 * Determine which key we should be checking.
509 */
510 if (len > sizeof(key_buf)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100511 rc = -1;
512 goto out;
David Vincze03368b82020-04-01 12:53:53 +0200513 }
Tamas Banfe031092020-09-10 17:32:39 +0200514 rc = LOAD_IMAGE_DATA(hdr, fap, off, key_buf, len);
David Vincze03368b82020-04-01 12:53:53 +0200515 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100516 goto out;
David Vincze03368b82020-04-01 12:53:53 +0200517 }
518 key_id = bootutil_find_key(image_index, key_buf, len);
519 /*
520 * The key may not be found, which is acceptable. There
521 * can be multiple signatures, each preceded by a key.
522 */
523#endif /* !MCUBOOT_HW_KEY */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300524 } else if (type == EXPECTED_SIG_TLV) {
David Brown43cda332017-09-01 09:53:23 -0600525 /* Ignore this signature if it is out of bounds. */
526 if (key_id < 0 || key_id >= bootutil_key_cnt) {
527 key_id = -1;
528 continue;
529 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300530 if (!EXPECTED_SIG_LEN(len) || len > sizeof(buf)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100531 rc = -1;
532 goto out;
David Brown43cda332017-09-01 09:53:23 -0600533 }
Tamas Banfe031092020-09-10 17:32:39 +0200534 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600535 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100536 goto out;
David Brown43cda332017-09-01 09:53:23 -0600537 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100538 FIH_CALL(bootutil_verify_sig, valid_signature, hash, sizeof(hash),
539 buf, len, key_id);
David Brown43cda332017-09-01 09:53:23 -0600540 key_id = -1;
David Vinczec3084132020-02-18 14:50:47 +0100541#endif /* EXPECTED_SIG_TLV */
542#ifdef MCUBOOT_HW_ROLLBACK_PROT
543 } else if (type == IMAGE_TLV_SEC_CNT) {
544 /*
545 * Verify the image's security counter.
546 * This must always be present.
547 */
548 if (len != sizeof(img_security_cnt)) {
549 /* Security counter is not valid. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100550 rc = -1;
551 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100552 }
553
Tamas Banfe031092020-09-10 17:32:39 +0200554 rc = LOAD_IMAGE_DATA(hdr, fap, off, &img_security_cnt, len);
David Vinczec3084132020-02-18 14:50:47 +0100555 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100556 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100557 }
558
Raef Colese8fe6cf2020-05-26 13:07:40 +0100559 FIH_CALL(boot_nv_security_counter_get, fih_rc, image_index,
560 &security_cnt);
561 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
562 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100563 }
564
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300565 BOOT_LOG_DBG("NV Counter read from efuse = %u", fih_uint_decode(security_cnt));
566
567 BOOT_LOG_DBG("NV Counter read from image = %" PRIu32, img_security_cnt);
568
David Vinczec3084132020-02-18 14:50:47 +0100569 /* Compare the new image's security counter value against the
570 * stored security counter value.
571 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100572 fih_rc = fih_int_encode_zero_equality(img_security_cnt <
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300573 (uint32_t)fih_uint_decode(security_cnt));
Raef Colese8fe6cf2020-05-26 13:07:40 +0100574 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200575 /* The image's security counter is not accepted. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100576 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100577 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300578#ifndef CYW20829
David Vinczec3084132020-02-18 14:50:47 +0100579 /* The image's security counter has been successfully verified. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100580 security_counter_valid = fih_rc;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300581 }
582#else
583 /* The image's security counter has been successfully verified. */
584 security_counter_valid = fih_int_encode(HW_ROLLBACK_CNT_VALID);
585 } else if (type == IMAGE_TLV_PROV_PACK) {
586
587 if (fih_eq(security_counter_valid, fih_int_encode(HW_ROLLBACK_CNT_VALID))) {
588 /*
589 * Verify the image reprovisioning packet.
590 * This must always be present.
591 */
592 BOOT_LOG_INF("Prov packet length 0x51 TLV = %" PRIu16, len);
593
594 if (len != sizeof(reprov_packet)) {
595 /* Re-provisioning packet is not valid. */
596 rc = -1;
597 goto out;
598 }
599
600 rc = LOAD_IMAGE_DATA(hdr, fap, off, reprov_packet, len);
601 if (rc) {
602 goto out;
603 }
604
605 security_counter_valid = fih_int_encode(fih_int_decode(security_counter_valid) | REPROV_PACK_VALID);
606 }
607 else{
608 goto out;
609 }
610#endif /* CYW20829 */
David Vinczec3084132020-02-18 14:50:47 +0100611#endif /* MCUBOOT_HW_ROLLBACK_PROT */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800612 }
613 }
David Brown43cda332017-09-01 09:53:23 -0600614
Raef Colese8fe6cf2020-05-26 13:07:40 +0100615 rc = !sha256_valid;
616 if (rc) {
617 goto out;
618 }
David Brown43cda332017-09-01 09:53:23 -0600619#ifdef EXPECTED_SIG_TLV
Raef Colese8fe6cf2020-05-26 13:07:40 +0100620 fih_rc = fih_int_encode_zero_equality(fih_not_eq(valid_signature,
621 FIH_SUCCESS));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800622#endif
David Vinczec3084132020-02-18 14:50:47 +0100623#ifdef MCUBOOT_HW_ROLLBACK_PROT
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300624#ifdef CYW20829
625 if (fih_not_eq(security_counter_valid, fih_int_encode(REPROV_PACK_VALID | HW_ROLLBACK_CNT_VALID))) {
626 BOOT_LOG_DBG("Reprovisioning packet TLV 0x51 is not present image = %d", image_index);
627#else
Raef Colese8fe6cf2020-05-26 13:07:40 +0100628 if (fih_not_eq(security_counter_valid, FIH_SUCCESS)) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300629#endif /* CYW20829 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100630 rc = -1;
631 goto out;
632 }
David Vinczec3084132020-02-18 14:50:47 +0100633#endif
Raef Colese8fe6cf2020-05-26 13:07:40 +0100634
635out:
636 if (rc) {
637 fih_rc = fih_int_encode(rc);
David Vinczec3084132020-02-18 14:50:47 +0100638 }
David Brown43cda332017-09-01 09:53:23 -0600639
Raef Colese8fe6cf2020-05-26 13:07:40 +0100640 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800641}