Make BN_mod_exp_mont_consttime take a const context.
BN_mod_exp_mont_consttime does not modify its |BN_MONT_CTX| so that
value should be const.
Cherry-picked to make the next diff apply better.
(cherry picked from commit efb42fbb607c11391a08d5787bcffd08b9a0c78f)
Bug: 33752052
Change-Id: I6222a5a9eba3e34bf95957312af048b30da14a6b
diff --git a/src/crypto/bn/exponentiation.c b/src/crypto/bn/exponentiation.c
index d3063c9..916a909 100644
--- a/src/crypto/bn/exponentiation.c
+++ b/src/crypto/bn/exponentiation.c
@@ -851,10 +851,10 @@
*/
int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
const BIGNUM *m, BN_CTX *ctx,
- BN_MONT_CTX *in_mont) {
+ const BN_MONT_CTX *mont) {
int i, bits, ret = 0, window, wvalue;
int top;
- BN_MONT_CTX *mont = NULL;
+ BN_MONT_CTX *new_mont = NULL;
int numPowers;
unsigned char *powerbufFree = NULL;
@@ -877,15 +877,13 @@
BN_CTX_start(ctx);
- /* Allocate a montgomery context if it was not supplied by the caller.
- * If this is not done, things will break in the montgomery part. */
- if (in_mont != NULL) {
- mont = in_mont;
- } else {
- mont = BN_MONT_CTX_new();
- if (mont == NULL || !BN_MONT_CTX_set(mont, m, ctx)) {
+ /* Allocate a montgomery context if it was not supplied by the caller. */
+ if (mont == NULL) {
+ new_mont = BN_MONT_CTX_new();
+ if (new_mont == NULL || !BN_MONT_CTX_set(new_mont, m, ctx)) {
goto err;
}
+ mont = new_mont;
}
#ifdef RSAZ_ENABLED
@@ -1008,7 +1006,7 @@
const BN_ULONG * not_used, const BN_ULONG * np,
const BN_ULONG * n0, int num);
- BN_ULONG *np = mont->N.d, *n0 = mont->n0, *np2;
+ const BN_ULONG *np = mont->N.d, *n0 = mont->n0, *np2;
/* BN_to_montgomery can contaminate words above .top
* [in BN_DEBUG[_DEBUG] build]... */
@@ -1022,9 +1020,11 @@
if (top & 7) {
np2 = np;
} else {
- for (np2 = am.d + top, i = 0; i < top; i++) {
- np2[2 * i] = np[i];
+ BN_ULONG *np_double = am.d + top;
+ for (i = 0; i < top; i++) {
+ np_double[2 * i] = np[i];
}
+ np2 = np_double;
}
bn_scatter5(tmp.d, top, powerbuf, 0);
@@ -1189,10 +1189,9 @@
goto err;
}
ret = 1;
+
err:
- if (in_mont == NULL) {
- BN_MONT_CTX_free(mont);
- }
+ BN_MONT_CTX_free(new_mont);
if (powerbuf != NULL) {
OPENSSL_cleanse(powerbuf, powerbufLen);
OPENSSL_free(powerbufFree);
diff --git a/src/include/openssl/bn.h b/src/include/openssl/bn.h
index ec1c8ff..297ce5a 100644
--- a/src/include/openssl/bn.h
+++ b/src/include/openssl/bn.h
@@ -771,7 +771,8 @@
OPENSSL_EXPORT int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a,
const BIGNUM *p, const BIGNUM *m,
- BN_CTX *ctx, BN_MONT_CTX *in_mont);
+ BN_CTX *ctx,
+ const BN_MONT_CTX *mont);
OPENSSL_EXPORT int BN_mod_exp_mont_word(BIGNUM *r, BN_ULONG a, const BIGNUM *p,
const BIGNUM *m, BN_CTX *ctx,