blob: 7e0af0d788609e076be18ac13589d3d5ef67ec2f [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
7 *
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"
David Browne629bf32017-04-24 13:37:33 -060036#include "bootutil/sha256.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"
Christopher Collins92ea77f2016-12-12 15:59:26 -080039
Fabio Utzigba1fbe62017-07-21 14:01:20 -030040#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030041
Fabio Utzigba829042018-09-18 08:29:34 -030042#ifdef MCUBOOT_ENC_IMAGES
43#include "bootutil/enc_key.h"
44#endif
45#if defined(MCUBOOT_SIGN_RSA)
Christopher Collins92ea77f2016-12-12 15:59:26 -080046#include "mbedtls/rsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060047#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -030048#if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080049#include "mbedtls/ecdsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060050#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080051#include "mbedtls/asn1.h"
52
53#include "bootutil_priv.h"
54
55/*
56 * Compute SHA256 over the image.
57 */
58static int
Fabio Utzig10ee6482019-08-01 12:04:52 -030059bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
60 struct image_header *hdr, const struct flash_area *fap,
61 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *hash_result,
62 uint8_t *seed, int seed_len)
Christopher Collins92ea77f2016-12-12 15:59:26 -080063{
David Browne629bf32017-04-24 13:37:33 -060064 bootutil_sha256_context sha256_ctx;
Christopher Collins92ea77f2016-12-12 15:59:26 -080065 uint32_t blk_sz;
66 uint32_t size;
Fabio Utzig2fc80df2018-12-14 06:47:38 -020067 uint16_t hdr_size;
Christopher Collins92ea77f2016-12-12 15:59:26 -080068 uint32_t off;
69 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -030070 uint32_t blk_off;
Fabio Utzige52c08e2019-09-11 19:32:00 -030071 uint32_t tlv_off;
Christopher Collins92ea77f2016-12-12 15:59:26 -080072
Fabio Utzigb0f04732019-07-31 09:49:19 -030073#if (BOOT_IMAGE_NUMBER == 1) || !defined(MCUBOOT_ENC_IMAGES)
Fabio Utzig10ee6482019-08-01 12:04:52 -030074 (void)enc_state;
Fabio Utzigb0f04732019-07-31 09:49:19 -030075 (void)image_index;
Fabio Utzigc9621352019-08-08 12:15:51 -030076 (void)hdr_size;
Fabio Utzige52c08e2019-09-11 19:32:00 -030077 (void)blk_off;
78 (void)tlv_off;
Fabio Utzigb0f04732019-07-31 09:49:19 -030079#endif
80
Fabio Utzigbc077932019-08-26 11:16:34 -030081#ifdef MCUBOOT_ENC_IMAGES
82 /* Encrypted images only exist in the secondary slot */
83 if (MUST_DECRYPT(fap, image_index, hdr) &&
84 !boot_enc_valid(enc_state, image_index, fap)) {
85 return -1;
86 }
87#endif
88
David Browne629bf32017-04-24 13:37:33 -060089 bootutil_sha256_init(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -080090
91 /* in some cases (split image) the hash is seeded with data from
92 * the loader image */
Fabio Utzig53986042017-12-12 14:57:07 -020093 if (seed && (seed_len > 0)) {
David Browne629bf32017-04-24 13:37:33 -060094 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -080095 }
96
David Vinczee32483f2019-06-13 10:46:24 +020097 /* Hash is computed over image header and image itself. */
Fabio Utzige52c08e2019-09-11 19:32:00 -030098 size = hdr_size = hdr->ih_hdr_size;
99 size += hdr->ih_img_size;
100 tlv_off = size;
David Vinczee32483f2019-06-13 10:46:24 +0200101
Fabio Utzige52c08e2019-09-11 19:32:00 -0300102 /* If protected TLVs are present they are also hashed. */
103 size += hdr->ih_protect_tlv_size;
David Vinczee32483f2019-06-13 10:46:24 +0200104
Christopher Collins92ea77f2016-12-12 15:59:26 -0800105 for (off = 0; off < size; off += blk_sz) {
106 blk_sz = size - off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300107 if (blk_sz > tmp_buf_sz) {
108 blk_sz = tmp_buf_sz;
109 }
Fabio Utzigc21c2102019-09-10 10:10:42 -0300110#ifdef MCUBOOT_ENC_IMAGES
111 /* The only data that is encrypted in an image is the payload;
112 * both header and TLVs (when protected) are not.
113 */
114 if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) {
115 /* read only the header */
116 blk_sz = hdr_size - off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300117 }
118 if ((off < tlv_off) && ((off + blk_sz) > tlv_off)) {
119 /* read only up to the end of the image payload */
120 blk_sz = tlv_off - off;
Fabio Utzigc21c2102019-09-10 10:10:42 -0300121 }
122#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800123 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
124 if (rc) {
125 return rc;
126 }
Fabio Utzigba829042018-09-18 08:29:34 -0300127#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzigc21c2102019-09-10 10:10:42 -0300128 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzige52c08e2019-09-11 19:32:00 -0300129 /* Only payload is encrypted (area between header and TLVs) */
130 if (off >= hdr_size && off < tlv_off) {
Fabio Utzigc21c2102019-09-10 10:10:42 -0300131 blk_off = (off - hdr_size) & 0xf;
132 boot_encrypt(enc_state, image_index, fap, off - hdr_size,
133 blk_sz, blk_off, tmp_buf);
134 }
Fabio Utzigba829042018-09-18 08:29:34 -0300135 }
136#endif
David Browne629bf32017-04-24 13:37:33 -0600137 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800138 }
David Browne629bf32017-04-24 13:37:33 -0600139 bootutil_sha256_finish(&sha256_ctx, hash_result);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800140
141 return 0;
142}
143
144/*
David Brown43cda332017-09-01 09:53:23 -0600145 * Currently, we only support being able to verify one type of
146 * signature, because there is a single verification function that we
147 * call. List the type of TLV we are expecting. If we aren't
148 * configured for any signature, don't define this macro.
149 */
Fabio Utzig48764842019-05-10 19:28:24 -0300150#if (defined(MCUBOOT_SIGN_RSA) + \
151 defined(MCUBOOT_SIGN_EC) + \
152 defined(MCUBOOT_SIGN_EC256) + \
153 defined(MCUBOOT_SIGN_ED25519)) > 1
Fabio Utzig3501c012019-05-13 15:07:25 -0700154#error "Only a single signature type is supported!"
155#endif
156
David Brown43cda332017-09-01 09:53:23 -0600157#if defined(MCUBOOT_SIGN_RSA)
Fabio Utzig3501c012019-05-13 15:07:25 -0700158# if MCUBOOT_SIGN_RSA_LEN == 2048
159# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
160# elif MCUBOOT_SIGN_RSA_LEN == 3072
161# define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS
162# else
163# error "Unsupported RSA signature length"
David Brown43cda332017-09-01 09:53:23 -0600164# endif
Fabio Utzig3501c012019-05-13 15:07:25 -0700165# define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8)
166# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) /* 2048 bits */
David Brown43cda332017-09-01 09:53:23 -0600167#elif defined(MCUBOOT_SIGN_EC)
168# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224
Fabio Utzig3501c012019-05-13 15:07:25 -0700169# define SIG_BUF_SIZE 128
David Browna3608262019-12-12 15:35:31 -0700170# define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */
David Brown43cda332017-09-01 09:53:23 -0600171#elif defined(MCUBOOT_SIGN_EC256)
172# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
Fabio Utzig3501c012019-05-13 15:07:25 -0700173# define SIG_BUF_SIZE 128
David Browna3608262019-12-12 15:35:31 -0700174# define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */
Fabio Utzig48764842019-05-10 19:28:24 -0300175#elif defined(MCUBOOT_SIGN_ED25519)
176# define EXPECTED_SIG_TLV IMAGE_TLV_ED25519
177# define SIG_BUF_SIZE 64
178# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE)
Fabio Utzig3501c012019-05-13 15:07:25 -0700179#else
180# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
David Brown43cda332017-09-01 09:53:23 -0600181#endif
182
183#ifdef EXPECTED_SIG_TLV
David Vincze03368b82020-04-01 12:53:53 +0200184#if !defined(MCUBOOT_HW_KEY)
David Brown43cda332017-09-01 09:53:23 -0600185static int
186bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
187{
188 bootutil_sha256_context sha256_ctx;
189 int i;
190 const struct bootutil_key *key;
191 uint8_t hash[32];
192
Fabio Utzig15c14672019-08-09 07:45:20 -0300193 if (keyhash_len > 32) {
194 return -1;
195 }
David Brown43cda332017-09-01 09:53:23 -0600196
197 for (i = 0; i < bootutil_key_cnt; i++) {
198 key = &bootutil_keys[i];
199 bootutil_sha256_init(&sha256_ctx);
200 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
201 bootutil_sha256_finish(&sha256_ctx, hash);
202 if (!memcmp(hash, keyhash, keyhash_len)) {
203 return i;
204 }
205 }
206 return -1;
207}
David Vincze03368b82020-04-01 12:53:53 +0200208#else
209extern unsigned int pub_key_len;
210static int
211bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
212{
213 bootutil_sha256_context sha256_ctx;
214 uint8_t hash[32];
215 uint8_t key_hash[32];
216 size_t key_hash_size = sizeof(key_hash);
217 int rc;
218
219 bootutil_sha256_init(&sha256_ctx);
220 bootutil_sha256_update(&sha256_ctx, key, key_len);
221 bootutil_sha256_finish(&sha256_ctx, hash);
222
223 rc = boot_retrieve_public_key_hash(image_index, key_hash, &key_hash_size);
224 if (rc) {
225 return rc;
226 }
227
228 if (!memcmp(hash, key_hash, key_hash_size)) {
229 bootutil_keys[0].key = key;
230 pub_key_len = key_len;
231 return 0;
232 }
233 return -1;
234}
235#endif /* !MCUBOOT_HW_KEY */
David Brown43cda332017-09-01 09:53:23 -0600236#endif
237
David Vinczec3084132020-02-18 14:50:47 +0100238#ifdef MCUBOOT_HW_ROLLBACK_PROT
239/**
240 * Reads the value of an image's security counter.
241 *
242 * @param hdr Pointer to the image header structure.
243 * @param fap Pointer to a description structure of the image's
244 * flash area.
245 * @param security_cnt Pointer to store the security counter value.
246 *
247 * @return 0 on success; nonzero on failure.
248 */
249int32_t
250bootutil_get_img_security_cnt(struct image_header *hdr,
251 const struct flash_area *fap,
252 uint32_t *img_security_cnt)
253{
254 struct image_tlv_iter it;
255 uint32_t off;
256 uint16_t len;
257 int32_t rc;
258
259 if ((hdr == NULL) ||
260 (fap == NULL) ||
261 (img_security_cnt == NULL)) {
262 /* Invalid parameter. */
263 return BOOT_EBADARGS;
264 }
265
266 /* The security counter TLV is in the protected part of the TLV area. */
267 if (hdr->ih_protect_tlv_size == 0) {
268 return BOOT_EBADIMAGE;
269 }
270
271 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_SEC_CNT, true);
272 if (rc) {
273 return rc;
274 }
275
276 /* Traverse through the protected TLV area to find
277 * the security counter TLV.
278 */
279
280 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
281 if (rc != 0) {
282 /* Security counter TLV has not been found. */
283 return -1;
284 }
285
286 if (len != sizeof(*img_security_cnt)) {
287 /* Security counter is not valid. */
288 return BOOT_EBADIMAGE;
289 }
290
291 rc = flash_area_read(fap, off, img_security_cnt, len);
292 if (rc != 0) {
293 return BOOT_EFLASH;
294 }
295
296 return 0;
297}
298#endif /* MCUBOOT_HW_ROLLBACK_PROT */
299
David Brown43cda332017-09-01 09:53:23 -0600300/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800301 * Verify the integrity of the image.
302 * Return non-zero if image could not be validated/does not validate.
303 */
304int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300305bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
306 struct image_header *hdr, const struct flash_area *fap,
307 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *seed,
308 int seed_len, uint8_t *out_hash)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800309{
310 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300311 uint16_t len;
David Brownd13318a2019-12-04 17:28:40 -0700312 uint16_t type;
David Brown43cda332017-09-01 09:53:23 -0600313 int sha256_valid = 0;
314#ifdef EXPECTED_SIG_TLV
315 int valid_signature = 0;
316 int key_id = -1;
David Vincze03368b82020-04-01 12:53:53 +0200317#ifdef MCUBOOT_HW_KEY
318 /* Few extra bytes for encoding and for public exponent. */
319 uint8_t key_buf[SIG_BUF_SIZE + 24];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800320#endif
David Vincze03368b82020-04-01 12:53:53 +0200321#endif /* EXPECTED_SIG_TLV */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300322 struct image_tlv_iter it;
Fabio Utzig3501c012019-05-13 15:07:25 -0700323 uint8_t buf[SIG_BUF_SIZE];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800324 uint8_t hash[32];
325 int rc;
David Vinczec3084132020-02-18 14:50:47 +0100326#ifdef MCUBOOT_HW_ROLLBACK_PROT
327 uint32_t security_cnt = UINT32_MAX;
328 uint32_t img_security_cnt = 0;
329 int32_t security_counter_valid = 0;
330#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800331
Fabio Utzig10ee6482019-08-01 12:04:52 -0300332 rc = bootutil_img_hash(enc_state, image_index, hdr, fap, tmp_buf,
333 tmp_buf_sz, hash, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800334 if (rc) {
335 return rc;
336 }
337
338 if (out_hash) {
339 memcpy(out_hash, hash, 32);
340 }
341
Fabio Utzig61fd8882019-09-14 20:00:20 -0300342 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false);
David Brownf5b33d82017-09-01 10:58:27 -0600343 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300344 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600345 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800346
David Brown43cda332017-09-01 09:53:23 -0600347 /*
348 * Traverse through all of the TLVs, performing any checks we know
349 * and are able to do.
350 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300351 while (true) {
352 rc = bootutil_tlv_iter_next(&it, &off, &len, &type);
353 if (rc < 0) {
354 return -1;
355 } else if (rc > 0) {
356 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800357 }
David Brown43cda332017-09-01 09:53:23 -0600358
Fabio Utzig61fd8882019-09-14 20:00:20 -0300359 if (type == IMAGE_TLV_SHA256) {
David Brown43cda332017-09-01 09:53:23 -0600360 /*
361 * Verify the SHA256 image hash. This must always be
362 * present.
363 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300364 if (len != sizeof(hash)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800365 return -1;
366 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300367 rc = flash_area_read(fap, off, buf, sizeof hash);
David Brown43cda332017-09-01 09:53:23 -0600368 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300369 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800370 }
David Brown43cda332017-09-01 09:53:23 -0600371 if (memcmp(hash, buf, sizeof(hash))) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300372 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800373 }
David Brown43cda332017-09-01 09:53:23 -0600374
375 sha256_valid = 1;
376#ifdef EXPECTED_SIG_TLV
David Vincze03368b82020-04-01 12:53:53 +0200377#ifndef MCUBOOT_HW_KEY
Fabio Utzig61fd8882019-09-14 20:00:20 -0300378 } else if (type == IMAGE_TLV_KEYHASH) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800379 /*
David Brown43cda332017-09-01 09:53:23 -0600380 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800381 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300382 if (len > 32) {
David Brown43cda332017-09-01 09:53:23 -0600383 return -1;
384 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300385 rc = flash_area_read(fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600386 if (rc) {
387 return rc;
388 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300389 key_id = bootutil_find_key(buf, len);
David Brown43cda332017-09-01 09:53:23 -0600390 /*
391 * The key may not be found, which is acceptable. There
392 * can be multiple signatures, each preceded by a key.
393 */
David Vincze03368b82020-04-01 12:53:53 +0200394#else
395 } else if (type == IMAGE_TLV_PUBKEY) {
396 /*
397 * Determine which key we should be checking.
398 */
399 if (len > sizeof(key_buf)) {
400 return -1;
401 }
402 rc = flash_area_read(fap, off, key_buf, len);
403 if (rc) {
404 return rc;
405 }
406 key_id = bootutil_find_key(image_index, key_buf, len);
407 /*
408 * The key may not be found, which is acceptable. There
409 * can be multiple signatures, each preceded by a key.
410 */
411#endif /* !MCUBOOT_HW_KEY */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300412 } else if (type == EXPECTED_SIG_TLV) {
David Brown43cda332017-09-01 09:53:23 -0600413 /* Ignore this signature if it is out of bounds. */
414 if (key_id < 0 || key_id >= bootutil_key_cnt) {
415 key_id = -1;
416 continue;
417 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300418 if (!EXPECTED_SIG_LEN(len) || len > sizeof(buf)) {
David Brown43cda332017-09-01 09:53:23 -0600419 return -1;
420 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300421 rc = flash_area_read(fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600422 if (rc) {
423 return -1;
424 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300425 rc = bootutil_verify_sig(hash, sizeof(hash), buf, len, key_id);
David Brown43cda332017-09-01 09:53:23 -0600426 if (rc == 0) {
427 valid_signature = 1;
428 }
429 key_id = -1;
David Vinczec3084132020-02-18 14:50:47 +0100430#endif /* EXPECTED_SIG_TLV */
431#ifdef MCUBOOT_HW_ROLLBACK_PROT
432 } else if (type == IMAGE_TLV_SEC_CNT) {
433 /*
434 * Verify the image's security counter.
435 * This must always be present.
436 */
437 if (len != sizeof(img_security_cnt)) {
438 /* Security counter is not valid. */
439 return -1;
440 }
441
442 rc = flash_area_read(fap, off, &img_security_cnt, len);
443 if (rc) {
444 return rc;
445 }
446
447 rc = boot_nv_security_counter_get(image_index, &security_cnt);
448 if (rc) {
449 return rc;
450 }
451
452 /* Compare the new image's security counter value against the
453 * stored security counter value.
454 */
455 if (img_security_cnt < security_cnt) {
456 /* The image's security counter is not accepted. */
457 return -1;
458 }
459
460 /* The image's security counter has been successfully verified. */
461 security_counter_valid = 1;
462#endif /* MCUBOOT_HW_ROLLBACK_PROT */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800463 }
464 }
David Brown43cda332017-09-01 09:53:23 -0600465
466 if (!sha256_valid) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800467 return -1;
David Brown43cda332017-09-01 09:53:23 -0600468#ifdef EXPECTED_SIG_TLV
David Vinczec3084132020-02-18 14:50:47 +0100469 } else if (!valid_signature) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800470 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800471#endif
David Vinczec3084132020-02-18 14:50:47 +0100472#ifdef MCUBOOT_HW_ROLLBACK_PROT
473 } else if (!security_counter_valid) {
474 return -1;
475#endif
476 }
David Brown43cda332017-09-01 09:53:23 -0600477
Christopher Collins92ea77f2016-12-12 15:59:26 -0800478 return 0;
479}