65 #ifndef OPENSSL_NO_RSA
68 #ifndef OPENSSL_NO_DSA
78 #ifndef OPENSSL_NO_HW_CSWIFT
98 #define CSWIFT_LIB_NAME "cswift engine"
101 #define DECIMAL_SIZE(type) ((sizeof(type)*8+2)/3+1)
103 static int cswift_destroy(
ENGINE *
e);
104 static int cswift_init(
ENGINE *
e);
105 static int cswift_finish(
ENGINE *
e);
106 static int cswift_ctrl(
ENGINE *
e,
int cmd,
long i,
void *
p,
void (*
f)(
void));
107 #ifndef OPENSSL_NO_RSA
114 #ifndef OPENSSL_NO_RSA
120 #ifndef OPENSSL_NO_RSA
128 #ifndef OPENSSL_NO_DSA
130 static DSA_SIG *cswift_dsa_sign(
const unsigned char *dgst,
int dlen,
DSA *dsa);
131 static int cswift_dsa_verify(
const unsigned char *dgst,
int dgst_len,
135 #ifndef OPENSSL_NO_DH
138 static int cswift_mod_exp_dh(
const DH *dh,
BIGNUM *r,
144 static int cswift_rand_bytes(
unsigned char *buf,
int num);
145 static int cswift_rand_status(
void);
148 #define CSWIFT_CMD_SO_PATH ENGINE_CMD_BASE
152 "Specifies the path to the 'cswift' shared library",
157 #ifndef OPENSSL_NO_RSA
161 "CryptoSwift RSA method",
178 #ifndef OPENSSL_NO_DSA
182 "CryptoSwift DSA method",
197 #ifndef OPENSSL_NO_DH
201 "CryptoSwift DH method",
226 static const char *engine_cswift_id =
"cswift";
227 static const char *engine_cswift_name =
"CryptoSwift hardware engine support";
231 static int bind_helper(
ENGINE *e)
233 #ifndef OPENSSL_NO_RSA
236 #ifndef OPENSSL_NO_DH
241 #ifndef OPENSSL_NO_RSA
244 #ifndef OPENSSL_NO_DSA
247 #ifndef OPENSSL_NO_DH
258 #ifndef OPENSSL_NO_RSA
273 #ifndef OPENSSL_NO_DH
281 ERR_load_CSWIFT_strings();
285 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
286 static ENGINE *engine_cswift(
void)
291 if(!bind_helper(ret))
302 ENGINE *toadd = engine_cswift();
315 static DSO *cswift_dso = NULL;
325 static const char *CSWIFT_LIBNAME = NULL;
326 static const char *get_CSWIFT_LIBNAME(
void)
329 return CSWIFT_LIBNAME;
332 static void free_CSWIFT_LIBNAME(
void)
336 CSWIFT_LIBNAME = NULL;
338 static long set_CSWIFT_LIBNAME(
const char *name)
340 free_CSWIFT_LIBNAME();
341 return (((CSWIFT_LIBNAME =
BUF_strdup(name)) != NULL) ? 1 : 0);
343 static const char *CSWIFT_F1 =
"swAcquireAccContext";
344 static const char *CSWIFT_F2 =
"swAttachKeyParam";
345 static const char *CSWIFT_F3 =
"swSimpleRequest";
346 static const char *CSWIFT_F4 =
"swReleaseAccContext";
372 static int cswift_destroy(
ENGINE *e)
374 free_CSWIFT_LIBNAME();
375 ERR_unload_CSWIFT_strings();
380 static int cswift_init(
ENGINE *e)
388 if(cswift_dso != NULL)
394 cswift_dso =
DSO_load(NULL, get_CSWIFT_LIBNAME(), NULL, 0);
395 if(cswift_dso == NULL)
413 p_CSwift_AcquireAccContext = p1;
414 p_CSwift_AttachKeyParam = p2;
415 p_CSwift_SimpleRequest = p3;
416 p_CSwift_ReleaseAccContext = p4;
419 if(!get_context(&hac))
424 release_context(hac);
433 p_CSwift_AcquireAccContext = NULL;
434 p_CSwift_AttachKeyParam = NULL;
435 p_CSwift_SimpleRequest = NULL;
436 p_CSwift_ReleaseAccContext = NULL;
440 static int cswift_finish(
ENGINE *e)
442 free_CSWIFT_LIBNAME();
443 if(cswift_dso == NULL)
454 p_CSwift_AcquireAccContext = NULL;
455 p_CSwift_AttachKeyParam = NULL;
456 p_CSwift_SimpleRequest = NULL;
457 p_CSwift_ReleaseAccContext = NULL;
461 static int cswift_ctrl(
ENGINE *e,
int cmd,
long i,
void *p,
void (*
f)(
void))
463 int initialised = ((cswift_dso == NULL) ? 0 : 1);
477 return set_CSWIFT_LIBNAME((
const char *)p);
502 int to_return, acquired;
504 modulus = exponent = argument = result = NULL;
508 if(!get_context(&hac))
533 (
unsigned char *)modulus->
d);
536 (
unsigned char *)exponent->
d);
539 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
551 sprintf(tmpbuf,
"%ld", sw_status);
558 arg.
value = (
unsigned char *)argument->
d;
560 memset(result->
d, 0, res.
nbytes);
561 res.
value = (
unsigned char *)result->
d;
563 if((sw_status = p_CSwift_SimpleRequest(hac,
SW_CMD_MODEXP, &arg, 1,
568 sprintf(tmpbuf,
"%ld", sw_status);
577 release_context(hac);
583 #ifndef OPENSSL_NO_RSA
590 while( ((out->
nbytes = (numbytes+mod)) % 32) )
601 memset(out->
value, 0, mod);
607 #ifndef OPENSSL_NO_RSA
628 if(!get_context(&hac))
638 if(!result || !argument)
651 if(!cswift_bn_32copy(&sw_param.
up.
crt.
p, p))
656 if(!cswift_bn_32copy(&sw_param.
up.
crt.
q, q))
661 if(!cswift_bn_32copy(&sw_param.
up.
crt.
dmp1, dmp1))
666 if(!cswift_bn_32copy(&sw_param.
up.
crt.
dmq1, dmq1))
671 if(!cswift_bn_32copy(&sw_param.
up.
crt.
iqmp, iqmp))
696 sprintf(tmpbuf,
"%ld", sw_status);
703 arg.
value = (
unsigned char *)argument->
d;
705 memset(result->
d, 0, res.
nbytes);
706 res.
value = (
unsigned char *)result->
d;
713 sprintf(tmpbuf,
"%ld", sw_status);
736 release_context(hac);
741 #ifndef OPENSSL_NO_RSA
764 def_rsa_method=RSA_PKCS1_RSAref();
770 return def_rsa_method->
rsa_mod_exp(r0, I, rsa, ctx);
773 to_return = cswift_mod_exp_crt(r0, I, rsa->
p, rsa->
q, rsa->
dmp1,
794 def_rsa_method=RSA_PKCS1_RSAref();
800 return def_rsa_method->
bn_mod_exp(r, a, p, m, ctx, m_ctx);
803 return cswift_mod_exp(r, a, p, m, ctx);
807 #ifndef OPENSSL_NO_DSA
808 static DSA_SIG *cswift_dsa_sign(
const unsigned char *dgst,
int dlen,
DSA *dsa)
825 if(!get_context(&hac))
854 (
unsigned char *)dsa_p->
d);
857 (
unsigned char *)dsa_q->
d);
860 (
unsigned char *)dsa_g->
d);
863 (
unsigned char *)dsa_key->
d);
866 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
878 sprintf(tmpbuf,
"%ld", sw_status);
885 arg.
value = (
unsigned char *)dgst;
887 memset(result->
d, 0, res.
nbytes);
888 res.
value = (
unsigned char *)result->
d;
892 if(sw_status !=
SW_OK)
896 sprintf(tmpbuf,
"%ld", sw_status);
903 to_return->
r =
BN_bin2bn((
unsigned char *)result->
d, 20, NULL);
904 to_return->
s =
BN_bin2bn((
unsigned char *)result->
d + 20, 20, NULL);
908 release_context(hac);
917 static int cswift_dsa_verify(
const unsigned char *dgst,
int dgst_len,
924 unsigned long sig_result;
936 if(!get_context(&hac))
965 (
unsigned char *)dsa_p->
d);
968 (
unsigned char *)dsa_q->
d);
971 (
unsigned char *)dsa_g->
d);
974 (
unsigned char *)dsa_key->
d);
977 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
989 sprintf(tmpbuf,
"%ld", sw_status);
996 arg[0].
value = (
unsigned char *)dgst;
998 arg[1].
value = (
unsigned char *)argument->
d;
999 memset(arg[1].value, 0, 40);
1003 res.
value = (
unsigned char *)(&sig_result);
1007 if(sw_status !=
SW_OK)
1011 sprintf(tmpbuf,
"%ld", sw_status);
1016 to_return = ((sig_result == 0) ? 0 : 1);
1020 release_context(hac);
1030 #ifndef OPENSSL_NO_DH
1032 static int cswift_mod_exp_dh(
const DH *dh,
BIGNUM *r,
1036 return cswift_mod_exp(r, a, p, m, ctx);
1041 static int cswift_rand_bytes(
unsigned char *buf,
int num)
1048 unsigned char buf32[1024];
1051 if (!get_context(&hac))
1064 while(num >= (
int)
sizeof(buf32))
1066 largenum.
value = buf;
1067 largenum.
nbytes =
sizeof(buf32);
1076 sprintf(tmpbuf,
"%ld", swrc);
1080 buf +=
sizeof(buf32);
1081 num -=
sizeof(buf32);
1085 largenum.
nbytes =
sizeof(buf32);
1086 largenum.
value = buf32;
1092 sprintf(tmpbuf,
"%ld", swrc);
1096 memcpy(buf, largenum.
value, num);
1102 release_context(hac);
1107 static int cswift_rand_status(
void)
1115 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
1116 static int bind_fn(
ENGINE *e,
const char *
id)
1118 if(
id && (strcmp(
id, engine_cswift_id) != 0))