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