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