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 #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");
35 #define AES_BLOCK_LEN 16
36 #define CCMP_HDR_LEN 8
37 #define CCMP_MIC_LEN 8
38 #define CCMP_TK_LEN 16
41 struct ieee80211_ccmp_data
{
45 u8 tx_pn
[CCMP_PN_LEN
];
46 u8 rx_pn
[CCMP_PN_LEN
];
48 u32 dot11RSNAStatsCCMPFormatErrors
;
49 u32 dot11RSNAStatsCCMPReplays
;
50 u32 dot11RSNAStatsCCMPDecryptErrors
;
54 struct crypto_tfm
*tfm
;
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
];
62 void ieee80211_ccmp_aes_encrypt(struct crypto_tfm
*tfm
,
63 const u8 pt
[16], u8 ct
[16])
65 crypto_cipher_encrypt_one((void *)tfm
, ct
, pt
);
68 static void * ieee80211_ccmp_init(int key_idx
)
70 struct ieee80211_ccmp_data
*priv
;
72 priv
= kzalloc(sizeof(*priv
), GFP_ATOMIC
);
75 priv
->key_idx
= key_idx
;
77 priv
->tfm
= (void *)crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC
);
78 if (IS_ERR(priv
->tfm
)) {
79 printk(KERN_DEBUG
"ieee80211_crypt_ccmp: could not allocate "
90 crypto_free_cipher((void *)priv
->tfm
);
98 static void ieee80211_ccmp_deinit(void *priv
)
100 struct ieee80211_ccmp_data
*_priv
= priv
;
102 if (_priv
&& _priv
->tfm
)
103 crypto_free_cipher((void *)_priv
->tfm
);
108 static inline void xor_block(u8
*b
, u8
*a
, size_t len
)
111 for (i
= 0; i
< len
; i
++)
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
,
123 int a4_included
, qc_included
;
124 u8 aad
[2 * AES_BLOCK_LEN
];
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
));
130 qc_included = ((WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA) &&
131 (WLAN_FC_GET_STYPE(fc) & 0x08));
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));
140 pos
= (u8
*) &hdr
->addr4
;
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
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;
159 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
161 * SC with bits 4..15 (seq#) masked to zero
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);
176 memcpy(aad
+ 24, hdr
->addr4
, ETH_ALEN
);
178 aad
[a4_included
? 30 : 24] = qc
;
179 /* rest of QC masked */
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
);
190 ieee80211_ccmp_aes_encrypt(tfm
, b0
, s0
);
193 static int ieee80211_ccmp_encrypt(struct sk_buff
*skb
, int hdr_len
, void *priv
)
195 struct ieee80211_ccmp_data
*key
= priv
;
198 struct ieee80211_hdr_4addr
*hdr
;
199 int blocks
, last
, len
;
206 if (skb_headroom(skb
) < CCMP_HDR_LEN
||
207 skb_tailroom(skb
) < CCMP_MIC_LEN
||
211 data_len
= skb
->len
- hdr_len
;
212 pos
= skb_push(skb
, CCMP_HDR_LEN
);
213 memmove(pos
, pos
+ CCMP_HDR_LEN
, hdr_len
);
215 // mic = skb_put(skb, CCMP_MIC_LEN);
220 if (key
->tx_pn
[i
] != 0)
225 *pos
++ = key
->tx_pn
[5];
226 *pos
++ = key
->tx_pn
[4];
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];
234 hdr
= (struct ieee80211_hdr_4addr
*)skb
->data
;
235 //mic is moved to here by john
236 mic
= skb_put(skb
, CCMP_MIC_LEN
);
238 ccmp_init_blocks(key
->tfm
, hdr
, key
->tx_pn
, data_len
, b0
, b
, s0
);
240 blocks
= (data_len
+ AES_BLOCK_LEN
- 1) / AES_BLOCK_LEN
;
241 last
= data_len
% AES_BLOCK_LEN
;
243 for (i
= 1; i
<= blocks
; i
++) {
244 len
= (i
== blocks
&& last
) ? last
: AES_BLOCK_LEN
;
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;
251 ieee80211_ccmp_aes_encrypt(key
->tfm
, b0
, e
);
252 xor_block(pos
, e
, len
);
256 for (i
= 0; i
< CCMP_MIC_LEN
; i
++)
257 mic
[i
] = b
[i
] ^ s0
[i
];
263 static int ieee80211_ccmp_decrypt(struct sk_buff
*skb
, int hdr_len
, void *priv
)
265 struct ieee80211_ccmp_data
*key
= priv
;
267 struct ieee80211_hdr_4addr
*hdr
;
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
;
274 int i
, blocks
, last
, len
;
276 if (skb
->len
< hdr_len
+ CCMP_HDR_LEN
+ CCMP_MIC_LEN
) {
277 key
->dot11RSNAStatsCCMPFormatErrors
++;
281 hdr
= (struct ieee80211_hdr_4addr
*)skb
->data
;
282 pos
= skb
->data
+ hdr_len
;
284 if (!(keyidx
& (1 << 5))) {
285 if (net_ratelimit()) {
286 printk(KERN_DEBUG
"CCMP: received packet without ExtIV"
287 " flag from %pM\n", hdr
->addr2
);
289 key
->dot11RSNAStatsCCMPFormatErrors
++;
293 if (key
->key_idx
!= keyidx
) {
294 printk(KERN_DEBUG
"CCMP: RX tkey->key_idx=%d frame "
295 "keyidx=%d priv=%p\n", key
->key_idx
, keyidx
, priv
);
299 if (net_ratelimit()) {
300 printk(KERN_DEBUG
"CCMP: received packet from %pM"
301 " with keyid=%d that does not have a configured"
302 " key\n", hdr
->addr2
, keyidx
);
315 if (memcmp(pn
, key
->rx_pn
, CCMP_PN_LEN
) <= 0) {
316 if (net_ratelimit()) {
317 printk(KERN_DEBUG
"CCMP: replay detected: STA=%pM"
318 " previous PN %pm received PN %pm\n",
319 hdr
->addr2
, key
->rx_pn
, pn
);
321 key
->dot11RSNAStatsCCMPReplays
++;
325 ccmp_init_blocks(key
->tfm
, hdr
, pn
, data_len
, b0
, a
, b
);
326 xor_block(mic
, b
, CCMP_MIC_LEN
);
328 blocks
= (data_len
+ AES_BLOCK_LEN
- 1) / AES_BLOCK_LEN
;
329 last
= data_len
% AES_BLOCK_LEN
;
331 for (i
= 1; i
<= blocks
; i
++) {
332 len
= (i
== blocks
&& last
) ? last
: AES_BLOCK_LEN
;
333 /* Decrypt, with counter */
334 b0
[14] = (i
>> 8) & 0xff;
336 ieee80211_ccmp_aes_encrypt(key
->tfm
, b0
, b
);
337 xor_block(pos
, b
, len
);
339 xor_block(a
, pos
, len
);
340 ieee80211_ccmp_aes_encrypt(key
->tfm
, a
, a
);
344 if (memcmp(mic
, a
, CCMP_MIC_LEN
) != 0) {
345 if (net_ratelimit()) {
346 printk(KERN_DEBUG
"CCMP: decrypt failed: STA="
347 "%pM\n", hdr
->addr2
);
349 key
->dot11RSNAStatsCCMPDecryptErrors
++;
353 memcpy(key
->rx_pn
, pn
, CCMP_PN_LEN
);
355 /* Remove hdr and MIC */
356 memmove(skb
->data
+ CCMP_HDR_LEN
, skb
->data
, hdr_len
);
357 skb_pull(skb
, CCMP_HDR_LEN
);
358 skb_trim(skb
, skb
->len
- CCMP_MIC_LEN
);
364 static int ieee80211_ccmp_set_key(void *key
, int len
, u8
*seq
, void *priv
)
366 struct ieee80211_ccmp_data
*data
= priv
;
368 struct crypto_tfm
*tfm
= data
->tfm
;
370 keyidx
= data
->key_idx
;
371 memset(data
, 0, sizeof(*data
));
372 data
->key_idx
= keyidx
;
374 if (len
== CCMP_TK_LEN
) {
375 memcpy(data
->key
, key
, CCMP_TK_LEN
);
378 data
->rx_pn
[0] = seq
[5];
379 data
->rx_pn
[1] = seq
[4];
380 data
->rx_pn
[2] = seq
[3];
381 data
->rx_pn
[3] = seq
[2];
382 data
->rx_pn
[4] = seq
[1];
383 data
->rx_pn
[5] = seq
[0];
385 crypto_cipher_setkey((void *)data
->tfm
, data
->key
, CCMP_TK_LEN
);
395 static int ieee80211_ccmp_get_key(void *key
, int len
, u8
*seq
, void *priv
)
397 struct ieee80211_ccmp_data
*data
= priv
;
399 if (len
< CCMP_TK_LEN
)
404 memcpy(key
, data
->key
, CCMP_TK_LEN
);
407 seq
[0] = data
->tx_pn
[5];
408 seq
[1] = data
->tx_pn
[4];
409 seq
[2] = data
->tx_pn
[3];
410 seq
[3] = data
->tx_pn
[2];
411 seq
[4] = data
->tx_pn
[1];
412 seq
[5] = data
->tx_pn
[0];
419 static char * ieee80211_ccmp_print_stats(char *p
, void *priv
)
421 struct ieee80211_ccmp_data
*ccmp
= priv
;
422 p
+= sprintf(p
, "key[%d] alg=CCMP key_set=%d "
423 "tx_pn=%pm rx_pn=%pm "
424 "format_errors=%d replays=%d decrypt_errors=%d\n",
425 ccmp
->key_idx
, ccmp
->key_set
,
426 ccmp
->tx_pn
, ccmp
->rx_pn
,
427 ccmp
->dot11RSNAStatsCCMPFormatErrors
,
428 ccmp
->dot11RSNAStatsCCMPReplays
,
429 ccmp
->dot11RSNAStatsCCMPDecryptErrors
);
434 void ieee80211_ccmp_null(void)
436 // printk("============>%s()\n", __func__);
439 static struct ieee80211_crypto_ops ieee80211_crypt_ccmp
= {
441 .init
= ieee80211_ccmp_init
,
442 .deinit
= ieee80211_ccmp_deinit
,
443 .encrypt_mpdu
= ieee80211_ccmp_encrypt
,
444 .decrypt_mpdu
= ieee80211_ccmp_decrypt
,
445 .encrypt_msdu
= NULL
,
446 .decrypt_msdu
= NULL
,
447 .set_key
= ieee80211_ccmp_set_key
,
448 .get_key
= ieee80211_ccmp_get_key
,
449 .print_stats
= ieee80211_ccmp_print_stats
,
450 .extra_prefix_len
= CCMP_HDR_LEN
,
451 .extra_postfix_len
= CCMP_MIC_LEN
,
452 .owner
= THIS_MODULE
,
456 int ieee80211_crypto_ccmp_init(void)
458 return ieee80211_register_crypto_ops(&ieee80211_crypt_ccmp
);
462 void ieee80211_crypto_ccmp_exit(void)
464 ieee80211_unregister_crypto_ops(&ieee80211_crypt_ccmp
);