cryptodev.c revision 1.33 1 /* $NetBSD: cryptodev.c,v 1.33 2008/02/04 00:35:34 tls 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) 2001 Theo de Raadt
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * Effort sponsored in part by the Defense Advanced Research Projects
32 * Agency (DARPA) and Air Force Research Laboratory, Air Force
33 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
34 *
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.33 2008/02/04 00:35:34 tls Exp $");
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/pool.h>
45 #include <sys/sysctl.h>
46 #include <sys/file.h>
47 #include <sys/filedesc.h>
48 #include <sys/errno.h>
49 #include <sys/md5.h>
50 #include <sys/sha1.h>
51 #include <sys/conf.h>
52 #include <sys/device.h>
53 #include <sys/kauth.h>
54
55 #include "opt_ocf.h"
56 #include <opencrypto/cryptodev.h>
57 #include <opencrypto/xform.h>
58
59 struct csession {
60 TAILQ_ENTRY(csession) next;
61 u_int64_t sid;
62 u_int32_t ses;
63
64 u_int32_t cipher;
65 struct enc_xform *txform;
66 u_int32_t mac;
67 struct auth_hash *thash;
68
69 void * key;
70 int keylen;
71 u_char tmp_iv[EALG_MAX_BLOCK_LEN];
72
73 void * mackey;
74 int mackeylen;
75 u_char tmp_mac[CRYPTO_MAX_MAC_LEN];
76
77 struct iovec iovec[1]; /* user requests never have more */
78 struct uio uio;
79 int error;
80 };
81
82 struct fcrypt {
83 TAILQ_HEAD(csessionlist, csession) csessions;
84 int sesn;
85 };
86
87 /* For our fixed-size allocations */
88 struct pool fcrpl;
89 struct pool csepl;
90
91 /* Declaration of master device (fd-cloning/ctxt-allocating) entrypoints */
92 static int cryptoopen(dev_t dev, int flag, int mode, struct lwp *l);
93 static int cryptoread(dev_t dev, struct uio *uio, int ioflag);
94 static int cryptowrite(dev_t dev, struct uio *uio, int ioflag);
95 static int cryptoselect(dev_t dev, int rw, struct lwp *l);
96
97 /* Declaration of cloned-device (per-ctxt) entrypoints */
98 static int cryptof_read(struct file *, off_t *, struct uio *, kauth_cred_t, int);
99 static int cryptof_write(struct file *, off_t *, struct uio *, kauth_cred_t, int);
100 static int cryptof_ioctl(struct file *, u_long, void*, struct lwp *l);
101 static int cryptof_close(struct file *, struct lwp *);
102
103 static const struct fileops cryptofops = {
104 cryptof_read,
105 cryptof_write,
106 cryptof_ioctl,
107 fnullop_fcntl,
108 fnullop_poll,
109 fbadop_stat,
110 cryptof_close,
111 fnullop_kqfilter
112 };
113
114 static struct csession *csefind(struct fcrypt *, u_int);
115 static int csedelete(struct fcrypt *, struct csession *);
116 static struct csession *cseadd(struct fcrypt *, struct csession *);
117 static struct csession *csecreate(struct fcrypt *, u_int64_t, void *, u_int64_t,
118 void *, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
119 struct auth_hash *);
120 static int csefree(struct csession *);
121
122 static int cryptodev_op(struct csession *, struct crypt_op *, struct lwp *);
123 static int cryptodev_key(struct crypt_kop *);
124 int cryptodev_dokey(struct crypt_kop *kop, struct crparam kvp[]);
125
126 static int cryptodev_cb(void *);
127 static int cryptodevkey_cb(void *);
128
129 /*
130 * sysctl-able control variables for /dev/crypto now defined in crypto.c:
131 * crypto_usercrypto, crypto_userasmcrypto, crypto_devallowsoft.
132 */
133
134 /* ARGSUSED */
135 int
136 cryptof_read(struct file *fp, off_t *poff,
137 struct uio *uio, kauth_cred_t cred, int flags)
138 {
139 return (EIO);
140 }
141
142 /* ARGSUSED */
143 int
144 cryptof_write(struct file *fp, off_t *poff,
145 struct uio *uio, kauth_cred_t cred, int flags)
146 {
147 return (EIO);
148 }
149
150 /* ARGSUSED */
151 int
152 cryptof_ioctl(struct file *fp, u_long cmd, void* data, struct lwp *l)
153 {
154 struct cryptoini cria, crie;
155 struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
156 struct csession *cse;
157 struct session_op *sop;
158 struct crypt_op *cop;
159 struct enc_xform *txform = NULL;
160 struct auth_hash *thash = NULL;
161 u_int64_t sid;
162 u_int32_t ses;
163 int error = 0;
164
165 /* backwards compatibility */
166 struct file *criofp;
167 struct fcrypt *criofcr;
168 int criofd;
169
170 switch (cmd) {
171 case CRIOGET: /* XXX deprecated, remove after 5.0 */
172 if ((error = falloc(l, &criofp, &criofd)) != 0)
173 return error;
174 criofcr = pool_get(&fcrpl, PR_WAITOK);
175 mutex_spin_enter(&crypto_mtx);
176 TAILQ_INIT(&criofcr->csessions);
177 /*
178 * Don't ever return session 0, to allow detection of
179 * failed creation attempts with multi-create ioctl.
180 */
181 criofcr->sesn = 1;
182 mutex_spin_exit(&crypto_mtx);
183 (void)fdclone(l, criofp, criofd, (FREAD|FWRITE),
184 &cryptofops, criofcr);
185 *(u_int32_t *)data = criofd;
186 return error;
187 break;
188 case CIOCGSESSION:
189 sop = (struct session_op *)data;
190 switch (sop->cipher) {
191 case 0:
192 break;
193 case CRYPTO_DES_CBC:
194 txform = &enc_xform_des;
195 break;
196 case CRYPTO_3DES_CBC:
197 txform = &enc_xform_3des;
198 break;
199 case CRYPTO_BLF_CBC:
200 txform = &enc_xform_blf;
201 break;
202 case CRYPTO_CAST_CBC:
203 txform = &enc_xform_cast5;
204 break;
205 case CRYPTO_SKIPJACK_CBC:
206 txform = &enc_xform_skipjack;
207 break;
208 case CRYPTO_AES_CBC:
209 txform = &enc_xform_rijndael128;
210 break;
211 case CRYPTO_NULL_CBC:
212 txform = &enc_xform_null;
213 break;
214 case CRYPTO_ARC4:
215 txform = &enc_xform_arc4;
216 break;
217 default:
218 DPRINTF(("Invalid cipher %d\n", sop->cipher));
219 return (EINVAL);
220 }
221
222 switch (sop->mac) {
223 case 0:
224 break;
225 case CRYPTO_MD5_HMAC:
226 thash = &auth_hash_hmac_md5;
227 break;
228 case CRYPTO_SHA1_HMAC:
229 thash = &auth_hash_hmac_sha1;
230 break;
231 case CRYPTO_MD5_HMAC_96:
232 thash = &auth_hash_hmac_md5_96;
233 break;
234 case CRYPTO_SHA1_HMAC_96:
235 thash = &auth_hash_hmac_sha1_96;
236 break;
237 case CRYPTO_SHA2_HMAC:
238 if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize)
239 thash = &auth_hash_hmac_sha2_256;
240 else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize)
241 thash = &auth_hash_hmac_sha2_384;
242 else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize)
243 thash = &auth_hash_hmac_sha2_512;
244 else {
245 DPRINTF(("Invalid mackeylen %d\n",
246 sop->mackeylen));
247 return (EINVAL);
248 }
249 break;
250 case CRYPTO_RIPEMD160_HMAC:
251 thash = &auth_hash_hmac_ripemd_160_96;
252 break;
253 case CRYPTO_MD5:
254 thash = &auth_hash_md5;
255 break;
256 case CRYPTO_SHA1:
257 thash = &auth_hash_sha1;
258 break;
259 case CRYPTO_NULL_HMAC:
260 thash = &auth_hash_null;
261 break;
262 default:
263 DPRINTF(("Invalid mac %d\n", sop->mac));
264 return (EINVAL);
265 }
266
267 bzero(&crie, sizeof(crie));
268 bzero(&cria, sizeof(cria));
269
270 if (txform) {
271 crie.cri_alg = txform->type;
272 crie.cri_klen = sop->keylen * 8;
273 if (sop->keylen > txform->maxkey ||
274 sop->keylen < txform->minkey) {
275 DPRINTF(("keylen %d not in [%d,%d]\n",
276 sop->keylen, txform->minkey,
277 txform->maxkey));
278 error = EINVAL;
279 goto bail;
280 }
281
282 crie.cri_key = malloc(crie.cri_klen / 8, M_XDATA,
283 M_WAITOK);
284 if ((error = copyin(sop->key, crie.cri_key,
285 crie.cri_klen / 8)))
286 goto bail;
287 if (thash)
288 crie.cri_next = &cria;
289 }
290
291 if (thash) {
292 cria.cri_alg = thash->type;
293 cria.cri_klen = sop->mackeylen * 8;
294 if (sop->mackeylen != thash->keysize) {
295 DPRINTF(("mackeylen %d != keysize %d\n",
296 sop->mackeylen, thash->keysize));
297 error = EINVAL;
298 goto bail;
299 }
300
301 if (cria.cri_klen) {
302 cria.cri_key = malloc(cria.cri_klen / 8,
303 M_XDATA, M_WAITOK);
304 if ((error = copyin(sop->mackey, cria.cri_key,
305 cria.cri_klen / 8)))
306 goto bail;
307 }
308 }
309 /* crypto_newsession requires that we hold the mutex. */
310 mutex_spin_enter(&crypto_mtx);
311 error = crypto_newsession(&sid, (txform ? &crie : &cria),
312 crypto_devallowsoft);
313 if (error) {
314 DPRINTF(("SIOCSESSION violates kernel parameters %d\n",
315 error));
316 goto bail;
317 }
318
319 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
320 cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
321 thash);
322
323 if (cse == NULL) {
324 DPRINTF(("csecreate failed\n"));
325 crypto_freesession(sid);
326 error = EINVAL;
327 goto bail;
328 }
329 sop->ses = cse->ses;
330
331 bail:
332 mutex_spin_exit(&crypto_mtx);
333 if (error) {
334 if (crie.cri_key)
335 FREE(crie.cri_key, M_XDATA);
336 if (cria.cri_key)
337 FREE(cria.cri_key, M_XDATA);
338 }
339 break;
340 case CIOCFSESSION:
341 mutex_spin_enter(&crypto_mtx);
342 ses = *(u_int32_t *)data;
343 cse = csefind(fcr, ses);
344 if (cse == NULL)
345 return (EINVAL);
346 csedelete(fcr, cse);
347 error = csefree(cse);
348 mutex_spin_exit(&crypto_mtx);
349 break;
350 case CIOCCRYPT:
351 mutex_spin_enter(&crypto_mtx);
352 cop = (struct crypt_op *)data;
353 cse = csefind(fcr, cop->ses);
354 mutex_spin_exit(&crypto_mtx);
355 if (cse == NULL) {
356 DPRINTF(("csefind failed\n"));
357 return (EINVAL);
358 }
359 error = cryptodev_op(cse, cop, l);
360 break;
361 case CIOCKEY:
362 error = cryptodev_key((struct crypt_kop *)data);
363 break;
364 case CIOCASYMFEAT:
365 error = crypto_getfeat((int *)data);
366 break;
367 default:
368 DPRINTF(("invalid ioctl cmd %ld\n", cmd));
369 error = EINVAL;
370 }
371 return (error);
372 }
373
374 static int
375 cryptodev_op(struct csession *cse, struct crypt_op *cop, struct lwp *l)
376 {
377 struct cryptop *crp = NULL;
378 struct cryptodesc *crde = NULL, *crda = NULL;
379 int error;
380
381 if (cop->len > 256*1024-4)
382 return (E2BIG);
383
384 if (cse->txform) {
385 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
386 return (EINVAL);
387 }
388
389 bzero(&cse->uio, sizeof(cse->uio));
390 cse->uio.uio_iovcnt = 1;
391 cse->uio.uio_resid = 0;
392 cse->uio.uio_rw = UIO_WRITE;
393 cse->uio.uio_iov = cse->iovec;
394 UIO_SETUP_SYSSPACE(&cse->uio);
395 memset(&cse->iovec, 0, sizeof(cse->iovec));
396 cse->uio.uio_iov[0].iov_len = cop->len;
397 cse->uio.uio_iov[0].iov_base = malloc(cop->len, M_XDATA, M_WAITOK);
398 cse->uio.uio_resid = cse->uio.uio_iov[0].iov_len;
399
400 crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
401 if (crp == NULL) {
402 error = ENOMEM;
403 goto bail;
404 }
405
406 if (cse->thash) {
407 crda = crp->crp_desc;
408 if (cse->txform)
409 crde = crda->crd_next;
410 } else {
411 if (cse->txform)
412 crde = crp->crp_desc;
413 else {
414 error = EINVAL;
415 goto bail;
416 }
417 }
418
419 if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
420 goto bail;
421
422 if (crda) {
423 crda->crd_skip = 0;
424 crda->crd_len = cop->len;
425 crda->crd_inject = 0; /* ??? */
426
427 crda->crd_alg = cse->mac;
428 crda->crd_key = cse->mackey;
429 crda->crd_klen = cse->mackeylen * 8;
430 }
431
432 if (crde) {
433 if (cop->op == COP_ENCRYPT)
434 crde->crd_flags |= CRD_F_ENCRYPT;
435 else
436 crde->crd_flags &= ~CRD_F_ENCRYPT;
437 crde->crd_len = cop->len;
438 crde->crd_inject = 0;
439
440 crde->crd_alg = cse->cipher;
441 crde->crd_key = cse->key;
442 crde->crd_klen = cse->keylen * 8;
443 }
444
445 crp->crp_ilen = cop->len;
446 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
447 | (cop->flags & COP_F_BATCH);
448 crp->crp_buf = (void *)&cse->uio;
449 crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
450 crp->crp_sid = cse->sid;
451 crp->crp_opaque = (void *)cse;
452
453 if (cop->iv) {
454 if (crde == NULL) {
455 error = EINVAL;
456 goto bail;
457 }
458 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
459 error = EINVAL;
460 goto bail;
461 }
462 if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
463 goto bail;
464 bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
465 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
466 crde->crd_skip = 0;
467 } else if (crde) {
468 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
469 crde->crd_skip = 0;
470 } else {
471 crde->crd_flags |= CRD_F_IV_PRESENT;
472 crde->crd_skip = cse->txform->blocksize;
473 crde->crd_len -= cse->txform->blocksize;
474 }
475 }
476
477 if (cop->mac) {
478 if (crda == NULL) {
479 error = EINVAL;
480 goto bail;
481 }
482 crp->crp_mac=cse->tmp_mac;
483 }
484
485 /*
486 * XXX there was a comment here which said that we went to
487 * XXX splcrypto() but needed to only if CRYPTO_F_CBIMM,
488 * XXX disabled on NetBSD since 1.6O due to a race condition.
489 * XXX But crypto_dispatch went to splcrypto() itself! (And
490 * XXX now takes the crypto_mtx mutex itself). We do, however,
491 * XXX need to hold the mutex across the call to cv_wait().
492 * XXX (should we arrange for crypto_dispatch to return to
493 * XXX us with it held? it seems quite ugly to do so.)
494 */
495 error = crypto_dispatch(crp);
496 mutex_spin_enter(&crypto_mtx);
497 if (error != 0) {
498 DPRINTF(("cryptodev_op: not waiting, error.\n"));
499 mutex_spin_exit(&crypto_mtx);
500 goto bail;
501 }
502 while (!(crp->crp_flags & CRYPTO_F_DONE)) {
503 DPRINTF(("cryptodev_op: sleeping on cv %08x for crp %08x\n", \
504 (uint32_t)&crp->crp_cv, (uint32_t)crp));
505 cv_wait(&crp->crp_cv, &crypto_mtx); /* XXX cv_wait_sig? */
506 }
507 if (crp->crp_flags & CRYPTO_F_ONRETQ) {
508 DPRINTF(("cryptodev_op: DONE, not woken by cryptoret.\n"));
509 (void)crypto_ret_q_remove(crp);
510 }
511 mutex_spin_exit(&crypto_mtx);
512
513 if (crp->crp_etype != 0) {
514 error = crp->crp_etype;
515 goto bail;
516 }
517
518 if (cse->error) {
519 error = cse->error;
520 goto bail;
521 }
522
523 if (cop->dst &&
524 (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
525 goto bail;
526
527 if (cop->mac &&
528 (error = copyout(crp->crp_mac, cop->mac, cse->thash->authsize)))
529 goto bail;
530
531 bail:
532 if (crp)
533 crypto_freereq(crp);
534 if (cse->uio.uio_iov[0].iov_base)
535 free(cse->uio.uio_iov[0].iov_base, M_XDATA);
536
537 return (error);
538 }
539
540 static int
541 cryptodev_cb(void *op)
542 {
543 struct cryptop *crp = (struct cryptop *) op;
544 struct csession *cse = (struct csession *)crp->crp_opaque;
545 int error = 0;
546
547 mutex_spin_enter(&crypto_mtx);
548 cse->error = crp->crp_etype;
549 if (crp->crp_etype == EAGAIN) {
550 /* always drop mutex to call dispatch routine */
551 mutex_spin_exit(&crypto_mtx);
552 error = crypto_dispatch(crp);
553 mutex_spin_enter(&crypto_mtx);
554 }
555 if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
556 cv_signal(&crp->crp_cv);
557 }
558 mutex_spin_exit(&crypto_mtx);
559 return (0);
560 }
561
562 static int
563 cryptodevkey_cb(void *op)
564 {
565 struct cryptkop *krp = (struct cryptkop *) op;
566
567 mutex_spin_enter(&crypto_mtx);
568 cv_signal(&krp->krp_cv);
569 mutex_spin_exit(&crypto_mtx);
570 return (0);
571 }
572
573 static int
574 cryptodev_key(struct crypt_kop *kop)
575 {
576 struct cryptkop *krp = NULL;
577 int error = EINVAL;
578 int in, out, size, i;
579
580 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
581 return (EFBIG);
582 }
583
584 in = kop->crk_iparams;
585 out = kop->crk_oparams;
586 switch (kop->crk_op) {
587 case CRK_MOD_EXP:
588 if (in == 3 && out == 1)
589 break;
590 return (EINVAL);
591 case CRK_MOD_EXP_CRT:
592 if (in == 6 && out == 1)
593 break;
594 return (EINVAL);
595 case CRK_DSA_SIGN:
596 if (in == 5 && out == 2)
597 break;
598 return (EINVAL);
599 case CRK_DSA_VERIFY:
600 if (in == 7 && out == 0)
601 break;
602 return (EINVAL);
603 case CRK_DH_COMPUTE_KEY:
604 if (in == 3 && out == 1)
605 break;
606 return (EINVAL);
607 case CRK_MOD_ADD:
608 if (in == 3 && out == 1)
609 break;
610 return (EINVAL);
611 case CRK_MOD_ADDINV:
612 if (in == 2 && out == 1)
613 break;
614 return (EINVAL);
615 case CRK_MOD_SUB:
616 if (in == 3 && out == 1)
617 break;
618 return (EINVAL);
619 case CRK_MOD_MULT:
620 if (in == 3 && out == 1)
621 break;
622 return (EINVAL);
623 case CRK_MOD_MULTINV:
624 if (in == 2 && out == 1)
625 break;
626 return (EINVAL);
627 case CRK_MOD:
628 if (in == 2 && out == 1)
629 break;
630 return (EINVAL);
631 default:
632 return (EINVAL);
633 }
634
635 krp = pool_get(&cryptkop_pool, PR_WAITOK);
636 if (!krp)
637 return (ENOMEM);
638 bzero(krp, sizeof *krp);
639 cv_init(&krp->krp_cv, "crykdev");
640 krp->krp_op = kop->crk_op;
641 krp->krp_status = kop->crk_status;
642 krp->krp_iparams = kop->crk_iparams;
643 krp->krp_oparams = kop->crk_oparams;
644 krp->krp_status = 0;
645 krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
646
647 for (i = 0; i < CRK_MAXPARAM; i++)
648 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
649 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
650 size = (krp->krp_param[i].crp_nbits + 7) / 8;
651 if (size == 0)
652 continue;
653 krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
654 if (i >= krp->krp_iparams)
655 continue;
656 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
657 if (error)
658 goto fail;
659 }
660
661 error = crypto_kdispatch(krp);
662 if (error != 0) {
663 goto fail;
664 }
665
666 mutex_spin_enter(&crypto_mtx);
667 while (!(krp->krp_flags & CRYPTO_F_DONE)) {
668 cv_wait(&krp->krp_cv, &crypto_mtx); /* XXX cv_wait_sig? */
669 }
670 if (krp->krp_flags & CRYPTO_F_ONRETQ) {
671 DPRINTF(("cryptodev_key: DONE early, not via cryptoret.\n"));
672 (void)crypto_ret_kq_remove(krp);
673 }
674 mutex_spin_exit(&crypto_mtx);
675
676 if (krp->krp_status != 0) {
677 error = krp->krp_status;
678 goto fail;
679 }
680
681 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
682 size = (krp->krp_param[i].crp_nbits + 7) / 8;
683 if (size == 0)
684 continue;
685 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
686 if (error)
687 goto fail;
688 }
689
690 fail:
691 if (krp) {
692 kop->crk_status = krp->krp_status;
693 for (i = 0; i < CRK_MAXPARAM; i++) {
694 if (krp->krp_param[i].crp_p)
695 FREE(krp->krp_param[i].crp_p, M_XDATA);
696 }
697 pool_put(&cryptkop_pool, krp);
698 }
699 return (error);
700 }
701
702 /* ARGSUSED */
703 static int
704 cryptof_close(struct file *fp, struct lwp *l)
705 {
706 struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
707 struct csession *cse;
708
709 mutex_spin_enter(&crypto_mtx);
710 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
711 TAILQ_REMOVE(&fcr->csessions, cse, next);
712 (void)csefree(cse);
713 }
714 pool_put(&fcrpl, fcr);
715
716 fp->f_data = NULL;
717 mutex_spin_exit(&crypto_mtx);
718
719 return 0;
720 }
721
722 /* csefind: call with crypto_mtx held. */
723 static struct csession *
724 csefind(struct fcrypt *fcr, u_int ses)
725 {
726 struct csession *cse, *ret = NULL;
727
728 KASSERT(mutex_owned(&crypto_mtx));
729 TAILQ_FOREACH(cse, &fcr->csessions, next)
730 if (cse->ses == ses)
731 ret = cse;
732 return (ret);
733 }
734
735 /* csedelete: call with crypto_mtx held. */
736 static int
737 csedelete(struct fcrypt *fcr, struct csession *cse_del)
738 {
739 struct csession *cse;
740 int ret = 0;
741
742 KASSERT(mutex_owned(&crypto_mtx));
743 TAILQ_FOREACH(cse, &fcr->csessions, next) {
744 if (cse == cse_del) {
745 TAILQ_REMOVE(&fcr->csessions, cse, next);
746 ret = 1;
747 }
748 }
749 return (ret);
750 }
751
752 /* cseadd: call with crypto_mtx held. */
753 static struct csession *
754 cseadd(struct fcrypt *fcr, struct csession *cse)
755 {
756 KASSERT(mutex_owned(&crypto_mtx));
757 /* don't let session ID wrap! */
758 if (fcr->sesn + 1 == 0) return NULL;
759 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
760 cse->ses = fcr->sesn++;
761 return (cse);
762 }
763
764 /* csecreate: call with crypto_mtx held. */
765 static struct csession *
766 csecreate(struct fcrypt *fcr, u_int64_t sid, void *key, u_int64_t keylen,
767 void *mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
768 struct enc_xform *txform, struct auth_hash *thash)
769 {
770 struct csession *cse;
771
772 KASSERT(mutex_owned(&crypto_mtx));
773 cse = pool_get(&csepl, PR_NOWAIT);
774 if (cse == NULL)
775 return NULL;
776 cse->key = key;
777 cse->keylen = keylen/8;
778 cse->mackey = mackey;
779 cse->mackeylen = mackeylen/8;
780 cse->sid = sid;
781 cse->cipher = cipher;
782 cse->mac = mac;
783 cse->txform = txform;
784 cse->thash = thash;
785 if (cseadd(fcr, cse))
786 return (cse);
787 else {
788 pool_put(&csepl, cse);
789 return NULL;
790 }
791 }
792
793 /* csefree: call with crypto_mtx held. */
794 static int
795 csefree(struct csession *cse)
796 {
797 int error;
798
799 KASSERT(mutex_owned(&crypto_mtx));
800 error = crypto_freesession(cse->sid);
801 if (cse->key)
802 FREE(cse->key, M_XDATA);
803 if (cse->mackey)
804 FREE(cse->mackey, M_XDATA);
805 pool_put(&csepl, cse);
806 return (error);
807 }
808
809 static int
810 cryptoopen(dev_t dev, int flag, int mode,
811 struct lwp *l)
812 {
813 struct file *fp;
814 struct fcrypt *fcr;
815 int fd, error;
816
817 if (crypto_usercrypto == 0)
818 return (ENXIO);
819
820 if ((error = falloc(l, &fp, &fd)) != 0)
821 return error;
822
823 fcr = pool_get(&fcrpl, PR_WAITOK);
824 mutex_spin_enter(&crypto_mtx);
825 TAILQ_INIT(&fcr->csessions);
826 /*
827 * Don't ever return session 0, to allow detection of
828 * failed creation attempts with multi-create ioctl.
829 */
830 fcr->sesn = 1;
831 mutex_spin_exit(&crypto_mtx);
832 return fdclone(l, fp, fd, flag, &cryptofops, fcr);
833 }
834
835 static int
836 cryptoread(dev_t dev, struct uio *uio, int ioflag)
837 {
838 return (EIO);
839 }
840
841 static int
842 cryptowrite(dev_t dev, struct uio *uio, int ioflag)
843 {
844 return (EIO);
845 }
846
847 int
848 cryptoselect(dev_t dev, int rw, struct lwp *l)
849 {
850 return (0);
851 }
852
853 /*static*/
854 struct cdevsw crypto_cdevsw = {
855 /* open */ cryptoopen,
856 /* close */ noclose,
857 /* read */ cryptoread,
858 /* write */ cryptowrite,
859 /* ioctl */ noioctl,
860 /* ttstop?*/ nostop,
861 /* ??*/ notty,
862 /* poll */ cryptoselect /*nopoll*/,
863 /* mmap */ nommap,
864 /* kqfilter */ nokqfilter,
865 /* type */ D_OTHER,
866 };
867
868 /*
869 * Pseudo-device initialization routine for /dev/crypto
870 */
871 void cryptoattach(int);
872
873 void
874 cryptoattach(int num)
875 {
876 pool_init(&fcrpl, sizeof(struct fcrypt), 0, 0, 0, "fcrpl",
877 NULL, IPL_NET); /* XXX IPL_NET ("splcrypto") */
878 pool_init(&csepl, sizeof(struct csession), 0, 0, 0, "csepl",
879 NULL, IPL_NET); /* XXX IPL_NET ("splcrypto") */
880
881 /*
882 * Preallocate space for 64 users, with 5 sessions each.
883 * (consider that a TLS protocol session requires at least
884 * 3DES, MD5, and SHA1 (both hashes are used in the PRF) for
885 * the negotiation, plus HMAC_SHA1 for the actual SSL records,
886 * consuming one session here for each algorithm.
887 */
888 pool_prime(&fcrpl, 64);
889 pool_prime(&csepl, 64 * 5);
890 }
891