59
59
/* This engine is not (currently) compiled in by default. Do enable it,
60
* reconfigure OpenSSL with "-DOPENSSL_USE_GMP -lgmp". The GMP libraries and
60
* reconfigure OpenSSL with "enable-gmp -lgmp". The GMP libraries and
61
61
* headers must reside in one of the paths searched by the compiler/linker,
62
62
* otherwise paths must be specified - eg. try configuring with
63
* "-DOPENSSL_USE_GMP -I<includepath> -L<libpath> -lgmp". YMMV. */
63
* "enable-gmp -I<includepath> -L<libpath> -lgmp". YMMV. */
65
65
/* As for what this does - it's a largely unoptimised implementation of an
66
66
* ENGINE that uses the GMP library to perform RSA private key operations. To
251
253
return to_return;
254
/* HACK - use text I/O functions in openssl and GMP to handle conversions. This
257
/* Most often limb sizes will be the same. If not, we use hex conversion
258
* which is neat, but extremely inefficient. */
256
259
static int bn2gmp(const BIGNUM *bn, mpz_t g)
259
char *tmpchar = BN_bn2hex(bn);
260
if(!tmpchar) return 0;
261
toret = (mpz_set_str(g, tmpchar, 16) == 0 ? 1 : 0);
262
OPENSSL_free(tmpchar);
262
if(((sizeof(bn->d[0]) * 8) == GMP_NUMB_BITS) &&
263
(BN_BITS2 == GMP_NUMB_BITS))
265
/* The common case */
266
if(!_mpz_realloc (g, bn->top))
268
memcpy(&g->_mp_d[0], &bn->d[0], bn->top * sizeof(bn->d[0]));
269
g->_mp_size = bn->top;
271
g->_mp_size = -g->_mp_size;
277
char *tmpchar = BN_bn2hex(bn);
278
if(!tmpchar) return 0;
279
toret = (mpz_set_str(g, tmpchar, 16) == 0 ? 1 : 0);
280
OPENSSL_free(tmpchar);
266
285
static int gmp2bn(mpz_t g, BIGNUM *bn)
269
char *tmpchar = OPENSSL_malloc(mpz_sizeinbase(g, 16) + 10);
270
if(!tmpchar) return 0;
271
mpz_get_str(tmpchar, 16, g);
272
toret = BN_hex2bn(&bn, tmpchar);
273
OPENSSL_free(tmpchar);
287
if(((sizeof(bn->d[0]) * 8) == GMP_NUMB_BITS) &&
288
(BN_BITS2 == GMP_NUMB_BITS))
290
/* The common case */
291
int s = (g->_mp_size >= 0) ? g->_mp_size : -g->_mp_size;
293
if(bn_expand2 (bn, s) == NULL)
296
memcpy(&bn->d[0], &g->_mp_d[0], s * sizeof(bn->d[0]));
298
bn->neg = g->_mp_size >= 0 ? 0 : 1;
304
char *tmpchar = OPENSSL_malloc(mpz_sizeinbase(g, 16) + 10);
305
if(!tmpchar) return 0;
306
mpz_get_str(tmpchar, 16, g);
307
toret = BN_hex2bn(&bn, tmpchar);
308
OPENSSL_free(tmpchar);
277
313
#ifndef OPENSSL_NO_RSA
454
#endif /* !OPENSSL_NO_GMP */
418
456
/* This stuff is needed if this ENGINE is being compiled into a self-contained
419
457
* shared-library. */
420
#ifdef ENGINE_DYNAMIC_SUPPORT
458
#ifndef OPENSSL_NO_DYNAMIC_ENGINE
459
IMPLEMENT_DYNAMIC_CHECK_FN()
460
#ifndef OPENSSL_NO_GMP
421
461
static int bind_fn(ENGINE *e, const char *id)
423
463
if(id && (strcmp(id, engine_e_gmp_id) != 0))
429
IMPLEMENT_DYNAMIC_CHECK_FN()
430
469
IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
431
#endif /* ENGINE_DYNAMIC_SUPPORT */
472
int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns) { return 0; }
474
#endif /* OPENSSL_NO_DYNAMIC_ENGINE */
433
#endif /* !OPENSSL_NO_HW_GMP */
434
476
#endif /* !OPENSSL_NO_HW */