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/err.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/random.h>
17 #include <linux/skbuff.h>
18 #include <linux/netdevice.h>
19 #include <linux/if_ether.h>
20 #include <linux/if_arp.h>
21 #include <asm/string.h>
22 #include <linux/wireless.h>
24 #include <net/ieee80211.h>
26 #include <linux/crypto.h>
27 #include <asm/scatterlist.h>
29 MODULE_AUTHOR("Jouni Malinen");
30 MODULE_DESCRIPTION("Host AP crypt: CCMP");
31 MODULE_LICENSE("GPL");
33 #define AES_BLOCK_LEN 16
34 #define CCMP_HDR_LEN 8
35 #define CCMP_MIC_LEN 8
36 #define CCMP_TK_LEN 16
39 struct ieee80211_ccmp_data
{
43 u8 tx_pn
[CCMP_PN_LEN
];
44 u8 rx_pn
[CCMP_PN_LEN
];
46 u32 dot11RSNAStatsCCMPFormatErrors
;
47 u32 dot11RSNAStatsCCMPReplays
;
48 u32 dot11RSNAStatsCCMPDecryptErrors
;
52 struct crypto_cipher
*tfm
;
54 /* scratch buffers for virt_to_page() (crypto API) */
55 u8 tx_b0
[AES_BLOCK_LEN
], tx_b
[AES_BLOCK_LEN
],
56 tx_e
[AES_BLOCK_LEN
], tx_s0
[AES_BLOCK_LEN
];
57 u8 rx_b0
[AES_BLOCK_LEN
], rx_b
[AES_BLOCK_LEN
], rx_a
[AES_BLOCK_LEN
];
60 static inline void ieee80211_ccmp_aes_encrypt(struct crypto_cipher
*tfm
,
61 const u8 pt
[16], u8 ct
[16])
63 crypto_cipher_encrypt_one(tfm
, ct
, pt
);
66 static void *ieee80211_ccmp_init(int key_idx
)
68 struct ieee80211_ccmp_data
*priv
;
70 priv
= kzalloc(sizeof(*priv
), GFP_ATOMIC
);
73 priv
->key_idx
= key_idx
;
75 priv
->tfm
= crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC
);
76 if (IS_ERR(priv
->tfm
)) {
77 printk(KERN_DEBUG
"ieee80211_crypt_ccmp: could not allocate "
88 crypto_free_cipher(priv
->tfm
);
95 static void ieee80211_ccmp_deinit(void *priv
)
97 struct ieee80211_ccmp_data
*_priv
= priv
;
98 if (_priv
&& _priv
->tfm
)
99 crypto_free_cipher(_priv
->tfm
);
103 static inline void xor_block(u8
* b
, u8
* a
, size_t len
)
106 for (i
= 0; i
< len
; i
++)
110 static void ccmp_init_blocks(struct crypto_cipher
*tfm
,
111 struct ieee80211_hdr_4addr
*hdr
,
112 u8
* pn
, size_t dlen
, u8
* b0
, u8
* auth
, u8
* s0
)
117 int a4_included
, qc_included
;
118 u8 aad
[2 * AES_BLOCK_LEN
];
120 fc
= le16_to_cpu(hdr
->frame_ctl
);
121 a4_included
= ((fc
& (IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
)) ==
122 (IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
));
123 qc_included
= ((WLAN_FC_GET_TYPE(fc
) == IEEE80211_FTYPE_DATA
) &&
124 (WLAN_FC_GET_STYPE(fc
) & IEEE80211_STYPE_QOS_DATA
));
129 pos
= (u8
*) & hdr
->addr4
;
136 /* CCM Initial Block:
137 * Flag (Include authentication header, M=3 (8-octet MIC),
138 * L=1 (2-octet Dlen))
139 * Nonce: 0x00 | A2 | PN
143 memcpy(b0
+ 2, hdr
->addr2
, ETH_ALEN
);
144 memcpy(b0
+ 8, pn
, CCMP_PN_LEN
);
145 b0
[14] = (dlen
>> 8) & 0xff;
146 b0
[15] = dlen
& 0xff;
149 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
151 * SC with bits 4..15 (seq#) masked to zero
156 aad
[0] = 0; /* aad_len >> 8 */
157 aad
[1] = aad_len
& 0xff;
158 aad
[2] = pos
[0] & 0x8f;
159 aad
[3] = pos
[1] & 0xc7;
160 memcpy(aad
+ 4, hdr
->addr1
, 3 * ETH_ALEN
);
161 pos
= (u8
*) & hdr
->seq_ctl
;
162 aad
[22] = pos
[0] & 0x0f;
163 aad
[23] = 0; /* all bits masked */
164 memset(aad
+ 24, 0, 8);
166 memcpy(aad
+ 24, hdr
->addr4
, ETH_ALEN
);
168 aad
[a4_included
? 30 : 24] = qc
;
169 /* rest of QC masked */
172 /* Start with the first block and AAD */
173 ieee80211_ccmp_aes_encrypt(tfm
, b0
, auth
);
174 xor_block(auth
, aad
, AES_BLOCK_LEN
);
175 ieee80211_ccmp_aes_encrypt(tfm
, auth
, auth
);
176 xor_block(auth
, &aad
[AES_BLOCK_LEN
], AES_BLOCK_LEN
);
177 ieee80211_ccmp_aes_encrypt(tfm
, auth
, auth
);
180 ieee80211_ccmp_aes_encrypt(tfm
, b0
, s0
);
183 static int ieee80211_ccmp_hdr(struct sk_buff
*skb
, int hdr_len
,
184 u8
*aeskey
, int keylen
, void *priv
)
186 struct ieee80211_ccmp_data
*key
= priv
;
190 if (skb_headroom(skb
) < CCMP_HDR_LEN
|| skb
->len
< hdr_len
)
193 if (aeskey
!= NULL
&& keylen
>= CCMP_TK_LEN
)
194 memcpy(aeskey
, key
->key
, CCMP_TK_LEN
);
196 pos
= skb_push(skb
, CCMP_HDR_LEN
);
197 memmove(pos
, pos
+ CCMP_HDR_LEN
, hdr_len
);
203 if (key
->tx_pn
[i
] != 0)
208 *pos
++ = key
->tx_pn
[5];
209 *pos
++ = key
->tx_pn
[4];
211 *pos
++ = (key
->key_idx
<< 6) | (1 << 5) /* Ext IV included */ ;
212 *pos
++ = key
->tx_pn
[3];
213 *pos
++ = key
->tx_pn
[2];
214 *pos
++ = key
->tx_pn
[1];
215 *pos
++ = key
->tx_pn
[0];
220 static int ieee80211_ccmp_encrypt(struct sk_buff
*skb
, int hdr_len
, void *priv
)
222 struct ieee80211_ccmp_data
*key
= priv
;
223 int data_len
, i
, blocks
, last
, len
;
225 struct ieee80211_hdr_4addr
*hdr
;
231 if (skb_tailroom(skb
) < CCMP_MIC_LEN
|| skb
->len
< hdr_len
)
234 data_len
= skb
->len
- hdr_len
;
235 len
= ieee80211_ccmp_hdr(skb
, hdr_len
, NULL
, 0, priv
);
239 pos
= skb
->data
+ hdr_len
+ CCMP_HDR_LEN
;
240 mic
= skb_put(skb
, CCMP_MIC_LEN
);
241 hdr
= (struct ieee80211_hdr_4addr
*)skb
->data
;
242 ccmp_init_blocks(key
->tfm
, hdr
, key
->tx_pn
, data_len
, b0
, b
, s0
);
244 blocks
= (data_len
+ AES_BLOCK_LEN
- 1) / AES_BLOCK_LEN
;
245 last
= data_len
% AES_BLOCK_LEN
;
247 for (i
= 1; i
<= blocks
; i
++) {
248 len
= (i
== blocks
&& last
) ? last
: AES_BLOCK_LEN
;
250 xor_block(b
, pos
, len
);
251 ieee80211_ccmp_aes_encrypt(key
->tfm
, b
, b
);
252 /* Encryption, with counter */
253 b0
[14] = (i
>> 8) & 0xff;
255 ieee80211_ccmp_aes_encrypt(key
->tfm
, b0
, e
);
256 xor_block(pos
, e
, len
);
260 for (i
= 0; i
< CCMP_MIC_LEN
; i
++)
261 mic
[i
] = b
[i
] ^ s0
[i
];
266 static int ieee80211_ccmp_decrypt(struct sk_buff
*skb
, int hdr_len
, void *priv
)
268 struct ieee80211_ccmp_data
*key
= priv
;
270 struct ieee80211_hdr_4addr
*hdr
;
275 int i
, blocks
, last
, len
;
276 size_t data_len
= skb
->len
- hdr_len
- CCMP_HDR_LEN
- CCMP_MIC_LEN
;
277 u8
*mic
= skb
->data
+ skb
->len
- CCMP_MIC_LEN
;
279 if (skb
->len
< hdr_len
+ CCMP_HDR_LEN
+ CCMP_MIC_LEN
) {
280 key
->dot11RSNAStatsCCMPFormatErrors
++;
284 hdr
= (struct ieee80211_hdr_4addr
*)skb
->data
;
285 pos
= skb
->data
+ hdr_len
;
287 if (!(keyidx
& (1 << 5))) {
288 if (net_ratelimit()) {
289 printk(KERN_DEBUG
"CCMP: received packet without ExtIV"
290 " flag from " MAC_FMT
"\n", MAC_ARG(hdr
->addr2
));
292 key
->dot11RSNAStatsCCMPFormatErrors
++;
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
);
302 if (net_ratelimit()) {
303 printk(KERN_DEBUG
"CCMP: received packet from " MAC_FMT
304 " with keyid=%d that does not have a configured"
305 " key\n", MAC_ARG(hdr
->addr2
), keyidx
);
318 if (memcmp(pn
, key
->rx_pn
, CCMP_PN_LEN
) <= 0) {
319 if (net_ratelimit()) {
320 printk(KERN_DEBUG
"CCMP: replay detected: STA=" MAC_FMT
321 " previous PN %02x%02x%02x%02x%02x%02x "
322 "received PN %02x%02x%02x%02x%02x%02x\n",
323 MAC_ARG(hdr
->addr2
), MAC_ARG(key
->rx_pn
),
326 key
->dot11RSNAStatsCCMPReplays
++;
330 ccmp_init_blocks(key
->tfm
, hdr
, pn
, data_len
, b0
, a
, b
);
331 xor_block(mic
, b
, CCMP_MIC_LEN
);
333 blocks
= (data_len
+ AES_BLOCK_LEN
- 1) / AES_BLOCK_LEN
;
334 last
= data_len
% AES_BLOCK_LEN
;
336 for (i
= 1; i
<= blocks
; i
++) {
337 len
= (i
== blocks
&& last
) ? last
: AES_BLOCK_LEN
;
338 /* Decrypt, with counter */
339 b0
[14] = (i
>> 8) & 0xff;
341 ieee80211_ccmp_aes_encrypt(key
->tfm
, b0
, b
);
342 xor_block(pos
, b
, len
);
344 xor_block(a
, pos
, len
);
345 ieee80211_ccmp_aes_encrypt(key
->tfm
, a
, a
);
349 if (memcmp(mic
, a
, CCMP_MIC_LEN
) != 0) {
350 if (net_ratelimit()) {
351 printk(KERN_DEBUG
"CCMP: decrypt failed: STA="
352 MAC_FMT
"\n", MAC_ARG(hdr
->addr2
));
354 key
->dot11RSNAStatsCCMPDecryptErrors
++;
358 memcpy(key
->rx_pn
, pn
, CCMP_PN_LEN
);
360 /* Remove hdr and MIC */
361 memmove(skb
->data
+ CCMP_HDR_LEN
, skb
->data
, hdr_len
);
362 skb_pull(skb
, CCMP_HDR_LEN
);
363 skb_trim(skb
, skb
->len
- CCMP_MIC_LEN
);
368 static int ieee80211_ccmp_set_key(void *key
, int len
, u8
* seq
, void *priv
)
370 struct ieee80211_ccmp_data
*data
= priv
;
372 struct crypto_cipher
*tfm
= data
->tfm
;
374 keyidx
= data
->key_idx
;
375 memset(data
, 0, sizeof(*data
));
376 data
->key_idx
= keyidx
;
378 if (len
== CCMP_TK_LEN
) {
379 memcpy(data
->key
, key
, CCMP_TK_LEN
);
382 data
->rx_pn
[0] = seq
[5];
383 data
->rx_pn
[1] = seq
[4];
384 data
->rx_pn
[2] = seq
[3];
385 data
->rx_pn
[3] = seq
[2];
386 data
->rx_pn
[4] = seq
[1];
387 data
->rx_pn
[5] = seq
[0];
389 crypto_cipher_setkey(data
->tfm
, data
->key
, CCMP_TK_LEN
);
398 static int ieee80211_ccmp_get_key(void *key
, int len
, u8
* seq
, void *priv
)
400 struct ieee80211_ccmp_data
*data
= priv
;
402 if (len
< CCMP_TK_LEN
)
407 memcpy(key
, data
->key
, CCMP_TK_LEN
);
410 seq
[0] = data
->tx_pn
[5];
411 seq
[1] = data
->tx_pn
[4];
412 seq
[2] = data
->tx_pn
[3];
413 seq
[3] = data
->tx_pn
[2];
414 seq
[4] = data
->tx_pn
[1];
415 seq
[5] = data
->tx_pn
[0];
421 static char *ieee80211_ccmp_print_stats(char *p
, void *priv
)
423 struct ieee80211_ccmp_data
*ccmp
= priv
;
424 p
+= sprintf(p
, "key[%d] alg=CCMP key_set=%d "
425 "tx_pn=%02x%02x%02x%02x%02x%02x "
426 "rx_pn=%02x%02x%02x%02x%02x%02x "
427 "format_errors=%d replays=%d decrypt_errors=%d\n",
428 ccmp
->key_idx
, ccmp
->key_set
,
429 MAC_ARG(ccmp
->tx_pn
), MAC_ARG(ccmp
->rx_pn
),
430 ccmp
->dot11RSNAStatsCCMPFormatErrors
,
431 ccmp
->dot11RSNAStatsCCMPReplays
,
432 ccmp
->dot11RSNAStatsCCMPDecryptErrors
);
437 static struct ieee80211_crypto_ops ieee80211_crypt_ccmp
= {
439 .init
= ieee80211_ccmp_init
,
440 .deinit
= ieee80211_ccmp_deinit
,
441 .build_iv
= ieee80211_ccmp_hdr
,
442 .encrypt_mpdu
= ieee80211_ccmp_encrypt
,
443 .decrypt_mpdu
= ieee80211_ccmp_decrypt
,
444 .encrypt_msdu
= NULL
,
445 .decrypt_msdu
= NULL
,
446 .set_key
= ieee80211_ccmp_set_key
,
447 .get_key
= ieee80211_ccmp_get_key
,
448 .print_stats
= ieee80211_ccmp_print_stats
,
449 .extra_mpdu_prefix_len
= CCMP_HDR_LEN
,
450 .extra_mpdu_postfix_len
= CCMP_MIC_LEN
,
451 .owner
= THIS_MODULE
,
454 static int __init
ieee80211_crypto_ccmp_init(void)
456 return ieee80211_register_crypto_ops(&ieee80211_crypt_ccmp
);
459 static void __exit
ieee80211_crypto_ccmp_exit(void)
461 ieee80211_unregister_crypto_ops(&ieee80211_crypt_ccmp
);
464 module_init(ieee80211_crypto_ccmp_init
);
465 module_exit(ieee80211_crypto_ccmp_exit
);