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