This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
commit 13ec1c27bd130d91c18f6ed8da41765a5a87fc1c Author: makejian <[email protected]> AuthorDate: Wed Nov 25 14:54:21 2026 +0800 crypto/mbedtls: support AES sessions keyed by key id Add mbedtls_aes_set{128,192,256}key_{enc,dec}_keyid(): like the plain setkey functions, but the buffer the caller hands over carries a 32-bit key id (padded to the full key size) and the session is created with SOP_F_KEYID, so the crypto backend loads the key from its key store instead of taking the material inline. The key must already live in a store the backend can reach, e.g. one populated through CIOCKEY CRK_ALLOCATE_KEY/CRK_IMPORT_KEY/CRK_SAVE_KEY. The regular setkey path clears SOP_F_KEYID, so a context that was once keyed by id can be reused with plain key material. Verified on sim:crypto + software cryptodev with the swkey key store (/dev/nvs): a key imported and saved through CIOCKEY, then used via mbedtls_aes_set128key_enc_keyid() + mbedtls_aes_crypt_cbc(), produces the same ciphertext as the direct cryptodev reference call with the real key material. Signed-off-by: makejian <[email protected]> --- crypto/mbedtls/include/aes_alt.h | 19 ++++++++++ crypto/mbedtls/source/aes_alt.c | 78 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/crypto/mbedtls/include/aes_alt.h b/crypto/mbedtls/include/aes_alt.h index 0ba13cc41..602b5ea05 100644 --- a/crypto/mbedtls/include/aes_alt.h +++ b/crypto/mbedtls/include/aes_alt.h @@ -43,4 +43,23 @@ mbedtls_aes_context; #define mbedtls_aes_xts_context mbedtls_aes_context +int mbedtls_aes_set128key_enc_keyid(FAR mbedtls_aes_context *ctx, + FAR const unsigned char *key, + unsigned int keybits); +int mbedtls_aes_set192key_enc_keyid(FAR mbedtls_aes_context *ctx, + FAR const unsigned char *key, + unsigned int keybits); +int mbedtls_aes_set256key_enc_keyid(FAR mbedtls_aes_context *ctx, + FAR const unsigned char *key, + unsigned int keybits); +int mbedtls_aes_set128key_dec_keyid(FAR mbedtls_aes_context *ctx, + FAR const unsigned char *key, + unsigned int keybits); +int mbedtls_aes_set192key_dec_keyid(FAR mbedtls_aes_context *ctx, + FAR const unsigned char *key, + unsigned int keybits); +int mbedtls_aes_set256key_dec_keyid(FAR mbedtls_aes_context *ctx, + FAR const unsigned char *key, + unsigned int keybits); + #endif /* __APPS_CRYPTO_MBEDTLS_INCLUDE_AES_ALT_H */ diff --git a/crypto/mbedtls/source/aes_alt.c b/crypto/mbedtls/source/aes_alt.c index 4be6e44da..c025a94b0 100644 --- a/crypto/mbedtls/source/aes_alt.c +++ b/crypto/mbedtls/source/aes_alt.c @@ -35,6 +35,41 @@ #define ECB_BLOCK_SIZE 16 #define NONCE_LENGTH 4 +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/* Set the session key by key id instead of key material: the key + * buffer carries the 32-bit key id in its first bytes, padded to the + * full key size (the kernel reads the id from the first four bytes; + * the size only decides which AES variant the session uses). The + * key itself must already live in a key store the backend can reach, + * e.g. one allocated through CIOCKEY CRK_ALLOCATE_KEY. + */ + +static int aes_setkey_keyid(FAR mbedtls_aes_context *ctx, + FAR const unsigned char *key, + unsigned int keybits) +{ + switch (keybits) + { + case 128: + break; + case 192: + break; + case 256: + break; + default: + return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH; + } + + memcpy(ctx->key, key, keybits / 8); + ctx->dev.session.key = (caddr_t)ctx->key; + ctx->dev.session.keylen = keybits / 8; + ctx->dev.session.flags |= SOP_F_KEYID; + return 0; +} + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -68,6 +103,7 @@ int mbedtls_aes_setkey_enc(FAR mbedtls_aes_context *ctx, memcpy(ctx->key, key, keybits / 8); ctx->dev.session.key = (caddr_t)ctx->key; ctx->dev.session.keylen = keybits / 8; + ctx->dev.session.flags &= ~SOP_F_KEYID; return 0; } @@ -78,6 +114,48 @@ int mbedtls_aes_setkey_dec(FAR mbedtls_aes_context *ctx, return mbedtls_aes_setkey_enc(ctx, key, keybits); } +int mbedtls_aes_set128key_enc_keyid(FAR mbedtls_aes_context *ctx, + FAR const unsigned char *key, + unsigned int keybits) +{ + return aes_setkey_keyid(ctx, key, keybits); +} + +int mbedtls_aes_set192key_enc_keyid(FAR mbedtls_aes_context *ctx, + FAR const unsigned char *key, + unsigned int keybits) +{ + return aes_setkey_keyid(ctx, key, keybits); +} + +int mbedtls_aes_set256key_enc_keyid(FAR mbedtls_aes_context *ctx, + FAR const unsigned char *key, + unsigned int keybits) +{ + return aes_setkey_keyid(ctx, key, keybits); +} + +int mbedtls_aes_set128key_dec_keyid(FAR mbedtls_aes_context *ctx, + FAR const unsigned char *key, + unsigned int keybits) +{ + return aes_setkey_keyid(ctx, key, keybits); +} + +int mbedtls_aes_set192key_dec_keyid(FAR mbedtls_aes_context *ctx, + FAR const unsigned char *key, + unsigned int keybits) +{ + return aes_setkey_keyid(ctx, key, keybits); +} + +int mbedtls_aes_set256key_dec_keyid(FAR mbedtls_aes_context *ctx, + FAR const unsigned char *key, + unsigned int keybits) +{ + return aes_setkey_keyid(ctx, key, keybits); +} + /* AES-ECB block encryption/decryption */ int mbedtls_aes_crypt_ecb(FAR mbedtls_aes_context *ctx,
