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