Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1 | /* |
David Brown | aac7111 | 2020-02-03 16:13:42 -0700 | [diff] [blame] | 2 | * 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 Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 7 | * Copyright (c) 2021 Infineon Technologies AG |
David Brown | aac7111 | 2020-02-03 16:13:42 -0700 | [diff] [blame] | 8 | * |
| 9 | * Original license: |
| 10 | * |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 11 | * 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 Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 29 | #include <stddef.h> |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 30 | #include <stdint.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 31 | #include <inttypes.h> |
| 32 | #include <string.h> |
| 33 | |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 34 | #include <flash_map_backend/flash_map_backend.h> |
| 35 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 36 | #include "bootutil/image.h" |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 37 | #include "bootutil/crypto/sha256.h" |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 38 | #include "bootutil/sign_key.h" |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 39 | #include "bootutil/security_cnt.h" |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 40 | #include "bootutil/fault_injection_hardening.h" |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 41 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 42 | #include "bootutil/bootutil_log.h" |
| 43 | |
Fabio Utzig | ba1fbe6 | 2017-07-21 14:01:20 -0300 | [diff] [blame] | 44 | #include "mcuboot_config/mcuboot_config.h" |
Fabio Utzig | eed80b6 | 2017-06-10 08:03:05 -0300 | [diff] [blame] | 45 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 46 | #ifdef MCUBOOT_ENC_IMAGES |
| 47 | #include "bootutil/enc_key.h" |
| 48 | #endif |
| 49 | #if defined(MCUBOOT_SIGN_RSA) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 50 | #include "mbedtls/rsa.h" |
David Brown | d7e350d | 2017-04-24 13:29:28 -0600 | [diff] [blame] | 51 | #endif |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 52 | #if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 53 | #include "mbedtls/ecdsa.h" |
David Brown | d7e350d | 2017-04-24 13:29:28 -0600 | [diff] [blame] | 54 | #endif |
Arvin Farahmand | f824019 | 2020-05-05 11:43:52 -0400 | [diff] [blame] | 55 | #if defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_SIGN_RSA) || \ |
| 56 | defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 57 | #include "mbedtls/asn1.h" |
Arvin Farahmand | f824019 | 2020-05-05 11:43:52 -0400 | [diff] [blame] | 58 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 59 | |
| 60 | #include "bootutil_priv.h" |
| 61 | |
Roman Okhrimenko | 2516562 | 2023-05-23 08:58:29 +0300 | [diff] [blame^] | 62 | #ifdef CYW20829 |
| 63 | #include "cy_security_cnt_platform.h" |
| 64 | #endif |
| 65 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 66 | /* |
| 67 | * Compute SHA256 over the image. |
| 68 | */ |
| 69 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 70 | bootutil_img_hash(struct enc_key_data *enc_state, int image_index, |
| 71 | struct image_header *hdr, const struct flash_area *fap, |
| 72 | uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *hash_result, |
| 73 | uint8_t *seed, int seed_len) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 74 | { |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 75 | bootutil_sha256_context sha256_ctx; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 76 | uint32_t blk_sz; |
| 77 | uint32_t size; |
Fabio Utzig | 2fc80df | 2018-12-14 06:47:38 -0200 | [diff] [blame] | 78 | uint16_t hdr_size; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 79 | uint32_t off; |
| 80 | int rc; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 81 | uint32_t blk_off; |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 82 | uint32_t tlv_off; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 83 | |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 84 | #if (BOOT_IMAGE_NUMBER == 1) || !defined(MCUBOOT_ENC_IMAGES) || \ |
| 85 | defined(MCUBOOT_RAM_LOAD) |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 86 | (void)enc_state; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 87 | (void)image_index; |
Fabio Utzig | c962135 | 2019-08-08 12:15:51 -0300 | [diff] [blame] | 88 | (void)hdr_size; |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 89 | (void)blk_off; |
| 90 | (void)tlv_off; |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 91 | #ifdef MCUBOOT_RAM_LOAD |
| 92 | (void)blk_sz; |
| 93 | (void)off; |
| 94 | (void)rc; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 95 | (void)fap; |
| 96 | (void)tmp_buf; |
| 97 | (void)tmp_buf_sz; |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 98 | #endif |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 99 | #endif |
| 100 | |
Fabio Utzig | bc07793 | 2019-08-26 11:16:34 -0300 | [diff] [blame] | 101 | #ifdef MCUBOOT_ENC_IMAGES |
| 102 | /* Encrypted images only exist in the secondary slot */ |
| 103 | if (MUST_DECRYPT(fap, image_index, hdr) && |
| 104 | !boot_enc_valid(enc_state, image_index, fap)) { |
| 105 | return -1; |
| 106 | } |
| 107 | #endif |
| 108 | |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 109 | bootutil_sha256_init(&sha256_ctx); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 110 | |
| 111 | /* in some cases (split image) the hash is seeded with data from |
| 112 | * the loader image */ |
Fabio Utzig | 5398604 | 2017-12-12 14:57:07 -0200 | [diff] [blame] | 113 | if (seed && (seed_len > 0)) { |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 114 | bootutil_sha256_update(&sha256_ctx, seed, seed_len); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 115 | } |
| 116 | |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 117 | /* Hash is computed over image header and image itself. */ |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 118 | size = hdr_size = hdr->ih_hdr_size; |
| 119 | size += hdr->ih_img_size; |
| 120 | tlv_off = size; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 121 | |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 122 | /* If protected TLVs are present they are also hashed. */ |
| 123 | size += hdr->ih_protect_tlv_size; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 124 | |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 125 | #ifdef MCUBOOT_RAM_LOAD |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 126 | bootutil_sha256_update(&sha256_ctx, |
| 127 | (void*)(IMAGE_RAM_BASE + hdr->ih_load_addr), |
| 128 | size); |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 129 | #else |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 130 | for (off = 0; off < size; off += blk_sz) { |
| 131 | blk_sz = size - off; |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 132 | if (blk_sz > tmp_buf_sz) { |
| 133 | blk_sz = tmp_buf_sz; |
| 134 | } |
Fabio Utzig | c21c210 | 2019-09-10 10:10:42 -0300 | [diff] [blame] | 135 | #ifdef MCUBOOT_ENC_IMAGES |
| 136 | /* The only data that is encrypted in an image is the payload; |
| 137 | * both header and TLVs (when protected) are not. |
| 138 | */ |
| 139 | if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) { |
| 140 | /* read only the header */ |
| 141 | blk_sz = hdr_size - off; |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 142 | } |
| 143 | if ((off < tlv_off) && ((off + blk_sz) > tlv_off)) { |
| 144 | /* read only up to the end of the image payload */ |
| 145 | blk_sz = tlv_off - off; |
Fabio Utzig | c21c210 | 2019-09-10 10:10:42 -0300 | [diff] [blame] | 146 | } |
| 147 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 148 | rc = flash_area_read(fap, off, tmp_buf, blk_sz); |
| 149 | if (rc) { |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 150 | bootutil_sha256_drop(&sha256_ctx); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 151 | return rc; |
| 152 | } |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 153 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | c21c210 | 2019-09-10 10:10:42 -0300 | [diff] [blame] | 154 | if (MUST_DECRYPT(fap, image_index, hdr)) { |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 155 | /* Only payload is encrypted (area between header and TLVs) */ |
| 156 | if (off >= hdr_size && off < tlv_off) { |
Fabio Utzig | c21c210 | 2019-09-10 10:10:42 -0300 | [diff] [blame] | 157 | blk_off = (off - hdr_size) & 0xf; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 158 | #ifdef MCUBOOT_ENC_IMAGES_XIP |
| 159 | rc = bootutil_img_encrypt(enc_state, image_index, hdr, fap, off, |
Fabio Utzig | c21c210 | 2019-09-10 10:10:42 -0300 | [diff] [blame] | 160 | blk_sz, blk_off, tmp_buf); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 161 | #else |
| 162 | rc = boot_encrypt(enc_state, image_index, fap, off - hdr_size, |
| 163 | blk_sz, blk_off, tmp_buf); |
| 164 | #endif |
| 165 | if (rc) { |
| 166 | return rc; |
| 167 | } |
Fabio Utzig | c21c210 | 2019-09-10 10:10:42 -0300 | [diff] [blame] | 168 | } |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 169 | } |
| 170 | #endif |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 171 | bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 172 | } |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 173 | #endif /* MCUBOOT_RAM_LOAD */ |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 174 | bootutil_sha256_finish(&sha256_ctx, hash_result); |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 175 | bootutil_sha256_drop(&sha256_ctx); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | /* |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 181 | * Currently, we only support being able to verify one type of |
| 182 | * signature, because there is a single verification function that we |
| 183 | * call. List the type of TLV we are expecting. If we aren't |
| 184 | * configured for any signature, don't define this macro. |
| 185 | */ |
Fabio Utzig | 4876484 | 2019-05-10 19:28:24 -0300 | [diff] [blame] | 186 | #if (defined(MCUBOOT_SIGN_RSA) + \ |
| 187 | defined(MCUBOOT_SIGN_EC) + \ |
| 188 | defined(MCUBOOT_SIGN_EC256) + \ |
| 189 | defined(MCUBOOT_SIGN_ED25519)) > 1 |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 190 | #error "Only a single signature type is supported!" |
| 191 | #endif |
| 192 | |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 193 | #if defined(MCUBOOT_SIGN_RSA) |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 194 | # if MCUBOOT_SIGN_RSA_LEN == 2048 |
| 195 | # define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS |
| 196 | # elif MCUBOOT_SIGN_RSA_LEN == 3072 |
| 197 | # define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS |
| 198 | # else |
| 199 | # error "Unsupported RSA signature length" |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 200 | # endif |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 201 | # define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8) |
| 202 | # define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) /* 2048 bits */ |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 203 | #elif defined(MCUBOOT_SIGN_EC) |
| 204 | # define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224 |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 205 | # define SIG_BUF_SIZE 128 |
David Brown | a360826 | 2019-12-12 15:35:31 -0700 | [diff] [blame] | 206 | # define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */ |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 207 | #elif defined(MCUBOOT_SIGN_EC256) |
| 208 | # define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256 |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 209 | # define SIG_BUF_SIZE 128 |
David Brown | a360826 | 2019-12-12 15:35:31 -0700 | [diff] [blame] | 210 | # define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */ |
Fabio Utzig | 4876484 | 2019-05-10 19:28:24 -0300 | [diff] [blame] | 211 | #elif defined(MCUBOOT_SIGN_ED25519) |
| 212 | # define EXPECTED_SIG_TLV IMAGE_TLV_ED25519 |
| 213 | # define SIG_BUF_SIZE 64 |
| 214 | # define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 215 | #else |
| 216 | # define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */ |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 217 | #endif |
| 218 | |
| 219 | #ifdef EXPECTED_SIG_TLV |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 220 | #if !defined(MCUBOOT_HW_KEY) |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 221 | static int |
| 222 | bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len) |
| 223 | { |
| 224 | bootutil_sha256_context sha256_ctx; |
| 225 | int i; |
| 226 | const struct bootutil_key *key; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 227 | uint8_t hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE]; |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 228 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 229 | if (keyhash_len != BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE) { |
Fabio Utzig | 15c1467 | 2019-08-09 07:45:20 -0300 | [diff] [blame] | 230 | return -1; |
| 231 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 232 | |
| 233 | for (i = 0; i < bootutil_key_cnt; i++) { |
| 234 | key = &bootutil_keys[i]; |
| 235 | bootutil_sha256_init(&sha256_ctx); |
| 236 | bootutil_sha256_update(&sha256_ctx, key->key, *key->len); |
| 237 | bootutil_sha256_finish(&sha256_ctx, hash); |
| 238 | if (!memcmp(hash, keyhash, keyhash_len)) { |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 239 | bootutil_sha256_drop(&sha256_ctx); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 240 | return i; |
| 241 | } |
| 242 | } |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 243 | bootutil_sha256_drop(&sha256_ctx); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 244 | return -1; |
| 245 | } |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 246 | #else |
| 247 | extern unsigned int pub_key_len; |
| 248 | static int |
| 249 | bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len) |
| 250 | { |
| 251 | bootutil_sha256_context sha256_ctx; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 252 | uint8_t hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE]; |
| 253 | uint8_t key_hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE]; |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 254 | size_t key_hash_size = sizeof(key_hash); |
| 255 | int rc; |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 256 | fih_int fih_rc; |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 257 | |
| 258 | bootutil_sha256_init(&sha256_ctx); |
| 259 | bootutil_sha256_update(&sha256_ctx, key, key_len); |
| 260 | bootutil_sha256_finish(&sha256_ctx, hash); |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 261 | bootutil_sha256_drop(&sha256_ctx); |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 262 | |
| 263 | rc = boot_retrieve_public_key_hash(image_index, key_hash, &key_hash_size); |
| 264 | if (rc) { |
| 265 | return rc; |
| 266 | } |
| 267 | |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 268 | /* Adding hardening to avoid this potential attack: |
| 269 | * - Image is signed with an arbitrary key and the corresponding public |
| 270 | * key is added as a TLV field. |
| 271 | * - During public key validation (comparing against key-hash read from |
| 272 | * HW) a fault is injected to accept the public key as valid one. |
| 273 | */ |
| 274 | FIH_CALL(boot_fih_memequal, fih_rc, hash, key_hash, key_hash_size); |
| 275 | if (fih_eq(fih_rc, FIH_SUCCESS)) { |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 276 | bootutil_keys[0].key = key; |
| 277 | pub_key_len = key_len; |
| 278 | return 0; |
| 279 | } |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 280 | |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 281 | return -1; |
| 282 | } |
| 283 | #endif /* !MCUBOOT_HW_KEY */ |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 284 | #endif |
| 285 | |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 286 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 287 | /** |
| 288 | * Reads the value of an image's security counter. |
| 289 | * |
| 290 | * @param hdr Pointer to the image header structure. |
| 291 | * @param fap Pointer to a description structure of the image's |
| 292 | * flash area. |
| 293 | * @param security_cnt Pointer to store the security counter value. |
| 294 | * |
| 295 | * @return 0 on success; nonzero on failure. |
| 296 | */ |
| 297 | int32_t |
| 298 | bootutil_get_img_security_cnt(struct image_header *hdr, |
| 299 | const struct flash_area *fap, |
| 300 | uint32_t *img_security_cnt) |
| 301 | { |
| 302 | struct image_tlv_iter it; |
| 303 | uint32_t off; |
| 304 | uint16_t len; |
| 305 | int32_t rc; |
| 306 | |
| 307 | if ((hdr == NULL) || |
| 308 | (fap == NULL) || |
| 309 | (img_security_cnt == NULL)) { |
| 310 | /* Invalid parameter. */ |
| 311 | return BOOT_EBADARGS; |
| 312 | } |
| 313 | |
| 314 | /* The security counter TLV is in the protected part of the TLV area. */ |
| 315 | if (hdr->ih_protect_tlv_size == 0) { |
| 316 | return BOOT_EBADIMAGE; |
| 317 | } |
| 318 | |
| 319 | rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_SEC_CNT, true); |
| 320 | if (rc) { |
| 321 | return rc; |
| 322 | } |
| 323 | |
| 324 | /* Traverse through the protected TLV area to find |
| 325 | * the security counter TLV. |
| 326 | */ |
| 327 | |
| 328 | rc = bootutil_tlv_iter_next(&it, &off, &len, NULL); |
| 329 | if (rc != 0) { |
| 330 | /* Security counter TLV has not been found. */ |
| 331 | return -1; |
| 332 | } |
| 333 | |
| 334 | if (len != sizeof(*img_security_cnt)) { |
| 335 | /* Security counter is not valid. */ |
| 336 | return BOOT_EBADIMAGE; |
| 337 | } |
| 338 | |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 339 | rc = LOAD_IMAGE_DATA(hdr, fap, off, img_security_cnt, len); |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 340 | if (rc != 0) { |
| 341 | return BOOT_EFLASH; |
| 342 | } |
| 343 | |
| 344 | return 0; |
| 345 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 346 | #ifdef CYW20829 |
| 347 | /** |
| 348 | * Reads the content of an image's reprovisioning packet. |
| 349 | * |
| 350 | * @param hdr Pointer to the image header structure. |
| 351 | * @param fap Pointer to a description structure of the image's |
| 352 | * flash area. |
| 353 | * @param reprov_packet Pointer to store the reprovisioning packet. |
| 354 | * |
| 355 | * @return 0 on success; nonzero on failure. |
| 356 | */ |
| 357 | int32_t |
| 358 | bootutil_get_img_reprov_packet(struct image_header *hdr, |
| 359 | const struct flash_area *fap, |
| 360 | uint8_t *reprov_packet) |
| 361 | { |
| 362 | struct image_tlv_iter it; |
| 363 | uint32_t off; |
| 364 | uint16_t len; |
| 365 | int32_t rc; |
| 366 | |
| 367 | if ((hdr == NULL) || |
| 368 | (fap == NULL) || |
| 369 | (reprov_packet == NULL)) { |
| 370 | /* Invalid parameter. */ |
| 371 | return BOOT_EBADARGS; |
| 372 | } |
| 373 | |
| 374 | /* The reprovisioning packet TLV is in the protected part of the TLV area. */ |
| 375 | if (hdr->ih_protect_tlv_size == 0) { |
| 376 | return BOOT_EBADIMAGE; |
| 377 | } |
| 378 | |
| 379 | rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_PROV_PACK, true); |
| 380 | if (rc) { |
| 381 | return rc; |
| 382 | } |
| 383 | |
| 384 | /* Traverse through the protected TLV area to find |
| 385 | * the reprovisioning apcket TLV. |
| 386 | */ |
| 387 | |
| 388 | rc = bootutil_tlv_iter_next(&it, &off, &len, NULL); |
| 389 | if (rc != 0) { |
| 390 | /* Reprovisioning packet TLV has not been found. */ |
| 391 | return -1; |
| 392 | } |
| 393 | |
| 394 | if (len != REPROV_PACK_SIZE) { |
| 395 | /* Reprovisioning packet is not valid. */ |
| 396 | return BOOT_EBADIMAGE; |
| 397 | } |
| 398 | |
| 399 | rc = LOAD_IMAGE_DATA(hdr, fap, off, reprov_packet, len); |
| 400 | if (rc != 0) { |
| 401 | return BOOT_EFLASH; |
| 402 | } |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | #endif /* CYW20289 */ |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 407 | #endif /* MCUBOOT_HW_ROLLBACK_PROT */ |
| 408 | |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 409 | /* |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 410 | * Verify the integrity of the image. |
| 411 | * Return non-zero if image could not be validated/does not validate. |
| 412 | */ |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 413 | fih_int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 414 | bootutil_img_validate(struct enc_key_data *enc_state, int image_index, |
| 415 | struct image_header *hdr, const struct flash_area *fap, |
| 416 | uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *seed, |
| 417 | int seed_len, uint8_t *out_hash) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 418 | { |
| 419 | uint32_t off; |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 420 | uint16_t len; |
David Brown | d13318a | 2019-12-04 17:28:40 -0700 | [diff] [blame] | 421 | uint16_t type; |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 422 | int sha256_valid = 0; |
| 423 | #ifdef EXPECTED_SIG_TLV |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 424 | fih_int valid_signature = FIH_FAILURE; |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 425 | int key_id = -1; |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 426 | #ifdef MCUBOOT_HW_KEY |
| 427 | /* Few extra bytes for encoding and for public exponent. */ |
| 428 | uint8_t key_buf[SIG_BUF_SIZE + 24]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 429 | #endif |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 430 | #endif /* EXPECTED_SIG_TLV */ |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 431 | struct image_tlv_iter it; |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 432 | uint8_t buf[SIG_BUF_SIZE]; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 433 | uint8_t hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE]; |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 434 | int rc = 0; |
| 435 | fih_int fih_rc = FIH_FAILURE; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 436 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 437 | fih_uint security_cnt = fih_uint_encode(UINT_MAX); |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 438 | uint32_t img_security_cnt = 0; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 439 | uint8_t reprov_packet[REPROV_PACK_SIZE]; |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 440 | fih_int security_counter_valid = FIH_FAILURE; |
Roman Okhrimenko | 2516562 | 2023-05-23 08:58:29 +0300 | [diff] [blame^] | 441 | #ifdef CYW20829 |
| 442 | fih_uint extracted_img_cnt = fih_uint_encode(UINT_MAX); |
| 443 | #endif /* CYW20829 */ |
| 444 | #endif /* MCUBOOT_HW_ROLLBACK_PROT */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 445 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 446 | rc = bootutil_img_hash(enc_state, image_index, hdr, fap, tmp_buf, |
| 447 | tmp_buf_sz, hash, seed, seed_len); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 448 | if (rc) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 449 | goto out; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | if (out_hash) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 453 | memcpy(out_hash, hash, BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 454 | } |
| 455 | |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 456 | rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false); |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 457 | if (rc) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 458 | goto out; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 459 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 460 | |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 461 | /* |
| 462 | * Traverse through all of the TLVs, performing any checks we know |
| 463 | * and are able to do. |
| 464 | */ |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 465 | while (true) { |
| 466 | rc = bootutil_tlv_iter_next(&it, &off, &len, &type); |
| 467 | if (rc < 0) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 468 | goto out; |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 469 | } else if (rc > 0) { |
| 470 | break; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 471 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 472 | |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 473 | if (type == IMAGE_TLV_SHA256) { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 474 | /* |
| 475 | * Verify the SHA256 image hash. This must always be |
| 476 | * present. |
| 477 | */ |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 478 | if (len != sizeof(hash)) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 479 | rc = -1; |
| 480 | goto out; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 481 | } |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 482 | rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, sizeof(hash)); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 483 | if (rc) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 484 | goto out; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 485 | } |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 486 | |
| 487 | FIH_CALL(boot_fih_memequal, fih_rc, hash, buf, sizeof(hash)); |
| 488 | if (fih_not_eq(fih_rc, FIH_SUCCESS)) { |
| 489 | goto out; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 490 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 491 | |
| 492 | sha256_valid = 1; |
| 493 | #ifdef EXPECTED_SIG_TLV |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 494 | #ifndef MCUBOOT_HW_KEY |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 495 | } else if (type == IMAGE_TLV_KEYHASH) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 496 | /* |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 497 | * Determine which key we should be checking. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 498 | */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 499 | if (len != BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 500 | rc = -1; |
| 501 | goto out; |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 502 | } |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 503 | rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 504 | if (rc) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 505 | goto out; |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 506 | } |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 507 | key_id = bootutil_find_key(buf, len); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 508 | /* |
| 509 | * The key may not be found, which is acceptable. There |
| 510 | * can be multiple signatures, each preceded by a key. |
| 511 | */ |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 512 | #else |
| 513 | } else if (type == IMAGE_TLV_PUBKEY) { |
| 514 | /* |
| 515 | * Determine which key we should be checking. |
| 516 | */ |
| 517 | if (len > sizeof(key_buf)) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 518 | rc = -1; |
| 519 | goto out; |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 520 | } |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 521 | rc = LOAD_IMAGE_DATA(hdr, fap, off, key_buf, len); |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 522 | if (rc) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 523 | goto out; |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 524 | } |
| 525 | key_id = bootutil_find_key(image_index, key_buf, len); |
| 526 | /* |
| 527 | * The key may not be found, which is acceptable. There |
| 528 | * can be multiple signatures, each preceded by a key. |
| 529 | */ |
| 530 | #endif /* !MCUBOOT_HW_KEY */ |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 531 | } else if (type == EXPECTED_SIG_TLV) { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 532 | /* Ignore this signature if it is out of bounds. */ |
| 533 | if (key_id < 0 || key_id >= bootutil_key_cnt) { |
| 534 | key_id = -1; |
| 535 | continue; |
| 536 | } |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 537 | if (!EXPECTED_SIG_LEN(len) || len > sizeof(buf)) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 538 | rc = -1; |
| 539 | goto out; |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 540 | } |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 541 | rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 542 | if (rc) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 543 | goto out; |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 544 | } |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 545 | FIH_CALL(bootutil_verify_sig, valid_signature, hash, sizeof(hash), |
| 546 | buf, len, key_id); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 547 | key_id = -1; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 548 | #endif /* EXPECTED_SIG_TLV */ |
| 549 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 550 | } else if (type == IMAGE_TLV_SEC_CNT) { |
| 551 | /* |
| 552 | * Verify the image's security counter. |
| 553 | * This must always be present. |
| 554 | */ |
| 555 | if (len != sizeof(img_security_cnt)) { |
| 556 | /* Security counter is not valid. */ |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 557 | rc = -1; |
| 558 | goto out; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 559 | } |
| 560 | |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 561 | rc = LOAD_IMAGE_DATA(hdr, fap, off, &img_security_cnt, len); |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 562 | if (rc) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 563 | goto out; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 564 | } |
| 565 | |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 566 | FIH_CALL(boot_nv_security_counter_get, fih_rc, image_index, |
| 567 | &security_cnt); |
| 568 | if (fih_not_eq(fih_rc, FIH_SUCCESS)) { |
| 569 | goto out; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 570 | } |
| 571 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 572 | BOOT_LOG_DBG("NV Counter read from efuse = %u", fih_uint_decode(security_cnt)); |
| 573 | |
Roman Okhrimenko | 2516562 | 2023-05-23 08:58:29 +0300 | [diff] [blame^] | 574 | #ifdef CYW20829 |
| 575 | BOOT_LOG_DBG("Full NV Counter read from image = 0x%X", img_security_cnt); |
| 576 | |
| 577 | FIH_CALL(platform_security_counter_check_extract, fih_rc, |
| 578 | (uint32_t)image_index, fih_uint_encode(img_security_cnt), &extracted_img_cnt); |
| 579 | |
| 580 | if (fih_not_eq(fih_rc, FIH_SUCCESS)) { |
| 581 | /* The image's security counter exceeds registered value for this image */ |
| 582 | goto out; |
| 583 | } |
| 584 | |
| 585 | img_security_cnt = fih_uint_decode(extracted_img_cnt); |
| 586 | #endif /*CYW20829*/ |
| 587 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 588 | BOOT_LOG_DBG("NV Counter read from image = %" PRIu32, img_security_cnt); |
| 589 | |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 590 | /* Compare the new image's security counter value against the |
| 591 | * stored security counter value. |
| 592 | */ |
Roman Okhrimenko | 2516562 | 2023-05-23 08:58:29 +0300 | [diff] [blame^] | 593 | fih_rc = fih_int_encode_zero_equality( (int32_t)(img_security_cnt < |
| 594 | fih_uint_decode(security_cnt)) ); |
| 595 | |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 596 | if (fih_not_eq(fih_rc, FIH_SUCCESS)) { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 597 | /* The image's security counter is not accepted. */ |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 598 | goto out; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 599 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 600 | #ifndef CYW20829 |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 601 | /* The image's security counter has been successfully verified. */ |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 602 | security_counter_valid = fih_rc; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 603 | } |
| 604 | #else |
| 605 | /* The image's security counter has been successfully verified. */ |
| 606 | security_counter_valid = fih_int_encode(HW_ROLLBACK_CNT_VALID); |
| 607 | } else if (type == IMAGE_TLV_PROV_PACK) { |
| 608 | |
| 609 | if (fih_eq(security_counter_valid, fih_int_encode(HW_ROLLBACK_CNT_VALID))) { |
| 610 | /* |
| 611 | * Verify the image reprovisioning packet. |
| 612 | * This must always be present. |
| 613 | */ |
| 614 | BOOT_LOG_INF("Prov packet length 0x51 TLV = %" PRIu16, len); |
| 615 | |
| 616 | if (len != sizeof(reprov_packet)) { |
| 617 | /* Re-provisioning packet is not valid. */ |
| 618 | rc = -1; |
| 619 | goto out; |
| 620 | } |
| 621 | |
| 622 | rc = LOAD_IMAGE_DATA(hdr, fap, off, reprov_packet, len); |
| 623 | if (rc) { |
| 624 | goto out; |
| 625 | } |
| 626 | |
| 627 | security_counter_valid = fih_int_encode(fih_int_decode(security_counter_valid) | REPROV_PACK_VALID); |
| 628 | } |
| 629 | else{ |
| 630 | goto out; |
| 631 | } |
| 632 | #endif /* CYW20829 */ |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 633 | #endif /* MCUBOOT_HW_ROLLBACK_PROT */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 634 | } |
| 635 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 636 | |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 637 | rc = !sha256_valid; |
| 638 | if (rc) { |
| 639 | goto out; |
| 640 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 641 | #ifdef EXPECTED_SIG_TLV |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 642 | fih_rc = fih_int_encode_zero_equality(fih_not_eq(valid_signature, |
| 643 | FIH_SUCCESS)); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 644 | #endif |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 645 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 646 | #ifdef CYW20829 |
| 647 | if (fih_not_eq(security_counter_valid, fih_int_encode(REPROV_PACK_VALID | HW_ROLLBACK_CNT_VALID))) { |
| 648 | BOOT_LOG_DBG("Reprovisioning packet TLV 0x51 is not present image = %d", image_index); |
| 649 | #else |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 650 | if (fih_not_eq(security_counter_valid, FIH_SUCCESS)) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 651 | #endif /* CYW20829 */ |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 652 | rc = -1; |
| 653 | goto out; |
| 654 | } |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 655 | #endif |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 656 | |
| 657 | out: |
| 658 | if (rc) { |
| 659 | fih_rc = fih_int_encode(rc); |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 660 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 661 | |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 662 | FIH_RET(fih_rc); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 663 | } |