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