cryptodev.c revision 1.92 1 /* $NetBSD: cryptodev.c,v 1.92 2017/06/02 09:46:57 knakahara 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.92 2017/06/02 09:46:57 knakahara 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
90 #ifdef _KERNEL_OPT
91 #include "opt_ocf.h"
92 #include "opt_compat_netbsd.h"
93 #endif
94
95 #include <opencrypto/cryptodev.h>
96 #include <opencrypto/cryptodev_internal.h>
97 #include <opencrypto/xform.h>
98
99 #include "ioconf.h"
100
101 kmutex_t cryptodev_mtx;
102
103 struct csession {
104 TAILQ_ENTRY(csession) next;
105 u_int64_t sid;
106 u_int32_t ses;
107
108 u_int32_t cipher; /* note: shares name space in crd_alg */
109 const struct enc_xform *txform;
110 u_int32_t mac; /* note: shares name space in crd_alg */
111 const struct auth_hash *thash;
112 u_int32_t comp_alg; /* note: shares name space in crd_alg */
113 const struct comp_algo *tcomp;
114
115 void * key;
116 int keylen;
117 u_char tmp_iv[EALG_MAX_BLOCK_LEN];
118
119 void * mackey;
120 int mackeylen;
121 u_char tmp_mac[CRYPTO_MAX_MAC_LEN];
122
123 struct iovec iovec[1]; /* user requests never have more */
124 struct uio uio;
125 int error;
126 };
127
128 struct fcrypt {
129 TAILQ_HEAD(csessionlist, csession) csessions;
130 TAILQ_HEAD(crprethead, cryptop) crp_ret_mq;
131 TAILQ_HEAD(krprethead, cryptkop) crp_ret_mkq;
132 int sesn;
133 struct selinfo sinfo;
134 u_int32_t requestid;
135 struct timespec atime;
136 struct timespec mtime;
137 struct timespec btime;
138 };
139
140 /* For our fixed-size allocations */
141 static struct pool fcrpl;
142 static struct pool csepl;
143
144 /* Declaration of master device (fd-cloning/ctxt-allocating) entrypoints */
145 static int cryptoopen(dev_t dev, int flag, int mode, struct lwp *l);
146 static int cryptoread(dev_t dev, struct uio *uio, int ioflag);
147 static int cryptowrite(dev_t dev, struct uio *uio, int ioflag);
148 static int cryptoselect(dev_t dev, int rw, struct lwp *l);
149
150 static int crypto_refcount = 0; /* Prevent detaching while in use */
151
152 /* Declaration of cloned-device (per-ctxt) entrypoints */
153 static int cryptof_read(struct file *, off_t *, struct uio *,
154 kauth_cred_t, int);
155 static int cryptof_write(struct file *, off_t *, struct uio *,
156 kauth_cred_t, int);
157 static int cryptof_ioctl(struct file *, u_long, void *);
158 static int cryptof_close(struct file *);
159 static int cryptof_poll(struct file *, int);
160 static int cryptof_stat(struct file *, struct stat *);
161
162 static const struct fileops cryptofops = {
163 .fo_read = cryptof_read,
164 .fo_write = cryptof_write,
165 .fo_ioctl = cryptof_ioctl,
166 .fo_fcntl = fnullop_fcntl,
167 .fo_poll = cryptof_poll,
168 .fo_stat = cryptof_stat,
169 .fo_close = cryptof_close,
170 .fo_kqfilter = fnullop_kqfilter,
171 .fo_restart = fnullop_restart,
172 };
173
174 struct csession *cryptodev_csefind(struct fcrypt *, u_int);
175 static struct csession *csefind(struct fcrypt *, u_int);
176 static int csedelete(struct fcrypt *, struct csession *);
177 static struct csession *cseadd(struct fcrypt *, struct csession *);
178 static struct csession *csecreate(struct fcrypt *, u_int64_t, void *,
179 u_int64_t, void *, u_int64_t, u_int32_t, u_int32_t, u_int32_t,
180 const struct enc_xform *, const struct auth_hash *,
181 const struct comp_algo *);
182 static int csefree(struct csession *);
183
184 static int cryptodev_key(struct crypt_kop *);
185 static int cryptodev_mkey(struct fcrypt *, struct crypt_n_kop *, int);
186 static int cryptodev_msessionfin(struct fcrypt *, int, u_int32_t *);
187
188 static int cryptodev_cb(void *);
189 static int cryptodevkey_cb(void *);
190
191 static int cryptodev_mcb(void *);
192 static int cryptodevkey_mcb(void *);
193
194 static int cryptodev_getmstatus(struct fcrypt *, struct crypt_result *,
195 int);
196 static int cryptodev_getstatus(struct fcrypt *, struct crypt_result *);
197
198 #ifdef COMPAT_50
199 extern int ocryptof_ioctl(struct file *, u_long, void *);
200 #endif
201
202 /*
203 * sysctl-able control variables for /dev/crypto now defined in crypto.c:
204 * crypto_usercrypto, crypto_userasmcrypto, crypto_devallowsoft.
205 */
206
207 /* ARGSUSED */
208 int
209 cryptof_read(file_t *fp, off_t *poff,
210 struct uio *uio, kauth_cred_t cred, int flags)
211 {
212 return EIO;
213 }
214
215 /* ARGSUSED */
216 int
217 cryptof_write(file_t *fp, off_t *poff,
218 struct uio *uio, kauth_cred_t cred, int flags)
219 {
220 return EIO;
221 }
222
223 /* ARGSUSED */
224 int
225 cryptof_ioctl(struct file *fp, u_long cmd, void *data)
226 {
227 struct fcrypt *fcr = fp->f_fcrypt;
228 struct csession *cse;
229 struct session_op *sop;
230 struct session_n_op *snop;
231 struct crypt_op *cop;
232 struct crypt_mop *mop;
233 struct crypt_mkop *mkop;
234 struct crypt_n_op *cnop;
235 struct crypt_n_kop *knop;
236 struct crypt_sgop *sgop;
237 struct crypt_sfop *sfop;
238 struct cryptret *crypt_ret;
239 struct crypt_result *crypt_res;
240 u_int32_t ses;
241 u_int32_t *sesid;
242 int error = 0;
243 size_t count;
244
245 /* backwards compatibility */
246 file_t *criofp;
247 struct fcrypt *criofcr;
248 int criofd;
249
250 mutex_enter(&cryptodev_mtx);
251 getnanotime(&fcr->atime);
252 mutex_exit(&cryptodev_mtx);
253
254 switch (cmd) {
255 case CRIOGET: /* XXX deprecated, remove after 5.0 */
256 if ((error = fd_allocfile(&criofp, &criofd)) != 0)
257 return error;
258 criofcr = pool_get(&fcrpl, PR_WAITOK);
259 mutex_enter(&cryptodev_mtx);
260 TAILQ_INIT(&criofcr->csessions);
261 TAILQ_INIT(&criofcr->crp_ret_mq);
262 TAILQ_INIT(&criofcr->crp_ret_mkq);
263 selinit(&criofcr->sinfo);
264
265 /*
266 * Don't ever return session 0, to allow detection of
267 * failed creation attempts with multi-create ioctl.
268 */
269 criofcr->sesn = 1;
270 criofcr->requestid = 1;
271 crypto_refcount++;
272 mutex_exit(&cryptodev_mtx);
273 (void)fd_clone(criofp, criofd, (FREAD|FWRITE),
274 &cryptofops, criofcr);
275 *(u_int32_t *)data = criofd;
276 return error;
277 break;
278 case CIOCGSESSION:
279 sop = (struct session_op *)data;
280 error = cryptodev_session(fcr, sop);
281 break;
282 case CIOCNGSESSION:
283 sgop = (struct crypt_sgop *)data;
284 snop = kmem_alloc((sgop->count *
285 sizeof(struct session_n_op)), KM_SLEEP);
286 error = copyin(sgop->sessions, snop, sgop->count *
287 sizeof(struct session_n_op));
288 if (error) {
289 goto mbail;
290 }
291
292 mutex_enter(&cryptodev_mtx);
293 fcr->mtime = fcr->atime;
294 mutex_exit(&cryptodev_mtx);
295 error = cryptodev_msession(fcr, snop, sgop->count);
296 if (error) {
297 goto mbail;
298 }
299
300 error = copyout(snop, sgop->sessions, sgop->count *
301 sizeof(struct session_n_op));
302 mbail:
303 kmem_free(snop, sgop->count * sizeof(struct session_n_op));
304 break;
305 case CIOCFSESSION:
306 mutex_enter(&cryptodev_mtx);
307 fcr->mtime = fcr->atime;
308 ses = *(u_int32_t *)data;
309 cse = csefind(fcr, ses);
310 if (cse == NULL) {
311 mutex_exit(&cryptodev_mtx);
312 return EINVAL;
313 }
314 csedelete(fcr, cse);
315 mutex_exit(&cryptodev_mtx);
316 error = csefree(cse);
317 break;
318 case CIOCNFSESSION:
319 mutex_enter(&cryptodev_mtx);
320 fcr->mtime = fcr->atime;
321 mutex_exit(&cryptodev_mtx);
322 sfop = (struct crypt_sfop *)data;
323 sesid = kmem_alloc((sfop->count * sizeof(u_int32_t)),
324 KM_SLEEP);
325 error = copyin(sfop->sesid, sesid,
326 (sfop->count * sizeof(u_int32_t)));
327 if (!error) {
328 error = cryptodev_msessionfin(fcr, sfop->count, sesid);
329 }
330 kmem_free(sesid, (sfop->count * sizeof(u_int32_t)));
331 break;
332 case CIOCCRYPT:
333 mutex_enter(&cryptodev_mtx);
334 fcr->mtime = fcr->atime;
335 cop = (struct crypt_op *)data;
336 cse = csefind(fcr, cop->ses);
337 mutex_exit(&cryptodev_mtx);
338 if (cse == NULL) {
339 DPRINTF("csefind failed\n");
340 return EINVAL;
341 }
342 error = cryptodev_op(cse, cop, curlwp);
343 DPRINTF("cryptodev_op error = %d\n", error);
344 break;
345 case CIOCNCRYPTM:
346 mutex_enter(&cryptodev_mtx);
347 fcr->mtime = fcr->atime;
348 mutex_exit(&cryptodev_mtx);
349 mop = (struct crypt_mop *)data;
350 cnop = kmem_alloc((mop->count * sizeof(struct crypt_n_op)),
351 KM_SLEEP);
352 error = copyin(mop->reqs, cnop,
353 (mop->count * sizeof(struct crypt_n_op)));
354 if(!error) {
355 error = cryptodev_mop(fcr, cnop, mop->count, curlwp);
356 if (!error) {
357 error = copyout(cnop, mop->reqs,
358 (mop->count * sizeof(struct crypt_n_op)));
359 }
360 }
361 kmem_free(cnop, (mop->count * sizeof(struct crypt_n_op)));
362 break;
363 case CIOCKEY:
364 error = cryptodev_key((struct crypt_kop *)data);
365 DPRINTF("cryptodev_key error = %d\n", error);
366 break;
367 case CIOCNFKEYM:
368 mutex_enter(&cryptodev_mtx);
369 fcr->mtime = fcr->atime;
370 mutex_exit(&cryptodev_mtx);
371 mkop = (struct crypt_mkop *)data;
372 knop = kmem_alloc((mkop->count * sizeof(struct crypt_n_kop)),
373 KM_SLEEP);
374 error = copyin(mkop->reqs, knop,
375 (mkop->count * sizeof(struct crypt_n_kop)));
376 if (!error) {
377 error = cryptodev_mkey(fcr, knop, mkop->count);
378 if (!error)
379 error = copyout(knop, mkop->reqs,
380 (mkop->count * sizeof(struct crypt_n_kop)));
381 }
382 kmem_free(knop, (mkop->count * sizeof(struct crypt_n_kop)));
383 break;
384 case CIOCASYMFEAT:
385 error = crypto_getfeat((int *)data);
386 break;
387 case CIOCNCRYPTRETM:
388 mutex_enter(&cryptodev_mtx);
389 fcr->mtime = fcr->atime;
390 mutex_exit(&cryptodev_mtx);
391 crypt_ret = (struct cryptret *)data;
392 count = crypt_ret->count;
393 crypt_res = kmem_alloc((count * sizeof(struct crypt_result)),
394 KM_SLEEP);
395 error = copyin(crypt_ret->results, crypt_res,
396 (count * sizeof(struct crypt_result)));
397 if (error)
398 goto reterr;
399 crypt_ret->count = cryptodev_getmstatus(fcr, crypt_res,
400 crypt_ret->count);
401 /* sanity check count */
402 if (crypt_ret->count > count) {
403 printf("%s.%d: error returned count %zd > original "
404 " count %zd\n",
405 __FILE__, __LINE__, crypt_ret->count, count);
406 crypt_ret->count = count;
407
408 }
409 error = copyout(crypt_res, crypt_ret->results,
410 (crypt_ret->count * sizeof(struct crypt_result)));
411 reterr:
412 kmem_free(crypt_res, (count * sizeof(struct crypt_result)));
413 break;
414 case CIOCNCRYPTRET:
415 error = cryptodev_getstatus(fcr, (struct crypt_result *)data);
416 break;
417 default:
418 #ifdef COMPAT_50
419 /* Check for backward compatible commands */
420 error = ocryptof_ioctl(fp, cmd, data);
421 #else
422 return EINVAL;
423 #endif
424 }
425 return error;
426 }
427
428 int
429 cryptodev_op(struct csession *cse, struct crypt_op *cop, struct lwp *l)
430 {
431 struct cryptop *crp = NULL;
432 struct cryptodesc *crde = NULL, *crda = NULL, *crdc = NULL;
433 int error;
434 int iov_len = cop->len;
435 int flags=0;
436 int dst_len; /* copyout size */
437
438 if (cop->len > 256*1024-4)
439 return E2BIG;
440
441 if (cse->txform) {
442 if (cop->len < cse->txform->blocksize
443 + (cop->iv ? 0 : cse->txform->ivsize) ||
444 (cop->len - (cop->iv ? 0 : cse->txform->ivsize))
445 % cse->txform->blocksize != 0)
446 return EINVAL;
447 }
448
449 DPRINTF("cryptodev_op[%u]: iov_len %d\n",
450 CRYPTO_SESID2LID(cse->sid), iov_len);
451 if ((cse->tcomp) && cop->dst_len) {
452 if (iov_len < cop->dst_len) {
453 /* Need larger iov to deal with decompress */
454 iov_len = cop->dst_len;
455 }
456 DPRINTF("cryptodev_op: iov_len -> %d for decompress\n", iov_len);
457 }
458
459 (void)memset(&cse->uio, 0, sizeof(cse->uio));
460 cse->uio.uio_iovcnt = 1;
461 cse->uio.uio_resid = 0;
462 cse->uio.uio_rw = UIO_WRITE;
463 cse->uio.uio_iov = cse->iovec;
464 UIO_SETUP_SYSSPACE(&cse->uio);
465 memset(&cse->iovec, 0, sizeof(cse->iovec));
466
467 /* the iov needs to be big enough to handle the uncompressed
468 * data.... */
469 cse->uio.uio_iov[0].iov_len = iov_len;
470 if (iov_len > 0)
471 cse->uio.uio_iov[0].iov_base = kmem_alloc(iov_len, KM_SLEEP);
472 cse->uio.uio_resid = cse->uio.uio_iov[0].iov_len;
473 DPRINTF("lid[%u]: uio.iov_base %p malloced %d bytes\n",
474 CRYPTO_SESID2LID(cse->sid),
475 cse->uio.uio_iov[0].iov_base, iov_len);
476
477 crp = crypto_getreq((cse->tcomp != NULL) + (cse->txform != NULL) + (cse->thash != NULL));
478 if (crp == NULL) {
479 error = ENOMEM;
480 goto bail;
481 }
482 DPRINTF("lid[%u]: crp %p\n", CRYPTO_SESID2LID(cse->sid), crp);
483
484 /* crds are always ordered tcomp, thash, then txform */
485 /* with optional missing links */
486
487 /* XXX: If we're going to compress then hash or encrypt, we need
488 * to be able to pass on the new size of the data.
489 */
490
491 if (cse->tcomp) {
492 crdc = crp->crp_desc;
493 }
494
495 if (cse->thash) {
496 crda = crdc ? crdc->crd_next : crp->crp_desc;
497 if (cse->txform && crda)
498 crde = crda->crd_next;
499 } else {
500 if (cse->txform) {
501 crde = crdc ? crdc->crd_next : crp->crp_desc;
502 } else if (!cse->tcomp) {
503 error = EINVAL;
504 goto bail;
505 }
506 }
507
508 DPRINTF("ocf[%u]: iov_len %zu, cop->len %u\n",
509 CRYPTO_SESID2LID(cse->sid),
510 cse->uio.uio_iov[0].iov_len,
511 cop->len);
512
513 if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
514 {
515 printf("copyin failed %s %d \n", (char *)cop->src, error);
516 goto bail;
517 }
518
519 if (crdc) {
520 switch (cop->op) {
521 case COP_COMP:
522 crdc->crd_flags |= CRD_F_COMP;
523 break;
524 case COP_DECOMP:
525 crdc->crd_flags &= ~CRD_F_COMP;
526 break;
527 default:
528 break;
529 }
530 /* more data to follow? */
531 if (cop->flags & COP_F_MORE) {
532 flags |= CRYPTO_F_MORE;
533 }
534 crdc->crd_len = cop->len;
535 crdc->crd_inject = 0;
536
537 crdc->crd_alg = cse->comp_alg;
538 crdc->crd_key = NULL;
539 crdc->crd_klen = 0;
540 DPRINTF("lid[%u]: crdc setup for comp_alg %d.\n",
541 CRYPTO_SESID2LID(cse->sid), crdc->crd_alg);
542 }
543
544 if (crda) {
545 crda->crd_skip = 0;
546 crda->crd_len = cop->len;
547 crda->crd_inject = 0; /* ??? */
548
549 crda->crd_alg = cse->mac;
550 crda->crd_key = cse->mackey;
551 crda->crd_klen = cse->mackeylen * 8;
552 DPRINTF("crda setup for mac %d.\n", crda->crd_alg);
553 }
554
555 if (crde) {
556 switch (cop->op) {
557 case COP_ENCRYPT:
558 crde->crd_flags |= CRD_F_ENCRYPT;
559 break;
560 case COP_DECRYPT:
561 crde->crd_flags &= ~CRD_F_ENCRYPT;
562 break;
563 default:
564 break;
565 }
566 crde->crd_len = cop->len;
567 crde->crd_inject = 0;
568
569 if (cse->cipher == CRYPTO_AES_GCM_16 && crda)
570 crda->crd_len = 0;
571 else if (cse->cipher == CRYPTO_AES_GMAC)
572 crde->crd_len = 0;
573
574 crde->crd_alg = cse->cipher;
575 crde->crd_key = cse->key;
576 crde->crd_klen = cse->keylen * 8;
577 DPRINTF("crde setup for cipher %d.\n", crde->crd_alg);
578 }
579
580
581 crp->crp_ilen = cop->len;
582 /*
583 * The request is flagged as CRYPTO_F_USER as long as it is running
584 * in the user IOCTL thread. However, whether the request completes
585 * immediately or belatedly is depends on the used encryption driver.
586 */
587 crp->crp_flags = CRYPTO_F_IOV | (cop->flags & COP_F_BATCH) | CRYPTO_F_USER |
588 flags;
589 crp->crp_buf = (void *)&cse->uio;
590 crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
591 crp->crp_sid = cse->sid;
592 crp->crp_opaque = (void *)cse;
593
594 if (cop->iv) {
595 if (crde == NULL) {
596 error = EINVAL;
597 goto bail;
598 }
599 if (cse->txform->ivsize == 0) {
600 error = EINVAL;
601 goto bail;
602 }
603 if ((error = copyin(cop->iv, cse->tmp_iv,
604 cse->txform->ivsize)))
605 goto bail;
606 (void)memcpy(crde->crd_iv, cse->tmp_iv, cse->txform->ivsize);
607 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
608 crde->crd_skip = 0;
609 } else if (crde) {
610 if (cse->txform->ivsize == 0) {
611 crde->crd_skip = 0;
612 } else {
613 if (!(crde->crd_flags & CRD_F_ENCRYPT))
614 crde->crd_flags |= CRD_F_IV_PRESENT;
615 crde->crd_skip = cse->txform->ivsize;
616 crde->crd_len -= cse->txform->ivsize;
617 }
618 }
619
620 if (cop->mac) {
621 if (crda == NULL) {
622 error = EINVAL;
623 goto bail;
624 }
625 crp->crp_mac=cse->tmp_mac;
626 }
627
628 cv_init(&crp->crp_cv, "crydev");
629
630 /*
631 * XXX there was a comment here which said that we went to
632 * XXX splcrypto() but needed to only if CRYPTO_F_CBIMM,
633 * XXX disabled on NetBSD since 1.6O due to a race condition.
634 * XXX But crypto_dispatch went to splcrypto() itself! (And
635 * XXX now takes the cryptodev_mtx mutex itself). We do, however,
636 * XXX need to hold the mutex across the call to cv_wait().
637 * XXX (should we arrange for crypto_dispatch to return to
638 * XXX us with it held? it seems quite ugly to do so.)
639 */
640 #ifdef notyet
641 eagain:
642 #endif
643 error = crypto_dispatch(crp);
644 mutex_enter(&cryptodev_mtx);
645
646 /*
647 * Don't touch crp before returned by any error or recieved
648 * cv_signal(&crp->crp_cv). It is required to restructure locks.
649 */
650
651 switch (error) {
652 #ifdef notyet /* don't loop forever -- but EAGAIN not possible here yet */
653 case EAGAIN:
654 mutex_exit(&cryptodev_mtx);
655 goto eagain;
656 break;
657 #endif
658 case 0:
659 break;
660 default:
661 DPRINTF("not waiting, error.\n");
662 mutex_exit(&cryptodev_mtx);
663 cv_destroy(&crp->crp_cv);
664 goto bail;
665 }
666
667 while (!(crp->crp_flags & CRYPTO_F_DQRETQ)) {
668 DPRINTF("cse->sid[%d]: sleeping on cv %p for crp %p\n",
669 (uint32_t)cse->sid, &crp->crp_cv, crp);
670 cv_wait(&crp->crp_cv, &cryptodev_mtx); /* XXX cv_wait_sig? */
671 }
672 mutex_exit(&cryptodev_mtx);
673 cv_destroy(&crp->crp_cv);
674
675 if (crp->crp_etype != 0) {
676 DPRINTF("crp_etype %d\n", crp->crp_etype);
677 error = crp->crp_etype;
678 goto bail;
679 }
680
681 if (cse->error) {
682 DPRINTF("cse->error %d\n", cse->error);
683 error = cse->error;
684 goto bail;
685 }
686
687 dst_len = crp->crp_ilen;
688 /* let the user know how much data was returned */
689 if (crp->crp_olen) {
690 if (crp->crp_olen > (cop->dst_len ? cop->dst_len : cop->len)) {
691 error = ENOSPC;
692 goto bail;
693 }
694 dst_len = cop->dst_len = crp->crp_olen;
695 }
696
697 if (cop->dst) {
698 DPRINTF("copyout %d bytes to %p\n", dst_len, cop->dst);
699 }
700 if (cop->dst &&
701 (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, dst_len)))
702 {
703 DPRINTF("copyout error %d\n", error);
704 goto bail;
705 }
706
707 if (cop->mac &&
708 (error = copyout(crp->crp_mac, cop->mac, cse->thash->authsize))) {
709 DPRINTF("mac copyout error %d\n", error);
710 goto bail;
711 }
712
713
714 bail:
715 if (crp) {
716 crypto_freereq(crp);
717 }
718 if (cse->uio.uio_iov[0].iov_base) {
719 kmem_free(cse->uio.uio_iov[0].iov_base,iov_len);
720 }
721
722 return error;
723 }
724
725 static int
726 cryptodev_cb(void *op)
727 {
728 struct cryptop *crp = (struct cryptop *) op;
729 struct csession *cse = (struct csession *)crp->crp_opaque;
730 int error = 0;
731
732 mutex_enter(&cryptodev_mtx);
733 cse->error = crp->crp_etype;
734 if (crp->crp_etype == EAGAIN) {
735 /* always drop mutex to call dispatch routine */
736 mutex_exit(&cryptodev_mtx);
737 error = crypto_dispatch(crp);
738 mutex_enter(&cryptodev_mtx);
739 }
740 if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
741 crp->crp_flags |= CRYPTO_F_DQRETQ;
742 cv_signal(&crp->crp_cv);
743 }
744 mutex_exit(&cryptodev_mtx);
745 return 0;
746 }
747
748 static int
749 cryptodev_mcb(void *op)
750 {
751 struct cryptop *crp = (struct cryptop *) op;
752 struct csession *cse = (struct csession *)crp->crp_opaque;
753 int error=0;
754
755 mutex_enter(&cryptodev_mtx);
756 cse->error = crp->crp_etype;
757 if (crp->crp_etype == EAGAIN) {
758 mutex_exit(&cryptodev_mtx);
759 error = crypto_dispatch(crp);
760 mutex_enter(&cryptodev_mtx);
761 }
762 if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
763 cv_signal(&crp->crp_cv);
764 }
765
766 TAILQ_INSERT_TAIL(&crp->fcrp->crp_ret_mq, crp, crp_next);
767 selnotify(&crp->fcrp->sinfo, 0, 0);
768 mutex_exit(&cryptodev_mtx);
769 return 0;
770 }
771
772 static int
773 cryptodevkey_cb(void *op)
774 {
775 struct cryptkop *krp = op;
776
777 mutex_enter(&cryptodev_mtx);
778 krp->krp_flags |= CRYPTO_F_DQRETQ;
779 cv_signal(&krp->krp_cv);
780 mutex_exit(&cryptodev_mtx);
781 return 0;
782 }
783
784 static int
785 cryptodevkey_mcb(void *op)
786 {
787 struct cryptkop *krp = op;
788
789 mutex_enter(&cryptodev_mtx);
790 cv_signal(&krp->krp_cv);
791 TAILQ_INSERT_TAIL(&krp->fcrp->crp_ret_mkq, krp, krp_next);
792 selnotify(&krp->fcrp->sinfo, 0, 0);
793 mutex_exit(&cryptodev_mtx);
794 return 0;
795 }
796
797 static int
798 cryptodev_key(struct crypt_kop *kop)
799 {
800 struct cryptkop *krp = NULL;
801 int error = EINVAL;
802 int in, out, size, i;
803
804 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM)
805 return EFBIG;
806
807 in = kop->crk_iparams;
808 out = kop->crk_oparams;
809 switch (kop->crk_op) {
810 case CRK_MOD_EXP:
811 if (in == 3 && out == 1)
812 break;
813 return EINVAL;
814 case CRK_MOD_EXP_CRT:
815 if (in == 6 && out == 1)
816 break;
817 return EINVAL;
818 case CRK_DSA_SIGN:
819 if (in == 5 && out == 2)
820 break;
821 return EINVAL;
822 case CRK_DSA_VERIFY:
823 if (in == 7 && out == 0)
824 break;
825 return EINVAL;
826 case CRK_DH_COMPUTE_KEY:
827 if (in == 3 && out == 1)
828 break;
829 return EINVAL;
830 case CRK_MOD_ADD:
831 if (in == 3 && out == 1)
832 break;
833 return EINVAL;
834 case CRK_MOD_ADDINV:
835 if (in == 2 && out == 1)
836 break;
837 return EINVAL;
838 case CRK_MOD_SUB:
839 if (in == 3 && out == 1)
840 break;
841 return EINVAL;
842 case CRK_MOD_MULT:
843 if (in == 3 && out == 1)
844 break;
845 return EINVAL;
846 case CRK_MOD_MULTINV:
847 if (in == 2 && out == 1)
848 break;
849 return EINVAL;
850 case CRK_MOD:
851 if (in == 2 && out == 1)
852 break;
853 return EINVAL;
854 default:
855 return EINVAL;
856 }
857
858 krp = crypto_kgetreq(1, PR_WAITOK);
859 if (krp == NULL) {
860 /* limited by opencrypto.crypto_ret_kq.maxlen */
861 return ENOMEM;
862 }
863 (void)memset(krp, 0, sizeof *krp);
864 cv_init(&krp->krp_cv, "crykdev");
865 krp->krp_op = kop->crk_op;
866 krp->krp_status = kop->crk_status;
867 krp->krp_iparams = kop->crk_iparams;
868 krp->krp_oparams = kop->crk_oparams;
869 krp->krp_status = 0;
870 krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
871
872 for (i = 0; i < CRK_MAXPARAM; i++)
873 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
874 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
875 size = (krp->krp_param[i].crp_nbits + 7) / 8;
876 if (size == 0)
877 continue;
878 krp->krp_param[i].crp_p = kmem_alloc(size, KM_SLEEP);
879 if (i >= krp->krp_iparams)
880 continue;
881 error = copyin(kop->crk_param[i].crp_p,
882 krp->krp_param[i].crp_p, size);
883 if (error)
884 goto fail;
885 }
886
887 error = crypto_kdispatch(krp);
888 if (error != 0) {
889 goto fail;
890 }
891
892 mutex_enter(&cryptodev_mtx);
893 while (!(krp->krp_flags & CRYPTO_F_DQRETQ)) {
894 cv_wait(&krp->krp_cv, &cryptodev_mtx); /* XXX cv_wait_sig? */
895 }
896 mutex_exit(&cryptodev_mtx);
897
898 if (krp->krp_status != 0) {
899 DPRINTF("krp->krp_status 0x%08x\n", krp->krp_status);
900 error = krp->krp_status;
901 goto fail;
902 }
903
904 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams;
905 i++) {
906 size = (krp->krp_param[i].crp_nbits + 7) / 8;
907 if (size == 0)
908 continue;
909 error = copyout(krp->krp_param[i].crp_p,
910 kop->crk_param[i].crp_p, size);
911 if (error) {
912 DPRINTF("copyout oparam %d failed, "
913 "error=%d\n", i-krp->krp_iparams, error);
914 goto fail;
915 }
916 }
917
918 fail:
919 kop->crk_status = krp->krp_status;
920 for (i = 0; i < CRK_MAXPARAM; i++) {
921 struct crparam *kp = &(krp->krp_param[i]);
922 if (krp->krp_param[i].crp_p) {
923 size = (kp->crp_nbits + 7) / 8;
924 KASSERT(size > 0);
925 (void)memset(kp->crp_p, 0, size);
926 kmem_free(kp->crp_p, size);
927 }
928 }
929 cv_destroy(&krp->krp_cv);
930 crypto_kfreereq(krp);
931 DPRINTF("error=0x%08x\n", error);
932 return error;
933 }
934
935 /* ARGSUSED */
936 static int
937 cryptof_close(struct file *fp)
938 {
939 struct fcrypt *fcr = fp->f_fcrypt;
940 struct csession *cse;
941
942 mutex_enter(&cryptodev_mtx);
943 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
944 TAILQ_REMOVE(&fcr->csessions, cse, next);
945 mutex_exit(&cryptodev_mtx);
946 (void)csefree(cse);
947 mutex_enter(&cryptodev_mtx);
948 }
949 seldestroy(&fcr->sinfo);
950 fp->f_fcrypt = NULL;
951 crypto_refcount--;
952 mutex_exit(&cryptodev_mtx);
953
954 pool_put(&fcrpl, fcr);
955 return 0;
956 }
957
958 /* needed for compatibility module */
959 struct csession *cryptodev_csefind(struct fcrypt *fcr, u_int ses)
960 {
961 return csefind(fcr, ses);
962 }
963
964 /* csefind: call with cryptodev_mtx held. */
965 static struct csession *
966 csefind(struct fcrypt *fcr, u_int ses)
967 {
968 struct csession *cse, *cnext, *ret = NULL;
969
970 KASSERT(mutex_owned(&cryptodev_mtx));
971 TAILQ_FOREACH_SAFE(cse, &fcr->csessions, next, cnext)
972 if (cse->ses == ses)
973 ret = cse;
974
975 return ret;
976 }
977
978 /* csedelete: call with cryptodev_mtx held. */
979 static int
980 csedelete(struct fcrypt *fcr, struct csession *cse_del)
981 {
982 struct csession *cse, *cnext;
983 int ret = 0;
984
985 KASSERT(mutex_owned(&cryptodev_mtx));
986 TAILQ_FOREACH_SAFE(cse, &fcr->csessions, next, cnext) {
987 if (cse == cse_del) {
988 TAILQ_REMOVE(&fcr->csessions, cse, next);
989 ret = 1;
990 }
991 }
992 return ret;
993 }
994
995 static struct csession *
996 cseadd(struct fcrypt *fcr, struct csession *cse)
997 {
998 mutex_enter(&cryptodev_mtx);
999 /* don't let session ID wrap! */
1000 if (fcr->sesn + 1 == 0) return NULL;
1001 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
1002 cse->ses = fcr->sesn++;
1003 mutex_exit(&cryptodev_mtx);
1004 return cse;
1005 }
1006
1007 static struct csession *
1008 csecreate(struct fcrypt *fcr, u_int64_t sid, void *key, u_int64_t keylen,
1009 void *mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
1010 u_int32_t comp_alg, const struct enc_xform *txform,
1011 const struct auth_hash *thash, const struct comp_algo *tcomp)
1012 {
1013 struct csession *cse;
1014
1015 cse = pool_get(&csepl, PR_NOWAIT);
1016 if (cse == NULL)
1017 return NULL;
1018 cse->key = key;
1019 cse->keylen = keylen/8;
1020 cse->mackey = mackey;
1021 cse->mackeylen = mackeylen/8;
1022 cse->sid = sid;
1023 cse->cipher = cipher;
1024 cse->mac = mac;
1025 cse->comp_alg = comp_alg;
1026 cse->txform = txform;
1027 cse->thash = thash;
1028 cse->tcomp = tcomp;
1029 cse->error = 0;
1030 if (cseadd(fcr, cse))
1031 return cse;
1032 else {
1033 pool_put(&csepl, cse);
1034 return NULL;
1035 }
1036 }
1037
1038 /* csefree: call with cryptodev_mtx held. */
1039 static int
1040 csefree(struct csession *cse)
1041 {
1042 int error;
1043
1044 error = crypto_freesession(cse->sid);
1045 if (cse->key)
1046 free(cse->key, M_XDATA);
1047 if (cse->mackey)
1048 free(cse->mackey, M_XDATA);
1049 pool_put(&csepl, cse);
1050 return error;
1051 }
1052
1053 static int
1054 cryptoopen(dev_t dev, int flag, int mode,
1055 struct lwp *l)
1056 {
1057 file_t *fp;
1058 struct fcrypt *fcr;
1059 int fd, error;
1060
1061 if (crypto_usercrypto == 0)
1062 return ENXIO;
1063
1064 if ((error = fd_allocfile(&fp, &fd)) != 0)
1065 return error;
1066
1067 fcr = pool_get(&fcrpl, PR_WAITOK);
1068 getnanotime(&fcr->btime);
1069 fcr->atime = fcr->mtime = fcr->btime;
1070 mutex_enter(&cryptodev_mtx);
1071 TAILQ_INIT(&fcr->csessions);
1072 TAILQ_INIT(&fcr->crp_ret_mq);
1073 TAILQ_INIT(&fcr->crp_ret_mkq);
1074 selinit(&fcr->sinfo);
1075 /*
1076 * Don't ever return session 0, to allow detection of
1077 * failed creation attempts with multi-create ioctl.
1078 */
1079 fcr->sesn = 1;
1080 fcr->requestid = 1;
1081 crypto_refcount++;
1082 mutex_exit(&cryptodev_mtx);
1083 return fd_clone(fp, fd, flag, &cryptofops, fcr);
1084 }
1085
1086 static int
1087 cryptoread(dev_t dev, struct uio *uio, int ioflag)
1088 {
1089 return EIO;
1090 }
1091
1092 static int
1093 cryptowrite(dev_t dev, struct uio *uio, int ioflag)
1094 {
1095 return EIO;
1096 }
1097
1098 int
1099 cryptoselect(dev_t dev, int rw, struct lwp *l)
1100 {
1101 return 0;
1102 }
1103
1104 /*static*/
1105 struct cdevsw crypto_cdevsw = {
1106 .d_open = cryptoopen,
1107 .d_close = noclose,
1108 .d_read = cryptoread,
1109 .d_write = cryptowrite,
1110 .d_ioctl = noioctl,
1111 .d_stop = nostop,
1112 .d_tty = notty,
1113 .d_poll = cryptoselect /*nopoll*/,
1114 .d_mmap = nommap,
1115 .d_kqfilter = nokqfilter,
1116 .d_discard = nodiscard,
1117 .d_flag = D_OTHER
1118 };
1119
1120 int
1121 cryptodev_mop(struct fcrypt *fcr,
1122 struct crypt_n_op * cnop,
1123 int count, struct lwp *l)
1124 {
1125 struct cryptop *crp = NULL;
1126 struct cryptodesc *crde = NULL, *crda = NULL, *crdc = NULL;
1127 int req, error=0;
1128 struct csession *cse;
1129 int flags=0;
1130 int iov_len;
1131
1132 for (req = 0; req < count; req++) {
1133 mutex_enter(&cryptodev_mtx);
1134 cse = csefind(fcr, cnop[req].ses);
1135 if (cse == NULL) {
1136 DPRINTF("csefind failed\n");
1137 cnop[req].status = EINVAL;
1138 mutex_exit(&cryptodev_mtx);
1139 continue;
1140 }
1141 mutex_exit(&cryptodev_mtx);
1142
1143 if (cnop[req].len > 256*1024-4) {
1144 DPRINTF("length failed\n");
1145 cnop[req].status = EINVAL;
1146 continue;
1147 }
1148 if (cse->txform) {
1149 if (cnop[req].len < cse->txform->blocksize -
1150 (cnop[req].iv ? 0 : cse->txform->ivsize) ||
1151 (cnop[req].len -
1152 (cnop[req].iv ? 0 : cse->txform->ivsize))
1153 % cse->txform->blocksize) {
1154 cnop[req].status = EINVAL;
1155 continue;
1156 }
1157 }
1158
1159 crp = crypto_getreq((cse->txform != NULL) +
1160 (cse->thash != NULL) +
1161 (cse->tcomp != NULL));
1162 if (crp == NULL) {
1163 cnop[req].status = ENOMEM;
1164 goto bail;
1165 }
1166
1167 iov_len = cnop[req].len;
1168 /* got a compression/decompression max size? */
1169 if ((cse->tcomp) && cnop[req].dst_len) {
1170 if (iov_len < cnop[req].dst_len) {
1171 /* Need larger iov to deal with decompress */
1172 iov_len = cnop[req].dst_len;
1173 }
1174 DPRINTF("iov_len -> %d for decompress\n", iov_len);
1175 }
1176
1177 (void)memset(&crp->uio, 0, sizeof(crp->uio));
1178 crp->uio.uio_iovcnt = 1;
1179 crp->uio.uio_resid = 0;
1180 crp->uio.uio_rw = UIO_WRITE;
1181 crp->uio.uio_iov = crp->iovec;
1182 UIO_SETUP_SYSSPACE(&crp->uio);
1183 memset(&crp->iovec, 0, sizeof(crp->iovec));
1184 crp->uio.uio_iov[0].iov_len = iov_len;
1185 DPRINTF("kmem_alloc(%d) for iov \n", iov_len);
1186 crp->uio.uio_iov[0].iov_base = kmem_alloc(iov_len, KM_SLEEP);
1187 crp->uio.uio_resid = crp->uio.uio_iov[0].iov_len;
1188
1189 if (cse->tcomp) {
1190 crdc = crp->crp_desc;
1191 }
1192
1193 if (cse->thash) {
1194 crda = crdc ? crdc->crd_next : crp->crp_desc;
1195 if (cse->txform && crda)
1196 crde = crda->crd_next;
1197 } else {
1198 if (cse->txform) {
1199 crde = crdc ? crdc->crd_next : crp->crp_desc;
1200 } else if (!cse->tcomp) {
1201 error = EINVAL;
1202 goto bail;
1203 }
1204 }
1205
1206 if ((copyin(cnop[req].src,
1207 crp->uio.uio_iov[0].iov_base, cnop[req].len))) {
1208 cnop[req].status = EINVAL;
1209 goto bail;
1210 }
1211
1212 if (crdc) {
1213 switch (cnop[req].op) {
1214 case COP_COMP:
1215 crdc->crd_flags |= CRD_F_COMP;
1216 break;
1217 case COP_DECOMP:
1218 crdc->crd_flags &= ~CRD_F_COMP;
1219 break;
1220 default:
1221 break;
1222 }
1223 /* more data to follow? */
1224 if (cnop[req].flags & COP_F_MORE) {
1225 flags |= CRYPTO_F_MORE;
1226 }
1227 crdc->crd_len = cnop[req].len;
1228 crdc->crd_inject = 0;
1229
1230 crdc->crd_alg = cse->comp_alg;
1231 crdc->crd_key = NULL;
1232 crdc->crd_klen = 0;
1233 DPRINTF("cse->sid[%d]: crdc setup for comp_alg %d"
1234 " len %d.\n",
1235 (uint32_t)cse->sid, crdc->crd_alg,
1236 crdc->crd_len);
1237 }
1238
1239 if (crda) {
1240 crda->crd_skip = 0;
1241 crda->crd_len = cnop[req].len;
1242 crda->crd_inject = 0; /* ??? */
1243
1244 crda->crd_alg = cse->mac;
1245 crda->crd_key = cse->mackey;
1246 crda->crd_klen = cse->mackeylen * 8;
1247 }
1248
1249 if (crde) {
1250 if (cnop[req].op == COP_ENCRYPT)
1251 crde->crd_flags |= CRD_F_ENCRYPT;
1252 else
1253 crde->crd_flags &= ~CRD_F_ENCRYPT;
1254 crde->crd_len = cnop[req].len;
1255 crde->crd_inject = 0;
1256
1257 crde->crd_alg = cse->cipher;
1258 #ifdef notyet /* XXX must notify h/w driver new key, drain */
1259 if(cnop[req].key && cnop[req].keylen) {
1260 crde->crd_key = malloc(cnop[req].keylen,
1261 M_XDATA, M_WAITOK);
1262 if((error = copyin(cnop[req].key,
1263 crde->crd_key, cnop[req].keylen))) {
1264 cnop[req].status = EINVAL;
1265 goto bail;
1266 }
1267 crde->crd_klen = cnop[req].keylen * 8;
1268 } else { ... }
1269 #endif
1270 crde->crd_key = cse->key;
1271 crde->crd_klen = cse->keylen * 8;
1272 }
1273
1274 crp->crp_ilen = cnop[req].len;
1275 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM |
1276 (cnop[req].flags & COP_F_BATCH) | flags;
1277 crp->crp_buf = (void *)&crp->uio;
1278 crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_mcb;
1279 crp->crp_sid = cse->sid;
1280 crp->crp_opaque = (void *)cse;
1281 crp->fcrp = fcr;
1282 crp->dst = cnop[req].dst;
1283 crp->len = cnop[req].len; /* input len, iov may be larger */
1284 crp->mac = cnop[req].mac;
1285 DPRINTF("iov_base %p dst %p len %d mac %p\n",
1286 crp->uio.uio_iov[0].iov_base, crp->dst, crp->len,
1287 crp->mac);
1288
1289 if (cnop[req].iv) {
1290 if (crde == NULL) {
1291 cnop[req].status = EINVAL;
1292 goto bail;
1293 }
1294 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
1295 cnop[req].status = EINVAL;
1296 goto bail;
1297 }
1298 if ((error = copyin(cnop[req].iv, crp->tmp_iv,
1299 cse->txform->ivsize))) {
1300 cnop[req].status = EINVAL;
1301 goto bail;
1302 }
1303 (void)memcpy(crde->crd_iv, crp->tmp_iv,
1304 cse->txform->ivsize);
1305 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
1306 crde->crd_skip = 0;
1307 } else if (crde) {
1308 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
1309 crde->crd_skip = 0;
1310 } else {
1311 if (!(crde->crd_flags & CRD_F_ENCRYPT))
1312 crde->crd_flags |= CRD_F_IV_PRESENT;
1313 crde->crd_skip = cse->txform->ivsize;
1314 crde->crd_len -= cse->txform->ivsize;
1315 }
1316 }
1317
1318 if (cnop[req].mac) {
1319 if (crda == NULL) {
1320 cnop[req].status = EINVAL;
1321 goto bail;
1322 }
1323 crp->crp_mac=cse->tmp_mac;
1324 }
1325 cnop[req].reqid = atomic_inc_32_nv(&(fcr->requestid));
1326 crp->crp_reqid = cnop[req].reqid;
1327 crp->crp_usropaque = cnop[req].opaque;
1328 cv_init(&crp->crp_cv, "crydev");
1329 #ifdef notyet
1330 eagain:
1331 #endif
1332 cnop[req].status = crypto_dispatch(crp);
1333 mutex_enter(&cryptodev_mtx); /* XXX why mutex? */
1334
1335 switch (cnop[req].status) {
1336 #ifdef notyet /* don't loop forever -- but EAGAIN not possible here yet */
1337 case EAGAIN:
1338 mutex_exit(&cryptodev_mtx);
1339 goto eagain;
1340 break;
1341 #endif
1342 case 0:
1343 break;
1344 default:
1345 DPRINTF("not waiting, error.\n");
1346 mutex_exit(&cryptodev_mtx);
1347 cv_destroy(&crp->crp_cv);
1348 goto bail;
1349 }
1350
1351 mutex_exit(&cryptodev_mtx);
1352 cv_destroy(&crp->crp_cv);
1353 bail:
1354 if (cnop[req].status) {
1355 if (crp) {
1356 if (crp->uio.uio_iov[0].iov_base) {
1357 kmem_free(crp->uio.uio_iov[0].iov_base,
1358 crp->uio.uio_iov[0].iov_len);
1359 }
1360 crypto_freereq(crp);
1361 }
1362 error = 0;
1363 }
1364 }
1365 return error;
1366 }
1367
1368 static int
1369 cryptodev_mkey(struct fcrypt *fcr, struct crypt_n_kop *kop, int count)
1370 {
1371 struct cryptkop *krp = NULL;
1372 int error = EINVAL;
1373 int in, out, size, i, req;
1374
1375 for (req = 0; req < count; req++) {
1376 if (kop[req].crk_iparams + kop[req].crk_oparams > CRK_MAXPARAM)
1377 return EFBIG;
1378
1379 in = kop[req].crk_iparams;
1380 out = kop[req].crk_oparams;
1381 switch (kop[req].crk_op) {
1382 case CRK_MOD_EXP:
1383 if (in == 3 && out == 1)
1384 break;
1385 kop[req].crk_status = EINVAL;
1386 continue;
1387 case CRK_MOD_EXP_CRT:
1388 if (in == 6 && out == 1)
1389 break;
1390 kop[req].crk_status = EINVAL;
1391 continue;
1392 case CRK_DSA_SIGN:
1393 if (in == 5 && out == 2)
1394 break;
1395 kop[req].crk_status = EINVAL;
1396 continue;
1397 case CRK_DSA_VERIFY:
1398 if (in == 7 && out == 0)
1399 break;
1400 kop[req].crk_status = EINVAL;
1401 continue;
1402 case CRK_DH_COMPUTE_KEY:
1403 if (in == 3 && out == 1)
1404 break;
1405 kop[req].crk_status = EINVAL;
1406 continue;
1407 case CRK_MOD_ADD:
1408 if (in == 3 && out == 1)
1409 break;
1410 kop[req].crk_status = EINVAL;
1411 continue;
1412 case CRK_MOD_ADDINV:
1413 if (in == 2 && out == 1)
1414 break;
1415 kop[req].crk_status = EINVAL;
1416 continue;
1417 case CRK_MOD_SUB:
1418 if (in == 3 && out == 1)
1419 break;
1420 kop[req].crk_status = EINVAL;
1421 continue;
1422 case CRK_MOD_MULT:
1423 if (in == 3 && out == 1)
1424 break;
1425 kop[req].crk_status = EINVAL;
1426 continue;
1427 case CRK_MOD_MULTINV:
1428 if (in == 2 && out == 1)
1429 break;
1430 kop[req].crk_status = EINVAL;
1431 continue;
1432 case CRK_MOD:
1433 if (in == 2 && out == 1)
1434 break;
1435 kop[req].crk_status = EINVAL;
1436 continue;
1437 default:
1438 kop[req].crk_status = EINVAL;
1439 continue;
1440 }
1441
1442 krp = crypto_kgetreq(1, PR_WAITOK);
1443 if (krp == NULL) {
1444 /* limited by opencrypto.crypto_ret_kq.maxlen */
1445 continue;
1446 }
1447 (void)memset(krp, 0, sizeof *krp);
1448 cv_init(&krp->krp_cv, "crykdev");
1449 krp->krp_op = kop[req].crk_op;
1450 krp->krp_status = kop[req].crk_status;
1451 krp->krp_iparams = kop[req].crk_iparams;
1452 krp->krp_oparams = kop[req].crk_oparams;
1453 krp->krp_status = 0;
1454 krp->krp_callback =
1455 (int (*) (struct cryptkop *)) cryptodevkey_mcb;
1456 (void)memcpy(krp->crk_param, kop[req].crk_param,
1457 sizeof(kop[req].crk_param));
1458
1459 krp->krp_flags = CRYPTO_F_CBIMM;
1460
1461 for (i = 0; i < CRK_MAXPARAM; i++)
1462 krp->krp_param[i].crp_nbits =
1463 kop[req].crk_param[i].crp_nbits;
1464 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
1465 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1466 if (size == 0)
1467 continue;
1468 krp->krp_param[i].crp_p =
1469 kmem_alloc(size, KM_SLEEP);
1470 if (i >= krp->krp_iparams)
1471 continue;
1472 kop[req].crk_status =
1473 copyin(kop[req].crk_param[i].crp_p,
1474 krp->krp_param[i].crp_p, size);
1475 if (kop[req].crk_status)
1476 goto fail;
1477 }
1478 krp->fcrp = fcr;
1479
1480 kop[req].crk_reqid = atomic_inc_32_nv(&(fcr->requestid));
1481 krp->krp_reqid = kop[req].crk_reqid;
1482 krp->krp_usropaque = kop[req].crk_opaque;
1483
1484 kop[req].crk_status = crypto_kdispatch(krp);
1485 if (kop[req].crk_status != 0) {
1486 goto fail;
1487 }
1488
1489 fail:
1490 if(kop[req].crk_status) {
1491 if (krp) {
1492 kop[req].crk_status = krp->krp_status;
1493 for (i = 0; i < CRK_MAXPARAM; i++) {
1494 struct crparam *kp =
1495 &(krp->krp_param[i]);
1496 if (kp->crp_p) {
1497 size = (kp->crp_nbits + 7) / 8;
1498 KASSERT(size > 0);
1499 memset(kp->crp_p, 0, size);
1500 kmem_free(kp->crp_p, size);
1501 }
1502 }
1503 cv_destroy(&krp->krp_cv);
1504 crypto_kfreereq(krp);
1505 }
1506 }
1507 error = 0;
1508 }
1509 DPRINTF("error=0x%08x\n", error);
1510 return error;
1511 }
1512
1513 int
1514 cryptodev_session(struct fcrypt *fcr, struct session_op *sop)
1515 {
1516 struct cryptoini cria, crie;
1517 struct cryptoini cric; /* compressor */
1518 struct cryptoini *crihead = NULL;
1519 const struct enc_xform *txform = NULL;
1520 const struct auth_hash *thash = NULL;
1521 const struct comp_algo *tcomp = NULL;
1522 struct csession *cse;
1523 u_int64_t sid;
1524 int error = 0;
1525
1526 DPRINTF("cipher=%d, mac=%d\n", sop->cipher, sop->mac);
1527
1528 /* XXX there must be a way to not embed the list of xforms here */
1529 switch (sop->cipher) {
1530 case 0:
1531 break;
1532 case CRYPTO_DES_CBC:
1533 txform = &enc_xform_des;
1534 break;
1535 case CRYPTO_3DES_CBC:
1536 txform = &enc_xform_3des;
1537 break;
1538 case CRYPTO_BLF_CBC:
1539 txform = &enc_xform_blf;
1540 break;
1541 case CRYPTO_CAST_CBC:
1542 txform = &enc_xform_cast5;
1543 break;
1544 case CRYPTO_SKIPJACK_CBC:
1545 txform = &enc_xform_skipjack;
1546 break;
1547 case CRYPTO_AES_CBC:
1548 txform = &enc_xform_rijndael128;
1549 break;
1550 case CRYPTO_CAMELLIA_CBC:
1551 txform = &enc_xform_camellia;
1552 break;
1553 case CRYPTO_AES_CTR:
1554 txform = &enc_xform_aes_ctr;
1555 break;
1556 case CRYPTO_AES_GCM_16:
1557 txform = &enc_xform_aes_gcm;
1558 break;
1559 case CRYPTO_AES_GMAC:
1560 txform = &enc_xform_aes_gmac;
1561 break;
1562 case CRYPTO_NULL_CBC:
1563 txform = &enc_xform_null;
1564 break;
1565 case CRYPTO_ARC4:
1566 txform = &enc_xform_arc4;
1567 break;
1568 default:
1569 DPRINTF("Invalid cipher %d\n", sop->cipher);
1570 return EINVAL;
1571 }
1572
1573 switch (sop->comp_alg) {
1574 case 0:
1575 break;
1576 case CRYPTO_DEFLATE_COMP:
1577 tcomp = &comp_algo_deflate;
1578 break;
1579 case CRYPTO_GZIP_COMP:
1580 tcomp = &comp_algo_gzip;
1581 DPRINTF("tcomp for GZIP\n");
1582 break;
1583 default:
1584 DPRINTF("Invalid compression alg %d\n", sop->comp_alg);
1585 return EINVAL;
1586 }
1587
1588 switch (sop->mac) {
1589 case 0:
1590 break;
1591 case CRYPTO_MD5_HMAC:
1592 thash = &auth_hash_hmac_md5;
1593 break;
1594 case CRYPTO_SHA1_HMAC:
1595 thash = &auth_hash_hmac_sha1;
1596 break;
1597 case CRYPTO_MD5_HMAC_96:
1598 thash = &auth_hash_hmac_md5_96;
1599 break;
1600 case CRYPTO_SHA1_HMAC_96:
1601 thash = &auth_hash_hmac_sha1_96;
1602 break;
1603 case CRYPTO_SHA2_HMAC:
1604 /* XXX switching on key length seems questionable */
1605 if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize) {
1606 thash = &auth_hash_hmac_sha2_256;
1607 } else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize) {
1608 thash = &auth_hash_hmac_sha2_384;
1609 } else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize) {
1610 thash = &auth_hash_hmac_sha2_512;
1611 } else {
1612 DPRINTF("Invalid mackeylen %d\n", sop->mackeylen);
1613 return EINVAL;
1614 }
1615 break;
1616 case CRYPTO_RIPEMD160_HMAC:
1617 thash = &auth_hash_hmac_ripemd_160;
1618 break;
1619 case CRYPTO_RIPEMD160_HMAC_96:
1620 thash = &auth_hash_hmac_ripemd_160_96;
1621 break;
1622 case CRYPTO_MD5:
1623 thash = &auth_hash_md5;
1624 break;
1625 case CRYPTO_SHA1:
1626 thash = &auth_hash_sha1;
1627 break;
1628 case CRYPTO_AES_XCBC_MAC_96:
1629 thash = &auth_hash_aes_xcbc_mac_96;
1630 break;
1631 case CRYPTO_AES_128_GMAC:
1632 thash = &auth_hash_gmac_aes_128;
1633 break;
1634 case CRYPTO_AES_192_GMAC:
1635 thash = &auth_hash_gmac_aes_192;
1636 break;
1637 case CRYPTO_AES_256_GMAC:
1638 thash = &auth_hash_gmac_aes_256;
1639 break;
1640 case CRYPTO_NULL_HMAC:
1641 thash = &auth_hash_null;
1642 break;
1643 default:
1644 DPRINTF("Invalid mac %d\n", sop->mac);
1645 return EINVAL;
1646 }
1647
1648 memset(&crie, 0, sizeof(crie));
1649 memset(&cria, 0, sizeof(cria));
1650 memset(&cric, 0, sizeof(cric));
1651
1652 if (tcomp) {
1653 cric.cri_alg = tcomp->type;
1654 cric.cri_klen = 0;
1655 DPRINTF("tcomp->type = %d\n", tcomp->type);
1656
1657 crihead = &cric;
1658 if (txform) {
1659 cric.cri_next = &crie;
1660 } else if (thash) {
1661 cric.cri_next = &cria;
1662 }
1663 }
1664
1665 if (txform) {
1666 crie.cri_alg = txform->type;
1667 crie.cri_klen = sop->keylen * 8;
1668 if (sop->keylen > txform->maxkey ||
1669 sop->keylen < txform->minkey) {
1670 DPRINTF("keylen %d not in [%d,%d]\n",
1671 sop->keylen, txform->minkey, txform->maxkey);
1672 error = EINVAL;
1673 goto bail;
1674 }
1675
1676 crie.cri_key = malloc(crie.cri_klen / 8, M_XDATA, M_WAITOK);
1677 if ((error = copyin(sop->key, crie.cri_key, crie.cri_klen / 8)))
1678 goto bail;
1679 if (!crihead) {
1680 crihead = &crie;
1681 }
1682 if (thash)
1683 crie.cri_next = &cria;
1684 }
1685
1686 if (thash) {
1687 cria.cri_alg = thash->type;
1688 cria.cri_klen = sop->mackeylen * 8;
1689 if (sop->mackeylen != thash->keysize) {
1690 DPRINTF("mackeylen %d != keysize %d\n",
1691 sop->mackeylen, thash->keysize);
1692 error = EINVAL;
1693 goto bail;
1694 }
1695 if (cria.cri_klen) {
1696 cria.cri_key = malloc(cria.cri_klen / 8, M_XDATA,
1697 M_WAITOK);
1698 if ((error = copyin(sop->mackey, cria.cri_key,
1699 cria.cri_klen / 8))) {
1700 goto bail;
1701 }
1702 }
1703 if (!crihead) {
1704 crihead = &cria;
1705 }
1706 }
1707
1708 error = crypto_newsession(&sid, crihead, crypto_devallowsoft);
1709 if (!error) {
1710 DPRINTF("got session %d\n", (uint32_t)sid);
1711 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
1712 cria.cri_key, cria.cri_klen, (txform ? sop->cipher : 0), sop->mac,
1713 (tcomp ? sop->comp_alg : 0), txform, thash, tcomp);
1714 if (cse != NULL) {
1715 sop->ses = cse->ses;
1716 } else {
1717 DPRINTF("csecreate failed\n");
1718 crypto_freesession(sid);
1719 error = EINVAL;
1720 }
1721 } else {
1722 DPRINTF("SIOCSESSION violates kernel parameters %d\n", error);
1723 }
1724 bail:
1725 if (error) {
1726 if (crie.cri_key) {
1727 memset(crie.cri_key, 0, crie.cri_klen / 8);
1728 free(crie.cri_key, M_XDATA);
1729 }
1730 if (cria.cri_key) {
1731 memset(cria.cri_key, 0, cria.cri_klen / 8);
1732 free(cria.cri_key, M_XDATA);
1733 }
1734 }
1735 return error;
1736 }
1737
1738 int
1739 cryptodev_msession(struct fcrypt *fcr, struct session_n_op *sn_ops,
1740 int count)
1741 {
1742 int i;
1743
1744 for (i = 0; i < count; i++, sn_ops++) {
1745 struct session_op s_op;
1746 s_op.cipher = sn_ops->cipher;
1747 s_op.mac = sn_ops->mac;
1748 s_op.keylen = sn_ops->keylen;
1749 s_op.key = sn_ops->key;
1750 s_op.mackeylen = sn_ops->mackeylen;
1751 s_op.mackey = sn_ops->mackey;
1752
1753 sn_ops->status = cryptodev_session(fcr, &s_op);
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 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_NET); /* XXX IPL_NET ("splcrypto") */
2086 pool_init(&csepl, sizeof(struct csession), 0, 0, 0, "csepl",
2087 NULL, IPL_NET); /* XXX IPL_NET ("splcrypto") */
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 = config_cfdriver_attach(&crypto_cd);
2169 if (error) {
2170 return error;
2171 }
2172
2173 error = config_cfattach_attach(crypto_cd.cd_name, &crypto_ca);
2174 if (error) {
2175 config_cfdriver_detach(&crypto_cd);
2176 aprint_error("%s: unable to register cfattach\n",
2177 crypto_cd.cd_name);
2178
2179 return error;
2180 }
2181
2182 error = config_cfdata_attach(crypto_cfdata, 1);
2183 if (error) {
2184 config_cfattach_detach(crypto_cd.cd_name, &crypto_ca);
2185 config_cfdriver_detach(&crypto_cd);
2186 aprint_error("%s: unable to register cfdata\n",
2187 crypto_cd.cd_name);
2188
2189 return error;
2190 }
2191
2192 error = devsw_attach(crypto_cd.cd_name, NULL, &bmajor,
2193 &crypto_cdevsw, &cmajor);
2194 if (error) {
2195 error = config_cfdata_detach(crypto_cfdata);
2196 if (error) {
2197 return error;
2198 }
2199 config_cfattach_detach(crypto_cd.cd_name, &crypto_ca);
2200 config_cfdriver_detach(&crypto_cd);
2201 aprint_error("%s: unable to register devsw\n",
2202 crypto_cd.cd_name);
2203
2204 return error;
2205 }
2206
2207 (void)config_attach_pseudo(crypto_cfdata);
2208 #endif
2209
2210 return error;
2211 case MODULE_CMD_FINI:
2212 #ifdef _MODULE
2213 error = config_cfdata_detach(crypto_cfdata);
2214 if (error) {
2215 return error;
2216 }
2217
2218 config_cfattach_detach(crypto_cd.cd_name, &crypto_ca);
2219 config_cfdriver_detach(&crypto_cd);
2220 devsw_detach(NULL, &crypto_cdevsw);
2221 #endif
2222
2223 return error;
2224 #ifdef _MODULE
2225 case MODULE_CMD_AUTOUNLOAD:
2226 #if 0 /*
2227 * XXX Completely disable auto-unload for now, since there is still
2228 * XXX a (small) window where in-module ref-counting doesn't help
2229 */
2230 if (crypto_refcount != 0)
2231 #endif
2232 return EBUSY;
2233 /* FALLTHROUGH */
2234 #endif
2235 default:
2236 return ENOTTY;
2237 }
2238 }
2239