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