cryptodev.c revision 1.35 1 /* $NetBSD: cryptodev.c,v 1.35 2008/03/21 21:55:01 ad 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.35 2008/03/21 21:55:01 ad 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(file_t *, off_t *, struct uio *, kauth_cred_t, int);
99 static int cryptof_write(file_t *, off_t *, struct uio *, kauth_cred_t, int);
100 static int cryptof_ioctl(file_t *, u_long, void *);
101 static int cryptof_close(file_t *);
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(file_t *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(file_t *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(file_t *fp, u_long cmd, void *data)
153 {
154 struct cryptoini cria, crie;
155 struct fcrypt *fcr = 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 file_t *criofp;
167 struct fcrypt *criofcr;
168 int criofd;
169
170 switch (cmd) {
171 case CRIOGET: /* XXX deprecated, remove after 5.0 */
172 if ((error = fd_allocfile(&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)fd_clone(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 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
315 cria.cri_key, cria.cri_klen, sop->cipher, sop->mac,
316 txform, thash);
317 if (cse != NULL) {
318 sop->ses = cse->ses;
319 } else {
320 DPRINTF(("csecreate failed\n"));
321 crypto_freesession(sid);
322 error = EINVAL;
323 }
324 } else {
325 DPRINTF(("SIOCSESSION violates kernel parameters %d\n",
326 error));
327 }
328 mutex_spin_exit(&crypto_mtx);
329 bail:
330 if (error) {
331 if (crie.cri_key)
332 FREE(crie.cri_key, M_XDATA);
333 if (cria.cri_key)
334 FREE(cria.cri_key, M_XDATA);
335 }
336 break;
337 case CIOCFSESSION:
338 mutex_spin_enter(&crypto_mtx);
339 ses = *(u_int32_t *)data;
340 cse = csefind(fcr, ses);
341 if (cse == NULL)
342 return (EINVAL);
343 csedelete(fcr, cse);
344 error = csefree(cse);
345 mutex_spin_exit(&crypto_mtx);
346 break;
347 case CIOCCRYPT:
348 mutex_spin_enter(&crypto_mtx);
349 cop = (struct crypt_op *)data;
350 cse = csefind(fcr, cop->ses);
351 mutex_spin_exit(&crypto_mtx);
352 if (cse == NULL) {
353 DPRINTF(("csefind failed\n"));
354 return (EINVAL);
355 }
356 error = cryptodev_op(cse, cop, curlwp);
357 DPRINTF(("cryptodev_op error = %d\n", error));
358 break;
359 case CIOCKEY:
360 error = cryptodev_key((struct crypt_kop *)data);
361 DPRINTF(("cryptodev_key error = %d\n", error));
362 break;
363 case CIOCASYMFEAT:
364 error = crypto_getfeat((int *)data);
365 break;
366 default:
367 DPRINTF(("invalid ioctl cmd %ld\n", cmd));
368 error = EINVAL;
369 }
370 return (error);
371 }
372
373 static int
374 cryptodev_op(struct csession *cse, struct crypt_op *cop, struct lwp *l)
375 {
376 struct cryptop *crp = NULL;
377 struct cryptodesc *crde = NULL, *crda = NULL;
378 int error;
379
380 if (cop->len > 256*1024-4)
381 return (E2BIG);
382
383 if (cse->txform) {
384 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
385 return (EINVAL);
386 }
387
388 bzero(&cse->uio, sizeof(cse->uio));
389 cse->uio.uio_iovcnt = 1;
390 cse->uio.uio_resid = 0;
391 cse->uio.uio_rw = UIO_WRITE;
392 cse->uio.uio_iov = cse->iovec;
393 UIO_SETUP_SYSSPACE(&cse->uio);
394 memset(&cse->iovec, 0, sizeof(cse->iovec));
395 cse->uio.uio_iov[0].iov_len = cop->len;
396 cse->uio.uio_iov[0].iov_base = malloc(cop->len, M_XDATA, M_WAITOK);
397 cse->uio.uio_resid = cse->uio.uio_iov[0].iov_len;
398
399 crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
400 if (crp == NULL) {
401 error = ENOMEM;
402 goto bail;
403 }
404
405 if (cse->thash) {
406 crda = crp->crp_desc;
407 if (cse->txform)
408 crde = crda->crd_next;
409 } else {
410 if (cse->txform)
411 crde = crp->crp_desc;
412 else {
413 error = EINVAL;
414 goto bail;
415 }
416 }
417
418 if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
419 goto bail;
420
421 if (crda) {
422 crda->crd_skip = 0;
423 crda->crd_len = cop->len;
424 crda->crd_inject = 0; /* ??? */
425
426 crda->crd_alg = cse->mac;
427 crda->crd_key = cse->mackey;
428 crda->crd_klen = cse->mackeylen * 8;
429 }
430
431 if (crde) {
432 if (cop->op == COP_ENCRYPT)
433 crde->crd_flags |= CRD_F_ENCRYPT;
434 else
435 crde->crd_flags &= ~CRD_F_ENCRYPT;
436 crde->crd_len = cop->len;
437 crde->crd_inject = 0;
438
439 crde->crd_alg = cse->cipher;
440 crde->crd_key = cse->key;
441 crde->crd_klen = cse->keylen * 8;
442 }
443
444 crp->crp_ilen = cop->len;
445 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
446 | (cop->flags & COP_F_BATCH);
447 crp->crp_buf = (void *)&cse->uio;
448 crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
449 crp->crp_sid = cse->sid;
450 crp->crp_opaque = (void *)cse;
451
452 if (cop->iv) {
453 if (crde == NULL) {
454 error = EINVAL;
455 goto bail;
456 }
457 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
458 error = EINVAL;
459 goto bail;
460 }
461 if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
462 goto bail;
463 bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
464 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
465 crde->crd_skip = 0;
466 } else if (crde) {
467 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
468 crde->crd_skip = 0;
469 } else {
470 crde->crd_flags |= CRD_F_IV_PRESENT;
471 crde->crd_skip = cse->txform->blocksize;
472 crde->crd_len -= cse->txform->blocksize;
473 }
474 }
475
476 if (cop->mac) {
477 if (crda == NULL) {
478 error = EINVAL;
479 goto bail;
480 }
481 crp->crp_mac=cse->tmp_mac;
482 }
483
484 /*
485 * XXX there was a comment here which said that we went to
486 * XXX splcrypto() but needed to only if CRYPTO_F_CBIMM,
487 * XXX disabled on NetBSD since 1.6O due to a race condition.
488 * XXX But crypto_dispatch went to splcrypto() itself! (And
489 * XXX now takes the crypto_mtx mutex itself). We do, however,
490 * XXX need to hold the mutex across the call to cv_wait().
491 * XXX (should we arrange for crypto_dispatch to return to
492 * XXX us with it held? it seems quite ugly to do so.)
493 */
494 error = crypto_dispatch(crp);
495 mutex_spin_enter(&crypto_mtx);
496 if (error != 0) {
497 DPRINTF(("cryptodev_op: not waiting, error.\n"));
498 mutex_spin_exit(&crypto_mtx);
499 goto bail;
500 }
501 while (!(crp->crp_flags & CRYPTO_F_DONE)) {
502 DPRINTF(("cryptodev_op: sleeping on cv %08x for crp %08x\n", \
503 (uint32_t)&crp->crp_cv, (uint32_t)crp));
504 cv_wait(&crp->crp_cv, &crypto_mtx); /* XXX cv_wait_sig? */
505 }
506 if (crp->crp_flags & CRYPTO_F_ONRETQ) {
507 DPRINTF(("cryptodev_op: DONE, not woken by cryptoret.\n"));
508 (void)crypto_ret_q_remove(crp);
509 }
510 mutex_spin_exit(&crypto_mtx);
511
512 if (crp->crp_etype != 0) {
513 DPRINTF(("cryptodev_op: crp_etype %d\n", crp->crp_etype));
514 error = crp->crp_etype;
515 goto bail;
516 }
517
518 if (cse->error) {
519 DPRINTF(("cryptodev_op: cse->error %d\n", cse->error));
520 error = cse->error;
521 goto bail;
522 }
523
524 if (cop->dst &&
525 (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len))) {
526 DPRINTF(("cryptodev_op: copyout error %d\n", error));
527 goto bail;
528 }
529
530 if (cop->mac &&
531 (error = copyout(crp->crp_mac, cop->mac, cse->thash->authsize))) {
532 DPRINTF(("cryptodev_op: mac copyout error %d\n", error));
533 goto bail;
534 }
535
536 bail:
537 if (crp)
538 crypto_freereq(crp);
539 if (cse->uio.uio_iov[0].iov_base)
540 free(cse->uio.uio_iov[0].iov_base, M_XDATA);
541
542 return (error);
543 }
544
545 static int
546 cryptodev_cb(void *op)
547 {
548 struct cryptop *crp = (struct cryptop *) op;
549 struct csession *cse = (struct csession *)crp->crp_opaque;
550 int error = 0;
551
552 mutex_spin_enter(&crypto_mtx);
553 cse->error = crp->crp_etype;
554 if (crp->crp_etype == EAGAIN) {
555 /* always drop mutex to call dispatch routine */
556 mutex_spin_exit(&crypto_mtx);
557 error = crypto_dispatch(crp);
558 mutex_spin_enter(&crypto_mtx);
559 }
560 if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
561 cv_signal(&crp->crp_cv);
562 }
563 mutex_spin_exit(&crypto_mtx);
564 return (0);
565 }
566
567 static int
568 cryptodevkey_cb(void *op)
569 {
570 struct cryptkop *krp = (struct cryptkop *) op;
571
572 mutex_spin_enter(&crypto_mtx);
573 cv_signal(&krp->krp_cv);
574 mutex_spin_exit(&crypto_mtx);
575 return (0);
576 }
577
578 static int
579 cryptodev_key(struct crypt_kop *kop)
580 {
581 struct cryptkop *krp = NULL;
582 int error = EINVAL;
583 int in, out, size, i;
584
585 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
586 return (EFBIG);
587 }
588
589 in = kop->crk_iparams;
590 out = kop->crk_oparams;
591 switch (kop->crk_op) {
592 case CRK_MOD_EXP:
593 if (in == 3 && out == 1)
594 break;
595 return (EINVAL);
596 case CRK_MOD_EXP_CRT:
597 if (in == 6 && out == 1)
598 break;
599 return (EINVAL);
600 case CRK_DSA_SIGN:
601 if (in == 5 && out == 2)
602 break;
603 return (EINVAL);
604 case CRK_DSA_VERIFY:
605 if (in == 7 && out == 0)
606 break;
607 return (EINVAL);
608 case CRK_DH_COMPUTE_KEY:
609 if (in == 3 && out == 1)
610 break;
611 return (EINVAL);
612 case CRK_MOD_ADD:
613 if (in == 3 && out == 1)
614 break;
615 return (EINVAL);
616 case CRK_MOD_ADDINV:
617 if (in == 2 && out == 1)
618 break;
619 return (EINVAL);
620 case CRK_MOD_SUB:
621 if (in == 3 && out == 1)
622 break;
623 return (EINVAL);
624 case CRK_MOD_MULT:
625 if (in == 3 && out == 1)
626 break;
627 return (EINVAL);
628 case CRK_MOD_MULTINV:
629 if (in == 2 && out == 1)
630 break;
631 return (EINVAL);
632 case CRK_MOD:
633 if (in == 2 && out == 1)
634 break;
635 return (EINVAL);
636 default:
637 return (EINVAL);
638 }
639
640 krp = pool_get(&cryptkop_pool, PR_WAITOK);
641 if (!krp)
642 return (ENOMEM);
643 bzero(krp, sizeof *krp);
644 cv_init(&krp->krp_cv, "crykdev");
645 krp->krp_op = kop->crk_op;
646 krp->krp_status = kop->crk_status;
647 krp->krp_iparams = kop->crk_iparams;
648 krp->krp_oparams = kop->crk_oparams;
649 krp->krp_status = 0;
650 krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
651
652 for (i = 0; i < CRK_MAXPARAM; i++)
653 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
654 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
655 size = (krp->krp_param[i].crp_nbits + 7) / 8;
656 if (size == 0)
657 continue;
658 krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
659 if (i >= krp->krp_iparams)
660 continue;
661 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
662 if (error)
663 goto fail;
664 }
665
666 error = crypto_kdispatch(krp);
667 if (error != 0) {
668 goto fail;
669 }
670
671 mutex_spin_enter(&crypto_mtx);
672 while (!(krp->krp_flags & CRYPTO_F_DONE)) {
673 cv_wait(&krp->krp_cv, &crypto_mtx); /* XXX cv_wait_sig? */
674 }
675 if (krp->krp_flags & CRYPTO_F_ONRETQ) {
676 DPRINTF(("cryptodev_key: DONE early, not via cryptoret.\n"));
677 (void)crypto_ret_kq_remove(krp);
678 }
679 mutex_spin_exit(&crypto_mtx);
680
681 if (krp->krp_status != 0) {
682 DPRINTF(("cryptodev_key: krp->krp_status 0x%08x\n", krp->krp_status));
683 error = krp->krp_status;
684 goto fail;
685 }
686
687 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
688 size = (krp->krp_param[i].crp_nbits + 7) / 8;
689 if (size == 0)
690 continue;
691 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
692 if (error) {
693 DPRINTF(("cryptodev_key: copyout oparam %d failed, error=%d\n", i-krp->krp_iparams, error));
694 goto fail;
695 }
696 }
697
698 fail:
699 if (krp) {
700 kop->crk_status = krp->krp_status;
701 for (i = 0; i < CRK_MAXPARAM; i++) {
702 if (krp->krp_param[i].crp_p)
703 FREE(krp->krp_param[i].crp_p, M_XDATA);
704 }
705 pool_put(&cryptkop_pool, krp);
706 }
707 DPRINTF(("cryptodev_key: error=0x%08x\n", error));
708 return (error);
709 }
710
711 /* ARGSUSED */
712 static int
713 cryptof_close(file_t *fp)
714 {
715 struct fcrypt *fcr = fp->f_data;
716 struct csession *cse;
717
718 mutex_spin_enter(&crypto_mtx);
719 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
720 TAILQ_REMOVE(&fcr->csessions, cse, next);
721 (void)csefree(cse);
722 }
723 pool_put(&fcrpl, fcr);
724
725 fp->f_data = NULL;
726 mutex_spin_exit(&crypto_mtx);
727
728 return 0;
729 }
730
731 /* csefind: call with crypto_mtx held. */
732 static struct csession *
733 csefind(struct fcrypt *fcr, u_int ses)
734 {
735 struct csession *cse, *ret = NULL;
736
737 KASSERT(mutex_owned(&crypto_mtx));
738 TAILQ_FOREACH(cse, &fcr->csessions, next)
739 if (cse->ses == ses)
740 ret = cse;
741 return (ret);
742 }
743
744 /* csedelete: call with crypto_mtx held. */
745 static int
746 csedelete(struct fcrypt *fcr, struct csession *cse_del)
747 {
748 struct csession *cse;
749 int ret = 0;
750
751 KASSERT(mutex_owned(&crypto_mtx));
752 TAILQ_FOREACH(cse, &fcr->csessions, next) {
753 if (cse == cse_del) {
754 TAILQ_REMOVE(&fcr->csessions, cse, next);
755 ret = 1;
756 }
757 }
758 return (ret);
759 }
760
761 /* cseadd: call with crypto_mtx held. */
762 static struct csession *
763 cseadd(struct fcrypt *fcr, struct csession *cse)
764 {
765 KASSERT(mutex_owned(&crypto_mtx));
766 /* don't let session ID wrap! */
767 if (fcr->sesn + 1 == 0) return NULL;
768 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
769 cse->ses = fcr->sesn++;
770 return (cse);
771 }
772
773 /* csecreate: call with crypto_mtx held. */
774 static struct csession *
775 csecreate(struct fcrypt *fcr, u_int64_t sid, void *key, u_int64_t keylen,
776 void *mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
777 struct enc_xform *txform, struct auth_hash *thash)
778 {
779 struct csession *cse;
780
781 KASSERT(mutex_owned(&crypto_mtx));
782 cse = pool_get(&csepl, PR_NOWAIT);
783 if (cse == NULL)
784 return NULL;
785 cse->key = key;
786 cse->keylen = keylen/8;
787 cse->mackey = mackey;
788 cse->mackeylen = mackeylen/8;
789 cse->sid = sid;
790 cse->cipher = cipher;
791 cse->mac = mac;
792 cse->txform = txform;
793 cse->thash = thash;
794 cse->error = 0;
795 if (cseadd(fcr, cse))
796 return (cse);
797 else {
798 pool_put(&csepl, cse);
799 return NULL;
800 }
801 }
802
803 /* csefree: call with crypto_mtx held. */
804 static int
805 csefree(struct csession *cse)
806 {
807 int error;
808
809 KASSERT(mutex_owned(&crypto_mtx));
810 error = crypto_freesession(cse->sid);
811 if (cse->key)
812 FREE(cse->key, M_XDATA);
813 if (cse->mackey)
814 FREE(cse->mackey, M_XDATA);
815 pool_put(&csepl, cse);
816 return (error);
817 }
818
819 static int
820 cryptoopen(dev_t dev, int flag, int mode,
821 struct lwp *l)
822 {
823 file_t *fp;
824 struct fcrypt *fcr;
825 int fd, error;
826
827 if (crypto_usercrypto == 0)
828 return (ENXIO);
829
830 if ((error = fd_allocfile(&fp, &fd)) != 0)
831 return error;
832
833 fcr = pool_get(&fcrpl, PR_WAITOK);
834 mutex_spin_enter(&crypto_mtx);
835 TAILQ_INIT(&fcr->csessions);
836 /*
837 * Don't ever return session 0, to allow detection of
838 * failed creation attempts with multi-create ioctl.
839 */
840 fcr->sesn = 1;
841 mutex_spin_exit(&crypto_mtx);
842 return fd_clone(fp, fd, flag, &cryptofops, fcr);
843 }
844
845 static int
846 cryptoread(dev_t dev, struct uio *uio, int ioflag)
847 {
848 return (EIO);
849 }
850
851 static int
852 cryptowrite(dev_t dev, struct uio *uio, int ioflag)
853 {
854 return (EIO);
855 }
856
857 int
858 cryptoselect(dev_t dev, int rw, struct lwp *l)
859 {
860 return (0);
861 }
862
863 /*static*/
864 struct cdevsw crypto_cdevsw = {
865 /* open */ cryptoopen,
866 /* close */ noclose,
867 /* read */ cryptoread,
868 /* write */ cryptowrite,
869 /* ioctl */ noioctl,
870 /* ttstop?*/ nostop,
871 /* ??*/ notty,
872 /* poll */ cryptoselect /*nopoll*/,
873 /* mmap */ nommap,
874 /* kqfilter */ nokqfilter,
875 /* type */ D_OTHER,
876 };
877
878 /*
879 * Pseudo-device initialization routine for /dev/crypto
880 */
881 void cryptoattach(int);
882
883 void
884 cryptoattach(int num)
885 {
886 pool_init(&fcrpl, sizeof(struct fcrypt), 0, 0, 0, "fcrpl",
887 NULL, IPL_NET); /* XXX IPL_NET ("splcrypto") */
888 pool_init(&csepl, sizeof(struct csession), 0, 0, 0, "csepl",
889 NULL, IPL_NET); /* XXX IPL_NET ("splcrypto") */
890
891 /*
892 * Preallocate space for 64 users, with 5 sessions each.
893 * (consider that a TLS protocol session requires at least
894 * 3DES, MD5, and SHA1 (both hashes are used in the PRF) for
895 * the negotiation, plus HMAC_SHA1 for the actual SSL records,
896 * consuming one session here for each algorithm.
897 */
898 pool_prime(&fcrpl, 64);
899 pool_prime(&csepl, 64 * 5);
900 }
901