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/version.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 "ieee80211.h"
26 #include <linux/crypto.h>
27 #include <linux/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 void ieee80211_ccmp_aes_encrypt(struct crypto_tfm
*tfm
,
61 const u8 pt
[16], u8 ct
[16])
63 crypto_cipher_encrypt_one((void*)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
= (void *)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((void*)priv
->tfm
);
96 static void ieee80211_ccmp_deinit(void *priv
)
98 struct ieee80211_ccmp_data
*_priv
= priv
;
100 if (_priv
&& _priv
->tfm
)
101 crypto_free_cipher((void*)_priv
->tfm
);
106 static inline void xor_block(u8
*b
, u8
*a
, size_t len
)
109 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 qc_included
= ((WLAN_FC_GET_TYPE(fc
) == IEEE80211_FTYPE_DATA
) &&
134 (WLAN_FC_GET_STYPE(fc
) & 0x80));
139 pos
= (u8
*) &hdr
->addr4
;
145 /* CCM Initial Block:
146 * Flag (Include authentication header, M=3 (8-octet MIC),
147 * L=1 (2-octet Dlen))
148 * Nonce: 0x00 | A2 | PN
152 memcpy(b0
+ 2, hdr
->addr2
, ETH_ALEN
);
153 memcpy(b0
+ 8, pn
, CCMP_PN_LEN
);
154 b0
[14] = (dlen
>> 8) & 0xff;
155 b0
[15] = dlen
& 0xff;
158 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
160 * SC with bits 4..15 (seq#) masked to zero
165 aad
[0] = 0; /* aad_len >> 8 */
166 aad
[1] = aad_len
& 0xff;
167 aad
[2] = pos
[0] & 0x8f;
168 aad
[3] = pos
[1] & 0xc7;
169 memcpy(aad
+ 4, hdr
->addr1
, 3 * ETH_ALEN
);
170 pos
= (u8
*) &hdr
->seq_ctl
;
171 aad
[22] = pos
[0] & 0x0f;
172 aad
[23] = 0; /* all bits masked */
173 memset(aad
+ 24, 0, 8);
175 memcpy(aad
+ 24, hdr
->addr4
, ETH_ALEN
);
177 aad
[a4_included
? 30 : 24] = qc
;
178 /* rest of QC masked */
181 /* Start with the first block and AAD */
182 ieee80211_ccmp_aes_encrypt(tfm
, b0
, auth
);
183 xor_block(auth
, aad
, AES_BLOCK_LEN
);
184 ieee80211_ccmp_aes_encrypt(tfm
, auth
, auth
);
185 xor_block(auth
, &aad
[AES_BLOCK_LEN
], AES_BLOCK_LEN
);
186 ieee80211_ccmp_aes_encrypt(tfm
, auth
, auth
);
189 ieee80211_ccmp_aes_encrypt(tfm
, b0
, s0
);
194 static int ieee80211_ccmp_encrypt(struct sk_buff
*skb
, int hdr_len
, void *priv
)
196 struct ieee80211_ccmp_data
*key
= priv
;
199 struct ieee80211_hdr_4addr
*hdr
;
200 cb_desc
*tcb_desc
= (cb_desc
*)(skb
->cb
+ MAX_DEV_ADDR_SIZE
);
202 if (skb_headroom(skb
) < CCMP_HDR_LEN
||
203 skb_tailroom(skb
) < CCMP_MIC_LEN
||
207 data_len
= skb
->len
- hdr_len
;
208 pos
= skb_push(skb
, CCMP_HDR_LEN
);
209 memmove(pos
, pos
+ CCMP_HDR_LEN
, hdr_len
);
215 if (key
->tx_pn
[i
] != 0)
220 *pos
++ = key
->tx_pn
[5];
221 *pos
++ = key
->tx_pn
[4];
223 *pos
++ = (key
->key_idx
<< 6) | (1 << 5) /* Ext IV included */;
224 *pos
++ = key
->tx_pn
[3];
225 *pos
++ = key
->tx_pn
[2];
226 *pos
++ = key
->tx_pn
[1];
227 *pos
++ = key
->tx_pn
[0];
230 hdr
= (struct ieee80211_hdr_4addr
*) skb
->data
;
231 if (!tcb_desc
->bHwSec
)
233 int blocks
, last
, len
;
240 mic
= skb_put(skb
, CCMP_MIC_LEN
);
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
];
267 static int ieee80211_ccmp_decrypt(struct sk_buff
*skb
, int hdr_len
, void *priv
)
269 struct ieee80211_ccmp_data
*key
= priv
;
271 struct ieee80211_hdr_4addr
*hdr
;
272 cb_desc
*tcb_desc
= (cb_desc
*)(skb
->cb
+ MAX_DEV_ADDR_SIZE
);
275 if (skb
->len
< hdr_len
+ CCMP_HDR_LEN
+ CCMP_MIC_LEN
) {
276 key
->dot11RSNAStatsCCMPFormatErrors
++;
280 hdr
= (struct ieee80211_hdr_4addr
*) skb
->data
;
281 pos
= skb
->data
+ hdr_len
;
283 if (!(keyidx
& (1 << 5))) {
284 if (net_ratelimit()) {
285 printk(KERN_DEBUG
"CCMP: received packet without ExtIV"
286 " flag from %pM\n", hdr
->addr2
);
288 key
->dot11RSNAStatsCCMPFormatErrors
++;
292 if (key
->key_idx
!= keyidx
) {
293 printk(KERN_DEBUG
"CCMP: RX tkey->key_idx=%d frame "
294 "keyidx=%d priv=%p\n", key
->key_idx
, keyidx
, priv
);
298 if (net_ratelimit()) {
299 printk(KERN_DEBUG
"CCMP: received packet from %pM"
300 " with keyid=%d that does not have a configured"
301 " key\n", hdr
->addr2
, keyidx
);
314 if (memcmp(pn
, key
->rx_pn
, CCMP_PN_LEN
) <= 0) {
315 if (net_ratelimit()) {
316 printk(KERN_DEBUG
"CCMP: replay detected: STA=%pM"
317 " previous PN %pm received PN %pm\n",
318 hdr
->addr2
, key
->rx_pn
, pn
);
320 key
->dot11RSNAStatsCCMPReplays
++;
323 if (!tcb_desc
->bHwSec
)
325 size_t data_len
= skb
->len
- hdr_len
- CCMP_HDR_LEN
- CCMP_MIC_LEN
;
326 u8
*mic
= skb
->data
+ skb
->len
- CCMP_MIC_LEN
;
330 int i
, blocks
, last
, len
;
333 ccmp_init_blocks(key
->tfm
, hdr
, pn
, data_len
, b0
, a
, b
);
334 xor_block(mic
, b
, CCMP_MIC_LEN
);
336 blocks
= (data_len
+ AES_BLOCK_LEN
- 1) / AES_BLOCK_LEN
;
337 last
= data_len
% AES_BLOCK_LEN
;
339 for (i
= 1; i
<= blocks
; i
++) {
340 len
= (i
== blocks
&& last
) ? last
: AES_BLOCK_LEN
;
341 /* Decrypt, with counter */
342 b0
[14] = (i
>> 8) & 0xff;
344 ieee80211_ccmp_aes_encrypt(key
->tfm
, b0
, b
);
345 xor_block(pos
, b
, len
);
347 xor_block(a
, pos
, len
);
348 ieee80211_ccmp_aes_encrypt(key
->tfm
, a
, a
);
352 if (memcmp(mic
, a
, CCMP_MIC_LEN
) != 0) {
353 if (net_ratelimit()) {
354 printk(KERN_DEBUG
"CCMP: decrypt failed: STA="
355 "%pM\n", hdr
->addr2
);
357 key
->dot11RSNAStatsCCMPDecryptErrors
++;
361 memcpy(key
->rx_pn
, pn
, CCMP_PN_LEN
);
363 /* Remove hdr and MIC */
364 memmove(skb
->data
+ CCMP_HDR_LEN
, skb
->data
, hdr_len
);
365 skb_pull(skb
, CCMP_HDR_LEN
);
366 skb_trim(skb
, skb
->len
- CCMP_MIC_LEN
);
372 static int ieee80211_ccmp_set_key(void *key
, int len
, u8
*seq
, void *priv
)
374 struct ieee80211_ccmp_data
*data
= priv
;
376 struct crypto_tfm
*tfm
= data
->tfm
;
378 keyidx
= data
->key_idx
;
379 memset(data
, 0, sizeof(*data
));
380 data
->key_idx
= keyidx
;
382 if (len
== CCMP_TK_LEN
) {
383 memcpy(data
->key
, key
, CCMP_TK_LEN
);
386 data
->rx_pn
[0] = seq
[5];
387 data
->rx_pn
[1] = seq
[4];
388 data
->rx_pn
[2] = seq
[3];
389 data
->rx_pn
[3] = seq
[2];
390 data
->rx_pn
[4] = seq
[1];
391 data
->rx_pn
[5] = seq
[0];
393 crypto_cipher_setkey((void*)data
->tfm
, data
->key
, CCMP_TK_LEN
);
403 static int ieee80211_ccmp_get_key(void *key
, int len
, u8
*seq
, void *priv
)
405 struct ieee80211_ccmp_data
*data
= priv
;
407 if (len
< CCMP_TK_LEN
)
412 memcpy(key
, data
->key
, CCMP_TK_LEN
);
415 seq
[0] = data
->tx_pn
[5];
416 seq
[1] = data
->tx_pn
[4];
417 seq
[2] = data
->tx_pn
[3];
418 seq
[3] = data
->tx_pn
[2];
419 seq
[4] = data
->tx_pn
[1];
420 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=%pm rx_pn=%pm "
432 "format_errors=%d replays=%d decrypt_errors=%d\n",
433 ccmp
->key_idx
, ccmp
->key_set
,
434 ccmp
->tx_pn
, ccmp
->rx_pn
,
435 ccmp
->dot11RSNAStatsCCMPFormatErrors
,
436 ccmp
->dot11RSNAStatsCCMPReplays
,
437 ccmp
->dot11RSNAStatsCCMPDecryptErrors
);
442 void ieee80211_ccmp_null(void)
447 static struct ieee80211_crypto_ops ieee80211_crypt_ccmp
= {
449 .init
= ieee80211_ccmp_init
,
450 .deinit
= ieee80211_ccmp_deinit
,
451 .encrypt_mpdu
= ieee80211_ccmp_encrypt
,
452 .decrypt_mpdu
= ieee80211_ccmp_decrypt
,
453 .encrypt_msdu
= NULL
,
454 .decrypt_msdu
= NULL
,
455 .set_key
= ieee80211_ccmp_set_key
,
456 .get_key
= ieee80211_ccmp_get_key
,
457 .print_stats
= ieee80211_ccmp_print_stats
,
458 .extra_prefix_len
= CCMP_HDR_LEN
,
459 .extra_postfix_len
= CCMP_MIC_LEN
,
460 .owner
= THIS_MODULE
,
463 int __init
ieee80211_crypto_ccmp_init(void)
465 return ieee80211_register_crypto_ops(&ieee80211_crypt_ccmp
);
468 void ieee80211_crypto_ccmp_exit(void)
470 ieee80211_unregister_crypto_ops(&ieee80211_crypt_ccmp
);