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) |
Roman Okhrimenko | 883cb5b | 2024-03-28 17:22:33 +0200 | [diff] [blame^] | 129 | if (IS_RAM_BOOTABLE(hdr) && IS_RAM_BOOT_STAGE()) |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 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 | |
Roman Okhrimenko | 883cb5b | 2024-03-28 17:22:33 +0200 | [diff] [blame^] | 230 | /* Complex result masks for bootutil_img_validate() */ |
| 231 | #define SET_MASK_IMAGE_TLV_SHA256 ((signed)0x0000002E) |
| 232 | fih_int FIH_MASK_IMAGE_TLV_SHA256 = FIH_INT_INIT( |
| 233 | SET_MASK_IMAGE_TLV_SHA256); |
| 234 | |
| 235 | #define SET_MASK_SIG_TLV_EXPECTED ((signed)0x00007400) |
| 236 | fih_int FIH_MASK_SIG_TLV_EXPECTED = FIH_INT_INIT( |
| 237 | SET_MASK_SIG_TLV_EXPECTED); |
| 238 | |
| 239 | #define SET_MASK_IMAGE_TLV_SEC_CNT ((signed)0x005C0000) |
| 240 | fih_int FIH_MASK_IMAGE_TLV_SEC_CNT = FIH_INT_INIT( |
| 241 | SET_MASK_IMAGE_TLV_SEC_CNT); |
| 242 | |
| 243 | #define CHK_MASK_IMAGE_TLV_SHA256 SET_MASK_IMAGE_TLV_SHA256 |
| 244 | |
| 245 | #if defined MCUBOOT_SKIP_VALIDATE_SECONDARY_SLOT |
| 246 | #if defined MCUBOOT_VALIDATE_PRIMARY_SLOT |
| 247 | #error Boot slot validation cannot be enabled if upgrade slot validation is disabled |
| 248 | #endif |
| 249 | #endif |
| 250 | |
| 251 | #if defined(MCUBOOT_SIGN_RSA) || \ |
| 252 | defined(MCUBOOT_SIGN_EC) || \ |
| 253 | defined(MCUBOOT_SIGN_EC256) || \ |
| 254 | defined(MCUBOOT_SIGN_ED25519) |
| 255 | |
| 256 | #if defined MCUBOOT_SKIP_VALIDATE_SECONDARY_SLOT |
| 257 | #define CHK_MASK_SIG_TLV_EXPECTED ((signed)0) |
| 258 | #else |
| 259 | #define CHK_MASK_SIG_TLV_EXPECTED SET_MASK_SIG_TLV_EXPECTED |
| 260 | #endif /* MCUBOOT_SKIP_VALIDATE_SECONDARY_SLOT */ |
| 261 | #else |
| 262 | #define CHK_MASK_SIG_TLV_EXPECTED ((signed)0) |
| 263 | #endif /* defined(MCUBOOT_SIGN_RSA) || |
| 264 | defined(MCUBOOT_SIGN_EC) || |
| 265 | defined(MCUBOOT_SIGN_EC256) || |
| 266 | defined(MCUBOOT_SIGN_ED25519) */ |
| 267 | |
| 268 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 269 | #define CHK_MASK_IMAGE_TLV_SEC_CNT SET_MASK_IMAGE_TLV_SEC_CNT |
| 270 | #else |
| 271 | #define CHK_MASK_IMAGE_TLV_SEC_CNT ((signed)0) |
| 272 | #endif /* MCUBOOT_HW_ROLLBACK_PROT */ |
| 273 | |
| 274 | fih_int FIH_IMG_VALIDATE_COMPLEX_OK = FIH_INT_INIT( \ |
| 275 | CHK_MASK_IMAGE_TLV_SHA256 | \ |
| 276 | CHK_MASK_SIG_TLV_EXPECTED | \ |
| 277 | CHK_MASK_IMAGE_TLV_SEC_CNT); |
| 278 | |
| 279 | #undef SET_MASK_IMAGE_TLV_SHA256 |
| 280 | #undef SET_MASK_SIG_TLV_EXPECTED |
| 281 | #undef SET_MASK_IMAGE_TLV_SEC_CNT |
| 282 | |
| 283 | #undef CHK_MASK_IMAGE_TLV_SHA256 |
| 284 | #undef CHK_MASK_SIG_TLV_EXPECTED |
| 285 | #undef CHK_MASK_IMAGE_TLV_SEC_CNT |
| 286 | |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 287 | #ifdef EXPECTED_SIG_TLV |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 288 | #if !defined(MCUBOOT_HW_KEY) |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 289 | static int |
| 290 | bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len) |
| 291 | { |
| 292 | bootutil_sha256_context sha256_ctx; |
| 293 | int i; |
| 294 | const struct bootutil_key *key; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 295 | uint8_t hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE]; |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 296 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 297 | if (keyhash_len != BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE) { |
Fabio Utzig | 15c1467 | 2019-08-09 07:45:20 -0300 | [diff] [blame] | 298 | return -1; |
| 299 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 300 | |
| 301 | for (i = 0; i < bootutil_key_cnt; i++) { |
| 302 | key = &bootutil_keys[i]; |
| 303 | bootutil_sha256_init(&sha256_ctx); |
| 304 | bootutil_sha256_update(&sha256_ctx, key->key, *key->len); |
| 305 | bootutil_sha256_finish(&sha256_ctx, hash); |
| 306 | if (!memcmp(hash, keyhash, keyhash_len)) { |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 307 | bootutil_sha256_drop(&sha256_ctx); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 308 | return i; |
| 309 | } |
| 310 | } |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 311 | bootutil_sha256_drop(&sha256_ctx); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 312 | return -1; |
| 313 | } |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 314 | #else |
| 315 | extern unsigned int pub_key_len; |
| 316 | static int |
| 317 | bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len) |
| 318 | { |
| 319 | bootutil_sha256_context sha256_ctx; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 320 | uint8_t hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE]; |
| 321 | uint8_t key_hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE]; |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 322 | size_t key_hash_size = sizeof(key_hash); |
| 323 | int rc; |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 324 | fih_int fih_rc; |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 325 | |
| 326 | bootutil_sha256_init(&sha256_ctx); |
| 327 | bootutil_sha256_update(&sha256_ctx, key, key_len); |
| 328 | bootutil_sha256_finish(&sha256_ctx, hash); |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 329 | bootutil_sha256_drop(&sha256_ctx); |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 330 | |
| 331 | rc = boot_retrieve_public_key_hash(image_index, key_hash, &key_hash_size); |
| 332 | if (rc) { |
| 333 | return rc; |
| 334 | } |
| 335 | |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 336 | /* Adding hardening to avoid this potential attack: |
| 337 | * - Image is signed with an arbitrary key and the corresponding public |
| 338 | * key is added as a TLV field. |
| 339 | * - During public key validation (comparing against key-hash read from |
| 340 | * HW) a fault is injected to accept the public key as valid one. |
| 341 | */ |
| 342 | 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] | 343 | if (FIH_TRUE == fih_eq(fih_rc, FIH_SUCCESS)) { |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 344 | bootutil_keys[0].key = key; |
| 345 | pub_key_len = key_len; |
| 346 | return 0; |
| 347 | } |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 348 | |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 349 | return -1; |
| 350 | } |
| 351 | #endif /* !MCUBOOT_HW_KEY */ |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 352 | #endif |
| 353 | |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 354 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 355 | /** |
| 356 | * Reads the value of an image's security counter. |
| 357 | * |
| 358 | * @param hdr Pointer to the image header structure. |
| 359 | * @param fap Pointer to a description structure of the image's |
| 360 | * flash area. |
| 361 | * @param security_cnt Pointer to store the security counter value. |
| 362 | * |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 363 | * @return FIH_SUCCESS on success; FIH_FAILURE on failure. |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 364 | */ |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 365 | fih_int |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 366 | bootutil_get_img_security_cnt(struct image_header *hdr, |
| 367 | const struct flash_area *fap, |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 368 | fih_uint *img_security_cnt) |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 369 | { |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 370 | uint32_t img_sec_cnt = 0u; |
| 371 | uint32_t img_chk_cnt = 0u; |
| 372 | struct image_tlv_iter it = {0}; |
| 373 | uint32_t off = 0u; |
| 374 | uint16_t len = 0u; |
| 375 | int32_t rc = -1; |
| 376 | fih_int fih_rc = FIH_FAILURE; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 377 | |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 378 | if ((NULL == hdr) || |
| 379 | (NULL == fap) || |
| 380 | (NULL == img_security_cnt)) { |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 381 | /* Invalid parameter. */ |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 382 | goto out; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /* 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] | 386 | if (0u == hdr->ih_protect_tlv_size) { |
| 387 | goto out; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | 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] | 391 | if (rc != 0) { |
| 392 | goto out; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* Traverse through the protected TLV area to find |
| 396 | * the security counter TLV. |
| 397 | */ |
| 398 | |
| 399 | rc = bootutil_tlv_iter_next(&it, &off, &len, NULL); |
| 400 | if (rc != 0) { |
| 401 | /* Security counter TLV has not been found. */ |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 402 | goto out; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 403 | } |
| 404 | |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 405 | if (len != sizeof(img_sec_cnt)) { |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 406 | /* Security counter is not valid. */ |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 407 | goto out; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 408 | } |
| 409 | |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 410 | rc = LOAD_IMAGE_DATA(hdr, fap, off, &img_sec_cnt, len); |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 411 | if (rc != 0) { |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 412 | goto out; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 413 | } |
| 414 | |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 415 | *img_security_cnt = fih_uint_encode(img_sec_cnt); |
| 416 | |
| 417 | rc = LOAD_IMAGE_DATA(hdr, fap, off, &img_chk_cnt, len); |
| 418 | if (rc != 0) { |
| 419 | goto out; |
| 420 | } |
| 421 | |
| 422 | if (FIH_TRUE == fih_uint_eq(fih_uint_encode(img_chk_cnt), |
| 423 | *img_security_cnt)) { |
| 424 | |
| 425 | if (img_sec_cnt == img_chk_cnt) { |
| 426 | fih_rc = FIH_SUCCESS; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | out: |
| 431 | FIH_RET(fih_rc); |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 432 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 433 | #ifdef CYW20829 |
| 434 | /** |
| 435 | * Reads the content of an image's reprovisioning packet. |
| 436 | * |
| 437 | * @param hdr Pointer to the image header structure. |
| 438 | * @param fap Pointer to a description structure of the image's |
| 439 | * flash area. |
| 440 | * @param reprov_packet Pointer to store the reprovisioning packet. |
| 441 | * |
| 442 | * @return 0 on success; nonzero on failure. |
| 443 | */ |
| 444 | int32_t |
| 445 | bootutil_get_img_reprov_packet(struct image_header *hdr, |
| 446 | const struct flash_area *fap, |
| 447 | uint8_t *reprov_packet) |
| 448 | { |
| 449 | struct image_tlv_iter it; |
| 450 | uint32_t off; |
| 451 | uint16_t len; |
| 452 | int32_t rc; |
| 453 | |
| 454 | if ((hdr == NULL) || |
| 455 | (fap == NULL) || |
| 456 | (reprov_packet == NULL)) { |
| 457 | /* Invalid parameter. */ |
| 458 | return BOOT_EBADARGS; |
| 459 | } |
| 460 | |
| 461 | /* The reprovisioning packet TLV is in the protected part of the TLV area. */ |
| 462 | if (hdr->ih_protect_tlv_size == 0) { |
| 463 | return BOOT_EBADIMAGE; |
| 464 | } |
| 465 | |
| 466 | rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_PROV_PACK, true); |
| 467 | if (rc) { |
| 468 | return rc; |
| 469 | } |
| 470 | |
| 471 | /* Traverse through the protected TLV area to find |
| 472 | * the reprovisioning apcket TLV. |
| 473 | */ |
| 474 | |
| 475 | rc = bootutil_tlv_iter_next(&it, &off, &len, NULL); |
| 476 | if (rc != 0) { |
| 477 | /* Reprovisioning packet TLV has not been found. */ |
| 478 | return -1; |
| 479 | } |
| 480 | |
| 481 | if (len != REPROV_PACK_SIZE) { |
| 482 | /* Reprovisioning packet is not valid. */ |
| 483 | return BOOT_EBADIMAGE; |
| 484 | } |
| 485 | |
| 486 | rc = LOAD_IMAGE_DATA(hdr, fap, off, reprov_packet, len); |
| 487 | if (rc != 0) { |
| 488 | return BOOT_EFLASH; |
| 489 | } |
| 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | #endif /* CYW20289 */ |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 494 | #endif /* MCUBOOT_HW_ROLLBACK_PROT */ |
| 495 | |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 496 | /* |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 497 | * Verify the integrity of the image. |
| 498 | * Return non-zero if image could not be validated/does not validate. |
| 499 | */ |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 500 | fih_int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 501 | bootutil_img_validate(struct enc_key_data *enc_state, int image_index, |
| 502 | struct image_header *hdr, const struct flash_area *fap, |
| 503 | uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *seed, |
| 504 | int seed_len, uint8_t *out_hash) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 505 | { |
| 506 | uint32_t off; |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 507 | uint16_t len; |
David Brown | d13318a | 2019-12-04 17:28:40 -0700 | [diff] [blame] | 508 | uint16_t type; |
Roman Okhrimenko | 883cb5b | 2024-03-28 17:22:33 +0200 | [diff] [blame^] | 509 | |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 510 | #ifdef EXPECTED_SIG_TLV |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 511 | fih_int valid_signature = FIH_FAILURE; |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 512 | int key_id = -1; |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 513 | #ifdef MCUBOOT_HW_KEY |
| 514 | /* Few extra bytes for encoding and for public exponent. */ |
| 515 | uint8_t key_buf[SIG_BUF_SIZE + 24]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 516 | #endif |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 517 | #endif /* EXPECTED_SIG_TLV */ |
Roman Okhrimenko | 883cb5b | 2024-03-28 17:22:33 +0200 | [diff] [blame^] | 518 | |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 519 | struct image_tlv_iter it; |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 520 | uint8_t buf[SIG_BUF_SIZE]; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 521 | uint8_t hash[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE]; |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 522 | int rc = 0; |
| 523 | fih_int fih_rc = FIH_FAILURE; |
Roman Okhrimenko | 883cb5b | 2024-03-28 17:22:33 +0200 | [diff] [blame^] | 524 | /* fih_complex_result stores patterns of successful execution |
| 525 | * of required checks |
| 526 | */ |
| 527 | fih_int fih_complex_result = FIH_INT_ZERO; |
| 528 | |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 529 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 530 | fih_uint security_cnt = FIH_UINT_MAX; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 531 | uint32_t img_security_cnt = 0; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 532 | uint8_t reprov_packet[REPROV_PACK_SIZE]; |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 533 | fih_int security_counter_valid = FIH_FAILURE; |
Roman Okhrimenko | 2516562 | 2023-05-23 08:58:29 +0300 | [diff] [blame] | 534 | #ifdef CYW20829 |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 535 | fih_uint extracted_img_cnt = FIH_UINT_MAX; |
Roman Okhrimenko | 2516562 | 2023-05-23 08:58:29 +0300 | [diff] [blame] | 536 | #endif /* CYW20829 */ |
| 537 | #endif /* MCUBOOT_HW_ROLLBACK_PROT */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 538 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 539 | rc = bootutil_img_hash(enc_state, image_index, hdr, fap, tmp_buf, |
| 540 | tmp_buf_sz, hash, seed, seed_len); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 541 | if (rc) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 542 | goto out; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | if (out_hash) { |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 546 | (void)memcpy(out_hash, hash, BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 547 | } |
| 548 | |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 549 | rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false); |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 550 | if (rc) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 551 | goto out; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 552 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 553 | |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 554 | /* |
| 555 | * Traverse through all of the TLVs, performing any checks we know |
| 556 | * and are able to do. |
| 557 | */ |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 558 | while (true) { |
| 559 | rc = bootutil_tlv_iter_next(&it, &off, &len, &type); |
| 560 | if (rc < 0) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 561 | goto out; |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 562 | } else if (rc > 0) { |
| 563 | break; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 564 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 565 | |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 566 | if (type == IMAGE_TLV_SHA256) { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 567 | /* |
| 568 | * Verify the SHA256 image hash. This must always be |
| 569 | * present. |
| 570 | */ |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 571 | if (len != sizeof(hash)) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 572 | rc = -1; |
| 573 | goto out; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 574 | } |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 575 | rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, sizeof(hash)); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 576 | if (rc) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 577 | goto out; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 578 | } |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 579 | |
| 580 | FIH_CALL(boot_fih_memequal, fih_rc, hash, buf, sizeof(hash)); |
Roman Okhrimenko | 883cb5b | 2024-03-28 17:22:33 +0200 | [diff] [blame^] | 581 | |
| 582 | if (fih_eq(fih_rc, FIH_SUCCESS) == FIH_TRUE) { |
| 583 | /* Encode succesful completion pattern to complex_result */ |
| 584 | fih_complex_result = fih_or(fih_complex_result, |
| 585 | FIH_MASK_IMAGE_TLV_SHA256); |
| 586 | } |
| 587 | else { |
| 588 | BOOT_LOG_DBG("IMAGE_TLV_SHA256 is invalid"); |
| 589 | rc = -1; |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 590 | goto out; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 591 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 592 | |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 593 | #ifdef EXPECTED_SIG_TLV |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 594 | #ifndef MCUBOOT_HW_KEY |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 595 | } else if (type == IMAGE_TLV_KEYHASH) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 596 | /* |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 597 | * Determine which key we should be checking. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 598 | */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 599 | if (len != BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 600 | rc = -1; |
| 601 | goto out; |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 602 | } |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 603 | rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 604 | if (rc) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 605 | goto out; |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 606 | } |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 607 | key_id = bootutil_find_key(buf, len); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 608 | /* |
| 609 | * The key may not be found, which is acceptable. There |
| 610 | * can be multiple signatures, each preceded by a key. |
| 611 | */ |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 612 | #else |
| 613 | } else if (type == IMAGE_TLV_PUBKEY) { |
| 614 | /* |
| 615 | * Determine which key we should be checking. |
| 616 | */ |
| 617 | if (len > sizeof(key_buf)) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 618 | rc = -1; |
| 619 | goto out; |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 620 | } |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 621 | rc = LOAD_IMAGE_DATA(hdr, fap, off, key_buf, len); |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 622 | if (rc) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 623 | goto out; |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 624 | } |
| 625 | key_id = bootutil_find_key(image_index, key_buf, len); |
| 626 | /* |
| 627 | * The key may not be found, which is acceptable. There |
| 628 | * can be multiple signatures, each preceded by a key. |
| 629 | */ |
| 630 | #endif /* !MCUBOOT_HW_KEY */ |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 631 | } else if (type == EXPECTED_SIG_TLV) { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 632 | /* Ignore this signature if it is out of bounds. */ |
| 633 | if (key_id < 0 || key_id >= bootutil_key_cnt) { |
| 634 | key_id = -1; |
| 635 | continue; |
| 636 | } |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 637 | if (!EXPECTED_SIG_LEN(len) || len > sizeof(buf)) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 638 | rc = -1; |
| 639 | goto out; |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 640 | } |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 641 | rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 642 | if (rc) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 643 | goto out; |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 644 | } |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 645 | FIH_CALL(bootutil_verify_sig, valid_signature, hash, sizeof(hash), |
| 646 | buf, len, key_id); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 647 | key_id = -1; |
Roman Okhrimenko | 883cb5b | 2024-03-28 17:22:33 +0200 | [diff] [blame^] | 648 | |
| 649 | if (FIH_TRUE == fih_eq(FIH_SUCCESS, valid_signature)) { |
| 650 | /* Encode succesful completion pattern to complex_result */ |
| 651 | fih_complex_result = fih_or(fih_complex_result, |
| 652 | FIH_MASK_SIG_TLV_EXPECTED); |
| 653 | } else { |
| 654 | BOOT_LOG_DBG("Invalid signature of bootable image %d", |
| 655 | image_index); |
| 656 | rc = -1; |
| 657 | goto out; |
| 658 | } |
| 659 | |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 660 | #endif /* EXPECTED_SIG_TLV */ |
| 661 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 662 | } else if (type == IMAGE_TLV_SEC_CNT) { |
| 663 | /* |
| 664 | * Verify the image's security counter. |
| 665 | * This must always be present. |
| 666 | */ |
| 667 | if (len != sizeof(img_security_cnt)) { |
| 668 | /* Security counter is not valid. */ |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 669 | rc = -1; |
| 670 | goto out; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 671 | } |
| 672 | |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 673 | rc = LOAD_IMAGE_DATA(hdr, fap, off, &img_security_cnt, len); |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 674 | if (rc) { |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 675 | goto out; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 676 | } |
| 677 | |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 678 | FIH_CALL(boot_nv_security_counter_get, fih_rc, image_index, |
| 679 | &security_cnt); |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 680 | if (fih_eq(fih_rc, FIH_SUCCESS) != FIH_TRUE) { |
Roman Okhrimenko | 883cb5b | 2024-03-28 17:22:33 +0200 | [diff] [blame^] | 681 | rc = -1; |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 682 | goto out; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 683 | } |
| 684 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 685 | BOOT_LOG_DBG("NV Counter read from efuse = %u", fih_uint_decode(security_cnt)); |
| 686 | |
Roman Okhrimenko | 2516562 | 2023-05-23 08:58:29 +0300 | [diff] [blame] | 687 | #ifdef CYW20829 |
| 688 | BOOT_LOG_DBG("Full NV Counter read from image = 0x%X", img_security_cnt); |
| 689 | |
| 690 | FIH_CALL(platform_security_counter_check_extract, fih_rc, |
| 691 | (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] | 692 | |
| 693 | if (fih_eq(fih_rc, FIH_SUCCESS) != FIH_TRUE) { |
Roman Okhrimenko | 2516562 | 2023-05-23 08:58:29 +0300 | [diff] [blame] | 694 | /* The image's security counter exceeds registered value for this image */ |
Roman Okhrimenko | 883cb5b | 2024-03-28 17:22:33 +0200 | [diff] [blame^] | 695 | rc = -1; |
Roman Okhrimenko | 2516562 | 2023-05-23 08:58:29 +0300 | [diff] [blame] | 696 | goto out; |
| 697 | } |
| 698 | |
| 699 | img_security_cnt = fih_uint_decode(extracted_img_cnt); |
| 700 | #endif /*CYW20829*/ |
| 701 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 702 | BOOT_LOG_DBG("NV Counter read from image = %" PRIu32, img_security_cnt); |
| 703 | |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 704 | /* Compare the new image's security counter value against the |
| 705 | * stored security counter value. |
| 706 | */ |
Roman Okhrimenko | 883cb5b | 2024-03-28 17:22:33 +0200 | [diff] [blame^] | 707 | if (FIH_TRUE == fih_uint_ge(fih_uint_encode(img_security_cnt), security_cnt)) { |
| 708 | /* Encode succesful completion pattern to complex_result */ |
| 709 | fih_complex_result = fih_or(fih_complex_result, |
| 710 | FIH_MASK_IMAGE_TLV_SEC_CNT); |
| 711 | #ifdef CYW20829 |
| 712 | /* The image's security counter has been successfully verified. */ |
| 713 | security_counter_valid = fih_int_encode(HW_ROLLBACK_CNT_VALID); |
| 714 | #endif |
| 715 | } else { |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 716 | /* The image's security counter is not accepted. */ |
Roman Okhrimenko | 883cb5b | 2024-03-28 17:22:33 +0200 | [diff] [blame^] | 717 | rc = -1; |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 718 | goto out; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 719 | } |
Roman Okhrimenko | 883cb5b | 2024-03-28 17:22:33 +0200 | [diff] [blame^] | 720 | |
| 721 | #ifdef CYW20829 |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 722 | } else if (type == IMAGE_TLV_PROV_PACK) { |
| 723 | |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 724 | 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] | 725 | /* |
| 726 | * Verify the image reprovisioning packet. |
| 727 | * This must always be present. |
| 728 | */ |
| 729 | BOOT_LOG_INF("Prov packet length 0x51 TLV = %" PRIu16, len); |
| 730 | |
| 731 | if (len != sizeof(reprov_packet)) { |
| 732 | /* Re-provisioning packet is not valid. */ |
| 733 | rc = -1; |
| 734 | goto out; |
| 735 | } |
| 736 | |
| 737 | rc = LOAD_IMAGE_DATA(hdr, fap, off, reprov_packet, len); |
| 738 | if (rc) { |
| 739 | goto out; |
| 740 | } |
| 741 | |
| 742 | security_counter_valid = fih_int_encode(fih_int_decode(security_counter_valid) | REPROV_PACK_VALID); |
| 743 | } |
Roman Okhrimenko | 883cb5b | 2024-03-28 17:22:33 +0200 | [diff] [blame^] | 744 | else { |
| 745 | rc = -1; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 746 | goto out; |
| 747 | } |
| 748 | #endif /* CYW20829 */ |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 749 | #endif /* MCUBOOT_HW_ROLLBACK_PROT */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 750 | } |
| 751 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 752 | |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 753 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 754 | #ifdef CYW20829 |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 755 | 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] | 756 | BOOT_LOG_DBG("Reprovisioning packet TLV 0x51 is not present image = %d", image_index); |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 757 | rc = -1; |
| 758 | goto out; |
| 759 | } |
Roman Okhrimenko | 883cb5b | 2024-03-28 17:22:33 +0200 | [diff] [blame^] | 760 | #endif /* CYW20829 */ |
| 761 | #endif /* MCUBOOT_HW_ROLLBACK_PROT */ |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 762 | |
| 763 | out: |
Roman Okhrimenko | 883cb5b | 2024-03-28 17:22:33 +0200 | [diff] [blame^] | 764 | if (rc < 0) { |
| 765 | fih_complex_result = FIH_FAILURE; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 766 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 767 | |
Roman Okhrimenko | 883cb5b | 2024-03-28 17:22:33 +0200 | [diff] [blame^] | 768 | FIH_RET(fih_complex_result); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 769 | } |