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