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