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