diff options
Diffstat (limited to 'src/lib/libcrypto/rsa/rsa_eay.c')
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_eay.c | 97 |
1 files changed, 82 insertions, 15 deletions
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; |
