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