eng_cryptodev.c revision 9dc0df1bac950d6e491f9a7c7e4888f2b301cb15
/*
* Copyright (c) 2002 Bob Beck <beck@openbsd.org>
* Copyright (c) 2002 Theo de Raadt
* Copyright (c) 2002 Markus Friedl
* All rights reserved.
*
* 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
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*
*/
(defined(OpenBSD) || defined(__FreeBSD_version))
# if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041)
# define HAVE_CRYPTODEV
# endif
# if (OpenBSD >= 200110)
# define HAVE_SYSLOG_R
# endif
#endif
#ifndef HAVE_CRYPTODEV
void
ENGINE_load_cryptodev(void)
{
return;
}
#else
#include <crypto/cryptodev.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdarg.h>
#include <syslog.h>
#include <errno.h>
#include <string.h>
struct dev_crypto_state {
struct session_op d_sess;
int d_fd;
};
static u_int32_t cryptodev_asymfeat = 0;
static int get_asym_dev_crypto(void);
static int open_dev_crypto(void);
static int get_dev_crypto(void);
static int cryptodev_max_iv(int cipher);
static int cipher_nid_to_cryptodev(int nid);
static int get_cryptodev_ciphers(const int **cnids);
static int get_cryptodev_digests(const int **cnids);
static int cryptodev_usable_ciphers(const int **nids);
static int cryptodev_usable_digests(const int **nids);
BN_MONT_CTX *m_ctx);
static int cryptodev_dh_compute_key(unsigned char *key,
void (*f)());
void ENGINE_load_cryptodev(void);
static const ENGINE_CMD_DEFN cryptodev_defns[] = {
};
static struct {
int id;
int nid;
int ivmax;
int keylen;
} ciphers[] = {
{ CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0, },
{ 0, NID_undef, 0, 0, },
};
static struct {
int id;
int nid;
} digests[] = {
{ CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, },
{ CRYPTO_MD5_KPDK, NID_undef, },
{ CRYPTO_SHA1_KPDK, NID_undef, },
{ CRYPTO_MD5, NID_md5, },
{ CRYPTO_SHA1, NID_undef, },
{ 0, NID_undef, },
};
/*
*/
static int
open_dev_crypto(void)
{
static int fd = -1;
if (fd == -1) {
return (-1);
/* close on exec */
fd = -1;
return (-1);
}
}
return (fd);
}
static int
get_dev_crypto(void)
{
return (-1);
return (-1);
/* close on exec */
return (-1);
}
return (retfd);
}
/* Caching version for asym operations */
static int
get_asym_dev_crypto(void)
{
static int fd = -1;
if (fd == -1)
fd = get_dev_crypto();
return fd;
}
/*
* XXXX this needs to be set for each alg - and determined from
* a running card.
*/
static int
cryptodev_max_iv(int cipher)
{
int i;
return (0);
}
/*
* XXXX this needs to be set for each alg - and determined from
* a running card. For now, fake it out - but most of these
* for real devices should return 1 for the supported key
* sizes the device can handle.
*/
static int
{
int i;
return (0);
}
/* convert libcrypto nids to cryptodev */
static int
{
int i;
return (0);
}
/*
* XXX note, that some of these openssl doesn't deal with yet!
* returning them here is harmless, as long as we return NULL
* when asked for a handler in the cryptodev_engine_ciphers routine
*/
static int
get_cryptodev_ciphers(const int **cnids)
{
static int nids[CRYPTO_ALGORITHM_MAX];
struct session_op sess;
if ((fd = get_dev_crypto()) < 0) {
return (0);
}
continue;
}
if (count > 0)
else
return (count);
}
/*
* XXX note, that some of these openssl doesn't deal with yet!
* returning them here is harmless, as long as we return NULL
* when asked for a handler in the cryptodev_engine_digests routine
*/
static int
get_cryptodev_digests(const int **cnids)
{
static int nids[CRYPTO_ALGORITHM_MAX];
struct session_op sess;
if ((fd = get_dev_crypto()) < 0) {
return (0);
}
continue;
}
if (count > 0)
else
return (count);
}
/*
* thing called by the engine init crud which determines what it
* can use for ciphers from this engine. We want to return
* only what we can do, anythine else is handled by software.
*
* If we can't initialize the device to do anything useful for
* any reason, we want to return a NULL array, and 0 length,
* which forces everything to be done is software. By putting
* the initalization of the device in here, we ensure we can
* use this engine as the default, and if for whatever reason
* software
*
* This can (should) be greatly expanded to perhaps take into
* account speed of the device, and what we want to do.
* (although the disabling of particular alg's could be controlled
* by the device driver with sysctl's.) - this is where we
* want most of the decisions made about what we actually want
*/
static int
cryptodev_usable_ciphers(const int **nids)
{
return (get_cryptodev_ciphers(nids));
}
static int
cryptodev_usable_digests(const int **nids)
{
/*
* XXXX just disable all digests for now, because it sucks.
* we need a better way to decide this - i.e. I may not
* want digests on slow cards like hifn on fast machines,
* but might want them on slow or loaded machines, etc.
* will also want them when using crypto cards that don't
* suck moose gonads - would be nice to be able to decide something
* as reasonable default without having hackery that's card dependent.
* of course, the default should probably be just do everything,
* with perhaps a sysctl to turn algoritms off (or have them off
* by default) on cards that generally suck like the hifn.
*/
return (0);
}
static int
{
void *iiv;
unsigned char save_iv[EVP_MAX_IV_LENGTH];
return (0);
if (!inl)
return (1);
return (0);
}
} else
/* XXX need better errror handling
* this can fail for a number of different reasons.
*/
return (0);
}
else
}
return (1);
}
static int
{
int cipher;
return (0);
return (0);
return (0);
return (0);
return (0);
}
return (1);
}
/*
* free anything we allocated earlier when initting a
* session, and close the session.
*/
static int
{
int ret = 0;
return (0);
/* XXX if this ioctl fails, someting's wrong. the invoker
* may have called us with a bogus ctx, or we could
* have a device that for whatever reason just doesn't
* want to play ball - it's not clear what's right
* here - should this be an error? should it just
* increase a counter, hmm. For right now, we return
* 0 - I don't believe that to be "right". we could
* call the gorpy openssl lib error handlers that
* print messages to users of the library. hmm..
*/
ret = 0;
} else {
ret = 1;
}
return (ret);
}
/*
* libcrypto EVP stuff - this is how we get wired to EVP so the engine
* gets called when libcrypto requests a cipher NID.
*/
/* DES CBC EVP */
const EVP_CIPHER cryptodev_des_cbc = {
8, 8, 8,
sizeof(struct dev_crypto_state),
};
/* 3DES CBC EVP */
const EVP_CIPHER cryptodev_3des_cbc = {
8, 24, 8,
sizeof(struct dev_crypto_state),
};
const EVP_CIPHER cryptodev_bf_cbc = {
8, 16, 8,
sizeof(struct dev_crypto_state),
};
const EVP_CIPHER cryptodev_cast_cbc = {
8, 16, 8,
sizeof(struct dev_crypto_state),
};
const EVP_CIPHER cryptodev_aes_cbc = {
16, 16, 16,
sizeof(struct dev_crypto_state),
};
/*
* Registered by the ENGINE when used to find out how to deal with
* a particular NID in the ENGINE. this says what we'll do at the
* top level - note, that list is restricted by what we answer with
*/
static int
{
if (!cipher)
return (cryptodev_usable_ciphers(nids));
switch (nid) {
case NID_des_ede3_cbc:
*cipher = &cryptodev_3des_cbc;
break;
case NID_des_cbc:
*cipher = &cryptodev_des_cbc;
break;
case NID_bf_cbc:
*cipher = &cryptodev_bf_cbc;
break;
case NID_cast5_cbc:
*cipher = &cryptodev_cast_cbc;
break;
case NID_aes_128_cbc:
*cipher = &cryptodev_aes_cbc;
break;
default:
break;
}
}
static int
{
if (!digest)
return (cryptodev_usable_digests(nids));
switch (nid) {
case NID_md5:
break;
default:
break;
}
}
/*
* Upon completion of use, the caller is responsible for freeing
* crp->crp_p.
*/
static int
{
int i, j, k;
u_char *b;
bits = BN_num_bits(a);
if (b == NULL)
return (1);
for (i = 0, j = 0; i < a->top; i++) {
for (k = 0; k < BN_BITS2 / 8; k++) {
if ((j + k) >= bytes)
return (0);
b[j + k] = a->d[i] >> (k * 8);
}
j += BN_BITS2 / 8;
}
return (0);
}
static int
{
int i, bytes;
if (bytes == 0)
return (-1);
return (-1);
for (i = 0; i < bytes; i++)
return (0);
}
static void
{
int i;
}
}
static int
{
if ((fd = get_asym_dev_crypto()) < 0)
return (ret);
if (r) {
kop->crk_oparams++;
}
if (s) {
kop->crk_oparams++;
}
if (r)
if (s)
ret = 0;
}
return (ret);
}
static int
{
int ret = 1;
/* Currently, we know we can do mod exp iff we can do any
* asymmetric operations at all.
*/
if (cryptodev_asymfeat == 0) {
return (ret);
}
/* inputs: a^p % m */
goto err;
goto err;
goto err;
}
err:
return (ret);
}
static int
{
int r;
ctx = BN_CTX_new();
return (r);
}
static int
{
int ret = 1;
/* XXX 0 means failure?? */
return (0);
}
/* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
goto err;
goto err;
goto err;
goto err;
goto err;
goto err;
}
err:
return (ret);
}
static RSA_METHOD cryptodev_rsa = {
"cryptodev RSA method",
NULL, /* rsa_pub_enc */
NULL, /* rsa_pub_dec */
NULL, /* rsa_priv_enc */
NULL, /* rsa_priv_dec */
NULL,
NULL,
NULL, /* init */
NULL, /* finish */
0, /* flags */
NULL, /* app_data */
NULL, /* rsa_sign */
NULL /* rsa_verify */
};
static int
{
}
static int
{
int ret = 0;
/* v = ( g^u1 * y^u2 mod p ) mod q */
/* let t1 = g ^ u1 mod p */
ret = 0;
goto err;
/* let t2 = y ^ u2 mod p */
goto err;
/* let u1 = t1 * t2 mod p */
goto err;
ret = 1;
err:
return(ret);
}
static DSA_SIG *
{
goto err;
BN_free(r);
goto err;
}
/* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
goto err;
goto err;
goto err;
goto err;
BN_num_bytes(dsa->q), s) == 0) {
dsaret = DSA_SIG_new();
dsaret->r = r;
dsaret->s = s;
} else {
BN_free(r);
BN_free(s);
}
err:
return (dsaret);
}
static int
{
int dsaret = 1;
/* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
goto err;
goto err;
goto err;
goto err;
goto err;
goto err;
} else {
}
err:
return (dsaret);
}
static DSA_METHOD cryptodev_dsa = {
"cryptodev DSA method",
NULL,
NULL, /* dsa_sign_setup */
NULL,
NULL, /* dsa_mod_exp */
NULL,
NULL, /* init */
NULL, /* finish */
0, /* flags */
NULL /* app_data */
};
static int
{
}
static int
{
int dhret = 1;
if ((fd = get_asym_dev_crypto()) < 0) {
}
/* inputs: dh->priv_key pub_key dh->p key */
goto err;
goto err;
goto err;
}
err:
return (dhret);
}
static DH_METHOD cryptodev_dh = {
"cryptodev DH method",
NULL, /* cryptodev_dh_generate_key */
NULL,
NULL,
NULL,
NULL,
0, /* flags */
NULL /* app_data */
};
/*
* ctrl right now is just a wrapper that doesn't do much
* but I expect we'll want some options soon.
*/
static int
{
#ifdef HAVE_SYSLOG_R
#endif
switch (cmd) {
default:
#ifdef HAVE_SYSLOG_R
"cryptodev_ctrl: unknown command %d", cmd);
#else
#endif
break;
}
return (1);
}
void
ENGINE_load_cryptodev(void)
{
int fd;
return;
if ((fd = get_dev_crypto()) < 0) {
return;
}
/*
* find out what asymmetric crypto algorithms we support
*/
return;
}
return;
}
if (cryptodev_asymfeat & CRF_MOD_EXP) {
else
}
}
if (cryptodev_asymfeat & CRF_DSA_SIGN)
if (cryptodev_asymfeat & CRF_MOD_EXP) {
}
if (cryptodev_asymfeat & CRF_DSA_VERIFY)
}
if (cryptodev_asymfeat & CRF_MOD_EXP) {
}
}
}
#endif /* HAVE_CRYPTODEV */