2002-10-04 20:31:23 +00:00
|
|
|
/* $OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $ */
|
|
|
|
|
2005-01-07 02:29:27 +00:00
|
|
|
/*-
|
2002-10-04 20:31:23 +00:00
|
|
|
* Copyright (c) 2001 Theo de Raadt
|
2007-03-21 03:42:51 +00:00
|
|
|
* Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
|
2014-12-12 19:56:36 +00:00
|
|
|
* Copyright (c) 2014 The FreeBSD Foundation
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Portions of this software were developed by John-Mark Gurney
|
|
|
|
* under sponsorship of the FreeBSD Foundation and
|
|
|
|
* Rubicon Communications, LLC (Netgate).
|
2002-10-04 20:31:23 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2003-06-11 05:57:50 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2002-10-04 20:31:23 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/filedesc.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/random.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/kernel.h>
|
2004-05-30 20:27:19 +00:00
|
|
|
#include <sys/module.h>
|
2002-10-04 20:31:23 +00:00
|
|
|
#include <sys/fcntl.h>
|
2007-03-21 03:42:51 +00:00
|
|
|
#include <sys/bus.h>
|
2014-09-22 17:32:27 +00:00
|
|
|
#include <sys/user.h>
|
2014-12-12 19:56:36 +00:00
|
|
|
#include <sys/sdt.h>
|
2002-10-04 20:31:23 +00:00
|
|
|
|
|
|
|
#include <opencrypto/cryptodev.h>
|
|
|
|
#include <opencrypto/xform.h>
|
|
|
|
|
2014-12-12 19:56:36 +00:00
|
|
|
SDT_PROVIDER_DECLARE(opencrypto);
|
|
|
|
|
|
|
|
SDT_PROBE_DEFINE1(opencrypto, dev, ioctl, error, "int"/*line number*/);
|
|
|
|
|
2010-07-29 20:42:20 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <compat/freebsd32/freebsd32.h>
|
|
|
|
|
|
|
|
struct session_op32 {
|
|
|
|
u_int32_t cipher;
|
|
|
|
u_int32_t mac;
|
|
|
|
u_int32_t keylen;
|
|
|
|
u_int32_t key;
|
|
|
|
int mackeylen;
|
|
|
|
u_int32_t mackey;
|
|
|
|
u_int32_t ses;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct session2_op32 {
|
|
|
|
u_int32_t cipher;
|
|
|
|
u_int32_t mac;
|
|
|
|
u_int32_t keylen;
|
|
|
|
u_int32_t key;
|
|
|
|
int mackeylen;
|
|
|
|
u_int32_t mackey;
|
|
|
|
u_int32_t ses;
|
|
|
|
int crid;
|
|
|
|
int pad[4];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct crypt_op32 {
|
|
|
|
u_int32_t ses;
|
|
|
|
u_int16_t op;
|
|
|
|
u_int16_t flags;
|
|
|
|
u_int len;
|
|
|
|
u_int32_t src, dst;
|
|
|
|
u_int32_t mac;
|
|
|
|
u_int32_t iv;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct crparam32 {
|
|
|
|
u_int32_t crp_p;
|
|
|
|
u_int crp_nbits;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct crypt_kop32 {
|
|
|
|
u_int crk_op;
|
|
|
|
u_int crk_status;
|
|
|
|
u_short crk_iparams;
|
|
|
|
u_short crk_oparams;
|
|
|
|
u_int crk_crid;
|
|
|
|
struct crparam32 crk_param[CRK_MAXPARAM];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct cryptotstat32 {
|
|
|
|
struct timespec32 acc;
|
|
|
|
struct timespec32 min;
|
|
|
|
struct timespec32 max;
|
|
|
|
u_int32_t count;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct cryptostats32 {
|
|
|
|
u_int32_t cs_ops;
|
|
|
|
u_int32_t cs_errs;
|
|
|
|
u_int32_t cs_kops;
|
|
|
|
u_int32_t cs_kerrs;
|
|
|
|
u_int32_t cs_intrs;
|
|
|
|
u_int32_t cs_rets;
|
|
|
|
u_int32_t cs_blocks;
|
|
|
|
u_int32_t cs_kblocks;
|
|
|
|
struct cryptotstat32 cs_invoke;
|
|
|
|
struct cryptotstat32 cs_done;
|
|
|
|
struct cryptotstat32 cs_cb;
|
|
|
|
struct cryptotstat32 cs_finis;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define CIOCGSESSION32 _IOWR('c', 101, struct session_op32)
|
|
|
|
#define CIOCCRYPT32 _IOWR('c', 103, struct crypt_op32)
|
|
|
|
#define CIOCKEY32 _IOWR('c', 104, struct crypt_kop32)
|
|
|
|
#define CIOCGSESSION232 _IOWR('c', 106, struct session2_op32)
|
|
|
|
#define CIOCKEY232 _IOWR('c', 107, struct crypt_kop32)
|
|
|
|
|
|
|
|
static void
|
|
|
|
session_op_from_32(const struct session_op32 *from, struct session_op *to)
|
|
|
|
{
|
|
|
|
|
|
|
|
CP(*from, *to, cipher);
|
|
|
|
CP(*from, *to, mac);
|
|
|
|
CP(*from, *to, keylen);
|
|
|
|
PTRIN_CP(*from, *to, key);
|
|
|
|
CP(*from, *to, mackeylen);
|
|
|
|
PTRIN_CP(*from, *to, mackey);
|
|
|
|
CP(*from, *to, ses);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
session2_op_from_32(const struct session2_op32 *from, struct session2_op *to)
|
|
|
|
{
|
|
|
|
|
|
|
|
session_op_from_32((const struct session_op32 *)from,
|
|
|
|
(struct session_op *)to);
|
|
|
|
CP(*from, *to, crid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
session_op_to_32(const struct session_op *from, struct session_op32 *to)
|
|
|
|
{
|
|
|
|
|
|
|
|
CP(*from, *to, cipher);
|
|
|
|
CP(*from, *to, mac);
|
|
|
|
CP(*from, *to, keylen);
|
|
|
|
PTROUT_CP(*from, *to, key);
|
|
|
|
CP(*from, *to, mackeylen);
|
|
|
|
PTROUT_CP(*from, *to, mackey);
|
|
|
|
CP(*from, *to, ses);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
session2_op_to_32(const struct session2_op *from, struct session2_op32 *to)
|
|
|
|
{
|
|
|
|
|
|
|
|
session_op_to_32((const struct session_op *)from,
|
|
|
|
(struct session_op32 *)to);
|
|
|
|
CP(*from, *to, crid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
crypt_op_from_32(const struct crypt_op32 *from, struct crypt_op *to)
|
|
|
|
{
|
|
|
|
|
|
|
|
CP(*from, *to, ses);
|
|
|
|
CP(*from, *to, op);
|
|
|
|
CP(*from, *to, flags);
|
|
|
|
CP(*from, *to, len);
|
|
|
|
PTRIN_CP(*from, *to, src);
|
|
|
|
PTRIN_CP(*from, *to, dst);
|
|
|
|
PTRIN_CP(*from, *to, mac);
|
|
|
|
PTRIN_CP(*from, *to, iv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
crypt_op_to_32(const struct crypt_op *from, struct crypt_op32 *to)
|
|
|
|
{
|
|
|
|
|
|
|
|
CP(*from, *to, ses);
|
|
|
|
CP(*from, *to, op);
|
|
|
|
CP(*from, *to, flags);
|
|
|
|
CP(*from, *to, len);
|
|
|
|
PTROUT_CP(*from, *to, src);
|
|
|
|
PTROUT_CP(*from, *to, dst);
|
|
|
|
PTROUT_CP(*from, *to, mac);
|
|
|
|
PTROUT_CP(*from, *to, iv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
crparam_from_32(const struct crparam32 *from, struct crparam *to)
|
|
|
|
{
|
|
|
|
|
|
|
|
PTRIN_CP(*from, *to, crp_p);
|
|
|
|
CP(*from, *to, crp_nbits);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
crparam_to_32(const struct crparam *from, struct crparam32 *to)
|
|
|
|
{
|
|
|
|
|
|
|
|
PTROUT_CP(*from, *to, crp_p);
|
|
|
|
CP(*from, *to, crp_nbits);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
crypt_kop_from_32(const struct crypt_kop32 *from, struct crypt_kop *to)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
CP(*from, *to, crk_op);
|
|
|
|
CP(*from, *to, crk_status);
|
|
|
|
CP(*from, *to, crk_iparams);
|
|
|
|
CP(*from, *to, crk_oparams);
|
|
|
|
CP(*from, *to, crk_crid);
|
|
|
|
for (i = 0; i < CRK_MAXPARAM; i++)
|
|
|
|
crparam_from_32(&from->crk_param[i], &to->crk_param[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
crypt_kop_to_32(const struct crypt_kop *from, struct crypt_kop32 *to)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
CP(*from, *to, crk_op);
|
|
|
|
CP(*from, *to, crk_status);
|
|
|
|
CP(*from, *to, crk_iparams);
|
|
|
|
CP(*from, *to, crk_oparams);
|
|
|
|
CP(*from, *to, crk_crid);
|
|
|
|
for (i = 0; i < CRK_MAXPARAM; i++)
|
|
|
|
crparam_to_32(&from->crk_param[i], &to->crk_param[i]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-10-04 20:31:23 +00:00
|
|
|
struct csession {
|
|
|
|
TAILQ_ENTRY(csession) next;
|
2018-07-18 00:56:25 +00:00
|
|
|
crypto_session_t cses;
|
2002-10-04 20:31:23 +00:00
|
|
|
u_int32_t ses;
|
2003-06-02 23:28:06 +00:00
|
|
|
struct mtx lock; /* for op submission */
|
2002-10-04 20:31:23 +00:00
|
|
|
|
|
|
|
u_int32_t cipher;
|
|
|
|
struct enc_xform *txform;
|
|
|
|
u_int32_t mac;
|
|
|
|
struct auth_hash *thash;
|
|
|
|
|
|
|
|
caddr_t key;
|
|
|
|
int keylen;
|
|
|
|
|
|
|
|
caddr_t mackey;
|
|
|
|
int mackeylen;
|
2018-01-26 23:21:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct cryptop_data {
|
|
|
|
struct csession *cse;
|
2002-10-04 20:31:23 +00:00
|
|
|
|
2018-01-26 23:21:50 +00:00
|
|
|
struct iovec iovec[1];
|
2002-10-04 20:31:23 +00:00
|
|
|
struct uio uio;
|
2018-01-26 23:21:50 +00:00
|
|
|
bool done;
|
2002-10-04 20:31:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct fcrypt {
|
|
|
|
TAILQ_HEAD(csessionlist, csession) csessions;
|
|
|
|
int sesn;
|
|
|
|
};
|
|
|
|
|
2019-06-11 23:28:07 +00:00
|
|
|
static struct timeval warninterval = { .tv_sec = 60, .tv_usec = 0 };
|
|
|
|
SYSCTL_TIMEVAL_SEC(_kern, OID_AUTO, cryptodev_warn_interval, CTLFLAG_RW,
|
|
|
|
&warninterval,
|
|
|
|
"Delay in seconds between warnings of deprecated /dev/crypto algorithms");
|
|
|
|
|
2002-10-04 20:31:23 +00:00
|
|
|
static int cryptof_ioctl(struct file *, u_long, void *,
|
|
|
|
struct ucred *, struct thread *);
|
|
|
|
static int cryptof_stat(struct file *, struct stat *,
|
|
|
|
struct ucred *, struct thread *);
|
|
|
|
static int cryptof_close(struct file *, struct thread *);
|
2014-09-22 16:20:47 +00:00
|
|
|
static int cryptof_fill_kinfo(struct file *, struct kinfo_file *,
|
|
|
|
struct filedesc *);
|
2002-10-04 20:31:23 +00:00
|
|
|
|
|
|
|
static struct fileops cryptofops = {
|
2014-09-12 21:29:10 +00:00
|
|
|
.fo_read = invfo_rdwr,
|
|
|
|
.fo_write = invfo_rdwr,
|
|
|
|
.fo_truncate = invfo_truncate,
|
2003-06-18 18:16:40 +00:00
|
|
|
.fo_ioctl = cryptof_ioctl,
|
2014-09-12 21:29:10 +00:00
|
|
|
.fo_poll = invfo_poll,
|
|
|
|
.fo_kqfilter = invfo_kqfilter,
|
2003-06-18 18:16:40 +00:00
|
|
|
.fo_stat = cryptof_stat,
|
2011-08-16 20:07:47 +00:00
|
|
|
.fo_close = cryptof_close,
|
|
|
|
.fo_chmod = invfo_chmod,
|
|
|
|
.fo_chown = invfo_chown,
|
2013-08-15 07:54:31 +00:00
|
|
|
.fo_sendfile = invfo_sendfile,
|
2014-09-22 16:20:47 +00:00
|
|
|
.fo_fill_kinfo = cryptof_fill_kinfo,
|
2002-10-04 20:31:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct csession *csefind(struct fcrypt *, u_int);
|
|
|
|
static int csedelete(struct fcrypt *, struct csession *);
|
|
|
|
static struct csession *cseadd(struct fcrypt *, struct csession *);
|
2018-07-13 23:46:07 +00:00
|
|
|
static struct csession *csecreate(struct fcrypt *, crypto_session_t, caddr_t,
|
2002-10-04 20:31:23 +00:00
|
|
|
u_int64_t, caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
|
|
|
|
struct auth_hash *);
|
2018-07-18 00:56:25 +00:00
|
|
|
static void csefree(struct csession *);
|
2002-10-04 20:31:23 +00:00
|
|
|
|
|
|
|
static int cryptodev_op(struct csession *, struct crypt_op *,
|
|
|
|
struct ucred *, struct thread *td);
|
2014-12-12 19:56:36 +00:00
|
|
|
static int cryptodev_aead(struct csession *, struct crypt_aead *,
|
|
|
|
struct ucred *, struct thread *);
|
2002-10-04 20:31:23 +00:00
|
|
|
static int cryptodev_key(struct crypt_kop *);
|
2007-03-21 03:42:51 +00:00
|
|
|
static int cryptodev_find(struct crypt_find_op *);
|
2002-10-04 20:31:23 +00:00
|
|
|
|
2007-03-21 03:42:51 +00:00
|
|
|
/*
|
|
|
|
* Check a crypto identifier to see if it requested
|
|
|
|
* a software device/driver. This can be done either
|
|
|
|
* by device name/class or through search constraints.
|
|
|
|
*/
|
|
|
|
static int
|
2014-12-12 19:56:36 +00:00
|
|
|
checkforsoftware(int *cridp)
|
2007-03-21 03:42:51 +00:00
|
|
|
{
|
2014-12-12 19:56:36 +00:00
|
|
|
int crid;
|
|
|
|
|
|
|
|
crid = *cridp;
|
2014-03-11 01:45:46 +00:00
|
|
|
|
|
|
|
if (!crypto_devallowsoft) {
|
2014-12-12 19:56:36 +00:00
|
|
|
if (crid & CRYPTOCAP_F_SOFTWARE) {
|
|
|
|
if (crid & CRYPTOCAP_F_HARDWARE) {
|
|
|
|
*cridp = CRYPTOCAP_F_HARDWARE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return EINVAL;
|
|
|
|
}
|
2014-03-11 01:45:46 +00:00
|
|
|
if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
|
|
|
|
(crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
|
2014-12-12 19:56:36 +00:00
|
|
|
return EINVAL;
|
2014-03-11 01:45:46 +00:00
|
|
|
}
|
2007-03-21 03:42:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-10-04 20:31:23 +00:00
|
|
|
/* ARGSUSED */
|
|
|
|
static int
|
|
|
|
cryptof_ioctl(
|
|
|
|
struct file *fp,
|
|
|
|
u_long cmd,
|
|
|
|
void *data,
|
|
|
|
struct ucred *active_cred,
|
|
|
|
struct thread *td)
|
|
|
|
{
|
2007-03-21 03:42:51 +00:00
|
|
|
#define SES2(p) ((struct session2_op *)p)
|
2002-10-04 20:31:23 +00:00
|
|
|
struct cryptoini cria, crie;
|
2003-01-13 00:33:17 +00:00
|
|
|
struct fcrypt *fcr = fp->f_data;
|
2002-10-04 20:31:23 +00:00
|
|
|
struct csession *cse;
|
|
|
|
struct session_op *sop;
|
|
|
|
struct crypt_op *cop;
|
2014-12-12 19:56:36 +00:00
|
|
|
struct crypt_aead *caead;
|
2002-10-04 20:31:23 +00:00
|
|
|
struct enc_xform *txform = NULL;
|
|
|
|
struct auth_hash *thash = NULL;
|
2007-03-21 03:42:51 +00:00
|
|
|
struct crypt_kop *kop;
|
2018-07-18 00:56:25 +00:00
|
|
|
crypto_session_t cses;
|
2002-10-04 20:31:23 +00:00
|
|
|
u_int32_t ses;
|
2007-03-21 03:42:51 +00:00
|
|
|
int error = 0, crid;
|
2010-07-29 20:42:20 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
|
|
|
struct session2_op sopc;
|
|
|
|
struct crypt_op copc;
|
|
|
|
struct crypt_kop kopc;
|
|
|
|
#endif
|
2002-10-04 20:31:23 +00:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case CIOCGSESSION:
|
2007-03-21 03:42:51 +00:00
|
|
|
case CIOCGSESSION2:
|
2010-07-29 20:42:20 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
|
|
|
case CIOCGSESSION32:
|
|
|
|
case CIOCGSESSION232:
|
|
|
|
if (cmd == CIOCGSESSION32) {
|
|
|
|
session_op_from_32(data, (struct session_op *)&sopc);
|
|
|
|
sop = (struct session_op *)&sopc;
|
|
|
|
} else if (cmd == CIOCGSESSION232) {
|
|
|
|
session2_op_from_32(data, &sopc);
|
|
|
|
sop = (struct session_op *)&sopc;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
sop = (struct session_op *)data;
|
2002-10-04 20:31:23 +00:00
|
|
|
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;
|
|
|
|
break;
|
|
|
|
case CRYPTO_SKIPJACK_CBC:
|
|
|
|
txform = &enc_xform_skipjack;
|
|
|
|
break;
|
|
|
|
case CRYPTO_AES_CBC:
|
|
|
|
txform = &enc_xform_rijndael128;
|
|
|
|
break;
|
2010-09-23 11:52:32 +00:00
|
|
|
case CRYPTO_AES_XTS:
|
|
|
|
txform = &enc_xform_aes_xts;
|
|
|
|
break;
|
2002-10-04 20:31:23 +00:00
|
|
|
case CRYPTO_NULL_CBC:
|
|
|
|
txform = &enc_xform_null;
|
|
|
|
break;
|
|
|
|
case CRYPTO_ARC4:
|
|
|
|
txform = &enc_xform_arc4;
|
|
|
|
break;
|
2007-05-09 19:37:02 +00:00
|
|
|
case CRYPTO_CAMELLIA_CBC:
|
|
|
|
txform = &enc_xform_camellia;
|
|
|
|
break;
|
2014-12-12 19:56:36 +00:00
|
|
|
case CRYPTO_AES_ICM:
|
|
|
|
txform = &enc_xform_aes_icm;
|
|
|
|
break;
|
|
|
|
case CRYPTO_AES_NIST_GCM_16:
|
|
|
|
txform = &enc_xform_aes_nist_gcm;
|
|
|
|
break;
|
2018-03-29 04:02:50 +00:00
|
|
|
case CRYPTO_CHACHA20:
|
|
|
|
txform = &enc_xform_chacha20;
|
|
|
|
break;
|
2019-02-15 03:53:03 +00:00
|
|
|
case CRYPTO_AES_CCM_16:
|
|
|
|
txform = &enc_xform_ccm;
|
|
|
|
break;
|
2014-12-12 19:56:36 +00:00
|
|
|
|
2002-10-04 20:31:23 +00:00
|
|
|
default:
|
2014-12-12 19:56:36 +00:00
|
|
|
CRYPTDEB("invalid cipher");
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (sop->mac) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case CRYPTO_MD5_HMAC:
|
2006-05-17 18:24:17 +00:00
|
|
|
thash = &auth_hash_hmac_md5;
|
2002-10-04 20:31:23 +00:00
|
|
|
break;
|
2018-08-17 00:31:06 +00:00
|
|
|
case CRYPTO_POLY1305:
|
|
|
|
thash = &auth_hash_poly1305;
|
|
|
|
break;
|
2002-10-04 20:31:23 +00:00
|
|
|
case CRYPTO_SHA1_HMAC:
|
2006-05-17 18:24:17 +00:00
|
|
|
thash = &auth_hash_hmac_sha1;
|
2002-10-04 20:31:23 +00:00
|
|
|
break;
|
2018-07-09 07:26:12 +00:00
|
|
|
case CRYPTO_SHA2_224_HMAC:
|
|
|
|
thash = &auth_hash_hmac_sha2_224;
|
|
|
|
break;
|
2006-05-17 18:24:17 +00:00
|
|
|
case CRYPTO_SHA2_256_HMAC:
|
|
|
|
thash = &auth_hash_hmac_sha2_256;
|
|
|
|
break;
|
|
|
|
case CRYPTO_SHA2_384_HMAC:
|
|
|
|
thash = &auth_hash_hmac_sha2_384;
|
|
|
|
break;
|
|
|
|
case CRYPTO_SHA2_512_HMAC:
|
|
|
|
thash = &auth_hash_hmac_sha2_512;
|
2002-10-04 20:31:23 +00:00
|
|
|
break;
|
|
|
|
case CRYPTO_RIPEMD160_HMAC:
|
2006-05-17 18:24:17 +00:00
|
|
|
thash = &auth_hash_hmac_ripemd_160;
|
2002-10-04 20:31:23 +00:00
|
|
|
break;
|
2014-12-12 19:56:36 +00:00
|
|
|
case CRYPTO_AES_128_NIST_GMAC:
|
|
|
|
thash = &auth_hash_nist_gmac_aes_128;
|
|
|
|
break;
|
|
|
|
case CRYPTO_AES_192_NIST_GMAC:
|
|
|
|
thash = &auth_hash_nist_gmac_aes_192;
|
|
|
|
break;
|
|
|
|
case CRYPTO_AES_256_NIST_GMAC:
|
|
|
|
thash = &auth_hash_nist_gmac_aes_256;
|
|
|
|
break;
|
|
|
|
|
2019-02-15 03:53:03 +00:00
|
|
|
case CRYPTO_AES_CCM_CBC_MAC:
|
|
|
|
switch (sop->keylen) {
|
|
|
|
case 16:
|
|
|
|
thash = &auth_hash_ccm_cbc_mac_128;
|
|
|
|
break;
|
|
|
|
case 24:
|
|
|
|
thash = &auth_hash_ccm_cbc_mac_192;
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
thash = &auth_hash_ccm_cbc_mac_256;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CRYPTDEB("Invalid CBC MAC key size %d",
|
|
|
|
sop->keylen);
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl,
|
|
|
|
error, __LINE__);
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
break;
|
2002-10-04 20:31:23 +00:00
|
|
|
#ifdef notdef
|
|
|
|
case CRYPTO_MD5:
|
|
|
|
thash = &auth_hash_md5;
|
|
|
|
break;
|
2018-07-09 07:28:13 +00:00
|
|
|
#endif
|
2002-10-04 20:31:23 +00:00
|
|
|
case CRYPTO_SHA1:
|
|
|
|
thash = &auth_hash_sha1;
|
|
|
|
break;
|
2018-07-09 07:28:13 +00:00
|
|
|
case CRYPTO_SHA2_224:
|
|
|
|
thash = &auth_hash_sha2_224;
|
|
|
|
break;
|
|
|
|
case CRYPTO_SHA2_256:
|
|
|
|
thash = &auth_hash_sha2_256;
|
|
|
|
break;
|
|
|
|
case CRYPTO_SHA2_384:
|
|
|
|
thash = &auth_hash_sha2_384;
|
|
|
|
break;
|
|
|
|
case CRYPTO_SHA2_512:
|
|
|
|
thash = &auth_hash_sha2_512;
|
|
|
|
break;
|
|
|
|
|
2002-10-04 20:31:23 +00:00
|
|
|
case CRYPTO_NULL_HMAC:
|
|
|
|
thash = &auth_hash_null;
|
|
|
|
break;
|
Import Blake2 algorithms (blake2b, blake2s) from libb2
The upstream repository is on github BLAKE2/libb2. Files landed in
sys/contrib/libb2 are the unmodified upstream files, except for one
difference: secure_zero_memory's contents have been replaced with
explicit_bzero() only because the previous implementation broke powerpc
link. Preferential use of explicit_bzero() is in progress upstream, so
it is anticipated we will be able to drop this diff in the future.
sys/crypto/blake2 contains the source files needed to port libb2 to our
build system, a wrapped (limited) variant of the algorithm to match the API
of our auth_transform softcrypto abstraction, incorporation into the Open
Crypto Framework (OCF) cryptosoft(4) driver, as well as an x86 SSE/AVX
accelerated OCF driver, blake2(4).
Optimized variants of blake2 are compiled for a number of x86 machines
(anything from SSE2 to AVX + XOP). On those machines, FPU context will need
to be explicitly saved before using blake2(4)-provided algorithms directly.
Use via cryptodev / OCF saves FPU state automatically, and use via the
auth_transform softcrypto abstraction does not use FPU.
The intent of the OCF driver is mostly to enable testing in userspace via
/dev/crypto. ATF tests are added with published KAT test vectors to
validate correctness.
Reviewed by: jhb, markj
Obtained from: github BLAKE2/libb2
Differential Revision: https://reviews.freebsd.org/D14662
2018-03-21 16:18:14 +00:00
|
|
|
|
|
|
|
case CRYPTO_BLAKE2B:
|
|
|
|
thash = &auth_hash_blake2b;
|
|
|
|
break;
|
|
|
|
case CRYPTO_BLAKE2S:
|
|
|
|
thash = &auth_hash_blake2s;
|
|
|
|
break;
|
|
|
|
|
2002-10-04 20:31:23 +00:00
|
|
|
default:
|
2014-12-12 19:56:36 +00:00
|
|
|
CRYPTDEB("invalid mac");
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
bzero(&crie, sizeof(crie));
|
|
|
|
bzero(&cria, sizeof(cria));
|
|
|
|
|
|
|
|
if (txform) {
|
|
|
|
crie.cri_alg = txform->type;
|
|
|
|
crie.cri_klen = sop->keylen * 8;
|
|
|
|
if (sop->keylen > txform->maxkey ||
|
|
|
|
sop->keylen < txform->minkey) {
|
2014-12-12 19:56:36 +00:00
|
|
|
CRYPTDEB("invalid cipher parameters");
|
2002-10-04 20:31:23 +00:00
|
|
|
error = EINVAL;
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error,
|
|
|
|
__LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2008-10-23 20:26:15 +00:00
|
|
|
crie.cri_key = malloc(crie.cri_klen / 8,
|
|
|
|
M_XDATA, M_WAITOK);
|
2002-10-04 20:31:23 +00:00
|
|
|
if ((error = copyin(sop->key, crie.cri_key,
|
2014-12-12 19:56:36 +00:00
|
|
|
crie.cri_klen / 8))) {
|
|
|
|
CRYPTDEB("invalid key");
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error,
|
|
|
|
__LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
goto bail;
|
2014-12-12 19:56:36 +00:00
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
if (thash)
|
|
|
|
crie.cri_next = &cria;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thash) {
|
|
|
|
cria.cri_alg = thash->type;
|
|
|
|
cria.cri_klen = sop->mackeylen * 8;
|
2017-09-26 16:18:10 +00:00
|
|
|
if (thash->keysize != 0 &&
|
|
|
|
sop->mackeylen > thash->keysize) {
|
2014-12-12 19:56:36 +00:00
|
|
|
CRYPTDEB("invalid mac key length");
|
2002-10-04 20:31:23 +00:00
|
|
|
error = EINVAL;
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error,
|
|
|
|
__LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cria.cri_klen) {
|
2008-10-23 20:26:15 +00:00
|
|
|
cria.cri_key = malloc(cria.cri_klen / 8,
|
|
|
|
M_XDATA, M_WAITOK);
|
2002-10-04 20:31:23 +00:00
|
|
|
if ((error = copyin(sop->mackey, cria.cri_key,
|
2014-12-12 19:56:36 +00:00
|
|
|
cria.cri_klen / 8))) {
|
|
|
|
CRYPTDEB("invalid mac key");
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl,
|
|
|
|
error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
goto bail;
|
2014-12-12 19:56:36 +00:00
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-11 16:53:45 +00:00
|
|
|
/* NB: CIOCGSESSION2 has the crid */
|
2010-07-29 20:42:20 +00:00
|
|
|
if (cmd == CIOCGSESSION2
|
|
|
|
#ifdef COMPAT_FREEBSD32
|
|
|
|
|| cmd == CIOCGSESSION232
|
|
|
|
#endif
|
|
|
|
) {
|
2007-03-21 03:42:51 +00:00
|
|
|
crid = SES2(sop)->crid;
|
2014-12-12 19:56:36 +00:00
|
|
|
error = checkforsoftware(&crid);
|
|
|
|
if (error) {
|
|
|
|
CRYPTDEB("checkforsoftware");
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error,
|
|
|
|
__LINE__);
|
2006-05-17 18:01:51 +00:00
|
|
|
goto bail;
|
2014-12-12 19:56:36 +00:00
|
|
|
}
|
2007-03-21 03:42:51 +00:00
|
|
|
} else
|
|
|
|
crid = CRYPTOCAP_F_HARDWARE;
|
2018-07-18 00:56:25 +00:00
|
|
|
error = crypto_newsession(&cses, (txform ? &crie : &cria), crid);
|
2014-12-12 19:56:36 +00:00
|
|
|
if (error) {
|
|
|
|
CRYPTDEB("crypto_newsession");
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2007-03-21 03:42:51 +00:00
|
|
|
goto bail;
|
2014-12-12 19:56:36 +00:00
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
|
2018-07-18 00:56:25 +00:00
|
|
|
cse = csecreate(fcr, cses, crie.cri_key, crie.cri_klen,
|
2002-10-04 20:31:23 +00:00
|
|
|
cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
|
|
|
|
thash);
|
|
|
|
|
|
|
|
if (cse == NULL) {
|
2018-07-18 00:56:25 +00:00
|
|
|
crypto_freesession(cses);
|
2002-10-04 20:31:23 +00:00
|
|
|
error = EINVAL;
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2014-12-12 19:56:36 +00:00
|
|
|
CRYPTDEB("csecreate");
|
2002-10-04 20:31:23 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
sop->ses = cse->ses;
|
2010-07-29 20:42:20 +00:00
|
|
|
if (cmd == CIOCGSESSION2
|
|
|
|
#ifdef COMPAT_FREEBSD32
|
|
|
|
|| cmd == CIOCGSESSION232
|
|
|
|
#endif
|
|
|
|
) {
|
2007-03-21 03:42:51 +00:00
|
|
|
/* return hardware/driver id */
|
2018-07-18 00:56:25 +00:00
|
|
|
SES2(sop)->crid = crypto_ses2hid(cse->cses);
|
2007-03-21 03:42:51 +00:00
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
bail:
|
|
|
|
if (error) {
|
|
|
|
if (crie.cri_key)
|
2008-10-23 15:53:51 +00:00
|
|
|
free(crie.cri_key, M_XDATA);
|
2002-10-04 20:31:23 +00:00
|
|
|
if (cria.cri_key)
|
2008-10-23 15:53:51 +00:00
|
|
|
free(cria.cri_key, M_XDATA);
|
2002-10-04 20:31:23 +00:00
|
|
|
}
|
2010-07-29 20:42:20 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
|
|
|
else {
|
|
|
|
if (cmd == CIOCGSESSION32)
|
|
|
|
session_op_to_32(sop, data);
|
|
|
|
else if (cmd == CIOCGSESSION232)
|
|
|
|
session2_op_to_32((struct session2_op *)sop,
|
|
|
|
data);
|
|
|
|
}
|
|
|
|
#endif
|
2002-10-04 20:31:23 +00:00
|
|
|
break;
|
|
|
|
case CIOCFSESSION:
|
|
|
|
ses = *(u_int32_t *)data;
|
|
|
|
cse = csefind(fcr, ses);
|
2018-01-11 00:22:24 +00:00
|
|
|
if (cse == NULL) {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
return (EINVAL);
|
2018-01-11 00:22:24 +00:00
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
csedelete(fcr, cse);
|
2018-07-18 00:56:25 +00:00
|
|
|
csefree(cse);
|
2002-10-04 20:31:23 +00:00
|
|
|
break;
|
|
|
|
case CIOCCRYPT:
|
2010-07-29 20:42:20 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
|
|
|
case CIOCCRYPT32:
|
|
|
|
if (cmd == CIOCCRYPT32) {
|
|
|
|
cop = &copc;
|
|
|
|
crypt_op_from_32(data, cop);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
cop = (struct crypt_op *)data;
|
2002-10-04 20:31:23 +00:00
|
|
|
cse = csefind(fcr, cop->ses);
|
2014-12-12 19:56:36 +00:00
|
|
|
if (cse == NULL) {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
return (EINVAL);
|
2014-12-12 19:56:36 +00:00
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
error = cryptodev_op(cse, cop, active_cred, td);
|
2010-07-29 20:42:20 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
|
|
|
if (error == 0 && cmd == CIOCCRYPT32)
|
|
|
|
crypt_op_to_32(cop, data);
|
|
|
|
#endif
|
2002-10-04 20:31:23 +00:00
|
|
|
break;
|
|
|
|
case CIOCKEY:
|
2007-03-21 03:42:51 +00:00
|
|
|
case CIOCKEY2:
|
2010-07-29 20:42:20 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
|
|
|
case CIOCKEY32:
|
|
|
|
case CIOCKEY232:
|
|
|
|
#endif
|
2018-01-11 00:22:24 +00:00
|
|
|
if (!crypto_userasymcrypto) {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2007-03-21 03:42:51 +00:00
|
|
|
return (EPERM); /* XXX compat? */
|
2018-01-11 00:22:24 +00:00
|
|
|
}
|
2010-07-29 20:42:20 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
|
|
|
if (cmd == CIOCKEY32 || cmd == CIOCKEY232) {
|
|
|
|
kop = &kopc;
|
|
|
|
crypt_kop_from_32(data, kop);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
kop = (struct crypt_kop *)data;
|
|
|
|
if (cmd == CIOCKEY
|
|
|
|
#ifdef COMPAT_FREEBSD32
|
|
|
|
|| cmd == CIOCKEY32
|
|
|
|
#endif
|
|
|
|
) {
|
2007-03-21 03:42:51 +00:00
|
|
|
/* NB: crypto core enforces s/w driver use */
|
|
|
|
kop->crk_crid =
|
|
|
|
CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
|
|
|
|
}
|
2010-07-29 20:42:20 +00:00
|
|
|
mtx_lock(&Giant);
|
2007-03-21 03:42:51 +00:00
|
|
|
error = cryptodev_key(kop);
|
|
|
|
mtx_unlock(&Giant);
|
2010-07-29 20:42:20 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
|
|
|
if (cmd == CIOCKEY32 || cmd == CIOCKEY232)
|
|
|
|
crypt_kop_to_32(kop, data);
|
|
|
|
#endif
|
2002-10-04 20:31:23 +00:00
|
|
|
break;
|
|
|
|
case CIOCASYMFEAT:
|
2007-03-21 03:42:51 +00:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
*(int *)data = 0;
|
2018-01-11 00:22:24 +00:00
|
|
|
} else {
|
2007-03-21 03:42:51 +00:00
|
|
|
error = crypto_getfeat((int *)data);
|
2018-01-11 00:22:24 +00:00
|
|
|
if (error)
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error,
|
|
|
|
__LINE__);
|
|
|
|
}
|
2007-03-21 03:42:51 +00:00
|
|
|
break;
|
|
|
|
case CIOCFINDDEV:
|
|
|
|
error = cryptodev_find((struct crypt_find_op *)data);
|
2002-10-04 20:31:23 +00:00
|
|
|
break;
|
2014-12-12 19:56:36 +00:00
|
|
|
case CIOCCRYPTAEAD:
|
|
|
|
caead = (struct crypt_aead *)data;
|
|
|
|
cse = csefind(fcr, caead->ses);
|
2018-01-11 00:22:24 +00:00
|
|
|
if (cse == NULL) {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2014-12-12 19:56:36 +00:00
|
|
|
return (EINVAL);
|
2018-01-11 00:22:24 +00:00
|
|
|
}
|
2014-12-12 19:56:36 +00:00
|
|
|
error = cryptodev_aead(cse, caead, active_cred, td);
|
|
|
|
break;
|
2002-10-04 20:31:23 +00:00
|
|
|
default:
|
|
|
|
error = EINVAL;
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2007-03-21 03:42:51 +00:00
|
|
|
break;
|
2002-10-04 20:31:23 +00:00
|
|
|
}
|
|
|
|
return (error);
|
2007-03-21 03:42:51 +00:00
|
|
|
#undef SES2
|
2002-10-04 20:31:23 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 23:21:50 +00:00
|
|
|
static int cryptodev_cb(struct cryptop *);
|
2002-10-04 20:31:23 +00:00
|
|
|
|
2018-01-26 23:21:50 +00:00
|
|
|
static struct cryptop_data *
|
|
|
|
cod_alloc(struct csession *cse, size_t len, struct thread *td)
|
|
|
|
{
|
|
|
|
struct cryptop_data *cod;
|
|
|
|
struct uio *uio;
|
|
|
|
|
|
|
|
cod = malloc(sizeof(struct cryptop_data), M_XDATA, M_WAITOK | M_ZERO);
|
|
|
|
|
|
|
|
cod->cse = cse;
|
|
|
|
uio = &cod->uio;
|
|
|
|
uio->uio_iov = cod->iovec;
|
|
|
|
uio->uio_iovcnt = 1;
|
|
|
|
uio->uio_resid = len;
|
|
|
|
uio->uio_segflg = UIO_SYSSPACE;
|
|
|
|
uio->uio_rw = UIO_WRITE;
|
|
|
|
uio->uio_td = td;
|
|
|
|
uio->uio_iov[0].iov_len = len;
|
|
|
|
uio->uio_iov[0].iov_base = malloc(len, M_XDATA, M_WAITOK);
|
|
|
|
return (cod);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cod_free(struct cryptop_data *cod)
|
|
|
|
{
|
|
|
|
|
|
|
|
free(cod->uio.uio_iov[0].iov_base, M_XDATA);
|
|
|
|
free(cod, M_XDATA);
|
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
|
2019-08-27 21:29:37 +00:00
|
|
|
static void
|
|
|
|
cryptodev_warn(struct csession *cse)
|
|
|
|
{
|
|
|
|
static struct timeval arc4warn, blfwarn, castwarn, deswarn, md5warn;
|
|
|
|
static struct timeval skipwarn, tdeswarn;
|
|
|
|
|
|
|
|
switch (cse->cipher) {
|
|
|
|
case CRYPTO_DES_CBC:
|
|
|
|
if (ratecheck(&deswarn, &warninterval))
|
|
|
|
gone_in(13, "DES cipher via /dev/crypto");
|
|
|
|
break;
|
|
|
|
case CRYPTO_3DES_CBC:
|
|
|
|
if (ratecheck(&tdeswarn, &warninterval))
|
|
|
|
gone_in(13, "3DES cipher via /dev/crypto");
|
|
|
|
break;
|
|
|
|
case CRYPTO_BLF_CBC:
|
|
|
|
if (ratecheck(&blfwarn, &warninterval))
|
|
|
|
gone_in(13, "Blowfish cipher via /dev/crypto");
|
|
|
|
break;
|
|
|
|
case CRYPTO_CAST_CBC:
|
|
|
|
if (ratecheck(&castwarn, &warninterval))
|
|
|
|
gone_in(13, "CAST128 cipher via /dev/crypto");
|
|
|
|
break;
|
|
|
|
case CRYPTO_SKIPJACK_CBC:
|
|
|
|
if (ratecheck(&skipwarn, &warninterval))
|
|
|
|
gone_in(13, "Skipjack cipher via /dev/crypto");
|
|
|
|
break;
|
|
|
|
case CRYPTO_ARC4:
|
|
|
|
if (ratecheck(&arc4warn, &warninterval))
|
|
|
|
gone_in(13, "ARC4 cipher via /dev/crypto");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (cse->mac) {
|
|
|
|
case CRYPTO_MD5_HMAC:
|
|
|
|
if (ratecheck(&md5warn, &warninterval))
|
|
|
|
gone_in(13, "MD5-HMAC authenticator via /dev/crypto");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-04 20:31:23 +00:00
|
|
|
static int
|
|
|
|
cryptodev_op(
|
|
|
|
struct csession *cse,
|
|
|
|
struct crypt_op *cop,
|
|
|
|
struct ucred *active_cred,
|
|
|
|
struct thread *td)
|
|
|
|
{
|
2018-01-26 23:21:50 +00:00
|
|
|
struct cryptop_data *cod = NULL;
|
2002-10-04 20:31:23 +00:00
|
|
|
struct cryptop *crp = NULL;
|
|
|
|
struct cryptodesc *crde = NULL, *crda = NULL;
|
2003-11-19 22:42:34 +00:00
|
|
|
int error;
|
2002-10-04 20:31:23 +00:00
|
|
|
|
2014-12-12 19:56:36 +00:00
|
|
|
if (cop->len > 256*1024-4) {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
return (E2BIG);
|
2014-12-12 19:56:36 +00:00
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
|
2005-08-18 11:58:03 +00:00
|
|
|
if (cse->txform) {
|
2014-12-12 19:56:36 +00:00
|
|
|
if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0) {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2005-08-18 11:58:03 +00:00
|
|
|
return (EINVAL);
|
2014-12-12 19:56:36 +00:00
|
|
|
}
|
2005-08-18 11:58:03 +00:00
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
|
2018-01-26 23:21:50 +00:00
|
|
|
if (cse->thash)
|
|
|
|
cod = cod_alloc(cse, cop->len + cse->thash->hashsize, td);
|
|
|
|
else
|
|
|
|
cod = cod_alloc(cse, cop->len, td);
|
2002-10-04 20:31:23 +00:00
|
|
|
|
|
|
|
crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
|
|
|
|
if (crp == NULL) {
|
2014-12-12 19:56:36 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
error = ENOMEM;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2017-09-22 00:21:58 +00:00
|
|
|
if (cse->thash && cse->txform) {
|
|
|
|
if (cop->flags & COP_F_CIPHER_FIRST) {
|
2002-10-04 20:31:23 +00:00
|
|
|
crde = crp->crp_desc;
|
2017-09-22 00:21:58 +00:00
|
|
|
crda = crde->crd_next;
|
|
|
|
} else {
|
|
|
|
crda = crp->crp_desc;
|
|
|
|
crde = crda->crd_next;
|
2002-10-04 20:31:23 +00:00
|
|
|
}
|
2017-09-22 00:21:58 +00:00
|
|
|
} else if (cse->thash) {
|
|
|
|
crda = crp->crp_desc;
|
|
|
|
} else if (cse->txform) {
|
|
|
|
crde = crp->crp_desc;
|
|
|
|
} else {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
|
|
|
error = EINVAL;
|
|
|
|
goto bail;
|
2002-10-04 20:31:23 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 23:21:50 +00:00
|
|
|
if ((error = copyin(cop->src, cod->uio.uio_iov[0].iov_base,
|
2014-12-12 19:56:36 +00:00
|
|
|
cop->len))) {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
goto bail;
|
2014-12-12 19:56:36 +00:00
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
|
|
|
|
if (crda) {
|
|
|
|
crda->crd_skip = 0;
|
|
|
|
crda->crd_len = cop->len;
|
2006-05-22 16:24:11 +00:00
|
|
|
crda->crd_inject = cop->len;
|
2002-10-04 20:31:23 +00:00
|
|
|
|
|
|
|
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 = cop->len;
|
2003-02-23 07:25:48 +00:00
|
|
|
crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
|
|
|
|
| (cop->flags & COP_F_BATCH);
|
2018-01-26 23:21:50 +00:00
|
|
|
crp->crp_uio = &cod->uio;
|
|
|
|
crp->crp_callback = cryptodev_cb;
|
2018-07-18 00:56:25 +00:00
|
|
|
crp->crp_session = cse->cses;
|
2018-01-26 23:21:50 +00:00
|
|
|
crp->crp_opaque = cod;
|
2002-10-04 20:31:23 +00:00
|
|
|
|
|
|
|
if (cop->iv) {
|
|
|
|
if (crde == NULL) {
|
2014-12-12 19:56:36 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
|
2014-12-12 19:56:36 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto bail;
|
|
|
|
}
|
2018-01-11 18:07:21 +00:00
|
|
|
if ((error = copyin(cop->iv, crde->crd_iv,
|
cryptodev: Match intent for enc_xform ciphers with blocksize != ivsize
No functional change for Skipjack, AES-ICM, Blowfish, CAST-128, Camellia,
DES3, Rijndael128, DES. All of these have identical IV and blocksizes
declared in the associated enc_xform.
Functional changes for:
* AES-GCM: block len of 1, IV len of 12
* AES-XTS: block len of 16, IV len of 8
* NULL: block len of 4, IV len of 0
For these, it seems like the IV specified in the enc_xform is correct (and
the blocksize used before was wrong).
Additionally, the not-yet-OCFed cipher Chacha20 has a logical block length
of 1 byte, and a 16 byte IV + nonce.
Rationalize references to IV lengths to refer to the declared ivsize, rather
than declared blocksize.
Sponsored by: Dell EMC Isilon
2018-03-26 20:30:07 +00:00
|
|
|
cse->txform->ivsize))) {
|
2014-12-12 19:56:36 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
goto bail;
|
2014-12-12 19:56:36 +00:00
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
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;
|
cryptodev: Match intent for enc_xform ciphers with blocksize != ivsize
No functional change for Skipjack, AES-ICM, Blowfish, CAST-128, Camellia,
DES3, Rijndael128, DES. All of these have identical IV and blocksizes
declared in the associated enc_xform.
Functional changes for:
* AES-GCM: block len of 1, IV len of 12
* AES-XTS: block len of 16, IV len of 8
* NULL: block len of 4, IV len of 0
For these, it seems like the IV specified in the enc_xform is correct (and
the blocksize used before was wrong).
Additionally, the not-yet-OCFed cipher Chacha20 has a logical block length
of 1 byte, and a 16 byte IV + nonce.
Rationalize references to IV lengths to refer to the declared ivsize, rather
than declared blocksize.
Sponsored by: Dell EMC Isilon
2018-03-26 20:30:07 +00:00
|
|
|
crde->crd_skip = cse->txform->ivsize;
|
|
|
|
crde->crd_len -= cse->txform->ivsize;
|
2002-10-04 20:31:23 +00:00
|
|
|
}
|
|
|
|
|
2006-05-22 16:24:11 +00:00
|
|
|
if (cop->mac && crda == NULL) {
|
2014-12-12 19:56:36 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2006-05-22 16:24:11 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto bail;
|
2002-10-04 20:31:23 +00:00
|
|
|
}
|
2019-08-27 21:29:37 +00:00
|
|
|
cryptodev_warn(cse);
|
2002-10-04 20:31:23 +00:00
|
|
|
|
2009-09-04 09:48:18 +00:00
|
|
|
again:
|
2003-06-02 23:28:06 +00:00
|
|
|
/*
|
|
|
|
* 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);
|
2014-12-12 19:56:36 +00:00
|
|
|
if (error != 0) {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
goto bail;
|
2014-12-12 19:56:36 +00:00
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
|
2018-01-26 23:21:50 +00:00
|
|
|
mtx_lock(&cse->lock);
|
|
|
|
while (!cod->done)
|
|
|
|
mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
|
|
|
|
mtx_unlock(&cse->lock);
|
|
|
|
|
2009-09-04 09:48:18 +00:00
|
|
|
if (crp->crp_etype == EAGAIN) {
|
|
|
|
crp->crp_etype = 0;
|
|
|
|
crp->crp_flags &= ~CRYPTO_F_DONE;
|
2018-01-26 23:21:50 +00:00
|
|
|
cod->done = false;
|
2009-09-04 09:48:18 +00:00
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
2002-10-04 20:31:23 +00:00
|
|
|
if (crp->crp_etype != 0) {
|
2014-12-12 19:56:36 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
error = crp->crp_etype;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cop->dst &&
|
2018-01-26 23:21:50 +00:00
|
|
|
(error = copyout(cod->uio.uio_iov[0].iov_base, cop->dst,
|
2014-12-12 19:56:36 +00:00
|
|
|
cop->len))) {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
goto bail;
|
2014-12-12 19:56:36 +00:00
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
|
|
|
|
if (cop->mac &&
|
2018-01-26 23:21:50 +00:00
|
|
|
(error = copyout((caddr_t)cod->uio.uio_iov[0].iov_base + cop->len,
|
2014-12-12 19:56:36 +00:00
|
|
|
cop->mac, cse->thash->hashsize))) {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
goto bail;
|
2014-12-12 19:56:36 +00:00
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
|
|
|
|
bail:
|
|
|
|
if (crp)
|
|
|
|
crypto_freereq(crp);
|
2018-01-26 23:21:50 +00:00
|
|
|
if (cod)
|
|
|
|
cod_free(cod);
|
2002-10-04 20:31:23 +00:00
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2014-12-12 19:56:36 +00:00
|
|
|
static int
|
|
|
|
cryptodev_aead(
|
|
|
|
struct csession *cse,
|
|
|
|
struct crypt_aead *caead,
|
|
|
|
struct ucred *active_cred,
|
|
|
|
struct thread *td)
|
|
|
|
{
|
2018-01-26 23:21:50 +00:00
|
|
|
struct cryptop_data *cod = NULL;
|
2014-12-12 19:56:36 +00:00
|
|
|
struct cryptop *crp = NULL;
|
|
|
|
struct cryptodesc *crde = NULL, *crda = NULL;
|
|
|
|
int error;
|
|
|
|
|
2018-01-11 00:22:24 +00:00
|
|
|
if (caead->len > 256*1024-4 || caead->aadlen > 256*1024-4) {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2014-12-12 19:56:36 +00:00
|
|
|
return (E2BIG);
|
2018-01-11 00:22:24 +00:00
|
|
|
}
|
2014-12-12 19:56:36 +00:00
|
|
|
|
|
|
|
if (cse->txform == NULL || cse->thash == NULL || caead->tag == NULL ||
|
2018-01-11 00:22:24 +00:00
|
|
|
(caead->len % cse->txform->blocksize) != 0) {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2014-12-12 19:56:36 +00:00
|
|
|
return (EINVAL);
|
2018-01-11 00:22:24 +00:00
|
|
|
}
|
2014-12-12 19:56:36 +00:00
|
|
|
|
2018-01-26 23:21:50 +00:00
|
|
|
cod = cod_alloc(cse, caead->aadlen + caead->len + cse->thash->hashsize,
|
|
|
|
td);
|
2014-12-12 19:56:36 +00:00
|
|
|
|
|
|
|
crp = crypto_getreq(2);
|
|
|
|
if (crp == NULL) {
|
|
|
|
error = ENOMEM;
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2014-12-12 19:56:36 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2017-09-22 00:34:46 +00:00
|
|
|
if (caead->flags & COP_F_CIPHER_FIRST) {
|
|
|
|
crde = crp->crp_desc;
|
|
|
|
crda = crde->crd_next;
|
|
|
|
} else {
|
|
|
|
crda = crp->crp_desc;
|
|
|
|
crde = crda->crd_next;
|
|
|
|
}
|
2014-12-12 19:56:36 +00:00
|
|
|
|
2018-01-26 23:21:50 +00:00
|
|
|
if ((error = copyin(caead->aad, cod->uio.uio_iov[0].iov_base,
|
2018-01-11 00:22:24 +00:00
|
|
|
caead->aadlen))) {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2014-12-12 19:56:36 +00:00
|
|
|
goto bail;
|
2018-01-11 00:22:24 +00:00
|
|
|
}
|
2014-12-12 19:56:36 +00:00
|
|
|
|
2018-01-26 23:21:50 +00:00
|
|
|
if ((error = copyin(caead->src, (char *)cod->uio.uio_iov[0].iov_base +
|
2018-01-11 00:22:24 +00:00
|
|
|
caead->aadlen, caead->len))) {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2014-12-12 19:56:36 +00:00
|
|
|
goto bail;
|
2018-01-11 00:22:24 +00:00
|
|
|
}
|
2014-12-12 19:56:36 +00:00
|
|
|
|
2017-09-22 00:34:46 +00:00
|
|
|
/*
|
2019-02-15 03:53:03 +00:00
|
|
|
* For GCM/CCM, crd_len covers only the AAD. For other ciphers
|
2017-09-22 00:34:46 +00:00
|
|
|
* chained with an HMAC, crd_len covers both the AAD and the
|
|
|
|
* cipher text.
|
|
|
|
*/
|
2017-09-22 00:15:54 +00:00
|
|
|
crda->crd_skip = 0;
|
2019-02-15 03:53:03 +00:00
|
|
|
if (cse->cipher == CRYPTO_AES_NIST_GCM_16 ||
|
|
|
|
cse->cipher == CRYPTO_AES_CCM_16)
|
2017-09-22 00:34:46 +00:00
|
|
|
crda->crd_len = caead->aadlen;
|
|
|
|
else
|
|
|
|
crda->crd_len = caead->aadlen + caead->len;
|
2017-09-22 00:15:54 +00:00
|
|
|
crda->crd_inject = caead->aadlen + caead->len;
|
2014-12-12 19:56:36 +00:00
|
|
|
|
|
|
|
crda->crd_alg = cse->mac;
|
|
|
|
crda->crd_key = cse->mackey;
|
|
|
|
crda->crd_klen = cse->mackeylen * 8;
|
|
|
|
|
|
|
|
if (caead->op == COP_ENCRYPT)
|
|
|
|
crde->crd_flags |= CRD_F_ENCRYPT;
|
|
|
|
else
|
|
|
|
crde->crd_flags &= ~CRD_F_ENCRYPT;
|
2017-09-22 00:15:54 +00:00
|
|
|
crde->crd_skip = caead->aadlen;
|
2014-12-12 19:56:36 +00:00
|
|
|
crde->crd_len = caead->len;
|
2017-09-22 00:15:54 +00:00
|
|
|
crde->crd_inject = caead->aadlen;
|
2014-12-12 19:56:36 +00:00
|
|
|
|
|
|
|
crde->crd_alg = cse->cipher;
|
|
|
|
crde->crd_key = cse->key;
|
|
|
|
crde->crd_klen = cse->keylen * 8;
|
|
|
|
|
2017-09-22 00:15:54 +00:00
|
|
|
crp->crp_ilen = caead->aadlen + caead->len;
|
2014-12-12 19:56:36 +00:00
|
|
|
crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
|
|
|
|
| (caead->flags & COP_F_BATCH);
|
2018-01-26 23:21:50 +00:00
|
|
|
crp->crp_uio = &cod->uio;
|
|
|
|
crp->crp_callback = cryptodev_cb;
|
2018-07-18 00:56:25 +00:00
|
|
|
crp->crp_session = cse->cses;
|
2018-01-26 23:21:50 +00:00
|
|
|
crp->crp_opaque = cod;
|
2014-12-12 19:56:36 +00:00
|
|
|
|
|
|
|
if (caead->iv) {
|
2018-01-11 18:07:21 +00:00
|
|
|
if (caead->ivlen > sizeof(crde->crd_iv)) {
|
2014-12-12 19:56:36 +00:00
|
|
|
error = EINVAL;
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2014-12-12 19:56:36 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2018-01-11 18:07:21 +00:00
|
|
|
if ((error = copyin(caead->iv, crde->crd_iv, caead->ivlen))) {
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2014-12-12 19:56:36 +00:00
|
|
|
goto bail;
|
2018-01-11 00:22:24 +00:00
|
|
|
}
|
2014-12-12 19:56:36 +00:00
|
|
|
crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
|
|
|
|
} else {
|
|
|
|
crde->crd_flags |= CRD_F_IV_PRESENT;
|
cryptodev: Match intent for enc_xform ciphers with blocksize != ivsize
No functional change for Skipjack, AES-ICM, Blowfish, CAST-128, Camellia,
DES3, Rijndael128, DES. All of these have identical IV and blocksizes
declared in the associated enc_xform.
Functional changes for:
* AES-GCM: block len of 1, IV len of 12
* AES-XTS: block len of 16, IV len of 8
* NULL: block len of 4, IV len of 0
For these, it seems like the IV specified in the enc_xform is correct (and
the blocksize used before was wrong).
Additionally, the not-yet-OCFed cipher Chacha20 has a logical block length
of 1 byte, and a 16 byte IV + nonce.
Rationalize references to IV lengths to refer to the declared ivsize, rather
than declared blocksize.
Sponsored by: Dell EMC Isilon
2018-03-26 20:30:07 +00:00
|
|
|
crde->crd_skip += cse->txform->ivsize;
|
|
|
|
crde->crd_len -= cse->txform->ivsize;
|
2014-12-12 19:56:36 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 23:21:50 +00:00
|
|
|
if ((error = copyin(caead->tag, (caddr_t)cod->uio.uio_iov[0].iov_base +
|
2018-01-11 00:22:24 +00:00
|
|
|
caead->len + caead->aadlen, cse->thash->hashsize))) {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2014-12-12 19:56:36 +00:00
|
|
|
goto bail;
|
2018-01-11 00:22:24 +00:00
|
|
|
}
|
2019-08-27 21:29:37 +00:00
|
|
|
cryptodev_warn(cse);
|
2014-12-12 19:56:36 +00:00
|
|
|
again:
|
|
|
|
/*
|
|
|
|
* 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);
|
2018-01-11 00:22:24 +00:00
|
|
|
if (error != 0) {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2014-12-12 19:56:36 +00:00
|
|
|
goto bail;
|
2018-01-11 00:22:24 +00:00
|
|
|
}
|
2014-12-12 19:56:36 +00:00
|
|
|
|
2018-01-26 23:21:50 +00:00
|
|
|
mtx_lock(&cse->lock);
|
|
|
|
while (!cod->done)
|
|
|
|
mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
|
|
|
|
mtx_unlock(&cse->lock);
|
|
|
|
|
2014-12-12 19:56:36 +00:00
|
|
|
if (crp->crp_etype == EAGAIN) {
|
|
|
|
crp->crp_etype = 0;
|
|
|
|
crp->crp_flags &= ~CRYPTO_F_DONE;
|
2018-01-26 23:21:50 +00:00
|
|
|
cod->done = false;
|
2014-12-12 19:56:36 +00:00
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (crp->crp_etype != 0) {
|
|
|
|
error = crp->crp_etype;
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2014-12-12 19:56:36 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2017-09-22 00:15:54 +00:00
|
|
|
if (caead->dst && (error = copyout(
|
2018-01-26 23:21:50 +00:00
|
|
|
(caddr_t)cod->uio.uio_iov[0].iov_base + caead->aadlen, caead->dst,
|
2018-01-11 00:22:24 +00:00
|
|
|
caead->len))) {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2014-12-12 19:56:36 +00:00
|
|
|
goto bail;
|
2018-01-11 00:22:24 +00:00
|
|
|
}
|
2014-12-12 19:56:36 +00:00
|
|
|
|
2018-01-26 23:21:50 +00:00
|
|
|
if ((error = copyout((caddr_t)cod->uio.uio_iov[0].iov_base +
|
2018-01-11 00:22:24 +00:00
|
|
|
caead->aadlen + caead->len, caead->tag, cse->thash->hashsize))) {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2014-12-12 19:56:36 +00:00
|
|
|
goto bail;
|
2018-01-11 00:22:24 +00:00
|
|
|
}
|
2014-12-12 19:56:36 +00:00
|
|
|
|
|
|
|
bail:
|
|
|
|
crypto_freereq(crp);
|
2018-01-26 23:21:50 +00:00
|
|
|
if (cod)
|
|
|
|
cod_free(cod);
|
2014-12-12 19:56:36 +00:00
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2002-10-04 20:31:23 +00:00
|
|
|
static int
|
2018-01-26 23:21:50 +00:00
|
|
|
cryptodev_cb(struct cryptop *crp)
|
2002-10-04 20:31:23 +00:00
|
|
|
{
|
2018-01-26 23:21:50 +00:00
|
|
|
struct cryptop_data *cod = crp->crp_opaque;
|
2002-10-04 20:31:23 +00:00
|
|
|
|
2018-01-26 23:21:50 +00:00
|
|
|
/*
|
|
|
|
* Lock to ensure the wakeup() is not missed by the loops
|
|
|
|
* waiting on cod->done in cryptodev_op() and
|
|
|
|
* cryptodev_aead().
|
|
|
|
*/
|
|
|
|
mtx_lock(&cod->cse->lock);
|
|
|
|
cod->done = true;
|
|
|
|
mtx_unlock(&cod->cse->lock);
|
|
|
|
wakeup(cod);
|
2002-10-04 20:31:23 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cryptodevkey_cb(void *op)
|
|
|
|
{
|
|
|
|
struct cryptkop *krp = (struct cryptkop *) op;
|
|
|
|
|
2007-03-21 03:42:51 +00:00
|
|
|
wakeup_one(krp);
|
2002-10-04 20:31:23 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cryptodev_key(struct crypt_kop *kop)
|
|
|
|
{
|
|
|
|
struct cryptkop *krp = NULL;
|
|
|
|
int error = EINVAL;
|
|
|
|
int in, out, size, i;
|
|
|
|
|
|
|
|
if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
return (EFBIG);
|
|
|
|
}
|
|
|
|
|
|
|
|
in = kop->crk_iparams;
|
|
|
|
out = kop->crk_oparams;
|
|
|
|
switch (kop->crk_op) {
|
|
|
|
case CRK_MOD_EXP:
|
|
|
|
if (in == 3 && out == 1)
|
|
|
|
break;
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
return (EINVAL);
|
|
|
|
case CRK_MOD_EXP_CRT:
|
|
|
|
if (in == 6 && out == 1)
|
|
|
|
break;
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
return (EINVAL);
|
|
|
|
case CRK_DSA_SIGN:
|
|
|
|
if (in == 5 && out == 2)
|
|
|
|
break;
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
return (EINVAL);
|
|
|
|
case CRK_DSA_VERIFY:
|
|
|
|
if (in == 7 && out == 0)
|
|
|
|
break;
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
return (EINVAL);
|
|
|
|
case CRK_DH_COMPUTE_KEY:
|
|
|
|
if (in == 3 && out == 1)
|
|
|
|
break;
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
return (EINVAL);
|
|
|
|
default:
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
2007-10-08 20:08:34 +00:00
|
|
|
krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK|M_ZERO);
|
2018-01-11 00:22:24 +00:00
|
|
|
if (!krp) {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
return (ENOMEM);
|
2018-01-11 00:22:24 +00:00
|
|
|
}
|
2002-10-04 20:31:23 +00: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;
|
2007-03-21 03:42:51 +00:00
|
|
|
krp->krp_crid = kop->crk_crid;
|
2002-10-04 20:31:23 +00:00
|
|
|
krp->krp_status = 0;
|
|
|
|
krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
|
|
|
|
|
2007-10-08 20:08:34 +00:00
|
|
|
for (i = 0; i < CRK_MAXPARAM; i++) {
|
2018-01-11 00:22:24 +00:00
|
|
|
if (kop->crk_param[i].crp_nbits > 65536) {
|
2007-10-08 20:08:34 +00:00
|
|
|
/* Limit is the same as in OpenBSD */
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2007-10-08 20:08:34 +00:00
|
|
|
goto fail;
|
2018-01-11 00:22:24 +00:00
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
|
2007-10-08 20:08:34 +00:00
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
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-10-23 15:53:51 +00:00
|
|
|
krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
|
2002-10-04 20:31:23 +00:00
|
|
|
if (i >= krp->krp_iparams)
|
|
|
|
continue;
|
|
|
|
error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
|
2018-01-11 00:22:24 +00:00
|
|
|
if (error) {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
goto fail;
|
2018-01-11 00:22:24 +00:00
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
error = crypto_kdispatch(krp);
|
2018-01-11 00:22:24 +00:00
|
|
|
if (error) {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
goto fail;
|
2018-01-11 00:22:24 +00:00
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
error = tsleep(krp, PSOCK, "crydev", 0);
|
|
|
|
if (error) {
|
|
|
|
/* XXX can this happen? if so, how do we recover? */
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2007-03-21 03:42:51 +00:00
|
|
|
kop->crk_crid = krp->krp_crid; /* device that did the work */
|
2002-10-04 20:31:23 +00:00
|
|
|
if (krp->krp_status != 0) {
|
|
|
|
error = krp->krp_status;
|
2018-01-11 00:22:24 +00:00
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
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 = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
|
2018-01-11 00:22:24 +00:00
|
|
|
if (error) {
|
|
|
|
SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
|
2002-10-04 20:31:23 +00:00
|
|
|
goto fail;
|
2018-01-11 00:22:24 +00:00
|
|
|
}
|
2002-10-04 20:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (krp) {
|
|
|
|
kop->crk_status = krp->krp_status;
|
|
|
|
for (i = 0; i < CRK_MAXPARAM; i++) {
|
|
|
|
if (krp->krp_param[i].crp_p)
|
2008-10-23 15:53:51 +00:00
|
|
|
free(krp->krp_param[i].crp_p, M_XDATA);
|
2002-10-04 20:31:23 +00:00
|
|
|
}
|
|
|
|
free(krp, M_XDATA);
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2007-03-21 03:42:51 +00:00
|
|
|
static int
|
|
|
|
cryptodev_find(struct crypt_find_op *find)
|
|
|
|
{
|
|
|
|
device_t dev;
|
2014-12-12 19:56:36 +00:00
|
|
|
size_t fnlen = sizeof find->name;
|
2007-03-21 03:42:51 +00:00
|
|
|
|
|
|
|
if (find->crid != -1) {
|
|
|
|
dev = crypto_find_device_byhid(find->crid);
|
|
|
|
if (dev == NULL)
|
|
|
|
return (ENOENT);
|
2014-12-12 19:56:36 +00:00
|
|
|
strncpy(find->name, device_get_nameunit(dev), fnlen);
|
|
|
|
find->name[fnlen - 1] = '\x0';
|
2007-03-21 03:42:51 +00:00
|
|
|
} else {
|
2014-12-12 19:56:36 +00:00
|
|
|
find->name[fnlen - 1] = '\x0';
|
2007-03-21 03:42:51 +00:00
|
|
|
find->crid = crypto_find_driver(find->name);
|
|
|
|
if (find->crid == -1)
|
|
|
|
return (ENOENT);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2002-10-04 20:31:23 +00:00
|
|
|
/* ARGSUSED */
|
|
|
|
static int
|
|
|
|
cryptof_stat(
|
|
|
|
struct file *fp,
|
|
|
|
struct stat *sb,
|
|
|
|
struct ucred *active_cred,
|
|
|
|
struct thread *td)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (EOPNOTSUPP);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
static int
|
|
|
|
cryptof_close(struct file *fp, struct thread *td)
|
|
|
|
{
|
2003-01-13 00:33:17 +00:00
|
|
|
struct fcrypt *fcr = fp->f_data;
|
2002-10-04 20:31:23 +00:00
|
|
|
struct csession *cse;
|
|
|
|
|
|
|
|
while ((cse = TAILQ_FIRST(&fcr->csessions))) {
|
|
|
|
TAILQ_REMOVE(&fcr->csessions, cse, next);
|
2018-07-18 00:56:25 +00:00
|
|
|
csefree(cse);
|
2002-10-04 20:31:23 +00:00
|
|
|
}
|
2008-10-23 15:53:51 +00:00
|
|
|
free(fcr, M_XDATA);
|
2003-01-13 00:33:17 +00:00
|
|
|
fp->f_data = NULL;
|
2002-10-04 20:31:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-22 16:20:47 +00:00
|
|
|
static int
|
|
|
|
cryptof_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
|
|
|
|
{
|
|
|
|
|
|
|
|
kif->kf_type = KF_TYPE_CRYPTO;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2002-10-04 20:31:23 +00:00
|
|
|
static struct csession *
|
|
|
|
csefind(struct fcrypt *fcr, u_int ses)
|
|
|
|
{
|
|
|
|
struct csession *cse;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(cse, &fcr->csessions, next)
|
|
|
|
if (cse->ses == ses)
|
|
|
|
return (cse);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
csedelete(struct fcrypt *fcr, struct csession *cse_del)
|
|
|
|
{
|
|
|
|
struct csession *cse;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(cse, &fcr->csessions, next) {
|
|
|
|
if (cse == cse_del) {
|
|
|
|
TAILQ_REMOVE(&fcr->csessions, cse, next);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct csession *
|
|
|
|
cseadd(struct fcrypt *fcr, struct csession *cse)
|
|
|
|
{
|
|
|
|
TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
|
|
|
|
cse->ses = fcr->sesn++;
|
|
|
|
return (cse);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct csession *
|
2018-07-18 00:56:25 +00:00
|
|
|
csecreate(struct fcrypt *fcr, crypto_session_t cses, caddr_t key, u_int64_t keylen,
|
2002-10-04 20:31:23 +00:00
|
|
|
caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
|
|
|
|
struct enc_xform *txform, struct auth_hash *thash)
|
|
|
|
{
|
|
|
|
struct csession *cse;
|
|
|
|
|
2008-10-23 20:26:15 +00:00
|
|
|
cse = malloc(sizeof(struct csession), M_XDATA, M_NOWAIT | M_ZERO);
|
2002-10-04 20:31:23 +00:00
|
|
|
if (cse == NULL)
|
|
|
|
return NULL;
|
2003-06-02 23:28:06 +00:00
|
|
|
mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
|
2002-10-04 20:31:23 +00:00
|
|
|
cse->key = key;
|
|
|
|
cse->keylen = keylen/8;
|
|
|
|
cse->mackey = mackey;
|
|
|
|
cse->mackeylen = mackeylen/8;
|
2018-07-18 00:56:25 +00:00
|
|
|
cse->cses = cses;
|
2002-10-04 20:31:23 +00:00
|
|
|
cse->cipher = cipher;
|
|
|
|
cse->mac = mac;
|
|
|
|
cse->txform = txform;
|
|
|
|
cse->thash = thash;
|
|
|
|
cseadd(fcr, cse);
|
|
|
|
return (cse);
|
|
|
|
}
|
|
|
|
|
2018-07-18 00:56:25 +00:00
|
|
|
static void
|
2002-10-04 20:31:23 +00:00
|
|
|
csefree(struct csession *cse)
|
|
|
|
{
|
|
|
|
|
2018-07-18 00:56:25 +00:00
|
|
|
crypto_freesession(cse->cses);
|
2003-06-02 23:28:06 +00:00
|
|
|
mtx_destroy(&cse->lock);
|
2002-10-04 20:31:23 +00:00
|
|
|
if (cse->key)
|
2008-10-23 15:53:51 +00:00
|
|
|
free(cse->key, M_XDATA);
|
2002-10-04 20:31:23 +00:00
|
|
|
if (cse->mackey)
|
2008-10-23 15:53:51 +00:00
|
|
|
free(cse->mackey, M_XDATA);
|
|
|
|
free(cse, M_XDATA);
|
2002-10-04 20:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-06-16 09:47:26 +00:00
|
|
|
cryptoopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
|
2002-10-04 20:31:23 +00:00
|
|
|
{
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-06-16 09:47:26 +00:00
|
|
|
cryptoread(struct cdev *dev, struct uio *uio, int ioflag)
|
2002-10-04 20:31:23 +00:00
|
|
|
{
|
|
|
|
return (EIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-06-16 09:47:26 +00:00
|
|
|
cryptowrite(struct cdev *dev, struct uio *uio, int ioflag)
|
2002-10-04 20:31:23 +00:00
|
|
|
{
|
|
|
|
return (EIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-06-16 09:47:26 +00:00
|
|
|
cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
|
2002-10-04 20:31:23 +00:00
|
|
|
{
|
|
|
|
struct file *f;
|
|
|
|
struct fcrypt *fcr;
|
|
|
|
int fd, error;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case CRIOGET:
|
2008-10-23 20:26:15 +00:00
|
|
|
fcr = malloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK);
|
2002-10-04 20:31:23 +00:00
|
|
|
TAILQ_INIT(&fcr->csessions);
|
|
|
|
fcr->sesn = 0;
|
|
|
|
|
2011-04-01 13:28:34 +00:00
|
|
|
error = falloc(td, &f, &fd, 0);
|
2002-10-04 20:31:23 +00:00
|
|
|
|
|
|
|
if (error) {
|
2008-10-23 15:53:51 +00:00
|
|
|
free(fcr, M_XDATA);
|
2002-10-04 20:31:23 +00:00
|
|
|
return (error);
|
|
|
|
}
|
2003-10-19 20:41:07 +00:00
|
|
|
/* falloc automatically provides an extra reference to 'f'. */
|
2007-12-30 01:42:15 +00:00
|
|
|
finit(f, FREAD | FWRITE, DTYPE_CRYPTO, fcr, &cryptofops);
|
2002-10-04 20:31:23 +00:00
|
|
|
*(u_int32_t *)data = fd;
|
|
|
|
fdrop(f, td);
|
|
|
|
break;
|
2007-03-21 03:42:51 +00:00
|
|
|
case CRIOFINDDEV:
|
|
|
|
error = cryptodev_find((struct crypt_find_op *)data);
|
|
|
|
break;
|
|
|
|
case CRIOASYMFEAT:
|
|
|
|
error = crypto_getfeat((int *)data);
|
|
|
|
break;
|
2002-10-04 20:31:23 +00:00
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct cdevsw crypto_cdevsw = {
|
2004-02-21 21:10:55 +00:00
|
|
|
.d_version = D_VERSION,
|
|
|
|
.d_flags = D_NEEDGIANT,
|
2003-03-03 12:15:54 +00:00
|
|
|
.d_open = cryptoopen,
|
|
|
|
.d_read = cryptoread,
|
|
|
|
.d_write = cryptowrite,
|
|
|
|
.d_ioctl = cryptoioctl,
|
|
|
|
.d_name = "crypto",
|
2002-10-04 20:31:23 +00:00
|
|
|
};
|
2004-06-16 09:47:26 +00:00
|
|
|
static struct cdev *crypto_dev;
|
2002-10-04 20:31:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialization code, both for static and dynamic loading.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
cryptodev_modevent(module_t mod, int type, void *unused)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case MOD_LOAD:
|
|
|
|
if (bootverbose)
|
|
|
|
printf("crypto: <crypto device>\n");
|
2002-11-08 23:07:41 +00:00
|
|
|
crypto_dev = make_dev(&crypto_cdevsw, 0,
|
2002-10-04 20:31:23 +00:00
|
|
|
UID_ROOT, GID_WHEEL, 0666,
|
|
|
|
"crypto");
|
|
|
|
return 0;
|
|
|
|
case MOD_UNLOAD:
|
|
|
|
/*XXX disallow if active sessions */
|
|
|
|
destroy_dev(crypto_dev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static moduledata_t cryptodev_mod = {
|
|
|
|
"cryptodev",
|
|
|
|
cryptodev_modevent,
|
2012-10-10 08:36:38 +00:00
|
|
|
0
|
2002-10-04 20:31:23 +00:00
|
|
|
};
|
|
|
|
MODULE_VERSION(cryptodev, 1);
|
|
|
|
DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
|
2002-10-16 14:31:34 +00:00
|
|
|
MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
|
2006-02-27 16:56:22 +00:00
|
|
|
MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);
|