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