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