|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +#include <assert.h> |
| 7 | +#include <string.h> |
| 8 | +#include <stdint.h> |
| 9 | + |
| 10 | +#include <mcuboot_config/mcuboot_config.h> |
| 11 | +#include "bootutil/bootutil_log.h" |
| 12 | + |
| 13 | +#include <psa/crypto.h> |
| 14 | +#include <psa/crypto_types.h> |
| 15 | +#include <zephyr/sys/util.h> |
| 16 | +#if defined(CONFIG_BOOT_SIGNATURE_USING_KMU) |
| 17 | +#include <cracen_psa_kmu.h> |
| 18 | +#endif |
| 19 | + |
| 20 | +BOOT_LOG_MODULE_DECLARE(ed25519_psa); |
| 21 | + |
| 22 | +#define EDDSA_KEY_LENGTH 32 |
| 23 | +#define EDDSA_SIGNAGURE_LENGTH 64 |
| 24 | + |
| 25 | +#if defined(CONFIG_BOOT_SIGNATURE_USING_KMU) |
| 26 | +/* List of KMU stored key ids available for MCUboot */ |
| 27 | +#define PSA_KEY_INDEX_SIZE 2 |
| 28 | + |
| 29 | +#define PSA_KEY_STARTING_ID CONFIG_NCS_BOOT_SIGNATURE_KMU_BASE_SLOT |
| 30 | + |
| 31 | +#define MAKE_PSA_KMU_KEY_ID(id) PSA_KEY_HANDLE_FROM_CRACEN_KMU_SLOT(CRACEN_KMU_KEY_USAGE_SCHEME_RAW, id) |
| 32 | +static psa_key_id_t key_ids[] = { |
| 33 | + MAKE_PSA_KMU_KEY_ID(PSA_KEY_STARTING_ID), |
| 34 | + MAKE_PSA_KMU_KEY_ID(PSA_KEY_STARTING_ID + PSA_KEY_INDEX_SIZE), |
| 35 | + MAKE_PSA_KMU_KEY_ID(PSA_KEY_STARTING_ID + (2 * PSA_KEY_INDEX_SIZE)) |
| 36 | +}; |
| 37 | + |
| 38 | +#define KEY_SLOTS_COUNT CONFIG_BOOT_SIGNATURE_KMU_SLOTS |
| 39 | + |
| 40 | +#if defined(CONFIG_BOOT_KMU_KEYS_REVOCATION) |
| 41 | +#include <bootutil/key_revocation.h> |
| 42 | +#define VALIDATED_WITH_UNINITIALIZED INT32_MAX |
| 43 | +static int32_t validated_with = VALIDATED_WITH_UNINITIALIZED; |
| 44 | +#endif |
| 45 | + |
| 46 | +BUILD_ASSERT(CONFIG_BOOT_SIGNATURE_KMU_SLOTS <= ARRAY_SIZE(key_ids), |
| 47 | + "Invalid number of KMU slots, up to 3 are supported on nRF54L15"); |
| 48 | +#endif |
| 49 | + |
| 50 | +#if defined(CONFIG_NCS_BOOT_SIGNATURE_USING_ITS) |
| 51 | +static const psa_key_id_t key_ids[] = { |
| 52 | + 0x40022100, |
| 53 | + 0x40022101, |
| 54 | + 0x40022102, |
| 55 | + 0x40022103 |
| 56 | +}; |
| 57 | + |
| 58 | +#define KEY_SLOTS_COUNT ARRAY_SIZE(key_ids) |
| 59 | +#endif |
| 60 | + |
| 61 | +int ED25519_verify(const uint8_t *message, size_t message_len, |
| 62 | + const uint8_t signature[EDDSA_SIGNAGURE_LENGTH], |
| 63 | + const uint8_t public_key[EDDSA_KEY_LENGTH]) |
| 64 | +{ |
| 65 | + ARG_UNUSED(public_key); |
| 66 | + /* Set to any error */ |
| 67 | + psa_status_t status = PSA_ERROR_BAD_STATE; |
| 68 | + |
| 69 | + /* Initialize PSA Crypto */ |
| 70 | + status = psa_crypto_init(); |
| 71 | + if (status != PSA_SUCCESS) { |
| 72 | + BOOT_LOG_ERR("PSA crypto init failed %d", status); |
| 73 | + return 0; |
| 74 | + } |
| 75 | + |
| 76 | + status = PSA_ERROR_BAD_STATE; |
| 77 | + |
| 78 | + for (int i = 0; i < KEY_SLOTS_COUNT; ++i) { |
| 79 | + psa_key_id_t kid = key_ids[i]; |
| 80 | + |
| 81 | + status = psa_verify_message(kid, PSA_ALG_PURE_EDDSA, message, |
| 82 | + message_len, signature, |
| 83 | + EDDSA_SIGNAGURE_LENGTH); |
| 84 | + if (status == PSA_SUCCESS) { |
| 85 | +#if defined(CONFIG_BOOT_KMU_KEYS_REVOCATION) |
| 86 | + if(i < validated_with) { |
| 87 | + validated_with = i; |
| 88 | + } |
| 89 | +#endif |
| 90 | + return 1; |
| 91 | + } |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + BOOT_LOG_ERR("ED25519 signature verification failed %d", status); |
| 96 | + |
| 97 | + return 0; |
| 98 | +} |
| 99 | + |
| 100 | +#if defined(CONFIG_BOOT_KMU_KEYS_REVOCATION) |
| 101 | +int exec_revoke(void) |
| 102 | +{ |
| 103 | + int ret = BOOT_KEY_REVOKE_OK; |
| 104 | + psa_status_t status = psa_crypto_init(); |
| 105 | + |
| 106 | + if (validated_with == VALIDATED_WITH_UNINITIALIZED) { |
| 107 | + ret = BOOT_KEY_REVOKE_INVALID; |
| 108 | + goto out; |
| 109 | + } |
| 110 | + |
| 111 | + if (status != PSA_SUCCESS) { |
| 112 | + BOOT_LOG_ERR("PSA crypto init failed with error %d", status); |
| 113 | + ret = BOOT_KEY_REVOKE_FAILED; |
| 114 | + goto out; |
| 115 | + } |
| 116 | + for (int i = 0; i < CONFIG_BOOT_SIGNATURE_KMU_SLOTS; i++) { |
| 117 | + if ( i == validated_with) { |
| 118 | + break; |
| 119 | + } |
| 120 | + BOOT_LOG_DBG("Invalidating key ID %d", i); |
| 121 | + |
| 122 | + status = psa_destroy_key(key_ids[i]); |
| 123 | + if (status == PSA_SUCCESS) { |
| 124 | + BOOT_LOG_DBG("Success on key ID %d", i); |
| 125 | + } else { |
| 126 | + BOOT_LOG_DBG("Key invalidation failed with: %d", status); |
| 127 | + } |
| 128 | + } |
| 129 | +out: |
| 130 | + return ret; |
| 131 | +} |
| 132 | +#endif /* CONFIG_BOOT_KMU_KEYS_REVOCATION */ |
| 133 | + |
| 134 | +#if defined(CONFIG_BOOT_SIGNATURE_USING_KMU) |
| 135 | +void nrf_crypto_keys_housekeeping(void) |
| 136 | +{ |
| 137 | + psa_status_t status; |
| 138 | + |
| 139 | + /* We will continue through all keys, even if we have error while |
| 140 | + * processing any of it. Only doing BOOT_LOG_DBG, as we do not |
| 141 | + * really want to inform on failures to lock. |
| 142 | + */ |
| 143 | + for (int i = 0; i < CONFIG_BOOT_SIGNATURE_KMU_SLOTS; ++i) { |
| 144 | + psa_key_attributes_t attr; |
| 145 | + |
| 146 | + status = psa_get_key_attributes(key_ids[i], &attr); |
| 147 | + BOOT_LOG_DBG("KMU key 0x%x(%d) attr query status == %d", |
| 148 | + key_ids[i], i, status); |
| 149 | + |
| 150 | + if (status == PSA_SUCCESS) { |
| 151 | + status = cracen_kmu_block(&attr); |
| 152 | + BOOT_LOG_DBG("KMU key lock status == %d", status); |
| 153 | + } |
| 154 | + |
| 155 | + status = psa_purge_key(key_ids[i]); |
| 156 | + BOOT_LOG_DBG("KMU key 0x%x(%d) purge status == %d", |
| 157 | + key_ids[i], i, status); |
| 158 | + } |
| 159 | +} |
| 160 | +#endif |
0 commit comments