Clear no-op BN_MASK2 masks.
This is an OpenSSL thing to support platforms where BN_ULONG is not
actually the size it claims to be. We define BN_ULONG to uint32_t and
uint64_t which are guaranteed by C to implement arithemetic modulo 2^32
and 2^64, respectively. Thus there is no need for any of this.
Change-Id: I098cd4cc050a136b9f2c091dfbc28dd83e01f531
Reviewed-on: https://boringssl-review.googlesource.com/21784
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
diff --git a/crypto/fipsmodule/bn/add.c b/crypto/fipsmodule/bn/add.c
index bbe275e..201c526 100644
--- a/crypto/fipsmodule/bn/add.c
+++ b/crypto/fipsmodule/bn/add.c
@@ -133,7 +133,7 @@
while (dif) {
dif--;
t1 = *(ap++);
- t2 = (t1 + 1) & BN_MASK2;
+ t2 = t1 + 1;
*(rp++) = t2;
if (t2) {
carry = 0;
@@ -162,8 +162,6 @@
BN_ULONG l;
int i;
- w &= BN_MASK2;
-
// degenerate case: w is zero
if (!w) {
return 1;
@@ -185,7 +183,7 @@
}
for (i = 0; w != 0 && i < a->top; i++) {
- a->d[i] = l = (a->d[i] + w) & BN_MASK2;
+ a->d[i] = l = a->d[i] + w;
w = (w > l) ? 1 : 0;
}
@@ -285,12 +283,12 @@
t2 = *(bp++);
if (carry) {
carry = (t1 <= t2);
- t1 = (t1 - t2 - 1) & BN_MASK2;
+ t1 -= t2 + 1;
} else {
carry = (t1 < t2);
- t1 = (t1 - t2) & BN_MASK2;
+ t1 -= t2;
}
- *(rp++) = t1 & BN_MASK2;
+ *(rp++) = t1;
}
if (carry) // subtracted
@@ -303,7 +301,7 @@
while (dif) {
dif--;
t1 = *(ap++);
- t2 = (t1 - 1) & BN_MASK2;
+ t2 = t1 - 1;
*(rp++) = t2;
if (t1) {
break;
@@ -325,8 +323,6 @@
int BN_sub_word(BIGNUM *a, BN_ULONG w) {
int i;
- w &= BN_MASK2;
-
// degenerate case: w is zero
if (!w) {
return 1;
@@ -361,7 +357,7 @@
a->d[i] -= w;
break;
} else {
- a->d[i] = (a->d[i] - w) & BN_MASK2;
+ a->d[i] -= w;
i++;
w = 1;
}
diff --git a/crypto/fipsmodule/bn/div.c b/crypto/fipsmodule/bn/div.c
index 1bcff50..0b8b9b9 100644
--- a/crypto/fipsmodule/bn/div.c
+++ b/crypto/fipsmodule/bn/div.c
@@ -128,7 +128,7 @@
}
ret = q << BN_BITS4;
- h = ((h << BN_BITS4) | (l >> BN_BITS4)) & BN_MASK2;
+ h = (h << BN_BITS4) | (l >> BN_BITS4);
l = (l & BN_MASK2l) << BN_BITS4;
}
@@ -569,8 +569,6 @@
BN_ULONG ret = 0;
int i, j;
- w &= BN_MASK2;
-
if (!w) {
// actually this an error (division by zero)
return (BN_ULONG) - 1;
@@ -592,7 +590,7 @@
BN_ULONG d;
BN_ULONG unused_rem;
bn_div_rem_words(&d, &unused_rem, ret, l, w);
- ret = (l - ((d * w) & BN_MASK2)) & BN_MASK2;
+ ret = l - (d * w);
a->d[i] = d;
}
@@ -634,7 +632,6 @@
}
#endif
- w &= BN_MASK2;
for (i = a->top - 1; i >= 0; i--) {
#ifndef BN_ULLONG
ret = ((ret << BN_BITS4) | ((a->d[i] >> BN_BITS4) & BN_MASK2l)) % w;
diff --git a/crypto/fipsmodule/bn/exponentiation.c b/crypto/fipsmodule/bn/exponentiation.c
index f4e028b..b6b7fa9 100644
--- a/crypto/fipsmodule/bn/exponentiation.c
+++ b/crypto/fipsmodule/bn/exponentiation.c
@@ -655,9 +655,9 @@
goto err;
}
// 2^(top*BN_BITS2) - m
- r->d[0] = (0 - m->d[0]) & BN_MASK2;
+ r->d[0] = 0 - m->d[0];
for (i = 1; i < j; i++) {
- r->d[i] = (~m->d[i]) & BN_MASK2;
+ r->d[i] = ~m->d[i];
}
r->top = j;
// Upper words will be zero if the corresponding words of 'm'
@@ -963,9 +963,9 @@
// by Shay Gueron's suggestion
if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {
// 2^(top*BN_BITS2) - m
- tmp.d[0] = (0 - m->d[0]) & BN_MASK2;
+ tmp.d[0] = 0 - m->d[0];
for (i = 1; i < top; i++) {
- tmp.d[i] = (~m->d[i]) & BN_MASK2;
+ tmp.d[i] = ~m->d[i];
}
tmp.top = top;
} else if (!BN_to_montgomery(&tmp, BN_value_one(), mont, ctx)) {
diff --git a/crypto/fipsmodule/bn/generic.c b/crypto/fipsmodule/bn/generic.c
index b70080f..11c377c 100644
--- a/crypto/fipsmodule/bn/generic.c
+++ b/crypto/fipsmodule/bn/generic.c
@@ -209,21 +209,21 @@
assert(n >= 0);
if (n <= 0) {
- return (BN_ULONG)0;
+ return 0;
}
while (n & ~3) {
ll += (BN_ULLONG)a[0] + b[0];
- r[0] = (BN_ULONG)ll & BN_MASK2;
+ r[0] = (BN_ULONG)ll;
ll >>= BN_BITS2;
ll += (BN_ULLONG)a[1] + b[1];
- r[1] = (BN_ULONG)ll & BN_MASK2;
+ r[1] = (BN_ULONG)ll;
ll >>= BN_BITS2;
ll += (BN_ULLONG)a[2] + b[2];
- r[2] = (BN_ULONG)ll & BN_MASK2;
+ r[2] = (BN_ULONG)ll;
ll >>= BN_BITS2;
ll += (BN_ULLONG)a[3] + b[3];
- r[3] = (BN_ULONG)ll & BN_MASK2;
+ r[3] = (BN_ULONG)ll;
ll >>= BN_BITS2;
a += 4;
b += 4;
@@ -232,7 +232,7 @@
}
while (n) {
ll += (BN_ULLONG)a[0] + b[0];
- r[0] = (BN_ULONG)ll & BN_MASK2;
+ r[0] = (BN_ULONG)ll;
ll >>= BN_BITS2;
a++;
b++;
@@ -256,27 +256,27 @@
c = 0;
while (n & ~3) {
t = a[0];
- t = (t + c) & BN_MASK2;
+ t += c;
c = (t < c);
- l = (t + b[0]) & BN_MASK2;
+ l = t + b[0];
c += (l < t);
r[0] = l;
t = a[1];
- t = (t + c) & BN_MASK2;
+ t += c;
c = (t < c);
- l = (t + b[1]) & BN_MASK2;
+ l = t + b[1];
c += (l < t);
r[1] = l;
t = a[2];
- t = (t + c) & BN_MASK2;
+ t += c;
c = (t < c);
- l = (t + b[2]) & BN_MASK2;
+ l = t + b[2];
c += (l < t);
r[2] = l;
t = a[3];
- t = (t + c) & BN_MASK2;
+ t += c;
c = (t < c);
- l = (t + b[3]) & BN_MASK2;
+ l = t + b[3];
c += (l < t);
r[3] = l;
a += 4;
@@ -286,9 +286,9 @@
}
while (n) {
t = a[0];
- t = (t + c) & BN_MASK2;
+ t += c;
c = (t < c);
- l = (t + b[0]) & BN_MASK2;
+ l = t + b[0];
c += (l < t);
r[0] = l;
a++;
@@ -314,25 +314,25 @@
while (n & ~3) {
t1 = a[0];
t2 = b[0];
- r[0] = (t1 - t2 - c) & BN_MASK2;
+ r[0] = t1 - t2 - c;
if (t1 != t2) {
c = (t1 < t2);
}
t1 = a[1];
t2 = b[1];
- r[1] = (t1 - t2 - c) & BN_MASK2;
+ r[1] = t1 - t2 - c;
if (t1 != t2) {
c = (t1 < t2);
}
t1 = a[2];
t2 = b[2];
- r[2] = (t1 - t2 - c) & BN_MASK2;
+ r[2] = t1 - t2 - c;
if (t1 != t2) {
c = (t1 < t2);
}
t1 = a[3];
t2 = b[3];
- r[3] = (t1 - t2 - c) & BN_MASK2;
+ r[3] = t1 - t2 - c;
if (t1 != t2) {
c = (t1 < t2);
}
@@ -344,7 +344,7 @@
while (n) {
t1 = a[0];
t2 = b[0];
- r[0] = (t1 - t2 - c) & BN_MASK2;
+ r[0] = t1 - t2 - c;
if (t1 != t2) {
c = (t1 < t2);
}
@@ -372,7 +372,7 @@
t += (c0); /* no carry */ \
(c0) = (BN_ULONG)Lw(t); \
hi = (BN_ULONG)Hw(t); \
- (c1) = ((c1) + (hi)) & BN_MASK2; \
+ (c1) += (hi); \
if ((c1) < hi) { \
(c2)++; \
} \
@@ -385,14 +385,14 @@
BN_ULLONG tt = t + (c0); /* no carry */ \
(c0) = (BN_ULONG)Lw(tt); \
hi = (BN_ULONG)Hw(tt); \
- (c1) = ((c1) + hi) & BN_MASK2; \
+ (c1) += hi; \
if ((c1) < hi) { \
(c2)++; \
} \
t += (c0); /* no carry */ \
(c0) = (BN_ULONG)Lw(t); \
hi = (BN_ULONG)Hw(t); \
- (c1) = ((c1) + hi) & BN_MASK2; \
+ (c1) += hi; \
if ((c1) < hi) { \
(c2)++; \
} \
@@ -405,7 +405,7 @@
t += (c0); /* no carry */ \
(c0) = (BN_ULONG)Lw(t); \
hi = (BN_ULONG)Hw(t); \
- (c1) = ((c1) + hi) & BN_MASK2; \
+ (c1) += hi; \
if ((c1) < hi) { \
(c2)++; \
} \
diff --git a/crypto/fipsmodule/bn/internal.h b/crypto/fipsmodule/bn/internal.h
index ecd7d6c..b1e0b0d 100644
--- a/crypto/fipsmodule/bn/internal.h
+++ b/crypto/fipsmodule/bn/internal.h
@@ -191,8 +191,8 @@
}
#if defined(BN_ULLONG)
-#define Lw(t) (((BN_ULONG)(t))&BN_MASK2)
-#define Hw(t) (((BN_ULONG)((t)>>BN_BITS2))&BN_MASK2)
+#define Lw(t) ((BN_ULONG)(t))
+#define Hw(t) ((BN_ULONG)((t) >> BN_BITS2))
#endif
// bn_correct_top decrements |bn->top| until |bn->d[top-1]| is non-zero or
diff --git a/crypto/fipsmodule/bn/montgomery.c b/crypto/fipsmodule/bn/montgomery.c
index 8024e27..5219187 100644
--- a/crypto/fipsmodule/bn/montgomery.c
+++ b/crypto/fipsmodule/bn/montgomery.c
@@ -290,8 +290,8 @@
n0 = mont->n0[0];
for (carry = 0, i = 0; i < nl; i++, rp++) {
- v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);
- v = (v + carry + rp[nl]) & BN_MASK2;
+ v = bn_mul_add_words(rp, np, nl, rp[0] * n0);
+ v += carry + rp[nl];
carry |= (v != rp[nl]);
carry &= (v <= rp[nl]);
rp[nl] = v;
diff --git a/crypto/fipsmodule/bn/mul.c b/crypto/fipsmodule/bn/mul.c
index a4e27f2..b6f3ff1 100644
--- a/crypto/fipsmodule/bn/mul.c
+++ b/crypto/fipsmodule/bn/mul.c
@@ -141,7 +141,7 @@
if (dl < 0) {
for (;;) {
t = b[0];
- r[0] = (0 - t - c) & BN_MASK2;
+ r[0] = 0 - t - c;
if (t != 0) {
c = 1;
}
@@ -150,7 +150,7 @@
}
t = b[1];
- r[1] = (0 - t - c) & BN_MASK2;
+ r[1] = 0 - t - c;
if (t != 0) {
c = 1;
}
@@ -159,7 +159,7 @@
}
t = b[2];
- r[2] = (0 - t - c) & BN_MASK2;
+ r[2] = 0 - t - c;
if (t != 0) {
c = 1;
}
@@ -168,7 +168,7 @@
}
t = b[3];
- r[3] = (0 - t - c) & BN_MASK2;
+ r[3] = 0 - t - c;
if (t != 0) {
c = 1;
}
@@ -183,7 +183,7 @@
int save_dl = dl;
while (c) {
t = a[0];
- r[0] = (t - c) & BN_MASK2;
+ r[0] = t - c;
if (t != 0) {
c = 0;
}
@@ -192,7 +192,7 @@
}
t = a[1];
- r[1] = (t - c) & BN_MASK2;
+ r[1] = t - c;
if (t != 0) {
c = 0;
}
@@ -201,7 +201,7 @@
}
t = a[2];
- r[2] = (t - c) & BN_MASK2;
+ r[2] = t - c;
if (t != 0) {
c = 0;
}
@@ -210,7 +210,7 @@
}
t = a[3];
- r[3] = (t - c) & BN_MASK2;
+ r[3] = t - c;
if (t != 0) {
c = 0;
}
@@ -407,7 +407,7 @@
if (c1) {
p = &(r[n + n2]);
lo = *p;
- ln = (lo + c1) & BN_MASK2;
+ ln = lo + c1;
*p = ln;
// The overflow will stop before we over write
@@ -416,7 +416,7 @@
do {
p++;
lo = *p;
- ln = (lo + 1) & BN_MASK2;
+ ln = lo + 1;
*p = ln;
} while (ln == 0);
}
@@ -544,7 +544,7 @@
if (c1) {
p = &(r[n + n2]);
lo = *p;
- ln = (lo + c1) & BN_MASK2;
+ ln = lo + c1;
*p = ln;
// The overflow will stop before we over write
@@ -553,7 +553,7 @@
do {
p++;
lo = *p;
- ln = (lo + 1) & BN_MASK2;
+ ln = lo + 1;
*p = ln;
} while (ln == 0);
}
@@ -757,7 +757,7 @@
if (c1) {
p = &(r[n + n2]);
lo = *p;
- ln = (lo + c1) & BN_MASK2;
+ ln = lo + c1;
*p = ln;
// The overflow will stop before we over write
@@ -766,7 +766,7 @@
do {
p++;
lo = *p;
- ln = (lo + 1) & BN_MASK2;
+ ln = lo + 1;
*p = ln;
} while (ln == 0);
}
@@ -774,9 +774,6 @@
}
int BN_mul_word(BIGNUM *bn, BN_ULONG w) {
- BN_ULONG ll;
-
- w &= BN_MASK2;
if (!bn->top) {
return 1;
}
@@ -786,7 +783,7 @@
return 1;
}
- ll = bn_mul_words(bn->d, bn->d, bn->top, w);
+ BN_ULONG ll = bn_mul_words(bn->d, bn->d, bn->top, w);
if (ll) {
if (!bn_wexpand(bn, bn->top + 1)) {
return 0;
diff --git a/crypto/fipsmodule/bn/shift.c b/crypto/fipsmodule/bn/shift.c
index d3fcf39..64afa78 100644
--- a/crypto/fipsmodule/bn/shift.c
+++ b/crypto/fipsmodule/bn/shift.c
@@ -90,8 +90,8 @@
} else {
for (i = a->top - 1; i >= 0; i--) {
l = f[i];
- t[nw + i + 1] |= (l >> rb) & BN_MASK2;
- t[nw + i] = (l << lb) & BN_MASK2;
+ t[nw + i + 1] |= l >> rb;
+ t[nw + i] = l << lb;
}
}
OPENSSL_memset(t, 0, nw * sizeof(t[0]));
@@ -121,7 +121,7 @@
c = 0;
for (i = 0; i < a->top; i++) {
t = *(ap++);
- *(rp++) = ((t << 1) | c) & BN_MASK2;
+ *(rp++) = (t << 1) | c;
c = (t & BN_TBIT) ? 1 : 0;
}
if (c) {
@@ -173,11 +173,12 @@
} else {
l = *(f++);
for (i = j - 1; i != 0; i--) {
- tmp = (l >> rb) & BN_MASK2;
+ tmp = l >> rb;
l = *(f++);
- *(t++) = (tmp | (l << lb)) & BN_MASK2;
+ *(t++) = tmp | (l << lb);
}
- if ((l = (l >> rb) & BN_MASK2)) {
+ l >>= rb;
+ if (l) {
*(t) = l;
}
}
@@ -214,7 +215,7 @@
}
while (i > 0) {
t = ap[--i];
- rp[i] = ((t >> 1) & BN_MASK2) | c;
+ rp[i] = (t >> 1) | c;
c = (t & 1) ? BN_TBIT : 0;
}
r->top = j;
@@ -227,19 +228,17 @@
}
int BN_set_bit(BIGNUM *a, int n) {
- int i, j, k;
-
if (n < 0) {
return 0;
}
- i = n / BN_BITS2;
- j = n % BN_BITS2;
+ int i = n / BN_BITS2;
+ int j = n % BN_BITS2;
if (a->top <= i) {
if (!bn_wexpand(a, i + 1)) {
return 0;
}
- for (k = a->top; k < i + 1; k++) {
+ for (int k = a->top; k < i + 1; k++) {
a->d[k] = 0;
}
a->top = i + 1;
@@ -269,13 +268,11 @@
}
int BN_is_bit_set(const BIGNUM *a, int n) {
- int i, j;
-
if (n < 0) {
return 0;
}
- i = n / BN_BITS2;
- j = n % BN_BITS2;
+ int i = n / BN_BITS2;
+ int j = n % BN_BITS2;
if (a->top <= i) {
return 0;
}
@@ -284,14 +281,12 @@
}
int BN_mask_bits(BIGNUM *a, int n) {
- int b, w;
-
if (n < 0) {
return 0;
}
- w = n / BN_BITS2;
- b = n % BN_BITS2;
+ int w = n / BN_BITS2;
+ int b = n % BN_BITS2;
if (w >= a->top) {
return 0;
}