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
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 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
28 #include "rtl_crypto.h"
30 #include <linux/crypto.h>
33 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
34 #include <asm/scatterlist.h>
36 #include <linux/scatterlist.h>
38 //#include <asm/scatterlist.h>
40 MODULE_AUTHOR("Jouni Malinen");
41 MODULE_DESCRIPTION("Host AP crypt: CCMP");
42 MODULE_LICENSE("GPL");
45 #define OPENSUSE_SLED 0
48 #define AES_BLOCK_LEN 16
49 #define CCMP_HDR_LEN 8
50 #define CCMP_MIC_LEN 8
51 #define CCMP_TK_LEN 16
54 struct ieee80211_ccmp_data {
58 u8 tx_pn[CCMP_PN_LEN];
59 u8 rx_pn[CCMP_PN_LEN];
61 u32 dot11RSNAStatsCCMPFormatErrors;
62 u32 dot11RSNAStatsCCMPReplays;
63 u32 dot11RSNAStatsCCMPDecryptErrors;
67 struct crypto_tfm *tfm;
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];
75 void ieee80211_ccmp_aes_encrypt(struct crypto_tfm *tfm,
76 const u8 pt[16], u8 ct[16])
78 #if((LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21)) && (!OPENSUSE_SLED))
79 struct scatterlist src, dst;
81 src.page = virt_to_page(pt);
82 src.offset = offset_in_page(pt);
83 src.length = AES_BLOCK_LEN;
85 dst.page = virt_to_page(ct);
86 dst.offset = offset_in_page(ct);
87 dst.length = AES_BLOCK_LEN;
89 crypto_cipher_encrypt(tfm, &dst, &src, AES_BLOCK_LEN);
91 crypto_cipher_encrypt_one((void*)tfm, ct, pt);
95 static void * ieee80211_ccmp_init(int key_idx)
97 struct ieee80211_ccmp_data *priv;
99 priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
102 priv->key_idx = key_idx;
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 "
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 "
125 #if(LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21))
126 crypto_free_tfm(priv->tfm);
128 crypto_free_cipher((void*)priv->tfm);
137 static void ieee80211_ccmp_deinit(void *priv)
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);
144 crypto_free_cipher((void*)_priv->tfm);
150 static inline void xor_block(u8 *b, u8 *a, size_t len)
153 for (i = 0; i < len; i++)
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,
167 int a4_included, qc_included;
168 u8 aad[2 * AES_BLOCK_LEN];
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));
174 qc_included = ((WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA) &&
175 (WLAN_FC_GET_STYPE(fc) & 0x08));
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));
184 pos = (u8 *) &hdr->addr4;
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
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;
203 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
205 * SC with bits 4..15 (seq#) masked to zero
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);
220 memcpy(aad + 24, hdr->addr4, ETH_ALEN);
222 aad[a4_included ? 30 : 24] = qc;
223 /* rest of QC masked */
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);
234 ieee80211_ccmp_aes_encrypt(tfm, b0, s0);
239 static int ieee80211_ccmp_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
241 struct ieee80211_ccmp_data *key = priv;
244 struct ieee80211_hdr_4addr *hdr;
245 cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
247 if (skb_headroom(skb) < CCMP_HDR_LEN ||
248 skb_tailroom(skb) < CCMP_MIC_LEN ||
252 data_len = skb->len - hdr_len;
253 pos = skb_push(skb, CCMP_HDR_LEN);
254 memmove(pos, pos + CCMP_HDR_LEN, hdr_len);
256 // mic = skb_put(skb, CCMP_MIC_LEN);
261 if (key->tx_pn[i] != 0)
266 *pos++ = key->tx_pn[5];
267 *pos++ = key->tx_pn[4];
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];
276 hdr = (struct ieee80211_hdr_4addr *) skb->data;
277 if (!tcb_desc->bHwSec)
279 int blocks, last, len;
286 //mic is moved to here by john
287 mic = skb_put(skb, CCMP_MIC_LEN);
289 ccmp_init_blocks(key->tfm, hdr, key->tx_pn, data_len, b0, b, s0);
291 blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN;
292 last = data_len % AES_BLOCK_LEN;
294 for (i = 1; i <= blocks; i++) {
295 len = (i == blocks && last) ? last : AES_BLOCK_LEN;
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;
302 ieee80211_ccmp_aes_encrypt(key->tfm, b0, e);
303 xor_block(pos, e, len);
307 for (i = 0; i < CCMP_MIC_LEN; i++)
308 mic[i] = b[i] ^ s0[i];
314 static int ieee80211_ccmp_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
316 struct ieee80211_ccmp_data *key = priv;
318 struct ieee80211_hdr_4addr *hdr;
319 cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
322 if (skb->len < hdr_len + CCMP_HDR_LEN + CCMP_MIC_LEN) {
323 key->dot11RSNAStatsCCMPFormatErrors++;
327 hdr = (struct ieee80211_hdr_4addr *) skb->data;
328 pos = skb->data + hdr_len;
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);
335 key->dot11RSNAStatsCCMPFormatErrors++;
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);
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);
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);
367 key->dot11RSNAStatsCCMPReplays++;
370 if (!tcb_desc->bHwSec)
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;
377 int i, blocks, last, len;
380 ccmp_init_blocks(key->tfm, hdr, pn, data_len, b0, a, b);
381 xor_block(mic, b, CCMP_MIC_LEN);
383 blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN;
384 last = data_len % AES_BLOCK_LEN;
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;
391 ieee80211_ccmp_aes_encrypt(key->tfm, b0, b);
392 xor_block(pos, b, len);
394 xor_block(a, pos, len);
395 ieee80211_ccmp_aes_encrypt(key->tfm, a, a);
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);
404 key->dot11RSNAStatsCCMPDecryptErrors++;
408 memcpy(key->rx_pn, pn, CCMP_PN_LEN);
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);
419 static int ieee80211_ccmp_set_key(void *key, int len, u8 *seq, void *priv)
421 struct ieee80211_ccmp_data *data = priv;
423 struct crypto_tfm *tfm = data->tfm;
425 keyidx = data->key_idx;
426 memset(data, 0, sizeof(*data));
427 data->key_idx = keyidx;
429 if (len == CCMP_TK_LEN) {
430 memcpy(data->key, key, CCMP_TK_LEN);
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];
440 crypto_cipher_setkey((void*)data->tfm, data->key, CCMP_TK_LEN);
450 static int ieee80211_ccmp_get_key(void *key, int len, u8 *seq, void *priv)
452 struct ieee80211_ccmp_data *data = priv;
454 if (len < CCMP_TK_LEN)
459 memcpy(key, data->key, CCMP_TK_LEN);
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];
474 static char * ieee80211_ccmp_print_stats(char *p, void *priv)
476 struct ieee80211_ccmp_data *ccmp = priv;
479 p += sprintf(p, "key[%d] alg=CCMP key_set=%d tx_pn=",
480 ccmp->key_idx, ccmp->key_set);
482 for (i = 0; i < ARRAY_SIZE(ccmp->tx_pn); i++)
483 p += sprintf(p, "%02x", ccmp->tx_pn[i]);
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]);
489 p += sprintf(p, " format_errors=%d replays=%d decrypt_errors=%d\n",
490 ccmp->dot11RSNAStatsCCMPFormatErrors,
491 ccmp->dot11RSNAStatsCCMPReplays,
492 ccmp->dot11RSNAStatsCCMPDecryptErrors);
497 void ieee80211_ccmp_null(void)
499 // printk("============>%s()\n", __FUNCTION__);
503 static struct ieee80211_crypto_ops ieee80211_crypt_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,
520 int __init ieee80211_crypto_ccmp_init(void)
522 return ieee80211_register_crypto_ops(&ieee80211_crypt_ccmp);
526 void ieee80211_crypto_ccmp_exit(void)
528 ieee80211_unregister_crypto_ops(&ieee80211_crypt_ccmp);
531 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
532 //EXPORT_SYMBOL(ieee80211_ccmp_null);
534 EXPORT_SYMBOL_NOVERS(ieee80211_ccmp_null);
537 //module_init(ieee80211_crypto_ccmp_init);
538 //module_exit(ieee80211_crypto_ccmp_exit);