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