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