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