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");
39 MODULE_VERSION(PRISM2_VERSION);
42 #define AES_BLOCK_LEN 16
43 #define CCMP_HDR_LEN 8
44 #define CCMP_MIC_LEN 8
45 #define CCMP_TK_LEN 16
49 struct hostap_ccmp_data {
53 u8 tx_pn[CCMP_PN_LEN];
54 u8 rx_pn[CCMP_PN_LEN];
56 u32 dot11RSNAStatsCCMPFormatErrors;
57 u32 dot11RSNAStatsCCMPReplays;
58 u32 dot11RSNAStatsCCMPDecryptErrors;
62 struct crypto_tfm *tfm;
64 /* scratch buffers for virt_to_page() (crypto API) */
65 u8 tx_b0[AES_BLOCK_LEN], tx_b[AES_BLOCK_LEN],
66 tx_e[AES_BLOCK_LEN], tx_s0[AES_BLOCK_LEN];
67 u8 rx_b0[AES_BLOCK_LEN], rx_b[AES_BLOCK_LEN], rx_a[AES_BLOCK_LEN];
71 void hostap_ccmp_aes_encrypt(struct crypto_tfm *tfm,
72 const u8 pt[16], u8 ct[16])
74 struct scatterlist src, dst;
76 src.page = virt_to_page(pt);
77 src.offset = offset_in_page(pt);
78 src.length = AES_BLOCK_LEN;
80 dst.page = virt_to_page(ct);
81 dst.offset = offset_in_page(ct);
82 dst.length = AES_BLOCK_LEN;
84 crypto_cipher_encrypt(tfm, &dst, &src, AES_BLOCK_LEN);
88 static void * hostap_ccmp_init(int key_idx)
90 struct hostap_ccmp_data *priv;
92 if (!try_module_get(THIS_MODULE))
95 priv = (struct hostap_ccmp_data *) kmalloc(sizeof(*priv), GFP_ATOMIC);
99 memset(priv, 0, sizeof(*priv));
100 priv->key_idx = key_idx;
102 priv->tfm = crypto_alloc_tfm("aes", 0);
103 if (priv->tfm == NULL) {
104 printk(KERN_DEBUG "hostap_crypt_ccmp: could not allocate "
114 crypto_free_tfm(priv->tfm);
117 module_put(THIS_MODULE);
122 static void hostap_ccmp_deinit(void *priv)
124 struct hostap_ccmp_data *_priv = priv;
125 if (_priv && _priv->tfm)
126 crypto_free_tfm(_priv->tfm);
128 module_put(THIS_MODULE);
132 static inline void xor_block(u8 *b, u8 *a, size_t len)
135 for (i = 0; i < len; i++)
140 static void ccmp_init_blocks(struct crypto_tfm *tfm,
141 struct hostap_ieee80211_hdr *hdr,
142 u8 *pn, size_t dlen, u8 *b0, u8 *auth,
148 int a4_included, qc_included;
149 u8 aad[2 * AES_BLOCK_LEN];
151 fc = le16_to_cpu(hdr->frame_control);
152 a4_included = ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
153 (WLAN_FC_TODS | WLAN_FC_FROMDS));
154 qc_included = ((HOSTAP_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA) &&
155 (HOSTAP_FC_GET_STYPE(fc) & 0x08));
160 pos = (u8 *) &hdr->addr4;
167 /* CCM Initial Block:
168 * Flag (Include authentication header, M=3 (8-octet MIC),
169 * L=1 (2-octet Dlen))
170 * Nonce: 0x00 | A2 | PN
174 memcpy(b0 + 2, hdr->addr2, ETH_ALEN);
175 memcpy(b0 + 8, pn, CCMP_PN_LEN);
176 b0[14] = (dlen >> 8) & 0xff;
177 b0[15] = dlen & 0xff;
180 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
182 * SC with bits 4..15 (seq#) masked to zero
187 aad[0] = 0; /* aad_len >> 8 */
188 aad[1] = aad_len & 0xff;
189 aad[2] = pos[0] & 0x8f;
190 aad[3] = pos[1] & 0xc7;
191 memcpy(aad + 4, hdr->addr1, 3 * ETH_ALEN);
192 pos = (u8 *) &hdr->seq_ctrl;
193 aad[22] = pos[0] & 0x0f;
194 aad[23] = 0; /* all bits masked */
195 memset(aad + 24, 0, 8);
197 memcpy(aad + 24, hdr->addr4, ETH_ALEN);
199 aad[a4_included ? 30 : 24] = qc;
200 /* rest of QC masked */
203 /* Start with the first block and AAD */
204 hostap_ccmp_aes_encrypt(tfm, b0, auth);
205 xor_block(auth, aad, AES_BLOCK_LEN);
206 hostap_ccmp_aes_encrypt(tfm, auth, auth);
207 xor_block(auth, &aad[AES_BLOCK_LEN], AES_BLOCK_LEN);
208 hostap_ccmp_aes_encrypt(tfm, auth, auth);
211 hostap_ccmp_aes_encrypt(tfm, b0, s0);
215 static int hostap_ccmp_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
217 struct hostap_ccmp_data *key = priv;
218 int data_len, i, blocks, last, len;
220 struct hostap_ieee80211_hdr *hdr;
226 if (skb_headroom(skb) < CCMP_HDR_LEN ||
227 skb_tailroom(skb) < CCMP_MIC_LEN ||
231 data_len = skb->len - hdr_len;
232 pos = skb_push(skb, CCMP_HDR_LEN);
233 memmove(pos, pos + CCMP_HDR_LEN, hdr_len);
235 mic = skb_put(skb, CCMP_MIC_LEN);
240 if (key->tx_pn[i] != 0)
245 *pos++ = key->tx_pn[5];
246 *pos++ = key->tx_pn[4];
248 *pos++ = (key->key_idx << 6) | (1 << 5) /* Ext IV included */;
249 *pos++ = key->tx_pn[3];
250 *pos++ = key->tx_pn[2];
251 *pos++ = key->tx_pn[1];
252 *pos++ = key->tx_pn[0];
254 hdr = (struct hostap_ieee80211_hdr *) skb->data;
255 ccmp_init_blocks(key->tfm, hdr, key->tx_pn, data_len, b0, b, s0);
257 blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN;
258 last = data_len % AES_BLOCK_LEN;
260 for (i = 1; i <= blocks; i++) {
261 len = (i == blocks && last) ? last : AES_BLOCK_LEN;
263 xor_block(b, pos, len);
264 hostap_ccmp_aes_encrypt(key->tfm, b, b);
265 /* Encryption, with counter */
266 b0[14] = (i >> 8) & 0xff;
268 hostap_ccmp_aes_encrypt(key->tfm, b0, e);
269 xor_block(pos, e, len);
273 for (i = 0; i < CCMP_MIC_LEN; i++)
274 mic[i] = b[i] ^ s0[i];
280 static int hostap_ccmp_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
282 struct hostap_ccmp_data *key = priv;
284 struct hostap_ieee80211_hdr *hdr;
289 int i, blocks, last, len;
290 size_t data_len = skb->len - hdr_len - CCMP_HDR_LEN - CCMP_MIC_LEN;
291 u8 *mic = skb->data + skb->len - CCMP_MIC_LEN;
293 if (skb->len < hdr_len + CCMP_HDR_LEN + CCMP_MIC_LEN) {
294 key->dot11RSNAStatsCCMPFormatErrors++;
298 hdr = (struct hostap_ieee80211_hdr *) skb->data;
299 pos = skb->data + hdr_len;
301 if (!(keyidx & (1 << 5))) {
302 if (net_ratelimit()) {
303 printk(KERN_DEBUG "CCMP: received packet without ExtIV"
304 " flag from " MACSTR "\n", MAC2STR(hdr->addr2));
306 key->dot11RSNAStatsCCMPFormatErrors++;
310 if (key->key_idx != keyidx) {
311 printk(KERN_DEBUG "CCMP: RX tkey->key_idx=%d frame "
312 "keyidx=%d priv=%p\n", key->key_idx, keyidx, priv);
316 if (net_ratelimit()) {
317 printk(KERN_DEBUG "CCMP: received packet from " MACSTR
318 " with keyid=%d that does not have a configured"
319 " key\n", MAC2STR(hdr->addr2), keyidx);
332 if (memcmp(pn, key->rx_pn, CCMP_PN_LEN) <= 0) {
333 if (net_ratelimit()) {
334 printk(KERN_DEBUG "CCMP: replay detected: STA=" MACSTR
335 " previous PN %02x%02x%02x%02x%02x%02x "
336 "received PN %02x%02x%02x%02x%02x%02x\n",
337 MAC2STR(hdr->addr2), MAC2STR(key->rx_pn),
340 key->dot11RSNAStatsCCMPReplays++;
344 ccmp_init_blocks(key->tfm, hdr, pn, data_len, b0, a, b);
345 xor_block(mic, b, CCMP_MIC_LEN);
347 blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN;
348 last = data_len % AES_BLOCK_LEN;
350 for (i = 1; i <= blocks; i++) {
351 len = (i == blocks && last) ? last : AES_BLOCK_LEN;
352 /* Decrypt, with counter */
353 b0[14] = (i >> 8) & 0xff;
355 hostap_ccmp_aes_encrypt(key->tfm, b0, b);
356 xor_block(pos, b, len);
358 xor_block(a, pos, len);
359 hostap_ccmp_aes_encrypt(key->tfm, a, a);
363 if (memcmp(mic, a, CCMP_MIC_LEN) != 0) {
364 if (net_ratelimit()) {
365 printk(KERN_DEBUG "CCMP: decrypt failed: STA="
366 MACSTR "\n", MAC2STR(hdr->addr2));
368 key->dot11RSNAStatsCCMPDecryptErrors++;
372 memcpy(key->rx_pn, pn, CCMP_PN_LEN);
374 /* Remove hdr and MIC */
375 memmove(skb->data + CCMP_HDR_LEN, skb->data, hdr_len);
376 skb_pull(skb, CCMP_HDR_LEN);
377 skb_trim(skb, skb->len - CCMP_MIC_LEN);
383 static int hostap_ccmp_set_key(void *key, int len, u8 *seq, void *priv)
385 struct hostap_ccmp_data *data = priv;
387 struct crypto_tfm *tfm = data->tfm;
389 keyidx = data->key_idx;
390 memset(data, 0, sizeof(*data));
391 data->key_idx = keyidx;
393 if (len == CCMP_TK_LEN) {
394 memcpy(data->key, key, CCMP_TK_LEN);
397 data->rx_pn[0] = seq[5];
398 data->rx_pn[1] = seq[4];
399 data->rx_pn[2] = seq[3];
400 data->rx_pn[3] = seq[2];
401 data->rx_pn[4] = seq[1];
402 data->rx_pn[5] = seq[0];
404 crypto_cipher_setkey(data->tfm, data->key, CCMP_TK_LEN);
405 } else if (len == 0) {
414 static int hostap_ccmp_get_key(void *key, int len, u8 *seq, void *priv)
416 struct hostap_ccmp_data *data = priv;
418 if (len < CCMP_TK_LEN)
423 memcpy(key, data->key, CCMP_TK_LEN);
426 seq[0] = data->tx_pn[5];
427 seq[1] = data->tx_pn[4];
428 seq[2] = data->tx_pn[3];
429 seq[3] = data->tx_pn[2];
430 seq[4] = data->tx_pn[1];
431 seq[5] = data->tx_pn[0];
438 static char * hostap_ccmp_print_stats(char *p, void *priv)
440 struct hostap_ccmp_data *ccmp = priv;
441 p += sprintf(p, "key[%d] alg=CCMP key_set=%d "
442 "tx_pn=%02x%02x%02x%02x%02x%02x "
443 "rx_pn=%02x%02x%02x%02x%02x%02x "
444 "format_errors=%d replays=%d decrypt_errors=%d\n",
445 ccmp->key_idx, ccmp->key_set,
446 MAC2STR(ccmp->tx_pn), MAC2STR(ccmp->rx_pn),
447 ccmp->dot11RSNAStatsCCMPFormatErrors,
448 ccmp->dot11RSNAStatsCCMPReplays,
449 ccmp->dot11RSNAStatsCCMPDecryptErrors);
455 static struct hostap_crypto_ops hostap_crypt_ccmp = {
457 .init = hostap_ccmp_init,
458 .deinit = hostap_ccmp_deinit,
459 .encrypt_mpdu = hostap_ccmp_encrypt,
460 .decrypt_mpdu = hostap_ccmp_decrypt,
461 .encrypt_msdu = NULL,
462 .decrypt_msdu = NULL,
463 .set_key = hostap_ccmp_set_key,
464 .get_key = hostap_ccmp_get_key,
465 .print_stats = hostap_ccmp_print_stats,
466 .extra_prefix_len = CCMP_HDR_LEN,
467 .extra_postfix_len = CCMP_MIC_LEN
471 static int __init hostap_crypto_ccmp_init(void)
473 if (hostap_register_crypto_ops(&hostap_crypt_ccmp) < 0)
480 static void __exit hostap_crypto_ccmp_exit(void)
482 hostap_unregister_crypto_ops(&hostap_crypt_ccmp);
486 module_init(hostap_crypto_ccmp_init);
487 module_exit(hostap_crypto_ccmp_exit);