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>
74 #include <openssl/dh.h>
75 #include <openssl/bn.h>
78 #ifndef OPENSSL_NO_HW_AEP
82 #include "vendor_defns/aep.h"
85 #define AEP_LIB_NAME "aep engine"
86 #define FAIL_TO_SW 0x10101010
88 #include "e_aep_err.c"
90 static int aep_init(ENGINE *e);
91 static int aep_finish(ENGINE *e);
92 static int aep_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
93 static int aep_destroy(ENGINE *e);
95 static AEP_RV aep_get_connection(AEP_CONNECTION_HNDL_PTR hConnection);
96 static AEP_RV aep_return_connection(AEP_CONNECTION_HNDL hConnection);
97 static AEP_RV aep_close_connection(AEP_CONNECTION_HNDL hConnection);
98 static AEP_RV aep_close_all_connections(int use_engine_lock, int *in_use);
101 static int aep_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
102 const BIGNUM *m, BN_CTX *ctx);
104 static AEP_RV aep_mod_exp_crt(BIGNUM *r,const BIGNUM *a, const BIGNUM *p,
105 const BIGNUM *q, const BIGNUM *dmp1,const BIGNUM *dmq1,
106 const BIGNUM *iqmp, BN_CTX *ctx);
109 #ifndef OPENSSL_NO_RSA
110 static int aep_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
113 /* This function is aliased to mod_exp (with the mont stuff dropped). */
114 static int aep_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
115 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
118 #ifndef OPENSSL_NO_DSA
119 static int aep_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
120 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
121 BN_CTX *ctx, BN_MONT_CTX *in_mont);
123 static int aep_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
124 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
129 /* This function is aliased to mod_exp (with the DH and mont dropped). */
130 #ifndef OPENSSL_NO_DH
131 static int aep_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
132 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
137 static int aep_rand(unsigned char *buf, int num);
138 static int aep_rand_status(void);
141 /* Bignum conversion stuff */
142 static AEP_RV GetBigNumSize(AEP_VOID_PTR ArbBigNum, AEP_U32* BigNumSize);
143 static AEP_RV MakeAEPBigNum(AEP_VOID_PTR ArbBigNum, AEP_U32 BigNumSize,
144 unsigned char* AEP_BigNum);
145 static AEP_RV ConvertAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
146 unsigned char* AEP_BigNum);
148 /* The definitions for control commands specific to this engine */
149 #define AEP_CMD_SO_PATH ENGINE_CMD_BASE
150 static const ENGINE_CMD_DEFN aep_cmd_defns[] =
154 "Specifies the path to the 'aep' shared library",
155 ENGINE_CMD_FLAG_STRING
160 #ifndef OPENSSL_NO_RSA
161 /* Our internal RSA_METHOD that we provide pointers to */
162 static RSA_METHOD aep_rsa =
165 NULL, /*rsa_pub_encrypt*/
166 NULL, /*rsa_pub_decrypt*/
167 NULL, /*rsa_priv_encrypt*/
168 NULL, /*rsa_priv_encrypt*/
169 aep_rsa_mod_exp, /*rsa_mod_exp*/
170 aep_mod_exp_mont, /*bn_mod_exp*/
181 #ifndef OPENSSL_NO_DSA
182 /* Our internal DSA_METHOD that we provide pointers to */
183 static DSA_METHOD aep_dsa =
186 NULL, /* dsa_do_sign */
187 NULL, /* dsa_sign_setup */
188 NULL, /* dsa_do_verify */
189 aep_dsa_mod_exp, /* dsa_mod_exp */
190 aep_mod_exp_dsa, /* bn_mod_exp */
195 NULL, /* dsa_paramgen */
196 NULL /* dsa_keygen */
200 #ifndef OPENSSL_NO_DH
201 /* Our internal DH_METHOD that we provide pointers to */
202 static DH_METHOD aep_dh =
217 /* our internal RAND_method that we provide pointers to */
218 static RAND_METHOD aep_random =
220 /*"AEP RAND method", */
230 /*Define an array of structures to hold connections*/
231 static AEP_CONNECTION_ENTRY aep_app_conn_table[MAX_PROCESS_CONNECTIONS];
233 /*Used to determine if this is a new process*/
234 static pid_t recorded_pid = 0;
237 static AEP_U8 rand_block[RAND_BLK_SIZE];
238 static AEP_U32 rand_block_bytes = 0;
241 /* Constants used when creating the ENGINE */
242 static const char *engine_aep_id = "aep";
243 static const char *engine_aep_name = "Aep hardware engine support";
245 static int max_key_len = 2176;
248 /* This internal function is used by ENGINE_aep() and possibly by the
249 * "dynamic" ENGINE support too */
250 static int bind_aep(ENGINE *e)
252 #ifndef OPENSSL_NO_RSA
253 const RSA_METHOD *meth1;
255 #ifndef OPENSSL_NO_DSA
256 const DSA_METHOD *meth2;
258 #ifndef OPENSSL_NO_DH
259 const DH_METHOD *meth3;
262 if(!ENGINE_set_id(e, engine_aep_id) ||
263 !ENGINE_set_name(e, engine_aep_name) ||
264 #ifndef OPENSSL_NO_RSA
265 !ENGINE_set_RSA(e, &aep_rsa) ||
267 #ifndef OPENSSL_NO_DSA
268 !ENGINE_set_DSA(e, &aep_dsa) ||
270 #ifndef OPENSSL_NO_DH
271 !ENGINE_set_DH(e, &aep_dh) ||
274 !ENGINE_set_RAND(e, &aep_random) ||
276 !ENGINE_set_init_function(e, aep_init) ||
277 !ENGINE_set_destroy_function(e, aep_destroy) ||
278 !ENGINE_set_finish_function(e, aep_finish) ||
279 !ENGINE_set_ctrl_function(e, aep_ctrl) ||
280 !ENGINE_set_cmd_defns(e, aep_cmd_defns))
283 #ifndef OPENSSL_NO_RSA
284 /* We know that the "PKCS1_SSLeay()" functions hook properly
285 * to the aep-specific mod_exp and mod_exp_crt so we use
286 * those functions. NB: We don't use ENGINE_openssl() or
287 * anything "more generic" because something like the RSAref
288 * code may not hook properly, and if you own one of these
289 * cards then you have the right to do RSA operations on it
291 meth1 = RSA_PKCS1_SSLeay();
292 aep_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
293 aep_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
294 aep_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
295 aep_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
299 #ifndef OPENSSL_NO_DSA
300 /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
302 meth2 = DSA_OpenSSL();
303 aep_dsa.dsa_do_sign = meth2->dsa_do_sign;
304 aep_dsa.dsa_sign_setup = meth2->dsa_sign_setup;
305 aep_dsa.dsa_do_verify = meth2->dsa_do_verify;
307 aep_dsa = *DSA_get_default_method();
308 aep_dsa.dsa_mod_exp = aep_dsa_mod_exp;
309 aep_dsa.bn_mod_exp = aep_mod_exp_dsa;
312 #ifndef OPENSSL_NO_DH
313 /* Much the same for Diffie-Hellman */
314 meth3 = DH_OpenSSL();
315 aep_dh.generate_key = meth3->generate_key;
316 aep_dh.compute_key = meth3->compute_key;
317 aep_dh.bn_mod_exp = meth3->bn_mod_exp;
320 /* Ensure the aep error handling is set up */
321 ERR_load_AEPHK_strings();
326 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
327 static int bind_helper(ENGINE *e, const char *id)
329 if(id && (strcmp(id, engine_aep_id) != 0))
335 IMPLEMENT_DYNAMIC_CHECK_FN()
336 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
338 static ENGINE *engine_aep(void)
340 ENGINE *ret = ENGINE_new();
351 void ENGINE_load_aep(void)
353 /* Copied from eng_[openssl|dyn].c */
354 ENGINE *toadd = engine_aep();
362 /* This is a process-global DSO handle used for loading and unloading
363 * the Aep library. NB: This is only set (or unset) during an
364 * init() or finish() call (reference counts permitting) and they're
365 * operating with global locks, so this should be thread-safe
367 static DSO *aep_dso = NULL;
369 /* These are the static string constants for the DSO file name and the function
370 * symbol names to bind to.
372 static const char *AEP_LIBNAME = NULL;
373 static const char *get_AEP_LIBNAME(void)
379 static void free_AEP_LIBNAME(void)
382 OPENSSL_free((void*)AEP_LIBNAME);
385 static long set_AEP_LIBNAME(const char *name)
388 return ((AEP_LIBNAME = BUF_strdup(name)) != NULL ? 1 : 0);
391 static const char *AEP_F1 = "AEP_ModExp";
392 static const char *AEP_F2 = "AEP_ModExpCrt";
394 static const char *AEP_F3 = "AEP_GenRandom";
396 static const char *AEP_F4 = "AEP_Finalize";
397 static const char *AEP_F5 = "AEP_Initialize";
398 static const char *AEP_F6 = "AEP_OpenConnection";
399 static const char *AEP_F7 = "AEP_SetBNCallBacks";
400 static const char *AEP_F8 = "AEP_CloseConnection";
402 /* These are the function pointers that are (un)set when the library has
403 * successfully (un)loaded. */
404 static t_AEP_OpenConnection *p_AEP_OpenConnection = NULL;
405 static t_AEP_CloseConnection *p_AEP_CloseConnection = NULL;
406 static t_AEP_ModExp *p_AEP_ModExp = NULL;
407 static t_AEP_ModExpCrt *p_AEP_ModExpCrt = NULL;
409 static t_AEP_GenRandom *p_AEP_GenRandom = NULL;
411 static t_AEP_Initialize *p_AEP_Initialize = NULL;
412 static t_AEP_Finalize *p_AEP_Finalize = NULL;
413 static t_AEP_SetBNCallBacks *p_AEP_SetBNCallBacks = NULL;
415 /* (de)initialisation functions. */
416 static int aep_init(ENGINE *e)
424 t_AEP_Initialize *p5;
425 t_AEP_OpenConnection *p6;
426 t_AEP_SetBNCallBacks *p7;
427 t_AEP_CloseConnection *p8;
433 AEPHKerr(AEPHK_F_AEP_INIT,AEPHK_R_ALREADY_LOADED);
436 /* Attempt to load libaep.so. */
438 aep_dso = DSO_load(NULL, get_AEP_LIBNAME(), NULL, 0);
442 AEPHKerr(AEPHK_F_AEP_INIT,AEPHK_R_NOT_LOADED);
446 if( !(p1 = (t_AEP_ModExp *) DSO_bind_func( aep_dso,AEP_F1)) ||
447 !(p2 = (t_AEP_ModExpCrt*) DSO_bind_func( aep_dso,AEP_F2)) ||
449 !(p3 = (t_AEP_GenRandom*) DSO_bind_func( aep_dso,AEP_F3)) ||
451 !(p4 = (t_AEP_Finalize*) DSO_bind_func( aep_dso,AEP_F4)) ||
452 !(p5 = (t_AEP_Initialize*) DSO_bind_func( aep_dso,AEP_F5)) ||
453 !(p6 = (t_AEP_OpenConnection*) DSO_bind_func( aep_dso,AEP_F6)) ||
454 !(p7 = (t_AEP_SetBNCallBacks*) DSO_bind_func( aep_dso,AEP_F7)) ||
455 !(p8 = (t_AEP_CloseConnection*) DSO_bind_func( aep_dso,AEP_F8)))
457 AEPHKerr(AEPHK_F_AEP_INIT,AEPHK_R_NOT_LOADED);
461 /* Copy the pointers */
464 p_AEP_ModExpCrt = p2;
466 p_AEP_GenRandom = p3;
469 p_AEP_Initialize = p5;
470 p_AEP_OpenConnection = p6;
471 p_AEP_SetBNCallBacks = p7;
472 p_AEP_CloseConnection = p8;
483 p_AEP_OpenConnection = NULL;
485 p_AEP_ModExpCrt = NULL;
487 p_AEP_GenRandom = NULL;
489 p_AEP_Initialize = NULL;
490 p_AEP_Finalize = NULL;
491 p_AEP_SetBNCallBacks = NULL;
492 p_AEP_CloseConnection = NULL;
497 /* Destructor (complements the "ENGINE_aep()" constructor) */
498 static int aep_destroy(ENGINE *e)
501 ERR_unload_AEPHK_strings();
505 static int aep_finish(ENGINE *e)
507 int to_return = 0, in_use;
512 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_NOT_LOADED);
516 rv = aep_close_all_connections(0, &in_use);
519 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_CLOSE_HANDLES_FAILED);
524 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_CONNECTIONS_IN_USE);
528 rv = p_AEP_Finalize();
531 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_FINALIZE_FAILED);
535 if(!DSO_free(aep_dso))
537 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_UNIT_FAILURE);
542 p_AEP_CloseConnection = NULL;
543 p_AEP_OpenConnection = NULL;
545 p_AEP_ModExpCrt = NULL;
547 p_AEP_GenRandom = NULL;
549 p_AEP_Initialize = NULL;
550 p_AEP_Finalize = NULL;
551 p_AEP_SetBNCallBacks = NULL;
558 static int aep_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
560 int initialised = ((aep_dso == NULL) ? 0 : 1);
563 case AEP_CMD_SO_PATH:
566 AEPHKerr(AEPHK_F_AEP_CTRL,
567 ERR_R_PASSED_NULL_PARAMETER);
572 AEPHKerr(AEPHK_F_AEP_CTRL,
573 AEPHK_R_ALREADY_LOADED);
576 return set_AEP_LIBNAME((const char*)p);
580 AEPHKerr(AEPHK_F_AEP_CTRL,AEPHK_R_CTRL_COMMAND_NOT_IMPLEMENTED);
584 static int aep_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
585 const BIGNUM *m, BN_CTX *ctx)
589 AEP_CONNECTION_HNDL hConnection;
592 r_len = BN_num_bits(m);
594 /* Perform in software if modulus is too large for hardware. */
596 if (r_len > max_key_len){
597 AEPHKerr(AEPHK_F_AEP_MOD_EXP, AEPHK_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
598 return BN_mod_exp(r, a, p, m, ctx);
601 /*Grab a connection from the pool*/
602 rv = aep_get_connection(&hConnection);
605 AEPHKerr(AEPHK_F_AEP_MOD_EXP,AEPHK_R_GET_HANDLE_FAILED);
606 return BN_mod_exp(r, a, p, m, ctx);
609 /*To the card with the mod exp*/
610 rv = p_AEP_ModExp(hConnection,(void*)a, (void*)p,(void*)m, (void*)r,NULL);
614 AEPHKerr(AEPHK_F_AEP_MOD_EXP,AEPHK_R_MOD_EXP_FAILED);
615 rv = aep_close_connection(hConnection);
616 return BN_mod_exp(r, a, p, m, ctx);
619 /*Return the connection to the pool*/
620 rv = aep_return_connection(hConnection);
623 AEPHKerr(AEPHK_F_AEP_MOD_EXP,AEPHK_R_RETURN_CONNECTION_FAILED);
632 static AEP_RV aep_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
633 const BIGNUM *q, const BIGNUM *dmp1,
634 const BIGNUM *dmq1,const BIGNUM *iqmp, BN_CTX *ctx)
636 AEP_RV rv = AEP_R_OK;
637 AEP_CONNECTION_HNDL hConnection;
639 /*Grab a connection from the pool*/
640 rv = aep_get_connection(&hConnection);
643 AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT,AEPHK_R_GET_HANDLE_FAILED);
647 /*To the card with the mod exp*/
648 rv = p_AEP_ModExpCrt(hConnection,(void*)a, (void*)p, (void*)q, (void*)dmp1,(void*)dmq1,
649 (void*)iqmp,(void*)r,NULL);
652 AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT,AEPHK_R_MOD_EXP_CRT_FAILED);
653 rv = aep_close_connection(hConnection);
657 /*Return the connection to the pool*/
658 rv = aep_return_connection(hConnection);
661 AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT,AEPHK_R_RETURN_CONNECTION_FAILED);
671 static int aep_rand(unsigned char *buf,int len )
673 AEP_RV rv = AEP_R_OK;
674 AEP_CONNECTION_HNDL hConnection;
676 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
678 /*Can the request be serviced with what's already in the buffer?*/
679 if (len <= rand_block_bytes)
681 memcpy(buf, &rand_block[RAND_BLK_SIZE - rand_block_bytes], len);
682 rand_block_bytes -= len;
683 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
686 /*If not the get another block of random bytes*/
688 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
690 rv = aep_get_connection(&hConnection);
693 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_HANDLE_FAILED);
697 if (len > RAND_BLK_SIZE)
699 rv = p_AEP_GenRandom(hConnection, len, 2, buf, NULL);
702 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_RANDOM_FAILED);
708 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
710 rv = p_AEP_GenRandom(hConnection, RAND_BLK_SIZE, 2, &rand_block[0], NULL);
713 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_RANDOM_FAILED);
718 rand_block_bytes = RAND_BLK_SIZE;
720 memcpy(buf, &rand_block[RAND_BLK_SIZE - rand_block_bytes], len);
721 rand_block_bytes -= len;
723 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
726 rv = aep_return_connection(hConnection);
729 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_RETURN_CONNECTION_FAILED);
737 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
742 static int aep_rand_status(void)
748 #ifndef OPENSSL_NO_RSA
749 static int aep_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
752 AEP_RV rv = AEP_R_OK;
756 AEPHKerr(AEPHK_F_AEP_RSA_MOD_EXP,AEPHK_R_NOT_LOADED);
760 /*See if we have all the necessary bits for a crt*/
761 if (rsa->q && rsa->dmp1 && rsa->dmq1 && rsa->iqmp)
763 rv = aep_mod_exp_crt(r0,I,rsa->p,rsa->q, rsa->dmp1,rsa->dmq1,rsa->iqmp,ctx);
765 if (rv == FAIL_TO_SW){
766 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
767 to_return = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
770 else if (rv != AEP_R_OK)
775 if (!rsa->d || !rsa->n)
777 AEPHKerr(AEPHK_F_AEP_RSA_MOD_EXP,AEPHK_R_MISSING_KEY_COMPONENTS);
781 rv = aep_mod_exp(r0,I,rsa->d,rsa->n,ctx);
794 #ifndef OPENSSL_NO_DSA
795 static int aep_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
796 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
797 BN_CTX *ctx, BN_MONT_CTX *in_mont)
803 /* let rr = a1 ^ p1 mod m */
804 if (!aep_mod_exp(rr,a1,p1,m,ctx)) goto end;
805 /* let t = a2 ^ p2 mod m */
806 if (!aep_mod_exp(&t,a2,p2,m,ctx)) goto end;
807 /* let rr = rr * t mod m */
808 if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
815 static int aep_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
816 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
819 return aep_mod_exp(r, a, p, m, ctx);
823 /* This function is aliased to mod_exp (with the mont stuff dropped). */
824 static int aep_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
825 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
827 return aep_mod_exp(r, a, p, m, ctx);
830 #ifndef OPENSSL_NO_DH
831 /* This function is aliased to mod_exp (with the dh and mont dropped). */
832 static int aep_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
833 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
836 return aep_mod_exp(r, a, p, m, ctx);
840 static AEP_RV aep_get_connection(AEP_CONNECTION_HNDL_PTR phConnection)
843 AEP_RV rv = AEP_R_OK;
845 /*Get the current process id*/
848 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
853 curr_pid = GetThreadID();
856 /*Check if this is the first time this is being called from the current
858 if (recorded_pid != curr_pid)
860 /*Remember our pid so we can check if we're in a new process*/
861 recorded_pid = curr_pid;
863 /*Call Finalize to make sure we have not inherited some data
864 from a parent process*/
867 /*Initialise the AEP API*/
868 rv = p_AEP_Initialize(NULL);
872 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_INIT_FAILURE);
877 /*Set the AEP big num call back functions*/
878 rv = p_AEP_SetBNCallBacks(&GetBigNumSize, &MakeAEPBigNum,
883 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_SETBNCALLBACK_FAILURE);
889 /*Reset the rand byte count*/
890 rand_block_bytes = 0;
893 /*Init the structures*/
894 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
896 aep_app_conn_table[count].conn_state = NotConnected;
897 aep_app_conn_table[count].conn_hndl = 0;
900 /*Open a connection*/
901 rv = p_AEP_OpenConnection(phConnection);
905 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_UNIT_FAILURE);
910 aep_app_conn_table[0].conn_state = InUse;
911 aep_app_conn_table[0].conn_hndl = *phConnection;
914 /*Check the existing connections to see if we can find a free one*/
915 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
917 if (aep_app_conn_table[count].conn_state == Connected)
919 aep_app_conn_table[count].conn_state = InUse;
920 *phConnection = aep_app_conn_table[count].conn_hndl;
924 /*If no connections available, we're going to have to try
926 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
928 if (aep_app_conn_table[count].conn_state == NotConnected)
930 /*Open a connection*/
931 rv = p_AEP_OpenConnection(phConnection);
935 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_UNIT_FAILURE);
939 aep_app_conn_table[count].conn_state = InUse;
940 aep_app_conn_table[count].conn_hndl = *phConnection;
944 rv = AEP_R_GENERAL_ERROR;
946 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
951 static AEP_RV aep_return_connection(AEP_CONNECTION_HNDL hConnection)
955 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
957 /*Find the connection item that matches this connection handle*/
958 for(count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
960 if (aep_app_conn_table[count].conn_hndl == hConnection)
962 aep_app_conn_table[count].conn_state = Connected;
967 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
972 static AEP_RV aep_close_connection(AEP_CONNECTION_HNDL hConnection)
975 AEP_RV rv = AEP_R_OK;
977 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
979 /*Find the connection item that matches this connection handle*/
980 for(count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
982 if (aep_app_conn_table[count].conn_hndl == hConnection)
984 rv = p_AEP_CloseConnection(aep_app_conn_table[count].conn_hndl);
987 aep_app_conn_table[count].conn_state = NotConnected;
988 aep_app_conn_table[count].conn_hndl = 0;
994 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
998 static AEP_RV aep_close_all_connections(int use_engine_lock, int *in_use)
1001 AEP_RV rv = AEP_R_OK;
1004 if (use_engine_lock) CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
1005 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
1007 switch (aep_app_conn_table[count].conn_state)
1010 rv = p_AEP_CloseConnection(aep_app_conn_table[count].conn_hndl);
1013 aep_app_conn_table[count].conn_state = NotConnected;
1014 aep_app_conn_table[count].conn_hndl = 0;
1024 if (use_engine_lock) CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
1028 /*BigNum call back functions, used to convert OpenSSL bignums into AEP bignums.
1029 Note only 32bit Openssl build support*/
1031 static AEP_RV GetBigNumSize(AEP_VOID_PTR ArbBigNum, AEP_U32* BigNumSize)
1035 /*Cast the ArbBigNum pointer to our BIGNUM struct*/
1036 bn = (BIGNUM*) ArbBigNum;
1038 #ifdef SIXTY_FOUR_BIT_LONG
1039 *BigNumSize = bn->top << 3;
1041 /*Size of the bignum in bytes is equal to the bn->top (no of 32 bit
1042 words) multiplies by 4*/
1043 *BigNumSize = bn->top << 2;
1049 static AEP_RV MakeAEPBigNum(AEP_VOID_PTR ArbBigNum, AEP_U32 BigNumSize,
1050 unsigned char* AEP_BigNum)
1054 #ifndef SIXTY_FOUR_BIT_LONG
1059 /*Cast the ArbBigNum pointer to our BIGNUM struct*/
1060 bn = (BIGNUM*) ArbBigNum;
1062 #ifdef SIXTY_FOUR_BIT_LONG
1063 memcpy(AEP_BigNum, bn->d, BigNumSize);
1065 /*Must copy data into a (monotone) least significant byte first format
1066 performing endian conversion if necessary*/
1067 for(i=0;i<bn->top;i++)
1069 buf = (unsigned char*)&bn->d[i];
1071 *((AEP_U32*)AEP_BigNum) = (AEP_U32)
1072 ((unsigned) buf[1] << 8 | buf[0]) |
1073 ((unsigned) buf[3] << 8 | buf[2]) << 16;
1082 /*Turn an AEP Big Num back to a user big num*/
1083 static AEP_RV ConvertAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
1084 unsigned char* AEP_BigNum)
1087 #ifndef SIXTY_FOUR_BIT_LONG
1091 bn = (BIGNUM*)ArbBigNum;
1093 /*Expand the result bn so that it can hold our big num.
1095 bn_expand(bn, (int)(BigNumSize << 3));
1097 #ifdef SIXTY_FOUR_BIT_LONG
1098 bn->top = BigNumSize >> 3;
1100 if((BigNumSize & 7) != 0)
1103 memset(bn->d, 0, bn->top << 3);
1105 memcpy(bn->d, AEP_BigNum, BigNumSize);
1107 bn->top = BigNumSize >> 2;
1109 for(i=0;i<bn->top;i++)
1111 bn->d[i] = (AEP_U32)
1112 ((unsigned) AEP_BigNum[3] << 8 | AEP_BigNum[2]) << 16 |
1113 ((unsigned) AEP_BigNum[1] << 8 | AEP_BigNum[0]);
1121 #endif /* !OPENSSL_NO_HW_AEP */
1122 #endif /* !OPENSSL_NO_HW */