cryptodev.c revision 1.89.2.3 1 /* $NetBSD: cryptodev.c,v 1.89.2.3 2017/05/17 01:44:18 pgoyette 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 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * Copyright (c) 2001 Theo de Raadt
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 *
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. The name of the author may not be used to endorse or promote products
47 * derived from this software without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 *
60 * Effort sponsored in part by the Defense Advanced Research Projects
61 * Agency (DARPA) and Air Force Research Laboratory, Air Force
62 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
63 *
64 */
65
66 #include <sys/cdefs.h>
67 __KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.89.2.3 2017/05/17 01:44:18 pgoyette Exp $");
68
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kmem.h>
72 #include <sys/malloc.h>
73 #include <sys/mbuf.h>
74 #include <sys/pool.h>
75 #include <sys/sysctl.h>
76 #include <sys/file.h>
77 #include <sys/filedesc.h>
78 #include <sys/errno.h>
79 #include <sys/md5.h>
80 #include <sys/sha1.h>
81 #include <sys/conf.h>
82 #include <sys/device.h>
83 #include <sys/kauth.h>
84 #include <sys/select.h>
85 #include <sys/poll.h>
86 #include <sys/atomic.h>
87 #include <sys/stat.h>
88 #include <sys/module.h>
89
90 #ifdef _KERNEL_OPT
91 #include "opt_ocf.h"
92 #include "opt_compat_netbsd.h"
93 #endif
94
95 #include <opencrypto/cryptodev.h>
96 #include <opencrypto/cryptodev_internal.h>
97 #include <opencrypto/xform.h>
98
99 #include "ioconf.h"
100
101 kmutex_t crypto_mtx;
102
103 struct csession {
104 TAILQ_ENTRY(csession) next;
105 u_int64_t sid;
106 u_int32_t ses;
107
108 u_int32_t cipher; /* note: shares name space in crd_alg */
109 const struct enc_xform *txform;
110 u_int32_t mac; /* note: shares name space in crd_alg */
111 const struct auth_hash *thash;
112 u_int32_t comp_alg; /* note: shares name space in crd_alg */
113 const struct comp_algo *tcomp;
114
115 void * key;
116 int keylen;
117 u_char tmp_iv[EALG_MAX_BLOCK_LEN];
118
119 void * mackey;
120 int mackeylen;
121 u_char tmp_mac[CRYPTO_MAX_MAC_LEN];
122
123 struct iovec iovec[1]; /* user requests never have more */
124 struct uio uio;
125 int error;
126 };
127
128 struct fcrypt {
129 TAILQ_HEAD(csessionlist, csession) csessions;
130 TAILQ_HEAD(crprethead, cryptop) crp_ret_mq;
131 TAILQ_HEAD(krprethead, cryptkop) crp_ret_mkq;
132 int sesn;
133 struct selinfo sinfo;
134 u_int32_t requestid;
135 struct timespec atime;
136 struct timespec mtime;
137 struct timespec btime;
138 };
139
140 /* For our fixed-size allocations */
141 static struct pool fcrpl;
142 static struct pool csepl;
143
144 /* Declaration of master device (fd-cloning/ctxt-allocating) entrypoints */
145 static int cryptoopen(dev_t dev, int flag, int mode, struct lwp *l);
146 static int cryptoread(dev_t dev, struct uio *uio, int ioflag);
147 static int cryptowrite(dev_t dev, struct uio *uio, int ioflag);
148 static int cryptoselect(dev_t dev, int rw, struct lwp *l);
149
150 static int crypto_refcount = 0; /* Prevent detaching while in use */
151
152 /* Declaration of cloned-device (per-ctxt) entrypoints */
153 static int cryptof_read(struct file *, off_t *, struct uio *,
154 kauth_cred_t, int);
155 static int cryptof_write(struct file *, off_t *, struct uio *,
156 kauth_cred_t, int);
157 static int cryptof_ioctl(struct file *, u_long, void *);
158 static int cryptof_close(struct file *);
159 static int cryptof_poll(struct file *, int);
160 static int cryptof_stat(struct file *, struct stat *);
161
162 static const struct fileops cryptofops = {
163 .fo_read = cryptof_read,
164 .fo_write = cryptof_write,
165 .fo_ioctl = cryptof_ioctl,
166 .fo_fcntl = fnullop_fcntl,
167 .fo_poll = cryptof_poll,
168 .fo_stat = cryptof_stat,
169 .fo_close = cryptof_close,
170 .fo_kqfilter = fnullop_kqfilter,
171 .fo_restart = fnullop_restart,
172 };
173
174 struct csession *cryptodev_csefind(struct fcrypt *, u_int);
175 static struct csession *csefind(struct fcrypt *, u_int);
176 static int csedelete(struct fcrypt *, struct csession *);
177 static struct csession *cseadd(struct fcrypt *, struct csession *);
178 static struct csession *csecreate(struct fcrypt *, u_int64_t, void *,
179 u_int64_t, void *, u_int64_t, u_int32_t, u_int32_t, u_int32_t,
180 const struct enc_xform *, const struct auth_hash *,
181 const struct comp_algo *);
182 static int csefree(struct csession *);
183
184 static int cryptodev_key(struct crypt_kop *);
185 static int cryptodev_mkey(struct fcrypt *, struct crypt_n_kop *, int);
186 static int cryptodev_msessionfin(struct fcrypt *, int, u_int32_t *);
187
188 static int cryptodev_cb(void *);
189 static int cryptodevkey_cb(void *);
190
191 static int cryptodev_mcb(void *);
192 static int cryptodevkey_mcb(void *);
193
194 static int cryptodev_getmstatus(struct fcrypt *, struct crypt_result *,
195 int);
196 static int cryptodev_getstatus(struct fcrypt *, struct crypt_result *);
197
198 #ifdef COMPAT_50
199 extern int ocryptof_ioctl(struct file *, u_long, void *);
200 #endif
201
202 /*
203 * sysctl-able control variables for /dev/crypto now defined in crypto.c:
204 * crypto_usercrypto, crypto_userasmcrypto, crypto_devallowsoft.
205 */
206
207 /* ARGSUSED */
208 int
209 cryptof_read(file_t *fp, off_t *poff,
210 struct uio *uio, kauth_cred_t cred, int flags)
211 {
212 return EIO;
213 }
214
215 /* ARGSUSED */
216 int
217 cryptof_write(file_t *fp, off_t *poff,
218 struct uio *uio, kauth_cred_t cred, int flags)
219 {
220 return EIO;
221 }
222
223 /* ARGSUSED */
224 int
225 cryptof_ioctl(struct file *fp, u_long cmd, void *data)
226 {
227 struct fcrypt *fcr = fp->f_fcrypt;
228 struct csession *cse;
229 struct session_op *sop;
230 struct session_n_op *snop;
231 struct crypt_op *cop;
232 struct crypt_mop *mop;
233 struct crypt_mkop *mkop;
234 struct crypt_n_op *cnop;
235 struct crypt_n_kop *knop;
236 struct crypt_sgop *sgop;
237 struct crypt_sfop *sfop;
238 struct cryptret *crypt_ret;
239 struct crypt_result *crypt_res;
240 u_int32_t ses;
241 u_int32_t *sesid;
242 int error = 0;
243 size_t count;
244
245 /* backwards compatibility */
246 file_t *criofp;
247 struct fcrypt *criofcr;
248 int criofd;
249
250 mutex_enter(&crypto_mtx);
251 getnanotime(&fcr->atime);
252 mutex_exit(&crypto_mtx);
253
254 switch (cmd) {
255 case CRIOGET: /* XXX deprecated, remove after 5.0 */
256 if ((error = fd_allocfile(&criofp, &criofd)) != 0)
257 return error;
258 criofcr = pool_get(&fcrpl, PR_WAITOK);
259 mutex_enter(&crypto_mtx);
260 TAILQ_INIT(&criofcr->csessions);
261 TAILQ_INIT(&criofcr->crp_ret_mq);
262 TAILQ_INIT(&criofcr->crp_ret_mkq);
263 selinit(&criofcr->sinfo);
264
265 /*
266 * Don't ever return session 0, to allow detection of
267 * failed creation attempts with multi-create ioctl.
268 */
269 criofcr->sesn = 1;
270 criofcr->requestid = 1;
271 crypto_refcount++;
272 mutex_exit(&crypto_mtx);
273 (void)fd_clone(criofp, criofd, (FREAD|FWRITE),
274 &cryptofops, criofcr);
275 *(u_int32_t *)data = criofd;
276 return error;
277 break;
278 case CIOCGSESSION:
279 sop = (struct session_op *)data;
280 error = cryptodev_session(fcr, sop);
281 break;
282 case CIOCNGSESSION:
283 sgop = (struct crypt_sgop *)data;
284 snop = kmem_alloc((sgop->count *
285 sizeof(struct session_n_op)), KM_SLEEP);
286 error = copyin(sgop->sessions, snop, sgop->count *
287 sizeof(struct session_n_op));
288 if (error) {
289 goto mbail;
290 }
291
292 mutex_enter(&crypto_mtx);
293 fcr->mtime = fcr->atime;
294 mutex_exit(&crypto_mtx);
295 error = cryptodev_msession(fcr, snop, sgop->count);
296 if (error) {
297 goto mbail;
298 }
299
300 error = copyout(snop, sgop->sessions, sgop->count *
301 sizeof(struct session_n_op));
302 mbail:
303 kmem_free(snop, sgop->count * sizeof(struct session_n_op));
304 break;
305 case CIOCFSESSION:
306 mutex_enter(&crypto_mtx);
307 fcr->mtime = fcr->atime;
308 ses = *(u_int32_t *)data;
309 cse = csefind(fcr, ses);
310 if (cse == NULL) {
311 mutex_exit(&crypto_mtx);
312 return EINVAL;
313 }
314 csedelete(fcr, cse);
315 mutex_exit(&crypto_mtx);
316 error = csefree(cse);
317 break;
318 case CIOCNFSESSION:
319 mutex_enter(&crypto_mtx);
320 fcr->mtime = fcr->atime;
321 mutex_exit(&crypto_mtx);
322 sfop = (struct crypt_sfop *)data;
323 sesid = kmem_alloc((sfop->count * sizeof(u_int32_t)),
324 KM_SLEEP);
325 error = copyin(sfop->sesid, sesid,
326 (sfop->count * sizeof(u_int32_t)));
327 if (!error) {
328 error = cryptodev_msessionfin(fcr, sfop->count, sesid);
329 }
330 kmem_free(sesid, (sfop->count * sizeof(u_int32_t)));
331 break;
332 case CIOCCRYPT:
333 mutex_enter(&crypto_mtx);
334 fcr->mtime = fcr->atime;
335 cop = (struct crypt_op *)data;
336 cse = csefind(fcr, cop->ses);
337 mutex_exit(&crypto_mtx);
338 if (cse == NULL) {
339 DPRINTF(("csefind failed\n"));
340 return EINVAL;
341 }
342 error = cryptodev_op(cse, cop, curlwp);
343 DPRINTF(("cryptodev_op error = %d\n", error));
344 break;
345 case CIOCNCRYPTM:
346 mutex_enter(&crypto_mtx);
347 fcr->mtime = fcr->atime;
348 mutex_exit(&crypto_mtx);
349 mop = (struct crypt_mop *)data;
350 cnop = kmem_alloc((mop->count * sizeof(struct crypt_n_op)),
351 KM_SLEEP);
352 error = copyin(mop->reqs, cnop,
353 (mop->count * sizeof(struct crypt_n_op)));
354 if(!error) {
355 error = cryptodev_mop(fcr, cnop, mop->count, curlwp);
356 if (!error) {
357 error = copyout(cnop, mop->reqs,
358 (mop->count * sizeof(struct crypt_n_op)));
359 }
360 }
361 kmem_free(cnop, (mop->count * sizeof(struct crypt_n_op)));
362 break;
363 case CIOCKEY:
364 error = cryptodev_key((struct crypt_kop *)data);
365 DPRINTF(("cryptodev_key error = %d\n", error));
366 break;
367 case CIOCNFKEYM:
368 mutex_enter(&crypto_mtx);
369 fcr->mtime = fcr->atime;
370 mutex_exit(&crypto_mtx);
371 mkop = (struct crypt_mkop *)data;
372 knop = kmem_alloc((mkop->count * sizeof(struct crypt_n_kop)),
373 KM_SLEEP);
374 error = copyin(mkop->reqs, knop,
375 (mkop->count * sizeof(struct crypt_n_kop)));
376 if (!error) {
377 error = cryptodev_mkey(fcr, knop, mkop->count);
378 if (!error)
379 error = copyout(knop, mkop->reqs,
380 (mkop->count * sizeof(struct crypt_n_kop)));
381 }
382 kmem_free(knop, (mkop->count * sizeof(struct crypt_n_kop)));
383 break;
384 case CIOCASYMFEAT:
385 error = crypto_getfeat((int *)data);
386 break;
387 case CIOCNCRYPTRETM:
388 mutex_enter(&crypto_mtx);
389 fcr->mtime = fcr->atime;
390 mutex_exit(&crypto_mtx);
391 crypt_ret = (struct cryptret *)data;
392 count = crypt_ret->count;
393 crypt_res = kmem_alloc((count * sizeof(struct crypt_result)),
394 KM_SLEEP);
395 error = copyin(crypt_ret->results, crypt_res,
396 (count * sizeof(struct crypt_result)));
397 if (error)
398 goto reterr;
399 crypt_ret->count = cryptodev_getmstatus(fcr, crypt_res,
400 crypt_ret->count);
401 /* sanity check count */
402 if (crypt_ret->count > count) {
403 printf("%s.%d: error returned count %zd > original "
404 " count %zd\n",
405 __FILE__, __LINE__, crypt_ret->count, count);
406 crypt_ret->count = count;
407
408 }
409 error = copyout(crypt_res, crypt_ret->results,
410 (crypt_ret->count * sizeof(struct crypt_result)));
411 reterr:
412 kmem_free(crypt_res, (count * sizeof(struct crypt_result)));
413 break;
414 case CIOCNCRYPTRET:
415 error = cryptodev_getstatus(fcr, (struct crypt_result *)data);
416 break;
417 default:
418 #ifdef COMPAT_50
419 /* Check for backward compatible commands */
420 error = ocryptof_ioctl(fp, cmd, data);
421 #else
422 return EINVAL;
423 #endif
424 }
425 return error;
426 }
427
428 int
429 cryptodev_op(struct csession *cse, struct crypt_op *cop, struct lwp *l)
430 {
431 struct cryptop *crp = NULL;
432 struct cryptodesc *crde = NULL, *crda = NULL, *crdc = NULL;
433 int error;
434 int iov_len = cop->len;
435 int flags=0;
436 int dst_len; /* copyout size */
437
438 if (cop->len > 256*1024-4)
439 return E2BIG;
440
441 if (cse->txform) {
442 if (cop->len < cse->txform->blocksize
443 + (cop->iv ? 0 : cse->txform->ivsize) ||
444 (cop->len - (cop->iv ? 0 : cse->txform->ivsize))
445 % cse->txform->blocksize != 0)
446 return EINVAL;
447 }
448
449 DPRINTF(("cryptodev_op[%u]: iov_len %d\n",
450 CRYPTO_SESID2LID(cse->sid), iov_len));
451 if ((cse->tcomp) && cop->dst_len) {
452 if (iov_len < cop->dst_len) {
453 /* Need larger iov to deal with decompress */
454 iov_len = cop->dst_len;
455 }
456 DPRINTF(("cryptodev_op: iov_len -> %d for decompress\n", iov_len));
457 }
458
459 (void)memset(&cse->uio, 0, sizeof(cse->uio));
460 cse->uio.uio_iovcnt = 1;
461 cse->uio.uio_resid = 0;
462 cse->uio.uio_rw = UIO_WRITE;
463 cse->uio.uio_iov = cse->iovec;
464 UIO_SETUP_SYSSPACE(&cse->uio);
465 memset(&cse->iovec, 0, sizeof(cse->iovec));
466
467 /* the iov needs to be big enough to handle the uncompressed
468 * data.... */
469 cse->uio.uio_iov[0].iov_len = iov_len;
470 if (iov_len > 0)
471 cse->uio.uio_iov[0].iov_base = kmem_alloc(iov_len, KM_SLEEP);
472 cse->uio.uio_resid = cse->uio.uio_iov[0].iov_len;
473 DPRINTF(("cryptodev_op[%u]: uio.iov_base %p malloced %d bytes\n",
474 CRYPTO_SESID2LID(cse->sid),
475 cse->uio.uio_iov[0].iov_base, iov_len));
476
477 crp = crypto_getreq((cse->tcomp != NULL) + (cse->txform != NULL) + (cse->thash != NULL));
478 if (crp == NULL) {
479 error = ENOMEM;
480 goto bail;
481 }
482 DPRINTF(("cryptodev_op[%u]: crp %p\n",
483 CRYPTO_SESID2LID(cse->sid), crp));
484
485 /* crds are always ordered tcomp, thash, then txform */
486 /* with optional missing links */
487
488 /* XXX: If we're going to compress then hash or encrypt, we need
489 * to be able to pass on the new size of the data.
490 */
491
492 if (cse->tcomp) {
493 crdc = crp->crp_desc;
494 }
495
496 if (cse->thash) {
497 crda = crdc ? crdc->crd_next : crp->crp_desc;
498 if (cse->txform && crda)
499 crde = crda->crd_next;
500 } else {
501 if (cse->txform) {
502 crde = crdc ? crdc->crd_next : crp->crp_desc;
503 } else if (!cse->tcomp) {
504 error = EINVAL;
505 goto bail;
506 }
507 }
508
509 DPRINTF(("ocf[%u]: iov_len %zu, cop->len %u\n",
510 CRYPTO_SESID2LID(cse->sid),
511 cse->uio.uio_iov[0].iov_len,
512 cop->len));
513
514 if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
515 {
516 printf("copyin failed %s %d \n", (char *)cop->src, error);
517 goto bail;
518 }
519
520 if (crdc) {
521 switch (cop->op) {
522 case COP_COMP:
523 crdc->crd_flags |= CRD_F_COMP;
524 break;
525 case COP_DECOMP:
526 crdc->crd_flags &= ~CRD_F_COMP;
527 break;
528 default:
529 break;
530 }
531 /* more data to follow? */
532 if (cop->flags & COP_F_MORE) {
533 flags |= CRYPTO_F_MORE;
534 }
535 crdc->crd_len = cop->len;
536 crdc->crd_inject = 0;
537
538 crdc->crd_alg = cse->comp_alg;
539 crdc->crd_key = NULL;
540 crdc->crd_klen = 0;
541 DPRINTF(("cryptodev_op[%u]: crdc setup for comp_alg %d.\n",
542 CRYPTO_SESID2LID(cse->sid), crdc->crd_alg));
543 }
544
545 if (crda) {
546 crda->crd_skip = 0;
547 crda->crd_len = cop->len;
548 crda->crd_inject = 0; /* ??? */
549
550 crda->crd_alg = cse->mac;
551 crda->crd_key = cse->mackey;
552 crda->crd_klen = cse->mackeylen * 8;
553 DPRINTF(("cryptodev_op: crda setup for mac %d.\n", crda->crd_alg));
554 }
555
556 if (crde) {
557 switch (cop->op) {
558 case COP_ENCRYPT:
559 crde->crd_flags |= CRD_F_ENCRYPT;
560 break;
561 case COP_DECRYPT:
562 crde->crd_flags &= ~CRD_F_ENCRYPT;
563 break;
564 default:
565 break;
566 }
567 crde->crd_len = cop->len;
568 crde->crd_inject = 0;
569
570 if (cse->cipher == CRYPTO_AES_GCM_16 && crda)
571 crda->crd_len = 0;
572 else if (cse->cipher == CRYPTO_AES_GMAC)
573 crde->crd_len = 0;
574
575 crde->crd_alg = cse->cipher;
576 crde->crd_key = cse->key;
577 crde->crd_klen = cse->keylen * 8;
578 DPRINTF(("cryptodev_op: crde setup for cipher %d.\n", crde->crd_alg));
579 }
580
581
582 crp->crp_ilen = cop->len;
583 /*
584 * The request is flagged as CRYPTO_F_USER as long as it is running
585 * in the user IOCTL thread. However, whether the request completes
586 * immediately or belatedly is depends on the used encryption driver.
587 */
588 crp->crp_flags = CRYPTO_F_IOV | (cop->flags & COP_F_BATCH) | CRYPTO_F_USER |
589 flags;
590 crp->crp_buf = (void *)&cse->uio;
591 crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
592 crp->crp_sid = cse->sid;
593 crp->crp_opaque = (void *)cse;
594
595 if (cop->iv) {
596 if (crde == NULL) {
597 error = EINVAL;
598 goto bail;
599 }
600 if (cse->txform->ivsize == 0) {
601 error = EINVAL;
602 goto bail;
603 }
604 if ((error = copyin(cop->iv, cse->tmp_iv,
605 cse->txform->ivsize)))
606 goto bail;
607 (void)memcpy(crde->crd_iv, cse->tmp_iv, cse->txform->ivsize);
608 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
609 crde->crd_skip = 0;
610 } else if (crde) {
611 if (cse->txform->ivsize == 0) {
612 crde->crd_skip = 0;
613 } else {
614 if (!(crde->crd_flags & CRD_F_ENCRYPT))
615 crde->crd_flags |= CRD_F_IV_PRESENT;
616 crde->crd_skip = cse->txform->ivsize;
617 crde->crd_len -= cse->txform->ivsize;
618 }
619 }
620
621 if (cop->mac) {
622 if (crda == NULL) {
623 error = EINVAL;
624 goto bail;
625 }
626 crp->crp_mac=cse->tmp_mac;
627 }
628
629 cv_init(&crp->crp_cv, "crydev");
630
631 /*
632 * XXX there was a comment here which said that we went to
633 * XXX splcrypto() but needed to only if CRYPTO_F_CBIMM,
634 * XXX disabled on NetBSD since 1.6O due to a race condition.
635 * XXX But crypto_dispatch went to splcrypto() itself! (And
636 * XXX now takes the crypto_mtx mutex itself). We do, however,
637 * XXX need to hold the mutex across the call to cv_wait().
638 * XXX (should we arrange for crypto_dispatch to return to
639 * XXX us with it held? it seems quite ugly to do so.)
640 */
641 #ifdef notyet
642 eagain:
643 #endif
644 error = crypto_dispatch(crp);
645 mutex_enter(&crypto_mtx);
646
647 /*
648 * Don't touch crp before returned by any error or recieved
649 * cv_signal(&crp->crp_cv). It is required to restructure locks.
650 */
651
652 switch (error) {
653 #ifdef notyet /* don't loop forever -- but EAGAIN not possible here yet */
654 case EAGAIN:
655 mutex_exit(&crypto_mtx);
656 goto eagain;
657 break;
658 #endif
659 case 0:
660 break;
661 default:
662 DPRINTF(("cryptodev_op: not waiting, error.\n"));
663 mutex_exit(&crypto_mtx);
664 cv_destroy(&crp->crp_cv);
665 goto bail;
666 }
667
668 while (!(crp->crp_flags & CRYPTO_F_DQRETQ)) {
669 DPRINTF(("cryptodev_op[%d]: sleeping on cv %p for crp %p\n",
670 (uint32_t)cse->sid, &crp->crp_cv, crp));
671 cv_wait(&crp->crp_cv, &crypto_mtx); /* XXX cv_wait_sig? */
672 }
673 mutex_exit(&crypto_mtx);
674 cv_destroy(&crp->crp_cv);
675
676 if (crp->crp_etype != 0) {
677 DPRINTF(("cryptodev_op: crp_etype %d\n", crp->crp_etype));
678 error = crp->crp_etype;
679 goto bail;
680 }
681
682 if (cse->error) {
683 DPRINTF(("cryptodev_op: cse->error %d\n", cse->error));
684 error = cse->error;
685 goto bail;
686 }
687
688 dst_len = crp->crp_ilen;
689 /* let the user know how much data was returned */
690 if (crp->crp_olen) {
691 if (crp->crp_olen > (cop->dst_len ? cop->dst_len : cop->len)) {
692 error = ENOSPC;
693 goto bail;
694 }
695 dst_len = cop->dst_len = crp->crp_olen;
696 }
697
698 if (cop->dst) {
699 DPRINTF(("cryptodev_op: copyout %d bytes to %p\n", dst_len, cop->dst));
700 }
701 if (cop->dst &&
702 (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, dst_len)))
703 {
704 DPRINTF(("cryptodev_op: copyout error %d\n", error));
705 goto bail;
706 }
707
708 if (cop->mac &&
709 (error = copyout(crp->crp_mac, cop->mac, cse->thash->authsize))) {
710 DPRINTF(("cryptodev_op: mac copyout error %d\n", error));
711 goto bail;
712 }
713
714
715 bail:
716 if (crp) {
717 crypto_freereq(crp);
718 }
719 if (cse->uio.uio_iov[0].iov_base) {
720 kmem_free(cse->uio.uio_iov[0].iov_base,iov_len);
721 }
722
723 return error;
724 }
725
726 static int
727 cryptodev_cb(void *op)
728 {
729 struct cryptop *crp = (struct cryptop *) op;
730 struct csession *cse = (struct csession *)crp->crp_opaque;
731 int error = 0;
732
733 mutex_enter(&crypto_mtx);
734 cse->error = crp->crp_etype;
735 if (crp->crp_etype == EAGAIN) {
736 /* always drop mutex to call dispatch routine */
737 mutex_exit(&crypto_mtx);
738 error = crypto_dispatch(crp);
739 mutex_enter(&crypto_mtx);
740 }
741 if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
742 crp->crp_flags |= CRYPTO_F_DQRETQ;
743 cv_signal(&crp->crp_cv);
744 }
745 mutex_exit(&crypto_mtx);
746 return 0;
747 }
748
749 static int
750 cryptodev_mcb(void *op)
751 {
752 struct cryptop *crp = (struct cryptop *) op;
753 struct csession *cse = (struct csession *)crp->crp_opaque;
754 int error=0;
755
756 mutex_enter(&crypto_mtx);
757 cse->error = crp->crp_etype;
758 if (crp->crp_etype == EAGAIN) {
759 mutex_exit(&crypto_mtx);
760 error = crypto_dispatch(crp);
761 mutex_enter(&crypto_mtx);
762 }
763 if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
764 cv_signal(&crp->crp_cv);
765 }
766
767 TAILQ_INSERT_TAIL(&crp->fcrp->crp_ret_mq, crp, crp_next);
768 selnotify(&crp->fcrp->sinfo, 0, 0);
769 mutex_exit(&crypto_mtx);
770 return 0;
771 }
772
773 static int
774 cryptodevkey_cb(void *op)
775 {
776 struct cryptkop *krp = op;
777
778 mutex_enter(&crypto_mtx);
779 krp->krp_flags |= CRYPTO_F_DQRETQ;
780 cv_signal(&krp->krp_cv);
781 mutex_exit(&crypto_mtx);
782 return 0;
783 }
784
785 static int
786 cryptodevkey_mcb(void *op)
787 {
788 struct cryptkop *krp = op;
789
790 mutex_enter(&crypto_mtx);
791 cv_signal(&krp->krp_cv);
792 TAILQ_INSERT_TAIL(&krp->fcrp->crp_ret_mkq, krp, krp_next);
793 selnotify(&krp->fcrp->sinfo, 0, 0);
794 mutex_exit(&crypto_mtx);
795 return 0;
796 }
797
798 static int
799 cryptodev_key(struct crypt_kop *kop)
800 {
801 struct cryptkop *krp = NULL;
802 int error = EINVAL;
803 int in, out, size, i;
804
805 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM)
806 return EFBIG;
807
808 in = kop->crk_iparams;
809 out = kop->crk_oparams;
810 switch (kop->crk_op) {
811 case CRK_MOD_EXP:
812 if (in == 3 && out == 1)
813 break;
814 return EINVAL;
815 case CRK_MOD_EXP_CRT:
816 if (in == 6 && out == 1)
817 break;
818 return EINVAL;
819 case CRK_DSA_SIGN:
820 if (in == 5 && out == 2)
821 break;
822 return EINVAL;
823 case CRK_DSA_VERIFY:
824 if (in == 7 && out == 0)
825 break;
826 return EINVAL;
827 case CRK_DH_COMPUTE_KEY:
828 if (in == 3 && out == 1)
829 break;
830 return EINVAL;
831 case CRK_MOD_ADD:
832 if (in == 3 && out == 1)
833 break;
834 return EINVAL;
835 case CRK_MOD_ADDINV:
836 if (in == 2 && out == 1)
837 break;
838 return EINVAL;
839 case CRK_MOD_SUB:
840 if (in == 3 && out == 1)
841 break;
842 return EINVAL;
843 case CRK_MOD_MULT:
844 if (in == 3 && out == 1)
845 break;
846 return EINVAL;
847 case CRK_MOD_MULTINV:
848 if (in == 2 && out == 1)
849 break;
850 return EINVAL;
851 case CRK_MOD:
852 if (in == 2 && out == 1)
853 break;
854 return EINVAL;
855 default:
856 return EINVAL;
857 }
858
859 krp = pool_get(&cryptkop_pool, PR_WAITOK);
860 (void)memset(krp, 0, sizeof *krp);
861 cv_init(&krp->krp_cv, "crykdev");
862 krp->krp_op = kop->crk_op;
863 krp->krp_status = kop->crk_status;
864 krp->krp_iparams = kop->crk_iparams;
865 krp->krp_oparams = kop->crk_oparams;
866 krp->krp_status = 0;
867 krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
868
869 for (i = 0; i < CRK_MAXPARAM; i++)
870 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
871 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
872 size = (krp->krp_param[i].crp_nbits + 7) / 8;
873 if (size == 0)
874 continue;
875 krp->krp_param[i].crp_p = kmem_alloc(size, KM_SLEEP);
876 if (i >= krp->krp_iparams)
877 continue;
878 error = copyin(kop->crk_param[i].crp_p,
879 krp->krp_param[i].crp_p, size);
880 if (error)
881 goto fail;
882 }
883
884 error = crypto_kdispatch(krp);
885 if (error != 0) {
886 goto fail;
887 }
888
889 mutex_enter(&crypto_mtx);
890 while (!(krp->krp_flags & CRYPTO_F_DQRETQ)) {
891 cv_wait(&krp->krp_cv, &crypto_mtx); /* XXX cv_wait_sig? */
892 }
893 mutex_exit(&crypto_mtx);
894
895 if (krp->krp_status != 0) {
896 DPRINTF(("cryptodev_key: krp->krp_status 0x%08x\n",
897 krp->krp_status));
898 error = krp->krp_status;
899 goto fail;
900 }
901
902 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams;
903 i++) {
904 size = (krp->krp_param[i].crp_nbits + 7) / 8;
905 if (size == 0)
906 continue;
907 error = copyout(krp->krp_param[i].crp_p,
908 kop->crk_param[i].crp_p, size);
909 if (error) {
910 DPRINTF(("cryptodev_key: copyout oparam %d failed, "
911 "error=%d\n", i-krp->krp_iparams, error));
912 goto fail;
913 }
914 }
915
916 fail:
917 kop->crk_status = krp->krp_status;
918 for (i = 0; i < CRK_MAXPARAM; i++) {
919 struct crparam *kp = &(krp->krp_param[i]);
920 if (krp->krp_param[i].crp_p) {
921 size = (kp->crp_nbits + 7) / 8;
922 KASSERT(size > 0);
923 (void)memset(kp->crp_p, 0, size);
924 kmem_free(kp->crp_p, size);
925 }
926 }
927 cv_destroy(&krp->krp_cv);
928 pool_put(&cryptkop_pool, krp);
929 DPRINTF(("cryptodev_key: error=0x%08x\n", error));
930 return error;
931 }
932
933 /* ARGSUSED */
934 static int
935 cryptof_close(struct file *fp)
936 {
937 struct fcrypt *fcr = fp->f_fcrypt;
938 struct csession *cse;
939
940 mutex_enter(&crypto_mtx);
941 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
942 TAILQ_REMOVE(&fcr->csessions, cse, next);
943 mutex_exit(&crypto_mtx);
944 (void)csefree(cse);
945 mutex_enter(&crypto_mtx);
946 }
947 seldestroy(&fcr->sinfo);
948 fp->f_fcrypt = NULL;
949 crypto_refcount--;
950 mutex_exit(&crypto_mtx);
951
952 pool_put(&fcrpl, fcr);
953 return 0;
954 }
955
956 /* needed for compatibility module */
957 struct csession *cryptodev_csefind(struct fcrypt *fcr, u_int ses)
958 {
959 return csefind(fcr, ses);
960 }
961
962 /* csefind: call with crypto_mtx held. */
963 static struct csession *
964 csefind(struct fcrypt *fcr, u_int ses)
965 {
966 struct csession *cse, *cnext, *ret = NULL;
967
968 KASSERT(mutex_owned(&crypto_mtx));
969 TAILQ_FOREACH_SAFE(cse, &fcr->csessions, next, cnext)
970 if (cse->ses == ses)
971 ret = cse;
972
973 return ret;
974 }
975
976 /* csedelete: call with crypto_mtx held. */
977 static int
978 csedelete(struct fcrypt *fcr, struct csession *cse_del)
979 {
980 struct csession *cse, *cnext;
981 int ret = 0;
982
983 KASSERT(mutex_owned(&crypto_mtx));
984 TAILQ_FOREACH_SAFE(cse, &fcr->csessions, next, cnext) {
985 if (cse == cse_del) {
986 TAILQ_REMOVE(&fcr->csessions, cse, next);
987 ret = 1;
988 }
989 }
990 return ret;
991 }
992
993 static struct csession *
994 cseadd(struct fcrypt *fcr, struct csession *cse)
995 {
996 mutex_enter(&crypto_mtx);
997 /* don't let session ID wrap! */
998 if (fcr->sesn + 1 == 0) return NULL;
999 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
1000 cse->ses = fcr->sesn++;
1001 mutex_exit(&crypto_mtx);
1002 return cse;
1003 }
1004
1005 static struct csession *
1006 csecreate(struct fcrypt *fcr, u_int64_t sid, void *key, u_int64_t keylen,
1007 void *mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
1008 u_int32_t comp_alg, const struct enc_xform *txform,
1009 const struct auth_hash *thash, const struct comp_algo *tcomp)
1010 {
1011 struct csession *cse;
1012
1013 cse = pool_get(&csepl, PR_NOWAIT);
1014 if (cse == NULL)
1015 return NULL;
1016 cse->key = key;
1017 cse->keylen = keylen/8;
1018 cse->mackey = mackey;
1019 cse->mackeylen = mackeylen/8;
1020 cse->sid = sid;
1021 cse->cipher = cipher;
1022 cse->mac = mac;
1023 cse->comp_alg = comp_alg;
1024 cse->txform = txform;
1025 cse->thash = thash;
1026 cse->tcomp = tcomp;
1027 cse->error = 0;
1028 if (cseadd(fcr, cse))
1029 return cse;
1030 else {
1031 pool_put(&csepl, cse);
1032 return NULL;
1033 }
1034 }
1035
1036 /* csefree: call with crypto_mtx held. */
1037 static int
1038 csefree(struct csession *cse)
1039 {
1040 int error;
1041
1042 error = crypto_freesession(cse->sid);
1043 if (cse->key)
1044 free(cse->key, M_XDATA);
1045 if (cse->mackey)
1046 free(cse->mackey, M_XDATA);
1047 pool_put(&csepl, cse);
1048 return error;
1049 }
1050
1051 static int
1052 cryptoopen(dev_t dev, int flag, int mode,
1053 struct lwp *l)
1054 {
1055 file_t *fp;
1056 struct fcrypt *fcr;
1057 int fd, error;
1058
1059 if (crypto_usercrypto == 0)
1060 return ENXIO;
1061
1062 if ((error = fd_allocfile(&fp, &fd)) != 0)
1063 return error;
1064
1065 fcr = pool_get(&fcrpl, PR_WAITOK);
1066 getnanotime(&fcr->btime);
1067 fcr->atime = fcr->mtime = fcr->btime;
1068 mutex_enter(&crypto_mtx);
1069 TAILQ_INIT(&fcr->csessions);
1070 TAILQ_INIT(&fcr->crp_ret_mq);
1071 TAILQ_INIT(&fcr->crp_ret_mkq);
1072 selinit(&fcr->sinfo);
1073 /*
1074 * Don't ever return session 0, to allow detection of
1075 * failed creation attempts with multi-create ioctl.
1076 */
1077 fcr->sesn = 1;
1078 fcr->requestid = 1;
1079 crypto_refcount++;
1080 mutex_exit(&crypto_mtx);
1081 return fd_clone(fp, fd, flag, &cryptofops, fcr);
1082 }
1083
1084 static int
1085 cryptoread(dev_t dev, struct uio *uio, int ioflag)
1086 {
1087 return EIO;
1088 }
1089
1090 static int
1091 cryptowrite(dev_t dev, struct uio *uio, int ioflag)
1092 {
1093 return EIO;
1094 }
1095
1096 int
1097 cryptoselect(dev_t dev, int rw, struct lwp *l)
1098 {
1099 return 0;
1100 }
1101
1102 /*static*/
1103 struct cdevsw crypto_cdevsw = {
1104 DEVSW_MODULE_INIT
1105 .d_open = cryptoopen,
1106 .d_close = noclose,
1107 .d_read = cryptoread,
1108 .d_write = cryptowrite,
1109 .d_ioctl = noioctl,
1110 .d_stop = nostop,
1111 .d_tty = notty,
1112 .d_poll = cryptoselect /*nopoll*/,
1113 .d_mmap = nommap,
1114 .d_kqfilter = nokqfilter,
1115 .d_discard = nodiscard,
1116 .d_flag = D_OTHER
1117 };
1118
1119 int
1120 cryptodev_mop(struct fcrypt *fcr,
1121 struct crypt_n_op * cnop,
1122 int count, struct lwp *l)
1123 {
1124 struct cryptop *crp = NULL;
1125 struct cryptodesc *crde = NULL, *crda = NULL, *crdc = NULL;
1126 int req, error=0;
1127 struct csession *cse;
1128 int flags=0;
1129 int iov_len;
1130
1131 for (req = 0; req < count; req++) {
1132 mutex_enter(&crypto_mtx);
1133 cse = csefind(fcr, cnop[req].ses);
1134 if (cse == NULL) {
1135 DPRINTF(("csefind failed\n"));
1136 cnop[req].status = EINVAL;
1137 mutex_exit(&crypto_mtx);
1138 continue;
1139 }
1140 mutex_exit(&crypto_mtx);
1141
1142 if (cnop[req].len > 256*1024-4) {
1143 DPRINTF(("length failed\n"));
1144 cnop[req].status = EINVAL;
1145 continue;
1146 }
1147 if (cse->txform) {
1148 if (cnop[req].len < cse->txform->blocksize -
1149 (cnop[req].iv ? 0 : cse->txform->ivsize) ||
1150 (cnop[req].len -
1151 (cnop[req].iv ? 0 : cse->txform->ivsize))
1152 % cse->txform->blocksize) {
1153 cnop[req].status = EINVAL;
1154 continue;
1155 }
1156 }
1157
1158 crp = crypto_getreq((cse->txform != NULL) +
1159 (cse->thash != NULL) +
1160 (cse->tcomp != NULL));
1161 if (crp == NULL) {
1162 cnop[req].status = ENOMEM;
1163 goto bail;
1164 }
1165
1166 iov_len = cnop[req].len;
1167 /* got a compression/decompression max size? */
1168 if ((cse->tcomp) && cnop[req].dst_len) {
1169 if (iov_len < cnop[req].dst_len) {
1170 /* Need larger iov to deal with decompress */
1171 iov_len = cnop[req].dst_len;
1172 }
1173 DPRINTF(("cryptodev_mop: iov_len -> %d for decompress\n", iov_len));
1174 }
1175
1176 (void)memset(&crp->uio, 0, sizeof(crp->uio));
1177 crp->uio.uio_iovcnt = 1;
1178 crp->uio.uio_resid = 0;
1179 crp->uio.uio_rw = UIO_WRITE;
1180 crp->uio.uio_iov = crp->iovec;
1181 UIO_SETUP_SYSSPACE(&crp->uio);
1182 memset(&crp->iovec, 0, sizeof(crp->iovec));
1183 crp->uio.uio_iov[0].iov_len = iov_len;
1184 DPRINTF(("cryptodev_mop: kmem_alloc(%d) for iov \n", iov_len));
1185 crp->uio.uio_iov[0].iov_base = kmem_alloc(iov_len, KM_SLEEP);
1186 crp->uio.uio_resid = crp->uio.uio_iov[0].iov_len;
1187
1188 if (cse->tcomp) {
1189 crdc = crp->crp_desc;
1190 }
1191
1192 if (cse->thash) {
1193 crda = crdc ? crdc->crd_next : crp->crp_desc;
1194 if (cse->txform && crda)
1195 crde = crda->crd_next;
1196 } else {
1197 if (cse->txform) {
1198 crde = crdc ? crdc->crd_next : crp->crp_desc;
1199 } else if (!cse->tcomp) {
1200 error = EINVAL;
1201 goto bail;
1202 }
1203 }
1204
1205 if ((copyin(cnop[req].src,
1206 crp->uio.uio_iov[0].iov_base, cnop[req].len))) {
1207 cnop[req].status = EINVAL;
1208 goto bail;
1209 }
1210
1211 if (crdc) {
1212 switch (cnop[req].op) {
1213 case COP_COMP:
1214 crdc->crd_flags |= CRD_F_COMP;
1215 break;
1216 case COP_DECOMP:
1217 crdc->crd_flags &= ~CRD_F_COMP;
1218 break;
1219 default:
1220 break;
1221 }
1222 /* more data to follow? */
1223 if (cnop[req].flags & COP_F_MORE) {
1224 flags |= CRYPTO_F_MORE;
1225 }
1226 crdc->crd_len = cnop[req].len;
1227 crdc->crd_inject = 0;
1228
1229 crdc->crd_alg = cse->comp_alg;
1230 crdc->crd_key = NULL;
1231 crdc->crd_klen = 0;
1232 DPRINTF(("cryptodev_mop[%d]: crdc setup for comp_alg %d"
1233 " len %d.\n",
1234 (uint32_t)cse->sid, crdc->crd_alg,
1235 crdc->crd_len));
1236 }
1237
1238 if (crda) {
1239 crda->crd_skip = 0;
1240 crda->crd_len = cnop[req].len;
1241 crda->crd_inject = 0; /* ??? */
1242
1243 crda->crd_alg = cse->mac;
1244 crda->crd_key = cse->mackey;
1245 crda->crd_klen = cse->mackeylen * 8;
1246 }
1247
1248 if (crde) {
1249 if (cnop[req].op == COP_ENCRYPT)
1250 crde->crd_flags |= CRD_F_ENCRYPT;
1251 else
1252 crde->crd_flags &= ~CRD_F_ENCRYPT;
1253 crde->crd_len = cnop[req].len;
1254 crde->crd_inject = 0;
1255
1256 crde->crd_alg = cse->cipher;
1257 #ifdef notyet /* XXX must notify h/w driver new key, drain */
1258 if(cnop[req].key && cnop[req].keylen) {
1259 crde->crd_key = malloc(cnop[req].keylen,
1260 M_XDATA, M_WAITOK);
1261 if((error = copyin(cnop[req].key,
1262 crde->crd_key, cnop[req].keylen))) {
1263 cnop[req].status = EINVAL;
1264 goto bail;
1265 }
1266 crde->crd_klen = cnop[req].keylen * 8;
1267 } else { ... }
1268 #endif
1269 crde->crd_key = cse->key;
1270 crde->crd_klen = cse->keylen * 8;
1271 }
1272
1273 crp->crp_ilen = cnop[req].len;
1274 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM |
1275 (cnop[req].flags & COP_F_BATCH) | flags;
1276 crp->crp_buf = (void *)&crp->uio;
1277 crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_mcb;
1278 crp->crp_sid = cse->sid;
1279 crp->crp_opaque = (void *)cse;
1280 crp->fcrp = fcr;
1281 crp->dst = cnop[req].dst;
1282 crp->len = cnop[req].len; /* input len, iov may be larger */
1283 crp->mac = cnop[req].mac;
1284 DPRINTF(("cryptodev_mop: iov_base %p dst %p len %d mac %p\n",
1285 crp->uio.uio_iov[0].iov_base, crp->dst, crp->len,
1286 crp->mac));
1287
1288 if (cnop[req].iv) {
1289 if (crde == NULL) {
1290 cnop[req].status = EINVAL;
1291 goto bail;
1292 }
1293 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
1294 cnop[req].status = EINVAL;
1295 goto bail;
1296 }
1297 if ((error = copyin(cnop[req].iv, crp->tmp_iv,
1298 cse->txform->ivsize))) {
1299 cnop[req].status = EINVAL;
1300 goto bail;
1301 }
1302 (void)memcpy(crde->crd_iv, crp->tmp_iv,
1303 cse->txform->ivsize);
1304 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
1305 crde->crd_skip = 0;
1306 } else if (crde) {
1307 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
1308 crde->crd_skip = 0;
1309 } else {
1310 if (!(crde->crd_flags & CRD_F_ENCRYPT))
1311 crde->crd_flags |= CRD_F_IV_PRESENT;
1312 crde->crd_skip = cse->txform->ivsize;
1313 crde->crd_len -= cse->txform->ivsize;
1314 }
1315 }
1316
1317 if (cnop[req].mac) {
1318 if (crda == NULL) {
1319 cnop[req].status = EINVAL;
1320 goto bail;
1321 }
1322 crp->crp_mac=cse->tmp_mac;
1323 }
1324 cnop[req].reqid = atomic_inc_32_nv(&(fcr->requestid));
1325 crp->crp_reqid = cnop[req].reqid;
1326 crp->crp_usropaque = cnop[req].opaque;
1327 cv_init(&crp->crp_cv, "crydev");
1328 #ifdef notyet
1329 eagain:
1330 #endif
1331 cnop[req].status = crypto_dispatch(crp);
1332 mutex_enter(&crypto_mtx); /* XXX why mutex? */
1333
1334 switch (cnop[req].status) {
1335 #ifdef notyet /* don't loop forever -- but EAGAIN not possible here yet */
1336 case EAGAIN:
1337 mutex_exit(&crypto_mtx);
1338 goto eagain;
1339 break;
1340 #endif
1341 case 0:
1342 break;
1343 default:
1344 DPRINTF(("cryptodev_op: not waiting, error.\n"));
1345 mutex_exit(&crypto_mtx);
1346 cv_destroy(&crp->crp_cv);
1347 goto bail;
1348 }
1349
1350 mutex_exit(&crypto_mtx);
1351 cv_destroy(&crp->crp_cv);
1352 bail:
1353 if (cnop[req].status) {
1354 if (crp) {
1355 if (crp->uio.uio_iov[0].iov_base) {
1356 kmem_free(crp->uio.uio_iov[0].iov_base,
1357 crp->uio.uio_iov[0].iov_len);
1358 }
1359 crypto_freereq(crp);
1360 }
1361 error = 0;
1362 }
1363 }
1364 return error;
1365 }
1366
1367 static int
1368 cryptodev_mkey(struct fcrypt *fcr, struct crypt_n_kop *kop, int count)
1369 {
1370 struct cryptkop *krp = NULL;
1371 int error = EINVAL;
1372 int in, out, size, i, req;
1373
1374 for (req = 0; req < count; req++) {
1375 if (kop[req].crk_iparams + kop[req].crk_oparams > CRK_MAXPARAM)
1376 return EFBIG;
1377
1378 in = kop[req].crk_iparams;
1379 out = kop[req].crk_oparams;
1380 switch (kop[req].crk_op) {
1381 case CRK_MOD_EXP:
1382 if (in == 3 && out == 1)
1383 break;
1384 kop[req].crk_status = EINVAL;
1385 continue;
1386 case CRK_MOD_EXP_CRT:
1387 if (in == 6 && out == 1)
1388 break;
1389 kop[req].crk_status = EINVAL;
1390 continue;
1391 case CRK_DSA_SIGN:
1392 if (in == 5 && out == 2)
1393 break;
1394 kop[req].crk_status = EINVAL;
1395 continue;
1396 case CRK_DSA_VERIFY:
1397 if (in == 7 && out == 0)
1398 break;
1399 kop[req].crk_status = EINVAL;
1400 continue;
1401 case CRK_DH_COMPUTE_KEY:
1402 if (in == 3 && out == 1)
1403 break;
1404 kop[req].crk_status = EINVAL;
1405 continue;
1406 case CRK_MOD_ADD:
1407 if (in == 3 && out == 1)
1408 break;
1409 kop[req].crk_status = EINVAL;
1410 continue;
1411 case CRK_MOD_ADDINV:
1412 if (in == 2 && out == 1)
1413 break;
1414 kop[req].crk_status = EINVAL;
1415 continue;
1416 case CRK_MOD_SUB:
1417 if (in == 3 && out == 1)
1418 break;
1419 kop[req].crk_status = EINVAL;
1420 continue;
1421 case CRK_MOD_MULT:
1422 if (in == 3 && out == 1)
1423 break;
1424 kop[req].crk_status = EINVAL;
1425 continue;
1426 case CRK_MOD_MULTINV:
1427 if (in == 2 && out == 1)
1428 break;
1429 kop[req].crk_status = EINVAL;
1430 continue;
1431 case CRK_MOD:
1432 if (in == 2 && out == 1)
1433 break;
1434 kop[req].crk_status = EINVAL;
1435 continue;
1436 default:
1437 kop[req].crk_status = EINVAL;
1438 continue;
1439 }
1440
1441 krp = pool_get(&cryptkop_pool, PR_WAITOK);
1442 (void)memset(krp, 0, sizeof *krp);
1443 cv_init(&krp->krp_cv, "crykdev");
1444 krp->krp_op = kop[req].crk_op;
1445 krp->krp_status = kop[req].crk_status;
1446 krp->krp_iparams = kop[req].crk_iparams;
1447 krp->krp_oparams = kop[req].crk_oparams;
1448 krp->krp_status = 0;
1449 krp->krp_callback =
1450 (int (*) (struct cryptkop *)) cryptodevkey_mcb;
1451 (void)memcpy(krp->crk_param, kop[req].crk_param,
1452 sizeof(kop[req].crk_param));
1453
1454 krp->krp_flags = CRYPTO_F_CBIMM;
1455
1456 for (i = 0; i < CRK_MAXPARAM; i++)
1457 krp->krp_param[i].crp_nbits =
1458 kop[req].crk_param[i].crp_nbits;
1459 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
1460 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1461 if (size == 0)
1462 continue;
1463 krp->krp_param[i].crp_p =
1464 kmem_alloc(size, KM_SLEEP);
1465 if (i >= krp->krp_iparams)
1466 continue;
1467 kop[req].crk_status =
1468 copyin(kop[req].crk_param[i].crp_p,
1469 krp->krp_param[i].crp_p, size);
1470 if (kop[req].crk_status)
1471 goto fail;
1472 }
1473 krp->fcrp = fcr;
1474
1475 kop[req].crk_reqid = atomic_inc_32_nv(&(fcr->requestid));
1476 krp->krp_reqid = kop[req].crk_reqid;
1477 krp->krp_usropaque = kop[req].crk_opaque;
1478
1479 kop[req].crk_status = crypto_kdispatch(krp);
1480 if (kop[req].crk_status != 0) {
1481 goto fail;
1482 }
1483
1484 fail:
1485 if(kop[req].crk_status) {
1486 if (krp) {
1487 kop[req].crk_status = krp->krp_status;
1488 for (i = 0; i < CRK_MAXPARAM; i++) {
1489 struct crparam *kp =
1490 &(krp->krp_param[i]);
1491 if (kp->crp_p) {
1492 size = (kp->crp_nbits + 7) / 8;
1493 KASSERT(size > 0);
1494 memset(kp->crp_p, 0, size);
1495 kmem_free(kp->crp_p, size);
1496 }
1497 }
1498 cv_destroy(&krp->krp_cv);
1499 pool_put(&cryptkop_pool, krp);
1500 }
1501 }
1502 error = 0;
1503 }
1504 DPRINTF(("cryptodev_key: error=0x%08x\n", error));
1505 return error;
1506 }
1507
1508 int
1509 cryptodev_session(struct fcrypt *fcr, struct session_op *sop)
1510 {
1511 struct cryptoini cria, crie;
1512 struct cryptoini cric; /* compressor */
1513 struct cryptoini *crihead = NULL;
1514 const struct enc_xform *txform = NULL;
1515 const struct auth_hash *thash = NULL;
1516 const struct comp_algo *tcomp = NULL;
1517 struct csession *cse;
1518 u_int64_t sid;
1519 int error = 0;
1520
1521 DPRINTF(("cryptodev_session() cipher=%d, mac=%d\n", sop->cipher, sop->mac));
1522
1523 /* XXX there must be a way to not embed the list of xforms here */
1524 switch (sop->cipher) {
1525 case 0:
1526 break;
1527 case CRYPTO_DES_CBC:
1528 txform = &enc_xform_des;
1529 break;
1530 case CRYPTO_3DES_CBC:
1531 txform = &enc_xform_3des;
1532 break;
1533 case CRYPTO_BLF_CBC:
1534 txform = &enc_xform_blf;
1535 break;
1536 case CRYPTO_CAST_CBC:
1537 txform = &enc_xform_cast5;
1538 break;
1539 case CRYPTO_SKIPJACK_CBC:
1540 txform = &enc_xform_skipjack;
1541 break;
1542 case CRYPTO_AES_CBC:
1543 txform = &enc_xform_rijndael128;
1544 break;
1545 case CRYPTO_CAMELLIA_CBC:
1546 txform = &enc_xform_camellia;
1547 break;
1548 case CRYPTO_AES_CTR:
1549 txform = &enc_xform_aes_ctr;
1550 break;
1551 case CRYPTO_AES_GCM_16:
1552 txform = &enc_xform_aes_gcm;
1553 break;
1554 case CRYPTO_AES_GMAC:
1555 txform = &enc_xform_aes_gmac;
1556 break;
1557 case CRYPTO_NULL_CBC:
1558 txform = &enc_xform_null;
1559 break;
1560 case CRYPTO_ARC4:
1561 txform = &enc_xform_arc4;
1562 break;
1563 default:
1564 DPRINTF(("Invalid cipher %d\n", sop->cipher));
1565 return EINVAL;
1566 }
1567
1568 switch (sop->comp_alg) {
1569 case 0:
1570 break;
1571 case CRYPTO_DEFLATE_COMP:
1572 tcomp = &comp_algo_deflate;
1573 break;
1574 case CRYPTO_GZIP_COMP:
1575 tcomp = &comp_algo_gzip;
1576 DPRINTF(("cryptodev_session() tcomp for GZIP\n"));
1577 break;
1578 default:
1579 DPRINTF(("Invalid compression alg %d\n", sop->comp_alg));
1580 return EINVAL;
1581 }
1582
1583 switch (sop->mac) {
1584 case 0:
1585 break;
1586 case CRYPTO_MD5_HMAC:
1587 thash = &auth_hash_hmac_md5;
1588 break;
1589 case CRYPTO_SHA1_HMAC:
1590 thash = &auth_hash_hmac_sha1;
1591 break;
1592 case CRYPTO_MD5_HMAC_96:
1593 thash = &auth_hash_hmac_md5_96;
1594 break;
1595 case CRYPTO_SHA1_HMAC_96:
1596 thash = &auth_hash_hmac_sha1_96;
1597 break;
1598 case CRYPTO_SHA2_HMAC:
1599 /* XXX switching on key length seems questionable */
1600 if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize) {
1601 thash = &auth_hash_hmac_sha2_256;
1602 } else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize) {
1603 thash = &auth_hash_hmac_sha2_384;
1604 } else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize) {
1605 thash = &auth_hash_hmac_sha2_512;
1606 } else {
1607 DPRINTF(("Invalid mackeylen %d\n", sop->mackeylen));
1608 return EINVAL;
1609 }
1610 break;
1611 case CRYPTO_RIPEMD160_HMAC:
1612 thash = &auth_hash_hmac_ripemd_160;
1613 break;
1614 case CRYPTO_RIPEMD160_HMAC_96:
1615 thash = &auth_hash_hmac_ripemd_160_96;
1616 break;
1617 case CRYPTO_MD5:
1618 thash = &auth_hash_md5;
1619 break;
1620 case CRYPTO_SHA1:
1621 thash = &auth_hash_sha1;
1622 break;
1623 case CRYPTO_AES_XCBC_MAC_96:
1624 thash = &auth_hash_aes_xcbc_mac_96;
1625 break;
1626 case CRYPTO_AES_128_GMAC:
1627 thash = &auth_hash_gmac_aes_128;
1628 break;
1629 case CRYPTO_AES_192_GMAC:
1630 thash = &auth_hash_gmac_aes_192;
1631 break;
1632 case CRYPTO_AES_256_GMAC:
1633 thash = &auth_hash_gmac_aes_256;
1634 break;
1635 case CRYPTO_NULL_HMAC:
1636 thash = &auth_hash_null;
1637 break;
1638 default:
1639 DPRINTF(("Invalid mac %d\n", sop->mac));
1640 return EINVAL;
1641 }
1642
1643 memset(&crie, 0, sizeof(crie));
1644 memset(&cria, 0, sizeof(cria));
1645 memset(&cric, 0, sizeof(cric));
1646
1647 if (tcomp) {
1648 cric.cri_alg = tcomp->type;
1649 cric.cri_klen = 0;
1650 DPRINTF(("tcomp->type = %d\n", tcomp->type));
1651
1652 crihead = &cric;
1653 if (txform) {
1654 cric.cri_next = &crie;
1655 } else if (thash) {
1656 cric.cri_next = &cria;
1657 }
1658 }
1659
1660 if (txform) {
1661 crie.cri_alg = txform->type;
1662 crie.cri_klen = sop->keylen * 8;
1663 if (sop->keylen > txform->maxkey ||
1664 sop->keylen < txform->minkey) {
1665 DPRINTF(("keylen %d not in [%d,%d]\n",
1666 sop->keylen, txform->minkey, txform->maxkey));
1667 error = EINVAL;
1668 goto bail;
1669 }
1670
1671 crie.cri_key = malloc(crie.cri_klen / 8, M_XDATA, M_WAITOK);
1672 if ((error = copyin(sop->key, crie.cri_key, crie.cri_klen / 8)))
1673 goto bail;
1674 if (!crihead) {
1675 crihead = &crie;
1676 }
1677 if (thash)
1678 crie.cri_next = &cria;
1679 }
1680
1681 if (thash) {
1682 cria.cri_alg = thash->type;
1683 cria.cri_klen = sop->mackeylen * 8;
1684 if (sop->mackeylen != thash->keysize) {
1685 DPRINTF(("mackeylen %d != keysize %d\n",
1686 sop->mackeylen, thash->keysize));
1687 error = EINVAL;
1688 goto bail;
1689 }
1690 if (cria.cri_klen) {
1691 cria.cri_key = malloc(cria.cri_klen / 8, M_XDATA,
1692 M_WAITOK);
1693 if ((error = copyin(sop->mackey, cria.cri_key,
1694 cria.cri_klen / 8))) {
1695 goto bail;
1696 }
1697 }
1698 if (!crihead) {
1699 crihead = &cria;
1700 }
1701 }
1702
1703 error = crypto_newsession(&sid, crihead, crypto_devallowsoft);
1704 if (!error) {
1705 DPRINTF(("cryptodev_session: got session %d\n", (uint32_t)sid));
1706 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
1707 cria.cri_key, cria.cri_klen, (txform ? sop->cipher : 0), sop->mac,
1708 (tcomp ? sop->comp_alg : 0), txform, thash, tcomp);
1709 if (cse != NULL) {
1710 sop->ses = cse->ses;
1711 } else {
1712 DPRINTF(("csecreate failed\n"));
1713 crypto_freesession(sid);
1714 error = EINVAL;
1715 }
1716 } else {
1717 DPRINTF(("SIOCSESSION violates kernel parameters %d\n",
1718 error));
1719 }
1720 bail:
1721 if (error) {
1722 if (crie.cri_key) {
1723 memset(crie.cri_key, 0, crie.cri_klen / 8);
1724 free(crie.cri_key, M_XDATA);
1725 }
1726 if (cria.cri_key) {
1727 memset(cria.cri_key, 0, cria.cri_klen / 8);
1728 free(cria.cri_key, M_XDATA);
1729 }
1730 }
1731 return error;
1732 }
1733
1734 int
1735 cryptodev_msession(struct fcrypt *fcr, struct session_n_op *sn_ops,
1736 int count)
1737 {
1738 int i;
1739
1740 for (i = 0; i < count; i++, sn_ops++) {
1741 struct session_op s_op;
1742 s_op.cipher = sn_ops->cipher;
1743 s_op.mac = sn_ops->mac;
1744 s_op.keylen = sn_ops->keylen;
1745 s_op.key = sn_ops->key;
1746 s_op.mackeylen = sn_ops->mackeylen;
1747 s_op.mackey = sn_ops->mackey;
1748
1749 sn_ops->status = cryptodev_session(fcr, &s_op);
1750 sn_ops->ses = s_op.ses;
1751 }
1752
1753 return 0;
1754 }
1755
1756 static int
1757 cryptodev_msessionfin(struct fcrypt *fcr, int count, u_int32_t *sesid)
1758 {
1759 struct csession *cse;
1760 int req, error = 0;
1761
1762 mutex_enter(&crypto_mtx);
1763 for(req = 0; req < count; req++) {
1764 cse = csefind(fcr, sesid[req]);
1765 if (cse == NULL)
1766 continue;
1767 csedelete(fcr, cse);
1768 mutex_exit(&crypto_mtx);
1769 error = csefree(cse);
1770 mutex_enter(&crypto_mtx);
1771 }
1772 mutex_exit(&crypto_mtx);
1773 return error;
1774 }
1775
1776 /*
1777 * collect as many completed requests as are availble, or count completed
1778 * requests whichever is less.
1779 * return the number of requests.
1780 */
1781 static int
1782 cryptodev_getmstatus(struct fcrypt *fcr, struct crypt_result *crypt_res,
1783 int count)
1784 {
1785 struct cryptop *crp = NULL;
1786 struct cryptkop *krp = NULL;
1787 struct csession *cse;
1788 int i, size, req = 0;
1789 int completed=0;
1790
1791 /* On queue so nobody else can grab them
1792 * and copyout can be delayed-- no locking */
1793 TAILQ_HEAD(, cryptop) crp_delfree_q =
1794 TAILQ_HEAD_INITIALIZER(crp_delfree_q);
1795 TAILQ_HEAD(, cryptkop) krp_delfree_q =
1796 TAILQ_HEAD_INITIALIZER(krp_delfree_q);
1797
1798 /* at this point we do not know which response user is requesting for
1799 * (symmetric or asymmetric) so we copyout one from each i.e if the
1800 * count is 2 then 1 from symmetric and 1 from asymmetric queue and
1801 * if 3 then 2 symmetric and 1 asymmetric and so on */
1802
1803 /* pull off a list of requests while protected from changes */
1804 mutex_enter(&crypto_mtx);
1805 while (req < count) {
1806 crp = TAILQ_FIRST(&fcr->crp_ret_mq);
1807 if (crp) {
1808 TAILQ_REMOVE(&fcr->crp_ret_mq, crp, crp_next);
1809 TAILQ_INSERT_TAIL(&crp_delfree_q, crp, crp_next);
1810 cse = (struct csession *)crp->crp_opaque;
1811
1812 /* see if the session is still valid */
1813 cse = csefind(fcr, cse->ses);
1814 if (cse != NULL) {
1815 crypt_res[req].status = 0;
1816 } else {
1817 DPRINTF(("csefind failed\n"));
1818 crypt_res[req].status = EINVAL;
1819 }
1820 req++;
1821 }
1822 if(req < count) {
1823 crypt_res[req].status = 0;
1824 krp = TAILQ_FIRST(&fcr->crp_ret_mkq);
1825 if (krp) {
1826 TAILQ_REMOVE(&fcr->crp_ret_mkq, krp, krp_next);
1827 TAILQ_INSERT_TAIL(&krp_delfree_q, krp, krp_next);
1828 req++;
1829 }
1830 }
1831 }
1832 mutex_exit(&crypto_mtx);
1833
1834 /* now do all the work outside the mutex */
1835 for(req=0; req < count ;) {
1836 crp = TAILQ_FIRST(&crp_delfree_q);
1837 if (crp) {
1838 if (crypt_res[req].status != 0) {
1839 /* csefind failed during collection */
1840 goto bail;
1841 }
1842 cse = (struct csession *)crp->crp_opaque;
1843 crypt_res[req].reqid = crp->crp_reqid;
1844 crypt_res[req].opaque = crp->crp_usropaque;
1845 completed++;
1846
1847 if (crp->crp_etype != 0) {
1848 crypt_res[req].status = crp->crp_etype;
1849 goto bail;
1850 }
1851
1852 if (cse->error) {
1853 crypt_res[req].status = cse->error;
1854 goto bail;
1855 }
1856
1857 if (crp->dst && (crypt_res[req].status =
1858 copyout(crp->uio.uio_iov[0].iov_base, crp->dst,
1859 crp->len)))
1860 goto bail;
1861
1862 if (crp->mac && (crypt_res[req].status =
1863 copyout(crp->crp_mac, crp->mac,
1864 cse->thash->authsize)))
1865 goto bail;
1866
1867 bail:
1868 TAILQ_REMOVE(&crp_delfree_q, crp, crp_next);
1869 kmem_free(crp->uio.uio_iov[0].iov_base,
1870 crp->uio.uio_iov[0].iov_len);
1871 crypto_freereq(crp);
1872 req++;
1873 }
1874
1875 if (req < count) {
1876 krp = TAILQ_FIRST(&krp_delfree_q);
1877 if (krp) {
1878 crypt_res[req].reqid = krp->krp_reqid;
1879 crypt_res[req].opaque = krp->krp_usropaque;
1880 completed++;
1881 if (krp->krp_status != 0) {
1882 DPRINTF(("cryptodev_key: "
1883 "krp->krp_status 0x%08x\n",
1884 krp->krp_status));
1885 crypt_res[req].status = krp->krp_status;
1886 goto fail;
1887 }
1888
1889 for (i = krp->krp_iparams; i < krp->krp_iparams
1890 + krp->krp_oparams; i++) {
1891 size = (krp->krp_param[i].crp_nbits
1892 + 7) / 8;
1893 if (size == 0)
1894 continue;
1895 crypt_res[req].status = copyout
1896 (krp->krp_param[i].crp_p,
1897 krp->crk_param[i].crp_p, size);
1898 if (crypt_res[req].status) {
1899 DPRINTF(("cryptodev_key: "
1900 "copyout oparam %d failed, "
1901 "error=%d\n",
1902 i - krp->krp_iparams,
1903 crypt_res[req].status));
1904 goto fail;
1905 }
1906 }
1907 fail:
1908 TAILQ_REMOVE(&krp_delfree_q, krp, krp_next);
1909 /* not sure what to do for this */
1910 /* kop[req].crk_status = krp->krp_status; */
1911 for (i = 0; i < CRK_MAXPARAM; i++) {
1912 struct crparam *kp = &(krp->krp_param[i]);
1913 if (kp->crp_p) {
1914 size = (kp->crp_nbits + 7) / 8;
1915 KASSERT(size > 0);
1916 (void)memset(kp->crp_p, 0, size);
1917 kmem_free(kp->crp_p, size);
1918 }
1919 }
1920 cv_destroy(&krp->krp_cv);
1921 pool_put(&cryptkop_pool, krp);
1922 req++;
1923 }
1924 }
1925 }
1926
1927 return completed;
1928 }
1929
1930 static int
1931 cryptodev_getstatus (struct fcrypt *fcr, struct crypt_result *crypt_res)
1932 {
1933 struct cryptop *crp = NULL, *cnext;
1934 struct cryptkop *krp = NULL, *knext;
1935 struct csession *cse;
1936 int i, size, req = 0;
1937
1938 mutex_enter(&crypto_mtx);
1939 /* Here we dont know for which request the user is requesting the
1940 * response so checking in both the queues */
1941 TAILQ_FOREACH_SAFE(crp, &fcr->crp_ret_mq, crp_next, cnext) {
1942 if(crp && (crp->crp_reqid == crypt_res->reqid)) {
1943 cse = (struct csession *)crp->crp_opaque;
1944 crypt_res->opaque = crp->crp_usropaque;
1945 cse = csefind(fcr, cse->ses);
1946 if (cse == NULL) {
1947 DPRINTF(("csefind failed\n"));
1948 crypt_res->status = EINVAL;
1949 goto bail;
1950 }
1951
1952 if (crp->crp_etype != 0) {
1953 crypt_res->status = crp->crp_etype;
1954 goto bail;
1955 }
1956
1957 if (cse->error) {
1958 crypt_res->status = cse->error;
1959 goto bail;
1960 }
1961
1962 if (crp->dst && (crypt_res->status =
1963 copyout(crp->uio.uio_iov[0].iov_base,
1964 crp->dst, crp->len)))
1965 goto bail;
1966
1967 if (crp->mac && (crypt_res->status =
1968 copyout(crp->crp_mac, crp->mac,
1969 cse->thash->authsize)))
1970 goto bail;
1971 bail:
1972 TAILQ_REMOVE(&fcr->crp_ret_mq, crp, crp_next);
1973
1974 mutex_exit(&crypto_mtx);
1975 crypto_freereq(crp);
1976 return 0;
1977 }
1978 }
1979
1980 TAILQ_FOREACH_SAFE(krp, &fcr->crp_ret_mkq, krp_next, knext) {
1981 if(krp && (krp->krp_reqid == crypt_res->reqid)) {
1982 crypt_res[req].opaque = krp->krp_usropaque;
1983 if (krp->krp_status != 0) {
1984 DPRINTF(("cryptodev_key: "
1985 "krp->krp_status 0x%08x\n",
1986 krp->krp_status));
1987 crypt_res[req].status = krp->krp_status;
1988 goto fail;
1989 }
1990
1991 for (i = krp->krp_iparams; i < krp->krp_iparams +
1992 krp->krp_oparams; i++) {
1993 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1994 if (size == 0)
1995 continue;
1996 crypt_res[req].status = copyout(
1997 krp->krp_param[i].crp_p,
1998 krp->crk_param[i].crp_p, size);
1999 if (crypt_res[req].status) {
2000 DPRINTF(("cryptodev_key: copyout oparam"
2001 "%d failed, error=%d\n",
2002 i - krp->krp_iparams,
2003 crypt_res[req].status));
2004 goto fail;
2005 }
2006 }
2007 fail:
2008 TAILQ_REMOVE(&fcr->crp_ret_mkq, krp, krp_next);
2009 mutex_exit(&crypto_mtx);
2010 /* not sure what to do for this */
2011 /* kop[req].crk_status = krp->krp_status; */
2012 for (i = 0; i < CRK_MAXPARAM; i++) {
2013 struct crparam *kp = &(krp->krp_param[i]);
2014 if (kp->crp_p) {
2015 size = (kp->crp_nbits + 7) / 8;
2016 KASSERT(size > 0);
2017 memset(kp->crp_p, 0, size);
2018 kmem_free(kp->crp_p, size);
2019 }
2020 }
2021 cv_destroy(&krp->krp_cv);
2022 pool_put(&cryptkop_pool, krp);
2023 return 0;
2024 }
2025 }
2026 mutex_exit(&crypto_mtx);
2027 return EINPROGRESS;
2028 }
2029
2030 static int
2031 cryptof_stat(struct file *fp, struct stat *st)
2032 {
2033 struct fcrypt *fcr = fp->f_fcrypt;
2034
2035 (void)memset(st, 0, sizeof(*st));
2036
2037 mutex_enter(&crypto_mtx);
2038 st->st_dev = makedev(cdevsw_lookup_major(&crypto_cdevsw), fcr->sesn);
2039 st->st_atimespec = fcr->atime;
2040 st->st_mtimespec = fcr->mtime;
2041 st->st_ctimespec = st->st_birthtimespec = fcr->btime;
2042 st->st_uid = kauth_cred_geteuid(fp->f_cred);
2043 st->st_gid = kauth_cred_getegid(fp->f_cred);
2044 mutex_exit(&crypto_mtx);
2045
2046 return 0;
2047 }
2048
2049 static int
2050 cryptof_poll(struct file *fp, int events)
2051 {
2052 struct fcrypt *fcr = fp->f_fcrypt;
2053 int revents = 0;
2054
2055 if (!(events & (POLLIN | POLLRDNORM))) {
2056 /* only support read and POLLIN */
2057 return 0;
2058 }
2059
2060 mutex_enter(&crypto_mtx);
2061 if (TAILQ_EMPTY(&fcr->crp_ret_mq) && TAILQ_EMPTY(&fcr->crp_ret_mkq)) {
2062 /* no completed requests pending, save the poll for later */
2063 selrecord(curlwp, &fcr->sinfo);
2064 } else {
2065 /* let the app(s) know that there are completed requests */
2066 revents = events & (POLLIN | POLLRDNORM);
2067 }
2068 mutex_exit(&crypto_mtx);
2069
2070 return revents;
2071 }
2072
2073 /*
2074 * Pseudo-device initialization routine for /dev/crypto
2075 */
2076 void
2077 cryptoattach(int num)
2078 {
2079 crypto_init();
2080
2081 mutex_init(&crypto_mtx, MUTEX_DEFAULT, IPL_NONE);
2082
2083 pool_init(&fcrpl, sizeof(struct fcrypt), 0, 0, 0, "fcrpl",
2084 NULL, IPL_NET); /* XXX IPL_NET ("splcrypto") */
2085 pool_init(&csepl, sizeof(struct csession), 0, 0, 0, "csepl",
2086 NULL, IPL_NET); /* XXX IPL_NET ("splcrypto") */
2087
2088 /*
2089 * Preallocate space for 64 users, with 5 sessions each.
2090 * (consider that a TLS protocol session requires at least
2091 * 3DES, MD5, and SHA1 (both hashes are used in the PRF) for
2092 * the negotiation, plus HMAC_SHA1 for the actual SSL records,
2093 * consuming one session here for each algorithm.
2094 */
2095 pool_prime(&fcrpl, 64);
2096 pool_prime(&csepl, 64 * 5);
2097 }
2098
2099 void crypto_attach(device_t, device_t, void *);
2100
2101 void
2102 crypto_attach(device_t parent, device_t self, void * opaque)
2103 {
2104
2105 cryptoattach(0);
2106 }
2107
2108 int crypto_detach(device_t, int);
2109
2110 int
2111 crypto_detach(device_t self, int num)
2112 {
2113
2114 pool_destroy(&fcrpl);
2115 pool_destroy(&csepl);
2116
2117 mutex_destroy(&crypto_mtx);
2118
2119 return 0;
2120 }
2121
2122 int crypto_match(device_t, cfdata_t, void *);
2123
2124 int
2125 crypto_match(device_t parent, cfdata_t data, void *opaque)
2126 {
2127
2128 return 1;
2129 }
2130
2131 MODULE(MODULE_CLASS_DRIVER, crypto, "opencrypto");
2132
2133 CFDRIVER_DECL(crypto, DV_DULL, NULL);
2134
2135 CFATTACH_DECL2_NEW(crypto, 0, crypto_match, crypto_attach, crypto_detach,
2136 NULL, NULL, NULL);
2137
2138 #ifdef _MODULE
2139 static int cryptoloc[] = { -1, -1 };
2140
2141 static struct cfdata crypto_cfdata[] = {
2142 {
2143 .cf_name = "crypto",
2144 .cf_atname = "crypto",
2145 .cf_unit = 0,
2146 .cf_fstate = 0,
2147 .cf_loc = cryptoloc,
2148 .cf_flags = 0,
2149 .cf_pspec = NULL,
2150 },
2151 { NULL, NULL, 0, 0, NULL, 0, NULL }
2152 };
2153 #endif
2154
2155 static int
2156 crypto_modcmd(modcmd_t cmd, void *arg)
2157 {
2158 int error = 0;
2159 #ifdef _MODULE
2160 device_t dev;
2161 devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
2162 #endif
2163
2164 switch (cmd) {
2165 case MODULE_CMD_INIT:
2166 #ifdef _MODULE
2167
2168 error = config_cfdriver_attach(&crypto_cd);
2169 if (error) {
2170 return error;
2171 }
2172
2173 error = config_cfattach_attach(crypto_cd.cd_name, &crypto_ca);
2174 if (error) {
2175 config_cfdriver_detach(&crypto_cd);
2176 aprint_error("%s: unable to register cfattach\n",
2177 crypto_cd.cd_name);
2178
2179 return error;
2180 }
2181
2182 error = config_cfdata_attach(crypto_cfdata, 1);
2183 if (error) {
2184 config_cfattach_detach(crypto_cd.cd_name, &crypto_ca);
2185 config_cfdriver_detach(&crypto_cd);
2186 aprint_error("%s: unable to register cfdata\n",
2187 crypto_cd.cd_name);
2188
2189 return error;
2190 }
2191
2192 error = devsw_attach(crypto_cd.cd_name, NULL, &bmajor,
2193 &crypto_cdevsw, &cmajor);
2194 if (error) {
2195 error = config_cfdata_detach(crypto_cfdata);
2196 if (error) {
2197 return error;
2198 }
2199 config_cfattach_detach(crypto_cd.cd_name, &crypto_ca);
2200 config_cfdriver_detach(&crypto_cd);
2201 aprint_error("%s: unable to register devsw\n",
2202 crypto_cd.cd_name);
2203
2204 return error;
2205 }
2206
2207 dev = config_attach_pseudo(crypto_cfdata);
2208 device_release(dev);
2209 #endif
2210
2211 return error;
2212 case MODULE_CMD_FINI:
2213 #ifdef _MODULE
2214 error = config_cfdata_detach(crypto_cfdata);
2215 if (error) {
2216 return error;
2217 }
2218
2219 config_cfattach_detach(crypto_cd.cd_name, &crypto_ca);
2220 config_cfdriver_detach(&crypto_cd);
2221 devsw_detach(NULL, &crypto_cdevsw);
2222 #endif
2223
2224 return error;
2225 #ifdef _MODULE
2226 case MODULE_CMD_AUTOUNLOAD:
2227 #if 0 /*
2228 * XXX Completely disable auto-unload for now, since there is still
2229 * XXX a (small) window where in-module ref-counting doesn't help
2230 */
2231 if (crypto_refcount != 0)
2232 #endif
2233 return EBUSY;
2234 /* FALLTHROUGH */
2235 #endif
2236 default:
2237 return ENOTTY;
2238 }
2239 }
2240