blob: 79dc2fd7aa344962371ed2535e59d4682bd9ed5d [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>
INFINEON\DovhalA3b578f32024-11-29 01:06:04 +020033#include <assert.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080034
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020035#include <flash_map_backend/flash_map_backend.h>
36
Christopher Collins92ea77f2016-12-12 15:59:26 -080037#include "bootutil/image.h"
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +090038#include "bootutil/crypto/sha256.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080039#include "bootutil/sign_key.h"
David Vinczec3084132020-02-18 14:50:47 +010040#include "bootutil/security_cnt.h"
Raef Colese8fe6cf2020-05-26 13:07:40 +010041#include "bootutil/fault_injection_hardening.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080042
Roman Okhrimenko977b3752022-03-31 14:40:48 +030043#include "bootutil/bootutil_log.h"
44
Fabio Utzigba1fbe62017-07-21 14:01:20 -030045#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030046
Fabio Utzigba829042018-09-18 08:29:34 -030047#ifdef MCUBOOT_ENC_IMAGES
48#include "bootutil/enc_key.h"
49#endif
50#if defined(MCUBOOT_SIGN_RSA)
Christopher Collins92ea77f2016-12-12 15:59:26 -080051#include "mbedtls/rsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060052#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -030053#if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080054#include "mbedtls/ecdsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060055#endif
Arvin Farahmandf8240192020-05-05 11:43:52 -040056#if defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_SIGN_RSA) || \
57 defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080058#include "mbedtls/asn1.h"
Arvin Farahmandf8240192020-05-05 11:43:52 -040059#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080060
61#include "bootutil_priv.h"
62
Roman Okhrimenko25165622023-05-23 08:58:29 +030063#ifdef CYW20829
64#include "cy_security_cnt_platform.h"
65#endif
66
Christopher Collins92ea77f2016-12-12 15:59:26 -080067/*
68 * Compute SHA256 over the image.
69 */
70static int
Fabio Utzig10ee6482019-08-01 12:04:52 -030071bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
72 struct image_header *hdr, const struct flash_area *fap,
73 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *hash_result,
74 uint8_t *seed, int seed_len)
Christopher Collins92ea77f2016-12-12 15:59:26 -080075{
David Browne629bf32017-04-24 13:37:33 -060076 bootutil_sha256_context sha256_ctx;
Christopher Collins92ea77f2016-12-12 15:59:26 -080077 uint32_t blk_sz;
78 uint32_t size;
Fabio Utzig2fc80df2018-12-14 06:47:38 -020079 uint16_t hdr_size;
Christopher Collins92ea77f2016-12-12 15:59:26 -080080 uint32_t off;
81 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -030082 uint32_t blk_off;
Fabio Utzige52c08e2019-09-11 19:32:00 -030083 uint32_t tlv_off;
Christopher Collins92ea77f2016-12-12 15:59:26 -080084
Tamas Banfe031092020-09-10 17:32:39 +020085#if (BOOT_IMAGE_NUMBER == 1) || !defined(MCUBOOT_ENC_IMAGES) || \
86 defined(MCUBOOT_RAM_LOAD)
Fabio Utzig10ee6482019-08-01 12:04:52 -030087 (void)enc_state;
Fabio Utzigb0f04732019-07-31 09:49:19 -030088 (void)image_index;
Fabio Utzigc9621352019-08-08 12:15:51 -030089 (void)hdr_size;
Fabio Utzige52c08e2019-09-11 19:32:00 -030090 (void)blk_off;
91 (void)tlv_off;
Tamas Banfe031092020-09-10 17:32:39 +020092#ifdef MCUBOOT_RAM_LOAD
93 (void)blk_sz;
94 (void)off;
95 (void)rc;
Roman Okhrimenko977b3752022-03-31 14:40:48 +030096 (void)fap;
97 (void)tmp_buf;
98 (void)tmp_buf_sz;
Tamas Banfe031092020-09-10 17:32:39 +020099#endif
Fabio Utzigb0f04732019-07-31 09:49:19 -0300100#endif
101
Fabio Utzigbc077932019-08-26 11:16:34 -0300102#ifdef MCUBOOT_ENC_IMAGES
103 /* Encrypted images only exist in the secondary slot */
104 if (MUST_DECRYPT(fap, image_index, hdr) &&
105 !boot_enc_valid(enc_state, image_index, fap)) {
106 return -1;
107 }
108#endif
109
David Browne629bf32017-04-24 13:37:33 -0600110 bootutil_sha256_init(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800111
112 /* in some cases (split image) the hash is seeded with data from
113 * the loader image */
Fabio Utzig53986042017-12-12 14:57:07 -0200114 if (seed && (seed_len > 0)) {
David Browne629bf32017-04-24 13:37:33 -0600115 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800116 }
117
David Vinczee32483f2019-06-13 10:46:24 +0200118 /* Hash is computed over image header and image itself. */
Fabio Utzige52c08e2019-09-11 19:32:00 -0300119 size = hdr_size = hdr->ih_hdr_size;
120 size += hdr->ih_img_size;
121 tlv_off = size;
David Vinczee32483f2019-06-13 10:46:24 +0200122
Fabio Utzige52c08e2019-09-11 19:32:00 -0300123 /* If protected TLVs are present they are also hashed. */
124 size += hdr->ih_protect_tlv_size;
David Vinczee32483f2019-06-13 10:46:24 +0200125
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300126 do
INFINEON\DovhalA3b578f32024-11-29 01:06:04 +0200127 {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300128#if defined(MCUBOOT_RAM_LOAD)
129#if defined(MCUBOOT_MULTI_MEMORY_LOAD)
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200130 if (IS_RAM_BOOTABLE(hdr) && IS_RAM_BOOT_STAGE())
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300131#endif /* MCUBOOT_MULTI_MEMORY_LOAD */
132 {
133 bootutil_sha256_update(
134 &sha256_ctx, (void *)(IMAGE_RAM_BASE + hdr->ih_load_addr), size);
135 break;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300136 }
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300137#endif /* MCUBOOT_RAM_LOAD */
138#if !(defined(MCUBOOT_RAM_LOAD) && !defined(MCUBOOT_MULTI_MEMORY_LOAD))
139 {
140 for (off = 0; off < size; off += blk_sz) {
141 blk_sz = size - off;
142 if (blk_sz > tmp_buf_sz) {
143 blk_sz = tmp_buf_sz;
144 }
Fabio Utzigc21c2102019-09-10 10:10:42 -0300145#ifdef MCUBOOT_ENC_IMAGES
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300146 /* The only data that is encrypted in an image is the payload;
147 * both header and TLVs (when protected) are not.
148 */
149 if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) {
150 /* read only the header */
151 blk_sz = hdr_size - off;
152 }
153 if ((off < tlv_off) && ((off + blk_sz) > tlv_off)) {
154 /* read only up to the end of the image payload */
155 blk_sz = tlv_off - off;
156 }
157#endif /* MCUBOOT_ENC_IMAGES */
158 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300159 if (rc) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300160 bootutil_sha256_drop(&sha256_ctx);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300161 return rc;
162 }
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300163#ifdef MCUBOOT_ENC_IMAGES
164 if (MUST_DECRYPT(fap, image_index, hdr)) {
165 /* Only payload is encrypted (area between header and TLVs) */
166 if (off >= hdr_size && off < tlv_off) {
167 blk_off = (off - hdr_size) & 0xf;
168#ifdef MCUBOOT_ENC_IMAGES_XIP
169 rc = bootutil_img_encrypt(enc_state, image_index, hdr, fap, off,
170 blk_sz, blk_off, tmp_buf);
171#else
172 rc = boot_encrypt(enc_state, image_index, fap, off - hdr_size,
173 blk_sz, blk_off, tmp_buf);
174#endif /* MCUBOOT_ENC_IMAGES_XIP */
175 if (rc) {
176 return rc;
177 }
178 }
179 }
180#endif /* MCUBOOT_ENC_IMAGES */
181 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Fabio Utzigc21c2102019-09-10 10:10:42 -0300182 }
Fabio Utzigba829042018-09-18 08:29:34 -0300183 }
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300184#endif /* !MCUBOOT_RAM_LOAD || MCUBOOT_MULTI_MEMORY_LOAD */
185 } while (false);
David Browne629bf32017-04-24 13:37:33 -0600186 bootutil_sha256_finish(&sha256_ctx, hash_result);
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900187 bootutil_sha256_drop(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800188
189 return 0;
190}
191
192/*
David Brown43cda332017-09-01 09:53:23 -0600193 * Currently, we only support being able to verify one type of
194 * signature, because there is a single verification function that we
195 * call. List the type of TLV we are expecting. If we aren't
196 * configured for any signature, don't define this macro.
197 */
Fabio Utzig48764842019-05-10 19:28:24 -0300198#if (defined(MCUBOOT_SIGN_RSA) + \
199 defined(MCUBOOT_SIGN_EC) + \
200 defined(MCUBOOT_SIGN_EC256) + \
201 defined(MCUBOOT_SIGN_ED25519)) > 1
Fabio Utzig3501c012019-05-13 15:07:25 -0700202#error "Only a single signature type is supported!"
203#endif
204
David Brown43cda332017-09-01 09:53:23 -0600205#if defined(MCUBOOT_SIGN_RSA)
Fabio Utzig3501c012019-05-13 15:07:25 -0700206# if MCUBOOT_SIGN_RSA_LEN == 2048
207# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
208# elif MCUBOOT_SIGN_RSA_LEN == 3072
209# define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS
210# else
211# error "Unsupported RSA signature length"
David Brown43cda332017-09-01 09:53:23 -0600212# endif
Fabio Utzig3501c012019-05-13 15:07:25 -0700213# define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8)
214# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) /* 2048 bits */
David Brown43cda332017-09-01 09:53:23 -0600215#elif defined(MCUBOOT_SIGN_EC)
216# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224
Fabio Utzig3501c012019-05-13 15:07:25 -0700217# define SIG_BUF_SIZE 128
David Browna3608262019-12-12 15:35:31 -0700218# define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */
David Brown43cda332017-09-01 09:53:23 -0600219#elif defined(MCUBOOT_SIGN_EC256)
220# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
Fabio Utzig3501c012019-05-13 15:07:25 -0700221# define SIG_BUF_SIZE 128
David Browna3608262019-12-12 15:35:31 -0700222# define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */
Fabio Utzig48764842019-05-10 19:28:24 -0300223#elif defined(MCUBOOT_SIGN_ED25519)
224# define EXPECTED_SIG_TLV IMAGE_TLV_ED25519
225# define SIG_BUF_SIZE 64
226# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE)
Fabio Utzig3501c012019-05-13 15:07:25 -0700227#else
228# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
David Brown43cda332017-09-01 09:53:23 -0600229#endif
230
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200231/* Complex result masks for bootutil_img_validate() */
232#define SET_MASK_IMAGE_TLV_SHA256 ((signed)0x0000002E)
INFINEON\DovhalA3b578f32024-11-29 01:06:04 +0200233fih_int FIH_MASK_IMAGE_TLV_SHA256 = FIH_INT_INIT_GLOBAL(
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200234 SET_MASK_IMAGE_TLV_SHA256);
235
236#define SET_MASK_SIG_TLV_EXPECTED ((signed)0x00007400)
INFINEON\DovhalA3b578f32024-11-29 01:06:04 +0200237fih_int FIH_MASK_SIG_TLV_EXPECTED = FIH_INT_INIT_GLOBAL(
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200238 SET_MASK_SIG_TLV_EXPECTED);
239
240#define SET_MASK_IMAGE_TLV_SEC_CNT ((signed)0x005C0000)
INFINEON\DovhalA3b578f32024-11-29 01:06:04 +0200241fih_int FIH_MASK_IMAGE_TLV_SEC_CNT = FIH_INT_INIT_GLOBAL(
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200242 SET_MASK_IMAGE_TLV_SEC_CNT);
243
244#define CHK_MASK_IMAGE_TLV_SHA256 SET_MASK_IMAGE_TLV_SHA256
245
246#if defined MCUBOOT_SKIP_VALIDATE_SECONDARY_SLOT
247 #if defined MCUBOOT_VALIDATE_PRIMARY_SLOT
248 #error Boot slot validation cannot be enabled if upgrade slot validation is disabled
249 #endif
250#endif
251
252#if defined(MCUBOOT_SIGN_RSA) || \
253 defined(MCUBOOT_SIGN_EC) || \
254 defined(MCUBOOT_SIGN_EC256) || \
255 defined(MCUBOOT_SIGN_ED25519)
256
257 #if defined MCUBOOT_SKIP_VALIDATE_SECONDARY_SLOT
258 #define CHK_MASK_SIG_TLV_EXPECTED ((signed)0)
259 #else
260 #define CHK_MASK_SIG_TLV_EXPECTED SET_MASK_SIG_TLV_EXPECTED
261 #endif /* MCUBOOT_SKIP_VALIDATE_SECONDARY_SLOT */
262#else
263 #define CHK_MASK_SIG_TLV_EXPECTED ((signed)0)
264#endif /* defined(MCUBOOT_SIGN_RSA) ||
265 defined(MCUBOOT_SIGN_EC) ||
266 defined(MCUBOOT_SIGN_EC256) ||
267 defined(MCUBOOT_SIGN_ED25519) */
268
269#ifdef MCUBOOT_HW_ROLLBACK_PROT
270 #define CHK_MASK_IMAGE_TLV_SEC_CNT SET_MASK_IMAGE_TLV_SEC_CNT
271#else
272 #define CHK_MASK_IMAGE_TLV_SEC_CNT ((signed)0)
273#endif /* MCUBOOT_HW_ROLLBACK_PROT */
274
INFINEON\DovhalA3b578f32024-11-29 01:06:04 +0200275fih_int FIH_IMG_VALIDATE_COMPLEX_OK = FIH_INT_INIT_GLOBAL( \
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200276 CHK_MASK_IMAGE_TLV_SHA256 | \
277 CHK_MASK_SIG_TLV_EXPECTED | \
278 CHK_MASK_IMAGE_TLV_SEC_CNT);
279
280#undef SET_MASK_IMAGE_TLV_SHA256
281#undef SET_MASK_SIG_TLV_EXPECTED
282#undef SET_MASK_IMAGE_TLV_SEC_CNT
283
284#undef CHK_MASK_IMAGE_TLV_SHA256
285#undef CHK_MASK_SIG_TLV_EXPECTED
286#undef CHK_MASK_IMAGE_TLV_SEC_CNT
287
David Brown43cda332017-09-01 09:53:23 -0600288#ifdef EXPECTED_SIG_TLV
David Vincze03368b82020-04-01 12:53:53 +0200289#if !defined(MCUBOOT_HW_KEY)
David Brown43cda332017-09-01 09:53:23 -0600290static int
291bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
292{
293 bootutil_sha256_context sha256_ctx;
294 int i;
295 const struct bootutil_key *key;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300296 uint8_t hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
David Brown43cda332017-09-01 09:53:23 -0600297
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300298 if (keyhash_len != BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE) {
Fabio Utzig15c14672019-08-09 07:45:20 -0300299 return -1;
300 }
David Brown43cda332017-09-01 09:53:23 -0600301
302 for (i = 0; i < bootutil_key_cnt; i++) {
303 key = &bootutil_keys[i];
304 bootutil_sha256_init(&sha256_ctx);
305 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
306 bootutil_sha256_finish(&sha256_ctx, hash);
307 if (!memcmp(hash, keyhash, keyhash_len)) {
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900308 bootutil_sha256_drop(&sha256_ctx);
David Brown43cda332017-09-01 09:53:23 -0600309 return i;
310 }
311 }
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900312 bootutil_sha256_drop(&sha256_ctx);
David Brown43cda332017-09-01 09:53:23 -0600313 return -1;
314}
David Vincze03368b82020-04-01 12:53:53 +0200315#else
316extern unsigned int pub_key_len;
317static int
318bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
319{
320 bootutil_sha256_context sha256_ctx;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300321 uint8_t hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
322 uint8_t key_hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
David Vincze03368b82020-04-01 12:53:53 +0200323 size_t key_hash_size = sizeof(key_hash);
324 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100325 fih_int fih_rc;
David Vincze03368b82020-04-01 12:53:53 +0200326
327 bootutil_sha256_init(&sha256_ctx);
328 bootutil_sha256_update(&sha256_ctx, key, key_len);
329 bootutil_sha256_finish(&sha256_ctx, hash);
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900330 bootutil_sha256_drop(&sha256_ctx);
David Vincze03368b82020-04-01 12:53:53 +0200331
332 rc = boot_retrieve_public_key_hash(image_index, key_hash, &key_hash_size);
333 if (rc) {
334 return rc;
335 }
336
Raef Colese8fe6cf2020-05-26 13:07:40 +0100337 /* Adding hardening to avoid this potential attack:
338 * - Image is signed with an arbitrary key and the corresponding public
339 * key is added as a TLV field.
340 * - During public key validation (comparing against key-hash read from
341 * HW) a fault is injected to accept the public key as valid one.
342 */
343 FIH_CALL(boot_fih_memequal, fih_rc, hash, key_hash, key_hash_size);
INFINEON\DovhalA3b578f32024-11-29 01:06:04 +0200344 if (fih_eq(fih_rc, FIH_SUCCESS)) {
David Vincze03368b82020-04-01 12:53:53 +0200345 bootutil_keys[0].key = key;
346 pub_key_len = key_len;
347 return 0;
348 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100349
David Vincze03368b82020-04-01 12:53:53 +0200350 return -1;
351}
352#endif /* !MCUBOOT_HW_KEY */
David Brown43cda332017-09-01 09:53:23 -0600353#endif
354
David Vinczec3084132020-02-18 14:50:47 +0100355#ifdef MCUBOOT_HW_ROLLBACK_PROT
356/**
357 * Reads the value of an image's security counter.
358 *
359 * @param hdr Pointer to the image header structure.
360 * @param fap Pointer to a description structure of the image's
361 * flash area.
362 * @param security_cnt Pointer to store the security counter value.
363 *
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300364 * @return FIH_SUCCESS on success; FIH_FAILURE on failure.
David Vinczec3084132020-02-18 14:50:47 +0100365 */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300366fih_int
David Vinczec3084132020-02-18 14:50:47 +0100367bootutil_get_img_security_cnt(struct image_header *hdr,
368 const struct flash_area *fap,
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300369 fih_uint *img_security_cnt)
David Vinczec3084132020-02-18 14:50:47 +0100370{
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300371 uint32_t img_sec_cnt = 0u;
372 uint32_t img_chk_cnt = 0u;
373 struct image_tlv_iter it = {0};
374 uint32_t off = 0u;
375 uint16_t len = 0u;
376 int32_t rc = -1;
377 fih_int fih_rc = FIH_FAILURE;
David Vinczec3084132020-02-18 14:50:47 +0100378
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300379 if ((NULL == hdr) ||
380 (NULL == fap) ||
381 (NULL == img_security_cnt)) {
David Vinczec3084132020-02-18 14:50:47 +0100382 /* Invalid parameter. */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300383 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100384 }
385
386 /* The security counter TLV is in the protected part of the TLV area. */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300387 if (0u == hdr->ih_protect_tlv_size) {
388 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100389 }
390
391 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_SEC_CNT, true);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300392 if (rc != 0) {
393 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100394 }
395
396 /* Traverse through the protected TLV area to find
397 * the security counter TLV.
398 */
399
400 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
401 if (rc != 0) {
402 /* Security counter TLV has not been found. */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300403 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100404 }
405
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300406 if (len != sizeof(img_sec_cnt)) {
David Vinczec3084132020-02-18 14:50:47 +0100407 /* Security counter is not valid. */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300408 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100409 }
410
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300411 rc = LOAD_IMAGE_DATA(hdr, fap, off, &img_sec_cnt, len);
David Vinczec3084132020-02-18 14:50:47 +0100412 if (rc != 0) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300413 goto out;
David Vinczec3084132020-02-18 14:50:47 +0100414 }
415
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300416 *img_security_cnt = fih_uint_encode(img_sec_cnt);
417
418 rc = LOAD_IMAGE_DATA(hdr, fap, off, &img_chk_cnt, len);
419 if (rc != 0) {
420 goto out;
421 }
422
INFINEON\DovhalA3b578f32024-11-29 01:06:04 +0200423 if (fih_uint_eq(fih_uint_encode(img_chk_cnt), *img_security_cnt)) {
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300424
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;
INFINEON\DovhalA3b578f32024-11-29 01:06:04 +0200532#ifdef CYW20829
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300533 uint8_t reprov_packet[REPROV_PACK_SIZE];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100534 fih_int security_counter_valid = FIH_FAILURE;
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300535 fih_uint extracted_img_cnt = FIH_UINT_MAX;
INFINEON\DovhalA3b578f32024-11-29 01:06:04 +0200536#endif /* CYW20829 */
Roman Okhrimenko25165622023-05-23 08:58:29 +0300537#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
INFINEON\DovhalA3b578f32024-11-29 01:06:04 +0200582 if (fih_eq(fih_rc, FIH_SUCCESS)) {
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200583 /* 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
INFINEON\DovhalA3b578f32024-11-29 01:06:04 +0200649 if (fih_eq(FIH_SUCCESS, valid_signature)) {
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200650 /* 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);
INFINEON\DovhalA3b578f32024-11-29 01:06:04 +0200680 if (!fih_eq(fih_rc, FIH_SUCCESS)) {
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
INFINEON\DovhalA3b578f32024-11-29 01:06:04 +0200693 if (!fih_eq(fih_rc, FIH_SUCCESS)) {
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 */
INFINEON\DovhalA3b578f32024-11-29 01:06:04 +0200707 if (fih_uint_ge(fih_uint_encode(img_security_cnt), security_cnt)) {
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200708 /* 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. */
INFINEON\DovhalA3b578f32024-11-29 01:06:04 +0200713 security_counter_valid = FIH_INT_INIT(HW_ROLLBACK_CNT_VALID);
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200714#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
INFINEON\DovhalA3b578f32024-11-29 01:06:04 +0200724 if (fih_eq(security_counter_valid, FIH_INT_INIT(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
INFINEON\DovhalA3b578f32024-11-29 01:06:04 +0200755 if (!fih_eq(security_counter_valid, FIH_INT_INIT(REPROV_PACK_VALID | HW_ROLLBACK_CNT_VALID))) {
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}