1 --- a/net/mac80211/Kconfig
2 +++ b/net/mac80211/Kconfig
3 @@ -5,7 +5,6 @@ config MAC80211
7 - select BACKPORT_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)
31 + u8 *b_0, *aad, *b, *s_0;
33 + b_0 = scratch + 3 * AES_BLOCK_SIZE;
34 + aad = scratch + 4 * AES_BLOCK_SIZE;
36 + s_0 = scratch + AES_BLOCK_SIZE;
38 + crypto_cipher_encrypt_one(tfm, b, b_0);
40 + /* Extra Authenticate-only data (always two AES blocks) */
41 + for (i = 0; i < AES_BLOCK_SIZE; i++)
43 + crypto_cipher_encrypt_one(tfm, b, aad);
45 + aad += AES_BLOCK_SIZE;
47 + for (i = 0; i < AES_BLOCK_SIZE; i++)
49 + crypto_cipher_encrypt_one(tfm, a, aad);
51 + /* Mask out bits from auth-only-b_0 */
54 + /* S_0 is used to encrypt T (= MIC) */
57 + crypto_cipher_encrypt_one(tfm, s_0, b_0);
61 +void ieee80211_aes_ccm_encrypt(struct crypto_cipher *tfm, u8 *scratch,
62 + u8 *data, size_t data_len,
65 - struct scatterlist assoc, pt, ct[2];
66 + int i, j, last_len, num_blocks;
67 + u8 *pos, *cpos, *b, *s_0, *e, *b_0;
69 - char aead_req_data[sizeof(struct aead_request) +
70 - crypto_aead_reqsize(tfm)]
71 - __aligned(__alignof__(struct aead_request));
72 - struct aead_request *aead_req = (void *) aead_req_data;
74 - memset(aead_req, 0, sizeof(aead_req_data));
76 - sg_init_one(&pt, data, data_len);
77 - sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad));
78 - sg_init_table(ct, 2);
79 - sg_set_buf(&ct[0], data, data_len);
80 - sg_set_buf(&ct[1], mic, IEEE80211_CCMP_MIC_LEN);
82 - aead_request_set_tfm(aead_req, tfm);
83 - aead_request_set_assoc(aead_req, &assoc, assoc.length);
84 - aead_request_set_crypt(aead_req, &pt, ct, data_len, b_0);
86 + s_0 = scratch + AES_BLOCK_SIZE;
87 + e = scratch + 2 * AES_BLOCK_SIZE;
88 + b_0 = scratch + 3 * AES_BLOCK_SIZE;
90 + num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_SIZE);
91 + last_len = data_len % AES_BLOCK_SIZE;
92 + aes_ccm_prepare(tfm, scratch, b);
94 + /* Process payload blocks */
97 + for (j = 1; j <= num_blocks; j++) {
98 + int blen = (j == num_blocks && last_len) ?
99 + last_len : AES_BLOCK_SIZE;
101 + /* Authentication followed by encryption */
102 + for (i = 0; i < blen; i++)
104 + crypto_cipher_encrypt_one(tfm, b, b);
106 + b_0[14] = (j >> 8) & 0xff;
107 + b_0[15] = j & 0xff;
108 + crypto_cipher_encrypt_one(tfm, e, b_0);
109 + for (i = 0; i < blen; i++)
110 + *cpos++ = *pos++ ^ e[i];
113 - crypto_aead_encrypt(aead_req);
114 + for (i = 0; i < IEEE80211_CCMP_MIC_LEN; i++)
115 + mic[i] = b[i] ^ s_0[i];
118 -int ieee80211_aes_ccm_decrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
119 - u8 *data, size_t data_len, u8 *mic)
121 +int ieee80211_aes_ccm_decrypt(struct crypto_cipher *tfm, u8 *scratch,
122 + u8 *cdata, size_t data_len, u8 *mic, u8 *data)
124 - struct scatterlist assoc, pt, ct[2];
125 - char aead_req_data[sizeof(struct aead_request) +
126 - crypto_aead_reqsize(tfm)]
127 - __aligned(__alignof__(struct aead_request));
128 - struct aead_request *aead_req = (void *) aead_req_data;
130 - memset(aead_req, 0, sizeof(aead_req_data));
132 - sg_init_one(&pt, data, data_len);
133 - sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad));
134 - sg_init_table(ct, 2);
135 - sg_set_buf(&ct[0], data, data_len);
136 - sg_set_buf(&ct[1], mic, IEEE80211_CCMP_MIC_LEN);
138 - aead_request_set_tfm(aead_req, tfm);
139 - aead_request_set_assoc(aead_req, &assoc, assoc.length);
140 - aead_request_set_crypt(aead_req, ct, &pt,
141 - data_len + IEEE80211_CCMP_MIC_LEN, b_0);
142 + int i, j, last_len, num_blocks;
143 + u8 *pos, *cpos, *b, *s_0, *a, *b_0;
146 + s_0 = scratch + AES_BLOCK_SIZE;
147 + a = scratch + 2 * AES_BLOCK_SIZE;
148 + b_0 = scratch + 3 * AES_BLOCK_SIZE;
150 + num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_SIZE);
151 + last_len = data_len % AES_BLOCK_SIZE;
152 + aes_ccm_prepare(tfm, scratch, a);
154 + /* Process payload blocks */
157 + for (j = 1; j <= num_blocks; j++) {
158 + int blen = (j == num_blocks && last_len) ?
159 + last_len : AES_BLOCK_SIZE;
161 + /* Decryption followed by authentication */
162 + b_0[14] = (j >> 8) & 0xff;
163 + b_0[15] = j & 0xff;
164 + crypto_cipher_encrypt_one(tfm, b, b_0);
165 + for (i = 0; i < blen; i++) {
166 + *pos = *cpos++ ^ b[i];
169 + crypto_cipher_encrypt_one(tfm, a, a);
172 + for (i = 0; i < IEEE80211_CCMP_MIC_LEN; i++) {
173 + if ((mic[i] ^ s_0[i]) != a[i])
177 - return crypto_aead_decrypt(aead_req);
181 -struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[])
183 +struct crypto_cipher *ieee80211_aes_key_setup_encrypt(const u8 key[])
185 - struct crypto_aead *tfm;
187 + struct crypto_cipher *tfm;
189 - tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC);
193 - err = crypto_aead_setkey(tfm, key, WLAN_KEY_LEN_CCMP);
195 - err = crypto_aead_setauthsize(tfm, IEEE80211_CCMP_MIC_LEN);
198 + tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
200 + crypto_cipher_setkey(tfm, key, WLAN_KEY_LEN_CCMP);
202 - crypto_free_aead(tfm);
203 - return ERR_PTR(err);
207 -void ieee80211_aes_key_free(struct crypto_aead *tfm)
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 @@ -84,7 +84,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 @@ -304,15 +304,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 @@ -334,21 +341,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 @@ -404,8 +410,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 @@ -461,9 +466,9 @@ static int ccmp_encrypt_skb(struct ieee8
316 pos += IEEE80211_CCMP_HDR_LEN;
317 - ccmp_special_blocks(skb, pn, b_0, aad);
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 @@ -526,16 +531,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);
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;