1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069 |
- /* $OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $ */
- /*-
- * Linux port done by David McCullough <david_mccullough@mcafee.com>
- * Copyright (C) 2006-2010 David McCullough
- * Copyright (C) 2004-2005 Intel Corporation.
- * The license and original author are listed below.
- *
- * Copyright (c) 2001 Theo de Raadt
- * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. The name of the author may not be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * Effort sponsored in part by the Defense Advanced Research Projects
- * Agency (DARPA) and Air Force Research Laboratory, Air Force
- * Materiel Command, USAF, under agreement number F30602-01-2-0537.
- *
- __FBSDID("$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.34 2007/05/09 19:37:02 gnn Exp $");
- */
- #include <linux/version.h>
- #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38) && !defined(AUTOCONF_INCLUDED)
- #include <linux/config.h>
- #endif
- #include <linux/types.h>
- #include <linux/time.h>
- #include <linux/delay.h>
- #include <linux/list.h>
- #include <linux/init.h>
- #include <linux/sched.h>
- #include <linux/unistd.h>
- #include <linux/module.h>
- #include <linux/wait.h>
- #include <linux/slab.h>
- #include <linux/fs.h>
- #include <linux/dcache.h>
- #include <linux/file.h>
- #include <linux/mount.h>
- #include <linux/miscdevice.h>
- #include <asm/uaccess.h>
- #include <cryptodev.h>
- #include <uio.h>
- extern asmlinkage long sys_dup(unsigned int fildes);
- #define debug cryptodev_debug
- int cryptodev_debug = 0;
- module_param(cryptodev_debug, int, 0644);
- MODULE_PARM_DESC(cryptodev_debug, "Enable cryptodev debug");
- struct csession_info {
- u_int16_t blocksize;
- u_int16_t minkey, maxkey;
- u_int16_t keysize;
- /* u_int16_t hashsize; */
- u_int16_t authsize;
- u_int16_t authkey;
- /* u_int16_t ctxsize; */
- };
- struct csession {
- struct list_head list;
- u_int64_t sid;
- u_int32_t ses;
- wait_queue_head_t waitq;
- u_int32_t cipher;
- u_int32_t mac;
- caddr_t key;
- int keylen;
- u_char tmp_iv[EALG_MAX_BLOCK_LEN];
- caddr_t mackey;
- int mackeylen;
- struct csession_info info;
- struct iovec iovec;
- struct uio uio;
- int error;
- };
- struct fcrypt {
- struct list_head csessions;
- int sesn;
- };
- static struct csession *csefind(struct fcrypt *, u_int);
- static int csedelete(struct fcrypt *, struct csession *);
- static struct csession *cseadd(struct fcrypt *, struct csession *);
- static struct csession *csecreate(struct fcrypt *, u_int64_t,
- struct cryptoini *crie, struct cryptoini *cria, struct csession_info *);
- static int csefree(struct csession *);
- static int cryptodev_op(struct csession *, struct crypt_op *);
- static int cryptodev_key(struct crypt_kop *);
- static int cryptodev_find(struct crypt_find_op *);
- static int cryptodev_cb(void *);
- static int cryptodev_open(struct inode *inode, struct file *filp);
- /*
- * Check a crypto identifier to see if it requested
- * a valid crid and it's capabilities match.
- */
- static int
- checkcrid(int crid)
- {
- int hid = crid & ~(CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE);
- int typ = crid & (CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE);
- int caps = 0;
-
- /* if the user hasn't selected a driver, then just call newsession */
- if (hid == 0 && typ != 0)
- return 0;
- caps = crypto_getcaps(hid);
- /* didn't find anything with capabilities */
- if (caps == 0) {
- dprintk("%s: hid=%x typ=%x not matched\n", __FUNCTION__, hid, typ);
- return EINVAL;
- }
-
- /* the user didn't specify SW or HW, so the driver is ok */
- if (typ == 0)
- return 0;
- /* if the type specified didn't match */
- if (typ != (caps & (CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE))) {
- dprintk("%s: hid=%x typ=%x caps=%x not matched\n", __FUNCTION__,
- hid, typ, caps);
- return EINVAL;
- }
- return 0;
- }
- static int
- cryptodev_op(struct csession *cse, struct crypt_op *cop)
- {
- struct cryptop *crp = NULL;
- struct cryptodesc *crde = NULL, *crda = NULL;
- int error = 0;
- dprintk("%s()\n", __FUNCTION__);
- if (cop->len > CRYPTO_MAX_DATA_LEN) {
- dprintk("%s: %d > %d\n", __FUNCTION__, cop->len, CRYPTO_MAX_DATA_LEN);
- return (E2BIG);
- }
- if (cse->info.blocksize && (cop->len % cse->info.blocksize) != 0) {
- dprintk("%s: blocksize=%d len=%d\n", __FUNCTION__, cse->info.blocksize,
- cop->len);
- return (EINVAL);
- }
- cse->uio.uio_iov = &cse->iovec;
- cse->uio.uio_iovcnt = 1;
- cse->uio.uio_offset = 0;
- #if 0
- cse->uio.uio_resid = cop->len;
- cse->uio.uio_segflg = UIO_SYSSPACE;
- cse->uio.uio_rw = UIO_WRITE;
- cse->uio.uio_td = td;
- #endif
- cse->uio.uio_iov[0].iov_len = cop->len;
- if (cse->info.authsize)
- cse->uio.uio_iov[0].iov_len += cse->info.authsize;
- cse->uio.uio_iov[0].iov_base = kmalloc(cse->uio.uio_iov[0].iov_len,
- GFP_KERNEL);
- if (cse->uio.uio_iov[0].iov_base == NULL) {
- dprintk("%s: iov_base kmalloc(%d) failed\n", __FUNCTION__,
- (int)cse->uio.uio_iov[0].iov_len);
- return (ENOMEM);
- }
- crp = crypto_getreq((cse->info.blocksize != 0) + (cse->info.authsize != 0));
- if (crp == NULL) {
- dprintk("%s: ENOMEM\n", __FUNCTION__);
- error = ENOMEM;
- goto bail;
- }
- if (cse->info.authsize && cse->info.blocksize) {
- if (cop->op == COP_ENCRYPT) {
- crde = crp->crp_desc;
- crda = crde->crd_next;
- } else {
- crda = crp->crp_desc;
- crde = crda->crd_next;
- }
- } else if (cse->info.authsize) {
- crda = crp->crp_desc;
- } else if (cse->info.blocksize) {
- crde = crp->crp_desc;
- } else {
- dprintk("%s: bad request\n", __FUNCTION__);
- error = EINVAL;
- goto bail;
- }
- if ((error = copy_from_user(cse->uio.uio_iov[0].iov_base, cop->src,
- cop->len))) {
- dprintk("%s: bad copy\n", __FUNCTION__);
- goto bail;
- }
- if (crda) {
- crda->crd_skip = 0;
- crda->crd_len = cop->len;
- crda->crd_inject = cop->len;
- crda->crd_alg = cse->mac;
- crda->crd_key = cse->mackey;
- crda->crd_klen = cse->mackeylen * 8;
- }
- if (crde) {
- if (cop->op == COP_ENCRYPT)
- crde->crd_flags |= CRD_F_ENCRYPT;
- else
- crde->crd_flags &= ~CRD_F_ENCRYPT;
- crde->crd_len = cop->len;
- crde->crd_inject = 0;
- crde->crd_alg = cse->cipher;
- crde->crd_key = cse->key;
- crde->crd_klen = cse->keylen * 8;
- }
- crp->crp_ilen = cse->uio.uio_iov[0].iov_len;
- crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
- | (cop->flags & COP_F_BATCH);
- crp->crp_buf = (caddr_t)&cse->uio;
- crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
- crp->crp_sid = cse->sid;
- crp->crp_opaque = (void *)cse;
- if (cop->iv) {
- if (crde == NULL) {
- error = EINVAL;
- dprintk("%s no crde\n", __FUNCTION__);
- goto bail;
- }
- if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
- error = EINVAL;
- dprintk("%s arc4 with IV\n", __FUNCTION__);
- goto bail;
- }
- if ((error = copy_from_user(cse->tmp_iv, cop->iv,
- cse->info.blocksize))) {
- dprintk("%s bad iv copy\n", __FUNCTION__);
- goto bail;
- }
- memcpy(crde->crd_iv, cse->tmp_iv, cse->info.blocksize);
- crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
- crde->crd_skip = 0;
- } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
- crde->crd_skip = 0;
- } else if (crde) {
- crde->crd_flags |= CRD_F_IV_PRESENT;
- crde->crd_skip = cse->info.blocksize;
- crde->crd_len -= cse->info.blocksize;
- }
- if (cop->mac && crda == NULL) {
- error = EINVAL;
- dprintk("%s no crda\n", __FUNCTION__);
- goto bail;
- }
- /*
- * Let the dispatch run unlocked, then, interlock against the
- * callback before checking if the operation completed and going
- * to sleep. This insures drivers don't inherit our lock which
- * results in a lock order reversal between crypto_dispatch forced
- * entry and the crypto_done callback into us.
- */
- error = crypto_dispatch(crp);
- if (error) {
- dprintk("%s error in crypto_dispatch\n", __FUNCTION__);
- goto bail;
- }
- dprintk("%s about to WAIT\n", __FUNCTION__);
- /*
- * we really need to wait for driver to complete to maintain
- * state, luckily interrupts will be remembered
- */
- do {
- error = wait_event_interruptible(crp->crp_waitq,
- ((crp->crp_flags & CRYPTO_F_DONE) != 0));
- /*
- * we can't break out of this loop or we will leave behind
- * a huge mess, however, staying here means if your driver
- * is broken user applications can hang and not be killed.
- * The solution, fix your driver :-)
- */
- if (error) {
- schedule();
- error = 0;
- }
- } while ((crp->crp_flags & CRYPTO_F_DONE) == 0);
- dprintk("%s finished WAITING error=%d\n", __FUNCTION__, error);
- if (crp->crp_etype != 0) {
- error = crp->crp_etype;
- dprintk("%s error in crp processing\n", __FUNCTION__);
- goto bail;
- }
- if (cse->error) {
- error = cse->error;
- dprintk("%s error in cse processing\n", __FUNCTION__);
- goto bail;
- }
- if (cop->dst && (error = copy_to_user(cop->dst,
- cse->uio.uio_iov[0].iov_base, cop->len))) {
- dprintk("%s bad dst copy\n", __FUNCTION__);
- goto bail;
- }
- if (cop->mac &&
- (error=copy_to_user(cop->mac,
- (caddr_t)cse->uio.uio_iov[0].iov_base + cop->len,
- cse->info.authsize))) {
- dprintk("%s bad mac copy\n", __FUNCTION__);
- goto bail;
- }
- bail:
- if (crp)
- crypto_freereq(crp);
- if (cse->uio.uio_iov[0].iov_base)
- kfree(cse->uio.uio_iov[0].iov_base);
- return (error);
- }
- static int
- cryptodev_cb(void *op)
- {
- struct cryptop *crp = (struct cryptop *) op;
- struct csession *cse = (struct csession *)crp->crp_opaque;
- int error;
- dprintk("%s()\n", __FUNCTION__);
- error = crp->crp_etype;
- if (error == EAGAIN) {
- crp->crp_flags &= ~CRYPTO_F_DONE;
- #ifdef NOTYET
- /*
- * DAVIDM I am fairly sure that we should turn this into a batch
- * request to stop bad karma/lockup, revisit
- */
- crp->crp_flags |= CRYPTO_F_BATCH;
- #endif
- return crypto_dispatch(crp);
- }
- if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
- cse->error = error;
- wake_up_interruptible(&crp->crp_waitq);
- }
- return (0);
- }
- static int
- cryptodevkey_cb(void *op)
- {
- struct cryptkop *krp = (struct cryptkop *) op;
- dprintk("%s()\n", __FUNCTION__);
- wake_up_interruptible(&krp->krp_waitq);
- return (0);
- }
- static int
- cryptodev_key(struct crypt_kop *kop)
- {
- struct cryptkop *krp = NULL;
- int error = EINVAL;
- int in, out, size, i;
- dprintk("%s()\n", __FUNCTION__);
- if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
- dprintk("%s params too big\n", __FUNCTION__);
- return (EFBIG);
- }
- in = kop->crk_iparams;
- out = kop->crk_oparams;
- switch (kop->crk_op) {
- case CRK_MOD_EXP:
- if (in == 3 && out == 1)
- break;
- return (EINVAL);
- case CRK_MOD_EXP_CRT:
- if (in == 6 && out == 1)
- break;
- return (EINVAL);
- case CRK_DSA_SIGN:
- if (in == 5 && out == 2)
- break;
- return (EINVAL);
- case CRK_DSA_VERIFY:
- if (in == 7 && out == 0)
- break;
- return (EINVAL);
- case CRK_DH_COMPUTE_KEY:
- if (in == 3 && out == 1)
- break;
- return (EINVAL);
- default:
- return (EINVAL);
- }
- krp = (struct cryptkop *)kmalloc(sizeof *krp, GFP_KERNEL);
- if (!krp)
- return (ENOMEM);
- bzero(krp, sizeof *krp);
- krp->krp_op = kop->crk_op;
- krp->krp_status = kop->crk_status;
- krp->krp_iparams = kop->crk_iparams;
- krp->krp_oparams = kop->crk_oparams;
- krp->krp_crid = kop->crk_crid;
- krp->krp_status = 0;
- krp->krp_flags = CRYPTO_KF_CBIMM;
- krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
- init_waitqueue_head(&krp->krp_waitq);
- for (i = 0; i < CRK_MAXPARAM; i++)
- krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
- for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
- size = (krp->krp_param[i].crp_nbits + 7) / 8;
- if (size == 0)
- continue;
- krp->krp_param[i].crp_p = (caddr_t) kmalloc(size, GFP_KERNEL);
- if (i >= krp->krp_iparams)
- continue;
- error = copy_from_user(krp->krp_param[i].crp_p,
- kop->crk_param[i].crp_p, size);
- if (error)
- goto fail;
- }
- error = crypto_kdispatch(krp);
- if (error)
- goto fail;
- do {
- error = wait_event_interruptible(krp->krp_waitq,
- ((krp->krp_flags & CRYPTO_KF_DONE) != 0));
- /*
- * we can't break out of this loop or we will leave behind
- * a huge mess, however, staying here means if your driver
- * is broken user applications can hang and not be killed.
- * The solution, fix your driver :-)
- */
- if (error) {
- schedule();
- error = 0;
- }
- } while ((krp->krp_flags & CRYPTO_KF_DONE) == 0);
- dprintk("%s finished WAITING error=%d\n", __FUNCTION__, error);
-
- kop->crk_crid = krp->krp_crid; /* device that did the work */
- if (krp->krp_status != 0) {
- error = krp->krp_status;
- goto fail;
- }
- for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
- size = (krp->krp_param[i].crp_nbits + 7) / 8;
- if (size == 0)
- continue;
- error = copy_to_user(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p,
- size);
- if (error)
- goto fail;
- }
- fail:
- if (krp) {
- kop->crk_status = krp->krp_status;
- for (i = 0; i < CRK_MAXPARAM; i++) {
- if (krp->krp_param[i].crp_p)
- kfree(krp->krp_param[i].crp_p);
- }
- kfree(krp);
- }
- return (error);
- }
- static int
- cryptodev_find(struct crypt_find_op *find)
- {
- device_t dev;
- if (find->crid != -1) {
- dev = crypto_find_device_byhid(find->crid);
- if (dev == NULL)
- return (ENOENT);
- strlcpy(find->name, device_get_nameunit(dev),
- sizeof(find->name));
- } else {
- find->crid = crypto_find_driver(find->name);
- if (find->crid == -1)
- return (ENOENT);
- }
- return (0);
- }
- static struct csession *
- csefind(struct fcrypt *fcr, u_int ses)
- {
- struct csession *cse;
- dprintk("%s()\n", __FUNCTION__);
- list_for_each_entry(cse, &fcr->csessions, list)
- if (cse->ses == ses)
- return (cse);
- return (NULL);
- }
- static int
- csedelete(struct fcrypt *fcr, struct csession *cse_del)
- {
- struct csession *cse;
- dprintk("%s()\n", __FUNCTION__);
- list_for_each_entry(cse, &fcr->csessions, list) {
- if (cse == cse_del) {
- list_del(&cse->list);
- return (1);
- }
- }
- return (0);
- }
-
- static struct csession *
- cseadd(struct fcrypt *fcr, struct csession *cse)
- {
- dprintk("%s()\n", __FUNCTION__);
- list_add_tail(&cse->list, &fcr->csessions);
- cse->ses = fcr->sesn++;
- return (cse);
- }
- static struct csession *
- csecreate(struct fcrypt *fcr, u_int64_t sid, struct cryptoini *crie,
- struct cryptoini *cria, struct csession_info *info)
- {
- struct csession *cse;
- dprintk("%s()\n", __FUNCTION__);
- cse = (struct csession *) kmalloc(sizeof(struct csession), GFP_KERNEL);
- if (cse == NULL)
- return NULL;
- memset(cse, 0, sizeof(struct csession));
- INIT_LIST_HEAD(&cse->list);
- init_waitqueue_head(&cse->waitq);
- cse->key = crie->cri_key;
- cse->keylen = crie->cri_klen/8;
- cse->mackey = cria->cri_key;
- cse->mackeylen = cria->cri_klen/8;
- cse->sid = sid;
- cse->cipher = crie->cri_alg;
- cse->mac = cria->cri_alg;
- cse->info = *info;
- cseadd(fcr, cse);
- return (cse);
- }
- static int
- csefree(struct csession *cse)
- {
- int error;
- dprintk("%s()\n", __FUNCTION__);
- error = crypto_freesession(cse->sid);
- if (cse->key)
- kfree(cse->key);
- if (cse->mackey)
- kfree(cse->mackey);
- kfree(cse);
- return(error);
- }
- static int
- cryptodev_ioctl(
- struct inode *inode,
- struct file *filp,
- unsigned int cmd,
- unsigned long arg)
- {
- struct cryptoini cria, crie;
- struct fcrypt *fcr = filp->private_data;
- struct csession *cse;
- struct csession_info info;
- struct session2_op sop;
- struct crypt_op cop;
- struct crypt_kop kop;
- struct crypt_find_op fop;
- u_int64_t sid;
- u_int32_t ses = 0;
- int feat, fd, error = 0, crid;
- mm_segment_t fs;
- dprintk("%s(cmd=%x arg=%lx)\n", __FUNCTION__, cmd, arg);
- switch (cmd) {
- case CRIOGET: {
- dprintk("%s(CRIOGET)\n", __FUNCTION__);
- fs = get_fs();
- set_fs(get_ds());
- for (fd = 0; fd < files_fdtable(current->files)->max_fds; fd++)
- if (files_fdtable(current->files)->fd[fd] == filp)
- break;
- fd = sys_dup(fd);
- set_fs(fs);
- put_user(fd, (int *) arg);
- return IS_ERR_VALUE(fd) ? fd : 0;
- }
- #define CIOCGSESSSTR (cmd == CIOCGSESSION ? "CIOCGSESSION" : "CIOCGSESSION2")
- case CIOCGSESSION:
- case CIOCGSESSION2:
- dprintk("%s(%s)\n", __FUNCTION__, CIOCGSESSSTR);
- memset(&crie, 0, sizeof(crie));
- memset(&cria, 0, sizeof(cria));
- memset(&info, 0, sizeof(info));
- memset(&sop, 0, sizeof(sop));
- if (copy_from_user(&sop, (void*)arg, (cmd == CIOCGSESSION) ?
- sizeof(struct session_op) : sizeof(sop))) {
- dprintk("%s(%s) - bad copy\n", __FUNCTION__, CIOCGSESSSTR);
- error = EFAULT;
- goto bail;
- }
- switch (sop.cipher) {
- case 0:
- dprintk("%s(%s) - no cipher\n", __FUNCTION__, CIOCGSESSSTR);
- break;
- case CRYPTO_NULL_CBC:
- info.blocksize = NULL_BLOCK_LEN;
- info.minkey = NULL_MIN_KEY_LEN;
- info.maxkey = NULL_MAX_KEY_LEN;
- break;
- case CRYPTO_DES_CBC:
- info.blocksize = DES_BLOCK_LEN;
- info.minkey = DES_MIN_KEY_LEN;
- info.maxkey = DES_MAX_KEY_LEN;
- break;
- case CRYPTO_3DES_CBC:
- info.blocksize = DES3_BLOCK_LEN;
- info.minkey = DES3_MIN_KEY_LEN;
- info.maxkey = DES3_MAX_KEY_LEN;
- break;
- case CRYPTO_BLF_CBC:
- info.blocksize = BLOWFISH_BLOCK_LEN;
- info.minkey = BLOWFISH_MIN_KEY_LEN;
- info.maxkey = BLOWFISH_MAX_KEY_LEN;
- break;
- case CRYPTO_CAST_CBC:
- info.blocksize = CAST128_BLOCK_LEN;
- info.minkey = CAST128_MIN_KEY_LEN;
- info.maxkey = CAST128_MAX_KEY_LEN;
- break;
- case CRYPTO_SKIPJACK_CBC:
- info.blocksize = SKIPJACK_BLOCK_LEN;
- info.minkey = SKIPJACK_MIN_KEY_LEN;
- info.maxkey = SKIPJACK_MAX_KEY_LEN;
- break;
- case CRYPTO_AES_CBC:
- info.blocksize = AES_BLOCK_LEN;
- info.minkey = AES_MIN_KEY_LEN;
- info.maxkey = AES_MAX_KEY_LEN;
- break;
- case CRYPTO_ARC4:
- info.blocksize = ARC4_BLOCK_LEN;
- info.minkey = ARC4_MIN_KEY_LEN;
- info.maxkey = ARC4_MAX_KEY_LEN;
- break;
- case CRYPTO_CAMELLIA_CBC:
- info.blocksize = CAMELLIA_BLOCK_LEN;
- info.minkey = CAMELLIA_MIN_KEY_LEN;
- info.maxkey = CAMELLIA_MAX_KEY_LEN;
- break;
- default:
- dprintk("%s(%s) - bad cipher\n", __FUNCTION__, CIOCGSESSSTR);
- error = EINVAL;
- goto bail;
- }
- switch (sop.mac) {
- case 0:
- dprintk("%s(%s) - no mac\n", __FUNCTION__, CIOCGSESSSTR);
- break;
- case CRYPTO_NULL_HMAC:
- info.authsize = NULL_HASH_LEN;
- break;
- case CRYPTO_MD5:
- info.authsize = MD5_HASH_LEN;
- break;
- case CRYPTO_SHA1:
- info.authsize = SHA1_HASH_LEN;
- break;
- case CRYPTO_SHA2_256:
- info.authsize = SHA2_256_HASH_LEN;
- break;
- case CRYPTO_SHA2_384:
- info.authsize = SHA2_384_HASH_LEN;
- break;
- case CRYPTO_SHA2_512:
- info.authsize = SHA2_512_HASH_LEN;
- break;
- case CRYPTO_RIPEMD160:
- info.authsize = RIPEMD160_HASH_LEN;
- break;
- case CRYPTO_MD5_HMAC:
- info.authsize = MD5_HASH_LEN;
- info.authkey = 16;
- break;
- case CRYPTO_SHA1_HMAC:
- info.authsize = SHA1_HASH_LEN;
- info.authkey = 20;
- break;
- case CRYPTO_SHA2_256_HMAC:
- info.authsize = SHA2_256_HASH_LEN;
- info.authkey = 32;
- break;
- case CRYPTO_SHA2_384_HMAC:
- info.authsize = SHA2_384_HASH_LEN;
- info.authkey = 48;
- break;
- case CRYPTO_SHA2_512_HMAC:
- info.authsize = SHA2_512_HASH_LEN;
- info.authkey = 64;
- break;
- case CRYPTO_RIPEMD160_HMAC:
- info.authsize = RIPEMD160_HASH_LEN;
- info.authkey = 20;
- break;
- default:
- dprintk("%s(%s) - bad mac\n", __FUNCTION__, CIOCGSESSSTR);
- error = EINVAL;
- goto bail;
- }
- if (info.blocksize) {
- crie.cri_alg = sop.cipher;
- crie.cri_klen = sop.keylen * 8;
- if ((info.maxkey && sop.keylen > info.maxkey) ||
- sop.keylen < info.minkey) {
- dprintk("%s(%s) - bad key\n", __FUNCTION__, CIOCGSESSSTR);
- error = EINVAL;
- goto bail;
- }
- crie.cri_key = (u_int8_t *) kmalloc(crie.cri_klen/8+1, GFP_KERNEL);
- if (copy_from_user(crie.cri_key, sop.key,
- crie.cri_klen/8)) {
- dprintk("%s(%s) - bad copy\n", __FUNCTION__, CIOCGSESSSTR);
- error = EFAULT;
- goto bail;
- }
- if (info.authsize)
- crie.cri_next = &cria;
- }
- if (info.authsize) {
- cria.cri_alg = sop.mac;
- cria.cri_klen = sop.mackeylen * 8;
- if (info.authkey && sop.mackeylen != info.authkey) {
- dprintk("%s(%s) - mackeylen %d != %d\n", __FUNCTION__,
- CIOCGSESSSTR, sop.mackeylen, info.authkey);
- error = EINVAL;
- goto bail;
- }
- if (cria.cri_klen) {
- cria.cri_key = (u_int8_t *) kmalloc(cria.cri_klen/8,GFP_KERNEL);
- if (copy_from_user(cria.cri_key, sop.mackey,
- cria.cri_klen / 8)) {
- dprintk("%s(%s) - bad copy\n", __FUNCTION__, CIOCGSESSSTR);
- error = EFAULT;
- goto bail;
- }
- }
- }
- /* NB: CIOGSESSION2 has the crid */
- if (cmd == CIOCGSESSION2) {
- crid = sop.crid;
- error = checkcrid(crid);
- if (error) {
- dprintk("%s(%s) - checkcrid %x\n", __FUNCTION__,
- CIOCGSESSSTR, error);
- goto bail;
- }
- } else {
- /* allow either HW or SW to be used */
- crid = CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
- }
- error = crypto_newsession(&sid, (info.blocksize ? &crie : &cria), crid);
- if (error) {
- dprintk("%s(%s) - newsession %d\n",__FUNCTION__,CIOCGSESSSTR,error);
- goto bail;
- }
- cse = csecreate(fcr, sid, &crie, &cria, &info);
- if (cse == NULL) {
- crypto_freesession(sid);
- error = EINVAL;
- dprintk("%s(%s) - csecreate failed\n", __FUNCTION__, CIOCGSESSSTR);
- goto bail;
- }
- sop.ses = cse->ses;
- if (cmd == CIOCGSESSION2) {
- /* return hardware/driver id */
- sop.crid = CRYPTO_SESID2HID(cse->sid);
- }
- if (copy_to_user((void*)arg, &sop, (cmd == CIOCGSESSION) ?
- sizeof(struct session_op) : sizeof(sop))) {
- dprintk("%s(%s) - bad copy\n", __FUNCTION__, CIOCGSESSSTR);
- error = EFAULT;
- }
- bail:
- if (error) {
- dprintk("%s(%s) - bail %d\n", __FUNCTION__, CIOCGSESSSTR, error);
- if (crie.cri_key)
- kfree(crie.cri_key);
- if (cria.cri_key)
- kfree(cria.cri_key);
- }
- break;
- case CIOCFSESSION:
- dprintk("%s(CIOCFSESSION)\n", __FUNCTION__);
- get_user(ses, (uint32_t*)arg);
- cse = csefind(fcr, ses);
- if (cse == NULL) {
- error = EINVAL;
- dprintk("%s(CIOCFSESSION) - Fail %d\n", __FUNCTION__, error);
- break;
- }
- csedelete(fcr, cse);
- error = csefree(cse);
- break;
- case CIOCCRYPT:
- dprintk("%s(CIOCCRYPT)\n", __FUNCTION__);
- if(copy_from_user(&cop, (void*)arg, sizeof(cop))) {
- dprintk("%s(CIOCCRYPT) - bad copy\n", __FUNCTION__);
- error = EFAULT;
- goto bail;
- }
- cse = csefind(fcr, cop.ses);
- if (cse == NULL) {
- error = EINVAL;
- dprintk("%s(CIOCCRYPT) - Fail %d\n", __FUNCTION__, error);
- break;
- }
- error = cryptodev_op(cse, &cop);
- if(copy_to_user((void*)arg, &cop, sizeof(cop))) {
- dprintk("%s(CIOCCRYPT) - bad return copy\n", __FUNCTION__);
- error = EFAULT;
- goto bail;
- }
- break;
- case CIOCKEY:
- case CIOCKEY2:
- dprintk("%s(CIOCKEY)\n", __FUNCTION__);
- if (!crypto_userasymcrypto)
- return (EPERM); /* XXX compat? */
- if(copy_from_user(&kop, (void*)arg, sizeof(kop))) {
- dprintk("%s(CIOCKEY) - bad copy\n", __FUNCTION__);
- error = EFAULT;
- goto bail;
- }
- if (cmd == CIOCKEY) {
- /* NB: crypto core enforces s/w driver use */
- kop.crk_crid =
- CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
- }
- error = cryptodev_key(&kop);
- if(copy_to_user((void*)arg, &kop, sizeof(kop))) {
- dprintk("%s(CIOCGKEY) - bad return copy\n", __FUNCTION__);
- error = EFAULT;
- goto bail;
- }
- break;
- case CIOCASYMFEAT:
- dprintk("%s(CIOCASYMFEAT)\n", __FUNCTION__);
- if (!crypto_userasymcrypto) {
- /*
- * NB: if user asym crypto operations are
- * not permitted return "no algorithms"
- * so well-behaved applications will just
- * fallback to doing them in software.
- */
- feat = 0;
- } else
- error = crypto_getfeat(&feat);
- if (!error) {
- error = copy_to_user((void*)arg, &feat, sizeof(feat));
- }
- break;
- case CIOCFINDDEV:
- if (copy_from_user(&fop, (void*)arg, sizeof(fop))) {
- dprintk("%s(CIOCFINDDEV) - bad copy\n", __FUNCTION__);
- error = EFAULT;
- goto bail;
- }
- error = cryptodev_find(&fop);
- if (copy_to_user((void*)arg, &fop, sizeof(fop))) {
- dprintk("%s(CIOCFINDDEV) - bad return copy\n", __FUNCTION__);
- error = EFAULT;
- goto bail;
- }
- break;
- default:
- dprintk("%s(unknown ioctl 0x%x)\n", __FUNCTION__, cmd);
- error = EINVAL;
- break;
- }
- return(-error);
- }
- #ifdef HAVE_UNLOCKED_IOCTL
- static long
- cryptodev_unlocked_ioctl(
- struct file *filp,
- unsigned int cmd,
- unsigned long arg)
- {
- return cryptodev_ioctl(NULL, filp, cmd, arg);
- }
- #endif
- static int
- cryptodev_open(struct inode *inode, struct file *filp)
- {
- struct fcrypt *fcr;
- dprintk("%s()\n", __FUNCTION__);
- #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
- /*
- * on 2.6.35 private_data points to a miscdevice structure, we override
- * it, which is currently safe to do.
- */
- if (filp->private_data) {
- printk("cryptodev: Private data already exists - %p!\n", filp->private_data);
- return(-ENODEV);
- }
- #endif
- fcr = kmalloc(sizeof(*fcr), GFP_KERNEL);
- if (!fcr) {
- dprintk("%s() - malloc failed\n", __FUNCTION__);
- return(-ENOMEM);
- }
- memset(fcr, 0, sizeof(*fcr));
- INIT_LIST_HEAD(&fcr->csessions);
- filp->private_data = fcr;
- return(0);
- }
- static int
- cryptodev_release(struct inode *inode, struct file *filp)
- {
- struct fcrypt *fcr = filp->private_data;
- struct csession *cse, *tmp;
- dprintk("%s()\n", __FUNCTION__);
- if (!filp) {
- printk("cryptodev: No private data on release\n");
- return(0);
- }
- list_for_each_entry_safe(cse, tmp, &fcr->csessions, list) {
- list_del(&cse->list);
- (void)csefree(cse);
- }
- filp->private_data = NULL;
- kfree(fcr);
- return(0);
- }
- static struct file_operations cryptodev_fops = {
- .owner = THIS_MODULE,
- .open = cryptodev_open,
- .release = cryptodev_release,
- #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
- .ioctl = cryptodev_ioctl,
- #endif
- #ifdef HAVE_UNLOCKED_IOCTL
- .unlocked_ioctl = cryptodev_unlocked_ioctl,
- #endif
- };
- static struct miscdevice cryptodev = {
- .minor = CRYPTODEV_MINOR,
- .name = "crypto",
- .fops = &cryptodev_fops,
- };
- static int __init
- cryptodev_init(void)
- {
- int rc;
- dprintk("%s(%p)\n", __FUNCTION__, cryptodev_init);
- rc = misc_register(&cryptodev);
- if (rc) {
- printk(KERN_ERR "cryptodev: registration of /dev/crypto failed\n");
- return(rc);
- }
- return(0);
- }
- static void __exit
- cryptodev_exit(void)
- {
- dprintk("%s()\n", __FUNCTION__);
- misc_deregister(&cryptodev);
- }
- module_init(cryptodev_init);
- module_exit(cryptodev_exit);
- MODULE_LICENSE("Dual BSD/GPL");
- MODULE_AUTHOR("David McCullough <david_mccullough@mcafee.com>");
- MODULE_DESCRIPTION("Cryptodev (user interface to OCF)");
|