cryptodev.c revision 1.85.2.1 1 /* $NetBSD: cryptodev.c,v 1.85.2.1 2016/07/18 03:50:00 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.85.2.1 2016/07/18 03:50:00 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 #include <sys/localcount.h>
90
91 #ifdef _KERNEL_OPT
92 #include "opt_ocf.h"
93 #include "opt_compat_netbsd.h"
94 #endif
95
96 #include <opencrypto/cryptodev.h>
97 #include <opencrypto/cryptodev_internal.h>
98 #include <opencrypto/xform.h>
99
100 #include "ioconf.h"
101
102 struct csession {
103 TAILQ_ENTRY(csession) next;
104 u_int64_t sid;
105 u_int32_t ses;
106
107 u_int32_t cipher; /* note: shares name space in crd_alg */
108 const struct enc_xform *txform;
109 u_int32_t mac; /* note: shares name space in crd_alg */
110 const struct auth_hash *thash;
111 u_int32_t comp_alg; /* note: shares name space in crd_alg */
112 const struct comp_algo *tcomp;
113
114 void * key;
115 int keylen;
116 u_char tmp_iv[EALG_MAX_BLOCK_LEN];
117
118 void * mackey;
119 int mackeylen;
120 u_char tmp_mac[CRYPTO_MAX_MAC_LEN];
121
122 struct iovec iovec[1]; /* user requests never have more */
123 struct uio uio;
124 int error;
125 };
126
127 struct fcrypt {
128 TAILQ_HEAD(csessionlist, csession) csessions;
129 TAILQ_HEAD(crprethead, cryptop) crp_ret_mq;
130 TAILQ_HEAD(krprethead, cryptkop) crp_ret_mkq;
131 int sesn;
132 struct selinfo sinfo;
133 u_int32_t requestid;
134 struct timespec atime;
135 struct timespec mtime;
136 struct timespec btime;
137 };
138
139 /* For our fixed-size allocations */
140 static struct pool fcrpl;
141 static struct pool csepl;
142
143 /* Declaration of master device (fd-cloning/ctxt-allocating) entrypoints */
144 static int cryptoopen(dev_t dev, int flag, int mode, struct lwp *l);
145 static int cryptoread(dev_t dev, struct uio *uio, int ioflag);
146 static int cryptowrite(dev_t dev, struct uio *uio, int ioflag);
147 static int cryptoselect(dev_t dev, int rw, struct lwp *l);
148
149 static int crypto_refcount = 0; /* Prevent detaching while in use */
150
151 /* Declaration of cloned-device (per-ctxt) entrypoints */
152 static int cryptof_read(struct file *, off_t *, struct uio *,
153 kauth_cred_t, int);
154 static int cryptof_write(struct file *, off_t *, struct uio *,
155 kauth_cred_t, int);
156 static int cryptof_ioctl(struct file *, u_long, void *);
157 static int cryptof_close(struct file *);
158 static int cryptof_poll(struct file *, int);
159 static int cryptof_stat(struct file *, struct stat *);
160
161 static const struct fileops cryptofops = {
162 .fo_read = cryptof_read,
163 .fo_write = cryptof_write,
164 .fo_ioctl = cryptof_ioctl,
165 .fo_fcntl = fnullop_fcntl,
166 .fo_poll = cryptof_poll,
167 .fo_stat = cryptof_stat,
168 .fo_close = cryptof_close,
169 .fo_kqfilter = fnullop_kqfilter,
170 .fo_restart = fnullop_restart,
171 };
172
173 struct csession *cryptodev_csefind(struct fcrypt *, u_int);
174 static struct csession *csefind(struct fcrypt *, u_int);
175 static int csedelete(struct fcrypt *, struct csession *);
176 static struct csession *cseadd(struct fcrypt *, struct csession *);
177 static struct csession *csecreate(struct fcrypt *, u_int64_t, void *,
178 u_int64_t, void *, u_int64_t, u_int32_t, u_int32_t, u_int32_t,
179 const struct enc_xform *, const struct auth_hash *,
180 const struct comp_algo *);
181 static int csefree(struct csession *);
182
183 static int cryptodev_key(struct crypt_kop *);
184 static int cryptodev_mkey(struct fcrypt *, struct crypt_n_kop *, int);
185 static int cryptodev_msessionfin(struct fcrypt *, int, u_int32_t *);
186
187 static int cryptodev_cb(void *);
188 static int cryptodevkey_cb(void *);
189
190 static int cryptodev_mcb(void *);
191 static int cryptodevkey_mcb(void *);
192
193 static int cryptodev_getmstatus(struct fcrypt *, struct crypt_result *,
194 int);
195 static int cryptodev_getstatus(struct fcrypt *, struct crypt_result *);
196
197 #ifdef COMPAT_50
198 extern int ocryptof_ioctl(struct file *, u_long, void *);
199 #endif
200
201 /*
202 * sysctl-able control variables for /dev/crypto now defined in crypto.c:
203 * crypto_usercrypto, crypto_userasmcrypto, crypto_devallowsoft.
204 */
205
206 /* ARGSUSED */
207 int
208 cryptof_read(file_t *fp, off_t *poff,
209 struct uio *uio, kauth_cred_t cred, int flags)
210 {
211 return EIO;
212 }
213
214 /* ARGSUSED */
215 int
216 cryptof_write(file_t *fp, off_t *poff,
217 struct uio *uio, kauth_cred_t cred, int flags)
218 {
219 return EIO;
220 }
221
222 /* ARGSUSED */
223 int
224 cryptof_ioctl(struct file *fp, u_long cmd, void *data)
225 {
226 struct fcrypt *fcr = fp->f_fcrypt;
227 struct csession *cse;
228 struct session_op *sop;
229 struct session_n_op *snop;
230 struct crypt_op *cop;
231 struct crypt_mop *mop;
232 struct crypt_mkop *mkop;
233 struct crypt_n_op *cnop;
234 struct crypt_n_kop *knop;
235 struct crypt_sgop *sgop;
236 struct crypt_sfop *sfop;
237 struct cryptret *crypt_ret;
238 struct crypt_result *crypt_res;
239 u_int32_t ses;
240 u_int32_t *sesid;
241 int error = 0;
242 size_t count;
243
244 /* backwards compatibility */
245 file_t *criofp;
246 struct fcrypt *criofcr;
247 int criofd;
248
249 mutex_enter(&crypto_mtx);
250 getnanotime(&fcr->atime);
251 mutex_exit(&crypto_mtx);
252
253 switch (cmd) {
254 case CRIOGET: /* XXX deprecated, remove after 5.0 */
255 if ((error = fd_allocfile(&criofp, &criofd)) != 0)
256 return error;
257 criofcr = pool_get(&fcrpl, PR_WAITOK);
258 mutex_enter(&crypto_mtx);
259 TAILQ_INIT(&criofcr->csessions);
260 TAILQ_INIT(&criofcr->crp_ret_mq);
261 TAILQ_INIT(&criofcr->crp_ret_mkq);
262 selinit(&criofcr->sinfo);
263
264 /*
265 * Don't ever return session 0, to allow detection of
266 * failed creation attempts with multi-create ioctl.
267 */
268 criofcr->sesn = 1;
269 criofcr->requestid = 1;
270 crypto_refcount++;
271 mutex_exit(&crypto_mtx);
272 (void)fd_clone(criofp, criofd, (FREAD|FWRITE),
273 &cryptofops, criofcr);
274 *(u_int32_t *)data = criofd;
275 return error;
276 break;
277 case CIOCGSESSION:
278 sop = (struct session_op *)data;
279 error = cryptodev_session(fcr, sop);
280 break;
281 case CIOCNGSESSION:
282 sgop = (struct crypt_sgop *)data;
283 snop = kmem_alloc((sgop->count *
284 sizeof(struct session_n_op)), KM_SLEEP);
285 error = copyin(sgop->sessions, snop, sgop->count *
286 sizeof(struct session_n_op));
287 if (error) {
288 goto mbail;
289 }
290
291 mutex_enter(&crypto_mtx);
292 fcr->mtime = fcr->atime;
293 mutex_exit(&crypto_mtx);
294 error = cryptodev_msession(fcr, snop, sgop->count);
295 if (error) {
296 goto mbail;
297 }
298
299 error = copyout(snop, sgop->sessions, sgop->count *
300 sizeof(struct session_n_op));
301 mbail:
302 kmem_free(snop, sgop->count * sizeof(struct session_n_op));
303 break;
304 case CIOCFSESSION:
305 mutex_enter(&crypto_mtx);
306 fcr->mtime = fcr->atime;
307 ses = *(u_int32_t *)data;
308 cse = csefind(fcr, ses);
309 if (cse == NULL) {
310 mutex_exit(&crypto_mtx);
311 return EINVAL;
312 }
313 csedelete(fcr, cse);
314 mutex_exit(&crypto_mtx);
315 error = csefree(cse);
316 break;
317 case CIOCNFSESSION:
318 mutex_enter(&crypto_mtx);
319 fcr->mtime = fcr->atime;
320 mutex_exit(&crypto_mtx);
321 sfop = (struct crypt_sfop *)data;
322 sesid = kmem_alloc((sfop->count * sizeof(u_int32_t)),
323 KM_SLEEP);
324 error = copyin(sfop->sesid, sesid,
325 (sfop->count * sizeof(u_int32_t)));
326 if (!error) {
327 error = cryptodev_msessionfin(fcr, sfop->count, sesid);
328 }
329 kmem_free(sesid, (sfop->count * sizeof(u_int32_t)));
330 break;
331 case CIOCCRYPT:
332 mutex_enter(&crypto_mtx);
333 fcr->mtime = fcr->atime;
334 cop = (struct crypt_op *)data;
335 cse = csefind(fcr, cop->ses);
336 mutex_exit(&crypto_mtx);
337 if (cse == NULL) {
338 DPRINTF(("csefind failed\n"));
339 return EINVAL;
340 }
341 error = cryptodev_op(cse, cop, curlwp);
342 DPRINTF(("cryptodev_op error = %d\n", error));
343 break;
344 case CIOCNCRYPTM:
345 mutex_enter(&crypto_mtx);
346 fcr->mtime = fcr->atime;
347 mutex_exit(&crypto_mtx);
348 mop = (struct crypt_mop *)data;
349 cnop = kmem_alloc((mop->count * sizeof(struct crypt_n_op)),
350 KM_SLEEP);
351 error = copyin(mop->reqs, cnop,
352 (mop->count * sizeof(struct crypt_n_op)));
353 if(!error) {
354 error = cryptodev_mop(fcr, cnop, mop->count, curlwp);
355 if (!error) {
356 error = copyout(cnop, mop->reqs,
357 (mop->count * sizeof(struct crypt_n_op)));
358 }
359 }
360 kmem_free(cnop, (mop->count * sizeof(struct crypt_n_op)));
361 break;
362 case CIOCKEY:
363 error = cryptodev_key((struct crypt_kop *)data);
364 DPRINTF(("cryptodev_key error = %d\n", error));
365 break;
366 case CIOCNFKEYM:
367 mutex_enter(&crypto_mtx);
368 fcr->mtime = fcr->atime;
369 mutex_exit(&crypto_mtx);
370 mkop = (struct crypt_mkop *)data;
371 knop = kmem_alloc((mkop->count * sizeof(struct crypt_n_kop)),
372 KM_SLEEP);
373 error = copyin(mkop->reqs, knop,
374 (mkop->count * sizeof(struct crypt_n_kop)));
375 if (!error) {
376 error = cryptodev_mkey(fcr, knop, mkop->count);
377 if (!error)
378 error = copyout(knop, mkop->reqs,
379 (mkop->count * sizeof(struct crypt_n_kop)));
380 }
381 kmem_free(knop, (mkop->count * sizeof(struct crypt_n_kop)));
382 break;
383 case CIOCASYMFEAT:
384 error = crypto_getfeat((int *)data);
385 break;
386 case CIOCNCRYPTRETM:
387 mutex_enter(&crypto_mtx);
388 fcr->mtime = fcr->atime;
389 mutex_exit(&crypto_mtx);
390 crypt_ret = (struct cryptret *)data;
391 count = crypt_ret->count;
392 crypt_res = kmem_alloc((count * sizeof(struct crypt_result)),
393 KM_SLEEP);
394 error = copyin(crypt_ret->results, crypt_res,
395 (count * sizeof(struct crypt_result)));
396 if (error)
397 goto reterr;
398 crypt_ret->count = cryptodev_getmstatus(fcr, crypt_res,
399 crypt_ret->count);
400 /* sanity check count */
401 if (crypt_ret->count > count) {
402 printf("%s.%d: error returned count %zd > original "
403 " count %zd\n",
404 __FILE__, __LINE__, crypt_ret->count, count);
405 crypt_ret->count = count;
406
407 }
408 error = copyout(crypt_res, crypt_ret->results,
409 (crypt_ret->count * sizeof(struct crypt_result)));
410 reterr:
411 kmem_free(crypt_res, (count * sizeof(struct crypt_result)));
412 break;
413 case CIOCNCRYPTRET:
414 error = cryptodev_getstatus(fcr, (struct crypt_result *)data);
415 break;
416 default:
417 #ifdef COMPAT_50
418 /* Check for backward compatible commands */
419 error = ocryptof_ioctl(fp, cmd, data);
420 #else
421 return EINVAL;
422 #endif
423 }
424 return error;
425 }
426
427 int
428 cryptodev_op(struct csession *cse, struct crypt_op *cop, struct lwp *l)
429 {
430 struct cryptop *crp = NULL;
431 struct cryptodesc *crde = NULL, *crda = NULL, *crdc = NULL;
432 int error;
433 int iov_len = cop->len;
434 int flags=0;
435 int dst_len; /* copyout size */
436
437 if (cop->len > 256*1024-4)
438 return E2BIG;
439
440 if (cse->txform) {
441 if (cop->len < cse->txform->blocksize
442 + (cop->iv ? 0 : cse->txform->ivsize) ||
443 (cop->len - (cop->iv ? 0 : cse->txform->ivsize))
444 % cse->txform->blocksize != 0)
445 return EINVAL;
446 }
447
448 DPRINTF(("cryptodev_op[%u]: iov_len %d\n",
449 CRYPTO_SESID2LID(cse->sid), iov_len));
450 if ((cse->tcomp) && cop->dst_len) {
451 if (iov_len < cop->dst_len) {
452 /* Need larger iov to deal with decompress */
453 iov_len = cop->dst_len;
454 }
455 DPRINTF(("cryptodev_op: iov_len -> %d for decompress\n", iov_len));
456 }
457
458 (void)memset(&cse->uio, 0, sizeof(cse->uio));
459 cse->uio.uio_iovcnt = 1;
460 cse->uio.uio_resid = 0;
461 cse->uio.uio_rw = UIO_WRITE;
462 cse->uio.uio_iov = cse->iovec;
463 UIO_SETUP_SYSSPACE(&cse->uio);
464 memset(&cse->iovec, 0, sizeof(cse->iovec));
465
466 /* the iov needs to be big enough to handle the uncompressed
467 * data.... */
468 cse->uio.uio_iov[0].iov_len = iov_len;
469 if (iov_len > 0)
470 cse->uio.uio_iov[0].iov_base = kmem_alloc(iov_len, KM_SLEEP);
471 cse->uio.uio_resid = cse->uio.uio_iov[0].iov_len;
472 DPRINTF(("cryptodev_op[%u]: uio.iov_base %p malloced %d bytes\n",
473 CRYPTO_SESID2LID(cse->sid),
474 cse->uio.uio_iov[0].iov_base, iov_len));
475
476 crp = crypto_getreq((cse->tcomp != NULL) + (cse->txform != NULL) + (cse->thash != NULL));
477 if (crp == NULL) {
478 error = ENOMEM;
479 goto bail;
480 }
481 DPRINTF(("cryptodev_op[%u]: crp %p\n",
482 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(("cryptodev_op[%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(("cryptodev_op: 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(("cryptodev_op: crde setup for cipher %d.\n", crde->crd_alg));
578 }
579
580
581 crp->crp_ilen = cop->len;
582 /* The reqest is flagged as CRYPTO_F_USER as long as it is running
583 * in the user IOCTL thread. This flag lets us skip using the retq for
584 * the request if it completes immediately. If the request ends up being
585 * delayed or is not completed immediately the flag is removed.
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 * If the request was going to be completed by the
648 * ioctl thread then it would have been done by now.
649 * Remove the F_USER flag so crypto_done() is not confused
650 * if the crypto device calls it after this point.
651 */
652 crp->crp_flags &= ~(CRYPTO_F_USER);
653
654 switch (error) {
655 #ifdef notyet /* don't loop forever -- but EAGAIN not possible here yet */
656 case EAGAIN:
657 mutex_exit(&crypto_mtx);
658 goto eagain;
659 break;
660 #endif
661 case 0:
662 break;
663 default:
664 DPRINTF(("cryptodev_op: not waiting, error.\n"));
665 mutex_exit(&crypto_mtx);
666 cv_destroy(&crp->crp_cv);
667 goto bail;
668 }
669
670 while (!(crp->crp_flags & CRYPTO_F_DONE)) {
671 DPRINTF(("cryptodev_op[%d]: sleeping on cv %p for crp %p\n",
672 (uint32_t)cse->sid, &crp->crp_cv, crp));
673 cv_wait(&crp->crp_cv, &crypto_mtx); /* XXX cv_wait_sig? */
674 }
675 if (crp->crp_flags & CRYPTO_F_ONRETQ) {
676 /* XXX this should never happen now with the CRYPTO_F_USER flag
677 * changes.
678 */
679 DPRINTF(("cryptodev_op: DONE, not woken by cryptoret.\n"));
680 (void)crypto_ret_q_remove(crp);
681 }
682 mutex_exit(&crypto_mtx);
683 cv_destroy(&crp->crp_cv);
684
685 if (crp->crp_etype != 0) {
686 DPRINTF(("cryptodev_op: crp_etype %d\n", crp->crp_etype));
687 error = crp->crp_etype;
688 goto bail;
689 }
690
691 if (cse->error) {
692 DPRINTF(("cryptodev_op: cse->error %d\n", cse->error));
693 error = cse->error;
694 goto bail;
695 }
696
697 dst_len = crp->crp_ilen;
698 /* let the user know how much data was returned */
699 if (crp->crp_olen) {
700 if (crp->crp_olen > (cop->dst_len ? cop->dst_len : cop->len)) {
701 error = ENOSPC;
702 goto bail;
703 }
704 dst_len = cop->dst_len = crp->crp_olen;
705 }
706
707 if (cop->dst) {
708 DPRINTF(("cryptodev_op: copyout %d bytes to %p\n", dst_len, cop->dst));
709 }
710 if (cop->dst &&
711 (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, dst_len)))
712 {
713 DPRINTF(("cryptodev_op: copyout error %d\n", error));
714 goto bail;
715 }
716
717 if (cop->mac &&
718 (error = copyout(crp->crp_mac, cop->mac, cse->thash->authsize))) {
719 DPRINTF(("cryptodev_op: mac copyout error %d\n", error));
720 goto bail;
721 }
722
723
724 bail:
725 if (crp) {
726 crypto_freereq(crp);
727 }
728 if (cse->uio.uio_iov[0].iov_base) {
729 kmem_free(cse->uio.uio_iov[0].iov_base,iov_len);
730 }
731
732 return error;
733 }
734
735 static int
736 cryptodev_cb(void *op)
737 {
738 struct cryptop *crp = (struct cryptop *) op;
739 struct csession *cse = (struct csession *)crp->crp_opaque;
740 int error = 0;
741
742 mutex_enter(&crypto_mtx);
743 cse->error = crp->crp_etype;
744 if (crp->crp_etype == EAGAIN) {
745 /* always drop mutex to call dispatch routine */
746 mutex_exit(&crypto_mtx);
747 error = crypto_dispatch(crp);
748 mutex_enter(&crypto_mtx);
749 }
750 if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
751 cv_signal(&crp->crp_cv);
752 }
753 mutex_exit(&crypto_mtx);
754 return 0;
755 }
756
757 static int
758 cryptodev_mcb(void *op)
759 {
760 struct cryptop *crp = (struct cryptop *) op;
761 struct csession *cse = (struct csession *)crp->crp_opaque;
762 int error=0;
763
764 mutex_enter(&crypto_mtx);
765 cse->error = crp->crp_etype;
766 if (crp->crp_etype == EAGAIN) {
767 mutex_exit(&crypto_mtx);
768 error = crypto_dispatch(crp);
769 mutex_enter(&crypto_mtx);
770 }
771 if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
772 cv_signal(&crp->crp_cv);
773 }
774
775 TAILQ_INSERT_TAIL(&crp->fcrp->crp_ret_mq, crp, crp_next);
776 selnotify(&crp->fcrp->sinfo, 0, 0);
777 mutex_exit(&crypto_mtx);
778 return 0;
779 }
780
781 static int
782 cryptodevkey_cb(void *op)
783 {
784 struct cryptkop *krp = op;
785
786 mutex_enter(&crypto_mtx);
787 cv_signal(&krp->krp_cv);
788 mutex_exit(&crypto_mtx);
789 return 0;
790 }
791
792 static int
793 cryptodevkey_mcb(void *op)
794 {
795 struct cryptkop *krp = op;
796
797 mutex_enter(&crypto_mtx);
798 cv_signal(&krp->krp_cv);
799 TAILQ_INSERT_TAIL(&krp->fcrp->crp_ret_mkq, krp, krp_next);
800 selnotify(&krp->fcrp->sinfo, 0, 0);
801 mutex_exit(&crypto_mtx);
802 return 0;
803 }
804
805 static int
806 cryptodev_key(struct crypt_kop *kop)
807 {
808 struct cryptkop *krp = NULL;
809 int error = EINVAL;
810 int in, out, size, i;
811
812 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM)
813 return EFBIG;
814
815 in = kop->crk_iparams;
816 out = kop->crk_oparams;
817 switch (kop->crk_op) {
818 case CRK_MOD_EXP:
819 if (in == 3 && out == 1)
820 break;
821 return EINVAL;
822 case CRK_MOD_EXP_CRT:
823 if (in == 6 && out == 1)
824 break;
825 return EINVAL;
826 case CRK_DSA_SIGN:
827 if (in == 5 && out == 2)
828 break;
829 return EINVAL;
830 case CRK_DSA_VERIFY:
831 if (in == 7 && out == 0)
832 break;
833 return EINVAL;
834 case CRK_DH_COMPUTE_KEY:
835 if (in == 3 && out == 1)
836 break;
837 return EINVAL;
838 case CRK_MOD_ADD:
839 if (in == 3 && out == 1)
840 break;
841 return EINVAL;
842 case CRK_MOD_ADDINV:
843 if (in == 2 && out == 1)
844 break;
845 return EINVAL;
846 case CRK_MOD_SUB:
847 if (in == 3 && out == 1)
848 break;
849 return EINVAL;
850 case CRK_MOD_MULT:
851 if (in == 3 && out == 1)
852 break;
853 return EINVAL;
854 case CRK_MOD_MULTINV:
855 if (in == 2 && out == 1)
856 break;
857 return EINVAL;
858 case CRK_MOD:
859 if (in == 2 && out == 1)
860 break;
861 return EINVAL;
862 default:
863 return EINVAL;
864 }
865
866 krp = pool_get(&cryptkop_pool, PR_WAITOK);
867 (void)memset(krp, 0, sizeof *krp);
868 cv_init(&krp->krp_cv, "crykdev");
869 krp->krp_op = kop->crk_op;
870 krp->krp_status = kop->crk_status;
871 krp->krp_iparams = kop->crk_iparams;
872 krp->krp_oparams = kop->crk_oparams;
873 krp->krp_status = 0;
874 krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
875
876 for (i = 0; i < CRK_MAXPARAM; i++)
877 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
878 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
879 size = (krp->krp_param[i].crp_nbits + 7) / 8;
880 if (size == 0)
881 continue;
882 krp->krp_param[i].crp_p = kmem_alloc(size, KM_SLEEP);
883 if (i >= krp->krp_iparams)
884 continue;
885 error = copyin(kop->crk_param[i].crp_p,
886 krp->krp_param[i].crp_p, size);
887 if (error)
888 goto fail;
889 }
890
891 error = crypto_kdispatch(krp);
892 if (error != 0) {
893 goto fail;
894 }
895
896 mutex_enter(&crypto_mtx);
897 while (!(krp->krp_flags & CRYPTO_F_DONE)) {
898 cv_wait(&krp->krp_cv, &crypto_mtx); /* XXX cv_wait_sig? */
899 }
900 if (krp->krp_flags & CRYPTO_F_ONRETQ) {
901 DPRINTF(("cryptodev_key: DONE early, not via cryptoret.\n"));
902 (void)crypto_ret_kq_remove(krp);
903 }
904 mutex_exit(&crypto_mtx);
905
906 if (krp->krp_status != 0) {
907 DPRINTF(("cryptodev_key: krp->krp_status 0x%08x\n",
908 krp->krp_status));
909 error = krp->krp_status;
910 goto fail;
911 }
912
913 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams;
914 i++) {
915 size = (krp->krp_param[i].crp_nbits + 7) / 8;
916 if (size == 0)
917 continue;
918 error = copyout(krp->krp_param[i].crp_p,
919 kop->crk_param[i].crp_p, size);
920 if (error) {
921 DPRINTF(("cryptodev_key: copyout oparam %d failed, "
922 "error=%d\n", i-krp->krp_iparams, error));
923 goto fail;
924 }
925 }
926
927 fail:
928 kop->crk_status = krp->krp_status;
929 for (i = 0; i < CRK_MAXPARAM; i++) {
930 struct crparam *kp = &(krp->krp_param[i]);
931 if (krp->krp_param[i].crp_p) {
932 size = (kp->crp_nbits + 7) / 8;
933 KASSERT(size > 0);
934 (void)memset(kp->crp_p, 0, size);
935 kmem_free(kp->crp_p, size);
936 }
937 }
938 cv_destroy(&krp->krp_cv);
939 pool_put(&cryptkop_pool, krp);
940 DPRINTF(("cryptodev_key: error=0x%08x\n", error));
941 return error;
942 }
943
944 /* ARGSUSED */
945 static int
946 cryptof_close(struct file *fp)
947 {
948 struct fcrypt *fcr = fp->f_fcrypt;
949 struct csession *cse;
950
951 mutex_enter(&crypto_mtx);
952 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
953 TAILQ_REMOVE(&fcr->csessions, cse, next);
954 mutex_exit(&crypto_mtx);
955 (void)csefree(cse);
956 mutex_enter(&crypto_mtx);
957 }
958 seldestroy(&fcr->sinfo);
959 fp->f_fcrypt = NULL;
960 crypto_refcount--;
961 mutex_exit(&crypto_mtx);
962
963 pool_put(&fcrpl, fcr);
964 return 0;
965 }
966
967 /* needed for compatibility module */
968 struct csession *cryptodev_csefind(struct fcrypt *fcr, u_int ses)
969 {
970 return csefind(fcr, ses);
971 }
972
973 /* csefind: call with crypto_mtx held. */
974 static struct csession *
975 csefind(struct fcrypt *fcr, u_int ses)
976 {
977 struct csession *cse, *cnext, *ret = NULL;
978
979 KASSERT(mutex_owned(&crypto_mtx));
980 TAILQ_FOREACH_SAFE(cse, &fcr->csessions, next, cnext)
981 if (cse->ses == ses)
982 ret = cse;
983
984 return ret;
985 }
986
987 /* csedelete: call with crypto_mtx held. */
988 static int
989 csedelete(struct fcrypt *fcr, struct csession *cse_del)
990 {
991 struct csession *cse, *cnext;
992 int ret = 0;
993
994 KASSERT(mutex_owned(&crypto_mtx));
995 TAILQ_FOREACH_SAFE(cse, &fcr->csessions, next, cnext) {
996 if (cse == cse_del) {
997 TAILQ_REMOVE(&fcr->csessions, cse, next);
998 ret = 1;
999 }
1000 }
1001 return ret;
1002 }
1003
1004 static struct csession *
1005 cseadd(struct fcrypt *fcr, struct csession *cse)
1006 {
1007 mutex_enter(&crypto_mtx);
1008 /* don't let session ID wrap! */
1009 if (fcr->sesn + 1 == 0) return NULL;
1010 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
1011 cse->ses = fcr->sesn++;
1012 mutex_exit(&crypto_mtx);
1013 return cse;
1014 }
1015
1016 static struct csession *
1017 csecreate(struct fcrypt *fcr, u_int64_t sid, void *key, u_int64_t keylen,
1018 void *mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
1019 u_int32_t comp_alg, const struct enc_xform *txform,
1020 const struct auth_hash *thash, const struct comp_algo *tcomp)
1021 {
1022 struct csession *cse;
1023
1024 cse = pool_get(&csepl, PR_NOWAIT);
1025 if (cse == NULL)
1026 return NULL;
1027 cse->key = key;
1028 cse->keylen = keylen/8;
1029 cse->mackey = mackey;
1030 cse->mackeylen = mackeylen/8;
1031 cse->sid = sid;
1032 cse->cipher = cipher;
1033 cse->mac = mac;
1034 cse->comp_alg = comp_alg;
1035 cse->txform = txform;
1036 cse->thash = thash;
1037 cse->tcomp = tcomp;
1038 cse->error = 0;
1039 if (cseadd(fcr, cse))
1040 return cse;
1041 else {
1042 pool_put(&csepl, cse);
1043 return NULL;
1044 }
1045 }
1046
1047 /* csefree: call with crypto_mtx held. */
1048 static int
1049 csefree(struct csession *cse)
1050 {
1051 int error;
1052
1053 error = crypto_freesession(cse->sid);
1054 if (cse->key)
1055 free(cse->key, M_XDATA);
1056 if (cse->mackey)
1057 free(cse->mackey, M_XDATA);
1058 pool_put(&csepl, cse);
1059 return error;
1060 }
1061
1062 static int
1063 cryptoopen(dev_t dev, int flag, int mode,
1064 struct lwp *l)
1065 {
1066 file_t *fp;
1067 struct fcrypt *fcr;
1068 int fd, error;
1069
1070 if (crypto_usercrypto == 0)
1071 return ENXIO;
1072
1073 if ((error = fd_allocfile(&fp, &fd)) != 0)
1074 return error;
1075
1076 fcr = pool_get(&fcrpl, PR_WAITOK);
1077 getnanotime(&fcr->btime);
1078 fcr->atime = fcr->mtime = fcr->btime;
1079 mutex_enter(&crypto_mtx);
1080 TAILQ_INIT(&fcr->csessions);
1081 TAILQ_INIT(&fcr->crp_ret_mq);
1082 TAILQ_INIT(&fcr->crp_ret_mkq);
1083 selinit(&fcr->sinfo);
1084 /*
1085 * Don't ever return session 0, to allow detection of
1086 * failed creation attempts with multi-create ioctl.
1087 */
1088 fcr->sesn = 1;
1089 fcr->requestid = 1;
1090 crypto_refcount++;
1091 mutex_exit(&crypto_mtx);
1092 return fd_clone(fp, fd, flag, &cryptofops, fcr);
1093 }
1094
1095 static int
1096 cryptoread(dev_t dev, struct uio *uio, int ioflag)
1097 {
1098 return EIO;
1099 }
1100
1101 static int
1102 cryptowrite(dev_t dev, struct uio *uio, int ioflag)
1103 {
1104 return EIO;
1105 }
1106
1107 int
1108 cryptoselect(dev_t dev, int rw, struct lwp *l)
1109 {
1110 return 0;
1111 }
1112
1113 #ifdef _MODULE
1114 struct localcount crypto_localcount;
1115 #endif
1116
1117 /*static*/
1118 struct cdevsw crypto_cdevsw = {
1119 .d_open = cryptoopen,
1120 .d_close = noclose,
1121 .d_read = cryptoread,
1122 .d_write = cryptowrite,
1123 .d_ioctl = noioctl,
1124 .d_stop = nostop,
1125 .d_tty = notty,
1126 .d_poll = cryptoselect /*nopoll*/,
1127 .d_mmap = nommap,
1128 .d_kqfilter = nokqfilter,
1129 .d_discard = nodiscard,
1130 #ifdef _MODULE
1131 .d_localcount = &crypto_localcount,
1132 #endif
1133 .d_flag = D_OTHER
1134 };
1135
1136 int
1137 cryptodev_mop(struct fcrypt *fcr,
1138 struct crypt_n_op * cnop,
1139 int count, struct lwp *l)
1140 {
1141 struct cryptop *crp = NULL;
1142 struct cryptodesc *crde = NULL, *crda = NULL, *crdc = NULL;
1143 int req, error=0;
1144 struct csession *cse;
1145 int flags=0;
1146 int iov_len;
1147
1148 for (req = 0; req < count; req++) {
1149 mutex_enter(&crypto_mtx);
1150 cse = csefind(fcr, cnop[req].ses);
1151 if (cse == NULL) {
1152 DPRINTF(("csefind failed\n"));
1153 cnop[req].status = EINVAL;
1154 mutex_exit(&crypto_mtx);
1155 continue;
1156 }
1157 mutex_exit(&crypto_mtx);
1158
1159 if (cnop[req].len > 256*1024-4) {
1160 DPRINTF(("length failed\n"));
1161 cnop[req].status = EINVAL;
1162 continue;
1163 }
1164 if (cse->txform) {
1165 if (cnop[req].len < cse->txform->blocksize -
1166 (cnop[req].iv ? 0 : cse->txform->ivsize) ||
1167 (cnop[req].len -
1168 (cnop[req].iv ? 0 : cse->txform->ivsize))
1169 % cse->txform->blocksize) {
1170 cnop[req].status = EINVAL;
1171 continue;
1172 }
1173 }
1174
1175 crp = crypto_getreq((cse->txform != NULL) +
1176 (cse->thash != NULL) +
1177 (cse->tcomp != NULL));
1178 if (crp == NULL) {
1179 cnop[req].status = ENOMEM;
1180 goto bail;
1181 }
1182
1183 iov_len = cnop[req].len;
1184 /* got a compression/decompression max size? */
1185 if ((cse->tcomp) && cnop[req].dst_len) {
1186 if (iov_len < cnop[req].dst_len) {
1187 /* Need larger iov to deal with decompress */
1188 iov_len = cnop[req].dst_len;
1189 }
1190 DPRINTF(("cryptodev_mop: iov_len -> %d for decompress\n", iov_len));
1191 }
1192
1193 (void)memset(&crp->uio, 0, sizeof(crp->uio));
1194 crp->uio.uio_iovcnt = 1;
1195 crp->uio.uio_resid = 0;
1196 crp->uio.uio_rw = UIO_WRITE;
1197 crp->uio.uio_iov = crp->iovec;
1198 UIO_SETUP_SYSSPACE(&crp->uio);
1199 memset(&crp->iovec, 0, sizeof(crp->iovec));
1200 crp->uio.uio_iov[0].iov_len = iov_len;
1201 DPRINTF(("cryptodev_mop: kmem_alloc(%d) for iov \n", iov_len));
1202 crp->uio.uio_iov[0].iov_base = kmem_alloc(iov_len, KM_SLEEP);
1203 crp->uio.uio_resid = crp->uio.uio_iov[0].iov_len;
1204
1205 if (cse->tcomp) {
1206 crdc = crp->crp_desc;
1207 }
1208
1209 if (cse->thash) {
1210 crda = crdc ? crdc->crd_next : crp->crp_desc;
1211 if (cse->txform && crda)
1212 crde = crda->crd_next;
1213 } else {
1214 if (cse->txform) {
1215 crde = crdc ? crdc->crd_next : crp->crp_desc;
1216 } else if (!cse->tcomp) {
1217 error = EINVAL;
1218 goto bail;
1219 }
1220 }
1221
1222 if ((copyin(cnop[req].src,
1223 crp->uio.uio_iov[0].iov_base, cnop[req].len))) {
1224 cnop[req].status = EINVAL;
1225 goto bail;
1226 }
1227
1228 if (crdc) {
1229 switch (cnop[req].op) {
1230 case COP_COMP:
1231 crdc->crd_flags |= CRD_F_COMP;
1232 break;
1233 case COP_DECOMP:
1234 crdc->crd_flags &= ~CRD_F_COMP;
1235 break;
1236 default:
1237 break;
1238 }
1239 /* more data to follow? */
1240 if (cnop[req].flags & COP_F_MORE) {
1241 flags |= CRYPTO_F_MORE;
1242 }
1243 crdc->crd_len = cnop[req].len;
1244 crdc->crd_inject = 0;
1245
1246 crdc->crd_alg = cse->comp_alg;
1247 crdc->crd_key = NULL;
1248 crdc->crd_klen = 0;
1249 DPRINTF(("cryptodev_mop[%d]: crdc setup for comp_alg %d"
1250 " len %d.\n",
1251 (uint32_t)cse->sid, crdc->crd_alg,
1252 crdc->crd_len));
1253 }
1254
1255 if (crda) {
1256 crda->crd_skip = 0;
1257 crda->crd_len = cnop[req].len;
1258 crda->crd_inject = 0; /* ??? */
1259
1260 crda->crd_alg = cse->mac;
1261 crda->crd_key = cse->mackey;
1262 crda->crd_klen = cse->mackeylen * 8;
1263 }
1264
1265 if (crde) {
1266 if (cnop[req].op == COP_ENCRYPT)
1267 crde->crd_flags |= CRD_F_ENCRYPT;
1268 else
1269 crde->crd_flags &= ~CRD_F_ENCRYPT;
1270 crde->crd_len = cnop[req].len;
1271 crde->crd_inject = 0;
1272
1273 crde->crd_alg = cse->cipher;
1274 #ifdef notyet /* XXX must notify h/w driver new key, drain */
1275 if(cnop[req].key && cnop[req].keylen) {
1276 crde->crd_key = malloc(cnop[req].keylen,
1277 M_XDATA, M_WAITOK);
1278 if((error = copyin(cnop[req].key,
1279 crde->crd_key, cnop[req].keylen))) {
1280 cnop[req].status = EINVAL;
1281 goto bail;
1282 }
1283 crde->crd_klen = cnop[req].keylen * 8;
1284 } else { ... }
1285 #endif
1286 crde->crd_key = cse->key;
1287 crde->crd_klen = cse->keylen * 8;
1288 }
1289
1290 crp->crp_ilen = cnop[req].len;
1291 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM |
1292 (cnop[req].flags & COP_F_BATCH) | flags;
1293 crp->crp_buf = (void *)&crp->uio;
1294 crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_mcb;
1295 crp->crp_sid = cse->sid;
1296 crp->crp_opaque = (void *)cse;
1297 crp->fcrp = fcr;
1298 crp->dst = cnop[req].dst;
1299 crp->len = cnop[req].len; /* input len, iov may be larger */
1300 crp->mac = cnop[req].mac;
1301 DPRINTF(("cryptodev_mop: iov_base %p dst %p len %d mac %p\n",
1302 crp->uio.uio_iov[0].iov_base, crp->dst, crp->len,
1303 crp->mac));
1304
1305 if (cnop[req].iv) {
1306 if (crde == NULL) {
1307 cnop[req].status = EINVAL;
1308 goto bail;
1309 }
1310 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
1311 cnop[req].status = EINVAL;
1312 goto bail;
1313 }
1314 if ((error = copyin(cnop[req].iv, crp->tmp_iv,
1315 cse->txform->ivsize))) {
1316 cnop[req].status = EINVAL;
1317 goto bail;
1318 }
1319 (void)memcpy(crde->crd_iv, crp->tmp_iv,
1320 cse->txform->ivsize);
1321 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
1322 crde->crd_skip = 0;
1323 } else if (crde) {
1324 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
1325 crde->crd_skip = 0;
1326 } else {
1327 if (!(crde->crd_flags & CRD_F_ENCRYPT))
1328 crde->crd_flags |= CRD_F_IV_PRESENT;
1329 crde->crd_skip = cse->txform->ivsize;
1330 crde->crd_len -= cse->txform->ivsize;
1331 }
1332 }
1333
1334 if (cnop[req].mac) {
1335 if (crda == NULL) {
1336 cnop[req].status = EINVAL;
1337 goto bail;
1338 }
1339 crp->crp_mac=cse->tmp_mac;
1340 }
1341 cnop[req].reqid = atomic_inc_32_nv(&(fcr->requestid));
1342 crp->crp_reqid = cnop[req].reqid;
1343 crp->crp_usropaque = cnop[req].opaque;
1344 cv_init(&crp->crp_cv, "crydev");
1345 #ifdef notyet
1346 eagain:
1347 #endif
1348 cnop[req].status = crypto_dispatch(crp);
1349 mutex_enter(&crypto_mtx); /* XXX why mutex? */
1350
1351 switch (cnop[req].status) {
1352 #ifdef notyet /* don't loop forever -- but EAGAIN not possible here yet */
1353 case EAGAIN:
1354 mutex_exit(&crypto_mtx);
1355 goto eagain;
1356 break;
1357 #endif
1358 case 0:
1359 break;
1360 default:
1361 DPRINTF(("cryptodev_op: not waiting, error.\n"));
1362 mutex_exit(&crypto_mtx);
1363 cv_destroy(&crp->crp_cv);
1364 goto bail;
1365 }
1366
1367 mutex_exit(&crypto_mtx);
1368 cv_destroy(&crp->crp_cv);
1369 bail:
1370 if (cnop[req].status) {
1371 if (crp) {
1372 if (crp->uio.uio_iov[0].iov_base) {
1373 kmem_free(crp->uio.uio_iov[0].iov_base,
1374 crp->uio.uio_iov[0].iov_len);
1375 }
1376 crypto_freereq(crp);
1377 }
1378 error = 0;
1379 }
1380 }
1381 return error;
1382 }
1383
1384 static int
1385 cryptodev_mkey(struct fcrypt *fcr, struct crypt_n_kop *kop, int count)
1386 {
1387 struct cryptkop *krp = NULL;
1388 int error = EINVAL;
1389 int in, out, size, i, req;
1390
1391 for (req = 0; req < count; req++) {
1392 if (kop[req].crk_iparams + kop[req].crk_oparams > CRK_MAXPARAM)
1393 return EFBIG;
1394
1395 in = kop[req].crk_iparams;
1396 out = kop[req].crk_oparams;
1397 switch (kop[req].crk_op) {
1398 case CRK_MOD_EXP:
1399 if (in == 3 && out == 1)
1400 break;
1401 kop[req].crk_status = EINVAL;
1402 continue;
1403 case CRK_MOD_EXP_CRT:
1404 if (in == 6 && out == 1)
1405 break;
1406 kop[req].crk_status = EINVAL;
1407 continue;
1408 case CRK_DSA_SIGN:
1409 if (in == 5 && out == 2)
1410 break;
1411 kop[req].crk_status = EINVAL;
1412 continue;
1413 case CRK_DSA_VERIFY:
1414 if (in == 7 && out == 0)
1415 break;
1416 kop[req].crk_status = EINVAL;
1417 continue;
1418 case CRK_DH_COMPUTE_KEY:
1419 if (in == 3 && out == 1)
1420 break;
1421 kop[req].crk_status = EINVAL;
1422 continue;
1423 case CRK_MOD_ADD:
1424 if (in == 3 && out == 1)
1425 break;
1426 kop[req].crk_status = EINVAL;
1427 continue;
1428 case CRK_MOD_ADDINV:
1429 if (in == 2 && out == 1)
1430 break;
1431 kop[req].crk_status = EINVAL;
1432 continue;
1433 case CRK_MOD_SUB:
1434 if (in == 3 && out == 1)
1435 break;
1436 kop[req].crk_status = EINVAL;
1437 continue;
1438 case CRK_MOD_MULT:
1439 if (in == 3 && out == 1)
1440 break;
1441 kop[req].crk_status = EINVAL;
1442 continue;
1443 case CRK_MOD_MULTINV:
1444 if (in == 2 && out == 1)
1445 break;
1446 kop[req].crk_status = EINVAL;
1447 continue;
1448 case CRK_MOD:
1449 if (in == 2 && out == 1)
1450 break;
1451 kop[req].crk_status = EINVAL;
1452 continue;
1453 default:
1454 kop[req].crk_status = EINVAL;
1455 continue;
1456 }
1457
1458 krp = pool_get(&cryptkop_pool, PR_WAITOK);
1459 (void)memset(krp, 0, sizeof *krp);
1460 cv_init(&krp->krp_cv, "crykdev");
1461 krp->krp_op = kop[req].crk_op;
1462 krp->krp_status = kop[req].crk_status;
1463 krp->krp_iparams = kop[req].crk_iparams;
1464 krp->krp_oparams = kop[req].crk_oparams;
1465 krp->krp_status = 0;
1466 krp->krp_callback =
1467 (int (*) (struct cryptkop *)) cryptodevkey_mcb;
1468 (void)memcpy(krp->crk_param, kop[req].crk_param,
1469 sizeof(kop[req].crk_param));
1470
1471 krp->krp_flags = CRYPTO_F_CBIMM;
1472
1473 for (i = 0; i < CRK_MAXPARAM; i++)
1474 krp->krp_param[i].crp_nbits =
1475 kop[req].crk_param[i].crp_nbits;
1476 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
1477 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1478 if (size == 0)
1479 continue;
1480 krp->krp_param[i].crp_p =
1481 kmem_alloc(size, KM_SLEEP);
1482 if (i >= krp->krp_iparams)
1483 continue;
1484 kop[req].crk_status =
1485 copyin(kop[req].crk_param[i].crp_p,
1486 krp->krp_param[i].crp_p, size);
1487 if (kop[req].crk_status)
1488 goto fail;
1489 }
1490 krp->fcrp = fcr;
1491
1492 kop[req].crk_reqid = atomic_inc_32_nv(&(fcr->requestid));
1493 krp->krp_reqid = kop[req].crk_reqid;
1494 krp->krp_usropaque = kop[req].crk_opaque;
1495
1496 kop[req].crk_status = crypto_kdispatch(krp);
1497 if (kop[req].crk_status != 0) {
1498 goto fail;
1499 }
1500
1501 fail:
1502 if(kop[req].crk_status) {
1503 if (krp) {
1504 kop[req].crk_status = krp->krp_status;
1505 for (i = 0; i < CRK_MAXPARAM; i++) {
1506 struct crparam *kp =
1507 &(krp->krp_param[i]);
1508 if (kp->crp_p) {
1509 size = (kp->crp_nbits + 7) / 8;
1510 KASSERT(size > 0);
1511 memset(kp->crp_p, 0, size);
1512 kmem_free(kp->crp_p, size);
1513 }
1514 }
1515 cv_destroy(&krp->krp_cv);
1516 pool_put(&cryptkop_pool, krp);
1517 }
1518 }
1519 error = 0;
1520 }
1521 DPRINTF(("cryptodev_key: error=0x%08x\n", error));
1522 return error;
1523 }
1524
1525 int
1526 cryptodev_session(struct fcrypt *fcr, struct session_op *sop)
1527 {
1528 struct cryptoini cria, crie;
1529 struct cryptoini cric; /* compressor */
1530 struct cryptoini *crihead = NULL;
1531 const struct enc_xform *txform = NULL;
1532 const struct auth_hash *thash = NULL;
1533 const struct comp_algo *tcomp = NULL;
1534 struct csession *cse;
1535 u_int64_t sid;
1536 int error = 0;
1537
1538 DPRINTF(("cryptodev_session() cipher=%d, mac=%d\n", sop->cipher, sop->mac));
1539
1540 /* XXX there must be a way to not embed the list of xforms here */
1541 switch (sop->cipher) {
1542 case 0:
1543 break;
1544 case CRYPTO_DES_CBC:
1545 txform = &enc_xform_des;
1546 break;
1547 case CRYPTO_3DES_CBC:
1548 txform = &enc_xform_3des;
1549 break;
1550 case CRYPTO_BLF_CBC:
1551 txform = &enc_xform_blf;
1552 break;
1553 case CRYPTO_CAST_CBC:
1554 txform = &enc_xform_cast5;
1555 break;
1556 case CRYPTO_SKIPJACK_CBC:
1557 txform = &enc_xform_skipjack;
1558 break;
1559 case CRYPTO_AES_CBC:
1560 txform = &enc_xform_rijndael128;
1561 break;
1562 case CRYPTO_CAMELLIA_CBC:
1563 txform = &enc_xform_camellia;
1564 break;
1565 case CRYPTO_AES_CTR:
1566 txform = &enc_xform_aes_ctr;
1567 break;
1568 case CRYPTO_AES_GCM_16:
1569 txform = &enc_xform_aes_gcm;
1570 break;
1571 case CRYPTO_AES_GMAC:
1572 txform = &enc_xform_aes_gmac;
1573 break;
1574 case CRYPTO_NULL_CBC:
1575 txform = &enc_xform_null;
1576 break;
1577 case CRYPTO_ARC4:
1578 txform = &enc_xform_arc4;
1579 break;
1580 default:
1581 DPRINTF(("Invalid cipher %d\n", sop->cipher));
1582 return EINVAL;
1583 }
1584
1585 switch (sop->comp_alg) {
1586 case 0:
1587 break;
1588 case CRYPTO_DEFLATE_COMP:
1589 tcomp = &comp_algo_deflate;
1590 break;
1591 case CRYPTO_GZIP_COMP:
1592 tcomp = &comp_algo_gzip;
1593 DPRINTF(("cryptodev_session() tcomp for GZIP\n"));
1594 break;
1595 default:
1596 DPRINTF(("Invalid compression alg %d\n", sop->comp_alg));
1597 return EINVAL;
1598 }
1599
1600 switch (sop->mac) {
1601 case 0:
1602 break;
1603 case CRYPTO_MD5_HMAC:
1604 thash = &auth_hash_hmac_md5;
1605 break;
1606 case CRYPTO_SHA1_HMAC:
1607 thash = &auth_hash_hmac_sha1;
1608 break;
1609 case CRYPTO_MD5_HMAC_96:
1610 thash = &auth_hash_hmac_md5_96;
1611 break;
1612 case CRYPTO_SHA1_HMAC_96:
1613 thash = &auth_hash_hmac_sha1_96;
1614 break;
1615 case CRYPTO_SHA2_HMAC:
1616 /* XXX switching on key length seems questionable */
1617 if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize) {
1618 thash = &auth_hash_hmac_sha2_256;
1619 } else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize) {
1620 thash = &auth_hash_hmac_sha2_384;
1621 } else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize) {
1622 thash = &auth_hash_hmac_sha2_512;
1623 } else {
1624 DPRINTF(("Invalid mackeylen %d\n", sop->mackeylen));
1625 return EINVAL;
1626 }
1627 break;
1628 case CRYPTO_RIPEMD160_HMAC:
1629 thash = &auth_hash_hmac_ripemd_160;
1630 break;
1631 case CRYPTO_RIPEMD160_HMAC_96:
1632 thash = &auth_hash_hmac_ripemd_160_96;
1633 break;
1634 case CRYPTO_MD5:
1635 thash = &auth_hash_md5;
1636 break;
1637 case CRYPTO_SHA1:
1638 thash = &auth_hash_sha1;
1639 break;
1640 case CRYPTO_AES_XCBC_MAC_96:
1641 thash = &auth_hash_aes_xcbc_mac_96;
1642 break;
1643 case CRYPTO_AES_128_GMAC:
1644 thash = &auth_hash_gmac_aes_128;
1645 break;
1646 case CRYPTO_AES_192_GMAC:
1647 thash = &auth_hash_gmac_aes_192;
1648 break;
1649 case CRYPTO_AES_256_GMAC:
1650 thash = &auth_hash_gmac_aes_256;
1651 break;
1652 case CRYPTO_NULL_HMAC:
1653 thash = &auth_hash_null;
1654 break;
1655 default:
1656 DPRINTF(("Invalid mac %d\n", sop->mac));
1657 return EINVAL;
1658 }
1659
1660 memset(&crie, 0, sizeof(crie));
1661 memset(&cria, 0, sizeof(cria));
1662 memset(&cric, 0, sizeof(cric));
1663
1664 if (tcomp) {
1665 cric.cri_alg = tcomp->type;
1666 cric.cri_klen = 0;
1667 DPRINTF(("tcomp->type = %d\n", tcomp->type));
1668
1669 crihead = &cric;
1670 if (txform) {
1671 cric.cri_next = &crie;
1672 } else if (thash) {
1673 cric.cri_next = &cria;
1674 }
1675 }
1676
1677 if (txform) {
1678 crie.cri_alg = txform->type;
1679 crie.cri_klen = sop->keylen * 8;
1680 if (sop->keylen > txform->maxkey ||
1681 sop->keylen < txform->minkey) {
1682 DPRINTF(("keylen %d not in [%d,%d]\n",
1683 sop->keylen, txform->minkey, txform->maxkey));
1684 error = EINVAL;
1685 goto bail;
1686 }
1687
1688 crie.cri_key = malloc(crie.cri_klen / 8, M_XDATA, M_WAITOK);
1689 if ((error = copyin(sop->key, crie.cri_key, crie.cri_klen / 8)))
1690 goto bail;
1691 if (!crihead) {
1692 crihead = &crie;
1693 }
1694 if (thash)
1695 crie.cri_next = &cria;
1696 }
1697
1698 if (thash) {
1699 cria.cri_alg = thash->type;
1700 cria.cri_klen = sop->mackeylen * 8;
1701 if (sop->mackeylen != thash->keysize) {
1702 DPRINTF(("mackeylen %d != keysize %d\n",
1703 sop->mackeylen, thash->keysize));
1704 error = EINVAL;
1705 goto bail;
1706 }
1707 if (cria.cri_klen) {
1708 cria.cri_key = malloc(cria.cri_klen / 8, M_XDATA,
1709 M_WAITOK);
1710 if ((error = copyin(sop->mackey, cria.cri_key,
1711 cria.cri_klen / 8))) {
1712 goto bail;
1713 }
1714 }
1715 if (!crihead) {
1716 crihead = &cria;
1717 }
1718 }
1719
1720 error = crypto_newsession(&sid, crihead, crypto_devallowsoft);
1721 if (!error) {
1722 DPRINTF(("cryptodev_session: got session %d\n", (uint32_t)sid));
1723 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
1724 cria.cri_key, cria.cri_klen, (txform ? sop->cipher : 0), sop->mac,
1725 (tcomp ? sop->comp_alg : 0), txform, thash, tcomp);
1726 if (cse != NULL) {
1727 sop->ses = cse->ses;
1728 } else {
1729 DPRINTF(("csecreate failed\n"));
1730 crypto_freesession(sid);
1731 error = EINVAL;
1732 }
1733 } else {
1734 DPRINTF(("SIOCSESSION violates kernel parameters %d\n",
1735 error));
1736 }
1737 bail:
1738 if (error) {
1739 if (crie.cri_key) {
1740 memset(crie.cri_key, 0, crie.cri_klen / 8);
1741 free(crie.cri_key, M_XDATA);
1742 }
1743 if (cria.cri_key) {
1744 memset(cria.cri_key, 0, cria.cri_klen / 8);
1745 free(cria.cri_key, M_XDATA);
1746 }
1747 }
1748 return error;
1749 }
1750
1751 int
1752 cryptodev_msession(struct fcrypt *fcr, struct session_n_op *sn_ops,
1753 int count)
1754 {
1755 int i;
1756
1757 for (i = 0; i < count; i++, sn_ops++) {
1758 struct session_op s_op;
1759 s_op.cipher = sn_ops->cipher;
1760 s_op.mac = sn_ops->mac;
1761 s_op.keylen = sn_ops->keylen;
1762 s_op.key = sn_ops->key;
1763 s_op.mackeylen = sn_ops->mackeylen;
1764 s_op.mackey = sn_ops->mackey;
1765
1766 sn_ops->status = cryptodev_session(fcr, &s_op);
1767 sn_ops->ses = s_op.ses;
1768 }
1769
1770 return 0;
1771 }
1772
1773 static int
1774 cryptodev_msessionfin(struct fcrypt *fcr, int count, u_int32_t *sesid)
1775 {
1776 struct csession *cse;
1777 int req, error = 0;
1778
1779 mutex_enter(&crypto_mtx);
1780 for(req = 0; req < count; req++) {
1781 cse = csefind(fcr, sesid[req]);
1782 if (cse == NULL)
1783 continue;
1784 csedelete(fcr, cse);
1785 mutex_exit(&crypto_mtx);
1786 error = csefree(cse);
1787 mutex_enter(&crypto_mtx);
1788 }
1789 mutex_exit(&crypto_mtx);
1790 return error;
1791 }
1792
1793 /*
1794 * collect as many completed requests as are availble, or count completed
1795 * requests whichever is less.
1796 * return the number of requests.
1797 */
1798 static int
1799 cryptodev_getmstatus(struct fcrypt *fcr, struct crypt_result *crypt_res,
1800 int count)
1801 {
1802 struct cryptop *crp = NULL;
1803 struct cryptkop *krp = NULL;
1804 struct csession *cse;
1805 int i, size, req = 0;
1806 int completed=0;
1807
1808 /* On queue so nobody else can grab them
1809 * and copyout can be delayed-- no locking */
1810 TAILQ_HEAD(, cryptop) crp_delfree_q =
1811 TAILQ_HEAD_INITIALIZER(crp_delfree_q);
1812 TAILQ_HEAD(, cryptkop) krp_delfree_q =
1813 TAILQ_HEAD_INITIALIZER(krp_delfree_q);
1814
1815 /* at this point we do not know which response user is requesting for
1816 * (symmetric or asymmetric) so we copyout one from each i.e if the
1817 * count is 2 then 1 from symmetric and 1 from asymmetric queue and
1818 * if 3 then 2 symmetric and 1 asymmetric and so on */
1819
1820 /* pull off a list of requests while protected from changes */
1821 mutex_enter(&crypto_mtx);
1822 while (req < count) {
1823 crp = TAILQ_FIRST(&fcr->crp_ret_mq);
1824 if (crp) {
1825 TAILQ_REMOVE(&fcr->crp_ret_mq, crp, crp_next);
1826 TAILQ_INSERT_TAIL(&crp_delfree_q, crp, crp_next);
1827 cse = (struct csession *)crp->crp_opaque;
1828
1829 /* see if the session is still valid */
1830 cse = csefind(fcr, cse->ses);
1831 if (cse != NULL) {
1832 crypt_res[req].status = 0;
1833 } else {
1834 DPRINTF(("csefind failed\n"));
1835 crypt_res[req].status = EINVAL;
1836 }
1837 req++;
1838 }
1839 if(req < count) {
1840 crypt_res[req].status = 0;
1841 krp = TAILQ_FIRST(&fcr->crp_ret_mkq);
1842 if (krp) {
1843 TAILQ_REMOVE(&fcr->crp_ret_mkq, krp, krp_next);
1844 TAILQ_INSERT_TAIL(&krp_delfree_q, krp, krp_next);
1845 req++;
1846 }
1847 }
1848 }
1849 mutex_exit(&crypto_mtx);
1850
1851 /* now do all the work outside the mutex */
1852 for(req=0; req < count ;) {
1853 crp = TAILQ_FIRST(&crp_delfree_q);
1854 if (crp) {
1855 if (crypt_res[req].status != 0) {
1856 /* csefind failed during collection */
1857 goto bail;
1858 }
1859 cse = (struct csession *)crp->crp_opaque;
1860 crypt_res[req].reqid = crp->crp_reqid;
1861 crypt_res[req].opaque = crp->crp_usropaque;
1862 completed++;
1863
1864 if (crp->crp_etype != 0) {
1865 crypt_res[req].status = crp->crp_etype;
1866 goto bail;
1867 }
1868
1869 if (cse->error) {
1870 crypt_res[req].status = cse->error;
1871 goto bail;
1872 }
1873
1874 if (crp->dst && (crypt_res[req].status =
1875 copyout(crp->uio.uio_iov[0].iov_base, crp->dst,
1876 crp->len)))
1877 goto bail;
1878
1879 if (crp->mac && (crypt_res[req].status =
1880 copyout(crp->crp_mac, crp->mac,
1881 cse->thash->authsize)))
1882 goto bail;
1883
1884 bail:
1885 TAILQ_REMOVE(&crp_delfree_q, crp, crp_next);
1886 kmem_free(crp->uio.uio_iov[0].iov_base,
1887 crp->uio.uio_iov[0].iov_len);
1888 crypto_freereq(crp);
1889 req++;
1890 }
1891
1892 if (req < count) {
1893 krp = TAILQ_FIRST(&krp_delfree_q);
1894 if (krp) {
1895 crypt_res[req].reqid = krp->krp_reqid;
1896 crypt_res[req].opaque = krp->krp_usropaque;
1897 completed++;
1898 if (krp->krp_status != 0) {
1899 DPRINTF(("cryptodev_key: "
1900 "krp->krp_status 0x%08x\n",
1901 krp->krp_status));
1902 crypt_res[req].status = krp->krp_status;
1903 goto fail;
1904 }
1905
1906 for (i = krp->krp_iparams; i < krp->krp_iparams
1907 + krp->krp_oparams; i++) {
1908 size = (krp->krp_param[i].crp_nbits
1909 + 7) / 8;
1910 if (size == 0)
1911 continue;
1912 crypt_res[req].status = copyout
1913 (krp->krp_param[i].crp_p,
1914 krp->crk_param[i].crp_p, size);
1915 if (crypt_res[req].status) {
1916 DPRINTF(("cryptodev_key: "
1917 "copyout oparam %d failed, "
1918 "error=%d\n",
1919 i - krp->krp_iparams,
1920 crypt_res[req].status));
1921 goto fail;
1922 }
1923 }
1924 fail:
1925 TAILQ_REMOVE(&krp_delfree_q, krp, krp_next);
1926 /* not sure what to do for this */
1927 /* kop[req].crk_status = krp->krp_status; */
1928 for (i = 0; i < CRK_MAXPARAM; i++) {
1929 struct crparam *kp = &(krp->krp_param[i]);
1930 if (kp->crp_p) {
1931 size = (kp->crp_nbits + 7) / 8;
1932 KASSERT(size > 0);
1933 (void)memset(kp->crp_p, 0, size);
1934 kmem_free(kp->crp_p, size);
1935 }
1936 }
1937 cv_destroy(&krp->krp_cv);
1938 pool_put(&cryptkop_pool, krp);
1939 req++;
1940 }
1941 }
1942 }
1943
1944 return completed;
1945 }
1946
1947 static int
1948 cryptodev_getstatus (struct fcrypt *fcr, struct crypt_result *crypt_res)
1949 {
1950 struct cryptop *crp = NULL, *cnext;
1951 struct cryptkop *krp = NULL, *knext;
1952 struct csession *cse;
1953 int i, size, req = 0;
1954
1955 mutex_enter(&crypto_mtx);
1956 /* Here we dont know for which request the user is requesting the
1957 * response so checking in both the queues */
1958 TAILQ_FOREACH_SAFE(crp, &fcr->crp_ret_mq, crp_next, cnext) {
1959 if(crp && (crp->crp_reqid == crypt_res->reqid)) {
1960 cse = (struct csession *)crp->crp_opaque;
1961 crypt_res->opaque = crp->crp_usropaque;
1962 cse = csefind(fcr, cse->ses);
1963 if (cse == NULL) {
1964 DPRINTF(("csefind failed\n"));
1965 crypt_res->status = EINVAL;
1966 goto bail;
1967 }
1968
1969 if (crp->crp_etype != 0) {
1970 crypt_res->status = crp->crp_etype;
1971 goto bail;
1972 }
1973
1974 if (cse->error) {
1975 crypt_res->status = cse->error;
1976 goto bail;
1977 }
1978
1979 if (crp->dst && (crypt_res->status =
1980 copyout(crp->uio.uio_iov[0].iov_base,
1981 crp->dst, crp->len)))
1982 goto bail;
1983
1984 if (crp->mac && (crypt_res->status =
1985 copyout(crp->crp_mac, crp->mac,
1986 cse->thash->authsize)))
1987 goto bail;
1988 bail:
1989 TAILQ_REMOVE(&fcr->crp_ret_mq, crp, crp_next);
1990
1991 mutex_exit(&crypto_mtx);
1992 crypto_freereq(crp);
1993 return 0;
1994 }
1995 }
1996
1997 TAILQ_FOREACH_SAFE(krp, &fcr->crp_ret_mkq, krp_next, knext) {
1998 if(krp && (krp->krp_reqid == crypt_res->reqid)) {
1999 crypt_res[req].opaque = krp->krp_usropaque;
2000 if (krp->krp_status != 0) {
2001 DPRINTF(("cryptodev_key: "
2002 "krp->krp_status 0x%08x\n",
2003 krp->krp_status));
2004 crypt_res[req].status = krp->krp_status;
2005 goto fail;
2006 }
2007
2008 for (i = krp->krp_iparams; i < krp->krp_iparams +
2009 krp->krp_oparams; i++) {
2010 size = (krp->krp_param[i].crp_nbits + 7) / 8;
2011 if (size == 0)
2012 continue;
2013 crypt_res[req].status = copyout(
2014 krp->krp_param[i].crp_p,
2015 krp->crk_param[i].crp_p, size);
2016 if (crypt_res[req].status) {
2017 DPRINTF(("cryptodev_key: copyout oparam"
2018 "%d failed, error=%d\n",
2019 i - krp->krp_iparams,
2020 crypt_res[req].status));
2021 goto fail;
2022 }
2023 }
2024 fail:
2025 TAILQ_REMOVE(&fcr->crp_ret_mkq, krp, krp_next);
2026 mutex_exit(&crypto_mtx);
2027 /* not sure what to do for this */
2028 /* kop[req].crk_status = krp->krp_status; */
2029 for (i = 0; i < CRK_MAXPARAM; i++) {
2030 struct crparam *kp = &(krp->krp_param[i]);
2031 if (kp->crp_p) {
2032 size = (kp->crp_nbits + 7) / 8;
2033 KASSERT(size > 0);
2034 memset(kp->crp_p, 0, size);
2035 kmem_free(kp->crp_p, size);
2036 }
2037 }
2038 cv_destroy(&krp->krp_cv);
2039 pool_put(&cryptkop_pool, krp);
2040 return 0;
2041 }
2042 }
2043 mutex_exit(&crypto_mtx);
2044 return EINPROGRESS;
2045 }
2046
2047 static int
2048 cryptof_stat(struct file *fp, struct stat *st)
2049 {
2050 struct fcrypt *fcr = fp->f_fcrypt;
2051
2052 (void)memset(st, 0, sizeof(*st));
2053
2054 mutex_enter(&crypto_mtx);
2055 st->st_dev = makedev(cdevsw_lookup_major(&crypto_cdevsw), fcr->sesn);
2056 st->st_atimespec = fcr->atime;
2057 st->st_mtimespec = fcr->mtime;
2058 st->st_ctimespec = st->st_birthtimespec = fcr->btime;
2059 st->st_uid = kauth_cred_geteuid(fp->f_cred);
2060 st->st_gid = kauth_cred_getegid(fp->f_cred);
2061 mutex_exit(&crypto_mtx);
2062
2063 return 0;
2064 }
2065
2066 static int
2067 cryptof_poll(struct file *fp, int events)
2068 {
2069 struct fcrypt *fcr = fp->f_fcrypt;
2070 int revents = 0;
2071
2072 if (!(events & (POLLIN | POLLRDNORM))) {
2073 /* only support read and POLLIN */
2074 return 0;
2075 }
2076
2077 mutex_enter(&crypto_mtx);
2078 if (TAILQ_EMPTY(&fcr->crp_ret_mq) && TAILQ_EMPTY(&fcr->crp_ret_mkq)) {
2079 /* no completed requests pending, save the poll for later */
2080 selrecord(curlwp, &fcr->sinfo);
2081 } else {
2082 /* let the app(s) know that there are completed requests */
2083 revents = events & (POLLIN | POLLRDNORM);
2084 }
2085 mutex_exit(&crypto_mtx);
2086
2087 return revents;
2088 }
2089
2090 /*
2091 * Pseudo-device initialization routine for /dev/crypto
2092 */
2093 void
2094 cryptoattach(int num)
2095 {
2096 crypto_init();
2097
2098 pool_init(&fcrpl, sizeof(struct fcrypt), 0, 0, 0, "fcrpl",
2099 NULL, IPL_NET); /* XXX IPL_NET ("splcrypto") */
2100 pool_init(&csepl, sizeof(struct csession), 0, 0, 0, "csepl",
2101 NULL, IPL_NET); /* XXX IPL_NET ("splcrypto") */
2102
2103 /*
2104 * Preallocate space for 64 users, with 5 sessions each.
2105 * (consider that a TLS protocol session requires at least
2106 * 3DES, MD5, and SHA1 (both hashes are used in the PRF) for
2107 * the negotiation, plus HMAC_SHA1 for the actual SSL records,
2108 * consuming one session here for each algorithm.
2109 */
2110 pool_prime(&fcrpl, 64);
2111 pool_prime(&csepl, 64 * 5);
2112 }
2113
2114 void crypto_attach(device_t, device_t, void *);
2115
2116 void
2117 crypto_attach(device_t parent, device_t self, void * opaque)
2118 {
2119
2120 cryptoattach(0);
2121 }
2122
2123 int crypto_detach(device_t, int);
2124
2125 int
2126 crypto_detach(device_t self, int num)
2127 {
2128
2129 pool_destroy(&fcrpl);
2130 pool_destroy(&csepl);
2131
2132 return 0;
2133 }
2134
2135 int crypto_match(device_t, cfdata_t, void *);
2136
2137 int
2138 crypto_match(device_t parent, cfdata_t data, void *opaque)
2139 {
2140
2141 return 1;
2142 }
2143
2144 MODULE(MODULE_CLASS_DRIVER, crypto, "opencrypto");
2145
2146 CFDRIVER_DECL(crypto, DV_DULL, NULL);
2147
2148 CFATTACH_DECL2_NEW(crypto, 0, crypto_match, crypto_attach, crypto_detach,
2149 NULL, NULL, NULL);
2150
2151 #ifdef _MODULE
2152 static int cryptoloc[] = { -1, -1 };
2153
2154 static struct cfdata crypto_cfdata[] = {
2155 {
2156 .cf_name = "crypto",
2157 .cf_atname = "crypto",
2158 .cf_unit = 0,
2159 .cf_fstate = 0,
2160 .cf_loc = cryptoloc,
2161 .cf_flags = 0,
2162 .cf_pspec = NULL,
2163 },
2164 { NULL, NULL, 0, 0, NULL, 0, NULL }
2165 };
2166 #endif
2167
2168 static int
2169 crypto_modcmd(modcmd_t cmd, void *arg)
2170 {
2171 int error = 0;
2172 #ifdef _MODULE
2173 devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
2174 #endif
2175
2176 switch (cmd) {
2177 case MODULE_CMD_INIT:
2178 #ifdef _MODULE
2179
2180 error = config_cfdriver_attach(&crypto_cd);
2181 if (error) {
2182 return error;
2183 }
2184
2185 error = config_cfattach_attach(crypto_cd.cd_name, &crypto_ca);
2186 if (error) {
2187 config_cfdriver_detach(&crypto_cd);
2188 aprint_error("%s: unable to register cfattach\n",
2189 crypto_cd.cd_name);
2190
2191 return error;
2192 }
2193
2194 error = config_cfdata_attach(crypto_cfdata, 1);
2195 if (error) {
2196 config_cfattach_detach(crypto_cd.cd_name, &crypto_ca);
2197 config_cfdriver_detach(&crypto_cd);
2198 aprint_error("%s: unable to register cfdata\n",
2199 crypto_cd.cd_name);
2200
2201 return error;
2202 }
2203
2204 error = devsw_attach(crypto_cd.cd_name, NULL, &bmajor,
2205 &crypto_cdevsw, &cmajor);
2206 if (error) {
2207 error = config_cfdata_detach(crypto_cfdata);
2208 if (error) {
2209 return error;
2210 }
2211 config_cfattach_detach(crypto_cd.cd_name, &crypto_ca);
2212 config_cfdriver_detach(&crypto_cd);
2213 aprint_error("%s: unable to register devsw\n",
2214 crypto_cd.cd_name);
2215
2216 return error;
2217 }
2218
2219 (void)config_attach_pseudo(crypto_cfdata);
2220 #endif
2221
2222 return error;
2223 case MODULE_CMD_FINI:
2224 #ifdef _MODULE
2225 error = config_cfdata_detach(crypto_cfdata);
2226 if (error) {
2227 return error;
2228 }
2229
2230 config_cfattach_detach(crypto_cd.cd_name, &crypto_ca);
2231 config_cfdriver_detach(&crypto_cd);
2232 devsw_detach(NULL, &crypto_cdevsw);
2233 #endif
2234
2235 return error;
2236 #ifdef _MODULE
2237 case MODULE_CMD_AUTOUNLOAD:
2238 #if 0 /*
2239 * XXX Completely disable auto-unload for now, since there is still
2240 * XXX a (small) window where in-module ref-counting doesn't help
2241 */
2242 if (crypto_refcount != 0)
2243 #endif
2244 return EBUSY;
2245 /* FALLTHROUGH */
2246 #endif
2247 default:
2248 return ENOTTY;
2249 }
2250 }
2251