Add HostAP wireless driver.
[linux-2.6-block.git] / drivers / net / wireless / hostap / hostap_crypt_ccmp.c
1 /*
2  * Host AP crypt: host-based CCMP encryption implementation for Host AP driver
3  *
4  * Copyright (c) 2003-2004, Jouni Malinen <jkmaline@cc.hut.fi>
5  *
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
9  * more details.
10  */
11
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>
25
26 #include "hostap_crypt.h"
27 #include "hostap_wlan.h"
28 #include "hostap_80211.h"
29
30 #ifndef CONFIG_CRYPTO
31 #error CONFIG_CRYPTO is required to build this module.
32 #endif
33 #include <linux/crypto.h>
34 #include <asm/scatterlist.h>
35
36 MODULE_AUTHOR("Jouni Malinen");
37 MODULE_DESCRIPTION("Host AP crypt: CCMP");
38 MODULE_LICENSE("GPL");
39
40
41 #define AES_BLOCK_LEN 16
42 #define CCMP_HDR_LEN 8
43 #define CCMP_MIC_LEN 8
44 #define CCMP_TK_LEN 16
45 #define CCMP_PN_LEN 6
46
47
48 struct hostap_ccmp_data {
49         u8 key[CCMP_TK_LEN];
50         int key_set;
51
52         u8 tx_pn[CCMP_PN_LEN];
53         u8 rx_pn[CCMP_PN_LEN];
54
55         u32 dot11RSNAStatsCCMPFormatErrors;
56         u32 dot11RSNAStatsCCMPReplays;
57         u32 dot11RSNAStatsCCMPDecryptErrors;
58
59         int key_idx;
60
61         struct crypto_tfm *tfm;
62
63         /* scratch buffers for virt_to_page() (crypto API) */
64         u8 tx_b0[AES_BLOCK_LEN], tx_b[AES_BLOCK_LEN],
65                 tx_e[AES_BLOCK_LEN], tx_s0[AES_BLOCK_LEN];
66         u8 rx_b0[AES_BLOCK_LEN], rx_b[AES_BLOCK_LEN], rx_a[AES_BLOCK_LEN];
67 };
68
69
70 void hostap_ccmp_aes_encrypt(struct crypto_tfm *tfm,
71                              const u8 pt[16], u8 ct[16])
72 {
73         struct scatterlist src, dst;
74
75         src.page = virt_to_page(pt);
76         src.offset = offset_in_page(pt);
77         src.length = AES_BLOCK_LEN;
78
79         dst.page = virt_to_page(ct);
80         dst.offset = offset_in_page(ct);
81         dst.length = AES_BLOCK_LEN;
82
83         crypto_cipher_encrypt(tfm, &dst, &src, AES_BLOCK_LEN);
84 }
85
86
87 static void * hostap_ccmp_init(int key_idx)
88 {
89         struct hostap_ccmp_data *priv;
90
91         if (!try_module_get(THIS_MODULE))
92                 return NULL;
93
94         priv = (struct hostap_ccmp_data *) kmalloc(sizeof(*priv), GFP_ATOMIC);
95         if (priv == NULL) {
96                 goto fail;
97         }
98         memset(priv, 0, sizeof(*priv));
99         priv->key_idx = key_idx;
100
101         priv->tfm = crypto_alloc_tfm("aes", 0);
102         if (priv->tfm == NULL) {
103                 printk(KERN_DEBUG "hostap_crypt_ccmp: could not allocate "
104                        "crypto API aes\n");
105                 goto fail;
106         }
107
108         return priv;
109
110 fail:
111         if (priv) {
112                 if (priv->tfm)
113                         crypto_free_tfm(priv->tfm);
114                 kfree(priv);
115         }
116         module_put(THIS_MODULE);
117         return NULL;
118 }
119
120
121 static void hostap_ccmp_deinit(void *priv)
122 {
123         struct hostap_ccmp_data *_priv = priv;
124         if (_priv && _priv->tfm)
125                 crypto_free_tfm(_priv->tfm);
126         kfree(priv);
127         module_put(THIS_MODULE);
128 }
129
130
131 static inline void xor_block(u8 *b, u8 *a, size_t len)
132 {
133         int i;
134         for (i = 0; i < len; i++)
135                 b[i] ^= a[i];
136 }
137
138
139 static void ccmp_init_blocks(struct crypto_tfm *tfm,
140                              struct hostap_ieee80211_hdr *hdr,
141                              u8 *pn, size_t dlen, u8 *b0, u8 *auth,
142                              u8 *s0)
143 {
144         u8 *pos, qc = 0;
145         size_t aad_len;
146         u16 fc;
147         int a4_included, qc_included;
148         u8 aad[2 * AES_BLOCK_LEN];
149
150         fc = le16_to_cpu(hdr->frame_control);
151         a4_included = ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
152                        (WLAN_FC_TODS | WLAN_FC_FROMDS));
153         qc_included = ((WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA) &&
154                        (WLAN_FC_GET_STYPE(fc) & 0x08));
155         aad_len = 22;
156         if (a4_included)
157                 aad_len += 6;
158         if (qc_included) {
159                 pos = (u8 *) &hdr->addr4;
160                 if (a4_included)
161                         pos += 6;
162                 qc = *pos & 0x0f;
163                 aad_len += 2;
164         }
165
166         /* CCM Initial Block:
167          * Flag (Include authentication header, M=3 (8-octet MIC),
168          *       L=1 (2-octet Dlen))
169          * Nonce: 0x00 | A2 | PN
170          * Dlen */
171         b0[0] = 0x59;
172         b0[1] = qc;
173         memcpy(b0 + 2, hdr->addr2, ETH_ALEN);
174         memcpy(b0 + 8, pn, CCMP_PN_LEN);
175         b0[14] = (dlen >> 8) & 0xff;
176         b0[15] = dlen & 0xff;
177
178         /* AAD:
179          * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
180          * A1 | A2 | A3
181          * SC with bits 4..15 (seq#) masked to zero
182          * A4 (if present)
183          * QC (if present)
184          */
185         pos = (u8 *) hdr;
186         aad[0] = 0; /* aad_len >> 8 */
187         aad[1] = aad_len & 0xff;
188         aad[2] = pos[0] & 0x8f;
189         aad[3] = pos[1] & 0xc7;
190         memcpy(aad + 4, hdr->addr1, 3 * ETH_ALEN);
191         pos = (u8 *) &hdr->seq_ctrl;
192         aad[22] = pos[0] & 0x0f;
193         aad[23] = 0; /* all bits masked */
194         memset(aad + 24, 0, 8);
195         if (a4_included)
196                 memcpy(aad + 24, hdr->addr4, ETH_ALEN);
197         if (qc_included) {
198                 aad[a4_included ? 30 : 24] = qc;
199                 /* rest of QC masked */
200         }
201
202         /* Start with the first block and AAD */
203         hostap_ccmp_aes_encrypt(tfm, b0, auth);
204         xor_block(auth, aad, AES_BLOCK_LEN);
205         hostap_ccmp_aes_encrypt(tfm, auth, auth);
206         xor_block(auth, &aad[AES_BLOCK_LEN], AES_BLOCK_LEN);
207         hostap_ccmp_aes_encrypt(tfm, auth, auth);
208         b0[0] &= 0x07;
209         b0[14] = b0[15] = 0;
210         hostap_ccmp_aes_encrypt(tfm, b0, s0);
211 }
212
213
214 static int hostap_ccmp_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
215 {
216         struct hostap_ccmp_data *key = priv;
217         int data_len, i, blocks, last, len;
218         u8 *pos, *mic;
219         struct hostap_ieee80211_hdr *hdr;
220         u8 *b0 = key->tx_b0;
221         u8 *b = key->tx_b;
222         u8 *e = key->tx_e;
223         u8 *s0 = key->tx_s0;
224
225         if (skb_headroom(skb) < CCMP_HDR_LEN ||
226             skb_tailroom(skb) < CCMP_MIC_LEN ||
227             skb->len < hdr_len)
228                 return -1;
229
230         data_len = skb->len - hdr_len;
231         pos = skb_push(skb, CCMP_HDR_LEN);
232         memmove(pos, pos + CCMP_HDR_LEN, hdr_len);
233         pos += hdr_len;
234         mic = skb_put(skb, CCMP_MIC_LEN);
235
236         i = CCMP_PN_LEN - 1;
237         while (i >= 0) {
238                 key->tx_pn[i]++;
239                 if (key->tx_pn[i] != 0)
240                         break;
241                 i--;
242         }
243
244         *pos++ = key->tx_pn[5];
245         *pos++ = key->tx_pn[4];
246         *pos++ = 0;
247         *pos++ = (key->key_idx << 6) | (1 << 5) /* Ext IV included */;
248         *pos++ = key->tx_pn[3];
249         *pos++ = key->tx_pn[2];
250         *pos++ = key->tx_pn[1];
251         *pos++ = key->tx_pn[0];
252
253         hdr = (struct hostap_ieee80211_hdr *) skb->data;
254         ccmp_init_blocks(key->tfm, hdr, key->tx_pn, data_len, b0, b, s0);
255
256         blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN;
257         last = data_len % AES_BLOCK_LEN;
258
259         for (i = 1; i <= blocks; i++) {
260                 len = (i == blocks && last) ? last : AES_BLOCK_LEN;
261                 /* Authentication */
262                 xor_block(b, pos, len);
263                 hostap_ccmp_aes_encrypt(key->tfm, b, b);
264                 /* Encryption, with counter */
265                 b0[14] = (i >> 8) & 0xff;
266                 b0[15] = i & 0xff;
267                 hostap_ccmp_aes_encrypt(key->tfm, b0, e);
268                 xor_block(pos, e, len);
269                 pos += len;
270         }
271
272         for (i = 0; i < CCMP_MIC_LEN; i++)
273                 mic[i] = b[i] ^ s0[i];
274
275         return 0;
276 }
277
278
279 static int hostap_ccmp_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
280 {
281         struct hostap_ccmp_data *key = priv;
282         u8 keyidx, *pos;
283         struct hostap_ieee80211_hdr *hdr;
284         u8 *b0 = key->rx_b0;
285         u8 *b = key->rx_b;
286         u8 *a = key->rx_a;
287         u8 pn[6];
288         int i, blocks, last, len;
289         size_t data_len = skb->len - hdr_len - CCMP_HDR_LEN - CCMP_MIC_LEN;
290         u8 *mic = skb->data + skb->len - CCMP_MIC_LEN;
291
292         if (skb->len < hdr_len + CCMP_HDR_LEN + CCMP_MIC_LEN) {
293                 key->dot11RSNAStatsCCMPFormatErrors++;
294                 return -1;
295         }
296
297         hdr = (struct hostap_ieee80211_hdr *) skb->data;
298         pos = skb->data + hdr_len;
299         keyidx = pos[3];
300         if (!(keyidx & (1 << 5))) {
301                 if (net_ratelimit()) {
302                         printk(KERN_DEBUG "CCMP: received packet without ExtIV"
303                                " flag from " MACSTR "\n", MAC2STR(hdr->addr2));
304                 }
305                 key->dot11RSNAStatsCCMPFormatErrors++;
306                 return -2;
307         }
308         keyidx >>= 6;
309         if (key->key_idx != keyidx) {
310                 printk(KERN_DEBUG "CCMP: RX tkey->key_idx=%d frame "
311                        "keyidx=%d priv=%p\n", key->key_idx, keyidx, priv);
312                 return -6;
313         }
314         if (!key->key_set) {
315                 if (net_ratelimit()) {
316                         printk(KERN_DEBUG "CCMP: received packet from " MACSTR
317                                " with keyid=%d that does not have a configured"
318                                " key\n", MAC2STR(hdr->addr2), keyidx);
319                 }
320                 return -3;
321         }
322
323         pn[0] = pos[7];
324         pn[1] = pos[6];
325         pn[2] = pos[5];
326         pn[3] = pos[4];
327         pn[4] = pos[1];
328         pn[5] = pos[0];
329         pos += 8;
330
331         if (memcmp(pn, key->rx_pn, CCMP_PN_LEN) <= 0) {
332                 if (net_ratelimit()) {
333                         printk(KERN_DEBUG "CCMP: replay detected: STA=" MACSTR
334                                " previous PN %02x%02x%02x%02x%02x%02x "
335                                "received PN %02x%02x%02x%02x%02x%02x\n",
336                                MAC2STR(hdr->addr2), MAC2STR(key->rx_pn),
337                                MAC2STR(pn));
338                 }
339                 key->dot11RSNAStatsCCMPReplays++;
340                 return -4;
341         }
342
343         ccmp_init_blocks(key->tfm, hdr, pn, data_len, b0, a, b);
344         xor_block(mic, b, CCMP_MIC_LEN);
345
346         blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN;
347         last = data_len % AES_BLOCK_LEN;
348
349         for (i = 1; i <= blocks; i++) {
350                 len = (i == blocks && last) ? last : AES_BLOCK_LEN;
351                 /* Decrypt, with counter */
352                 b0[14] = (i >> 8) & 0xff;
353                 b0[15] = i & 0xff;
354                 hostap_ccmp_aes_encrypt(key->tfm, b0, b);
355                 xor_block(pos, b, len);
356                 /* Authentication */
357                 xor_block(a, pos, len);
358                 hostap_ccmp_aes_encrypt(key->tfm, a, a);
359                 pos += len;
360         }
361
362         if (memcmp(mic, a, CCMP_MIC_LEN) != 0) {
363                 if (net_ratelimit()) {
364                         printk(KERN_DEBUG "CCMP: decrypt failed: STA="
365                                MACSTR "\n", MAC2STR(hdr->addr2));
366                 }
367                 key->dot11RSNAStatsCCMPDecryptErrors++;
368                 return -5;
369         }
370
371         memcpy(key->rx_pn, pn, CCMP_PN_LEN);
372
373         /* Remove hdr and MIC */
374         memmove(skb->data + CCMP_HDR_LEN, skb->data, hdr_len);
375         skb_pull(skb, CCMP_HDR_LEN);
376         skb_trim(skb, skb->len - CCMP_MIC_LEN);
377
378         return keyidx;
379 }
380
381
382 static int hostap_ccmp_set_key(void *key, int len, u8 *seq, void *priv)
383 {
384         struct hostap_ccmp_data *data = priv;
385         int keyidx;
386         struct crypto_tfm *tfm = data->tfm;
387
388         keyidx = data->key_idx;
389         memset(data, 0, sizeof(*data));
390         data->key_idx = keyidx;
391         data->tfm = tfm;
392         if (len == CCMP_TK_LEN) {
393                 memcpy(data->key, key, CCMP_TK_LEN);
394                 data->key_set = 1;
395                 if (seq) {
396                         data->rx_pn[0] = seq[5];
397                         data->rx_pn[1] = seq[4];
398                         data->rx_pn[2] = seq[3];
399                         data->rx_pn[3] = seq[2];
400                         data->rx_pn[4] = seq[1];
401                         data->rx_pn[5] = seq[0];
402                 }
403                 crypto_cipher_setkey(data->tfm, data->key, CCMP_TK_LEN);
404         } else if (len == 0) {
405                 data->key_set = 0;
406         } else
407                 return -1;
408
409         return 0;
410 }
411
412
413 static int hostap_ccmp_get_key(void *key, int len, u8 *seq, void *priv)
414 {
415         struct hostap_ccmp_data *data = priv;
416
417         if (len < CCMP_TK_LEN)
418                 return -1;
419
420         if (!data->key_set)
421                 return 0;
422         memcpy(key, data->key, CCMP_TK_LEN);
423
424         if (seq) {
425                 seq[0] = data->tx_pn[5];
426                 seq[1] = data->tx_pn[4];
427                 seq[2] = data->tx_pn[3];
428                 seq[3] = data->tx_pn[2];
429                 seq[4] = data->tx_pn[1];
430                 seq[5] = data->tx_pn[0];
431         }
432
433         return CCMP_TK_LEN;
434 }
435
436
437 static char * hostap_ccmp_print_stats(char *p, void *priv)
438 {
439         struct hostap_ccmp_data *ccmp = priv;
440         p += sprintf(p, "key[%d] alg=CCMP key_set=%d "
441                      "tx_pn=%02x%02x%02x%02x%02x%02x "
442                      "rx_pn=%02x%02x%02x%02x%02x%02x "
443                      "format_errors=%d replays=%d decrypt_errors=%d\n",
444                      ccmp->key_idx, ccmp->key_set,
445                      MAC2STR(ccmp->tx_pn), MAC2STR(ccmp->rx_pn),
446                      ccmp->dot11RSNAStatsCCMPFormatErrors,
447                      ccmp->dot11RSNAStatsCCMPReplays,
448                      ccmp->dot11RSNAStatsCCMPDecryptErrors);
449
450         return p;
451 }
452
453
454 static struct hostap_crypto_ops hostap_crypt_ccmp = {
455         .name                   = "CCMP",
456         .init                   = hostap_ccmp_init,
457         .deinit                 = hostap_ccmp_deinit,
458         .encrypt_mpdu           = hostap_ccmp_encrypt,
459         .decrypt_mpdu           = hostap_ccmp_decrypt,
460         .encrypt_msdu           = NULL,
461         .decrypt_msdu           = NULL,
462         .set_key                = hostap_ccmp_set_key,
463         .get_key                = hostap_ccmp_get_key,
464         .print_stats            = hostap_ccmp_print_stats,
465         .extra_prefix_len       = CCMP_HDR_LEN,
466         .extra_postfix_len      = CCMP_MIC_LEN
467 };
468
469
470 static int __init hostap_crypto_ccmp_init(void)
471 {
472         if (hostap_register_crypto_ops(&hostap_crypt_ccmp) < 0)
473                 return -1;
474
475         return 0;
476 }
477
478
479 static void __exit hostap_crypto_ccmp_exit(void)
480 {
481         hostap_unregister_crypto_ops(&hostap_crypt_ccmp);
482 }
483
484
485 module_init(hostap_crypto_ccmp_init);
486 module_exit(hostap_crypto_ccmp_exit);