95fea441a4f2d3dffc7ea08d9ce03412d08e352b
[openwrt/openwrt.git] / package / kernel / mac80211 / patches / 100-remove-cryptoapi-dependencies.patch
1 --- a/net/mac80211/Kconfig
2 +++ b/net/mac80211/Kconfig
3 @@ -5,8 +5,6 @@ config MAC80211
4 depends on CRYPTO
5 depends on CRYPTO_ARC4
6 depends on CRYPTO_AES
7 - select BPAUTO_CRYPTO_CCM
8 - depends on CRYPTO_GCM
9 depends on CRC32
10 ---help---
11 This option enables the hardware independent IEEE 802.11
12 --- a/net/mac80211/Makefile
13 +++ b/net/mac80211/Makefile
14 @@ -16,9 +16,7 @@ mac80211-y := \
15 michael.o \
16 tkip.o \
17 aes_ccm.o \
18 - aes_gcm.o \
19 aes_cmac.o \
20 - aes_gmac.o \
21 fils_aead.o \
22 cfg.o \
23 ethtool.o \
24 --- a/net/mac80211/aes_ccm.c
25 +++ b/net/mac80211/aes_ccm.c
26 @@ -13,103 +13,132 @@
27 #include <linux/types.h>
28 #include <linux/err.h>
29 #include <crypto/aead.h>
30 +#include <crypto/aes.h>
31
32 #include <net/mac80211.h>
33 #include "key.h"
34 #include "aes_ccm.h"
35
36 -int ieee80211_aes_ccm_encrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
37 - u8 *data, size_t data_len, u8 *mic,
38 - size_t mic_len)
39 +static void aes_ccm_prepare(struct crypto_cipher *tfm, u8 *b_0, u8 *aad, u8 *s_0,
40 + u8 *a, u8 *b)
41 {
42 - struct scatterlist sg[3];
43 - struct aead_request *aead_req;
44 - int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm);
45 - u8 *__aad;
46 -
47 - aead_req = kzalloc(reqsize + CCM_AAD_LEN, GFP_ATOMIC);
48 - if (!aead_req)
49 - return -ENOMEM;
50 -
51 - __aad = (u8 *)aead_req + reqsize;
52 - memcpy(__aad, aad, CCM_AAD_LEN);
53 -
54 - sg_init_table(sg, 3);
55 - sg_set_buf(&sg[0], &__aad[2], be16_to_cpup((__be16 *)__aad));
56 - sg_set_buf(&sg[1], data, data_len);
57 - sg_set_buf(&sg[2], mic, mic_len);
58 -
59 - aead_request_set_tfm(aead_req, tfm);
60 - aead_request_set_crypt(aead_req, sg, sg, data_len, b_0);
61 - aead_request_set_ad(aead_req, sg[0].length);
62 + int i;
63
64 - crypto_aead_encrypt(aead_req);
65 - kzfree(aead_req);
66 + crypto_cipher_encrypt_one(tfm, b, b_0);
67
68 - return 0;
69 + /* Extra Authenticate-only data (always two AES blocks) */
70 + for (i = 0; i < AES_BLOCK_SIZE; i++)
71 + aad[i] ^= b[i];
72 + crypto_cipher_encrypt_one(tfm, b, aad);
73 +
74 + aad += AES_BLOCK_SIZE;
75 +
76 + for (i = 0; i < AES_BLOCK_SIZE; i++)
77 + aad[i] ^= b[i];
78 + crypto_cipher_encrypt_one(tfm, a, aad);
79 +
80 + /* Mask out bits from auth-only-b_0 */
81 + b_0[0] &= 0x07;
82 +
83 + /* S_0 is used to encrypt T (= MIC) */
84 + b_0[14] = 0;
85 + b_0[15] = 0;
86 + crypto_cipher_encrypt_one(tfm, s_0, b_0);
87 +}
88 +
89 +
90 +void ieee80211_aes_ccm_encrypt(struct crypto_cipher *tfm, u8 *b_0, u8 *aad,
91 + u8 *data, size_t data_len, u8 *mic,
92 + size_t mic_len)
93 +{
94 + int i, j, last_len, num_blocks;
95 + u8 b[AES_BLOCK_SIZE];
96 + u8 s_0[AES_BLOCK_SIZE];
97 + u8 e[AES_BLOCK_SIZE];
98 + u8 *pos, *cpos;
99 +
100 + num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_SIZE);
101 + last_len = data_len % AES_BLOCK_SIZE;
102 + aes_ccm_prepare(tfm, b_0, aad, s_0, b, b);
103 +
104 + /* Process payload blocks */
105 + pos = data;
106 + cpos = data;
107 + for (j = 1; j <= num_blocks; j++) {
108 + int blen = (j == num_blocks && last_len) ?
109 + last_len : AES_BLOCK_SIZE;
110 +
111 + /* Authentication followed by encryption */
112 + for (i = 0; i < blen; i++)
113 + b[i] ^= pos[i];
114 + crypto_cipher_encrypt_one(tfm, b, b);
115 +
116 + b_0[14] = (j >> 8) & 0xff;
117 + b_0[15] = j & 0xff;
118 + crypto_cipher_encrypt_one(tfm, e, b_0);
119 + for (i = 0; i < blen; i++)
120 + *cpos++ = *pos++ ^ e[i];
121 + }
122 +
123 + for (i = 0; i < mic_len; i++)
124 + mic[i] = b[i] ^ s_0[i];
125 }
126
127 -int ieee80211_aes_ccm_decrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
128 +int ieee80211_aes_ccm_decrypt(struct crypto_cipher *tfm, u8 *b_0, u8 *aad,
129 u8 *data, size_t data_len, u8 *mic,
130 size_t mic_len)
131 {
132 - struct scatterlist sg[3];
133 - struct aead_request *aead_req;
134 - int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm);
135 - u8 *__aad;
136 - int err;
137 -
138 - if (data_len == 0)
139 - return -EINVAL;
140 -
141 - aead_req = kzalloc(reqsize + CCM_AAD_LEN, GFP_ATOMIC);
142 - if (!aead_req)
143 - return -ENOMEM;
144 -
145 - __aad = (u8 *)aead_req + reqsize;
146 - memcpy(__aad, aad, CCM_AAD_LEN);
147 -
148 - sg_init_table(sg, 3);
149 - sg_set_buf(&sg[0], &__aad[2], be16_to_cpup((__be16 *)__aad));
150 - sg_set_buf(&sg[1], data, data_len);
151 - sg_set_buf(&sg[2], mic, mic_len);
152 -
153 - aead_request_set_tfm(aead_req, tfm);
154 - aead_request_set_crypt(aead_req, sg, sg, data_len + mic_len, b_0);
155 - aead_request_set_ad(aead_req, sg[0].length);
156 + int i, j, last_len, num_blocks;
157 + u8 *pos, *cpos;
158 + u8 a[AES_BLOCK_SIZE];
159 + u8 b[AES_BLOCK_SIZE];
160 + u8 s_0[AES_BLOCK_SIZE];
161 +
162 + num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_SIZE);
163 + last_len = data_len % AES_BLOCK_SIZE;
164 + aes_ccm_prepare(tfm, b_0, aad, s_0, a, b);
165 +
166 + /* Process payload blocks */
167 + cpos = data;
168 + pos = data;
169 + for (j = 1; j <= num_blocks; j++) {
170 + int blen = (j == num_blocks && last_len) ?
171 + last_len : AES_BLOCK_SIZE;
172 +
173 + /* Decryption followed by authentication */
174 + b_0[14] = (j >> 8) & 0xff;
175 + b_0[15] = j & 0xff;
176 + crypto_cipher_encrypt_one(tfm, b, b_0);
177 + for (i = 0; i < blen; i++) {
178 + *pos = *cpos++ ^ b[i];
179 + a[i] ^= *pos++;
180 + }
181 + crypto_cipher_encrypt_one(tfm, a, a);
182 + }
183 +
184 + for (i = 0; i < mic_len; i++) {
185 + if ((mic[i] ^ s_0[i]) != a[i])
186 + return -1;
187 + }
188
189 - err = crypto_aead_decrypt(aead_req);
190 - kzfree(aead_req);
191 -
192 - return err;
193 + return 0;
194 }
195
196 -struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[],
197 - size_t key_len,
198 - size_t mic_len)
199 +struct crypto_cipher *ieee80211_aes_key_setup_encrypt(const u8 key[],
200 + size_t key_len,
201 + size_t mic_len)
202 {
203 - struct crypto_aead *tfm;
204 - int err;
205 + struct crypto_cipher *tfm;
206
207 - tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC);
208 - if (IS_ERR(tfm))
209 - return tfm;
210 -
211 - err = crypto_aead_setkey(tfm, key, key_len);
212 - if (err)
213 - goto free_aead;
214 - err = crypto_aead_setauthsize(tfm, mic_len);
215 - if (err)
216 - goto free_aead;
217 + tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
218 + if (!IS_ERR(tfm))
219 + crypto_cipher_setkey(tfm, key, key_len);
220
221 return tfm;
222 -
223 -free_aead:
224 - crypto_free_aead(tfm);
225 - return ERR_PTR(err);
226 }
227
228 -void ieee80211_aes_key_free(struct crypto_aead *tfm)
229 +
230 +void ieee80211_aes_key_free(struct crypto_cipher *tfm)
231 {
232 - crypto_free_aead(tfm);
233 + crypto_free_cipher(tfm);
234 }
235 --- a/net/mac80211/aes_gmac.h
236 +++ b/net/mac80211/aes_gmac.h
237 @@ -15,10 +15,22 @@
238 #define GMAC_MIC_LEN 16
239 #define GMAC_NONCE_LEN 12
240
241 -struct crypto_aead *ieee80211_aes_gmac_key_setup(const u8 key[],
242 - size_t key_len);
243 -int ieee80211_aes_gmac(struct crypto_aead *tfm, const u8 *aad, u8 *nonce,
244 - const u8 *data, size_t data_len, u8 *mic);
245 -void ieee80211_aes_gmac_key_free(struct crypto_aead *tfm);
246 +static inline struct crypto_aead *
247 +ieee80211_aes_gmac_key_setup(const u8 key[], size_t key_len)
248 +{
249 + return NULL;
250 +}
251 +
252 +static inline int
253 +ieee80211_aes_gmac(struct crypto_aead *tfm, const u8 *aad, u8 *nonce,
254 + const u8 *data, size_t data_len, u8 *mic)
255 +{
256 + return -EOPNOTSUPP;
257 +}
258 +
259 +static inline void
260 +ieee80211_aes_gmac_key_free(struct crypto_aead *tfm)
261 +{
262 +}
263
264 #endif /* AES_GMAC_H */
265 --- a/net/mac80211/key.h
266 +++ b/net/mac80211/key.h
267 @@ -88,7 +88,7 @@ struct ieee80211_key {
268 * Management frames.
269 */
270 u8 rx_pn[IEEE80211_NUM_TIDS + 1][IEEE80211_CCMP_PN_LEN];
271 - struct crypto_aead *tfm;
272 + struct crypto_cipher *tfm;
273 u32 replays; /* dot11RSNAStatsCCMPReplays */
274 } ccmp;
275 struct {
276 --- a/net/mac80211/wpa.c
277 +++ b/net/mac80211/wpa.c
278 @@ -305,7 +305,8 @@ ieee80211_crypto_tkip_decrypt(struct iee
279 }
280
281
282 -static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad)
283 +static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad,
284 + u16 data_len)
285 {
286 __le16 mask_fc;
287 int a4_included, mgmt;
288 @@ -335,14 +336,8 @@ static void ccmp_special_blocks(struct s
289 else
290 qos_tid = 0;
291
292 - /* In CCM, the initial vectors (IV) used for CTR mode encryption and CBC
293 - * mode authentication are not allowed to collide, yet both are derived
294 - * from this vector b_0. We only set L := 1 here to indicate that the
295 - * data size can be represented in (L+1) bytes. The CCM layer will take
296 - * care of storing the data length in the top (L+1) bytes and setting
297 - * and clearing the other bits as is required to derive the two IVs.
298 - */
299 - b_0[0] = 0x1;
300 + /* First block, b_0 */
301 + b_0[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
302
303 /* Nonce: Nonce Flags | A2 | PN
304 * Nonce Flags: Priority (b0..b3) | Management (b4) | Reserved (b5..b7)
305 @@ -350,6 +345,8 @@ static void ccmp_special_blocks(struct s
306 b_0[1] = qos_tid | (mgmt << 4);
307 memcpy(&b_0[2], hdr->addr2, ETH_ALEN);
308 memcpy(&b_0[8], pn, IEEE80211_CCMP_PN_LEN);
309 + /* l(m) */
310 + put_unaligned_be16(data_len, &b_0[14]);
311
312 /* AAD (extra authenticate-only data) / masked 802.11 header
313 * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
314 @@ -406,7 +403,7 @@ static int ccmp_encrypt_skb(struct ieee8
315 u8 *pos;
316 u8 pn[6];
317 u64 pn64;
318 - u8 aad[CCM_AAD_LEN];
319 + u8 aad[2 * AES_BLOCK_SIZE];
320 u8 b_0[AES_BLOCK_SIZE];
321
322 if (info->control.hw_key &&
323 @@ -461,9 +458,11 @@ static int ccmp_encrypt_skb(struct ieee8
324 return 0;
325
326 pos += IEEE80211_CCMP_HDR_LEN;
327 - ccmp_special_blocks(skb, pn, b_0, aad);
328 - return ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, b_0, aad, pos, len,
329 - skb_put(skb, mic_len), mic_len);
330 + ccmp_special_blocks(skb, pn, b_0, aad, len);
331 + ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, b_0, aad, pos, len,
332 + skb_put(skb, mic_len), mic_len);
333 +
334 + return 0;
335 }
336
337
338 @@ -536,7 +535,7 @@ ieee80211_crypto_ccmp_decrypt(struct iee
339 u8 aad[2 * AES_BLOCK_SIZE];
340 u8 b_0[AES_BLOCK_SIZE];
341 /* hardware didn't decrypt/verify MIC */
342 - ccmp_special_blocks(skb, pn, b_0, aad);
343 + ccmp_special_blocks(skb, pn, b_0, aad, data_len);
344
345 if (ieee80211_aes_ccm_decrypt(
346 key->u.ccmp.tfm, b_0, aad,
347 @@ -638,7 +637,7 @@ static int gcmp_encrypt_skb(struct ieee8
348 u8 *pos;
349 u8 pn[6];
350 u64 pn64;
351 - u8 aad[GCM_AAD_LEN];
352 + u8 aad[2 * AES_BLOCK_SIZE];
353 u8 j_0[AES_BLOCK_SIZE];
354
355 if (info->control.hw_key &&
356 @@ -695,8 +694,10 @@ static int gcmp_encrypt_skb(struct ieee8
357
358 pos += IEEE80211_GCMP_HDR_LEN;
359 gcmp_special_blocks(skb, pn, j_0, aad);
360 - return ieee80211_aes_gcm_encrypt(key->u.gcmp.tfm, j_0, aad, pos, len,
361 - skb_put(skb, IEEE80211_GCMP_MIC_LEN));
362 + ieee80211_aes_gcm_encrypt(key->u.gcmp.tfm, j_0, aad, pos, len,
363 + skb_put(skb, IEEE80211_GCMP_MIC_LEN));
364 +
365 + return 0;
366 }
367
368 ieee80211_tx_result
369 @@ -1120,9 +1121,9 @@ ieee80211_crypto_aes_gmac_encrypt(struct
370 struct ieee80211_key *key = tx->key;
371 struct ieee80211_mmie_16 *mmie;
372 struct ieee80211_hdr *hdr;
373 - u8 aad[GMAC_AAD_LEN];
374 + u8 aad[20];
375 u64 pn64;
376 - u8 nonce[GMAC_NONCE_LEN];
377 + u8 nonce[12];
378
379 if (WARN_ON(skb_queue_len(&tx->skbs) != 1))
380 return TX_DROP;
381 @@ -1168,7 +1169,7 @@ ieee80211_crypto_aes_gmac_decrypt(struct
382 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
383 struct ieee80211_key *key = rx->key;
384 struct ieee80211_mmie_16 *mmie;
385 - u8 aad[GMAC_AAD_LEN], mic[GMAC_MIC_LEN], ipn[6], nonce[GMAC_NONCE_LEN];
386 + u8 aad[20], mic[16], ipn[6], nonce[12];
387 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
388
389 if (!ieee80211_is_mgmt(hdr->frame_control))
390 --- a/net/mac80211/aes_ccm.h
391 +++ b/net/mac80211/aes_ccm.h
392 @@ -12,17 +12,15 @@
393
394 #include <linux/crypto.h>
395
396 -#define CCM_AAD_LEN 32
397 -
398 -struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[],
399 - size_t key_len,
400 - size_t mic_len);
401 -int ieee80211_aes_ccm_encrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
402 - u8 *data, size_t data_len, u8 *mic,
403 - size_t mic_len);
404 -int ieee80211_aes_ccm_decrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
405 +struct crypto_cipher *ieee80211_aes_key_setup_encrypt(const u8 key[],
406 + size_t key_len,
407 + size_t mic_len);
408 +void ieee80211_aes_ccm_encrypt(struct crypto_cipher *tfm, u8 *b_0, u8 *aad,
409 + u8 *data, size_t data_len, u8 *mic,
410 + size_t mic_len);
411 +int ieee80211_aes_ccm_decrypt(struct crypto_cipher *tfm, u8 *b_0, u8 *aad,
412 u8 *data, size_t data_len, u8 *mic,
413 size_t mic_len);
414 -void ieee80211_aes_key_free(struct crypto_aead *tfm);
415 +void ieee80211_aes_key_free(struct crypto_cipher *tfm);
416
417 #endif /* AES_CCM_H */
418 --- a/net/mac80211/aes_gcm.h
419 +++ b/net/mac80211/aes_gcm.h
420 @@ -11,14 +11,28 @@
421
422 #include <linux/crypto.h>
423
424 -#define GCM_AAD_LEN 32
425 +static inline void
426 +ieee80211_aes_gcm_encrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad,
427 + u8 *data, size_t data_len, u8 *mic)
428 +{
429 +}
430
431 -int ieee80211_aes_gcm_encrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad,
432 - u8 *data, size_t data_len, u8 *mic);
433 -int ieee80211_aes_gcm_decrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad,
434 - u8 *data, size_t data_len, u8 *mic);
435 -struct crypto_aead *ieee80211_aes_gcm_key_setup_encrypt(const u8 key[],
436 - size_t key_len);
437 -void ieee80211_aes_gcm_key_free(struct crypto_aead *tfm);
438 +static inline int
439 +ieee80211_aes_gcm_decrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad,
440 + u8 *data, size_t data_len, u8 *mic)
441 +{
442 + return -EOPNOTSUPP;
443 +}
444 +
445 +static inline struct crypto_aead *
446 +ieee80211_aes_gcm_key_setup_encrypt(const u8 key[], size_t key_len)
447 +{
448 + return NULL;
449 +}
450 +
451 +static inline void
452 +ieee80211_aes_gcm_key_free(struct crypto_aead *tfm)
453 +{
454 +}
455
456 #endif /* AES_GCM_H */