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