size_t a bunch of bn words bits.
Also replace a pointless call to bn_mul_words with a memset.
Change-Id: Ief30ddab0e84864561b73fe2776bd0477931cf7f
Reviewed-on: https://boringssl-review.googlesource.com/23066
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/fipsmodule/bn/asm/x86_64-gcc.c b/crypto/fipsmodule/bn/asm/x86_64-gcc.c
index bcf12eb..d8434f7 100644
--- a/crypto/fipsmodule/bn/asm/x86_64-gcc.c
+++ b/crypto/fipsmodule/bn/asm/x86_64-gcc.c
@@ -93,11 +93,11 @@
#undef sqr
#define sqr(r0, r1, a) __asm__("mulq %2" : "=a"(r0), "=d"(r1) : "a"(a) : "cc");
-BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,
+BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, size_t num,
BN_ULONG w) {
BN_ULONG c1 = 0;
- if (num <= 0) {
+ if (num == 0) {
return (c1);
}
@@ -126,10 +126,11 @@
return c1;
}
-BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w) {
+BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, size_t num,
+ BN_ULONG w) {
BN_ULONG c1 = 0;
- if (num <= 0) {
+ if (num == 0) {
return c1;
}
@@ -156,8 +157,8 @@
return c1;
}
-void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n) {
- if (n <= 0) {
+void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, size_t n) {
+ if (n == 0) {
return;
}
@@ -184,11 +185,11 @@
}
BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
- int n) {
+ size_t n) {
BN_ULONG ret;
size_t i = 0;
- if (n <= 0) {
+ if (n == 0) {
return 0;
}
@@ -211,11 +212,11 @@
}
BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
- int n) {
+ size_t n) {
BN_ULONG ret;
size_t i = 0;
- if (n <= 0) {
+ if (n == 0) {
return 0;
}
diff --git a/crypto/fipsmodule/bn/generic.c b/crypto/fipsmodule/bn/generic.c
index 44e0f2c..e81eec3 100644
--- a/crypto/fipsmodule/bn/generic.c
+++ b/crypto/fipsmodule/bn/generic.c
@@ -124,12 +124,11 @@
#endif // !BN_ULLONG
-BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,
+BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, size_t num,
BN_ULONG w) {
BN_ULONG c1 = 0;
- assert(num >= 0);
- if (num <= 0) {
+ if (num == 0) {
return c1;
}
@@ -153,11 +152,11 @@
return c1;
}
-BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w) {
+BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, size_t num,
+ BN_ULONG w) {
BN_ULONG c1 = 0;
- assert(num >= 0);
- if (num <= 0) {
+ if (num == 0) {
return c1;
}
@@ -179,9 +178,8 @@
return c1;
}
-void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n) {
- assert(n >= 0);
- if (n <= 0) {
+void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, size_t n) {
+ if (n == 0) {
return;
}
@@ -204,11 +202,10 @@
#ifdef BN_ULLONG
BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
- int n) {
+ size_t n) {
BN_ULLONG ll = 0;
- assert(n >= 0);
- if (n <= 0) {
+ if (n == 0) {
return 0;
}
@@ -245,11 +242,10 @@
#else // !BN_ULLONG
BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
- int n) {
+ size_t n) {
BN_ULONG c, l, t;
- assert(n >= 0);
- if (n <= 0) {
+ if (n == 0) {
return (BN_ULONG)0;
}
@@ -302,12 +298,11 @@
#endif // !BN_ULLONG
BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
- int n) {
+ size_t n) {
BN_ULONG t1, t2;
int c = 0;
- assert(n >= 0);
- if (n <= 0) {
+ if (n == 0) {
return (BN_ULONG)0;
}
diff --git a/crypto/fipsmodule/bn/internal.h b/crypto/fipsmodule/bn/internal.h
index 57cf755..b5ddd7e 100644
--- a/crypto/fipsmodule/bn/internal.h
+++ b/crypto/fipsmodule/bn/internal.h
@@ -214,34 +214,34 @@
// the result in |rp|. |ap| and |rp| must both be |num| words long. It returns
// the carry word of the operation. |ap| and |rp| may be equal but otherwise may
// not alias.
-BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,
+BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, size_t num,
BN_ULONG w);
// bn_mul_words multiples |ap| by |w| and places the result in |rp|. |ap| and
// |rp| must both be |num| words long. It returns the carry word of the
// operation. |ap| and |rp| may be equal but otherwise may not alias.
-BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
+BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, size_t num, BN_ULONG w);
// bn_sqr_words sets |rp[2*i]| and |rp[2*i+1]| to |ap[i]|'s square, for all |i|
// up to |num|. |ap| is an array of |num| words and |rp| an array of |2*num|
// words. |ap| and |rp| may not alias.
//
// This gives the contribution of the |ap[i]*ap[i]| terms when squaring |ap|.
-void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num);
+void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, size_t num);
// bn_add_words adds |ap| to |bp| and places the result in |rp|, each of which
// are |num| words long. It returns the carry bit, which is one if the operation
// overflowed and zero otherwise. Any pair of |ap|, |bp|, and |rp| may be equal
// to each other but otherwise may not alias.
BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
- int num);
+ size_t num);
// bn_sub_words subtracts |bp| from |ap| and places the result in |rp|. It
// returns the borrow bit, which is one if the computation underflowed and zero
// otherwise. Any pair of |ap|, |bp|, and |rp| may be equal to each other but
// otherwise may not alias.
BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
- int num);
+ size_t num);
// bn_mul_comba4 sets |r| to the product of |a| and |b|.
void bn_mul_comba4(BN_ULONG r[8], BN_ULONG a[4], BN_ULONG b[4]);
diff --git a/crypto/fipsmodule/bn/mul.c b/crypto/fipsmodule/bn/mul.c
index 65f3c2b..800a0bb 100644
--- a/crypto/fipsmodule/bn/mul.c
+++ b/crypto/fipsmodule/bn/mul.c
@@ -60,49 +60,44 @@
#include <string.h>
#include "internal.h"
+#include "../../internal.h"
#define BN_MUL_RECURSIVE_SIZE_NORMAL 16
#define BN_SQR_RECURSIVE_SIZE_NORMAL BN_MUL_RECURSIVE_SIZE_NORMAL
-static void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b,
- int nb) {
- BN_ULONG *rr;
-
+static void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, size_t na, BN_ULONG *b,
+ size_t nb) {
if (na < nb) {
- int itmp;
- BN_ULONG *ltmp;
-
- itmp = na;
+ size_t itmp = na;
na = nb;
nb = itmp;
- ltmp = a;
+ BN_ULONG *ltmp = a;
a = b;
b = ltmp;
}
- rr = &(r[na]);
- if (nb <= 0) {
- (void)bn_mul_words(r, a, na, 0);
+ BN_ULONG *rr = &(r[na]);
+ if (nb == 0) {
+ OPENSSL_memset(r, 0, na * sizeof(BN_ULONG));
return;
- } else {
- rr[0] = bn_mul_words(r, a, na, b[0]);
}
+ rr[0] = bn_mul_words(r, a, na, b[0]);
for (;;) {
- if (--nb <= 0) {
+ if (--nb == 0) {
return;
}
rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);
- if (--nb <= 0) {
+ if (--nb == 0) {
return;
}
rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);
- if (--nb <= 0) {
+ if (--nb == 0) {
return;
}
rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);
- if (--nb <= 0) {
+ if (--nb == 0) {
return;
}
rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);
@@ -659,27 +654,30 @@
}
// tmp must have 2*n words
-static void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n,
+static void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, size_t n,
BN_ULONG *tmp) {
- int max = n * 2;
+ if (n == 0) {
+ return;
+ }
+
+ size_t max = n * 2;
const BN_ULONG *ap = a;
BN_ULONG *rp = r;
rp[0] = rp[max - 1] = 0;
rp++;
- int j = n;
// Compute the contribution of a[i] * a[j] for all i < j.
- if (--j > 0) {
+ if (n > 1) {
ap++;
- rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
+ rp[n - 1] = bn_mul_words(rp, ap, n - 1, ap[-1]);
rp += 2;
}
-
- for (int i = n - 2; i > 0; i--) {
- j--;
- ap++;
- rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);
- rp += 2;
+ if (n > 2) {
+ for (size_t i = n - 2; i > 0; i--) {
+ ap++;
+ rp[i] = bn_mul_add_words(rp, ap, i, ap[-1]);
+ rp += 2;
+ }
}
// The final result fits in |max| words, so none of the following operations