|
13 | 13 | #include <psa/crypto.h> |
14 | 14 | #include <psa/crypto_types.h> |
15 | 15 | #include <zephyr/sys/util.h> |
16 | | -#if defined(CONFIG_BOOT_SIGNATURE_USING_KMU) |
17 | | -#include <cracen_psa_kmu.h> |
18 | | -#endif |
19 | 16 |
|
20 | 17 | BOOT_LOG_MODULE_REGISTER(ed25519_psa); |
21 | 18 |
|
22 | 19 | #define SHA512_DIGEST_LENGTH 64 |
23 | 20 | #define EDDSA_KEY_LENGTH 32 |
24 | 21 | #define EDDSA_SIGNAGURE_LENGTH 64 |
25 | 22 |
|
26 | | -#if defined(CONFIG_BOOT_SIGNATURE_USING_KMU) |
27 | | -/* List of KMU stored key ids available for MCUboot */ |
28 | | -#define PSA_KEY_INDEX_SIZE 2 |
29 | | - |
30 | | -#if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1 || \ |
31 | | - defined(CONFIG_NCS_BOOT_SIGNATURE_KMU_UROT_MAPPING) |
32 | | -#define PSA_KEY_STARTING_ID 226 |
33 | | -#else |
34 | | -#define PSA_KEY_STARTING_ID 242 |
35 | | -#endif |
36 | | - |
37 | | -#define MAKE_PSA_KMU_KEY_ID(id) PSA_KEY_HANDLE_FROM_CRACEN_KMU_SLOT(CRACEN_KMU_KEY_USAGE_SCHEME_RAW, id) |
38 | | -static psa_key_id_t key_ids[] = { |
39 | | - MAKE_PSA_KMU_KEY_ID(PSA_KEY_STARTING_ID), |
40 | | - MAKE_PSA_KMU_KEY_ID(PSA_KEY_STARTING_ID + PSA_KEY_INDEX_SIZE), |
41 | | - MAKE_PSA_KMU_KEY_ID(PSA_KEY_STARTING_ID + (2 * PSA_KEY_INDEX_SIZE)) |
42 | | -}; |
43 | | - |
44 | | -#define KEY_SLOTS_COUNT CONFIG_BOOT_SIGNATURE_KMU_SLOTS |
45 | | - |
46 | | -#if defined(CONFIG_BOOT_KMU_KEYS_REVOCATION) |
47 | | -#include <bootutil/key_revocation.h> |
48 | | -#define VALIDATED_WITH_UNINITIALIZED INT32_MAX |
49 | | -static int32_t validated_with = VALIDATED_WITH_UNINITIALIZED; |
50 | | -#endif |
51 | | - |
52 | | -BUILD_ASSERT(CONFIG_BOOT_SIGNATURE_KMU_SLOTS <= ARRAY_SIZE(key_ids), |
53 | | - "Invalid number of KMU slots, up to 3 are supported on nRF54L15"); |
54 | | -#endif |
55 | | - |
56 | | -#if defined(CONFIG_NCS_BOOT_SIGNATURE_USING_ITS) |
57 | | -static const psa_key_id_t key_ids[] = { |
58 | | - 0x40022100, |
59 | | - 0x40022101, |
60 | | - 0x40022102, |
61 | | - 0x40022103 |
62 | | -}; |
63 | | - |
64 | | -#define KEY_SLOTS_COUNT ARRAY_SIZE(key_ids) |
65 | | -#endif |
66 | | - |
67 | | -#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU) && !defined(CONFIG_NCS_BOOT_SIGNATURE_USING_ITS) |
68 | 23 | int ED25519_verify(const uint8_t *message, size_t message_len, |
69 | 24 | const uint8_t signature[EDDSA_SIGNAGURE_LENGTH], |
70 | 25 | const uint8_t public_key[EDDSA_KEY_LENGTH]) |
@@ -117,105 +72,3 @@ int ED25519_verify(const uint8_t *message, size_t message_len, |
117 | 72 |
|
118 | 73 | return ret; |
119 | 74 | } |
120 | | -#else |
121 | | -int ED25519_verify(const uint8_t *message, size_t message_len, |
122 | | - const uint8_t signature[EDDSA_SIGNAGURE_LENGTH], |
123 | | - const uint8_t public_key[EDDSA_KEY_LENGTH]) |
124 | | -{ |
125 | | - ARG_UNUSED(public_key); |
126 | | - /* Set to any error */ |
127 | | - psa_status_t status = PSA_ERROR_BAD_STATE; |
128 | | - |
129 | | - /* Initialize PSA Crypto */ |
130 | | - status = psa_crypto_init(); |
131 | | - if (status != PSA_SUCCESS) { |
132 | | - BOOT_LOG_ERR("PSA crypto init failed %d", status); |
133 | | - return 0; |
134 | | - } |
135 | | - |
136 | | - status = PSA_ERROR_BAD_STATE; |
137 | | - |
138 | | - for (int i = 0; i < KEY_SLOTS_COUNT; ++i) { |
139 | | - psa_key_id_t kid = key_ids[i]; |
140 | | - |
141 | | - status = psa_verify_message(kid, PSA_ALG_PURE_EDDSA, message, |
142 | | - message_len, signature, |
143 | | - EDDSA_SIGNAGURE_LENGTH); |
144 | | - if (status == PSA_SUCCESS) { |
145 | | -#if defined(CONFIG_BOOT_KMU_KEYS_REVOCATION) |
146 | | - if(i < validated_with) { |
147 | | - validated_with = i; |
148 | | - } |
149 | | -#endif |
150 | | - return 1; |
151 | | - } |
152 | | - |
153 | | - } |
154 | | - |
155 | | - BOOT_LOG_ERR("ED25519 signature verification failed %d", status); |
156 | | - |
157 | | - return 0; |
158 | | -} |
159 | | -#if defined(CONFIG_BOOT_KMU_KEYS_REVOCATION) |
160 | | -int exec_revoke(void) |
161 | | -{ |
162 | | - int ret = BOOT_KEY_REVOKE_OK; |
163 | | - psa_status_t status = psa_crypto_init(); |
164 | | - |
165 | | - if (validated_with == VALIDATED_WITH_UNINITIALIZED) { |
166 | | - ret = BOOT_KEY_REVOKE_INVALID; |
167 | | - goto out; |
168 | | - } |
169 | | - |
170 | | - if (status != PSA_SUCCESS) { |
171 | | - BOOT_LOG_ERR("PSA crypto init failed with error %d", status); |
172 | | - ret = BOOT_KEY_REVOKE_FAILED; |
173 | | - goto out; |
174 | | - } |
175 | | - for (int i = 0; i < CONFIG_BOOT_SIGNATURE_KMU_SLOTS; i++) { |
176 | | - if ( i == validated_with) { |
177 | | - break; |
178 | | - } |
179 | | - BOOT_LOG_DBG("Invalidating key ID %d", i); |
180 | | - |
181 | | - status = psa_destroy_key(key_ids[i]); |
182 | | - if (status == PSA_SUCCESS) { |
183 | | - BOOT_LOG_DBG("Success on key ID %d", i); |
184 | | - } else { |
185 | | - BOOT_LOG_DBG("Key invalidation failed with: %d", status); |
186 | | - } |
187 | | - } |
188 | | -out: |
189 | | - return ret; |
190 | | -} |
191 | | -#endif /* CONFIG_BOOT_KMU_KEYS_REVOCATION */ |
192 | | - |
193 | | -#if defined(CONFIG_BOOT_SIGNATURE_USING_KMU) |
194 | | -void nrf_crypto_keys_housekeeping(void) |
195 | | -{ |
196 | | - psa_status_t status; |
197 | | - |
198 | | - /* We will continue through all keys, even if we have error while |
199 | | - * processing any of it. Only doing BOOT_LOG_DBG, as we do not |
200 | | - * really want to inform on failures to lock. |
201 | | - */ |
202 | | - for (int i = 0; i < CONFIG_BOOT_SIGNATURE_KMU_SLOTS; ++i) { |
203 | | - psa_key_attributes_t attr; |
204 | | - |
205 | | - status = psa_get_key_attributes(key_ids[i], &attr); |
206 | | - BOOT_LOG_DBG("KMU key 0x%x(%d) attr query status == %d", |
207 | | - key_ids[i], i, status); |
208 | | - |
209 | | - if (status == PSA_SUCCESS) { |
210 | | - status = cracen_kmu_block(&attr); |
211 | | - BOOT_LOG_DBG("KMU key lock status == %d", status); |
212 | | - } |
213 | | - |
214 | | - status = psa_purge_key(key_ids[i]); |
215 | | - BOOT_LOG_DBG("KMU key 0x%x(%d) purge status == %d", |
216 | | - key_ids[i], i, status); |
217 | | - } |
218 | | -} |
219 | | -#endif |
220 | | - |
221 | | -#endif |
0 commit comments