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 #include "hw_aep_err.c"
86 static int aep_init(ENGINE *e);
87 static int aep_finish(ENGINE *e);
88 static int aep_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
89 static int aep_destroy(ENGINE *e);
91 static AEP_RV aep_get_connection(AEP_CONNECTION_HNDL_PTR hConnection);
92 static AEP_RV aep_return_connection(AEP_CONNECTION_HNDL hConnection);
93 static AEP_RV aep_close_all_connections(int use_engine_lock, int *in_use);
96 static int aep_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
97 const BIGNUM *m, BN_CTX *ctx);
99 static AEP_RV aep_mod_exp_crt(BIGNUM *r,const BIGNUM *a, const BIGNUM *p,
100 const BIGNUM *q, const BIGNUM *dmp1,const BIGNUM *dmq1,
101 const BIGNUM *iqmp, BN_CTX *ctx);
104 #ifndef OPENSSL_NO_RSA
105 static int aep_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
108 /* This function is aliased to mod_exp (with the mont stuff dropped). */
109 static int aep_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
110 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
113 #ifndef OPENSSL_NO_DSA
114 static int aep_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
115 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
116 BN_CTX *ctx, BN_MONT_CTX *in_mont);
118 static int aep_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
119 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
124 /* This function is aliased to mod_exp (with the DH and mont dropped). */
125 #ifndef OPENSSL_NO_DH
126 static int aep_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
127 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
132 static int aep_rand(unsigned char *buf, int num);
133 static int aep_rand_status(void);
136 /* Bignum conversion stuff */
137 static AEP_RV GetBigNumSize(void* ArbBigNum, AEP_U32* BigNumSize);
138 static AEP_RV MakeAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
139 unsigned char* AEP_BigNum);
140 static AEP_RV ConvertAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
141 unsigned char* AEP_BigNum);
143 /* The definitions for control commands specific to this engine */
144 #define AEP_CMD_SO_PATH ENGINE_CMD_BASE
145 static const ENGINE_CMD_DEFN aep_cmd_defns[] =
149 "Specifies the path to the 'aep' shared library",
150 ENGINE_CMD_FLAG_STRING
155 #ifndef OPENSSL_NO_RSA
156 /* Our internal RSA_METHOD that we provide pointers to */
157 static RSA_METHOD aep_rsa =
160 NULL, /*rsa_pub_encrypt*/
161 NULL, /*rsa_pub_decrypt*/
162 NULL, /*rsa_priv_encrypt*/
163 NULL, /*rsa_priv_encrypt*/
164 aep_rsa_mod_exp, /*rsa_mod_exp*/
165 aep_mod_exp_mont, /*bn_mod_exp*/
175 #ifndef OPENSSL_NO_DSA
176 /* Our internal DSA_METHOD that we provide pointers to */
177 static DSA_METHOD aep_dsa =
180 NULL, /* dsa_do_sign */
181 NULL, /* dsa_sign_setup */
182 NULL, /* dsa_do_verify */
183 aep_dsa_mod_exp, /* dsa_mod_exp */
184 aep_mod_exp_dsa, /* bn_mod_exp */
192 #ifndef OPENSSL_NO_DH
193 /* Our internal DH_METHOD that we provide pointers to */
194 static DH_METHOD aep_dh =
208 /* our internal RAND_method that we provide pointers to */
209 static RAND_METHOD aep_random =
211 /*"AEP RAND method", */
221 /*Define an array of structures to hold connections*/
222 static AEP_CONNECTION_ENTRY aep_app_conn_table[MAX_PROCESS_CONNECTIONS];
224 /*Used to determine if this is a new process*/
225 static pid_t recorded_pid = 0;
228 static AEP_U8 rand_block[RAND_BLK_SIZE];
229 static AEP_U32 rand_block_bytes = 0;
232 /* Constants used when creating the ENGINE */
233 static const char *engine_aep_id = "aep";
234 static const char *engine_aep_name = "Aep hardware engine support";
237 /* This internal function is used by ENGINE_aep() and possibly by the
238 * "dynamic" ENGINE support too */
239 static int bind_aep(ENGINE *e)
241 #ifndef OPENSSL_NO_RSA
242 const RSA_METHOD *meth1;
244 #ifndef OPENSSL_NO_DSA
245 const DSA_METHOD *meth2;
247 #ifndef OPENSSL_NO_DH
248 const DH_METHOD *meth3;
251 if(!ENGINE_set_id(e, engine_aep_id) ||
252 !ENGINE_set_name(e, engine_aep_name) ||
253 #ifndef OPENSSL_NO_RSA
254 !ENGINE_set_RSA(e, &aep_rsa) ||
256 #ifndef OPENSSL_NO_DSA
257 !ENGINE_set_DSA(e, &aep_dsa) ||
259 #ifndef OPENSSL_NO_DH
260 !ENGINE_set_DH(e, &aep_dh) ||
263 !ENGINE_set_RAND(e, &aep_random) ||
265 !ENGINE_set_init_function(e, aep_init) ||
266 !ENGINE_set_destroy_function(e, aep_destroy) ||
267 !ENGINE_set_finish_function(e, aep_finish) ||
268 !ENGINE_set_ctrl_function(e, aep_ctrl) ||
269 !ENGINE_set_cmd_defns(e, aep_cmd_defns))
272 #ifndef OPENSSL_NO_RSA
273 /* We know that the "PKCS1_SSLeay()" functions hook properly
274 * to the aep-specific mod_exp and mod_exp_crt so we use
275 * those functions. NB: We don't use ENGINE_openssl() or
276 * anything "more generic" because something like the RSAref
277 * code may not hook properly, and if you own one of these
278 * cards then you have the right to do RSA operations on it
280 meth1 = RSA_PKCS1_SSLeay();
281 aep_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
282 aep_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
283 aep_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
284 aep_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
288 #ifndef OPENSSL_NO_DSA
289 /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
291 meth2 = DSA_OpenSSL();
292 aep_dsa.dsa_do_sign = meth2->dsa_do_sign;
293 aep_dsa.dsa_sign_setup = meth2->dsa_sign_setup;
294 aep_dsa.dsa_do_verify = meth2->dsa_do_verify;
296 aep_dsa = *DSA_get_default_method();
297 aep_dsa.dsa_mod_exp = aep_dsa_mod_exp;
298 aep_dsa.bn_mod_exp = aep_mod_exp_dsa;
301 #ifndef OPENSSL_NO_DH
302 /* Much the same for Diffie-Hellman */
303 meth3 = DH_OpenSSL();
304 aep_dh.generate_key = meth3->generate_key;
305 aep_dh.compute_key = meth3->compute_key;
306 aep_dh.bn_mod_exp = meth3->bn_mod_exp;
309 /* Ensure the aep error handling is set up */
310 ERR_load_AEPHK_strings();
315 #ifdef ENGINE_DYNAMIC_SUPPORT
316 static int bind_helper(ENGINE *e, const char *id)
318 if(id && (strcmp(id, engine_aep_id) != 0))
324 IMPLEMENT_DYNAMIC_CHECK_FN()
325 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
327 static ENGINE *engine_aep(void)
329 ENGINE *ret = ENGINE_new();
340 void ENGINE_load_aep(void)
342 /* Copied from eng_[openssl|dyn].c */
343 ENGINE *toadd = engine_aep();
351 /* This is a process-global DSO handle used for loading and unloading
352 * the Aep library. NB: This is only set (or unset) during an
353 * init() or finish() call (reference counts permitting) and they're
354 * operating with global locks, so this should be thread-safe
356 static DSO *aep_dso = NULL;
358 /* These are the static string constants for the DSO file name and the function
359 * symbol names to bind to.
361 static const char *AEP_LIBNAME = "aep";
363 static const char *AEP_F1 = "AEP_ModExp";
364 static const char *AEP_F2 = "AEP_ModExpCrt";
366 static const char *AEP_F3 = "AEP_GenRandom";
368 static const char *AEP_F4 = "AEP_Finalize";
369 static const char *AEP_F5 = "AEP_Initialize";
370 static const char *AEP_F6 = "AEP_OpenConnection";
371 static const char *AEP_F7 = "AEP_SetBNCallBacks";
372 static const char *AEP_F8 = "AEP_CloseConnection";
374 /* These are the function pointers that are (un)set when the library has
375 * successfully (un)loaded. */
376 static t_AEP_OpenConnection *p_AEP_OpenConnection = NULL;
377 static t_AEP_CloseConnection *p_AEP_CloseConnection = NULL;
378 static t_AEP_ModExp *p_AEP_ModExp = NULL;
379 static t_AEP_ModExpCrt *p_AEP_ModExpCrt = NULL;
381 static t_AEP_GenRandom *p_AEP_GenRandom = NULL;
383 static t_AEP_Initialize *p_AEP_Initialize = NULL;
384 static t_AEP_Finalize *p_AEP_Finalize = NULL;
385 static t_AEP_SetBNCallBacks *p_AEP_SetBNCallBacks = NULL;
387 /* (de)initialisation functions. */
388 static int aep_init(ENGINE *e)
396 t_AEP_Initialize *p5;
397 t_AEP_OpenConnection *p6;
398 t_AEP_SetBNCallBacks *p7;
399 t_AEP_CloseConnection *p8;
405 AEPHKerr(AEPHK_F_AEP_INIT,AEPHK_R_ALREADY_LOADED);
408 /* Attempt to load libaep.so. */
410 aep_dso = DSO_load(NULL, AEP_LIBNAME, NULL, 0);
414 AEPHKerr(AEPHK_F_AEP_INIT,AEPHK_R_NOT_LOADED);
418 if( !(p1 = (t_AEP_ModExp *) DSO_bind_func( aep_dso,AEP_F1)) ||
419 !(p2 = (t_AEP_ModExpCrt*) DSO_bind_func( aep_dso,AEP_F2)) ||
421 !(p3 = (t_AEP_GenRandom*) DSO_bind_func( aep_dso,AEP_F3)) ||
423 !(p4 = (t_AEP_Finalize*) DSO_bind_func( aep_dso,AEP_F4)) ||
424 !(p5 = (t_AEP_Initialize*) DSO_bind_func( aep_dso,AEP_F5)) ||
425 !(p6 = (t_AEP_OpenConnection*) DSO_bind_func( aep_dso,AEP_F6)) ||
426 !(p7 = (t_AEP_SetBNCallBacks*) DSO_bind_func( aep_dso,AEP_F7)) ||
427 !(p8 = (t_AEP_CloseConnection*) DSO_bind_func( aep_dso,AEP_F8)))
429 AEPHKerr(AEPHK_F_AEP_INIT,AEPHK_R_NOT_LOADED);
433 /* Copy the pointers */
436 p_AEP_ModExpCrt = p2;
438 p_AEP_GenRandom = p3;
441 p_AEP_Initialize = p5;
442 p_AEP_OpenConnection = p6;
443 p_AEP_SetBNCallBacks = p7;
444 p_AEP_CloseConnection = p8;
455 p_AEP_OpenConnection = NULL;
457 p_AEP_ModExpCrt = NULL;
459 p_AEP_GenRandom = NULL;
461 p_AEP_Initialize = NULL;
462 p_AEP_Finalize = NULL;
463 p_AEP_SetBNCallBacks = NULL;
464 p_AEP_CloseConnection = NULL;
469 /* Destructor (complements the "ENGINE_aep()" constructor) */
470 static int aep_destroy(ENGINE *e)
472 ERR_unload_AEPHK_strings();
476 static int aep_finish(ENGINE *e)
478 int to_return = 0, in_use;
483 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_NOT_LOADED);
487 rv = aep_close_all_connections(0, &in_use);
490 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_CLOSE_HANDLES_FAILED);
495 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_CONNECTIONS_IN_USE);
499 rv = p_AEP_Finalize();
502 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_FINALIZE_FAILED);
506 if(!DSO_free(aep_dso))
508 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_UNIT_FAILURE);
513 p_AEP_CloseConnection = NULL;
514 p_AEP_OpenConnection = NULL;
516 p_AEP_ModExpCrt = NULL;
518 p_AEP_GenRandom = NULL;
520 p_AEP_Initialize = NULL;
521 p_AEP_Finalize = NULL;
522 p_AEP_SetBNCallBacks = NULL;
529 static int aep_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
531 int initialised = ((aep_dso == NULL) ? 0 : 1);
534 case AEP_CMD_SO_PATH:
537 AEPHKerr(AEPHK_F_AEP_CTRL,
538 ERR_R_PASSED_NULL_PARAMETER);
543 AEPHKerr(AEPHK_F_AEP_CTRL,
544 AEPHK_R_ALREADY_LOADED);
547 AEP_LIBNAME = (const char *)p;
552 AEPHKerr(AEPHK_F_AEP_CTRL,AEPHK_R_CTRL_COMMAND_NOT_IMPLEMENTED);
556 static int aep_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
557 const BIGNUM *m, BN_CTX *ctx)
560 AEP_CONNECTION_HNDL hConnection;
563 /*Grab a connection from the pool*/
564 rv = aep_get_connection(&hConnection);
567 AEPHKerr(AEPHK_F_AEP_MOD_EXP,AEPHK_R_GET_HANDLE_FAILED);
571 /*To the card with the mod exp*/
572 rv = p_AEP_ModExp(hConnection,(void*)a, (void*)p,(void*)m, (void*)r,NULL);
576 AEPHKerr(AEPHK_F_AEP_MOD_EXP,AEPHK_R_MOD_EXP_FAILED);
577 rv = aep_return_connection(hConnection);
581 /*Return the connection to the pool*/
582 rv = aep_return_connection(hConnection);
585 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_RETURN_CONNECTION_FAILED);
594 static AEP_RV aep_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
595 const BIGNUM *q, const BIGNUM *dmp1,
596 const BIGNUM *dmq1,const BIGNUM *iqmp, BN_CTX *ctx)
598 AEP_RV rv = AEP_R_OK;
599 AEP_CONNECTION_HNDL hConnection;
601 /*Grab a connection from the pool*/
602 rv = aep_get_connection(&hConnection);
605 AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT,AEPHK_R_GET_HANDLE_FAILED);
609 /*To the card with the mod exp*/
610 rv = p_AEP_ModExpCrt(hConnection,(void*)a, (void*)p, (void*)q, (void*)dmp1,(void*)dmq1,
611 (void*)iqmp,(void*)r,NULL);
614 AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT,AEPHK_R_MOD_EXP_CRT_FAILED);
615 rv = aep_return_connection(hConnection);
619 /*Return the connection to the pool*/
620 rv = aep_return_connection(hConnection);
623 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_RETURN_CONNECTION_FAILED);
633 static int aep_rand(unsigned char *buf,int len )
635 AEP_RV rv = AEP_R_OK;
636 AEP_CONNECTION_HNDL hConnection;
641 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
643 /*Can the request be serviced with what's already in the buffer?*/
644 if (len <= rand_block_bytes)
646 memcpy(buf, &rand_block[RAND_BLK_SIZE - rand_block_bytes], len);
647 rand_block_bytes -= len;
648 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
651 /*If not the get another block of random bytes*/
653 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
655 rv = aep_get_connection(&hConnection);
658 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_HANDLE_FAILED);
662 if (len > RAND_BLK_SIZE)
664 rv = p_AEP_GenRandom(hConnection, len, 2, buf, NULL);
667 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_RANDOM_FAILED);
673 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
675 rv = p_AEP_GenRandom(hConnection, RAND_BLK_SIZE, 2, &rand_block[0], NULL);
678 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_RANDOM_FAILED);
683 rand_block_bytes = RAND_BLK_SIZE;
685 memcpy(buf, &rand_block[RAND_BLK_SIZE - rand_block_bytes], len);
686 rand_block_bytes -= len;
688 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
691 rv = aep_return_connection(hConnection);
694 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_RETURN_CONNECTION_FAILED);
702 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
707 static int aep_rand_status(void)
713 #ifndef OPENSSL_NO_RSA
714 static int aep_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
718 AEP_RV rv = AEP_R_OK;
720 if ((ctx = BN_CTX_new()) == NULL)
725 AEPHKerr(AEPHK_F_AEP_RSA_MOD_EXP,AEPHK_R_NOT_LOADED);
729 /*See if we have all the necessary bits for a crt*/
730 if (rsa->q && rsa->dmp1 && rsa->dmq1 && rsa->iqmp)
732 rv = aep_mod_exp_crt(r0,I,rsa->p,rsa->q, rsa->dmp1,rsa->dmq1,rsa->iqmp,ctx);
738 if (!rsa->d || !rsa->n)
740 AEPHKerr(AEPHK_F_AEP_RSA_MOD_EXP,AEPHK_R_MISSING_KEY_COMPONENTS);
744 rv = aep_mod_exp(r0,I,rsa->d,rsa->n,ctx);
759 #ifndef OPENSSL_NO_DSA
760 static int aep_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
761 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
762 BN_CTX *ctx, BN_MONT_CTX *in_mont)
768 /* let rr = a1 ^ p1 mod m */
769 if (!aep_mod_exp(rr,a1,p1,m,ctx)) goto end;
770 /* let t = a2 ^ p2 mod m */
771 if (!aep_mod_exp(&t,a2,p2,m,ctx)) goto end;
772 /* let rr = rr * t mod m */
773 if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
780 static int aep_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
781 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
784 return aep_mod_exp(r, a, p, m, ctx);
788 /* This function is aliased to mod_exp (with the mont stuff dropped). */
789 static int aep_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
790 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
792 return aep_mod_exp(r, a, p, m, ctx);
795 #ifndef OPENSSL_NO_DH
796 /* This function is aliased to mod_exp (with the dh and mont dropped). */
797 static int aep_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
798 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
801 return aep_mod_exp(r, a, p, m, ctx);
805 static AEP_RV aep_get_connection(AEP_CONNECTION_HNDL_PTR phConnection)
808 AEP_RV rv = AEP_R_OK;
810 /*Get the current process id*/
813 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
817 /*Check if this is the first time this is being called from the current
819 if (recorded_pid != curr_pid)
821 /*Remember our pid so we can check if we're in a new process*/
822 recorded_pid = curr_pid;
824 /*Call Finalize to make sure we have not inherited some data
825 from a parent process*/
828 /*Initialise the AEP API*/
829 rv = p_AEP_Initialize(NULL);
833 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_INIT_FAILURE);
838 /*Set the AEP big num call back functions*/
839 rv = p_AEP_SetBNCallBacks(&GetBigNumSize, &MakeAEPBigNum,
844 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_SETBNCALLBACK_FAILURE);
850 /*Reset the rand byte count*/
851 rand_block_bytes = 0;
854 /*Init the structures*/
855 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
857 aep_app_conn_table[count].conn_state = NotConnected;
858 aep_app_conn_table[count].conn_hndl = 0;
861 /*Open a connection*/
862 rv = p_AEP_OpenConnection(phConnection);
866 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_UNIT_FAILURE);
871 aep_app_conn_table[0].conn_state = InUse;
872 aep_app_conn_table[0].conn_hndl = *phConnection;
875 /*Check the existing connections to see if we can find a free one*/
876 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
878 if (aep_app_conn_table[count].conn_state == Connected)
880 aep_app_conn_table[count].conn_state = InUse;
881 *phConnection = aep_app_conn_table[count].conn_hndl;
885 /*If no connections available, we're going to have to try
887 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
889 if (aep_app_conn_table[count].conn_state == NotConnected)
891 /*Open a connection*/
892 rv = p_AEP_OpenConnection(phConnection);
896 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_UNIT_FAILURE);
900 aep_app_conn_table[count].conn_state = InUse;
901 aep_app_conn_table[count].conn_hndl = *phConnection;
905 rv = AEP_R_GENERAL_ERROR;
907 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
912 static AEP_RV aep_return_connection(AEP_CONNECTION_HNDL hConnection)
916 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
918 /*Find the connection item that matches this connection handle*/
919 for(count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
921 if (aep_app_conn_table[count].conn_hndl == hConnection)
923 aep_app_conn_table[count].conn_state = Connected;
928 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
933 static AEP_RV aep_close_all_connections(int use_engine_lock, int *in_use)
939 if (use_engine_lock) CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
940 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
942 switch (aep_app_conn_table[count].conn_state)
945 rv = p_AEP_CloseConnection(aep_app_conn_table[count].conn_hndl);
948 aep_app_conn_table[count].conn_state = NotConnected;
949 aep_app_conn_table[count].conn_hndl = 0;
958 if (use_engine_lock) CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
962 /*BigNum call back functions, used to convert OpenSSL bignums into AEP bignums.
963 Note only 32bit Openssl build support*/
965 static AEP_RV GetBigNumSize(void* ArbBigNum, AEP_U32* BigNumSize)
969 /*Cast the ArbBigNum pointer to our BIGNUM struct*/
970 bn = (BIGNUM*) ArbBigNum;
972 #ifdef SIXTY_FOUR_BIT_LONG
973 *BigNumSize = bn->top << 3;
975 /*Size of the bignum in bytes is equal to the bn->top (no of 32 bit
976 words) multiplies by 4*/
977 *BigNumSize = bn->top << 2;
983 static AEP_RV MakeAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
984 unsigned char* AEP_BigNum)
988 #ifndef SIXTY_FOUR_BIT_LONG
993 /*Cast the ArbBigNum pointer to our BIGNUM struct*/
994 bn = (BIGNUM*) ArbBigNum;
996 #ifdef SIXTY_FOUR_BIT_LONG
997 memcpy(AEP_BigNum, bn->d, BigNumSize);
999 /*Must copy data into a (monotone) least significant byte first format
1000 performing endian conversion if necessary*/
1001 for(i=0;i<bn->top;i++)
1003 buf = (unsigned char*)&bn->d[i];
1005 *((AEP_U32*)AEP_BigNum) =
1006 (AEP_U32) ((unsigned) buf[1] << 8 | buf[0]) |
1007 ((unsigned) buf[3] << 8 | buf[2]) << 16;
1016 /*Turn an AEP Big Num back to a user big num*/
1017 static AEP_RV ConvertAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
1018 unsigned char* AEP_BigNum)
1021 #ifndef SIXTY_FOUR_BIT_LONG
1025 bn = (BIGNUM*)ArbBigNum;
1027 /*Expand the result bn so that it can hold our big num.
1029 bn_expand(bn, (int)(BigNumSize << 3));
1031 #ifdef SIXTY_FOUR_BIT_LONG
1032 bn->top = BigNumSize >> 3;
1034 if((BigNumSize & 7) != 0)
1037 memset(bn->d, 0, bn->top << 3);
1039 memcpy(bn->d, AEP_BigNum, BigNumSize);
1041 bn->top = BigNumSize >> 2;
1043 for(i=0;i<bn->top;i++)
1045 bn->d[i] = (AEP_U32)
1046 ((unsigned) AEP_BigNum[3] << 8 | AEP_BigNum[2]) << 16 |
1047 ((unsigned) AEP_BigNum[1] << 8 | AEP_BigNum[0]);
1055 #endif /* !OPENSSL_NO_HW_AEP */
1056 #endif /* !OPENSSL_NO_HW */