Skip to content

Commit 7fcb22d

Browse files
author
Eric Biggers
committed
lib/crypto: aescfb: 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-33-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@kernel.org>
1 parent 8612043 commit 7fcb22d

3 files changed

Lines changed: 22 additions & 22 deletions

File tree

drivers/char/tpm/tpm2-sessions.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ struct tpm2_auth {
126126
u8 session_key[SHA256_DIGEST_SIZE];
127127
u8 passphrase[SHA256_DIGEST_SIZE];
128128
int passphrase_len;
129-
struct crypto_aes_ctx aes_ctx;
129+
struct aes_enckey aes_key;
130130
/* saved session attributes: */
131131
u8 attrs;
132132
__be32 ordinal;
@@ -677,8 +677,8 @@ int tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf)
677677
auth->scratch);
678678

679679
len = tpm_buf_read_u16(buf, &offset_p);
680-
aes_expandkey(&auth->aes_ctx, auth->scratch, AES_KEY_BYTES);
681-
aescfb_encrypt(&auth->aes_ctx, &buf->data[offset_p],
680+
aes_prepareenckey(&auth->aes_key, auth->scratch, AES_KEY_BYTES);
681+
aescfb_encrypt(&auth->aes_key, &buf->data[offset_p],
682682
&buf->data[offset_p], len,
683683
auth->scratch + AES_KEY_BYTES);
684684
/* reset p to beginning of parameters for HMAC */
@@ -858,8 +858,8 @@ int tpm_buf_check_hmac_response(struct tpm_chip *chip, struct tpm_buf *buf,
858858
auth->scratch);
859859

860860
len = tpm_buf_read_u16(buf, &offset_p);
861-
aes_expandkey(&auth->aes_ctx, auth->scratch, AES_KEY_BYTES);
862-
aescfb_decrypt(&auth->aes_ctx, &buf->data[offset_p],
861+
aes_prepareenckey(&auth->aes_key, auth->scratch, AES_KEY_BYTES);
862+
aescfb_decrypt(&auth->aes_key, &buf->data[offset_p],
863863
&buf->data[offset_p], len,
864864
auth->scratch + AES_KEY_BYTES);
865865
}

include/crypto/aes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,9 @@ extern const u8 crypto_aes_inv_sbox[];
343343
extern const u32 aes_enc_tab[256];
344344
extern const u32 aes_dec_tab[256];
345345

346-
void aescfb_encrypt(const struct crypto_aes_ctx *ctx, u8 *dst, const u8 *src,
346+
void aescfb_encrypt(const struct aes_enckey *key, u8 *dst, const u8 *src,
347347
int len, const u8 iv[AES_BLOCK_SIZE]);
348-
void aescfb_decrypt(const struct crypto_aes_ctx *ctx, u8 *dst, const u8 *src,
348+
void aescfb_decrypt(const struct aes_enckey *key, u8 *dst, const u8 *src,
349349
int len, const u8 iv[AES_BLOCK_SIZE]);
350350

351351
#endif

lib/crypto/aescfb.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <linux/module.h>
1212
#include <asm/irqflags.h>
1313

14-
static void aescfb_encrypt_block(const struct crypto_aes_ctx *ctx, void *dst,
14+
static void aescfb_encrypt_block(const struct aes_enckey *key, void *dst,
1515
const void *src)
1616
{
1717
unsigned long flags;
@@ -25,27 +25,27 @@ static void aescfb_encrypt_block(const struct crypto_aes_ctx *ctx, void *dst,
2525
* interrupts disabled.
2626
*/
2727
local_irq_save(flags);
28-
aes_encrypt(ctx, dst, src);
28+
aes_encrypt(key, dst, src);
2929
local_irq_restore(flags);
3030
}
3131

3232
/**
3333
* aescfb_encrypt - Perform AES-CFB encryption on a block of data
3434
*
35-
* @ctx: The AES-CFB key schedule
35+
* @key: The AES-CFB key schedule
3636
* @dst: Pointer to the ciphertext output buffer
3737
* @src: Pointer the plaintext (may equal @dst for encryption in place)
3838
* @len: The size in bytes of the plaintext and ciphertext.
3939
* @iv: The initialization vector (IV) to use for this block of data
4040
*/
41-
void aescfb_encrypt(const struct crypto_aes_ctx *ctx, u8 *dst, const u8 *src,
41+
void aescfb_encrypt(const struct aes_enckey *key, u8 *dst, const u8 *src,
4242
int len, const u8 iv[AES_BLOCK_SIZE])
4343
{
4444
u8 ks[AES_BLOCK_SIZE];
4545
const u8 *v = iv;
4646

4747
while (len > 0) {
48-
aescfb_encrypt_block(ctx, ks, v);
48+
aescfb_encrypt_block(key, ks, v);
4949
crypto_xor_cpy(dst, src, ks, min(len, AES_BLOCK_SIZE));
5050
v = dst;
5151

@@ -61,18 +61,18 @@ EXPORT_SYMBOL(aescfb_encrypt);
6161
/**
6262
* aescfb_decrypt - Perform AES-CFB decryption on a block of data
6363
*
64-
* @ctx: The AES-CFB key schedule
64+
* @key: The AES-CFB key schedule
6565
* @dst: Pointer to the plaintext output buffer
6666
* @src: Pointer the ciphertext (may equal @dst for decryption in place)
6767
* @len: The size in bytes of the plaintext and ciphertext.
6868
* @iv: The initialization vector (IV) to use for this block of data
6969
*/
70-
void aescfb_decrypt(const struct crypto_aes_ctx *ctx, u8 *dst, const u8 *src,
70+
void aescfb_decrypt(const struct aes_enckey *key, u8 *dst, const u8 *src,
7171
int len, const u8 iv[AES_BLOCK_SIZE])
7272
{
7373
u8 ks[2][AES_BLOCK_SIZE];
7474

75-
aescfb_encrypt_block(ctx, ks[0], iv);
75+
aescfb_encrypt_block(key, ks[0], iv);
7676

7777
for (int i = 0; len > 0; i ^= 1) {
7878
if (len > AES_BLOCK_SIZE)
@@ -81,7 +81,7 @@ void aescfb_decrypt(const struct crypto_aes_ctx *ctx, u8 *dst, const u8 *src,
8181
* performing the XOR, as that may update in place and
8282
* overwrite the ciphertext.
8383
*/
84-
aescfb_encrypt_block(ctx, ks[!i], src);
84+
aescfb_encrypt_block(key, ks[!i], src);
8585

8686
crypto_xor_cpy(dst, src, ks[i], min(len, AES_BLOCK_SIZE));
8787

@@ -214,30 +214,30 @@ static struct {
214214
static int __init libaescfb_init(void)
215215
{
216216
for (int i = 0; i < ARRAY_SIZE(aescfb_tv); i++) {
217-
struct crypto_aes_ctx ctx;
217+
struct aes_enckey key;
218218
u8 buf[64];
219219

220-
if (aes_expandkey(&ctx, aescfb_tv[i].key, aescfb_tv[i].klen)) {
221-
pr_err("aes_expandkey() failed on vector %d\n", i);
220+
if (aes_prepareenckey(&key, aescfb_tv[i].key, aescfb_tv[i].klen)) {
221+
pr_err("aes_prepareenckey() failed on vector %d\n", i);
222222
return -ENODEV;
223223
}
224224

225-
aescfb_encrypt(&ctx, buf, aescfb_tv[i].ptext, aescfb_tv[i].len,
225+
aescfb_encrypt(&key, buf, aescfb_tv[i].ptext, aescfb_tv[i].len,
226226
aescfb_tv[i].iv);
227227
if (memcmp(buf, aescfb_tv[i].ctext, aescfb_tv[i].len)) {
228228
pr_err("aescfb_encrypt() #1 failed on vector %d\n", i);
229229
return -ENODEV;
230230
}
231231

232232
/* decrypt in place */
233-
aescfb_decrypt(&ctx, buf, buf, aescfb_tv[i].len, aescfb_tv[i].iv);
233+
aescfb_decrypt(&key, buf, buf, aescfb_tv[i].len, aescfb_tv[i].iv);
234234
if (memcmp(buf, aescfb_tv[i].ptext, aescfb_tv[i].len)) {
235235
pr_err("aescfb_decrypt() failed on vector %d\n", i);
236236
return -ENODEV;
237237
}
238238

239239
/* encrypt in place */
240-
aescfb_encrypt(&ctx, buf, buf, aescfb_tv[i].len, aescfb_tv[i].iv);
240+
aescfb_encrypt(&key, buf, buf, aescfb_tv[i].len, aescfb_tv[i].iv);
241241
if (memcmp(buf, aescfb_tv[i].ctext, aescfb_tv[i].len)) {
242242
pr_err("aescfb_encrypt() #2 failed on vector %d\n", i);
243243

0 commit comments

Comments
 (0)