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