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