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