1 /* ====================================================================
2 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * licensing@OpenSSL.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
49 * This product includes cryptographic software written by Eric Young
50 * (eay@cryptsoft.com). This product includes software written by Tim
51 * Hudson (tjh@cryptsoft.com).
56 #include <openssl/bn.h>
59 #include <openssl/e_os2.h>
60 #if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
61 #include <sys/types.h>
68 #include <openssl/crypto.h>
69 #include <openssl/dso.h>
70 #include <openssl/engine.h>
71 #include <openssl/buffer.h>
72 #include <openssl/rsa.h>
73 #include <openssl/dsa.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 "e_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)(void));
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, BN_CTX *ctx);
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*/
179 #ifndef OPENSSL_NO_DSA
180 /* Our internal DSA_METHOD that we provide pointers to */
181 static DSA_METHOD aep_dsa =
184 NULL, /* dsa_do_sign */
185 NULL, /* dsa_sign_setup */
186 NULL, /* dsa_do_verify */
187 aep_dsa_mod_exp, /* dsa_mod_exp */
188 aep_mod_exp_dsa, /* bn_mod_exp */
193 NULL, /* dsa_paramgen */
194 NULL /* dsa_keygen */
198 #ifndef OPENSSL_NO_DH
199 /* Our internal DH_METHOD that we provide pointers to */
200 static DH_METHOD aep_dh =
215 /* our internal RAND_method that we provide pointers to */
216 static RAND_METHOD aep_random =
218 /*"AEP RAND method", */
228 /*Define an array of structures to hold connections*/
229 static AEP_CONNECTION_ENTRY aep_app_conn_table[MAX_PROCESS_CONNECTIONS];
231 /*Used to determine if this is a new process*/
232 static pid_t recorded_pid = 0;
235 static AEP_U8 rand_block[RAND_BLK_SIZE];
236 static AEP_U32 rand_block_bytes = 0;
239 /* Constants used when creating the ENGINE */
240 static const char *engine_aep_id = "aep";
241 static const char *engine_aep_name = "Aep hardware engine support";
243 static int max_key_len = 2176;
246 /* This internal function is used by ENGINE_aep() and possibly by the
247 * "dynamic" ENGINE support too */
248 static int bind_aep(ENGINE *e)
250 #ifndef OPENSSL_NO_RSA
251 const RSA_METHOD *meth1;
253 #ifndef OPENSSL_NO_DSA
254 const DSA_METHOD *meth2;
256 #ifndef OPENSSL_NO_DH
257 const DH_METHOD *meth3;
260 if(!ENGINE_set_id(e, engine_aep_id) ||
261 !ENGINE_set_name(e, engine_aep_name) ||
262 #ifndef OPENSSL_NO_RSA
263 !ENGINE_set_RSA(e, &aep_rsa) ||
265 #ifndef OPENSSL_NO_DSA
266 !ENGINE_set_DSA(e, &aep_dsa) ||
268 #ifndef OPENSSL_NO_DH
269 !ENGINE_set_DH(e, &aep_dh) ||
272 !ENGINE_set_RAND(e, &aep_random) ||
274 !ENGINE_set_init_function(e, aep_init) ||
275 !ENGINE_set_destroy_function(e, aep_destroy) ||
276 !ENGINE_set_finish_function(e, aep_finish) ||
277 !ENGINE_set_ctrl_function(e, aep_ctrl) ||
278 !ENGINE_set_cmd_defns(e, aep_cmd_defns))
281 #ifndef OPENSSL_NO_RSA
282 /* We know that the "PKCS1_SSLeay()" functions hook properly
283 * to the aep-specific mod_exp and mod_exp_crt so we use
284 * those functions. NB: We don't use ENGINE_openssl() or
285 * anything "more generic" because something like the RSAref
286 * code may not hook properly, and if you own one of these
287 * cards then you have the right to do RSA operations on it
289 meth1 = RSA_PKCS1_SSLeay();
290 aep_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
291 aep_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
292 aep_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
293 aep_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
297 #ifndef OPENSSL_NO_DSA
298 /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
300 meth2 = DSA_OpenSSL();
301 aep_dsa.dsa_do_sign = meth2->dsa_do_sign;
302 aep_dsa.dsa_sign_setup = meth2->dsa_sign_setup;
303 aep_dsa.dsa_do_verify = meth2->dsa_do_verify;
305 aep_dsa = *DSA_get_default_method();
306 aep_dsa.dsa_mod_exp = aep_dsa_mod_exp;
307 aep_dsa.bn_mod_exp = aep_mod_exp_dsa;
310 #ifndef OPENSSL_NO_DH
311 /* Much the same for Diffie-Hellman */
312 meth3 = DH_OpenSSL();
313 aep_dh.generate_key = meth3->generate_key;
314 aep_dh.compute_key = meth3->compute_key;
315 aep_dh.bn_mod_exp = meth3->bn_mod_exp;
318 /* Ensure the aep error handling is set up */
319 ERR_load_AEPHK_strings();
324 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
325 static int bind_helper(ENGINE *e, const char *id)
327 if(id && (strcmp(id, engine_aep_id) != 0))
333 IMPLEMENT_DYNAMIC_CHECK_FN()
334 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
336 static ENGINE *engine_aep(void)
338 ENGINE *ret = ENGINE_new();
349 void ENGINE_load_aep(void)
351 /* Copied from eng_[openssl|dyn].c */
352 ENGINE *toadd = engine_aep();
360 /* This is a process-global DSO handle used for loading and unloading
361 * the Aep library. NB: This is only set (or unset) during an
362 * init() or finish() call (reference counts permitting) and they're
363 * operating with global locks, so this should be thread-safe
365 static DSO *aep_dso = NULL;
367 /* These are the static string constants for the DSO file name and the function
368 * symbol names to bind to.
370 static const char *AEP_LIBNAME = NULL;
371 static const char *get_AEP_LIBNAME(void)
377 static void free_AEP_LIBNAME(void)
380 OPENSSL_free((void*)AEP_LIBNAME);
383 static long set_AEP_LIBNAME(const char *name)
386 return ((AEP_LIBNAME = BUF_strdup(name)) != NULL ? 1 : 0);
389 static const char *AEP_F1 = "AEP_ModExp";
390 static const char *AEP_F2 = "AEP_ModExpCrt";
392 static const char *AEP_F3 = "AEP_GenRandom";
394 static const char *AEP_F4 = "AEP_Finalize";
395 static const char *AEP_F5 = "AEP_Initialize";
396 static const char *AEP_F6 = "AEP_OpenConnection";
397 static const char *AEP_F7 = "AEP_SetBNCallBacks";
398 static const char *AEP_F8 = "AEP_CloseConnection";
400 /* These are the function pointers that are (un)set when the library has
401 * successfully (un)loaded. */
402 static t_AEP_OpenConnection *p_AEP_OpenConnection = NULL;
403 static t_AEP_CloseConnection *p_AEP_CloseConnection = NULL;
404 static t_AEP_ModExp *p_AEP_ModExp = NULL;
405 static t_AEP_ModExpCrt *p_AEP_ModExpCrt = NULL;
407 static t_AEP_GenRandom *p_AEP_GenRandom = NULL;
409 static t_AEP_Initialize *p_AEP_Initialize = NULL;
410 static t_AEP_Finalize *p_AEP_Finalize = NULL;
411 static t_AEP_SetBNCallBacks *p_AEP_SetBNCallBacks = NULL;
413 /* (de)initialisation functions. */
414 static int aep_init(ENGINE *e)
422 t_AEP_Initialize *p5;
423 t_AEP_OpenConnection *p6;
424 t_AEP_SetBNCallBacks *p7;
425 t_AEP_CloseConnection *p8;
431 AEPHKerr(AEPHK_F_AEP_INIT,AEPHK_R_ALREADY_LOADED);
434 /* Attempt to load libaep.so. */
436 aep_dso = DSO_load(NULL, get_AEP_LIBNAME(), NULL, 0);
440 AEPHKerr(AEPHK_F_AEP_INIT,AEPHK_R_NOT_LOADED);
444 if( !(p1 = (t_AEP_ModExp *) DSO_bind_func( aep_dso,AEP_F1)) ||
445 !(p2 = (t_AEP_ModExpCrt*) DSO_bind_func( aep_dso,AEP_F2)) ||
447 !(p3 = (t_AEP_GenRandom*) DSO_bind_func( aep_dso,AEP_F3)) ||
449 !(p4 = (t_AEP_Finalize*) DSO_bind_func( aep_dso,AEP_F4)) ||
450 !(p5 = (t_AEP_Initialize*) DSO_bind_func( aep_dso,AEP_F5)) ||
451 !(p6 = (t_AEP_OpenConnection*) DSO_bind_func( aep_dso,AEP_F6)) ||
452 !(p7 = (t_AEP_SetBNCallBacks*) DSO_bind_func( aep_dso,AEP_F7)) ||
453 !(p8 = (t_AEP_CloseConnection*) DSO_bind_func( aep_dso,AEP_F8)))
455 AEPHKerr(AEPHK_F_AEP_INIT,AEPHK_R_NOT_LOADED);
459 /* Copy the pointers */
462 p_AEP_ModExpCrt = p2;
464 p_AEP_GenRandom = p3;
467 p_AEP_Initialize = p5;
468 p_AEP_OpenConnection = p6;
469 p_AEP_SetBNCallBacks = p7;
470 p_AEP_CloseConnection = p8;
481 p_AEP_OpenConnection = NULL;
483 p_AEP_ModExpCrt = NULL;
485 p_AEP_GenRandom = NULL;
487 p_AEP_Initialize = NULL;
488 p_AEP_Finalize = NULL;
489 p_AEP_SetBNCallBacks = NULL;
490 p_AEP_CloseConnection = NULL;
495 /* Destructor (complements the "ENGINE_aep()" constructor) */
496 static int aep_destroy(ENGINE *e)
499 ERR_unload_AEPHK_strings();
503 static int aep_finish(ENGINE *e)
505 int to_return = 0, in_use;
510 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_NOT_LOADED);
514 rv = aep_close_all_connections(0, &in_use);
517 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_CLOSE_HANDLES_FAILED);
522 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_CONNECTIONS_IN_USE);
526 rv = p_AEP_Finalize();
529 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_FINALIZE_FAILED);
533 if(!DSO_free(aep_dso))
535 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_UNIT_FAILURE);
540 p_AEP_CloseConnection = NULL;
541 p_AEP_OpenConnection = NULL;
543 p_AEP_ModExpCrt = NULL;
545 p_AEP_GenRandom = NULL;
547 p_AEP_Initialize = NULL;
548 p_AEP_Finalize = NULL;
549 p_AEP_SetBNCallBacks = NULL;
556 static int aep_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
558 int initialised = ((aep_dso == NULL) ? 0 : 1);
561 case AEP_CMD_SO_PATH:
564 AEPHKerr(AEPHK_F_AEP_CTRL,
565 ERR_R_PASSED_NULL_PARAMETER);
570 AEPHKerr(AEPHK_F_AEP_CTRL,
571 AEPHK_R_ALREADY_LOADED);
574 return set_AEP_LIBNAME((const char*)p);
578 AEPHKerr(AEPHK_F_AEP_CTRL,AEPHK_R_CTRL_COMMAND_NOT_IMPLEMENTED);
582 static int aep_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
583 const BIGNUM *m, BN_CTX *ctx)
587 AEP_CONNECTION_HNDL hConnection;
590 r_len = BN_num_bits(m);
592 /* Perform in software if modulus is too large for hardware. */
594 if (r_len > max_key_len){
595 AEPHKerr(AEPHK_F_AEP_MOD_EXP, AEPHK_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
596 return BN_mod_exp(r, a, p, m, ctx);
599 /*Grab a connection from the pool*/
600 rv = aep_get_connection(&hConnection);
603 AEPHKerr(AEPHK_F_AEP_MOD_EXP,AEPHK_R_GET_HANDLE_FAILED);
604 return BN_mod_exp(r, a, p, m, ctx);
607 /*To the card with the mod exp*/
608 rv = p_AEP_ModExp(hConnection,(void*)a, (void*)p,(void*)m, (void*)r,NULL);
612 AEPHKerr(AEPHK_F_AEP_MOD_EXP,AEPHK_R_MOD_EXP_FAILED);
613 rv = aep_close_connection(hConnection);
614 return BN_mod_exp(r, a, p, m, ctx);
617 /*Return the connection to the pool*/
618 rv = aep_return_connection(hConnection);
621 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_RETURN_CONNECTION_FAILED);
630 static AEP_RV aep_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
631 const BIGNUM *q, const BIGNUM *dmp1,
632 const BIGNUM *dmq1,const BIGNUM *iqmp, BN_CTX *ctx)
634 AEP_RV rv = AEP_R_OK;
635 AEP_CONNECTION_HNDL hConnection;
637 /*Grab a connection from the pool*/
638 rv = aep_get_connection(&hConnection);
641 AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT,AEPHK_R_GET_HANDLE_FAILED);
645 /*To the card with the mod exp*/
646 rv = p_AEP_ModExpCrt(hConnection,(void*)a, (void*)p, (void*)q, (void*)dmp1,(void*)dmq1,
647 (void*)iqmp,(void*)r,NULL);
650 AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT,AEPHK_R_MOD_EXP_CRT_FAILED);
651 rv = aep_close_connection(hConnection);
655 /*Return the connection to the pool*/
656 rv = aep_return_connection(hConnection);
659 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_RETURN_CONNECTION_FAILED);
669 static int aep_rand(unsigned char *buf,int len )
671 AEP_RV rv = AEP_R_OK;
672 AEP_CONNECTION_HNDL hConnection;
674 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
676 /*Can the request be serviced with what's already in the buffer?*/
677 if (len <= rand_block_bytes)
679 memcpy(buf, &rand_block[RAND_BLK_SIZE - rand_block_bytes], len);
680 rand_block_bytes -= len;
681 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
684 /*If not the get another block of random bytes*/
686 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
688 rv = aep_get_connection(&hConnection);
691 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_HANDLE_FAILED);
695 if (len > RAND_BLK_SIZE)
697 rv = p_AEP_GenRandom(hConnection, len, 2, buf, NULL);
700 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_RANDOM_FAILED);
706 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
708 rv = p_AEP_GenRandom(hConnection, RAND_BLK_SIZE, 2, &rand_block[0], NULL);
711 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_RANDOM_FAILED);
716 rand_block_bytes = RAND_BLK_SIZE;
718 memcpy(buf, &rand_block[RAND_BLK_SIZE - rand_block_bytes], len);
719 rand_block_bytes -= len;
721 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
724 rv = aep_return_connection(hConnection);
727 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_RETURN_CONNECTION_FAILED);
735 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
740 static int aep_rand_status(void)
746 #ifndef OPENSSL_NO_RSA
747 static int aep_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
750 AEP_RV rv = AEP_R_OK;
754 AEPHKerr(AEPHK_F_AEP_RSA_MOD_EXP,AEPHK_R_NOT_LOADED);
758 /*See if we have all the necessary bits for a crt*/
759 if (rsa->q && rsa->dmp1 && rsa->dmq1 && rsa->iqmp)
761 rv = aep_mod_exp_crt(r0,I,rsa->p,rsa->q, rsa->dmp1,rsa->dmq1,rsa->iqmp,ctx);
763 if (rv == FAIL_TO_SW){
764 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
765 to_return = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
768 else if (rv != AEP_R_OK)
773 if (!rsa->d || !rsa->n)
775 AEPHKerr(AEPHK_F_AEP_RSA_MOD_EXP,AEPHK_R_MISSING_KEY_COMPONENTS);
779 rv = aep_mod_exp(r0,I,rsa->d,rsa->n,ctx);
792 #ifndef OPENSSL_NO_DSA
793 static int aep_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
794 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
795 BN_CTX *ctx, BN_MONT_CTX *in_mont)
801 /* let rr = a1 ^ p1 mod m */
802 if (!aep_mod_exp(rr,a1,p1,m,ctx)) goto end;
803 /* let t = a2 ^ p2 mod m */
804 if (!aep_mod_exp(&t,a2,p2,m,ctx)) goto end;
805 /* let rr = rr * t mod m */
806 if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
813 static int aep_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
814 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
817 return aep_mod_exp(r, a, p, m, ctx);
821 /* This function is aliased to mod_exp (with the mont stuff dropped). */
822 static int aep_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
823 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
825 return aep_mod_exp(r, a, p, m, ctx);
828 #ifndef OPENSSL_NO_DH
829 /* This function is aliased to mod_exp (with the dh and mont dropped). */
830 static int aep_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
831 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
834 return aep_mod_exp(r, a, p, m, ctx);
838 static AEP_RV aep_get_connection(AEP_CONNECTION_HNDL_PTR phConnection)
841 AEP_RV rv = AEP_R_OK;
843 /*Get the current process id*/
846 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
851 curr_pid = GetThreadID();
854 /*Check if this is the first time this is being called from the current
856 if (recorded_pid != curr_pid)
858 /*Remember our pid so we can check if we're in a new process*/
859 recorded_pid = curr_pid;
861 /*Call Finalize to make sure we have not inherited some data
862 from a parent process*/
865 /*Initialise the AEP API*/
866 rv = p_AEP_Initialize(NULL);
870 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_INIT_FAILURE);
875 /*Set the AEP big num call back functions*/
876 rv = p_AEP_SetBNCallBacks(&GetBigNumSize, &MakeAEPBigNum,
881 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_SETBNCALLBACK_FAILURE);
887 /*Reset the rand byte count*/
888 rand_block_bytes = 0;
891 /*Init the structures*/
892 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
894 aep_app_conn_table[count].conn_state = NotConnected;
895 aep_app_conn_table[count].conn_hndl = 0;
898 /*Open a connection*/
899 rv = p_AEP_OpenConnection(phConnection);
903 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_UNIT_FAILURE);
908 aep_app_conn_table[0].conn_state = InUse;
909 aep_app_conn_table[0].conn_hndl = *phConnection;
912 /*Check the existing connections to see if we can find a free one*/
913 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
915 if (aep_app_conn_table[count].conn_state == Connected)
917 aep_app_conn_table[count].conn_state = InUse;
918 *phConnection = aep_app_conn_table[count].conn_hndl;
922 /*If no connections available, we're going to have to try
924 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
926 if (aep_app_conn_table[count].conn_state == NotConnected)
928 /*Open a connection*/
929 rv = p_AEP_OpenConnection(phConnection);
933 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_UNIT_FAILURE);
937 aep_app_conn_table[count].conn_state = InUse;
938 aep_app_conn_table[count].conn_hndl = *phConnection;
942 rv = AEP_R_GENERAL_ERROR;
944 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
949 static AEP_RV aep_return_connection(AEP_CONNECTION_HNDL hConnection)
953 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
955 /*Find the connection item that matches this connection handle*/
956 for(count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
958 if (aep_app_conn_table[count].conn_hndl == hConnection)
960 aep_app_conn_table[count].conn_state = Connected;
965 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
970 static AEP_RV aep_close_connection(AEP_CONNECTION_HNDL hConnection)
973 AEP_RV rv = AEP_R_OK;
975 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
977 /*Find the connection item that matches this connection handle*/
978 for(count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
980 if (aep_app_conn_table[count].conn_hndl == hConnection)
982 rv = p_AEP_CloseConnection(aep_app_conn_table[count].conn_hndl);
985 aep_app_conn_table[count].conn_state = NotConnected;
986 aep_app_conn_table[count].conn_hndl = 0;
992 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
996 static AEP_RV aep_close_all_connections(int use_engine_lock, int *in_use)
999 AEP_RV rv = AEP_R_OK;
1002 if (use_engine_lock) CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
1003 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
1005 switch (aep_app_conn_table[count].conn_state)
1008 rv = p_AEP_CloseConnection(aep_app_conn_table[count].conn_hndl);
1011 aep_app_conn_table[count].conn_state = NotConnected;
1012 aep_app_conn_table[count].conn_hndl = 0;
1022 if (use_engine_lock) CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
1026 /*BigNum call back functions, used to convert OpenSSL bignums into AEP bignums.
1027 Note only 32bit Openssl build support*/
1029 static AEP_RV GetBigNumSize(AEP_VOID_PTR ArbBigNum, AEP_U32* BigNumSize)
1033 /*Cast the ArbBigNum pointer to our BIGNUM struct*/
1034 bn = (BIGNUM*) ArbBigNum;
1036 #ifdef SIXTY_FOUR_BIT_LONG
1037 *BigNumSize = bn->top << 3;
1039 /*Size of the bignum in bytes is equal to the bn->top (no of 32 bit
1040 words) multiplies by 4*/
1041 *BigNumSize = bn->top << 2;
1047 static AEP_RV MakeAEPBigNum(AEP_VOID_PTR ArbBigNum, AEP_U32 BigNumSize,
1048 unsigned char* AEP_BigNum)
1052 #ifndef SIXTY_FOUR_BIT_LONG
1057 /*Cast the ArbBigNum pointer to our BIGNUM struct*/
1058 bn = (BIGNUM*) ArbBigNum;
1060 #ifdef SIXTY_FOUR_BIT_LONG
1061 memcpy(AEP_BigNum, bn->d, BigNumSize);
1063 /*Must copy data into a (monotone) least significant byte first format
1064 performing endian conversion if necessary*/
1065 for(i=0;i<bn->top;i++)
1067 buf = (unsigned char*)&bn->d[i];
1069 *((AEP_U32*)AEP_BigNum) = (AEP_U32)
1070 ((unsigned) buf[1] << 8 | buf[0]) |
1071 ((unsigned) buf[3] << 8 | buf[2]) << 16;
1080 /*Turn an AEP Big Num back to a user big num*/
1081 static AEP_RV ConvertAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
1082 unsigned char* AEP_BigNum)
1085 #ifndef SIXTY_FOUR_BIT_LONG
1089 bn = (BIGNUM*)ArbBigNum;
1091 /*Expand the result bn so that it can hold our big num.
1093 bn_expand(bn, (int)(BigNumSize << 3));
1095 #ifdef SIXTY_FOUR_BIT_LONG
1096 bn->top = BigNumSize >> 3;
1098 if((BigNumSize & 7) != 0)
1101 memset(bn->d, 0, bn->top << 3);
1103 memcpy(bn->d, AEP_BigNum, BigNumSize);
1105 bn->top = BigNumSize >> 2;
1107 for(i=0;i<bn->top;i++)
1109 bn->d[i] = (AEP_U32)
1110 ((unsigned) AEP_BigNum[3] << 8 | AEP_BigNum[2]) << 16 |
1111 ((unsigned) AEP_BigNum[1] << 8 | AEP_BigNum[0]);
1119 #endif /* !OPENSSL_NO_HW_AEP */
1120 #endif /* !OPENSSL_NO_HW */