Skip to content

Commit 5f9f181

Browse files
Felix Fietkaulinvjw
authored andcommitted
mac80211: remove the dependency on crypto_blkcipher
The only thing that using crypto_blkcipher with ecb does over just using arc4 directly is wrapping the encrypt/decrypt function into a for loop, looping over each individual character. To be able to do this, it pulls in around 40 kb worth of unnecessary kernel modules (at least on a MIPS embedded device). Using arc4 directly not only eliminates those dependencies, it also makes the code smaller. Signed-off-by: Felix Fietkau <nbd@openwrt.org> Signed-off-by: John W. Linville <linville@tuxdriver.com>
1 parent 203afec commit 5f9f181

6 files changed

Lines changed: 23 additions & 28 deletions

File tree

‎net/mac80211/Kconfig‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ config MAC80211
22
tristate "Generic IEEE 802.11 Networking Stack (mac80211)"
33
depends on CFG80211
44
select CRYPTO
5-
select CRYPTO_ECB
65
select CRYPTO_ARC4
76
select CRYPTO_AES
87
select CRC32

‎net/mac80211/ieee80211_i.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -809,8 +809,8 @@ struct ieee80211_local {
809809

810810
struct rate_control_ref *rate_ctrl;
811811

812-
struct crypto_blkcipher *wep_tx_tfm;
813-
struct crypto_blkcipher *wep_rx_tfm;
812+
struct crypto_cipher *wep_tx_tfm;
813+
struct crypto_cipher *wep_rx_tfm;
814814
u32 wep_iv;
815815

816816
/* see iface.c */

‎net/mac80211/tkip.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ EXPORT_SYMBOL(ieee80211_get_tkip_key);
202202
* @payload_len is the length of payload (_not_ including IV/ICV length).
203203
* @ta is the transmitter addresses.
204204
*/
205-
int ieee80211_tkip_encrypt_data(struct crypto_blkcipher *tfm,
205+
int ieee80211_tkip_encrypt_data(struct crypto_cipher *tfm,
206206
struct ieee80211_key *key,
207207
u8 *pos, size_t payload_len, u8 *ta)
208208
{
@@ -223,7 +223,7 @@ int ieee80211_tkip_encrypt_data(struct crypto_blkcipher *tfm,
223223
* beginning of the buffer containing IEEE 802.11 header payload, i.e.,
224224
* including IV, Ext. IV, real data, Michael MIC, ICV. @payload_len is the
225225
* length of payload, including IV, Ext. IV, MIC, ICV. */
226-
int ieee80211_tkip_decrypt_data(struct crypto_blkcipher *tfm,
226+
int ieee80211_tkip_decrypt_data(struct crypto_cipher *tfm,
227227
struct ieee80211_key *key,
228228
u8 *payload, size_t payload_len, u8 *ta,
229229
u8 *ra, int only_iv, int queue,

‎net/mac80211/tkip.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
u8 *ieee80211_tkip_add_iv(u8 *pos, struct ieee80211_key *key, u16 iv16);
1717

18-
int ieee80211_tkip_encrypt_data(struct crypto_blkcipher *tfm,
18+
int ieee80211_tkip_encrypt_data(struct crypto_cipher *tfm,
1919
struct ieee80211_key *key,
2020
u8 *pos, size_t payload_len, u8 *ta);
2121
enum {
@@ -24,7 +24,7 @@ enum {
2424
TKIP_DECRYPT_INVALID_KEYIDX = -2,
2525
TKIP_DECRYPT_REPLAY = -3,
2626
};
27-
int ieee80211_tkip_decrypt_data(struct crypto_blkcipher *tfm,
27+
int ieee80211_tkip_decrypt_data(struct crypto_cipher *tfm,
2828
struct ieee80211_key *key,
2929
u8 *payload, size_t payload_len, u8 *ta,
3030
u8 *ra, int only_iv, int queue,

‎net/mac80211/wep.c‎

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,15 @@ int ieee80211_wep_init(struct ieee80211_local *local)
3030
/* start WEP IV from a random value */
3131
get_random_bytes(&local->wep_iv, WEP_IV_LEN);
3232

33-
local->wep_tx_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0,
34-
CRYPTO_ALG_ASYNC);
33+
local->wep_tx_tfm = crypto_alloc_cipher("arc4", 0, CRYPTO_ALG_ASYNC);
3534
if (IS_ERR(local->wep_tx_tfm)) {
3635
local->wep_rx_tfm = ERR_PTR(-EINVAL);
3736
return PTR_ERR(local->wep_tx_tfm);
3837
}
3938

40-
local->wep_rx_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0,
41-
CRYPTO_ALG_ASYNC);
39+
local->wep_rx_tfm = crypto_alloc_cipher("arc4", 0, CRYPTO_ALG_ASYNC);
4240
if (IS_ERR(local->wep_rx_tfm)) {
43-
crypto_free_blkcipher(local->wep_tx_tfm);
41+
crypto_free_cipher(local->wep_tx_tfm);
4442
local->wep_tx_tfm = ERR_PTR(-EINVAL);
4543
return PTR_ERR(local->wep_rx_tfm);
4644
}
@@ -51,9 +49,9 @@ int ieee80211_wep_init(struct ieee80211_local *local)
5149
void ieee80211_wep_free(struct ieee80211_local *local)
5250
{
5351
if (!IS_ERR(local->wep_tx_tfm))
54-
crypto_free_blkcipher(local->wep_tx_tfm);
52+
crypto_free_cipher(local->wep_tx_tfm);
5553
if (!IS_ERR(local->wep_rx_tfm))
56-
crypto_free_blkcipher(local->wep_rx_tfm);
54+
crypto_free_cipher(local->wep_rx_tfm);
5755
}
5856

5957
static inline bool ieee80211_wep_weak_iv(u32 iv, int keylen)
@@ -127,22 +125,21 @@ static void ieee80211_wep_remove_iv(struct ieee80211_local *local,
127125
/* Perform WEP encryption using given key. data buffer must have tailroom
128126
* for 4-byte ICV. data_len must not include this ICV. Note: this function
129127
* does _not_ add IV. data = RC4(data | CRC32(data)) */
130-
int ieee80211_wep_encrypt_data(struct crypto_blkcipher *tfm, u8 *rc4key,
128+
int ieee80211_wep_encrypt_data(struct crypto_cipher *tfm, u8 *rc4key,
131129
size_t klen, u8 *data, size_t data_len)
132130
{
133-
struct blkcipher_desc desc = { .tfm = tfm };
134-
struct scatterlist sg;
135131
__le32 icv;
132+
int i;
136133

137134
if (IS_ERR(tfm))
138135
return -1;
139136

140137
icv = cpu_to_le32(~crc32_le(~0, data, data_len));
141138
put_unaligned(icv, (__le32 *)(data + data_len));
142139

143-
crypto_blkcipher_setkey(tfm, rc4key, klen);
144-
sg_init_one(&sg, data, data_len + WEP_ICV_LEN);
145-
crypto_blkcipher_encrypt(&desc, &sg, &sg, sg.length);
140+
crypto_cipher_setkey(tfm, rc4key, klen);
141+
for (i = 0; i < data_len + WEP_ICV_LEN; i++)
142+
crypto_cipher_encrypt_one(tfm, data + i, data + i);
146143

147144
return 0;
148145
}
@@ -186,19 +183,18 @@ int ieee80211_wep_encrypt(struct ieee80211_local *local,
186183
/* Perform WEP decryption using given key. data buffer includes encrypted
187184
* payload, including 4-byte ICV, but _not_ IV. data_len must not include ICV.
188185
* Return 0 on success and -1 on ICV mismatch. */
189-
int ieee80211_wep_decrypt_data(struct crypto_blkcipher *tfm, u8 *rc4key,
186+
int ieee80211_wep_decrypt_data(struct crypto_cipher *tfm, u8 *rc4key,
190187
size_t klen, u8 *data, size_t data_len)
191188
{
192-
struct blkcipher_desc desc = { .tfm = tfm };
193-
struct scatterlist sg;
194189
__le32 crc;
190+
int i;
195191

196192
if (IS_ERR(tfm))
197193
return -1;
198194

199-
crypto_blkcipher_setkey(tfm, rc4key, klen);
200-
sg_init_one(&sg, data, data_len + WEP_ICV_LEN);
201-
crypto_blkcipher_decrypt(&desc, &sg, &sg, sg.length);
195+
crypto_cipher_setkey(tfm, rc4key, klen);
196+
for (i = 0; i < data_len + WEP_ICV_LEN; i++)
197+
crypto_cipher_decrypt_one(tfm, data + i, data + i);
202198

203199
crc = cpu_to_le32(~crc32_le(~0, data, data_len));
204200
if (memcmp(&crc, data + data_len, WEP_ICV_LEN) != 0)

‎net/mac80211/wep.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919
int ieee80211_wep_init(struct ieee80211_local *local);
2020
void ieee80211_wep_free(struct ieee80211_local *local);
21-
int ieee80211_wep_encrypt_data(struct crypto_blkcipher *tfm, u8 *rc4key,
21+
int ieee80211_wep_encrypt_data(struct crypto_cipher *tfm, u8 *rc4key,
2222
size_t klen, u8 *data, size_t data_len);
2323
int ieee80211_wep_encrypt(struct ieee80211_local *local,
2424
struct sk_buff *skb,
2525
const u8 *key, int keylen, int keyidx);
26-
int ieee80211_wep_decrypt_data(struct crypto_blkcipher *tfm, u8 *rc4key,
26+
int ieee80211_wep_decrypt_data(struct crypto_cipher *tfm, u8 *rc4key,
2727
size_t klen, u8 *data, size_t data_len);
2828
bool ieee80211_wep_is_weak_iv(struct sk_buff *skb, struct ieee80211_key *key);
2929

0 commit comments

Comments
 (0)