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