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
79 #ifndef OPENSSL_NO_RSA
82 #ifndef OPENSSL_NO_DSA
90 #ifndef OPENSSL_NO_HW_AEP
97 #define AEP_LIB_NAME "aep engine"
98 #define FAIL_TO_SW 0x10101010
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);
110 static AEP_RV aep_close_all_connections(
int use_engine_lock,
int *in_use);
113 #ifndef OPENSSL_NO_RSA
123 #ifndef OPENSSL_NO_RSA
128 #ifndef OPENSSL_NO_RSA
134 #ifndef OPENSSL_NO_DSA
146 #ifndef OPENSSL_NO_DH
153 static int aep_rand(
unsigned char *buf,
int num);
154 static int aep_rand_status(
void);
160 unsigned char* AEP_BigNum);
161 static AEP_RV ConvertAEPBigNum(
void* ArbBigNum,
AEP_U32 BigNumSize,
162 unsigned char* AEP_BigNum);
165 #define AEP_CMD_SO_PATH ENGINE_CMD_BASE
170 "Specifies the path to the 'aep' shared library",
176 #ifndef OPENSSL_NO_RSA
197 #ifndef OPENSSL_NO_DSA
216 #ifndef OPENSSL_NO_DH
250 static pid_t recorded_pid = 0;
254 static AEP_U32 rand_block_bytes = 0;
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;
266 static int bind_aep(
ENGINE *e)
268 #ifndef OPENSSL_NO_RSA
271 #ifndef OPENSSL_NO_DSA
274 #ifndef OPENSSL_NO_DH
280 #ifndef OPENSSL_NO_RSA
283 #ifndef OPENSSL_NO_DSA
286 #ifndef OPENSSL_NO_DH
299 #ifndef OPENSSL_NO_RSA
315 #ifndef OPENSSL_NO_DSA
328 #ifndef OPENSSL_NO_DH
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))
354 static ENGINE *engine_aep(
void)
370 ENGINE *toadd = engine_aep();
383 static DSO *aep_dso = NULL;
388 static const char *AEP_LIBNAME = NULL;
389 static const char *get_AEP_LIBNAME(
void)
395 static void free_AEP_LIBNAME(
void)
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";
425 static t_AEP_GenRandom *p_AEP_GenRandom = NULL;
432 static int aep_init(
ENGINE *e)
454 aep_dso =
DSO_load(NULL, get_AEP_LIBNAME(), NULL, 0);
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;
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;
533 rv = aep_close_all_connections(0, &in_use);
545 rv = p_AEP_Finalize();
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);
593 return set_AEP_LIBNAME((
const char*)p);
613 if (r_len > max_key_len){
619 rv = aep_get_connection(&hConnection);
627 rv = p_AEP_ModExp(hConnection,(
void*)a, (
void*)p,(
void*)m, (
void*)r,NULL);
632 rv = aep_close_connection(hConnection);
637 rv = aep_return_connection(hConnection);
649 #ifndef OPENSSL_NO_RSA
658 rv = aep_get_connection(&hConnection);
666 rv = p_AEP_ModExpCrt(hConnection,(
void*)a, (
void*)p, (
void*)q, (
void*)dmp1,(
void*)dmq1,
667 (
void*)iqmp,(
void*)r,NULL);
671 rv = aep_close_connection(hConnection);
676 rv = aep_return_connection(hConnection);
690 static int aep_rand(
unsigned char *buf,
int len )
698 if (len <= rand_block_bytes)
700 memcpy(buf, &rand_block[
RAND_BLK_SIZE - rand_block_bytes], len);
701 rand_block_bytes -=
len;
709 rv = aep_get_connection(&hConnection);
718 rv = p_AEP_GenRandom(hConnection, len, 2, buf, NULL);
729 rv = p_AEP_GenRandom(hConnection,
RAND_BLK_SIZE, 2, &rand_block[0], NULL);
739 memcpy(buf, &rand_block[
RAND_BLK_SIZE - rand_block_bytes], len);
740 rand_block_bytes -=
len;
745 rv = aep_return_connection(hConnection);
761 static int aep_rand_status(
void)
767 #ifndef OPENSSL_NO_RSA
782 rv = aep_mod_exp_crt(r0,I,rsa->
p,rsa->
q, rsa->
dmp1,rsa->
dmq1,rsa->
iqmp,ctx);
794 if (!rsa->
d || !rsa->
n)
800 rv = aep_mod_exp(r0,I,rsa->
d,rsa->
n,ctx);
813 #ifndef OPENSSL_NO_DSA
823 if (!aep_mod_exp(rr,a1,p1,m,ctx))
goto end;
825 if (!aep_mod_exp(&t,a2,p2,m,ctx))
goto end;
838 return aep_mod_exp(r, a, p, m, ctx);
842 #ifndef OPENSSL_NO_RSA
847 return aep_mod_exp(r, a, p, m, ctx);
851 #ifndef OPENSSL_NO_DH
853 static int aep_mod_exp_dh(
const DH *dh,
BIGNUM *r,
const BIGNUM *a,
857 return aep_mod_exp(r, a, p, m, ctx);
875 if (recorded_pid != curr_pid)
878 recorded_pid = curr_pid;
885 rv = p_AEP_Initialize(NULL);
895 rv = p_AEP_SetBNCallBacks(&GetBigNumSize, &MakeAEPBigNum,
907 rand_block_bytes = 0;
918 rv = p_AEP_OpenConnection(phConnection);
928 aep_app_conn_table[0].
conn_hndl = *phConnection;
934 if (aep_app_conn_table[count].conn_state ==
Connected)
937 *phConnection = aep_app_conn_table[count].
conn_hndl;
945 if (aep_app_conn_table[count].conn_state ==
NotConnected)
948 rv = p_AEP_OpenConnection(phConnection);
957 aep_app_conn_table[count].
conn_hndl = *phConnection;
977 if (aep_app_conn_table[count].conn_hndl == hConnection)
999 if (aep_app_conn_table[count].conn_hndl == hConnection)
1001 rv = p_AEP_CloseConnection(aep_app_conn_table[count].conn_hndl);
1005 aep_app_conn_table[count].
conn_hndl = 0;
1015 static AEP_RV aep_close_all_connections(
int use_engine_lock,
int *in_use)
1024 switch (aep_app_conn_table[count].conn_state)
1027 rv = p_AEP_CloseConnection(aep_app_conn_table[count].conn_hndl);
1031 aep_app_conn_table[count].
conn_hndl = 0;
1053 bn = (
BIGNUM*) ArbBigNum;
1055 #ifdef SIXTY_FOUR_BIT_LONG
1056 *BigNumSize = bn->
top << 3;
1060 *BigNumSize = bn->
top << 2;
1067 unsigned char* AEP_BigNum)
1071 #ifndef SIXTY_FOUR_BIT_LONG
1077 bn = (
BIGNUM*) ArbBigNum;
1079 #ifdef SIXTY_FOUR_BIT_LONG
1080 memcpy(AEP_BigNum, bn->
d, BigNumSize);
1084 for(i=0;i<bn->
top;i++)
1086 buf = (
unsigned char*)&bn->
d[i];
1089 ((unsigned) buf[1] << 8 | buf[0]) |
1090 ((unsigned) buf[3] << 8 | buf[2]) << 16;
1100 static AEP_RV ConvertAEPBigNum(
void* ArbBigNum,
AEP_U32 BigNumSize,
1101 unsigned char* AEP_BigNum)
1104 #ifndef SIXTY_FOUR_BIT_LONG
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++)
1129 ((
unsigned) AEP_BigNum[3] << 8 | AEP_BigNum[2]) << 16 |
1130 ((
unsigned) AEP_BigNum[1] << 8 | AEP_BigNum[0]);