2 * lib80211 crypt: host-based CCMP encryption implementation for lib80211
4 * Copyright (c) 2003-2004, Jouni Malinen <j@w1.fi>
5 * Copyright (c) 2008, John W. Linville <linville@tuxdriver.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation. See README and COPYING for
13 #include <linux/kernel.h>
14 #include <linux/err.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/random.h>
19 #include <linux/skbuff.h>
20 #include <linux/netdevice.h>
21 #include <linux/if_ether.h>
22 #include <linux/if_arp.h>
23 #include <asm/string.h>
24 #include <linux/wireless.h>
26 #include <linux/ieee80211.h>
28 #include <linux/crypto.h>
30 #include <net/lib80211.h>
32 MODULE_AUTHOR("Jouni Malinen");
33 MODULE_DESCRIPTION("Host AP crypt: CCMP");
34 MODULE_LICENSE("GPL");
36 #define AES_BLOCK_LEN 16
37 #define CCMP_HDR_LEN 8
38 #define CCMP_MIC_LEN 8
39 #define CCMP_TK_LEN 16
42 struct lib80211_ccmp_data
{
46 u8 tx_pn
[CCMP_PN_LEN
];
47 u8 rx_pn
[CCMP_PN_LEN
];
49 u32 dot11RSNAStatsCCMPFormatErrors
;
50 u32 dot11RSNAStatsCCMPReplays
;
51 u32 dot11RSNAStatsCCMPDecryptErrors
;
55 struct crypto_cipher
*tfm
;
57 /* scratch buffers for virt_to_page() (crypto API) */
58 u8 tx_b0
[AES_BLOCK_LEN
], tx_b
[AES_BLOCK_LEN
],
59 tx_e
[AES_BLOCK_LEN
], tx_s0
[AES_BLOCK_LEN
];
60 u8 rx_b0
[AES_BLOCK_LEN
], rx_b
[AES_BLOCK_LEN
], rx_a
[AES_BLOCK_LEN
];
63 static inline void lib80211_ccmp_aes_encrypt(struct crypto_cipher
*tfm
,
64 const u8 pt
[16], u8 ct
[16])
66 crypto_cipher_encrypt_one(tfm
, ct
, pt
);
69 static void *lib80211_ccmp_init(int key_idx
)
71 struct lib80211_ccmp_data
*priv
;
73 priv
= kzalloc(sizeof(*priv
), GFP_ATOMIC
);
76 priv
->key_idx
= key_idx
;
78 priv
->tfm
= crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC
);
79 if (IS_ERR(priv
->tfm
)) {
89 crypto_free_cipher(priv
->tfm
);
96 static void lib80211_ccmp_deinit(void *priv
)
98 struct lib80211_ccmp_data
*_priv
= priv
;
99 if (_priv
&& _priv
->tfm
)
100 crypto_free_cipher(_priv
->tfm
);
104 static inline void xor_block(u8
* b
, u8
* a
, size_t len
)
107 for (i
= 0; i
< len
; i
++)
111 static void ccmp_init_blocks(struct crypto_cipher
*tfm
,
112 struct ieee80211_hdr
*hdr
,
113 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 a4_included
= ieee80211_has_a4(hdr
->frame_control
);
121 qc_included
= ieee80211_is_data_qos(hdr
->frame_control
);
127 pos
= (u8
*) & hdr
->addr4
;
134 /* CCM Initial Block:
135 * Flag (Include authentication header, M=3 (8-octet MIC),
136 * L=1 (2-octet Dlen))
137 * Nonce: 0x00 | A2 | PN
141 memcpy(b0
+ 2, hdr
->addr2
, ETH_ALEN
);
142 memcpy(b0
+ 8, pn
, CCMP_PN_LEN
);
143 b0
[14] = (dlen
>> 8) & 0xff;
144 b0
[15] = dlen
& 0xff;
147 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
149 * SC with bits 4..15 (seq#) masked to zero
154 aad
[0] = 0; /* aad_len >> 8 */
155 aad
[1] = aad_len
& 0xff;
156 aad
[2] = pos
[0] & 0x8f;
157 aad
[3] = pos
[1] & 0xc7;
158 memcpy(aad
+ 4, hdr
->addr1
, 3 * ETH_ALEN
);
159 pos
= (u8
*) & hdr
->seq_ctrl
;
160 aad
[22] = pos
[0] & 0x0f;
161 aad
[23] = 0; /* all bits masked */
162 memset(aad
+ 24, 0, 8);
164 memcpy(aad
+ 24, hdr
->addr4
, ETH_ALEN
);
166 aad
[a4_included
? 30 : 24] = qc
;
167 /* rest of QC masked */
170 /* Start with the first block and AAD */
171 lib80211_ccmp_aes_encrypt(tfm
, b0
, auth
);
172 xor_block(auth
, aad
, AES_BLOCK_LEN
);
173 lib80211_ccmp_aes_encrypt(tfm
, auth
, auth
);
174 xor_block(auth
, &aad
[AES_BLOCK_LEN
], AES_BLOCK_LEN
);
175 lib80211_ccmp_aes_encrypt(tfm
, auth
, auth
);
178 lib80211_ccmp_aes_encrypt(tfm
, b0
, s0
);
181 static int lib80211_ccmp_hdr(struct sk_buff
*skb
, int hdr_len
,
182 u8
*aeskey
, int keylen
, void *priv
)
184 struct lib80211_ccmp_data
*key
= priv
;
188 if (skb_headroom(skb
) < CCMP_HDR_LEN
|| skb
->len
< hdr_len
)
191 if (aeskey
!= NULL
&& keylen
>= CCMP_TK_LEN
)
192 memcpy(aeskey
, key
->key
, CCMP_TK_LEN
);
194 pos
= skb_push(skb
, CCMP_HDR_LEN
);
195 memmove(pos
, pos
+ CCMP_HDR_LEN
, hdr_len
);
201 if (key
->tx_pn
[i
] != 0)
206 *pos
++ = key
->tx_pn
[5];
207 *pos
++ = key
->tx_pn
[4];
209 *pos
++ = (key
->key_idx
<< 6) | (1 << 5) /* Ext IV included */ ;
210 *pos
++ = key
->tx_pn
[3];
211 *pos
++ = key
->tx_pn
[2];
212 *pos
++ = key
->tx_pn
[1];
213 *pos
++ = key
->tx_pn
[0];
218 static int lib80211_ccmp_encrypt(struct sk_buff
*skb
, int hdr_len
, void *priv
)
220 struct lib80211_ccmp_data
*key
= priv
;
221 int data_len
, i
, blocks
, last
, len
;
223 struct ieee80211_hdr
*hdr
;
229 if (skb_tailroom(skb
) < CCMP_MIC_LEN
|| skb
->len
< hdr_len
)
232 data_len
= skb
->len
- hdr_len
;
233 len
= lib80211_ccmp_hdr(skb
, hdr_len
, NULL
, 0, priv
);
237 pos
= skb
->data
+ hdr_len
+ CCMP_HDR_LEN
;
238 hdr
= (struct ieee80211_hdr
*)skb
->data
;
239 ccmp_init_blocks(key
->tfm
, hdr
, key
->tx_pn
, data_len
, b0
, b
, s0
);
241 blocks
= DIV_ROUND_UP(data_len
, AES_BLOCK_LEN
);
242 last
= data_len
% AES_BLOCK_LEN
;
244 for (i
= 1; i
<= blocks
; i
++) {
245 len
= (i
== blocks
&& last
) ? last
: AES_BLOCK_LEN
;
247 xor_block(b
, pos
, len
);
248 lib80211_ccmp_aes_encrypt(key
->tfm
, b
, b
);
249 /* Encryption, with counter */
250 b0
[14] = (i
>> 8) & 0xff;
252 lib80211_ccmp_aes_encrypt(key
->tfm
, b0
, e
);
253 xor_block(pos
, e
, len
);
257 mic
= skb_put(skb
, CCMP_MIC_LEN
);
258 for (i
= 0; i
< CCMP_MIC_LEN
; i
++)
259 mic
[i
] = b
[i
] ^ s0
[i
];
265 * deal with seq counter wrapping correctly.
266 * refer to timer_after() for jiffies wrapping handling
268 static inline int ccmp_replay_check(u8
*pn_n
, u8
*pn_o
)
273 iv32_n
= (pn_n
[0] << 24) | (pn_n
[1] << 16) | (pn_n
[2] << 8) | pn_n
[3];
274 iv16_n
= (pn_n
[4] << 8) | pn_n
[5];
276 iv32_o
= (pn_o
[0] << 24) | (pn_o
[1] << 16) | (pn_o
[2] << 8) | pn_o
[3];
277 iv16_o
= (pn_o
[4] << 8) | pn_o
[5];
279 if ((s32
)iv32_n
- (s32
)iv32_o
< 0 ||
280 (iv32_n
== iv32_o
&& iv16_n
<= iv16_o
))
285 static int lib80211_ccmp_decrypt(struct sk_buff
*skb
, int hdr_len
, void *priv
)
287 struct lib80211_ccmp_data
*key
= priv
;
289 struct ieee80211_hdr
*hdr
;
294 int i
, blocks
, last
, len
;
295 size_t data_len
= skb
->len
- hdr_len
- CCMP_HDR_LEN
- CCMP_MIC_LEN
;
296 u8
*mic
= skb
->data
+ skb
->len
- CCMP_MIC_LEN
;
298 if (skb
->len
< hdr_len
+ CCMP_HDR_LEN
+ CCMP_MIC_LEN
) {
299 key
->dot11RSNAStatsCCMPFormatErrors
++;
303 hdr
= (struct ieee80211_hdr
*)skb
->data
;
304 pos
= skb
->data
+ hdr_len
;
306 if (!(keyidx
& (1 << 5))) {
307 if (net_ratelimit()) {
308 printk(KERN_DEBUG
"CCMP: received packet without ExtIV"
309 " flag from %pM\n", hdr
->addr2
);
311 key
->dot11RSNAStatsCCMPFormatErrors
++;
315 if (key
->key_idx
!= keyidx
) {
316 printk(KERN_DEBUG
"CCMP: RX tkey->key_idx=%d frame "
317 "keyidx=%d priv=%p\n", key
->key_idx
, keyidx
, priv
);
321 if (net_ratelimit()) {
322 printk(KERN_DEBUG
"CCMP: received packet from %pM"
323 " with keyid=%d that does not have a configured"
324 " key\n", hdr
->addr2
, keyidx
);
337 if (ccmp_replay_check(pn
, key
->rx_pn
)) {
338 #ifdef CONFIG_LIB80211_DEBUG
339 if (net_ratelimit()) {
340 printk(KERN_DEBUG
"CCMP: replay detected: STA=%pM "
341 "previous PN %02x%02x%02x%02x%02x%02x "
342 "received PN %02x%02x%02x%02x%02x%02x\n",
344 key
->rx_pn
[0], key
->rx_pn
[1], key
->rx_pn
[2],
345 key
->rx_pn
[3], key
->rx_pn
[4], key
->rx_pn
[5],
346 pn
[0], pn
[1], pn
[2], pn
[3], pn
[4], pn
[5]);
349 key
->dot11RSNAStatsCCMPReplays
++;
353 ccmp_init_blocks(key
->tfm
, hdr
, pn
, data_len
, b0
, a
, b
);
354 xor_block(mic
, b
, CCMP_MIC_LEN
);
356 blocks
= DIV_ROUND_UP(data_len
, AES_BLOCK_LEN
);
357 last
= data_len
% AES_BLOCK_LEN
;
359 for (i
= 1; i
<= blocks
; i
++) {
360 len
= (i
== blocks
&& last
) ? last
: AES_BLOCK_LEN
;
361 /* Decrypt, with counter */
362 b0
[14] = (i
>> 8) & 0xff;
364 lib80211_ccmp_aes_encrypt(key
->tfm
, b0
, b
);
365 xor_block(pos
, b
, len
);
367 xor_block(a
, pos
, len
);
368 lib80211_ccmp_aes_encrypt(key
->tfm
, a
, a
);
372 if (memcmp(mic
, a
, CCMP_MIC_LEN
) != 0) {
373 if (net_ratelimit()) {
374 printk(KERN_DEBUG
"CCMP: decrypt failed: STA="
375 "%pM\n", hdr
->addr2
);
377 key
->dot11RSNAStatsCCMPDecryptErrors
++;
381 memcpy(key
->rx_pn
, pn
, CCMP_PN_LEN
);
383 /* Remove hdr and MIC */
384 memmove(skb
->data
+ CCMP_HDR_LEN
, skb
->data
, hdr_len
);
385 skb_pull(skb
, CCMP_HDR_LEN
);
386 skb_trim(skb
, skb
->len
- CCMP_MIC_LEN
);
391 static int lib80211_ccmp_set_key(void *key
, int len
, u8
* seq
, void *priv
)
393 struct lib80211_ccmp_data
*data
= priv
;
395 struct crypto_cipher
*tfm
= data
->tfm
;
397 keyidx
= data
->key_idx
;
398 memset(data
, 0, sizeof(*data
));
399 data
->key_idx
= keyidx
;
401 if (len
== CCMP_TK_LEN
) {
402 memcpy(data
->key
, key
, CCMP_TK_LEN
);
405 data
->rx_pn
[0] = seq
[5];
406 data
->rx_pn
[1] = seq
[4];
407 data
->rx_pn
[2] = seq
[3];
408 data
->rx_pn
[3] = seq
[2];
409 data
->rx_pn
[4] = seq
[1];
410 data
->rx_pn
[5] = seq
[0];
412 crypto_cipher_setkey(data
->tfm
, data
->key
, CCMP_TK_LEN
);
421 static int lib80211_ccmp_get_key(void *key
, int len
, u8
* seq
, void *priv
)
423 struct lib80211_ccmp_data
*data
= priv
;
425 if (len
< CCMP_TK_LEN
)
430 memcpy(key
, data
->key
, CCMP_TK_LEN
);
433 seq
[0] = data
->tx_pn
[5];
434 seq
[1] = data
->tx_pn
[4];
435 seq
[2] = data
->tx_pn
[3];
436 seq
[3] = data
->tx_pn
[2];
437 seq
[4] = data
->tx_pn
[1];
438 seq
[5] = data
->tx_pn
[0];
444 static char *lib80211_ccmp_print_stats(char *p
, void *priv
)
446 struct lib80211_ccmp_data
*ccmp
= priv
;
448 p
+= sprintf(p
, "key[%d] alg=CCMP key_set=%d "
449 "tx_pn=%02x%02x%02x%02x%02x%02x "
450 "rx_pn=%02x%02x%02x%02x%02x%02x "
451 "format_errors=%d replays=%d decrypt_errors=%d\n",
452 ccmp
->key_idx
, ccmp
->key_set
,
453 ccmp
->tx_pn
[0], ccmp
->tx_pn
[1], ccmp
->tx_pn
[2],
454 ccmp
->tx_pn
[3], ccmp
->tx_pn
[4], ccmp
->tx_pn
[5],
455 ccmp
->rx_pn
[0], ccmp
->rx_pn
[1], ccmp
->rx_pn
[2],
456 ccmp
->rx_pn
[3], ccmp
->rx_pn
[4], ccmp
->rx_pn
[5],
457 ccmp
->dot11RSNAStatsCCMPFormatErrors
,
458 ccmp
->dot11RSNAStatsCCMPReplays
,
459 ccmp
->dot11RSNAStatsCCMPDecryptErrors
);
464 static struct lib80211_crypto_ops lib80211_crypt_ccmp
= {
466 .init
= lib80211_ccmp_init
,
467 .deinit
= lib80211_ccmp_deinit
,
468 .encrypt_mpdu
= lib80211_ccmp_encrypt
,
469 .decrypt_mpdu
= lib80211_ccmp_decrypt
,
470 .encrypt_msdu
= NULL
,
471 .decrypt_msdu
= NULL
,
472 .set_key
= lib80211_ccmp_set_key
,
473 .get_key
= lib80211_ccmp_get_key
,
474 .print_stats
= lib80211_ccmp_print_stats
,
475 .extra_mpdu_prefix_len
= CCMP_HDR_LEN
,
476 .extra_mpdu_postfix_len
= CCMP_MIC_LEN
,
477 .owner
= THIS_MODULE
,
480 static int __init
lib80211_crypto_ccmp_init(void)
482 return lib80211_register_crypto_ops(&lib80211_crypt_ccmp
);
485 static void __exit
lib80211_crypto_ccmp_exit(void)
487 lib80211_unregister_crypto_ops(&lib80211_crypt_ccmp
);
490 module_init(lib80211_crypto_ccmp_init
);
491 module_exit(lib80211_crypto_ccmp_exit
);