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 <linux/wireless.h>
23 #include <net/iw_handler.h>
24 #include <asm/string.h>
26 #include "hostap_crypt.h"
27 #include "hostap_wlan.h"
28 #include "hostap_80211.h"
31 #error CONFIG_CRYPTO is required to build this module.
33 #include <linux/crypto.h>
34 #include <asm/scatterlist.h>
36 MODULE_AUTHOR("Jouni Malinen");
37 MODULE_DESCRIPTION("Host AP crypt: CCMP");
38 MODULE_LICENSE("GPL");
41 #define AES_BLOCK_LEN 16
42 #define CCMP_HDR_LEN 8
43 #define CCMP_MIC_LEN 8
44 #define CCMP_TK_LEN 16
48 struct hostap_ccmp_data
{
52 u8 tx_pn
[CCMP_PN_LEN
];
53 u8 rx_pn
[CCMP_PN_LEN
];
55 u32 dot11RSNAStatsCCMPFormatErrors
;
56 u32 dot11RSNAStatsCCMPReplays
;
57 u32 dot11RSNAStatsCCMPDecryptErrors
;
61 struct crypto_tfm
*tfm
;
63 /* scratch buffers for virt_to_page() (crypto API) */
64 u8 tx_b0
[AES_BLOCK_LEN
], tx_b
[AES_BLOCK_LEN
],
65 tx_e
[AES_BLOCK_LEN
], tx_s0
[AES_BLOCK_LEN
];
66 u8 rx_b0
[AES_BLOCK_LEN
], rx_b
[AES_BLOCK_LEN
], rx_a
[AES_BLOCK_LEN
];
70 void hostap_ccmp_aes_encrypt(struct crypto_tfm
*tfm
,
71 const u8 pt
[16], u8 ct
[16])
73 struct scatterlist src
, dst
;
75 src
.page
= virt_to_page(pt
);
76 src
.offset
= offset_in_page(pt
);
77 src
.length
= AES_BLOCK_LEN
;
79 dst
.page
= virt_to_page(ct
);
80 dst
.offset
= offset_in_page(ct
);
81 dst
.length
= AES_BLOCK_LEN
;
83 crypto_cipher_encrypt(tfm
, &dst
, &src
, AES_BLOCK_LEN
);
87 static void * hostap_ccmp_init(int key_idx
)
89 struct hostap_ccmp_data
*priv
;
91 if (!try_module_get(THIS_MODULE
))
94 priv
= (struct hostap_ccmp_data
*) kmalloc(sizeof(*priv
), GFP_ATOMIC
);
98 memset(priv
, 0, sizeof(*priv
));
99 priv
->key_idx
= key_idx
;
101 priv
->tfm
= crypto_alloc_tfm("aes", 0);
102 if (priv
->tfm
== NULL
) {
103 printk(KERN_DEBUG
"hostap_crypt_ccmp: could not allocate "
113 crypto_free_tfm(priv
->tfm
);
116 module_put(THIS_MODULE
);
121 static void hostap_ccmp_deinit(void *priv
)
123 struct hostap_ccmp_data
*_priv
= priv
;
124 if (_priv
&& _priv
->tfm
)
125 crypto_free_tfm(_priv
->tfm
);
127 module_put(THIS_MODULE
);
131 static inline void xor_block(u8
*b
, u8
*a
, size_t len
)
134 for (i
= 0; i
< len
; i
++)
139 static void ccmp_init_blocks(struct crypto_tfm
*tfm
,
140 struct hostap_ieee80211_hdr
*hdr
,
141 u8
*pn
, size_t dlen
, u8
*b0
, u8
*auth
,
147 int a4_included
, qc_included
;
148 u8 aad
[2 * AES_BLOCK_LEN
];
150 fc
= le16_to_cpu(hdr
->frame_control
);
151 a4_included
= ((fc
& (WLAN_FC_TODS
| WLAN_FC_FROMDS
)) ==
152 (WLAN_FC_TODS
| WLAN_FC_FROMDS
));
153 qc_included
= ((WLAN_FC_GET_TYPE(fc
) == WLAN_FC_TYPE_DATA
) &&
154 (WLAN_FC_GET_STYPE(fc
) & 0x08));
159 pos
= (u8
*) &hdr
->addr4
;
166 /* CCM Initial Block:
167 * Flag (Include authentication header, M=3 (8-octet MIC),
168 * L=1 (2-octet Dlen))
169 * Nonce: 0x00 | A2 | PN
173 memcpy(b0
+ 2, hdr
->addr2
, ETH_ALEN
);
174 memcpy(b0
+ 8, pn
, CCMP_PN_LEN
);
175 b0
[14] = (dlen
>> 8) & 0xff;
176 b0
[15] = dlen
& 0xff;
179 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
181 * SC with bits 4..15 (seq#) masked to zero
186 aad
[0] = 0; /* aad_len >> 8 */
187 aad
[1] = aad_len
& 0xff;
188 aad
[2] = pos
[0] & 0x8f;
189 aad
[3] = pos
[1] & 0xc7;
190 memcpy(aad
+ 4, hdr
->addr1
, 3 * ETH_ALEN
);
191 pos
= (u8
*) &hdr
->seq_ctrl
;
192 aad
[22] = pos
[0] & 0x0f;
193 aad
[23] = 0; /* all bits masked */
194 memset(aad
+ 24, 0, 8);
196 memcpy(aad
+ 24, hdr
->addr4
, ETH_ALEN
);
198 aad
[a4_included
? 30 : 24] = qc
;
199 /* rest of QC masked */
202 /* Start with the first block and AAD */
203 hostap_ccmp_aes_encrypt(tfm
, b0
, auth
);
204 xor_block(auth
, aad
, AES_BLOCK_LEN
);
205 hostap_ccmp_aes_encrypt(tfm
, auth
, auth
);
206 xor_block(auth
, &aad
[AES_BLOCK_LEN
], AES_BLOCK_LEN
);
207 hostap_ccmp_aes_encrypt(tfm
, auth
, auth
);
210 hostap_ccmp_aes_encrypt(tfm
, b0
, s0
);
214 static int hostap_ccmp_encrypt(struct sk_buff
*skb
, int hdr_len
, void *priv
)
216 struct hostap_ccmp_data
*key
= priv
;
217 int data_len
, i
, blocks
, last
, len
;
219 struct hostap_ieee80211_hdr
*hdr
;
225 if (skb_headroom(skb
) < CCMP_HDR_LEN
||
226 skb_tailroom(skb
) < CCMP_MIC_LEN
||
230 data_len
= skb
->len
- hdr_len
;
231 pos
= skb_push(skb
, CCMP_HDR_LEN
);
232 memmove(pos
, pos
+ CCMP_HDR_LEN
, hdr_len
);
234 mic
= skb_put(skb
, CCMP_MIC_LEN
);
239 if (key
->tx_pn
[i
] != 0)
244 *pos
++ = key
->tx_pn
[5];
245 *pos
++ = key
->tx_pn
[4];
247 *pos
++ = (key
->key_idx
<< 6) | (1 << 5) /* Ext IV included */;
248 *pos
++ = key
->tx_pn
[3];
249 *pos
++ = key
->tx_pn
[2];
250 *pos
++ = key
->tx_pn
[1];
251 *pos
++ = key
->tx_pn
[0];
253 hdr
= (struct hostap_ieee80211_hdr
*) skb
->data
;
254 ccmp_init_blocks(key
->tfm
, hdr
, key
->tx_pn
, data_len
, b0
, b
, s0
);
256 blocks
= (data_len
+ AES_BLOCK_LEN
- 1) / AES_BLOCK_LEN
;
257 last
= data_len
% AES_BLOCK_LEN
;
259 for (i
= 1; i
<= blocks
; i
++) {
260 len
= (i
== blocks
&& last
) ? last
: AES_BLOCK_LEN
;
262 xor_block(b
, pos
, len
);
263 hostap_ccmp_aes_encrypt(key
->tfm
, b
, b
);
264 /* Encryption, with counter */
265 b0
[14] = (i
>> 8) & 0xff;
267 hostap_ccmp_aes_encrypt(key
->tfm
, b0
, e
);
268 xor_block(pos
, e
, len
);
272 for (i
= 0; i
< CCMP_MIC_LEN
; i
++)
273 mic
[i
] = b
[i
] ^ s0
[i
];
279 static int hostap_ccmp_decrypt(struct sk_buff
*skb
, int hdr_len
, void *priv
)
281 struct hostap_ccmp_data
*key
= priv
;
283 struct hostap_ieee80211_hdr
*hdr
;
288 int i
, blocks
, last
, len
;
289 size_t data_len
= skb
->len
- hdr_len
- CCMP_HDR_LEN
- CCMP_MIC_LEN
;
290 u8
*mic
= skb
->data
+ skb
->len
- CCMP_MIC_LEN
;
292 if (skb
->len
< hdr_len
+ CCMP_HDR_LEN
+ CCMP_MIC_LEN
) {
293 key
->dot11RSNAStatsCCMPFormatErrors
++;
297 hdr
= (struct hostap_ieee80211_hdr
*) skb
->data
;
298 pos
= skb
->data
+ hdr_len
;
300 if (!(keyidx
& (1 << 5))) {
301 if (net_ratelimit()) {
302 printk(KERN_DEBUG
"CCMP: received packet without ExtIV"
303 " flag from " MACSTR
"\n", MAC2STR(hdr
->addr2
));
305 key
->dot11RSNAStatsCCMPFormatErrors
++;
309 if (key
->key_idx
!= keyidx
) {
310 printk(KERN_DEBUG
"CCMP: RX tkey->key_idx=%d frame "
311 "keyidx=%d priv=%p\n", key
->key_idx
, keyidx
, priv
);
315 if (net_ratelimit()) {
316 printk(KERN_DEBUG
"CCMP: received packet from " MACSTR
317 " with keyid=%d that does not have a configured"
318 " key\n", MAC2STR(hdr
->addr2
), keyidx
);
331 if (memcmp(pn
, key
->rx_pn
, CCMP_PN_LEN
) <= 0) {
332 if (net_ratelimit()) {
333 printk(KERN_DEBUG
"CCMP: replay detected: STA=" MACSTR
334 " previous PN %02x%02x%02x%02x%02x%02x "
335 "received PN %02x%02x%02x%02x%02x%02x\n",
336 MAC2STR(hdr
->addr2
), MAC2STR(key
->rx_pn
),
339 key
->dot11RSNAStatsCCMPReplays
++;
343 ccmp_init_blocks(key
->tfm
, hdr
, pn
, data_len
, b0
, a
, b
);
344 xor_block(mic
, b
, CCMP_MIC_LEN
);
346 blocks
= (data_len
+ AES_BLOCK_LEN
- 1) / AES_BLOCK_LEN
;
347 last
= data_len
% AES_BLOCK_LEN
;
349 for (i
= 1; i
<= blocks
; i
++) {
350 len
= (i
== blocks
&& last
) ? last
: AES_BLOCK_LEN
;
351 /* Decrypt, with counter */
352 b0
[14] = (i
>> 8) & 0xff;
354 hostap_ccmp_aes_encrypt(key
->tfm
, b0
, b
);
355 xor_block(pos
, b
, len
);
357 xor_block(a
, pos
, len
);
358 hostap_ccmp_aes_encrypt(key
->tfm
, a
, a
);
362 if (memcmp(mic
, a
, CCMP_MIC_LEN
) != 0) {
363 if (net_ratelimit()) {
364 printk(KERN_DEBUG
"CCMP: decrypt failed: STA="
365 MACSTR
"\n", MAC2STR(hdr
->addr2
));
367 key
->dot11RSNAStatsCCMPDecryptErrors
++;
371 memcpy(key
->rx_pn
, pn
, CCMP_PN_LEN
);
373 /* Remove hdr and MIC */
374 memmove(skb
->data
+ CCMP_HDR_LEN
, skb
->data
, hdr_len
);
375 skb_pull(skb
, CCMP_HDR_LEN
);
376 skb_trim(skb
, skb
->len
- CCMP_MIC_LEN
);
382 static int hostap_ccmp_set_key(void *key
, int len
, u8
*seq
, void *priv
)
384 struct hostap_ccmp_data
*data
= priv
;
386 struct crypto_tfm
*tfm
= data
->tfm
;
388 keyidx
= data
->key_idx
;
389 memset(data
, 0, sizeof(*data
));
390 data
->key_idx
= keyidx
;
392 if (len
== CCMP_TK_LEN
) {
393 memcpy(data
->key
, key
, CCMP_TK_LEN
);
396 data
->rx_pn
[0] = seq
[5];
397 data
->rx_pn
[1] = seq
[4];
398 data
->rx_pn
[2] = seq
[3];
399 data
->rx_pn
[3] = seq
[2];
400 data
->rx_pn
[4] = seq
[1];
401 data
->rx_pn
[5] = seq
[0];
403 crypto_cipher_setkey(data
->tfm
, data
->key
, CCMP_TK_LEN
);
404 } else if (len
== 0) {
413 static int hostap_ccmp_get_key(void *key
, int len
, u8
*seq
, void *priv
)
415 struct hostap_ccmp_data
*data
= priv
;
417 if (len
< CCMP_TK_LEN
)
422 memcpy(key
, data
->key
, CCMP_TK_LEN
);
425 seq
[0] = data
->tx_pn
[5];
426 seq
[1] = data
->tx_pn
[4];
427 seq
[2] = data
->tx_pn
[3];
428 seq
[3] = data
->tx_pn
[2];
429 seq
[4] = data
->tx_pn
[1];
430 seq
[5] = data
->tx_pn
[0];
437 static char * hostap_ccmp_print_stats(char *p
, void *priv
)
439 struct hostap_ccmp_data
*ccmp
= priv
;
440 p
+= sprintf(p
, "key[%d] alg=CCMP key_set=%d "
441 "tx_pn=%02x%02x%02x%02x%02x%02x "
442 "rx_pn=%02x%02x%02x%02x%02x%02x "
443 "format_errors=%d replays=%d decrypt_errors=%d\n",
444 ccmp
->key_idx
, ccmp
->key_set
,
445 MAC2STR(ccmp
->tx_pn
), MAC2STR(ccmp
->rx_pn
),
446 ccmp
->dot11RSNAStatsCCMPFormatErrors
,
447 ccmp
->dot11RSNAStatsCCMPReplays
,
448 ccmp
->dot11RSNAStatsCCMPDecryptErrors
);
454 static struct hostap_crypto_ops hostap_crypt_ccmp
= {
456 .init
= hostap_ccmp_init
,
457 .deinit
= hostap_ccmp_deinit
,
458 .encrypt_mpdu
= hostap_ccmp_encrypt
,
459 .decrypt_mpdu
= hostap_ccmp_decrypt
,
460 .encrypt_msdu
= NULL
,
461 .decrypt_msdu
= NULL
,
462 .set_key
= hostap_ccmp_set_key
,
463 .get_key
= hostap_ccmp_get_key
,
464 .print_stats
= hostap_ccmp_print_stats
,
465 .extra_prefix_len
= CCMP_HDR_LEN
,
466 .extra_postfix_len
= CCMP_MIC_LEN
470 static int __init
hostap_crypto_ccmp_init(void)
472 if (hostap_register_crypto_ops(&hostap_crypt_ccmp
) < 0)
479 static void __exit
hostap_crypto_ccmp_exit(void)
481 hostap_unregister_crypto_ops(&hostap_crypt_ccmp
);
485 module_init(hostap_crypto_ccmp_init
);
486 module_exit(hostap_crypto_ccmp_exit
);