blob: 7071d7d88e4f4cb5a9185731d2140fe25f42bf02 [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
Arvin Farahmandf8240192020-05-05 11:43:52 -040051#if defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_SIGN_RSA) || \
52 defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080053#include "mbedtls/asn1.h"
Arvin Farahmandf8240192020-05-05 11:43:52 -040054#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080055
56#include "bootutil_priv.h"
57
58/*
59 * Compute SHA256 over the image.
60 */
61static int
Fabio Utzig10ee6482019-08-01 12:04:52 -030062bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
63 struct image_header *hdr, const struct flash_area *fap,
64 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *hash_result,
65 uint8_t *seed, int seed_len)
Christopher Collins92ea77f2016-12-12 15:59:26 -080066{
David Browne629bf32017-04-24 13:37:33 -060067 bootutil_sha256_context sha256_ctx;
Christopher Collins92ea77f2016-12-12 15:59:26 -080068 uint32_t blk_sz;
69 uint32_t size;
Fabio Utzig2fc80df2018-12-14 06:47:38 -020070 uint16_t hdr_size;
Christopher Collins92ea77f2016-12-12 15:59:26 -080071 uint32_t off;
72 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -030073 uint32_t blk_off;
Fabio Utzige52c08e2019-09-11 19:32:00 -030074 uint32_t tlv_off;
Christopher Collins92ea77f2016-12-12 15:59:26 -080075
Fabio Utzigb0f04732019-07-31 09:49:19 -030076#if (BOOT_IMAGE_NUMBER == 1) || !defined(MCUBOOT_ENC_IMAGES)
Fabio Utzig10ee6482019-08-01 12:04:52 -030077 (void)enc_state;
Fabio Utzigb0f04732019-07-31 09:49:19 -030078 (void)image_index;
Fabio Utzigc9621352019-08-08 12:15:51 -030079 (void)hdr_size;
Fabio Utzige52c08e2019-09-11 19:32:00 -030080 (void)blk_off;
81 (void)tlv_off;
Fabio Utzigb0f04732019-07-31 09:49:19 -030082#endif
83
Fabio Utzigbc077932019-08-26 11:16:34 -030084#ifdef MCUBOOT_ENC_IMAGES
85 /* Encrypted images only exist in the secondary slot */
86 if (MUST_DECRYPT(fap, image_index, hdr) &&
87 !boot_enc_valid(enc_state, image_index, fap)) {
88 return -1;
89 }
90#endif
91
David Browne629bf32017-04-24 13:37:33 -060092 bootutil_sha256_init(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -080093
94 /* in some cases (split image) the hash is seeded with data from
95 * the loader image */
Fabio Utzig53986042017-12-12 14:57:07 -020096 if (seed && (seed_len > 0)) {
David Browne629bf32017-04-24 13:37:33 -060097 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -080098 }
99
David Vinczee32483f2019-06-13 10:46:24 +0200100 /* Hash is computed over image header and image itself. */
Fabio Utzige52c08e2019-09-11 19:32:00 -0300101 size = hdr_size = hdr->ih_hdr_size;
102 size += hdr->ih_img_size;
103 tlv_off = size;
David Vinczee32483f2019-06-13 10:46:24 +0200104
Fabio Utzige52c08e2019-09-11 19:32:00 -0300105 /* If protected TLVs are present they are also hashed. */
106 size += hdr->ih_protect_tlv_size;
David Vinczee32483f2019-06-13 10:46:24 +0200107
Christopher Collins92ea77f2016-12-12 15:59:26 -0800108 for (off = 0; off < size; off += blk_sz) {
109 blk_sz = size - off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300110 if (blk_sz > tmp_buf_sz) {
111 blk_sz = tmp_buf_sz;
112 }
Fabio Utzigc21c2102019-09-10 10:10:42 -0300113#ifdef MCUBOOT_ENC_IMAGES
114 /* The only data that is encrypted in an image is the payload;
115 * both header and TLVs (when protected) are not.
116 */
117 if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) {
118 /* read only the header */
119 blk_sz = hdr_size - off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300120 }
121 if ((off < tlv_off) && ((off + blk_sz) > tlv_off)) {
122 /* read only up to the end of the image payload */
123 blk_sz = tlv_off - off;
Fabio Utzigc21c2102019-09-10 10:10:42 -0300124 }
125#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800126 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
127 if (rc) {
128 return rc;
129 }
Fabio Utzigba829042018-09-18 08:29:34 -0300130#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzigc21c2102019-09-10 10:10:42 -0300131 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzige52c08e2019-09-11 19:32:00 -0300132 /* Only payload is encrypted (area between header and TLVs) */
133 if (off >= hdr_size && off < tlv_off) {
Fabio Utzigc21c2102019-09-10 10:10:42 -0300134 blk_off = (off - hdr_size) & 0xf;
135 boot_encrypt(enc_state, image_index, fap, off - hdr_size,
136 blk_sz, blk_off, tmp_buf);
137 }
Fabio Utzigba829042018-09-18 08:29:34 -0300138 }
139#endif
David Browne629bf32017-04-24 13:37:33 -0600140 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800141 }
David Browne629bf32017-04-24 13:37:33 -0600142 bootutil_sha256_finish(&sha256_ctx, hash_result);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800143
144 return 0;
145}
146
147/*
David Brown43cda332017-09-01 09:53:23 -0600148 * Currently, we only support being able to verify one type of
149 * signature, because there is a single verification function that we
150 * call. List the type of TLV we are expecting. If we aren't
151 * configured for any signature, don't define this macro.
152 */
Fabio Utzig48764842019-05-10 19:28:24 -0300153#if (defined(MCUBOOT_SIGN_RSA) + \
154 defined(MCUBOOT_SIGN_EC) + \
155 defined(MCUBOOT_SIGN_EC256) + \
156 defined(MCUBOOT_SIGN_ED25519)) > 1
Fabio Utzig3501c012019-05-13 15:07:25 -0700157#error "Only a single signature type is supported!"
158#endif
159
David Brown43cda332017-09-01 09:53:23 -0600160#if defined(MCUBOOT_SIGN_RSA)
Fabio Utzig3501c012019-05-13 15:07:25 -0700161# if MCUBOOT_SIGN_RSA_LEN == 2048
162# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
163# elif MCUBOOT_SIGN_RSA_LEN == 3072
164# define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS
165# else
166# error "Unsupported RSA signature length"
David Brown43cda332017-09-01 09:53:23 -0600167# endif
Fabio Utzig3501c012019-05-13 15:07:25 -0700168# define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8)
169# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) /* 2048 bits */
David Brown43cda332017-09-01 09:53:23 -0600170#elif defined(MCUBOOT_SIGN_EC)
171# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224
Fabio Utzig3501c012019-05-13 15:07:25 -0700172# define SIG_BUF_SIZE 128
David Browna3608262019-12-12 15:35:31 -0700173# define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */
David Brown43cda332017-09-01 09:53:23 -0600174#elif defined(MCUBOOT_SIGN_EC256)
175# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
Fabio Utzig3501c012019-05-13 15:07:25 -0700176# define SIG_BUF_SIZE 128
David Browna3608262019-12-12 15:35:31 -0700177# define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */
Fabio Utzig48764842019-05-10 19:28:24 -0300178#elif defined(MCUBOOT_SIGN_ED25519)
179# define EXPECTED_SIG_TLV IMAGE_TLV_ED25519
180# define SIG_BUF_SIZE 64
181# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE)
Fabio Utzig3501c012019-05-13 15:07:25 -0700182#else
183# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
David Brown43cda332017-09-01 09:53:23 -0600184#endif
185
186#ifdef EXPECTED_SIG_TLV
David Vincze03368b82020-04-01 12:53:53 +0200187#if !defined(MCUBOOT_HW_KEY)
David Brown43cda332017-09-01 09:53:23 -0600188static int
189bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
190{
191 bootutil_sha256_context sha256_ctx;
192 int i;
193 const struct bootutil_key *key;
194 uint8_t hash[32];
195
Fabio Utzig15c14672019-08-09 07:45:20 -0300196 if (keyhash_len > 32) {
197 return -1;
198 }
David Brown43cda332017-09-01 09:53:23 -0600199
200 for (i = 0; i < bootutil_key_cnt; i++) {
201 key = &bootutil_keys[i];
202 bootutil_sha256_init(&sha256_ctx);
203 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
204 bootutil_sha256_finish(&sha256_ctx, hash);
205 if (!memcmp(hash, keyhash, keyhash_len)) {
206 return i;
207 }
208 }
209 return -1;
210}
David Vincze03368b82020-04-01 12:53:53 +0200211#else
212extern unsigned int pub_key_len;
213static int
214bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
215{
216 bootutil_sha256_context sha256_ctx;
217 uint8_t hash[32];
218 uint8_t key_hash[32];
219 size_t key_hash_size = sizeof(key_hash);
220 int rc;
221
222 bootutil_sha256_init(&sha256_ctx);
223 bootutil_sha256_update(&sha256_ctx, key, key_len);
224 bootutil_sha256_finish(&sha256_ctx, hash);
225
226 rc = boot_retrieve_public_key_hash(image_index, key_hash, &key_hash_size);
227 if (rc) {
228 return rc;
229 }
230
231 if (!memcmp(hash, key_hash, key_hash_size)) {
232 bootutil_keys[0].key = key;
233 pub_key_len = key_len;
234 return 0;
235 }
236 return -1;
237}
238#endif /* !MCUBOOT_HW_KEY */
David Brown43cda332017-09-01 09:53:23 -0600239#endif
240
David Vinczec3084132020-02-18 14:50:47 +0100241#ifdef MCUBOOT_HW_ROLLBACK_PROT
242/**
243 * Reads the value of an image's security counter.
244 *
245 * @param hdr Pointer to the image header structure.
246 * @param fap Pointer to a description structure of the image's
247 * flash area.
248 * @param security_cnt Pointer to store the security counter value.
249 *
250 * @return 0 on success; nonzero on failure.
251 */
252int32_t
253bootutil_get_img_security_cnt(struct image_header *hdr,
254 const struct flash_area *fap,
255 uint32_t *img_security_cnt)
256{
257 struct image_tlv_iter it;
258 uint32_t off;
259 uint16_t len;
260 int32_t rc;
261
262 if ((hdr == NULL) ||
263 (fap == NULL) ||
264 (img_security_cnt == NULL)) {
265 /* Invalid parameter. */
266 return BOOT_EBADARGS;
267 }
268
269 /* The security counter TLV is in the protected part of the TLV area. */
270 if (hdr->ih_protect_tlv_size == 0) {
271 return BOOT_EBADIMAGE;
272 }
273
274 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_SEC_CNT, true);
275 if (rc) {
276 return rc;
277 }
278
279 /* Traverse through the protected TLV area to find
280 * the security counter TLV.
281 */
282
283 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
284 if (rc != 0) {
285 /* Security counter TLV has not been found. */
286 return -1;
287 }
288
289 if (len != sizeof(*img_security_cnt)) {
290 /* Security counter is not valid. */
291 return BOOT_EBADIMAGE;
292 }
293
294 rc = flash_area_read(fap, off, img_security_cnt, len);
295 if (rc != 0) {
296 return BOOT_EFLASH;
297 }
298
299 return 0;
300}
301#endif /* MCUBOOT_HW_ROLLBACK_PROT */
302
David Brown43cda332017-09-01 09:53:23 -0600303/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800304 * Verify the integrity of the image.
305 * Return non-zero if image could not be validated/does not validate.
306 */
307int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300308bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
309 struct image_header *hdr, const struct flash_area *fap,
310 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *seed,
311 int seed_len, uint8_t *out_hash)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800312{
313 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300314 uint16_t len;
David Brownd13318a2019-12-04 17:28:40 -0700315 uint16_t type;
David Brown43cda332017-09-01 09:53:23 -0600316 int sha256_valid = 0;
317#ifdef EXPECTED_SIG_TLV
318 int valid_signature = 0;
319 int key_id = -1;
David Vincze03368b82020-04-01 12:53:53 +0200320#ifdef MCUBOOT_HW_KEY
321 /* Few extra bytes for encoding and for public exponent. */
322 uint8_t key_buf[SIG_BUF_SIZE + 24];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800323#endif
David Vincze03368b82020-04-01 12:53:53 +0200324#endif /* EXPECTED_SIG_TLV */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300325 struct image_tlv_iter it;
Fabio Utzig3501c012019-05-13 15:07:25 -0700326 uint8_t buf[SIG_BUF_SIZE];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800327 uint8_t hash[32];
328 int rc;
David Vinczec3084132020-02-18 14:50:47 +0100329#ifdef MCUBOOT_HW_ROLLBACK_PROT
330 uint32_t security_cnt = UINT32_MAX;
331 uint32_t img_security_cnt = 0;
332 int32_t security_counter_valid = 0;
333#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800334
Fabio Utzig10ee6482019-08-01 12:04:52 -0300335 rc = bootutil_img_hash(enc_state, image_index, hdr, fap, tmp_buf,
336 tmp_buf_sz, hash, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800337 if (rc) {
338 return rc;
339 }
340
341 if (out_hash) {
342 memcpy(out_hash, hash, 32);
343 }
344
Fabio Utzig61fd8882019-09-14 20:00:20 -0300345 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false);
David Brownf5b33d82017-09-01 10:58:27 -0600346 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300347 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600348 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800349
David Brown43cda332017-09-01 09:53:23 -0600350 /*
351 * Traverse through all of the TLVs, performing any checks we know
352 * and are able to do.
353 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300354 while (true) {
355 rc = bootutil_tlv_iter_next(&it, &off, &len, &type);
356 if (rc < 0) {
357 return -1;
358 } else if (rc > 0) {
359 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800360 }
David Brown43cda332017-09-01 09:53:23 -0600361
Fabio Utzig61fd8882019-09-14 20:00:20 -0300362 if (type == IMAGE_TLV_SHA256) {
David Brown43cda332017-09-01 09:53:23 -0600363 /*
364 * Verify the SHA256 image hash. This must always be
365 * present.
366 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300367 if (len != sizeof(hash)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800368 return -1;
369 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300370 rc = flash_area_read(fap, off, buf, sizeof hash);
David Brown43cda332017-09-01 09:53:23 -0600371 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300372 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800373 }
David Brown43cda332017-09-01 09:53:23 -0600374 if (memcmp(hash, buf, sizeof(hash))) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300375 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800376 }
David Brown43cda332017-09-01 09:53:23 -0600377
378 sha256_valid = 1;
379#ifdef EXPECTED_SIG_TLV
David Vincze03368b82020-04-01 12:53:53 +0200380#ifndef MCUBOOT_HW_KEY
Fabio Utzig61fd8882019-09-14 20:00:20 -0300381 } else if (type == IMAGE_TLV_KEYHASH) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800382 /*
David Brown43cda332017-09-01 09:53:23 -0600383 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800384 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300385 if (len > 32) {
David Brown43cda332017-09-01 09:53:23 -0600386 return -1;
387 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300388 rc = flash_area_read(fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600389 if (rc) {
390 return rc;
391 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300392 key_id = bootutil_find_key(buf, len);
David Brown43cda332017-09-01 09:53:23 -0600393 /*
394 * The key may not be found, which is acceptable. There
395 * can be multiple signatures, each preceded by a key.
396 */
David Vincze03368b82020-04-01 12:53:53 +0200397#else
398 } else if (type == IMAGE_TLV_PUBKEY) {
399 /*
400 * Determine which key we should be checking.
401 */
402 if (len > sizeof(key_buf)) {
403 return -1;
404 }
405 rc = flash_area_read(fap, off, key_buf, len);
406 if (rc) {
407 return rc;
408 }
409 key_id = bootutil_find_key(image_index, key_buf, len);
410 /*
411 * The key may not be found, which is acceptable. There
412 * can be multiple signatures, each preceded by a key.
413 */
414#endif /* !MCUBOOT_HW_KEY */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300415 } else if (type == EXPECTED_SIG_TLV) {
David Brown43cda332017-09-01 09:53:23 -0600416 /* Ignore this signature if it is out of bounds. */
417 if (key_id < 0 || key_id >= bootutil_key_cnt) {
418 key_id = -1;
419 continue;
420 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300421 if (!EXPECTED_SIG_LEN(len) || len > sizeof(buf)) {
David Brown43cda332017-09-01 09:53:23 -0600422 return -1;
423 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300424 rc = flash_area_read(fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600425 if (rc) {
426 return -1;
427 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300428 rc = bootutil_verify_sig(hash, sizeof(hash), buf, len, key_id);
David Brown43cda332017-09-01 09:53:23 -0600429 if (rc == 0) {
430 valid_signature = 1;
431 }
432 key_id = -1;
David Vinczec3084132020-02-18 14:50:47 +0100433#endif /* EXPECTED_SIG_TLV */
434#ifdef MCUBOOT_HW_ROLLBACK_PROT
435 } else if (type == IMAGE_TLV_SEC_CNT) {
436 /*
437 * Verify the image's security counter.
438 * This must always be present.
439 */
440 if (len != sizeof(img_security_cnt)) {
441 /* Security counter is not valid. */
442 return -1;
443 }
444
445 rc = flash_area_read(fap, off, &img_security_cnt, len);
446 if (rc) {
447 return rc;
448 }
449
450 rc = boot_nv_security_counter_get(image_index, &security_cnt);
451 if (rc) {
452 return rc;
453 }
454
455 /* Compare the new image's security counter value against the
456 * stored security counter value.
457 */
458 if (img_security_cnt < security_cnt) {
459 /* The image's security counter is not accepted. */
460 return -1;
461 }
462
463 /* The image's security counter has been successfully verified. */
464 security_counter_valid = 1;
465#endif /* MCUBOOT_HW_ROLLBACK_PROT */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800466 }
467 }
David Brown43cda332017-09-01 09:53:23 -0600468
469 if (!sha256_valid) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800470 return -1;
David Brown43cda332017-09-01 09:53:23 -0600471#ifdef EXPECTED_SIG_TLV
David Vinczec3084132020-02-18 14:50:47 +0100472 } else if (!valid_signature) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800473 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800474#endif
David Vinczec3084132020-02-18 14:50:47 +0100475#ifdef MCUBOOT_HW_ROLLBACK_PROT
476 } else if (!security_counter_valid) {
477 return -1;
478#endif
479 }
David Brown43cda332017-09-01 09:53:23 -0600480
Christopher Collins92ea77f2016-12-12 15:59:26 -0800481 return 0;
482}