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