]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/wireless/hostap/hostap_crypt_ccmp.c
ad26aac2d5fe7e7f5da4bd2ea5480da132dfeb19
[karo-tx-linux.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 MODULE_VERSION(PRISM2_VERSION);
40
41
42 #define AES_BLOCK_LEN 16
43 #define CCMP_HDR_LEN 8
44 #define CCMP_MIC_LEN 8
45 #define CCMP_TK_LEN 16
46 #define CCMP_PN_LEN 6
47
48
49 struct hostap_ccmp_data {
50         u8 key[CCMP_TK_LEN];
51         int key_set;
52
53         u8 tx_pn[CCMP_PN_LEN];
54         u8 rx_pn[CCMP_PN_LEN];
55
56         u32 dot11RSNAStatsCCMPFormatErrors;
57         u32 dot11RSNAStatsCCMPReplays;
58         u32 dot11RSNAStatsCCMPDecryptErrors;
59
60         int key_idx;
61
62         struct crypto_tfm *tfm;
63
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];
68 };
69
70
71 void hostap_ccmp_aes_encrypt(struct crypto_tfm *tfm,
72                              const u8 pt[16], u8 ct[16])
73 {
74         struct scatterlist src, dst;
75
76         src.page = virt_to_page(pt);
77         src.offset = offset_in_page(pt);
78         src.length = AES_BLOCK_LEN;
79
80         dst.page = virt_to_page(ct);
81         dst.offset = offset_in_page(ct);
82         dst.length = AES_BLOCK_LEN;
83
84         crypto_cipher_encrypt(tfm, &dst, &src, AES_BLOCK_LEN);
85 }
86
87
88 static void * hostap_ccmp_init(int key_idx)
89 {
90         struct hostap_ccmp_data *priv;
91
92         if (!try_module_get(THIS_MODULE))
93                 return NULL;
94
95         priv = (struct hostap_ccmp_data *) kmalloc(sizeof(*priv), GFP_ATOMIC);
96         if (priv == NULL) {
97                 goto fail;
98         }
99         memset(priv, 0, sizeof(*priv));
100         priv->key_idx = key_idx;
101
102         priv->tfm = crypto_alloc_tfm("aes", 0);
103         if (priv->tfm == NULL) {
104                 printk(KERN_DEBUG "hostap_crypt_ccmp: could not allocate "
105                        "crypto API aes\n");
106                 goto fail;
107         }
108
109         return priv;
110
111 fail:
112         if (priv) {
113                 if (priv->tfm)
114                         crypto_free_tfm(priv->tfm);
115                 kfree(priv);
116         }
117         module_put(THIS_MODULE);
118         return NULL;
119 }
120
121
122 static void hostap_ccmp_deinit(void *priv)
123 {
124         struct hostap_ccmp_data *_priv = priv;
125         if (_priv && _priv->tfm)
126                 crypto_free_tfm(_priv->tfm);
127         kfree(priv);
128         module_put(THIS_MODULE);
129 }
130
131
132 static inline void xor_block(u8 *b, u8 *a, size_t len)
133 {
134         int i;
135         for (i = 0; i < len; i++)
136                 b[i] ^= a[i];
137 }
138
139
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,
143                              u8 *s0)
144 {
145         u8 *pos, qc = 0;
146         size_t aad_len;
147         u16 fc;
148         int a4_included, qc_included;
149         u8 aad[2 * AES_BLOCK_LEN];
150
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 = ((WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA) &&
155                        (WLAN_FC_GET_STYPE(fc) & 0x08));
156         aad_len = 22;
157         if (a4_included)
158                 aad_len += 6;
159         if (qc_included) {
160                 pos = (u8 *) &hdr->addr4;
161                 if (a4_included)
162                         pos += 6;
163                 qc = *pos & 0x0f;
164                 aad_len += 2;
165         }
166
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
171          * Dlen */
172         b0[0] = 0x59;
173         b0[1] = qc;
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;
178
179         /* AAD:
180          * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
181          * A1 | A2 | A3
182          * SC with bits 4..15 (seq#) masked to zero
183          * A4 (if present)
184          * QC (if present)
185          */
186         pos = (u8 *) hdr;
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);
196         if (a4_included)
197                 memcpy(aad + 24, hdr->addr4, ETH_ALEN);
198         if (qc_included) {
199                 aad[a4_included ? 30 : 24] = qc;
200                 /* rest of QC masked */
201         }
202
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);
209         b0[0] &= 0x07;
210         b0[14] = b0[15] = 0;
211         hostap_ccmp_aes_encrypt(tfm, b0, s0);
212 }
213
214
215 static int hostap_ccmp_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
216 {
217         struct hostap_ccmp_data *key = priv;
218         int data_len, i, blocks, last, len;
219         u8 *pos, *mic;
220         struct hostap_ieee80211_hdr *hdr;
221         u8 *b0 = key->tx_b0;
222         u8 *b = key->tx_b;
223         u8 *e = key->tx_e;
224         u8 *s0 = key->tx_s0;
225
226         if (skb_headroom(skb) < CCMP_HDR_LEN ||
227             skb_tailroom(skb) < CCMP_MIC_LEN ||
228             skb->len < hdr_len)
229                 return -1;
230
231         data_len = skb->len - hdr_len;
232         pos = skb_push(skb, CCMP_HDR_LEN);
233         memmove(pos, pos + CCMP_HDR_LEN, hdr_len);
234         pos += hdr_len;
235         mic = skb_put(skb, CCMP_MIC_LEN);
236
237         i = CCMP_PN_LEN - 1;
238         while (i >= 0) {
239                 key->tx_pn[i]++;
240                 if (key->tx_pn[i] != 0)
241                         break;
242                 i--;
243         }
244
245         *pos++ = key->tx_pn[5];
246         *pos++ = key->tx_pn[4];
247         *pos++ = 0;
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];
253
254         hdr = (struct hostap_ieee80211_hdr *) skb->data;
255         ccmp_init_blocks(key->tfm, hdr, key->tx_pn, data_len, b0, b, s0);
256
257         blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN;
258         last = data_len % AES_BLOCK_LEN;
259
260         for (i = 1; i <= blocks; i++) {
261                 len = (i == blocks && last) ? last : AES_BLOCK_LEN;
262                 /* Authentication */
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;
267                 b0[15] = i & 0xff;
268                 hostap_ccmp_aes_encrypt(key->tfm, b0, e);
269                 xor_block(pos, e, len);
270                 pos += len;
271         }
272
273         for (i = 0; i < CCMP_MIC_LEN; i++)
274                 mic[i] = b[i] ^ s0[i];
275
276         return 0;
277 }
278
279
280 static int hostap_ccmp_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
281 {
282         struct hostap_ccmp_data *key = priv;
283         u8 keyidx, *pos;
284         struct hostap_ieee80211_hdr *hdr;
285         u8 *b0 = key->rx_b0;
286         u8 *b = key->rx_b;
287         u8 *a = key->rx_a;
288         u8 pn[6];
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;
292
293         if (skb->len < hdr_len + CCMP_HDR_LEN + CCMP_MIC_LEN) {
294                 key->dot11RSNAStatsCCMPFormatErrors++;
295                 return -1;
296         }
297
298         hdr = (struct hostap_ieee80211_hdr *) skb->data;
299         pos = skb->data + hdr_len;
300         keyidx = pos[3];
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));
305                 }
306                 key->dot11RSNAStatsCCMPFormatErrors++;
307                 return -2;
308         }
309         keyidx >>= 6;
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);
313                 return -6;
314         }
315         if (!key->key_set) {
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);
320                 }
321                 return -3;
322         }
323
324         pn[0] = pos[7];
325         pn[1] = pos[6];
326         pn[2] = pos[5];
327         pn[3] = pos[4];
328         pn[4] = pos[1];
329         pn[5] = pos[0];
330         pos += 8;
331
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),
338                                MAC2STR(pn));
339                 }
340                 key->dot11RSNAStatsCCMPReplays++;
341                 return -4;
342         }
343
344         ccmp_init_blocks(key->tfm, hdr, pn, data_len, b0, a, b);
345         xor_block(mic, b, CCMP_MIC_LEN);
346
347         blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN;
348         last = data_len % AES_BLOCK_LEN;
349
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;
354                 b0[15] = i & 0xff;
355                 hostap_ccmp_aes_encrypt(key->tfm, b0, b);
356                 xor_block(pos, b, len);
357                 /* Authentication */
358                 xor_block(a, pos, len);
359                 hostap_ccmp_aes_encrypt(key->tfm, a, a);
360                 pos += len;
361         }
362
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));
367                 }
368                 key->dot11RSNAStatsCCMPDecryptErrors++;
369                 return -5;
370         }
371
372         memcpy(key->rx_pn, pn, CCMP_PN_LEN);
373
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);
378
379         return keyidx;
380 }
381
382
383 static int hostap_ccmp_set_key(void *key, int len, u8 *seq, void *priv)
384 {
385         struct hostap_ccmp_data *data = priv;
386         int keyidx;
387         struct crypto_tfm *tfm = data->tfm;
388
389         keyidx = data->key_idx;
390         memset(data, 0, sizeof(*data));
391         data->key_idx = keyidx;
392         data->tfm = tfm;
393         if (len == CCMP_TK_LEN) {
394                 memcpy(data->key, key, CCMP_TK_LEN);
395                 data->key_set = 1;
396                 if (seq) {
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];
403                 }
404                 crypto_cipher_setkey(data->tfm, data->key, CCMP_TK_LEN);
405         } else if (len == 0) {
406                 data->key_set = 0;
407         } else
408                 return -1;
409
410         return 0;
411 }
412
413
414 static int hostap_ccmp_get_key(void *key, int len, u8 *seq, void *priv)
415 {
416         struct hostap_ccmp_data *data = priv;
417
418         if (len < CCMP_TK_LEN)
419                 return -1;
420
421         if (!data->key_set)
422                 return 0;
423         memcpy(key, data->key, CCMP_TK_LEN);
424
425         if (seq) {
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];
432         }
433
434         return CCMP_TK_LEN;
435 }
436
437
438 static char * hostap_ccmp_print_stats(char *p, void *priv)
439 {
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);
450
451         return p;
452 }
453
454
455 static struct hostap_crypto_ops hostap_crypt_ccmp = {
456         .name                   = "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
468 };
469
470
471 static int __init hostap_crypto_ccmp_init(void)
472 {
473         if (hostap_register_crypto_ops(&hostap_crypt_ccmp) < 0)
474                 return -1;
475
476         return 0;
477 }
478
479
480 static void __exit hostap_crypto_ccmp_exit(void)
481 {
482         hostap_unregister_crypto_ops(&hostap_crypt_ccmp);
483 }
484
485
486 module_init(hostap_crypto_ccmp_init);
487 module_exit(hostap_crypto_ccmp_exit);