diff options
Diffstat (limited to 'src/lib/libcrypto/rsa')
-rw-r--r-- | src/lib/libcrypto/rsa/Makefile.ssl | 3 | ||||
-rw-r--r-- | src/lib/libcrypto/rsa/rsa_eay.c | 97 | ||||
-rw-r--r-- | src/lib/libcrypto/rsa/rsa_lib.c | 10 |
3 files changed, 89 insertions, 21 deletions
diff --git a/src/lib/libcrypto/rsa/Makefile.ssl b/src/lib/libcrypto/rsa/Makefile.ssl index c159eedafe..2bee181d4e 100644 --- a/src/lib/libcrypto/rsa/Makefile.ssl +++ b/src/lib/libcrypto/rsa/Makefile.ssl | |||
@@ -41,7 +41,8 @@ all: lib | |||
41 | 41 | ||
42 | lib: $(LIBOBJ) | 42 | lib: $(LIBOBJ) |
43 | $(AR) $(LIB) $(LIBOBJ) | 43 | $(AR) $(LIB) $(LIBOBJ) |
44 | $(RANLIB) $(LIB) | 44 | @echo You may get an error following this line. Please ignore. |
45 | - $(RANLIB) $(LIB) | ||
45 | @touch lib | 46 | @touch lib |
46 | 47 | ||
47 | files: | 48 | files: |
diff --git a/src/lib/libcrypto/rsa/rsa_eay.c b/src/lib/libcrypto/rsa/rsa_eay.c index ccaa62b239..cde5ca27d5 100644 --- a/src/lib/libcrypto/rsa/rsa_eay.c +++ b/src/lib/libcrypto/rsa/rsa_eay.c | |||
@@ -141,9 +141,26 @@ static int RSA_eay_public_encrypt(int flen, unsigned char *from, | |||
141 | 141 | ||
142 | if ((rsa->_method_mod_n == NULL) && (rsa->flags & RSA_FLAG_CACHE_PUBLIC)) | 142 | if ((rsa->_method_mod_n == NULL) && (rsa->flags & RSA_FLAG_CACHE_PUBLIC)) |
143 | { | 143 | { |
144 | if ((rsa->_method_mod_n=BN_MONT_CTX_new()) != NULL) | 144 | BN_MONT_CTX* bn_mont_ctx; |
145 | if (!BN_MONT_CTX_set(rsa->_method_mod_n,rsa->n,ctx)) | 145 | if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL) |
146 | goto err; | 146 | goto err; |
147 | if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->n,ctx)) | ||
148 | { | ||
149 | BN_MONT_CTX_free(bn_mont_ctx); | ||
150 | goto err; | ||
151 | } | ||
152 | if (rsa->_method_mod_n == NULL) /* other thread may have finished first */ | ||
153 | { | ||
154 | CRYPTO_w_lock(CRYPTO_LOCK_RSA); | ||
155 | if (rsa->_method_mod_n == NULL) | ||
156 | { | ||
157 | rsa->_method_mod_n = bn_mont_ctx; | ||
158 | bn_mont_ctx = NULL; | ||
159 | } | ||
160 | CRYPTO_w_unlock(CRYPTO_LOCK_RSA); | ||
161 | } | ||
162 | if (bn_mont_ctx) | ||
163 | BN_MONT_CTX_free(bn_mont_ctx); | ||
147 | } | 164 | } |
148 | 165 | ||
149 | if (!meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx, | 166 | if (!meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx, |
@@ -378,9 +395,26 @@ static int RSA_eay_public_decrypt(int flen, unsigned char *from, | |||
378 | /* do the decrypt */ | 395 | /* do the decrypt */ |
379 | if ((rsa->_method_mod_n == NULL) && (rsa->flags & RSA_FLAG_CACHE_PUBLIC)) | 396 | if ((rsa->_method_mod_n == NULL) && (rsa->flags & RSA_FLAG_CACHE_PUBLIC)) |
380 | { | 397 | { |
381 | if ((rsa->_method_mod_n=BN_MONT_CTX_new()) != NULL) | 398 | BN_MONT_CTX* bn_mont_ctx; |
382 | if (!BN_MONT_CTX_set(rsa->_method_mod_n,rsa->n,ctx)) | 399 | if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL) |
383 | goto err; | 400 | goto err; |
401 | if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->n,ctx)) | ||
402 | { | ||
403 | BN_MONT_CTX_free(bn_mont_ctx); | ||
404 | goto err; | ||
405 | } | ||
406 | if (rsa->_method_mod_n == NULL) /* other thread may have finished first */ | ||
407 | { | ||
408 | CRYPTO_w_lock(CRYPTO_LOCK_RSA); | ||
409 | if (rsa->_method_mod_n == NULL) | ||
410 | { | ||
411 | rsa->_method_mod_n = bn_mont_ctx; | ||
412 | bn_mont_ctx = NULL; | ||
413 | } | ||
414 | CRYPTO_w_unlock(CRYPTO_LOCK_RSA); | ||
415 | } | ||
416 | if (bn_mont_ctx) | ||
417 | BN_MONT_CTX_free(bn_mont_ctx); | ||
384 | } | 418 | } |
385 | 419 | ||
386 | if (!meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx, | 420 | if (!meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx, |
@@ -433,20 +467,53 @@ static int RSA_eay_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa) | |||
433 | { | 467 | { |
434 | if (rsa->_method_mod_p == NULL) | 468 | if (rsa->_method_mod_p == NULL) |
435 | { | 469 | { |
436 | if ((rsa->_method_mod_p=BN_MONT_CTX_new()) != NULL) | 470 | BN_MONT_CTX* bn_mont_ctx; |
437 | if (!BN_MONT_CTX_set(rsa->_method_mod_p,rsa->p, | 471 | if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL) |
438 | ctx)) | 472 | goto err; |
439 | goto err; | 473 | if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->p,ctx)) |
474 | { | ||
475 | BN_MONT_CTX_free(bn_mont_ctx); | ||
476 | goto err; | ||
477 | } | ||
478 | if (rsa->_method_mod_p == NULL) /* other thread may have finished first */ | ||
479 | { | ||
480 | CRYPTO_w_lock(CRYPTO_LOCK_RSA); | ||
481 | if (rsa->_method_mod_p == NULL) | ||
482 | { | ||
483 | rsa->_method_mod_p = bn_mont_ctx; | ||
484 | bn_mont_ctx = NULL; | ||
485 | } | ||
486 | CRYPTO_w_unlock(CRYPTO_LOCK_RSA); | ||
487 | } | ||
488 | if (bn_mont_ctx) | ||
489 | BN_MONT_CTX_free(bn_mont_ctx); | ||
440 | } | 490 | } |
491 | |||
441 | if (rsa->_method_mod_q == NULL) | 492 | if (rsa->_method_mod_q == NULL) |
442 | { | 493 | { |
443 | if ((rsa->_method_mod_q=BN_MONT_CTX_new()) != NULL) | 494 | BN_MONT_CTX* bn_mont_ctx; |
444 | if (!BN_MONT_CTX_set(rsa->_method_mod_q,rsa->q, | 495 | if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL) |
445 | ctx)) | 496 | goto err; |
446 | goto err; | 497 | if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->q,ctx)) |
498 | { | ||
499 | BN_MONT_CTX_free(bn_mont_ctx); | ||
500 | goto err; | ||
501 | } | ||
502 | if (rsa->_method_mod_q == NULL) /* other thread may have finished first */ | ||
503 | { | ||
504 | CRYPTO_w_lock(CRYPTO_LOCK_RSA); | ||
505 | if (rsa->_method_mod_q == NULL) | ||
506 | { | ||
507 | rsa->_method_mod_q = bn_mont_ctx; | ||
508 | bn_mont_ctx = NULL; | ||
509 | } | ||
510 | CRYPTO_w_unlock(CRYPTO_LOCK_RSA); | ||
511 | } | ||
512 | if (bn_mont_ctx) | ||
513 | BN_MONT_CTX_free(bn_mont_ctx); | ||
447 | } | 514 | } |
448 | } | 515 | } |
449 | 516 | ||
450 | if (!BN_mod(&r1,I,rsa->q,ctx)) goto err; | 517 | if (!BN_mod(&r1,I,rsa->q,ctx)) goto err; |
451 | if (!meth->bn_mod_exp(&m1,&r1,rsa->dmq1,rsa->q,ctx, | 518 | if (!meth->bn_mod_exp(&m1,&r1,rsa->dmq1,rsa->q,ctx, |
452 | rsa->_method_mod_q)) goto err; | 519 | rsa->_method_mod_q)) goto err; |
diff --git a/src/lib/libcrypto/rsa/rsa_lib.c b/src/lib/libcrypto/rsa/rsa_lib.c index 5e1e8fcdf3..94395cc22c 100644 --- a/src/lib/libcrypto/rsa/rsa_lib.c +++ b/src/lib/libcrypto/rsa/rsa_lib.c | |||
@@ -191,13 +191,13 @@ RSA *RSA_new_method(ENGINE *engine) | |||
191 | ret->blinding=NULL; | 191 | ret->blinding=NULL; |
192 | ret->bignum_data=NULL; | 192 | ret->bignum_data=NULL; |
193 | ret->flags=meth->flags; | 193 | ret->flags=meth->flags; |
194 | CRYPTO_new_ex_data(rsa_meth,ret,&ret->ex_data); | ||
194 | if ((meth->init != NULL) && !meth->init(ret)) | 195 | if ((meth->init != NULL) && !meth->init(ret)) |
195 | { | 196 | { |
197 | CRYPTO_free_ex_data(rsa_meth,ret,&ret->ex_data); | ||
196 | OPENSSL_free(ret); | 198 | OPENSSL_free(ret); |
197 | ret=NULL; | 199 | ret=NULL; |
198 | } | 200 | } |
199 | else | ||
200 | CRYPTO_new_ex_data(rsa_meth,ret,&ret->ex_data); | ||
201 | return(ret); | 201 | return(ret); |
202 | } | 202 | } |
203 | 203 | ||
@@ -221,13 +221,13 @@ void RSA_free(RSA *r) | |||
221 | } | 221 | } |
222 | #endif | 222 | #endif |
223 | 223 | ||
224 | CRYPTO_free_ex_data(rsa_meth,r,&r->ex_data); | ||
225 | |||
226 | meth = ENGINE_get_RSA(r->engine); | 224 | meth = ENGINE_get_RSA(r->engine); |
227 | if (meth->finish != NULL) | 225 | if (meth->finish != NULL) |
228 | meth->finish(r); | 226 | meth->finish(r); |
229 | ENGINE_finish(r->engine); | 227 | ENGINE_finish(r->engine); |
230 | 228 | ||
229 | CRYPTO_free_ex_data(rsa_meth,r,&r->ex_data); | ||
230 | |||
231 | if (r->n != NULL) BN_clear_free(r->n); | 231 | if (r->n != NULL) BN_clear_free(r->n); |
232 | if (r->e != NULL) BN_clear_free(r->e); | 232 | if (r->e != NULL) BN_clear_free(r->e); |
233 | if (r->d != NULL) BN_clear_free(r->d); | 233 | if (r->d != NULL) BN_clear_free(r->d); |
@@ -325,7 +325,7 @@ int RSA_blinding_on(RSA *rsa, BN_CTX *p_ctx) | |||
325 | 325 | ||
326 | BN_CTX_start(ctx); | 326 | BN_CTX_start(ctx); |
327 | A = BN_CTX_get(ctx); | 327 | A = BN_CTX_get(ctx); |
328 | if (!BN_rand(A,BN_num_bits(rsa->n)-1,1,0)) goto err; | 328 | if (!BN_rand_range(A,rsa->n)) goto err; |
329 | if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err; | 329 | if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err; |
330 | 330 | ||
331 | if (!ENGINE_get_RSA(rsa->engine)->bn_mod_exp(A,A, | 331 | if (!ENGINE_get_RSA(rsa->engine)->bn_mod_exp(A,A, |