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__) || defined(__MINGW32__)
61 #include <sys/types.h>
68 #if defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_CLIB)
69 #define getpid GetThreadID
70 extern int GetThreadID(void);
71 #elif defined(_WIN32) && !defined(__WATCOMC__)
72 #define getpid _getpid
75 #include <openssl/crypto.h>
76 #include <openssl/dso.h>
77 #include <openssl/engine.h>
78 #include <openssl/buffer.h>
79 #ifndef OPENSSL_NO_RSA
80 #include <openssl/rsa.h>
82 #ifndef OPENSSL_NO_DSA
83 #include <openssl/dsa.h>
86 #include <openssl/dh.h>
90 #ifndef OPENSSL_NO_HW_AEP
94 #include "vendor_defns/aep.h"
97 #define AEP_LIB_NAME "aep engine"
98 #define FAIL_TO_SW 0x10101010
100 #include "e_aep_err.c"
102 static int aep_init(ENGINE *e);
103 static int aep_finish(ENGINE *e);
104 static int aep_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
105 static int aep_destroy(ENGINE *e);
107 static AEP_RV aep_get_connection(AEP_CONNECTION_HNDL_PTR hConnection);
108 static AEP_RV aep_return_connection(AEP_CONNECTION_HNDL hConnection);
109 static AEP_RV aep_close_connection(AEP_CONNECTION_HNDL hConnection);
110 static AEP_RV aep_close_all_connections(int use_engine_lock, int *in_use);
113 #ifndef OPENSSL_NO_RSA
114 static int aep_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
115 const BIGNUM *m, BN_CTX *ctx);
117 static AEP_RV aep_mod_exp_crt(BIGNUM *r,const BIGNUM *a, const BIGNUM *p,
118 const BIGNUM *q, const BIGNUM *dmp1,const BIGNUM *dmq1,
119 const BIGNUM *iqmp, BN_CTX *ctx);
123 #ifndef OPENSSL_NO_RSA
124 static int aep_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
127 /* This function is aliased to mod_exp (with the mont stuff dropped). */
128 #ifndef OPENSSL_NO_RSA
129 static int aep_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
130 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
134 #ifndef OPENSSL_NO_DSA
135 static int aep_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
136 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
137 BN_CTX *ctx, BN_MONT_CTX *in_mont);
139 static int aep_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
140 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
145 /* This function is aliased to mod_exp (with the DH and mont dropped). */
146 #ifndef OPENSSL_NO_DH
147 static int aep_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
148 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
153 static int aep_rand(unsigned char *buf, int num);
154 static int aep_rand_status(void);
157 /* Bignum conversion stuff */
158 static AEP_RV GetBigNumSize(AEP_VOID_PTR ArbBigNum, AEP_U32* BigNumSize);
159 static AEP_RV MakeAEPBigNum(AEP_VOID_PTR ArbBigNum, AEP_U32 BigNumSize,
160 unsigned char* AEP_BigNum);
161 static AEP_RV ConvertAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
162 unsigned char* AEP_BigNum);
164 /* The definitions for control commands specific to this engine */
165 #define AEP_CMD_SO_PATH ENGINE_CMD_BASE
166 static const ENGINE_CMD_DEFN aep_cmd_defns[] =
170 "Specifies the path to the 'aep' shared library",
171 ENGINE_CMD_FLAG_STRING
176 #ifndef OPENSSL_NO_RSA
177 /* Our internal RSA_METHOD that we provide pointers to */
178 static RSA_METHOD aep_rsa =
181 NULL, /*rsa_pub_encrypt*/
182 NULL, /*rsa_pub_decrypt*/
183 NULL, /*rsa_priv_encrypt*/
184 NULL, /*rsa_priv_encrypt*/
185 aep_rsa_mod_exp, /*rsa_mod_exp*/
186 aep_mod_exp_mont, /*bn_mod_exp*/
197 #ifndef OPENSSL_NO_DSA
198 /* Our internal DSA_METHOD that we provide pointers to */
199 static DSA_METHOD aep_dsa =
202 NULL, /* dsa_do_sign */
203 NULL, /* dsa_sign_setup */
204 NULL, /* dsa_do_verify */
205 aep_dsa_mod_exp, /* dsa_mod_exp */
206 aep_mod_exp_dsa, /* bn_mod_exp */
211 NULL, /* dsa_paramgen */
212 NULL /* dsa_keygen */
216 #ifndef OPENSSL_NO_DH
217 /* Our internal DH_METHOD that we provide pointers to */
218 static DH_METHOD aep_dh =
233 /* our internal RAND_method that we provide pointers to */
234 static RAND_METHOD aep_random =
236 /*"AEP RAND method", */
246 /*Define an array of structures to hold connections*/
247 static AEP_CONNECTION_ENTRY aep_app_conn_table[MAX_PROCESS_CONNECTIONS];
249 /*Used to determine if this is a new process*/
250 static pid_t recorded_pid = 0;
253 static AEP_U8 rand_block[RAND_BLK_SIZE];
254 static AEP_U32 rand_block_bytes = 0;
257 /* Constants used when creating the ENGINE */
258 static const char *engine_aep_id = "aep";
259 static const char *engine_aep_name = "Aep hardware engine support";
261 static int max_key_len = 2176;
264 /* This internal function is used by ENGINE_aep() and possibly by the
265 * "dynamic" ENGINE support too */
266 static int bind_aep(ENGINE *e)
268 #ifndef OPENSSL_NO_RSA
269 const RSA_METHOD *meth1;
271 #ifndef OPENSSL_NO_DSA
272 const DSA_METHOD *meth2;
274 #ifndef OPENSSL_NO_DH
275 const DH_METHOD *meth3;
278 if(!ENGINE_set_id(e, engine_aep_id) ||
279 !ENGINE_set_name(e, engine_aep_name) ||
280 #ifndef OPENSSL_NO_RSA
281 !ENGINE_set_RSA(e, &aep_rsa) ||
283 #ifndef OPENSSL_NO_DSA
284 !ENGINE_set_DSA(e, &aep_dsa) ||
286 #ifndef OPENSSL_NO_DH
287 !ENGINE_set_DH(e, &aep_dh) ||
290 !ENGINE_set_RAND(e, &aep_random) ||
292 !ENGINE_set_init_function(e, aep_init) ||
293 !ENGINE_set_destroy_function(e, aep_destroy) ||
294 !ENGINE_set_finish_function(e, aep_finish) ||
295 !ENGINE_set_ctrl_function(e, aep_ctrl) ||
296 !ENGINE_set_cmd_defns(e, aep_cmd_defns))
299 #ifndef OPENSSL_NO_RSA
300 /* We know that the "PKCS1_SSLeay()" functions hook properly
301 * to the aep-specific mod_exp and mod_exp_crt so we use
302 * those functions. NB: We don't use ENGINE_openssl() or
303 * anything "more generic" because something like the RSAref
304 * code may not hook properly, and if you own one of these
305 * cards then you have the right to do RSA operations on it
307 meth1 = RSA_PKCS1_SSLeay();
308 aep_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
309 aep_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
310 aep_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
311 aep_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
315 #ifndef OPENSSL_NO_DSA
316 /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
318 meth2 = DSA_OpenSSL();
319 aep_dsa.dsa_do_sign = meth2->dsa_do_sign;
320 aep_dsa.dsa_sign_setup = meth2->dsa_sign_setup;
321 aep_dsa.dsa_do_verify = meth2->dsa_do_verify;
323 aep_dsa = *DSA_get_default_method();
324 aep_dsa.dsa_mod_exp = aep_dsa_mod_exp;
325 aep_dsa.bn_mod_exp = aep_mod_exp_dsa;
328 #ifndef OPENSSL_NO_DH
329 /* Much the same for Diffie-Hellman */
330 meth3 = DH_OpenSSL();
331 aep_dh.generate_key = meth3->generate_key;
332 aep_dh.compute_key = meth3->compute_key;
333 aep_dh.bn_mod_exp = meth3->bn_mod_exp;
336 /* Ensure the aep error handling is set up */
337 ERR_load_AEPHK_strings();
342 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
343 static int bind_helper(ENGINE *e, const char *id)
345 if(id && (strcmp(id, engine_aep_id) != 0))
351 IMPLEMENT_DYNAMIC_CHECK_FN()
352 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
354 static ENGINE *engine_aep(void)
356 ENGINE *ret = ENGINE_new();
367 void ENGINE_load_aep(void)
369 /* Copied from eng_[openssl|dyn].c */
370 ENGINE *toadd = engine_aep();
378 /* This is a process-global DSO handle used for loading and unloading
379 * the Aep library. NB: This is only set (or unset) during an
380 * init() or finish() call (reference counts permitting) and they're
381 * operating with global locks, so this should be thread-safe
383 static DSO *aep_dso = NULL;
385 /* These are the static string constants for the DSO file name and the function
386 * symbol names to bind to.
388 static const char *AEP_LIBNAME = NULL;
389 static const char *get_AEP_LIBNAME(void)
395 static void free_AEP_LIBNAME(void)
398 OPENSSL_free((void*)AEP_LIBNAME);
401 static long set_AEP_LIBNAME(const char *name)
404 return ((AEP_LIBNAME = BUF_strdup(name)) != NULL ? 1 : 0);
407 static const char *AEP_F1 = "AEP_ModExp";
408 static const char *AEP_F2 = "AEP_ModExpCrt";
410 static const char *AEP_F3 = "AEP_GenRandom";
412 static const char *AEP_F4 = "AEP_Finalize";
413 static const char *AEP_F5 = "AEP_Initialize";
414 static const char *AEP_F6 = "AEP_OpenConnection";
415 static const char *AEP_F7 = "AEP_SetBNCallBacks";
416 static const char *AEP_F8 = "AEP_CloseConnection";
418 /* These are the function pointers that are (un)set when the library has
419 * successfully (un)loaded. */
420 static t_AEP_OpenConnection *p_AEP_OpenConnection = NULL;
421 static t_AEP_CloseConnection *p_AEP_CloseConnection = NULL;
422 static t_AEP_ModExp *p_AEP_ModExp = NULL;
423 static t_AEP_ModExpCrt *p_AEP_ModExpCrt = NULL;
425 static t_AEP_GenRandom *p_AEP_GenRandom = NULL;
427 static t_AEP_Initialize *p_AEP_Initialize = NULL;
428 static t_AEP_Finalize *p_AEP_Finalize = NULL;
429 static t_AEP_SetBNCallBacks *p_AEP_SetBNCallBacks = NULL;
431 /* (de)initialisation functions. */
432 static int aep_init(ENGINE *e)
440 t_AEP_Initialize *p5;
441 t_AEP_OpenConnection *p6;
442 t_AEP_SetBNCallBacks *p7;
443 t_AEP_CloseConnection *p8;
449 AEPHKerr(AEPHK_F_AEP_INIT,AEPHK_R_ALREADY_LOADED);
452 /* Attempt to load libaep.so. */
454 aep_dso = DSO_load(NULL, get_AEP_LIBNAME(), NULL, 0);
458 AEPHKerr(AEPHK_F_AEP_INIT,AEPHK_R_NOT_LOADED);
462 if( !(p1 = (t_AEP_ModExp *) DSO_bind_func( aep_dso,AEP_F1)) ||
463 !(p2 = (t_AEP_ModExpCrt*) DSO_bind_func( aep_dso,AEP_F2)) ||
465 !(p3 = (t_AEP_GenRandom*) DSO_bind_func( aep_dso,AEP_F3)) ||
467 !(p4 = (t_AEP_Finalize*) DSO_bind_func( aep_dso,AEP_F4)) ||
468 !(p5 = (t_AEP_Initialize*) DSO_bind_func( aep_dso,AEP_F5)) ||
469 !(p6 = (t_AEP_OpenConnection*) DSO_bind_func( aep_dso,AEP_F6)) ||
470 !(p7 = (t_AEP_SetBNCallBacks*) DSO_bind_func( aep_dso,AEP_F7)) ||
471 !(p8 = (t_AEP_CloseConnection*) DSO_bind_func( aep_dso,AEP_F8)))
473 AEPHKerr(AEPHK_F_AEP_INIT,AEPHK_R_NOT_LOADED);
477 /* Copy the pointers */
480 p_AEP_ModExpCrt = p2;
482 p_AEP_GenRandom = p3;
485 p_AEP_Initialize = p5;
486 p_AEP_OpenConnection = p6;
487 p_AEP_SetBNCallBacks = p7;
488 p_AEP_CloseConnection = p8;
500 p_AEP_OpenConnection = NULL;
502 p_AEP_ModExpCrt = NULL;
504 p_AEP_GenRandom = NULL;
506 p_AEP_Initialize = NULL;
507 p_AEP_Finalize = NULL;
508 p_AEP_SetBNCallBacks = NULL;
509 p_AEP_CloseConnection = NULL;
514 /* Destructor (complements the "ENGINE_aep()" constructor) */
515 static int aep_destroy(ENGINE *e)
518 ERR_unload_AEPHK_strings();
522 static int aep_finish(ENGINE *e)
524 int to_return = 0, in_use;
529 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_NOT_LOADED);
533 rv = aep_close_all_connections(0, &in_use);
536 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_CLOSE_HANDLES_FAILED);
541 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_CONNECTIONS_IN_USE);
545 rv = p_AEP_Finalize();
548 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_FINALIZE_FAILED);
552 if(!DSO_free(aep_dso))
554 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_UNIT_FAILURE);
559 p_AEP_CloseConnection = NULL;
560 p_AEP_OpenConnection = NULL;
562 p_AEP_ModExpCrt = NULL;
564 p_AEP_GenRandom = NULL;
566 p_AEP_Initialize = NULL;
567 p_AEP_Finalize = NULL;
568 p_AEP_SetBNCallBacks = NULL;
575 static int aep_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
577 int initialised = ((aep_dso == NULL) ? 0 : 1);
580 case AEP_CMD_SO_PATH:
583 AEPHKerr(AEPHK_F_AEP_CTRL,
584 ERR_R_PASSED_NULL_PARAMETER);
589 AEPHKerr(AEPHK_F_AEP_CTRL,
590 AEPHK_R_ALREADY_LOADED);
593 return set_AEP_LIBNAME((const char*)p);
597 AEPHKerr(AEPHK_F_AEP_CTRL,AEPHK_R_CTRL_COMMAND_NOT_IMPLEMENTED);
601 static int aep_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
602 const BIGNUM *m, BN_CTX *ctx)
606 AEP_CONNECTION_HNDL hConnection;
609 r_len = BN_num_bits(m);
611 /* Perform in software if modulus is too large for hardware. */
613 if (r_len > max_key_len){
614 AEPHKerr(AEPHK_F_AEP_MOD_EXP, AEPHK_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
615 return BN_mod_exp(r, a, p, m, ctx);
618 /*Grab a connection from the pool*/
619 rv = aep_get_connection(&hConnection);
622 AEPHKerr(AEPHK_F_AEP_MOD_EXP,AEPHK_R_GET_HANDLE_FAILED);
623 return BN_mod_exp(r, a, p, m, ctx);
626 /*To the card with the mod exp*/
627 rv = p_AEP_ModExp(hConnection,(void*)a, (void*)p,(void*)m, (void*)r,NULL);
631 AEPHKerr(AEPHK_F_AEP_MOD_EXP,AEPHK_R_MOD_EXP_FAILED);
632 rv = aep_close_connection(hConnection);
633 return BN_mod_exp(r, a, p, m, ctx);
636 /*Return the connection to the pool*/
637 rv = aep_return_connection(hConnection);
640 AEPHKerr(AEPHK_F_AEP_MOD_EXP,AEPHK_R_RETURN_CONNECTION_FAILED);
649 #ifndef OPENSSL_NO_RSA
650 static AEP_RV aep_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
651 const BIGNUM *q, const BIGNUM *dmp1,
652 const BIGNUM *dmq1,const BIGNUM *iqmp, BN_CTX *ctx)
654 AEP_RV rv = AEP_R_OK;
655 AEP_CONNECTION_HNDL hConnection;
657 /*Grab a connection from the pool*/
658 rv = aep_get_connection(&hConnection);
661 AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT,AEPHK_R_GET_HANDLE_FAILED);
665 /*To the card with the mod exp*/
666 rv = p_AEP_ModExpCrt(hConnection,(void*)a, (void*)p, (void*)q, (void*)dmp1,(void*)dmq1,
667 (void*)iqmp,(void*)r,NULL);
670 AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT,AEPHK_R_MOD_EXP_CRT_FAILED);
671 rv = aep_close_connection(hConnection);
675 /*Return the connection to the pool*/
676 rv = aep_return_connection(hConnection);
679 AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT,AEPHK_R_RETURN_CONNECTION_FAILED);
690 static int aep_rand(unsigned char *buf,int len )
692 AEP_RV rv = AEP_R_OK;
693 AEP_CONNECTION_HNDL hConnection;
695 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
697 /*Can the request be serviced with what's already in the buffer?*/
698 if (len <= rand_block_bytes)
700 memcpy(buf, &rand_block[RAND_BLK_SIZE - rand_block_bytes], len);
701 rand_block_bytes -= len;
702 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
705 /*If not the get another block of random bytes*/
707 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
709 rv = aep_get_connection(&hConnection);
712 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_HANDLE_FAILED);
716 if (len > RAND_BLK_SIZE)
718 rv = p_AEP_GenRandom(hConnection, len, 2, buf, NULL);
721 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_RANDOM_FAILED);
727 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
729 rv = p_AEP_GenRandom(hConnection, RAND_BLK_SIZE, 2, &rand_block[0], NULL);
732 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_RANDOM_FAILED);
737 rand_block_bytes = RAND_BLK_SIZE;
739 memcpy(buf, &rand_block[RAND_BLK_SIZE - rand_block_bytes], len);
740 rand_block_bytes -= len;
742 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
745 rv = aep_return_connection(hConnection);
748 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_RETURN_CONNECTION_FAILED);
756 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
761 static int aep_rand_status(void)
767 #ifndef OPENSSL_NO_RSA
768 static int aep_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
771 AEP_RV rv = AEP_R_OK;
775 AEPHKerr(AEPHK_F_AEP_RSA_MOD_EXP,AEPHK_R_NOT_LOADED);
779 /*See if we have all the necessary bits for a crt*/
780 if (rsa->q && rsa->dmp1 && rsa->dmq1 && rsa->iqmp)
782 rv = aep_mod_exp_crt(r0,I,rsa->p,rsa->q, rsa->dmp1,rsa->dmq1,rsa->iqmp,ctx);
784 if (rv == FAIL_TO_SW){
785 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
786 to_return = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
789 else if (rv != AEP_R_OK)
794 if (!rsa->d || !rsa->n)
796 AEPHKerr(AEPHK_F_AEP_RSA_MOD_EXP,AEPHK_R_MISSING_KEY_COMPONENTS);
800 rv = aep_mod_exp(r0,I,rsa->d,rsa->n,ctx);
813 #ifndef OPENSSL_NO_DSA
814 static int aep_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
815 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
816 BN_CTX *ctx, BN_MONT_CTX *in_mont)
822 /* let rr = a1 ^ p1 mod m */
823 if (!aep_mod_exp(rr,a1,p1,m,ctx)) goto end;
824 /* let t = a2 ^ p2 mod m */
825 if (!aep_mod_exp(&t,a2,p2,m,ctx)) goto end;
826 /* let rr = rr * t mod m */
827 if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
834 static int aep_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
835 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
838 return aep_mod_exp(r, a, p, m, ctx);
842 #ifndef OPENSSL_NO_RSA
843 /* This function is aliased to mod_exp (with the mont stuff dropped). */
844 static int aep_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
845 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
847 return aep_mod_exp(r, a, p, m, ctx);
851 #ifndef OPENSSL_NO_DH
852 /* This function is aliased to mod_exp (with the dh and mont dropped). */
853 static int aep_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
854 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
857 return aep_mod_exp(r, a, p, m, ctx);
861 static AEP_RV aep_get_connection(AEP_CONNECTION_HNDL_PTR phConnection)
864 AEP_RV rv = AEP_R_OK;
866 /*Get the current process id*/
869 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
873 /*Check if this is the first time this is being called from the current
875 if (recorded_pid != curr_pid)
877 /*Remember our pid so we can check if we're in a new process*/
878 recorded_pid = curr_pid;
880 /*Call Finalize to make sure we have not inherited some data
881 from a parent process*/
884 /*Initialise the AEP API*/
885 rv = p_AEP_Initialize(NULL);
889 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_INIT_FAILURE);
894 /*Set the AEP big num call back functions*/
895 rv = p_AEP_SetBNCallBacks(&GetBigNumSize, &MakeAEPBigNum,
900 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_SETBNCALLBACK_FAILURE);
906 /*Reset the rand byte count*/
907 rand_block_bytes = 0;
910 /*Init the structures*/
911 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
913 aep_app_conn_table[count].conn_state = NotConnected;
914 aep_app_conn_table[count].conn_hndl = 0;
917 /*Open a connection*/
918 rv = p_AEP_OpenConnection(phConnection);
922 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_UNIT_FAILURE);
927 aep_app_conn_table[0].conn_state = InUse;
928 aep_app_conn_table[0].conn_hndl = *phConnection;
931 /*Check the existing connections to see if we can find a free one*/
932 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
934 if (aep_app_conn_table[count].conn_state == Connected)
936 aep_app_conn_table[count].conn_state = InUse;
937 *phConnection = aep_app_conn_table[count].conn_hndl;
941 /*If no connections available, we're going to have to try
943 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
945 if (aep_app_conn_table[count].conn_state == NotConnected)
947 /*Open a connection*/
948 rv = p_AEP_OpenConnection(phConnection);
952 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_UNIT_FAILURE);
956 aep_app_conn_table[count].conn_state = InUse;
957 aep_app_conn_table[count].conn_hndl = *phConnection;
961 rv = AEP_R_GENERAL_ERROR;
963 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
968 static AEP_RV aep_return_connection(AEP_CONNECTION_HNDL hConnection)
972 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
974 /*Find the connection item that matches this connection handle*/
975 for(count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
977 if (aep_app_conn_table[count].conn_hndl == hConnection)
979 aep_app_conn_table[count].conn_state = Connected;
984 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
989 static AEP_RV aep_close_connection(AEP_CONNECTION_HNDL hConnection)
992 AEP_RV rv = AEP_R_OK;
994 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
996 /*Find the connection item that matches this connection handle*/
997 for(count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
999 if (aep_app_conn_table[count].conn_hndl == hConnection)
1001 rv = p_AEP_CloseConnection(aep_app_conn_table[count].conn_hndl);
1004 aep_app_conn_table[count].conn_state = NotConnected;
1005 aep_app_conn_table[count].conn_hndl = 0;
1011 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
1015 static AEP_RV aep_close_all_connections(int use_engine_lock, int *in_use)
1018 AEP_RV rv = AEP_R_OK;
1021 if (use_engine_lock) CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
1022 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
1024 switch (aep_app_conn_table[count].conn_state)
1027 rv = p_AEP_CloseConnection(aep_app_conn_table[count].conn_hndl);
1030 aep_app_conn_table[count].conn_state = NotConnected;
1031 aep_app_conn_table[count].conn_hndl = 0;
1041 if (use_engine_lock) CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
1045 /*BigNum call back functions, used to convert OpenSSL bignums into AEP bignums.
1046 Note only 32bit Openssl build support*/
1048 static AEP_RV GetBigNumSize(AEP_VOID_PTR ArbBigNum, AEP_U32* BigNumSize)
1052 /*Cast the ArbBigNum pointer to our BIGNUM struct*/
1053 bn = (BIGNUM*) ArbBigNum;
1055 #ifdef SIXTY_FOUR_BIT_LONG
1056 *BigNumSize = bn->top << 3;
1058 /*Size of the bignum in bytes is equal to the bn->top (no of 32 bit
1059 words) multiplies by 4*/
1060 *BigNumSize = bn->top << 2;
1066 static AEP_RV MakeAEPBigNum(AEP_VOID_PTR ArbBigNum, AEP_U32 BigNumSize,
1067 unsigned char* AEP_BigNum)
1071 #ifndef SIXTY_FOUR_BIT_LONG
1076 /*Cast the ArbBigNum pointer to our BIGNUM struct*/
1077 bn = (BIGNUM*) ArbBigNum;
1079 #ifdef SIXTY_FOUR_BIT_LONG
1080 memcpy(AEP_BigNum, bn->d, BigNumSize);
1082 /*Must copy data into a (monotone) least significant byte first format
1083 performing endian conversion if necessary*/
1084 for(i=0;i<bn->top;i++)
1086 buf = (unsigned char*)&bn->d[i];
1088 *((AEP_U32*)AEP_BigNum) = (AEP_U32)
1089 ((unsigned) buf[1] << 8 | buf[0]) |
1090 ((unsigned) buf[3] << 8 | buf[2]) << 16;
1099 /*Turn an AEP Big Num back to a user big num*/
1100 static AEP_RV ConvertAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
1101 unsigned char* AEP_BigNum)
1104 #ifndef SIXTY_FOUR_BIT_LONG
1108 bn = (BIGNUM*)ArbBigNum;
1110 /*Expand the result bn so that it can hold our big num.
1112 bn_expand(bn, (int)(BigNumSize << 3));
1114 #ifdef SIXTY_FOUR_BIT_LONG
1115 bn->top = BigNumSize >> 3;
1117 if((BigNumSize & 7) != 0)
1120 memset(bn->d, 0, bn->top << 3);
1122 memcpy(bn->d, AEP_BigNum, BigNumSize);
1124 bn->top = BigNumSize >> 2;
1126 for(i=0;i<bn->top;i++)
1128 bn->d[i] = (AEP_U32)
1129 ((unsigned) AEP_BigNum[3] << 8 | AEP_BigNum[2]) << 16 |
1130 ((unsigned) AEP_BigNum[1] << 8 | AEP_BigNum[0]);
1138 #endif /* !OPENSSL_NO_HW_AEP */
1139 #endif /* !OPENSSL_NO_HW */