Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
main ▾

Modern-Apps.git / firmware / qorvo-uwb / src / mcps_crypto_psa.c

mcps_crypto_psa.c367 lines · 10.5 KiBRaw
1/*
2 * MCPS crypto backend on PSA Crypto.
3 *
4 * The prebuilt FiRa bundle calls out to these for all 802.15.4z frame protection:
5 * AES-CMAC key derivation, CCM* frame encryption/authentication, and AES-ECB for
6 * header IEs. The SDK's own implementation (Src/UWB/mcps_crypto.c) forwards to a
7 * HAL_crypto layer built on Nordic's old nrf_crypto, which would mean pulling a second
8 * crypto provider into the image. This talks to PSA directly, which the FindFamily
9 * firmware already uses for the epoch-id HMAC and the STS HKDF.
10 *
11 * Contexts are handed out from static pools: the stack creates a couple per session and
12 * this avoids putting the heap on the ranging path.
13 */
14#include <qerr.h>
15
16#include <zephyr/kernel.h>
17#include <zephyr/logging/log.h>
18#include <psa/crypto.h>
19
20#include <stdbool.h>
21#include <stdint.h>
22#include <string.h>
23
24LOG_MODULE_REGISTER(qorvo_crypto, CONFIG_QORVO_UWB_LOG_LEVEL);
25
26/* 802.15.4 CCM* nonce is 13 bytes. */
27#define CCM_NONCE_LEN 13
28#define AES_BLOCK_LEN 16
29
30#define MAX_CCM_CTX 4
31#define MAX_ECB_CTX 4
32
33struct key_ctx {
34 psa_key_id_t key;
35 bool used;
36 bool encrypt; /* ECB only. */
37};
38
39static struct key_ctx ccm_ctxs[MAX_CCM_CTX];
40static struct key_ctx ecb_ctxs[MAX_ECB_CTX];
41
42static struct key_ctx *alloc_ctx(struct key_ctx *pool, size_t n)
43{
44 for (size_t i = 0; i < n; i++) {
45 if (!pool[i].used) {
46 pool[i].used = true;
47 return &pool[i];
48 }
49 }
50 return NULL;
51}
52
53static void free_ctx(struct key_ctx *ctx)
54{
55 if (ctx == NULL || !ctx->used) {
56 return;
57 }
58 (void)psa_destroy_key(ctx->key);
59 ctx->key = PSA_KEY_ID_NULL;
60 ctx->used = false;
61}
62
63enum qerr mcps_crypto_init(void)
64{
65 /* Idempotent: the FindFamily firmware calls this from main() as well. */
66 psa_status_t status = psa_crypto_init();
67
68 if (status != PSA_SUCCESS) {
69 LOG_ERR("psa_crypto_init: %d", (int)status);
70 return QERR_EIO;
71 }
72 return QERR_SUCCESS;
73}
74
75void mcps_crypto_deinit(void)
76{
77 for (size_t i = 0; i < MAX_CCM_CTX; i++) {
78 free_ctx(&ccm_ctxs[i]);
79 }
80 for (size_t i = 0; i < MAX_ECB_CTX; i++) {
81 free_ctx(&ecb_ctxs[i]);
82 }
83}
84
85enum qerr mcps_crypto_reinit(void)
86{
87 return QERR_SUCCESS;
88}
89
90uint32_t mcps_crypto_get_random(void)
91{
92 uint32_t v = 0;
93
94 if (psa_generate_random((uint8_t *)&v, sizeof(v)) != PSA_SUCCESS) {
95 /* Only reachable if the RNG driver is missing, which would already have
96 * failed psa_crypto_init. Zero is not a safe nonce, so say so loudly. */
97 LOG_ERR("psa_generate_random failed");
98 }
99 return v;
100}
101
102/* ---- AES-CMAC ---------------------------------------------------------- */
103
104static enum qerr cmac_digest(const uint8_t *key, size_t key_bits, const uint8_t *data,
105 unsigned int data_len, uint8_t *out)
106{
107 psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
108 psa_key_id_t key_id = PSA_KEY_ID_NULL;
109 psa_status_t status;
110 size_t out_len = 0;
111
112 psa_set_key_usage_flags(&attr, PSA_KEY_USAGE_SIGN_MESSAGE);
113 psa_set_key_algorithm(&attr, PSA_ALG_CMAC);
114 psa_set_key_type(&attr, PSA_KEY_TYPE_AES);
115 psa_set_key_bits(&attr, key_bits);
116
117 status = psa_import_key(&attr, key, key_bits / 8, &key_id);
118 if (status != PSA_SUCCESS) {
119 LOG_ERR("cmac import_key: %d", (int)status);
120 return QERR_EIO;
121 }
122 status = psa_mac_compute(key_id, PSA_ALG_CMAC, data, data_len, out, AES_BLOCK_LEN,
123 &out_len);
124 (void)psa_destroy_key(key_id);
125 if (status != PSA_SUCCESS) {
126 LOG_ERR("psa_mac_compute: %d", (int)status);
127 return QERR_EIO;
128 }
129 return QERR_SUCCESS;
130}
131
132enum qerr mcps_crypto_cmac_aes_128_digest(const uint8_t *key, const uint8_t *data,
133 unsigned int data_len, uint8_t *out)
134{
135 return cmac_digest(key, 128, data, data_len, out);
136}
137
138enum qerr mcps_crypto_cmac_aes_256_digest(const uint8_t *key, const uint8_t *data,
139 unsigned int data_len, uint8_t *out)
140{
141 return cmac_digest(key, 256, data, data_len, out);
142}
143
144/* ---- AES-CCM* ---------------------------------------------------------- */
145
146int mcps_crypto_aead_aes_ccm_star_128_create(void **ccm_star_ctx, const uint8_t *key)
147{
148 psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
149 struct key_ctx *ctx;
150 psa_status_t status;
151
152 if (ccm_star_ctx == NULL || key == NULL) {
153 return QERR_EINVAL;
154 }
155 ctx = alloc_ctx(ccm_ctxs, MAX_CCM_CTX);
156 if (ctx == NULL) {
157 LOG_ERR("out of CCM contexts");
158 return QERR_ENOMEM;
159 }
160
161 psa_set_key_usage_flags(&attr, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
162 /* The MAC length varies per frame, so permit any tag from 4 bytes up rather than
163 * pinning one length in the key policy. */
164 psa_set_key_algorithm(&attr, PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(PSA_ALG_CCM, 4));
165 psa_set_key_type(&attr, PSA_KEY_TYPE_AES);
166 psa_set_key_bits(&attr, 128);
167
168 status = psa_import_key(&attr, key, 16, &ctx->key);
169 if (status != PSA_SUCCESS) {
170 LOG_ERR("ccm import_key: %d", (int)status);
171 ctx->used = false;
172 return QERR_EIO;
173 }
174 *ccm_star_ctx = ctx;
175 return QERR_SUCCESS;
176}
177
178void mcps_crypto_aead_aes_ccm_star_128_destroy(void *ctx)
179{
180 free_ctx((struct key_ctx *)ctx);
181}
182
183/*
184 * CCM* keeps the MAC separate from the ciphertext, whereas psa_aead_encrypt returns them
185 * concatenated, so the multipart API is used to get the tag on its own.
186 *
187 * mac_len == 0 (CCM* security level "encryption only", where CCM degenerates to plain
188 * AES-CTR) is not implemented: PSA's CCM requires a tag of at least 4 bytes. FiRa STS
189 * frames use a 64-bit MIC, so this should not be reached — and an explicit error is
190 * better than silently mis-encrypting a frame.
191 */
192static int ccm_crypt(void *ctx_, const uint8_t *nonce, const uint8_t *header,
193 unsigned int header_len, const uint8_t *in, unsigned int data_len,
194 uint8_t *out, uint8_t *mac, unsigned int mac_len, bool encrypt)
195{
196 struct key_ctx *ctx = ctx_;
197 psa_aead_operation_t op = PSA_AEAD_OPERATION_INIT;
198 psa_algorithm_t alg;
199 psa_status_t status;
200 size_t out_len = 0;
201 size_t tag_len = 0;
202
203 if (ctx == NULL || !ctx->used || nonce == NULL) {
204 return QERR_EINVAL;
205 }
206 if (mac_len == 0) {
207 LOG_ERR("CCM* with a zero-length MIC is not supported");
208 return QERR_ENOTSUP;
209 }
210
211 alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, mac_len);
212 status = encrypt ? psa_aead_encrypt_setup(&op, ctx->key, alg)
213 : psa_aead_decrypt_setup(&op, ctx->key, alg);
214 if (status != PSA_SUCCESS) {
215 goto err;
216 }
217 status = psa_aead_set_lengths(&op, header_len, data_len);
218 if (status != PSA_SUCCESS) {
219 goto err;
220 }
221 status = psa_aead_set_nonce(&op, nonce, CCM_NONCE_LEN);
222 if (status != PSA_SUCCESS) {
223 goto err;
224 }
225 if (header_len) {
226 status = psa_aead_update_ad(&op, header, header_len);
227 if (status != PSA_SUCCESS) {
228 goto err;
229 }
230 }
231 if (data_len) {
232 status = psa_aead_update(&op, in, data_len, out, data_len, &out_len);
233 if (status != PSA_SUCCESS) {
234 goto err;
235 }
236 }
237 if (encrypt) {
238 status = psa_aead_finish(&op, NULL, 0, &out_len, mac, mac_len, &tag_len);
239 } else {
240 status = psa_aead_verify(&op, NULL, 0, &out_len, mac, mac_len);
241 }
242 if (status != PSA_SUCCESS) {
243 goto err;
244 }
245 return QERR_SUCCESS;
246
247err:
248 /* A decrypt failure here is usually an authentication failure, which on the
249 * ranging path means the peer's STS key doesn't match ours. */
250 LOG_ERR("CCM* %s failed: %d", encrypt ? "encrypt" : "decrypt", (int)status);
251 (void)psa_aead_abort(&op);
252 return QERR_EIO;
253}
254
255int mcps_crypto_aead_aes_ccm_star_128_encrypt(void *ctx, const uint8_t *nonce,
256 const uint8_t *header, unsigned int header_len,
257 uint8_t *data, unsigned int data_len, uint8_t *mac,
258 unsigned int mac_len)
259{
260 /* In place: PSA permits in == out for CCM. */
261 return ccm_crypt(ctx, nonce, header, header_len, data, data_len, data, mac, mac_len,
262 true);
263}
264
265int mcps_crypto_aead_aes_ccm_star_128_encrypt_inout(void *ctx, const uint8_t *nonce,
266 const uint8_t *header,
267 unsigned int header_len, uint8_t *data,
268 unsigned int data_len, uint8_t *out,
269 uint8_t *mac, unsigned int mac_len)
270{
271 return ccm_crypt(ctx, nonce, header, header_len, data, data_len, out, mac, mac_len,
272 true);
273}
274
275int mcps_crypto_aead_aes_ccm_star_128_decrypt(void *ctx, const uint8_t *nonce,
276 const uint8_t *header, unsigned int header_len,
277 uint8_t *data, unsigned int data_len, uint8_t *mac,
278 unsigned int mac_len)
279{
280 return ccm_crypt(ctx, nonce, header, header_len, data, data_len, data, mac, mac_len,
281 false);
282}
283
284int mcps_crypto_aead_aes_ccm_star_128_decrypt_inout(void *ctx, const uint8_t *nonce,
285 const uint8_t *header,
286 unsigned int header_len, uint8_t *data,
287 unsigned int data_len, uint8_t *out,
288 uint8_t *mac, unsigned int mac_len)
289{
290 return ccm_crypt(ctx, nonce, header, header_len, data, data_len, out, mac, mac_len,
291 false);
292}
293
294/* ---- AES-ECB ----------------------------------------------------------- */
295
296static int ecb_create(void **ecb_ctx, const uint8_t *key, bool encrypt)
297{
298 psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
299 struct key_ctx *ctx;
300 psa_status_t status;
301
302 if (ecb_ctx == NULL || key == NULL) {
303 return QERR_EINVAL;
304 }
305 ctx = alloc_ctx(ecb_ctxs, MAX_ECB_CTX);
306 if (ctx == NULL) {
307 LOG_ERR("out of ECB contexts");
308 return QERR_ENOMEM;
309 }
310 ctx->encrypt = encrypt;
311
312 psa_set_key_usage_flags(&attr, encrypt ? PSA_KEY_USAGE_ENCRYPT : PSA_KEY_USAGE_DECRYPT);
313 psa_set_key_algorithm(&attr, PSA_ALG_ECB_NO_PADDING);
314 psa_set_key_type(&attr, PSA_KEY_TYPE_AES);
315 psa_set_key_bits(&attr, 128);
316
317 status = psa_import_key(&attr, key, 16, &ctx->key);
318 if (status != PSA_SUCCESS) {
319 LOG_ERR("ecb import_key: %d", (int)status);
320 ctx->used = false;
321 return QERR_EIO;
322 }
323 *ecb_ctx = ctx;
324 return QERR_SUCCESS;
325}
326
327int mcps_crypto_aes_ecb_128_create_encrypt(void **ecb_ctx, const uint8_t *key)
328{
329 return ecb_create(ecb_ctx, key, true);
330}
331
332int mcps_crypto_aes_ecb_128_create_decrypt(void **ecb_ctx, const uint8_t *key)
333{
334 return ecb_create(ecb_ctx, key, false);
335}
336
337void mcps_crypto_aes_ecb_128_destroy(void *ctx)
338{
339 free_ctx((struct key_ctx *)ctx);
340}
341
342int mcps_crypto_aes_ecb_128_encrypt_decrypt(void *ctx_, const uint8_t *data,
343 unsigned int data_len, uint8_t *out)
344{
345 struct key_ctx *ctx = ctx_;
346 psa_status_t status;
347 size_t out_len = 0;
348
349 if (ctx == NULL || !ctx->used || data == NULL || out == NULL) {
350 return QERR_EINVAL;
351 }
352 if (data_len % AES_BLOCK_LEN) {
353 return QERR_EINVAL;
354 }
355 /* ECB has no IV, so psa_cipher_* neither prepends nor consumes one and the
356 * output is the same length as the input. */
357 status = ctx->encrypt ? psa_cipher_encrypt(ctx->key, PSA_ALG_ECB_NO_PADDING, data,
358 data_len, out, data_len, &out_len)
359 : psa_cipher_decrypt(ctx->key, PSA_ALG_ECB_NO_PADDING, data,
360 data_len, out, data_len, &out_len);
361 if (status != PSA_SUCCESS) {
362 LOG_ERR("psa_cipher_%s: %d", ctx->encrypt ? "encrypt" : "decrypt",
363 (int)status);
364 return QERR_EIO;
365 }
366 return QERR_SUCCESS;
367}