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