1 /* $OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $ */
2
3 /*-
4 * Copyright (c) 2001 Theo de Raadt
5 * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * Effort sponsored in part by the Defense Advanced Research Projects
31 * Agency (DARPA) and Air Force Research Laboratory, Air Force
32 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
33 */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: releng/9.2/sys/opencrypto/cryptodev.c 224914 2011-08-16 20:07:47Z kib $");
37
38 #include "opt_compat.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/sysctl.h>
47 #include <sys/file.h>
48 #include <sys/filedesc.h>
49 #include <sys/errno.h>
50 #include <sys/uio.h>
51 #include <sys/random.h>
52 #include <sys/conf.h>
53 #include <sys/kernel.h>
54 #include <sys/module.h>
55 #include <sys/fcntl.h>
56 #include <sys/bus.h>
57
58 #include <opencrypto/cryptodev.h>
59 #include <opencrypto/xform.h>
60
61 #ifdef COMPAT_FREEBSD32
62 #include <sys/mount.h>
63 #include <compat/freebsd32/freebsd32.h>
64
65 struct session_op32 {
66 u_int32_t cipher;
67 u_int32_t mac;
68 u_int32_t keylen;
69 u_int32_t key;
70 int mackeylen;
71 u_int32_t mackey;
72 u_int32_t ses;
73 };
74
75 struct session2_op32 {
76 u_int32_t cipher;
77 u_int32_t mac;
78 u_int32_t keylen;
79 u_int32_t key;
80 int mackeylen;
81 u_int32_t mackey;
82 u_int32_t ses;
83 int crid;
84 int pad[4];
85 };
86
87 struct crypt_op32 {
88 u_int32_t ses;
89 u_int16_t op;
90 u_int16_t flags;
91 u_int len;
92 u_int32_t src, dst;
93 u_int32_t mac;
94 u_int32_t iv;
95 };
96
97 struct crparam32 {
98 u_int32_t crp_p;
99 u_int crp_nbits;
100 };
101
102 struct crypt_kop32 {
103 u_int crk_op;
104 u_int crk_status;
105 u_short crk_iparams;
106 u_short crk_oparams;
107 u_int crk_crid;
108 struct crparam32 crk_param[CRK_MAXPARAM];
109 };
110
111 struct cryptotstat32 {
112 struct timespec32 acc;
113 struct timespec32 min;
114 struct timespec32 max;
115 u_int32_t count;
116 };
117
118 struct cryptostats32 {
119 u_int32_t cs_ops;
120 u_int32_t cs_errs;
121 u_int32_t cs_kops;
122 u_int32_t cs_kerrs;
123 u_int32_t cs_intrs;
124 u_int32_t cs_rets;
125 u_int32_t cs_blocks;
126 u_int32_t cs_kblocks;
127 struct cryptotstat32 cs_invoke;
128 struct cryptotstat32 cs_done;
129 struct cryptotstat32 cs_cb;
130 struct cryptotstat32 cs_finis;
131 };
132
133 #define CIOCGSESSION32 _IOWR('c', 101, struct session_op32)
134 #define CIOCCRYPT32 _IOWR('c', 103, struct crypt_op32)
135 #define CIOCKEY32 _IOWR('c', 104, struct crypt_kop32)
136 #define CIOCGSESSION232 _IOWR('c', 106, struct session2_op32)
137 #define CIOCKEY232 _IOWR('c', 107, struct crypt_kop32)
138
139 static void
140 session_op_from_32(const struct session_op32 *from, struct session_op *to)
141 {
142
143 CP(*from, *to, cipher);
144 CP(*from, *to, mac);
145 CP(*from, *to, keylen);
146 PTRIN_CP(*from, *to, key);
147 CP(*from, *to, mackeylen);
148 PTRIN_CP(*from, *to, mackey);
149 CP(*from, *to, ses);
150 }
151
152 static void
153 session2_op_from_32(const struct session2_op32 *from, struct session2_op *to)
154 {
155
156 session_op_from_32((const struct session_op32 *)from,
157 (struct session_op *)to);
158 CP(*from, *to, crid);
159 }
160
161 static void
162 session_op_to_32(const struct session_op *from, struct session_op32 *to)
163 {
164
165 CP(*from, *to, cipher);
166 CP(*from, *to, mac);
167 CP(*from, *to, keylen);
168 PTROUT_CP(*from, *to, key);
169 CP(*from, *to, mackeylen);
170 PTROUT_CP(*from, *to, mackey);
171 CP(*from, *to, ses);
172 }
173
174 static void
175 session2_op_to_32(const struct session2_op *from, struct session2_op32 *to)
176 {
177
178 session_op_to_32((const struct session_op *)from,
179 (struct session_op32 *)to);
180 CP(*from, *to, crid);
181 }
182
183 static void
184 crypt_op_from_32(const struct crypt_op32 *from, struct crypt_op *to)
185 {
186
187 CP(*from, *to, ses);
188 CP(*from, *to, op);
189 CP(*from, *to, flags);
190 CP(*from, *to, len);
191 PTRIN_CP(*from, *to, src);
192 PTRIN_CP(*from, *to, dst);
193 PTRIN_CP(*from, *to, mac);
194 PTRIN_CP(*from, *to, iv);
195 }
196
197 static void
198 crypt_op_to_32(const struct crypt_op *from, struct crypt_op32 *to)
199 {
200
201 CP(*from, *to, ses);
202 CP(*from, *to, op);
203 CP(*from, *to, flags);
204 CP(*from, *to, len);
205 PTROUT_CP(*from, *to, src);
206 PTROUT_CP(*from, *to, dst);
207 PTROUT_CP(*from, *to, mac);
208 PTROUT_CP(*from, *to, iv);
209 }
210
211 static void
212 crparam_from_32(const struct crparam32 *from, struct crparam *to)
213 {
214
215 PTRIN_CP(*from, *to, crp_p);
216 CP(*from, *to, crp_nbits);
217 }
218
219 static void
220 crparam_to_32(const struct crparam *from, struct crparam32 *to)
221 {
222
223 PTROUT_CP(*from, *to, crp_p);
224 CP(*from, *to, crp_nbits);
225 }
226
227 static void
228 crypt_kop_from_32(const struct crypt_kop32 *from, struct crypt_kop *to)
229 {
230 int i;
231
232 CP(*from, *to, crk_op);
233 CP(*from, *to, crk_status);
234 CP(*from, *to, crk_iparams);
235 CP(*from, *to, crk_oparams);
236 CP(*from, *to, crk_crid);
237 for (i = 0; i < CRK_MAXPARAM; i++)
238 crparam_from_32(&from->crk_param[i], &to->crk_param[i]);
239 }
240
241 static void
242 crypt_kop_to_32(const struct crypt_kop *from, struct crypt_kop32 *to)
243 {
244 int i;
245
246 CP(*from, *to, crk_op);
247 CP(*from, *to, crk_status);
248 CP(*from, *to, crk_iparams);
249 CP(*from, *to, crk_oparams);
250 CP(*from, *to, crk_crid);
251 for (i = 0; i < CRK_MAXPARAM; i++)
252 crparam_to_32(&from->crk_param[i], &to->crk_param[i]);
253 }
254 #endif
255
256 struct csession {
257 TAILQ_ENTRY(csession) next;
258 u_int64_t sid;
259 u_int32_t ses;
260 struct mtx lock; /* for op submission */
261
262 u_int32_t cipher;
263 struct enc_xform *txform;
264 u_int32_t mac;
265 struct auth_hash *thash;
266
267 caddr_t key;
268 int keylen;
269 u_char tmp_iv[EALG_MAX_BLOCK_LEN];
270
271 caddr_t mackey;
272 int mackeylen;
273
274 struct iovec iovec;
275 struct uio uio;
276 int error;
277 };
278
279 struct fcrypt {
280 TAILQ_HEAD(csessionlist, csession) csessions;
281 int sesn;
282 };
283
284 static int cryptof_rw(struct file *fp, struct uio *uio,
285 struct ucred *cred, int flags, struct thread *);
286 static int cryptof_truncate(struct file *, off_t, struct ucred *,
287 struct thread *);
288 static int cryptof_ioctl(struct file *, u_long, void *,
289 struct ucred *, struct thread *);
290 static int cryptof_poll(struct file *, int, struct ucred *, struct thread *);
291 static int cryptof_kqfilter(struct file *, struct knote *);
292 static int cryptof_stat(struct file *, struct stat *,
293 struct ucred *, struct thread *);
294 static int cryptof_close(struct file *, struct thread *);
295
296 static struct fileops cryptofops = {
297 .fo_read = cryptof_rw,
298 .fo_write = cryptof_rw,
299 .fo_truncate = cryptof_truncate,
300 .fo_ioctl = cryptof_ioctl,
301 .fo_poll = cryptof_poll,
302 .fo_kqfilter = cryptof_kqfilter,
303 .fo_stat = cryptof_stat,
304 .fo_close = cryptof_close,
305 .fo_chmod = invfo_chmod,
306 .fo_chown = invfo_chown,
307 };
308
309 static struct csession *csefind(struct fcrypt *, u_int);
310 static int csedelete(struct fcrypt *, struct csession *);
311 static struct csession *cseadd(struct fcrypt *, struct csession *);
312 static struct csession *csecreate(struct fcrypt *, u_int64_t, caddr_t,
313 u_int64_t, caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
314 struct auth_hash *);
315 static int csefree(struct csession *);
316
317 static int cryptodev_op(struct csession *, struct crypt_op *,
318 struct ucred *, struct thread *td);
319 static int cryptodev_key(struct crypt_kop *);
320 static int cryptodev_find(struct crypt_find_op *);
321
322 static int
323 cryptof_rw(
324 struct file *fp,
325 struct uio *uio,
326 struct ucred *active_cred,
327 int flags,
328 struct thread *td)
329 {
330
331 return (EIO);
332 }
333
334 static int
335 cryptof_truncate(
336 struct file *fp,
337 off_t length,
338 struct ucred *active_cred,
339 struct thread *td)
340 {
341
342 return (EINVAL);
343 }
344
345 /*
346 * Check a crypto identifier to see if it requested
347 * a software device/driver. This can be done either
348 * by device name/class or through search constraints.
349 */
350 static int
351 checkforsoftware(int crid)
352 {
353 if (crid & CRYPTOCAP_F_SOFTWARE)
354 return EINVAL; /* XXX */
355 if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
356 (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
357 return EINVAL; /* XXX */
358 return 0;
359 }
360
361 /* ARGSUSED */
362 static int
363 cryptof_ioctl(
364 struct file *fp,
365 u_long cmd,
366 void *data,
367 struct ucred *active_cred,
368 struct thread *td)
369 {
370 #define SES2(p) ((struct session2_op *)p)
371 struct cryptoini cria, crie;
372 struct fcrypt *fcr = fp->f_data;
373 struct csession *cse;
374 struct session_op *sop;
375 struct crypt_op *cop;
376 struct enc_xform *txform = NULL;
377 struct auth_hash *thash = NULL;
378 struct crypt_kop *kop;
379 u_int64_t sid;
380 u_int32_t ses;
381 int error = 0, crid;
382 #ifdef COMPAT_FREEBSD32
383 struct session2_op sopc;
384 struct crypt_op copc;
385 struct crypt_kop kopc;
386 #endif
387
388 switch (cmd) {
389 case CIOCGSESSION:
390 case CIOCGSESSION2:
391 #ifdef COMPAT_FREEBSD32
392 case CIOCGSESSION32:
393 case CIOCGSESSION232:
394 if (cmd == CIOCGSESSION32) {
395 session_op_from_32(data, (struct session_op *)&sopc);
396 sop = (struct session_op *)&sopc;
397 } else if (cmd == CIOCGSESSION232) {
398 session2_op_from_32(data, &sopc);
399 sop = (struct session_op *)&sopc;
400 } else
401 #endif
402 sop = (struct session_op *)data;
403 switch (sop->cipher) {
404 case 0:
405 break;
406 case CRYPTO_DES_CBC:
407 txform = &enc_xform_des;
408 break;
409 case CRYPTO_3DES_CBC:
410 txform = &enc_xform_3des;
411 break;
412 case CRYPTO_BLF_CBC:
413 txform = &enc_xform_blf;
414 break;
415 case CRYPTO_CAST_CBC:
416 txform = &enc_xform_cast5;
417 break;
418 case CRYPTO_SKIPJACK_CBC:
419 txform = &enc_xform_skipjack;
420 break;
421 case CRYPTO_AES_CBC:
422 txform = &enc_xform_rijndael128;
423 break;
424 case CRYPTO_AES_XTS:
425 txform = &enc_xform_aes_xts;
426 break;
427 case CRYPTO_NULL_CBC:
428 txform = &enc_xform_null;
429 break;
430 case CRYPTO_ARC4:
431 txform = &enc_xform_arc4;
432 break;
433 case CRYPTO_CAMELLIA_CBC:
434 txform = &enc_xform_camellia;
435 break;
436 default:
437 return (EINVAL);
438 }
439
440 switch (sop->mac) {
441 case 0:
442 break;
443 case CRYPTO_MD5_HMAC:
444 thash = &auth_hash_hmac_md5;
445 break;
446 case CRYPTO_SHA1_HMAC:
447 thash = &auth_hash_hmac_sha1;
448 break;
449 case CRYPTO_SHA2_256_HMAC:
450 thash = &auth_hash_hmac_sha2_256;
451 break;
452 case CRYPTO_SHA2_384_HMAC:
453 thash = &auth_hash_hmac_sha2_384;
454 break;
455 case CRYPTO_SHA2_512_HMAC:
456 thash = &auth_hash_hmac_sha2_512;
457 break;
458 case CRYPTO_RIPEMD160_HMAC:
459 thash = &auth_hash_hmac_ripemd_160;
460 break;
461 #ifdef notdef
462 case CRYPTO_MD5:
463 thash = &auth_hash_md5;
464 break;
465 case CRYPTO_SHA1:
466 thash = &auth_hash_sha1;
467 break;
468 #endif
469 case CRYPTO_NULL_HMAC:
470 thash = &auth_hash_null;
471 break;
472 default:
473 return (EINVAL);
474 }
475
476 bzero(&crie, sizeof(crie));
477 bzero(&cria, sizeof(cria));
478
479 if (txform) {
480 crie.cri_alg = txform->type;
481 crie.cri_klen = sop->keylen * 8;
482 if (sop->keylen > txform->maxkey ||
483 sop->keylen < txform->minkey) {
484 error = EINVAL;
485 goto bail;
486 }
487
488 crie.cri_key = malloc(crie.cri_klen / 8,
489 M_XDATA, M_WAITOK);
490 if ((error = copyin(sop->key, crie.cri_key,
491 crie.cri_klen / 8)))
492 goto bail;
493 if (thash)
494 crie.cri_next = &cria;
495 }
496
497 if (thash) {
498 cria.cri_alg = thash->type;
499 cria.cri_klen = sop->mackeylen * 8;
500 if (sop->mackeylen != thash->keysize) {
501 error = EINVAL;
502 goto bail;
503 }
504
505 if (cria.cri_klen) {
506 cria.cri_key = malloc(cria.cri_klen / 8,
507 M_XDATA, M_WAITOK);
508 if ((error = copyin(sop->mackey, cria.cri_key,
509 cria.cri_klen / 8)))
510 goto bail;
511 }
512 }
513
514 /* NB: CIOCGSESSION2 has the crid */
515 if (cmd == CIOCGSESSION2
516 #ifdef COMPAT_FREEBSD32
517 || cmd == CIOCGSESSION232
518 #endif
519 ) {
520 crid = SES2(sop)->crid;
521 error = checkforsoftware(crid);
522 if (error)
523 goto bail;
524 } else
525 crid = CRYPTOCAP_F_HARDWARE;
526 error = crypto_newsession(&sid, (txform ? &crie : &cria), crid);
527 if (error)
528 goto bail;
529
530 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
531 cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
532 thash);
533
534 if (cse == NULL) {
535 crypto_freesession(sid);
536 error = EINVAL;
537 goto bail;
538 }
539 sop->ses = cse->ses;
540 if (cmd == CIOCGSESSION2
541 #ifdef COMPAT_FREEBSD32
542 || cmd == CIOCGSESSION232
543 #endif
544 ) {
545 /* return hardware/driver id */
546 SES2(sop)->crid = CRYPTO_SESID2HID(cse->sid);
547 }
548 bail:
549 if (error) {
550 if (crie.cri_key)
551 free(crie.cri_key, M_XDATA);
552 if (cria.cri_key)
553 free(cria.cri_key, M_XDATA);
554 }
555 #ifdef COMPAT_FREEBSD32
556 else {
557 if (cmd == CIOCGSESSION32)
558 session_op_to_32(sop, data);
559 else if (cmd == CIOCGSESSION232)
560 session2_op_to_32((struct session2_op *)sop,
561 data);
562 }
563 #endif
564 break;
565 case CIOCFSESSION:
566 ses = *(u_int32_t *)data;
567 cse = csefind(fcr, ses);
568 if (cse == NULL)
569 return (EINVAL);
570 csedelete(fcr, cse);
571 error = csefree(cse);
572 break;
573 case CIOCCRYPT:
574 #ifdef COMPAT_FREEBSD32
575 case CIOCCRYPT32:
576 if (cmd == CIOCCRYPT32) {
577 cop = &copc;
578 crypt_op_from_32(data, cop);
579 } else
580 #endif
581 cop = (struct crypt_op *)data;
582 cse = csefind(fcr, cop->ses);
583 if (cse == NULL)
584 return (EINVAL);
585 error = cryptodev_op(cse, cop, active_cred, td);
586 #ifdef COMPAT_FREEBSD32
587 if (error == 0 && cmd == CIOCCRYPT32)
588 crypt_op_to_32(cop, data);
589 #endif
590 break;
591 case CIOCKEY:
592 case CIOCKEY2:
593 #ifdef COMPAT_FREEBSD32
594 case CIOCKEY32:
595 case CIOCKEY232:
596 #endif
597 if (!crypto_userasymcrypto)
598 return (EPERM); /* XXX compat? */
599 #ifdef COMPAT_FREEBSD32
600 if (cmd == CIOCKEY32 || cmd == CIOCKEY232) {
601 kop = &kopc;
602 crypt_kop_from_32(data, kop);
603 } else
604 #endif
605 kop = (struct crypt_kop *)data;
606 if (cmd == CIOCKEY
607 #ifdef COMPAT_FREEBSD32
608 || cmd == CIOCKEY32
609 #endif
610 ) {
611 /* NB: crypto core enforces s/w driver use */
612 kop->crk_crid =
613 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
614 }
615 mtx_lock(&Giant);
616 error = cryptodev_key(kop);
617 mtx_unlock(&Giant);
618 #ifdef COMPAT_FREEBSD32
619 if (cmd == CIOCKEY32 || cmd == CIOCKEY232)
620 crypt_kop_to_32(kop, data);
621 #endif
622 break;
623 case CIOCASYMFEAT:
624 if (!crypto_userasymcrypto) {
625 /*
626 * NB: if user asym crypto operations are
627 * not permitted return "no algorithms"
628 * so well-behaved applications will just
629 * fallback to doing them in software.
630 */
631 *(int *)data = 0;
632 } else
633 error = crypto_getfeat((int *)data);
634 break;
635 case CIOCFINDDEV:
636 error = cryptodev_find((struct crypt_find_op *)data);
637 break;
638 default:
639 error = EINVAL;
640 break;
641 }
642 return (error);
643 #undef SES2
644 }
645
646 static int cryptodev_cb(void *);
647
648
649 static int
650 cryptodev_op(
651 struct csession *cse,
652 struct crypt_op *cop,
653 struct ucred *active_cred,
654 struct thread *td)
655 {
656 struct cryptop *crp = NULL;
657 struct cryptodesc *crde = NULL, *crda = NULL;
658 int error;
659
660 if (cop->len > 256*1024-4)
661 return (E2BIG);
662
663 if (cse->txform) {
664 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
665 return (EINVAL);
666 }
667
668 cse->uio.uio_iov = &cse->iovec;
669 cse->uio.uio_iovcnt = 1;
670 cse->uio.uio_offset = 0;
671 cse->uio.uio_resid = cop->len;
672 cse->uio.uio_segflg = UIO_SYSSPACE;
673 cse->uio.uio_rw = UIO_WRITE;
674 cse->uio.uio_td = td;
675 cse->uio.uio_iov[0].iov_len = cop->len;
676 if (cse->thash) {
677 cse->uio.uio_iov[0].iov_len += cse->thash->hashsize;
678 cse->uio.uio_resid += cse->thash->hashsize;
679 }
680 cse->uio.uio_iov[0].iov_base = malloc(cse->uio.uio_iov[0].iov_len,
681 M_XDATA, M_WAITOK);
682
683 crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
684 if (crp == NULL) {
685 error = ENOMEM;
686 goto bail;
687 }
688
689 if (cse->thash) {
690 crda = crp->crp_desc;
691 if (cse->txform)
692 crde = crda->crd_next;
693 } else {
694 if (cse->txform)
695 crde = crp->crp_desc;
696 else {
697 error = EINVAL;
698 goto bail;
699 }
700 }
701
702 if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
703 goto bail;
704
705 if (crda) {
706 crda->crd_skip = 0;
707 crda->crd_len = cop->len;
708 crda->crd_inject = cop->len;
709
710 crda->crd_alg = cse->mac;
711 crda->crd_key = cse->mackey;
712 crda->crd_klen = cse->mackeylen * 8;
713 }
714
715 if (crde) {
716 if (cop->op == COP_ENCRYPT)
717 crde->crd_flags |= CRD_F_ENCRYPT;
718 else
719 crde->crd_flags &= ~CRD_F_ENCRYPT;
720 crde->crd_len = cop->len;
721 crde->crd_inject = 0;
722
723 crde->crd_alg = cse->cipher;
724 crde->crd_key = cse->key;
725 crde->crd_klen = cse->keylen * 8;
726 }
727
728 crp->crp_ilen = cop->len;
729 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
730 | (cop->flags & COP_F_BATCH);
731 crp->crp_buf = (caddr_t)&cse->uio;
732 crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
733 crp->crp_sid = cse->sid;
734 crp->crp_opaque = (void *)cse;
735
736 if (cop->iv) {
737 if (crde == NULL) {
738 error = EINVAL;
739 goto bail;
740 }
741 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
742 error = EINVAL;
743 goto bail;
744 }
745 if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
746 goto bail;
747 bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
748 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
749 crde->crd_skip = 0;
750 } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
751 crde->crd_skip = 0;
752 } else if (crde) {
753 crde->crd_flags |= CRD_F_IV_PRESENT;
754 crde->crd_skip = cse->txform->blocksize;
755 crde->crd_len -= cse->txform->blocksize;
756 }
757
758 if (cop->mac && crda == NULL) {
759 error = EINVAL;
760 goto bail;
761 }
762
763 again:
764 /*
765 * Let the dispatch run unlocked, then, interlock against the
766 * callback before checking if the operation completed and going
767 * to sleep. This insures drivers don't inherit our lock which
768 * results in a lock order reversal between crypto_dispatch forced
769 * entry and the crypto_done callback into us.
770 */
771 error = crypto_dispatch(crp);
772 mtx_lock(&cse->lock);
773 if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
774 error = msleep(crp, &cse->lock, PWAIT, "crydev", 0);
775 mtx_unlock(&cse->lock);
776
777 if (error != 0)
778 goto bail;
779
780 if (crp->crp_etype == EAGAIN) {
781 crp->crp_etype = 0;
782 crp->crp_flags &= ~CRYPTO_F_DONE;
783 goto again;
784 }
785
786 if (crp->crp_etype != 0) {
787 error = crp->crp_etype;
788 goto bail;
789 }
790
791 if (cse->error) {
792 error = cse->error;
793 goto bail;
794 }
795
796 if (cop->dst &&
797 (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
798 goto bail;
799
800 if (cop->mac &&
801 (error = copyout((caddr_t)cse->uio.uio_iov[0].iov_base + cop->len,
802 cop->mac, cse->thash->hashsize)))
803 goto bail;
804
805 bail:
806 if (crp)
807 crypto_freereq(crp);
808 if (cse->uio.uio_iov[0].iov_base)
809 free(cse->uio.uio_iov[0].iov_base, M_XDATA);
810
811 return (error);
812 }
813
814 static int
815 cryptodev_cb(void *op)
816 {
817 struct cryptop *crp = (struct cryptop *) op;
818 struct csession *cse = (struct csession *)crp->crp_opaque;
819
820 mtx_lock(&cse->lock);
821 cse->error = crp->crp_etype;
822 wakeup_one(crp);
823 mtx_unlock(&cse->lock);
824 return (0);
825 }
826
827 static int
828 cryptodevkey_cb(void *op)
829 {
830 struct cryptkop *krp = (struct cryptkop *) op;
831
832 wakeup_one(krp);
833 return (0);
834 }
835
836 static int
837 cryptodev_key(struct crypt_kop *kop)
838 {
839 struct cryptkop *krp = NULL;
840 int error = EINVAL;
841 int in, out, size, i;
842
843 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
844 return (EFBIG);
845 }
846
847 in = kop->crk_iparams;
848 out = kop->crk_oparams;
849 switch (kop->crk_op) {
850 case CRK_MOD_EXP:
851 if (in == 3 && out == 1)
852 break;
853 return (EINVAL);
854 case CRK_MOD_EXP_CRT:
855 if (in == 6 && out == 1)
856 break;
857 return (EINVAL);
858 case CRK_DSA_SIGN:
859 if (in == 5 && out == 2)
860 break;
861 return (EINVAL);
862 case CRK_DSA_VERIFY:
863 if (in == 7 && out == 0)
864 break;
865 return (EINVAL);
866 case CRK_DH_COMPUTE_KEY:
867 if (in == 3 && out == 1)
868 break;
869 return (EINVAL);
870 default:
871 return (EINVAL);
872 }
873
874 krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK|M_ZERO);
875 if (!krp)
876 return (ENOMEM);
877 krp->krp_op = kop->crk_op;
878 krp->krp_status = kop->crk_status;
879 krp->krp_iparams = kop->crk_iparams;
880 krp->krp_oparams = kop->crk_oparams;
881 krp->krp_crid = kop->crk_crid;
882 krp->krp_status = 0;
883 krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
884
885 for (i = 0; i < CRK_MAXPARAM; i++) {
886 if (kop->crk_param[i].crp_nbits > 65536)
887 /* Limit is the same as in OpenBSD */
888 goto fail;
889 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
890 }
891 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
892 size = (krp->krp_param[i].crp_nbits + 7) / 8;
893 if (size == 0)
894 continue;
895 krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
896 if (i >= krp->krp_iparams)
897 continue;
898 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
899 if (error)
900 goto fail;
901 }
902
903 error = crypto_kdispatch(krp);
904 if (error)
905 goto fail;
906 error = tsleep(krp, PSOCK, "crydev", 0);
907 if (error) {
908 /* XXX can this happen? if so, how do we recover? */
909 goto fail;
910 }
911
912 kop->crk_crid = krp->krp_crid; /* device that did the work */
913 if (krp->krp_status != 0) {
914 error = krp->krp_status;
915 goto fail;
916 }
917
918 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
919 size = (krp->krp_param[i].crp_nbits + 7) / 8;
920 if (size == 0)
921 continue;
922 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
923 if (error)
924 goto fail;
925 }
926
927 fail:
928 if (krp) {
929 kop->crk_status = krp->krp_status;
930 for (i = 0; i < CRK_MAXPARAM; i++) {
931 if (krp->krp_param[i].crp_p)
932 free(krp->krp_param[i].crp_p, M_XDATA);
933 }
934 free(krp, M_XDATA);
935 }
936 return (error);
937 }
938
939 static int
940 cryptodev_find(struct crypt_find_op *find)
941 {
942 device_t dev;
943
944 if (find->crid != -1) {
945 dev = crypto_find_device_byhid(find->crid);
946 if (dev == NULL)
947 return (ENOENT);
948 strlcpy(find->name, device_get_nameunit(dev),
949 sizeof(find->name));
950 } else {
951 find->crid = crypto_find_driver(find->name);
952 if (find->crid == -1)
953 return (ENOENT);
954 }
955 return (0);
956 }
957
958 /* ARGSUSED */
959 static int
960 cryptof_poll(
961 struct file *fp,
962 int events,
963 struct ucred *active_cred,
964 struct thread *td)
965 {
966
967 return (0);
968 }
969
970 /* ARGSUSED */
971 static int
972 cryptof_kqfilter(struct file *fp, struct knote *kn)
973 {
974
975 return (0);
976 }
977
978 /* ARGSUSED */
979 static int
980 cryptof_stat(
981 struct file *fp,
982 struct stat *sb,
983 struct ucred *active_cred,
984 struct thread *td)
985 {
986
987 return (EOPNOTSUPP);
988 }
989
990 /* ARGSUSED */
991 static int
992 cryptof_close(struct file *fp, struct thread *td)
993 {
994 struct fcrypt *fcr = fp->f_data;
995 struct csession *cse;
996
997 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
998 TAILQ_REMOVE(&fcr->csessions, cse, next);
999 (void)csefree(cse);
1000 }
1001 free(fcr, M_XDATA);
1002 fp->f_data = NULL;
1003 return 0;
1004 }
1005
1006 static struct csession *
1007 csefind(struct fcrypt *fcr, u_int ses)
1008 {
1009 struct csession *cse;
1010
1011 TAILQ_FOREACH(cse, &fcr->csessions, next)
1012 if (cse->ses == ses)
1013 return (cse);
1014 return (NULL);
1015 }
1016
1017 static int
1018 csedelete(struct fcrypt *fcr, struct csession *cse_del)
1019 {
1020 struct csession *cse;
1021
1022 TAILQ_FOREACH(cse, &fcr->csessions, next) {
1023 if (cse == cse_del) {
1024 TAILQ_REMOVE(&fcr->csessions, cse, next);
1025 return (1);
1026 }
1027 }
1028 return (0);
1029 }
1030
1031 static struct csession *
1032 cseadd(struct fcrypt *fcr, struct csession *cse)
1033 {
1034 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
1035 cse->ses = fcr->sesn++;
1036 return (cse);
1037 }
1038
1039 struct csession *
1040 csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
1041 caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
1042 struct enc_xform *txform, struct auth_hash *thash)
1043 {
1044 struct csession *cse;
1045
1046 #ifdef INVARIANTS
1047 /* NB: required when mtx_init is built with INVARIANTS */
1048 cse = malloc(sizeof(struct csession), M_XDATA, M_NOWAIT | M_ZERO);
1049 #else
1050 cse = malloc(sizeof(struct csession), M_XDATA, M_NOWAIT);
1051 #endif
1052 if (cse == NULL)
1053 return NULL;
1054 mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
1055 cse->key = key;
1056 cse->keylen = keylen/8;
1057 cse->mackey = mackey;
1058 cse->mackeylen = mackeylen/8;
1059 cse->sid = sid;
1060 cse->cipher = cipher;
1061 cse->mac = mac;
1062 cse->txform = txform;
1063 cse->thash = thash;
1064 cseadd(fcr, cse);
1065 return (cse);
1066 }
1067
1068 static int
1069 csefree(struct csession *cse)
1070 {
1071 int error;
1072
1073 error = crypto_freesession(cse->sid);
1074 mtx_destroy(&cse->lock);
1075 if (cse->key)
1076 free(cse->key, M_XDATA);
1077 if (cse->mackey)
1078 free(cse->mackey, M_XDATA);
1079 free(cse, M_XDATA);
1080 return (error);
1081 }
1082
1083 static int
1084 cryptoopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
1085 {
1086 return (0);
1087 }
1088
1089 static int
1090 cryptoread(struct cdev *dev, struct uio *uio, int ioflag)
1091 {
1092 return (EIO);
1093 }
1094
1095 static int
1096 cryptowrite(struct cdev *dev, struct uio *uio, int ioflag)
1097 {
1098 return (EIO);
1099 }
1100
1101 static int
1102 cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1103 {
1104 struct file *f;
1105 struct fcrypt *fcr;
1106 int fd, error;
1107
1108 switch (cmd) {
1109 case CRIOGET:
1110 fcr = malloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK);
1111 TAILQ_INIT(&fcr->csessions);
1112 fcr->sesn = 0;
1113
1114 error = falloc(td, &f, &fd, 0);
1115
1116 if (error) {
1117 free(fcr, M_XDATA);
1118 return (error);
1119 }
1120 /* falloc automatically provides an extra reference to 'f'. */
1121 finit(f, FREAD | FWRITE, DTYPE_CRYPTO, fcr, &cryptofops);
1122 *(u_int32_t *)data = fd;
1123 fdrop(f, td);
1124 break;
1125 case CRIOFINDDEV:
1126 error = cryptodev_find((struct crypt_find_op *)data);
1127 break;
1128 case CRIOASYMFEAT:
1129 error = crypto_getfeat((int *)data);
1130 break;
1131 default:
1132 error = EINVAL;
1133 break;
1134 }
1135 return (error);
1136 }
1137
1138 static struct cdevsw crypto_cdevsw = {
1139 .d_version = D_VERSION,
1140 .d_flags = D_NEEDGIANT,
1141 .d_open = cryptoopen,
1142 .d_read = cryptoread,
1143 .d_write = cryptowrite,
1144 .d_ioctl = cryptoioctl,
1145 .d_name = "crypto",
1146 };
1147 static struct cdev *crypto_dev;
1148
1149 /*
1150 * Initialization code, both for static and dynamic loading.
1151 */
1152 static int
1153 cryptodev_modevent(module_t mod, int type, void *unused)
1154 {
1155 switch (type) {
1156 case MOD_LOAD:
1157 if (bootverbose)
1158 printf("crypto: <crypto device>\n");
1159 crypto_dev = make_dev(&crypto_cdevsw, 0,
1160 UID_ROOT, GID_WHEEL, 0666,
1161 "crypto");
1162 return 0;
1163 case MOD_UNLOAD:
1164 /*XXX disallow if active sessions */
1165 destroy_dev(crypto_dev);
1166 return 0;
1167 }
1168 return EINVAL;
1169 }
1170
1171 static moduledata_t cryptodev_mod = {
1172 "cryptodev",
1173 cryptodev_modevent,
1174 0
1175 };
1176 MODULE_VERSION(cryptodev, 1);
1177 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1178 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
1179 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);
Cache object: 4d7f71fc07c81817aed1fc6214bdb6b1
|