blob: b16c03887d86681a547882277d12a97e7fa26a21 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
David Vinczee32483f2019-06-13 10:46:24 +020020/*
David Vinczec3084132020-02-18 14:50:47 +010021 * Modifications are Copyright (c) 2019-2020 Arm Limited.
David Vinczee32483f2019-06-13 10:46:24 +020022 */
23
Christopher Collins92ea77f2016-12-12 15:59:26 -080024#include <stddef.h>
David Vinczec3084132020-02-18 14:50:47 +010025#include <stdint.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080026#include <inttypes.h>
27#include <string.h>
28
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020029#include <flash_map_backend/flash_map_backend.h>
30
Christopher Collins92ea77f2016-12-12 15:59:26 -080031#include "bootutil/image.h"
David Browne629bf32017-04-24 13:37:33 -060032#include "bootutil/sha256.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080033#include "bootutil/sign_key.h"
David Vinczec3084132020-02-18 14:50:47 +010034#include "bootutil/security_cnt.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080035
Fabio Utzigba1fbe62017-07-21 14:01:20 -030036#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030037
Fabio Utzigba829042018-09-18 08:29:34 -030038#ifdef MCUBOOT_ENC_IMAGES
39#include "bootutil/enc_key.h"
40#endif
41#if defined(MCUBOOT_SIGN_RSA)
Christopher Collins92ea77f2016-12-12 15:59:26 -080042#include "mbedtls/rsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060043#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -030044#if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080045#include "mbedtls/ecdsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060046#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080047#include "mbedtls/asn1.h"
48
49#include "bootutil_priv.h"
50
51/*
52 * Compute SHA256 over the image.
53 */
54static int
Fabio Utzig10ee6482019-08-01 12:04:52 -030055bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
56 struct image_header *hdr, const struct flash_area *fap,
57 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *hash_result,
58 uint8_t *seed, int seed_len)
Christopher Collins92ea77f2016-12-12 15:59:26 -080059{
David Browne629bf32017-04-24 13:37:33 -060060 bootutil_sha256_context sha256_ctx;
Christopher Collins92ea77f2016-12-12 15:59:26 -080061 uint32_t blk_sz;
62 uint32_t size;
Fabio Utzig2fc80df2018-12-14 06:47:38 -020063 uint16_t hdr_size;
Christopher Collins92ea77f2016-12-12 15:59:26 -080064 uint32_t off;
65 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -030066 uint32_t blk_off;
Fabio Utzige52c08e2019-09-11 19:32:00 -030067 uint32_t tlv_off;
Christopher Collins92ea77f2016-12-12 15:59:26 -080068
Fabio Utzigb0f04732019-07-31 09:49:19 -030069#if (BOOT_IMAGE_NUMBER == 1) || !defined(MCUBOOT_ENC_IMAGES)
Fabio Utzig10ee6482019-08-01 12:04:52 -030070 (void)enc_state;
Fabio Utzigb0f04732019-07-31 09:49:19 -030071 (void)image_index;
Fabio Utzigc9621352019-08-08 12:15:51 -030072 (void)hdr_size;
Fabio Utzige52c08e2019-09-11 19:32:00 -030073 (void)blk_off;
74 (void)tlv_off;
Fabio Utzigb0f04732019-07-31 09:49:19 -030075#endif
76
Fabio Utzigbc077932019-08-26 11:16:34 -030077#ifdef MCUBOOT_ENC_IMAGES
78 /* Encrypted images only exist in the secondary slot */
79 if (MUST_DECRYPT(fap, image_index, hdr) &&
80 !boot_enc_valid(enc_state, image_index, fap)) {
81 return -1;
82 }
83#endif
84
David Browne629bf32017-04-24 13:37:33 -060085 bootutil_sha256_init(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -080086
87 /* in some cases (split image) the hash is seeded with data from
88 * the loader image */
Fabio Utzig53986042017-12-12 14:57:07 -020089 if (seed && (seed_len > 0)) {
David Browne629bf32017-04-24 13:37:33 -060090 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -080091 }
92
David Vinczee32483f2019-06-13 10:46:24 +020093 /* Hash is computed over image header and image itself. */
Fabio Utzige52c08e2019-09-11 19:32:00 -030094 size = hdr_size = hdr->ih_hdr_size;
95 size += hdr->ih_img_size;
96 tlv_off = size;
David Vinczee32483f2019-06-13 10:46:24 +020097
Fabio Utzige52c08e2019-09-11 19:32:00 -030098 /* If protected TLVs are present they are also hashed. */
99 size += hdr->ih_protect_tlv_size;
David Vinczee32483f2019-06-13 10:46:24 +0200100
Christopher Collins92ea77f2016-12-12 15:59:26 -0800101 for (off = 0; off < size; off += blk_sz) {
102 blk_sz = size - off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300103 if (blk_sz > tmp_buf_sz) {
104 blk_sz = tmp_buf_sz;
105 }
Fabio Utzigc21c2102019-09-10 10:10:42 -0300106#ifdef MCUBOOT_ENC_IMAGES
107 /* The only data that is encrypted in an image is the payload;
108 * both header and TLVs (when protected) are not.
109 */
110 if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) {
111 /* read only the header */
112 blk_sz = hdr_size - off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300113 }
114 if ((off < tlv_off) && ((off + blk_sz) > tlv_off)) {
115 /* read only up to the end of the image payload */
116 blk_sz = tlv_off - off;
Fabio Utzigc21c2102019-09-10 10:10:42 -0300117 }
118#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800119 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
120 if (rc) {
121 return rc;
122 }
Fabio Utzigba829042018-09-18 08:29:34 -0300123#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzigc21c2102019-09-10 10:10:42 -0300124 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzige52c08e2019-09-11 19:32:00 -0300125 /* Only payload is encrypted (area between header and TLVs) */
126 if (off >= hdr_size && off < tlv_off) {
Fabio Utzigc21c2102019-09-10 10:10:42 -0300127 blk_off = (off - hdr_size) & 0xf;
128 boot_encrypt(enc_state, image_index, fap, off - hdr_size,
129 blk_sz, blk_off, tmp_buf);
130 }
Fabio Utzigba829042018-09-18 08:29:34 -0300131 }
132#endif
David Browne629bf32017-04-24 13:37:33 -0600133 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800134 }
David Browne629bf32017-04-24 13:37:33 -0600135 bootutil_sha256_finish(&sha256_ctx, hash_result);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800136
137 return 0;
138}
139
140/*
David Brown43cda332017-09-01 09:53:23 -0600141 * Currently, we only support being able to verify one type of
142 * signature, because there is a single verification function that we
143 * call. List the type of TLV we are expecting. If we aren't
144 * configured for any signature, don't define this macro.
145 */
Fabio Utzig48764842019-05-10 19:28:24 -0300146#if (defined(MCUBOOT_SIGN_RSA) + \
147 defined(MCUBOOT_SIGN_EC) + \
148 defined(MCUBOOT_SIGN_EC256) + \
149 defined(MCUBOOT_SIGN_ED25519)) > 1
Fabio Utzig3501c012019-05-13 15:07:25 -0700150#error "Only a single signature type is supported!"
151#endif
152
David Brown43cda332017-09-01 09:53:23 -0600153#if defined(MCUBOOT_SIGN_RSA)
Fabio Utzig3501c012019-05-13 15:07:25 -0700154# if MCUBOOT_SIGN_RSA_LEN == 2048
155# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
156# elif MCUBOOT_SIGN_RSA_LEN == 3072
157# define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS
158# else
159# error "Unsupported RSA signature length"
David Brown43cda332017-09-01 09:53:23 -0600160# endif
Fabio Utzig3501c012019-05-13 15:07:25 -0700161# define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8)
162# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) /* 2048 bits */
David Brown43cda332017-09-01 09:53:23 -0600163#elif defined(MCUBOOT_SIGN_EC)
164# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224
Fabio Utzig3501c012019-05-13 15:07:25 -0700165# define SIG_BUF_SIZE 128
David Browna3608262019-12-12 15:35:31 -0700166# define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */
David Brown43cda332017-09-01 09:53:23 -0600167#elif defined(MCUBOOT_SIGN_EC256)
168# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
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 */
Fabio Utzig48764842019-05-10 19:28:24 -0300171#elif defined(MCUBOOT_SIGN_ED25519)
172# define EXPECTED_SIG_TLV IMAGE_TLV_ED25519
173# define SIG_BUF_SIZE 64
174# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE)
Fabio Utzig3501c012019-05-13 15:07:25 -0700175#else
176# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
David Brown43cda332017-09-01 09:53:23 -0600177#endif
178
179#ifdef EXPECTED_SIG_TLV
David Vincze03368b82020-04-01 12:53:53 +0200180#if !defined(MCUBOOT_HW_KEY)
David Brown43cda332017-09-01 09:53:23 -0600181static int
182bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
183{
184 bootutil_sha256_context sha256_ctx;
185 int i;
186 const struct bootutil_key *key;
187 uint8_t hash[32];
188
Fabio Utzig15c14672019-08-09 07:45:20 -0300189 if (keyhash_len > 32) {
190 return -1;
191 }
David Brown43cda332017-09-01 09:53:23 -0600192
193 for (i = 0; i < bootutil_key_cnt; i++) {
194 key = &bootutil_keys[i];
195 bootutil_sha256_init(&sha256_ctx);
196 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
197 bootutil_sha256_finish(&sha256_ctx, hash);
198 if (!memcmp(hash, keyhash, keyhash_len)) {
199 return i;
200 }
201 }
202 return -1;
203}
David Vincze03368b82020-04-01 12:53:53 +0200204#else
205extern unsigned int pub_key_len;
206static int
207bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
208{
209 bootutil_sha256_context sha256_ctx;
210 uint8_t hash[32];
211 uint8_t key_hash[32];
212 size_t key_hash_size = sizeof(key_hash);
213 int rc;
214
215 bootutil_sha256_init(&sha256_ctx);
216 bootutil_sha256_update(&sha256_ctx, key, key_len);
217 bootutil_sha256_finish(&sha256_ctx, hash);
218
219 rc = boot_retrieve_public_key_hash(image_index, key_hash, &key_hash_size);
220 if (rc) {
221 return rc;
222 }
223
224 if (!memcmp(hash, key_hash, key_hash_size)) {
225 bootutil_keys[0].key = key;
226 pub_key_len = key_len;
227 return 0;
228 }
229 return -1;
230}
231#endif /* !MCUBOOT_HW_KEY */
David Brown43cda332017-09-01 09:53:23 -0600232#endif
233
David Vinczec3084132020-02-18 14:50:47 +0100234#ifdef MCUBOOT_HW_ROLLBACK_PROT
235/**
236 * Reads the value of an image's security counter.
237 *
238 * @param hdr Pointer to the image header structure.
239 * @param fap Pointer to a description structure of the image's
240 * flash area.
241 * @param security_cnt Pointer to store the security counter value.
242 *
243 * @return 0 on success; nonzero on failure.
244 */
245int32_t
246bootutil_get_img_security_cnt(struct image_header *hdr,
247 const struct flash_area *fap,
248 uint32_t *img_security_cnt)
249{
250 struct image_tlv_iter it;
251 uint32_t off;
252 uint16_t len;
253 int32_t rc;
254
255 if ((hdr == NULL) ||
256 (fap == NULL) ||
257 (img_security_cnt == NULL)) {
258 /* Invalid parameter. */
259 return BOOT_EBADARGS;
260 }
261
262 /* The security counter TLV is in the protected part of the TLV area. */
263 if (hdr->ih_protect_tlv_size == 0) {
264 return BOOT_EBADIMAGE;
265 }
266
267 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_SEC_CNT, true);
268 if (rc) {
269 return rc;
270 }
271
272 /* Traverse through the protected TLV area to find
273 * the security counter TLV.
274 */
275
276 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
277 if (rc != 0) {
278 /* Security counter TLV has not been found. */
279 return -1;
280 }
281
282 if (len != sizeof(*img_security_cnt)) {
283 /* Security counter is not valid. */
284 return BOOT_EBADIMAGE;
285 }
286
287 rc = flash_area_read(fap, off, img_security_cnt, len);
288 if (rc != 0) {
289 return BOOT_EFLASH;
290 }
291
292 return 0;
293}
294#endif /* MCUBOOT_HW_ROLLBACK_PROT */
295
David Brown43cda332017-09-01 09:53:23 -0600296/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800297 * Verify the integrity of the image.
298 * Return non-zero if image could not be validated/does not validate.
299 */
300int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300301bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
302 struct image_header *hdr, const struct flash_area *fap,
303 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *seed,
304 int seed_len, uint8_t *out_hash)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800305{
306 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300307 uint16_t len;
David Brownd13318a2019-12-04 17:28:40 -0700308 uint16_t type;
David Brown43cda332017-09-01 09:53:23 -0600309 int sha256_valid = 0;
310#ifdef EXPECTED_SIG_TLV
311 int valid_signature = 0;
312 int key_id = -1;
David Vincze03368b82020-04-01 12:53:53 +0200313#ifdef MCUBOOT_HW_KEY
314 /* Few extra bytes for encoding and for public exponent. */
315 uint8_t key_buf[SIG_BUF_SIZE + 24];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800316#endif
David Vincze03368b82020-04-01 12:53:53 +0200317#endif /* EXPECTED_SIG_TLV */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300318 struct image_tlv_iter it;
Fabio Utzig3501c012019-05-13 15:07:25 -0700319 uint8_t buf[SIG_BUF_SIZE];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800320 uint8_t hash[32];
321 int rc;
David Vinczec3084132020-02-18 14:50:47 +0100322#ifdef MCUBOOT_HW_ROLLBACK_PROT
323 uint32_t security_cnt = UINT32_MAX;
324 uint32_t img_security_cnt = 0;
325 int32_t security_counter_valid = 0;
326#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800327
Fabio Utzig10ee6482019-08-01 12:04:52 -0300328 rc = bootutil_img_hash(enc_state, image_index, hdr, fap, tmp_buf,
329 tmp_buf_sz, hash, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800330 if (rc) {
331 return rc;
332 }
333
334 if (out_hash) {
335 memcpy(out_hash, hash, 32);
336 }
337
Fabio Utzig61fd8882019-09-14 20:00:20 -0300338 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false);
David Brownf5b33d82017-09-01 10:58:27 -0600339 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300340 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600341 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800342
David Brown43cda332017-09-01 09:53:23 -0600343 /*
344 * Traverse through all of the TLVs, performing any checks we know
345 * and are able to do.
346 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300347 while (true) {
348 rc = bootutil_tlv_iter_next(&it, &off, &len, &type);
349 if (rc < 0) {
350 return -1;
351 } else if (rc > 0) {
352 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800353 }
David Brown43cda332017-09-01 09:53:23 -0600354
Fabio Utzig61fd8882019-09-14 20:00:20 -0300355 if (type == IMAGE_TLV_SHA256) {
David Brown43cda332017-09-01 09:53:23 -0600356 /*
357 * Verify the SHA256 image hash. This must always be
358 * present.
359 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300360 if (len != sizeof(hash)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800361 return -1;
362 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300363 rc = flash_area_read(fap, off, buf, sizeof hash);
David Brown43cda332017-09-01 09:53:23 -0600364 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300365 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800366 }
David Brown43cda332017-09-01 09:53:23 -0600367 if (memcmp(hash, buf, sizeof(hash))) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300368 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800369 }
David Brown43cda332017-09-01 09:53:23 -0600370
371 sha256_valid = 1;
372#ifdef EXPECTED_SIG_TLV
David Vincze03368b82020-04-01 12:53:53 +0200373#ifndef MCUBOOT_HW_KEY
Fabio Utzig61fd8882019-09-14 20:00:20 -0300374 } else if (type == IMAGE_TLV_KEYHASH) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800375 /*
David Brown43cda332017-09-01 09:53:23 -0600376 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800377 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300378 if (len > 32) {
David Brown43cda332017-09-01 09:53:23 -0600379 return -1;
380 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300381 rc = flash_area_read(fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600382 if (rc) {
383 return rc;
384 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300385 key_id = bootutil_find_key(buf, len);
David Brown43cda332017-09-01 09:53:23 -0600386 /*
387 * The key may not be found, which is acceptable. There
388 * can be multiple signatures, each preceded by a key.
389 */
David Vincze03368b82020-04-01 12:53:53 +0200390#else
391 } else if (type == IMAGE_TLV_PUBKEY) {
392 /*
393 * Determine which key we should be checking.
394 */
395 if (len > sizeof(key_buf)) {
396 return -1;
397 }
398 rc = flash_area_read(fap, off, key_buf, len);
399 if (rc) {
400 return rc;
401 }
402 key_id = bootutil_find_key(image_index, key_buf, len);
403 /*
404 * The key may not be found, which is acceptable. There
405 * can be multiple signatures, each preceded by a key.
406 */
407#endif /* !MCUBOOT_HW_KEY */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300408 } else if (type == EXPECTED_SIG_TLV) {
David Brown43cda332017-09-01 09:53:23 -0600409 /* Ignore this signature if it is out of bounds. */
410 if (key_id < 0 || key_id >= bootutil_key_cnt) {
411 key_id = -1;
412 continue;
413 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300414 if (!EXPECTED_SIG_LEN(len) || len > sizeof(buf)) {
David Brown43cda332017-09-01 09:53:23 -0600415 return -1;
416 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300417 rc = flash_area_read(fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600418 if (rc) {
419 return -1;
420 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300421 rc = bootutil_verify_sig(hash, sizeof(hash), buf, len, key_id);
David Brown43cda332017-09-01 09:53:23 -0600422 if (rc == 0) {
423 valid_signature = 1;
424 }
425 key_id = -1;
David Vinczec3084132020-02-18 14:50:47 +0100426#endif /* EXPECTED_SIG_TLV */
427#ifdef MCUBOOT_HW_ROLLBACK_PROT
428 } else if (type == IMAGE_TLV_SEC_CNT) {
429 /*
430 * Verify the image's security counter.
431 * This must always be present.
432 */
433 if (len != sizeof(img_security_cnt)) {
434 /* Security counter is not valid. */
435 return -1;
436 }
437
438 rc = flash_area_read(fap, off, &img_security_cnt, len);
439 if (rc) {
440 return rc;
441 }
442
443 rc = boot_nv_security_counter_get(image_index, &security_cnt);
444 if (rc) {
445 return rc;
446 }
447
448 /* Compare the new image's security counter value against the
449 * stored security counter value.
450 */
451 if (img_security_cnt < security_cnt) {
452 /* The image's security counter is not accepted. */
453 return -1;
454 }
455
456 /* The image's security counter has been successfully verified. */
457 security_counter_valid = 1;
458#endif /* MCUBOOT_HW_ROLLBACK_PROT */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800459 }
460 }
David Brown43cda332017-09-01 09:53:23 -0600461
462 if (!sha256_valid) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800463 return -1;
David Brown43cda332017-09-01 09:53:23 -0600464#ifdef EXPECTED_SIG_TLV
David Vinczec3084132020-02-18 14:50:47 +0100465 } else if (!valid_signature) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800466 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800467#endif
David Vinczec3084132020-02-18 14:50:47 +0100468#ifdef MCUBOOT_HW_ROLLBACK_PROT
469 } else if (!security_counter_valid) {
470 return -1;
471#endif
472 }
David Brown43cda332017-09-01 09:53:23 -0600473
Christopher Collins92ea77f2016-12-12 15:59:26 -0800474 return 0;
475}