1 /* $OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $ */
4 * Copyright (c) 2001 Theo de Raadt
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Effort sponsored in part by the Defense Advanced Research Projects
30 * Agency (DARPA) and Air Force Research Laboratory, Air Force
31 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/sysctl.h>
45 #include <sys/filedesc.h>
46 #include <sys/errno.h>
48 #include <sys/random.h>
50 #include <sys/kernel.h>
51 #include <sys/module.h>
52 #include <sys/fcntl.h>
54 #include <opencrypto/cryptodev.h>
55 #include <opencrypto/xform.h>
58 TAILQ_ENTRY(csession) next;
61 struct mtx lock; /* for op submission */
64 struct enc_xform *txform;
66 struct auth_hash *thash;
70 u_char tmp_iv[EALG_MAX_BLOCK_LEN];
74 u_char tmp_mac[CRYPTO_MAX_MAC_LEN];
82 TAILQ_HEAD(csessionlist, csession) csessions;
86 static int cryptof_rw(struct file *fp, struct uio *uio,
87 struct ucred *cred, int flags, struct thread *);
88 static int cryptof_ioctl(struct file *, u_long, void *,
89 struct ucred *, struct thread *);
90 static int cryptof_poll(struct file *, int, struct ucred *, struct thread *);
91 static int cryptof_kqfilter(struct file *, struct knote *);
92 static int cryptof_stat(struct file *, struct stat *,
93 struct ucred *, struct thread *);
94 static int cryptof_close(struct file *, struct thread *);
96 static struct fileops cryptofops = {
97 .fo_read = cryptof_rw,
98 .fo_write = cryptof_rw,
99 .fo_ioctl = cryptof_ioctl,
100 .fo_poll = cryptof_poll,
101 .fo_kqfilter = cryptof_kqfilter,
102 .fo_stat = cryptof_stat,
103 .fo_close = cryptof_close
106 static struct csession *csefind(struct fcrypt *, u_int);
107 static int csedelete(struct fcrypt *, struct csession *);
108 static struct csession *cseadd(struct fcrypt *, struct csession *);
109 static struct csession *csecreate(struct fcrypt *, u_int64_t, caddr_t,
110 u_int64_t, caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
112 static int csefree(struct csession *);
114 static int cryptodev_op(struct csession *, struct crypt_op *,
115 struct ucred *, struct thread *td);
116 static int cryptodev_key(struct crypt_kop *);
122 struct ucred *active_cred,
136 struct ucred *active_cred,
139 struct cryptoini cria, crie;
140 struct fcrypt *fcr = fp->f_data;
141 struct csession *cse;
142 struct session_op *sop;
143 struct crypt_op *cop;
144 struct enc_xform *txform = NULL;
145 struct auth_hash *thash = NULL;
151 * XXX: Not sure Giant is needed, but better safe than sorry
156 sop = (struct session_op *)data;
157 switch (sop->cipher) {
161 txform = &enc_xform_des;
163 case CRYPTO_3DES_CBC:
164 txform = &enc_xform_3des;
167 txform = &enc_xform_blf;
169 case CRYPTO_CAST_CBC:
170 txform = &enc_xform_cast5;
172 case CRYPTO_SKIPJACK_CBC:
173 txform = &enc_xform_skipjack;
176 txform = &enc_xform_rijndael128;
178 case CRYPTO_NULL_CBC:
179 txform = &enc_xform_null;
182 txform = &enc_xform_arc4;
192 case CRYPTO_MD5_HMAC:
193 thash = &auth_hash_hmac_md5_96;
195 case CRYPTO_SHA1_HMAC:
196 thash = &auth_hash_hmac_sha1_96;
198 case CRYPTO_SHA2_HMAC:
199 if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize)
200 thash = &auth_hash_hmac_sha2_256;
201 else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize)
202 thash = &auth_hash_hmac_sha2_384;
203 else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize)
204 thash = &auth_hash_hmac_sha2_512;
210 case CRYPTO_RIPEMD160_HMAC:
211 thash = &auth_hash_hmac_ripemd_160_96;
215 thash = &auth_hash_md5;
218 thash = &auth_hash_sha1;
221 case CRYPTO_NULL_HMAC:
222 thash = &auth_hash_null;
229 bzero(&crie, sizeof(crie));
230 bzero(&cria, sizeof(cria));
233 crie.cri_alg = txform->type;
234 crie.cri_klen = sop->keylen * 8;
235 if (sop->keylen > txform->maxkey ||
236 sop->keylen < txform->minkey) {
241 MALLOC(crie.cri_key, u_int8_t *,
242 crie.cri_klen / 8, M_XDATA, M_WAITOK);
243 if ((error = copyin(sop->key, crie.cri_key,
247 crie.cri_next = &cria;
251 cria.cri_alg = thash->type;
252 cria.cri_klen = sop->mackeylen * 8;
253 if (sop->mackeylen != thash->keysize) {
259 MALLOC(cria.cri_key, u_int8_t *,
260 cria.cri_klen / 8, M_XDATA, M_WAITOK);
261 if ((error = copyin(sop->mackey, cria.cri_key,
267 error = crypto_newsession(&sid, (txform ? &crie : &cria), 1);
271 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
272 cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
276 crypto_freesession(sid);
285 FREE(crie.cri_key, M_XDATA);
287 FREE(cria.cri_key, M_XDATA);
291 ses = *(u_int32_t *)data;
292 cse = csefind(fcr, ses);
298 error = csefree(cse);
301 cop = (struct crypt_op *)data;
302 cse = csefind(fcr, cop->ses);
307 error = cryptodev_op(cse, cop, active_cred, td);
310 error = cryptodev_key((struct crypt_kop *)data);
313 error = crypto_getfeat((int *)data);
322 static int cryptodev_cb(void *);
327 struct csession *cse,
328 struct crypt_op *cop,
329 struct ucred *active_cred,
332 struct cryptop *crp = NULL;
333 struct cryptodesc *crde = NULL, *crda = NULL;
336 if (cop->len > 256*1024-4)
339 if (cse->txform && (cop->len % cse->txform->blocksize) != 0)
342 cse->uio.uio_iov = &cse->iovec;
343 cse->uio.uio_iovcnt = 1;
344 cse->uio.uio_offset = 0;
345 cse->uio.uio_resid = cop->len;
346 cse->uio.uio_segflg = UIO_SYSSPACE;
347 cse->uio.uio_rw = UIO_WRITE;
348 cse->uio.uio_td = td;
349 cse->uio.uio_iov[0].iov_len = cop->len;
350 cse->uio.uio_iov[0].iov_base = malloc(cop->len, M_XDATA, M_WAITOK);
352 crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
359 crda = crp->crp_desc;
361 crde = crda->crd_next;
364 crde = crp->crp_desc;
371 if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
376 crda->crd_len = cop->len;
377 crda->crd_inject = 0; /* ??? */
379 crda->crd_alg = cse->mac;
380 crda->crd_key = cse->mackey;
381 crda->crd_klen = cse->mackeylen * 8;
385 if (cop->op == COP_ENCRYPT)
386 crde->crd_flags |= CRD_F_ENCRYPT;
388 crde->crd_flags &= ~CRD_F_ENCRYPT;
389 crde->crd_len = cop->len;
390 crde->crd_inject = 0;
392 crde->crd_alg = cse->cipher;
393 crde->crd_key = cse->key;
394 crde->crd_klen = cse->keylen * 8;
397 crp->crp_ilen = cop->len;
398 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
399 | (cop->flags & COP_F_BATCH);
400 crp->crp_buf = (caddr_t)&cse->uio;
401 crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
402 crp->crp_sid = cse->sid;
403 crp->crp_opaque = (void *)cse;
410 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
414 if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
416 bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
417 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
419 } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
422 crde->crd_flags |= CRD_F_IV_PRESENT;
423 crde->crd_skip = cse->txform->blocksize;
424 crde->crd_len -= cse->txform->blocksize;
432 crp->crp_mac=cse->tmp_mac;
436 * Let the dispatch run unlocked, then, interlock against the
437 * callback before checking if the operation completed and going
438 * to sleep. This insures drivers don't inherit our lock which
439 * results in a lock order reversal between crypto_dispatch forced
440 * entry and the crypto_done callback into us.
442 error = crypto_dispatch(crp);
443 mtx_lock(&cse->lock);
444 if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
445 error = msleep(crp, &cse->lock, PWAIT, "crydev", 0);
446 mtx_unlock(&cse->lock);
451 if (crp->crp_etype != 0) {
452 error = crp->crp_etype;
462 (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
466 (error = copyout(crp->crp_mac, cop->mac, cse->thash->authsize)))
472 if (cse->uio.uio_iov[0].iov_base)
473 free(cse->uio.uio_iov[0].iov_base, M_XDATA);
479 cryptodev_cb(void *op)
481 struct cryptop *crp = (struct cryptop *) op;
482 struct csession *cse = (struct csession *)crp->crp_opaque;
484 cse->error = crp->crp_etype;
485 if (crp->crp_etype == EAGAIN)
486 return crypto_dispatch(crp);
487 mtx_lock(&cse->lock);
489 mtx_unlock(&cse->lock);
494 cryptodevkey_cb(void *op)
496 struct cryptkop *krp = (struct cryptkop *) op;
503 cryptodev_key(struct crypt_kop *kop)
505 struct cryptkop *krp = NULL;
507 int in, out, size, i;
509 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
513 in = kop->crk_iparams;
514 out = kop->crk_oparams;
515 switch (kop->crk_op) {
517 if (in == 3 && out == 1)
520 case CRK_MOD_EXP_CRT:
521 if (in == 6 && out == 1)
525 if (in == 5 && out == 2)
529 if (in == 7 && out == 0)
532 case CRK_DH_COMPUTE_KEY:
533 if (in == 3 && out == 1)
540 krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK);
543 bzero(krp, sizeof *krp);
544 krp->krp_op = kop->crk_op;
545 krp->krp_status = kop->crk_status;
546 krp->krp_iparams = kop->crk_iparams;
547 krp->krp_oparams = kop->crk_oparams;
549 krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
551 for (i = 0; i < CRK_MAXPARAM; i++)
552 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
553 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
554 size = (krp->krp_param[i].crp_nbits + 7) / 8;
557 MALLOC(krp->krp_param[i].crp_p, caddr_t, size, M_XDATA, M_WAITOK);
558 if (i >= krp->krp_iparams)
560 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
565 error = crypto_kdispatch(krp);
568 error = tsleep(krp, PSOCK, "crydev", 0);
570 /* XXX can this happen? if so, how do we recover? */
574 if (krp->krp_status != 0) {
575 error = krp->krp_status;
579 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
580 size = (krp->krp_param[i].crp_nbits + 7) / 8;
583 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
590 kop->crk_status = krp->krp_status;
591 for (i = 0; i < CRK_MAXPARAM; i++) {
592 if (krp->krp_param[i].crp_p)
593 FREE(krp->krp_param[i].crp_p, M_XDATA);
605 struct ucred *active_cred,
614 cryptof_kqfilter(struct file *fp, struct knote *kn)
625 struct ucred *active_cred,
634 cryptof_close(struct file *fp, struct thread *td)
636 struct fcrypt *fcr = fp->f_data;
637 struct csession *cse;
639 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
640 TAILQ_REMOVE(&fcr->csessions, cse, next);
648 static struct csession *
649 csefind(struct fcrypt *fcr, u_int ses)
651 struct csession *cse;
653 TAILQ_FOREACH(cse, &fcr->csessions, next)
660 csedelete(struct fcrypt *fcr, struct csession *cse_del)
662 struct csession *cse;
664 TAILQ_FOREACH(cse, &fcr->csessions, next) {
665 if (cse == cse_del) {
666 TAILQ_REMOVE(&fcr->csessions, cse, next);
673 static struct csession *
674 cseadd(struct fcrypt *fcr, struct csession *cse)
676 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
677 cse->ses = fcr->sesn++;
682 csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
683 caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
684 struct enc_xform *txform, struct auth_hash *thash)
686 struct csession *cse;
689 /* NB: required when mtx_init is built with INVARIANTS */
690 MALLOC(cse, struct csession *, sizeof(struct csession),
691 M_XDATA, M_NOWAIT | M_ZERO);
693 MALLOC(cse, struct csession *, sizeof(struct csession),
698 mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
700 cse->keylen = keylen/8;
701 cse->mackey = mackey;
702 cse->mackeylen = mackeylen/8;
704 cse->cipher = cipher;
706 cse->txform = txform;
713 csefree(struct csession *cse)
717 error = crypto_freesession(cse->sid);
718 mtx_destroy(&cse->lock);
720 FREE(cse->key, M_XDATA);
722 FREE(cse->mackey, M_XDATA);
728 cryptoopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
734 cryptoread(struct cdev *dev, struct uio *uio, int ioflag)
740 cryptowrite(struct cdev *dev, struct uio *uio, int ioflag)
746 cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
754 MALLOC(fcr, struct fcrypt *,
755 sizeof(struct fcrypt), M_XDATA, M_WAITOK);
756 TAILQ_INIT(&fcr->csessions);
759 error = falloc(td, &f, &fd);
765 /* falloc automatically provides an extra reference to 'f'. */
766 f->f_flag = FREAD | FWRITE;
767 f->f_type = DTYPE_CRYPTO;
768 f->f_ops = &cryptofops;
770 *(u_int32_t *)data = fd;
780 static struct cdevsw crypto_cdevsw = {
781 .d_version = D_VERSION,
782 .d_flags = D_NEEDGIANT,
783 .d_open = cryptoopen,
784 .d_read = cryptoread,
785 .d_write = cryptowrite,
786 .d_ioctl = cryptoioctl,
789 static struct cdev *crypto_dev;
792 * Initialization code, both for static and dynamic loading.
795 cryptodev_modevent(module_t mod, int type, void *unused)
800 printf("crypto: <crypto device>\n");
801 crypto_dev = make_dev(&crypto_cdevsw, 0,
802 UID_ROOT, GID_WHEEL, 0666,
806 /*XXX disallow if active sessions */
807 destroy_dev(crypto_dev);
813 static moduledata_t cryptodev_mod = {
818 MODULE_VERSION(cryptodev, 1);
819 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
820 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);