Skip to content

Commit a3b01ff

Browse files
ardbiesheuvelherbertx
authored andcommitted
chcr_ktls: use AES library for single use cipher
Allocating a cipher via the crypto API only to free it again after using it to encrypt a single block is unnecessary in cases where the algorithm is known at compile time. So replace this pattern with a call to the AES library. Cc: Ayush Sawal <[email protected]> Cc: Vinay Kumar Yadav <[email protected]> Cc: Rohit Maheshwari <[email protected]> Signed-off-by: Ard Biesheuvel <[email protected]> Reviewed-by: Eric Biggers <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent bbfd06c commit a3b01ff

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

drivers/net/ethernet/chelsio/inline_crypto/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ config CHELSIO_TLS_DEVICE
4242
depends on CHELSIO_T4
4343
depends on TLS
4444
depends on TLS_DEVICE
45+
select CRYPTO_LIB_AES
4546
help
4647
This flag enables support for kernel tls offload over Chelsio T6
4748
crypto accelerator. CONFIG_CHELSIO_TLS_DEVICE flag can be enabled

drivers/net/ethernet/chelsio/inline_crypto/ch_ktls/chcr_ktls.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <linux/ip.h>
1010
#include <net/ipv6.h>
1111
#include <linux/netdevice.h>
12+
#include <crypto/aes.h>
1213
#include "chcr_ktls.h"
1314

1415
static LIST_HEAD(uld_ctx_list);
@@ -74,7 +75,7 @@ static int chcr_ktls_save_keys(struct chcr_ktls_info *tx_info,
7475
unsigned char ghash_h[TLS_CIPHER_AES_GCM_256_TAG_SIZE];
7576
struct tls12_crypto_info_aes_gcm_128 *info_128_gcm;
7677
struct ktls_key_ctx *kctx = &tx_info->key_ctx;
77-
struct crypto_cipher *cipher;
78+
struct crypto_aes_ctx aes_ctx;
7879
unsigned char *key, *salt;
7980

8081
switch (crypto_info->cipher_type) {
@@ -135,18 +136,14 @@ static int chcr_ktls_save_keys(struct chcr_ktls_info *tx_info,
135136
/* Calculate the H = CIPH(K, 0 repeated 16 times).
136137
* It will go in key context
137138
*/
138-
cipher = crypto_alloc_cipher("aes", 0, 0);
139-
if (IS_ERR(cipher)) {
140-
ret = -ENOMEM;
141-
goto out;
142-
}
143139

144-
ret = crypto_cipher_setkey(cipher, key, keylen);
140+
ret = aes_expandkey(&aes_ctx, key, keylen);
145141
if (ret)
146-
goto out1;
142+
goto out;
147143

148144
memset(ghash_h, 0, ghash_size);
149-
crypto_cipher_encrypt_one(cipher, ghash_h, ghash_h);
145+
aes_encrypt(&aes_ctx, ghash_h, ghash_h);
146+
memzero_explicit(&aes_ctx, sizeof(aes_ctx));
150147

151148
/* fill the Key context */
152149
if (direction == TLS_OFFLOAD_CTX_DIR_TX) {
@@ -155,16 +152,14 @@ static int chcr_ktls_save_keys(struct chcr_ktls_info *tx_info,
155152
key_ctx_size >> 4);
156153
} else {
157154
ret = -EINVAL;
158-
goto out1;
155+
goto out;
159156
}
160157

161158
memcpy(kctx->salt, salt, tx_info->salt_size);
162159
memcpy(kctx->key, key, keylen);
163160
memcpy(kctx->key + keylen, ghash_h, ghash_size);
164161
tx_info->key_ctx_len = key_ctx_size;
165162

166-
out1:
167-
crypto_free_cipher(cipher);
168163
out:
169164
return ret;
170165
}

0 commit comments

Comments
 (0)