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