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