cryptodev.c revision 1.38 1 /* $NetBSD: cryptodev.c,v 1.38 2008/04/11 10:28:10 rmind Exp $ */
2 /* $FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $ */
3 /* $OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $ */
4
5 /*-
6 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Coyote Point Systems, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 /*
42 * Copyright (c) 2001 Theo de Raadt
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 *
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. The name of the author may not be used to endorse or promote products
54 * derived from this software without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
57 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
58 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
59 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
60 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
61 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
62 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
63 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
64 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
65 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66 *
67 * Effort sponsored in part by the Defense Advanced Research Projects
68 * Agency (DARPA) and Air Force Research Laboratory, Air Force
69 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
70 *
71 */
72
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.38 2008/04/11 10:28:10 rmind Exp $");
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kmem.h>
79 #include <sys/malloc.h>
80 #include <sys/mbuf.h>
81 #include <sys/pool.h>
82 #include <sys/sysctl.h>
83 #include <sys/file.h>
84 #include <sys/filedesc.h>
85 #include <sys/errno.h>
86 #include <sys/md5.h>
87 #include <sys/sha1.h>
88 #include <sys/conf.h>
89 #include <sys/device.h>
90 #include <sys/kauth.h>
91 #include <sys/select.h>
92 #include <sys/poll.h>
93 #include <sys/atomic.h>
94
95 #include "opt_ocf.h"
96 #include <opencrypto/cryptodev.h>
97 #include <opencrypto/xform.h>
98
99 struct csession {
100 TAILQ_ENTRY(csession) next;
101 u_int64_t sid;
102 u_int32_t ses;
103
104 u_int32_t cipher;
105 struct enc_xform *txform;
106 u_int32_t mac;
107 struct auth_hash *thash;
108
109 void * key;
110 int keylen;
111 u_char tmp_iv[EALG_MAX_BLOCK_LEN];
112
113 void * mackey;
114 int mackeylen;
115 u_char tmp_mac[CRYPTO_MAX_MAC_LEN];
116
117 struct iovec iovec[1]; /* user requests never have more */
118 struct uio uio;
119 int error;
120 };
121
122 struct fcrypt {
123 TAILQ_HEAD(csessionlist, csession) csessions;
124 TAILQ_HEAD(crprethead, cryptop) crp_ret_mq;
125 TAILQ_HEAD(krprethead, cryptkop) crp_ret_mkq;
126 int sesn;
127 struct selinfo sinfo;
128 u_int32_t requestid;
129 };
130
131 /* For our fixed-size allocations */
132 static struct pool fcrpl;
133 static struct pool csepl;
134
135 /* Declaration of master device (fd-cloning/ctxt-allocating) entrypoints */
136 static int cryptoopen(dev_t dev, int flag, int mode, struct lwp *l);
137 static int cryptoread(dev_t dev, struct uio *uio, int ioflag);
138 static int cryptowrite(dev_t dev, struct uio *uio, int ioflag);
139 static int cryptoselect(dev_t dev, int rw, struct lwp *l);
140
141 /* Declaration of cloned-device (per-ctxt) entrypoints */
142 static int cryptof_read(struct file *, off_t *, struct uio *,
143 kauth_cred_t, int);
144 static int cryptof_write(struct file *, off_t *, struct uio *,
145 kauth_cred_t, int);
146 static int cryptof_ioctl(struct file *, u_long, void *);
147 static int cryptof_close(struct file *);
148 static int cryptof_poll(struct file *, int);
149
150 static const struct fileops cryptofops = {
151 cryptof_read,
152 cryptof_write,
153 cryptof_ioctl,
154 fnullop_fcntl,
155 cryptof_poll,
156 fbadop_stat,
157 cryptof_close,
158 fnullop_kqfilter
159 };
160
161 static struct csession *csefind(struct fcrypt *, u_int);
162 static int csedelete(struct fcrypt *, struct csession *);
163 static struct csession *cseadd(struct fcrypt *, struct csession *);
164 static struct csession *csecreate(struct fcrypt *, u_int64_t, void *, u_int64_t,
165 void *, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
166 struct auth_hash *);
167 static int csefree(struct csession *);
168
169 static int cryptodev_op(struct csession *, struct crypt_op *, struct lwp *);
170 static int cryptodev_mop(struct fcrypt *, struct crypt_n_op *, int, struct
171 lwp *);
172 static int cryptodev_key(struct crypt_kop *);
173 static int cryptodev_mkey(struct fcrypt *, struct crypt_n_kop *, int);
174 static int cryptodev_session(struct fcrypt *, struct session_op *);
175 static int cryptodev_msession(struct fcrypt *, struct session_n_op *,
176 int);
177 static int cryptodev_msessionfin(struct fcrypt *, int, u_int32_t *);
178
179 static int cryptodev_cb(void *);
180 static int cryptodevkey_cb(void *);
181
182 static int cryptodev_mcb(void *);
183 static int cryptodevkey_mcb(void *);
184
185 static int cryptodev_getmstatus(struct fcrypt *, struct crypt_result *,
186 int);
187 static int cryptodev_getstatus(struct fcrypt *, struct crypt_result *);
188
189 /*
190 * sysctl-able control variables for /dev/crypto now defined in crypto.c:
191 * crypto_usercrypto, crypto_userasmcrypto, crypto_devallowsoft.
192 */
193
194 /* ARGSUSED */
195 int
196 cryptof_read(file_t *fp, off_t *poff,
197 struct uio *uio, kauth_cred_t cred, int flags)
198 {
199 return (EIO);
200 }
201
202 /* ARGSUSED */
203 int
204 cryptof_write(file_t *fp, off_t *poff,
205 struct uio *uio, kauth_cred_t cred, int flags)
206 {
207 return (EIO);
208 }
209
210 /* ARGSUSED */
211 int
212 cryptof_ioctl(struct file *fp, u_long cmd, void *data)
213 {
214 struct fcrypt *fcr = fp->f_data;
215 struct csession *cse;
216 struct session_op *sop;
217 struct session_n_op *snop;
218 struct crypt_op *cop;
219 struct crypt_mop *mop;
220 struct crypt_mkop *mkop;
221 struct crypt_n_op *cnop;
222 struct crypt_n_kop *knop;
223 struct crypt_sgop *sgop;
224 struct crypt_sfop *sfop;
225 struct cryptret *crypt_ret;
226 struct crypt_result *crypt_res;
227 u_int32_t ses;
228 u_int32_t *sesid;
229 int error = 0;
230 size_t count;
231
232 /* backwards compatibility */
233 file_t *criofp;
234 struct fcrypt *criofcr;
235 int criofd;
236
237 switch (cmd) {
238 case CRIOGET: /* XXX deprecated, remove after 5.0 */
239 if ((error = fd_allocfile(&criofp, &criofd)) != 0)
240 return error;
241 criofcr = pool_get(&fcrpl, PR_WAITOK);
242 if (criofcr == NULL) {
243 fd_abort(curproc, criofp, criofd);
244 return ENOMEM;
245 }
246 mutex_spin_enter(&crypto_mtx);
247 TAILQ_INIT(&criofcr->csessions);
248 TAILQ_INIT(&criofcr->crp_ret_mq);
249 TAILQ_INIT(&criofcr->crp_ret_mkq);
250 selinit(&criofcr->sinfo);
251
252 /*
253 * Don't ever return session 0, to allow detection of
254 * failed creation attempts with multi-create ioctl.
255 */
256 criofcr->sesn = 1;
257 criofcr->requestid = 1;
258 mutex_spin_exit(&crypto_mtx);
259 (void)fd_clone(criofp, criofd, (FREAD|FWRITE),
260 &cryptofops, criofcr);
261 *(u_int32_t *)data = criofd;
262 return error;
263 break;
264 case CIOCGSESSION:
265 sop = (struct session_op *)data;
266 error = cryptodev_session(fcr, sop);
267 break;
268 case CIOCNGSESSION:
269 sgop = (struct crypt_sgop *)data;
270 snop = kmem_alloc((sgop->count *
271 sizeof(struct session_n_op)), KM_SLEEP);
272 if (!snop) {
273 error = EINVAL;
274 goto mbail;
275 }
276 error = copyin(sgop->sessions, snop, sgop->count *
277 sizeof(struct session_n_op));
278 if (error) {
279 goto mbail;
280 }
281
282 error = cryptodev_msession(fcr, snop, sgop->count);
283 if (error) {
284 goto mbail;
285 }
286
287 error = copyout(snop, sgop->sessions, sgop->count *
288 sizeof(struct session_n_op));
289 mbail:
290 if(snop) {
291 kmem_free(snop, sgop->count *
292 sizeof(struct session_n_op));
293 }
294 break;
295 case CIOCFSESSION:
296 mutex_spin_enter(&crypto_mtx);
297 ses = *(u_int32_t *)data;
298 cse = csefind(fcr, ses);
299 if (cse == NULL)
300 return (EINVAL);
301 csedelete(fcr, cse);
302 error = csefree(cse);
303 mutex_spin_exit(&crypto_mtx);
304 break;
305 case CIOCNFSESSION:
306 sfop = (struct crypt_sfop *)data;
307 sesid = kmem_alloc((sfop->count * sizeof(u_int32_t)),
308 KM_SLEEP);
309 if (!sesid)
310 return (EINVAL);
311 error = copyin(sfop->sesid, sesid,
312 (sfop->count * sizeof(u_int32_t)));
313 if (!error) {
314 error = cryptodev_msessionfin(fcr, sfop->count, sesid);
315 }
316 kmem_free(sesid, (sfop->count * sizeof(u_int32_t)));
317 break;
318 case CIOCCRYPT:
319 mutex_spin_enter(&crypto_mtx);
320 cop = (struct crypt_op *)data;
321 cse = csefind(fcr, cop->ses);
322 mutex_spin_exit(&crypto_mtx);
323 if (cse == NULL) {
324 DPRINTF(("csefind failed\n"));
325 return (EINVAL);
326 }
327 error = cryptodev_op(cse, cop, curlwp);
328 DPRINTF(("cryptodev_op error = %d\n", error));
329 break;
330 case CIOCNCRYPTM:
331 mop = (struct crypt_mop *)data;
332 cnop = kmem_alloc((mop->count * sizeof(struct crypt_n_op)),
333 KM_SLEEP);
334 if(!cnop) {
335 return(EINVAL);
336 }
337 error = copyin(mop->reqs, cnop,
338 (mop->count * sizeof(struct crypt_n_op)));
339 if(!error) {
340 error = cryptodev_mop(fcr, cnop, mop->count,
341 curlwp);
342 if (!error) {
343 error = copyout(cnop, mop->reqs,
344 (mop->count *
345 sizeof(struct crypt_n_op)));
346 }
347 }
348 kmem_free(cnop, (mop->count * sizeof(struct crypt_n_op)));
349 break;
350 case CIOCKEY:
351 error = cryptodev_key((struct crypt_kop *)data);
352 DPRINTF(("cryptodev_key error = %d\n", error));
353 break;
354 case CIOCNFKEYM:
355 mkop = (struct crypt_mkop *)data;
356 knop = kmem_alloc((mkop->count * sizeof(struct crypt_n_kop)),
357 KM_SLEEP);
358 if (!knop) {
359 return(EINVAL);
360 }
361 error = copyin(mkop->reqs, knop,
362 (mkop->count * sizeof(struct crypt_n_kop)));
363 if (!error) {
364 error = cryptodev_mkey(fcr, knop, mkop->count);
365 if (!error)
366 error = copyout(knop, mkop->reqs,
367 (mkop->count *
368 sizeof(struct crypt_n_kop)));
369 }
370 kmem_free(knop, (mkop->count * sizeof(struct crypt_n_kop)));
371 break;
372 case CIOCASYMFEAT:
373 error = crypto_getfeat((int *)data);
374 break;
375 case CIOCNCRYPTRETM:
376 crypt_ret = (struct cryptret *)data;
377 count = crypt_ret->count;
378 crypt_res = kmem_alloc((count *
379 sizeof(struct crypt_result)),
380 KM_SLEEP);
381 if(!crypt_res)
382 return(EINVAL);
383 error = copyin(crypt_ret->results, crypt_res,
384 (count * sizeof(struct crypt_result)));
385 if (error)
386 goto reterr;
387 crypt_ret->count = cryptodev_getmstatus(fcr, crypt_res,
388 crypt_ret->count);
389 /* sanity check count */
390 if (crypt_ret->count > count) {
391 printf("%s.%d: error returned count %zd > original "
392 " count %zd\n",
393 __FILE__, __LINE__, crypt_ret->count, count);
394 crypt_ret->count = count;
395
396 }
397 error = copyout(crypt_res, crypt_ret->results,
398 (crypt_ret->count * sizeof(struct crypt_result)));
399 reterr:
400 kmem_free(crypt_res,
401 (count * sizeof(struct crypt_result)));
402 break;
403 case CIOCNCRYPTRET:
404 error = cryptodev_getstatus(fcr, (struct crypt_result *)data);
405 break;
406 default:
407 DPRINTF(("invalid ioctl cmd %ld\n", cmd));
408 error = EINVAL;
409 }
410 return (error);
411 }
412
413 static int
414 cryptodev_op(struct csession *cse, struct crypt_op *cop, struct lwp *l)
415 {
416 struct cryptop *crp = NULL;
417 struct cryptodesc *crde = NULL, *crda = NULL;
418 int error;
419
420 if (cop->len > 256*1024-4)
421 return (E2BIG);
422
423 if (cse->txform) {
424 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
425 return (EINVAL);
426 }
427
428 bzero(&cse->uio, sizeof(cse->uio));
429 cse->uio.uio_iovcnt = 1;
430 cse->uio.uio_resid = 0;
431 cse->uio.uio_rw = UIO_WRITE;
432 cse->uio.uio_iov = cse->iovec;
433 UIO_SETUP_SYSSPACE(&cse->uio);
434 memset(&cse->iovec, 0, sizeof(cse->iovec));
435 cse->uio.uio_iov[0].iov_len = cop->len;
436 cse->uio.uio_iov[0].iov_base = kmem_alloc(cop->len, KM_SLEEP);
437 cse->uio.uio_resid = cse->uio.uio_iov[0].iov_len;
438
439 crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
440 if (crp == NULL) {
441 error = ENOMEM;
442 goto bail;
443 }
444
445 if (cse->thash) {
446 crda = crp->crp_desc;
447 if (cse->txform)
448 crde = crda->crd_next;
449 } else {
450 if (cse->txform)
451 crde = crp->crp_desc;
452 else {
453 error = EINVAL;
454 goto bail;
455 }
456 }
457
458 if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
459 {
460 printf("copyin failed %s %d \n", (char *)cop->src, error);
461 goto bail;
462 }
463
464 if (crda) {
465 crda->crd_skip = 0;
466 crda->crd_len = cop->len;
467 crda->crd_inject = 0; /* ??? */
468
469 crda->crd_alg = cse->mac;
470 crda->crd_key = cse->mackey;
471 crda->crd_klen = cse->mackeylen * 8;
472 }
473
474 if (crde) {
475 if (cop->op == COP_ENCRYPT)
476 crde->crd_flags |= CRD_F_ENCRYPT;
477 else
478 crde->crd_flags &= ~CRD_F_ENCRYPT;
479 crde->crd_len = cop->len;
480 crde->crd_inject = 0;
481
482 crde->crd_alg = cse->cipher;
483 crde->crd_key = cse->key;
484 crde->crd_klen = cse->keylen * 8;
485 }
486
487 crp->crp_ilen = cop->len;
488 crp->crp_flags = CRYPTO_F_IOV | (cop->flags & COP_F_BATCH);
489 crp->crp_buf = (void *)&cse->uio;
490 crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
491 crp->crp_sid = cse->sid;
492 crp->crp_opaque = (void *)cse;
493
494 if (cop->iv) {
495 if (crde == NULL) {
496 error = EINVAL;
497 goto bail;
498 }
499 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
500 error = EINVAL;
501 goto bail;
502 }
503 if ((error = copyin(cop->iv, cse->tmp_iv,
504 cse->txform->blocksize)))
505 goto bail;
506 bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
507 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
508 crde->crd_skip = 0;
509 } else if (crde) {
510 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
511 crde->crd_skip = 0;
512 } else {
513 crde->crd_flags |= CRD_F_IV_PRESENT;
514 crde->crd_skip = cse->txform->blocksize;
515 crde->crd_len -= cse->txform->blocksize;
516 }
517 }
518
519 if (cop->mac) {
520 if (crda == NULL) {
521 error = EINVAL;
522 goto bail;
523 }
524 crp->crp_mac=cse->tmp_mac;
525 }
526
527 /*
528 * XXX there was a comment here which said that we went to
529 * XXX splcrypto() but needed to only if CRYPTO_F_CBIMM,
530 * XXX disabled on NetBSD since 1.6O due to a race condition.
531 * XXX But crypto_dispatch went to splcrypto() itself! (And
532 * XXX now takes the crypto_mtx mutex itself). We do, however,
533 * XXX need to hold the mutex across the call to cv_wait().
534 * XXX (should we arrange for crypto_dispatch to return to
535 * XXX us with it held? it seems quite ugly to do so.)
536 */
537 #ifdef notyet
538 eagain:
539 #endif
540 error = crypto_dispatch(crp);
541 mutex_spin_enter(&crypto_mtx);
542
543 switch (error) {
544 #ifdef notyet /* don't loop forever -- but EAGAIN not possible here yet */
545 case EAGAIN:
546 mutex_spin_exit(&crypto_mtx);
547 goto eagain;
548 break;
549 #endif
550 case 0:
551 break;
552 default:
553 DPRINTF(("cryptodev_op: not waiting, error.\n"));
554 mutex_spin_exit(&crypto_mtx);
555 goto bail;
556 }
557
558 while (!(crp->crp_flags & CRYPTO_F_DONE)) {
559 DPRINTF(("cryptodev_op: sleeping on cv %08x for crp %08x\n", \
560 (uint32_t)&crp->crp_cv, (uint32_t)crp));
561 cv_wait(&crp->crp_cv, &crypto_mtx); /* XXX cv_wait_sig? */
562 }
563 if (crp->crp_flags & CRYPTO_F_ONRETQ) {
564 DPRINTF(("cryptodev_op: DONE, not woken by cryptoret.\n"));
565 (void)crypto_ret_q_remove(crp);
566 }
567 mutex_spin_exit(&crypto_mtx);
568
569 if (crp->crp_etype != 0) {
570 DPRINTF(("cryptodev_op: crp_etype %d\n", crp->crp_etype));
571 error = crp->crp_etype;
572 goto bail;
573 }
574
575 if (cse->error) {
576 DPRINTF(("cryptodev_op: cse->error %d\n", cse->error));
577 error = cse->error;
578 goto bail;
579 }
580
581 if (cop->dst &&
582 (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
583 {
584 DPRINTF(("cryptodev_op: copyout error %d\n", error));
585 goto bail;
586 }
587
588 if (cop->mac &&
589 (error = copyout(crp->crp_mac, cop->mac, cse->thash->authsize))) {
590 DPRINTF(("cryptodev_op: mac copyout error %d\n", error));
591 goto bail;
592 }
593
594 bail:
595 if (crp)
596 crypto_freereq(crp);
597 if (cse->uio.uio_iov[0].iov_base)
598 kmem_free(cse->uio.uio_iov[0].iov_base,
599 cse->uio.uio_iov[0].iov_len);
600
601 return (error);
602 }
603
604 static int
605 cryptodev_cb(void *op)
606 {
607 struct cryptop *crp = (struct cryptop *) op;
608 struct csession *cse = (struct csession *)crp->crp_opaque;
609 int error = 0;
610
611 mutex_spin_enter(&crypto_mtx);
612 cse->error = crp->crp_etype;
613 if (crp->crp_etype == EAGAIN) {
614 /* always drop mutex to call dispatch routine */
615 mutex_spin_exit(&crypto_mtx);
616 error = crypto_dispatch(crp);
617 mutex_spin_enter(&crypto_mtx);
618 }
619 if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
620 cv_signal(&crp->crp_cv);
621 }
622 mutex_spin_exit(&crypto_mtx);
623 return (0);
624 }
625
626 static int
627 cryptodev_mcb(void *op)
628 {
629 struct cryptop *crp = (struct cryptop *) op;
630 struct csession *cse = (struct csession *)crp->crp_opaque;
631 int error=0;
632
633 mutex_spin_enter(&crypto_mtx);
634 cse->error = crp->crp_etype;
635 if (crp->crp_etype == EAGAIN) {
636 mutex_spin_exit(&crypto_mtx);
637 error = crypto_dispatch(crp);
638 mutex_spin_enter(&crypto_mtx);
639 }
640 if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
641 cv_signal(&crp->crp_cv);
642 }
643
644 TAILQ_INSERT_TAIL(&crp->fcrp->crp_ret_mq, crp, crp_next);
645 selnotify(&crp->fcrp->sinfo, 0, 0);
646 mutex_spin_exit(&crypto_mtx);
647 return (0);
648 }
649
650 static int
651 cryptodevkey_cb(void *op)
652 {
653 struct cryptkop *krp = (struct cryptkop *) op;
654
655 mutex_spin_enter(&crypto_mtx);
656 cv_signal(&krp->krp_cv);
657 mutex_spin_exit(&crypto_mtx);
658 return (0);
659 }
660
661 static int
662 cryptodevkey_mcb(void *op)
663 {
664 struct cryptkop *krp = (struct cryptkop *) op;
665
666 mutex_spin_enter(&crypto_mtx);
667 cv_signal(&krp->krp_cv);
668 TAILQ_INSERT_TAIL(&krp->fcrp->crp_ret_mkq, krp, krp_next);
669 selnotify(&krp->fcrp->sinfo, 0, 0);
670 mutex_spin_exit(&crypto_mtx);
671 return (0);
672 }
673
674 static int
675 cryptodev_key(struct crypt_kop *kop)
676 {
677 struct cryptkop *krp = NULL;
678 int error = EINVAL;
679 int in, out, size, i;
680
681 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
682 return (EFBIG);
683 }
684
685 in = kop->crk_iparams;
686 out = kop->crk_oparams;
687 switch (kop->crk_op) {
688 case CRK_MOD_EXP:
689 if (in == 3 && out == 1)
690 break;
691 return (EINVAL);
692 case CRK_MOD_EXP_CRT:
693 if (in == 6 && out == 1)
694 break;
695 return (EINVAL);
696 case CRK_DSA_SIGN:
697 if (in == 5 && out == 2)
698 break;
699 return (EINVAL);
700 case CRK_DSA_VERIFY:
701 if (in == 7 && out == 0)
702 break;
703 return (EINVAL);
704 case CRK_DH_COMPUTE_KEY:
705 if (in == 3 && out == 1)
706 break;
707 return (EINVAL);
708 case CRK_MOD_ADD:
709 if (in == 3 && out == 1)
710 break;
711 return (EINVAL);
712 case CRK_MOD_ADDINV:
713 if (in == 2 && out == 1)
714 break;
715 return (EINVAL);
716 case CRK_MOD_SUB:
717 if (in == 3 && out == 1)
718 break;
719 return (EINVAL);
720 case CRK_MOD_MULT:
721 if (in == 3 && out == 1)
722 break;
723 return (EINVAL);
724 case CRK_MOD_MULTINV:
725 if (in == 2 && out == 1)
726 break;
727 return (EINVAL);
728 case CRK_MOD:
729 if (in == 2 && out == 1)
730 break;
731 return (EINVAL);
732 default:
733 return (EINVAL);
734 }
735
736 krp = pool_get(&cryptkop_pool, PR_WAITOK);
737 if (!krp)
738 return (ENOMEM);
739 bzero(krp, sizeof *krp);
740 cv_init(&krp->krp_cv, "crykdev");
741 krp->krp_op = kop->crk_op;
742 krp->krp_status = kop->crk_status;
743 krp->krp_iparams = kop->crk_iparams;
744 krp->krp_oparams = kop->crk_oparams;
745 krp->krp_status = 0;
746 krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
747
748 for (i = 0; i < CRK_MAXPARAM; i++)
749 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
750 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
751 size = (krp->krp_param[i].crp_nbits + 7) / 8;
752 if (size == 0)
753 continue;
754 krp->krp_param[i].crp_p = kmem_alloc(size, KM_SLEEP);
755 if (i >= krp->krp_iparams)
756 continue;
757 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
758 if (error)
759 goto fail;
760 }
761
762 error = crypto_kdispatch(krp);
763 if (error != 0) {
764 goto fail;
765 }
766
767 mutex_spin_enter(&crypto_mtx);
768 while (!(krp->krp_flags & CRYPTO_F_DONE)) {
769 cv_wait(&krp->krp_cv, &crypto_mtx); /* XXX cv_wait_sig? */
770 }
771 if (krp->krp_flags & CRYPTO_F_ONRETQ) {
772 DPRINTF(("cryptodev_key: DONE early, not via cryptoret.\n"));
773 (void)crypto_ret_kq_remove(krp);
774 }
775 mutex_spin_exit(&crypto_mtx);
776
777 if (krp->krp_status != 0) {
778 DPRINTF(("cryptodev_key: krp->krp_status 0x%08x\n", krp->krp_status));
779 error = krp->krp_status;
780 goto fail;
781 }
782
783 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
784 size = (krp->krp_param[i].crp_nbits + 7) / 8;
785 if (size == 0)
786 continue;
787 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
788 if (error) {
789 DPRINTF(("cryptodev_key: copyout oparam %d failed, error=%d\n", i-krp->krp_iparams, error));
790 goto fail;
791 }
792 }
793
794 fail:
795 if (krp) {
796 kop->crk_status = krp->krp_status;
797 for (i = 0; i < CRK_MAXPARAM; i++) {
798 struct crparam *kp = &(krp->krp_param[i]);
799 if (krp->krp_param[i].crp_p) {
800 size = (kp->crp_nbits + 7) / 8;
801 KASSERT(size > 0);
802 memset(kp->crp_p, 0, size);
803 kmem_free(kp->crp_p, size);
804 }
805 }
806 pool_put(&cryptkop_pool, krp);
807 }
808 DPRINTF(("cryptodev_key: error=0x%08x\n", error));
809 return (error);
810 }
811
812 /* ARGSUSED */
813 static int
814 cryptof_close(struct file *fp)
815 {
816 struct fcrypt *fcr = fp->f_data;
817 struct csession *cse;
818
819 mutex_spin_enter(&crypto_mtx);
820 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
821 TAILQ_REMOVE(&fcr->csessions, cse, next);
822 (void)csefree(cse);
823 }
824 seldestroy(&fcr->sinfo);
825 fp->f_data = NULL;
826 mutex_spin_exit(&crypto_mtx);
827
828 pool_put(&fcrpl, fcr);
829 return 0;
830 }
831
832 /* csefind: call with crypto_mtx held. */
833 static struct csession *
834 csefind(struct fcrypt *fcr, u_int ses)
835 {
836 struct csession *cse, *ret = NULL;
837
838 KASSERT(mutex_owned(&crypto_mtx));
839 TAILQ_FOREACH(cse, &fcr->csessions, next)
840 if (cse->ses == ses)
841 ret = cse;
842
843 return (ret);
844 }
845
846 /* csedelete: call with crypto_mtx held. */
847 static int
848 csedelete(struct fcrypt *fcr, struct csession *cse_del)
849 {
850 struct csession *cse;
851 int ret = 0;
852
853 KASSERT(mutex_owned(&crypto_mtx));
854 TAILQ_FOREACH(cse, &fcr->csessions, next) {
855 if (cse == cse_del) {
856 TAILQ_REMOVE(&fcr->csessions, cse, next);
857 ret = 1;
858 }
859 }
860 return (ret);
861 }
862
863 /* cseadd: call with crypto_mtx held. */
864 static struct csession *
865 cseadd(struct fcrypt *fcr, struct csession *cse)
866 {
867 KASSERT(mutex_owned(&crypto_mtx));
868 /* don't let session ID wrap! */
869 if (fcr->sesn + 1 == 0) return NULL;
870 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
871 cse->ses = fcr->sesn++;
872 return (cse);
873 }
874
875 /* csecreate: call with crypto_mtx held. */
876 static struct csession *
877 csecreate(struct fcrypt *fcr, u_int64_t sid, void *key, u_int64_t keylen,
878 void *mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
879 struct enc_xform *txform, struct auth_hash *thash)
880 {
881 struct csession *cse;
882
883 KASSERT(mutex_owned(&crypto_mtx));
884 cse = pool_get(&csepl, PR_NOWAIT);
885 if (cse == NULL)
886 return NULL;
887 cse->key = key;
888 cse->keylen = keylen/8;
889 cse->mackey = mackey;
890 cse->mackeylen = mackeylen/8;
891 cse->sid = sid;
892 cse->cipher = cipher;
893 cse->mac = mac;
894 cse->txform = txform;
895 cse->thash = thash;
896 cse->error = 0;
897 if (cseadd(fcr, cse))
898 return (cse);
899 else {
900 pool_put(&csepl, cse);
901 return NULL;
902 }
903 }
904
905 /* csefree: call with crypto_mtx held. */
906 static int
907 csefree(struct csession *cse)
908 {
909 int error;
910
911 KASSERT(mutex_owned(&crypto_mtx));
912 error = crypto_freesession(cse->sid);
913 if (cse->key)
914 free(cse->key, M_XDATA);
915 if (cse->mackey)
916 free(cse->mackey, M_XDATA);
917 pool_put(&csepl, cse);
918 return (error);
919 }
920
921 static int
922 cryptoopen(dev_t dev, int flag, int mode,
923 struct lwp *l)
924 {
925 file_t *fp;
926 struct fcrypt *fcr;
927 int fd, error;
928
929 if (crypto_usercrypto == 0)
930 return (ENXIO);
931
932 if ((error = fd_allocfile(&fp, &fd)) != 0)
933 return error;
934
935 fcr = pool_get(&fcrpl, PR_WAITOK);
936 if (fcr == NULL) {
937 fd_abort(curproc, fp, fd);
938 return ENOMEM;
939 }
940 mutex_spin_enter(&crypto_mtx);
941 TAILQ_INIT(&fcr->csessions);
942 TAILQ_INIT(&fcr->crp_ret_mq);
943 TAILQ_INIT(&fcr->crp_ret_mkq);
944 selinit(&fcr->sinfo);
945 /*
946 * Don't ever return session 0, to allow detection of
947 * failed creation attempts with multi-create ioctl.
948 */
949 fcr->sesn = 1;
950 fcr->requestid = 1;
951 mutex_spin_exit(&crypto_mtx);
952 return fd_clone(fp, fd, flag, &cryptofops, fcr);
953 }
954
955 static int
956 cryptoread(dev_t dev, struct uio *uio, int ioflag)
957 {
958 return (EIO);
959 }
960
961 static int
962 cryptowrite(dev_t dev, struct uio *uio, int ioflag)
963 {
964 return (EIO);
965 }
966
967 int
968 cryptoselect(dev_t dev, int rw, struct lwp *l)
969 {
970 return (0);
971 }
972
973 /*static*/
974 struct cdevsw crypto_cdevsw = {
975 /* open */ cryptoopen,
976 /* close */ noclose,
977 /* read */ cryptoread,
978 /* write */ cryptowrite,
979 /* ioctl */ noioctl,
980 /* ttstop?*/ nostop,
981 /* ??*/ notty,
982 /* poll */ cryptoselect /*nopoll*/,
983 /* mmap */ nommap,
984 /* kqfilter */ nokqfilter,
985 /* type */ D_OTHER,
986 };
987
988 static int
989 cryptodev_mop(struct fcrypt *fcr,
990 struct crypt_n_op * cnop,
991 int count, struct lwp *l)
992 {
993 struct cryptop *crp = NULL;
994 struct cryptodesc *crde = NULL, *crda = NULL;
995 int req, error=0;
996 struct csession *cse;
997
998 for (req = 0; req < count; req++) {
999 mutex_spin_enter(&crypto_mtx);
1000 cse = csefind(fcr, cnop[req].ses);
1001 if (cse == NULL) {
1002 DPRINTF(("csefind failed\n"));
1003 cnop[req].status = EINVAL;
1004 mutex_spin_exit(&crypto_mtx);
1005 continue;
1006 }
1007 mutex_spin_exit(&crypto_mtx);
1008
1009 if (cnop[req].len > 256*1024-4) {
1010 DPRINTF(("length failed\n"));
1011 cnop[req].status = EINVAL;
1012 continue;
1013 }
1014 if (cse->txform) {
1015 if (cnop[req].len == 0 ||
1016 (cnop[req].len % cse->txform->blocksize) != 0) {
1017 cnop[req].status = EINVAL;
1018 continue;
1019 }
1020 }
1021
1022 crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
1023 if (crp == NULL) {
1024 cnop[req].status = ENOMEM;
1025 goto bail;
1026 }
1027
1028 bzero(&crp->uio, sizeof(crp->uio));
1029 crp->uio.uio_iovcnt = 1;
1030 crp->uio.uio_resid = 0;
1031 crp->uio.uio_rw = UIO_WRITE;
1032 crp->uio.uio_iov = crp->iovec;
1033 UIO_SETUP_SYSSPACE(&crp->uio);
1034 memset(&crp->iovec, 0, sizeof(crp->iovec));
1035 crp->uio.uio_iov[0].iov_len = cnop[req].len;
1036 crp->uio.uio_iov[0].iov_base = kmem_alloc(cnop[req].len,
1037 KM_SLEEP);
1038 crp->uio.uio_resid = crp->uio.uio_iov[0].iov_len;
1039
1040 if (cse->thash) {
1041 crda = crp->crp_desc;
1042 if (cse->txform)
1043 crde = crda->crd_next;
1044 } else {
1045 if (cse->txform)
1046 crde = crp->crp_desc;
1047 else {
1048 cnop[req].status = EINVAL;
1049 goto bail;
1050 }
1051 }
1052
1053 if ((copyin(cnop[req].src,
1054 crp->uio.uio_iov[0].iov_base, cnop[req].len))) {
1055 cnop[req].status = EINVAL;
1056 goto bail;
1057 }
1058
1059 if (crda) {
1060 crda->crd_skip = 0;
1061 crda->crd_len = cnop[req].len;
1062 crda->crd_inject = 0; /* ??? */
1063
1064 crda->crd_alg = cse->mac;
1065 crda->crd_key = cse->mackey;
1066 crda->crd_klen = cse->mackeylen * 8;
1067 }
1068
1069 if (crde) {
1070 if (cnop[req].op == COP_ENCRYPT)
1071 crde->crd_flags |= CRD_F_ENCRYPT;
1072 else
1073 crde->crd_flags &= ~CRD_F_ENCRYPT;
1074 crde->crd_len = cnop[req].len;
1075 crde->crd_inject = 0;
1076
1077 crde->crd_alg = cse->cipher;
1078 #ifdef notyet /* XXX must notify h/w driver new key, drain */
1079 if(cnop[req].key && cnop[req].keylen) {
1080 crde->crd_key = malloc(cnop[req].keylen,
1081 M_XDATA, M_WAITOK);
1082 if((error = copyin(cnop[req].key,
1083 crde->crd_key, cnop[req].keylen))) {
1084 cnop[req].status = EINVAL;
1085 goto bail;
1086 }
1087 crde->crd_klen = cnop[req].keylen * 8;
1088 } else { ... }
1089 #endif
1090 crde->crd_key = cse->key;
1091 crde->crd_klen = cse->keylen * 8;
1092 }
1093
1094 crp->crp_ilen = cnop[req].len;
1095 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
1096 | (cnop[req].flags & COP_F_BATCH);
1097 crp->crp_buf = (void *)&crp->uio;
1098 crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_mcb;
1099 crp->crp_sid = cse->sid;
1100 crp->crp_opaque = (void *)cse;
1101 crp->fcrp = fcr;
1102 crp->dst = cnop[req].dst;
1103 /* we can use the crp_ilen in cryptop(crp) for this */
1104 crp->len = cnop[req].len;
1105 crp->mac = cnop[req].mac;
1106
1107 if (cnop[req].iv) {
1108 if (crde == NULL) {
1109 cnop[req].status = EINVAL;
1110 goto bail;
1111 }
1112 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
1113 cnop[req].status = EINVAL;
1114 goto bail;
1115 }
1116 if ((error = copyin(cnop[req].iv, crp->tmp_iv,
1117 cse->txform->blocksize))) {
1118 cnop[req].status = EINVAL;
1119 goto bail;
1120 }
1121 bcopy(crp->tmp_iv, crde->crd_iv, cse->txform->blocksize);
1122 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
1123 crde->crd_skip = 0;
1124 } else if (crde) {
1125 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
1126 crde->crd_skip = 0;
1127 } else {
1128 crde->crd_flags |= CRD_F_IV_PRESENT;
1129 crde->crd_skip = cse->txform->blocksize;
1130 crde->crd_len -= cse->txform->blocksize;
1131 }
1132 }
1133
1134 if (cnop[req].mac) {
1135 if (crda == NULL) {
1136 cnop[req].status = EINVAL;
1137 goto bail;
1138 }
1139 crp->crp_mac=cse->tmp_mac;
1140 }
1141 cnop[req].reqid = atomic_inc_32_nv(&(fcr->requestid));
1142 crp->crp_reqid = cnop[req].reqid;
1143 crp->crp_usropaque = cnop[req].opaque;
1144 #ifdef notyet
1145 eagain:
1146 #endif
1147 cnop[req].status = crypto_dispatch(crp);
1148 mutex_spin_enter(&crypto_mtx); /* XXX why mutex? */
1149
1150 switch (cnop[req].status) {
1151 #ifdef notyet /* don't loop forever -- but EAGAIN not possible here yet */
1152 case EAGAIN:
1153 mutex_spin_exit(&crypto_mtx);
1154 goto eagain;
1155 break;
1156 #endif
1157 case 0:
1158 break;
1159 default:
1160 DPRINTF(("cryptodev_op: not waiting, error.\n"));
1161 mutex_spin_exit(&crypto_mtx);
1162 goto bail;
1163 }
1164
1165 mutex_spin_exit(&crypto_mtx);
1166 bail:
1167 if (cnop[req].status) {
1168 if (crp) {
1169 crypto_freereq(crp);
1170 if(cse->uio.uio_iov[0].iov_base) {
1171 kmem_free(cse->uio.uio_iov[0].iov_base,
1172 cse->uio.uio_iov[0].iov_len);
1173 }
1174 }
1175 error = 0;
1176 }
1177 }
1178 return (error);
1179 }
1180
1181 static int
1182 cryptodev_mkey(struct fcrypt *fcr, struct crypt_n_kop *kop, int count)
1183 {
1184 struct cryptkop *krp = NULL;
1185 int error = EINVAL;
1186 int in, out, size, i, req;
1187
1188 for (req = 0; req < count; req++) {
1189 if (kop[req].crk_iparams + kop[req].crk_oparams > CRK_MAXPARAM) {
1190 return (EFBIG);
1191 }
1192
1193 in = kop[req].crk_iparams;
1194 out = kop[req].crk_oparams;
1195 switch (kop[req].crk_op) {
1196 case CRK_MOD_EXP:
1197 if (in == 3 && out == 1)
1198 break;
1199 kop[req].crk_status = EINVAL;
1200 continue;
1201 case CRK_MOD_EXP_CRT:
1202 if (in == 6 && out == 1)
1203 break;
1204 kop[req].crk_status = EINVAL;
1205 continue;
1206 case CRK_DSA_SIGN:
1207 if (in == 5 && out == 2)
1208 break;
1209 kop[req].crk_status = EINVAL;
1210 continue;
1211 case CRK_DSA_VERIFY:
1212 if (in == 7 && out == 0)
1213 break;
1214 kop[req].crk_status = EINVAL;
1215 continue;
1216 case CRK_DH_COMPUTE_KEY:
1217 if (in == 3 && out == 1)
1218 break;
1219 kop[req].crk_status = EINVAL;
1220 continue;
1221 case CRK_MOD_ADD:
1222 if (in == 3 && out == 1)
1223 break;
1224 kop[req].crk_status = EINVAL;
1225 continue;
1226 case CRK_MOD_ADDINV:
1227 if (in == 2 && out == 1)
1228 break;
1229 kop[req].crk_status = EINVAL;
1230 continue;
1231 case CRK_MOD_SUB:
1232 if (in == 3 && out == 1)
1233 break;
1234 kop[req].crk_status = EINVAL;
1235 continue;
1236 case CRK_MOD_MULT:
1237 if (in == 3 && out == 1)
1238 break;
1239 kop[req].crk_status = EINVAL;
1240 continue;
1241 case CRK_MOD_MULTINV:
1242 if (in == 2 && out == 1)
1243 break;
1244 kop[req].crk_status = EINVAL;
1245 continue;
1246 case CRK_MOD:
1247 if (in == 2 && out == 1)
1248 break;
1249 kop[req].crk_status = EINVAL;
1250 continue;
1251 default:
1252 kop[req].crk_status = EINVAL;
1253 continue;
1254 }
1255
1256 krp = pool_get(&cryptkop_pool, PR_WAITOK);
1257 if (!krp) {
1258 kop[req].crk_status = ENOMEM;
1259 goto fail;
1260 }
1261 bzero(krp, sizeof *krp);
1262 cv_init(&krp->krp_cv, "crykdev");
1263 krp->krp_op = kop[req].crk_op;
1264 krp->krp_status = kop[req].crk_status;
1265 krp->krp_iparams = kop[req].crk_iparams;
1266 krp->krp_oparams = kop[req].crk_oparams;
1267 krp->krp_status = 0;
1268 krp->krp_callback =
1269 (int (*) (struct cryptkop *)) cryptodevkey_mcb;
1270 bcopy(kop[req].crk_param,
1271 krp->crk_param,
1272 sizeof(kop[req].crk_param));
1273
1274 krp->krp_flags = CRYPTO_F_CBIMM;
1275
1276 for (i = 0; i < CRK_MAXPARAM; i++)
1277 krp->krp_param[i].crp_nbits =
1278 kop[req].crk_param[i].crp_nbits;
1279 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
1280 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1281 if (size == 0)
1282 continue;
1283 krp->krp_param[i].crp_p =
1284 kmem_alloc(size, KM_SLEEP);
1285 if (i >= krp->krp_iparams)
1286 continue;
1287 kop[req].crk_status = copyin(kop[req].crk_param[i].crp_p,
1288 krp->krp_param[i].crp_p, size);
1289 if (kop[req].crk_status)
1290 goto fail;
1291 }
1292 krp->fcrp = fcr;
1293
1294 kop[req].crk_reqid = atomic_inc_32_nv(&(fcr->requestid));
1295 krp->krp_reqid = kop[req].crk_reqid;
1296 krp->krp_usropaque = kop[req].crk_opaque;
1297
1298 kop[req].crk_status = crypto_kdispatch(krp);
1299 if (kop[req].crk_status != 0) {
1300 goto fail;
1301 }
1302
1303 fail:
1304 if(kop[req].crk_status) {
1305 if (krp) {
1306 kop[req].crk_status = krp->krp_status;
1307 for (i = 0; i < CRK_MAXPARAM; i++) {
1308 struct crparam *kp =
1309 &(krp->krp_param[i]);
1310 if (kp->crp_p) {
1311 size = (kp->crp_nbits + 7) / 8;
1312 KASSERT(size > 0);
1313 memset(kp->crp_p, 0, size);
1314 kmem_free(kp->crp_p, size);
1315 }
1316 }
1317 pool_put(&cryptkop_pool, krp);
1318 }
1319 }
1320 error = 0;
1321 }
1322 DPRINTF(("cryptodev_key: error=0x%08x\n", error));
1323 return (error);
1324 }
1325
1326 static int
1327 cryptodev_session(struct fcrypt *fcr, struct session_op *sop) {
1328 struct cryptoini cria, crie;
1329 struct enc_xform *txform = NULL;
1330 struct auth_hash *thash = NULL;
1331 struct csession *cse;
1332 u_int64_t sid;
1333 int error = 0;
1334
1335 /* XXX there must be a way to not embed the list of xforms here */
1336 switch (sop->cipher) {
1337 case 0:
1338 break;
1339 case CRYPTO_DES_CBC:
1340 txform = &enc_xform_des;
1341 break;
1342 case CRYPTO_3DES_CBC:
1343 txform = &enc_xform_3des;
1344 break;
1345 case CRYPTO_BLF_CBC:
1346 txform = &enc_xform_blf;
1347 break;
1348 case CRYPTO_CAST_CBC:
1349 txform = &enc_xform_cast5;
1350 case CRYPTO_SKIPJACK_CBC:
1351 txform = &enc_xform_skipjack;
1352 break;
1353 case CRYPTO_AES_CBC:
1354 txform = &enc_xform_rijndael128;
1355 break;
1356 case CRYPTO_NULL_CBC:
1357 txform = &enc_xform_null;
1358 break;
1359 case CRYPTO_ARC4:
1360 txform = &enc_xform_arc4;
1361 break;
1362 default:
1363 DPRINTF(("Invalid cipher %d\n", sop->cipher));
1364 return EINVAL;
1365 }
1366
1367 switch (sop->mac) {
1368 case 0:
1369 break;
1370 case CRYPTO_MD5_HMAC:
1371 thash = &auth_hash_hmac_md5;
1372 break;
1373 case CRYPTO_SHA1_HMAC:
1374 thash = &auth_hash_hmac_sha1;
1375 break;
1376 case CRYPTO_MD5_HMAC_96:
1377 thash = &auth_hash_hmac_md5_96;
1378 break;
1379 case CRYPTO_SHA1_HMAC_96:
1380 thash = &auth_hash_hmac_sha1_96;
1381 break;
1382 case CRYPTO_SHA2_HMAC:
1383 /* XXX switching on key length seems questionable */
1384 if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize) {
1385 thash = &auth_hash_hmac_sha2_256;
1386 } else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize) {
1387 thash = &auth_hash_hmac_sha2_384;
1388 } else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize) {
1389 thash = &auth_hash_hmac_sha2_512;
1390 } else {
1391 DPRINTF(("Invalid mackeylen %d\n", sop->mackeylen));
1392 return EINVAL;
1393 }
1394 break;
1395 case CRYPTO_RIPEMD160_HMAC:
1396 thash = &auth_hash_hmac_ripemd_160;
1397 break;
1398 case CRYPTO_RIPEMD160_HMAC_96:
1399 thash = &auth_hash_hmac_ripemd_160_96;
1400 break;
1401 case CRYPTO_MD5:
1402 thash = &auth_hash_md5;
1403 break;
1404 case CRYPTO_SHA1:
1405 thash = &auth_hash_sha1;
1406 break;
1407 case CRYPTO_NULL_HMAC:
1408 thash = &auth_hash_null;
1409 break;
1410 default:
1411 DPRINTF(("Invalid mac %d\n", sop->mac));
1412 return (EINVAL);
1413 }
1414
1415 memset(&crie, 0, sizeof(crie));
1416 memset(&cria, 0, sizeof(cria));
1417
1418 if (txform) {
1419 crie.cri_alg = txform->type;
1420 crie.cri_klen = sop->keylen * 8;
1421 if (sop->keylen > txform->maxkey ||
1422 sop->keylen < txform->minkey) {
1423 DPRINTF(("keylen %d not in [%d,%d]\n",
1424 sop->keylen, txform->minkey,
1425 txform->maxkey));
1426 error = EINVAL ;
1427 goto bail;
1428 }
1429
1430 crie.cri_key = malloc(crie.cri_klen / 8, M_XDATA, M_WAITOK);
1431 if ((error = copyin(sop->key, crie.cri_key,
1432 crie.cri_klen / 8))) {
1433 goto bail;
1434 }
1435 if (thash) {
1436 crie.cri_next = &cria; /* XXX forces enc then hash? */
1437 }
1438 }
1439
1440 if (thash) {
1441 cria.cri_alg = thash->type;
1442 cria.cri_klen = sop->mackeylen * 8;
1443 if (sop->mackeylen != thash->keysize) {
1444 DPRINTF(("mackeylen %d != keysize %d\n",
1445 sop->mackeylen, thash->keysize));
1446 error = EINVAL;
1447 goto bail;
1448 }
1449 if (cria.cri_klen) {
1450 cria.cri_key = malloc(cria.cri_klen / 8, M_XDATA,
1451 M_WAITOK);
1452 if ((error = copyin(sop->mackey, cria.cri_key,
1453 cria.cri_klen / 8))) {
1454 goto bail;
1455 }
1456 }
1457 }
1458 /* crypto_newsession requires that we hold the mutex. */
1459 mutex_spin_enter(&crypto_mtx);
1460 error = crypto_newsession(&sid, (txform ? &crie : &cria),
1461 crypto_devallowsoft);
1462 if (!error) {
1463 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
1464 cria.cri_key, cria.cri_klen, sop->cipher,
1465 sop->mac, txform, thash);
1466 if (cse != NULL) {
1467 sop->ses = cse->ses;
1468 } else {
1469 DPRINTF(("csecreate failed\n"));
1470 crypto_freesession(sid);
1471 error = EINVAL;
1472 }
1473 } else {
1474 DPRINTF(("SIOCSESSION violates kernel parameters %d\n",
1475 error));
1476 }
1477 mutex_spin_exit(&crypto_mtx);
1478 bail:
1479 if (error) {
1480 if (crie.cri_key) {
1481 memset(crie.cri_key, 0, crie.cri_klen / 8);
1482 free(crie.cri_key, M_XDATA);
1483 }
1484 if (cria.cri_key) {
1485 memset(cria.cri_key, 0, cria.cri_klen / 8);
1486 free(cria.cri_key, M_XDATA);
1487 }
1488 }
1489 return error;
1490 }
1491
1492 static int
1493 cryptodev_msession(struct fcrypt *fcr, struct session_n_op *sn_ops,
1494 int count)
1495 {
1496 int i;
1497
1498 for (i = 0; i < count; i++, sn_ops++) {
1499 struct session_op s_op;
1500 s_op.cipher = sn_ops->cipher;
1501 s_op.mac = sn_ops->mac;
1502 s_op.keylen = sn_ops->keylen;
1503 s_op.key = sn_ops->key;
1504 s_op.mackeylen = sn_ops->mackeylen;
1505 s_op.mackey = sn_ops->mackey;
1506
1507 sn_ops->status = cryptodev_session(fcr, &s_op);
1508 sn_ops->ses = s_op.ses;
1509 }
1510
1511 return 0;
1512 }
1513
1514 static int
1515 cryptodev_msessionfin(struct fcrypt *fcr, int count, u_int32_t *sesid)
1516 {
1517 struct csession *cse;
1518 int req, error = 0;
1519
1520 mutex_spin_enter(&crypto_mtx);
1521 for(req = 0; req < count; req++) {
1522 cse = csefind(fcr, sesid[req]);
1523 if (cse == NULL)
1524 continue;
1525 csedelete(fcr, cse);
1526 error = csefree(cse);
1527 }
1528 mutex_spin_exit(&crypto_mtx);
1529 return 0;
1530 }
1531
1532 /*
1533 * collect as many completed requests as are availble, or count completed requests
1534 * whichever is less.
1535 * return the number of requests.
1536 */
1537 static int
1538 cryptodev_getmstatus(struct fcrypt *fcr, struct crypt_result *crypt_res,
1539 int count)
1540 {
1541 struct cryptop *crp = NULL;
1542 struct cryptkop *krp = NULL;
1543 struct csession *cse;
1544 int i, size, req = 0;
1545 int completed=0;
1546
1547 /* On stack so nobody else can grab them -- no locking */
1548 SLIST_HEAD(, cryptop) crp_delfree_l =
1549 SLIST_HEAD_INITIALIZER(crp_delfree_l);
1550 SLIST_HEAD(, cryptkop) krp_delfree_l =
1551 SLIST_HEAD_INITIALIZER(krp_delfree_l);
1552
1553 mutex_spin_enter(&crypto_mtx);
1554
1555 /* at this point we do not know which response user is requesting for
1556 * (symmetric or asymmetric) so we copyout one from each i.e if the
1557 * count is 2 then 1 from symmetric and 1 from asymmetric queue and
1558 * if 3 then 2 symmetric and 1 asymmetric and so on */
1559 for(; req < count ;) {
1560 crp = TAILQ_FIRST(&fcr->crp_ret_mq);
1561 if(crp) {
1562 cse = (struct csession *)crp->crp_opaque;
1563 crypt_res[req].reqid = crp->crp_reqid;
1564 crypt_res[req].opaque = crp->crp_usropaque;
1565 completed++;
1566 cse = csefind(fcr, cse->ses);
1567 if (cse == NULL) {
1568 DPRINTF(("csefind failed\n"));
1569 crypt_res[req].status = EINVAL;
1570 goto bail;
1571 }
1572
1573 if (crp->crp_etype != 0) {
1574 crypt_res[req].status = crp->crp_etype;
1575 goto bail;
1576 }
1577
1578 if (cse->error) {
1579 crypt_res[req].status = cse->error;
1580 goto bail;
1581 }
1582
1583 if (crp->dst &&
1584 (crypt_res[req].status = copyout
1585 (crp->uio.uio_iov[0].iov_base,
1586 crp->dst, crp->len)))
1587 goto bail;
1588
1589 if (crp->mac &&
1590 (crypt_res[req].status = copyout
1591 (crp->crp_mac, crp->mac,
1592 cse->thash->authsize)))
1593 goto bail;
1594 bail:
1595 TAILQ_REMOVE(&fcr->crp_ret_mq, crp, crp_next);
1596 SLIST_INSERT_HEAD(&crp_delfree_l, crp,
1597 crp_qun.crp_lnext);
1598 req++;
1599 }
1600
1601 if(req < count) {
1602 krp = TAILQ_FIRST(&fcr->crp_ret_mkq);
1603 if (krp) {
1604 crypt_res[req].reqid = krp->krp_reqid;
1605 crypt_res[req].opaque = krp->krp_usropaque;
1606 completed++;
1607 if (krp->krp_status != 0) {
1608 DPRINTF(("cryptodev_key: "
1609 "krp->krp_status"
1610 "0x%08x\n", krp->krp_status));
1611 crypt_res[req].status =
1612 krp->krp_status;
1613 goto fail;
1614 }
1615
1616 for (i = krp->krp_iparams; i < krp->krp_iparams
1617 + krp->krp_oparams; i++) {
1618 size = (krp->krp_param[i].crp_nbits
1619 + 7) / 8;
1620 if (size == 0)
1621 continue;
1622 crypt_res[req].status = copyout
1623 (krp->krp_param[i].crp_p,
1624 krp->crk_param[i].crp_p, size);
1625 if (crypt_res[req].status) {
1626 DPRINTF(("cryptodev_key: "
1627 "copyout oparam "
1628 "%d failed, "
1629 "error=%d\n",
1630 i-krp->krp_iparams,
1631 crypt_res[req].status));
1632 goto fail;
1633 }
1634 }
1635 fail:
1636 TAILQ_REMOVE(&fcr->crp_ret_mkq, krp, krp_next);
1637 /* not sure what to do for this */
1638 /* kop[req].crk_status = krp->krp_status; */
1639 SLIST_INSERT_HEAD(&krp_delfree_l, krp,
1640 krp_qun.krp_lnext);
1641 }
1642 req++;
1643 }
1644 }
1645 mutex_spin_exit(&crypto_mtx);
1646
1647 while(!SLIST_EMPTY(&crp_delfree_l)) {
1648 crp = SLIST_FIRST(&crp_delfree_l);
1649 SLIST_REMOVE_HEAD(&crp_delfree_l, crp_qun.crp_lnext);
1650 kmem_free(crp->uio.uio_iov[0].iov_base,
1651 crp->uio.uio_iov[0].iov_len);
1652 crypto_freereq(crp);
1653 }
1654
1655 while(!SLIST_EMPTY(&krp_delfree_l)) {
1656 krp = SLIST_FIRST(&krp_delfree_l);
1657 for (i = 0; i < CRK_MAXPARAM; i++) {
1658 struct crparam *kp = &(krp->krp_param[i]);
1659 if (kp->crp_p) {
1660 size = (kp->crp_nbits + 7) / 8;
1661 KASSERT(size > 0);
1662 memset(kp->crp_p, 0, size);
1663 kmem_free(kp->crp_p, size);
1664 }
1665 }
1666 SLIST_REMOVE_HEAD(&krp_delfree_l, krp_qun.krp_lnext);
1667 pool_put(&cryptkop_pool, krp);
1668 }
1669 return completed;
1670 }
1671
1672 static int
1673 cryptodev_getstatus (struct fcrypt *fcr, struct crypt_result *crypt_res)
1674 {
1675 struct cryptop *crp = NULL;
1676 struct cryptkop *krp = NULL;
1677 struct csession *cse;
1678 int i, size, req = 0;
1679
1680 mutex_spin_enter(&crypto_mtx);
1681 /* Here we dont know for which request the user is requesting the
1682 * response so checking in both the queues */
1683 TAILQ_FOREACH(crp, &fcr->crp_ret_mq, crp_next) {
1684 if(crp && (crp->crp_reqid == crypt_res->reqid)) {
1685 cse = (struct csession *)crp->crp_opaque;
1686 crypt_res->opaque = crp->crp_usropaque;
1687 cse = csefind(fcr, cse->ses);
1688 if (cse == NULL) {
1689 DPRINTF(("csefind failed\n"));
1690 crypt_res->status = EINVAL;
1691 goto bail;
1692 }
1693
1694 if (crp->crp_etype != 0) {
1695 crypt_res->status = crp->crp_etype;
1696 goto bail;
1697 }
1698
1699 if (cse->error) {
1700 crypt_res->status = cse->error;
1701 goto bail;
1702 }
1703
1704 if (crp->dst &&
1705 (crypt_res->status = copyout
1706 (crp->uio.uio_iov[0].iov_base,
1707 crp->dst, crp->len)))
1708 goto bail;
1709
1710 if (crp->mac &&
1711 (crypt_res->status = copyout(crp->crp_mac,
1712 crp->mac, cse->thash->authsize)))
1713 goto bail;
1714 bail:
1715 TAILQ_REMOVE(&fcr->crp_ret_mq, crp, crp_next);
1716
1717 mutex_spin_exit(&crypto_mtx);
1718 crypto_freereq(crp);
1719 return 0;
1720 }
1721 }
1722
1723 TAILQ_FOREACH(krp, &fcr->crp_ret_mkq, krp_next) {
1724 if(krp && (krp->krp_reqid == crypt_res->reqid)) {
1725 crypt_res[req].opaque = krp->krp_usropaque;
1726 if (krp->krp_status != 0) {
1727 DPRINTF(("cryptodev_key: "
1728 "krp->krp_status 0x%08x\n",
1729 krp->krp_status));
1730 crypt_res[req].status = krp->krp_status;
1731 goto fail;
1732 }
1733
1734 for (i = krp->krp_iparams; i < krp->krp_iparams
1735 + krp->krp_oparams; i++) {
1736 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1737 if (size == 0)
1738 continue;
1739 crypt_res[req].status = copyout
1740 (krp->krp_param[i].crp_p,
1741 krp->crk_param[i].crp_p, size);
1742 if (crypt_res[req].status) {
1743 DPRINTF(("cryptodev_key: copyout oparam"
1744 "%d failed, error=%d\n",
1745 i-krp->krp_iparams,
1746 crypt_result[req].status));
1747 goto fail;
1748 }
1749 }
1750 fail:
1751 TAILQ_REMOVE(&fcr->crp_ret_mkq, krp, krp_next);
1752 mutex_spin_exit(&crypto_mtx);
1753 /* not sure what to do for this */
1754 /* kop[req].crk_status = krp->krp_status; */
1755 for (i = 0; i < CRK_MAXPARAM; i++) {
1756 struct crparam *kp = &(krp->krp_param[i]);
1757 if (kp->crp_p) {
1758 size = (kp->crp_nbits + 7) / 8;
1759 KASSERT(size > 0);
1760 memset(kp->crp_p, 0, size);
1761 kmem_free(kp->crp_p, size);
1762 }
1763 }
1764 pool_put(&cryptkop_pool, krp);
1765 return 0;
1766 }
1767 }
1768 mutex_spin_exit(&crypto_mtx);
1769 return EINPROGRESS;
1770 }
1771
1772 static int
1773 cryptof_poll(struct file *fp, int events)
1774 {
1775 struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
1776 int revents = 0;
1777
1778 if (!(events & (POLLIN | POLLRDNORM))) {
1779 /* only support read and POLLIN */
1780 return 0;
1781 }
1782
1783 mutex_spin_enter(&crypto_mtx);
1784 if (TAILQ_EMPTY(&fcr->crp_ret_mq) && TAILQ_EMPTY(&fcr->crp_ret_mkq)) {
1785 /* no completed requests pending, save the poll for later */
1786 selrecord(curlwp, &fcr->sinfo);
1787 } else {
1788 /* let the app(s) know that there are completed requests */
1789 revents = events & (POLLIN | POLLRDNORM);
1790 }
1791 mutex_spin_exit(&crypto_mtx);
1792
1793 return revents;
1794 }
1795
1796 /*
1797 * Pseudo-device initialization routine for /dev/crypto
1798 */
1799 void cryptoattach(int);
1800
1801 void
1802 cryptoattach(int num)
1803 {
1804 pool_init(&fcrpl, sizeof(struct fcrypt), 0, 0, 0, "fcrpl",
1805 NULL, IPL_NET); /* XXX IPL_NET ("splcrypto") */
1806 pool_init(&csepl, sizeof(struct csession), 0, 0, 0, "csepl",
1807 NULL, IPL_NET); /* XXX IPL_NET ("splcrypto") */
1808
1809 /*
1810 * Preallocate space for 64 users, with 5 sessions each.
1811 * (consider that a TLS protocol session requires at least
1812 * 3DES, MD5, and SHA1 (both hashes are used in the PRF) for
1813 * the negotiation, plus HMAC_SHA1 for the actual SSL records,
1814 * consuming one session here for each algorithm.
1815 */
1816 pool_prime(&fcrpl, 64);
1817 pool_prime(&csepl, 64 * 5);
1818 }
1819