1 --- a/net/mac80211/Kconfig
2 +++ b/net/mac80211/Kconfig
3 @@ -5,7 +5,6 @@ config MAC80211
7 - depends on CRYPTO_CCM
9 select BACKPORT_AVERAGE
11 --- a/net/mac80211/aes_ccm.c
12 +++ b/net/mac80211/aes_ccm.c
14 * Copyright 2003-2004, Instant802 Networks, Inc.
15 * Copyright 2005-2006, Devicescape Software, Inc.
17 - * Rewrite: Copyright (C) 2013 Linaro Ltd <ard.biesheuvel@linaro.org>
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License version 2 as
21 * published by the Free Software Foundation.
26 -void ieee80211_aes_ccm_encrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
27 - u8 *data, size_t data_len, u8 *mic)
28 +static void aes_ccm_prepare(struct crypto_cipher *tfm, u8 *scratch, u8 *a)
30 - struct scatterlist assoc, pt, ct[2];
32 - struct aead_request req;
33 - u8 priv[crypto_aead_reqsize(tfm)];
36 - memset(&aead_req, 0, sizeof(aead_req));
38 - sg_init_one(&pt, data, data_len);
39 - sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad));
40 - sg_init_table(ct, 2);
41 - sg_set_buf(&ct[0], data, data_len);
42 - sg_set_buf(&ct[1], mic, IEEE80211_CCMP_MIC_LEN);
44 - aead_request_set_tfm(&aead_req.req, tfm);
45 - aead_request_set_assoc(&aead_req.req, &assoc, assoc.length);
46 - aead_request_set_crypt(&aead_req.req, &pt, ct, data_len, b_0);
48 + u8 *b_0, *aad, *b, *s_0;
50 - crypto_aead_encrypt(&aead_req.req);
51 + b_0 = scratch + 3 * AES_BLOCK_SIZE;
52 + aad = scratch + 4 * AES_BLOCK_SIZE;
54 + s_0 = scratch + AES_BLOCK_SIZE;
56 + crypto_cipher_encrypt_one(tfm, b, b_0);
58 + /* Extra Authenticate-only data (always two AES blocks) */
59 + for (i = 0; i < AES_BLOCK_SIZE; i++)
61 + crypto_cipher_encrypt_one(tfm, b, aad);
63 + aad += AES_BLOCK_SIZE;
65 + for (i = 0; i < AES_BLOCK_SIZE; i++)
67 + crypto_cipher_encrypt_one(tfm, a, aad);
69 + /* Mask out bits from auth-only-b_0 */
72 + /* S_0 is used to encrypt T (= MIC) */
75 + crypto_cipher_encrypt_one(tfm, s_0, b_0);
78 -int ieee80211_aes_ccm_decrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
79 - u8 *data, size_t data_len, u8 *mic)
81 +void ieee80211_aes_ccm_encrypt(struct crypto_cipher *tfm, u8 *scratch,
82 + u8 *data, size_t data_len,
85 - struct scatterlist assoc, pt, ct[2];
87 - struct aead_request req;
88 - u8 priv[crypto_aead_reqsize(tfm)];
91 - memset(&aead_req, 0, sizeof(aead_req));
93 - sg_init_one(&pt, data, data_len);
94 - sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad));
95 - sg_init_table(ct, 2);
96 - sg_set_buf(&ct[0], data, data_len);
97 - sg_set_buf(&ct[1], mic, IEEE80211_CCMP_MIC_LEN);
99 - aead_request_set_tfm(&aead_req.req, tfm);
100 - aead_request_set_assoc(&aead_req.req, &assoc, assoc.length);
101 - aead_request_set_crypt(&aead_req.req, ct, &pt,
102 - data_len + IEEE80211_CCMP_MIC_LEN, b_0);
103 + int i, j, last_len, num_blocks;
104 + u8 *pos, *cpos, *b, *s_0, *e, *b_0;
107 + s_0 = scratch + AES_BLOCK_SIZE;
108 + e = scratch + 2 * AES_BLOCK_SIZE;
109 + b_0 = scratch + 3 * AES_BLOCK_SIZE;
111 + num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_SIZE);
112 + last_len = data_len % AES_BLOCK_SIZE;
113 + aes_ccm_prepare(tfm, scratch, b);
115 + /* Process payload blocks */
118 + for (j = 1; j <= num_blocks; j++) {
119 + int blen = (j == num_blocks && last_len) ?
120 + last_len : AES_BLOCK_SIZE;
122 + /* Authentication followed by encryption */
123 + for (i = 0; i < blen; i++)
125 + crypto_cipher_encrypt_one(tfm, b, b);
127 + b_0[14] = (j >> 8) & 0xff;
128 + b_0[15] = j & 0xff;
129 + crypto_cipher_encrypt_one(tfm, e, b_0);
130 + for (i = 0; i < blen; i++)
131 + *cpos++ = *pos++ ^ e[i];
134 - return crypto_aead_decrypt(&aead_req.req);
135 + for (i = 0; i < IEEE80211_CCMP_MIC_LEN; i++)
136 + mic[i] = b[i] ^ s_0[i];
139 -struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[])
141 +int ieee80211_aes_ccm_decrypt(struct crypto_cipher *tfm, u8 *scratch,
142 + u8 *cdata, size_t data_len, u8 *mic, u8 *data)
144 - struct crypto_aead *tfm;
146 + int i, j, last_len, num_blocks;
147 + u8 *pos, *cpos, *b, *s_0, *a, *b_0;
149 - tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC);
153 - err = crypto_aead_setkey(tfm, key, WLAN_KEY_LEN_CCMP);
155 - err = crypto_aead_setauthsize(tfm, IEEE80211_CCMP_MIC_LEN);
159 + s_0 = scratch + AES_BLOCK_SIZE;
160 + a = scratch + 2 * AES_BLOCK_SIZE;
161 + b_0 = scratch + 3 * AES_BLOCK_SIZE;
163 + num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_SIZE);
164 + last_len = data_len % AES_BLOCK_SIZE;
165 + aes_ccm_prepare(tfm, scratch, a);
167 + /* Process payload blocks */
170 + for (j = 1; j <= num_blocks; j++) {
171 + int blen = (j == num_blocks && last_len) ?
172 + last_len : AES_BLOCK_SIZE;
174 + /* Decryption followed by authentication */
175 + b_0[14] = (j >> 8) & 0xff;
176 + b_0[15] = j & 0xff;
177 + crypto_cipher_encrypt_one(tfm, b, b_0);
178 + for (i = 0; i < blen; i++) {
179 + *pos = *cpos++ ^ b[i];
182 + crypto_cipher_encrypt_one(tfm, a, a);
185 + for (i = 0; i < IEEE80211_CCMP_MIC_LEN; i++) {
186 + if ((mic[i] ^ s_0[i]) != a[i])
190 - crypto_free_aead(tfm);
191 - return ERR_PTR(err);
195 -void ieee80211_aes_key_free(struct crypto_aead *tfm)
197 +struct crypto_cipher *ieee80211_aes_key_setup_encrypt(const u8 key[])
199 + struct crypto_cipher *tfm;
201 + tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
203 + crypto_cipher_setkey(tfm, key, WLAN_KEY_LEN_CCMP);
209 +void ieee80211_aes_key_free(struct crypto_cipher *tfm)
211 - crypto_free_aead(tfm);
212 + crypto_free_cipher(tfm);
214 --- a/net/mac80211/aes_ccm.h
215 +++ b/net/mac80211/aes_ccm.h
218 #include <linux/crypto.h>
220 -struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[]);
221 -void ieee80211_aes_ccm_encrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
222 - u8 *data, size_t data_len, u8 *mic);
223 -int ieee80211_aes_ccm_decrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
224 - u8 *data, size_t data_len, u8 *mic);
225 -void ieee80211_aes_key_free(struct crypto_aead *tfm);
226 +struct crypto_cipher *ieee80211_aes_key_setup_encrypt(const u8 key[]);
227 +void ieee80211_aes_ccm_encrypt(struct crypto_cipher *tfm, u8 *scratch,
228 + u8 *data, size_t data_len,
229 + u8 *cdata, u8 *mic);
230 +int ieee80211_aes_ccm_decrypt(struct crypto_cipher *tfm, u8 *scratch,
231 + u8 *cdata, size_t data_len,
232 + u8 *mic, u8 *data);
233 +void ieee80211_aes_key_free(struct crypto_cipher *tfm);
235 #endif /* AES_CCM_H */
236 --- a/net/mac80211/key.h
237 +++ b/net/mac80211/key.h
238 @@ -83,7 +83,7 @@ struct ieee80211_key {
241 u8 rx_pn[IEEE80211_NUM_TIDS + 1][IEEE80211_CCMP_PN_LEN];
242 - struct crypto_aead *tfm;
243 + struct crypto_cipher *tfm;
244 u32 replays; /* dot11RSNAStatsCCMPReplays */
247 --- a/net/mac80211/wpa.c
248 +++ b/net/mac80211/wpa.c
249 @@ -301,16 +301,22 @@ ieee80211_crypto_tkip_decrypt(struct iee
253 -static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad,
254 +static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *scratch,
258 int a4_included, mgmt;
262 + u16 data_len, len_a;
264 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
266 + memset(scratch, 0, 6 * AES_BLOCK_SIZE);
268 + b_0 = scratch + 3 * AES_BLOCK_SIZE;
269 + aad = scratch + 4 * AES_BLOCK_SIZE;
272 * Mask FC: zero subtype b4 b5 b6 (if not mgmt)
273 * Retry, PwrMgt, MoreData; set Protected
274 @@ -332,21 +338,20 @@ static void ccmp_special_blocks(struct s
278 - /* In CCM, the initial vectors (IV) used for CTR mode encryption and CBC
279 - * mode authentication are not allowed to collide, yet both are derived
280 - * from this vector b_0. We only set L := 1 here to indicate that the
281 - * data size can be represented in (L+1) bytes. The CCM layer will take
282 - * care of storing the data length in the top (L+1) bytes and setting
283 - * and clearing the other bits as is required to derive the two IVs.
286 + data_len = skb->len - hdrlen - IEEE80211_CCMP_HDR_LEN;
288 + data_len -= IEEE80211_CCMP_MIC_LEN;
290 + /* First block, b_0 */
291 + b_0[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
292 /* Nonce: Nonce Flags | A2 | PN
293 * Nonce Flags: Priority (b0..b3) | Management (b4) | Reserved (b5..b7)
295 b_0[1] = qos_tid | (mgmt << 4);
296 memcpy(&b_0[2], hdr->addr2, ETH_ALEN);
297 memcpy(&b_0[8], pn, IEEE80211_CCMP_PN_LEN);
299 + put_unaligned_be16(data_len, &b_0[14]);
301 /* AAD (extra authenticate-only data) / masked 802.11 header
302 * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
303 @@ -402,8 +407,7 @@ static int ccmp_encrypt_skb(struct ieee8
307 - u8 aad[2 * AES_BLOCK_SIZE];
308 - u8 b_0[AES_BLOCK_SIZE];
309 + u8 scratch[6 * AES_BLOCK_SIZE];
311 if (info->control.hw_key &&
312 !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) &&
313 @@ -456,9 +460,9 @@ static int ccmp_encrypt_skb(struct ieee8
316 pos += IEEE80211_CCMP_HDR_LEN;
317 - ccmp_special_blocks(skb, pn, b_0, aad, 0);
318 - ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, b_0, aad, pos, len,
319 - skb_put(skb, IEEE80211_CCMP_MIC_LEN));
320 + ccmp_special_blocks(skb, pn, scratch, 0);
321 + ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, scratch, pos, len,
322 + pos, skb_put(skb, IEEE80211_CCMP_MIC_LEN));
326 @@ -521,16 +525,16 @@ ieee80211_crypto_ccmp_decrypt(struct iee
329 if (!(status->flag & RX_FLAG_DECRYPTED)) {
330 - u8 aad[2 * AES_BLOCK_SIZE];
331 - u8 b_0[AES_BLOCK_SIZE];
332 + u8 scratch[6 * AES_BLOCK_SIZE];
333 /* hardware didn't decrypt/verify MIC */
334 - ccmp_special_blocks(skb, pn, b_0, aad, 1);
335 + ccmp_special_blocks(skb, pn, scratch, 1);
337 if (ieee80211_aes_ccm_decrypt(
338 - key->u.ccmp.tfm, b_0, aad,
339 + key->u.ccmp.tfm, scratch,
340 skb->data + hdrlen + IEEE80211_CCMP_HDR_LEN,
342 - skb->data + skb->len - IEEE80211_CCMP_MIC_LEN))
343 + skb->data + skb->len - IEEE80211_CCMP_MIC_LEN,
344 + skb->data + hdrlen + IEEE80211_CCMP_HDR_LEN))
345 return RX_DROP_UNUSABLE;