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