| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/bn.h> |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
#include <openssl/err.h> |
| 11 |
#include <openssl/mem.h> |
| 12 |
#include <openssl/type_check.h> |
| 13 |
|
| 14 |
#include "internal.h" |
| 15 |
#include "../../internal.h" |
| 16 |
|
| 17 |
|
| 18 |
#define BN_MUL_RECURSIVE_SIZE_NORMAL 16 |
| 19 |
#define BN_SQR_RECURSIVE_SIZE_NORMAL BN_MUL_RECURSIVE_SIZE_NORMAL |
| 20 |
|
| 21 |
|
| 22 |
static void bn_abs_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, |
| 23 |
size_t num, BN_ULONG *tmp) { |
| 24 |
BN_ULONG borrow = bn_sub_words(tmp, a, b, num); |
| 25 |
bn_sub_words(r, b, a, num); |
| 26 |
bn_select_words(r, 0 - borrow, r /* tmp < 0 */, tmp /* tmp >= 0 */, num); |
| 27 |
} |
| 28 |
|
| 29 |
static void bn_mul_normal(BN_ULONG *r, const BN_ULONG *a, size_t na, |
| 30 |
const BN_ULONG *b, size_t nb) { |
| 31 |
if (na < nb) { |
| 32 |
size_t itmp = na; |
| 33 |
na = nb; |
| 34 |
nb = itmp; |
| 35 |
const BN_ULONG *ltmp = a; |
| 36 |
a = b; |
| 37 |
b = ltmp; |
| 38 |
} |
| 39 |
BN_ULONG *rr = &(r[na]); |
| 40 |
if (nb == 0) { |
| 41 |
OPENSSL_memset(r, 0, na * sizeof(BN_ULONG)); |
| 42 |
return; |
| 43 |
} |
| 44 |
rr[0] = bn_mul_words(r, a, na, b[0]); |
| 45 |
|
| 46 |
for (;;) { |
| 47 |
if (--nb == 0) { |
| 48 |
return; |
| 49 |
} |
| 50 |
rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]); |
| 51 |
if (--nb == 0) { |
| 52 |
return; |
| 53 |
} |
| 54 |
rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]); |
| 55 |
if (--nb == 0) { |
| 56 |
return; |
| 57 |
} |
| 58 |
rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]); |
| 59 |
if (--nb == 0) { |
| 60 |
return; |
| 61 |
} |
| 62 |
rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]); |
| 63 |
rr += 4; |
| 64 |
r += 4; |
| 65 |
b += 4; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
// bn_sub_part_words sets |r| to |a| - |b|. It returns the borrow bit, which is |
| 70 |
// one if the operation underflowed and zero otherwise. |cl| is the common |
| 71 |
// length, that is, the shorter of len(a) or len(b). |dl| is the delta length, |
| 72 |
// that is, len(a) - len(b). |r|'s length matches the larger of |a| and |b|, or |
| 73 |
// cl + abs(dl). |
| 74 |
// |
| 75 |
// TODO(davidben): Make this take |size_t|. The |cl| + |dl| calling convention |
| 76 |
// is confusing. |
| 77 |
static BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, |
| 78 |
const BN_ULONG *b, int cl, int dl) { |
| 79 |
assert(cl >= 0); |
| 80 |
BN_ULONG borrow = bn_sub_words(r, a, b, cl); |
| 81 |
if (dl == 0) { |
| 82 |
return borrow; |
| 83 |
} |
| 84 |
|
| 85 |
r += cl; |
| 86 |
a += cl; |
| 87 |
b += cl; |
| 88 |
|
| 89 |
if (dl < 0) { |
| 90 |
// |a| is shorter than |b|. Complete the subtraction as if the excess words |
| 91 |
// in |a| were zeros. |
| 92 |
dl = -dl; |
| 93 |
for (int i = 0; i < dl; i++) { |
| 94 |
r[i] = CRYPTO_subc_w(0, b[i], borrow, &borrow); |
| 95 |
} |
| 96 |
} else { |
| 97 |
// |b| is shorter than |a|. Complete the subtraction as if the excess words |
| 98 |
// in |b| were zeros. |
| 99 |
for (int i = 0; i < dl; i++) { |
| 100 |
r[i] = CRYPTO_subc_w(a[i], 0, borrow, &borrow); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
return borrow; |
| 105 |
} |
| 106 |
|
| 107 |
// bn_abs_sub_part_words computes |r| = |a| - |b|, storing the absolute value |
| 108 |
// and returning a mask of all ones if the result was negative and all zeros if |
| 109 |
// the result was positive. |cl| and |dl| follow the |bn_sub_part_words| calling |
| 110 |
// convention. |
| 111 |
// |
| 112 |
// TODO(davidben): Make this take |size_t|. The |cl| + |dl| calling convention |
| 113 |
// is confusing. |
| 114 |
static BN_ULONG bn_abs_sub_part_words(BN_ULONG *r, const BN_ULONG *a, |
| 115 |
const BN_ULONG *b, int cl, int dl, |
| 116 |
BN_ULONG *tmp) { |
| 117 |
BN_ULONG borrow = bn_sub_part_words(tmp, a, b, cl, dl); |
| 118 |
bn_sub_part_words(r, b, a, cl, -dl); |
| 119 |
int r_len = cl + (dl < 0 ? -dl : dl); |
| 120 |
borrow = 0 - borrow; |
| 121 |
bn_select_words(r, borrow, r /* tmp < 0 */, tmp /* tmp >= 0 */, r_len); |
| 122 |
return borrow; |
| 123 |
} |
| 124 |
|
| 125 |
int bn_abs_sub_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 126 |
BN_CTX *ctx) { |
| 127 |
int cl = a->width < b->width ? a->width : b->width; |
| 128 |
int dl = a->width - b->width; |
| 129 |
int r_len = a->width < b->width ? b->width : a->width; |
| 130 |
BN_CTX_start(ctx); |
| 131 |
BIGNUM *tmp = BN_CTX_get(ctx); |
| 132 |
int ok = tmp != NULL && |
| 133 |
bn_wexpand(r, r_len) && |
| 134 |
bn_wexpand(tmp, r_len); |
| 135 |
if (ok) { |
| 136 |
bn_abs_sub_part_words(r->d, a->d, b->d, cl, dl, tmp->d); |
| 137 |
r->width = r_len; |
| 138 |
} |
| 139 |
BN_CTX_end(ctx); |
| 140 |
return ok; |
| 141 |
} |
| 142 |
|
| 143 |
// Karatsuba recursive multiplication algorithm |
| 144 |
// (cf. Knuth, The Art of Computer Programming, Vol. 2) |
| 145 |
|
| 146 |
// bn_mul_recursive sets |r| to |a| * |b|, using |t| as scratch space. |r| has |
| 147 |
// length 2*|n2|, |a| has length |n2| + |dna|, |b| has length |n2| + |dnb|, and |
| 148 |
// |t| has length 4*|n2|. |n2| must be a power of two. Finally, we must have |
| 149 |
// -|BN_MUL_RECURSIVE_SIZE_NORMAL|/2 <= |dna| <= 0 and |
| 150 |
// -|BN_MUL_RECURSIVE_SIZE_NORMAL|/2 <= |dnb| <= 0. |
| 151 |
// |
| 152 |
// TODO(davidben): Simplify and |size_t| the calling convention around lengths |
| 153 |
// here. |
| 154 |
static void bn_mul_recursive(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, |
| 155 |
int n2, int dna, int dnb, BN_ULONG *t) { |
| 156 |
// |n2| is a power of two. |
| 157 |
assert(n2 != 0 && (n2 & (n2 - 1)) == 0); |
| 158 |
// Check |dna| and |dnb| are in range. |
| 159 |
assert(-BN_MUL_RECURSIVE_SIZE_NORMAL/2 <= dna && dna <= 0); |
| 160 |
assert(-BN_MUL_RECURSIVE_SIZE_NORMAL/2 <= dnb && dnb <= 0); |
| 161 |
|
| 162 |
// Only call bn_mul_comba 8 if n2 == 8 and the |
| 163 |
// two arrays are complete [steve] |
| 164 |
if (n2 == 8 && dna == 0 && dnb == 0) { |
| 165 |
bn_mul_comba8(r, a, b); |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
// Else do normal multiply |
| 170 |
if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) { |
| 171 |
bn_mul_normal(r, a, n2 + dna, b, n2 + dnb); |
| 172 |
if (dna + dnb < 0) { |
| 173 |
OPENSSL_memset(&r[2 * n2 + dna + dnb], 0, |
| 174 |
sizeof(BN_ULONG) * -(dna + dnb)); |
| 175 |
} |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
// Split |a| and |b| into a0,a1 and b0,b1, where a0 and b0 have size |n|. |
| 180 |
// Split |t| into t0,t1,t2,t3, each of size |n|, with the remaining 4*|n| used |
| 181 |
// for recursive calls. |
| 182 |
// Split |r| into r0,r1,r2,r3. We must contribute a0*b0 to r0,r1, a0*a1+b0*b1 |
| 183 |
// to r1,r2, and a1*b1 to r2,r3. The middle term we will compute as: |
| 184 |
// |
| 185 |
// a0*a1 + b0*b1 = (a0 - a1)*(b1 - b0) + a1*b1 + a0*b0 |
| 186 |
// |
| 187 |
// Note that we know |n| >= |BN_MUL_RECURSIVE_SIZE_NORMAL|/2 above, so |
| 188 |
// |tna| and |tnb| are non-negative. |
| 189 |
int n = n2 / 2, tna = n + dna, tnb = n + dnb; |
| 190 |
|
| 191 |
// t0 = a0 - a1 and t1 = b1 - b0. The result will be multiplied, so we XOR |
| 192 |
// their sign masks, giving the sign of (a0 - a1)*(b1 - b0). t0 and t1 |
| 193 |
// themselves store the absolute value. |
| 194 |
BN_ULONG neg = bn_abs_sub_part_words(t, a, &a[n], tna, n - tna, &t[n2]); |
| 195 |
neg ^= bn_abs_sub_part_words(&t[n], &b[n], b, tnb, tnb - n, &t[n2]); |
| 196 |
|
| 197 |
// Compute: |
| 198 |
// t2,t3 = t0 * t1 = |(a0 - a1)*(b1 - b0)| |
| 199 |
// r0,r1 = a0 * b0 |
| 200 |
// r2,r3 = a1 * b1 |
| 201 |
if (n == 4 && dna == 0 && dnb == 0) { |
| 202 |
bn_mul_comba4(&t[n2], t, &t[n]); |
| 203 |
|
| 204 |
bn_mul_comba4(r, a, b); |
| 205 |
bn_mul_comba4(&r[n2], &a[n], &b[n]); |
| 206 |
} else if (n == 8 && dna == 0 && dnb == 0) { |
| 207 |
bn_mul_comba8(&t[n2], t, &t[n]); |
| 208 |
|
| 209 |
bn_mul_comba8(r, a, b); |
| 210 |
bn_mul_comba8(&r[n2], &a[n], &b[n]); |
| 211 |
} else { |
| 212 |
BN_ULONG *p = &t[n2 * 2]; |
| 213 |
bn_mul_recursive(&t[n2], t, &t[n], n, 0, 0, p); |
| 214 |
bn_mul_recursive(r, a, b, n, 0, 0, p); |
| 215 |
bn_mul_recursive(&r[n2], &a[n], &b[n], n, dna, dnb, p); |
| 216 |
} |
| 217 |
|
| 218 |
// t0,t1,c = r0,r1 + r2,r3 = a0*b0 + a1*b1 |
| 219 |
BN_ULONG c = bn_add_words(t, r, &r[n2], n2); |
| 220 |
|
| 221 |
// t2,t3,c = t0,t1,c + neg*t2,t3 = (a0 - a1)*(b1 - b0) + a1*b1 + a0*b0. |
| 222 |
// The second term is stored as the absolute value, so we do this with a |
| 223 |
// constant-time select. |
| 224 |
BN_ULONG c_neg = c - bn_sub_words(&t[n2 * 2], t, &t[n2], n2); |
| 225 |
BN_ULONG c_pos = c + bn_add_words(&t[n2], t, &t[n2], n2); |
| 226 |
bn_select_words(&t[n2], neg, &t[n2 * 2], &t[n2], n2); |
| 227 |
OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t), |
| 228 |
crypto_word_t_is_too_small) |
| 229 |
c = constant_time_select_w(neg, c_neg, c_pos); |
| 230 |
|
| 231 |
// We now have our three components. Add them together. |
| 232 |
// r1,r2,c = r1,r2 + t2,t3,c |
| 233 |
c += bn_add_words(&r[n], &r[n], &t[n2], n2); |
| 234 |
|
| 235 |
// Propagate the carry bit to the end. |
| 236 |
for (int i = n + n2; i < n2 + n2; i++) { |
| 237 |
BN_ULONG old = r[i]; |
| 238 |
r[i] = old + c; |
| 239 |
c = r[i] < old; |
| 240 |
} |
| 241 |
|
| 242 |
// The product should fit without carries. |
| 243 |
declassify_assert(c == 0); |
| 244 |
} |
| 245 |
|
| 246 |
// bn_mul_part_recursive sets |r| to |a| * |b|, using |t| as scratch space. |r| |
| 247 |
// has length 4*|n|, |a| has length |n| + |tna|, |b| has length |n| + |tnb|, and |
| 248 |
// |t| has length 8*|n|. |n| must be a power of two. Additionally, we must have |
| 249 |
// 0 <= tna < n and 0 <= tnb < n, and |tna| and |tnb| must differ by at most |
| 250 |
// one. |
| 251 |
// |
| 252 |
// TODO(davidben): Make this take |size_t| and perhaps the actual lengths of |a| |
| 253 |
// and |b|. |
| 254 |
static void bn_mul_part_recursive(BN_ULONG *r, const BN_ULONG *a, |
| 255 |
const BN_ULONG *b, int n, int tna, int tnb, |
| 256 |
BN_ULONG *t) { |
| 257 |
// |n| is a power of two. |
| 258 |
assert(n != 0 && (n & (n - 1)) == 0); |
| 259 |
// Check |tna| and |tnb| are in range. |
| 260 |
assert(0 <= tna && tna < n); |
| 261 |
assert(0 <= tnb && tnb < n); |
| 262 |
assert(-1 <= tna - tnb && tna - tnb <= 1); |
| 263 |
|
| 264 |
int n2 = n * 2; |
| 265 |
if (n < 8) { |
| 266 |
bn_mul_normal(r, a, n + tna, b, n + tnb); |
| 267 |
OPENSSL_memset(r + n2 + tna + tnb, 0, n2 - tna - tnb); |
| 268 |
return; |
| 269 |
} |
| 270 |
|
| 271 |
// Split |a| and |b| into a0,a1 and b0,b1, where a0 and b0 have size |n|. |a1| |
| 272 |
// and |b1| have size |tna| and |tnb|, respectively. |
| 273 |
// Split |t| into t0,t1,t2,t3, each of size |n|, with the remaining 4*|n| used |
| 274 |
// for recursive calls. |
| 275 |
// Split |r| into r0,r1,r2,r3. We must contribute a0*b0 to r0,r1, a0*a1+b0*b1 |
| 276 |
// to r1,r2, and a1*b1 to r2,r3. The middle term we will compute as: |
| 277 |
// |
| 278 |
// a0*a1 + b0*b1 = (a0 - a1)*(b1 - b0) + a1*b1 + a0*b0 |
| 279 |
|
| 280 |
// t0 = a0 - a1 and t1 = b1 - b0. The result will be multiplied, so we XOR |
| 281 |
// their sign masks, giving the sign of (a0 - a1)*(b1 - b0). t0 and t1 |
| 282 |
// themselves store the absolute value. |
| 283 |
BN_ULONG neg = bn_abs_sub_part_words(t, a, &a[n], tna, n - tna, &t[n2]); |
| 284 |
neg ^= bn_abs_sub_part_words(&t[n], &b[n], b, tnb, tnb - n, &t[n2]); |
| 285 |
|
| 286 |
// Compute: |
| 287 |
// t2,t3 = t0 * t1 = |(a0 - a1)*(b1 - b0)| |
| 288 |
// r0,r1 = a0 * b0 |
| 289 |
// r2,r3 = a1 * b1 |
| 290 |
if (n == 8) { |
| 291 |
bn_mul_comba8(&t[n2], t, &t[n]); |
| 292 |
bn_mul_comba8(r, a, b); |
| 293 |
|
| 294 |
bn_mul_normal(&r[n2], &a[n], tna, &b[n], tnb); |
| 295 |
// |bn_mul_normal| only writes |tna| + |tna| words. Zero the rest. |
| 296 |
OPENSSL_memset(&r[n2 + tna + tnb], 0, sizeof(BN_ULONG) * (n2 - tna - tnb)); |
| 297 |
} else { |
| 298 |
BN_ULONG *p = &t[n2 * 2]; |
| 299 |
bn_mul_recursive(&t[n2], t, &t[n], n, 0, 0, p); |
| 300 |
bn_mul_recursive(r, a, b, n, 0, 0, p); |
| 301 |
|
| 302 |
OPENSSL_memset(&r[n2], 0, sizeof(BN_ULONG) * n2); |
| 303 |
if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL && |
| 304 |
tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) { |
| 305 |
bn_mul_normal(&r[n2], &a[n], tna, &b[n], tnb); |
| 306 |
} else { |
| 307 |
int i = n; |
| 308 |
for (;;) { |
| 309 |
i /= 2; |
| 310 |
if (i < tna || i < tnb) { |
| 311 |
// E.g., n == 16, i == 8 and tna == 11. |tna| and |tnb| are within one |
| 312 |
// of each other, so if |tna| is larger and tna > i, then we know |
| 313 |
// tnb >= i, and this call is valid. |
| 314 |
bn_mul_part_recursive(&r[n2], &a[n], &b[n], i, tna - i, tnb - i, p); |
| 315 |
break; |
| 316 |
} |
| 317 |
if (i == tna || i == tnb) { |
| 318 |
// If there is only a bottom half to the number, just do it. We know |
| 319 |
// the larger of |tna - i| and |tnb - i| is zero. The other is zero or |
| 320 |
// -1 by because of |tna| and |tnb| differ by at most one. |
| 321 |
bn_mul_recursive(&r[n2], &a[n], &b[n], i, tna - i, tnb - i, p); |
| 322 |
break; |
| 323 |
} |
| 324 |
|
| 325 |
// This loop will eventually terminate when |i| falls below |
| 326 |
// |BN_MUL_RECURSIVE_SIZE_NORMAL| because we know one of |tna| and |tnb| |
| 327 |
// exceeds that. |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
// t0,t1,c = r0,r1 + r2,r3 = a0*b0 + a1*b1 |
| 333 |
BN_ULONG c = bn_add_words(t, r, &r[n2], n2); |
| 334 |
|
| 335 |
// t2,t3,c = t0,t1,c + neg*t2,t3 = (a0 - a1)*(b1 - b0) + a1*b1 + a0*b0. |
| 336 |
// The second term is stored as the absolute value, so we do this with a |
| 337 |
// constant-time select. |
| 338 |
BN_ULONG c_neg = c - bn_sub_words(&t[n2 * 2], t, &t[n2], n2); |
| 339 |
BN_ULONG c_pos = c + bn_add_words(&t[n2], t, &t[n2], n2); |
| 340 |
bn_select_words(&t[n2], neg, &t[n2 * 2], &t[n2], n2); |
| 341 |
OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t), |
| 342 |
crypto_word_t_is_too_small) |
| 343 |
c = constant_time_select_w(neg, c_neg, c_pos); |
| 344 |
|
| 345 |
// We now have our three components. Add them together. |
| 346 |
// r1,r2,c = r1,r2 + t2,t3,c |
| 347 |
c += bn_add_words(&r[n], &r[n], &t[n2], n2); |
| 348 |
|
| 349 |
// Propagate the carry bit to the end. |
| 350 |
for (int i = n + n2; i < n2 + n2; i++) { |
| 351 |
BN_ULONG old = r[i]; |
| 352 |
r[i] = old + c; |
| 353 |
c = r[i] < old; |
| 354 |
} |
| 355 |
|
| 356 |
// The product should fit without carries. |
| 357 |
declassify_assert(c == 0); |
| 358 |
} |
| 359 |
|
| 360 |
// bn_mul_impl implements |BN_mul| and |bn_mul_consttime|. Note this function |
| 361 |
// breaks |BIGNUM| invariants and may return a negative zero. This is handled by |
| 362 |
// the callers. |
| 363 |
static int bn_mul_impl(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 364 |
BN_CTX *ctx) { |
| 365 |
int al = a->width; |
| 366 |
int bl = b->width; |
| 367 |
if (al == 0 || bl == 0) { |
| 368 |
BN_zero(r); |
| 369 |
return 1; |
| 370 |
} |
| 371 |
|
| 372 |
int ret = 0; |
| 373 |
BIGNUM *rr; |
| 374 |
BN_CTX_start(ctx); |
| 375 |
if (r == a || r == b) { |
| 376 |
rr = BN_CTX_get(ctx); |
| 377 |
if (rr == NULL) { |
| 378 |
goto err; |
| 379 |
} |
| 380 |
} else { |
| 381 |
rr = r; |
| 382 |
} |
| 383 |
rr->neg = a->neg ^ b->neg; |
| 384 |
|
| 385 |
int i = al - bl; |
| 386 |
if (i == 0) { |
| 387 |
if (al == 8) { |
| 388 |
if (!bn_wexpand(rr, 16)) { |
| 389 |
goto err; |
| 390 |
} |
| 391 |
rr->width = 16; |
| 392 |
bn_mul_comba8(rr->d, a->d, b->d); |
| 393 |
goto end; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
int top = al + bl; |
| 398 |
static const int kMulNormalSize = 16; |
| 399 |
if (al >= kMulNormalSize && bl >= kMulNormalSize) { |
| 400 |
if (-1 <= i && i <= 1) { |
| 401 |
// Find the largest power of two less than or equal to the larger length. |
| 402 |
int j; |
| 403 |
if (i >= 0) { |
| 404 |
j = BN_num_bits_word((BN_ULONG)al); |
| 405 |
} else { |
| 406 |
j = BN_num_bits_word((BN_ULONG)bl); |
| 407 |
} |
| 408 |
j = 1 << (j - 1); |
| 409 |
assert(j <= al || j <= bl); |
| 410 |
BIGNUM *t = BN_CTX_get(ctx); |
| 411 |
if (t == NULL) { |
| 412 |
goto err; |
| 413 |
} |
| 414 |
if (al > j || bl > j) { |
| 415 |
// We know |al| and |bl| are at most one from each other, so if al > j, |
| 416 |
// bl >= j, and vice versa. Thus we can use |bn_mul_part_recursive|. |
| 417 |
// |
| 418 |
// TODO(davidben): This codepath is almost unused in standard |
| 419 |
// algorithms. Is this optimization necessary? See notes in |
| 420 |
// https://boringssl-review.googlesource.com/q/I0bd604e2cd6a75c266f64476c23a730ca1721ea6 |
| 421 |
assert(al >= j && bl >= j); |
| 422 |
if (!bn_wexpand(t, j * 8) || |
| 423 |
!bn_wexpand(rr, j * 4)) { |
| 424 |
goto err; |
| 425 |
} |
| 426 |
bn_mul_part_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d); |
| 427 |
} else { |
| 428 |
// al <= j && bl <= j. Additionally, we know j <= al or j <= bl, so one |
| 429 |
// of al - j or bl - j is zero. The other, by the bound on |i| above, is |
| 430 |
// zero or -1. Thus, we can use |bn_mul_recursive|. |
| 431 |
if (!bn_wexpand(t, j * 4) || |
| 432 |
!bn_wexpand(rr, j * 2)) { |
| 433 |
goto err; |
| 434 |
} |
| 435 |
bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d); |
| 436 |
} |
| 437 |
rr->width = top; |
| 438 |
goto end; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
if (!bn_wexpand(rr, top)) { |
| 443 |
goto err; |
| 444 |
} |
| 445 |
rr->width = top; |
| 446 |
bn_mul_normal(rr->d, a->d, al, b->d, bl); |
| 447 |
|
| 448 |
end: |
| 449 |
if (r != rr && !BN_copy(r, rr)) { |
| 450 |
goto err; |
| 451 |
} |
| 452 |
ret = 1; |
| 453 |
|
| 454 |
err: |
| 455 |
BN_CTX_end(ctx); |
| 456 |
return ret; |
| 457 |
} |
| 458 |
|
| 459 |
int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) { |
| 460 |
if (!bn_mul_impl(r, a, b, ctx)) { |
| 461 |
return 0; |
| 462 |
} |
| 463 |
|
| 464 |
// This additionally fixes any negative zeros created by |bn_mul_impl|. |
| 465 |
bn_set_minimal_width(r); |
| 466 |
return 1; |
| 467 |
} |
| 468 |
|
| 469 |
int bn_mul_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) { |
| 470 |
// Prevent negative zeros. |
| 471 |
if (a->neg || b->neg) { |
| 472 |
OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
| 473 |
return 0; |
| 474 |
} |
| 475 |
|
| 476 |
return bn_mul_impl(r, a, b, ctx); |
| 477 |
} |
| 478 |
|
| 479 |
void bn_mul_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a, |
| 480 |
const BN_ULONG *b, size_t num_b) { |
| 481 |
if (num_r != num_a + num_b) { |
| 482 |
abort(); |
| 483 |
} |
| 484 |
// TODO(davidben): Should this call |bn_mul_comba4| too? |BN_mul| does not |
| 485 |
// hit that code. |
| 486 |
if (num_a == 8 && num_b == 8) { |
| 487 |
bn_mul_comba8(r, a, b); |
| 488 |
} else { |
| 489 |
bn_mul_normal(r, a, num_a, b, num_b); |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
// tmp must have 2*n words |
| 494 |
static void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, size_t n, |
| 495 |
BN_ULONG *tmp) { |
| 496 |
if (n == 0) { |
| 497 |
return; |
| 498 |
} |
| 499 |
|
| 500 |
size_t max = n * 2; |
| 501 |
const BN_ULONG *ap = a; |
| 502 |
BN_ULONG *rp = r; |
| 503 |
rp[0] = rp[max - 1] = 0; |
| 504 |
rp++; |
| 505 |
|
| 506 |
// Compute the contribution of a[i] * a[j] for all i < j. |
| 507 |
if (n > 1) { |
| 508 |
ap++; |
| 509 |
rp[n - 1] = bn_mul_words(rp, ap, n - 1, ap[-1]); |
| 510 |
rp += 2; |
| 511 |
} |
| 512 |
if (n > 2) { |
| 513 |
for (size_t i = n - 2; i > 0; i--) { |
| 514 |
ap++; |
| 515 |
rp[i] = bn_mul_add_words(rp, ap, i, ap[-1]); |
| 516 |
rp += 2; |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
// The final result fits in |max| words, so none of the following operations |
| 521 |
// will overflow. |
| 522 |
|
| 523 |
// Double |r|, giving the contribution of a[i] * a[j] for all i != j. |
| 524 |
bn_add_words(r, r, r, max); |
| 525 |
|
| 526 |
// Add in the contribution of a[i] * a[i] for all i. |
| 527 |
bn_sqr_words(tmp, a, n); |
| 528 |
bn_add_words(r, r, tmp, max); |
| 529 |
} |
| 530 |
|
| 531 |
// bn_sqr_recursive sets |r| to |a|^2, using |t| as scratch space. |r| has |
| 532 |
// length 2*|n2|, |a| has length |n2|, and |t| has length 4*|n2|. |n2| must be |
| 533 |
// a power of two. |
| 534 |
static void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, size_t n2, |
| 535 |
BN_ULONG *t) { |
| 536 |
// |n2| is a power of two. |
| 537 |
assert(n2 != 0 && (n2 & (n2 - 1)) == 0); |
| 538 |
|
| 539 |
if (n2 == 4) { |
| 540 |
bn_sqr_comba4(r, a); |
| 541 |
return; |
| 542 |
} |
| 543 |
if (n2 == 8) { |
| 544 |
bn_sqr_comba8(r, a); |
| 545 |
return; |
| 546 |
} |
| 547 |
if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL) { |
| 548 |
bn_sqr_normal(r, a, n2, t); |
| 549 |
return; |
| 550 |
} |
| 551 |
|
| 552 |
// Split |a| into a0,a1, each of size |n|. |
| 553 |
// Split |t| into t0,t1,t2,t3, each of size |n|, with the remaining 4*|n| used |
| 554 |
// for recursive calls. |
| 555 |
// Split |r| into r0,r1,r2,r3. We must contribute a0^2 to r0,r1, 2*a0*a1 to |
| 556 |
// r1,r2, and a1^2 to r2,r3. |
| 557 |
size_t n = n2 / 2; |
| 558 |
BN_ULONG *t_recursive = &t[n2 * 2]; |
| 559 |
|
| 560 |
// t0 = |a0 - a1|. |
| 561 |
bn_abs_sub_words(t, a, &a[n], n, &t[n]); |
| 562 |
// t2,t3 = t0^2 = |a0 - a1|^2 = a0^2 - 2*a0*a1 + a1^2 |
| 563 |
bn_sqr_recursive(&t[n2], t, n, t_recursive); |
| 564 |
|
| 565 |
// r0,r1 = a0^2 |
| 566 |
bn_sqr_recursive(r, a, n, t_recursive); |
| 567 |
|
| 568 |
// r2,r3 = a1^2 |
| 569 |
bn_sqr_recursive(&r[n2], &a[n], n, t_recursive); |
| 570 |
|
| 571 |
// t0,t1,c = r0,r1 + r2,r3 = a0^2 + a1^2 |
| 572 |
BN_ULONG c = bn_add_words(t, r, &r[n2], n2); |
| 573 |
// t2,t3,c = t0,t1,c - t2,t3 = 2*a0*a1 |
| 574 |
c -= bn_sub_words(&t[n2], t, &t[n2], n2); |
| 575 |
|
| 576 |
// We now have our three components. Add them together. |
| 577 |
// r1,r2,c = r1,r2 + t2,t3,c |
| 578 |
c += bn_add_words(&r[n], &r[n], &t[n2], n2); |
| 579 |
|
| 580 |
// Propagate the carry bit to the end. |
| 581 |
for (size_t i = n + n2; i < n2 + n2; i++) { |
| 582 |
BN_ULONG old = r[i]; |
| 583 |
r[i] = old + c; |
| 584 |
c = r[i] < old; |
| 585 |
} |
| 586 |
|
| 587 |
// The square should fit without carries. |
| 588 |
assert(c == 0); |
| 589 |
} |
| 590 |
|
| 591 |
int BN_mul_word(BIGNUM *bn, BN_ULONG w) { |
| 592 |
if (!bn->width) { |
| 593 |
return 1; |
| 594 |
} |
| 595 |
|
| 596 |
if (w == 0) { |
| 597 |
BN_zero(bn); |
| 598 |
return 1; |
| 599 |
} |
| 600 |
|
| 601 |
BN_ULONG ll = bn_mul_words(bn->d, bn->d, bn->width, w); |
| 602 |
if (ll) { |
| 603 |
if (!bn_wexpand(bn, bn->width + 1)) { |
| 604 |
return 0; |
| 605 |
} |
| 606 |
bn->d[bn->width++] = ll; |
| 607 |
} |
| 608 |
|
| 609 |
return 1; |
| 610 |
} |
| 611 |
|
| 612 |
int bn_sqr_consttime(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) { |
| 613 |
int al = a->width; |
| 614 |
if (al <= 0) { |
| 615 |
r->width = 0; |
| 616 |
r->neg = 0; |
| 617 |
return 1; |
| 618 |
} |
| 619 |
|
| 620 |
int ret = 0; |
| 621 |
BN_CTX_start(ctx); |
| 622 |
BIGNUM *rr = (a != r) ? r : BN_CTX_get(ctx); |
| 623 |
BIGNUM *tmp = BN_CTX_get(ctx); |
| 624 |
if (!rr || !tmp) { |
| 625 |
goto err; |
| 626 |
} |
| 627 |
|
| 628 |
int max = 2 * al; // Non-zero (from above) |
| 629 |
if (!bn_wexpand(rr, max)) { |
| 630 |
goto err; |
| 631 |
} |
| 632 |
|
| 633 |
if (al == 4) { |
| 634 |
bn_sqr_comba4(rr->d, a->d); |
| 635 |
} else if (al == 8) { |
| 636 |
bn_sqr_comba8(rr->d, a->d); |
| 637 |
} else { |
| 638 |
if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) { |
| 639 |
BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2]; |
| 640 |
bn_sqr_normal(rr->d, a->d, al, t); |
| 641 |
} else { |
| 642 |
// If |al| is a power of two, we can use |bn_sqr_recursive|. |
| 643 |
if (al != 0 && (al & (al - 1)) == 0) { |
| 644 |
if (!bn_wexpand(tmp, al * 4)) { |
| 645 |
goto err; |
| 646 |
} |
| 647 |
bn_sqr_recursive(rr->d, a->d, al, tmp->d); |
| 648 |
} else { |
| 649 |
if (!bn_wexpand(tmp, max)) { |
| 650 |
goto err; |
| 651 |
} |
| 652 |
bn_sqr_normal(rr->d, a->d, al, tmp->d); |
| 653 |
} |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
rr->neg = 0; |
| 658 |
rr->width = max; |
| 659 |
|
| 660 |
if (rr != r && !BN_copy(r, rr)) { |
| 661 |
goto err; |
| 662 |
} |
| 663 |
ret = 1; |
| 664 |
|
| 665 |
err: |
| 666 |
BN_CTX_end(ctx); |
| 667 |
return ret; |
| 668 |
} |
| 669 |
|
| 670 |
int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) { |
| 671 |
if (!bn_sqr_consttime(r, a, ctx)) { |
| 672 |
return 0; |
| 673 |
} |
| 674 |
|
| 675 |
bn_set_minimal_width(r); |
| 676 |
return 1; |
| 677 |
} |
| 678 |
|
| 679 |
void bn_sqr_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a) { |
| 680 |
if (num_r != 2 * num_a || num_a > BN_SMALL_MAX_WORDS) { |
| 681 |
abort(); |
| 682 |
} |
| 683 |
if (num_a == 4) { |
| 684 |
bn_sqr_comba4(r, a); |
| 685 |
} else if (num_a == 8) { |
| 686 |
bn_sqr_comba8(r, a); |
| 687 |
} else { |
| 688 |
BN_ULONG tmp[2 * BN_SMALL_MAX_WORDS]; |
| 689 |
bn_sqr_normal(r, a, num_a, tmp); |
| 690 |
OPENSSL_cleanse(tmp, 2 * num_a * sizeof(BN_ULONG)); |
| 691 |
} |
| 692 |
} |