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/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_tfm
*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 void ieee80211_ccmp_aes_encrypt(struct crypto_tfm
*tfm
,
61 const u8 pt
[16], u8 ct
[16])
63 struct scatterlist src
, dst
;
65 src
.page
= virt_to_page(pt
);
66 src
.offset
= offset_in_page(pt
);
67 src
.length
= AES_BLOCK_LEN
;
69 dst
.page
= virt_to_page(ct
);
70 dst
.offset
= offset_in_page(ct
);
71 dst
.length
= AES_BLOCK_LEN
;
73 crypto_cipher_encrypt(tfm
, &dst
, &src
, AES_BLOCK_LEN
);
76 static void *ieee80211_ccmp_init(int key_idx
)
78 struct ieee80211_ccmp_data
*priv
;
80 priv
= kmalloc(sizeof(*priv
), GFP_ATOMIC
);
83 memset(priv
, 0, sizeof(*priv
));
84 priv
->key_idx
= key_idx
;
86 priv
->tfm
= crypto_alloc_tfm("aes", 0);
87 if (priv
->tfm
== NULL
) {
88 printk(KERN_DEBUG
"ieee80211_crypt_ccmp: could not allocate "
98 crypto_free_tfm(priv
->tfm
);
105 static void ieee80211_ccmp_deinit(void *priv
)
107 struct ieee80211_ccmp_data
*_priv
= priv
;
108 if (_priv
&& _priv
->tfm
)
109 crypto_free_tfm(_priv
->tfm
);
113 static inline void xor_block(u8
* b
, u8
* a
, size_t len
)
116 for (i
= 0; i
< len
; i
++)
120 static void ccmp_init_blocks(struct crypto_tfm
*tfm
,
121 struct ieee80211_hdr_4addr
*hdr
,
122 u8
* pn
, size_t dlen
, u8
* b0
, u8
* auth
, u8
* s0
)
127 int a4_included
, qc_included
;
128 u8 aad
[2 * AES_BLOCK_LEN
];
130 fc
= le16_to_cpu(hdr
->frame_ctl
);
131 a4_included
= ((fc
& (IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
)) ==
132 (IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
));
133 qc_included
= ((WLAN_FC_GET_TYPE(fc
) == IEEE80211_FTYPE_DATA
) &&
134 (WLAN_FC_GET_STYPE(fc
) & IEEE80211_STYPE_QOS_DATA
));
139 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_hdr(struct sk_buff
*skb
, int hdr_len
, void *priv
)
195 struct ieee80211_ccmp_data
*key
= priv
;
199 if (skb_headroom(skb
) < CCMP_HDR_LEN
|| skb
->len
< hdr_len
)
202 pos
= skb_push(skb
, CCMP_HDR_LEN
);
203 memmove(pos
, pos
+ CCMP_HDR_LEN
, hdr_len
);
209 if (key
->tx_pn
[i
] != 0)
214 *pos
++ = key
->tx_pn
[5];
215 *pos
++ = key
->tx_pn
[4];
217 *pos
++ = (key
->key_idx
<< 6) | (1 << 5) /* Ext IV included */ ;
218 *pos
++ = key
->tx_pn
[3];
219 *pos
++ = key
->tx_pn
[2];
220 *pos
++ = key
->tx_pn
[1];
221 *pos
++ = key
->tx_pn
[0];
226 static int ieee80211_ccmp_encrypt(struct sk_buff
*skb
, int hdr_len
, void *priv
)
228 struct ieee80211_ccmp_data
*key
= priv
;
229 int data_len
, i
, blocks
, last
, len
;
231 struct ieee80211_hdr_4addr
*hdr
;
237 if (skb_tailroom(skb
) < CCMP_MIC_LEN
|| skb
->len
< hdr_len
)
240 data_len
= skb
->len
- hdr_len
;
241 len
= ieee80211_ccmp_hdr(skb
, hdr_len
, priv
);
245 pos
= skb
->data
+ hdr_len
+ CCMP_HDR_LEN
;
246 mic
= skb_put(skb
, CCMP_MIC_LEN
);
247 hdr
= (struct ieee80211_hdr_4addr
*)skb
->data
;
248 ccmp_init_blocks(key
->tfm
, hdr
, key
->tx_pn
, data_len
, b0
, b
, s0
);
250 blocks
= (data_len
+ AES_BLOCK_LEN
- 1) / AES_BLOCK_LEN
;
251 last
= data_len
% AES_BLOCK_LEN
;
253 for (i
= 1; i
<= blocks
; i
++) {
254 len
= (i
== blocks
&& last
) ? last
: AES_BLOCK_LEN
;
256 xor_block(b
, pos
, len
);
257 ieee80211_ccmp_aes_encrypt(key
->tfm
, b
, b
);
258 /* Encryption, with counter */
259 b0
[14] = (i
>> 8) & 0xff;
261 ieee80211_ccmp_aes_encrypt(key
->tfm
, b0
, e
);
262 xor_block(pos
, e
, len
);
266 for (i
= 0; i
< CCMP_MIC_LEN
; i
++)
267 mic
[i
] = b
[i
] ^ s0
[i
];
272 static int ieee80211_ccmp_decrypt(struct sk_buff
*skb
, int hdr_len
, void *priv
)
274 struct ieee80211_ccmp_data
*key
= priv
;
276 struct ieee80211_hdr_4addr
*hdr
;
281 int i
, blocks
, last
, len
;
282 size_t data_len
= skb
->len
- hdr_len
- CCMP_HDR_LEN
- CCMP_MIC_LEN
;
283 u8
*mic
= skb
->data
+ skb
->len
- CCMP_MIC_LEN
;
285 if (skb
->len
< hdr_len
+ CCMP_HDR_LEN
+ CCMP_MIC_LEN
) {
286 key
->dot11RSNAStatsCCMPFormatErrors
++;
290 hdr
= (struct ieee80211_hdr_4addr
*)skb
->data
;
291 pos
= skb
->data
+ hdr_len
;
293 if (!(keyidx
& (1 << 5))) {
294 if (net_ratelimit()) {
295 printk(KERN_DEBUG
"CCMP: received packet without ExtIV"
296 " flag from " MAC_FMT
"\n", MAC_ARG(hdr
->addr2
));
298 key
->dot11RSNAStatsCCMPFormatErrors
++;
302 if (key
->key_idx
!= keyidx
) {
303 printk(KERN_DEBUG
"CCMP: RX tkey->key_idx=%d frame "
304 "keyidx=%d priv=%p\n", key
->key_idx
, keyidx
, priv
);
308 if (net_ratelimit()) {
309 printk(KERN_DEBUG
"CCMP: received packet from " MAC_FMT
310 " with keyid=%d that does not have a configured"
311 " key\n", MAC_ARG(hdr
->addr2
), keyidx
);
324 if (memcmp(pn
, key
->rx_pn
, CCMP_PN_LEN
) <= 0) {
325 if (net_ratelimit()) {
326 printk(KERN_DEBUG
"CCMP: replay detected: STA=" MAC_FMT
327 " previous PN %02x%02x%02x%02x%02x%02x "
328 "received PN %02x%02x%02x%02x%02x%02x\n",
329 MAC_ARG(hdr
->addr2
), MAC_ARG(key
->rx_pn
),
332 key
->dot11RSNAStatsCCMPReplays
++;
336 ccmp_init_blocks(key
->tfm
, hdr
, pn
, data_len
, b0
, a
, b
);
337 xor_block(mic
, b
, CCMP_MIC_LEN
);
339 blocks
= (data_len
+ AES_BLOCK_LEN
- 1) / AES_BLOCK_LEN
;
340 last
= data_len
% AES_BLOCK_LEN
;
342 for (i
= 1; i
<= blocks
; i
++) {
343 len
= (i
== blocks
&& last
) ? last
: AES_BLOCK_LEN
;
344 /* Decrypt, with counter */
345 b0
[14] = (i
>> 8) & 0xff;
347 ieee80211_ccmp_aes_encrypt(key
->tfm
, b0
, b
);
348 xor_block(pos
, b
, len
);
350 xor_block(a
, pos
, len
);
351 ieee80211_ccmp_aes_encrypt(key
->tfm
, a
, a
);
355 if (memcmp(mic
, a
, CCMP_MIC_LEN
) != 0) {
356 if (net_ratelimit()) {
357 printk(KERN_DEBUG
"CCMP: decrypt failed: STA="
358 MAC_FMT
"\n", MAC_ARG(hdr
->addr2
));
360 key
->dot11RSNAStatsCCMPDecryptErrors
++;
364 memcpy(key
->rx_pn
, pn
, CCMP_PN_LEN
);
366 /* Remove hdr and MIC */
367 memmove(skb
->data
+ CCMP_HDR_LEN
, skb
->data
, hdr_len
);
368 skb_pull(skb
, CCMP_HDR_LEN
);
369 skb_trim(skb
, skb
->len
- CCMP_MIC_LEN
);
374 static int ieee80211_ccmp_set_key(void *key
, int len
, u8
* seq
, void *priv
)
376 struct ieee80211_ccmp_data
*data
= priv
;
378 struct crypto_tfm
*tfm
= data
->tfm
;
380 keyidx
= data
->key_idx
;
381 memset(data
, 0, sizeof(*data
));
382 data
->key_idx
= keyidx
;
384 if (len
== CCMP_TK_LEN
) {
385 memcpy(data
->key
, key
, CCMP_TK_LEN
);
388 data
->rx_pn
[0] = seq
[5];
389 data
->rx_pn
[1] = seq
[4];
390 data
->rx_pn
[2] = seq
[3];
391 data
->rx_pn
[3] = seq
[2];
392 data
->rx_pn
[4] = seq
[1];
393 data
->rx_pn
[5] = seq
[0];
395 crypto_cipher_setkey(data
->tfm
, data
->key
, CCMP_TK_LEN
);
404 static int ieee80211_ccmp_get_key(void *key
, int len
, u8
* seq
, void *priv
)
406 struct ieee80211_ccmp_data
*data
= priv
;
408 if (len
< CCMP_TK_LEN
)
413 memcpy(key
, data
->key
, CCMP_TK_LEN
);
416 seq
[0] = data
->tx_pn
[5];
417 seq
[1] = data
->tx_pn
[4];
418 seq
[2] = data
->tx_pn
[3];
419 seq
[3] = data
->tx_pn
[2];
420 seq
[4] = data
->tx_pn
[1];
421 seq
[5] = data
->tx_pn
[0];
427 static char *ieee80211_ccmp_print_stats(char *p
, void *priv
)
429 struct ieee80211_ccmp_data
*ccmp
= priv
;
430 p
+= sprintf(p
, "key[%d] alg=CCMP key_set=%d "
431 "tx_pn=%02x%02x%02x%02x%02x%02x "
432 "rx_pn=%02x%02x%02x%02x%02x%02x "
433 "format_errors=%d replays=%d decrypt_errors=%d\n",
434 ccmp
->key_idx
, ccmp
->key_set
,
435 MAC_ARG(ccmp
->tx_pn
), MAC_ARG(ccmp
->rx_pn
),
436 ccmp
->dot11RSNAStatsCCMPFormatErrors
,
437 ccmp
->dot11RSNAStatsCCMPReplays
,
438 ccmp
->dot11RSNAStatsCCMPDecryptErrors
);
443 static struct ieee80211_crypto_ops ieee80211_crypt_ccmp
= {
445 .init
= ieee80211_ccmp_init
,
446 .deinit
= ieee80211_ccmp_deinit
,
447 .build_iv
= ieee80211_ccmp_hdr
,
448 .encrypt_mpdu
= ieee80211_ccmp_encrypt
,
449 .decrypt_mpdu
= ieee80211_ccmp_decrypt
,
450 .encrypt_msdu
= NULL
,
451 .decrypt_msdu
= NULL
,
452 .set_key
= ieee80211_ccmp_set_key
,
453 .get_key
= ieee80211_ccmp_get_key
,
454 .print_stats
= ieee80211_ccmp_print_stats
,
455 .extra_mpdu_prefix_len
= CCMP_HDR_LEN
,
456 .extra_mpdu_postfix_len
= CCMP_MIC_LEN
,
457 .owner
= THIS_MODULE
,
460 static int __init
ieee80211_crypto_ccmp_init(void)
462 return ieee80211_register_crypto_ops(&ieee80211_crypt_ccmp
);
465 static void __exit
ieee80211_crypto_ccmp_exit(void)
467 ieee80211_unregister_crypto_ops(&ieee80211_crypt_ccmp
);
470 module_init(ieee80211_crypto_ccmp_init
);
471 module_exit(ieee80211_crypto_ccmp_exit
);