67 #ifndef OPENSSL_NO_HW_IBMCA
72 #include "vendor_defns/ica_openssl_api.h"
75 #define IBMCA_LIB_NAME "ibmca engine"
78 static int ibmca_destroy(
ENGINE *
e);
79 static int ibmca_init(
ENGINE *
e);
80 static int ibmca_finish(
ENGINE *
e);
81 static int ibmca_ctrl(
ENGINE *
e,
int cmd,
long i,
void *
p,
void (*
f)());
83 static const char *IBMCA_F1 =
"icaOpenAdapter";
84 static const char *IBMCA_F2 =
"icaCloseAdapter";
85 static const char *IBMCA_F3 =
"icaRsaModExpo";
86 static const char *IBMCA_F4 =
"icaRandomNumberGenerate";
87 static const char *IBMCA_F5 =
"icaRsaCrt";
99 #ifndef OPENSSL_NO_RSA
108 #ifndef OPENSSL_NO_DSA
118 #ifndef OPENSSL_NO_DH
121 static int ibmca_mod_exp_dh(
const DH *dh,
BIGNUM *r,
127 static int ibmca_rand_bytes(
unsigned char *buf,
int num);
128 static int ibmca_rand_status(
void);
134 #define IBMCA_CMD_SO_PATH ENGINE_CMD_BASE
138 "Specifies the path to the 'atasi' shared library",
143 #ifndef OPENSSL_NO_RSA
163 #ifndef OPENSSL_NO_DSA
180 #ifndef OPENSSL_NO_DH
207 static const char *engine_ibmca_id =
"ibmca";
208 static const char *engine_ibmca_name =
"Ibmca hardware engine support";
212 static int bind_helper(
ENGINE *e)
214 #ifndef OPENSSL_NO_RSA
217 #ifndef OPENSSL_NO_DSA
220 #ifndef OPENSSL_NO_DH
225 #ifndef OPENSSL_NO_RSA
228 #ifndef OPENSSL_NO_DSA
231 #ifndef OPENSSL_NO_DH
242 #ifndef OPENSSL_NO_RSA
257 #ifndef OPENSSL_NO_DSA
266 #ifndef OPENSSL_NO_DH
274 ERR_load_IBMCA_strings();
278 static ENGINE *engine_ibmca(
void)
283 if(!bind_helper(ret))
291 #ifdef ENGINE_DYNAMIC_SUPPORT
297 ENGINE *toadd = engine_ibmca();
305 static int ibmca_destroy(
ENGINE *e)
310 ERR_unload_IBMCA_strings();
321 static DSO *ibmca_dso = NULL;
326 static unsigned int (ICA_CALL *p_icaOpenAdapter)();
327 static unsigned int (ICA_CALL *p_icaCloseAdapter)();
328 static unsigned int (ICA_CALL *p_icaRsaModExpo)();
329 static unsigned int (ICA_CALL *p_icaRandomNumberGenerate)();
330 static unsigned int (ICA_CALL *p_icaRsaCrt)();
335 unsigned int status=0;
337 status = p_icaOpenAdapter(0, p_handle);
346 p_icaCloseAdapter(handle);
350 static int ibmca_init(
ENGINE *e)
359 if(ibmca_dso != NULL)
374 ibmca_dso =
DSO_load(NULL, IBMCA_LIBNAME, NULL,
376 if(ibmca_dso == NULL)
383 ibmca_dso, IBMCA_F1)) ||
385 ibmca_dso, IBMCA_F2)) ||
387 ibmca_dso, IBMCA_F3)) ||
389 ibmca_dso, IBMCA_F4)) ||
391 ibmca_dso, IBMCA_F5)))
399 p_icaOpenAdapter = (
unsigned int (ICA_CALL *)())p1;
400 p_icaCloseAdapter = (
unsigned int (ICA_CALL *)())p2;
401 p_icaRsaModExpo = (
unsigned int (ICA_CALL *)())p3;
402 p_icaRandomNumberGenerate = (
unsigned int (ICA_CALL *)())p4;
403 p_icaRsaCrt = (
unsigned int (ICA_CALL *)())p5;
405 if(!get_context(&handle))
416 p_icaOpenAdapter = NULL;
417 p_icaCloseAdapter = NULL;
418 p_icaRsaModExpo = NULL;
419 p_icaRandomNumberGenerate = NULL;
424 static int ibmca_finish(
ENGINE *e)
426 if(ibmca_dso == NULL)
431 release_context(handle);
442 static int ibmca_ctrl(
ENGINE *e,
int cmd,
long i,
void *p,
void (*
f)())
444 int initialised = ((ibmca_dso == NULL) ? 0 : 1);
458 IBMCA_LIBNAME = (
const char *)p;
481 int inLen, outLen, tmpLen;
500 if( !argument || !result || !key)
571 memset(argument->
d, 0, outLen);
572 BN_bn2bin(a, (
unsigned char *)argument->
d + outLen -
579 if( (rc = p_icaRsaModExpo(handle, inLen,(
unsigned char *)argument->
d,
580 publKey, &outLen, (
unsigned char *)result->
d))
584 printf(
"rc = %d\n", rc);
591 BN_bin2bn((
unsigned char *)result->
d, outLen, r);
598 #ifndef OPENSSL_NO_RSA
608 if(!rsa->
d || !rsa->
n)
614 to_return = ibmca_mod_exp(r0, I, rsa->
d, rsa->
n, ctx);
618 to_return = ibmca_mod_exp_crt(r0, I, rsa->
p, rsa->
q, rsa->
dmp1,
645 unsigned int offset, pSize, qSize;
647 unsigned int keyRecordSize;
661 if(!argument || !result || !key)
681 keyRecordSize = pbytes+qbytes+dmp1bytes+dmq1bytes+iqmpbytes;
682 if ( keyRecordSize >
sizeof(privKey->
keyRecord )) {
687 if ( (qbytes + dmq1bytes) > 256 ){
692 if ( pbytes + dmp1bytes > 256 ) {
727 if ( (pbytes > pSize) || (dmp1bytes > pSize) ||
728 (iqmpbytes > pSize) || ( qbytes >qSize) ||
729 (dmq1bytes > qSize) ) {
754 if ( pSize < pbytes ) {
793 BN_bn2bin(a, (
unsigned char *)argument->
d + outLen -
797 memset(result->
d, 0, outLen);
801 if ( (rc = p_icaRsaCrt(handle, inLen, (
unsigned char *)argument->
d,
802 privKey, &outLen, (
unsigned char *)result->
d)) != 0)
804 printf(
"rc = %d\n", rc);
811 BN_bin2bn((
unsigned char *)result->
d, outLen, r);
820 #ifndef OPENSSL_NO_DSA
837 if (!ibmca_mod_exp(rr,a1,p1,m,ctx))
goto end;
839 if (!ibmca_mod_exp(&t,a2,p2,m,ctx))
goto end;
853 return ibmca_mod_exp(r, a, p, m, ctx);
861 return ibmca_mod_exp(r, a, p, m, ctx);
864 #ifndef OPENSSL_NO_DH
866 static int ibmca_mod_exp_dh(
DH const *dh,
BIGNUM *r,
870 return ibmca_mod_exp(r, a, p, m, ctx);
875 static int ibmca_rand_bytes(
unsigned char *buf,
int num)
887 ret = p_icaRandomNumberGenerate(handle, num, buf);
898 static int ibmca_rand_status(
void)
905 #ifdef ENGINE_DYNAMIC_SUPPORT
906 static int bind_fn(
ENGINE *e,
const char *
id)
908 if(
id && (strcmp(
id, engine_ibmca_id) != 0))