GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / staging / rtl8192u / ieee80211 / ieee80211_crypt_ccmp.c
blob4b078e536382efe9116c8a0f028cf1f6d639c4ea
1 /*
2 * Host AP crypt: host-based CCMP encryption implementation for Host AP driver
4 * Copyright (c) 2003-2004, Jouni Malinen <jkmaline@cc.hut.fi>
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.
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>
25 #include "ieee80211.h"
27 #include <linux/crypto.h>
28 #include <linux/scatterlist.h>
30 MODULE_AUTHOR("Jouni Malinen");
31 MODULE_DESCRIPTION("Host AP crypt: CCMP");
32 MODULE_LICENSE("GPL");
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
40 struct ieee80211_ccmp_data {
41 u8 key[CCMP_TK_LEN];
42 int key_set;
44 u8 tx_pn[CCMP_PN_LEN];
45 u8 rx_pn[CCMP_PN_LEN];
47 u32 dot11RSNAStatsCCMPFormatErrors;
48 u32 dot11RSNAStatsCCMPReplays;
49 u32 dot11RSNAStatsCCMPDecryptErrors;
51 int key_idx;
53 struct crypto_tfm *tfm;
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];
61 void ieee80211_ccmp_aes_encrypt(struct crypto_tfm *tfm,
62 const u8 pt[16], u8 ct[16])
64 crypto_cipher_encrypt_one((void*)tfm, ct, pt);
67 static void * ieee80211_ccmp_init(int key_idx)
69 struct ieee80211_ccmp_data *priv;
71 priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
72 if (priv == NULL)
73 goto fail;
74 priv->key_idx = key_idx;
76 priv->tfm = (void*)crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
77 if (IS_ERR(priv->tfm)) {
78 printk(KERN_DEBUG "ieee80211_crypt_ccmp: could not allocate "
79 "crypto API aes\n");
80 priv->tfm = NULL;
81 goto fail;
84 return priv;
86 fail:
87 if (priv) {
88 if (priv->tfm)
89 crypto_free_cipher((void*)priv->tfm);
90 kfree(priv);
93 return NULL;
97 static void ieee80211_ccmp_deinit(void *priv)
99 struct ieee80211_ccmp_data *_priv = priv;
101 if (_priv && _priv->tfm)
102 crypto_free_cipher((void*)_priv->tfm);
103 kfree(priv);
107 static inline void xor_block(u8 *b, u8 *a, size_t len)
109 int i;
110 for (i = 0; i < len; i++)
111 b[i] ^= a[i];
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)
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];
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));
131 qc_included = ((WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA) &&
132 (WLAN_FC_GET_STYPE(fc) & 0x08));
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;
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;
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)
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 */
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);
196 static int ieee80211_ccmp_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
198 struct ieee80211_ccmp_data *key = priv;
199 int data_len, i;
200 u8 *pos;
201 struct ieee80211_hdr_4addr *hdr;
202 cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
204 if (skb_headroom(skb) < CCMP_HDR_LEN ||
205 skb_tailroom(skb) < CCMP_MIC_LEN ||
206 skb->len < hdr_len)
207 return -1;
209 data_len = skb->len - hdr_len;
210 pos = skb_push(skb, CCMP_HDR_LEN);
211 memmove(pos, pos + CCMP_HDR_LEN, hdr_len);
212 pos += hdr_len;
213 // mic = skb_put(skb, CCMP_MIC_LEN);
215 i = CCMP_PN_LEN - 1;
216 while (i >= 0) {
217 key->tx_pn[i]++;
218 if (key->tx_pn[i] != 0)
219 break;
220 i--;
223 *pos++ = key->tx_pn[5];
224 *pos++ = key->tx_pn[4];
225 *pos++ = 0;
226 *pos++ = (key->key_idx << 6) | (1 << 5) /* Ext IV included */;
227 *pos++ = key->tx_pn[3];
228 *pos++ = key->tx_pn[2];
229 *pos++ = key->tx_pn[1];
230 *pos++ = key->tx_pn[0];
233 hdr = (struct ieee80211_hdr_4addr *) skb->data;
234 if (!tcb_desc->bHwSec)
236 int blocks, last, len;
237 u8 *mic;
238 u8 *b0 = key->tx_b0;
239 u8 *b = key->tx_b;
240 u8 *e = key->tx_e;
241 u8 *s0 = key->tx_s0;
243 //mic is moved to here by john
244 mic = skb_put(skb, CCMP_MIC_LEN);
246 ccmp_init_blocks(key->tfm, hdr, key->tx_pn, data_len, b0, b, s0);
248 blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN;
249 last = data_len % AES_BLOCK_LEN;
251 for (i = 1; i <= blocks; i++) {
252 len = (i == blocks && last) ? last : AES_BLOCK_LEN;
253 /* Authentication */
254 xor_block(b, pos, len);
255 ieee80211_ccmp_aes_encrypt(key->tfm, b, b);
256 /* Encryption, with counter */
257 b0[14] = (i >> 8) & 0xff;
258 b0[15] = i & 0xff;
259 ieee80211_ccmp_aes_encrypt(key->tfm, b0, e);
260 xor_block(pos, e, len);
261 pos += len;
264 for (i = 0; i < CCMP_MIC_LEN; i++)
265 mic[i] = b[i] ^ s0[i];
267 return 0;
271 static int ieee80211_ccmp_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
273 struct ieee80211_ccmp_data *key = priv;
274 u8 keyidx, *pos;
275 struct ieee80211_hdr_4addr *hdr;
276 cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
277 u8 pn[6];
279 if (skb->len < hdr_len + CCMP_HDR_LEN + CCMP_MIC_LEN) {
280 key->dot11RSNAStatsCCMPFormatErrors++;
281 return -1;
284 hdr = (struct ieee80211_hdr_4addr *) skb->data;
285 pos = skb->data + hdr_len;
286 keyidx = pos[3];
287 if (!(keyidx & (1 << 5))) {
288 if (net_ratelimit()) {
289 printk(KERN_DEBUG "CCMP: received packet without ExtIV"
290 " flag from %pM\n", hdr->addr2);
292 key->dot11RSNAStatsCCMPFormatErrors++;
293 return -2;
295 keyidx >>= 6;
296 if (key->key_idx != keyidx) {
297 printk(KERN_DEBUG "CCMP: RX tkey->key_idx=%d frame "
298 "keyidx=%d priv=%p\n", key->key_idx, keyidx, priv);
299 return -6;
301 if (!key->key_set) {
302 if (net_ratelimit()) {
303 printk(KERN_DEBUG "CCMP: received packet from %pM"
304 " with keyid=%d that does not have a configured"
305 " key\n", hdr->addr2, keyidx);
307 return -3;
310 pn[0] = pos[7];
311 pn[1] = pos[6];
312 pn[2] = pos[5];
313 pn[3] = pos[4];
314 pn[4] = pos[1];
315 pn[5] = pos[0];
316 pos += 8;
318 if (memcmp(pn, key->rx_pn, CCMP_PN_LEN) <= 0) {
319 if (net_ratelimit()) {
320 printk(KERN_DEBUG "CCMP: replay detected: STA=%pM"
321 " previous PN %pm received PN %pm\n",
322 hdr->addr2, key->rx_pn, pn);
324 key->dot11RSNAStatsCCMPReplays++;
325 return -4;
327 if (!tcb_desc->bHwSec)
329 size_t data_len = skb->len - hdr_len - CCMP_HDR_LEN - CCMP_MIC_LEN;
330 u8 *mic = skb->data + skb->len - CCMP_MIC_LEN;
331 u8 *b0 = key->rx_b0;
332 u8 *b = key->rx_b;
333 u8 *a = key->rx_a;
334 int i, blocks, last, len;
337 ccmp_init_blocks(key->tfm, hdr, pn, data_len, b0, a, b);
338 xor_block(mic, b, CCMP_MIC_LEN);
340 blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN;
341 last = data_len % AES_BLOCK_LEN;
343 for (i = 1; i <= blocks; i++) {
344 len = (i == blocks && last) ? last : AES_BLOCK_LEN;
345 /* Decrypt, with counter */
346 b0[14] = (i >> 8) & 0xff;
347 b0[15] = i & 0xff;
348 ieee80211_ccmp_aes_encrypt(key->tfm, b0, b);
349 xor_block(pos, b, len);
350 /* Authentication */
351 xor_block(a, pos, len);
352 ieee80211_ccmp_aes_encrypt(key->tfm, a, a);
353 pos += len;
356 if (memcmp(mic, a, CCMP_MIC_LEN) != 0) {
357 if (net_ratelimit()) {
358 printk(KERN_DEBUG "CCMP: decrypt failed: STA="
359 "%pM\n", hdr->addr2);
361 key->dot11RSNAStatsCCMPDecryptErrors++;
362 return -5;
365 memcpy(key->rx_pn, pn, CCMP_PN_LEN);
367 /* Remove hdr and MIC */
368 memmove(skb->data + CCMP_HDR_LEN, skb->data, hdr_len);
369 skb_pull(skb, CCMP_HDR_LEN);
370 skb_trim(skb, skb->len - CCMP_MIC_LEN);
372 return keyidx;
376 static int ieee80211_ccmp_set_key(void *key, int len, u8 *seq, void *priv)
378 struct ieee80211_ccmp_data *data = priv;
379 int keyidx;
380 struct crypto_tfm *tfm = data->tfm;
382 keyidx = data->key_idx;
383 memset(data, 0, sizeof(*data));
384 data->key_idx = keyidx;
385 data->tfm = tfm;
386 if (len == CCMP_TK_LEN) {
387 memcpy(data->key, key, CCMP_TK_LEN);
388 data->key_set = 1;
389 if (seq) {
390 data->rx_pn[0] = seq[5];
391 data->rx_pn[1] = seq[4];
392 data->rx_pn[2] = seq[3];
393 data->rx_pn[3] = seq[2];
394 data->rx_pn[4] = seq[1];
395 data->rx_pn[5] = seq[0];
397 crypto_cipher_setkey((void*)data->tfm, data->key, CCMP_TK_LEN);
398 } else if (len == 0)
399 data->key_set = 0;
400 else
401 return -1;
403 return 0;
407 static int ieee80211_ccmp_get_key(void *key, int len, u8 *seq, void *priv)
409 struct ieee80211_ccmp_data *data = priv;
411 if (len < CCMP_TK_LEN)
412 return -1;
414 if (!data->key_set)
415 return 0;
416 memcpy(key, data->key, CCMP_TK_LEN);
418 if (seq) {
419 seq[0] = data->tx_pn[5];
420 seq[1] = data->tx_pn[4];
421 seq[2] = data->tx_pn[3];
422 seq[3] = data->tx_pn[2];
423 seq[4] = data->tx_pn[1];
424 seq[5] = data->tx_pn[0];
427 return CCMP_TK_LEN;
431 static char * ieee80211_ccmp_print_stats(char *p, void *priv)
433 struct ieee80211_ccmp_data *ccmp = priv;
434 p += sprintf(p, "key[%d] alg=CCMP key_set=%d "
435 "tx_pn=%pm rx_pn=%pm "
436 "format_errors=%d replays=%d decrypt_errors=%d\n",
437 ccmp->key_idx, ccmp->key_set,
438 ccmp->tx_pn, ccmp->rx_pn,
439 ccmp->dot11RSNAStatsCCMPFormatErrors,
440 ccmp->dot11RSNAStatsCCMPReplays,
441 ccmp->dot11RSNAStatsCCMPDecryptErrors);
443 return p;
446 void ieee80211_ccmp_null(void)
448 // printk("============>%s()\n", __FUNCTION__);
449 return;
452 static struct ieee80211_crypto_ops ieee80211_crypt_ccmp = {
453 .name = "CCMP",
454 .init = ieee80211_ccmp_init,
455 .deinit = ieee80211_ccmp_deinit,
456 .encrypt_mpdu = ieee80211_ccmp_encrypt,
457 .decrypt_mpdu = ieee80211_ccmp_decrypt,
458 .encrypt_msdu = NULL,
459 .decrypt_msdu = NULL,
460 .set_key = ieee80211_ccmp_set_key,
461 .get_key = ieee80211_ccmp_get_key,
462 .print_stats = ieee80211_ccmp_print_stats,
463 .extra_prefix_len = CCMP_HDR_LEN,
464 .extra_postfix_len = CCMP_MIC_LEN,
465 .owner = THIS_MODULE,
468 int __init ieee80211_crypto_ccmp_init(void)
470 return ieee80211_register_crypto_ops(&ieee80211_crypt_ccmp);
473 void __exit ieee80211_crypto_ccmp_exit(void)
475 ieee80211_unregister_crypto_ops(&ieee80211_crypt_ccmp);