1 /* $OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $ */
4 * Copyright (c) 2001 Theo de Raadt
5 * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * Effort sponsored in part by the Defense Advanced Research Projects
31 * Agency (DARPA) and Air Force Research Laboratory, Air Force
32 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
43 #include <sys/mutex.h>
44 #include <sys/sysctl.h>
46 #include <sys/filedesc.h>
47 #include <sys/errno.h>
49 #include <sys/random.h>
51 #include <sys/kernel.h>
52 #include <sys/module.h>
53 #include <sys/fcntl.h>
56 #include <opencrypto/cryptodev.h>
57 #include <opencrypto/xform.h>
60 TAILQ_ENTRY(csession) next;
63 struct mtx lock; /* for op submission */
66 struct enc_xform *txform;
68 struct auth_hash *thash;
72 u_char tmp_iv[EALG_MAX_BLOCK_LEN];
83 TAILQ_HEAD(csessionlist, csession) csessions;
87 static int cryptof_rw(struct file *fp, struct uio *uio,
88 struct ucred *cred, int flags, struct thread *);
89 static int cryptof_truncate(struct file *, off_t, struct ucred *,
91 static int cryptof_ioctl(struct file *, u_long, void *,
92 struct ucred *, struct thread *);
93 static int cryptof_poll(struct file *, int, struct ucred *, struct thread *);
94 static int cryptof_kqfilter(struct file *, struct knote *);
95 static int cryptof_stat(struct file *, struct stat *,
96 struct ucred *, struct thread *);
97 static int cryptof_close(struct file *, struct thread *);
99 static struct fileops cryptofops = {
100 .fo_read = cryptof_rw,
101 .fo_write = cryptof_rw,
102 .fo_truncate = cryptof_truncate,
103 .fo_ioctl = cryptof_ioctl,
104 .fo_poll = cryptof_poll,
105 .fo_kqfilter = cryptof_kqfilter,
106 .fo_stat = cryptof_stat,
107 .fo_close = cryptof_close
110 static struct csession *csefind(struct fcrypt *, u_int);
111 static int csedelete(struct fcrypt *, struct csession *);
112 static struct csession *cseadd(struct fcrypt *, struct csession *);
113 static struct csession *csecreate(struct fcrypt *, u_int64_t, caddr_t,
114 u_int64_t, caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
116 static int csefree(struct csession *);
118 static int cryptodev_op(struct csession *, struct crypt_op *,
119 struct ucred *, struct thread *td);
120 static int cryptodev_key(struct crypt_kop *);
121 static int cryptodev_find(struct crypt_find_op *);
127 struct ucred *active_cred,
139 struct ucred *active_cred,
147 * Check a crypto identifier to see if it requested
148 * a software device/driver. This can be done either
149 * by device name/class or through search constraints.
152 checkforsoftware(int crid)
154 if (crid & CRYPTOCAP_F_SOFTWARE)
155 return EINVAL; /* XXX */
156 if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
157 (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
158 return EINVAL; /* XXX */
168 struct ucred *active_cred,
171 #define SES2(p) ((struct session2_op *)p)
172 struct cryptoini cria, crie;
173 struct fcrypt *fcr = fp->f_data;
174 struct csession *cse;
175 struct session_op *sop;
176 struct crypt_op *cop;
177 struct enc_xform *txform = NULL;
178 struct auth_hash *thash = NULL;
179 struct crypt_kop *kop;
187 sop = (struct session_op *)data;
188 switch (sop->cipher) {
192 txform = &enc_xform_des;
194 case CRYPTO_3DES_CBC:
195 txform = &enc_xform_3des;
198 txform = &enc_xform_blf;
200 case CRYPTO_CAST_CBC:
201 txform = &enc_xform_cast5;
203 case CRYPTO_SKIPJACK_CBC:
204 txform = &enc_xform_skipjack;
207 txform = &enc_xform_rijndael128;
209 case CRYPTO_NULL_CBC:
210 txform = &enc_xform_null;
213 txform = &enc_xform_arc4;
215 case CRYPTO_CAMELLIA_CBC:
216 txform = &enc_xform_camellia;
225 case CRYPTO_MD5_HMAC:
226 thash = &auth_hash_hmac_md5;
228 case CRYPTO_SHA1_HMAC:
229 thash = &auth_hash_hmac_sha1;
231 case CRYPTO_SHA2_256_HMAC:
232 thash = &auth_hash_hmac_sha2_256;
234 case CRYPTO_SHA2_384_HMAC:
235 thash = &auth_hash_hmac_sha2_384;
237 case CRYPTO_SHA2_512_HMAC:
238 thash = &auth_hash_hmac_sha2_512;
240 case CRYPTO_RIPEMD160_HMAC:
241 thash = &auth_hash_hmac_ripemd_160;
245 thash = &auth_hash_md5;
248 thash = &auth_hash_sha1;
251 case CRYPTO_NULL_HMAC:
252 thash = &auth_hash_null;
258 bzero(&crie, sizeof(crie));
259 bzero(&cria, sizeof(cria));
262 crie.cri_alg = txform->type;
263 crie.cri_klen = sop->keylen * 8;
264 if (sop->keylen > txform->maxkey ||
265 sop->keylen < txform->minkey) {
270 MALLOC(crie.cri_key, u_int8_t *,
271 crie.cri_klen / 8, M_XDATA, M_WAITOK);
272 if ((error = copyin(sop->key, crie.cri_key,
276 crie.cri_next = &cria;
280 cria.cri_alg = thash->type;
281 cria.cri_klen = sop->mackeylen * 8;
282 if (sop->mackeylen != thash->keysize) {
288 MALLOC(cria.cri_key, u_int8_t *,
289 cria.cri_klen / 8, M_XDATA, M_WAITOK);
290 if ((error = copyin(sop->mackey, cria.cri_key,
296 /* NB: CIOGSESSION2 has the crid */
297 if (cmd == CIOCGSESSION2) {
298 crid = SES2(sop)->crid;
299 error = checkforsoftware(crid);
303 crid = CRYPTOCAP_F_HARDWARE;
304 error = crypto_newsession(&sid, (txform ? &crie : &cria), crid);
308 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
309 cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
313 crypto_freesession(sid);
318 if (cmd == CIOCGSESSION2) {
319 /* return hardware/driver id */
320 SES2(sop)->crid = CRYPTO_SESID2HID(cse->sid);
325 FREE(crie.cri_key, M_XDATA);
327 FREE(cria.cri_key, M_XDATA);
331 ses = *(u_int32_t *)data;
332 cse = csefind(fcr, ses);
336 error = csefree(cse);
339 cop = (struct crypt_op *)data;
340 cse = csefind(fcr, cop->ses);
343 error = cryptodev_op(cse, cop, active_cred, td);
347 if (!crypto_userasymcrypto)
348 return (EPERM); /* XXX compat? */
350 kop = (struct crypt_kop *)data;
351 if (cmd == CIOCKEY) {
352 /* NB: crypto core enforces s/w driver use */
354 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
356 error = cryptodev_key(kop);
360 if (!crypto_userasymcrypto) {
362 * NB: if user asym crypto operations are
363 * not permitted return "no algorithms"
364 * so well-behaved applications will just
365 * fallback to doing them in software.
369 error = crypto_getfeat((int *)data);
372 error = cryptodev_find((struct crypt_find_op *)data);
382 static int cryptodev_cb(void *);
387 struct csession *cse,
388 struct crypt_op *cop,
389 struct ucred *active_cred,
392 struct cryptop *crp = NULL;
393 struct cryptodesc *crde = NULL, *crda = NULL;
396 if (cop->len > 256*1024-4)
400 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
404 cse->uio.uio_iov = &cse->iovec;
405 cse->uio.uio_iovcnt = 1;
406 cse->uio.uio_offset = 0;
407 cse->uio.uio_resid = cop->len;
408 cse->uio.uio_segflg = UIO_SYSSPACE;
409 cse->uio.uio_rw = UIO_WRITE;
410 cse->uio.uio_td = td;
411 cse->uio.uio_iov[0].iov_len = cop->len;
413 cse->uio.uio_iov[0].iov_len += cse->thash->hashsize;
414 cse->uio.uio_iov[0].iov_base = malloc(cse->uio.uio_iov[0].iov_len,
417 crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
424 crda = crp->crp_desc;
426 crde = crda->crd_next;
429 crde = crp->crp_desc;
436 if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
441 crda->crd_len = cop->len;
442 crda->crd_inject = cop->len;
444 crda->crd_alg = cse->mac;
445 crda->crd_key = cse->mackey;
446 crda->crd_klen = cse->mackeylen * 8;
450 if (cop->op == COP_ENCRYPT)
451 crde->crd_flags |= CRD_F_ENCRYPT;
453 crde->crd_flags &= ~CRD_F_ENCRYPT;
454 crde->crd_len = cop->len;
455 crde->crd_inject = 0;
457 crde->crd_alg = cse->cipher;
458 crde->crd_key = cse->key;
459 crde->crd_klen = cse->keylen * 8;
462 crp->crp_ilen = cop->len;
463 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
464 | (cop->flags & COP_F_BATCH);
465 crp->crp_buf = (caddr_t)&cse->uio;
466 crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
467 crp->crp_sid = cse->sid;
468 crp->crp_opaque = (void *)cse;
475 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
479 if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
481 bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
482 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
484 } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
487 crde->crd_flags |= CRD_F_IV_PRESENT;
488 crde->crd_skip = cse->txform->blocksize;
489 crde->crd_len -= cse->txform->blocksize;
492 if (cop->mac && crda == NULL) {
498 * Let the dispatch run unlocked, then, interlock against the
499 * callback before checking if the operation completed and going
500 * to sleep. This insures drivers don't inherit our lock which
501 * results in a lock order reversal between crypto_dispatch forced
502 * entry and the crypto_done callback into us.
504 error = crypto_dispatch(crp);
505 mtx_lock(&cse->lock);
506 if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
507 error = msleep(crp, &cse->lock, PWAIT, "crydev", 0);
508 mtx_unlock(&cse->lock);
513 if (crp->crp_etype != 0) {
514 error = crp->crp_etype;
524 (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
528 (error = copyout((caddr_t)cse->uio.uio_iov[0].iov_base + cop->len,
529 cop->mac, cse->thash->hashsize)))
535 if (cse->uio.uio_iov[0].iov_base)
536 free(cse->uio.uio_iov[0].iov_base, M_XDATA);
542 cryptodev_cb(void *op)
544 struct cryptop *crp = (struct cryptop *) op;
545 struct csession *cse = (struct csession *)crp->crp_opaque;
548 error = crp->crp_etype;
550 error = crypto_dispatch(crp);
551 mtx_lock(&cse->lock);
552 if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
556 mtx_unlock(&cse->lock);
561 cryptodevkey_cb(void *op)
563 struct cryptkop *krp = (struct cryptkop *) op;
570 cryptodev_key(struct crypt_kop *kop)
572 struct cryptkop *krp = NULL;
574 int in, out, size, i;
576 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
580 in = kop->crk_iparams;
581 out = kop->crk_oparams;
582 switch (kop->crk_op) {
584 if (in == 3 && out == 1)
587 case CRK_MOD_EXP_CRT:
588 if (in == 6 && out == 1)
592 if (in == 5 && out == 2)
596 if (in == 7 && out == 0)
599 case CRK_DH_COMPUTE_KEY:
600 if (in == 3 && out == 1)
607 krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK|M_ZERO);
610 krp->krp_op = kop->crk_op;
611 krp->krp_status = kop->crk_status;
612 krp->krp_iparams = kop->crk_iparams;
613 krp->krp_oparams = kop->crk_oparams;
614 krp->krp_crid = kop->crk_crid;
616 krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
618 for (i = 0; i < CRK_MAXPARAM; i++) {
619 if (kop->crk_param[i].crp_nbits > 65536)
620 /* Limit is the same as in OpenBSD */
622 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
624 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
625 size = (krp->krp_param[i].crp_nbits + 7) / 8;
628 MALLOC(krp->krp_param[i].crp_p, caddr_t, size, M_XDATA, M_WAITOK);
629 if (i >= krp->krp_iparams)
631 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
636 error = crypto_kdispatch(krp);
639 error = tsleep(krp, PSOCK, "crydev", 0);
641 /* XXX can this happen? if so, how do we recover? */
645 kop->crk_crid = krp->krp_crid; /* device that did the work */
646 if (krp->krp_status != 0) {
647 error = krp->krp_status;
651 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
652 size = (krp->krp_param[i].crp_nbits + 7) / 8;
655 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
662 kop->crk_status = krp->krp_status;
663 for (i = 0; i < CRK_MAXPARAM; i++) {
664 if (krp->krp_param[i].crp_p)
665 FREE(krp->krp_param[i].crp_p, M_XDATA);
673 cryptodev_find(struct crypt_find_op *find)
677 if (find->crid != -1) {
678 dev = crypto_find_device_byhid(find->crid);
681 strlcpy(find->name, device_get_nameunit(dev),
684 find->crid = crypto_find_driver(find->name);
685 if (find->crid == -1)
696 struct ucred *active_cred,
705 cryptof_kqfilter(struct file *fp, struct knote *kn)
716 struct ucred *active_cred,
725 cryptof_close(struct file *fp, struct thread *td)
727 struct fcrypt *fcr = fp->f_data;
728 struct csession *cse;
730 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
731 TAILQ_REMOVE(&fcr->csessions, cse, next);
739 static struct csession *
740 csefind(struct fcrypt *fcr, u_int ses)
742 struct csession *cse;
744 TAILQ_FOREACH(cse, &fcr->csessions, next)
751 csedelete(struct fcrypt *fcr, struct csession *cse_del)
753 struct csession *cse;
755 TAILQ_FOREACH(cse, &fcr->csessions, next) {
756 if (cse == cse_del) {
757 TAILQ_REMOVE(&fcr->csessions, cse, next);
764 static struct csession *
765 cseadd(struct fcrypt *fcr, struct csession *cse)
767 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
768 cse->ses = fcr->sesn++;
773 csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
774 caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
775 struct enc_xform *txform, struct auth_hash *thash)
777 struct csession *cse;
780 /* NB: required when mtx_init is built with INVARIANTS */
781 MALLOC(cse, struct csession *, sizeof(struct csession),
782 M_XDATA, M_NOWAIT | M_ZERO);
784 MALLOC(cse, struct csession *, sizeof(struct csession),
789 mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
791 cse->keylen = keylen/8;
792 cse->mackey = mackey;
793 cse->mackeylen = mackeylen/8;
795 cse->cipher = cipher;
797 cse->txform = txform;
804 csefree(struct csession *cse)
808 error = crypto_freesession(cse->sid);
809 mtx_destroy(&cse->lock);
811 FREE(cse->key, M_XDATA);
813 FREE(cse->mackey, M_XDATA);
819 cryptoopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
825 cryptoread(struct cdev *dev, struct uio *uio, int ioflag)
831 cryptowrite(struct cdev *dev, struct uio *uio, int ioflag)
837 cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
845 MALLOC(fcr, struct fcrypt *,
846 sizeof(struct fcrypt), M_XDATA, M_WAITOK);
847 TAILQ_INIT(&fcr->csessions);
850 error = falloc(td, &f, &fd);
856 /* falloc automatically provides an extra reference to 'f'. */
857 finit(f, FREAD | FWRITE, DTYPE_CRYPTO, fcr, &cryptofops);
858 *(u_int32_t *)data = fd;
862 error = cryptodev_find((struct crypt_find_op *)data);
865 error = crypto_getfeat((int *)data);
874 static struct cdevsw crypto_cdevsw = {
875 .d_version = D_VERSION,
876 .d_flags = D_NEEDGIANT,
877 .d_open = cryptoopen,
878 .d_read = cryptoread,
879 .d_write = cryptowrite,
880 .d_ioctl = cryptoioctl,
883 static struct cdev *crypto_dev;
886 * Initialization code, both for static and dynamic loading.
889 cryptodev_modevent(module_t mod, int type, void *unused)
894 printf("crypto: <crypto device>\n");
895 crypto_dev = make_dev(&crypto_cdevsw, 0,
896 UID_ROOT, GID_WHEEL, 0666,
900 /*XXX disallow if active sessions */
901 destroy_dev(crypto_dev);
907 static moduledata_t cryptodev_mod = {
912 MODULE_VERSION(cryptodev, 1);
913 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
914 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
915 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);