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 #ifndef OPENSSL_SYS_MSDOS
64 #include <sys/types.h>
71 #include <openssl/crypto.h>
72 #include <openssl/dso.h>
73 #include <openssl/engine.h>
76 #ifndef OPENSSL_NO_HW_AEP
80 #include "vendor_defns/aep.h"
83 #define AEP_LIB_NAME "aep engine"
84 #define FAIL_TO_SW 0x10101010
86 #include "hw_aep_err.c"
88 static int aep_init(ENGINE *e);
89 static int aep_finish(ENGINE *e);
90 static int aep_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
91 static int aep_destroy(ENGINE *e);
93 static AEP_RV aep_get_connection(AEP_CONNECTION_HNDL_PTR hConnection);
94 static AEP_RV aep_return_connection(AEP_CONNECTION_HNDL hConnection);
95 static AEP_RV aep_close_connection(AEP_CONNECTION_HNDL hConnection);
96 static AEP_RV aep_close_all_connections(int use_engine_lock, int *in_use);
99 static int aep_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
100 const BIGNUM *m, BN_CTX *ctx);
102 static AEP_RV aep_mod_exp_crt(BIGNUM *r,const BIGNUM *a, const BIGNUM *p,
103 const BIGNUM *q, const BIGNUM *dmp1,const BIGNUM *dmq1,
104 const BIGNUM *iqmp, BN_CTX *ctx);
107 #ifndef OPENSSL_NO_RSA
108 static int aep_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
111 /* This function is aliased to mod_exp (with the mont stuff dropped). */
112 static int aep_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
113 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
116 #ifndef OPENSSL_NO_DSA
117 static int aep_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
118 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
119 BN_CTX *ctx, BN_MONT_CTX *in_mont);
121 static int aep_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
122 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
127 /* This function is aliased to mod_exp (with the DH and mont dropped). */
128 #ifndef OPENSSL_NO_DH
129 static int aep_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
130 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
135 static int aep_rand(unsigned char *buf, int num);
136 static int aep_rand_status(void);
139 /* Bignum conversion stuff */
140 static AEP_RV GetBigNumSize(AEP_VOID_PTR ArbBigNum, AEP_U32* BigNumSize);
141 static AEP_RV MakeAEPBigNum(AEP_VOID_PTR ArbBigNum, AEP_U32 BigNumSize,
142 unsigned char* AEP_BigNum);
143 static AEP_RV ConvertAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
144 unsigned char* AEP_BigNum);
146 /* The definitions for control commands specific to this engine */
147 #define AEP_CMD_SO_PATH ENGINE_CMD_BASE
148 static const ENGINE_CMD_DEFN aep_cmd_defns[] =
152 "Specifies the path to the 'aep' shared library",
153 ENGINE_CMD_FLAG_STRING
158 #ifndef OPENSSL_NO_RSA
159 /* Our internal RSA_METHOD that we provide pointers to */
160 static RSA_METHOD aep_rsa =
163 NULL, /*rsa_pub_encrypt*/
164 NULL, /*rsa_pub_decrypt*/
165 NULL, /*rsa_priv_encrypt*/
166 NULL, /*rsa_priv_encrypt*/
167 aep_rsa_mod_exp, /*rsa_mod_exp*/
168 aep_mod_exp_mont, /*bn_mod_exp*/
178 #ifndef OPENSSL_NO_DSA
179 /* Our internal DSA_METHOD that we provide pointers to */
180 static DSA_METHOD aep_dsa =
183 NULL, /* dsa_do_sign */
184 NULL, /* dsa_sign_setup */
185 NULL, /* dsa_do_verify */
186 aep_dsa_mod_exp, /* dsa_mod_exp */
187 aep_mod_exp_dsa, /* bn_mod_exp */
195 #ifndef OPENSSL_NO_DH
196 /* Our internal DH_METHOD that we provide pointers to */
197 static DH_METHOD aep_dh =
211 /* our internal RAND_method that we provide pointers to */
212 static RAND_METHOD aep_random =
214 /*"AEP RAND method", */
224 /*Define an array of structures to hold connections*/
225 static AEP_CONNECTION_ENTRY aep_app_conn_table[MAX_PROCESS_CONNECTIONS];
227 /*Used to determine if this is a new process*/
228 static pid_t recorded_pid = 0;
231 static AEP_U8 rand_block[RAND_BLK_SIZE];
232 static AEP_U32 rand_block_bytes = 0;
235 /* Constants used when creating the ENGINE */
236 static const char *engine_aep_id = "aep";
237 static const char *engine_aep_name = "Aep hardware engine support";
239 static int max_key_len = 2176;
242 /* This internal function is used by ENGINE_aep() and possibly by the
243 * "dynamic" ENGINE support too */
244 static int bind_aep(ENGINE *e)
246 #ifndef OPENSSL_NO_RSA
247 const RSA_METHOD *meth1;
249 #ifndef OPENSSL_NO_DSA
250 const DSA_METHOD *meth2;
252 #ifndef OPENSSL_NO_DH
253 const DH_METHOD *meth3;
256 if(!ENGINE_set_id(e, engine_aep_id) ||
257 !ENGINE_set_name(e, engine_aep_name) ||
258 #ifndef OPENSSL_NO_RSA
259 !ENGINE_set_RSA(e, &aep_rsa) ||
261 #ifndef OPENSSL_NO_DSA
262 !ENGINE_set_DSA(e, &aep_dsa) ||
264 #ifndef OPENSSL_NO_DH
265 !ENGINE_set_DH(e, &aep_dh) ||
268 !ENGINE_set_RAND(e, &aep_random) ||
270 !ENGINE_set_init_function(e, aep_init) ||
271 !ENGINE_set_destroy_function(e, aep_destroy) ||
272 !ENGINE_set_finish_function(e, aep_finish) ||
273 !ENGINE_set_ctrl_function(e, aep_ctrl) ||
274 !ENGINE_set_cmd_defns(e, aep_cmd_defns))
277 #ifndef OPENSSL_NO_RSA
278 /* We know that the "PKCS1_SSLeay()" functions hook properly
279 * to the aep-specific mod_exp and mod_exp_crt so we use
280 * those functions. NB: We don't use ENGINE_openssl() or
281 * anything "more generic" because something like the RSAref
282 * code may not hook properly, and if you own one of these
283 * cards then you have the right to do RSA operations on it
285 meth1 = RSA_PKCS1_SSLeay();
286 aep_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
287 aep_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
288 aep_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
289 aep_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
293 #ifndef OPENSSL_NO_DSA
294 /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
296 meth2 = DSA_OpenSSL();
297 aep_dsa.dsa_do_sign = meth2->dsa_do_sign;
298 aep_dsa.dsa_sign_setup = meth2->dsa_sign_setup;
299 aep_dsa.dsa_do_verify = meth2->dsa_do_verify;
301 aep_dsa = *DSA_get_default_method();
302 aep_dsa.dsa_mod_exp = aep_dsa_mod_exp;
303 aep_dsa.bn_mod_exp = aep_mod_exp_dsa;
306 #ifndef OPENSSL_NO_DH
307 /* Much the same for Diffie-Hellman */
308 meth3 = DH_OpenSSL();
309 aep_dh.generate_key = meth3->generate_key;
310 aep_dh.compute_key = meth3->compute_key;
311 aep_dh.bn_mod_exp = meth3->bn_mod_exp;
314 /* Ensure the aep error handling is set up */
315 ERR_load_AEPHK_strings();
320 #ifdef ENGINE_DYNAMIC_SUPPORT
321 static int bind_helper(ENGINE *e, const char *id)
323 if(id && (strcmp(id, engine_aep_id) != 0))
329 IMPLEMENT_DYNAMIC_CHECK_FN()
330 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
332 static ENGINE *engine_aep(void)
334 ENGINE *ret = ENGINE_new();
345 void ENGINE_load_aep(void)
347 /* Copied from eng_[openssl|dyn].c */
348 ENGINE *toadd = engine_aep();
356 /* This is a process-global DSO handle used for loading and unloading
357 * the Aep library. NB: This is only set (or unset) during an
358 * init() or finish() call (reference counts permitting) and they're
359 * operating with global locks, so this should be thread-safe
361 static DSO *aep_dso = NULL;
363 /* These are the static string constants for the DSO file name and the function
364 * symbol names to bind to.
366 static const char *AEP_LIBNAME = "aep";
368 static const char *AEP_F1 = "AEP_ModExp";
369 static const char *AEP_F2 = "AEP_ModExpCrt";
371 static const char *AEP_F3 = "AEP_GenRandom";
373 static const char *AEP_F4 = "AEP_Finalize";
374 static const char *AEP_F5 = "AEP_Initialize";
375 static const char *AEP_F6 = "AEP_OpenConnection";
376 static const char *AEP_F7 = "AEP_SetBNCallBacks";
377 static const char *AEP_F8 = "AEP_CloseConnection";
379 /* These are the function pointers that are (un)set when the library has
380 * successfully (un)loaded. */
381 static t_AEP_OpenConnection *p_AEP_OpenConnection = NULL;
382 static t_AEP_CloseConnection *p_AEP_CloseConnection = NULL;
383 static t_AEP_ModExp *p_AEP_ModExp = NULL;
384 static t_AEP_ModExpCrt *p_AEP_ModExpCrt = NULL;
386 static t_AEP_GenRandom *p_AEP_GenRandom = NULL;
388 static t_AEP_Initialize *p_AEP_Initialize = NULL;
389 static t_AEP_Finalize *p_AEP_Finalize = NULL;
390 static t_AEP_SetBNCallBacks *p_AEP_SetBNCallBacks = NULL;
392 /* (de)initialisation functions. */
393 static int aep_init(ENGINE *e)
401 t_AEP_Initialize *p5;
402 t_AEP_OpenConnection *p6;
403 t_AEP_SetBNCallBacks *p7;
404 t_AEP_CloseConnection *p8;
410 AEPHKerr(AEPHK_F_AEP_INIT,AEPHK_R_ALREADY_LOADED);
413 /* Attempt to load libaep.so. */
415 aep_dso = DSO_load(NULL, AEP_LIBNAME, NULL, 0);
419 AEPHKerr(AEPHK_F_AEP_INIT,AEPHK_R_NOT_LOADED);
423 if( !(p1 = (t_AEP_ModExp *) DSO_bind_func( aep_dso,AEP_F1)) ||
424 !(p2 = (t_AEP_ModExpCrt*) DSO_bind_func( aep_dso,AEP_F2)) ||
426 !(p3 = (t_AEP_GenRandom*) DSO_bind_func( aep_dso,AEP_F3)) ||
428 !(p4 = (t_AEP_Finalize*) DSO_bind_func( aep_dso,AEP_F4)) ||
429 !(p5 = (t_AEP_Initialize*) DSO_bind_func( aep_dso,AEP_F5)) ||
430 !(p6 = (t_AEP_OpenConnection*) DSO_bind_func( aep_dso,AEP_F6)) ||
431 !(p7 = (t_AEP_SetBNCallBacks*) DSO_bind_func( aep_dso,AEP_F7)) ||
432 !(p8 = (t_AEP_CloseConnection*) DSO_bind_func( aep_dso,AEP_F8)))
434 AEPHKerr(AEPHK_F_AEP_INIT,AEPHK_R_NOT_LOADED);
438 /* Copy the pointers */
441 p_AEP_ModExpCrt = p2;
443 p_AEP_GenRandom = p3;
446 p_AEP_Initialize = p5;
447 p_AEP_OpenConnection = p6;
448 p_AEP_SetBNCallBacks = p7;
449 p_AEP_CloseConnection = p8;
460 p_AEP_OpenConnection = NULL;
462 p_AEP_ModExpCrt = NULL;
464 p_AEP_GenRandom = NULL;
466 p_AEP_Initialize = NULL;
467 p_AEP_Finalize = NULL;
468 p_AEP_SetBNCallBacks = NULL;
469 p_AEP_CloseConnection = NULL;
474 /* Destructor (complements the "ENGINE_aep()" constructor) */
475 static int aep_destroy(ENGINE *e)
477 ERR_unload_AEPHK_strings();
481 static int aep_finish(ENGINE *e)
483 int to_return = 0, in_use;
488 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_NOT_LOADED);
492 rv = aep_close_all_connections(0, &in_use);
495 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_CLOSE_HANDLES_FAILED);
500 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_CONNECTIONS_IN_USE);
504 rv = p_AEP_Finalize();
507 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_FINALIZE_FAILED);
511 if(!DSO_free(aep_dso))
513 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_UNIT_FAILURE);
518 p_AEP_CloseConnection = NULL;
519 p_AEP_OpenConnection = NULL;
521 p_AEP_ModExpCrt = NULL;
523 p_AEP_GenRandom = NULL;
525 p_AEP_Initialize = NULL;
526 p_AEP_Finalize = NULL;
527 p_AEP_SetBNCallBacks = NULL;
534 static int aep_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
536 int initialised = ((aep_dso == NULL) ? 0 : 1);
539 case AEP_CMD_SO_PATH:
542 AEPHKerr(AEPHK_F_AEP_CTRL,
543 ERR_R_PASSED_NULL_PARAMETER);
548 AEPHKerr(AEPHK_F_AEP_CTRL,
549 AEPHK_R_ALREADY_LOADED);
552 AEP_LIBNAME = (const char *)p;
557 AEPHKerr(AEPHK_F_AEP_CTRL,AEPHK_R_CTRL_COMMAND_NOT_IMPLEMENTED);
561 static int aep_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
562 const BIGNUM *m, BN_CTX *ctx)
566 AEP_CONNECTION_HNDL hConnection;
569 r_len = BN_num_bits(m);
571 /* Perform in software if modulus is too large for hardware. */
573 if (r_len > max_key_len){
574 AEPHKerr(AEPHK_F_AEP_MOD_EXP, AEPHK_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
575 return BN_mod_exp(r, a, p, m, ctx);
578 /*Grab a connection from the pool*/
579 rv = aep_get_connection(&hConnection);
582 AEPHKerr(AEPHK_F_AEP_MOD_EXP,AEPHK_R_GET_HANDLE_FAILED);
583 return BN_mod_exp(r, a, p, m, ctx);
586 /*To the card with the mod exp*/
587 rv = p_AEP_ModExp(hConnection,(void*)a, (void*)p,(void*)m, (void*)r,NULL);
591 AEPHKerr(AEPHK_F_AEP_MOD_EXP,AEPHK_R_MOD_EXP_FAILED);
592 rv = aep_close_connection(hConnection);
593 return BN_mod_exp(r, a, p, m, ctx);
596 /*Return the connection to the pool*/
597 rv = aep_return_connection(hConnection);
600 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_RETURN_CONNECTION_FAILED);
609 static AEP_RV aep_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
610 const BIGNUM *q, const BIGNUM *dmp1,
611 const BIGNUM *dmq1,const BIGNUM *iqmp, BN_CTX *ctx)
613 AEP_RV rv = AEP_R_OK;
614 AEP_CONNECTION_HNDL hConnection;
616 /*Grab a connection from the pool*/
617 rv = aep_get_connection(&hConnection);
620 AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT,AEPHK_R_GET_HANDLE_FAILED);
624 /*To the card with the mod exp*/
625 rv = p_AEP_ModExpCrt(hConnection,(void*)a, (void*)p, (void*)q, (void*)dmp1,(void*)dmq1,
626 (void*)iqmp,(void*)r,NULL);
629 AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT,AEPHK_R_MOD_EXP_CRT_FAILED);
630 rv = aep_close_connection(hConnection);
634 /*Return the connection to the pool*/
635 rv = aep_return_connection(hConnection);
638 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_RETURN_CONNECTION_FAILED);
648 static int aep_rand(unsigned char *buf,int len )
650 AEP_RV rv = AEP_R_OK;
651 AEP_CONNECTION_HNDL hConnection;
653 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
655 /*Can the request be serviced with what's already in the buffer?*/
656 if (len <= rand_block_bytes)
658 memcpy(buf, &rand_block[RAND_BLK_SIZE - rand_block_bytes], len);
659 rand_block_bytes -= len;
660 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
663 /*If not the get another block of random bytes*/
665 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
667 rv = aep_get_connection(&hConnection);
670 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_HANDLE_FAILED);
674 if (len > RAND_BLK_SIZE)
676 rv = p_AEP_GenRandom(hConnection, len, 2, buf, NULL);
679 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_RANDOM_FAILED);
685 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
687 rv = p_AEP_GenRandom(hConnection, RAND_BLK_SIZE, 2, &rand_block[0], NULL);
690 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_RANDOM_FAILED);
695 rand_block_bytes = RAND_BLK_SIZE;
697 memcpy(buf, &rand_block[RAND_BLK_SIZE - rand_block_bytes], len);
698 rand_block_bytes -= len;
700 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
703 rv = aep_return_connection(hConnection);
706 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_RETURN_CONNECTION_FAILED);
714 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
719 static int aep_rand_status(void)
725 #ifndef OPENSSL_NO_RSA
726 static int aep_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
730 AEP_RV rv = AEP_R_OK;
732 if ((ctx = BN_CTX_new()) == NULL)
737 AEPHKerr(AEPHK_F_AEP_RSA_MOD_EXP,AEPHK_R_NOT_LOADED);
741 /*See if we have all the necessary bits for a crt*/
742 if (rsa->q && rsa->dmp1 && rsa->dmq1 && rsa->iqmp)
744 rv = aep_mod_exp_crt(r0,I,rsa->p,rsa->q, rsa->dmp1,rsa->dmq1,rsa->iqmp,ctx);
746 if (rv == FAIL_TO_SW){
747 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
748 to_return = (*meth->rsa_mod_exp)(r0, I, rsa);
751 else if (rv != AEP_R_OK)
756 if (!rsa->d || !rsa->n)
758 AEPHKerr(AEPHK_F_AEP_RSA_MOD_EXP,AEPHK_R_MISSING_KEY_COMPONENTS);
762 rv = aep_mod_exp(r0,I,rsa->d,rsa->n,ctx);
777 #ifndef OPENSSL_NO_DSA
778 static int aep_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
779 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
780 BN_CTX *ctx, BN_MONT_CTX *in_mont)
786 /* let rr = a1 ^ p1 mod m */
787 if (!aep_mod_exp(rr,a1,p1,m,ctx)) goto end;
788 /* let t = a2 ^ p2 mod m */
789 if (!aep_mod_exp(&t,a2,p2,m,ctx)) goto end;
790 /* let rr = rr * t mod m */
791 if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
798 static int aep_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
799 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
802 return aep_mod_exp(r, a, p, m, ctx);
806 /* This function is aliased to mod_exp (with the mont stuff dropped). */
807 static int aep_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
808 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
810 return aep_mod_exp(r, a, p, m, ctx);
813 #ifndef OPENSSL_NO_DH
814 /* This function is aliased to mod_exp (with the dh and mont dropped). */
815 static int aep_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
816 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
819 return aep_mod_exp(r, a, p, m, ctx);
823 static AEP_RV aep_get_connection(AEP_CONNECTION_HNDL_PTR phConnection)
826 AEP_RV rv = AEP_R_OK;
828 /*Get the current process id*/
831 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
835 /*Check if this is the first time this is being called from the current
837 if (recorded_pid != curr_pid)
839 /*Remember our pid so we can check if we're in a new process*/
840 recorded_pid = curr_pid;
842 /*Call Finalize to make sure we have not inherited some data
843 from a parent process*/
846 /*Initialise the AEP API*/
847 rv = p_AEP_Initialize(NULL);
851 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_INIT_FAILURE);
856 /*Set the AEP big num call back functions*/
857 rv = p_AEP_SetBNCallBacks(&GetBigNumSize, &MakeAEPBigNum,
862 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_SETBNCALLBACK_FAILURE);
868 /*Reset the rand byte count*/
869 rand_block_bytes = 0;
872 /*Init the structures*/
873 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
875 aep_app_conn_table[count].conn_state = NotConnected;
876 aep_app_conn_table[count].conn_hndl = 0;
879 /*Open a connection*/
880 rv = p_AEP_OpenConnection(phConnection);
884 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_UNIT_FAILURE);
889 aep_app_conn_table[0].conn_state = InUse;
890 aep_app_conn_table[0].conn_hndl = *phConnection;
893 /*Check the existing connections to see if we can find a free one*/
894 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
896 if (aep_app_conn_table[count].conn_state == Connected)
898 aep_app_conn_table[count].conn_state = InUse;
899 *phConnection = aep_app_conn_table[count].conn_hndl;
903 /*If no connections available, we're going to have to try
905 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
907 if (aep_app_conn_table[count].conn_state == NotConnected)
909 /*Open a connection*/
910 rv = p_AEP_OpenConnection(phConnection);
914 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_UNIT_FAILURE);
918 aep_app_conn_table[count].conn_state = InUse;
919 aep_app_conn_table[count].conn_hndl = *phConnection;
923 rv = AEP_R_GENERAL_ERROR;
925 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
930 static AEP_RV aep_return_connection(AEP_CONNECTION_HNDL hConnection)
934 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
936 /*Find the connection item that matches this connection handle*/
937 for(count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
939 if (aep_app_conn_table[count].conn_hndl == hConnection)
941 aep_app_conn_table[count].conn_state = Connected;
946 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
951 static AEP_RV aep_close_connection(AEP_CONNECTION_HNDL hConnection)
954 AEP_RV rv = AEP_R_OK;
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 rv = p_AEP_CloseConnection(aep_app_conn_table[count].conn_hndl);
966 aep_app_conn_table[count].conn_state = NotConnected;
967 aep_app_conn_table[count].conn_hndl = 0;
973 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
977 static AEP_RV aep_close_all_connections(int use_engine_lock, int *in_use)
980 AEP_RV rv = AEP_R_OK;
983 if (use_engine_lock) CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
984 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
986 switch (aep_app_conn_table[count].conn_state)
989 rv = p_AEP_CloseConnection(aep_app_conn_table[count].conn_hndl);
992 aep_app_conn_table[count].conn_state = NotConnected;
993 aep_app_conn_table[count].conn_hndl = 0;
1003 if (use_engine_lock) CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
1007 /*BigNum call back functions, used to convert OpenSSL bignums into AEP bignums.
1008 Note only 32bit Openssl build support*/
1010 static AEP_RV GetBigNumSize(AEP_VOID_PTR ArbBigNum, AEP_U32* BigNumSize)
1014 /*Cast the ArbBigNum pointer to our BIGNUM struct*/
1015 bn = (BIGNUM*) ArbBigNum;
1017 #ifdef SIXTY_FOUR_BIT_LONG
1018 *BigNumSize = bn->top << 3;
1020 /*Size of the bignum in bytes is equal to the bn->top (no of 32 bit
1021 words) multiplies by 4*/
1022 *BigNumSize = bn->top << 2;
1028 static AEP_RV MakeAEPBigNum(AEP_VOID_PTR ArbBigNum, AEP_U32 BigNumSize,
1029 unsigned char* AEP_BigNum)
1033 #ifndef SIXTY_FOUR_BIT_LONG
1038 /*Cast the ArbBigNum pointer to our BIGNUM struct*/
1039 bn = (BIGNUM*) ArbBigNum;
1041 #ifdef SIXTY_FOUR_BIT_LONG
1042 memcpy(AEP_BigNum, bn->d, BigNumSize);
1044 /*Must copy data into a (monotone) least significant byte first format
1045 performing endian conversion if necessary*/
1046 for(i=0;i<bn->top;i++)
1048 buf = (unsigned char*)&bn->d[i];
1050 *((AEP_U32*)AEP_BigNum) = (AEP_U32)
1051 ((unsigned) buf[1] << 8 | buf[0]) |
1052 ((unsigned) buf[3] << 8 | buf[2]) << 16;
1061 /*Turn an AEP Big Num back to a user big num*/
1062 static AEP_RV ConvertAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
1063 unsigned char* AEP_BigNum)
1066 #ifndef SIXTY_FOUR_BIT_LONG
1070 bn = (BIGNUM*)ArbBigNum;
1072 /*Expand the result bn so that it can hold our big num.
1074 bn_expand(bn, (int)(BigNumSize << 3));
1076 #ifdef SIXTY_FOUR_BIT_LONG
1077 bn->top = BigNumSize >> 3;
1079 if((BigNumSize & 7) != 0)
1082 memset(bn->d, 0, bn->top << 3);
1084 memcpy(bn->d, AEP_BigNum, BigNumSize);
1086 bn->top = BigNumSize >> 2;
1088 for(i=0;i<bn->top;i++)
1090 bn->d[i] = (AEP_U32)
1091 ((unsigned) AEP_BigNum[3] << 8 | AEP_BigNum[2]) << 16 |
1092 ((unsigned) AEP_BigNum[1] << 8 | AEP_BigNum[0]);
1100 #endif /* !OPENSSL_NO_HW_AEP */
1101 #endif /* !OPENSSL_NO_HW */