[package] base-files, ppp: fix interface shutdown
[openwrt.git] / target / linux / generic-2.6 / files / crypto / ocf / cryptocteon / cryptocteon.c
1 /*
2 * Octeon Crypto for OCF
3 *
4 * Written by David McCullough <david_mccullough@mcafee.com>
5 * Copyright (C) 2009-2010 David McCullough
6 *
7 * LICENSE TERMS
8 *
9 * The free distribution and use of this software in both source and binary
10 * form is allowed (with or without changes) provided that:
11 *
12 * 1. distributions of this source code include the above copyright
13 * notice, this list of conditions and the following disclaimer;
14 *
15 * 2. distributions in binary form include the above copyright
16 * notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other associated materials;
18 *
19 * 3. the copyright holder's name is not used to endorse products
20 * built using this software without specific written permission.
21 *
22 * DISCLAIMER
23 *
24 * This software is provided 'as is' with no explicit or implied warranties
25 * in respect of its properties, including, but not limited to, correctness
26 * and/or fitness for purpose.
27 * ---------------------------------------------------------------------------
28 */
29
30 #ifndef AUTOCONF_INCLUDED
31 #include <linux/config.h>
32 #endif
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/list.h>
36 #include <linux/slab.h>
37 #include <linux/sched.h>
38 #include <linux/wait.h>
39 #include <linux/crypto.h>
40 #include <linux/mm.h>
41 #include <linux/skbuff.h>
42 #include <linux/random.h>
43 #include <linux/scatterlist.h>
44
45 #include <cryptodev.h>
46 #include <uio.h>
47
48 struct {
49 softc_device_decl sc_dev;
50 } octo_softc;
51
52 #define offset_in_page(p) ((unsigned long)(p) & ~PAGE_MASK)
53
54 struct octo_sess {
55 int octo_encalg;
56 #define MAX_CIPHER_KEYLEN 64
57 char octo_enckey[MAX_CIPHER_KEYLEN];
58 int octo_encklen;
59
60 int octo_macalg;
61 #define MAX_HASH_KEYLEN 64
62 char octo_mackey[MAX_HASH_KEYLEN];
63 int octo_macklen;
64 int octo_mackey_set;
65
66 int octo_mlen;
67 int octo_ivsize;
68
69 #if 0
70 int (*octo_decrypt)(struct scatterlist *sg, int sg_len,
71 uint8_t *key, int key_len, uint8_t * iv,
72 uint64_t *hminner, uint64_t *hmouter);
73
74 int (*octo_encrypt)(struct scatterlist *sg, int sg_len,
75 uint8_t *key, int key_len, uint8_t * iv,
76 uint64_t *hminner, uint64_t *hmouter);
77 #else
78 int (*octo_encrypt)(struct octo_sess *od,
79 struct scatterlist *sg, int sg_len,
80 int auth_off, int auth_len,
81 int crypt_off, int crypt_len,
82 int icv_off, uint8_t *ivp);
83 int (*octo_decrypt)(struct octo_sess *od,
84 struct scatterlist *sg, int sg_len,
85 int auth_off, int auth_len,
86 int crypt_off, int crypt_len,
87 int icv_off, uint8_t *ivp);
88 #endif
89
90 uint64_t octo_hminner[3];
91 uint64_t octo_hmouter[3];
92 };
93
94 int32_t octo_id = -1;
95 module_param(octo_id, int, 0444);
96 MODULE_PARM_DESC(octo_id, "Read-Only OCF ID for cryptocteon driver");
97
98 static struct octo_sess **octo_sessions = NULL;
99 static u_int32_t octo_sesnum = 0;
100
101 static int octo_process(device_t, struct cryptop *, int);
102 static int octo_newsession(device_t, u_int32_t *, struct cryptoini *);
103 static int octo_freesession(device_t, u_int64_t);
104
105 static device_method_t octo_methods = {
106 /* crypto device methods */
107 DEVMETHOD(cryptodev_newsession, octo_newsession),
108 DEVMETHOD(cryptodev_freesession,octo_freesession),
109 DEVMETHOD(cryptodev_process, octo_process),
110 };
111
112 #define debug octo_debug
113 int octo_debug = 0;
114 module_param(octo_debug, int, 0644);
115 MODULE_PARM_DESC(octo_debug, "Enable debug");
116
117
118 #include "cavium_crypto.c"
119
120
121 /*
122 * Generate a new octo session. We artifically limit it to a single
123 * hash/cipher or hash-cipher combo just to make it easier, most callers
124 * do not expect more than this anyway.
125 */
126 static int
127 octo_newsession(device_t dev, u_int32_t *sid, struct cryptoini *cri)
128 {
129 struct cryptoini *c, *encini = NULL, *macini = NULL;
130 struct octo_sess **ocd;
131 int i;
132
133 dprintk("%s()\n", __FUNCTION__);
134 if (sid == NULL || cri == NULL) {
135 dprintk("%s,%d - EINVAL\n", __FILE__, __LINE__);
136 return EINVAL;
137 }
138
139 /*
140 * To keep it simple, we only handle hash, cipher or hash/cipher in a
141 * session, you cannot currently do multiple ciphers/hashes in one
142 * session even though it would be possibel to code this driver to
143 * handle it.
144 */
145 for (i = 0, c = cri; c && i < 2; i++) {
146 if (c->cri_alg == CRYPTO_MD5_HMAC ||
147 c->cri_alg == CRYPTO_SHA1_HMAC ||
148 c->cri_alg == CRYPTO_NULL_HMAC) {
149 if (macini) {
150 break;
151 }
152 macini = c;
153 }
154 if (c->cri_alg == CRYPTO_DES_CBC ||
155 c->cri_alg == CRYPTO_3DES_CBC ||
156 c->cri_alg == CRYPTO_AES_CBC ||
157 c->cri_alg == CRYPTO_NULL_CBC) {
158 if (encini) {
159 break;
160 }
161 encini = c;
162 }
163 c = c->cri_next;
164 }
165 if (!macini && !encini) {
166 dprintk("%s,%d - EINVAL bad cipher/hash or combination\n",
167 __FILE__, __LINE__);
168 return EINVAL;
169 }
170 if (c) {
171 dprintk("%s,%d - EINVAL cannot handle chained cipher/hash combos\n",
172 __FILE__, __LINE__);
173 return EINVAL;
174 }
175
176 /*
177 * So we have something we can do, lets setup the session
178 */
179
180 if (octo_sessions) {
181 for (i = 1; i < octo_sesnum; i++)
182 if (octo_sessions[i] == NULL)
183 break;
184 } else
185 i = 1; /* NB: to silence compiler warning */
186
187 if (octo_sessions == NULL || i == octo_sesnum) {
188 if (octo_sessions == NULL) {
189 i = 1; /* We leave octo_sessions[0] empty */
190 octo_sesnum = CRYPTO_SW_SESSIONS;
191 } else
192 octo_sesnum *= 2;
193
194 ocd = kmalloc(octo_sesnum * sizeof(struct octo_sess *), SLAB_ATOMIC);
195 if (ocd == NULL) {
196 /* Reset session number */
197 if (octo_sesnum == CRYPTO_SW_SESSIONS)
198 octo_sesnum = 0;
199 else
200 octo_sesnum /= 2;
201 dprintk("%s,%d: ENOBUFS\n", __FILE__, __LINE__);
202 return ENOBUFS;
203 }
204 memset(ocd, 0, octo_sesnum * sizeof(struct octo_sess *));
205
206 /* Copy existing sessions */
207 if (octo_sessions) {
208 memcpy(ocd, octo_sessions,
209 (octo_sesnum / 2) * sizeof(struct octo_sess *));
210 kfree(octo_sessions);
211 }
212
213 octo_sessions = ocd;
214 }
215
216 ocd = &octo_sessions[i];
217 *sid = i;
218
219
220 *ocd = (struct octo_sess *) kmalloc(sizeof(struct octo_sess), SLAB_ATOMIC);
221 if (*ocd == NULL) {
222 octo_freesession(NULL, i);
223 dprintk("%s,%d: ENOBUFS\n", __FILE__, __LINE__);
224 return ENOBUFS;
225 }
226 memset(*ocd, 0, sizeof(struct octo_sess));
227
228 if (encini && encini->cri_key) {
229 (*ocd)->octo_encklen = (encini->cri_klen + 7) / 8;
230 memcpy((*ocd)->octo_enckey, encini->cri_key, (*ocd)->octo_encklen);
231 }
232
233 if (macini && macini->cri_key) {
234 (*ocd)->octo_macklen = (macini->cri_klen + 7) / 8;
235 memcpy((*ocd)->octo_mackey, macini->cri_key, (*ocd)->octo_macklen);
236 }
237
238 (*ocd)->octo_mlen = 0;
239 if (encini && encini->cri_mlen)
240 (*ocd)->octo_mlen = encini->cri_mlen;
241 else if (macini && macini->cri_mlen)
242 (*ocd)->octo_mlen = macini->cri_mlen;
243 else
244 (*ocd)->octo_mlen = 12;
245
246 /*
247 * point c at the enc if it exists, otherwise the mac
248 */
249 c = encini ? encini : macini;
250
251 switch (c->cri_alg) {
252 case CRYPTO_DES_CBC:
253 case CRYPTO_3DES_CBC:
254 (*ocd)->octo_ivsize = 8;
255 switch (macini ? macini->cri_alg : -1) {
256 case CRYPTO_MD5_HMAC:
257 (*ocd)->octo_encrypt = octo_des_cbc_md5_encrypt;
258 (*ocd)->octo_decrypt = octo_des_cbc_md5_decrypt;
259 octo_calc_hash(0, macini->cri_key, (*ocd)->octo_hminner,
260 (*ocd)->octo_hmouter);
261 break;
262 case CRYPTO_SHA1_HMAC:
263 (*ocd)->octo_encrypt = octo_des_cbc_sha1_encrypt;
264 (*ocd)->octo_decrypt = octo_des_cbc_sha1_encrypt;
265 octo_calc_hash(1, macini->cri_key, (*ocd)->octo_hminner,
266 (*ocd)->octo_hmouter);
267 break;
268 case -1:
269 (*ocd)->octo_encrypt = octo_des_cbc_encrypt;
270 (*ocd)->octo_decrypt = octo_des_cbc_decrypt;
271 break;
272 default:
273 octo_freesession(NULL, i);
274 dprintk("%s,%d: EINVALn", __FILE__, __LINE__);
275 return EINVAL;
276 }
277 break;
278 case CRYPTO_AES_CBC:
279 (*ocd)->octo_ivsize = 16;
280 switch (macini ? macini->cri_alg : -1) {
281 case CRYPTO_MD5_HMAC:
282 (*ocd)->octo_encrypt = octo_aes_cbc_md5_encrypt;
283 (*ocd)->octo_decrypt = octo_aes_cbc_md5_decrypt;
284 octo_calc_hash(0, macini->cri_key, (*ocd)->octo_hminner,
285 (*ocd)->octo_hmouter);
286 break;
287 case CRYPTO_SHA1_HMAC:
288 (*ocd)->octo_encrypt = octo_aes_cbc_sha1_encrypt;
289 (*ocd)->octo_decrypt = octo_aes_cbc_sha1_decrypt;
290 octo_calc_hash(1, macini->cri_key, (*ocd)->octo_hminner,
291 (*ocd)->octo_hmouter);
292 break;
293 case -1:
294 (*ocd)->octo_encrypt = octo_aes_cbc_encrypt;
295 (*ocd)->octo_decrypt = octo_aes_cbc_decrypt;
296 break;
297 default:
298 octo_freesession(NULL, i);
299 dprintk("%s,%d: EINVALn", __FILE__, __LINE__);
300 return EINVAL;
301 }
302 break;
303 case CRYPTO_MD5_HMAC:
304 (*ocd)->octo_encrypt = octo_null_md5_encrypt;
305 (*ocd)->octo_decrypt = octo_null_md5_encrypt;
306 octo_calc_hash(0, macini->cri_key, (*ocd)->octo_hminner,
307 (*ocd)->octo_hmouter);
308 break;
309 case CRYPTO_SHA1_HMAC:
310 (*ocd)->octo_encrypt = octo_null_sha1_encrypt;
311 (*ocd)->octo_decrypt = octo_null_sha1_encrypt;
312 octo_calc_hash(1, macini->cri_key, (*ocd)->octo_hminner,
313 (*ocd)->octo_hmouter);
314 break;
315 default:
316 octo_freesession(NULL, i);
317 dprintk("%s,%d: EINVALn", __FILE__, __LINE__);
318 return EINVAL;
319 }
320
321 (*ocd)->octo_encalg = encini ? encini->cri_alg : -1;
322 (*ocd)->octo_macalg = macini ? macini->cri_alg : -1;
323
324 return 0;
325 }
326
327 /*
328 * Free a session.
329 */
330 static int
331 octo_freesession(device_t dev, u_int64_t tid)
332 {
333 u_int32_t sid = CRYPTO_SESID2LID(tid);
334
335 dprintk("%s()\n", __FUNCTION__);
336 if (sid > octo_sesnum || octo_sessions == NULL ||
337 octo_sessions[sid] == NULL) {
338 dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
339 return(EINVAL);
340 }
341
342 /* Silently accept and return */
343 if (sid == 0)
344 return(0);
345
346 if (octo_sessions[sid])
347 kfree(octo_sessions[sid]);
348 octo_sessions[sid] = NULL;
349 return 0;
350 }
351
352 /*
353 * Process a request.
354 */
355 static int
356 octo_process(device_t dev, struct cryptop *crp, int hint)
357 {
358 struct cryptodesc *crd;
359 struct octo_sess *od;
360 u_int32_t lid;
361 #define SCATTERLIST_MAX 16
362 struct scatterlist sg[SCATTERLIST_MAX];
363 int sg_num, sg_len;
364 struct sk_buff *skb = NULL;
365 struct uio *uiop = NULL;
366 struct cryptodesc *enccrd = NULL, *maccrd = NULL;
367 unsigned char *ivp = NULL;
368 unsigned char iv_data[HASH_MAX_LEN];
369 int auth_off = 0, auth_len = 0, crypt_off = 0, crypt_len = 0, icv_off = 0;
370
371 dprintk("%s()\n", __FUNCTION__);
372 /* Sanity check */
373 if (crp == NULL) {
374 dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
375 return EINVAL;
376 }
377
378 crp->crp_etype = 0;
379
380 if (crp->crp_desc == NULL || crp->crp_buf == NULL) {
381 dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
382 crp->crp_etype = EINVAL;
383 goto done;
384 }
385
386 lid = crp->crp_sid & 0xffffffff;
387 if (lid >= octo_sesnum || lid == 0 || octo_sessions == NULL ||
388 octo_sessions[lid] == NULL) {
389 crp->crp_etype = ENOENT;
390 dprintk("%s,%d: ENOENT\n", __FILE__, __LINE__);
391 goto done;
392 }
393 od = octo_sessions[lid];
394
395 /*
396 * do some error checking outside of the loop for SKB and IOV processing
397 * this leaves us with valid skb or uiop pointers for later
398 */
399 if (crp->crp_flags & CRYPTO_F_SKBUF) {
400 skb = (struct sk_buff *) crp->crp_buf;
401 if (skb_shinfo(skb)->nr_frags >= SCATTERLIST_MAX) {
402 printk("%s,%d: %d nr_frags > SCATTERLIST_MAX", __FILE__, __LINE__,
403 skb_shinfo(skb)->nr_frags);
404 goto done;
405 }
406 } else if (crp->crp_flags & CRYPTO_F_IOV) {
407 uiop = (struct uio *) crp->crp_buf;
408 if (uiop->uio_iovcnt > SCATTERLIST_MAX) {
409 printk("%s,%d: %d uio_iovcnt > SCATTERLIST_MAX", __FILE__, __LINE__,
410 uiop->uio_iovcnt);
411 goto done;
412 }
413 }
414
415 /* point our enccrd and maccrd appropriately */
416 crd = crp->crp_desc;
417 if (crd->crd_alg == od->octo_encalg) enccrd = crd;
418 if (crd->crd_alg == od->octo_macalg) maccrd = crd;
419 crd = crd->crd_next;
420 if (crd) {
421 if (crd->crd_alg == od->octo_encalg) enccrd = crd;
422 if (crd->crd_alg == od->octo_macalg) maccrd = crd;
423 crd = crd->crd_next;
424 }
425 if (crd) {
426 crp->crp_etype = EINVAL;
427 dprintk("%s,%d: ENOENT - descriptors do not match session\n",
428 __FILE__, __LINE__);
429 goto done;
430 }
431
432 if (enccrd) {
433 if (enccrd->crd_flags & CRD_F_IV_EXPLICIT) {
434 ivp = enccrd->crd_iv;
435 } else {
436 ivp = iv_data;
437 crypto_copydata(crp->crp_flags, crp->crp_buf,
438 enccrd->crd_inject, od->octo_ivsize, (caddr_t) ivp);
439 }
440
441 if (maccrd) {
442 auth_off = maccrd->crd_skip;
443 auth_len = maccrd->crd_len;
444 icv_off = maccrd->crd_inject;
445 }
446
447 crypt_off = enccrd->crd_skip;
448 crypt_len = enccrd->crd_len;
449 } else { /* if (maccrd) */
450 auth_off = maccrd->crd_skip;
451 auth_len = maccrd->crd_len;
452 icv_off = maccrd->crd_inject;
453 }
454
455
456 /*
457 * setup the SG list to cover the buffer
458 */
459 memset(sg, 0, sizeof(sg));
460 if (crp->crp_flags & CRYPTO_F_SKBUF) {
461 int i, len;
462
463 sg_num = 0;
464 sg_len = 0;
465
466 len = skb_headlen(skb);
467 sg_set_page(&sg[sg_num], virt_to_page(skb->data), len,
468 offset_in_page(skb->data));
469 sg_len += len;
470 sg_num++;
471
472 for (i = 0; i < skb_shinfo(skb)->nr_frags && sg_num < SCATTERLIST_MAX;
473 i++) {
474 len = skb_shinfo(skb)->frags[i].size;
475 sg_set_page(&sg[sg_num], skb_shinfo(skb)->frags[i].page,
476 len, skb_shinfo(skb)->frags[i].page_offset);
477 sg_len += len;
478 sg_num++;
479 }
480 } else if (crp->crp_flags & CRYPTO_F_IOV) {
481 int len;
482
483 sg_len = 0;
484 for (sg_num = 0; sg_len < crp->crp_ilen &&
485 sg_num < uiop->uio_iovcnt &&
486 sg_num < SCATTERLIST_MAX; sg_num++) {
487 len = uiop->uio_iov[sg_num].iov_len;
488 sg_set_page(&sg[sg_num],
489 virt_to_page(uiop->uio_iov[sg_num].iov_base), len,
490 offset_in_page(uiop->uio_iov[sg_num].iov_base));
491 sg_len += len;
492 }
493 } else {
494 sg_len = crp->crp_ilen;
495 sg_set_page(&sg[0], virt_to_page(crp->crp_buf), sg_len,
496 offset_in_page(crp->crp_buf));
497 sg_num = 1;
498 }
499
500
501 /*
502 * setup a new explicit key
503 */
504 if (enccrd) {
505 if (enccrd->crd_flags & CRD_F_KEY_EXPLICIT) {
506 od->octo_encklen = (enccrd->crd_klen + 7) / 8;
507 memcpy(od->octo_enckey, enccrd->crd_key, od->octo_encklen);
508 }
509 }
510 if (maccrd) {
511 if (maccrd->crd_flags & CRD_F_KEY_EXPLICIT) {
512 od->octo_macklen = (maccrd->crd_klen + 7) / 8;
513 memcpy(od->octo_mackey, maccrd->crd_key, od->octo_macklen);
514 od->octo_mackey_set = 0;
515 }
516 if (!od->octo_mackey_set) {
517 octo_calc_hash(maccrd->crd_alg == CRYPTO_MD5_HMAC ? 0 : 1,
518 maccrd->crd_key, od->octo_hminner, od->octo_hmouter);
519 od->octo_mackey_set = 1;
520 }
521 }
522
523
524 if (!enccrd || (enccrd->crd_flags & CRD_F_ENCRYPT))
525 (*od->octo_encrypt)(od, sg, sg_len,
526 auth_off, auth_len, crypt_off, crypt_len, icv_off, ivp);
527 else
528 (*od->octo_decrypt)(od, sg, sg_len,
529 auth_off, auth_len, crypt_off, crypt_len, icv_off, ivp);
530
531 done:
532 crypto_done(crp);
533 return 0;
534 }
535
536 static int
537 cryptocteon_init(void)
538 {
539 dprintk("%s(%p)\n", __FUNCTION__, cryptocteon_init);
540
541 softc_device_init(&octo_softc, "cryptocteon", 0, octo_methods);
542
543 octo_id = crypto_get_driverid(softc_get_device(&octo_softc),
544 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SYNC);
545 if (octo_id < 0) {
546 printk("Cryptocteon device cannot initialize!");
547 return -ENODEV;
548 }
549
550 crypto_register(octo_id, CRYPTO_MD5_HMAC, 0,0);
551 crypto_register(octo_id, CRYPTO_SHA1_HMAC, 0,0);
552 //crypto_register(octo_id, CRYPTO_MD5, 0,0);
553 //crypto_register(octo_id, CRYPTO_SHA1, 0,0);
554 crypto_register(octo_id, CRYPTO_DES_CBC, 0,0);
555 crypto_register(octo_id, CRYPTO_3DES_CBC, 0,0);
556 crypto_register(octo_id, CRYPTO_AES_CBC, 0,0);
557
558 return(0);
559 }
560
561 static void
562 cryptocteon_exit(void)
563 {
564 dprintk("%s()\n", __FUNCTION__);
565 crypto_unregister_all(octo_id);
566 octo_id = -1;
567 }
568
569 module_init(cryptocteon_init);
570 module_exit(cryptocteon_exit);
571
572 MODULE_LICENSE("BSD");
573 MODULE_AUTHOR("David McCullough <david_mccullough@mcafee.com>");
574 MODULE_DESCRIPTION("Cryptocteon (OCF module for Cavium OCTEON crypto)");
This page took 0.080835 seconds and 5 git commands to generate.