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