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