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