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