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