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