This adds Atalla support code to the ENGINE framework. If you have an
Atalla card, you should be able to compile with the "hw-atalla" switch with "./config" or "perl Configure", and then you can use the command- line switch "-engine atalla" inside speed, s_cient and s_server (after checking out note (1)). Notes: (1) I've turned on native name translation when loading the shared- library, but this means that the Unix shared library needs to be libatasi.so rather than atasi.so. I got around this in my testing by creating a symbollic link from /usr/lib/libatasi.so to the real library, but something better will be needed. It also assumes in win32 that the DLL will be called atasi.dll - but as I don't have a win32/atalla environment to try I have no idea yet if this is the case. (2) Currently DSA verifies are not accelerated because I haven't yet got a mod_exp-based variant of BN_mod_exp2_mont() that yields correct results. (3) Currently the "init()" doesn't fail if the shared library can load successfully but the card is not operational. In this case, the ENGINE_init() call will succeed, but all RSA, DSA, DH, and the two BN_*** operations will fail until the ENGINE is switched back to something that does work. I expect to correct this next. (4) Although the API for the Atalla card just has the one crypto function suggesting an RSA private key operation - this is in fact just a straight mod_exp function that ignores all the RSA key parameters except the (private) exponent and modulus. This is why the only accelerator work is taking place inside the mod_exp function and there's no optimisation of RSA private key operations based on CRT etc.
This commit is contained in:
parent
9a4051050c
commit
cc015c48db
@ -23,9 +23,9 @@ APPS=
|
||||
|
||||
LIB=$(TOP)/libcrypto.a
|
||||
LIBSRC= engine_err.c engine_lib.c engine_list.c engine_openssl.c \
|
||||
hw_cswift.c hw_ncipher.c
|
||||
hw_atalla.c hw_cswift.c hw_ncipher.c
|
||||
LIBOBJ= engine_err.o engine_lib.o engine_list.o engine_openssl.o \
|
||||
hw_cswift.o hw_ncipher.o
|
||||
hw_atalla.o hw_cswift.o hw_ncipher.o
|
||||
|
||||
SRC= $(LIBSRC)
|
||||
|
||||
@ -117,6 +117,16 @@ engine_openssl.o: ../../include/openssl/opensslconf.h
|
||||
engine_openssl.o: ../../include/openssl/opensslv.h ../../include/openssl/rand.h
|
||||
engine_openssl.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h
|
||||
engine_openssl.o: ../../include/openssl/stack.h ../cryptlib.h engine_int.h
|
||||
hw_atalla.o: ../../include/openssl/bio.h ../../include/openssl/bn.h
|
||||
hw_atalla.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
|
||||
hw_atalla.o: ../../include/openssl/dh.h ../../include/openssl/dsa.h
|
||||
hw_atalla.o: ../../include/openssl/dso.h ../../include/openssl/e_os.h
|
||||
hw_atalla.o: ../../include/openssl/e_os2.h ../../include/openssl/engine.h
|
||||
hw_atalla.o: ../../include/openssl/err.h ../../include/openssl/lhash.h
|
||||
hw_atalla.o: ../../include/openssl/opensslconf.h
|
||||
hw_atalla.o: ../../include/openssl/opensslv.h ../../include/openssl/rand.h
|
||||
hw_atalla.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h
|
||||
hw_atalla.o: ../../include/openssl/stack.h ../cryptlib.h engine_int.h
|
||||
hw_cswift.o: ../../include/openssl/bio.h ../../include/openssl/bn.h
|
||||
hw_cswift.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
|
||||
hw_cswift.o: ../../include/openssl/dh.h ../../include/openssl/dsa.h
|
||||
|
@ -252,6 +252,10 @@ void ERR_load_ENGINE_strings(void);
|
||||
/* Error codes for the ENGINE functions. */
|
||||
|
||||
/* Function codes. */
|
||||
#define ENGINE_F_ATALLA_FINISH 135
|
||||
#define ENGINE_F_ATALLA_INIT 136
|
||||
#define ENGINE_F_ATALLA_MOD_EXP 137
|
||||
#define ENGINE_F_ATALLA_RSA_MOD_EXP 138
|
||||
#define ENGINE_F_CSWIFT_DSA_SIGN 133
|
||||
#define ENGINE_F_CSWIFT_DSA_VERIFY 134
|
||||
#define ENGINE_F_CSWIFT_FINISH 100
|
||||
|
@ -66,6 +66,10 @@
|
||||
#ifndef NO_ERR
|
||||
static ERR_STRING_DATA ENGINE_str_functs[]=
|
||||
{
|
||||
{ERR_PACK(0,ENGINE_F_ATALLA_FINISH,0), "ATALLA_FINISH"},
|
||||
{ERR_PACK(0,ENGINE_F_ATALLA_INIT,0), "ATALLA_INIT"},
|
||||
{ERR_PACK(0,ENGINE_F_ATALLA_MOD_EXP,0), "ATALLA_MOD_EXP"},
|
||||
{ERR_PACK(0,ENGINE_F_ATALLA_RSA_MOD_EXP,0), "ATALLA_RSA_MOD_EXP"},
|
||||
{ERR_PACK(0,ENGINE_F_CSWIFT_DSA_SIGN,0), "CSWIFT_DSA_SIGN"},
|
||||
{ERR_PACK(0,ENGINE_F_CSWIFT_DSA_VERIFY,0), "CSWIFT_DSA_VERIFY"},
|
||||
{ERR_PACK(0,ENGINE_F_CSWIFT_FINISH,0), "CSWIFT_FINISH"},
|
||||
|
@ -133,6 +133,11 @@ ENGINE *ENGINE_cswift();
|
||||
ENGINE *ENGINE_hwcrhk();
|
||||
#endif /* HW_NCIPHER */
|
||||
|
||||
#ifdef HW_ATALLA
|
||||
/* Returns a structure of atalla methods. */
|
||||
ENGINE *ENGINE_atalla();
|
||||
#endif /* HW_ATALLA */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -192,6 +192,10 @@ static int engine_internal_check(void)
|
||||
#ifdef HW_NCIPHER
|
||||
if(!engine_list_add(ENGINE_hwcrhk()))
|
||||
return 0;
|
||||
#endif /* HW_CSWIFT */
|
||||
#ifdef HW_ATALLA
|
||||
if(!engine_list_add(ENGINE_atalla()))
|
||||
return 0;
|
||||
#endif /* HW_CSWIFT */
|
||||
engine_list_flag = 1;
|
||||
return 1;
|
||||
|
437
crypto/engine/hw_atalla.c
Normal file
437
crypto/engine/hw_atalla.c
Normal file
@ -0,0 +1,437 @@
|
||||
/* crypto/engine/hw_atalla.c */
|
||||
/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
|
||||
* project 2000.
|
||||
*/
|
||||
/* ====================================================================
|
||||
* Copyright (c) 1999 The OpenSSL Project. 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 documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the OpenSSL Project
|
||||
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
|
||||
*
|
||||
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please contact
|
||||
* licensing@OpenSSL.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "OpenSSL"
|
||||
* nor may "OpenSSL" appear in their names without prior written
|
||||
* permission of the OpenSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the OpenSSL Project
|
||||
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED 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 OpenSSL PROJECT OR
|
||||
* ITS 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.
|
||||
* ====================================================================
|
||||
*
|
||||
* This product includes cryptographic software written by Eric Young
|
||||
* (eay@cryptsoft.com). This product includes software written by Tim
|
||||
* Hudson (tjh@cryptsoft.com).
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <openssl/crypto.h>
|
||||
#include "cryptlib.h"
|
||||
#include <openssl/dso.h>
|
||||
#include "engine_int.h"
|
||||
#include <openssl/engine.h>
|
||||
|
||||
#ifdef HW_ATALLA
|
||||
|
||||
#include "vendor_defns/atalla.h"
|
||||
|
||||
static int atalla_init();
|
||||
static int atalla_finish();
|
||||
|
||||
/* BIGNUM stuff */
|
||||
static int atalla_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
|
||||
const BIGNUM *m, BN_CTX *ctx);
|
||||
|
||||
/* RSA stuff */
|
||||
static int atalla_rsa_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa);
|
||||
/* This function is aliased to mod_exp (with the mont stuff dropped). */
|
||||
static int atalla_mod_exp_mont(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
|
||||
const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
|
||||
|
||||
/* DSA stuff */
|
||||
static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
|
||||
BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
|
||||
BN_CTX *ctx, BN_MONT_CTX *in_mont);
|
||||
static int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
|
||||
const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
|
||||
BN_MONT_CTX *m_ctx);
|
||||
|
||||
/* DH stuff */
|
||||
/* This function is alised to mod_exp (with the DH and mont dropped). */
|
||||
static int atalla_mod_exp_dh(DH *dh, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
|
||||
const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
|
||||
|
||||
|
||||
/* Our internal RSA_METHOD that we provide pointers to */
|
||||
static RSA_METHOD atalla_rsa =
|
||||
{
|
||||
"Atalla RSA method",
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
atalla_rsa_mod_exp,
|
||||
atalla_mod_exp_mont,
|
||||
NULL,
|
||||
NULL,
|
||||
0,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
|
||||
/* Our internal DSA_METHOD that we provide pointers to */
|
||||
static DSA_METHOD atalla_dsa =
|
||||
{
|
||||
"Atalla DSA method",
|
||||
NULL, /* dsa_do_sign */
|
||||
NULL, /* dsa_sign_setup */
|
||||
NULL, /* dsa_do_verify */
|
||||
atalla_dsa_mod_exp, /* dsa_mod_exp */
|
||||
atalla_mod_exp_dsa, /* bn_mod_exp */
|
||||
NULL, /* init */
|
||||
NULL, /* finish */
|
||||
0, /* flags */
|
||||
NULL /* app_data */
|
||||
};
|
||||
|
||||
/* Our internal DH_METHOD that we provide pointers to */
|
||||
static DH_METHOD atalla_dh =
|
||||
{
|
||||
"Atalla DH method",
|
||||
NULL,
|
||||
NULL,
|
||||
atalla_mod_exp_dh,
|
||||
NULL,
|
||||
NULL,
|
||||
0,
|
||||
NULL
|
||||
};
|
||||
|
||||
/* Our ENGINE structure. */
|
||||
static ENGINE engine_atalla =
|
||||
{
|
||||
"atalla",
|
||||
"Atalla hardware engine support",
|
||||
&atalla_rsa,
|
||||
&atalla_dsa,
|
||||
&atalla_dh,
|
||||
NULL,
|
||||
atalla_mod_exp,
|
||||
NULL,
|
||||
atalla_init,
|
||||
atalla_finish,
|
||||
0, /* no flags */
|
||||
0, 0, /* no references */
|
||||
NULL, NULL /* unlinked */
|
||||
};
|
||||
|
||||
/* As this is only ever called once, there's no need for locking
|
||||
* (indeed - the lock will already be held by our caller!!!) */
|
||||
ENGINE *ENGINE_atalla()
|
||||
{
|
||||
RSA_METHOD *meth1;
|
||||
DSA_METHOD *meth2;
|
||||
DH_METHOD *meth3;
|
||||
|
||||
/* We know that the "PKCS1_SSLeay()" functions hook properly
|
||||
* to the atalla-specific mod_exp and mod_exp_crt so we use
|
||||
* those functions. NB: We don't use ENGINE_openssl() or
|
||||
* anything "more generic" because something like the RSAref
|
||||
* code may not hook properly, and if you own one of these
|
||||
* cards then you have the right to do RSA operations on it
|
||||
* anyway! */
|
||||
meth1 = RSA_PKCS1_SSLeay();
|
||||
atalla_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
|
||||
atalla_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
|
||||
atalla_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
|
||||
atalla_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
|
||||
|
||||
/* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
|
||||
* bits. */
|
||||
meth2 = DSA_OpenSSL();
|
||||
atalla_dsa.dsa_do_sign = meth2->dsa_do_sign;
|
||||
atalla_dsa.dsa_sign_setup = meth2->dsa_sign_setup;
|
||||
atalla_dsa.dsa_do_verify = meth2->dsa_do_verify;
|
||||
|
||||
/* Much the same for Diffie-Hellman */
|
||||
meth3 = DH_OpenSSL();
|
||||
atalla_dh.generate_key = meth3->generate_key;
|
||||
atalla_dh.compute_key = meth3->compute_key;
|
||||
return &engine_atalla;
|
||||
}
|
||||
|
||||
/* This is a process-global DSO handle used for loading and unloading
|
||||
* the Atalla library. NB: This is only set (or unset) during an
|
||||
* init() or finish() call (reference counts permitting) and they're
|
||||
* operating with global locks, so this should be thread-safe
|
||||
* implicitly. */
|
||||
static DSO *atalla_dso = NULL;
|
||||
|
||||
/* These are the function pointers that are (un)set when the library has
|
||||
* successfully (un)loaded. */
|
||||
static tfnASI_GetHardwareConfig *p_Atalla_GetHardwareConfig = NULL;
|
||||
static tfnASI_RSAPrivateKeyOpFn *p_Atalla_RSAPrivateKeyOpFn = NULL;
|
||||
static tfnASI_GetPerformanceStatistics *p_Atalla_GetPerformanceStatistics = NULL;
|
||||
|
||||
/* (de)initialisation functions. */
|
||||
static int atalla_init()
|
||||
{
|
||||
tfnASI_GetHardwareConfig *p1;
|
||||
tfnASI_RSAPrivateKeyOpFn *p2;
|
||||
tfnASI_GetPerformanceStatistics *p3;
|
||||
|
||||
if(atalla_dso != NULL)
|
||||
{
|
||||
ENGINEerr(ENGINE_F_ATALLA_INIT,ENGINE_R_ALREADY_LOADED);
|
||||
goto err;
|
||||
}
|
||||
/* Attempt to load libatasi.so/atasi.dll/whatever. Needs to be
|
||||
* changed unfortunately because the Atalla drivers don't have
|
||||
* standard library names that can be platform-translated well. */
|
||||
/* TODO: Work out how to actually map to the names the Atalla
|
||||
* drivers really use - for now a symbollic link needs to be
|
||||
* created on the host system from libatasi.so to atasi.so on
|
||||
* unix variants. */
|
||||
atalla_dso = DSO_load(NULL, ATALLA_LIBNAME, NULL,
|
||||
DSO_FLAG_NAME_TRANSLATION);
|
||||
if(atalla_dso == NULL)
|
||||
{
|
||||
ENGINEerr(ENGINE_F_ATALLA_INIT,ENGINE_R_DSO_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
if(!(p1 = (tfnASI_GetHardwareConfig *)DSO_bind(
|
||||
atalla_dso, ATALLA_F1)) ||
|
||||
!(p2 = (tfnASI_RSAPrivateKeyOpFn *)DSO_bind(
|
||||
atalla_dso, ATALLA_F2)) ||
|
||||
!(p3 = (tfnASI_GetPerformanceStatistics *)DSO_bind(
|
||||
atalla_dso, ATALLA_F3)))
|
||||
{
|
||||
ENGINEerr(ENGINE_F_ATALLA_INIT,ENGINE_R_DSO_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
/* Copy the pointers */
|
||||
p_Atalla_GetHardwareConfig = p1;
|
||||
p_Atalla_RSAPrivateKeyOpFn = p2;
|
||||
p_Atalla_GetPerformanceStatistics = p3;
|
||||
/* TODO: Use p1 or p3 to test if we can actually talk to the
|
||||
* accelerator, similar to what the CryptoSwift code does at
|
||||
* the equivalent point. */
|
||||
#if 0
|
||||
if(!check_all_is_cool_function())
|
||||
{
|
||||
ENGINEerr(ENGINE_F_ATALLA_INIT,ENGINE_R_UNIT_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
#endif
|
||||
/* Everything's fine. */
|
||||
return 1;
|
||||
err:
|
||||
if(atalla_dso)
|
||||
DSO_free(atalla_dso);
|
||||
p_Atalla_GetHardwareConfig = NULL;
|
||||
p_Atalla_RSAPrivateKeyOpFn = NULL;
|
||||
p_Atalla_GetPerformanceStatistics = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int atalla_finish()
|
||||
{
|
||||
if(atalla_dso == NULL)
|
||||
{
|
||||
ENGINEerr(ENGINE_F_ATALLA_FINISH,ENGINE_R_NOT_LOADED);
|
||||
return 0;
|
||||
}
|
||||
if(!DSO_free(atalla_dso))
|
||||
{
|
||||
ENGINEerr(ENGINE_F_ATALLA_FINISH,ENGINE_R_DSO_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
atalla_dso = NULL;
|
||||
p_Atalla_GetHardwareConfig = NULL;
|
||||
p_Atalla_RSAPrivateKeyOpFn = NULL;
|
||||
p_Atalla_GetPerformanceStatistics = NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int atalla_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
|
||||
const BIGNUM *m, BN_CTX *ctx)
|
||||
{
|
||||
/* I need somewhere to store temporary serialised values for
|
||||
* use with the Atalla API calls. A neat cheat - I'll use
|
||||
* BIGNUMs from the BN_CTX but access their arrays directly as
|
||||
* byte arrays <grin>. This way I don't have to clean anything
|
||||
* up. */
|
||||
BIGNUM *modulus;
|
||||
BIGNUM *exponent;
|
||||
BIGNUM *argument;
|
||||
BIGNUM *result;
|
||||
RSAPrivateKey keydata;
|
||||
int to_return, numbytes;
|
||||
|
||||
modulus = exponent = argument = result = NULL;
|
||||
to_return = 0; /* expect failure */
|
||||
|
||||
if(!atalla_dso)
|
||||
{
|
||||
ENGINEerr(ENGINE_F_ATALLA_MOD_EXP,ENGINE_R_NOT_LOADED);
|
||||
goto err;
|
||||
}
|
||||
/* Prepare the params */
|
||||
modulus = BN_CTX_get(ctx);
|
||||
exponent = BN_CTX_get(ctx);
|
||||
argument = BN_CTX_get(ctx);
|
||||
result = BN_CTX_get(ctx);
|
||||
if(!modulus || !exponent || !argument || !result)
|
||||
{
|
||||
ENGINEerr(ENGINE_F_ATALLA_MOD_EXP,ENGINE_R_BN_CTX_FULL);
|
||||
goto err;
|
||||
}
|
||||
if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, m->top) ||
|
||||
!bn_wexpand(argument, m->top) || !bn_wexpand(result, m->top))
|
||||
{
|
||||
ENGINEerr(ENGINE_F_ATALLA_MOD_EXP,ENGINE_R_BN_EXPAND_FAIL);
|
||||
goto err;
|
||||
}
|
||||
/* Prepare the key-data */
|
||||
memset(&keydata, 0,sizeof keydata);
|
||||
numbytes = BN_num_bytes(m);
|
||||
memset(exponent->d, 0, numbytes);
|
||||
memset(modulus->d, 0, numbytes);
|
||||
BN_bn2bin(p, (unsigned char *)exponent->d + numbytes - BN_num_bytes(p));
|
||||
BN_bn2bin(m, (unsigned char *)modulus->d + numbytes - BN_num_bytes(m));
|
||||
keydata.privateExponent.data = (unsigned char *)exponent->d;
|
||||
keydata.privateExponent.len = numbytes;
|
||||
keydata.modulus.data = (unsigned char *)modulus->d;
|
||||
keydata.modulus.len = numbytes;
|
||||
/* Prepare the argument */
|
||||
memset(argument->d, 0, numbytes);
|
||||
memset(result->d, 0, numbytes);
|
||||
BN_bn2bin(a, (unsigned char *)argument->d + numbytes - BN_num_bytes(a));
|
||||
/* Perform the operation */
|
||||
if(p_Atalla_RSAPrivateKeyOpFn(&keydata, (unsigned char *)result->d,
|
||||
(unsigned char *)argument->d,
|
||||
keydata.modulus.len) != 0)
|
||||
{
|
||||
ENGINEerr(ENGINE_F_ATALLA_MOD_EXP,ENGINE_R_REQUEST_FAILED);
|
||||
goto err;
|
||||
}
|
||||
/* Convert the response */
|
||||
BN_bin2bn((unsigned char *)result->d, numbytes, r);
|
||||
to_return = 1;
|
||||
err:
|
||||
if(modulus) ctx->tos--;
|
||||
if(exponent) ctx->tos--;
|
||||
if(argument) ctx->tos--;
|
||||
if(result) ctx->tos--;
|
||||
return to_return;
|
||||
}
|
||||
|
||||
static int atalla_rsa_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa)
|
||||
{
|
||||
BN_CTX *ctx = NULL;
|
||||
int to_return = 0;
|
||||
|
||||
if(!atalla_dso)
|
||||
{
|
||||
ENGINEerr(ENGINE_F_ATALLA_RSA_MOD_EXP,ENGINE_R_NOT_LOADED);
|
||||
goto err;
|
||||
}
|
||||
if((ctx = BN_CTX_new()) == NULL)
|
||||
goto err;
|
||||
if(!rsa->d || !rsa->n)
|
||||
{
|
||||
ENGINEerr(ENGINE_F_ATALLA_RSA_MOD_EXP,ENGINE_R_MISSING_KEY_COMPONENTS);
|
||||
goto err;
|
||||
}
|
||||
to_return = atalla_mod_exp(r0, I, rsa->d, rsa->n, ctx);
|
||||
err:
|
||||
if(ctx)
|
||||
BN_CTX_free(ctx);
|
||||
return to_return;
|
||||
}
|
||||
|
||||
/* This code was liberated and adapted from the commented-out code in
|
||||
* dsa_ossl.c but doesn't work (for me at least). So I need to work out
|
||||
* some mod_exp-based variant of BN_mod_exp2_mont ... for now we resort
|
||||
* to software which should only hurt DSA clients, or DSA servers doing
|
||||
* client-authentication. */
|
||||
static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
|
||||
BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
|
||||
BN_CTX *ctx, BN_MONT_CTX *in_mont)
|
||||
{
|
||||
#if 0
|
||||
BIGNUM t;
|
||||
int to_return = 0;
|
||||
|
||||
BN_init(&t);
|
||||
/* let rr = a1 ^ p1 mod m */
|
||||
if (!atalla_mod_exp(rr,a1,p1,m,ctx)) goto end;
|
||||
/* let t = a2 ^ p2 mod m */
|
||||
if (!atalla_mod_exp(&t,a2,p2,m,ctx)) goto end;
|
||||
/* let p1 = rr * t mod m */
|
||||
if (!BN_mod_mul(p1,rr,&t,m,ctx)) goto end;
|
||||
to_return = 1;
|
||||
end:
|
||||
BN_free(&t);
|
||||
return to_return;
|
||||
#else
|
||||
return BN_mod_exp2_mont(rr, a1, p1, a2, p2, m, ctx, in_mont);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
static int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
|
||||
const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
|
||||
BN_MONT_CTX *m_ctx)
|
||||
{
|
||||
return atalla_mod_exp(r, a, p, m, ctx);
|
||||
}
|
||||
|
||||
/* This function is aliased to mod_exp (with the mont stuff dropped). */
|
||||
static int atalla_mod_exp_mont(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
|
||||
const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
|
||||
{
|
||||
return atalla_mod_exp(r, a, p, m, ctx);
|
||||
}
|
||||
|
||||
/* This function is aliased to mod_exp (with the dh and mont dropped). */
|
||||
static int atalla_mod_exp_dh(DH *dh, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
|
||||
const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
|
||||
{
|
||||
return atalla_mod_exp(r, a, p, m, ctx);
|
||||
}
|
||||
|
||||
#endif /* HW_ATALLA */
|
61
crypto/engine/vendor_defns/atalla.h
Normal file
61
crypto/engine/vendor_defns/atalla.h
Normal file
@ -0,0 +1,61 @@
|
||||
/* This header declares the necessary definitions for using the exponentiation
|
||||
* acceleration capabilities of Atalla cards. The only cryptographic operation
|
||||
* is performed by "ASI_RSAPrivateKeyOpFn" and this takes a structure that
|
||||
* defines an "RSA private key". However, it is really only performing a
|
||||
* regular mod_exp using the supplied modulus and exponent - no CRT form is
|
||||
* being used. Hence, it is a generic mod_exp function in disguise, and we use
|
||||
* it as such.
|
||||
*
|
||||
* Thanks to the people at Atalla for letting me know these definitions are
|
||||
* fine and that they can be reproduced here.
|
||||
*
|
||||
* Geoff.
|
||||
*/
|
||||
|
||||
typedef struct ItemStr
|
||||
{
|
||||
unsigned char *data;
|
||||
int len;
|
||||
} Item;
|
||||
|
||||
typedef struct RSAPrivateKeyStr
|
||||
{
|
||||
void *reserved;
|
||||
Item version;
|
||||
Item modulus;
|
||||
Item publicExponent;
|
||||
Item privateExponent;
|
||||
Item prime[2];
|
||||
Item exponent[2];
|
||||
Item coefficient;
|
||||
} RSAPrivateKey;
|
||||
|
||||
/* Predeclare the function pointer types that we dynamically load from the DSO.
|
||||
* These use the same names and form that Ben's original support code had (in
|
||||
* crypto/bn/bn_exp.c) unless of course I've inadvertently changed the style
|
||||
* somewhere along the way!
|
||||
*/
|
||||
|
||||
typedef int tfnASI_GetPerformanceStatistics(int reset_flag,
|
||||
unsigned int *ret_buf);
|
||||
|
||||
typedef int tfnASI_GetHardwareConfig(long card_num, unsigned int *ret_buf);
|
||||
|
||||
typedef int tfnASI_RSAPrivateKeyOpFn(RSAPrivateKey * rsaKey,
|
||||
unsigned char *output,
|
||||
unsigned char *input,
|
||||
unsigned int modulus_len);
|
||||
|
||||
/* These are the static string constants for the DSO file name and the function
|
||||
* symbol names to bind to. Regrettably, the DSO name on *nix appears to be
|
||||
* "atasi.so" rather than something more consistent like "libatasi.so". At the
|
||||
* time of writing, I'm not sure what the file name on win32 is but clearly
|
||||
* native name translation is not possible (eg libatasi.so on *nix, and
|
||||
* atasi.dll on win32). For the purposes of testing, I have created a symbollic
|
||||
* link called "libatasi.so" so that we can use native name-translation - a
|
||||
* better solution will be needed. */
|
||||
static const char *ATALLA_LIBNAME = "atasi";
|
||||
static const char *ATALLA_F1 = "ASI_GetHardwareConfig";
|
||||
static const char *ATALLA_F2 = "ASI_RSAPrivateKeyOpFn";
|
||||
static const char *ATALLA_F3 = "ASI_GetPerformanceStatistics";
|
||||
|
Loading…
x
Reference in New Issue
Block a user