From c93be52c9ef59801704a80bc39ddcfe3bd4277ed Mon Sep 17 00:00:00 2001 From: David Benjamin Date: Fri, 15 Mar 2019 13:46:04 -0500 Subject: [PATCH] Save a temporary in BN_mod_exp_mont's w=1 case. BN_mod_exp_mont is most commonly used in RSA verification, where the exponent sizes are small enough to use 1-bit "windows". There's no need to allocate the extra BIGNUM. Change-Id: I14fb523dfae7d77d2cec10a0209f09f22031d1af Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/35327 Reviewed-by: Adam Langley --- crypto/fipsmodule/bn/exponentiation.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crypto/fipsmodule/bn/exponentiation.c b/crypto/fipsmodule/bn/exponentiation.c index 9e408113..8d4a5c8b 100644 --- a/crypto/fipsmodule/bn/exponentiation.c +++ b/crypto/fipsmodule/bn/exponentiation.c @@ -614,10 +614,9 @@ int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, BN_MONT_CTX *new_mont = NULL; BN_CTX_start(ctx); - BIGNUM *d = BN_CTX_get(ctx); BIGNUM *r = BN_CTX_get(ctx); val[0] = BN_CTX_get(ctx); - if (!d || !r || !val[0]) { + if (r == NULL || val[0] == NULL) { goto err; } @@ -639,7 +638,9 @@ int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, goto err; } if (window > 1) { - if (!BN_mod_mul_montgomery(d, val[0], val[0], mont, ctx)) { + BIGNUM *d = BN_CTX_get(ctx); + if (d == NULL || + !BN_mod_mul_montgomery(d, val[0], val[0], mont, ctx)) { goto err; } for (int i = 1; i < 1 << (window - 1); i++) {