Skip to content

Commit e925518

Browse files
committed
bootutil: Allow using psa_key_id_t in AES crypto context
Store psa_key_id_t key in AES context instead of RAW key. Signed-off-by: Dominik Ermel <[email protected]>
1 parent 5b6f045 commit e925518

File tree

2 files changed

+42
-66
lines changed

2 files changed

+42
-66
lines changed

boot/bootutil/include/bootutil/crypto/aes_ctr_psa.h

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,18 @@
1616
#include "bootutil/enc_key_public.h"
1717
#include <psa/crypto.h>
1818

19-
#define BOOT_ENC_BLOCK_SIZE (16)
20-
2119
#ifdef __cplusplus
2220
extern "C" {
2321
#endif
2422

2523
typedef struct {
26-
/* Fixme: This should not be, here, psa_key_id should be passed */
27-
uint8_t key[BOOT_ENC_KEY_SIZE];
24+
psa_key_id_t key;
2825
} bootutil_aes_ctr_context;
2926

3027
void bootutil_aes_ctr_init(bootutil_aes_ctr_context *ctx);
3128

32-
static inline void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx)
33-
{
34-
memset(ctx, 0, sizeof(*ctx));
35-
}
36-
37-
static inline int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k)
38-
{
39-
memcpy(ctx->key, k, sizeof(ctx->key));
40-
41-
return 0;
42-
}
29+
void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx);
30+
int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k);
4331

4432
int bootutil_aes_ctr_encrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter,
4533
const uint8_t *m, uint32_t mlen, size_t blk_off, uint8_t *c);

boot/bootutil/src/encrypted_psa.c

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,45 @@ void bootutil_aes_ctr_init(bootutil_aes_ctr_context *ctx)
190190
{
191191
psa_status_t psa_ret = psa_crypto_init();
192192

193-
(void)ctx;
194-
195193
if (psa_ret != PSA_SUCCESS) {
196194
BOOT_LOG_ERR("AES init PSA crypto init failed %d", psa_ret);
197195
assert(0);
198196
}
197+
198+
ctx->key = PSA_KEY_ID_NULL;
199+
}
200+
201+
void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx)
202+
{
203+
psa_status_t psa_ret = psa_destroy_key(ctx->key);
204+
205+
if (psa_ret != PSA_SUCCESS) {
206+
BOOT_LOG_WRN("aes_ctr_drop: destruction failed %d", psa_ret);
207+
/* This should never happen. If we fail to destroy key this happens
208+
* either because it is invalid key number or something is really
209+
* wrong; either way we have no way to recover.
210+
*/
211+
assert(0);
212+
}
213+
214+
ctx->key = PSA_KEY_ID_NULL;
215+
}
216+
217+
int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k)
218+
{
219+
psa_status_t psa_ret = PSA_ERROR_BAD_STATE;
220+
psa_key_attributes_t kattr = PSA_KEY_ATTRIBUTES_INIT;
221+
222+
psa_set_key_type(&kattr, PSA_KEY_TYPE_AES);
223+
psa_set_key_usage_flags(&kattr, PSA_KEY_USAGE_DECRYPT | PSA_KEY_USAGE_ENCRYPT);
224+
psa_set_key_algorithm(&kattr, PSA_ALG_CTR);
225+
226+
psa_ret = psa_import_key(&kattr, k, HKDF_AES_KEY_SIZE, &ctx->key);
227+
if (psa_ret != PSA_SUCCESS) {
228+
BOOT_LOG_ERR("aes_ctr_set_key; import failed %d", psa_ret);
229+
return -1;
230+
}
231+
return 0;
199232
}
200233

201234
#if defined(MCUBOOT_ENC_IMAGES)
@@ -394,8 +427,7 @@ int bootutil_aes_ctr_encrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter,
394427
{
395428
int ret = 0;
396429
psa_status_t psa_ret = PSA_ERROR_BAD_STATE;
397-
psa_key_attributes_t kattr = PSA_KEY_ATTRIBUTES_INIT;
398-
psa_key_id_t kid;
430+
const psa_key_id_t kid = ctx->key;
399431
psa_cipher_operation_t psa_op;
400432
size_t elen = 0; /* Decrypted length */
401433

@@ -411,21 +443,6 @@ int bootutil_aes_ctr_encrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter,
411443

412444
psa_op = psa_cipher_operation_init();
413445

414-
/* Fixme: Import should happen when key is decrypted, but due to lack
415-
* of key destruction there is no way to destroy key stored by
416-
* psa other way than here. */
417-
psa_set_key_type(&kattr, PSA_KEY_TYPE_AES);
418-
psa_set_key_usage_flags(&kattr, PSA_KEY_USAGE_ENCRYPT);
419-
psa_set_key_algorithm(&kattr, PSA_ALG_CTR);
420-
421-
psa_ret = psa_import_key(&kattr, ctx->key, BOOT_ENC_KEY_SIZE, &kid);
422-
psa_reset_key_attributes(&kattr);
423-
if (psa_ret != PSA_SUCCESS) {
424-
BOOT_LOG_ERR("AES enc import key failed %d", psa_ret);
425-
ret = -1;
426-
goto gone;
427-
}
428-
429446
/* This could be done with psa_cipher_decrypt one-shot operation, but
430447
* multi-part operation is used to avoid re-allocating input buffer
431448
* to account for IV in front of data.
@@ -434,7 +451,7 @@ int bootutil_aes_ctr_encrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter,
434451
if (psa_ret != PSA_SUCCESS) {
435452
BOOT_LOG_ERR("AES enc setup failed %d", psa_ret);
436453
ret = -1;
437-
goto gone_with_key;
454+
goto gone;
438455
}
439456

440457
/* Fixme: hardcoded counter size, but it is hardcoded everywhere */
@@ -458,13 +475,6 @@ int bootutil_aes_ctr_encrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter,
458475
BOOT_LOG_WRN("AES enc cipher abort failed %d", psa_ret);
459476
/* Intentionally not changing the ret */
460477
}
461-
gone_with_key:
462-
/* Fixme: Should be removed once key is shared by id */
463-
psa_ret = psa_destroy_key(kid);
464-
if (psa_ret != PSA_SUCCESS) {
465-
BOOT_LOG_WRN("AES enc destroy key failed %d", psa_ret);
466-
/* Intentionally not changing the ret */
467-
}
468478
gone:
469479
return ret;
470480
}
@@ -474,8 +484,7 @@ int bootutil_aes_ctr_decrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter,
474484
{
475485
int ret = 0;
476486
psa_status_t psa_ret = PSA_ERROR_BAD_STATE;
477-
psa_key_attributes_t kattr = PSA_KEY_ATTRIBUTES_INIT;
478-
psa_key_id_t kid;
487+
const psa_key_id_t kid = ctx->key;
479488
psa_cipher_operation_t psa_op;
480489
size_t dlen = 0; /* Decrypted length */
481490

@@ -491,21 +500,6 @@ int bootutil_aes_ctr_decrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter,
491500

492501
psa_op = psa_cipher_operation_init();
493502

494-
/* Fixme: Import should happen when key is decrypted, but due to lack
495-
* of key destruction there is no way to destroy key stored by
496-
* psa other way than here. */
497-
psa_set_key_type(&kattr, PSA_KEY_TYPE_AES);
498-
psa_set_key_usage_flags(&kattr, PSA_KEY_USAGE_DECRYPT);
499-
psa_set_key_algorithm(&kattr, PSA_ALG_CTR);
500-
501-
psa_ret = psa_import_key(&kattr, ctx->key, BOOT_ENC_KEY_SIZE, &kid);
502-
psa_reset_key_attributes(&kattr);
503-
if (psa_ret != PSA_SUCCESS) {
504-
BOOT_LOG_ERR("AES dec import key failed %d", psa_ret);
505-
ret = -1;
506-
goto gone;
507-
}
508-
509503
/* This could be done with psa_cipher_decrypt one-shot operation, but
510504
* multi-part operation is used to avoid re-allocating input buffer
511505
* to account for IV in front of data.
@@ -514,7 +508,7 @@ int bootutil_aes_ctr_decrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter,
514508
if (psa_ret != PSA_SUCCESS) {
515509
BOOT_LOG_ERR("AES dec setup failed %d", psa_ret);
516510
ret = -1;
517-
goto gone_with_key;
511+
goto gone;
518512
}
519513

520514
/* Fixme: hardcoded counter size, but it is hardcoded everywhere */
@@ -538,12 +532,6 @@ int bootutil_aes_ctr_decrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter,
538532
BOOT_LOG_WRN("PSA dec abort failed %d", psa_ret);
539533
/* Intentionally not changing the ret */
540534
}
541-
gone_with_key:
542-
psa_ret = psa_destroy_key(kid);
543-
if (psa_ret != PSA_SUCCESS) {
544-
BOOT_LOG_WRN("PSA dec key failed %d", psa_ret);
545-
/* Intentionally not changing the ret */
546-
}
547535
gone:
548536
return ret;
549537
}

0 commit comments

Comments
 (0)