88 #ifndef OPENSSL_NO_RSA
94 #ifndef OPENSSL_NO_GMP
98 #define E_GMP_LIB_NAME "gmp engine"
101 static int e_gmp_destroy(
ENGINE *
e);
102 static int e_gmp_init(
ENGINE *
e);
103 static int e_gmp_finish(
ENGINE *
e);
104 static int e_gmp_ctrl(
ENGINE *
e,
int cmd,
long i,
void *
p,
void (*
f)(
void));
106 #ifndef OPENSSL_NO_RSA
109 static int e_gmp_rsa_finish(
RSA *r);
118 "Specifies the path to the 'e_gmp' shared library",
124 #ifndef OPENSSL_NO_RSA
148 static const char *engine_e_gmp_id =
"gmp";
149 static const char *engine_e_gmp_name =
"GMP engine support";
153 static int bind_helper(
ENGINE *e)
155 #ifndef OPENSSL_NO_RSA
160 #ifndef OPENSSL_NO_RSA
170 #ifndef OPENSSL_NO_RSA
180 ERR_load_GMP_strings();
184 static ENGINE *engine_gmp(
void)
189 if(!bind_helper(ret))
200 ENGINE *toadd = engine_gmp();
207 #ifndef OPENSSL_NO_RSA
209 static int hndidx_rsa = -1;
212 static int e_gmp_destroy(
ENGINE *e)
214 ERR_unload_GMP_strings();
219 static int e_gmp_init(
ENGINE *e)
221 #ifndef OPENSSL_NO_RSA
222 if (hndidx_rsa == -1)
224 "GMP-based RSA key handle",
227 if (hndidx_rsa == -1)
232 static int e_gmp_finish(
ENGINE *e)
237 static int e_gmp_ctrl(
ENGINE *e,
int cmd,
long i,
void *p,
void (*
f)(
void))
244 case E_GMP_CMD_SO_PATH:
261 static int bn2gmp(
const BIGNUM *bn, mpz_t g)
264 if(((
sizeof(bn->
d[0]) * 8) == GMP_NUMB_BITS) &&
265 (BN_BITS2 == GMP_NUMB_BITS))
268 if(!_mpz_realloc (g, bn->
top))
270 memcpy(&g->_mp_d[0], &bn->
d[0], bn->
top *
sizeof(bn->
d[0]));
271 g->_mp_size = bn->
top;
273 g->_mp_size = -g->_mp_size;
280 if(!tmpchar)
return 0;
281 toret = (mpz_set_str(g, tmpchar, 16) == 0 ? 1 : 0);
287 static int gmp2bn(mpz_t g,
BIGNUM *bn)
289 if(((
sizeof(bn->
d[0]) * 8) == GMP_NUMB_BITS) &&
290 (BN_BITS2 == GMP_NUMB_BITS))
293 int s = (g->_mp_size >= 0) ? g->_mp_size : -g->_mp_size;
298 memcpy(&bn->
d[0], &g->_mp_d[0], s *
sizeof(bn->
d[0]));
300 bn->
neg = g->_mp_size >= 0 ? 0 : 1;
307 if(!tmpchar)
return 0;
308 mpz_get_str(tmpchar, 16, g);
315 #ifndef OPENSSL_NO_RSA
333 if(hptr)
return hptr;
335 if(!hptr)
return NULL;
343 mpz_init(hptr->
dmp1);
344 mpz_init(hptr->
dmq1);
345 mpz_init(hptr->
iqmp);
350 if(!bn2gmp(rsa->
n, hptr->
n) || !bn2gmp(rsa->
e, hptr->
e))
352 if(!rsa->
p || !rsa->
q || !rsa->
d || !rsa->
dmp1 || !rsa->
dmq1 || !rsa->
iqmp)
357 if(!bn2gmp(rsa->
d, hptr->
d) || !bn2gmp(rsa->
p, hptr->
p) ||
358 !bn2gmp(rsa->
q, hptr->
q) || !bn2gmp(rsa->
dmp1, hptr->
dmp1) ||
370 mpz_clear(hptr->
dmp1);
371 mpz_clear(hptr->
dmq1);
372 mpz_clear(hptr->
iqmp);
381 static int e_gmp_rsa_finish(
RSA *rsa)
390 mpz_clear(hptr->
dmp1);
391 mpz_clear(hptr->
dmq1);
392 mpz_clear(hptr->
iqmp);
407 hptr = e_gmp_get_rsa(rsa);
422 if(!bn2gmp(I, hptr->
I0))
430 mpz_mod(hptr->
r1, hptr->
I0, hptr->
q);
431 mpz_powm(hptr->
m1, hptr->
r1, hptr->
dmq1, hptr->
q);
433 mpz_mod(hptr->
r1, hptr->
I0, hptr->
p);
434 mpz_powm(hptr->
r0, hptr->
r1, hptr->
dmp1, hptr->
p);
436 mpz_sub(hptr->
r0, hptr->
r0, hptr->
m1);
438 if(mpz_sgn(hptr->
r0) < 0)
439 mpz_add(hptr->
r0, hptr->
r0, hptr->
p);
440 mpz_mul(hptr->
r1, hptr->
r0, hptr->
iqmp);
441 mpz_mod(hptr->
r0, hptr->
r1, hptr->
p);
443 if(mpz_sgn(hptr->
r0) < 0)
444 mpz_add(hptr->
r0, hptr->
r0, hptr->
p);
445 mpz_mul(hptr->
r1, hptr->
r0, hptr->
q);
446 mpz_add(hptr->
r0, hptr->
r1, hptr->
m1);
449 if(gmp2bn(hptr->
r0, r))
460 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
462 #ifndef OPENSSL_NO_GMP
463 static int bind_fn(
ENGINE *e,
const char *
id)
465 if(
id && (strcmp(
id, engine_e_gmp_id) != 0))