1 /* crypto/engine/hw_aep.c */
4 /* ====================================================================
5 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * licensing@OpenSSL.org.
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com).
59 #include <openssl/bn.h>
62 #include <openssl/e_os2.h>
63 #if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
64 #include <sys/types.h>
71 #include <openssl/crypto.h>
72 #include <openssl/dso.h>
73 #include <openssl/engine.h>
74 #include <openssl/buffer.h>
77 #ifndef OPENSSL_NO_HW_AEP
81 #include "vendor_defns/aep.h"
84 #define AEP_LIB_NAME "aep engine"
85 #define FAIL_TO_SW 0x10101010
87 #include "e_aep_err.c"
89 static int aep_init(ENGINE *e);
90 static int aep_finish(ENGINE *e);
91 static int aep_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
92 static int aep_destroy(ENGINE *e);
94 static AEP_RV aep_get_connection(AEP_CONNECTION_HNDL_PTR hConnection);
95 static AEP_RV aep_return_connection(AEP_CONNECTION_HNDL hConnection);
96 static AEP_RV aep_close_connection(AEP_CONNECTION_HNDL hConnection);
97 static AEP_RV aep_close_all_connections(int use_engine_lock, int *in_use);
100 static int aep_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
101 const BIGNUM *m, BN_CTX *ctx);
103 static AEP_RV aep_mod_exp_crt(BIGNUM *r,const BIGNUM *a, const BIGNUM *p,
104 const BIGNUM *q, const BIGNUM *dmp1,const BIGNUM *dmq1,
105 const BIGNUM *iqmp, BN_CTX *ctx);
108 #ifndef OPENSSL_NO_RSA
109 static int aep_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
112 /* This function is aliased to mod_exp (with the mont stuff dropped). */
113 static int aep_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
114 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
117 #ifndef OPENSSL_NO_DSA
118 static int aep_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
119 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
120 BN_CTX *ctx, BN_MONT_CTX *in_mont);
122 static int aep_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
123 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
128 /* This function is aliased to mod_exp (with the DH and mont dropped). */
129 #ifndef OPENSSL_NO_DH
130 static int aep_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
131 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
136 static int aep_rand(unsigned char *buf, int num);
137 static int aep_rand_status(void);
140 /* Bignum conversion stuff */
141 static AEP_RV GetBigNumSize(AEP_VOID_PTR ArbBigNum, AEP_U32* BigNumSize);
142 static AEP_RV MakeAEPBigNum(AEP_VOID_PTR ArbBigNum, AEP_U32 BigNumSize,
143 unsigned char* AEP_BigNum);
144 static AEP_RV ConvertAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
145 unsigned char* AEP_BigNum);
147 /* The definitions for control commands specific to this engine */
148 #define AEP_CMD_SO_PATH ENGINE_CMD_BASE
149 static const ENGINE_CMD_DEFN aep_cmd_defns[] =
153 "Specifies the path to the 'aep' shared library",
154 ENGINE_CMD_FLAG_STRING
159 #ifndef OPENSSL_NO_RSA
160 /* Our internal RSA_METHOD that we provide pointers to */
161 static RSA_METHOD aep_rsa =
164 NULL, /*rsa_pub_encrypt*/
165 NULL, /*rsa_pub_decrypt*/
166 NULL, /*rsa_priv_encrypt*/
167 NULL, /*rsa_priv_encrypt*/
168 aep_rsa_mod_exp, /*rsa_mod_exp*/
169 aep_mod_exp_mont, /*bn_mod_exp*/
180 #ifndef OPENSSL_NO_DSA
181 /* Our internal DSA_METHOD that we provide pointers to */
182 static DSA_METHOD aep_dsa =
185 NULL, /* dsa_do_sign */
186 NULL, /* dsa_sign_setup */
187 NULL, /* dsa_do_verify */
188 aep_dsa_mod_exp, /* dsa_mod_exp */
189 aep_mod_exp_dsa, /* bn_mod_exp */
194 NULL, /* dsa_paramgen */
195 NULL /* dsa_keygen */
199 #ifndef OPENSSL_NO_DH
200 /* Our internal DH_METHOD that we provide pointers to */
201 static DH_METHOD aep_dh =
216 /* our internal RAND_method that we provide pointers to */
217 static RAND_METHOD aep_random =
219 /*"AEP RAND method", */
229 /*Define an array of structures to hold connections*/
230 static AEP_CONNECTION_ENTRY aep_app_conn_table[MAX_PROCESS_CONNECTIONS];
232 /*Used to determine if this is a new process*/
233 static pid_t recorded_pid = 0;
236 static AEP_U8 rand_block[RAND_BLK_SIZE];
237 static AEP_U32 rand_block_bytes = 0;
240 /* Constants used when creating the ENGINE */
241 static const char *engine_aep_id = "aep";
242 static const char *engine_aep_name = "Aep hardware engine support";
244 static int max_key_len = 2176;
247 /* This internal function is used by ENGINE_aep() and possibly by the
248 * "dynamic" ENGINE support too */
249 static int bind_aep(ENGINE *e)
251 #ifndef OPENSSL_NO_RSA
252 const RSA_METHOD *meth1;
254 #ifndef OPENSSL_NO_DSA
255 const DSA_METHOD *meth2;
257 #ifndef OPENSSL_NO_DH
258 const DH_METHOD *meth3;
261 if(!ENGINE_set_id(e, engine_aep_id) ||
262 !ENGINE_set_name(e, engine_aep_name) ||
263 #ifndef OPENSSL_NO_RSA
264 !ENGINE_set_RSA(e, &aep_rsa) ||
266 #ifndef OPENSSL_NO_DSA
267 !ENGINE_set_DSA(e, &aep_dsa) ||
269 #ifndef OPENSSL_NO_DH
270 !ENGINE_set_DH(e, &aep_dh) ||
273 !ENGINE_set_RAND(e, &aep_random) ||
275 !ENGINE_set_init_function(e, aep_init) ||
276 !ENGINE_set_destroy_function(e, aep_destroy) ||
277 !ENGINE_set_finish_function(e, aep_finish) ||
278 !ENGINE_set_ctrl_function(e, aep_ctrl) ||
279 !ENGINE_set_cmd_defns(e, aep_cmd_defns))
282 #ifndef OPENSSL_NO_RSA
283 /* We know that the "PKCS1_SSLeay()" functions hook properly
284 * to the aep-specific mod_exp and mod_exp_crt so we use
285 * those functions. NB: We don't use ENGINE_openssl() or
286 * anything "more generic" because something like the RSAref
287 * code may not hook properly, and if you own one of these
288 * cards then you have the right to do RSA operations on it
290 meth1 = RSA_PKCS1_SSLeay();
291 aep_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
292 aep_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
293 aep_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
294 aep_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
298 #ifndef OPENSSL_NO_DSA
299 /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
301 meth2 = DSA_OpenSSL();
302 aep_dsa.dsa_do_sign = meth2->dsa_do_sign;
303 aep_dsa.dsa_sign_setup = meth2->dsa_sign_setup;
304 aep_dsa.dsa_do_verify = meth2->dsa_do_verify;
306 aep_dsa = *DSA_get_default_method();
307 aep_dsa.dsa_mod_exp = aep_dsa_mod_exp;
308 aep_dsa.bn_mod_exp = aep_mod_exp_dsa;
311 #ifndef OPENSSL_NO_DH
312 /* Much the same for Diffie-Hellman */
313 meth3 = DH_OpenSSL();
314 aep_dh.generate_key = meth3->generate_key;
315 aep_dh.compute_key = meth3->compute_key;
316 aep_dh.bn_mod_exp = meth3->bn_mod_exp;
319 /* Ensure the aep error handling is set up */
320 ERR_load_AEPHK_strings();
325 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
326 static int bind_helper(ENGINE *e, const char *id)
328 if(id && (strcmp(id, engine_aep_id) != 0))
334 IMPLEMENT_DYNAMIC_CHECK_FN()
335 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
337 static ENGINE *engine_aep(void)
339 ENGINE *ret = ENGINE_new();
350 void ENGINE_load_aep(void)
352 /* Copied from eng_[openssl|dyn].c */
353 ENGINE *toadd = engine_aep();
361 /* This is a process-global DSO handle used for loading and unloading
362 * the Aep library. NB: This is only set (or unset) during an
363 * init() or finish() call (reference counts permitting) and they're
364 * operating with global locks, so this should be thread-safe
366 static DSO *aep_dso = NULL;
368 /* These are the static string constants for the DSO file name and the function
369 * symbol names to bind to.
371 static const char *AEP_LIBNAME = NULL;
372 static const char *get_AEP_LIBNAME(void)
378 static void free_AEP_LIBNAME(void)
381 OPENSSL_free((void*)AEP_LIBNAME);
384 static long set_AEP_LIBNAME(const char *name)
387 return ((AEP_LIBNAME = BUF_strdup(name)) != NULL ? 1 : 0);
390 static const char *AEP_F1 = "AEP_ModExp";
391 static const char *AEP_F2 = "AEP_ModExpCrt";
393 static const char *AEP_F3 = "AEP_GenRandom";
395 static const char *AEP_F4 = "AEP_Finalize";
396 static const char *AEP_F5 = "AEP_Initialize";
397 static const char *AEP_F6 = "AEP_OpenConnection";
398 static const char *AEP_F7 = "AEP_SetBNCallBacks";
399 static const char *AEP_F8 = "AEP_CloseConnection";
401 /* These are the function pointers that are (un)set when the library has
402 * successfully (un)loaded. */
403 static t_AEP_OpenConnection *p_AEP_OpenConnection = NULL;
404 static t_AEP_CloseConnection *p_AEP_CloseConnection = NULL;
405 static t_AEP_ModExp *p_AEP_ModExp = NULL;
406 static t_AEP_ModExpCrt *p_AEP_ModExpCrt = NULL;
408 static t_AEP_GenRandom *p_AEP_GenRandom = NULL;
410 static t_AEP_Initialize *p_AEP_Initialize = NULL;
411 static t_AEP_Finalize *p_AEP_Finalize = NULL;
412 static t_AEP_SetBNCallBacks *p_AEP_SetBNCallBacks = NULL;
414 /* (de)initialisation functions. */
415 static int aep_init(ENGINE *e)
423 t_AEP_Initialize *p5;
424 t_AEP_OpenConnection *p6;
425 t_AEP_SetBNCallBacks *p7;
426 t_AEP_CloseConnection *p8;
432 AEPHKerr(AEPHK_F_AEP_INIT,AEPHK_R_ALREADY_LOADED);
435 /* Attempt to load libaep.so. */
437 aep_dso = DSO_load(NULL, get_AEP_LIBNAME(), NULL, 0);
441 AEPHKerr(AEPHK_F_AEP_INIT,AEPHK_R_NOT_LOADED);
445 if( !(p1 = (t_AEP_ModExp *) DSO_bind_func( aep_dso,AEP_F1)) ||
446 !(p2 = (t_AEP_ModExpCrt*) DSO_bind_func( aep_dso,AEP_F2)) ||
448 !(p3 = (t_AEP_GenRandom*) DSO_bind_func( aep_dso,AEP_F3)) ||
450 !(p4 = (t_AEP_Finalize*) DSO_bind_func( aep_dso,AEP_F4)) ||
451 !(p5 = (t_AEP_Initialize*) DSO_bind_func( aep_dso,AEP_F5)) ||
452 !(p6 = (t_AEP_OpenConnection*) DSO_bind_func( aep_dso,AEP_F6)) ||
453 !(p7 = (t_AEP_SetBNCallBacks*) DSO_bind_func( aep_dso,AEP_F7)) ||
454 !(p8 = (t_AEP_CloseConnection*) DSO_bind_func( aep_dso,AEP_F8)))
456 AEPHKerr(AEPHK_F_AEP_INIT,AEPHK_R_NOT_LOADED);
460 /* Copy the pointers */
463 p_AEP_ModExpCrt = p2;
465 p_AEP_GenRandom = p3;
468 p_AEP_Initialize = p5;
469 p_AEP_OpenConnection = p6;
470 p_AEP_SetBNCallBacks = p7;
471 p_AEP_CloseConnection = p8;
482 p_AEP_OpenConnection = NULL;
484 p_AEP_ModExpCrt = NULL;
486 p_AEP_GenRandom = NULL;
488 p_AEP_Initialize = NULL;
489 p_AEP_Finalize = NULL;
490 p_AEP_SetBNCallBacks = NULL;
491 p_AEP_CloseConnection = NULL;
496 /* Destructor (complements the "ENGINE_aep()" constructor) */
497 static int aep_destroy(ENGINE *e)
500 ERR_unload_AEPHK_strings();
504 static int aep_finish(ENGINE *e)
506 int to_return = 0, in_use;
511 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_NOT_LOADED);
515 rv = aep_close_all_connections(0, &in_use);
518 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_CLOSE_HANDLES_FAILED);
523 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_CONNECTIONS_IN_USE);
527 rv = p_AEP_Finalize();
530 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_FINALIZE_FAILED);
534 if(!DSO_free(aep_dso))
536 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_UNIT_FAILURE);
541 p_AEP_CloseConnection = NULL;
542 p_AEP_OpenConnection = NULL;
544 p_AEP_ModExpCrt = NULL;
546 p_AEP_GenRandom = NULL;
548 p_AEP_Initialize = NULL;
549 p_AEP_Finalize = NULL;
550 p_AEP_SetBNCallBacks = NULL;
557 static int aep_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
559 int initialised = ((aep_dso == NULL) ? 0 : 1);
562 case AEP_CMD_SO_PATH:
565 AEPHKerr(AEPHK_F_AEP_CTRL,
566 ERR_R_PASSED_NULL_PARAMETER);
571 AEPHKerr(AEPHK_F_AEP_CTRL,
572 AEPHK_R_ALREADY_LOADED);
575 return set_AEP_LIBNAME((const char*)p);
579 AEPHKerr(AEPHK_F_AEP_CTRL,AEPHK_R_CTRL_COMMAND_NOT_IMPLEMENTED);
583 static int aep_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
584 const BIGNUM *m, BN_CTX *ctx)
588 AEP_CONNECTION_HNDL hConnection;
591 r_len = BN_num_bits(m);
593 /* Perform in software if modulus is too large for hardware. */
595 if (r_len > max_key_len){
596 AEPHKerr(AEPHK_F_AEP_MOD_EXP, AEPHK_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
597 return BN_mod_exp(r, a, p, m, ctx);
600 /*Grab a connection from the pool*/
601 rv = aep_get_connection(&hConnection);
604 AEPHKerr(AEPHK_F_AEP_MOD_EXP,AEPHK_R_GET_HANDLE_FAILED);
605 return BN_mod_exp(r, a, p, m, ctx);
608 /*To the card with the mod exp*/
609 rv = p_AEP_ModExp(hConnection,(void*)a, (void*)p,(void*)m, (void*)r,NULL);
613 AEPHKerr(AEPHK_F_AEP_MOD_EXP,AEPHK_R_MOD_EXP_FAILED);
614 rv = aep_close_connection(hConnection);
615 return BN_mod_exp(r, a, p, m, ctx);
618 /*Return the connection to the pool*/
619 rv = aep_return_connection(hConnection);
622 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_RETURN_CONNECTION_FAILED);
631 static AEP_RV aep_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
632 const BIGNUM *q, const BIGNUM *dmp1,
633 const BIGNUM *dmq1,const BIGNUM *iqmp, BN_CTX *ctx)
635 AEP_RV rv = AEP_R_OK;
636 AEP_CONNECTION_HNDL hConnection;
638 /*Grab a connection from the pool*/
639 rv = aep_get_connection(&hConnection);
642 AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT,AEPHK_R_GET_HANDLE_FAILED);
646 /*To the card with the mod exp*/
647 rv = p_AEP_ModExpCrt(hConnection,(void*)a, (void*)p, (void*)q, (void*)dmp1,(void*)dmq1,
648 (void*)iqmp,(void*)r,NULL);
651 AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT,AEPHK_R_MOD_EXP_CRT_FAILED);
652 rv = aep_close_connection(hConnection);
656 /*Return the connection to the pool*/
657 rv = aep_return_connection(hConnection);
660 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_RETURN_CONNECTION_FAILED);
670 static int aep_rand(unsigned char *buf,int len )
672 AEP_RV rv = AEP_R_OK;
673 AEP_CONNECTION_HNDL hConnection;
675 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
677 /*Can the request be serviced with what's already in the buffer?*/
678 if (len <= rand_block_bytes)
680 memcpy(buf, &rand_block[RAND_BLK_SIZE - rand_block_bytes], len);
681 rand_block_bytes -= len;
682 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
685 /*If not the get another block of random bytes*/
687 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
689 rv = aep_get_connection(&hConnection);
692 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_HANDLE_FAILED);
696 if (len > RAND_BLK_SIZE)
698 rv = p_AEP_GenRandom(hConnection, len, 2, buf, NULL);
701 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_RANDOM_FAILED);
707 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
709 rv = p_AEP_GenRandom(hConnection, RAND_BLK_SIZE, 2, &rand_block[0], NULL);
712 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_RANDOM_FAILED);
717 rand_block_bytes = RAND_BLK_SIZE;
719 memcpy(buf, &rand_block[RAND_BLK_SIZE - rand_block_bytes], len);
720 rand_block_bytes -= len;
722 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
725 rv = aep_return_connection(hConnection);
728 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_RETURN_CONNECTION_FAILED);
736 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
741 static int aep_rand_status(void)
747 #ifndef OPENSSL_NO_RSA
748 static int aep_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
752 AEP_RV rv = AEP_R_OK;
754 if ((ctx = BN_CTX_new()) == NULL)
759 AEPHKerr(AEPHK_F_AEP_RSA_MOD_EXP,AEPHK_R_NOT_LOADED);
763 /*See if we have all the necessary bits for a crt*/
764 if (rsa->q && rsa->dmp1 && rsa->dmq1 && rsa->iqmp)
766 rv = aep_mod_exp_crt(r0,I,rsa->p,rsa->q, rsa->dmp1,rsa->dmq1,rsa->iqmp,ctx);
768 if (rv == FAIL_TO_SW){
769 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
770 to_return = (*meth->rsa_mod_exp)(r0, I, rsa);
773 else if (rv != AEP_R_OK)
778 if (!rsa->d || !rsa->n)
780 AEPHKerr(AEPHK_F_AEP_RSA_MOD_EXP,AEPHK_R_MISSING_KEY_COMPONENTS);
784 rv = aep_mod_exp(r0,I,rsa->d,rsa->n,ctx);
799 #ifndef OPENSSL_NO_DSA
800 static int aep_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
801 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
802 BN_CTX *ctx, BN_MONT_CTX *in_mont)
808 /* let rr = a1 ^ p1 mod m */
809 if (!aep_mod_exp(rr,a1,p1,m,ctx)) goto end;
810 /* let t = a2 ^ p2 mod m */
811 if (!aep_mod_exp(&t,a2,p2,m,ctx)) goto end;
812 /* let rr = rr * t mod m */
813 if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
820 static int aep_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
821 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
824 return aep_mod_exp(r, a, p, m, ctx);
828 /* This function is aliased to mod_exp (with the mont stuff dropped). */
829 static int aep_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
830 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
832 return aep_mod_exp(r, a, p, m, ctx);
835 #ifndef OPENSSL_NO_DH
836 /* This function is aliased to mod_exp (with the dh and mont dropped). */
837 static int aep_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
838 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
841 return aep_mod_exp(r, a, p, m, ctx);
845 static AEP_RV aep_get_connection(AEP_CONNECTION_HNDL_PTR phConnection)
848 AEP_RV rv = AEP_R_OK;
850 /*Get the current process id*/
853 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
857 /*Check if this is the first time this is being called from the current
859 if (recorded_pid != curr_pid)
861 /*Remember our pid so we can check if we're in a new process*/
862 recorded_pid = curr_pid;
864 /*Call Finalize to make sure we have not inherited some data
865 from a parent process*/
868 /*Initialise the AEP API*/
869 rv = p_AEP_Initialize(NULL);
873 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_INIT_FAILURE);
878 /*Set the AEP big num call back functions*/
879 rv = p_AEP_SetBNCallBacks(&GetBigNumSize, &MakeAEPBigNum,
884 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_SETBNCALLBACK_FAILURE);
890 /*Reset the rand byte count*/
891 rand_block_bytes = 0;
894 /*Init the structures*/
895 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
897 aep_app_conn_table[count].conn_state = NotConnected;
898 aep_app_conn_table[count].conn_hndl = 0;
901 /*Open a connection*/
902 rv = p_AEP_OpenConnection(phConnection);
906 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_UNIT_FAILURE);
911 aep_app_conn_table[0].conn_state = InUse;
912 aep_app_conn_table[0].conn_hndl = *phConnection;
915 /*Check the existing connections to see if we can find a free one*/
916 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
918 if (aep_app_conn_table[count].conn_state == Connected)
920 aep_app_conn_table[count].conn_state = InUse;
921 *phConnection = aep_app_conn_table[count].conn_hndl;
925 /*If no connections available, we're going to have to try
927 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
929 if (aep_app_conn_table[count].conn_state == NotConnected)
931 /*Open a connection*/
932 rv = p_AEP_OpenConnection(phConnection);
936 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_UNIT_FAILURE);
940 aep_app_conn_table[count].conn_state = InUse;
941 aep_app_conn_table[count].conn_hndl = *phConnection;
945 rv = AEP_R_GENERAL_ERROR;
947 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
952 static AEP_RV aep_return_connection(AEP_CONNECTION_HNDL hConnection)
956 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
958 /*Find the connection item that matches this connection handle*/
959 for(count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
961 if (aep_app_conn_table[count].conn_hndl == hConnection)
963 aep_app_conn_table[count].conn_state = Connected;
968 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
973 static AEP_RV aep_close_connection(AEP_CONNECTION_HNDL hConnection)
976 AEP_RV rv = AEP_R_OK;
978 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
980 /*Find the connection item that matches this connection handle*/
981 for(count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
983 if (aep_app_conn_table[count].conn_hndl == hConnection)
985 rv = p_AEP_CloseConnection(aep_app_conn_table[count].conn_hndl);
988 aep_app_conn_table[count].conn_state = NotConnected;
989 aep_app_conn_table[count].conn_hndl = 0;
995 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
999 static AEP_RV aep_close_all_connections(int use_engine_lock, int *in_use)
1002 AEP_RV rv = AEP_R_OK;
1005 if (use_engine_lock) CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
1006 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
1008 switch (aep_app_conn_table[count].conn_state)
1011 rv = p_AEP_CloseConnection(aep_app_conn_table[count].conn_hndl);
1014 aep_app_conn_table[count].conn_state = NotConnected;
1015 aep_app_conn_table[count].conn_hndl = 0;
1025 if (use_engine_lock) CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
1029 /*BigNum call back functions, used to convert OpenSSL bignums into AEP bignums.
1030 Note only 32bit Openssl build support*/
1032 static AEP_RV GetBigNumSize(AEP_VOID_PTR ArbBigNum, AEP_U32* BigNumSize)
1036 /*Cast the ArbBigNum pointer to our BIGNUM struct*/
1037 bn = (BIGNUM*) ArbBigNum;
1039 #ifdef SIXTY_FOUR_BIT_LONG
1040 *BigNumSize = bn->top << 3;
1042 /*Size of the bignum in bytes is equal to the bn->top (no of 32 bit
1043 words) multiplies by 4*/
1044 *BigNumSize = bn->top << 2;
1050 static AEP_RV MakeAEPBigNum(AEP_VOID_PTR ArbBigNum, AEP_U32 BigNumSize,
1051 unsigned char* AEP_BigNum)
1055 #ifndef SIXTY_FOUR_BIT_LONG
1060 /*Cast the ArbBigNum pointer to our BIGNUM struct*/
1061 bn = (BIGNUM*) ArbBigNum;
1063 #ifdef SIXTY_FOUR_BIT_LONG
1064 memcpy(AEP_BigNum, bn->d, BigNumSize);
1066 /*Must copy data into a (monotone) least significant byte first format
1067 performing endian conversion if necessary*/
1068 for(i=0;i<bn->top;i++)
1070 buf = (unsigned char*)&bn->d[i];
1072 *((AEP_U32*)AEP_BigNum) = (AEP_U32)
1073 ((unsigned) buf[1] << 8 | buf[0]) |
1074 ((unsigned) buf[3] << 8 | buf[2]) << 16;
1083 /*Turn an AEP Big Num back to a user big num*/
1084 static AEP_RV ConvertAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
1085 unsigned char* AEP_BigNum)
1088 #ifndef SIXTY_FOUR_BIT_LONG
1092 bn = (BIGNUM*)ArbBigNum;
1094 /*Expand the result bn so that it can hold our big num.
1096 bn_expand(bn, (int)(BigNumSize << 3));
1098 #ifdef SIXTY_FOUR_BIT_LONG
1099 bn->top = BigNumSize >> 3;
1101 if((BigNumSize & 7) != 0)
1104 memset(bn->d, 0, bn->top << 3);
1106 memcpy(bn->d, AEP_BigNum, BigNumSize);
1108 bn->top = BigNumSize >> 2;
1110 for(i=0;i<bn->top;i++)
1112 bn->d[i] = (AEP_U32)
1113 ((unsigned) AEP_BigNum[3] << 8 | AEP_BigNum[2]) << 16 |
1114 ((unsigned) AEP_BigNum[1] << 8 | AEP_BigNum[0]);
1122 #endif /* !OPENSSL_NO_HW_AEP */
1123 #endif /* !OPENSSL_NO_HW */