Skip to content

Commit b2c15db

Browse files
author
Eric Biggers
committed
crypto: drbg - Use new AES library API
Switch from the old AES library functions (which use struct crypto_aes_ctx) to the new ones (which use struct aes_enckey). This eliminates the unnecessary computation and caching of the decryption round keys. The new AES en/decryption functions are also much faster and use AES instructions when supported by the CPU. Note that in addition to the change in the key preparation function and the key struct type itself, the change in the type of the key struct results in aes_encrypt() (which is temporarily a type-generic macro) calling the new encryption function rather than the old one. Acked-by: Ard Biesheuvel <ardb@kernel.org> Link: https://lore.kernel.org/r/20260112192035.10427-30-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@kernel.org>
1 parent 9b95f3a commit b2c15db

4 files changed

Lines changed: 21 additions & 31 deletions

File tree

crypto/df_sp80090a.c

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,17 @@
1414
#include <crypto/df_sp80090a.h>
1515
#include <crypto/internal/drbg.h>
1616

17-
static void drbg_kcapi_symsetkey(struct crypto_aes_ctx *aesctx,
18-
const unsigned char *key,
19-
u8 keylen);
20-
static void drbg_kcapi_symsetkey(struct crypto_aes_ctx *aesctx,
21-
const unsigned char *key, u8 keylen)
22-
{
23-
aes_expandkey(aesctx, key, keylen);
24-
}
25-
26-
static void drbg_kcapi_sym(struct crypto_aes_ctx *aesctx,
27-
unsigned char *outval,
17+
static void drbg_kcapi_sym(struct aes_enckey *aeskey, unsigned char *outval,
2818
const struct drbg_string *in, u8 blocklen_bytes)
2919
{
3020
/* there is only component in *in */
3121
BUG_ON(in->len < blocklen_bytes);
32-
aes_encrypt(aesctx, outval, in->buf);
22+
aes_encrypt(aeskey, outval, in->buf);
3323
}
3424

3525
/* BCC function for CTR DRBG as defined in 10.4.3 */
3626

37-
static void drbg_ctr_bcc(struct crypto_aes_ctx *aesctx,
27+
static void drbg_ctr_bcc(struct aes_enckey *aeskey,
3828
unsigned char *out, const unsigned char *key,
3929
struct list_head *in,
4030
u8 blocklen_bytes,
@@ -47,7 +37,7 @@ static void drbg_ctr_bcc(struct crypto_aes_ctx *aesctx,
4737
drbg_string_fill(&data, out, blocklen_bytes);
4838

4939
/* 10.4.3 step 2 / 4 */
50-
drbg_kcapi_symsetkey(aesctx, key, keylen);
40+
aes_prepareenckey(aeskey, key, keylen);
5141
list_for_each_entry(curr, in, list) {
5242
const unsigned char *pos = curr->buf;
5343
size_t len = curr->len;
@@ -56,7 +46,7 @@ static void drbg_ctr_bcc(struct crypto_aes_ctx *aesctx,
5646
/* 10.4.3 step 4.2 */
5747
if (blocklen_bytes == cnt) {
5848
cnt = 0;
59-
drbg_kcapi_sym(aesctx, out, &data, blocklen_bytes);
49+
drbg_kcapi_sym(aeskey, out, &data, blocklen_bytes);
6050
}
6151
out[cnt] ^= *pos;
6252
pos++;
@@ -66,7 +56,7 @@ static void drbg_ctr_bcc(struct crypto_aes_ctx *aesctx,
6656
}
6757
/* 10.4.3 step 4.2 for last block */
6858
if (cnt)
69-
drbg_kcapi_sym(aesctx, out, &data, blocklen_bytes);
59+
drbg_kcapi_sym(aeskey, out, &data, blocklen_bytes);
7060
}
7161

7262
/*
@@ -110,7 +100,7 @@ static void drbg_ctr_bcc(struct crypto_aes_ctx *aesctx,
110100
*/
111101

112102
/* Derivation Function for CTR DRBG as defined in 10.4.2 */
113-
int crypto_drbg_ctr_df(struct crypto_aes_ctx *aesctx,
103+
int crypto_drbg_ctr_df(struct aes_enckey *aeskey,
114104
unsigned char *df_data, size_t bytes_to_return,
115105
struct list_head *seedlist,
116106
u8 blocklen_bytes,
@@ -187,7 +177,7 @@ int crypto_drbg_ctr_df(struct crypto_aes_ctx *aesctx,
187177
*/
188178
drbg_cpu_to_be32(i, iv);
189179
/* 10.4.2 step 9.2 -- BCC and concatenation with temp */
190-
drbg_ctr_bcc(aesctx, temp + templen, K, &bcc_list,
180+
drbg_ctr_bcc(aeskey, temp + templen, K, &bcc_list,
191181
blocklen_bytes, keylen);
192182
/* 10.4.2 step 9.3 */
193183
i++;
@@ -201,15 +191,15 @@ int crypto_drbg_ctr_df(struct crypto_aes_ctx *aesctx,
201191
/* 10.4.2 step 12: overwriting of outval is implemented in next step */
202192

203193
/* 10.4.2 step 13 */
204-
drbg_kcapi_symsetkey(aesctx, temp, keylen);
194+
aes_prepareenckey(aeskey, temp, keylen);
205195
while (generated_len < bytes_to_return) {
206196
short blocklen = 0;
207197
/*
208198
* 10.4.2 step 13.1: the truncation of the key length is
209199
* implicit as the key is only drbg_blocklen in size based on
210200
* the implementation of the cipher function callback
211201
*/
212-
drbg_kcapi_sym(aesctx, X, &cipherin, blocklen_bytes);
202+
drbg_kcapi_sym(aeskey, X, &cipherin, blocklen_bytes);
213203
blocklen = (blocklen_bytes <
214204
(bytes_to_return - generated_len)) ?
215205
blocklen_bytes :

crypto/drbg.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,9 +1505,9 @@ static int drbg_kcapi_hash(struct drbg_state *drbg, unsigned char *outval,
15051505
#ifdef CONFIG_CRYPTO_DRBG_CTR
15061506
static int drbg_fini_sym_kernel(struct drbg_state *drbg)
15071507
{
1508-
struct crypto_aes_ctx *aesctx = (struct crypto_aes_ctx *)drbg->priv_data;
1508+
struct aes_enckey *aeskey = drbg->priv_data;
15091509

1510-
kfree(aesctx);
1510+
kfree(aeskey);
15111511
drbg->priv_data = NULL;
15121512

15131513
if (drbg->ctr_handle)
@@ -1526,16 +1526,16 @@ static int drbg_fini_sym_kernel(struct drbg_state *drbg)
15261526

15271527
static int drbg_init_sym_kernel(struct drbg_state *drbg)
15281528
{
1529-
struct crypto_aes_ctx *aesctx;
1529+
struct aes_enckey *aeskey;
15301530
struct crypto_skcipher *sk_tfm;
15311531
struct skcipher_request *req;
15321532
unsigned int alignmask;
15331533
char ctr_name[CRYPTO_MAX_ALG_NAME];
15341534

1535-
aesctx = kzalloc(sizeof(*aesctx), GFP_KERNEL);
1536-
if (!aesctx)
1535+
aeskey = kzalloc(sizeof(*aeskey), GFP_KERNEL);
1536+
if (!aeskey)
15371537
return -ENOMEM;
1538-
drbg->priv_data = aesctx;
1538+
drbg->priv_data = aeskey;
15391539

15401540
if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)",
15411541
drbg->core->backend_cra_name) >= CRYPTO_MAX_ALG_NAME) {

drivers/crypto/xilinx/xilinx-trng.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ struct xilinx_rng {
6060
void __iomem *rng_base;
6161
struct device *dev;
6262
unsigned char *scratchpadbuf;
63-
struct crypto_aes_ctx *aesctx;
63+
struct aes_enckey *aeskey;
6464
struct mutex lock; /* Protect access to TRNG device */
6565
struct hwrng trng;
6666
};
@@ -198,7 +198,7 @@ static int xtrng_reseed_internal(struct xilinx_rng *rng)
198198
ret = xtrng_collect_random_data(rng, entropy, TRNG_SEED_LEN_BYTES, true);
199199
if (ret != TRNG_SEED_LEN_BYTES)
200200
return -EINVAL;
201-
ret = crypto_drbg_ctr_df(rng->aesctx, rng->scratchpadbuf,
201+
ret = crypto_drbg_ctr_df(rng->aeskey, rng->scratchpadbuf,
202202
TRNG_SEED_LEN_BYTES, &seedlist, AES_BLOCK_SIZE,
203203
TRNG_SEED_LEN_BYTES);
204204
if (ret)
@@ -349,8 +349,8 @@ static int xtrng_probe(struct platform_device *pdev)
349349
return PTR_ERR(rng->rng_base);
350350
}
351351

352-
rng->aesctx = devm_kzalloc(&pdev->dev, sizeof(*rng->aesctx), GFP_KERNEL);
353-
if (!rng->aesctx)
352+
rng->aeskey = devm_kzalloc(&pdev->dev, sizeof(*rng->aeskey), GFP_KERNEL);
353+
if (!rng->aeskey)
354354
return -ENOMEM;
355355

356356
sb_size = crypto_drbg_ctr_df_datalen(TRNG_SEED_LEN_BYTES, AES_BLOCK_SIZE);

include/crypto/df_sp80090a.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static inline int crypto_drbg_ctr_df_datalen(u8 statelen, u8 blocklen)
1818
statelen + blocklen; /* temp */
1919
}
2020

21-
int crypto_drbg_ctr_df(struct crypto_aes_ctx *aes,
21+
int crypto_drbg_ctr_df(struct aes_enckey *aes,
2222
unsigned char *df_data,
2323
size_t bytes_to_return,
2424
struct list_head *seedlist,

0 commit comments

Comments
 (0)