|
| 1 | +/* |
| 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-2025 Arm Limited |
| 7 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 8 | + * |
| 9 | + * Original license: |
| 10 | + * |
| 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 | + |
| 29 | +#include <stdint.h> |
| 30 | + |
| 31 | +#include "bootutil/bootutil_log.h" |
| 32 | +#include "bootutil/crypto/sha.h" |
| 33 | +#include "bootutil/fault_injection_hardening.h" |
| 34 | +#include "bootutil/image.h" |
| 35 | +#include "bootutil/sign_key.h" |
| 36 | +#include "bootutil_priv.h" |
| 37 | +#include "mcuboot_config/mcuboot_config.h" |
| 38 | + |
| 39 | +#ifdef MCUBOOT_IMAGE_MULTI_SIG_SUPPORT |
| 40 | +#define NUM_OF_KEYS MCUBOOT_ROTPK_MAX_KEYS_PER_IMAGE |
| 41 | +#else |
| 42 | +#define NUM_OF_KEYS 1 |
| 43 | +#endif /* MCUBOOT_IMAGE_MULTI_SIG_SUPPORT */ |
| 44 | + |
| 45 | +#if defined(MCUBOOT_BUILTIN_KEY) |
| 46 | +int bootutil_find_key(uint8_t image_index, uint8_t *key_id_buf, uint8_t key_id_buf_len) |
| 47 | +{ |
| 48 | + uint32_t key_id; |
| 49 | + FIH_DECLARE(fih_rc, FIH_FAILURE); |
| 50 | + |
| 51 | + /* Key id is passed */ |
| 52 | + assert(key_id_buf_len == sizeof(uint32_t)); |
| 53 | + memcpy(&key_id, key_id_buf, sizeof(key_id)); |
| 54 | + |
| 55 | + /* Check if key id is associated with the image */ |
| 56 | + FIH_CALL(boot_verify_key_id_for_image, fih_rc, image_index, key_id); |
| 57 | + if (FIH_EQ(fih_rc, FIH_SUCCESS)) { |
| 58 | + return (int32_t)key_id; |
| 59 | + } |
| 60 | + |
| 61 | + return -1; |
| 62 | +} |
| 63 | + |
| 64 | +#elif defined(MCUBOOT_HW_KEY) |
| 65 | +extern unsigned int pub_key_len; |
| 66 | +int bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len) |
| 67 | +{ |
| 68 | + bootutil_sha_context sha_ctx; |
| 69 | + uint8_t hash[IMAGE_HASH_SIZE]; |
| 70 | + uint8_t key_hash[IMAGE_HASH_SIZE]; |
| 71 | + size_t key_hash_size = sizeof(key_hash); |
| 72 | + int rc; |
| 73 | + uint8_t key_index; |
| 74 | + FIH_DECLARE(fih_rc, FIH_FAILURE); |
| 75 | + |
| 76 | + bootutil_sha_init(&sha_ctx); |
| 77 | + bootutil_sha_update(&sha_ctx, key, key_len); |
| 78 | + bootutil_sha_finish(&sha_ctx, hash); |
| 79 | + bootutil_sha_drop(&sha_ctx); |
| 80 | + |
| 81 | + for(key_index = 0; key_index < NUM_OF_KEYS; key_index++) { |
| 82 | + rc = boot_retrieve_public_key_hash(image_index, key_index, key_hash, &key_hash_size); |
| 83 | + if (rc) { |
| 84 | + return -1; |
| 85 | + } |
| 86 | + |
| 87 | + /* Adding hardening to avoid this potential attack: |
| 88 | + * - Image is signed with an arbitrary key and the corresponding public |
| 89 | + * key is added as a TLV field. |
| 90 | + * - During public key validation (comparing against key-hash read from |
| 91 | + * HW) a fault is injected to accept the public key as valid one. |
| 92 | + */ |
| 93 | + FIH_CALL(boot_fih_memequal, fih_rc, hash, key_hash, key_hash_size); |
| 94 | + if (FIH_EQ(fih_rc, FIH_SUCCESS)) { |
| 95 | + BOOT_LOG_INF("Key %d hash found for image %d", key_index, image_index); |
| 96 | + bootutil_keys[0].key = key; |
| 97 | + pub_key_len = key_len; |
| 98 | + return 0; |
| 99 | + } |
| 100 | + } |
| 101 | + BOOT_LOG_ERR("Key hash NOT found for image %d!", image_index); |
| 102 | + |
| 103 | + return -1; |
| 104 | +} |
| 105 | + |
| 106 | +#else /* !defined MCUBOOT_BUILTIN_KEY && !defined MCUBOOT_HW_KEY */ |
| 107 | +int bootutil_find_key(uint8_t image_index, uint8_t *keyhash, uint8_t keyhash_len) |
| 108 | +{ |
| 109 | + bootutil_sha_context sha_ctx; |
| 110 | + int i; |
| 111 | + uint8_t hash[IMAGE_HASH_SIZE]; |
| 112 | + const struct bootutil_key *key; |
| 113 | + (void)image_index; |
| 114 | + |
| 115 | + if (keyhash_len > IMAGE_HASH_SIZE) { |
| 116 | + return -1; |
| 117 | + } |
| 118 | + |
| 119 | + for (i = 0; i < bootutil_key_cnt; i++) { |
| 120 | + key = &bootutil_keys[i]; |
| 121 | + bootutil_sha_init(&sha_ctx); |
| 122 | + bootutil_sha_update(&sha_ctx, key->key, *key->len); |
| 123 | + bootutil_sha_finish(&sha_ctx, hash); |
| 124 | + bootutil_sha_drop(&sha_ctx); |
| 125 | + if (!memcmp(hash, keyhash, keyhash_len)) { |
| 126 | + return i; |
| 127 | + } |
| 128 | + } |
| 129 | + return -1; |
| 130 | +} |
| 131 | +#endif |
0 commit comments