2010-01-31 17:32:56 +03:00
|
|
|
/* $NetBSD: cryptodev.c,v 1.52 2010/01/31 14:32:56 hubertf Exp $ */
|
2003-08-21 20:08:05 +04:00
|
|
|
/* $FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $ */
|
2003-07-26 01:12:39 +04:00
|
|
|
/* $OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $ */
|
|
|
|
|
2008-04-11 02:48:42 +04:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2008 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Coyote Point Systems, Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``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 FOUNDATION OR CONTRIBUTORS
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2003-07-26 01:12:39 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2001 Theo de Raadt
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2010-01-31 17:32:56 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.52 2010/01/31 14:32:56 hubertf Exp $");
|
2003-07-26 01:12:39 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2008-04-11 02:48:42 +04:00
|
|
|
#include <sys/kmem.h>
|
2003-07-26 01:12:39 +04:00
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
2008-01-26 03:04:27 +03:00
|
|
|
#include <sys/pool.h>
|
2003-07-26 01:12:39 +04:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/filedesc.h>
|
|
|
|
#include <sys/errno.h>
|
2003-07-28 23:37:03 +04:00
|
|
|
#include <sys/md5.h>
|
2003-07-26 01:12:39 +04:00
|
|
|
#include <sys/sha1.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/device.h>
|
2006-05-15 01:31:52 +04:00
|
|
|
#include <sys/kauth.h>
|
2008-04-11 02:48:42 +04:00
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/poll.h>
|
2008-04-11 10:25:35 +04:00
|
|
|
#include <sys/atomic.h>
|
2009-04-11 19:47:33 +04:00
|
|
|
#include <sys/stat.h>
|
2003-07-26 01:12:39 +04:00
|
|
|
|
2008-02-04 03:35:34 +03:00
|
|
|
#include "opt_ocf.h"
|
2003-07-26 01:12:39 +04:00
|
|
|
#include <opencrypto/cryptodev.h>
|
2009-03-25 04:26:12 +03:00
|
|
|
#include <opencrypto/ocryptodev.h>
|
2003-07-26 01:12:39 +04:00
|
|
|
#include <opencrypto/xform.h>
|
|
|
|
|
|
|
|
struct csession {
|
|
|
|
TAILQ_ENTRY(csession) next;
|
|
|
|
u_int64_t sid;
|
|
|
|
u_int32_t ses;
|
|
|
|
|
2009-03-25 04:26:12 +03:00
|
|
|
u_int32_t cipher; /* note: shares name space in crd_alg */
|
2003-07-26 01:12:39 +04:00
|
|
|
struct enc_xform *txform;
|
2009-03-25 04:26:12 +03:00
|
|
|
u_int32_t mac; /* note: shares name space in crd_alg */
|
2003-07-26 01:12:39 +04:00
|
|
|
struct auth_hash *thash;
|
2009-03-25 04:26:12 +03:00
|
|
|
u_int32_t comp_alg; /* note: shares name space in crd_alg */
|
|
|
|
struct comp_algo *tcomp;
|
2003-07-26 01:12:39 +04:00
|
|
|
|
2007-03-04 08:59:00 +03:00
|
|
|
void * key;
|
2003-07-26 01:12:39 +04:00
|
|
|
int keylen;
|
|
|
|
u_char tmp_iv[EALG_MAX_BLOCK_LEN];
|
|
|
|
|
2007-03-04 08:59:00 +03:00
|
|
|
void * mackey;
|
2003-07-26 01:12:39 +04:00
|
|
|
int mackeylen;
|
|
|
|
u_char tmp_mac[CRYPTO_MAX_MAC_LEN];
|
|
|
|
|
2008-01-26 03:04:27 +03:00
|
|
|
struct iovec iovec[1]; /* user requests never have more */
|
2003-07-26 01:12:39 +04:00
|
|
|
struct uio uio;
|
|
|
|
int error;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct fcrypt {
|
|
|
|
TAILQ_HEAD(csessionlist, csession) csessions;
|
2008-04-11 02:48:42 +04:00
|
|
|
TAILQ_HEAD(crprethead, cryptop) crp_ret_mq;
|
|
|
|
TAILQ_HEAD(krprethead, cryptkop) crp_ret_mkq;
|
2003-07-26 01:12:39 +04:00
|
|
|
int sesn;
|
2008-04-11 02:48:42 +04:00
|
|
|
struct selinfo sinfo;
|
|
|
|
u_int32_t requestid;
|
2009-04-11 19:47:33 +04:00
|
|
|
struct timespec atime;
|
|
|
|
struct timespec mtime;
|
|
|
|
struct timespec btime;
|
2003-07-26 01:12:39 +04:00
|
|
|
};
|
|
|
|
|
2008-01-26 03:04:27 +03:00
|
|
|
/* For our fixed-size allocations */
|
2008-04-11 14:28:10 +04:00
|
|
|
static struct pool fcrpl;
|
|
|
|
static struct pool csepl;
|
2003-07-26 01:12:39 +04:00
|
|
|
|
|
|
|
/* Declaration of master device (fd-cloning/ctxt-allocating) entrypoints */
|
2005-12-11 15:16:03 +03:00
|
|
|
static int cryptoopen(dev_t dev, int flag, int mode, struct lwp *l);
|
2003-07-26 01:12:39 +04:00
|
|
|
static int cryptoread(dev_t dev, struct uio *uio, int ioflag);
|
|
|
|
static int cryptowrite(dev_t dev, struct uio *uio, int ioflag);
|
2005-12-11 15:16:03 +03:00
|
|
|
static int cryptoselect(dev_t dev, int rw, struct lwp *l);
|
2003-07-26 01:12:39 +04:00
|
|
|
|
|
|
|
/* Declaration of cloned-device (per-ctxt) entrypoints */
|
2008-04-11 02:48:42 +04:00
|
|
|
static int cryptof_read(struct file *, off_t *, struct uio *,
|
2008-05-24 20:28:58 +04:00
|
|
|
kauth_cred_t, int);
|
2008-04-11 02:48:42 +04:00
|
|
|
static int cryptof_write(struct file *, off_t *, struct uio *,
|
2008-05-24 20:28:58 +04:00
|
|
|
kauth_cred_t, int);
|
2008-04-11 02:48:42 +04:00
|
|
|
static int cryptof_ioctl(struct file *, u_long, void *);
|
|
|
|
static int cryptof_close(struct file *);
|
|
|
|
static int cryptof_poll(struct file *, int);
|
2009-04-11 19:47:33 +04:00
|
|
|
static int cryptof_stat(struct file *, struct stat *);
|
2003-07-26 01:12:39 +04:00
|
|
|
|
2004-11-30 07:25:43 +03:00
|
|
|
static const struct fileops cryptofops = {
|
2009-04-04 14:12:51 +04:00
|
|
|
.fo_read = cryptof_read,
|
|
|
|
.fo_write = cryptof_write,
|
|
|
|
.fo_ioctl = cryptof_ioctl,
|
|
|
|
.fo_fcntl = fnullop_fcntl,
|
|
|
|
.fo_poll = cryptof_poll,
|
2009-04-11 19:47:33 +04:00
|
|
|
.fo_stat = cryptof_stat,
|
2009-04-04 14:12:51 +04:00
|
|
|
.fo_close = cryptof_close,
|
|
|
|
.fo_kqfilter = fnullop_kqfilter,
|
2009-12-20 12:36:05 +03:00
|
|
|
.fo_restart = fnullop_restart,
|
2003-07-26 01:12:39 +04:00
|
|
|
};
|
|
|
|
|
2009-03-25 04:26:12 +03:00
|
|
|
struct csession *cryptodev_csefind(struct fcrypt *, u_int);
|
2003-07-26 01:12:39 +04:00
|
|
|
static struct csession *csefind(struct fcrypt *, u_int);
|
|
|
|
static int csedelete(struct fcrypt *, struct csession *);
|
|
|
|
static struct csession *cseadd(struct fcrypt *, struct csession *);
|
2008-05-24 20:28:58 +04:00
|
|
|
static struct csession *csecreate(struct fcrypt *, u_int64_t, void *,
|
2009-03-25 04:26:12 +03:00
|
|
|
u_int64_t, void *, u_int64_t, u_int32_t, u_int32_t, u_int32_t,
|
|
|
|
struct enc_xform *, struct auth_hash *, struct comp_algo *);
|
2003-07-26 01:12:39 +04:00
|
|
|
static int csefree(struct csession *);
|
|
|
|
|
|
|
|
static int cryptodev_key(struct crypt_kop *);
|
2008-04-11 02:48:42 +04:00
|
|
|
static int cryptodev_mkey(struct fcrypt *, struct crypt_n_kop *, int);
|
|
|
|
static int cryptodev_msessionfin(struct fcrypt *, int, u_int32_t *);
|
2003-07-26 01:12:39 +04:00
|
|
|
|
|
|
|
static int cryptodev_cb(void *);
|
|
|
|
static int cryptodevkey_cb(void *);
|
|
|
|
|
2008-04-11 02:48:42 +04:00
|
|
|
static int cryptodev_mcb(void *);
|
|
|
|
static int cryptodevkey_mcb(void *);
|
|
|
|
|
|
|
|
static int cryptodev_getmstatus(struct fcrypt *, struct crypt_result *,
|
2008-05-24 20:28:58 +04:00
|
|
|
int);
|
2008-04-11 02:48:42 +04:00
|
|
|
static int cryptodev_getstatus(struct fcrypt *, struct crypt_result *);
|
|
|
|
|
2009-03-25 04:26:12 +03:00
|
|
|
extern int ocryptof_ioctl(struct file *, u_long, void *);
|
|
|
|
|
Clean up userlevel access to software kernel transforms, in preparation
for using /dev/crypto for OpenSSL:
1. Add comments explaining crypto_devallowsoft, explaining the
OpenBSD-style three-way logic actully implemented in crypto_newsession().
2. Pass crypto_devallowsoft as the final argument to crypto_newsession(),
instead of a constant 0 value.
3. Set the default value of crypto_devallowsoft to 1, to allow
/dev/crypto access only for hardware-supported transforms.
Items 1-3 may be revised to match the FreeBSD two-way logic, if the
consensus is that there's no point to forcing software transforms.
But as a first step, let the description match what the code actually does.
GC unused variables usercrypto, userasmcrypto, cryptodevallowsoft from
cryptodev.c, in favour of variables crypto_usercrypto, crypto_userasmcrypto,
crypto_devallowsoft, which are used as well as defined in crypto.c.
2003-11-19 06:18:33 +03:00
|
|
|
/*
|
|
|
|
* sysctl-able control variables for /dev/crypto now defined in crypto.c:
|
|
|
|
* crypto_usercrypto, crypto_userasmcrypto, crypto_devallowsoft.
|
|
|
|
*/
|
2003-07-26 01:12:39 +04:00
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
int
|
2008-03-22 00:54:58 +03:00
|
|
|
cryptof_read(file_t *fp, off_t *poff,
|
2006-11-16 04:32:37 +03:00
|
|
|
struct uio *uio, kauth_cred_t cred, int flags)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
2008-05-24 20:28:58 +04:00
|
|
|
return EIO;
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
int
|
2008-03-22 00:54:58 +03:00
|
|
|
cryptof_write(file_t *fp, off_t *poff,
|
2006-11-16 04:32:37 +03:00
|
|
|
struct uio *uio, kauth_cred_t cred, int flags)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
2008-05-24 20:28:58 +04:00
|
|
|
return EIO;
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
int
|
2008-04-11 02:48:42 +04:00
|
|
|
cryptof_ioctl(struct file *fp, u_long cmd, void *data)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
2008-03-22 00:54:58 +03:00
|
|
|
struct fcrypt *fcr = fp->f_data;
|
2003-07-26 01:12:39 +04:00
|
|
|
struct csession *cse;
|
|
|
|
struct session_op *sop;
|
2008-04-11 02:48:42 +04:00
|
|
|
struct session_n_op *snop;
|
2003-07-26 01:12:39 +04:00
|
|
|
struct crypt_op *cop;
|
2008-04-11 02:48:42 +04:00
|
|
|
struct crypt_mop *mop;
|
|
|
|
struct crypt_mkop *mkop;
|
|
|
|
struct crypt_n_op *cnop;
|
|
|
|
struct crypt_n_kop *knop;
|
|
|
|
struct crypt_sgop *sgop;
|
|
|
|
struct crypt_sfop *sfop;
|
|
|
|
struct cryptret *crypt_ret;
|
|
|
|
struct crypt_result *crypt_res;
|
2003-07-26 01:12:39 +04:00
|
|
|
u_int32_t ses;
|
2008-04-11 02:48:42 +04:00
|
|
|
u_int32_t *sesid;
|
2003-07-26 01:12:39 +04:00
|
|
|
int error = 0;
|
2008-04-11 02:48:42 +04:00
|
|
|
size_t count;
|
2003-07-26 01:12:39 +04:00
|
|
|
|
2008-01-26 03:04:27 +03:00
|
|
|
/* backwards compatibility */
|
2008-03-22 00:54:58 +03:00
|
|
|
file_t *criofp;
|
2008-01-26 03:04:27 +03:00
|
|
|
struct fcrypt *criofcr;
|
|
|
|
int criofd;
|
|
|
|
|
2009-04-12 03:05:26 +04:00
|
|
|
mutex_spin_enter(&crypto_mtx);
|
2009-04-11 19:47:33 +04:00
|
|
|
getnanotime(&fcr->atime);
|
2009-04-12 03:05:26 +04:00
|
|
|
mutex_spin_exit(&crypto_mtx);
|
2009-04-11 19:47:33 +04:00
|
|
|
|
2008-04-11 02:48:42 +04:00
|
|
|
switch (cmd) {
|
2008-01-26 03:04:27 +03:00
|
|
|
case CRIOGET: /* XXX deprecated, remove after 5.0 */
|
2008-04-11 02:48:42 +04:00
|
|
|
if ((error = fd_allocfile(&criofp, &criofd)) != 0)
|
|
|
|
return error;
|
|
|
|
criofcr = pool_get(&fcrpl, PR_WAITOK);
|
2008-02-04 03:35:34 +03:00
|
|
|
mutex_spin_enter(&crypto_mtx);
|
2008-04-11 02:48:42 +04:00
|
|
|
TAILQ_INIT(&criofcr->csessions);
|
|
|
|
TAILQ_INIT(&criofcr->crp_ret_mq);
|
|
|
|
TAILQ_INIT(&criofcr->crp_ret_mkq);
|
|
|
|
selinit(&criofcr->sinfo);
|
|
|
|
|
2008-01-26 03:04:27 +03:00
|
|
|
/*
|
|
|
|
* Don't ever return session 0, to allow detection of
|
|
|
|
* failed creation attempts with multi-create ioctl.
|
|
|
|
*/
|
2008-04-11 02:48:42 +04:00
|
|
|
criofcr->sesn = 1;
|
|
|
|
criofcr->requestid = 1;
|
2008-02-04 03:35:34 +03:00
|
|
|
mutex_spin_exit(&crypto_mtx);
|
2008-04-11 02:48:42 +04:00
|
|
|
(void)fd_clone(criofp, criofd, (FREAD|FWRITE),
|
2008-01-26 03:04:27 +03:00
|
|
|
&cryptofops, criofcr);
|
2008-04-11 02:48:42 +04:00
|
|
|
*(u_int32_t *)data = criofd;
|
2008-01-26 03:04:27 +03:00
|
|
|
return error;
|
2008-04-11 02:48:42 +04:00
|
|
|
break;
|
2003-07-26 01:12:39 +04:00
|
|
|
case CIOCGSESSION:
|
|
|
|
sop = (struct session_op *)data;
|
2008-04-11 02:48:42 +04:00
|
|
|
error = cryptodev_session(fcr, sop);
|
|
|
|
break;
|
|
|
|
case CIOCNGSESSION:
|
|
|
|
sgop = (struct crypt_sgop *)data;
|
|
|
|
snop = kmem_alloc((sgop->count *
|
|
|
|
sizeof(struct session_n_op)), KM_SLEEP);
|
|
|
|
error = copyin(sgop->sessions, snop, sgop->count *
|
|
|
|
sizeof(struct session_n_op));
|
|
|
|
if (error) {
|
|
|
|
goto mbail;
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
2009-04-12 03:05:26 +04:00
|
|
|
mutex_spin_enter(&crypto_mtx);
|
2009-04-11 19:47:33 +04:00
|
|
|
fcr->mtime = fcr->atime;
|
2009-04-12 03:05:26 +04:00
|
|
|
mutex_spin_exit(&crypto_mtx);
|
2008-04-11 02:48:42 +04:00
|
|
|
error = cryptodev_msession(fcr, snop, sgop->count);
|
|
|
|
if (error) {
|
|
|
|
goto mbail;
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
2008-04-11 02:48:42 +04:00
|
|
|
error = copyout(snop, sgop->sessions, sgop->count *
|
2008-05-24 20:28:58 +04:00
|
|
|
sizeof(struct session_n_op));
|
2008-04-11 02:48:42 +04:00
|
|
|
mbail:
|
2008-04-21 23:05:41 +04:00
|
|
|
kmem_free(snop, sgop->count * sizeof(struct session_n_op));
|
2003-07-26 01:12:39 +04:00
|
|
|
break;
|
|
|
|
case CIOCFSESSION:
|
2008-02-04 03:35:34 +03:00
|
|
|
mutex_spin_enter(&crypto_mtx);
|
2009-04-12 03:05:26 +04:00
|
|
|
fcr->mtime = fcr->atime;
|
2003-07-26 01:12:39 +04:00
|
|
|
ses = *(u_int32_t *)data;
|
|
|
|
cse = csefind(fcr, ses);
|
|
|
|
if (cse == NULL)
|
2008-05-24 20:28:58 +04:00
|
|
|
return EINVAL;
|
2003-07-26 01:12:39 +04:00
|
|
|
csedelete(fcr, cse);
|
|
|
|
error = csefree(cse);
|
2008-02-04 03:35:34 +03:00
|
|
|
mutex_spin_exit(&crypto_mtx);
|
2003-07-26 01:12:39 +04:00
|
|
|
break;
|
2008-04-11 02:48:42 +04:00
|
|
|
case CIOCNFSESSION:
|
2009-04-12 03:05:26 +04:00
|
|
|
mutex_spin_enter(&crypto_mtx);
|
2009-04-11 19:47:33 +04:00
|
|
|
fcr->mtime = fcr->atime;
|
2009-04-12 03:05:26 +04:00
|
|
|
mutex_spin_exit(&crypto_mtx);
|
2008-04-11 02:48:42 +04:00
|
|
|
sfop = (struct crypt_sfop *)data;
|
|
|
|
sesid = kmem_alloc((sfop->count * sizeof(u_int32_t)),
|
2008-05-24 20:28:58 +04:00
|
|
|
KM_SLEEP);
|
|
|
|
error = copyin(sfop->sesid, sesid,
|
|
|
|
(sfop->count * sizeof(u_int32_t)));
|
2008-04-11 02:48:42 +04:00
|
|
|
if (!error) {
|
|
|
|
error = cryptodev_msessionfin(fcr, sfop->count, sesid);
|
|
|
|
}
|
|
|
|
kmem_free(sesid, (sfop->count * sizeof(u_int32_t)));
|
|
|
|
break;
|
2003-07-26 01:12:39 +04:00
|
|
|
case CIOCCRYPT:
|
2008-02-04 03:35:34 +03:00
|
|
|
mutex_spin_enter(&crypto_mtx);
|
2009-04-12 03:05:26 +04:00
|
|
|
fcr->mtime = fcr->atime;
|
2003-07-26 01:12:39 +04:00
|
|
|
cop = (struct crypt_op *)data;
|
|
|
|
cse = csefind(fcr, cop->ses);
|
2008-02-04 03:35:34 +03:00
|
|
|
mutex_spin_exit(&crypto_mtx);
|
2006-03-06 03:50:44 +03:00
|
|
|
if (cse == NULL) {
|
|
|
|
DPRINTF(("csefind failed\n"));
|
2008-05-24 20:28:58 +04:00
|
|
|
return EINVAL;
|
2006-03-06 03:50:44 +03:00
|
|
|
}
|
2008-03-22 00:54:58 +03:00
|
|
|
error = cryptodev_op(cse, cop, curlwp);
|
2008-02-04 17:46:26 +03:00
|
|
|
DPRINTF(("cryptodev_op error = %d\n", error));
|
2003-07-26 01:12:39 +04:00
|
|
|
break;
|
2008-04-11 02:48:42 +04:00
|
|
|
case CIOCNCRYPTM:
|
2009-04-12 03:05:26 +04:00
|
|
|
mutex_spin_enter(&crypto_mtx);
|
2009-04-11 19:47:33 +04:00
|
|
|
fcr->mtime = fcr->atime;
|
2009-04-12 03:05:26 +04:00
|
|
|
mutex_spin_exit(&crypto_mtx);
|
2008-04-11 02:48:42 +04:00
|
|
|
mop = (struct crypt_mop *)data;
|
|
|
|
cnop = kmem_alloc((mop->count * sizeof(struct crypt_n_op)),
|
2008-05-24 20:28:58 +04:00
|
|
|
KM_SLEEP);
|
|
|
|
error = copyin(mop->reqs, cnop,
|
|
|
|
(mop->count * sizeof(struct crypt_n_op)));
|
2008-04-11 02:48:42 +04:00
|
|
|
if(!error) {
|
2008-05-24 20:28:58 +04:00
|
|
|
error = cryptodev_mop(fcr, cnop, mop->count, curlwp);
|
2008-04-11 02:48:42 +04:00
|
|
|
if (!error) {
|
|
|
|
error = copyout(cnop, mop->reqs,
|
2008-05-24 20:28:58 +04:00
|
|
|
(mop->count * sizeof(struct crypt_n_op)));
|
2008-04-11 02:48:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
kmem_free(cnop, (mop->count * sizeof(struct crypt_n_op)));
|
|
|
|
break;
|
2003-07-26 01:12:39 +04:00
|
|
|
case CIOCKEY:
|
|
|
|
error = cryptodev_key((struct crypt_kop *)data);
|
2008-02-04 17:46:26 +03:00
|
|
|
DPRINTF(("cryptodev_key error = %d\n", error));
|
2003-07-26 01:12:39 +04:00
|
|
|
break;
|
2008-04-11 02:48:42 +04:00
|
|
|
case CIOCNFKEYM:
|
2009-04-12 03:05:26 +04:00
|
|
|
mutex_spin_enter(&crypto_mtx);
|
2009-04-11 19:47:33 +04:00
|
|
|
fcr->mtime = fcr->atime;
|
2009-04-12 03:05:26 +04:00
|
|
|
mutex_spin_exit(&crypto_mtx);
|
2008-04-11 02:48:42 +04:00
|
|
|
mkop = (struct crypt_mkop *)data;
|
|
|
|
knop = kmem_alloc((mkop->count * sizeof(struct crypt_n_kop)),
|
2008-05-24 20:28:58 +04:00
|
|
|
KM_SLEEP);
|
2008-04-11 02:48:42 +04:00
|
|
|
error = copyin(mkop->reqs, knop,
|
2008-05-24 20:28:58 +04:00
|
|
|
(mkop->count * sizeof(struct crypt_n_kop)));
|
2008-04-11 02:48:42 +04:00
|
|
|
if (!error) {
|
|
|
|
error = cryptodev_mkey(fcr, knop, mkop->count);
|
|
|
|
if (!error)
|
|
|
|
error = copyout(knop, mkop->reqs,
|
2008-05-24 20:28:58 +04:00
|
|
|
(mkop->count * sizeof(struct crypt_n_kop)));
|
2008-04-11 02:48:42 +04:00
|
|
|
}
|
|
|
|
kmem_free(knop, (mkop->count * sizeof(struct crypt_n_kop)));
|
|
|
|
break;
|
2003-07-26 01:12:39 +04:00
|
|
|
case CIOCASYMFEAT:
|
|
|
|
error = crypto_getfeat((int *)data);
|
|
|
|
break;
|
2008-04-11 02:48:42 +04:00
|
|
|
case CIOCNCRYPTRETM:
|
2009-04-12 03:05:26 +04:00
|
|
|
mutex_spin_enter(&crypto_mtx);
|
2009-04-11 19:47:33 +04:00
|
|
|
fcr->mtime = fcr->atime;
|
2009-04-12 03:05:26 +04:00
|
|
|
mutex_spin_exit(&crypto_mtx);
|
2008-04-11 02:48:42 +04:00
|
|
|
crypt_ret = (struct cryptret *)data;
|
|
|
|
count = crypt_ret->count;
|
2008-05-24 20:28:58 +04:00
|
|
|
crypt_res = kmem_alloc((count * sizeof(struct crypt_result)),
|
|
|
|
KM_SLEEP);
|
2008-04-11 02:48:42 +04:00
|
|
|
error = copyin(crypt_ret->results, crypt_res,
|
2008-05-24 20:28:58 +04:00
|
|
|
(count * sizeof(struct crypt_result)));
|
2008-04-11 02:48:42 +04:00
|
|
|
if (error)
|
|
|
|
goto reterr;
|
|
|
|
crypt_ret->count = cryptodev_getmstatus(fcr, crypt_res,
|
2008-05-24 20:28:58 +04:00
|
|
|
crypt_ret->count);
|
2008-04-11 02:48:42 +04:00
|
|
|
/* sanity check count */
|
|
|
|
if (crypt_ret->count > count) {
|
2008-04-11 10:25:35 +04:00
|
|
|
printf("%s.%d: error returned count %zd > original "
|
2008-05-24 20:28:58 +04:00
|
|
|
" count %zd\n",
|
|
|
|
__FILE__, __LINE__, crypt_ret->count, count);
|
2008-04-11 02:48:42 +04:00
|
|
|
crypt_ret->count = count;
|
|
|
|
|
|
|
|
}
|
|
|
|
error = copyout(crypt_res, crypt_ret->results,
|
2008-05-24 20:28:58 +04:00
|
|
|
(crypt_ret->count * sizeof(struct crypt_result)));
|
2008-04-11 02:48:42 +04:00
|
|
|
reterr:
|
2008-05-24 20:28:58 +04:00
|
|
|
kmem_free(crypt_res, (count * sizeof(struct crypt_result)));
|
2008-04-11 02:48:42 +04:00
|
|
|
break;
|
|
|
|
case CIOCNCRYPTRET:
|
|
|
|
error = cryptodev_getstatus(fcr, (struct crypt_result *)data);
|
|
|
|
break;
|
2003-07-26 01:12:39 +04:00
|
|
|
default:
|
2009-03-25 04:26:12 +03:00
|
|
|
/* Check for backward compatible commands */
|
|
|
|
error = ocryptof_ioctl(fp, cmd, data);
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
2008-05-24 20:28:58 +04:00
|
|
|
return error;
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
2009-03-25 04:26:12 +03:00
|
|
|
int
|
2006-11-16 04:32:37 +03:00
|
|
|
cryptodev_op(struct csession *cse, struct crypt_op *cop, struct lwp *l)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
|
|
|
struct cryptop *crp = NULL;
|
2009-03-25 04:26:12 +03:00
|
|
|
struct cryptodesc *crde = NULL, *crda = NULL, *crdc = NULL;
|
2008-02-04 03:35:34 +03:00
|
|
|
int error;
|
2009-03-25 04:26:12 +03:00
|
|
|
int iov_len = cop->len;
|
|
|
|
int flags=0;
|
|
|
|
int dst_len; /* copyout size */
|
2003-07-26 01:12:39 +04:00
|
|
|
|
|
|
|
if (cop->len > 256*1024-4)
|
2008-05-24 20:28:58 +04:00
|
|
|
return E2BIG;
|
2003-07-26 01:12:39 +04:00
|
|
|
|
2005-08-23 03:06:34 +04:00
|
|
|
if (cse->txform) {
|
|
|
|
if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
|
2008-05-24 20:28:58 +04:00
|
|
|
return EINVAL;
|
2005-08-23 03:06:34 +04:00
|
|
|
}
|
2003-07-26 01:12:39 +04:00
|
|
|
|
2009-03-25 04:26:12 +03:00
|
|
|
DPRINTF(("cryptodev_op[%d]: iov_len %d\n", (uint32_t)cse->sid, iov_len));
|
|
|
|
if ((cse->tcomp) && cop->dst_len) {
|
|
|
|
if (iov_len < cop->dst_len) {
|
|
|
|
/* Need larger iov to deal with decompress */
|
|
|
|
iov_len = cop->dst_len;
|
|
|
|
}
|
|
|
|
DPRINTF(("cryptodev_op: iov_len -> %d for decompress\n", iov_len));
|
|
|
|
}
|
|
|
|
|
2008-05-24 20:28:58 +04:00
|
|
|
(void)memset(&cse->uio, 0, sizeof(cse->uio));
|
2003-07-26 01:12:39 +04:00
|
|
|
cse->uio.uio_iovcnt = 1;
|
|
|
|
cse->uio.uio_resid = 0;
|
|
|
|
cse->uio.uio_rw = UIO_WRITE;
|
|
|
|
cse->uio.uio_iov = cse->iovec;
|
2006-03-01 15:38:10 +03:00
|
|
|
UIO_SETUP_SYSSPACE(&cse->uio);
|
2008-01-26 03:04:27 +03:00
|
|
|
memset(&cse->iovec, 0, sizeof(cse->iovec));
|
2009-03-25 04:26:12 +03:00
|
|
|
|
|
|
|
/* the iov needs to be big enough to handle the uncompressed
|
|
|
|
* data.... */
|
|
|
|
cse->uio.uio_iov[0].iov_len = iov_len;
|
|
|
|
cse->uio.uio_iov[0].iov_base = kmem_alloc(iov_len, KM_SLEEP);
|
2008-01-26 03:04:27 +03:00
|
|
|
cse->uio.uio_resid = cse->uio.uio_iov[0].iov_len;
|
2009-03-25 04:26:12 +03:00
|
|
|
DPRINTF(("cryptodev_op[%d]: uio.iov_base %p malloced %d bytes\n",
|
|
|
|
(uint32_t)cse->sid, cse->uio.uio_iov[0].iov_base, iov_len));
|
2003-07-26 01:12:39 +04:00
|
|
|
|
2009-03-25 04:26:12 +03:00
|
|
|
crp = crypto_getreq((cse->tcomp != NULL) + (cse->txform != NULL) + (cse->thash != NULL));
|
2003-07-26 01:12:39 +04:00
|
|
|
if (crp == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
goto bail;
|
|
|
|
}
|
2009-03-25 04:26:12 +03:00
|
|
|
DPRINTF(("cryptodev_op[%d]: crp %p\n", (uint32_t)cse->sid, crp));
|
|
|
|
|
|
|
|
/* crds are always ordered tcomp, thash, then txform */
|
|
|
|
/* with optional missing links */
|
|
|
|
|
|
|
|
/* XXX: If we're going to compress then hash or encrypt, we need
|
|
|
|
* to be able to pass on the new size of the data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (cse->tcomp) {
|
|
|
|
crdc = crp->crp_desc;
|
|
|
|
}
|
2003-07-26 01:12:39 +04:00
|
|
|
|
|
|
|
if (cse->thash) {
|
2009-03-25 04:26:12 +03:00
|
|
|
crda = crdc ? crdc->crd_next : crp->crp_desc;
|
2008-05-24 20:42:00 +04:00
|
|
|
if (cse->txform && crda)
|
2003-07-26 01:12:39 +04:00
|
|
|
crde = crda->crd_next;
|
|
|
|
} else {
|
2009-03-25 04:26:12 +03:00
|
|
|
if (cse->txform) {
|
|
|
|
crde = crdc ? crdc->crd_next : crp->crp_desc;
|
|
|
|
} else if (!cse->tcomp) {
|
2003-07-26 01:12:39 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-25 04:26:12 +03:00
|
|
|
DPRINTF(("ocf[%d]: iov_len %d, cop->len %d\n",
|
|
|
|
(uint32_t)cse->sid,
|
|
|
|
cse->uio.uio_iov[0].iov_len,
|
|
|
|
cop->len));
|
|
|
|
|
2003-07-26 01:12:39 +04:00
|
|
|
if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
|
2008-04-11 02:48:42 +04:00
|
|
|
{
|
|
|
|
printf("copyin failed %s %d \n", (char *)cop->src, error);
|
2003-07-26 01:12:39 +04:00
|
|
|
goto bail;
|
2008-04-11 02:48:42 +04:00
|
|
|
}
|
2003-07-26 01:12:39 +04:00
|
|
|
|
2009-03-25 04:26:12 +03:00
|
|
|
if (crdc) {
|
|
|
|
switch (cop->op) {
|
|
|
|
case COP_COMP:
|
|
|
|
crdc->crd_flags |= CRD_F_COMP;
|
|
|
|
break;
|
|
|
|
case COP_DECOMP:
|
|
|
|
crdc->crd_flags &= ~CRD_F_COMP;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* more data to follow? */
|
|
|
|
if (cop->flags & COP_F_MORE) {
|
|
|
|
flags |= CRYPTO_F_MORE;
|
|
|
|
}
|
|
|
|
crdc->crd_len = cop->len;
|
|
|
|
crdc->crd_inject = 0;
|
|
|
|
|
|
|
|
crdc->crd_alg = cse->comp_alg;
|
|
|
|
crdc->crd_key = NULL;
|
|
|
|
crdc->crd_klen = 0;
|
|
|
|
DPRINTF(("cryptodev_op[%d]: crdc setup for comp_alg %d.\n",
|
|
|
|
(uint32_t)cse->sid, crdc->crd_alg));
|
|
|
|
}
|
|
|
|
|
2003-07-26 01:12:39 +04:00
|
|
|
if (crda) {
|
|
|
|
crda->crd_skip = 0;
|
|
|
|
crda->crd_len = cop->len;
|
|
|
|
crda->crd_inject = 0; /* ??? */
|
|
|
|
|
|
|
|
crda->crd_alg = cse->mac;
|
|
|
|
crda->crd_key = cse->mackey;
|
|
|
|
crda->crd_klen = cse->mackeylen * 8;
|
2009-03-25 04:26:12 +03:00
|
|
|
DPRINTF(("cryptodev_op: crda setup for mac %d.\n", crda->crd_alg));
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (crde) {
|
2009-03-25 04:26:12 +03:00
|
|
|
switch (cop->op) {
|
|
|
|
case COP_ENCRYPT:
|
2003-07-26 01:12:39 +04:00
|
|
|
crde->crd_flags |= CRD_F_ENCRYPT;
|
2009-03-25 04:26:12 +03:00
|
|
|
break;
|
|
|
|
case COP_DECRYPT:
|
2003-07-26 01:12:39 +04:00
|
|
|
crde->crd_flags &= ~CRD_F_ENCRYPT;
|
2009-03-25 04:26:12 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2003-07-26 01:12:39 +04:00
|
|
|
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;
|
2009-03-25 04:26:12 +03:00
|
|
|
DPRINTF(("cryptodev_op: crde setup for cipher %d.\n", crde->crd_alg));
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
2009-03-25 04:26:12 +03:00
|
|
|
|
2003-07-26 01:12:39 +04:00
|
|
|
crp->crp_ilen = cop->len;
|
2008-11-18 15:59:58 +03:00
|
|
|
/* The reqest is flagged as CRYPTO_F_USER as long as it is running
|
|
|
|
* in the user IOCTL thread. This flag lets us skip using the retq for
|
|
|
|
* the request if it completes immediately. If the request ends up being
|
|
|
|
* delayed or is not completed immediately the flag is removed.
|
|
|
|
*/
|
2009-03-25 04:26:12 +03:00
|
|
|
crp->crp_flags = CRYPTO_F_IOV | (cop->flags & COP_F_BATCH) | CRYPTO_F_USER |
|
|
|
|
flags;
|
2007-03-04 08:59:00 +03:00
|
|
|
crp->crp_buf = (void *)&cse->uio;
|
2003-07-26 01:12:39 +04:00
|
|
|
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;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
|
|
|
|
error = EINVAL;
|
|
|
|
goto bail;
|
|
|
|
}
|
2008-04-11 02:48:42 +04:00
|
|
|
if ((error = copyin(cop->iv, cse->tmp_iv,
|
2008-05-24 20:28:58 +04:00
|
|
|
cse->txform->blocksize)))
|
2003-07-26 01:12:39 +04:00
|
|
|
goto bail;
|
2008-05-24 20:28:58 +04:00
|
|
|
(void)memcpy(crde->crd_iv, cse->tmp_iv, cse->txform->blocksize);
|
2003-07-26 01:12:39 +04:00
|
|
|
crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
|
|
|
|
crde->crd_skip = 0;
|
|
|
|
} else if (crde) {
|
2006-04-04 21:30:20 +04:00
|
|
|
if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
|
|
|
|
crde->crd_skip = 0;
|
|
|
|
} else {
|
|
|
|
crde->crd_flags |= CRD_F_IV_PRESENT;
|
|
|
|
crde->crd_skip = cse->txform->blocksize;
|
|
|
|
crde->crd_len -= cse->txform->blocksize;
|
|
|
|
}
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cop->mac) {
|
|
|
|
if (crda == NULL) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
crp->crp_mac=cse->tmp_mac;
|
|
|
|
}
|
|
|
|
|
2008-02-04 03:35:34 +03:00
|
|
|
/*
|
|
|
|
* XXX there was a comment here which said that we went to
|
|
|
|
* XXX splcrypto() but needed to only if CRYPTO_F_CBIMM,
|
|
|
|
* XXX disabled on NetBSD since 1.6O due to a race condition.
|
|
|
|
* XXX But crypto_dispatch went to splcrypto() itself! (And
|
|
|
|
* XXX now takes the crypto_mtx mutex itself). We do, however,
|
|
|
|
* XXX need to hold the mutex across the call to cv_wait().
|
|
|
|
* XXX (should we arrange for crypto_dispatch to return to
|
|
|
|
* XXX us with it held? it seems quite ugly to do so.)
|
|
|
|
*/
|
2008-04-11 02:48:42 +04:00
|
|
|
#ifdef notyet
|
|
|
|
eagain:
|
|
|
|
#endif
|
2003-07-26 01:12:39 +04:00
|
|
|
error = crypto_dispatch(crp);
|
2008-02-04 03:35:34 +03:00
|
|
|
mutex_spin_enter(&crypto_mtx);
|
2008-04-11 02:48:42 +04:00
|
|
|
|
2008-11-18 15:59:58 +03:00
|
|
|
/*
|
|
|
|
* If the request was going to be completed by the
|
|
|
|
* ioctl thread then it would have been done by now.
|
2010-01-31 17:32:56 +03:00
|
|
|
* Remove the F_USER flag so crypto_done() is not confused
|
2008-11-18 15:59:58 +03:00
|
|
|
* if the crypto device calls it after this point.
|
|
|
|
*/
|
|
|
|
crp->crp_flags &= ~(CRYPTO_F_USER);
|
|
|
|
|
2008-04-11 02:48:42 +04:00
|
|
|
switch (error) {
|
|
|
|
#ifdef notyet /* don't loop forever -- but EAGAIN not possible here yet */
|
|
|
|
case EAGAIN:
|
|
|
|
mutex_spin_exit(&crypto_mtx);
|
|
|
|
goto eagain;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
2008-02-04 03:35:34 +03:00
|
|
|
DPRINTF(("cryptodev_op: not waiting, error.\n"));
|
|
|
|
mutex_spin_exit(&crypto_mtx);
|
2003-07-26 01:12:39 +04:00
|
|
|
goto bail;
|
|
|
|
}
|
2008-11-18 15:59:58 +03:00
|
|
|
|
2008-02-04 03:35:34 +03:00
|
|
|
while (!(crp->crp_flags & CRYPTO_F_DONE)) {
|
2009-03-25 04:26:12 +03:00
|
|
|
DPRINTF(("cryptodev_op[%d]: sleeping on cv %08x for crp %08x\n",
|
|
|
|
(uint32_t)cse->sid, (uint32_t)&crp->crp_cv,
|
|
|
|
(uint32_t)crp));
|
2008-02-04 03:35:34 +03:00
|
|
|
cv_wait(&crp->crp_cv, &crypto_mtx); /* XXX cv_wait_sig? */
|
|
|
|
}
|
|
|
|
if (crp->crp_flags & CRYPTO_F_ONRETQ) {
|
2008-11-18 15:59:58 +03:00
|
|
|
/* XXX this should never happen now with the CRYPTO_F_USER flag
|
|
|
|
* changes.
|
|
|
|
*/
|
2008-02-04 03:35:34 +03:00
|
|
|
DPRINTF(("cryptodev_op: DONE, not woken by cryptoret.\n"));
|
|
|
|
(void)crypto_ret_q_remove(crp);
|
|
|
|
}
|
|
|
|
mutex_spin_exit(&crypto_mtx);
|
2003-07-26 01:12:39 +04:00
|
|
|
|
|
|
|
if (crp->crp_etype != 0) {
|
2008-02-04 17:46:26 +03:00
|
|
|
DPRINTF(("cryptodev_op: crp_etype %d\n", crp->crp_etype));
|
2003-07-26 01:12:39 +04:00
|
|
|
error = crp->crp_etype;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cse->error) {
|
2008-02-04 17:46:26 +03:00
|
|
|
DPRINTF(("cryptodev_op: cse->error %d\n", cse->error));
|
2003-07-26 01:12:39 +04:00
|
|
|
error = cse->error;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2009-03-25 04:26:12 +03:00
|
|
|
dst_len = crp->crp_ilen;
|
|
|
|
/* let the user know how much data was returned */
|
|
|
|
if (crp->crp_olen) {
|
|
|
|
dst_len = cop->dst_len = crp->crp_olen;
|
|
|
|
}
|
|
|
|
crp->len = dst_len;
|
|
|
|
|
|
|
|
if (cop->dst) {
|
|
|
|
DPRINTF(("cryptodev_op: copyout %d bytes to %p\n", dst_len, cop->dst));
|
|
|
|
}
|
2003-07-26 01:12:39 +04:00
|
|
|
if (cop->dst &&
|
2009-03-25 04:26:12 +03:00
|
|
|
(error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, dst_len)))
|
2008-04-11 02:48:42 +04:00
|
|
|
{
|
2008-02-04 17:46:26 +03:00
|
|
|
DPRINTF(("cryptodev_op: copyout error %d\n", error));
|
2003-07-26 01:12:39 +04:00
|
|
|
goto bail;
|
2008-02-04 17:46:26 +03:00
|
|
|
}
|
2003-07-26 01:12:39 +04:00
|
|
|
|
|
|
|
if (cop->mac &&
|
2008-02-04 17:46:26 +03:00
|
|
|
(error = copyout(crp->crp_mac, cop->mac, cse->thash->authsize))) {
|
|
|
|
DPRINTF(("cryptodev_op: mac copyout error %d\n", error));
|
2003-07-26 01:12:39 +04:00
|
|
|
goto bail;
|
2008-02-04 17:46:26 +03:00
|
|
|
}
|
2003-07-26 01:12:39 +04:00
|
|
|
|
2009-03-25 04:26:12 +03:00
|
|
|
|
2003-07-26 01:12:39 +04:00
|
|
|
bail:
|
2008-11-18 15:59:58 +03:00
|
|
|
if (crp) {
|
2003-07-26 01:12:39 +04:00
|
|
|
crypto_freereq(crp);
|
2008-11-18 15:59:58 +03:00
|
|
|
}
|
2009-03-25 04:26:12 +03:00
|
|
|
if (cse->uio.uio_iov[0].iov_base) {
|
|
|
|
kmem_free(cse->uio.uio_iov[0].iov_base,iov_len);
|
|
|
|
}
|
2003-07-26 01:12:39 +04:00
|
|
|
|
2008-05-24 20:28:58 +04:00
|
|
|
return error;
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cryptodev_cb(void *op)
|
|
|
|
{
|
|
|
|
struct cryptop *crp = (struct cryptop *) op;
|
|
|
|
struct csession *cse = (struct csession *)crp->crp_opaque;
|
2008-02-04 03:35:34 +03:00
|
|
|
int error = 0;
|
2003-07-26 01:12:39 +04:00
|
|
|
|
2008-02-04 03:35:34 +03:00
|
|
|
mutex_spin_enter(&crypto_mtx);
|
2003-07-26 01:12:39 +04:00
|
|
|
cse->error = crp->crp_etype;
|
2008-02-04 03:35:34 +03:00
|
|
|
if (crp->crp_etype == EAGAIN) {
|
|
|
|
/* always drop mutex to call dispatch routine */
|
|
|
|
mutex_spin_exit(&crypto_mtx);
|
|
|
|
error = crypto_dispatch(crp);
|
|
|
|
mutex_spin_enter(&crypto_mtx);
|
|
|
|
}
|
|
|
|
if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
|
|
|
|
cv_signal(&crp->crp_cv);
|
|
|
|
}
|
|
|
|
mutex_spin_exit(&crypto_mtx);
|
2008-05-24 20:28:58 +04:00
|
|
|
return 0;
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
2008-04-11 02:48:42 +04:00
|
|
|
static int
|
|
|
|
cryptodev_mcb(void *op)
|
|
|
|
{
|
|
|
|
struct cryptop *crp = (struct cryptop *) op;
|
|
|
|
struct csession *cse = (struct csession *)crp->crp_opaque;
|
|
|
|
int error=0;
|
|
|
|
|
|
|
|
mutex_spin_enter(&crypto_mtx);
|
|
|
|
cse->error = crp->crp_etype;
|
|
|
|
if (crp->crp_etype == EAGAIN) {
|
|
|
|
mutex_spin_exit(&crypto_mtx);
|
|
|
|
error = crypto_dispatch(crp);
|
|
|
|
mutex_spin_enter(&crypto_mtx);
|
|
|
|
}
|
|
|
|
if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
|
|
|
|
cv_signal(&crp->crp_cv);
|
|
|
|
}
|
|
|
|
|
|
|
|
TAILQ_INSERT_TAIL(&crp->fcrp->crp_ret_mq, crp, crp_next);
|
|
|
|
selnotify(&crp->fcrp->sinfo, 0, 0);
|
2008-04-11 14:28:10 +04:00
|
|
|
mutex_spin_exit(&crypto_mtx);
|
2008-05-24 20:28:58 +04:00
|
|
|
return 0;
|
2008-04-11 02:48:42 +04:00
|
|
|
}
|
|
|
|
|
2003-07-26 01:12:39 +04:00
|
|
|
static int
|
|
|
|
cryptodevkey_cb(void *op)
|
2008-04-11 02:48:42 +04:00
|
|
|
{
|
2008-05-24 20:28:58 +04:00
|
|
|
struct cryptkop *krp = op;
|
2008-04-11 02:48:42 +04:00
|
|
|
|
|
|
|
mutex_spin_enter(&crypto_mtx);
|
|
|
|
cv_signal(&krp->krp_cv);
|
|
|
|
mutex_spin_exit(&crypto_mtx);
|
2008-05-24 20:28:58 +04:00
|
|
|
return 0;
|
2008-04-11 02:48:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cryptodevkey_mcb(void *op)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
2008-05-24 20:28:58 +04:00
|
|
|
struct cryptkop *krp = op;
|
2003-07-26 01:12:39 +04:00
|
|
|
|
2008-02-04 03:35:34 +03:00
|
|
|
mutex_spin_enter(&crypto_mtx);
|
|
|
|
cv_signal(&krp->krp_cv);
|
2008-04-11 02:48:42 +04:00
|
|
|
TAILQ_INSERT_TAIL(&krp->fcrp->crp_ret_mkq, krp, krp_next);
|
|
|
|
selnotify(&krp->fcrp->sinfo, 0, 0);
|
2008-04-11 14:28:10 +04:00
|
|
|
mutex_spin_exit(&crypto_mtx);
|
2008-05-24 20:28:58 +04:00
|
|
|
return 0;
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cryptodev_key(struct crypt_kop *kop)
|
|
|
|
{
|
|
|
|
struct cryptkop *krp = NULL;
|
|
|
|
int error = EINVAL;
|
|
|
|
int in, out, size, i;
|
|
|
|
|
2008-05-24 20:28:58 +04:00
|
|
|
if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM)
|
|
|
|
return EFBIG;
|
2003-07-26 01:12:39 +04:00
|
|
|
|
|
|
|
in = kop->crk_iparams;
|
|
|
|
out = kop->crk_oparams;
|
|
|
|
switch (kop->crk_op) {
|
|
|
|
case CRK_MOD_EXP:
|
|
|
|
if (in == 3 && out == 1)
|
|
|
|
break;
|
2008-05-24 20:28:58 +04:00
|
|
|
return EINVAL;
|
2003-07-26 01:12:39 +04:00
|
|
|
case CRK_MOD_EXP_CRT:
|
|
|
|
if (in == 6 && out == 1)
|
|
|
|
break;
|
2008-05-24 20:28:58 +04:00
|
|
|
return EINVAL;
|
2003-07-26 01:12:39 +04:00
|
|
|
case CRK_DSA_SIGN:
|
|
|
|
if (in == 5 && out == 2)
|
|
|
|
break;
|
2008-05-24 20:28:58 +04:00
|
|
|
return EINVAL;
|
2003-07-26 01:12:39 +04:00
|
|
|
case CRK_DSA_VERIFY:
|
|
|
|
if (in == 7 && out == 0)
|
|
|
|
break;
|
2008-05-24 20:28:58 +04:00
|
|
|
return EINVAL;
|
2003-07-26 01:12:39 +04:00
|
|
|
case CRK_DH_COMPUTE_KEY:
|
|
|
|
if (in == 3 && out == 1)
|
|
|
|
break;
|
2008-05-24 20:28:58 +04:00
|
|
|
return EINVAL;
|
2008-01-19 11:08:20 +03:00
|
|
|
case CRK_MOD_ADD:
|
|
|
|
if (in == 3 && out == 1)
|
|
|
|
break;
|
2008-05-24 20:28:58 +04:00
|
|
|
return EINVAL;
|
2008-01-19 11:08:20 +03:00
|
|
|
case CRK_MOD_ADDINV:
|
|
|
|
if (in == 2 && out == 1)
|
|
|
|
break;
|
2008-05-24 20:28:58 +04:00
|
|
|
return EINVAL;
|
2008-01-19 11:08:20 +03:00
|
|
|
case CRK_MOD_SUB:
|
|
|
|
if (in == 3 && out == 1)
|
|
|
|
break;
|
2008-05-24 20:28:58 +04:00
|
|
|
return EINVAL;
|
2008-01-19 11:08:20 +03:00
|
|
|
case CRK_MOD_MULT:
|
|
|
|
if (in == 3 && out == 1)
|
|
|
|
break;
|
2008-05-24 20:28:58 +04:00
|
|
|
return EINVAL;
|
2008-01-19 11:08:20 +03:00
|
|
|
case CRK_MOD_MULTINV:
|
|
|
|
if (in == 2 && out == 1)
|
|
|
|
break;
|
2008-05-24 20:28:58 +04:00
|
|
|
return EINVAL;
|
2008-01-19 11:08:20 +03:00
|
|
|
case CRK_MOD:
|
|
|
|
if (in == 2 && out == 1)
|
|
|
|
break;
|
2008-05-24 20:28:58 +04:00
|
|
|
return EINVAL;
|
2003-07-26 01:12:39 +04:00
|
|
|
default:
|
2008-05-24 20:28:58 +04:00
|
|
|
return EINVAL;
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
2008-02-04 03:35:34 +03:00
|
|
|
krp = pool_get(&cryptkop_pool, PR_WAITOK);
|
2008-05-24 20:28:58 +04:00
|
|
|
(void)memset(krp, 0, sizeof *krp);
|
2008-02-04 03:35:34 +03:00
|
|
|
cv_init(&krp->krp_cv, "crykdev");
|
2003-07-26 01:12:39 +04:00
|
|
|
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_status = 0;
|
|
|
|
krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
|
|
|
|
|
|
|
|
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;
|
2008-04-11 02:48:42 +04:00
|
|
|
krp->krp_param[i].crp_p = kmem_alloc(size, KM_SLEEP);
|
2003-07-26 01:12:39 +04:00
|
|
|
if (i >= krp->krp_iparams)
|
|
|
|
continue;
|
2008-05-24 20:28:58 +04:00
|
|
|
error = copyin(kop->crk_param[i].crp_p,
|
|
|
|
krp->krp_param[i].crp_p, size);
|
2003-07-26 01:12:39 +04:00
|
|
|
if (error)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = crypto_kdispatch(krp);
|
2008-02-04 03:35:34 +03:00
|
|
|
if (error != 0) {
|
2003-07-26 01:12:39 +04:00
|
|
|
goto fail;
|
2008-02-04 03:35:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mutex_spin_enter(&crypto_mtx);
|
|
|
|
while (!(krp->krp_flags & CRYPTO_F_DONE)) {
|
|
|
|
cv_wait(&krp->krp_cv, &crypto_mtx); /* XXX cv_wait_sig? */
|
|
|
|
}
|
|
|
|
if (krp->krp_flags & CRYPTO_F_ONRETQ) {
|
|
|
|
DPRINTF(("cryptodev_key: DONE early, not via cryptoret.\n"));
|
|
|
|
(void)crypto_ret_kq_remove(krp);
|
|
|
|
}
|
|
|
|
mutex_spin_exit(&crypto_mtx);
|
2003-07-26 01:12:39 +04:00
|
|
|
|
|
|
|
if (krp->krp_status != 0) {
|
2008-05-24 20:28:58 +04:00
|
|
|
DPRINTF(("cryptodev_key: krp->krp_status 0x%08x\n",
|
|
|
|
krp->krp_status));
|
2003-07-26 01:12:39 +04:00
|
|
|
error = krp->krp_status;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2008-05-24 20:28:58 +04:00
|
|
|
for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams;
|
|
|
|
i++) {
|
2003-07-26 01:12:39 +04:00
|
|
|
size = (krp->krp_param[i].crp_nbits + 7) / 8;
|
|
|
|
if (size == 0)
|
|
|
|
continue;
|
2008-05-24 20:28:58 +04:00
|
|
|
error = copyout(krp->krp_param[i].crp_p,
|
|
|
|
kop->crk_param[i].crp_p, size);
|
2008-02-04 17:46:26 +03:00
|
|
|
if (error) {
|
2008-05-24 20:28:58 +04:00
|
|
|
DPRINTF(("cryptodev_key: copyout oparam %d failed, "
|
|
|
|
"error=%d\n", i-krp->krp_iparams, error));
|
2003-07-26 01:12:39 +04:00
|
|
|
goto fail;
|
2008-02-04 17:46:26 +03:00
|
|
|
}
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
2008-05-24 20:29:34 +04:00
|
|
|
kop->crk_status = krp->krp_status;
|
|
|
|
for (i = 0; i < CRK_MAXPARAM; i++) {
|
|
|
|
struct crparam *kp = &(krp->krp_param[i]);
|
|
|
|
if (krp->krp_param[i].crp_p) {
|
|
|
|
size = (kp->crp_nbits + 7) / 8;
|
|
|
|
KASSERT(size > 0);
|
|
|
|
(void)memset(kp->crp_p, 0, size);
|
|
|
|
kmem_free(kp->crp_p, size);
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
}
|
2008-11-18 15:59:58 +03:00
|
|
|
cv_destroy(&krp->krp_cv);
|
2008-05-24 20:29:34 +04:00
|
|
|
pool_put(&cryptkop_pool, krp);
|
2008-02-04 17:46:26 +03:00
|
|
|
DPRINTF(("cryptodev_key: error=0x%08x\n", error));
|
2008-05-24 20:28:58 +04:00
|
|
|
return error;
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
static int
|
2008-04-11 02:48:42 +04:00
|
|
|
cryptof_close(struct file *fp)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
2008-03-22 00:54:58 +03:00
|
|
|
struct fcrypt *fcr = fp->f_data;
|
2003-07-26 01:12:39 +04:00
|
|
|
struct csession *cse;
|
|
|
|
|
2008-02-04 03:35:34 +03:00
|
|
|
mutex_spin_enter(&crypto_mtx);
|
2003-07-26 01:12:39 +04:00
|
|
|
while ((cse = TAILQ_FIRST(&fcr->csessions))) {
|
|
|
|
TAILQ_REMOVE(&fcr->csessions, cse, next);
|
|
|
|
(void)csefree(cse);
|
|
|
|
}
|
2008-04-11 02:48:42 +04:00
|
|
|
seldestroy(&fcr->sinfo);
|
2003-07-26 01:12:39 +04:00
|
|
|
fp->f_data = NULL;
|
2008-02-04 03:35:34 +03:00
|
|
|
mutex_spin_exit(&crypto_mtx);
|
2003-07-26 01:12:39 +04:00
|
|
|
|
2008-04-11 14:28:10 +04:00
|
|
|
pool_put(&fcrpl, fcr);
|
2003-07-26 01:12:39 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-25 04:26:12 +03:00
|
|
|
/* needed for compatibility module */
|
|
|
|
struct csession *cryptodev_csefind(struct fcrypt *fcr, u_int ses)
|
|
|
|
{
|
|
|
|
return csefind(fcr, ses);
|
|
|
|
}
|
|
|
|
|
2008-02-04 03:35:34 +03:00
|
|
|
/* csefind: call with crypto_mtx held. */
|
2003-07-26 01:12:39 +04:00
|
|
|
static struct csession *
|
|
|
|
csefind(struct fcrypt *fcr, u_int ses)
|
|
|
|
{
|
2008-11-18 15:59:58 +03:00
|
|
|
struct csession *cse, *cnext, *ret = NULL;
|
2003-07-26 01:12:39 +04:00
|
|
|
|
2008-02-04 03:35:34 +03:00
|
|
|
KASSERT(mutex_owned(&crypto_mtx));
|
2008-11-18 15:59:58 +03:00
|
|
|
TAILQ_FOREACH_SAFE(cse, &fcr->csessions, next, cnext)
|
2003-07-26 01:12:39 +04:00
|
|
|
if (cse->ses == ses)
|
2008-02-04 03:35:34 +03:00
|
|
|
ret = cse;
|
2008-04-11 02:48:42 +04:00
|
|
|
|
2008-05-24 20:28:58 +04:00
|
|
|
return ret;
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
2008-02-04 03:35:34 +03:00
|
|
|
/* csedelete: call with crypto_mtx held. */
|
2003-07-26 01:12:39 +04:00
|
|
|
static int
|
|
|
|
csedelete(struct fcrypt *fcr, struct csession *cse_del)
|
|
|
|
{
|
2008-11-18 15:59:58 +03:00
|
|
|
struct csession *cse, *cnext;
|
2008-02-04 03:35:34 +03:00
|
|
|
int ret = 0;
|
2003-07-26 01:12:39 +04:00
|
|
|
|
2008-02-04 03:35:34 +03:00
|
|
|
KASSERT(mutex_owned(&crypto_mtx));
|
2008-11-18 15:59:58 +03:00
|
|
|
TAILQ_FOREACH_SAFE(cse, &fcr->csessions, next, cnext) {
|
2003-07-26 01:12:39 +04:00
|
|
|
if (cse == cse_del) {
|
|
|
|
TAILQ_REMOVE(&fcr->csessions, cse, next);
|
2008-02-04 03:35:34 +03:00
|
|
|
ret = 1;
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
}
|
2008-05-24 20:28:58 +04:00
|
|
|
return ret;
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
2008-02-04 03:35:34 +03:00
|
|
|
/* cseadd: call with crypto_mtx held. */
|
2003-07-26 01:12:39 +04:00
|
|
|
static struct csession *
|
|
|
|
cseadd(struct fcrypt *fcr, struct csession *cse)
|
|
|
|
{
|
2008-02-04 03:35:34 +03:00
|
|
|
KASSERT(mutex_owned(&crypto_mtx));
|
|
|
|
/* don't let session ID wrap! */
|
|
|
|
if (fcr->sesn + 1 == 0) return NULL;
|
2003-07-26 01:12:39 +04:00
|
|
|
TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
|
|
|
|
cse->ses = fcr->sesn++;
|
2008-05-24 20:28:58 +04:00
|
|
|
return cse;
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
2008-02-04 03:35:34 +03:00
|
|
|
/* csecreate: call with crypto_mtx held. */
|
2003-07-26 01:12:39 +04:00
|
|
|
static struct csession *
|
2007-03-04 08:59:00 +03:00
|
|
|
csecreate(struct fcrypt *fcr, u_int64_t sid, void *key, u_int64_t keylen,
|
|
|
|
void *mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
|
2009-03-25 04:26:12 +03:00
|
|
|
u_int32_t comp_alg, struct enc_xform *txform, struct auth_hash *thash,
|
|
|
|
struct comp_algo *tcomp)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
|
|
|
struct csession *cse;
|
|
|
|
|
2008-02-04 03:35:34 +03:00
|
|
|
KASSERT(mutex_owned(&crypto_mtx));
|
2008-01-26 03:04:27 +03:00
|
|
|
cse = pool_get(&csepl, PR_NOWAIT);
|
2003-07-26 01:12:39 +04:00
|
|
|
if (cse == NULL)
|
|
|
|
return NULL;
|
|
|
|
cse->key = key;
|
|
|
|
cse->keylen = keylen/8;
|
|
|
|
cse->mackey = mackey;
|
|
|
|
cse->mackeylen = mackeylen/8;
|
|
|
|
cse->sid = sid;
|
|
|
|
cse->cipher = cipher;
|
|
|
|
cse->mac = mac;
|
2009-03-25 04:26:12 +03:00
|
|
|
cse->comp_alg = comp_alg;
|
2003-07-26 01:12:39 +04:00
|
|
|
cse->txform = txform;
|
|
|
|
cse->thash = thash;
|
2009-03-25 04:26:12 +03:00
|
|
|
cse->tcomp = tcomp;
|
2008-02-04 17:46:26 +03:00
|
|
|
cse->error = 0;
|
2008-01-25 10:09:56 +03:00
|
|
|
if (cseadd(fcr, cse))
|
2008-05-24 20:28:58 +04:00
|
|
|
return cse;
|
2008-01-25 10:09:56 +03:00
|
|
|
else {
|
2008-02-04 03:35:34 +03:00
|
|
|
pool_put(&csepl, cse);
|
2008-01-25 10:09:56 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
2008-02-04 03:35:34 +03:00
|
|
|
/* csefree: call with crypto_mtx held. */
|
2003-07-26 01:12:39 +04:00
|
|
|
static int
|
|
|
|
csefree(struct csession *cse)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2008-02-04 03:35:34 +03:00
|
|
|
KASSERT(mutex_owned(&crypto_mtx));
|
2003-07-26 01:12:39 +04:00
|
|
|
error = crypto_freesession(cse->sid);
|
|
|
|
if (cse->key)
|
2008-04-11 02:48:42 +04:00
|
|
|
free(cse->key, M_XDATA);
|
2003-07-26 01:12:39 +04:00
|
|
|
if (cse->mackey)
|
2008-04-11 02:48:42 +04:00
|
|
|
free(cse->mackey, M_XDATA);
|
2008-01-26 03:04:27 +03:00
|
|
|
pool_put(&csepl, cse);
|
2008-05-24 20:28:58 +04:00
|
|
|
return error;
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-11-16 04:32:37 +03:00
|
|
|
cryptoopen(dev_t dev, int flag, int mode,
|
|
|
|
struct lwp *l)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
2008-03-22 00:54:58 +03:00
|
|
|
file_t *fp;
|
2008-01-26 03:04:27 +03:00
|
|
|
struct fcrypt *fcr;
|
|
|
|
int fd, error;
|
|
|
|
|
2003-07-26 01:12:39 +04:00
|
|
|
if (crypto_usercrypto == 0)
|
2008-05-24 20:28:58 +04:00
|
|
|
return ENXIO;
|
2008-01-26 03:04:27 +03:00
|
|
|
|
2008-03-22 00:54:58 +03:00
|
|
|
if ((error = fd_allocfile(&fp, &fd)) != 0)
|
2008-01-26 03:04:27 +03:00
|
|
|
return error;
|
|
|
|
|
|
|
|
fcr = pool_get(&fcrpl, PR_WAITOK);
|
2009-04-11 19:47:33 +04:00
|
|
|
getnanotime(&fcr->btime);
|
|
|
|
fcr->atime = fcr->mtime = fcr->btime;
|
2008-02-04 03:35:34 +03:00
|
|
|
mutex_spin_enter(&crypto_mtx);
|
2008-01-26 03:04:27 +03:00
|
|
|
TAILQ_INIT(&fcr->csessions);
|
2008-04-11 02:48:42 +04:00
|
|
|
TAILQ_INIT(&fcr->crp_ret_mq);
|
|
|
|
TAILQ_INIT(&fcr->crp_ret_mkq);
|
|
|
|
selinit(&fcr->sinfo);
|
2008-01-26 03:04:27 +03:00
|
|
|
/*
|
|
|
|
* Don't ever return session 0, to allow detection of
|
|
|
|
* failed creation attempts with multi-create ioctl.
|
|
|
|
*/
|
|
|
|
fcr->sesn = 1;
|
2008-04-11 02:48:42 +04:00
|
|
|
fcr->requestid = 1;
|
2008-02-04 03:35:34 +03:00
|
|
|
mutex_spin_exit(&crypto_mtx);
|
2008-03-22 00:54:58 +03:00
|
|
|
return fd_clone(fp, fd, flag, &cryptofops, fcr);
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-11-16 04:32:37 +03:00
|
|
|
cryptoread(dev_t dev, struct uio *uio, int ioflag)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
2008-05-24 20:28:58 +04:00
|
|
|
return EIO;
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-11-16 04:32:37 +03:00
|
|
|
cryptowrite(dev_t dev, struct uio *uio, int ioflag)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
2008-05-24 20:28:58 +04:00
|
|
|
return EIO;
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-11-16 04:32:37 +03:00
|
|
|
cryptoselect(dev_t dev, int rw, struct lwp *l)
|
2003-07-26 01:12:39 +04:00
|
|
|
{
|
2008-05-24 20:28:58 +04:00
|
|
|
return 0;
|
2003-07-26 01:12:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/
|
|
|
|
struct cdevsw crypto_cdevsw = {
|
|
|
|
/* open */ cryptoopen,
|
2008-01-26 03:04:27 +03:00
|
|
|
/* close */ noclose,
|
2003-07-26 01:12:39 +04:00
|
|
|
/* read */ cryptoread,
|
|
|
|
/* write */ cryptowrite,
|
2008-01-26 03:04:27 +03:00
|
|
|
/* ioctl */ noioctl,
|
2003-07-26 01:12:39 +04:00
|
|
|
/* ttstop?*/ nostop,
|
|
|
|
/* ??*/ notty,
|
|
|
|
/* poll */ cryptoselect /*nopoll*/,
|
|
|
|
/* mmap */ nommap,
|
|
|
|
/* kqfilter */ nokqfilter,
|
2006-08-30 03:45:23 +04:00
|
|
|
/* type */ D_OTHER,
|
2003-07-26 01:12:39 +04:00
|
|
|
};
|
|
|
|
|
2009-03-25 04:26:12 +03:00
|
|
|
int
|
2008-04-11 02:48:42 +04:00
|
|
|
cryptodev_mop(struct fcrypt *fcr,
|
|
|
|
struct crypt_n_op * cnop,
|
|
|
|
int count, struct lwp *l)
|
|
|
|
{
|
|
|
|
struct cryptop *crp = NULL;
|
2009-03-25 04:26:12 +03:00
|
|
|
struct cryptodesc *crde = NULL, *crda = NULL, *crdc = NULL;
|
2008-04-11 02:48:42 +04:00
|
|
|
int req, error=0;
|
|
|
|
struct csession *cse;
|
2009-03-25 04:26:12 +03:00
|
|
|
int flags=0;
|
|
|
|
int iov_len;
|
2008-04-11 02:48:42 +04:00
|
|
|
|
|
|
|
for (req = 0; req < count; req++) {
|
|
|
|
mutex_spin_enter(&crypto_mtx);
|
|
|
|
cse = csefind(fcr, cnop[req].ses);
|
|
|
|
if (cse == NULL) {
|
|
|
|
DPRINTF(("csefind failed\n"));
|
|
|
|
cnop[req].status = EINVAL;
|
|
|
|
mutex_spin_exit(&crypto_mtx);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mutex_spin_exit(&crypto_mtx);
|
|
|
|
|
|
|
|
if (cnop[req].len > 256*1024-4) {
|
|
|
|
DPRINTF(("length failed\n"));
|
|
|
|
cnop[req].status = EINVAL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (cse->txform) {
|
|
|
|
if (cnop[req].len == 0 ||
|
|
|
|
(cnop[req].len % cse->txform->blocksize) != 0) {
|
|
|
|
cnop[req].status = EINVAL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-24 20:28:58 +04:00
|
|
|
crp = crypto_getreq((cse->txform != NULL) +
|
2009-03-25 04:26:12 +03:00
|
|
|
(cse->thash != NULL) +
|
|
|
|
(cse->tcomp != NULL));
|
2008-04-11 02:48:42 +04:00
|
|
|
if (crp == NULL) {
|
|
|
|
cnop[req].status = ENOMEM;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2009-03-25 04:26:12 +03:00
|
|
|
iov_len = cnop[req].len;
|
|
|
|
/* got a compression/decompression max size? */
|
|
|
|
if ((cse->tcomp) && cnop[req].dst_len) {
|
|
|
|
if (iov_len < cnop[req].dst_len) {
|
|
|
|
/* Need larger iov to deal with decompress */
|
|
|
|
iov_len = cnop[req].dst_len;
|
|
|
|
}
|
|
|
|
DPRINTF(("cryptodev_mop: iov_len -> %d for decompress\n", iov_len));
|
|
|
|
}
|
|
|
|
|
2008-05-24 20:28:58 +04:00
|
|
|
(void)memset(&crp->uio, 0, sizeof(crp->uio));
|
2008-04-11 02:48:42 +04:00
|
|
|
crp->uio.uio_iovcnt = 1;
|
|
|
|
crp->uio.uio_resid = 0;
|
|
|
|
crp->uio.uio_rw = UIO_WRITE;
|
|
|
|
crp->uio.uio_iov = crp->iovec;
|
|
|
|
UIO_SETUP_SYSSPACE(&crp->uio);
|
|
|
|
memset(&crp->iovec, 0, sizeof(crp->iovec));
|
2009-03-25 04:26:12 +03:00
|
|
|
crp->uio.uio_iov[0].iov_len = iov_len;
|
|
|
|
DPRINTF(("cryptodev_mop: kmem_alloc(%d) for iov \n", iov_len));
|
|
|
|
crp->uio.uio_iov[0].iov_base = kmem_alloc(iov_len, KM_SLEEP);
|
2008-04-11 02:48:42 +04:00
|
|
|
crp->uio.uio_resid = crp->uio.uio_iov[0].iov_len;
|
|
|
|
|
2009-03-25 04:26:12 +03:00
|
|
|
if (cse->tcomp) {
|
|
|
|
crdc = crp->crp_desc;
|
|
|
|
}
|
|
|
|
|
2008-04-11 02:48:42 +04:00
|
|
|
if (cse->thash) {
|
2009-03-25 04:26:12 +03:00
|
|
|
crda = crdc ? crdc->crd_next : crp->crp_desc;
|
2008-05-24 20:42:00 +04:00
|
|
|
if (cse->txform && crda)
|
2008-04-11 02:48:42 +04:00
|
|
|
crde = crda->crd_next;
|
|
|
|
} else {
|
2009-03-25 04:26:12 +03:00
|
|
|
if (cse->txform) {
|
|
|
|
crde = crdc ? crdc->crd_next : crp->crp_desc;
|
|
|
|
} else if (!cse->tcomp) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto bail;
|
2008-04-11 02:48:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((copyin(cnop[req].src,
|
|
|
|
crp->uio.uio_iov[0].iov_base, cnop[req].len))) {
|
|
|
|
cnop[req].status = EINVAL;
|
|
|
|
goto bail;
|
|
|
|
}
|
2009-03-25 04:26:12 +03:00
|
|
|
|
|
|
|
if (crdc) {
|
|
|
|
switch (cnop[req].op) {
|
|
|
|
case COP_COMP:
|
|
|
|
crdc->crd_flags |= CRD_F_COMP;
|
|
|
|
break;
|
|
|
|
case COP_DECOMP:
|
|
|
|
crdc->crd_flags &= ~CRD_F_COMP;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* more data to follow? */
|
|
|
|
if (cnop[req].flags & COP_F_MORE) {
|
|
|
|
flags |= CRYPTO_F_MORE;
|
|
|
|
}
|
|
|
|
crdc->crd_len = cnop[req].len;
|
|
|
|
crdc->crd_inject = 0;
|
|
|
|
|
|
|
|
crdc->crd_alg = cse->comp_alg;
|
|
|
|
crdc->crd_key = NULL;
|
|
|
|
crdc->crd_klen = 0;
|
|
|
|
DPRINTF(("cryptodev_mop[%d]: crdc setup for comp_alg %d"
|
|
|
|
" len %d.\n",
|
|
|
|
(uint32_t)cse->sid, crdc->crd_alg,
|
|
|
|
crdc->crd_len));
|
|
|
|
}
|
2008-04-11 02:48:42 +04:00
|
|
|
|
|
|
|
if (crda) {
|
|
|
|
crda->crd_skip = 0;
|
|
|
|
crda->crd_len = cnop[req].len;
|
|
|
|
crda->crd_inject = 0; /* ??? */
|
|
|
|
|
|
|
|
crda->crd_alg = cse->mac;
|
|
|
|
crda->crd_key = cse->mackey;
|
|
|
|
crda->crd_klen = cse->mackeylen * 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (crde) {
|
|
|
|
if (cnop[req].op == COP_ENCRYPT)
|
|
|
|
crde->crd_flags |= CRD_F_ENCRYPT;
|
|
|
|
else
|
|
|
|
crde->crd_flags &= ~CRD_F_ENCRYPT;
|
|
|
|
crde->crd_len = cnop[req].len;
|
|
|
|
crde->crd_inject = 0;
|
|
|
|
|
|
|
|
crde->crd_alg = cse->cipher;
|
|
|
|
#ifdef notyet /* XXX must notify h/w driver new key, drain */
|
|
|
|
if(cnop[req].key && cnop[req].keylen) {
|
|
|
|
crde->crd_key = malloc(cnop[req].keylen,
|
|
|
|
M_XDATA, M_WAITOK);
|
|
|
|
if((error = copyin(cnop[req].key,
|
|
|
|
crde->crd_key, cnop[req].keylen))) {
|
|
|
|
cnop[req].status = EINVAL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
crde->crd_klen = cnop[req].keylen * 8;
|
|
|
|
} else { ... }
|
|
|
|
#endif
|
|
|
|
crde->crd_key = cse->key;
|
|
|
|
crde->crd_klen = cse->keylen * 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
crp->crp_ilen = cnop[req].len;
|
2008-05-24 20:28:58 +04:00
|
|
|
crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM |
|
2009-03-25 04:26:12 +03:00
|
|
|
(cnop[req].flags & COP_F_BATCH) | flags;
|
2008-04-11 02:48:42 +04:00
|
|
|
crp->crp_buf = (void *)&crp->uio;
|
|
|
|
crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_mcb;
|
|
|
|
crp->crp_sid = cse->sid;
|
|
|
|
crp->crp_opaque = (void *)cse;
|
|
|
|
crp->fcrp = fcr;
|
|
|
|
crp->dst = cnop[req].dst;
|
2009-03-25 04:26:12 +03:00
|
|
|
crp->len = cnop[req].len; /* input len, iov may be larger */
|
2008-04-11 02:48:42 +04:00
|
|
|
crp->mac = cnop[req].mac;
|
2009-03-25 04:26:12 +03:00
|
|
|
DPRINTF(("cryptodev_mop: iov_base %p dst %p len %d mac %p\n",
|
|
|
|
crp->uio.uio_iov[0].iov_base, crp->dst, crp->len,
|
|
|
|
crp->mac));
|
2008-04-11 02:48:42 +04:00
|
|
|
|
|
|
|
if (cnop[req].iv) {
|
|
|
|
if (crde == NULL) {
|
|
|
|
cnop[req].status = EINVAL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
|
|
|
|
cnop[req].status = EINVAL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
if ((error = copyin(cnop[req].iv, crp->tmp_iv,
|
|
|
|
cse->txform->blocksize))) {
|
|
|
|
cnop[req].status = EINVAL;
|
|
|
|
goto bail;
|
|
|
|
}
|
2008-05-24 20:28:58 +04:00
|
|
|
(void)memcpy(crde->crd_iv, crp->tmp_iv,
|
|
|
|
cse->txform->blocksize);
|
2008-04-11 02:48:42 +04:00
|
|
|
crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
|
|
|
|
crde->crd_skip = 0;
|
|
|
|
} else if (crde) {
|
|
|
|
if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
|
|
|
|
crde->crd_skip = 0;
|
|
|
|
} else {
|
|
|
|
crde->crd_flags |= CRD_F_IV_PRESENT;
|
|
|
|
crde->crd_skip = cse->txform->blocksize;
|
|
|
|
crde->crd_len -= cse->txform->blocksize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cnop[req].mac) {
|
|
|
|
if (crda == NULL) {
|
|
|
|
cnop[req].status = EINVAL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
crp->crp_mac=cse->tmp_mac;
|
|
|
|
}
|
|
|
|
cnop[req].reqid = atomic_inc_32_nv(&(fcr->requestid));
|
|
|
|
crp->crp_reqid = cnop[req].reqid;
|
|
|
|
crp->crp_usropaque = cnop[req].opaque;
|
|
|
|
#ifdef notyet
|
|
|
|
eagain:
|
|
|
|
#endif
|
|
|
|
cnop[req].status = crypto_dispatch(crp);
|
|
|
|
mutex_spin_enter(&crypto_mtx); /* XXX why mutex? */
|
|
|
|
|
|
|
|
switch (cnop[req].status) {
|
|
|
|
#ifdef notyet /* don't loop forever -- but EAGAIN not possible here yet */
|
|
|
|
case EAGAIN:
|
|
|
|
mutex_spin_exit(&crypto_mtx);
|
|
|
|
goto eagain;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DPRINTF(("cryptodev_op: not waiting, error.\n"));
|
|
|
|
mutex_spin_exit(&crypto_mtx);
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_spin_exit(&crypto_mtx);
|
|
|
|
bail:
|
|
|
|
if (cnop[req].status) {
|
|
|
|
if (crp) {
|
2009-03-25 04:26:12 +03:00
|
|
|
if (crp->uio.uio_iov[0].iov_base) {
|
|
|
|
kmem_free(crp->uio.uio_iov[0].iov_base,
|
|
|
|
crp->uio.uio_iov[0].iov_len);
|
2008-04-11 02:48:42 +04:00
|
|
|
}
|
2009-03-25 04:26:12 +03:00
|
|
|
crypto_freereq(crp);
|
2008-04-11 02:48:42 +04:00
|
|
|
}
|
|
|
|
error = 0;
|
|
|
|
}
|
|
|
|
}
|
2008-05-24 20:28:58 +04:00
|
|
|
return error;
|
2008-04-11 02:48:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cryptodev_mkey(struct fcrypt *fcr, struct crypt_n_kop *kop, int count)
|
|
|
|
{
|
|
|
|
struct cryptkop *krp = NULL;
|
|
|
|
int error = EINVAL;
|
|
|
|
int in, out, size, i, req;
|
|
|
|
|
|
|
|
for (req = 0; req < count; req++) {
|
2008-05-24 20:28:58 +04:00
|
|
|
if (kop[req].crk_iparams + kop[req].crk_oparams > CRK_MAXPARAM)
|
|
|
|
return EFBIG;
|
2008-04-11 02:48:42 +04:00
|
|
|
|
|
|
|
in = kop[req].crk_iparams;
|
|
|
|
out = kop[req].crk_oparams;
|
|
|
|
switch (kop[req].crk_op) {
|
|
|
|
case CRK_MOD_EXP:
|
|
|
|
if (in == 3 && out == 1)
|
|
|
|
break;
|
|
|
|
kop[req].crk_status = EINVAL;
|
|
|
|
continue;
|
|
|
|
case CRK_MOD_EXP_CRT:
|
|
|
|
if (in == 6 && out == 1)
|
|
|
|
break;
|
|
|
|
kop[req].crk_status = EINVAL;
|
|
|
|
continue;
|
|
|
|
case CRK_DSA_SIGN:
|
|
|
|
if (in == 5 && out == 2)
|
|
|
|
break;
|
|
|
|
kop[req].crk_status = EINVAL;
|
|
|
|
continue;
|
|
|
|
case CRK_DSA_VERIFY:
|
|
|
|
if (in == 7 && out == 0)
|
|
|
|
break;
|
|
|
|
kop[req].crk_status = EINVAL;
|
|
|
|
continue;
|
|
|
|
case CRK_DH_COMPUTE_KEY:
|
|
|
|
if (in == 3 && out == 1)
|
|
|
|
break;
|
|
|
|
kop[req].crk_status = EINVAL;
|
|
|
|
continue;
|
|
|
|
case CRK_MOD_ADD:
|
|
|
|
if (in == 3 && out == 1)
|
|
|
|
break;
|
|
|
|
kop[req].crk_status = EINVAL;
|
|
|
|
continue;
|
|
|
|
case CRK_MOD_ADDINV:
|
|
|
|
if (in == 2 && out == 1)
|
|
|
|
break;
|
|
|
|
kop[req].crk_status = EINVAL;
|
|
|
|
continue;
|
|
|
|
case CRK_MOD_SUB:
|
|
|
|
if (in == 3 && out == 1)
|
|
|
|
break;
|
|
|
|
kop[req].crk_status = EINVAL;
|
|
|
|
continue;
|
|
|
|
case CRK_MOD_MULT:
|
|
|
|
if (in == 3 && out == 1)
|
|
|
|
break;
|
|
|
|
kop[req].crk_status = EINVAL;
|
|
|
|
continue;
|
|
|
|
case CRK_MOD_MULTINV:
|
|
|
|
if (in == 2 && out == 1)
|
|
|
|
break;
|
|
|
|
kop[req].crk_status = EINVAL;
|
|
|
|
continue;
|
|
|
|
case CRK_MOD:
|
|
|
|
if (in == 2 && out == 1)
|
|
|
|
break;
|
|
|
|
kop[req].crk_status = EINVAL;
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
kop[req].crk_status = EINVAL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
krp = pool_get(&cryptkop_pool, PR_WAITOK);
|
2008-05-24 20:28:58 +04:00
|
|
|
(void)memset(krp, 0, sizeof *krp);
|
2008-04-11 02:48:42 +04:00
|
|
|
cv_init(&krp->krp_cv, "crykdev");
|
|
|
|
krp->krp_op = kop[req].crk_op;
|
|
|
|
krp->krp_status = kop[req].crk_status;
|
|
|
|
krp->krp_iparams = kop[req].crk_iparams;
|
|
|
|
krp->krp_oparams = kop[req].crk_oparams;
|
|
|
|
krp->krp_status = 0;
|
|
|
|
krp->krp_callback =
|
|
|
|
(int (*) (struct cryptkop *)) cryptodevkey_mcb;
|
2008-05-24 20:28:58 +04:00
|
|
|
(void)memcpy(krp->crk_param, kop[req].crk_param,
|
|
|
|
sizeof(kop[req].crk_param));
|
2008-04-11 02:48:42 +04:00
|
|
|
|
|
|
|
krp->krp_flags = CRYPTO_F_CBIMM;
|
|
|
|
|
|
|
|
for (i = 0; i < CRK_MAXPARAM; i++)
|
|
|
|
krp->krp_param[i].crp_nbits =
|
|
|
|
kop[req].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 =
|
|
|
|
kmem_alloc(size, KM_SLEEP);
|
|
|
|
if (i >= krp->krp_iparams)
|
|
|
|
continue;
|
2008-05-24 20:28:58 +04:00
|
|
|
kop[req].crk_status =
|
|
|
|
copyin(kop[req].crk_param[i].crp_p,
|
|
|
|
krp->krp_param[i].crp_p, size);
|
2008-04-11 02:48:42 +04:00
|
|
|
if (kop[req].crk_status)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
krp->fcrp = fcr;
|
|
|
|
|
|
|
|
kop[req].crk_reqid = atomic_inc_32_nv(&(fcr->requestid));
|
|
|
|
krp->krp_reqid = kop[req].crk_reqid;
|
|
|
|
krp->krp_usropaque = kop[req].crk_opaque;
|
|
|
|
|
|
|
|
kop[req].crk_status = crypto_kdispatch(krp);
|
|
|
|
if (kop[req].crk_status != 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if(kop[req].crk_status) {
|
|
|
|
if (krp) {
|
|
|
|
kop[req].crk_status = krp->krp_status;
|
|
|
|
for (i = 0; i < CRK_MAXPARAM; i++) {
|
|
|
|
struct crparam *kp =
|
|
|
|
&(krp->krp_param[i]);
|
|
|
|
if (kp->crp_p) {
|
|
|
|
size = (kp->crp_nbits + 7) / 8;
|
|
|
|
KASSERT(size > 0);
|
|
|
|
memset(kp->crp_p, 0, size);
|
|
|
|
kmem_free(kp->crp_p, size);
|
|
|
|
}
|
|
|
|
}
|
2008-11-18 15:59:58 +03:00
|
|
|
cv_destroy(&krp->krp_cv);
|
2008-04-11 02:48:42 +04:00
|
|
|
pool_put(&cryptkop_pool, krp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
error = 0;
|
|
|
|
}
|
|
|
|
DPRINTF(("cryptodev_key: error=0x%08x\n", error));
|
2008-05-24 20:28:58 +04:00
|
|
|
return error;
|
2008-04-11 02:48:42 +04:00
|
|
|
}
|
|
|
|
|
2009-03-25 04:26:12 +03:00
|
|
|
int
|
2008-11-18 15:59:58 +03:00
|
|
|
cryptodev_session(struct fcrypt *fcr, struct session_op *sop)
|
|
|
|
{
|
2008-04-11 02:48:42 +04:00
|
|
|
struct cryptoini cria, crie;
|
2009-03-25 04:26:12 +03:00
|
|
|
struct cryptoini cric; /* compressor */
|
|
|
|
struct cryptoini *crihead = NULL;
|
2008-04-11 02:48:42 +04:00
|
|
|
struct enc_xform *txform = NULL;
|
|
|
|
struct auth_hash *thash = NULL;
|
2009-03-25 04:26:12 +03:00
|
|
|
struct comp_algo *tcomp = NULL;
|
2008-04-11 02:48:42 +04:00
|
|
|
struct csession *cse;
|
|
|
|
u_int64_t sid;
|
|
|
|
int error = 0;
|
|
|
|
|
2009-03-25 04:26:12 +03:00
|
|
|
DPRINTF(("cryptodev_session() cipher=%d, mac=%d\n", sop->cipher, sop->mac));
|
|
|
|
|
2008-04-11 02:48:42 +04:00
|
|
|
/* XXX there must be a way to not embed the list of xforms here */
|
|
|
|
switch (sop->cipher) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case CRYPTO_DES_CBC:
|
|
|
|
txform = &enc_xform_des;
|
|
|
|
break;
|
|
|
|
case CRYPTO_3DES_CBC:
|
|
|
|
txform = &enc_xform_3des;
|
|
|
|
break;
|
|
|
|
case CRYPTO_BLF_CBC:
|
|
|
|
txform = &enc_xform_blf;
|
|
|
|
break;
|
|
|
|
case CRYPTO_CAST_CBC:
|
|
|
|
txform = &enc_xform_cast5;
|
2010-01-31 17:32:56 +03:00
|
|
|
break;
|
2008-04-11 02:48:42 +04:00
|
|
|
case CRYPTO_SKIPJACK_CBC:
|
|
|
|
txform = &enc_xform_skipjack;
|
|
|
|
break;
|
|
|
|
case CRYPTO_AES_CBC:
|
|
|
|
txform = &enc_xform_rijndael128;
|
|
|
|
break;
|
|
|
|
case CRYPTO_NULL_CBC:
|
|
|
|
txform = &enc_xform_null;
|
|
|
|
break;
|
|
|
|
case CRYPTO_ARC4:
|
|
|
|
txform = &enc_xform_arc4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DPRINTF(("Invalid cipher %d\n", sop->cipher));
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2009-03-25 04:26:12 +03:00
|
|
|
switch (sop->comp_alg) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case CRYPTO_DEFLATE_COMP:
|
|
|
|
tcomp = &comp_algo_deflate;
|
|
|
|
break;
|
|
|
|
case CRYPTO_GZIP_COMP:
|
|
|
|
tcomp = &comp_algo_gzip;
|
|
|
|
DPRINTF(("cryptodev_session() tcomp for GZIP\n"));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DPRINTF(("Invalid compression alg %d\n", sop->comp_alg));
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2008-04-11 02:48:42 +04:00
|
|
|
switch (sop->mac) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case CRYPTO_MD5_HMAC:
|
|
|
|
thash = &auth_hash_hmac_md5;
|
|
|
|
break;
|
|
|
|
case CRYPTO_SHA1_HMAC:
|
|
|
|
thash = &auth_hash_hmac_sha1;
|
|
|
|
break;
|
|
|
|
case CRYPTO_MD5_HMAC_96:
|
|
|
|
thash = &auth_hash_hmac_md5_96;
|
|
|
|
break;
|
|
|
|
case CRYPTO_SHA1_HMAC_96:
|
|
|
|
thash = &auth_hash_hmac_sha1_96;
|
|
|
|
break;
|
|
|
|
case CRYPTO_SHA2_HMAC:
|
|
|
|
/* XXX switching on key length seems questionable */
|
|
|
|
if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize) {
|
|
|
|
thash = &auth_hash_hmac_sha2_256;
|
|
|
|
} else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize) {
|
|
|
|
thash = &auth_hash_hmac_sha2_384;
|
|
|
|
} else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize) {
|
|
|
|
thash = &auth_hash_hmac_sha2_512;
|
|
|
|
} else {
|
|
|
|
DPRINTF(("Invalid mackeylen %d\n", sop->mackeylen));
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CRYPTO_RIPEMD160_HMAC:
|
|
|
|
thash = &auth_hash_hmac_ripemd_160;
|
|
|
|
break;
|
|
|
|
case CRYPTO_RIPEMD160_HMAC_96:
|
|
|
|
thash = &auth_hash_hmac_ripemd_160_96;
|
|
|
|
break;
|
|
|
|
case CRYPTO_MD5:
|
|
|
|
thash = &auth_hash_md5;
|
|
|
|
break;
|
|
|
|
case CRYPTO_SHA1:
|
|
|
|
thash = &auth_hash_sha1;
|
|
|
|
break;
|
|
|
|
case CRYPTO_NULL_HMAC:
|
|
|
|
thash = &auth_hash_null;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DPRINTF(("Invalid mac %d\n", sop->mac));
|
2008-05-24 20:28:58 +04:00
|
|
|
return EINVAL;
|
2008-04-11 02:48:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(&crie, 0, sizeof(crie));
|
|
|
|
memset(&cria, 0, sizeof(cria));
|
2009-03-25 04:26:12 +03:00
|
|
|
memset(&cric, 0, sizeof(cric));
|
|
|
|
|
|
|
|
if (tcomp) {
|
|
|
|
cric.cri_alg = tcomp->type;
|
|
|
|
cric.cri_klen = 0;
|
|
|
|
DPRINTF(("tcomp->type = %d\n", tcomp->type));
|
|
|
|
|
|
|
|
crihead = &cric;
|
|
|
|
if (thash) {
|
|
|
|
cric.cri_next = &cria;
|
|
|
|
} else if (txform) {
|
|
|
|
cric.cri_next = &crie;
|
|
|
|
}
|
|
|
|
}
|
2008-04-11 02:48:42 +04:00
|
|
|
|
|
|
|
if (txform) {
|
|
|
|
crie.cri_alg = txform->type;
|
|
|
|
crie.cri_klen = sop->keylen * 8;
|
|
|
|
if (sop->keylen > txform->maxkey ||
|
|
|
|
sop->keylen < txform->minkey) {
|
|
|
|
DPRINTF(("keylen %d not in [%d,%d]\n",
|
2008-05-24 20:28:58 +04:00
|
|
|
sop->keylen, txform->minkey, txform->maxkey));
|
|
|
|
error = EINVAL;
|
2008-04-11 02:48:42 +04:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
crie.cri_key = malloc(crie.cri_klen / 8, M_XDATA, M_WAITOK);
|
2008-05-24 20:28:58 +04:00
|
|
|
if ((error = copyin(sop->key, crie.cri_key, crie.cri_klen / 8)))
|
2008-04-11 02:48:42 +04:00
|
|
|
goto bail;
|
2009-03-25 04:26:12 +03:00
|
|
|
if (!crihead) {
|
|
|
|
crihead = &crie;
|
|
|
|
}
|
|
|
|
}
|
2008-04-11 02:48:42 +04:00
|
|
|
|
|
|
|
if (thash) {
|
|
|
|
cria.cri_alg = thash->type;
|
|
|
|
cria.cri_klen = sop->mackeylen * 8;
|
|
|
|
if (sop->mackeylen != thash->keysize) {
|
|
|
|
DPRINTF(("mackeylen %d != keysize %d\n",
|
2008-05-24 20:28:58 +04:00
|
|
|
sop->mackeylen, thash->keysize));
|
2008-04-11 02:48:42 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
if (cria.cri_klen) {
|
|
|
|
cria.cri_key = malloc(cria.cri_klen / 8, M_XDATA,
|
2008-05-24 20:28:58 +04:00
|
|
|
M_WAITOK);
|
2008-04-11 02:48:42 +04:00
|
|
|
if ((error = copyin(sop->mackey, cria.cri_key,
|
2008-05-24 20:28:58 +04:00
|
|
|
cria.cri_klen / 8))) {
|
2008-04-11 02:48:42 +04:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
}
|
2009-03-25 04:26:12 +03:00
|
|
|
if (txform)
|
|
|
|
cria.cri_next = &crie; /* XXX forces enc then hash? */
|
|
|
|
if (!crihead) {
|
|
|
|
crihead = &cria;
|
|
|
|
}
|
2008-04-11 02:48:42 +04:00
|
|
|
}
|
2009-03-25 04:26:12 +03:00
|
|
|
|
2008-04-11 02:48:42 +04:00
|
|
|
/* crypto_newsession requires that we hold the mutex. */
|
|
|
|
mutex_spin_enter(&crypto_mtx);
|
2009-03-25 04:26:12 +03:00
|
|
|
error = crypto_newsession(&sid, crihead, crypto_devallowsoft);
|
2008-04-11 02:48:42 +04:00
|
|
|
if (!error) {
|
2009-03-25 04:26:12 +03:00
|
|
|
DPRINTF(("cyrptodev_session: got session %d\n", (uint32_t)sid));
|
2008-04-11 02:48:42 +04:00
|
|
|
cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
|
2009-03-25 04:26:12 +03:00
|
|
|
cria.cri_key, cria.cri_klen, (txform ? sop->cipher : 0), sop->mac,
|
|
|
|
(tcomp ? sop->comp_alg : 0), txform, thash, tcomp);
|
2008-04-11 02:48:42 +04:00
|
|
|
if (cse != NULL) {
|
|
|
|
sop->ses = cse->ses;
|
|
|
|
} else {
|
|
|
|
DPRINTF(("csecreate failed\n"));
|
|
|
|
crypto_freesession(sid);
|
|
|
|
error = EINVAL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DPRINTF(("SIOCSESSION violates kernel parameters %d\n",
|
2008-05-24 20:28:58 +04:00
|
|
|
error));
|
2008-04-11 02:48:42 +04:00
|
|
|
}
|
|
|
|
mutex_spin_exit(&crypto_mtx);
|
|
|
|
bail:
|
|
|
|
if (error) {
|
|
|
|
if (crie.cri_key) {
|
|
|
|
memset(crie.cri_key, 0, crie.cri_klen / 8);
|
|
|
|
free(crie.cri_key, M_XDATA);
|
|
|
|
}
|
|
|
|
if (cria.cri_key) {
|
|
|
|
memset(cria.cri_key, 0, cria.cri_klen / 8);
|
|
|
|
free(cria.cri_key, M_XDATA);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2009-03-25 04:26:12 +03:00
|
|
|
int
|
2008-04-11 02:48:42 +04:00
|
|
|
cryptodev_msession(struct fcrypt *fcr, struct session_n_op *sn_ops,
|
|
|
|
int count)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++, sn_ops++) {
|
|
|
|
struct session_op s_op;
|
|
|
|
s_op.cipher = sn_ops->cipher;
|
|
|
|
s_op.mac = sn_ops->mac;
|
|
|
|
s_op.keylen = sn_ops->keylen;
|
|
|
|
s_op.key = sn_ops->key;
|
|
|
|
s_op.mackeylen = sn_ops->mackeylen;
|
|
|
|
s_op.mackey = sn_ops->mackey;
|
|
|
|
|
|
|
|
sn_ops->status = cryptodev_session(fcr, &s_op);
|
|
|
|
sn_ops->ses = s_op.ses;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cryptodev_msessionfin(struct fcrypt *fcr, int count, u_int32_t *sesid)
|
|
|
|
{
|
|
|
|
struct csession *cse;
|
|
|
|
int req, error = 0;
|
|
|
|
|
|
|
|
mutex_spin_enter(&crypto_mtx);
|
|
|
|
for(req = 0; req < count; req++) {
|
|
|
|
cse = csefind(fcr, sesid[req]);
|
|
|
|
if (cse == NULL)
|
|
|
|
continue;
|
|
|
|
csedelete(fcr, cse);
|
|
|
|
error = csefree(cse);
|
|
|
|
}
|
|
|
|
mutex_spin_exit(&crypto_mtx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-05-24 20:28:58 +04:00
|
|
|
* collect as many completed requests as are availble, or count completed
|
|
|
|
* requests whichever is less.
|
2008-04-11 02:48:42 +04:00
|
|
|
* return the number of requests.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
cryptodev_getmstatus(struct fcrypt *fcr, struct crypt_result *crypt_res,
|
2008-05-24 20:28:58 +04:00
|
|
|
int count)
|
2008-04-11 02:48:42 +04:00
|
|
|
{
|
|
|
|
struct cryptop *crp = NULL;
|
|
|
|
struct cryptkop *krp = NULL;
|
|
|
|
struct csession *cse;
|
|
|
|
int i, size, req = 0;
|
|
|
|
int completed=0;
|
|
|
|
|
2009-03-25 04:26:12 +03:00
|
|
|
/* On queue so nobody else can grab them
|
|
|
|
* and copyout can be delayed-- no locking */
|
|
|
|
TAILQ_HEAD(, cryptop) crp_delfree_q =
|
|
|
|
TAILQ_HEAD_INITIALIZER(crp_delfree_q);
|
|
|
|
TAILQ_HEAD(, cryptkop) krp_delfree_q =
|
|
|
|
TAILQ_HEAD_INITIALIZER(krp_delfree_q);
|
2008-04-11 02:48:42 +04:00
|
|
|
|
|
|
|
/* at this point we do not know which response user is requesting for
|
|
|
|
* (symmetric or asymmetric) so we copyout one from each i.e if the
|
|
|
|
* count is 2 then 1 from symmetric and 1 from asymmetric queue and
|
|
|
|
* if 3 then 2 symmetric and 1 asymmetric and so on */
|
2009-03-25 04:26:12 +03:00
|
|
|
|
|
|
|
/* pull off a list of requests while protected from changes */
|
|
|
|
mutex_spin_enter(&crypto_mtx);
|
|
|
|
while (req < count) {
|
2008-04-11 02:48:42 +04:00
|
|
|
crp = TAILQ_FIRST(&fcr->crp_ret_mq);
|
2009-03-25 04:26:12 +03:00
|
|
|
if (crp) {
|
|
|
|
TAILQ_REMOVE(&fcr->crp_ret_mq, crp, crp_next);
|
|
|
|
TAILQ_INSERT_TAIL(&crp_delfree_q, crp, crp_next);
|
2008-04-11 02:48:42 +04:00
|
|
|
cse = (struct csession *)crp->crp_opaque;
|
2009-03-25 04:26:12 +03:00
|
|
|
|
|
|
|
/* see if the session is still valid */
|
2008-04-11 02:48:42 +04:00
|
|
|
cse = csefind(fcr, cse->ses);
|
2009-03-25 04:26:12 +03:00
|
|
|
if (cse != NULL) {
|
|
|
|
crypt_res[req].status = 0;
|
|
|
|
} else {
|
2008-04-11 02:48:42 +04:00
|
|
|
DPRINTF(("csefind failed\n"));
|
|
|
|
crypt_res[req].status = EINVAL;
|
2009-03-25 04:26:12 +03:00
|
|
|
}
|
|
|
|
req++;
|
|
|
|
}
|
|
|
|
if(req < count) {
|
|
|
|
crypt_res[req].status = 0;
|
|
|
|
krp = TAILQ_FIRST(&fcr->crp_ret_mkq);
|
|
|
|
if (krp) {
|
|
|
|
TAILQ_REMOVE(&fcr->crp_ret_mkq, krp, krp_next);
|
|
|
|
TAILQ_INSERT_TAIL(&krp_delfree_q, krp, krp_next);
|
|
|
|
req++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mutex_spin_exit(&crypto_mtx);
|
|
|
|
|
|
|
|
/* now do all the work outside the mutex */
|
|
|
|
for(req=0; req < count ;) {
|
|
|
|
crp = TAILQ_FIRST(&crp_delfree_q);
|
|
|
|
if (crp) {
|
|
|
|
if (crypt_res[req].status != 0) {
|
|
|
|
/* csefind failed during collection */
|
2008-04-11 02:48:42 +04:00
|
|
|
goto bail;
|
|
|
|
}
|
2009-03-25 04:26:12 +03:00
|
|
|
cse = (struct csession *)crp->crp_opaque;
|
|
|
|
crypt_res[req].reqid = crp->crp_reqid;
|
|
|
|
crypt_res[req].opaque = crp->crp_usropaque;
|
|
|
|
completed++;
|
2008-04-11 02:48:42 +04:00
|
|
|
|
|
|
|
if (crp->crp_etype != 0) {
|
|
|
|
crypt_res[req].status = crp->crp_etype;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cse->error) {
|
|
|
|
crypt_res[req].status = cse->error;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2008-05-24 20:28:58 +04:00
|
|
|
if (crp->dst && (crypt_res[req].status =
|
|
|
|
copyout(crp->uio.uio_iov[0].iov_base, crp->dst,
|
|
|
|
crp->len)))
|
2008-04-11 02:48:42 +04:00
|
|
|
goto bail;
|
|
|
|
|
2008-05-24 20:28:58 +04:00
|
|
|
if (crp->mac && (crypt_res[req].status =
|
|
|
|
copyout(crp->crp_mac, crp->mac,
|
|
|
|
cse->thash->authsize)))
|
2008-04-11 02:48:42 +04:00
|
|
|
goto bail;
|
2009-03-25 04:26:12 +03:00
|
|
|
|
2008-04-11 02:48:42 +04:00
|
|
|
bail:
|
2009-03-25 04:26:12 +03:00
|
|
|
TAILQ_REMOVE(&crp_delfree_q, crp, crp_next);
|
|
|
|
kmem_free(crp->uio.uio_iov[0].iov_base,
|
|
|
|
crp->uio.uio_iov[0].iov_len);
|
|
|
|
crypto_freereq(crp);
|
2008-04-11 02:48:42 +04:00
|
|
|
req++;
|
|
|
|
}
|
|
|
|
|
2009-03-25 04:26:12 +03:00
|
|
|
if (req < count) {
|
|
|
|
krp = TAILQ_FIRST(&krp_delfree_q);
|
2008-04-11 02:48:42 +04:00
|
|
|
if (krp) {
|
|
|
|
crypt_res[req].reqid = krp->krp_reqid;
|
|
|
|
crypt_res[req].opaque = krp->krp_usropaque;
|
|
|
|
completed++;
|
|
|
|
if (krp->krp_status != 0) {
|
|
|
|
DPRINTF(("cryptodev_key: "
|
2008-05-24 20:28:58 +04:00
|
|
|
"krp->krp_status 0x%08x\n",
|
|
|
|
krp->krp_status));
|
|
|
|
crypt_res[req].status = krp->krp_status;
|
2008-04-11 02:48:42 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = krp->krp_iparams; i < krp->krp_iparams
|
2008-05-24 20:28:58 +04:00
|
|
|
+ krp->krp_oparams; i++) {
|
2008-04-11 02:48:42 +04:00
|
|
|
size = (krp->krp_param[i].crp_nbits
|
2008-05-24 20:28:58 +04:00
|
|
|
+ 7) / 8;
|
2008-04-11 02:48:42 +04:00
|
|
|
if (size == 0)
|
|
|
|
continue;
|
|
|
|
crypt_res[req].status = copyout
|
|
|
|
(krp->krp_param[i].crp_p,
|
|
|
|
krp->crk_param[i].crp_p, size);
|
|
|
|
if (crypt_res[req].status) {
|
|
|
|
DPRINTF(("cryptodev_key: "
|
2008-05-24 20:28:58 +04:00
|
|
|
"copyout oparam %d failed, "
|
|
|
|
"error=%d\n",
|
|
|
|
i - krp->krp_iparams,
|
|
|
|
crypt_res[req].status));
|
2008-04-11 02:48:42 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fail:
|
2009-03-25 04:26:12 +03:00
|
|
|
TAILQ_REMOVE(&krp_delfree_q, krp, krp_next);
|
2008-04-11 02:48:42 +04:00
|
|
|
/* not sure what to do for this */
|
|
|
|
/* kop[req].crk_status = krp->krp_status; */
|
2009-03-25 04:26:12 +03:00
|
|
|
for (i = 0; i < CRK_MAXPARAM; i++) {
|
|
|
|
struct crparam *kp = &(krp->krp_param[i]);
|
|
|
|
if (kp->crp_p) {
|
|
|
|
size = (kp->crp_nbits + 7) / 8;
|
|
|
|
KASSERT(size > 0);
|
|
|
|
(void)memset(kp->crp_p, 0, size);
|
|
|
|
kmem_free(kp->crp_p, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cv_destroy(&krp->krp_cv);
|
|
|
|
pool_put(&cryptkop_pool, krp);
|
|
|
|
req++;
|
2008-04-11 02:48:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return completed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cryptodev_getstatus (struct fcrypt *fcr, struct crypt_result *crypt_res)
|
|
|
|
{
|
2008-11-18 15:59:58 +03:00
|
|
|
struct cryptop *crp = NULL, *cnext;
|
|
|
|
struct cryptkop *krp = NULL, *knext;
|
2008-04-11 02:48:42 +04:00
|
|
|
struct csession *cse;
|
|
|
|
int i, size, req = 0;
|
|
|
|
|
|
|
|
mutex_spin_enter(&crypto_mtx);
|
|
|
|
/* Here we dont know for which request the user is requesting the
|
|
|
|
* response so checking in both the queues */
|
2008-11-18 15:59:58 +03:00
|
|
|
TAILQ_FOREACH_SAFE(crp, &fcr->crp_ret_mq, crp_next, cnext) {
|
2008-04-11 02:48:42 +04:00
|
|
|
if(crp && (crp->crp_reqid == crypt_res->reqid)) {
|
|
|
|
cse = (struct csession *)crp->crp_opaque;
|
|
|
|
crypt_res->opaque = crp->crp_usropaque;
|
|
|
|
cse = csefind(fcr, cse->ses);
|
|
|
|
if (cse == NULL) {
|
|
|
|
DPRINTF(("csefind failed\n"));
|
|
|
|
crypt_res->status = EINVAL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (crp->crp_etype != 0) {
|
|
|
|
crypt_res->status = crp->crp_etype;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cse->error) {
|
|
|
|
crypt_res->status = cse->error;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2008-05-24 20:28:58 +04:00
|
|
|
if (crp->dst && (crypt_res->status =
|
|
|
|
copyout(crp->uio.uio_iov[0].iov_base,
|
|
|
|
crp->dst, crp->len)))
|
2008-04-11 02:48:42 +04:00
|
|
|
goto bail;
|
|
|
|
|
2008-05-24 20:28:58 +04:00
|
|
|
if (crp->mac && (crypt_res->status =
|
|
|
|
copyout(crp->crp_mac, crp->mac,
|
|
|
|
cse->thash->authsize)))
|
2008-04-11 02:48:42 +04:00
|
|
|
goto bail;
|
|
|
|
bail:
|
|
|
|
TAILQ_REMOVE(&fcr->crp_ret_mq, crp, crp_next);
|
|
|
|
|
|
|
|
mutex_spin_exit(&crypto_mtx);
|
|
|
|
crypto_freereq(crp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-18 15:59:58 +03:00
|
|
|
TAILQ_FOREACH_SAFE(krp, &fcr->crp_ret_mkq, krp_next, knext) {
|
2008-04-11 02:48:42 +04:00
|
|
|
if(krp && (krp->krp_reqid == crypt_res->reqid)) {
|
|
|
|
crypt_res[req].opaque = krp->krp_usropaque;
|
|
|
|
if (krp->krp_status != 0) {
|
|
|
|
DPRINTF(("cryptodev_key: "
|
2008-05-24 20:28:58 +04:00
|
|
|
"krp->krp_status 0x%08x\n",
|
|
|
|
krp->krp_status));
|
2008-04-11 02:48:42 +04:00
|
|
|
crypt_res[req].status = krp->krp_status;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2008-05-24 20:28:58 +04:00
|
|
|
for (i = krp->krp_iparams; i < krp->krp_iparams +
|
|
|
|
krp->krp_oparams; i++) {
|
2008-04-11 02:48:42 +04:00
|
|
|
size = (krp->krp_param[i].crp_nbits + 7) / 8;
|
|
|
|
if (size == 0)
|
|
|
|
continue;
|
2008-05-24 20:28:58 +04:00
|
|
|
crypt_res[req].status = copyout(
|
|
|
|
krp->krp_param[i].crp_p,
|
|
|
|
krp->crk_param[i].crp_p, size);
|
2008-04-11 02:48:42 +04:00
|
|
|
if (crypt_res[req].status) {
|
|
|
|
DPRINTF(("cryptodev_key: copyout oparam"
|
2008-05-24 20:28:58 +04:00
|
|
|
"%d failed, error=%d\n",
|
|
|
|
i - krp->krp_iparams,
|
|
|
|
crypt_res[req].status));
|
2008-04-11 02:48:42 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fail:
|
|
|
|
TAILQ_REMOVE(&fcr->crp_ret_mkq, krp, krp_next);
|
|
|
|
mutex_spin_exit(&crypto_mtx);
|
|
|
|
/* not sure what to do for this */
|
|
|
|
/* kop[req].crk_status = krp->krp_status; */
|
|
|
|
for (i = 0; i < CRK_MAXPARAM; i++) {
|
|
|
|
struct crparam *kp = &(krp->krp_param[i]);
|
|
|
|
if (kp->crp_p) {
|
|
|
|
size = (kp->crp_nbits + 7) / 8;
|
|
|
|
KASSERT(size > 0);
|
|
|
|
memset(kp->crp_p, 0, size);
|
|
|
|
kmem_free(kp->crp_p, size);
|
|
|
|
}
|
|
|
|
}
|
2008-11-18 15:59:58 +03:00
|
|
|
cv_destroy(&krp->krp_cv);
|
2008-04-11 02:48:42 +04:00
|
|
|
pool_put(&cryptkop_pool, krp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mutex_spin_exit(&crypto_mtx);
|
|
|
|
return EINPROGRESS;
|
|
|
|
}
|
|
|
|
|
2009-04-11 19:47:33 +04:00
|
|
|
static int
|
|
|
|
cryptof_stat(struct file *fp, struct stat *st)
|
|
|
|
{
|
|
|
|
struct fcrypt *fcr = fp->f_data;
|
|
|
|
|
|
|
|
(void)memset(st, 0, sizeof(st));
|
2009-04-12 03:05:26 +04:00
|
|
|
|
|
|
|
mutex_spin_enter(&crypto_mtx);
|
2009-04-11 19:47:33 +04:00
|
|
|
st->st_dev = makedev(cdevsw_lookup_major(&crypto_cdevsw), fcr->sesn);
|
|
|
|
st->st_atimespec = fcr->atime;
|
|
|
|
st->st_mtimespec = fcr->mtime;
|
|
|
|
st->st_ctimespec = st->st_birthtimespec = fcr->btime;
|
2009-04-12 03:05:26 +04:00
|
|
|
st->st_uid = kauth_cred_geteuid(fp->f_cred);
|
|
|
|
st->st_gid = kauth_cred_getegid(fp->f_cred);
|
|
|
|
mutex_spin_exit(&crypto_mtx);
|
|
|
|
|
2009-04-11 19:47:33 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-11 02:48:42 +04:00
|
|
|
static int
|
|
|
|
cryptof_poll(struct file *fp, int events)
|
|
|
|
{
|
|
|
|
struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
|
2008-04-11 14:28:10 +04:00
|
|
|
int revents = 0;
|
2008-04-11 02:48:42 +04:00
|
|
|
|
|
|
|
if (!(events & (POLLIN | POLLRDNORM))) {
|
|
|
|
/* only support read and POLLIN */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-11 14:28:10 +04:00
|
|
|
mutex_spin_enter(&crypto_mtx);
|
2008-04-11 02:48:42 +04:00
|
|
|
if (TAILQ_EMPTY(&fcr->crp_ret_mq) && TAILQ_EMPTY(&fcr->crp_ret_mkq)) {
|
2008-04-11 14:28:10 +04:00
|
|
|
/* no completed requests pending, save the poll for later */
|
2008-04-11 02:48:42 +04:00
|
|
|
selrecord(curlwp, &fcr->sinfo);
|
|
|
|
} else {
|
|
|
|
/* let the app(s) know that there are completed requests */
|
2008-04-11 14:28:10 +04:00
|
|
|
revents = events & (POLLIN | POLLRDNORM);
|
2008-04-11 02:48:42 +04:00
|
|
|
}
|
2008-04-11 14:28:10 +04:00
|
|
|
mutex_spin_exit(&crypto_mtx);
|
|
|
|
|
|
|
|
return revents;
|
2008-04-11 02:48:42 +04:00
|
|
|
}
|
|
|
|
|
2005-11-25 19:16:46 +03:00
|
|
|
/*
|
|
|
|
* Pseudo-device initialization routine for /dev/crypto
|
|
|
|
*/
|
|
|
|
void cryptoattach(int);
|
|
|
|
|
|
|
|
void
|
2006-11-16 04:32:37 +03:00
|
|
|
cryptoattach(int num)
|
2005-11-25 19:16:46 +03:00
|
|
|
{
|
2008-01-26 03:04:27 +03:00
|
|
|
pool_init(&fcrpl, sizeof(struct fcrypt), 0, 0, 0, "fcrpl",
|
2008-05-24 20:28:58 +04:00
|
|
|
NULL, IPL_NET); /* XXX IPL_NET ("splcrypto") */
|
2008-01-26 03:04:27 +03:00
|
|
|
pool_init(&csepl, sizeof(struct csession), 0, 0, 0, "csepl",
|
2008-05-24 20:28:58 +04:00
|
|
|
NULL, IPL_NET); /* XXX IPL_NET ("splcrypto") */
|
2008-01-26 03:04:27 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Preallocate space for 64 users, with 5 sessions each.
|
|
|
|
* (consider that a TLS protocol session requires at least
|
|
|
|
* 3DES, MD5, and SHA1 (both hashes are used in the PRF) for
|
|
|
|
* the negotiation, plus HMAC_SHA1 for the actual SSL records,
|
|
|
|
* consuming one session here for each algorithm.
|
|
|
|
*/
|
|
|
|
pool_prime(&fcrpl, 64);
|
|
|
|
pool_prime(&csepl, 64 * 5);
|
2005-11-25 19:16:46 +03:00
|
|
|
}
|