summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Nethercote <nnethercote@mozilla.com>2015-01-26 20:22:38 -0800
committerNicholas Nethercote <nnethercote@mozilla.com>2015-01-26 20:22:38 -0800
commit256ffe5e30e8401c4648c75834e88fbb55606968 (patch)
treeb8a68f0f2fa6645f61745935471ada0bebaefb09
parent2dd953845e31647310622d8ebde64a4f4774dbd9 (diff)
downloadnss-hg-256ffe5e30e8401c4648c75834e88fbb55606968.tar.gz
Bug 1096741 (part 1) - In mp_exptmod_safe_i(), instantiate |powersArray| lazily, r=rrelyea, r=kaie
-rw-r--r--lib/freebl/mpi/mpmontg.c25
1 files changed, 13 insertions, 12 deletions
diff --git a/lib/freebl/mpi/mpmontg.c b/lib/freebl/mpi/mpmontg.c
index 388e20c68..d619360aa 100644
--- a/lib/freebl/mpi/mpmontg.c
+++ b/lib/freebl/mpi/mpmontg.c
@@ -883,8 +883,8 @@ mp_err mp_exptmod_safe_i(const mp_int * montBase,
int expOff;
mp_int accum1, accum2, accum[WEAVE_WORD_SIZE];
mp_int tmp;
- unsigned char *powersArray;
- unsigned char *powers;
+ unsigned char *powersArray = NULL;
+ unsigned char *powers = NULL;
MP_DIGITS(&accum1) = 0;
MP_DIGITS(&accum2) = 0;
@@ -894,15 +894,6 @@ mp_err mp_exptmod_safe_i(const mp_int * montBase,
MP_DIGITS(&accum[3]) = 0;
MP_DIGITS(&tmp) = 0;
- powersArray = (unsigned char *)malloc(num_powers*(nLen*sizeof(mp_digit)+1));
- if (powersArray == NULL) {
- res = MP_MEM;
- goto CLEANUP;
- }
-
- /* powers[i] = base ** (i); */
- powers = (unsigned char *)MP_ALIGN(powersArray,num_powers);
-
/* grab the first window value. This allows us to preload accumulator1
* and save a conversion, some squares and a multiple*/
MP_CHECKOK( mpl_get_bits(exponent,
@@ -924,6 +915,13 @@ mp_err mp_exptmod_safe_i(const mp_int * montBase,
MP_CHECKOK( mp_copy(montBase, &accum[1]) );
SQR(montBase, &accum[2]);
MUL_NOWEAVE(montBase, &accum[2], &accum[3]);
+ powersArray = (unsigned char *)malloc(num_powers*(nLen*sizeof(mp_digit)+1));
+ if (!powersArray) {
+ res = MP_MEM;
+ goto CLEANUP;
+ }
+ /* powers[i] = base ** (i); */ \
+ powers = (unsigned char *)MP_ALIGN(powersArray,num_powers); \
MP_CHECKOK( mpi_to_weave(accum, powers, nLen, num_powers) );
if (first_window < 4) {
MP_CHECKOK( mp_copy(&accum[first_window], &accum1) );
@@ -945,7 +943,10 @@ mp_err mp_exptmod_safe_i(const mp_int * montBase,
* odd powers where k is the window size in the two other mp_modexpt
* implementations in this file. We will get some of that
* back by not needing the first 'k' squares and one multiply for the
- * first window */
+ * first window.
+ * Given the value of 4 for WEAVE_WORD_SIZE, this loop will only execute if
+ * num_powers > 2, in which case powers will have been allocated.
+ */
for (i = WEAVE_WORD_SIZE; i < num_powers; i++) {
int acc_index = i & (WEAVE_WORD_SIZE-1); /* i % WEAVE_WORD_SIZE */
if ( i & 1 ) {