1 --- a/net/mac80211/Makefile
2 +++ b/net/mac80211/Makefile
3 @@ -7,7 +7,6 @@ mac80211-y := \
10 ht.o agg-tx.o agg-rx.o \
11 @@ -19,8 +18,8 @@ mac80211-y := \
21 --- a/net/mac80211/aead_api.c
24 -// SPDX-License-Identifier: GPL-2.0-only
26 - * Copyright 2003-2004, Instant802 Networks, Inc.
27 - * Copyright 2005-2006, Devicescape Software, Inc.
28 - * Copyright 2014-2015, Qualcomm Atheros, Inc.
30 - * Rewrite: Copyright (C) 2013 Linaro Ltd <ard.biesheuvel@linaro.org>
33 -#include <linux/kernel.h>
34 -#include <linux/types.h>
35 -#include <linux/err.h>
36 -#include <linux/scatterlist.h>
37 -#include <crypto/aead.h>
39 -#include "aead_api.h"
41 -int aead_encrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad, size_t aad_len,
42 - u8 *data, size_t data_len, u8 *mic)
44 - size_t mic_len = crypto_aead_authsize(tfm);
45 - struct scatterlist sg[3];
46 - struct aead_request *aead_req;
47 - int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm);
51 - aead_req = kzalloc(reqsize + aad_len, GFP_ATOMIC);
55 - __aad = (u8 *)aead_req + reqsize;
56 - memcpy(__aad, aad, aad_len);
58 - sg_init_table(sg, 3);
59 - sg_set_buf(&sg[0], __aad, aad_len);
60 - sg_set_buf(&sg[1], data, data_len);
61 - sg_set_buf(&sg[2], mic, mic_len);
63 - aead_request_set_tfm(aead_req, tfm);
64 - aead_request_set_crypt(aead_req, sg, sg, data_len, b_0);
65 - aead_request_set_ad(aead_req, sg[0].length);
67 - ret = crypto_aead_encrypt(aead_req);
68 - kfree_sensitive(aead_req);
73 -int aead_decrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad, size_t aad_len,
74 - u8 *data, size_t data_len, u8 *mic)
76 - size_t mic_len = crypto_aead_authsize(tfm);
77 - struct scatterlist sg[3];
78 - struct aead_request *aead_req;
79 - int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm);
86 - aead_req = kzalloc(reqsize + aad_len, GFP_ATOMIC);
90 - __aad = (u8 *)aead_req + reqsize;
91 - memcpy(__aad, aad, aad_len);
93 - sg_init_table(sg, 3);
94 - sg_set_buf(&sg[0], __aad, aad_len);
95 - sg_set_buf(&sg[1], data, data_len);
96 - sg_set_buf(&sg[2], mic, mic_len);
98 - aead_request_set_tfm(aead_req, tfm);
99 - aead_request_set_crypt(aead_req, sg, sg, data_len + mic_len, b_0);
100 - aead_request_set_ad(aead_req, sg[0].length);
102 - err = crypto_aead_decrypt(aead_req);
103 - kfree_sensitive(aead_req);
108 -struct crypto_aead *
109 -aead_key_setup_encrypt(const char *alg, const u8 key[],
110 - size_t key_len, size_t mic_len)
112 - struct crypto_aead *tfm;
115 - tfm = crypto_alloc_aead(alg, 0, CRYPTO_ALG_ASYNC);
119 - err = crypto_aead_setkey(tfm, key, key_len);
122 - err = crypto_aead_setauthsize(tfm, mic_len);
129 - crypto_free_aead(tfm);
130 - return ERR_PTR(err);
133 -void aead_key_free(struct crypto_aead *tfm)
135 - crypto_free_aead(tfm);
137 --- a/net/mac80211/aead_api.h
140 -/* SPDX-License-Identifier: GPL-2.0-only */
145 -#include <crypto/aead.h>
146 -#include <linux/crypto.h>
148 -struct crypto_aead *
149 -aead_key_setup_encrypt(const char *alg, const u8 key[],
150 - size_t key_len, size_t mic_len);
152 -int aead_encrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
153 - size_t aad_len, u8 *data,
154 - size_t data_len, u8 *mic);
156 -int aead_decrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
157 - size_t aad_len, u8 *data,
158 - size_t data_len, u8 *mic);
160 -void aead_key_free(struct crypto_aead *tfm);
162 -#endif /* _AEAD_API_H */
163 --- a/net/mac80211/aes_ccm.h
164 +++ b/net/mac80211/aes_ccm.h
169 -#include "aead_api.h"
170 +#include <linux/crypto.h>
172 -#define CCM_AAD_LEN 32
174 -static inline struct crypto_aead *
175 -ieee80211_aes_key_setup_encrypt(const u8 key[], size_t key_len, size_t mic_len)
177 - return aead_key_setup_encrypt("ccm(aes)", key, key_len, mic_len);
181 -ieee80211_aes_ccm_encrypt(struct crypto_aead *tfm,
182 - u8 *b_0, u8 *aad, u8 *data,
183 - size_t data_len, u8 *mic)
185 - return aead_encrypt(tfm, b_0, aad + 2,
186 - be16_to_cpup((__be16 *)aad),
187 - data, data_len, mic);
191 -ieee80211_aes_ccm_decrypt(struct crypto_aead *tfm,
192 - u8 *b_0, u8 *aad, u8 *data,
193 - size_t data_len, u8 *mic)
195 - return aead_decrypt(tfm, b_0, aad + 2,
196 - be16_to_cpup((__be16 *)aad),
197 - data, data_len, mic);
200 -static inline void ieee80211_aes_key_free(struct crypto_aead *tfm)
202 - return aead_key_free(tfm);
204 +struct crypto_cipher *ieee80211_aes_key_setup_encrypt(const u8 key[],
207 +void ieee80211_aes_ccm_encrypt(struct crypto_cipher *tfm, u8 *b_0, u8 *aad,
208 + u8 *data, size_t data_len, u8 *mic,
210 +int ieee80211_aes_ccm_decrypt(struct crypto_cipher *tfm, u8 *b_0, u8 *aad,
211 + u8 *data, size_t data_len, u8 *mic,
213 +void ieee80211_aes_key_free(struct crypto_cipher *tfm);
215 #endif /* AES_CCM_H */
217 +++ b/net/mac80211/aes_gcm.c
220 + * Copyright 2014-2015, Qualcomm Atheros, Inc.
222 + * This program is free software; you can redistribute it and/or modify
223 + * it under the terms of the GNU General Public License version 2 as
224 + * published by the Free Software Foundation.
227 +#include <linux/kernel.h>
228 +#include <linux/types.h>
229 +#include <linux/err.h>
230 +#include <crypto/aead.h>
232 +#include <net/mac80211.h>
234 +#include "aes_gcm.h"
236 +int ieee80211_aes_gcm_encrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad,
237 + u8 *data, size_t data_len, u8 *mic)
239 + struct scatterlist sg[3];
240 + struct aead_request *aead_req;
241 + int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm);
244 + aead_req = kzalloc(reqsize + GCM_AAD_LEN, GFP_ATOMIC);
248 + __aad = (u8 *)aead_req + reqsize;
249 + memcpy(__aad, aad, GCM_AAD_LEN);
251 + sg_init_table(sg, 3);
252 + sg_set_buf(&sg[0], &__aad[2], be16_to_cpup((__be16 *)__aad));
253 + sg_set_buf(&sg[1], data, data_len);
254 + sg_set_buf(&sg[2], mic, IEEE80211_GCMP_MIC_LEN);
256 + aead_request_set_tfm(aead_req, tfm);
257 + aead_request_set_crypt(aead_req, sg, sg, data_len, j_0);
258 + aead_request_set_ad(aead_req, sg[0].length);
260 + crypto_aead_encrypt(aead_req);
265 +int ieee80211_aes_gcm_decrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad,
266 + u8 *data, size_t data_len, u8 *mic)
268 + struct scatterlist sg[3];
269 + struct aead_request *aead_req;
270 + int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm);
277 + aead_req = kzalloc(reqsize + GCM_AAD_LEN, GFP_ATOMIC);
281 + __aad = (u8 *)aead_req + reqsize;
282 + memcpy(__aad, aad, GCM_AAD_LEN);
284 + sg_init_table(sg, 3);
285 + sg_set_buf(&sg[0], &__aad[2], be16_to_cpup((__be16 *)__aad));
286 + sg_set_buf(&sg[1], data, data_len);
287 + sg_set_buf(&sg[2], mic, IEEE80211_GCMP_MIC_LEN);
289 + aead_request_set_tfm(aead_req, tfm);
290 + aead_request_set_crypt(aead_req, sg, sg,
291 + data_len + IEEE80211_GCMP_MIC_LEN, j_0);
292 + aead_request_set_ad(aead_req, sg[0].length);
294 + err = crypto_aead_decrypt(aead_req);
300 +struct crypto_aead *ieee80211_aes_gcm_key_setup_encrypt(const u8 key[],
303 + struct crypto_aead *tfm;
306 + tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
310 + err = crypto_aead_setkey(tfm, key, key_len);
313 + err = crypto_aead_setauthsize(tfm, IEEE80211_GCMP_MIC_LEN);
320 + crypto_free_aead(tfm);
321 + return ERR_PTR(err);
324 +void ieee80211_aes_gcm_key_free(struct crypto_aead *tfm)
326 + crypto_free_aead(tfm);
328 --- a/net/mac80211/aes_gcm.h
329 +++ b/net/mac80211/aes_gcm.h
334 -#include "aead_api.h"
335 +#include <linux/crypto.h>
337 -#define GCM_AAD_LEN 32
339 -static inline int ieee80211_aes_gcm_encrypt(struct crypto_aead *tfm,
340 - u8 *j_0, u8 *aad, u8 *data,
341 - size_t data_len, u8 *mic)
343 +ieee80211_aes_gcm_encrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad,
344 + u8 *data, size_t data_len, u8 *mic)
346 - return aead_encrypt(tfm, j_0, aad + 2,
347 - be16_to_cpup((__be16 *)aad),
348 - data, data_len, mic);
351 -static inline int ieee80211_aes_gcm_decrypt(struct crypto_aead *tfm,
352 - u8 *j_0, u8 *aad, u8 *data,
353 - size_t data_len, u8 *mic)
355 +ieee80211_aes_gcm_decrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad,
356 + u8 *data, size_t data_len, u8 *mic)
358 - return aead_decrypt(tfm, j_0, aad + 2,
359 - be16_to_cpup((__be16 *)aad),
360 - data, data_len, mic);
361 + return -EOPNOTSUPP;
364 static inline struct crypto_aead *
365 ieee80211_aes_gcm_key_setup_encrypt(const u8 key[], size_t key_len)
367 - return aead_key_setup_encrypt("gcm(aes)", key,
368 - key_len, IEEE80211_GCMP_MIC_LEN);
372 -static inline void ieee80211_aes_gcm_key_free(struct crypto_aead *tfm)
374 +ieee80211_aes_gcm_key_free(struct crypto_aead *tfm)
376 - return aead_key_free(tfm);
379 #endif /* AES_GCM_H */
380 --- a/net/mac80211/wpa.c
381 +++ b/net/mac80211/wpa.c
382 @@ -311,7 +311,8 @@ ieee80211_crypto_tkip_decrypt(struct iee
386 -static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad)
387 +static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad,
391 int a4_included, mgmt;
392 @@ -341,14 +342,8 @@ static void ccmp_special_blocks(struct s
396 - /* In CCM, the initial vectors (IV) used for CTR mode encryption and CBC
397 - * mode authentication are not allowed to collide, yet both are derived
398 - * from this vector b_0. We only set L := 1 here to indicate that the
399 - * data size can be represented in (L+1) bytes. The CCM layer will take
400 - * care of storing the data length in the top (L+1) bytes and setting
401 - * and clearing the other bits as is required to derive the two IVs.
404 + /* First block, b_0 */
405 + b_0[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
407 /* Nonce: Nonce Flags | A2 | PN
408 * Nonce Flags: Priority (b0..b3) | Management (b4) | Reserved (b5..b7)
409 @@ -356,6 +351,8 @@ static void ccmp_special_blocks(struct s
410 b_0[1] = qos_tid | (mgmt << 4);
411 memcpy(&b_0[2], hdr->addr2, ETH_ALEN);
412 memcpy(&b_0[8], pn, IEEE80211_CCMP_PN_LEN);
414 + put_unaligned_be16(data_len, &b_0[14]);
416 /* AAD (extra authenticate-only data) / masked 802.11 header
417 * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
418 @@ -412,7 +409,7 @@ static int ccmp_encrypt_skb(struct ieee8
422 - u8 aad[CCM_AAD_LEN];
423 + u8 aad[2 * AES_BLOCK_SIZE];
424 u8 b_0[AES_BLOCK_SIZE];
426 if (info->control.hw_key &&
427 @@ -467,9 +464,11 @@ static int ccmp_encrypt_skb(struct ieee8
430 pos += IEEE80211_CCMP_HDR_LEN;
431 - ccmp_special_blocks(skb, pn, b_0, aad);
432 - return ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, b_0, aad, pos, len,
433 - skb_put(skb, mic_len));
434 + ccmp_special_blocks(skb, pn, b_0, aad, len);
435 + ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, b_0, aad, pos, len,
436 + skb_put(skb, mic_len), mic_len);
442 @@ -542,13 +541,13 @@ ieee80211_crypto_ccmp_decrypt(struct iee
443 u8 aad[2 * AES_BLOCK_SIZE];
444 u8 b_0[AES_BLOCK_SIZE];
445 /* hardware didn't decrypt/verify MIC */
446 - ccmp_special_blocks(skb, pn, b_0, aad);
447 + ccmp_special_blocks(skb, pn, b_0, aad, data_len);
449 if (ieee80211_aes_ccm_decrypt(
450 key->u.ccmp.tfm, b_0, aad,
451 skb->data + hdrlen + IEEE80211_CCMP_HDR_LEN,
453 - skb->data + skb->len - mic_len))
454 + skb->data + skb->len - mic_len, mic_len))
455 return RX_DROP_UNUSABLE;
458 @@ -643,7 +642,7 @@ static int gcmp_encrypt_skb(struct ieee8
462 - u8 aad[GCM_AAD_LEN];
463 + u8 aad[2 * AES_BLOCK_SIZE];
464 u8 j_0[AES_BLOCK_SIZE];
466 if (info->control.hw_key &&
467 @@ -700,8 +699,10 @@ static int gcmp_encrypt_skb(struct ieee8
469 pos += IEEE80211_GCMP_HDR_LEN;
470 gcmp_special_blocks(skb, pn, j_0, aad);
471 - return ieee80211_aes_gcm_encrypt(key->u.gcmp.tfm, j_0, aad, pos, len,
472 - skb_put(skb, IEEE80211_GCMP_MIC_LEN));
473 + ieee80211_aes_gcm_encrypt(key->u.gcmp.tfm, j_0, aad, pos, len,
474 + skb_put(skb, IEEE80211_GCMP_MIC_LEN));
480 @@ -1128,9 +1129,9 @@ ieee80211_crypto_aes_gmac_encrypt(struct
481 struct ieee80211_key *key = tx->key;
482 struct ieee80211_mmie_16 *mmie;
483 struct ieee80211_hdr *hdr;
484 - u8 aad[GMAC_AAD_LEN];
487 - u8 nonce[GMAC_NONCE_LEN];
490 if (WARN_ON(skb_queue_len(&tx->skbs) != 1))
492 @@ -1176,7 +1177,7 @@ ieee80211_crypto_aes_gmac_decrypt(struct
493 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
494 struct ieee80211_key *key = rx->key;
495 struct ieee80211_mmie_16 *mmie;
496 - u8 aad[GMAC_AAD_LEN], *mic, ipn[6], nonce[GMAC_NONCE_LEN];
497 + u8 aad[20], *mic, ipn[6], nonce[12];
498 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
500 if (!ieee80211_is_mgmt(hdr->frame_control))
502 +++ b/net/mac80211/aes_ccm.c
505 + * Copyright 2003-2004, Instant802 Networks, Inc.
506 + * Copyright 2005-2006, Devicescape Software, Inc.
508 + * Rewrite: Copyright (C) 2013 Linaro Ltd <ard.biesheuvel@linaro.org>
510 + * This program is free software; you can redistribute it and/or modify
511 + * it under the terms of the GNU General Public License version 2 as
512 + * published by the Free Software Foundation.
515 +#include <linux/kernel.h>
516 +#include <linux/types.h>
517 +#include <linux/err.h>
518 +#include <crypto/aead.h>
519 +#include <crypto/aes.h>
521 +#include <net/mac80211.h>
523 +#include "aes_ccm.h"
525 +static void aes_ccm_prepare(struct crypto_cipher *tfm, u8 *b_0, u8 *aad, u8 *s_0,
530 + crypto_cipher_encrypt_one(tfm, b, b_0);
532 + /* Extra Authenticate-only data (always two AES blocks) */
533 + for (i = 0; i < AES_BLOCK_SIZE; i++)
535 + crypto_cipher_encrypt_one(tfm, b, aad);
537 + aad += AES_BLOCK_SIZE;
539 + for (i = 0; i < AES_BLOCK_SIZE; i++)
541 + crypto_cipher_encrypt_one(tfm, a, aad);
543 + /* Mask out bits from auth-only-b_0 */
546 + /* S_0 is used to encrypt T (= MIC) */
549 + crypto_cipher_encrypt_one(tfm, s_0, b_0);
553 +void ieee80211_aes_ccm_encrypt(struct crypto_cipher *tfm, u8 *b_0, u8 *aad,
554 + u8 *data, size_t data_len, u8 *mic,
557 + int i, j, last_len, num_blocks;
558 + u8 b[AES_BLOCK_SIZE];
559 + u8 s_0[AES_BLOCK_SIZE];
560 + u8 e[AES_BLOCK_SIZE];
563 + num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_SIZE);
564 + last_len = data_len % AES_BLOCK_SIZE;
565 + aes_ccm_prepare(tfm, b_0, aad, s_0, b, b);
567 + /* Process payload blocks */
570 + for (j = 1; j <= num_blocks; j++) {
571 + int blen = (j == num_blocks && last_len) ?
572 + last_len : AES_BLOCK_SIZE;
574 + /* Authentication followed by encryption */
575 + for (i = 0; i < blen; i++)
577 + crypto_cipher_encrypt_one(tfm, b, b);
579 + b_0[14] = (j >> 8) & 0xff;
580 + b_0[15] = j & 0xff;
581 + crypto_cipher_encrypt_one(tfm, e, b_0);
582 + for (i = 0; i < blen; i++)
583 + *cpos++ = *pos++ ^ e[i];
586 + for (i = 0; i < mic_len; i++)
587 + mic[i] = b[i] ^ s_0[i];
590 +int ieee80211_aes_ccm_decrypt(struct crypto_cipher *tfm, u8 *b_0, u8 *aad,
591 + u8 *data, size_t data_len, u8 *mic,
594 + int i, j, last_len, num_blocks;
596 + u8 a[AES_BLOCK_SIZE];
597 + u8 b[AES_BLOCK_SIZE];
598 + u8 s_0[AES_BLOCK_SIZE];
600 + num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_SIZE);
601 + last_len = data_len % AES_BLOCK_SIZE;
602 + aes_ccm_prepare(tfm, b_0, aad, s_0, a, b);
604 + /* Process payload blocks */
607 + for (j = 1; j <= num_blocks; j++) {
608 + int blen = (j == num_blocks && last_len) ?
609 + last_len : AES_BLOCK_SIZE;
611 + /* Decryption followed by authentication */
612 + b_0[14] = (j >> 8) & 0xff;
613 + b_0[15] = j & 0xff;
614 + crypto_cipher_encrypt_one(tfm, b, b_0);
615 + for (i = 0; i < blen; i++) {
616 + *pos = *cpos++ ^ b[i];
619 + crypto_cipher_encrypt_one(tfm, a, a);
622 + for (i = 0; i < mic_len; i++) {
623 + if ((mic[i] ^ s_0[i]) != a[i])
630 +struct crypto_cipher *ieee80211_aes_key_setup_encrypt(const u8 key[],
634 + struct crypto_cipher *tfm;
636 + tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
638 + crypto_cipher_setkey(tfm, key, key_len);
644 +void ieee80211_aes_key_free(struct crypto_cipher *tfm)
646 + crypto_free_cipher(tfm);
648 --- a/net/mac80211/Kconfig
649 +++ b/net/mac80211/Kconfig
650 @@ -6,8 +6,6 @@ config MAC80211
652 select BPAUTO_CRYPTO_LIB_ARC4
653 depends on CRYPTO_AES
654 - depends on CRYPTO_CCM
655 - depends on CRYPTO_GCM
656 depends on CRYPTO_CMAC
659 --- a/net/mac80211/aes_gmac.h
660 +++ b/net/mac80211/aes_gmac.h
662 #define GMAC_MIC_LEN 16
663 #define GMAC_NONCE_LEN 12
665 -struct crypto_aead *ieee80211_aes_gmac_key_setup(const u8 key[],
667 -int ieee80211_aes_gmac(struct crypto_aead *tfm, const u8 *aad, u8 *nonce,
668 - const u8 *data, size_t data_len, u8 *mic);
669 -void ieee80211_aes_gmac_key_free(struct crypto_aead *tfm);
670 +static inline struct crypto_aead *
671 +ieee80211_aes_gmac_key_setup(const u8 key[], size_t key_len)
677 +ieee80211_aes_gmac(struct crypto_aead *tfm, const u8 *aad, u8 *nonce,
678 + const u8 *data, size_t data_len, u8 *mic)
680 + return -EOPNOTSUPP;
684 +ieee80211_aes_gmac_key_free(struct crypto_aead *tfm)
688 #endif /* AES_GMAC_H */
689 --- a/net/mac80211/key.h
690 +++ b/net/mac80211/key.h
691 @@ -89,7 +89,7 @@ struct ieee80211_key {
694 u8 rx_pn[IEEE80211_NUM_TIDS + 1][IEEE80211_CCMP_PN_LEN];
695 - struct crypto_aead *tfm;
696 + struct crypto_cipher *tfm;
697 u32 replays; /* dot11RSNAStatsCCMPReplays */