1 /* $FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $ */
2 /* $DragonFly: src/sys/opencrypto/cryptodev.c,v 1.3 2003/06/25 03:56:08 dillon Exp $ */
3 /* $OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $ */
6 * Copyright (c) 2001 Theo de Raadt
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
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.
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
41 #include <sys/sysctl.h>
43 #include <sys/filedesc.h>
44 #include <sys/errno.h>
46 #include <sys/random.h>
48 #include <sys/kernel.h>
49 #include <sys/fcntl.h>
51 #include <sys/file2.h>
53 #include <opencrypto/cryptodev.h>
54 #include <opencrypto/xform.h>
57 TAILQ_ENTRY(csession) next;
62 struct enc_xform *txform;
64 struct auth_hash *thash;
68 u_char tmp_iv[EALG_MAX_BLOCK_LEN];
72 u_char tmp_mac[CRYPTO_MAX_MAC_LEN];
74 struct iovec iovec[UIO_MAXIOV];
80 TAILQ_HEAD(csessionlist, csession) csessions;
84 static int cryptof_rw(struct file *fp, struct uio *uio,
85 struct ucred *cred, int flags, struct thread *);
86 static int cryptof_ioctl(struct file *, u_long, caddr_t, struct thread *);
87 static int cryptof_poll(struct file *, int, struct ucred *, struct thread *);
88 static int cryptof_kqfilter(struct file *, struct knote *);
89 static int cryptof_stat(struct file *, struct stat *, struct thread *);
90 static int cryptof_close(struct file *, struct thread *);
92 static struct fileops cryptofops = {
102 static struct csession *csefind(struct fcrypt *, u_int);
103 static int csedelete(struct fcrypt *, struct csession *);
104 static struct csession *cseadd(struct fcrypt *, struct csession *);
105 static struct csession *csecreate(struct fcrypt *, u_int64_t, caddr_t,
106 u_int64_t, caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
108 static int csefree(struct csession *);
110 static int cryptodev_op(struct csession *, struct crypt_op *,
112 static int cryptodev_key(struct crypt_kop *);
118 struct ucred *active_cred,
134 struct cryptoini cria, crie;
135 struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
136 struct csession *cse;
137 struct session_op *sop;
138 struct crypt_op *cop;
139 struct enc_xform *txform = NULL;
140 struct auth_hash *thash = NULL;
147 sop = (struct session_op *)data;
148 switch (sop->cipher) {
152 txform = &enc_xform_des;
154 case CRYPTO_3DES_CBC:
155 txform = &enc_xform_3des;
158 txform = &enc_xform_blf;
160 case CRYPTO_CAST_CBC:
161 txform = &enc_xform_cast5;
163 case CRYPTO_SKIPJACK_CBC:
164 txform = &enc_xform_skipjack;
167 txform = &enc_xform_rijndael128;
169 case CRYPTO_NULL_CBC:
170 txform = &enc_xform_null;
173 txform = &enc_xform_arc4;
182 case CRYPTO_MD5_HMAC:
183 thash = &auth_hash_hmac_md5_96;
185 case CRYPTO_SHA1_HMAC:
186 thash = &auth_hash_hmac_sha1_96;
188 case CRYPTO_SHA2_HMAC:
189 if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize)
190 thash = &auth_hash_hmac_sha2_256;
191 else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize)
192 thash = &auth_hash_hmac_sha2_384;
193 else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize)
194 thash = &auth_hash_hmac_sha2_512;
198 case CRYPTO_RIPEMD160_HMAC:
199 thash = &auth_hash_hmac_ripemd_160_96;
203 thash = &auth_hash_md5;
206 thash = &auth_hash_sha1;
209 case CRYPTO_NULL_HMAC:
210 thash = &auth_hash_null;
216 bzero(&crie, sizeof(crie));
217 bzero(&cria, sizeof(cria));
220 crie.cri_alg = txform->type;
221 crie.cri_klen = sop->keylen * 8;
222 if (sop->keylen > txform->maxkey ||
223 sop->keylen < txform->minkey) {
228 MALLOC(crie.cri_key, u_int8_t *,
229 crie.cri_klen / 8, M_XDATA, M_WAITOK);
230 if ((error = copyin(sop->key, crie.cri_key,
234 crie.cri_next = &cria;
238 cria.cri_alg = thash->type;
239 cria.cri_klen = sop->mackeylen * 8;
240 if (sop->mackeylen != thash->keysize) {
246 MALLOC(cria.cri_key, u_int8_t *,
247 cria.cri_klen / 8, M_XDATA, M_WAITOK);
248 if ((error = copyin(sop->mackey, cria.cri_key,
254 error = crypto_newsession(&sid, (txform ? &crie : &cria), 1);
258 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
259 cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
263 crypto_freesession(sid);
272 FREE(crie.cri_key, M_XDATA);
274 FREE(cria.cri_key, M_XDATA);
278 ses = *(u_int32_t *)data;
279 cse = csefind(fcr, ses);
283 error = csefree(cse);
286 cop = (struct crypt_op *)data;
287 cse = csefind(fcr, cop->ses);
290 error = cryptodev_op(cse, cop, td);
293 error = cryptodev_key((struct crypt_kop *)data);
296 error = crypto_getfeat((int *)data);
304 static int cryptodev_cb(void *);
309 struct csession *cse,
310 struct crypt_op *cop,
313 struct cryptop *crp = NULL;
314 struct cryptodesc *crde = NULL, *crda = NULL;
317 if (cop->len > 256*1024-4)
320 if (cse->txform && (cop->len % cse->txform->blocksize) != 0)
323 bzero(&cse->uio, sizeof(cse->uio));
324 cse->uio.uio_iovcnt = 1;
325 cse->uio.uio_resid = 0;
326 cse->uio.uio_segflg = UIO_SYSSPACE;
327 cse->uio.uio_rw = UIO_WRITE;
328 cse->uio.uio_td = td;
329 cse->uio.uio_iov = cse->iovec;
330 bzero(&cse->iovec, sizeof(cse->iovec));
331 cse->uio.uio_iov[0].iov_len = cop->len;
332 cse->uio.uio_iov[0].iov_base = malloc(cop->len, M_XDATA, M_WAITOK);
333 for (i = 0; i < cse->uio.uio_iovcnt; i++)
334 cse->uio.uio_resid += cse->uio.uio_iov[0].iov_len;
336 crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
343 crda = crp->crp_desc;
345 crde = crda->crd_next;
348 crde = crp->crp_desc;
355 if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
360 crda->crd_len = cop->len;
361 crda->crd_inject = 0; /* ??? */
363 crda->crd_alg = cse->mac;
364 crda->crd_key = cse->mackey;
365 crda->crd_klen = cse->mackeylen * 8;
369 if (cop->op == COP_ENCRYPT)
370 crde->crd_flags |= CRD_F_ENCRYPT;
372 crde->crd_flags &= ~CRD_F_ENCRYPT;
373 crde->crd_len = cop->len;
374 crde->crd_inject = 0;
376 crde->crd_alg = cse->cipher;
377 crde->crd_key = cse->key;
378 crde->crd_klen = cse->keylen * 8;
381 crp->crp_ilen = cop->len;
382 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
383 | (cop->flags & COP_F_BATCH);
384 crp->crp_buf = (caddr_t)&cse->uio;
385 crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
386 crp->crp_sid = cse->sid;
387 crp->crp_opaque = (void *)cse;
394 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
398 if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
400 bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
401 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
403 } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
406 crde->crd_flags |= CRD_F_IV_PRESENT;
407 crde->crd_skip = cse->txform->blocksize;
408 crde->crd_len -= cse->txform->blocksize;
416 crp->crp_mac=cse->tmp_mac;
419 s = splcrypto(); /* NB: only needed with CRYPTO_F_CBIMM */
420 error = crypto_dispatch(crp);
421 if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
422 error = tsleep(crp, PSOCK, "crydev", 0);
427 if (crp->crp_etype != 0) {
428 error = crp->crp_etype;
438 (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
442 (error = copyout(crp->crp_mac, cop->mac, cse->thash->authsize)))
448 if (cse->uio.uio_iov[0].iov_base)
449 free(cse->uio.uio_iov[0].iov_base, M_XDATA);
455 cryptodev_cb(void *op)
457 struct cryptop *crp = (struct cryptop *) op;
458 struct csession *cse = (struct csession *)crp->crp_opaque;
460 cse->error = crp->crp_etype;
461 if (crp->crp_etype == EAGAIN)
462 return crypto_dispatch(crp);
468 cryptodevkey_cb(void *op)
470 struct cryptkop *krp = (struct cryptkop *) op;
477 cryptodev_key(struct crypt_kop *kop)
479 struct cryptkop *krp = NULL;
481 int in, out, size, i;
483 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
487 in = kop->crk_iparams;
488 out = kop->crk_oparams;
489 switch (kop->crk_op) {
491 if (in == 3 && out == 1)
494 case CRK_MOD_EXP_CRT:
495 if (in == 6 && out == 1)
499 if (in == 5 && out == 2)
503 if (in == 7 && out == 0)
506 case CRK_DH_COMPUTE_KEY:
507 if (in == 3 && out == 1)
514 krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK);
517 bzero(krp, sizeof *krp);
518 krp->krp_op = kop->crk_op;
519 krp->krp_status = kop->crk_status;
520 krp->krp_iparams = kop->crk_iparams;
521 krp->krp_oparams = kop->crk_oparams;
523 krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
525 for (i = 0; i < CRK_MAXPARAM; i++)
526 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
527 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
528 size = (krp->krp_param[i].crp_nbits + 7) / 8;
531 MALLOC(krp->krp_param[i].crp_p, caddr_t, size, M_XDATA, M_WAITOK);
532 if (i >= krp->krp_iparams)
534 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
539 error = crypto_kdispatch(krp);
541 error = tsleep(krp, PSOCK, "crydev", 0);
545 if (krp->krp_status != 0) {
546 error = krp->krp_status;
550 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
551 size = (krp->krp_param[i].crp_nbits + 7) / 8;
554 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
561 kop->crk_status = krp->krp_status;
562 for (i = 0; i < CRK_MAXPARAM; i++) {
563 if (krp->krp_param[i].crp_p)
564 FREE(krp->krp_param[i].crp_p, M_XDATA);
576 struct ucred *active_cred,
585 cryptof_kqfilter(struct file *fp, struct knote *kn)
604 cryptof_close(struct file *fp, struct thread *td)
606 struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
607 struct csession *cse;
609 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
610 TAILQ_REMOVE(&fcr->csessions, cse, next);
618 static struct csession *
619 csefind(struct fcrypt *fcr, u_int ses)
621 struct csession *cse;
623 TAILQ_FOREACH(cse, &fcr->csessions, next)
630 csedelete(struct fcrypt *fcr, struct csession *cse_del)
632 struct csession *cse;
634 TAILQ_FOREACH(cse, &fcr->csessions, next) {
635 if (cse == cse_del) {
636 TAILQ_REMOVE(&fcr->csessions, cse, next);
643 static struct csession *
644 cseadd(struct fcrypt *fcr, struct csession *cse)
646 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
647 cse->ses = fcr->sesn++;
652 csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
653 caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
654 struct enc_xform *txform, struct auth_hash *thash)
656 struct csession *cse;
658 MALLOC(cse, struct csession *, sizeof(struct csession),
663 cse->keylen = keylen/8;
664 cse->mackey = mackey;
665 cse->mackeylen = mackeylen/8;
667 cse->cipher = cipher;
669 cse->txform = txform;
676 csefree(struct csession *cse)
680 error = crypto_freesession(cse->sid);
682 FREE(cse->key, M_XDATA);
684 FREE(cse->mackey, M_XDATA);
690 cryptoopen(dev_t dev, int oflags, int devtype, struct thread *td)
692 if (crypto_usercrypto == 0)
698 cryptoread(dev_t dev, struct uio *uio, int ioflag)
704 cryptowrite(dev_t dev, struct uio *uio, int ioflag)
710 cryptoioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
717 MALLOC(fcr, struct fcrypt *,
718 sizeof(struct fcrypt), M_XDATA, M_WAITOK);
719 TAILQ_INIT(&fcr->csessions);
722 KKASSERT(td->td_proc);
723 error = falloc(td->td_proc, &f, &fd);
730 f->f_flag = FREAD | FWRITE;
731 f->f_type = DTYPE_CRYPTO;
732 f->f_ops = &cryptofops;
733 f->f_data = (caddr_t) fcr;
734 *(u_int32_t *)data = fd;
744 #define CRYPTO_MAJOR 70 /* from openbsd */
745 static struct cdevsw crypto_cdevsw = {
746 /* open */ cryptoopen,
747 /* close */ nullclose,
748 /* read */ cryptoread,
749 /* write */ cryptowrite,
750 /* ioctl */ cryptoioctl,
753 /* strategy */ nostrategy,
754 /* dev name */ "crypto",
755 /* dev major */ CRYPTO_MAJOR,
761 static dev_t crypto_dev;
764 * Initialization code, both for static and dynamic loading.
767 cryptodev_modevent(module_t mod, int type, void *unused)
772 printf("crypto: <crypto device>\n");
773 crypto_dev = make_dev(&crypto_cdevsw, 0,
774 UID_ROOT, GID_WHEEL, 0666,
778 /*XXX disallow if active sessions */
779 destroy_dev(crypto_dev);
785 static moduledata_t cryptodev_mod = {
790 MODULE_VERSION(cryptodev, 1);
791 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
793 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);