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