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 #ifndef OPENSSL_NO_RSA
73 #include <openssl/rsa.h>
75 #ifndef OPENSSL_NO_DSA
76 #include <openssl/dsa.h>
79 #include <openssl/dh.h>
81 #include <openssl/bn.h>
84 #ifndef OPENSSL_NO_HW_AEP
88 #include "vendor_defns/aep.h"
91 #define AEP_LIB_NAME "aep engine"
92 #define FAIL_TO_SW 0x10101010
94 #include "e_aep_err.c"
96 static int aep_init(ENGINE
*e
);
97 static int aep_finish(ENGINE
*e
);
98 static int aep_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
)(void));
99 static int aep_destroy(ENGINE
*e
);
101 static AEP_RV
aep_get_connection(AEP_CONNECTION_HNDL_PTR hConnection
);
102 static AEP_RV
aep_return_connection(AEP_CONNECTION_HNDL hConnection
);
103 static AEP_RV
aep_close_connection(AEP_CONNECTION_HNDL hConnection
);
104 static AEP_RV
aep_close_all_connections(int use_engine_lock
, int *in_use
);
107 #ifndef OPENSSL_NO_RSA
108 static int aep_mod_exp(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
109 const BIGNUM
*m
, BN_CTX
*ctx
);
111 static AEP_RV
aep_mod_exp_crt(BIGNUM
*r
,const BIGNUM
*a
, const BIGNUM
*p
,
112 const BIGNUM
*q
, const BIGNUM
*dmp1
,const BIGNUM
*dmq1
,
113 const BIGNUM
*iqmp
, BN_CTX
*ctx
);
117 #ifndef OPENSSL_NO_RSA
118 static int aep_rsa_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
, BN_CTX
*ctx
);
121 /* This function is aliased to mod_exp (with the mont stuff dropped). */
122 #ifndef OPENSSL_NO_RSA
123 static int aep_mod_exp_mont(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
124 const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
);
128 #ifndef OPENSSL_NO_DSA
129 static int aep_dsa_mod_exp(DSA
*dsa
, BIGNUM
*rr
, BIGNUM
*a1
,
130 BIGNUM
*p1
, BIGNUM
*a2
, BIGNUM
*p2
, BIGNUM
*m
,
131 BN_CTX
*ctx
, BN_MONT_CTX
*in_mont
);
133 static int aep_mod_exp_dsa(DSA
*dsa
, BIGNUM
*r
, BIGNUM
*a
,
134 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
139 /* This function is aliased to mod_exp (with the DH and mont dropped). */
140 #ifndef OPENSSL_NO_DH
141 static int aep_mod_exp_dh(const DH
*dh
, BIGNUM
*r
, const BIGNUM
*a
,
142 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
);
147 static int aep_rand(unsigned char *buf
, int num
);
148 static int aep_rand_status(void);
151 /* Bignum conversion stuff */
152 static AEP_RV
GetBigNumSize(AEP_VOID_PTR ArbBigNum
, AEP_U32
* BigNumSize
);
153 static AEP_RV
MakeAEPBigNum(AEP_VOID_PTR ArbBigNum
, AEP_U32 BigNumSize
,
154 unsigned char* AEP_BigNum
);
155 static AEP_RV
ConvertAEPBigNum(void* ArbBigNum
, AEP_U32 BigNumSize
,
156 unsigned char* AEP_BigNum
);
158 /* The definitions for control commands specific to this engine */
159 #define AEP_CMD_SO_PATH ENGINE_CMD_BASE
160 static const ENGINE_CMD_DEFN aep_cmd_defns
[] =
164 "Specifies the path to the 'aep' shared library",
165 ENGINE_CMD_FLAG_STRING
170 #ifndef OPENSSL_NO_RSA
171 /* Our internal RSA_METHOD that we provide pointers to */
172 static RSA_METHOD aep_rsa
=
175 NULL
, /*rsa_pub_encrypt*/
176 NULL
, /*rsa_pub_decrypt*/
177 NULL
, /*rsa_priv_encrypt*/
178 NULL
, /*rsa_priv_encrypt*/
179 aep_rsa_mod_exp
, /*rsa_mod_exp*/
180 aep_mod_exp_mont
, /*bn_mod_exp*/
191 #ifndef OPENSSL_NO_DSA
192 /* Our internal DSA_METHOD that we provide pointers to */
193 static DSA_METHOD aep_dsa
=
196 NULL
, /* dsa_do_sign */
197 NULL
, /* dsa_sign_setup */
198 NULL
, /* dsa_do_verify */
199 aep_dsa_mod_exp
, /* dsa_mod_exp */
200 aep_mod_exp_dsa
, /* bn_mod_exp */
205 NULL
, /* dsa_paramgen */
206 NULL
/* dsa_keygen */
210 #ifndef OPENSSL_NO_DH
211 /* Our internal DH_METHOD that we provide pointers to */
212 static DH_METHOD aep_dh
=
227 /* our internal RAND_method that we provide pointers to */
228 static RAND_METHOD aep_random
=
230 /*"AEP RAND method", */
240 /*Define an array of structures to hold connections*/
241 static AEP_CONNECTION_ENTRY aep_app_conn_table
[MAX_PROCESS_CONNECTIONS
];
243 /*Used to determine if this is a new process*/
244 static pid_t recorded_pid
= 0;
247 static AEP_U8 rand_block
[RAND_BLK_SIZE
];
248 static AEP_U32 rand_block_bytes
= 0;
251 /* Constants used when creating the ENGINE */
252 static const char *engine_aep_id
= "aep";
253 static const char *engine_aep_name
= "Aep hardware engine support";
255 static int max_key_len
= 2176;
258 /* This internal function is used by ENGINE_aep() and possibly by the
259 * "dynamic" ENGINE support too */
260 static int bind_aep(ENGINE
*e
)
262 #ifndef OPENSSL_NO_RSA
263 const RSA_METHOD
*meth1
;
265 #ifndef OPENSSL_NO_DSA
266 const DSA_METHOD
*meth2
;
268 #ifndef OPENSSL_NO_DH
269 const DH_METHOD
*meth3
;
272 if(!ENGINE_set_id(e
, engine_aep_id
) ||
273 !ENGINE_set_name(e
, engine_aep_name
) ||
274 #ifndef OPENSSL_NO_RSA
275 !ENGINE_set_RSA(e
, &aep_rsa
) ||
277 #ifndef OPENSSL_NO_DSA
278 !ENGINE_set_DSA(e
, &aep_dsa
) ||
280 #ifndef OPENSSL_NO_DH
281 !ENGINE_set_DH(e
, &aep_dh
) ||
284 !ENGINE_set_RAND(e
, &aep_random
) ||
286 !ENGINE_set_init_function(e
, aep_init
) ||
287 !ENGINE_set_destroy_function(e
, aep_destroy
) ||
288 !ENGINE_set_finish_function(e
, aep_finish
) ||
289 !ENGINE_set_ctrl_function(e
, aep_ctrl
) ||
290 !ENGINE_set_cmd_defns(e
, aep_cmd_defns
))
293 #ifndef OPENSSL_NO_RSA
294 /* We know that the "PKCS1_SSLeay()" functions hook properly
295 * to the aep-specific mod_exp and mod_exp_crt so we use
296 * those functions. NB: We don't use ENGINE_openssl() or
297 * anything "more generic" because something like the RSAref
298 * code may not hook properly, and if you own one of these
299 * cards then you have the right to do RSA operations on it
301 meth1
= RSA_PKCS1_SSLeay();
302 aep_rsa
.rsa_pub_enc
= meth1
->rsa_pub_enc
;
303 aep_rsa
.rsa_pub_dec
= meth1
->rsa_pub_dec
;
304 aep_rsa
.rsa_priv_enc
= meth1
->rsa_priv_enc
;
305 aep_rsa
.rsa_priv_dec
= meth1
->rsa_priv_dec
;
309 #ifndef OPENSSL_NO_DSA
310 /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
312 meth2
= DSA_OpenSSL();
313 aep_dsa
.dsa_do_sign
= meth2
->dsa_do_sign
;
314 aep_dsa
.dsa_sign_setup
= meth2
->dsa_sign_setup
;
315 aep_dsa
.dsa_do_verify
= meth2
->dsa_do_verify
;
317 aep_dsa
= *DSA_get_default_method();
318 aep_dsa
.dsa_mod_exp
= aep_dsa_mod_exp
;
319 aep_dsa
.bn_mod_exp
= aep_mod_exp_dsa
;
322 #ifndef OPENSSL_NO_DH
323 /* Much the same for Diffie-Hellman */
324 meth3
= DH_OpenSSL();
325 aep_dh
.generate_key
= meth3
->generate_key
;
326 aep_dh
.compute_key
= meth3
->compute_key
;
327 aep_dh
.bn_mod_exp
= meth3
->bn_mod_exp
;
330 /* Ensure the aep error handling is set up */
331 ERR_load_AEPHK_strings();
336 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
337 static int bind_helper(ENGINE
*e
, const char *id
)
339 if(id
&& (strcmp(id
, engine_aep_id
) != 0))
345 IMPLEMENT_DYNAMIC_CHECK_FN()
346 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper
)
348 static ENGINE
*engine_aep(void)
350 ENGINE
*ret
= ENGINE_new();
361 void ENGINE_load_aep(void)
363 /* Copied from eng_[openssl|dyn].c */
364 ENGINE
*toadd
= engine_aep();
372 /* This is a process-global DSO handle used for loading and unloading
373 * the Aep library. NB: This is only set (or unset) during an
374 * init() or finish() call (reference counts permitting) and they're
375 * operating with global locks, so this should be thread-safe
377 static DSO
*aep_dso
= NULL
;
379 /* These are the static string constants for the DSO file name and the function
380 * symbol names to bind to.
382 static const char *AEP_LIBNAME
= NULL
;
383 static const char *get_AEP_LIBNAME(void)
389 static void free_AEP_LIBNAME(void)
392 OPENSSL_free((void*)AEP_LIBNAME
);
395 static long set_AEP_LIBNAME(const char *name
)
398 return ((AEP_LIBNAME
= BUF_strdup(name
)) != NULL
? 1 : 0);
401 static const char *AEP_F1
= "AEP_ModExp";
402 static const char *AEP_F2
= "AEP_ModExpCrt";
404 static const char *AEP_F3
= "AEP_GenRandom";
406 static const char *AEP_F4
= "AEP_Finalize";
407 static const char *AEP_F5
= "AEP_Initialize";
408 static const char *AEP_F6
= "AEP_OpenConnection";
409 static const char *AEP_F7
= "AEP_SetBNCallBacks";
410 static const char *AEP_F8
= "AEP_CloseConnection";
412 /* These are the function pointers that are (un)set when the library has
413 * successfully (un)loaded. */
414 static t_AEP_OpenConnection
*p_AEP_OpenConnection
= NULL
;
415 static t_AEP_CloseConnection
*p_AEP_CloseConnection
= NULL
;
416 static t_AEP_ModExp
*p_AEP_ModExp
= NULL
;
417 static t_AEP_ModExpCrt
*p_AEP_ModExpCrt
= NULL
;
419 static t_AEP_GenRandom
*p_AEP_GenRandom
= NULL
;
421 static t_AEP_Initialize
*p_AEP_Initialize
= NULL
;
422 static t_AEP_Finalize
*p_AEP_Finalize
= NULL
;
423 static t_AEP_SetBNCallBacks
*p_AEP_SetBNCallBacks
= NULL
;
425 /* (de)initialisation functions. */
426 static int aep_init(ENGINE
*e
)
434 t_AEP_Initialize
*p5
;
435 t_AEP_OpenConnection
*p6
;
436 t_AEP_SetBNCallBacks
*p7
;
437 t_AEP_CloseConnection
*p8
;
443 AEPHKerr(AEPHK_F_AEP_INIT
,AEPHK_R_ALREADY_LOADED
);
446 /* Attempt to load libaep.so. */
448 aep_dso
= DSO_load(NULL
, get_AEP_LIBNAME(), NULL
, 0);
452 AEPHKerr(AEPHK_F_AEP_INIT
,AEPHK_R_NOT_LOADED
);
456 if( !(p1
= (t_AEP_ModExp
*) DSO_bind_func( aep_dso
,AEP_F1
)) ||
457 !(p2
= (t_AEP_ModExpCrt
*) DSO_bind_func( aep_dso
,AEP_F2
)) ||
459 !(p3
= (t_AEP_GenRandom
*) DSO_bind_func( aep_dso
,AEP_F3
)) ||
461 !(p4
= (t_AEP_Finalize
*) DSO_bind_func( aep_dso
,AEP_F4
)) ||
462 !(p5
= (t_AEP_Initialize
*) DSO_bind_func( aep_dso
,AEP_F5
)) ||
463 !(p6
= (t_AEP_OpenConnection
*) DSO_bind_func( aep_dso
,AEP_F6
)) ||
464 !(p7
= (t_AEP_SetBNCallBacks
*) DSO_bind_func( aep_dso
,AEP_F7
)) ||
465 !(p8
= (t_AEP_CloseConnection
*) DSO_bind_func( aep_dso
,AEP_F8
)))
467 AEPHKerr(AEPHK_F_AEP_INIT
,AEPHK_R_NOT_LOADED
);
471 /* Copy the pointers */
474 p_AEP_ModExpCrt
= p2
;
476 p_AEP_GenRandom
= p3
;
479 p_AEP_Initialize
= p5
;
480 p_AEP_OpenConnection
= p6
;
481 p_AEP_SetBNCallBacks
= p7
;
482 p_AEP_CloseConnection
= p8
;
494 p_AEP_OpenConnection
= NULL
;
496 p_AEP_ModExpCrt
= NULL
;
498 p_AEP_GenRandom
= NULL
;
500 p_AEP_Initialize
= NULL
;
501 p_AEP_Finalize
= NULL
;
502 p_AEP_SetBNCallBacks
= NULL
;
503 p_AEP_CloseConnection
= NULL
;
508 /* Destructor (complements the "ENGINE_aep()" constructor) */
509 static int aep_destroy(ENGINE
*e
)
512 ERR_unload_AEPHK_strings();
516 static int aep_finish(ENGINE
*e
)
518 int to_return
= 0, in_use
;
523 AEPHKerr(AEPHK_F_AEP_FINISH
,AEPHK_R_NOT_LOADED
);
527 rv
= aep_close_all_connections(0, &in_use
);
530 AEPHKerr(AEPHK_F_AEP_FINISH
,AEPHK_R_CLOSE_HANDLES_FAILED
);
535 AEPHKerr(AEPHK_F_AEP_FINISH
,AEPHK_R_CONNECTIONS_IN_USE
);
539 rv
= p_AEP_Finalize();
542 AEPHKerr(AEPHK_F_AEP_FINISH
,AEPHK_R_FINALIZE_FAILED
);
546 if(!DSO_free(aep_dso
))
548 AEPHKerr(AEPHK_F_AEP_FINISH
,AEPHK_R_UNIT_FAILURE
);
553 p_AEP_CloseConnection
= NULL
;
554 p_AEP_OpenConnection
= NULL
;
556 p_AEP_ModExpCrt
= NULL
;
558 p_AEP_GenRandom
= NULL
;
560 p_AEP_Initialize
= NULL
;
561 p_AEP_Finalize
= NULL
;
562 p_AEP_SetBNCallBacks
= NULL
;
569 static int aep_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
)(void))
571 int initialised
= ((aep_dso
== NULL
) ? 0 : 1);
574 case AEP_CMD_SO_PATH
:
577 AEPHKerr(AEPHK_F_AEP_CTRL
,
578 ERR_R_PASSED_NULL_PARAMETER
);
583 AEPHKerr(AEPHK_F_AEP_CTRL
,
584 AEPHK_R_ALREADY_LOADED
);
587 return set_AEP_LIBNAME((const char*)p
);
591 AEPHKerr(AEPHK_F_AEP_CTRL
,AEPHK_R_CTRL_COMMAND_NOT_IMPLEMENTED
);
595 static int aep_mod_exp(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
596 const BIGNUM
*m
, BN_CTX
*ctx
)
600 AEP_CONNECTION_HNDL hConnection
;
603 r_len
= BN_num_bits(m
);
605 /* Perform in software if modulus is too large for hardware. */
607 if (r_len
> max_key_len
){
608 AEPHKerr(AEPHK_F_AEP_MOD_EXP
, AEPHK_R_SIZE_TOO_LARGE_OR_TOO_SMALL
);
609 return BN_mod_exp(r
, a
, p
, m
, ctx
);
612 /*Grab a connection from the pool*/
613 rv
= aep_get_connection(&hConnection
);
616 AEPHKerr(AEPHK_F_AEP_MOD_EXP
,AEPHK_R_GET_HANDLE_FAILED
);
617 return BN_mod_exp(r
, a
, p
, m
, ctx
);
620 /*To the card with the mod exp*/
621 rv
= p_AEP_ModExp(hConnection
,(void*)a
, (void*)p
,(void*)m
, (void*)r
,NULL
);
625 AEPHKerr(AEPHK_F_AEP_MOD_EXP
,AEPHK_R_MOD_EXP_FAILED
);
626 rv
= aep_close_connection(hConnection
);
627 return BN_mod_exp(r
, a
, p
, m
, ctx
);
630 /*Return the connection to the pool*/
631 rv
= aep_return_connection(hConnection
);
634 AEPHKerr(AEPHK_F_AEP_MOD_EXP
,AEPHK_R_RETURN_CONNECTION_FAILED
);
643 #ifndef OPENSSL_NO_RSA
644 static AEP_RV
aep_mod_exp_crt(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
645 const BIGNUM
*q
, const BIGNUM
*dmp1
,
646 const BIGNUM
*dmq1
,const BIGNUM
*iqmp
, BN_CTX
*ctx
)
648 AEP_RV rv
= AEP_R_OK
;
649 AEP_CONNECTION_HNDL hConnection
;
651 /*Grab a connection from the pool*/
652 rv
= aep_get_connection(&hConnection
);
655 AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT
,AEPHK_R_GET_HANDLE_FAILED
);
659 /*To the card with the mod exp*/
660 rv
= p_AEP_ModExpCrt(hConnection
,(void*)a
, (void*)p
, (void*)q
, (void*)dmp1
,(void*)dmq1
,
661 (void*)iqmp
,(void*)r
,NULL
);
664 AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT
,AEPHK_R_MOD_EXP_CRT_FAILED
);
665 rv
= aep_close_connection(hConnection
);
669 /*Return the connection to the pool*/
670 rv
= aep_return_connection(hConnection
);
673 AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT
,AEPHK_R_RETURN_CONNECTION_FAILED
);
684 static int aep_rand(unsigned char *buf
,int len
)
686 AEP_RV rv
= AEP_R_OK
;
687 AEP_CONNECTION_HNDL hConnection
;
689 CRYPTO_w_lock(CRYPTO_LOCK_RAND
);
691 /*Can the request be serviced with what's already in the buffer?*/
692 if (len
<= rand_block_bytes
)
694 memcpy(buf
, &rand_block
[RAND_BLK_SIZE
- rand_block_bytes
], len
);
695 rand_block_bytes
-= len
;
696 CRYPTO_w_unlock(CRYPTO_LOCK_RAND
);
699 /*If not the get another block of random bytes*/
701 CRYPTO_w_unlock(CRYPTO_LOCK_RAND
);
703 rv
= aep_get_connection(&hConnection
);
706 AEPHKerr(AEPHK_F_AEP_RAND
,AEPHK_R_GET_HANDLE_FAILED
);
710 if (len
> RAND_BLK_SIZE
)
712 rv
= p_AEP_GenRandom(hConnection
, len
, 2, buf
, NULL
);
715 AEPHKerr(AEPHK_F_AEP_RAND
,AEPHK_R_GET_RANDOM_FAILED
);
721 CRYPTO_w_lock(CRYPTO_LOCK_RAND
);
723 rv
= p_AEP_GenRandom(hConnection
, RAND_BLK_SIZE
, 2, &rand_block
[0], NULL
);
726 AEPHKerr(AEPHK_F_AEP_RAND
,AEPHK_R_GET_RANDOM_FAILED
);
731 rand_block_bytes
= RAND_BLK_SIZE
;
733 memcpy(buf
, &rand_block
[RAND_BLK_SIZE
- rand_block_bytes
], len
);
734 rand_block_bytes
-= len
;
736 CRYPTO_w_unlock(CRYPTO_LOCK_RAND
);
739 rv
= aep_return_connection(hConnection
);
742 AEPHKerr(AEPHK_F_AEP_RAND
,AEPHK_R_RETURN_CONNECTION_FAILED
);
750 CRYPTO_w_unlock(CRYPTO_LOCK_RAND
);
755 static int aep_rand_status(void)
761 #ifndef OPENSSL_NO_RSA
762 static int aep_rsa_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
, BN_CTX
*ctx
)
765 AEP_RV rv
= AEP_R_OK
;
769 AEPHKerr(AEPHK_F_AEP_RSA_MOD_EXP
,AEPHK_R_NOT_LOADED
);
773 /*See if we have all the necessary bits for a crt*/
774 if (rsa
->q
&& rsa
->dmp1
&& rsa
->dmq1
&& rsa
->iqmp
)
776 rv
= aep_mod_exp_crt(r0
,I
,rsa
->p
,rsa
->q
, rsa
->dmp1
,rsa
->dmq1
,rsa
->iqmp
,ctx
);
778 if (rv
== FAIL_TO_SW
){
779 const RSA_METHOD
*meth
= RSA_PKCS1_SSLeay();
780 to_return
= (*meth
->rsa_mod_exp
)(r0
, I
, rsa
, ctx
);
783 else if (rv
!= AEP_R_OK
)
788 if (!rsa
->d
|| !rsa
->n
)
790 AEPHKerr(AEPHK_F_AEP_RSA_MOD_EXP
,AEPHK_R_MISSING_KEY_COMPONENTS
);
794 rv
= aep_mod_exp(r0
,I
,rsa
->d
,rsa
->n
,ctx
);
807 #ifndef OPENSSL_NO_DSA
808 static int aep_dsa_mod_exp(DSA
*dsa
, BIGNUM
*rr
, BIGNUM
*a1
,
809 BIGNUM
*p1
, BIGNUM
*a2
, BIGNUM
*p2
, BIGNUM
*m
,
810 BN_CTX
*ctx
, BN_MONT_CTX
*in_mont
)
816 /* let rr = a1 ^ p1 mod m */
817 if (!aep_mod_exp(rr
,a1
,p1
,m
,ctx
)) goto end
;
818 /* let t = a2 ^ p2 mod m */
819 if (!aep_mod_exp(&t
,a2
,p2
,m
,ctx
)) goto end
;
820 /* let rr = rr * t mod m */
821 if (!BN_mod_mul(rr
,rr
,&t
,m
,ctx
)) goto end
;
828 static int aep_mod_exp_dsa(DSA
*dsa
, BIGNUM
*r
, BIGNUM
*a
,
829 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
832 return aep_mod_exp(r
, a
, p
, m
, ctx
);
836 #ifndef OPENSSL_NO_RSA
837 /* This function is aliased to mod_exp (with the mont stuff dropped). */
838 static int aep_mod_exp_mont(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
839 const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
)
841 return aep_mod_exp(r
, a
, p
, m
, ctx
);
845 #ifndef OPENSSL_NO_DH
846 /* This function is aliased to mod_exp (with the dh and mont dropped). */
847 static int aep_mod_exp_dh(const DH
*dh
, BIGNUM
*r
, const BIGNUM
*a
,
848 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
851 return aep_mod_exp(r
, a
, p
, m
, ctx
);
855 static AEP_RV
aep_get_connection(AEP_CONNECTION_HNDL_PTR phConnection
)
858 AEP_RV rv
= AEP_R_OK
;
860 /*Get the current process id*/
863 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE
);
868 curr_pid
= GetThreadID();
871 /*Check if this is the first time this is being called from the current
873 if (recorded_pid
!= curr_pid
)
875 /*Remember our pid so we can check if we're in a new process*/
876 recorded_pid
= curr_pid
;
878 /*Call Finalize to make sure we have not inherited some data
879 from a parent process*/
882 /*Initialise the AEP API*/
883 rv
= p_AEP_Initialize(NULL
);
887 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION
,AEPHK_R_INIT_FAILURE
);
892 /*Set the AEP big num call back functions*/
893 rv
= p_AEP_SetBNCallBacks(&GetBigNumSize
, &MakeAEPBigNum
,
898 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION
,AEPHK_R_SETBNCALLBACK_FAILURE
);
904 /*Reset the rand byte count*/
905 rand_block_bytes
= 0;
908 /*Init the structures*/
909 for (count
= 0;count
< MAX_PROCESS_CONNECTIONS
;count
++)
911 aep_app_conn_table
[count
].conn_state
= NotConnected
;
912 aep_app_conn_table
[count
].conn_hndl
= 0;
915 /*Open a connection*/
916 rv
= p_AEP_OpenConnection(phConnection
);
920 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION
,AEPHK_R_UNIT_FAILURE
);
925 aep_app_conn_table
[0].conn_state
= InUse
;
926 aep_app_conn_table
[0].conn_hndl
= *phConnection
;
929 /*Check the existing connections to see if we can find a free one*/
930 for (count
= 0;count
< MAX_PROCESS_CONNECTIONS
;count
++)
932 if (aep_app_conn_table
[count
].conn_state
== Connected
)
934 aep_app_conn_table
[count
].conn_state
= InUse
;
935 *phConnection
= aep_app_conn_table
[count
].conn_hndl
;
939 /*If no connections available, we're going to have to try
941 for (count
= 0;count
< MAX_PROCESS_CONNECTIONS
;count
++)
943 if (aep_app_conn_table
[count
].conn_state
== NotConnected
)
945 /*Open a connection*/
946 rv
= p_AEP_OpenConnection(phConnection
);
950 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION
,AEPHK_R_UNIT_FAILURE
);
954 aep_app_conn_table
[count
].conn_state
= InUse
;
955 aep_app_conn_table
[count
].conn_hndl
= *phConnection
;
959 rv
= AEP_R_GENERAL_ERROR
;
961 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE
);
966 static AEP_RV
aep_return_connection(AEP_CONNECTION_HNDL hConnection
)
970 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE
);
972 /*Find the connection item that matches this connection handle*/
973 for(count
= 0;count
< MAX_PROCESS_CONNECTIONS
;count
++)
975 if (aep_app_conn_table
[count
].conn_hndl
== hConnection
)
977 aep_app_conn_table
[count
].conn_state
= Connected
;
982 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE
);
987 static AEP_RV
aep_close_connection(AEP_CONNECTION_HNDL hConnection
)
990 AEP_RV rv
= AEP_R_OK
;
992 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE
);
994 /*Find the connection item that matches this connection handle*/
995 for(count
= 0;count
< MAX_PROCESS_CONNECTIONS
;count
++)
997 if (aep_app_conn_table
[count
].conn_hndl
== hConnection
)
999 rv
= p_AEP_CloseConnection(aep_app_conn_table
[count
].conn_hndl
);
1002 aep_app_conn_table
[count
].conn_state
= NotConnected
;
1003 aep_app_conn_table
[count
].conn_hndl
= 0;
1009 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE
);
1013 static AEP_RV
aep_close_all_connections(int use_engine_lock
, int *in_use
)
1016 AEP_RV rv
= AEP_R_OK
;
1019 if (use_engine_lock
) CRYPTO_w_lock(CRYPTO_LOCK_ENGINE
);
1020 for (count
= 0;count
< MAX_PROCESS_CONNECTIONS
;count
++)
1022 switch (aep_app_conn_table
[count
].conn_state
)
1025 rv
= p_AEP_CloseConnection(aep_app_conn_table
[count
].conn_hndl
);
1028 aep_app_conn_table
[count
].conn_state
= NotConnected
;
1029 aep_app_conn_table
[count
].conn_hndl
= 0;
1039 if (use_engine_lock
) CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE
);
1043 /*BigNum call back functions, used to convert OpenSSL bignums into AEP bignums.
1044 Note only 32bit Openssl build support*/
1046 static AEP_RV
GetBigNumSize(AEP_VOID_PTR ArbBigNum
, AEP_U32
* BigNumSize
)
1050 /*Cast the ArbBigNum pointer to our BIGNUM struct*/
1051 bn
= (BIGNUM
*) ArbBigNum
;
1053 #ifdef SIXTY_FOUR_BIT_LONG
1054 *BigNumSize
= bn
->top
<< 3;
1056 /*Size of the bignum in bytes is equal to the bn->top (no of 32 bit
1057 words) multiplies by 4*/
1058 *BigNumSize
= bn
->top
<< 2;
1064 static AEP_RV
MakeAEPBigNum(AEP_VOID_PTR ArbBigNum
, AEP_U32 BigNumSize
,
1065 unsigned char* AEP_BigNum
)
1069 #ifndef SIXTY_FOUR_BIT_LONG
1074 /*Cast the ArbBigNum pointer to our BIGNUM struct*/
1075 bn
= (BIGNUM
*) ArbBigNum
;
1077 #ifdef SIXTY_FOUR_BIT_LONG
1078 memcpy(AEP_BigNum
, bn
->d
, BigNumSize
);
1080 /*Must copy data into a (monotone) least significant byte first format
1081 performing endian conversion if necessary*/
1082 for(i
=0;i
<bn
->top
;i
++)
1084 buf
= (unsigned char*)&bn
->d
[i
];
1086 *((AEP_U32
*)AEP_BigNum
) = (AEP_U32
)
1087 ((unsigned) buf
[1] << 8 | buf
[0]) |
1088 ((unsigned) buf
[3] << 8 | buf
[2]) << 16;
1097 /*Turn an AEP Big Num back to a user big num*/
1098 static AEP_RV
ConvertAEPBigNum(void* ArbBigNum
, AEP_U32 BigNumSize
,
1099 unsigned char* AEP_BigNum
)
1102 #ifndef SIXTY_FOUR_BIT_LONG
1106 bn
= (BIGNUM
*)ArbBigNum
;
1108 /*Expand the result bn so that it can hold our big num.
1110 bn_expand(bn
, (int)(BigNumSize
<< 3));
1112 #ifdef SIXTY_FOUR_BIT_LONG
1113 bn
->top
= BigNumSize
>> 3;
1115 if((BigNumSize
& 7) != 0)
1118 memset(bn
->d
, 0, bn
->top
<< 3);
1120 memcpy(bn
->d
, AEP_BigNum
, BigNumSize
);
1122 bn
->top
= BigNumSize
>> 2;
1124 for(i
=0;i
<bn
->top
;i
++)
1126 bn
->d
[i
] = (AEP_U32
)
1127 ((unsigned) AEP_BigNum
[3] << 8 | AEP_BigNum
[2]) << 16 |
1128 ((unsigned) AEP_BigNum
[1] << 8 | AEP_BigNum
[0]);
1136 #endif /* !OPENSSL_NO_HW_AEP */
1137 #endif /* !OPENSSL_NO_HW */