cryptodev.c revision 1.121 1 /* $NetBSD: cryptodev.c,v 1.121 2022/05/22 11:39:45 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.121 2022/05/22 11:39:45 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 int error;
722
723 if ((error = crp->crp_etype) == EAGAIN) {
724 error = crypto_dispatch(crp);
725 if (error == 0)
726 return;
727 }
728
729 mutex_enter(&cryptodev_mtx);
730 cse->error = error;
731 crp->crp_devflags |= CRYPTODEV_F_RET;
732 cv_signal(&crp->crp_cv);
733 mutex_exit(&cryptodev_mtx);
734 }
735
736 static void
737 cryptodev_mcb(struct cryptop *crp)
738 {
739 struct csession *cse = crp->crp_opaque;
740 int error;
741
742 if ((error = crp->crp_etype) == EAGAIN) {
743 error = crypto_dispatch(crp);
744 if (error == 0)
745 return;
746 }
747
748 mutex_enter(&cryptodev_mtx);
749 cse->error = error;
750 TAILQ_INSERT_TAIL(&crp->fcrp->crp_ret_mq, crp, crp_next);
751 selnotify(&crp->fcrp->sinfo, 0, 0);
752 mutex_exit(&cryptodev_mtx);
753 }
754
755 static void
756 cryptodevkey_cb(struct cryptkop *krp)
757 {
758
759 mutex_enter(&cryptodev_mtx);
760 krp->krp_devflags |= CRYPTODEV_F_RET;
761 cv_signal(&krp->krp_cv);
762 mutex_exit(&cryptodev_mtx);
763 }
764
765 static void
766 cryptodevkey_mcb(struct cryptkop *krp)
767 {
768
769 mutex_enter(&cryptodev_mtx);
770 cv_signal(&krp->krp_cv);
771 TAILQ_INSERT_TAIL(&krp->fcrp->crp_ret_mkq, krp, krp_next);
772 selnotify(&krp->fcrp->sinfo, 0, 0);
773 mutex_exit(&cryptodev_mtx);
774 }
775
776 static int
777 cryptodev_key(struct crypt_kop *kop)
778 {
779 struct cryptkop *krp = NULL;
780 int error = EINVAL;
781 int in, out, size, i;
782
783 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM)
784 return EFBIG;
785
786 in = kop->crk_iparams;
787 out = kop->crk_oparams;
788 switch (kop->crk_op) {
789 case CRK_MOD_EXP:
790 if (in == 3 && out == 1)
791 break;
792 return EINVAL;
793 case CRK_MOD_EXP_CRT:
794 if (in == 6 && out == 1)
795 break;
796 return EINVAL;
797 case CRK_DSA_SIGN:
798 if (in == 5 && out == 2)
799 break;
800 return EINVAL;
801 case CRK_DSA_VERIFY:
802 if (in == 7 && out == 0)
803 break;
804 return EINVAL;
805 case CRK_DH_COMPUTE_KEY:
806 if (in == 3 && out == 1)
807 break;
808 return EINVAL;
809 case CRK_MOD_ADD:
810 if (in == 3 && out == 1)
811 break;
812 return EINVAL;
813 case CRK_MOD_ADDINV:
814 if (in == 2 && out == 1)
815 break;
816 return EINVAL;
817 case CRK_MOD_SUB:
818 if (in == 3 && out == 1)
819 break;
820 return EINVAL;
821 case CRK_MOD_MULT:
822 if (in == 3 && out == 1)
823 break;
824 return EINVAL;
825 case CRK_MOD_MULTINV:
826 if (in == 2 && out == 1)
827 break;
828 return EINVAL;
829 case CRK_MOD:
830 if (in == 2 && out == 1)
831 break;
832 return EINVAL;
833 default:
834 return EINVAL;
835 }
836
837 krp = crypto_kgetreq(1, PR_WAITOK);
838 if (krp == NULL) {
839 /* limited by opencrypto.crypto_ret_kq.maxlen */
840 return ENOMEM;
841 }
842 (void)memset(krp, 0, sizeof *krp);
843 cv_init(&krp->krp_cv, "crykdev");
844 krp->krp_op = kop->crk_op;
845 krp->krp_status = kop->crk_status;
846 krp->krp_iparams = kop->crk_iparams;
847 krp->krp_oparams = kop->crk_oparams;
848 krp->krp_status = 0;
849 krp->krp_callback = cryptodevkey_cb;
850
851 for (i = 0; i < CRK_MAXPARAM; i++)
852 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
853 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
854 size = (krp->krp_param[i].crp_nbits + 7) / 8;
855 if (size == 0)
856 continue;
857 krp->krp_param[i].crp_p = kmem_alloc(size, KM_SLEEP);
858 if (i >= krp->krp_iparams)
859 continue;
860 error = copyin(kop->crk_param[i].crp_p,
861 krp->krp_param[i].crp_p, size);
862 if (error)
863 goto fail;
864 }
865
866 error = crypto_kdispatch(krp);
867 if (error != 0) {
868 goto fail;
869 }
870
871 mutex_enter(&cryptodev_mtx);
872 while (!(krp->krp_devflags & CRYPTODEV_F_RET)) {
873 cv_wait(&krp->krp_cv, &cryptodev_mtx); /* XXX cv_wait_sig? */
874 }
875 mutex_exit(&cryptodev_mtx);
876
877 if (krp->krp_status != 0) {
878 DPRINTF("krp->krp_status 0x%08x\n", krp->krp_status);
879 error = krp->krp_status;
880 goto fail;
881 }
882
883 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams;
884 i++) {
885 size = (krp->krp_param[i].crp_nbits + 7) / 8;
886 if (size == 0)
887 continue;
888 error = copyout(krp->krp_param[i].crp_p,
889 kop->crk_param[i].crp_p, size);
890 if (error) {
891 DPRINTF("copyout oparam %d failed, "
892 "error=%d\n", i-krp->krp_iparams, error);
893 goto fail;
894 }
895 }
896
897 fail:
898 kop->crk_status = krp->krp_status;
899 for (i = 0; i < CRK_MAXPARAM; i++) {
900 struct crparam *kp = &(krp->krp_param[i]);
901 if (krp->krp_param[i].crp_p) {
902 size = (kp->crp_nbits + 7) / 8;
903 KASSERT(size > 0);
904 (void)memset(kp->crp_p, 0, size);
905 kmem_free(kp->crp_p, size);
906 }
907 }
908 cv_destroy(&krp->krp_cv);
909 crypto_kfreereq(krp);
910 DPRINTF("error=0x%08x\n", error);
911 return error;
912 }
913
914 /* ARGSUSED */
915 static int
916 cryptof_close(struct file *fp)
917 {
918 struct fcrypt *fcr = fp->f_fcrypt;
919 struct csession *cse;
920
921 mutex_enter(&cryptodev_mtx);
922 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
923 TAILQ_REMOVE(&fcr->csessions, cse, next);
924 mutex_exit(&cryptodev_mtx);
925 csefree(cse);
926 mutex_enter(&cryptodev_mtx);
927 }
928 seldestroy(&fcr->sinfo);
929 fp->f_fcrypt = NULL;
930 crypto_refcount--;
931 mutex_exit(&cryptodev_mtx);
932
933 pool_put(&fcrpl, fcr);
934 return 0;
935 }
936
937 /* needed for compatibility module */
938 struct csession *cryptodev_csefind(struct fcrypt *fcr, u_int ses)
939 {
940 return csefind(fcr, ses);
941 }
942
943 /* csefind: call with cryptodev_mtx held. */
944 static struct csession *
945 csefind(struct fcrypt *fcr, u_int ses)
946 {
947 struct csession *cse, *cnext, *ret = NULL;
948
949 KASSERT(mutex_owned(&cryptodev_mtx));
950 TAILQ_FOREACH_SAFE(cse, &fcr->csessions, next, cnext)
951 if (cse->ses == ses)
952 ret = cse;
953
954 return ret;
955 }
956
957 /* csedelete: call with cryptodev_mtx held. */
958 static int
959 csedelete(struct fcrypt *fcr, struct csession *cse_del)
960 {
961 struct csession *cse, *cnext;
962 int ret = 0;
963
964 KASSERT(mutex_owned(&cryptodev_mtx));
965 TAILQ_FOREACH_SAFE(cse, &fcr->csessions, next, cnext) {
966 if (cse == cse_del) {
967 TAILQ_REMOVE(&fcr->csessions, cse, next);
968 ret = 1;
969 }
970 }
971 return ret;
972 }
973
974 static struct csession *
975 cseadd(struct fcrypt *fcr, struct csession *cse)
976 {
977 mutex_enter(&cryptodev_mtx);
978 /* don't let session ID wrap! */
979 if (fcr->sesn + 1 == 0) return NULL;
980 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
981 cse->ses = fcr->sesn++;
982 mutex_exit(&cryptodev_mtx);
983 return cse;
984 }
985
986 static struct csession *
987 csecreate(struct fcrypt *fcr, u_int64_t sid, void *key, u_int64_t keylen,
988 void *mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
989 u_int32_t comp_alg, const struct enc_xform *txform,
990 const struct auth_hash *thash, const struct comp_algo *tcomp)
991 {
992 struct csession *cse;
993
994 cse = pool_get(&csepl, PR_NOWAIT);
995 if (cse == NULL)
996 return NULL;
997 cse->key = key;
998 cse->keylen = keylen/8;
999 cse->mackey = mackey;
1000 cse->mackeylen = mackeylen/8;
1001 cse->sid = sid;
1002 cse->cipher = cipher;
1003 cse->mac = mac;
1004 cse->comp_alg = comp_alg;
1005 cse->txform = txform;
1006 cse->thash = thash;
1007 cse->tcomp = tcomp;
1008 cse->error = 0;
1009 if (cseadd(fcr, cse))
1010 return cse;
1011 else {
1012 pool_put(&csepl, cse);
1013 return NULL;
1014 }
1015 }
1016
1017 static void
1018 csefree(struct csession *cse)
1019 {
1020
1021 crypto_freesession(cse->sid);
1022 if (cse->key)
1023 free(cse->key, M_XDATA);
1024 if (cse->mackey)
1025 free(cse->mackey, M_XDATA);
1026 pool_put(&csepl, cse);
1027 }
1028
1029 static int
1030 cryptoopen(dev_t dev, int flag, int mode,
1031 struct lwp *l)
1032 {
1033 file_t *fp;
1034 struct fcrypt *fcr;
1035 int fd, error;
1036
1037 if (crypto_usercrypto == 0)
1038 return ENXIO;
1039
1040 if ((error = fd_allocfile(&fp, &fd)) != 0)
1041 return error;
1042
1043 fcr = pool_get(&fcrpl, PR_WAITOK);
1044 getnanotime(&fcr->btime);
1045 fcr->atime = fcr->mtime = fcr->btime;
1046 mutex_enter(&cryptodev_mtx);
1047 TAILQ_INIT(&fcr->csessions);
1048 TAILQ_INIT(&fcr->crp_ret_mq);
1049 TAILQ_INIT(&fcr->crp_ret_mkq);
1050 selinit(&fcr->sinfo);
1051 /*
1052 * Don't ever return session 0, to allow detection of
1053 * failed creation attempts with multi-create ioctl.
1054 */
1055 fcr->sesn = 1;
1056 fcr->requestid = 1;
1057 crypto_refcount++;
1058 mutex_exit(&cryptodev_mtx);
1059 return fd_clone(fp, fd, flag, &cryptofops, fcr);
1060 }
1061
1062 static int
1063 cryptoread(dev_t dev, struct uio *uio, int ioflag)
1064 {
1065 return EIO;
1066 }
1067
1068 static int
1069 cryptowrite(dev_t dev, struct uio *uio, int ioflag)
1070 {
1071 return EIO;
1072 }
1073
1074 int
1075 cryptoselect(dev_t dev, int rw, struct lwp *l)
1076 {
1077 return 0;
1078 }
1079
1080 /*static*/
1081 struct cdevsw crypto_cdevsw = {
1082 .d_open = cryptoopen,
1083 .d_close = noclose,
1084 .d_read = cryptoread,
1085 .d_write = cryptowrite,
1086 .d_ioctl = noioctl,
1087 .d_stop = nostop,
1088 .d_tty = notty,
1089 .d_poll = cryptoselect /*nopoll*/,
1090 .d_mmap = nommap,
1091 .d_kqfilter = nokqfilter,
1092 .d_discard = nodiscard,
1093 .d_flag = D_OTHER
1094 };
1095
1096 int
1097 cryptodev_mop(struct fcrypt *fcr,
1098 struct crypt_n_op * cnop,
1099 int count, struct lwp *l)
1100 {
1101 struct cryptop *crp = NULL;
1102 struct cryptodesc *crde = NULL, *crda = NULL, *crdc = NULL;
1103 int req, error=0;
1104 struct csession *cse;
1105 int flags=0;
1106 int iov_len;
1107
1108 for (req = 0; req < count; req++) {
1109 mutex_enter(&cryptodev_mtx);
1110 cse = csefind(fcr, cnop[req].ses);
1111 if (cse == NULL) {
1112 DPRINTF("csefind failed\n");
1113 cnop[req].status = EINVAL;
1114 mutex_exit(&cryptodev_mtx);
1115 continue;
1116 }
1117 mutex_exit(&cryptodev_mtx);
1118
1119 if (cnop[req].len > 256*1024-4) {
1120 DPRINTF("length failed\n");
1121 cnop[req].status = EINVAL;
1122 continue;
1123 }
1124 if (cse->txform) {
1125 if (cnop[req].len < cse->txform->blocksize -
1126 (cnop[req].iv ? 0 : cse->txform->ivsize) ||
1127 (cnop[req].len -
1128 (cnop[req].iv ? 0 : cse->txform->ivsize))
1129 % cse->txform->blocksize) {
1130 cnop[req].status = EINVAL;
1131 continue;
1132 }
1133 }
1134
1135 if (cse->txform == NULL &&
1136 cse->thash == NULL &&
1137 cse->tcomp == NULL) {
1138 cnop[req].status = EINVAL;
1139 goto bail;
1140 }
1141
1142 /* sanitize */
1143 if (cnop[req].len <= 0) {
1144 cnop[req].status = ENOMEM;
1145 goto bail;
1146 }
1147
1148 crp = crypto_getreq((cse->txform != NULL) +
1149 (cse->thash != NULL) +
1150 (cse->tcomp != NULL));
1151 if (crp == NULL) {
1152 cnop[req].status = ENOMEM;
1153 goto bail;
1154 }
1155
1156 iov_len = cnop[req].len;
1157 /* got a compression/decompression max size? */
1158 if ((cse->tcomp) && cnop[req].dst_len) {
1159 if (iov_len < cnop[req].dst_len) {
1160 /* Need larger iov to deal with decompress */
1161 iov_len = cnop[req].dst_len;
1162 }
1163 DPRINTF("iov_len -> %d for decompress\n", iov_len);
1164 }
1165
1166 (void)memset(&crp->uio, 0, sizeof(crp->uio));
1167 crp->uio.uio_iovcnt = 1;
1168 crp->uio.uio_resid = 0;
1169 crp->uio.uio_rw = UIO_WRITE;
1170 crp->uio.uio_iov = crp->iovec;
1171 UIO_SETUP_SYSSPACE(&crp->uio);
1172 memset(&crp->iovec, 0, sizeof(crp->iovec));
1173 crp->uio.uio_iov[0].iov_len = iov_len;
1174 DPRINTF("kmem_alloc(%d) for iov \n", iov_len);
1175 crp->uio.uio_iov[0].iov_base = kmem_alloc(iov_len, KM_SLEEP);
1176 crp->uio.uio_resid = crp->uio.uio_iov[0].iov_len;
1177
1178 if (cse->tcomp) {
1179 crdc = crp->crp_desc;
1180 }
1181
1182 if (cse->thash) {
1183 crda = crdc ? crdc->crd_next : crp->crp_desc;
1184 if (cse->txform && crda)
1185 crde = crda->crd_next;
1186 } else {
1187 if (cse->txform) {
1188 crde = crdc ? crdc->crd_next : crp->crp_desc;
1189 } else if (!cse->tcomp) {
1190 error = EINVAL;
1191 goto bail;
1192 }
1193 }
1194
1195 if ((copyin(cnop[req].src,
1196 crp->uio.uio_iov[0].iov_base, cnop[req].len))) {
1197 cnop[req].status = EINVAL;
1198 goto bail;
1199 }
1200
1201 if (crdc) {
1202 switch (cnop[req].op) {
1203 case COP_COMP:
1204 crdc->crd_flags |= CRD_F_COMP;
1205 break;
1206 case COP_DECOMP:
1207 crdc->crd_flags &= ~CRD_F_COMP;
1208 break;
1209 default:
1210 break;
1211 }
1212 /* more data to follow? */
1213 if (cnop[req].flags & COP_F_MORE) {
1214 flags |= CRYPTO_F_MORE;
1215 }
1216 crdc->crd_len = cnop[req].len;
1217 crdc->crd_inject = 0;
1218
1219 crdc->crd_alg = cse->comp_alg;
1220 crdc->crd_key = NULL;
1221 crdc->crd_klen = 0;
1222 DPRINTF("cse->sid[%d]: crdc setup for comp_alg %d"
1223 " len %d.\n",
1224 (uint32_t)cse->sid, crdc->crd_alg,
1225 crdc->crd_len);
1226 }
1227
1228 if (crda) {
1229 crda->crd_skip = 0;
1230 crda->crd_len = cnop[req].len;
1231 crda->crd_inject = 0; /* ??? */
1232
1233 crda->crd_alg = cse->mac;
1234 crda->crd_key = cse->mackey;
1235 crda->crd_klen = cse->mackeylen * 8;
1236 }
1237
1238 if (crde) {
1239 if (cnop[req].op == COP_ENCRYPT)
1240 crde->crd_flags |= CRD_F_ENCRYPT;
1241 else
1242 crde->crd_flags &= ~CRD_F_ENCRYPT;
1243 crde->crd_len = cnop[req].len;
1244 crde->crd_inject = 0;
1245
1246 crde->crd_alg = cse->cipher;
1247 #ifdef notyet /* XXX must notify h/w driver new key, drain */
1248 if(cnop[req].key && cnop[req].keylen) {
1249 crde->crd_key = malloc(cnop[req].keylen,
1250 M_XDATA, M_WAITOK);
1251 if((error = copyin(cnop[req].key,
1252 crde->crd_key, cnop[req].keylen))) {
1253 cnop[req].status = EINVAL;
1254 goto bail;
1255 }
1256 crde->crd_klen = cnop[req].keylen * 8;
1257 } else { ... }
1258 #endif
1259 crde->crd_key = cse->key;
1260 crde->crd_klen = cse->keylen * 8;
1261 }
1262
1263 crp->crp_ilen = cnop[req].len;
1264 crp->crp_flags = CRYPTO_F_IOV |
1265 (cnop[req].flags & COP_F_BATCH) | flags;
1266 crp->crp_buf = (void *)&crp->uio;
1267 crp->crp_callback = cryptodev_mcb;
1268 crp->crp_sid = cse->sid;
1269 crp->crp_opaque = cse;
1270 crp->fcrp = fcr;
1271 crp->dst = cnop[req].dst;
1272 crp->len = cnop[req].len; /* input len, iov may be larger */
1273 crp->mac = cnop[req].mac;
1274 DPRINTF("iov_base %p dst %p len %d mac %p\n",
1275 crp->uio.uio_iov[0].iov_base, crp->dst, crp->len,
1276 crp->mac);
1277
1278 if (cnop[req].iv) {
1279 if (crde == NULL) {
1280 cnop[req].status = EINVAL;
1281 goto bail;
1282 }
1283 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
1284 cnop[req].status = EINVAL;
1285 goto bail;
1286 }
1287 if ((error = copyin(cnop[req].iv, crp->tmp_iv,
1288 cse->txform->ivsize))) {
1289 cnop[req].status = EINVAL;
1290 goto bail;
1291 }
1292 (void)memcpy(crde->crd_iv, crp->tmp_iv,
1293 cse->txform->ivsize);
1294 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
1295 crde->crd_skip = 0;
1296 } else if (crde) {
1297 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
1298 crde->crd_skip = 0;
1299 } else {
1300 if (!(crde->crd_flags & CRD_F_ENCRYPT))
1301 crde->crd_flags |= CRD_F_IV_PRESENT;
1302 crde->crd_skip = cse->txform->ivsize;
1303 crde->crd_len -= cse->txform->ivsize;
1304 }
1305 }
1306
1307 if (cnop[req].mac) {
1308 if (crda == NULL) {
1309 cnop[req].status = EINVAL;
1310 goto bail;
1311 }
1312 crp->crp_mac=cse->tmp_mac;
1313 }
1314 cnop[req].reqid = atomic_inc_32_nv(&(fcr->requestid));
1315 crp->crp_reqid = cnop[req].reqid;
1316 crp->crp_usropaque = cnop[req].opaque;
1317 cv_init(&crp->crp_cv, "crydev");
1318 #ifdef notyet
1319 eagain:
1320 #endif
1321 cnop[req].status = crypto_dispatch(crp);
1322 mutex_enter(&cryptodev_mtx); /* XXX why mutex? */
1323
1324 switch (cnop[req].status) {
1325 #ifdef notyet /* don't loop forever -- but EAGAIN not possible here yet */
1326 case EAGAIN:
1327 mutex_exit(&cryptodev_mtx);
1328 goto eagain;
1329 break;
1330 #endif
1331 case 0:
1332 break;
1333 default:
1334 DPRINTF("not waiting, error.\n");
1335 mutex_exit(&cryptodev_mtx);
1336 cv_destroy(&crp->crp_cv);
1337 goto bail;
1338 }
1339
1340 mutex_exit(&cryptodev_mtx);
1341 cv_destroy(&crp->crp_cv);
1342 bail:
1343 if (cnop[req].status) {
1344 if (crp) {
1345 if (crp->uio.uio_iov[0].iov_base) {
1346 kmem_free(crp->uio.uio_iov[0].iov_base,
1347 crp->uio.uio_iov[0].iov_len);
1348 }
1349 crypto_freereq(crp);
1350 }
1351 error = 0;
1352 }
1353 }
1354 return error;
1355 }
1356
1357 static int
1358 cryptodev_mkey(struct fcrypt *fcr, struct crypt_n_kop *kop, int count)
1359 {
1360 struct cryptkop *krp = NULL;
1361 int error = EINVAL;
1362 int in, out, size, i, req;
1363
1364 for (req = 0; req < count; req++) {
1365 if (kop[req].crk_iparams + kop[req].crk_oparams > CRK_MAXPARAM)
1366 return EFBIG;
1367
1368 in = kop[req].crk_iparams;
1369 out = kop[req].crk_oparams;
1370 switch (kop[req].crk_op) {
1371 case CRK_MOD_EXP:
1372 if (in == 3 && out == 1)
1373 break;
1374 kop[req].crk_status = EINVAL;
1375 continue;
1376 case CRK_MOD_EXP_CRT:
1377 if (in == 6 && out == 1)
1378 break;
1379 kop[req].crk_status = EINVAL;
1380 continue;
1381 case CRK_DSA_SIGN:
1382 if (in == 5 && out == 2)
1383 break;
1384 kop[req].crk_status = EINVAL;
1385 continue;
1386 case CRK_DSA_VERIFY:
1387 if (in == 7 && out == 0)
1388 break;
1389 kop[req].crk_status = EINVAL;
1390 continue;
1391 case CRK_DH_COMPUTE_KEY:
1392 if (in == 3 && out == 1)
1393 break;
1394 kop[req].crk_status = EINVAL;
1395 continue;
1396 case CRK_MOD_ADD:
1397 if (in == 3 && out == 1)
1398 break;
1399 kop[req].crk_status = EINVAL;
1400 continue;
1401 case CRK_MOD_ADDINV:
1402 if (in == 2 && out == 1)
1403 break;
1404 kop[req].crk_status = EINVAL;
1405 continue;
1406 case CRK_MOD_SUB:
1407 if (in == 3 && out == 1)
1408 break;
1409 kop[req].crk_status = EINVAL;
1410 continue;
1411 case CRK_MOD_MULT:
1412 if (in == 3 && out == 1)
1413 break;
1414 kop[req].crk_status = EINVAL;
1415 continue;
1416 case CRK_MOD_MULTINV:
1417 if (in == 2 && out == 1)
1418 break;
1419 kop[req].crk_status = EINVAL;
1420 continue;
1421 case CRK_MOD:
1422 if (in == 2 && out == 1)
1423 break;
1424 kop[req].crk_status = EINVAL;
1425 continue;
1426 default:
1427 kop[req].crk_status = EINVAL;
1428 continue;
1429 }
1430
1431 krp = crypto_kgetreq(1, PR_WAITOK);
1432 if (krp == NULL) {
1433 /* limited by opencrypto.crypto_ret_kq.maxlen */
1434 continue;
1435 }
1436 (void)memset(krp, 0, sizeof *krp);
1437 cv_init(&krp->krp_cv, "crykdev");
1438 krp->krp_op = kop[req].crk_op;
1439 krp->krp_status = kop[req].crk_status;
1440 krp->krp_iparams = kop[req].crk_iparams;
1441 krp->krp_oparams = kop[req].crk_oparams;
1442 krp->krp_status = 0;
1443 krp->krp_callback = cryptodevkey_mcb;
1444 (void)memcpy(krp->crk_param, kop[req].crk_param,
1445 sizeof(kop[req].crk_param));
1446
1447 krp->krp_flags = 0;
1448
1449 for (i = 0; i < CRK_MAXPARAM; i++)
1450 krp->krp_param[i].crp_nbits =
1451 kop[req].crk_param[i].crp_nbits;
1452 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
1453 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1454 if (size == 0)
1455 continue;
1456 krp->krp_param[i].crp_p =
1457 kmem_alloc(size, KM_SLEEP);
1458 if (i >= krp->krp_iparams)
1459 continue;
1460 kop[req].crk_status =
1461 copyin(kop[req].crk_param[i].crp_p,
1462 krp->krp_param[i].crp_p, size);
1463 if (kop[req].crk_status)
1464 goto fail;
1465 }
1466 krp->fcrp = fcr;
1467
1468 kop[req].crk_reqid = atomic_inc_32_nv(&(fcr->requestid));
1469 krp->krp_reqid = kop[req].crk_reqid;
1470 krp->krp_usropaque = kop[req].crk_opaque;
1471
1472 kop[req].crk_status = crypto_kdispatch(krp);
1473 if (kop[req].crk_status != 0) {
1474 goto fail;
1475 }
1476
1477 fail:
1478 if(kop[req].crk_status) {
1479 if (krp) {
1480 kop[req].crk_status = krp->krp_status;
1481 for (i = 0; i < CRK_MAXPARAM; i++) {
1482 struct crparam *kp =
1483 &(krp->krp_param[i]);
1484 if (kp->crp_p) {
1485 size = (kp->crp_nbits + 7) / 8;
1486 KASSERT(size > 0);
1487 memset(kp->crp_p, 0, size);
1488 kmem_free(kp->crp_p, size);
1489 }
1490 }
1491 cv_destroy(&krp->krp_cv);
1492 crypto_kfreereq(krp);
1493 }
1494 }
1495 error = 0;
1496 }
1497 DPRINTF("error=0x%08x\n", error);
1498 return error;
1499 }
1500
1501 int
1502 cryptodev_session(struct fcrypt *fcr, struct session_op *sop)
1503 {
1504 struct cryptoini cria, crie;
1505 struct cryptoini cric; /* compressor */
1506 struct cryptoini *crihead = NULL;
1507 const struct enc_xform *txform = NULL;
1508 const struct auth_hash *thash = NULL;
1509 const struct comp_algo *tcomp = NULL;
1510 struct csession *cse;
1511 u_int64_t sid;
1512 int error = 0;
1513
1514 DPRINTF("cipher=%d, mac=%d\n", sop->cipher, sop->mac);
1515
1516 /* XXX there must be a way to not embed the list of xforms here */
1517 switch (sop->cipher) {
1518 case 0:
1519 break;
1520 case CRYPTO_DES_CBC:
1521 txform = &enc_xform_des;
1522 break;
1523 case CRYPTO_3DES_CBC:
1524 txform = &enc_xform_3des;
1525 break;
1526 case CRYPTO_BLF_CBC:
1527 txform = &enc_xform_blf;
1528 break;
1529 case CRYPTO_CAST_CBC:
1530 txform = &enc_xform_cast5;
1531 break;
1532 case CRYPTO_SKIPJACK_CBC:
1533 txform = &enc_xform_skipjack;
1534 break;
1535 case CRYPTO_AES_CBC:
1536 txform = &enc_xform_aes;
1537 break;
1538 case CRYPTO_CAMELLIA_CBC:
1539 txform = &enc_xform_camellia;
1540 break;
1541 case CRYPTO_AES_CTR:
1542 txform = &enc_xform_aes_ctr;
1543 break;
1544 case CRYPTO_AES_GCM_16:
1545 txform = &enc_xform_aes_gcm;
1546 break;
1547 case CRYPTO_AES_GMAC:
1548 txform = &enc_xform_aes_gmac;
1549 break;
1550 case CRYPTO_NULL_CBC:
1551 txform = &enc_xform_null;
1552 break;
1553 case CRYPTO_ARC4:
1554 txform = &enc_xform_arc4;
1555 break;
1556 default:
1557 DPRINTF("Invalid cipher %d\n", sop->cipher);
1558 return EINVAL;
1559 }
1560
1561 switch (sop->comp_alg) {
1562 case 0:
1563 break;
1564 case CRYPTO_DEFLATE_COMP:
1565 tcomp = &comp_algo_deflate;
1566 break;
1567 case CRYPTO_GZIP_COMP:
1568 tcomp = &comp_algo_gzip;
1569 DPRINTF("tcomp for GZIP\n");
1570 break;
1571 default:
1572 DPRINTF("Invalid compression alg %d\n", sop->comp_alg);
1573 return EINVAL;
1574 }
1575
1576 switch (sop->mac) {
1577 case 0:
1578 break;
1579 case CRYPTO_MD5_HMAC:
1580 thash = &auth_hash_hmac_md5;
1581 break;
1582 case CRYPTO_SHA1_HMAC:
1583 thash = &auth_hash_hmac_sha1;
1584 break;
1585 case CRYPTO_MD5_HMAC_96:
1586 thash = &auth_hash_hmac_md5_96;
1587 break;
1588 case CRYPTO_SHA1_HMAC_96:
1589 thash = &auth_hash_hmac_sha1_96;
1590 break;
1591 case CRYPTO_SHA2_HMAC:
1592 /* XXX switching on key length seems questionable */
1593 if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize) {
1594 thash = &auth_hash_hmac_sha2_256;
1595 } else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize) {
1596 thash = &auth_hash_hmac_sha2_384;
1597 } else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize) {
1598 thash = &auth_hash_hmac_sha2_512;
1599 } else {
1600 DPRINTF("Invalid mackeylen %d\n", sop->mackeylen);
1601 return EINVAL;
1602 }
1603 break;
1604 case CRYPTO_SHA2_384_HMAC:
1605 thash = &auth_hash_hmac_sha2_384;
1606 break;
1607 case CRYPTO_SHA2_512_HMAC:
1608 thash = &auth_hash_hmac_sha2_512;
1609 break;
1610 case CRYPTO_RIPEMD160_HMAC:
1611 thash = &auth_hash_hmac_ripemd_160;
1612 break;
1613 case CRYPTO_RIPEMD160_HMAC_96:
1614 thash = &auth_hash_hmac_ripemd_160_96;
1615 break;
1616 case CRYPTO_MD5:
1617 thash = &auth_hash_md5;
1618 break;
1619 case CRYPTO_SHA1:
1620 thash = &auth_hash_sha1;
1621 break;
1622 case CRYPTO_AES_XCBC_MAC_96:
1623 thash = &auth_hash_aes_xcbc_mac_96;
1624 break;
1625 case CRYPTO_AES_128_GMAC:
1626 thash = &auth_hash_gmac_aes_128;
1627 break;
1628 case CRYPTO_AES_192_GMAC:
1629 thash = &auth_hash_gmac_aes_192;
1630 break;
1631 case CRYPTO_AES_256_GMAC:
1632 thash = &auth_hash_gmac_aes_256;
1633 break;
1634 case CRYPTO_NULL_HMAC:
1635 thash = &auth_hash_null;
1636 break;
1637 default:
1638 DPRINTF("Invalid mac %d\n", sop->mac);
1639 return EINVAL;
1640 }
1641
1642 memset(&crie, 0, sizeof(crie));
1643 memset(&cria, 0, sizeof(cria));
1644 memset(&cric, 0, sizeof(cric));
1645
1646 if (tcomp) {
1647 cric.cri_alg = tcomp->type;
1648 cric.cri_klen = 0;
1649 DPRINTF("tcomp->type = %d\n", tcomp->type);
1650
1651 crihead = &cric;
1652 if (txform) {
1653 cric.cri_next = &crie;
1654 } else if (thash) {
1655 cric.cri_next = &cria;
1656 }
1657 }
1658
1659 if (txform) {
1660 crie.cri_alg = txform->type;
1661 crie.cri_klen = sop->keylen * 8;
1662 if (sop->keylen > txform->maxkey ||
1663 sop->keylen < txform->minkey) {
1664 DPRINTF("keylen %d not in [%d,%d]\n",
1665 sop->keylen, txform->minkey, txform->maxkey);
1666 error = EINVAL;
1667 goto bail;
1668 }
1669
1670 crie.cri_key = malloc(crie.cri_klen / 8, M_XDATA, M_WAITOK);
1671 if ((error = copyin(sop->key, crie.cri_key, crie.cri_klen / 8)))
1672 goto bail;
1673 if (!crihead) {
1674 crihead = &crie;
1675 }
1676 if (thash)
1677 crie.cri_next = &cria;
1678 }
1679
1680 if (thash) {
1681 cria.cri_alg = thash->type;
1682 cria.cri_klen = sop->mackeylen * 8;
1683 if (sop->mackeylen != thash->keysize) {
1684 DPRINTF("mackeylen %d != keysize %d\n",
1685 sop->mackeylen, thash->keysize);
1686 error = EINVAL;
1687 goto bail;
1688 }
1689 if (cria.cri_klen) {
1690 cria.cri_key = malloc(cria.cri_klen / 8, M_XDATA,
1691 M_WAITOK);
1692 if ((error = copyin(sop->mackey, cria.cri_key,
1693 cria.cri_klen / 8))) {
1694 goto bail;
1695 }
1696 }
1697 if (!crihead) {
1698 crihead = &cria;
1699 }
1700 }
1701
1702 error = crypto_newsession(&sid, crihead, crypto_devallowsoft);
1703 if (!error) {
1704 DPRINTF("got session %d\n", (uint32_t)sid);
1705 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
1706 cria.cri_key, cria.cri_klen, (txform ? sop->cipher : 0), sop->mac,
1707 (tcomp ? sop->comp_alg : 0), txform, thash, tcomp);
1708 if (cse != NULL) {
1709 sop->ses = cse->ses;
1710 } else {
1711 DPRINTF("csecreate failed\n");
1712 crypto_freesession(sid);
1713 error = EINVAL;
1714 }
1715 } else {
1716 DPRINTF("SIOCSESSION violates kernel parameters %d\n", error);
1717 }
1718 bail:
1719 if (error) {
1720 if (crie.cri_key) {
1721 memset(crie.cri_key, 0, crie.cri_klen / 8);
1722 free(crie.cri_key, M_XDATA);
1723 }
1724 if (cria.cri_key) {
1725 memset(cria.cri_key, 0, cria.cri_klen / 8);
1726 free(cria.cri_key, M_XDATA);
1727 }
1728 }
1729 return error;
1730 }
1731
1732 int
1733 cryptodev_msession(struct fcrypt *fcr, struct session_n_op *sn_ops,
1734 int count)
1735 {
1736 int i;
1737
1738 for (i = 0; i < count; i++, sn_ops++) {
1739 struct session_op s_op;
1740 s_op.cipher = sn_ops->cipher;
1741 s_op.mac = sn_ops->mac;
1742 s_op.comp_alg = sn_ops->comp_alg;
1743 s_op.keylen = sn_ops->keylen;
1744 s_op.key = sn_ops->key;
1745 s_op.mackeylen = sn_ops->mackeylen;
1746 s_op.mackey = sn_ops->mackey;
1747 s_op.ses = ~0;
1748
1749 sn_ops->status = cryptodev_session(fcr, &s_op);
1750
1751 sn_ops->ses = s_op.ses;
1752 }
1753
1754 return 0;
1755 }
1756
1757 static void
1758 cryptodev_msessionfin(struct fcrypt *fcr, int count, u_int32_t *sesid)
1759 {
1760 struct csession *cse;
1761 int req;
1762
1763 mutex_enter(&cryptodev_mtx);
1764 for(req = 0; req < count; req++) {
1765 cse = csefind(fcr, sesid[req]);
1766 if (cse == NULL)
1767 continue;
1768 csedelete(fcr, cse);
1769 mutex_exit(&cryptodev_mtx);
1770 csefree(cse);
1771 mutex_enter(&cryptodev_mtx);
1772 }
1773 mutex_exit(&cryptodev_mtx);
1774 }
1775
1776 /*
1777 * collect as many completed requests as are availble, or count completed
1778 * requests whichever is less.
1779 * return the number of requests.
1780 */
1781 static int
1782 cryptodev_getmstatus(struct fcrypt *fcr, struct crypt_result *crypt_res,
1783 int count)
1784 {
1785 struct cryptop *crp = NULL;
1786 struct cryptkop *krp = NULL;
1787 struct csession *cse;
1788 int i, size, req = 0;
1789 int completed=0;
1790
1791 /* On queue so nobody else can grab them
1792 * and copyout can be delayed-- no locking */
1793 TAILQ_HEAD(, cryptop) crp_delfree_q =
1794 TAILQ_HEAD_INITIALIZER(crp_delfree_q);
1795 TAILQ_HEAD(, cryptkop) krp_delfree_q =
1796 TAILQ_HEAD_INITIALIZER(krp_delfree_q);
1797
1798 /* at this point we do not know which response user is requesting for
1799 * (symmetric or asymmetric) so we copyout one from each i.e if the
1800 * count is 2 then 1 from symmetric and 1 from asymmetric queue and
1801 * if 3 then 2 symmetric and 1 asymmetric and so on */
1802
1803 /* pull off a list of requests while protected from changes */
1804 mutex_enter(&cryptodev_mtx);
1805 while (req < count) {
1806 crp = TAILQ_FIRST(&fcr->crp_ret_mq);
1807 if (crp) {
1808 TAILQ_REMOVE(&fcr->crp_ret_mq, crp, crp_next);
1809 TAILQ_INSERT_TAIL(&crp_delfree_q, crp, crp_next);
1810 cse = (struct csession *)crp->crp_opaque;
1811
1812 /* see if the session is still valid */
1813 cse = csefind(fcr, cse->ses);
1814 if (cse != NULL) {
1815 crypt_res[req].status = 0;
1816 } else {
1817 DPRINTF("csefind failed\n");
1818 crypt_res[req].status = EINVAL;
1819 }
1820 req++;
1821 }
1822 if(req < count) {
1823 crypt_res[req].status = 0;
1824 krp = TAILQ_FIRST(&fcr->crp_ret_mkq);
1825 if (krp) {
1826 TAILQ_REMOVE(&fcr->crp_ret_mkq, krp, krp_next);
1827 TAILQ_INSERT_TAIL(&krp_delfree_q, krp, krp_next);
1828 req++;
1829 }
1830 }
1831 }
1832 mutex_exit(&cryptodev_mtx);
1833
1834 /* now do all the work outside the mutex */
1835 for(req=0; req < count ;) {
1836 crp = TAILQ_FIRST(&crp_delfree_q);
1837 if (crp) {
1838 if (crypt_res[req].status != 0) {
1839 /* csefind failed during collection */
1840 goto bail;
1841 }
1842 cse = (struct csession *)crp->crp_opaque;
1843 crypt_res[req].reqid = crp->crp_reqid;
1844 crypt_res[req].opaque = crp->crp_usropaque;
1845 completed++;
1846
1847 if (crp->crp_etype != 0) {
1848 crypt_res[req].status = crp->crp_etype;
1849 goto bail;
1850 }
1851
1852 if (cse->error) {
1853 crypt_res[req].status = cse->error;
1854 goto bail;
1855 }
1856
1857 if (crp->dst && (crypt_res[req].status =
1858 copyout(crp->uio.uio_iov[0].iov_base, crp->dst,
1859 crp->len)))
1860 goto bail;
1861
1862 if (crp->mac && (crypt_res[req].status =
1863 copyout(crp->crp_mac, crp->mac,
1864 cse->thash->authsize)))
1865 goto bail;
1866
1867 bail:
1868 TAILQ_REMOVE(&crp_delfree_q, crp, crp_next);
1869 kmem_free(crp->uio.uio_iov[0].iov_base,
1870 crp->uio.uio_iov[0].iov_len);
1871 crypto_freereq(crp);
1872 req++;
1873 }
1874
1875 if (req < count) {
1876 krp = TAILQ_FIRST(&krp_delfree_q);
1877 if (krp) {
1878 crypt_res[req].reqid = krp->krp_reqid;
1879 crypt_res[req].opaque = krp->krp_usropaque;
1880 completed++;
1881 if (krp->krp_status != 0) {
1882 DPRINTF("krp->krp_status 0x%08x\n",
1883 krp->krp_status);
1884 crypt_res[req].status = krp->krp_status;
1885 goto fail;
1886 }
1887
1888 for (i = krp->krp_iparams; i < krp->krp_iparams
1889 + krp->krp_oparams; i++) {
1890 size = (krp->krp_param[i].crp_nbits
1891 + 7) / 8;
1892 if (size == 0)
1893 continue;
1894 crypt_res[req].status = copyout
1895 (krp->krp_param[i].crp_p,
1896 krp->crk_param[i].crp_p, size);
1897 if (crypt_res[req].status) {
1898 DPRINTF("copyout oparam %d failed, "
1899 "error=%d\n",
1900 i - krp->krp_iparams,
1901 crypt_res[req].status);
1902 goto fail;
1903 }
1904 }
1905 fail:
1906 TAILQ_REMOVE(&krp_delfree_q, krp, krp_next);
1907 /* not sure what to do for this */
1908 /* kop[req].crk_status = krp->krp_status; */
1909 for (i = 0; i < CRK_MAXPARAM; i++) {
1910 struct crparam *kp = &(krp->krp_param[i]);
1911 if (kp->crp_p) {
1912 size = (kp->crp_nbits + 7) / 8;
1913 KASSERT(size > 0);
1914 (void)memset(kp->crp_p, 0, size);
1915 kmem_free(kp->crp_p, size);
1916 }
1917 }
1918 cv_destroy(&krp->krp_cv);
1919 crypto_kfreereq(krp);
1920 req++;
1921 }
1922 }
1923 }
1924
1925 return completed;
1926 }
1927
1928 static int
1929 cryptodev_getstatus (struct fcrypt *fcr, struct crypt_result *crypt_res)
1930 {
1931 struct cryptop *crp = NULL, *cnext;
1932 struct cryptkop *krp = NULL, *knext;
1933 struct csession *cse;
1934 int i, size, req = 0;
1935
1936 mutex_enter(&cryptodev_mtx);
1937 /* Here we dont know for which request the user is requesting the
1938 * response so checking in both the queues */
1939 TAILQ_FOREACH_SAFE(crp, &fcr->crp_ret_mq, crp_next, cnext) {
1940 if(crp && (crp->crp_reqid == crypt_res->reqid)) {
1941 cse = (struct csession *)crp->crp_opaque;
1942 crypt_res->opaque = crp->crp_usropaque;
1943 cse = csefind(fcr, cse->ses);
1944 if (cse == NULL) {
1945 DPRINTF("csefind failed\n");
1946 crypt_res->status = EINVAL;
1947 goto bail;
1948 }
1949
1950 if (crp->crp_etype != 0) {
1951 crypt_res->status = crp->crp_etype;
1952 goto bail;
1953 }
1954
1955 if (cse->error) {
1956 crypt_res->status = cse->error;
1957 goto bail;
1958 }
1959
1960 if (crp->dst && (crypt_res->status =
1961 copyout(crp->uio.uio_iov[0].iov_base,
1962 crp->dst, crp->len)))
1963 goto bail;
1964
1965 if (crp->mac && (crypt_res->status =
1966 copyout(crp->crp_mac, crp->mac,
1967 cse->thash->authsize)))
1968 goto bail;
1969 bail:
1970 TAILQ_REMOVE(&fcr->crp_ret_mq, crp, crp_next);
1971
1972 mutex_exit(&cryptodev_mtx);
1973 crypto_freereq(crp);
1974 return 0;
1975 }
1976 }
1977
1978 TAILQ_FOREACH_SAFE(krp, &fcr->crp_ret_mkq, krp_next, knext) {
1979 if(krp && (krp->krp_reqid == crypt_res->reqid)) {
1980 crypt_res[req].opaque = krp->krp_usropaque;
1981 if (krp->krp_status != 0) {
1982 DPRINTF("krp->krp_status 0x%08x\n",
1983 krp->krp_status);
1984 crypt_res[req].status = krp->krp_status;
1985 goto fail;
1986 }
1987
1988 for (i = krp->krp_iparams; i < krp->krp_iparams +
1989 krp->krp_oparams; i++) {
1990 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1991 if (size == 0)
1992 continue;
1993 crypt_res[req].status = copyout(
1994 krp->krp_param[i].crp_p,
1995 krp->crk_param[i].crp_p, size);
1996 if (crypt_res[req].status) {
1997 DPRINTF("copyout oparam "
1998 "%d failed, error=%d\n",
1999 i - krp->krp_iparams,
2000 crypt_res[req].status);
2001 goto fail;
2002 }
2003 }
2004 fail:
2005 TAILQ_REMOVE(&fcr->crp_ret_mkq, krp, krp_next);
2006 mutex_exit(&cryptodev_mtx);
2007 /* not sure what to do for this */
2008 /* kop[req].crk_status = krp->krp_status; */
2009 for (i = 0; i < CRK_MAXPARAM; i++) {
2010 struct crparam *kp = &(krp->krp_param[i]);
2011 if (kp->crp_p) {
2012 size = (kp->crp_nbits + 7) / 8;
2013 KASSERT(size > 0);
2014 memset(kp->crp_p, 0, size);
2015 kmem_free(kp->crp_p, size);
2016 }
2017 }
2018 cv_destroy(&krp->krp_cv);
2019 crypto_kfreereq(krp);
2020 return 0;
2021 }
2022 }
2023 mutex_exit(&cryptodev_mtx);
2024 return EINPROGRESS;
2025 }
2026
2027 static int
2028 cryptof_stat(struct file *fp, struct stat *st)
2029 {
2030 struct fcrypt *fcr = fp->f_fcrypt;
2031
2032 (void)memset(st, 0, sizeof(*st));
2033
2034 mutex_enter(&cryptodev_mtx);
2035 st->st_dev = makedev(cdevsw_lookup_major(&crypto_cdevsw), fcr->sesn);
2036 st->st_atimespec = fcr->atime;
2037 st->st_mtimespec = fcr->mtime;
2038 st->st_ctimespec = st->st_birthtimespec = fcr->btime;
2039 st->st_uid = kauth_cred_geteuid(fp->f_cred);
2040 st->st_gid = kauth_cred_getegid(fp->f_cred);
2041 mutex_exit(&cryptodev_mtx);
2042
2043 return 0;
2044 }
2045
2046 static int
2047 cryptof_poll(struct file *fp, int events)
2048 {
2049 struct fcrypt *fcr = fp->f_fcrypt;
2050 int revents = 0;
2051
2052 if (!(events & (POLLIN | POLLRDNORM))) {
2053 /* only support read and POLLIN */
2054 return 0;
2055 }
2056
2057 mutex_enter(&cryptodev_mtx);
2058 if (TAILQ_EMPTY(&fcr->crp_ret_mq) && TAILQ_EMPTY(&fcr->crp_ret_mkq)) {
2059 /* no completed requests pending, save the poll for later */
2060 selrecord(curlwp, &fcr->sinfo);
2061 } else {
2062 /* let the app(s) know that there are completed requests */
2063 revents = events & (POLLIN | POLLRDNORM);
2064 }
2065 mutex_exit(&cryptodev_mtx);
2066
2067 return revents;
2068 }
2069
2070 /*
2071 * Pseudo-device initialization routine for /dev/crypto
2072 */
2073 void
2074 cryptoattach(int num)
2075 {
2076
2077 crypto_init();
2078
2079 mutex_init(&cryptodev_mtx, MUTEX_DEFAULT, IPL_NONE);
2080
2081 pool_init(&fcrpl, sizeof(struct fcrypt), 0, 0, 0, "fcrpl",
2082 NULL, IPL_NONE);
2083 pool_init(&csepl, sizeof(struct csession), 0, 0, 0, "csepl",
2084 NULL, IPL_NONE);
2085
2086 /*
2087 * Preallocate space for 64 users, with 5 sessions each.
2088 * (consider that a TLS protocol session requires at least
2089 * 3DES, MD5, and SHA1 (both hashes are used in the PRF) for
2090 * the negotiation, plus HMAC_SHA1 for the actual SSL records,
2091 * consuming one session here for each algorithm.
2092 */
2093 pool_prime(&fcrpl, 64);
2094 pool_prime(&csepl, 64 * 5);
2095 }
2096
2097 void crypto_attach(device_t, device_t, void *);
2098
2099 void
2100 crypto_attach(device_t parent, device_t self, void * opaque)
2101 {
2102
2103 cryptoattach(0);
2104 }
2105
2106 int crypto_detach(device_t, int);
2107
2108 int
2109 crypto_detach(device_t self, int num)
2110 {
2111
2112 pool_destroy(&fcrpl);
2113 pool_destroy(&csepl);
2114
2115 mutex_destroy(&cryptodev_mtx);
2116
2117 return 0;
2118 }
2119
2120 int crypto_match(device_t, cfdata_t, void *);
2121
2122 int
2123 crypto_match(device_t parent, cfdata_t data, void *opaque)
2124 {
2125
2126 return 1;
2127 }
2128
2129 MODULE(MODULE_CLASS_DRIVER, crypto, "opencrypto");
2130
2131 CFDRIVER_DECL(crypto, DV_DULL, NULL);
2132
2133 CFATTACH_DECL2_NEW(crypto, 0, crypto_match, crypto_attach, crypto_detach,
2134 NULL, NULL, NULL);
2135
2136 #ifdef _MODULE
2137 static int cryptoloc[] = { -1, -1 };
2138
2139 static struct cfdata crypto_cfdata[] = {
2140 {
2141 .cf_name = "crypto",
2142 .cf_atname = "crypto",
2143 .cf_unit = 0,
2144 .cf_fstate = 0,
2145 .cf_loc = cryptoloc,
2146 .cf_flags = 0,
2147 .cf_pspec = NULL,
2148 },
2149 { NULL, NULL, 0, 0, NULL, 0, NULL }
2150 };
2151 #endif
2152
2153 static int
2154 crypto_modcmd(modcmd_t cmd, void *arg)
2155 {
2156 int error = 0;
2157 #ifdef _MODULE
2158 devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
2159 #endif
2160
2161 switch (cmd) {
2162 case MODULE_CMD_INIT:
2163 #ifdef _MODULE
2164
2165 error = devsw_attach(crypto_cd.cd_name, NULL, &bmajor,
2166 &crypto_cdevsw, &cmajor);
2167 if (error) {
2168 aprint_error("%s: unable to register devsw, error %d\n",
2169 crypto_cd.cd_name, error);
2170 return error;
2171 }
2172
2173 error = config_cfdriver_attach(&crypto_cd);
2174 if (error) {
2175 devsw_detach(NULL, &crypto_cdevsw);
2176 return error;
2177 }
2178
2179 error = config_cfattach_attach(crypto_cd.cd_name, &crypto_ca);
2180 if (error) {
2181 config_cfdriver_detach(&crypto_cd);
2182 devsw_detach(NULL, &crypto_cdevsw);
2183 aprint_error("%s: unable to register cfattach\n",
2184 crypto_cd.cd_name);
2185
2186 return error;
2187 }
2188
2189 error = config_cfdata_attach(crypto_cfdata, 1);
2190 if (error) {
2191 config_cfattach_detach(crypto_cd.cd_name, &crypto_ca);
2192 config_cfdriver_detach(&crypto_cd);
2193 devsw_detach(NULL, &crypto_cdevsw);
2194 aprint_error("%s: unable to register cfdata\n",
2195 crypto_cd.cd_name);
2196
2197 return error;
2198 }
2199
2200 (void)config_attach_pseudo(crypto_cfdata);
2201 #endif
2202
2203 return error;
2204 case MODULE_CMD_FINI:
2205 #ifdef _MODULE
2206 if (crypto_refcount != 0)
2207 return EBUSY;
2208 error = config_cfdata_detach(crypto_cfdata);
2209 if (error) {
2210 return error;
2211 }
2212
2213 config_cfattach_detach(crypto_cd.cd_name, &crypto_ca);
2214 config_cfdriver_detach(&crypto_cd);
2215 devsw_detach(NULL, &crypto_cdevsw);
2216 #endif
2217
2218 return error;
2219 #ifdef _MODULE
2220 case MODULE_CMD_AUTOUNLOAD:
2221 #if 0 /*
2222 * XXX Completely disable auto-unload for now, since there is still
2223 * XXX a (small) window where in-module ref-counting doesn't help
2224 */
2225 if (crypto_refcount != 0)
2226 #endif
2227 return EBUSY;
2228 /* FALLTHROUGH */
2229 #endif
2230 default:
2231 return ENOTTY;
2232 }
2233 }
2234