Skip to content

Commit 4be2970

Browse files
Ard Biesheuvelherbertx
authored andcommitted
net/lib80211: move TKIP handling to ARC4 library code
The crypto API abstraction is not very useful for invoking ciphers directly, especially in the case of arc4, which only has a generic implementation in C. So let's invoke the library code directly. Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent af1f3d3 commit 4be2970

File tree

2 files changed

+18
-31
lines changed

2 files changed

+18
-31
lines changed

net/wireless/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ config LIB80211_CRYPT_CCMP
219219

220220
config LIB80211_CRYPT_TKIP
221221
tristate
222+
select CRYPTO_LIB_ARC4
222223

223224
config LIB80211_DEBUG
224225
bool "lib80211 debugging messages"

net/wireless/lib80211_crypt_tkip.c

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1414

1515
#include <linux/err.h>
16+
#include <linux/fips.h>
1617
#include <linux/module.h>
1718
#include <linux/init.h>
1819
#include <linux/slab.h>
@@ -29,6 +30,7 @@
2930
#include <linux/ieee80211.h>
3031
#include <net/iw_handler.h>
3132

33+
#include <crypto/arc4.h>
3234
#include <crypto/hash.h>
3335
#include <linux/crypto.h>
3436
#include <linux/crc32.h>
@@ -64,9 +66,9 @@ struct lib80211_tkip_data {
6466

6567
int key_idx;
6668

67-
struct crypto_cipher *rx_tfm_arc4;
69+
struct arc4_ctx rx_ctx_arc4;
70+
struct arc4_ctx tx_ctx_arc4;
6871
struct crypto_shash *rx_tfm_michael;
69-
struct crypto_cipher *tx_tfm_arc4;
7072
struct crypto_shash *tx_tfm_michael;
7173

7274
/* scratch buffers for virt_to_page() (crypto API) */
@@ -93,30 +95,21 @@ static void *lib80211_tkip_init(int key_idx)
9395
{
9496
struct lib80211_tkip_data *priv;
9597

98+
if (fips_enabled)
99+
return NULL;
100+
96101
priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
97102
if (priv == NULL)
98103
goto fail;
99104

100105
priv->key_idx = key_idx;
101106

102-
priv->tx_tfm_arc4 = crypto_alloc_cipher("arc4", 0, 0);
103-
if (IS_ERR(priv->tx_tfm_arc4)) {
104-
priv->tx_tfm_arc4 = NULL;
105-
goto fail;
106-
}
107-
108107
priv->tx_tfm_michael = crypto_alloc_shash("michael_mic", 0, 0);
109108
if (IS_ERR(priv->tx_tfm_michael)) {
110109
priv->tx_tfm_michael = NULL;
111110
goto fail;
112111
}
113112

114-
priv->rx_tfm_arc4 = crypto_alloc_cipher("arc4", 0, 0);
115-
if (IS_ERR(priv->rx_tfm_arc4)) {
116-
priv->rx_tfm_arc4 = NULL;
117-
goto fail;
118-
}
119-
120113
priv->rx_tfm_michael = crypto_alloc_shash("michael_mic", 0, 0);
121114
if (IS_ERR(priv->rx_tfm_michael)) {
122115
priv->rx_tfm_michael = NULL;
@@ -128,9 +121,7 @@ static void *lib80211_tkip_init(int key_idx)
128121
fail:
129122
if (priv) {
130123
crypto_free_shash(priv->tx_tfm_michael);
131-
crypto_free_cipher(priv->tx_tfm_arc4);
132124
crypto_free_shash(priv->rx_tfm_michael);
133-
crypto_free_cipher(priv->rx_tfm_arc4);
134125
kfree(priv);
135126
}
136127

@@ -142,11 +133,9 @@ static void lib80211_tkip_deinit(void *priv)
142133
struct lib80211_tkip_data *_priv = priv;
143134
if (_priv) {
144135
crypto_free_shash(_priv->tx_tfm_michael);
145-
crypto_free_cipher(_priv->tx_tfm_arc4);
146136
crypto_free_shash(_priv->rx_tfm_michael);
147-
crypto_free_cipher(_priv->rx_tfm_arc4);
148137
}
149-
kfree(priv);
138+
kzfree(priv);
150139
}
151140

152141
static inline u16 RotR1(u16 val)
@@ -345,7 +334,6 @@ static int lib80211_tkip_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
345334
int len;
346335
u8 rc4key[16], *pos, *icv;
347336
u32 crc;
348-
int i;
349337

350338
if (tkey->flags & IEEE80211_CRYPTO_TKIP_COUNTERMEASURES) {
351339
struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
@@ -370,9 +358,9 @@ static int lib80211_tkip_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
370358
icv[2] = crc >> 16;
371359
icv[3] = crc >> 24;
372360

373-
crypto_cipher_setkey(tkey->tx_tfm_arc4, rc4key, 16);
374-
for (i = 0; i < len + 4; i++)
375-
crypto_cipher_encrypt_one(tkey->tx_tfm_arc4, pos + i, pos + i);
361+
arc4_setkey(&tkey->tx_ctx_arc4, rc4key, 16);
362+
arc4_crypt(&tkey->tx_ctx_arc4, pos, pos, len + 4);
363+
376364
return 0;
377365
}
378366

@@ -400,7 +388,6 @@ static int lib80211_tkip_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
400388
u8 icv[4];
401389
u32 crc;
402390
int plen;
403-
int i;
404391

405392
hdr = (struct ieee80211_hdr *)skb->data;
406393

@@ -453,9 +440,8 @@ static int lib80211_tkip_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
453440

454441
plen = skb->len - hdr_len - 12;
455442

456-
crypto_cipher_setkey(tkey->rx_tfm_arc4, rc4key, 16);
457-
for (i = 0; i < plen + 4; i++)
458-
crypto_cipher_decrypt_one(tkey->rx_tfm_arc4, pos + i, pos + i);
443+
arc4_setkey(&tkey->rx_ctx_arc4, rc4key, 16);
444+
arc4_crypt(&tkey->rx_ctx_arc4, pos, pos, plen + 4);
459445

460446
crc = ~crc32_le(~0, pos, plen);
461447
icv[0] = crc;
@@ -640,17 +626,17 @@ static int lib80211_tkip_set_key(void *key, int len, u8 * seq, void *priv)
640626
struct lib80211_tkip_data *tkey = priv;
641627
int keyidx;
642628
struct crypto_shash *tfm = tkey->tx_tfm_michael;
643-
struct crypto_cipher *tfm2 = tkey->tx_tfm_arc4;
629+
struct arc4_ctx *tfm2 = &tkey->tx_ctx_arc4;
644630
struct crypto_shash *tfm3 = tkey->rx_tfm_michael;
645-
struct crypto_cipher *tfm4 = tkey->rx_tfm_arc4;
631+
struct arc4_ctx *tfm4 = &tkey->rx_ctx_arc4;
646632

647633
keyidx = tkey->key_idx;
648634
memset(tkey, 0, sizeof(*tkey));
649635
tkey->key_idx = keyidx;
650636
tkey->tx_tfm_michael = tfm;
651-
tkey->tx_tfm_arc4 = tfm2;
637+
tkey->tx_ctx_arc4 = *tfm2;
652638
tkey->rx_tfm_michael = tfm3;
653-
tkey->rx_tfm_arc4 = tfm4;
639+
tkey->rx_ctx_arc4 = *tfm4;
654640
if (len == TKIP_KEY_LEN) {
655641
memcpy(tkey->key, key, TKIP_KEY_LEN);
656642
tkey->key_set = 1;

0 commit comments

Comments
 (0)