These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / staging / rtl8192u / ieee80211 / ieee80211_crypt_ccmp.c
1 /*
2  * Host AP crypt: host-based CCMP encryption implementation for Host AP driver
3  *
4  * Copyright (c) 2003-2004, Jouni Malinen <jkmaline@cc.hut.fi>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation. See README and COPYING for
9  * more details.
10  */
11
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/random.h>
16 #include <linux/skbuff.h>
17 #include <linux/netdevice.h>
18 #include <linux/if_ether.h>
19 #include <linux/if_arp.h>
20 #include <linux/string.h>
21 #include <linux/wireless.h>
22
23 #include "ieee80211.h"
24
25 #include <linux/crypto.h>
26     #include <linux/scatterlist.h>
27
28 MODULE_AUTHOR("Jouni Malinen");
29 MODULE_DESCRIPTION("Host AP crypt: CCMP");
30 MODULE_LICENSE("GPL");
31
32 #define AES_BLOCK_LEN 16
33 #define CCMP_HDR_LEN 8
34 #define CCMP_MIC_LEN 8
35 #define CCMP_TK_LEN 16
36 #define CCMP_PN_LEN 6
37
38 struct ieee80211_ccmp_data {
39         u8 key[CCMP_TK_LEN];
40         int key_set;
41
42         u8 tx_pn[CCMP_PN_LEN];
43         u8 rx_pn[CCMP_PN_LEN];
44
45         u32 dot11RSNAStatsCCMPFormatErrors;
46         u32 dot11RSNAStatsCCMPReplays;
47         u32 dot11RSNAStatsCCMPDecryptErrors;
48
49         int key_idx;
50
51         struct crypto_tfm *tfm;
52
53         /* scratch buffers for virt_to_page() (crypto API) */
54         u8 tx_b0[AES_BLOCK_LEN], tx_b[AES_BLOCK_LEN],
55                 tx_e[AES_BLOCK_LEN], tx_s0[AES_BLOCK_LEN];
56         u8 rx_b0[AES_BLOCK_LEN], rx_b[AES_BLOCK_LEN], rx_a[AES_BLOCK_LEN];
57 };
58
59 static void ieee80211_ccmp_aes_encrypt(struct crypto_tfm *tfm,
60                              const u8 pt[16], u8 ct[16])
61 {
62         crypto_cipher_encrypt_one((void *)tfm, ct, pt);
63 }
64
65 static void *ieee80211_ccmp_init(int key_idx)
66 {
67         struct ieee80211_ccmp_data *priv;
68
69         priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
70         if (priv == NULL)
71                 goto fail;
72         priv->key_idx = key_idx;
73
74         priv->tfm = (void *)crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
75         if (IS_ERR(priv->tfm)) {
76                 printk(KERN_DEBUG "ieee80211_crypt_ccmp: could not allocate crypto API aes\n");
77                 priv->tfm = NULL;
78                 goto fail;
79         }
80
81         return priv;
82
83 fail:
84         if (priv) {
85                 if (priv->tfm)
86                         crypto_free_cipher((void *)priv->tfm);
87                 kfree(priv);
88         }
89
90         return NULL;
91 }
92
93
94 static void ieee80211_ccmp_deinit(void *priv)
95 {
96         struct ieee80211_ccmp_data *_priv = priv;
97
98         if (_priv && _priv->tfm)
99                 crypto_free_cipher((void *)_priv->tfm);
100         kfree(priv);
101 }
102
103
104 static inline void xor_block(u8 *b, u8 *a, size_t len)
105 {
106         int i;
107
108         for (i = 0; i < len; i++)
109                 b[i] ^= a[i];
110 }
111
112
113
114 static void ccmp_init_blocks(struct crypto_tfm *tfm,
115                              struct rtl_80211_hdr_4addr *hdr,
116                              u8 *pn, size_t dlen, u8 *b0, u8 *auth,
117                              u8 *s0)
118 {
119         u8 *pos, qc = 0;
120         size_t aad_len;
121         u16 fc;
122         int a4_included, qc_included;
123         u8 aad[2 * AES_BLOCK_LEN];
124
125         fc = le16_to_cpu(hdr->frame_ctl);
126         a4_included = ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
127                        (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS));
128         /* qc_included = ((WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA) &&
129          *             (WLAN_FC_GET_STYPE(fc) & 0x08));
130          */
131         /* fixed by David :2006.9.6 */
132         qc_included = (WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA) &&
133                        (WLAN_FC_GET_STYPE(fc) & 0x80);
134         aad_len = 22;
135         if (a4_included)
136                 aad_len += 6;
137         if (qc_included) {
138                 pos = (u8 *) &hdr->addr4;
139                 if (a4_included)
140                         pos += 6;
141                 qc = *pos & 0x0f;
142                 aad_len += 2;
143         }
144         /* CCM Initial Block:
145          * Flag (Include authentication header, M=3 (8-octet MIC),
146          *       L=1 (2-octet Dlen))
147          * Nonce: 0x00 | A2 | PN
148          * Dlen
149          */
150         b0[0] = 0x59;
151         b0[1] = qc;
152         memcpy(b0 + 2, hdr->addr2, ETH_ALEN);
153         memcpy(b0 + 8, pn, CCMP_PN_LEN);
154         b0[14] = (dlen >> 8) & 0xff;
155         b0[15] = dlen & 0xff;
156
157         /* AAD:
158          * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
159          * A1 | A2 | A3
160          * SC with bits 4..15 (seq#) masked to zero
161          * A4 (if present)
162          * QC (if present)
163          */
164         pos = (u8 *) hdr;
165         aad[0] = 0; /* aad_len >> 8 */
166         aad[1] = aad_len & 0xff;
167         aad[2] = pos[0] & 0x8f;
168         aad[3] = pos[1] & 0xc7;
169         memcpy(aad + 4, hdr->addr1, 3 * ETH_ALEN);
170         pos = (u8 *) &hdr->seq_ctl;
171         aad[22] = pos[0] & 0x0f;
172         aad[23] = 0; /* all bits masked */
173         memset(aad + 24, 0, 8);
174         if (a4_included)
175                 memcpy(aad + 24, hdr->addr4, ETH_ALEN);
176         if (qc_included) {
177                 aad[a4_included ? 30 : 24] = qc;
178                 /* rest of QC masked */
179         }
180
181         /* Start with the first block and AAD */
182         ieee80211_ccmp_aes_encrypt(tfm, b0, auth);
183         xor_block(auth, aad, AES_BLOCK_LEN);
184         ieee80211_ccmp_aes_encrypt(tfm, auth, auth);
185         xor_block(auth, &aad[AES_BLOCK_LEN], AES_BLOCK_LEN);
186         ieee80211_ccmp_aes_encrypt(tfm, auth, auth);
187         b0[0] &= 0x07;
188         b0[14] = b0[15] = 0;
189         ieee80211_ccmp_aes_encrypt(tfm, b0, s0);
190 }
191
192
193
194 static int ieee80211_ccmp_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
195 {
196         struct ieee80211_ccmp_data *key = priv;
197         int data_len, i;
198         u8 *pos;
199         struct rtl_80211_hdr_4addr *hdr;
200         cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
201
202         if (skb_headroom(skb) < CCMP_HDR_LEN ||
203             skb_tailroom(skb) < CCMP_MIC_LEN ||
204             skb->len < hdr_len)
205                 return -1;
206
207         data_len = skb->len - hdr_len;
208         pos = skb_push(skb, CCMP_HDR_LEN);
209         memmove(pos, pos + CCMP_HDR_LEN, hdr_len);
210         pos += hdr_len;
211         /* mic = skb_put(skb, CCMP_MIC_LEN); */
212
213         i = CCMP_PN_LEN - 1;
214         while (i >= 0) {
215                 key->tx_pn[i]++;
216                 if (key->tx_pn[i] != 0)
217                         break;
218                 i--;
219         }
220
221         *pos++ = key->tx_pn[5];
222         *pos++ = key->tx_pn[4];
223         *pos++ = 0;
224         *pos++ = (key->key_idx << 6) | (1 << 5) /* Ext IV included */;
225         *pos++ = key->tx_pn[3];
226         *pos++ = key->tx_pn[2];
227         *pos++ = key->tx_pn[1];
228         *pos++ = key->tx_pn[0];
229
230
231         hdr = (struct rtl_80211_hdr_4addr *) skb->data;
232         if (!tcb_desc->bHwSec) {
233                 int blocks, last, len;
234                 u8 *mic;
235                 u8 *b0 = key->tx_b0;
236                 u8 *b = key->tx_b;
237                 u8 *e = key->tx_e;
238                 u8 *s0 = key->tx_s0;
239
240                 /* mic is moved to here by john */
241                 mic = skb_put(skb, CCMP_MIC_LEN);
242
243                 ccmp_init_blocks(key->tfm, hdr, key->tx_pn, data_len, b0, b, s0);
244
245                 blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN;
246                 last = data_len % AES_BLOCK_LEN;
247
248                 for (i = 1; i <= blocks; i++) {
249                         len = (i == blocks && last) ? last : AES_BLOCK_LEN;
250                         /* Authentication */
251                         xor_block(b, pos, len);
252                         ieee80211_ccmp_aes_encrypt(key->tfm, b, b);
253                         /* Encryption, with counter */
254                         b0[14] = (i >> 8) & 0xff;
255                         b0[15] = i & 0xff;
256                         ieee80211_ccmp_aes_encrypt(key->tfm, b0, e);
257                         xor_block(pos, e, len);
258                         pos += len;
259                 }
260
261                 for (i = 0; i < CCMP_MIC_LEN; i++)
262                         mic[i] = b[i] ^ s0[i];
263         }
264         return 0;
265 }
266
267
268 static int ieee80211_ccmp_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
269 {
270         struct ieee80211_ccmp_data *key = priv;
271         u8 keyidx, *pos;
272         struct rtl_80211_hdr_4addr *hdr;
273         cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
274         u8 pn[6];
275
276         if (skb->len < hdr_len + CCMP_HDR_LEN + CCMP_MIC_LEN) {
277                 key->dot11RSNAStatsCCMPFormatErrors++;
278                 return -1;
279         }
280
281         hdr = (struct rtl_80211_hdr_4addr *) skb->data;
282         pos = skb->data + hdr_len;
283         keyidx = pos[3];
284         if (!(keyidx & (1 << 5))) {
285                 if (net_ratelimit()) {
286                         printk(KERN_DEBUG "CCMP: received packet without ExtIV flag from %pM\n",
287                                 hdr->addr2);
288                 }
289                 key->dot11RSNAStatsCCMPFormatErrors++;
290                 return -2;
291         }
292         keyidx >>= 6;
293         if (key->key_idx != keyidx) {
294                 printk(KERN_DEBUG "CCMP: RX tkey->key_idx=%d frame keyidx=%d priv=%p\n",
295                         key->key_idx, keyidx, priv);
296                 return -6;
297         }
298         if (!key->key_set) {
299                 if (net_ratelimit()) {
300                         printk(KERN_DEBUG "CCMP: received packet from %pM with keyid=%d that does not have a configured key\n",
301                                 hdr->addr2, keyidx);
302                 }
303                 return -3;
304         }
305
306         pn[0] = pos[7];
307         pn[1] = pos[6];
308         pn[2] = pos[5];
309         pn[3] = pos[4];
310         pn[4] = pos[1];
311         pn[5] = pos[0];
312         pos += 8;
313
314         if (memcmp(pn, key->rx_pn, CCMP_PN_LEN) <= 0) {
315                 if (net_ratelimit()) {
316                         printk(KERN_DEBUG "CCMP: replay detected: STA=%pM previous PN %pm received PN %pm\n",
317                                hdr->addr2, key->rx_pn, pn);
318                 }
319                 key->dot11RSNAStatsCCMPReplays++;
320                 return -4;
321         }
322         if (!tcb_desc->bHwSec) {
323                 size_t data_len = skb->len - hdr_len - CCMP_HDR_LEN - CCMP_MIC_LEN;
324                 u8 *mic = skb->data + skb->len - CCMP_MIC_LEN;
325                 u8 *b0 = key->rx_b0;
326                 u8 *b = key->rx_b;
327                 u8 *a = key->rx_a;
328                 int i, blocks, last, len;
329
330
331                 ccmp_init_blocks(key->tfm, hdr, pn, data_len, b0, a, b);
332                 xor_block(mic, b, CCMP_MIC_LEN);
333
334                 blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN;
335                 last = data_len % AES_BLOCK_LEN;
336
337                 for (i = 1; i <= blocks; i++) {
338                         len = (i == blocks && last) ? last : AES_BLOCK_LEN;
339                         /* Decrypt, with counter */
340                         b0[14] = (i >> 8) & 0xff;
341                         b0[15] = i & 0xff;
342                         ieee80211_ccmp_aes_encrypt(key->tfm, b0, b);
343                         xor_block(pos, b, len);
344                         /* Authentication */
345                         xor_block(a, pos, len);
346                         ieee80211_ccmp_aes_encrypt(key->tfm, a, a);
347                         pos += len;
348                 }
349
350                 if (memcmp(mic, a, CCMP_MIC_LEN) != 0) {
351                         if (net_ratelimit()) {
352                                 printk(KERN_DEBUG "CCMP: decrypt failed: STA=%pM\n",
353                                         hdr->addr2);
354                         }
355                         key->dot11RSNAStatsCCMPDecryptErrors++;
356                         return -5;
357                 }
358
359                 memcpy(key->rx_pn, pn, CCMP_PN_LEN);
360         }
361         /* Remove hdr and MIC */
362         memmove(skb->data + CCMP_HDR_LEN, skb->data, hdr_len);
363         skb_pull(skb, CCMP_HDR_LEN);
364         skb_trim(skb, skb->len - CCMP_MIC_LEN);
365
366         return keyidx;
367 }
368
369
370 static int ieee80211_ccmp_set_key(void *key, int len, u8 *seq, void *priv)
371 {
372         struct ieee80211_ccmp_data *data = priv;
373         int keyidx;
374         struct crypto_tfm *tfm = data->tfm;
375
376         keyidx = data->key_idx;
377         memset(data, 0, sizeof(*data));
378         data->key_idx = keyidx;
379         data->tfm = tfm;
380         if (len == CCMP_TK_LEN) {
381                 memcpy(data->key, key, CCMP_TK_LEN);
382                 data->key_set = 1;
383                 if (seq) {
384                         data->rx_pn[0] = seq[5];
385                         data->rx_pn[1] = seq[4];
386                         data->rx_pn[2] = seq[3];
387                         data->rx_pn[3] = seq[2];
388                         data->rx_pn[4] = seq[1];
389                         data->rx_pn[5] = seq[0];
390                 }
391                 crypto_cipher_setkey((void *)data->tfm, data->key, CCMP_TK_LEN);
392         } else if (len == 0)
393                 data->key_set = 0;
394         else
395                 return -1;
396
397         return 0;
398 }
399
400
401 static int ieee80211_ccmp_get_key(void *key, int len, u8 *seq, void *priv)
402 {
403         struct ieee80211_ccmp_data *data = priv;
404
405         if (len < CCMP_TK_LEN)
406                 return -1;
407
408         if (!data->key_set)
409                 return 0;
410         memcpy(key, data->key, CCMP_TK_LEN);
411
412         if (seq) {
413                 seq[0] = data->tx_pn[5];
414                 seq[1] = data->tx_pn[4];
415                 seq[2] = data->tx_pn[3];
416                 seq[3] = data->tx_pn[2];
417                 seq[4] = data->tx_pn[1];
418                 seq[5] = data->tx_pn[0];
419         }
420
421         return CCMP_TK_LEN;
422 }
423
424
425 static char *ieee80211_ccmp_print_stats(char *p, void *priv)
426 {
427         struct ieee80211_ccmp_data *ccmp = priv;
428
429         p += sprintf(p, "key[%d] alg=CCMP key_set=%d tx_pn=%pm rx_pn=%pm format_errors=%d replays=%d decrypt_errors=%d\n",
430                      ccmp->key_idx, ccmp->key_set,
431                      ccmp->tx_pn, ccmp->rx_pn,
432                      ccmp->dot11RSNAStatsCCMPFormatErrors,
433                      ccmp->dot11RSNAStatsCCMPReplays,
434                      ccmp->dot11RSNAStatsCCMPDecryptErrors);
435
436         return p;
437 }
438
439 void ieee80211_ccmp_null(void)
440 {
441         /* printk("============>%s()\n", __func__); */
442         return;
443 }
444
445 static struct ieee80211_crypto_ops ieee80211_crypt_ccmp = {
446         .name                   = "CCMP",
447         .init                   = ieee80211_ccmp_init,
448         .deinit                 = ieee80211_ccmp_deinit,
449         .encrypt_mpdu           = ieee80211_ccmp_encrypt,
450         .decrypt_mpdu           = ieee80211_ccmp_decrypt,
451         .encrypt_msdu           = NULL,
452         .decrypt_msdu           = NULL,
453         .set_key                = ieee80211_ccmp_set_key,
454         .get_key                = ieee80211_ccmp_get_key,
455         .print_stats            = ieee80211_ccmp_print_stats,
456         .extra_prefix_len       = CCMP_HDR_LEN,
457         .extra_postfix_len      = CCMP_MIC_LEN,
458         .owner                  = THIS_MODULE,
459 };
460
461 int __init ieee80211_crypto_ccmp_init(void)
462 {
463         return ieee80211_register_crypto_ops(&ieee80211_crypt_ccmp);
464 }
465
466 void __exit ieee80211_crypto_ccmp_exit(void)
467 {
468         ieee80211_unregister_crypto_ops(&ieee80211_crypt_ccmp);
469 }