Skip to content

Commit 65c7022

Browse files
author
Eric Biggers
committed
crypto: inside-secure - 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. This driver used crypto_aes_ctx::key_enc, but only to access the copy of the raw key that is stored at the beginning of the expanded key. To eliminate the dependency on this field, instead just access the raw key directly, which is already available in the relevant functions. Acked-by: Ard Biesheuvel <ardb@kernel.org> Link: https://lore.kernel.org/r/20260112192035.10427-31-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@kernel.org>
1 parent b2c15db commit 65c7022

2 files changed

Lines changed: 12 additions & 14 deletions

File tree

drivers/crypto/inside-secure/safexcel_cipher.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,27 +2507,25 @@ static int safexcel_aead_gcm_setkey(struct crypto_aead *ctfm, const u8 *key,
25072507
struct crypto_tfm *tfm = crypto_aead_tfm(ctfm);
25082508
struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
25092509
struct safexcel_crypto_priv *priv = ctx->base.priv;
2510-
struct crypto_aes_ctx aes;
2510+
struct aes_enckey aes;
25112511
u32 hashkey[AES_BLOCK_SIZE >> 2];
25122512
int ret, i;
25132513

2514-
ret = aes_expandkey(&aes, key, len);
2515-
if (ret) {
2516-
memzero_explicit(&aes, sizeof(aes));
2514+
ret = aes_prepareenckey(&aes, key, len);
2515+
if (ret)
25172516
return ret;
2518-
}
25192517

25202518
if (priv->flags & EIP197_TRC_CACHE && ctx->base.ctxr_dma) {
25212519
for (i = 0; i < len / sizeof(u32); i++) {
2522-
if (le32_to_cpu(ctx->key[i]) != aes.key_enc[i]) {
2520+
if (ctx->key[i] != get_unaligned((__le32 *)key + i)) {
25232521
ctx->base.needs_inv = true;
25242522
break;
25252523
}
25262524
}
25272525
}
25282526

25292527
for (i = 0; i < len / sizeof(u32); i++)
2530-
ctx->key[i] = cpu_to_le32(aes.key_enc[i]);
2528+
ctx->key[i] = get_unaligned((__le32 *)key + i);
25312529

25322530
ctx->key_len = len;
25332531

drivers/crypto/inside-secure/safexcel_hash.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct safexcel_ahash_ctx {
3030
bool fb_init_done;
3131
bool fb_do_setkey;
3232

33-
struct crypto_aes_ctx *aes;
33+
struct aes_enckey *aes;
3434
struct crypto_ahash *fback;
3535
struct crypto_shash *shpre;
3636
struct shash_desc *shdesc;
@@ -1976,7 +1976,7 @@ static int safexcel_xcbcmac_setkey(struct crypto_ahash *tfm, const u8 *key,
19761976
u32 key_tmp[3 * AES_BLOCK_SIZE / sizeof(u32)];
19771977
int ret, i;
19781978

1979-
ret = aes_expandkey(ctx->aes, key, len);
1979+
ret = aes_prepareenckey(ctx->aes, key, len);
19801980
if (ret)
19811981
return ret;
19821982

@@ -1990,9 +1990,9 @@ static int safexcel_xcbcmac_setkey(struct crypto_ahash *tfm, const u8 *key,
19901990
for (i = 0; i < 3 * AES_BLOCK_SIZE / sizeof(u32); i++)
19911991
ctx->base.ipad.word[i] = swab32(key_tmp[i]);
19921992

1993-
ret = aes_expandkey(ctx->aes,
1994-
(u8 *)key_tmp + 2 * AES_BLOCK_SIZE,
1995-
AES_MIN_KEY_SIZE);
1993+
ret = aes_prepareenckey(ctx->aes,
1994+
(u8 *)key_tmp + 2 * AES_BLOCK_SIZE,
1995+
AES_MIN_KEY_SIZE);
19961996
if (ret)
19971997
return ret;
19981998

@@ -2062,12 +2062,12 @@ static int safexcel_cmac_setkey(struct crypto_ahash *tfm, const u8 *key,
20622062
int ret, i;
20632063

20642064
/* precompute the CMAC key material */
2065-
ret = aes_expandkey(ctx->aes, key, len);
2065+
ret = aes_prepareenckey(ctx->aes, key, len);
20662066
if (ret)
20672067
return ret;
20682068

20692069
for (i = 0; i < len / sizeof(u32); i++)
2070-
ctx->base.ipad.word[i + 8] = swab32(ctx->aes->key_enc[i]);
2070+
ctx->base.ipad.word[i + 8] = get_unaligned_be32(&key[4 * i]);
20712071

20722072
/* code below borrowed from crypto/cmac.c */
20732073
/* encrypt the zero block */

0 commit comments

Comments
 (0)