blob: 239f369f081c98f444151d00d0adc6a810280a3b [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
David Vinczee3697842024-04-03 10:24:26 +02006 * Copyright (c) 2019-2024 Arm Limited
David Brownaac71112020-02-03 16:13:42 -07007 *
8 * Original license:
9 *
Christopher Collins92ea77f2016-12-12 15:59:26 -080010 * Licensed to the Apache Software Foundation (ASF) under one
11 * or more contributor license agreements. See the NOTICE file
12 * distributed with this work for additional information
13 * regarding copyright ownership. The ASF licenses this file
14 * to you under the Apache License, Version 2.0 (the
15 * "License"); you may not use this file except in compliance
16 * with the License. You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing,
21 * software distributed under the License is distributed on an
22 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23 * KIND, either express or implied. See the License for the
24 * specific language governing permissions and limitations
25 * under the License.
26 */
27
Christopher Collins92ea77f2016-12-12 15:59:26 -080028#include <stddef.h>
David Vinczec3084132020-02-18 14:50:47 +010029#include <stdint.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080030#include <inttypes.h>
31#include <string.h>
32
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020033#include <flash_map_backend/flash_map_backend.h>
34
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include "bootutil/image.h"
Roland Mikhel03c9ad02023-03-08 15:56:43 +010036#include "bootutil/crypto/sha.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080037#include "bootutil/sign_key.h"
David Vinczec3084132020-02-18 14:50:47 +010038#include "bootutil/security_cnt.h"
Raef Colese8fe6cf2020-05-26 13:07:40 +010039#include "bootutil/fault_injection_hardening.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080040
Fabio Utzigba1fbe62017-07-21 14:01:20 -030041#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030042
Fabio Utzigba829042018-09-18 08:29:34 -030043#ifdef MCUBOOT_ENC_IMAGES
44#include "bootutil/enc_key.h"
45#endif
46#if defined(MCUBOOT_SIGN_RSA)
Christopher Collins92ea77f2016-12-12 15:59:26 -080047#include "mbedtls/rsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060048#endif
Roland Mikhel206b9142023-02-23 15:28:52 +010049#if defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080050#include "mbedtls/ecdsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060051#endif
Arvin Farahmandf8240192020-05-05 11:43:52 -040052#if defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_SIGN_RSA) || \
Roland Mikhel206b9142023-02-23 15:28:52 +010053 defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080054#include "mbedtls/asn1.h"
Arvin Farahmandf8240192020-05-05 11:43:52 -040055#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080056
57#include "bootutil_priv.h"
58
59/*
Roland Mikhel03c9ad02023-03-08 15:56:43 +010060 * Compute SHA hash over the image.
61 * (SHA384 if ECDSA-P384 is being used,
62 * SHA256 otherwise).
Christopher Collins92ea77f2016-12-12 15:59:26 -080063 */
64static int
Fabio Utzig10ee6482019-08-01 12:04:52 -030065bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
66 struct image_header *hdr, const struct flash_area *fap,
67 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *hash_result,
68 uint8_t *seed, int seed_len)
Christopher Collins92ea77f2016-12-12 15:59:26 -080069{
Roland Mikhel03c9ad02023-03-08 15:56:43 +010070 bootutil_sha_context sha_ctx;
Christopher Collins92ea77f2016-12-12 15:59:26 -080071 uint32_t blk_sz;
72 uint32_t size;
Fabio Utzig2fc80df2018-12-14 06:47:38 -020073 uint16_t hdr_size;
Christopher Collins92ea77f2016-12-12 15:59:26 -080074 uint32_t off;
75 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -030076 uint32_t blk_off;
Fabio Utzige52c08e2019-09-11 19:32:00 -030077 uint32_t tlv_off;
Christopher Collins92ea77f2016-12-12 15:59:26 -080078
Tamas Banfe031092020-09-10 17:32:39 +020079#if (BOOT_IMAGE_NUMBER == 1) || !defined(MCUBOOT_ENC_IMAGES) || \
80 defined(MCUBOOT_RAM_LOAD)
Fabio Utzig10ee6482019-08-01 12:04:52 -030081 (void)enc_state;
Fabio Utzigb0f04732019-07-31 09:49:19 -030082 (void)image_index;
Fabio Utzigc9621352019-08-08 12:15:51 -030083 (void)hdr_size;
Fabio Utzige52c08e2019-09-11 19:32:00 -030084 (void)blk_off;
85 (void)tlv_off;
Tamas Banfe031092020-09-10 17:32:39 +020086#ifdef MCUBOOT_RAM_LOAD
87 (void)blk_sz;
88 (void)off;
89 (void)rc;
David Brown122f9e72021-05-21 15:58:56 -060090 (void)fap;
91 (void)tmp_buf;
92 (void)tmp_buf_sz;
Tamas Banfe031092020-09-10 17:32:39 +020093#endif
Fabio Utzigb0f04732019-07-31 09:49:19 -030094#endif
95
Fabio Utzigbc077932019-08-26 11:16:34 -030096#ifdef MCUBOOT_ENC_IMAGES
97 /* Encrypted images only exist in the secondary slot */
98 if (MUST_DECRYPT(fap, image_index, hdr) &&
99 !boot_enc_valid(enc_state, image_index, fap)) {
100 return -1;
101 }
102#endif
103
Roland Mikhel03c9ad02023-03-08 15:56:43 +0100104 bootutil_sha_init(&sha_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800105
106 /* in some cases (split image) the hash is seeded with data from
107 * the loader image */
Fabio Utzig53986042017-12-12 14:57:07 -0200108 if (seed && (seed_len > 0)) {
Roland Mikhel03c9ad02023-03-08 15:56:43 +0100109 bootutil_sha_update(&sha_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800110 }
111
David Vinczee32483f2019-06-13 10:46:24 +0200112 /* Hash is computed over image header and image itself. */
Fabio Utzige52c08e2019-09-11 19:32:00 -0300113 size = hdr_size = hdr->ih_hdr_size;
114 size += hdr->ih_img_size;
115 tlv_off = size;
David Vinczee32483f2019-06-13 10:46:24 +0200116
Fabio Utzige52c08e2019-09-11 19:32:00 -0300117 /* If protected TLVs are present they are also hashed. */
118 size += hdr->ih_protect_tlv_size;
David Vinczee32483f2019-06-13 10:46:24 +0200119
Tamas Banfe031092020-09-10 17:32:39 +0200120#ifdef MCUBOOT_RAM_LOAD
Roland Mikhel03c9ad02023-03-08 15:56:43 +0100121 bootutil_sha_update(&sha_ctx,
122 (void*)(IMAGE_RAM_BASE + hdr->ih_load_addr),
123 size);
Tamas Banfe031092020-09-10 17:32:39 +0200124#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800125 for (off = 0; off < size; off += blk_sz) {
126 blk_sz = size - off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300127 if (blk_sz > tmp_buf_sz) {
128 blk_sz = tmp_buf_sz;
129 }
Fabio Utzigc21c2102019-09-10 10:10:42 -0300130#ifdef MCUBOOT_ENC_IMAGES
131 /* The only data that is encrypted in an image is the payload;
132 * both header and TLVs (when protected) are not.
133 */
134 if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) {
135 /* read only the header */
136 blk_sz = hdr_size - off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300137 }
138 if ((off < tlv_off) && ((off + blk_sz) > tlv_off)) {
139 /* read only up to the end of the image payload */
140 blk_sz = tlv_off - off;
Fabio Utzigc21c2102019-09-10 10:10:42 -0300141 }
142#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800143 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
144 if (rc) {
Roland Mikhel03c9ad02023-03-08 15:56:43 +0100145 bootutil_sha_drop(&sha_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800146 return rc;
147 }
Fabio Utzigba829042018-09-18 08:29:34 -0300148#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzigc21c2102019-09-10 10:10:42 -0300149 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzige52c08e2019-09-11 19:32:00 -0300150 /* Only payload is encrypted (area between header and TLVs) */
Dominik Ermel3f112862024-07-17 14:43:05 +0000151 int slot = flash_area_id_to_multi_image_slot(image_index,
152 flash_area_get_id(fap));
153
Fabio Utzige52c08e2019-09-11 19:32:00 -0300154 if (off >= hdr_size && off < tlv_off) {
Fabio Utzigc21c2102019-09-10 10:10:42 -0300155 blk_off = (off - hdr_size) & 0xf;
Dominik Ermel3f112862024-07-17 14:43:05 +0000156 boot_encrypt(enc_state, slot, off - hdr_size,
157 blk_sz, blk_off, tmp_buf);
Fabio Utzigc21c2102019-09-10 10:10:42 -0300158 }
Fabio Utzigba829042018-09-18 08:29:34 -0300159 }
160#endif
Roland Mikhel03c9ad02023-03-08 15:56:43 +0100161 bootutil_sha_update(&sha_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800162 }
Tamas Banfe031092020-09-10 17:32:39 +0200163#endif /* MCUBOOT_RAM_LOAD */
Roland Mikhel03c9ad02023-03-08 15:56:43 +0100164 bootutil_sha_finish(&sha_ctx, hash_result);
165 bootutil_sha_drop(&sha_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800166
167 return 0;
168}
169
170/*
David Brown43cda332017-09-01 09:53:23 -0600171 * Currently, we only support being able to verify one type of
172 * signature, because there is a single verification function that we
173 * call. List the type of TLV we are expecting. If we aren't
174 * configured for any signature, don't define this macro.
175 */
Fabio Utzig48764842019-05-10 19:28:24 -0300176#if (defined(MCUBOOT_SIGN_RSA) + \
Fabio Utzig48764842019-05-10 19:28:24 -0300177 defined(MCUBOOT_SIGN_EC256) + \
Roland Mikhel03c9ad02023-03-08 15:56:43 +0100178 defined(MCUBOOT_SIGN_EC384) + \
Fabio Utzig48764842019-05-10 19:28:24 -0300179 defined(MCUBOOT_SIGN_ED25519)) > 1
Fabio Utzig3501c012019-05-13 15:07:25 -0700180#error "Only a single signature type is supported!"
181#endif
182
David Brown43cda332017-09-01 09:53:23 -0600183#if defined(MCUBOOT_SIGN_RSA)
Fabio Utzig3501c012019-05-13 15:07:25 -0700184# if MCUBOOT_SIGN_RSA_LEN == 2048
185# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
186# elif MCUBOOT_SIGN_RSA_LEN == 3072
187# define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS
188# else
189# error "Unsupported RSA signature length"
David Brown43cda332017-09-01 09:53:23 -0600190# endif
Fabio Utzig3501c012019-05-13 15:07:25 -0700191# define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8)
192# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) /* 2048 bits */
Roland Mikhel63d23462023-02-08 14:00:54 +0100193#elif defined(MCUBOOT_SIGN_EC256) || \
Roland Mikhel03c9ad02023-03-08 15:56:43 +0100194 defined(MCUBOOT_SIGN_EC384) || \
Roland Mikhel63d23462023-02-08 14:00:54 +0100195 defined(MCUBOOT_SIGN_EC)
196# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA_SIG
Fabio Utzig3501c012019-05-13 15:07:25 -0700197# define SIG_BUF_SIZE 128
Roland Mikhel63d23462023-02-08 14:00:54 +0100198# define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */
Fabio Utzig48764842019-05-10 19:28:24 -0300199#elif defined(MCUBOOT_SIGN_ED25519)
200# define EXPECTED_SIG_TLV IMAGE_TLV_ED25519
201# define SIG_BUF_SIZE 64
202# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE)
Fabio Utzig3501c012019-05-13 15:07:25 -0700203#else
204# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
David Brown43cda332017-09-01 09:53:23 -0600205#endif
206
David Vinczee3697842024-04-03 10:24:26 +0200207#if (defined(MCUBOOT_HW_KEY) + \
208 defined(MCUBOOT_BUILTIN_KEY)) > 1
209#error "Please use either MCUBOOT_HW_KEY or the MCUBOOT_BUILTIN_KEY feature."
210#endif
211
David Brown43cda332017-09-01 09:53:23 -0600212#ifdef EXPECTED_SIG_TLV
David Vinczee3697842024-04-03 10:24:26 +0200213
214#if !defined(MCUBOOT_BUILTIN_KEY)
215#if !defined(MCUBOOT_HW_KEY)
216/* The key TLV contains the hash of the public key. */
217# define EXPECTED_KEY_TLV IMAGE_TLV_KEYHASH
218# define KEY_BUF_SIZE IMAGE_HASH_SIZE
219#else
220/* The key TLV contains the whole public key.
221 * Add a few extra bytes to the key buffer size for encoding and
222 * for public exponent.
223 */
224# define EXPECTED_KEY_TLV IMAGE_TLV_PUBKEY
225# define KEY_BUF_SIZE (SIG_BUF_SIZE + 24)
226#endif /* !MCUBOOT_HW_KEY */
227
David Vincze03368b82020-04-01 12:53:53 +0200228#if !defined(MCUBOOT_HW_KEY)
David Brown43cda332017-09-01 09:53:23 -0600229static int
230bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
231{
Roland Mikhel03c9ad02023-03-08 15:56:43 +0100232 bootutil_sha_context sha_ctx;
David Brown43cda332017-09-01 09:53:23 -0600233 int i;
234 const struct bootutil_key *key;
Roland Mikhel03c9ad02023-03-08 15:56:43 +0100235 uint8_t hash[IMAGE_HASH_SIZE];
David Brown43cda332017-09-01 09:53:23 -0600236
Roland Mikhel03c9ad02023-03-08 15:56:43 +0100237 if (keyhash_len > IMAGE_HASH_SIZE) {
Fabio Utzig15c14672019-08-09 07:45:20 -0300238 return -1;
239 }
David Brown43cda332017-09-01 09:53:23 -0600240
241 for (i = 0; i < bootutil_key_cnt; i++) {
242 key = &bootutil_keys[i];
Roland Mikhel03c9ad02023-03-08 15:56:43 +0100243 bootutil_sha_init(&sha_ctx);
244 bootutil_sha_update(&sha_ctx, key->key, *key->len);
245 bootutil_sha_finish(&sha_ctx, hash);
David Brown43cda332017-09-01 09:53:23 -0600246 if (!memcmp(hash, keyhash, keyhash_len)) {
Roland Mikhel03c9ad02023-03-08 15:56:43 +0100247 bootutil_sha_drop(&sha_ctx);
David Brown43cda332017-09-01 09:53:23 -0600248 return i;
249 }
250 }
Roland Mikhel03c9ad02023-03-08 15:56:43 +0100251 bootutil_sha_drop(&sha_ctx);
David Brown43cda332017-09-01 09:53:23 -0600252 return -1;
253}
David Vinczee3697842024-04-03 10:24:26 +0200254#else /* !MCUBOOT_HW_KEY */
David Vincze03368b82020-04-01 12:53:53 +0200255extern unsigned int pub_key_len;
256static int
257bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
258{
Roland Mikhel03c9ad02023-03-08 15:56:43 +0100259 bootutil_sha_context sha_ctx;
260 uint8_t hash[IMAGE_HASH_SIZE];
261 uint8_t key_hash[IMAGE_HASH_SIZE];
David Vincze03368b82020-04-01 12:53:53 +0200262 size_t key_hash_size = sizeof(key_hash);
263 int rc;
Michael Grand5047f032022-11-24 16:49:56 +0100264 FIH_DECLARE(fih_rc, FIH_FAILURE);
David Vincze03368b82020-04-01 12:53:53 +0200265
Roland Mikhel03c9ad02023-03-08 15:56:43 +0100266 bootutil_sha_init(&sha_ctx);
267 bootutil_sha_update(&sha_ctx, key, key_len);
268 bootutil_sha_finish(&sha_ctx, hash);
269 bootutil_sha_drop(&sha_ctx);
David Vincze03368b82020-04-01 12:53:53 +0200270
271 rc = boot_retrieve_public_key_hash(image_index, key_hash, &key_hash_size);
272 if (rc) {
Mark Horvath7ebf0e82021-02-19 09:55:35 +0100273 return -1;
David Vincze03368b82020-04-01 12:53:53 +0200274 }
275
Raef Colese8fe6cf2020-05-26 13:07:40 +0100276 /* Adding hardening to avoid this potential attack:
277 * - Image is signed with an arbitrary key and the corresponding public
278 * key is added as a TLV field.
279 * - During public key validation (comparing against key-hash read from
280 * HW) a fault is injected to accept the public key as valid one.
281 */
282 FIH_CALL(boot_fih_memequal, fih_rc, hash, key_hash, key_hash_size);
Michael Grand5047f032022-11-24 16:49:56 +0100283 if (FIH_EQ(fih_rc, FIH_SUCCESS)) {
David Vincze03368b82020-04-01 12:53:53 +0200284 bootutil_keys[0].key = key;
285 pub_key_len = key_len;
286 return 0;
287 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100288
David Vincze03368b82020-04-01 12:53:53 +0200289 return -1;
290}
291#endif /* !MCUBOOT_HW_KEY */
David Vinczee3697842024-04-03 10:24:26 +0200292#endif /* !MCUBOOT_BUILTIN_KEY */
293#endif /* EXPECTED_SIG_TLV */
David Brown43cda332017-09-01 09:53:23 -0600294
David Vinczec3084132020-02-18 14:50:47 +0100295/**
296 * Reads the value of an image's security counter.
297 *
298 * @param hdr Pointer to the image header structure.
299 * @param fap Pointer to a description structure of the image's
300 * flash area.
301 * @param security_cnt Pointer to store the security counter value.
302 *
303 * @return 0 on success; nonzero on failure.
304 */
305int32_t
306bootutil_get_img_security_cnt(struct image_header *hdr,
307 const struct flash_area *fap,
308 uint32_t *img_security_cnt)
309{
310 struct image_tlv_iter it;
311 uint32_t off;
312 uint16_t len;
313 int32_t rc;
314
315 if ((hdr == NULL) ||
316 (fap == NULL) ||
317 (img_security_cnt == NULL)) {
318 /* Invalid parameter. */
319 return BOOT_EBADARGS;
320 }
321
322 /* The security counter TLV is in the protected part of the TLV area. */
323 if (hdr->ih_protect_tlv_size == 0) {
324 return BOOT_EBADIMAGE;
325 }
326
327 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_SEC_CNT, true);
328 if (rc) {
329 return rc;
330 }
331
332 /* Traverse through the protected TLV area to find
333 * the security counter TLV.
334 */
335
336 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
337 if (rc != 0) {
338 /* Security counter TLV has not been found. */
339 return -1;
340 }
341
342 if (len != sizeof(*img_security_cnt)) {
343 /* Security counter is not valid. */
344 return BOOT_EBADIMAGE;
345 }
346
Tamas Banfe031092020-09-10 17:32:39 +0200347 rc = LOAD_IMAGE_DATA(hdr, fap, off, img_security_cnt, len);
David Vinczec3084132020-02-18 14:50:47 +0100348 if (rc != 0) {
349 return BOOT_EFLASH;
350 }
351
352 return 0;
353}
David Vinczec3084132020-02-18 14:50:47 +0100354
David Brownda2e2ab2024-04-10 14:59:00 -0600355#ifndef ALLOW_ROGUE_TLVS
356/*
357 * The following list of TLVs are the only entries allowed in the unprotected
358 * TLV section. All other TLV entries must be in the protected section.
359 */
360static const uint16_t allowed_unprot_tlvs[] = {
361 IMAGE_TLV_KEYHASH,
362 IMAGE_TLV_PUBKEY,
363 IMAGE_TLV_SHA256,
364 IMAGE_TLV_SHA384,
365 IMAGE_TLV_RSA2048_PSS,
366 IMAGE_TLV_ECDSA224,
367 IMAGE_TLV_ECDSA_SIG,
368 IMAGE_TLV_RSA3072_PSS,
369 IMAGE_TLV_ED25519,
370 IMAGE_TLV_ENC_RSA2048,
371 IMAGE_TLV_ENC_KW,
372 IMAGE_TLV_ENC_EC256,
373 IMAGE_TLV_ENC_X25519,
374 /* Mark end with ANY. */
375 IMAGE_TLV_ANY,
376};
377#endif
378
David Brown43cda332017-09-01 09:53:23 -0600379/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800380 * Verify the integrity of the image.
381 * Return non-zero if image could not be validated/does not validate.
382 */
Michael Grand5047f032022-11-24 16:49:56 +0100383fih_ret
Fabio Utzig10ee6482019-08-01 12:04:52 -0300384bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
385 struct image_header *hdr, const struct flash_area *fap,
386 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *seed,
387 int seed_len, uint8_t *out_hash)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800388{
389 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300390 uint16_t len;
David Brownd13318a2019-12-04 17:28:40 -0700391 uint16_t type;
Roland Mikhel03c9ad02023-03-08 15:56:43 +0100392 int image_hash_valid = 0;
David Brown43cda332017-09-01 09:53:23 -0600393#ifdef EXPECTED_SIG_TLV
Michael Grand5047f032022-11-24 16:49:56 +0100394 FIH_DECLARE(valid_signature, FIH_FAILURE);
David Vinczee3697842024-04-03 10:24:26 +0200395#ifndef MCUBOOT_BUILTIN_KEY
David Brown43cda332017-09-01 09:53:23 -0600396 int key_id = -1;
David Vinczee3697842024-04-03 10:24:26 +0200397#else
398 /* Pass a key ID equal to the image index, the underlying crypto library
399 * is responsible for mapping the image index to a builtin key ID.
400 */
401 int key_id = image_index;
402#endif /* !MCUBOOT_BUILTIN_KEY */
David Vincze03368b82020-04-01 12:53:53 +0200403#ifdef MCUBOOT_HW_KEY
David Vinczee3697842024-04-03 10:24:26 +0200404 uint8_t key_buf[KEY_BUF_SIZE];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800405#endif
David Vincze03368b82020-04-01 12:53:53 +0200406#endif /* EXPECTED_SIG_TLV */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300407 struct image_tlv_iter it;
Fabio Utzig3501c012019-05-13 15:07:25 -0700408 uint8_t buf[SIG_BUF_SIZE];
Roland Mikhel03c9ad02023-03-08 15:56:43 +0100409 uint8_t hash[IMAGE_HASH_SIZE];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100410 int rc = 0;
Michael Grand5047f032022-11-24 16:49:56 +0100411 FIH_DECLARE(fih_rc, FIH_FAILURE);
David Vinczec3084132020-02-18 14:50:47 +0100412#ifdef MCUBOOT_HW_ROLLBACK_PROT
Raef Colese8fe6cf2020-05-26 13:07:40 +0100413 fih_int security_cnt = fih_int_encode(INT_MAX);
David Vinczec3084132020-02-18 14:50:47 +0100414 uint32_t img_security_cnt = 0;
Michael Grand5047f032022-11-24 16:49:56 +0100415 FIH_DECLARE(security_counter_valid, FIH_FAILURE);
David Vinczec3084132020-02-18 14:50:47 +0100416#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800417
Fabio Utzig10ee6482019-08-01 12:04:52 -0300418 rc = bootutil_img_hash(enc_state, image_index, hdr, fap, tmp_buf,
419 tmp_buf_sz, hash, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800420 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100421 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800422 }
423
424 if (out_hash) {
Roland Mikhel03c9ad02023-03-08 15:56:43 +0100425 memcpy(out_hash, hash, IMAGE_HASH_SIZE);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800426 }
427
Fabio Utzig61fd8882019-09-14 20:00:20 -0300428 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false);
David Brownf5b33d82017-09-01 10:58:27 -0600429 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100430 goto out;
David Brownf5b33d82017-09-01 10:58:27 -0600431 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800432
Andrzej Puzdrowski334b6a62022-01-21 16:59:18 +0100433 if (it.tlv_end > bootutil_max_image_size(fap)) {
434 rc = -1;
435 goto out;
436 }
437
David Brown43cda332017-09-01 09:53:23 -0600438 /*
439 * Traverse through all of the TLVs, performing any checks we know
440 * and are able to do.
441 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300442 while (true) {
443 rc = bootutil_tlv_iter_next(&it, &off, &len, &type);
444 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100445 goto out;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300446 } else if (rc > 0) {
447 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800448 }
David Brown43cda332017-09-01 09:53:23 -0600449
David Brownda2e2ab2024-04-10 14:59:00 -0600450#ifndef ALLOW_ROGUE_TLVS
451 /*
452 * Ensure that the non-protected TLV only has entries necessary to hold
453 * the signature. We also allow encryption related keys to be in the
454 * unprotected area.
455 */
456 if (!bootutil_tlv_iter_is_prot(&it, off)) {
457 bool found = false;
458 for (const uint16_t *p = allowed_unprot_tlvs; *p != IMAGE_TLV_ANY; p++) {
459 if (type == *p) {
460 found = true;
461 break;
462 }
463 }
464 if (!found) {
465 FIH_SET(fih_rc, FIH_FAILURE);
466 goto out;
467 }
468 }
469#endif
470
Roland Mikhel03c9ad02023-03-08 15:56:43 +0100471 if (type == EXPECTED_HASH_TLV) {
472 /* Verify the image hash. This must always be present. */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300473 if (len != sizeof(hash)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100474 rc = -1;
475 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800476 }
Tamas Banfe031092020-09-10 17:32:39 +0200477 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, sizeof(hash));
David Brown43cda332017-09-01 09:53:23 -0600478 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100479 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800480 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100481
482 FIH_CALL(boot_fih_memequal, fih_rc, hash, buf, sizeof(hash));
Michael Grand5047f032022-11-24 16:49:56 +0100483 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
484 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100485 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800486 }
David Brown43cda332017-09-01 09:53:23 -0600487
Roland Mikhel03c9ad02023-03-08 15:56:43 +0100488 image_hash_valid = 1;
David Vinczee3697842024-04-03 10:24:26 +0200489#ifdef EXPECTED_KEY_TLV
490 } else if (type == EXPECTED_KEY_TLV) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800491 /*
David Brown43cda332017-09-01 09:53:23 -0600492 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800493 */
David Vinczee3697842024-04-03 10:24:26 +0200494 if (len > KEY_BUF_SIZE) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100495 rc = -1;
496 goto out;
David Brown43cda332017-09-01 09:53:23 -0600497 }
David Vinczee3697842024-04-03 10:24:26 +0200498#ifndef MCUBOOT_HW_KEY
Tamas Banfe031092020-09-10 17:32:39 +0200499 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600500 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100501 goto out;
David Brown43cda332017-09-01 09:53:23 -0600502 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300503 key_id = bootutil_find_key(buf, len);
David Vincze03368b82020-04-01 12:53:53 +0200504#else
Tamas Banfe031092020-09-10 17:32:39 +0200505 rc = LOAD_IMAGE_DATA(hdr, fap, off, key_buf, len);
David Vincze03368b82020-04-01 12:53:53 +0200506 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100507 goto out;
David Vincze03368b82020-04-01 12:53:53 +0200508 }
509 key_id = bootutil_find_key(image_index, key_buf, len);
David Vinczee3697842024-04-03 10:24:26 +0200510#endif /* !MCUBOOT_HW_KEY */
David Vincze03368b82020-04-01 12:53:53 +0200511 /*
512 * The key may not be found, which is acceptable. There
513 * can be multiple signatures, each preceded by a key.
514 */
David Vinczee3697842024-04-03 10:24:26 +0200515#endif /* EXPECTED_KEY_TLV */
516#ifdef EXPECTED_SIG_TLV
Fabio Utzig61fd8882019-09-14 20:00:20 -0300517 } else if (type == EXPECTED_SIG_TLV) {
David Brown43cda332017-09-01 09:53:23 -0600518 /* Ignore this signature if it is out of bounds. */
519 if (key_id < 0 || key_id >= bootutil_key_cnt) {
520 key_id = -1;
521 continue;
522 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300523 if (!EXPECTED_SIG_LEN(len) || len > sizeof(buf)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100524 rc = -1;
525 goto out;
David Brown43cda332017-09-01 09:53:23 -0600526 }
Tamas Banfe031092020-09-10 17:32:39 +0200527 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600528 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100529 goto out;
David Brown43cda332017-09-01 09:53:23 -0600530 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100531 FIH_CALL(bootutil_verify_sig, valid_signature, hash, sizeof(hash),
532 buf, len, key_id);
David Brown43cda332017-09-01 09:53:23 -0600533 key_id = -1;
David Vinczec3084132020-02-18 14:50:47 +0100534#endif /* EXPECTED_SIG_TLV */
535#ifdef MCUBOOT_HW_ROLLBACK_PROT
536 } else if (type == IMAGE_TLV_SEC_CNT) {
537 /*
538 * Verify the image's security counter.
539 * This must always be present.
540 */
541 if (len != sizeof(img_security_cnt)) {
542 /* Security counter is not valid. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100543 rc = -1;
544 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100545 }
546
Tamas Banfe031092020-09-10 17:32:39 +0200547 rc = LOAD_IMAGE_DATA(hdr, fap, off, &img_security_cnt, len);
David Vinczec3084132020-02-18 14:50:47 +0100548 if (rc) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100549 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100550 }
551
Raef Colese8fe6cf2020-05-26 13:07:40 +0100552 FIH_CALL(boot_nv_security_counter_get, fih_rc, image_index,
553 &security_cnt);
Michael Grand5047f032022-11-24 16:49:56 +0100554 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
555 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100556 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100557 }
558
559 /* Compare the new image's security counter value against the
560 * stored security counter value.
561 */
Michael Grand5047f032022-11-24 16:49:56 +0100562 fih_rc = fih_ret_encode_zero_equality(img_security_cnt <
Roland Mikhel61962b92023-04-25 14:03:08 +0200563 (uint32_t)fih_int_decode(security_cnt));
Michael Grand5047f032022-11-24 16:49:56 +0100564 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
565 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100566 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100567 }
568
569 /* The image's security counter has been successfully verified. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100570 security_counter_valid = fih_rc;
David Vinczec3084132020-02-18 14:50:47 +0100571#endif /* MCUBOOT_HW_ROLLBACK_PROT */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800572 }
573 }
David Brown43cda332017-09-01 09:53:23 -0600574
Roland Mikhel03c9ad02023-03-08 15:56:43 +0100575 rc = !image_hash_valid;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100576 if (rc) {
577 goto out;
578 }
David Brown43cda332017-09-01 09:53:23 -0600579#ifdef EXPECTED_SIG_TLV
Michael Grand5047f032022-11-24 16:49:56 +0100580 FIH_SET(fih_rc, valid_signature);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800581#endif
David Vinczec3084132020-02-18 14:50:47 +0100582#ifdef MCUBOOT_HW_ROLLBACK_PROT
Michael Grand5047f032022-11-24 16:49:56 +0100583 if (FIH_NOT_EQ(security_counter_valid, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100584 rc = -1;
585 goto out;
586 }
David Vinczec3084132020-02-18 14:50:47 +0100587#endif
Raef Colese8fe6cf2020-05-26 13:07:40 +0100588
589out:
590 if (rc) {
Michael Grand5047f032022-11-24 16:49:56 +0100591 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczec3084132020-02-18 14:50:47 +0100592 }
David Brown43cda332017-09-01 09:53:23 -0600593
Raef Colese8fe6cf2020-05-26 13:07:40 +0100594 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800595}