Skip to content

Commit 4568675

Browse files
committed
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 <makejian@xiaomi.com>
1 parent a8e07e0 commit 4568675

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

‎crypto/mbedtls/include/aes_alt.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,23 @@ mbedtls_aes_context;
4343

4444
#define mbedtls_aes_xts_context mbedtls_aes_context
4545

46+
int mbedtls_aes_set128key_enc_keyid(FAR mbedtls_aes_context *ctx,
47+
FAR const unsigned char *key,
48+
unsigned int keybits);
49+
int mbedtls_aes_set192key_enc_keyid(FAR mbedtls_aes_context *ctx,
50+
FAR const unsigned char *key,
51+
unsigned int keybits);
52+
int mbedtls_aes_set256key_enc_keyid(FAR mbedtls_aes_context *ctx,
53+
FAR const unsigned char *key,
54+
unsigned int keybits);
55+
int mbedtls_aes_set128key_dec_keyid(FAR mbedtls_aes_context *ctx,
56+
FAR const unsigned char *key,
57+
unsigned int keybits);
58+
int mbedtls_aes_set192key_dec_keyid(FAR mbedtls_aes_context *ctx,
59+
FAR const unsigned char *key,
60+
unsigned int keybits);
61+
int mbedtls_aes_set256key_dec_keyid(FAR mbedtls_aes_context *ctx,
62+
FAR const unsigned char *key,
63+
unsigned int keybits);
64+
4665
#endif /* __APPS_CRYPTO_MBEDTLS_INCLUDE_AES_ALT_H */

‎crypto/mbedtls/source/aes_alt.c‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,41 @@
3535
#define ECB_BLOCK_SIZE 16
3636
#define NONCE_LENGTH 4
3737

38+
/****************************************************************************
39+
* Private Functions
40+
****************************************************************************/
41+
42+
/* Set the session key by key id instead of key material: the key
43+
* buffer carries the 32-bit key id in its first bytes, padded to the
44+
* full key size (the kernel reads the id from the first four bytes;
45+
* the size only decides which AES variant the session uses). The
46+
* key itself must already live in a key store the backend can reach,
47+
* e.g. one allocated through CIOCKEY CRK_ALLOCATE_KEY.
48+
*/
49+
50+
static int aes_setkey_keyid(FAR mbedtls_aes_context *ctx,
51+
FAR const unsigned char *key,
52+
unsigned int keybits)
53+
{
54+
switch (keybits)
55+
{
56+
case 128:
57+
break;
58+
case 192:
59+
break;
60+
case 256:
61+
break;
62+
default:
63+
return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
64+
}
65+
66+
memcpy(ctx->key, key, keybits / 8);
67+
ctx->dev.session.key = (caddr_t)ctx->key;
68+
ctx->dev.session.keylen = keybits / 8;
69+
ctx->dev.session.flags |= SOP_F_KEYID;
70+
return 0;
71+
}
72+
3873
/****************************************************************************
3974
* Public Functions
4075
****************************************************************************/
@@ -68,6 +103,7 @@ int mbedtls_aes_setkey_enc(FAR mbedtls_aes_context *ctx,
68103
memcpy(ctx->key, key, keybits / 8);
69104
ctx->dev.session.key = (caddr_t)ctx->key;
70105
ctx->dev.session.keylen = keybits / 8;
106+
ctx->dev.session.flags &= ~SOP_F_KEYID;
71107
return 0;
72108
}
73109

@@ -78,6 +114,48 @@ int mbedtls_aes_setkey_dec(FAR mbedtls_aes_context *ctx,
78114
return mbedtls_aes_setkey_enc(ctx, key, keybits);
79115
}
80116

117+
int mbedtls_aes_set128key_enc_keyid(FAR mbedtls_aes_context *ctx,
118+
FAR const unsigned char *key,
119+
unsigned int keybits)
120+
{
121+
return aes_setkey_keyid(ctx, key, keybits);
122+
}
123+
124+
int mbedtls_aes_set192key_enc_keyid(FAR mbedtls_aes_context *ctx,
125+
FAR const unsigned char *key,
126+
unsigned int keybits)
127+
{
128+
return aes_setkey_keyid(ctx, key, keybits);
129+
}
130+
131+
int mbedtls_aes_set256key_enc_keyid(FAR mbedtls_aes_context *ctx,
132+
FAR const unsigned char *key,
133+
unsigned int keybits)
134+
{
135+
return aes_setkey_keyid(ctx, key, keybits);
136+
}
137+
138+
int mbedtls_aes_set128key_dec_keyid(FAR mbedtls_aes_context *ctx,
139+
FAR const unsigned char *key,
140+
unsigned int keybits)
141+
{
142+
return aes_setkey_keyid(ctx, key, keybits);
143+
}
144+
145+
int mbedtls_aes_set192key_dec_keyid(FAR mbedtls_aes_context *ctx,
146+
FAR const unsigned char *key,
147+
unsigned int keybits)
148+
{
149+
return aes_setkey_keyid(ctx, key, keybits);
150+
}
151+
152+
int mbedtls_aes_set256key_dec_keyid(FAR mbedtls_aes_context *ctx,
153+
FAR const unsigned char *key,
154+
unsigned int keybits)
155+
{
156+
return aes_setkey_keyid(ctx, key, keybits);
157+
}
158+
81159
/* AES-ECB block encryption/decryption */
82160

83161
int mbedtls_aes_crypt_ecb(FAR mbedtls_aes_context *ctx,

0 commit comments

Comments
 (0)