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_ioctl(struct file *, u_long, void *,
90 struct ucred *, struct thread *);
91 static int cryptof_poll(struct file *, int, struct ucred *, struct thread *);
92 static int cryptof_kqfilter(struct file *, struct knote *);
93 static int cryptof_stat(struct file *, struct stat *,
94 struct ucred *, struct thread *);
95 static int cryptof_close(struct file *, struct thread *);
97 static struct fileops cryptofops = {
98 .fo_read = cryptof_rw,
99 .fo_write = cryptof_rw,
100 .fo_ioctl = cryptof_ioctl,
101 .fo_poll = cryptof_poll,
102 .fo_kqfilter = cryptof_kqfilter,
103 .fo_stat = cryptof_stat,
104 .fo_close = cryptof_close
107 static struct csession *csefind(struct fcrypt *, u_int);
108 static int csedelete(struct fcrypt *, struct csession *);
109 static struct csession *cseadd(struct fcrypt *, struct csession *);
110 static struct csession *csecreate(struct fcrypt *, u_int64_t, caddr_t,
111 u_int64_t, caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
113 static int csefree(struct csession *);
115 static int cryptodev_op(struct csession *, struct crypt_op *,
116 struct ucred *, struct thread *td);
117 static int cryptodev_key(struct crypt_kop *);
118 static int cryptodev_find(struct crypt_find_op *);
124 struct ucred *active_cred,
133 * Check a crypto identifier to see if it requested
134 * a software device/driver. This can be done either
135 * by device name/class or through search constraints.
138 checkforsoftware(int crid)
140 if (crid & CRYPTOCAP_F_SOFTWARE)
141 return EINVAL; /* XXX */
142 if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
143 (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
144 return EINVAL; /* XXX */
154 struct ucred *active_cred,
157 #define SES2(p) ((struct session2_op *)p)
158 struct cryptoini cria, crie;
159 struct fcrypt *fcr = fp->f_data;
160 struct csession *cse;
161 struct session_op *sop;
162 struct crypt_op *cop;
163 struct enc_xform *txform = NULL;
164 struct auth_hash *thash = NULL;
165 struct crypt_kop *kop;
173 sop = (struct session_op *)data;
174 switch (sop->cipher) {
178 txform = &enc_xform_des;
180 case CRYPTO_3DES_CBC:
181 txform = &enc_xform_3des;
184 txform = &enc_xform_blf;
186 case CRYPTO_CAST_CBC:
187 txform = &enc_xform_cast5;
189 case CRYPTO_SKIPJACK_CBC:
190 txform = &enc_xform_skipjack;
193 txform = &enc_xform_rijndael128;
195 case CRYPTO_NULL_CBC:
196 txform = &enc_xform_null;
199 txform = &enc_xform_arc4;
201 case CRYPTO_CAMELLIA_CBC:
202 txform = &enc_xform_camellia;
211 case CRYPTO_MD5_HMAC:
212 thash = &auth_hash_hmac_md5;
214 case CRYPTO_SHA1_HMAC:
215 thash = &auth_hash_hmac_sha1;
217 case CRYPTO_SHA2_256_HMAC:
218 thash = &auth_hash_hmac_sha2_256;
220 case CRYPTO_SHA2_384_HMAC:
221 thash = &auth_hash_hmac_sha2_384;
223 case CRYPTO_SHA2_512_HMAC:
224 thash = &auth_hash_hmac_sha2_512;
226 case CRYPTO_RIPEMD160_HMAC:
227 thash = &auth_hash_hmac_ripemd_160;
231 thash = &auth_hash_md5;
234 thash = &auth_hash_sha1;
237 case CRYPTO_NULL_HMAC:
238 thash = &auth_hash_null;
244 bzero(&crie, sizeof(crie));
245 bzero(&cria, sizeof(cria));
248 crie.cri_alg = txform->type;
249 crie.cri_klen = sop->keylen * 8;
250 if (sop->keylen > txform->maxkey ||
251 sop->keylen < txform->minkey) {
256 MALLOC(crie.cri_key, u_int8_t *,
257 crie.cri_klen / 8, M_XDATA, M_WAITOK);
258 if ((error = copyin(sop->key, crie.cri_key,
262 crie.cri_next = &cria;
266 cria.cri_alg = thash->type;
267 cria.cri_klen = sop->mackeylen * 8;
268 if (sop->mackeylen != thash->keysize) {
274 MALLOC(cria.cri_key, u_int8_t *,
275 cria.cri_klen / 8, M_XDATA, M_WAITOK);
276 if ((error = copyin(sop->mackey, cria.cri_key,
282 /* NB: CIOGSESSION2 has the crid */
283 if (cmd == CIOCGSESSION2) {
284 crid = SES2(sop)->crid;
285 error = checkforsoftware(crid);
289 crid = CRYPTOCAP_F_HARDWARE;
290 error = crypto_newsession(&sid, (txform ? &crie : &cria), crid);
294 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
295 cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
299 crypto_freesession(sid);
304 if (cmd == CIOCGSESSION2) {
305 /* return hardware/driver id */
306 SES2(sop)->crid = CRYPTO_SESID2HID(cse->sid);
311 FREE(crie.cri_key, M_XDATA);
313 FREE(cria.cri_key, M_XDATA);
317 ses = *(u_int32_t *)data;
318 cse = csefind(fcr, ses);
322 error = csefree(cse);
325 cop = (struct crypt_op *)data;
326 cse = csefind(fcr, cop->ses);
329 error = cryptodev_op(cse, cop, active_cred, td);
333 if (!crypto_userasymcrypto)
334 return (EPERM); /* XXX compat? */
336 kop = (struct crypt_kop *)data;
337 if (cmd == CIOCKEY) {
338 /* NB: crypto core enforces s/w driver use */
340 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
342 error = cryptodev_key(kop);
346 if (!crypto_userasymcrypto) {
348 * NB: if user asym crypto operations are
349 * not permitted return "no algorithms"
350 * so well-behaved applications will just
351 * fallback to doing them in software.
355 error = crypto_getfeat((int *)data);
358 error = cryptodev_find((struct crypt_find_op *)data);
368 static int cryptodev_cb(void *);
373 struct csession *cse,
374 struct crypt_op *cop,
375 struct ucred *active_cred,
378 struct cryptop *crp = NULL;
379 struct cryptodesc *crde = NULL, *crda = NULL;
382 if (cop->len > 256*1024-4)
386 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
390 cse->uio.uio_iov = &cse->iovec;
391 cse->uio.uio_iovcnt = 1;
392 cse->uio.uio_offset = 0;
393 cse->uio.uio_resid = cop->len;
394 cse->uio.uio_segflg = UIO_SYSSPACE;
395 cse->uio.uio_rw = UIO_WRITE;
396 cse->uio.uio_td = td;
397 cse->uio.uio_iov[0].iov_len = cop->len;
399 cse->uio.uio_iov[0].iov_len += cse->thash->hashsize;
400 cse->uio.uio_iov[0].iov_base = malloc(cse->uio.uio_iov[0].iov_len,
403 crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
410 crda = crp->crp_desc;
412 crde = crda->crd_next;
415 crde = crp->crp_desc;
422 if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
427 crda->crd_len = cop->len;
428 crda->crd_inject = cop->len;
430 crda->crd_alg = cse->mac;
431 crda->crd_key = cse->mackey;
432 crda->crd_klen = cse->mackeylen * 8;
436 if (cop->op == COP_ENCRYPT)
437 crde->crd_flags |= CRD_F_ENCRYPT;
439 crde->crd_flags &= ~CRD_F_ENCRYPT;
440 crde->crd_len = cop->len;
441 crde->crd_inject = 0;
443 crde->crd_alg = cse->cipher;
444 crde->crd_key = cse->key;
445 crde->crd_klen = cse->keylen * 8;
448 crp->crp_ilen = cop->len;
449 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
450 | (cop->flags & COP_F_BATCH);
451 crp->crp_buf = (caddr_t)&cse->uio;
452 crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
453 crp->crp_sid = cse->sid;
454 crp->crp_opaque = (void *)cse;
461 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
465 if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
467 bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
468 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
470 } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
473 crde->crd_flags |= CRD_F_IV_PRESENT;
474 crde->crd_skip = cse->txform->blocksize;
475 crde->crd_len -= cse->txform->blocksize;
478 if (cop->mac && crda == NULL) {
484 * Let the dispatch run unlocked, then, interlock against the
485 * callback before checking if the operation completed and going
486 * to sleep. This insures drivers don't inherit our lock which
487 * results in a lock order reversal between crypto_dispatch forced
488 * entry and the crypto_done callback into us.
490 error = crypto_dispatch(crp);
491 mtx_lock(&cse->lock);
492 if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
493 error = msleep(crp, &cse->lock, PWAIT, "crydev", 0);
494 mtx_unlock(&cse->lock);
499 if (crp->crp_etype != 0) {
500 error = crp->crp_etype;
510 (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
514 (error = copyout((caddr_t)cse->uio.uio_iov[0].iov_base + cop->len,
515 cop->mac, cse->thash->hashsize)))
521 if (cse->uio.uio_iov[0].iov_base)
522 free(cse->uio.uio_iov[0].iov_base, M_XDATA);
528 cryptodev_cb(void *op)
530 struct cryptop *crp = (struct cryptop *) op;
531 struct csession *cse = (struct csession *)crp->crp_opaque;
534 error = crp->crp_etype;
536 error = crypto_dispatch(crp);
537 mtx_lock(&cse->lock);
538 if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
542 mtx_unlock(&cse->lock);
547 cryptodevkey_cb(void *op)
549 struct cryptkop *krp = (struct cryptkop *) op;
556 cryptodev_key(struct crypt_kop *kop)
558 struct cryptkop *krp = NULL;
560 int in, out, size, i;
562 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
566 in = kop->crk_iparams;
567 out = kop->crk_oparams;
568 switch (kop->crk_op) {
570 if (in == 3 && out == 1)
573 case CRK_MOD_EXP_CRT:
574 if (in == 6 && out == 1)
578 if (in == 5 && out == 2)
582 if (in == 7 && out == 0)
585 case CRK_DH_COMPUTE_KEY:
586 if (in == 3 && out == 1)
593 krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK|M_ZERO);
596 krp->krp_op = kop->crk_op;
597 krp->krp_status = kop->crk_status;
598 krp->krp_iparams = kop->crk_iparams;
599 krp->krp_oparams = kop->crk_oparams;
600 krp->krp_crid = kop->crk_crid;
602 krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
604 for (i = 0; i < CRK_MAXPARAM; i++) {
605 if (kop->crk_param[i].crp_nbits > 65536)
606 /* Limit is the same as in OpenBSD */
608 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
610 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
611 size = (krp->krp_param[i].crp_nbits + 7) / 8;
614 MALLOC(krp->krp_param[i].crp_p, caddr_t, size, M_XDATA, M_WAITOK);
615 if (i >= krp->krp_iparams)
617 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
622 error = crypto_kdispatch(krp);
625 error = tsleep(krp, PSOCK, "crydev", 0);
627 /* XXX can this happen? if so, how do we recover? */
631 kop->crk_crid = krp->krp_crid; /* device that did the work */
632 if (krp->krp_status != 0) {
633 error = krp->krp_status;
637 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
638 size = (krp->krp_param[i].crp_nbits + 7) / 8;
641 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
648 kop->crk_status = krp->krp_status;
649 for (i = 0; i < CRK_MAXPARAM; i++) {
650 if (krp->krp_param[i].crp_p)
651 FREE(krp->krp_param[i].crp_p, M_XDATA);
659 cryptodev_find(struct crypt_find_op *find)
663 if (find->crid != -1) {
664 dev = crypto_find_device_byhid(find->crid);
667 strlcpy(find->name, device_get_nameunit(dev),
670 find->crid = crypto_find_driver(find->name);
671 if (find->crid == -1)
682 struct ucred *active_cred,
691 cryptof_kqfilter(struct file *fp, struct knote *kn)
702 struct ucred *active_cred,
711 cryptof_close(struct file *fp, struct thread *td)
713 struct fcrypt *fcr = fp->f_data;
714 struct csession *cse;
716 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
717 TAILQ_REMOVE(&fcr->csessions, cse, next);
725 static struct csession *
726 csefind(struct fcrypt *fcr, u_int ses)
728 struct csession *cse;
730 TAILQ_FOREACH(cse, &fcr->csessions, next)
737 csedelete(struct fcrypt *fcr, struct csession *cse_del)
739 struct csession *cse;
741 TAILQ_FOREACH(cse, &fcr->csessions, next) {
742 if (cse == cse_del) {
743 TAILQ_REMOVE(&fcr->csessions, cse, next);
750 static struct csession *
751 cseadd(struct fcrypt *fcr, struct csession *cse)
753 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
754 cse->ses = fcr->sesn++;
759 csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
760 caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
761 struct enc_xform *txform, struct auth_hash *thash)
763 struct csession *cse;
766 /* NB: required when mtx_init is built with INVARIANTS */
767 MALLOC(cse, struct csession *, sizeof(struct csession),
768 M_XDATA, M_NOWAIT | M_ZERO);
770 MALLOC(cse, struct csession *, sizeof(struct csession),
775 mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
777 cse->keylen = keylen/8;
778 cse->mackey = mackey;
779 cse->mackeylen = mackeylen/8;
781 cse->cipher = cipher;
783 cse->txform = txform;
790 csefree(struct csession *cse)
794 error = crypto_freesession(cse->sid);
795 mtx_destroy(&cse->lock);
797 FREE(cse->key, M_XDATA);
799 FREE(cse->mackey, M_XDATA);
805 cryptoopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
811 cryptoread(struct cdev *dev, struct uio *uio, int ioflag)
817 cryptowrite(struct cdev *dev, struct uio *uio, int ioflag)
823 cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
831 MALLOC(fcr, struct fcrypt *,
832 sizeof(struct fcrypt), M_XDATA, M_WAITOK);
833 TAILQ_INIT(&fcr->csessions);
836 error = falloc(td, &f, &fd);
842 /* falloc automatically provides an extra reference to 'f'. */
844 f->f_flag = FREAD | FWRITE;
845 f->f_type = DTYPE_CRYPTO;
847 f->f_ops = &cryptofops;
849 *(u_int32_t *)data = fd;
853 error = cryptodev_find((struct crypt_find_op *)data);
856 error = crypto_getfeat((int *)data);
865 static struct cdevsw crypto_cdevsw = {
866 .d_version = D_VERSION,
867 .d_flags = D_NEEDGIANT,
868 .d_open = cryptoopen,
869 .d_read = cryptoread,
870 .d_write = cryptowrite,
871 .d_ioctl = cryptoioctl,
874 static struct cdev *crypto_dev;
877 * Initialization code, both for static and dynamic loading.
880 cryptodev_modevent(module_t mod, int type, void *unused)
885 printf("crypto: <crypto device>\n");
886 crypto_dev = make_dev(&crypto_cdevsw, 0,
887 UID_ROOT, GID_WHEEL, 0666,
891 /*XXX disallow if active sessions */
892 destroy_dev(crypto_dev);
898 static moduledata_t cryptodev_mod = {
903 MODULE_VERSION(cryptodev, 1);
904 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
905 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
906 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);