|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | + |
| 8 | +#include <string.h> |
| 9 | + |
| 10 | +#include "mcuboot_config/mcuboot_config.h" |
| 11 | + |
| 12 | +#include "bootutil/sign_key.h" |
| 13 | + |
| 14 | +#include <assert.h> |
| 15 | +#include <string.h> |
| 16 | +#include <stdint.h> |
| 17 | + |
| 18 | +#include "bootutil/bootutil_log.h" |
| 19 | +#include "bootutil/crypto/sha.h" |
| 20 | +#include "bootutil_priv.h" |
| 21 | + |
| 22 | +BOOT_LOG_MODULE_DECLARE(mcuboot); |
| 23 | + |
| 24 | +#ifdef MCUBOOT_SIGN_RSA |
| 25 | +#include <psa/crypto.h> |
| 26 | +#include <psa/crypto_types.h> |
| 27 | +#include <zephyr/sys/util.h> |
| 28 | +#include "bootutil_priv.h" |
| 29 | +#include "bootutil/sign_key.h" |
| 30 | +#include "bootutil/fault_injection_hardening.h" |
| 31 | +#ifdef CONFIG_NCS_MCUBOOT_LCS_AWARE |
| 32 | +#include <nrf_lcs/nrf_lcs.h> |
| 33 | +#endif |
| 34 | +#define RSA_SIGNATURE_LENGTH (256) |
| 35 | +#define RSA_PUBLIC_KEY_BIT_SIZE (2048) |
| 36 | + |
| 37 | +extern const unsigned int rsa_pub_key_len; |
| 38 | + |
| 39 | +int rsa_verify(const uint8_t *message, size_t message_len, |
| 40 | + const uint8_t signature[RSA_SIGNATURE_LENGTH], |
| 41 | + const uint8_t public_key[PSA_EXPORT_PUBLIC_KEY_MAX_SIZE]) |
| 42 | +{ |
| 43 | + /* Set to any error */ |
| 44 | + psa_status_t status = PSA_ERROR_BAD_STATE; |
| 45 | + psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT; |
| 46 | + int ret = 0; /* Fail by default */ |
| 47 | + psa_key_id_t key_id; |
| 48 | + |
| 49 | + BOOT_LOG_DBG("rsa_verify: PSA implementation, plain key"); |
| 50 | + |
| 51 | + /* Initialize PSA Crypto */ |
| 52 | + status = psa_crypto_init(); |
| 53 | + if (status != PSA_SUCCESS) { |
| 54 | + BOOT_LOG_ERR("PSA crypto init failed %d\n", status); |
| 55 | + return 0; |
| 56 | + } |
| 57 | + |
| 58 | + status = PSA_ERROR_BAD_STATE; |
| 59 | + |
| 60 | + psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_VERIFY_HASH); |
| 61 | + psa_set_key_lifetime(&key_attr, PSA_KEY_LIFETIME_VOLATILE); |
| 62 | + psa_set_key_algorithm(&key_attr, PSA_ALG_RSA_PSS(PSA_ALG_SHA_256)); |
| 63 | + psa_set_key_type(&key_attr, PSA_KEY_TYPE_RSA_PUBLIC_KEY); |
| 64 | + psa_set_key_bits(&key_attr, RSA_PUBLIC_KEY_BIT_SIZE); |
| 65 | + |
| 66 | + BOOT_LOG_DBG("Importing RSA PSS key of size: %d", rsa_pub_key_len); |
| 67 | + |
| 68 | + status = psa_import_key(&key_attr, public_key, rsa_pub_key_len, |
| 69 | + &key_id); |
| 70 | + if (status != PSA_SUCCESS) { |
| 71 | + BOOT_LOG_ERR("RSA import key failed %d", status); |
| 72 | + ret = 0; |
| 73 | + } |
| 74 | + |
| 75 | + BOOT_LOG_DBG("Key imported. key_id: %d", (int)key_id); |
| 76 | + |
| 77 | + status = PSA_ERROR_BAD_STATE; |
| 78 | + |
| 79 | + BOOT_LOG_INF("Verifying RSA PSS with signature len: %d", RSA_SIGNATURE_LENGTH); |
| 80 | + |
| 81 | + status = psa_verify_hash(key_id, PSA_ALG_RSA_PSS(PSA_ALG_SHA_256), |
| 82 | + message, message_len, signature, RSA_SIGNATURE_LENGTH); |
| 83 | + if (status != PSA_SUCCESS) { |
| 84 | + BOOT_LOG_ERR("RSA signature verification failed %d", status); |
| 85 | + ret = 0; |
| 86 | + } else { |
| 87 | + BOOT_LOG_INF("RSA signature verification successful"); |
| 88 | + ret = 1; |
| 89 | + } |
| 90 | + |
| 91 | + status = psa_destroy_key(key_id); |
| 92 | + if (status != PSA_SUCCESS) { |
| 93 | + BOOT_LOG_ERR("Failed to destroy imported public key: %d", status); |
| 94 | + ret = 0; |
| 95 | + } else { |
| 96 | + BOOT_LOG_DBG("Destroyed imported key"); |
| 97 | + } |
| 98 | + |
| 99 | + return ret; |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +static fih_ret |
| 104 | +bootutil_verify(uint8_t *buf, uint32_t blen, |
| 105 | + uint8_t *sig, size_t slen, |
| 106 | + uint8_t key_id) |
| 107 | +{ |
| 108 | + int rc; |
| 109 | + FIH_DECLARE(fih_rc, FIH_FAILURE); |
| 110 | + uint8_t *pubkey = NULL; |
| 111 | + |
| 112 | +#ifdef CONFIG_NCS_MCUBOOT_LCS_AWARE |
| 113 | + if (nrf_lcs_get() != NRF_LCS_SECURED) { |
| 114 | + if (key_id != CONFIG_NCS_MCUBOOT_MANUFACTURING_APP_KEY_IDENTIFIER) { |
| 115 | + BOOT_LOG_ERR("bootutil_verify_sig: invalid key ID %d for non-secured device", key_id); |
| 116 | + FIH_RET(FIH_FAILURE); |
| 117 | + } |
| 118 | + BOOT_LOG_DBG("bootutil_verify_sig: using manufacturing application key ID %d", key_id); |
| 119 | + } |
| 120 | +#endif /* CONFIG_NCS_MCUBOOT_LCS_AWARE */ |
| 121 | + |
| 122 | + BOOT_LOG_DBG("bootutil_verify_sig: RSA key_id %d", key_id); |
| 123 | + |
| 124 | + if (slen != RSA_SIGNATURE_LENGTH) { |
| 125 | + BOOT_LOG_DBG("bootutil_verify: expected slen %d, got %u", |
| 126 | + RSA_SIGNATURE_LENGTH, (unsigned int)slen); |
| 127 | + FIH_SET(fih_rc, FIH_FAILURE); |
| 128 | + goto out; |
| 129 | + } |
| 130 | + |
| 131 | + pubkey = (uint8_t *)bootutil_keys[key_id].key; |
| 132 | + |
| 133 | + BOOT_LOG_DBG("bootutil_verify: RSA key_id %d", (int)key_id); |
| 134 | + |
| 135 | + rc = rsa_verify(buf, blen, sig, pubkey); |
| 136 | + |
| 137 | + BOOT_LOG_DBG("bootutil_verify: rsa_verify status %d", rc); |
| 138 | + |
| 139 | + if (rc == 0) { |
| 140 | + /* if verify returns 0, there was an error. */ |
| 141 | + FIH_SET(fih_rc, FIH_FAILURE); |
| 142 | + goto out; |
| 143 | + } |
| 144 | + |
| 145 | + FIH_SET(fih_rc, FIH_SUCCESS); |
| 146 | +out: |
| 147 | + |
| 148 | + FIH_RET(fih_rc); |
| 149 | +} |
| 150 | + |
| 151 | +/* Hash signature verification function. |
| 152 | + * Verifies hash against provided signature. |
| 153 | + * The function verifies that hash is of expected size and then |
| 154 | + * calls bootutil_verify to do the signature verification. |
| 155 | + */ |
| 156 | +fih_ret |
| 157 | +bootutil_verify_sig(uint8_t *hash, uint32_t hlen, |
| 158 | + uint8_t *sig, size_t slen, |
| 159 | + uint8_t key_id) |
| 160 | +{ |
| 161 | + FIH_DECLARE(fih_rc, FIH_FAILURE); |
| 162 | + |
| 163 | + BOOT_LOG_DBG("bootutil_verify_sig: RSA key_id %d", (int)key_id); |
| 164 | + |
| 165 | + if (hlen != IMAGE_HASH_SIZE) { |
| 166 | + BOOT_LOG_DBG("bootutil_verify_sig: expected hlen %d, got %d", |
| 167 | + IMAGE_HASH_SIZE, hlen); |
| 168 | + FIH_SET(fih_rc, FIH_FAILURE); |
| 169 | + goto out; |
| 170 | + } |
| 171 | + |
| 172 | + FIH_CALL(bootutil_verify, fih_rc, hash, IMAGE_HASH_SIZE, sig, |
| 173 | + slen, key_id); |
| 174 | + |
| 175 | +out: |
| 176 | + FIH_RET(fih_rc); |
| 177 | +} |
| 178 | + |
| 179 | +/* Image verification function. |
| 180 | + * The function directly calls bootutil_verify to verify signature |
| 181 | + * of image. |
| 182 | + */ |
| 183 | +fih_ret |
| 184 | +bootutil_verify_img(uint8_t *img, uint32_t size, |
| 185 | + uint8_t *sig, size_t slen, |
| 186 | + uint8_t key_id) |
| 187 | +{ |
| 188 | + FIH_DECLARE(fih_rc, FIH_FAILURE); |
| 189 | + |
| 190 | + BOOT_LOG_DBG("bootutil_verify_img: RSA key_id %d", (int)key_id); |
| 191 | + |
| 192 | + FIH_CALL(bootutil_verify, fih_rc, img, size, sig, |
| 193 | + slen, key_id); |
| 194 | + |
| 195 | + FIH_RET(fih_rc); |
| 196 | +} |
| 197 | + |
| 198 | +#endif /* MCUBOOT_SIGN_RSA */ |
0 commit comments