Skip to content

Commit 608fb34

Browse files
committed
lib80211: Use skcipher and ahash
This patch replaces uses of blkcipher with skcipher and the long obsolete hash interface with ahash. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 3b5cf20 commit 608fb34

2 files changed

Lines changed: 81 additions & 64 deletions

File tree

‎net/wireless/lib80211_crypt_tkip.c‎

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
#include <linux/ieee80211.h>
3030
#include <net/iw_handler.h>
3131

32-
#include <linux/crypto.h>
32+
#include <crypto/hash.h>
33+
#include <crypto/skcipher.h>
3334
#include <linux/crc32.h>
3435

3536
#include <net/lib80211.h>
@@ -63,10 +64,10 @@ struct lib80211_tkip_data {
6364

6465
int key_idx;
6566

66-
struct crypto_blkcipher *rx_tfm_arc4;
67-
struct crypto_hash *rx_tfm_michael;
68-
struct crypto_blkcipher *tx_tfm_arc4;
69-
struct crypto_hash *tx_tfm_michael;
67+
struct crypto_skcipher *rx_tfm_arc4;
68+
struct crypto_ahash *rx_tfm_michael;
69+
struct crypto_skcipher *tx_tfm_arc4;
70+
struct crypto_ahash *tx_tfm_michael;
7071

7172
/* scratch buffers for virt_to_page() (crypto API) */
7273
u8 rx_hdr[16], tx_hdr[16];
@@ -98,29 +99,29 @@ static void *lib80211_tkip_init(int key_idx)
9899

99100
priv->key_idx = key_idx;
100101

101-
priv->tx_tfm_arc4 = crypto_alloc_blkcipher("ecb(arc4)", 0,
102-
CRYPTO_ALG_ASYNC);
102+
priv->tx_tfm_arc4 = crypto_alloc_skcipher("ecb(arc4)", 0,
103+
CRYPTO_ALG_ASYNC);
103104
if (IS_ERR(priv->tx_tfm_arc4)) {
104105
priv->tx_tfm_arc4 = NULL;
105106
goto fail;
106107
}
107108

108-
priv->tx_tfm_michael = crypto_alloc_hash("michael_mic", 0,
109-
CRYPTO_ALG_ASYNC);
109+
priv->tx_tfm_michael = crypto_alloc_ahash("michael_mic", 0,
110+
CRYPTO_ALG_ASYNC);
110111
if (IS_ERR(priv->tx_tfm_michael)) {
111112
priv->tx_tfm_michael = NULL;
112113
goto fail;
113114
}
114115

115-
priv->rx_tfm_arc4 = crypto_alloc_blkcipher("ecb(arc4)", 0,
116-
CRYPTO_ALG_ASYNC);
116+
priv->rx_tfm_arc4 = crypto_alloc_skcipher("ecb(arc4)", 0,
117+
CRYPTO_ALG_ASYNC);
117118
if (IS_ERR(priv->rx_tfm_arc4)) {
118119
priv->rx_tfm_arc4 = NULL;
119120
goto fail;
120121
}
121122

122-
priv->rx_tfm_michael = crypto_alloc_hash("michael_mic", 0,
123-
CRYPTO_ALG_ASYNC);
123+
priv->rx_tfm_michael = crypto_alloc_ahash("michael_mic", 0,
124+
CRYPTO_ALG_ASYNC);
124125
if (IS_ERR(priv->rx_tfm_michael)) {
125126
priv->rx_tfm_michael = NULL;
126127
goto fail;
@@ -130,14 +131,10 @@ static void *lib80211_tkip_init(int key_idx)
130131

131132
fail:
132133
if (priv) {
133-
if (priv->tx_tfm_michael)
134-
crypto_free_hash(priv->tx_tfm_michael);
135-
if (priv->tx_tfm_arc4)
136-
crypto_free_blkcipher(priv->tx_tfm_arc4);
137-
if (priv->rx_tfm_michael)
138-
crypto_free_hash(priv->rx_tfm_michael);
139-
if (priv->rx_tfm_arc4)
140-
crypto_free_blkcipher(priv->rx_tfm_arc4);
134+
crypto_free_ahash(priv->tx_tfm_michael);
135+
crypto_free_skcipher(priv->tx_tfm_arc4);
136+
crypto_free_ahash(priv->rx_tfm_michael);
137+
crypto_free_skcipher(priv->rx_tfm_arc4);
141138
kfree(priv);
142139
}
143140

@@ -148,14 +145,10 @@ static void lib80211_tkip_deinit(void *priv)
148145
{
149146
struct lib80211_tkip_data *_priv = priv;
150147
if (_priv) {
151-
if (_priv->tx_tfm_michael)
152-
crypto_free_hash(_priv->tx_tfm_michael);
153-
if (_priv->tx_tfm_arc4)
154-
crypto_free_blkcipher(_priv->tx_tfm_arc4);
155-
if (_priv->rx_tfm_michael)
156-
crypto_free_hash(_priv->rx_tfm_michael);
157-
if (_priv->rx_tfm_arc4)
158-
crypto_free_blkcipher(_priv->rx_tfm_arc4);
148+
crypto_free_ahash(_priv->tx_tfm_michael);
149+
crypto_free_skcipher(_priv->tx_tfm_arc4);
150+
crypto_free_ahash(_priv->rx_tfm_michael);
151+
crypto_free_skcipher(_priv->rx_tfm_arc4);
159152
}
160153
kfree(priv);
161154
}
@@ -353,11 +346,12 @@ static int lib80211_tkip_hdr(struct sk_buff *skb, int hdr_len,
353346
static int lib80211_tkip_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
354347
{
355348
struct lib80211_tkip_data *tkey = priv;
356-
struct blkcipher_desc desc = { .tfm = tkey->tx_tfm_arc4 };
349+
SKCIPHER_REQUEST_ON_STACK(req, tkey->tx_tfm_arc4);
357350
int len;
358351
u8 rc4key[16], *pos, *icv;
359352
u32 crc;
360353
struct scatterlist sg;
354+
int err;
361355

362356
if (tkey->flags & IEEE80211_CRYPTO_TKIP_COUNTERMEASURES) {
363357
struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
@@ -382,9 +376,14 @@ static int lib80211_tkip_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
382376
icv[2] = crc >> 16;
383377
icv[3] = crc >> 24;
384378

385-
crypto_blkcipher_setkey(tkey->tx_tfm_arc4, rc4key, 16);
379+
crypto_skcipher_setkey(tkey->tx_tfm_arc4, rc4key, 16);
386380
sg_init_one(&sg, pos, len + 4);
387-
return crypto_blkcipher_encrypt(&desc, &sg, &sg, len + 4);
381+
skcipher_request_set_tfm(req, tkey->tx_tfm_arc4);
382+
skcipher_request_set_callback(req, 0, NULL, NULL);
383+
skcipher_request_set_crypt(req, &sg, &sg, len + 4, NULL);
384+
err = crypto_skcipher_encrypt(req);
385+
skcipher_request_zero(req);
386+
return err;
388387
}
389388

390389
/*
@@ -403,7 +402,7 @@ static inline int tkip_replay_check(u32 iv32_n, u16 iv16_n,
403402
static int lib80211_tkip_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
404403
{
405404
struct lib80211_tkip_data *tkey = priv;
406-
struct blkcipher_desc desc = { .tfm = tkey->rx_tfm_arc4 };
405+
SKCIPHER_REQUEST_ON_STACK(req, tkey->rx_tfm_arc4);
407406
u8 rc4key[16];
408407
u8 keyidx, *pos;
409408
u32 iv32;
@@ -413,6 +412,7 @@ static int lib80211_tkip_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
413412
u32 crc;
414413
struct scatterlist sg;
415414
int plen;
415+
int err;
416416

417417
hdr = (struct ieee80211_hdr *)skb->data;
418418

@@ -465,9 +465,14 @@ static int lib80211_tkip_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
465465

466466
plen = skb->len - hdr_len - 12;
467467

468-
crypto_blkcipher_setkey(tkey->rx_tfm_arc4, rc4key, 16);
468+
crypto_skcipher_setkey(tkey->rx_tfm_arc4, rc4key, 16);
469469
sg_init_one(&sg, pos, plen + 4);
470-
if (crypto_blkcipher_decrypt(&desc, &sg, &sg, plen + 4)) {
470+
skcipher_request_set_tfm(req, tkey->rx_tfm_arc4);
471+
skcipher_request_set_callback(req, 0, NULL, NULL);
472+
skcipher_request_set_crypt(req, &sg, &sg, plen + 4, NULL);
473+
err = crypto_skcipher_decrypt(req);
474+
skcipher_request_zero(req);
475+
if (err) {
471476
net_dbg_ratelimited("TKIP: failed to decrypt received packet from %pM\n",
472477
hdr->addr2);
473478
return -7;
@@ -505,11 +510,12 @@ static int lib80211_tkip_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
505510
return keyidx;
506511
}
507512

508-
static int michael_mic(struct crypto_hash *tfm_michael, u8 * key, u8 * hdr,
513+
static int michael_mic(struct crypto_ahash *tfm_michael, u8 * key, u8 * hdr,
509514
u8 * data, size_t data_len, u8 * mic)
510515
{
511-
struct hash_desc desc;
516+
AHASH_REQUEST_ON_STACK(req, tfm_michael);
512517
struct scatterlist sg[2];
518+
int err;
513519

514520
if (tfm_michael == NULL) {
515521
pr_warn("%s(): tfm_michael == NULL\n", __func__);
@@ -519,12 +525,15 @@ static int michael_mic(struct crypto_hash *tfm_michael, u8 * key, u8 * hdr,
519525
sg_set_buf(&sg[0], hdr, 16);
520526
sg_set_buf(&sg[1], data, data_len);
521527

522-
if (crypto_hash_setkey(tfm_michael, key, 8))
528+
if (crypto_ahash_setkey(tfm_michael, key, 8))
523529
return -1;
524530

525-
desc.tfm = tfm_michael;
526-
desc.flags = 0;
527-
return crypto_hash_digest(&desc, sg, data_len + 16, mic);
531+
ahash_request_set_tfm(req, tfm_michael);
532+
ahash_request_set_callback(req, 0, NULL, NULL);
533+
ahash_request_set_crypt(req, sg, mic, data_len + 16);
534+
err = crypto_ahash_digest(req);
535+
ahash_request_zero(req);
536+
return err;
528537
}
529538

530539
static void michael_mic_hdr(struct sk_buff *skb, u8 * hdr)
@@ -645,10 +654,10 @@ static int lib80211_tkip_set_key(void *key, int len, u8 * seq, void *priv)
645654
{
646655
struct lib80211_tkip_data *tkey = priv;
647656
int keyidx;
648-
struct crypto_hash *tfm = tkey->tx_tfm_michael;
649-
struct crypto_blkcipher *tfm2 = tkey->tx_tfm_arc4;
650-
struct crypto_hash *tfm3 = tkey->rx_tfm_michael;
651-
struct crypto_blkcipher *tfm4 = tkey->rx_tfm_arc4;
657+
struct crypto_ahash *tfm = tkey->tx_tfm_michael;
658+
struct crypto_skcipher *tfm2 = tkey->tx_tfm_arc4;
659+
struct crypto_ahash *tfm3 = tkey->rx_tfm_michael;
660+
struct crypto_skcipher *tfm4 = tkey->rx_tfm_arc4;
652661

653662
keyidx = tkey->key_idx;
654663
memset(tkey, 0, sizeof(*tkey));

‎net/wireless/lib80211_crypt_wep.c‎

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
#include <net/lib80211.h>
2424

25-
#include <linux/crypto.h>
25+
#include <crypto/skcipher.h>
2626
#include <linux/crc32.h>
2727

2828
MODULE_AUTHOR("Jouni Malinen");
@@ -35,8 +35,8 @@ struct lib80211_wep_data {
3535
u8 key[WEP_KEY_LEN + 1];
3636
u8 key_len;
3737
u8 key_idx;
38-
struct crypto_blkcipher *tx_tfm;
39-
struct crypto_blkcipher *rx_tfm;
38+
struct crypto_skcipher *tx_tfm;
39+
struct crypto_skcipher *rx_tfm;
4040
};
4141

4242
static void *lib80211_wep_init(int keyidx)
@@ -48,13 +48,13 @@ static void *lib80211_wep_init(int keyidx)
4848
goto fail;
4949
priv->key_idx = keyidx;
5050

51-
priv->tx_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
51+
priv->tx_tfm = crypto_alloc_skcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
5252
if (IS_ERR(priv->tx_tfm)) {
5353
priv->tx_tfm = NULL;
5454
goto fail;
5555
}
5656

57-
priv->rx_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
57+
priv->rx_tfm = crypto_alloc_skcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
5858
if (IS_ERR(priv->rx_tfm)) {
5959
priv->rx_tfm = NULL;
6060
goto fail;
@@ -66,10 +66,8 @@ static void *lib80211_wep_init(int keyidx)
6666

6767
fail:
6868
if (priv) {
69-
if (priv->tx_tfm)
70-
crypto_free_blkcipher(priv->tx_tfm);
71-
if (priv->rx_tfm)
72-
crypto_free_blkcipher(priv->rx_tfm);
69+
crypto_free_skcipher(priv->tx_tfm);
70+
crypto_free_skcipher(priv->rx_tfm);
7371
kfree(priv);
7472
}
7573
return NULL;
@@ -79,10 +77,8 @@ static void lib80211_wep_deinit(void *priv)
7977
{
8078
struct lib80211_wep_data *_priv = priv;
8179
if (_priv) {
82-
if (_priv->tx_tfm)
83-
crypto_free_blkcipher(_priv->tx_tfm);
84-
if (_priv->rx_tfm)
85-
crypto_free_blkcipher(_priv->rx_tfm);
80+
crypto_free_skcipher(_priv->tx_tfm);
81+
crypto_free_skcipher(_priv->rx_tfm);
8682
}
8783
kfree(priv);
8884
}
@@ -133,11 +129,12 @@ static int lib80211_wep_build_iv(struct sk_buff *skb, int hdr_len,
133129
static int lib80211_wep_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
134130
{
135131
struct lib80211_wep_data *wep = priv;
136-
struct blkcipher_desc desc = { .tfm = wep->tx_tfm };
132+
SKCIPHER_REQUEST_ON_STACK(req, wep->tx_tfm);
137133
u32 crc, klen, len;
138134
u8 *pos, *icv;
139135
struct scatterlist sg;
140136
u8 key[WEP_KEY_LEN + 3];
137+
int err;
141138

142139
/* other checks are in lib80211_wep_build_iv */
143140
if (skb_tailroom(skb) < 4)
@@ -165,9 +162,14 @@ static int lib80211_wep_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
165162
icv[2] = crc >> 16;
166163
icv[3] = crc >> 24;
167164

168-
crypto_blkcipher_setkey(wep->tx_tfm, key, klen);
165+
crypto_skcipher_setkey(wep->tx_tfm, key, klen);
169166
sg_init_one(&sg, pos, len + 4);
170-
return crypto_blkcipher_encrypt(&desc, &sg, &sg, len + 4);
167+
skcipher_request_set_tfm(req, wep->tx_tfm);
168+
skcipher_request_set_callback(req, 0, NULL, NULL);
169+
skcipher_request_set_crypt(req, &sg, &sg, len + 4, NULL);
170+
err = crypto_skcipher_encrypt(req);
171+
skcipher_request_zero(req);
172+
return err;
171173
}
172174

173175
/* Perform WEP decryption on given buffer. Buffer includes whole WEP part of
@@ -180,11 +182,12 @@ static int lib80211_wep_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
180182
static int lib80211_wep_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
181183
{
182184
struct lib80211_wep_data *wep = priv;
183-
struct blkcipher_desc desc = { .tfm = wep->rx_tfm };
185+
SKCIPHER_REQUEST_ON_STACK(req, wep->rx_tfm);
184186
u32 crc, klen, plen;
185187
u8 key[WEP_KEY_LEN + 3];
186188
u8 keyidx, *pos, icv[4];
187189
struct scatterlist sg;
190+
int err;
188191

189192
if (skb->len < hdr_len + 8)
190193
return -1;
@@ -205,9 +208,14 @@ static int lib80211_wep_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
205208
/* Apply RC4 to data and compute CRC32 over decrypted data */
206209
plen = skb->len - hdr_len - 8;
207210

208-
crypto_blkcipher_setkey(wep->rx_tfm, key, klen);
211+
crypto_skcipher_setkey(wep->rx_tfm, key, klen);
209212
sg_init_one(&sg, pos, plen + 4);
210-
if (crypto_blkcipher_decrypt(&desc, &sg, &sg, plen + 4))
213+
skcipher_request_set_tfm(req, wep->rx_tfm);
214+
skcipher_request_set_callback(req, 0, NULL, NULL);
215+
skcipher_request_set_crypt(req, &sg, &sg, plen + 4, NULL);
216+
err = crypto_skcipher_decrypt(req);
217+
skcipher_request_zero(req);
218+
if (err)
211219
return -7;
212220

213221
crc = ~crc32_le(~0, pos, plen);

0 commit comments

Comments
 (0)