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