blob: 352b7e5f3daca4f7e7c7e2325b30e3b47d96d169 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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 <string.h>
61
Robert Sloan99319a12017-11-27 10:32:46 -080062#include <openssl/err.h>
63#include <openssl/mem.h>
Robert Sloanab8b8882018-03-26 11:39:51 -070064#include <openssl/type_check.h>
Robert Sloan99319a12017-11-27 10:32:46 -080065
Adam Langleyd9e397b2015-01-22 14:27:53 -080066#include "internal.h"
Robert Sloan99319a12017-11-27 10:32:46 -080067#include "../../internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -080068
69
David Benjamin4969cc92016-04-22 15:02:23 -040070#define BN_MUL_RECURSIVE_SIZE_NORMAL 16
71#define BN_SQR_RECURSIVE_SIZE_NORMAL BN_MUL_RECURSIVE_SIZE_NORMAL
72
73
Robert Sloanab8b8882018-03-26 11:39:51 -070074static 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
Robert Sloan99319a12017-11-27 10:32:46 -080081static void bn_mul_normal(BN_ULONG *r, const BN_ULONG *a, size_t na,
82 const BN_ULONG *b, size_t nb) {
Adam Langleyd9e397b2015-01-22 14:27:53 -080083 if (na < nb) {
Robert Sloan99319a12017-11-27 10:32:46 -080084 size_t itmp = na;
Adam Langleyd9e397b2015-01-22 14:27:53 -080085 na = nb;
86 nb = itmp;
Robert Sloan99319a12017-11-27 10:32:46 -080087 const BN_ULONG *ltmp = a;
Adam Langleyd9e397b2015-01-22 14:27:53 -080088 a = b;
89 b = ltmp;
90 }
Robert Sloan99319a12017-11-27 10:32:46 -080091 BN_ULONG *rr = &(r[na]);
92 if (nb == 0) {
93 OPENSSL_memset(r, 0, na * sizeof(BN_ULONG));
Adam Langleyd9e397b2015-01-22 14:27:53 -080094 return;
Adam Langleyd9e397b2015-01-22 14:27:53 -080095 }
Robert Sloan99319a12017-11-27 10:32:46 -080096 rr[0] = bn_mul_words(r, a, na, b[0]);
Adam Langleyd9e397b2015-01-22 14:27:53 -080097
98 for (;;) {
Robert Sloan99319a12017-11-27 10:32:46 -080099 if (--nb == 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800100 return;
101 }
102 rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);
Robert Sloan99319a12017-11-27 10:32:46 -0800103 if (--nb == 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800104 return;
105 }
106 rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);
Robert Sloan99319a12017-11-27 10:32:46 -0800107 if (--nb == 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800108 return;
109 }
110 rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);
Robert Sloan99319a12017-11-27 10:32:46 -0800111 if (--nb == 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800112 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
Adam Langleyd9e397b2015-01-22 14:27:53 -0800121#if !defined(OPENSSL_X86) || defined(OPENSSL_NO_ASM)
Robert Sloan8f860b12017-08-28 07:37:06 -0700122// Here follows specialised variants of bn_add_words() and bn_sub_words(). They
123// have the property performing operations on arrays of different sizes. The
124// sizes of those arrays is expressed through cl, which is the common length (
125// basicall, min(len(a),len(b)) ), and dl, which is the delta between the two
126// lengths, calculated as len(a)-len(b). All lengths are the number of
127// BN_ULONGs... For the operations that require a result array as parameter,
128// it must have the length cl+abs(dl). These functions should probably end up
129// in bn_asm.c as soon as there are assembler counterparts for the systems that
130// use assembler files.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800131
132static BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a,
133 const BN_ULONG *b, int cl, int dl) {
134 BN_ULONG c, t;
135
136 assert(cl >= 0);
137 c = bn_sub_words(r, a, b, cl);
138
Adam Langleye9ada862015-05-11 17:20:37 -0700139 if (dl == 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800140 return c;
Adam Langleye9ada862015-05-11 17:20:37 -0700141 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800142
143 r += cl;
144 a += cl;
145 b += cl;
146
147 if (dl < 0) {
148 for (;;) {
149 t = b[0];
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700150 r[0] = 0 - t - c;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800151 if (t != 0) {
152 c = 1;
153 }
154 if (++dl >= 0) {
155 break;
156 }
157
158 t = b[1];
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700159 r[1] = 0 - t - c;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800160 if (t != 0) {
161 c = 1;
162 }
163 if (++dl >= 0) {
164 break;
165 }
166
167 t = b[2];
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700168 r[2] = 0 - t - c;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800169 if (t != 0) {
170 c = 1;
171 }
172 if (++dl >= 0) {
173 break;
174 }
175
176 t = b[3];
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700177 r[3] = 0 - t - c;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800178 if (t != 0) {
179 c = 1;
180 }
181 if (++dl >= 0) {
182 break;
183 }
184
185 b += 4;
186 r += 4;
187 }
188 } else {
189 int save_dl = dl;
190 while (c) {
191 t = a[0];
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700192 r[0] = t - c;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800193 if (t != 0) {
194 c = 0;
195 }
196 if (--dl <= 0) {
197 break;
198 }
199
200 t = a[1];
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700201 r[1] = t - c;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800202 if (t != 0) {
203 c = 0;
204 }
205 if (--dl <= 0) {
206 break;
207 }
208
209 t = a[2];
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700210 r[2] = t - c;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800211 if (t != 0) {
212 c = 0;
213 }
214 if (--dl <= 0) {
215 break;
216 }
217
218 t = a[3];
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700219 r[3] = t - c;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800220 if (t != 0) {
221 c = 0;
222 }
223 if (--dl <= 0) {
224 break;
225 }
226
227 save_dl = dl;
228 a += 4;
229 r += 4;
230 }
231 if (dl > 0) {
232 if (save_dl > dl) {
233 switch (save_dl - dl) {
234 case 1:
235 r[1] = a[1];
236 if (--dl <= 0) {
237 break;
238 }
Robert Sloan2e9e66a2017-09-25 09:08:29 -0700239 OPENSSL_FALLTHROUGH;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800240 case 2:
241 r[2] = a[2];
242 if (--dl <= 0) {
243 break;
244 }
Robert Sloan2e9e66a2017-09-25 09:08:29 -0700245 OPENSSL_FALLTHROUGH;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800246 case 3:
247 r[3] = a[3];
248 if (--dl <= 0) {
249 break;
250 }
251 }
252 a += 4;
253 r += 4;
254 }
255 }
256
257 if (dl > 0) {
258 for (;;) {
259 r[0] = a[0];
260 if (--dl <= 0) {
261 break;
262 }
263 r[1] = a[1];
264 if (--dl <= 0) {
265 break;
266 }
267 r[2] = a[2];
268 if (--dl <= 0) {
269 break;
270 }
271 r[3] = a[3];
272 if (--dl <= 0) {
273 break;
274 }
275
276 a += 4;
277 r += 4;
278 }
279 }
280 }
281
282 return c;
283}
284#else
Robert Sloan8f860b12017-08-28 07:37:06 -0700285// On other platforms the function is defined in asm.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800286BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
287 int cl, int dl);
288#endif
289
Robert Sloanab8b8882018-03-26 11:39:51 -0700290// bn_abs_sub_part_words computes |r| = |a| - |b|, storing the absolute value
291// and returning a mask of all ones if the result was negative and all zeros if
292// the result was positive. |cl| and |dl| follow the |bn_sub_part_words| calling
293// convention.
294//
295// TODO(davidben): Make this take |size_t|. The |cl| + |dl| calling convention
296// is confusing. The trouble is 32-bit x86 implements |bn_sub_part_words| in
297// assembly, but we can probably just delete it?
298static BN_ULONG bn_abs_sub_part_words(BN_ULONG *r, const BN_ULONG *a,
299 const BN_ULONG *b, int cl, int dl,
300 BN_ULONG *tmp) {
301 BN_ULONG borrow = bn_sub_part_words(tmp, a, b, cl, dl);
302 bn_sub_part_words(r, b, a, cl, -dl);
303 int r_len = cl + (dl < 0 ? -dl : dl);
304 borrow = 0 - borrow;
305 bn_select_words(r, borrow, r /* tmp < 0 */, tmp /* tmp >= 0 */, r_len);
306 return borrow;
307}
308
Robert Sloan8f860b12017-08-28 07:37:06 -0700309// Karatsuba recursive multiplication algorithm
310// (cf. Knuth, The Art of Computer Programming, Vol. 2)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800311
Robert Sloanab8b8882018-03-26 11:39:51 -0700312// bn_mul_recursive sets |r| to |a| * |b|, using |t| as scratch space. |r| has
313// length 2*|n2|, |a| has length |n2| + |dna|, |b| has length |n2| + |dnb|, and
314// |t| has length 4*|n2|. |n2| must be a power of two. Finally, we must have
315// -|BN_MUL_RECURSIVE_SIZE_NORMAL|/2 <= |dna| <= 0 and
316// -|BN_MUL_RECURSIVE_SIZE_NORMAL|/2 <= |dnb| <= 0.
317//
318// TODO(davidben): Simplify and |size_t| the calling convention around lengths
319// here.
Robert Sloan99319a12017-11-27 10:32:46 -0800320static void bn_mul_recursive(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
321 int n2, int dna, int dnb, BN_ULONG *t) {
Robert Sloanab8b8882018-03-26 11:39:51 -0700322 // |n2| is a power of two.
323 assert(n2 != 0 && (n2 & (n2 - 1)) == 0);
324 // Check |dna| and |dnb| are in range.
325 assert(-BN_MUL_RECURSIVE_SIZE_NORMAL/2 <= dna && dna <= 0);
326 assert(-BN_MUL_RECURSIVE_SIZE_NORMAL/2 <= dnb && dnb <= 0);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800327
Robert Sloan8f860b12017-08-28 07:37:06 -0700328 // Only call bn_mul_comba 8 if n2 == 8 and the
329 // two arrays are complete [steve]
Adam Langleyd9e397b2015-01-22 14:27:53 -0800330 if (n2 == 8 && dna == 0 && dnb == 0) {
331 bn_mul_comba8(r, a, b);
332 return;
333 }
334
Robert Sloan8f860b12017-08-28 07:37:06 -0700335 // Else do normal multiply
Adam Langleyd9e397b2015-01-22 14:27:53 -0800336 if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) {
337 bn_mul_normal(r, a, n2 + dna, b, n2 + dnb);
Robert Sloanab8b8882018-03-26 11:39:51 -0700338 if (dna + dnb < 0) {
Robert Sloan69939df2017-01-09 10:53:07 -0800339 OPENSSL_memset(&r[2 * n2 + dna + dnb], 0,
340 sizeof(BN_ULONG) * -(dna + dnb));
Adam Langleye9ada862015-05-11 17:20:37 -0700341 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800342 return;
343 }
344
Robert Sloanab8b8882018-03-26 11:39:51 -0700345 // Split |a| and |b| into a0,a1 and b0,b1, where a0 and b0 have size |n|.
346 // Split |t| into t0,t1,t2,t3, each of size |n|, with the remaining 4*|n| used
347 // for recursive calls.
348 // Split |r| into r0,r1,r2,r3. We must contribute a0*b0 to r0,r1, a0*a1+b0*b1
349 // to r1,r2, and a1*b1 to r2,r3. The middle term we will compute as:
350 //
351 // a0*a1 + b0*b1 = (a0 - a1)*(b1 - b0) + a1*b1 + a0*b0
352 //
353 // Note that we know |n| >= |BN_MUL_RECURSIVE_SIZE_NORMAL|/2 above, so
354 // |tna| and |tnb| are non-negative.
355 int n = n2 / 2, tna = n + dna, tnb = n + dnb;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800356
Robert Sloanab8b8882018-03-26 11:39:51 -0700357 // t0 = a0 - a1 and t1 = b1 - b0. The result will be multiplied, so we XOR
358 // their sign masks, giving the sign of (a0 - a1)*(b1 - b0). t0 and t1
359 // themselves store the absolute value.
360 BN_ULONG neg = bn_abs_sub_part_words(t, a, &a[n], tna, n - tna, &t[n2]);
361 neg ^= bn_abs_sub_part_words(&t[n], &b[n], b, tnb, tnb - n, &t[n2]);
362
363 // Compute:
364 // t2,t3 = t0 * t1 = |(a0 - a1)*(b1 - b0)|
365 // r0,r1 = a0 * b0
366 // r2,r3 = a1 * b1
Adam Langleyd9e397b2015-01-22 14:27:53 -0800367 if (n == 4 && dna == 0 && dnb == 0) {
Robert Sloanab8b8882018-03-26 11:39:51 -0700368 bn_mul_comba4(&t[n2], t, &t[n]);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800369
370 bn_mul_comba4(r, a, b);
Robert Sloanab8b8882018-03-26 11:39:51 -0700371 bn_mul_comba4(&r[n2], &a[n], &b[n]);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800372 } else if (n == 8 && dna == 0 && dnb == 0) {
Robert Sloanab8b8882018-03-26 11:39:51 -0700373 bn_mul_comba8(&t[n2], t, &t[n]);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800374
375 bn_mul_comba8(r, a, b);
Robert Sloanab8b8882018-03-26 11:39:51 -0700376 bn_mul_comba8(&r[n2], &a[n], &b[n]);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800377 } else {
Robert Sloanab8b8882018-03-26 11:39:51 -0700378 BN_ULONG *p = &t[n2 * 2];
379 bn_mul_recursive(&t[n2], t, &t[n], n, 0, 0, p);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800380 bn_mul_recursive(r, a, b, n, 0, 0, p);
Robert Sloanab8b8882018-03-26 11:39:51 -0700381 bn_mul_recursive(&r[n2], &a[n], &b[n], n, dna, dnb, p);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800382 }
383
Robert Sloanab8b8882018-03-26 11:39:51 -0700384 // t0,t1,c = r0,r1 + r2,r3 = a0*b0 + a1*b1
385 BN_ULONG c = bn_add_words(t, r, &r[n2], n2);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800386
Robert Sloanab8b8882018-03-26 11:39:51 -0700387 // t2,t3,c = t0,t1,c + neg*t2,t3 = (a0 - a1)*(b1 - b0) + a1*b1 + a0*b0.
388 // The second term is stored as the absolute value, so we do this with a
389 // constant-time select.
390 BN_ULONG c_neg = c - bn_sub_words(&t[n2 * 2], t, &t[n2], n2);
391 BN_ULONG c_pos = c + bn_add_words(&t[n2], t, &t[n2], n2);
392 bn_select_words(&t[n2], neg, &t[n2 * 2], &t[n2], n2);
393 OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
394 crypto_word_t_too_small);
395 c = constant_time_select_w(neg, c_neg, c_pos);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800396
Robert Sloanab8b8882018-03-26 11:39:51 -0700397 // We now have our three components. Add them together.
398 // r1,r2,c = r1,r2 + t2,t3,c
399 c += bn_add_words(&r[n], &r[n], &t[n2], n2);
400
401 // Propagate the carry bit to the end.
402 for (int i = n + n2; i < n2 + n2; i++) {
403 BN_ULONG old = r[i];
404 r[i] = old + c;
405 c = r[i] < old;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800406 }
407
Robert Sloanab8b8882018-03-26 11:39:51 -0700408 // The product should fit without carries.
409 assert(c == 0);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800410}
411
Robert Sloanab8b8882018-03-26 11:39:51 -0700412// bn_mul_part_recursive sets |r| to |a| * |b|, using |t| as scratch space. |r|
413// has length 4*|n|, |a| has length |n| + |tna|, |b| has length |n| + |tnb|, and
414// |t| has length 8*|n|. |n| must be a power of two. Additionally, we must have
415// 0 <= tna < n and 0 <= tnb < n, and |tna| and |tnb| must differ by at most
416// one.
417//
418// TODO(davidben): Make this take |size_t| and perhaps the actual lengths of |a|
419// and |b|.
Robert Sloan99319a12017-11-27 10:32:46 -0800420static void bn_mul_part_recursive(BN_ULONG *r, const BN_ULONG *a,
421 const BN_ULONG *b, int n, int tna, int tnb,
422 BN_ULONG *t) {
Robert Sloanab8b8882018-03-26 11:39:51 -0700423 // |n| is a power of two.
424 assert(n != 0 && (n & (n - 1)) == 0);
425 // Check |tna| and |tnb| are in range.
426 assert(0 <= tna && tna < n);
427 assert(0 <= tnb && tnb < n);
428 assert(-1 <= tna - tnb && tna - tnb <= 1);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800429
Robert Sloanab8b8882018-03-26 11:39:51 -0700430 int n2 = n * 2;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800431 if (n < 8) {
432 bn_mul_normal(r, a, n + tna, b, n + tnb);
Robert Sloanab8b8882018-03-26 11:39:51 -0700433 OPENSSL_memset(r + n2 + tna + tnb, 0, n2 - tna - tnb);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800434 return;
435 }
436
Robert Sloanab8b8882018-03-26 11:39:51 -0700437 // Split |a| and |b| into a0,a1 and b0,b1, where a0 and b0 have size |n|. |a1|
438 // and |b1| have size |tna| and |tnb|, respectively.
439 // Split |t| into t0,t1,t2,t3, each of size |n|, with the remaining 4*|n| used
440 // for recursive calls.
441 // Split |r| into r0,r1,r2,r3. We must contribute a0*b0 to r0,r1, a0*a1+b0*b1
442 // to r1,r2, and a1*b1 to r2,r3. The middle term we will compute as:
443 //
444 // a0*a1 + b0*b1 = (a0 - a1)*(b1 - b0) + a1*b1 + a0*b0
Adam Langleyd9e397b2015-01-22 14:27:53 -0800445
Robert Sloanab8b8882018-03-26 11:39:51 -0700446 // t0 = a0 - a1 and t1 = b1 - b0. The result will be multiplied, so we XOR
447 // their sign masks, giving the sign of (a0 - a1)*(b1 - b0). t0 and t1
448 // themselves store the absolute value.
449 BN_ULONG neg = bn_abs_sub_part_words(t, a, &a[n], tna, n - tna, &t[n2]);
450 neg ^= bn_abs_sub_part_words(&t[n], &b[n], b, tnb, tnb - n, &t[n2]);
451
452 // Compute:
453 // t2,t3 = t0 * t1 = |(a0 - a1)*(b1 - b0)|
454 // r0,r1 = a0 * b0
455 // r2,r3 = a1 * b1
Adam Langleyd9e397b2015-01-22 14:27:53 -0800456 if (n == 8) {
Robert Sloanab8b8882018-03-26 11:39:51 -0700457 bn_mul_comba8(&t[n2], t, &t[n]);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800458 bn_mul_comba8(r, a, b);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800459
Robert Sloanab8b8882018-03-26 11:39:51 -0700460 bn_mul_normal(&r[n2], &a[n], tna, &b[n], tnb);
461 // |bn_mul_normal| only writes |tna| + |tna| words. Zero the rest.
462 OPENSSL_memset(&r[n2 + tna + tnb], 0, sizeof(BN_ULONG) * (n2 - tna - tnb));
463 } else {
464 BN_ULONG *p = &t[n2 * 2];
465 bn_mul_recursive(&t[n2], t, &t[n], n, 0, 0, p);
466 bn_mul_recursive(r, a, b, n, 0, 0, p);
467
468 OPENSSL_memset(&r[n2], 0, sizeof(BN_ULONG) * n2);
469 if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL &&
470 tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {
471 bn_mul_normal(&r[n2], &a[n], tna, &b[n], tnb);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800472 } else {
Robert Sloanab8b8882018-03-26 11:39:51 -0700473 int i = n;
474 for (;;) {
475 i /= 2;
476 if (i < tna || i < tnb) {
477 // E.g., n == 16, i == 8 and tna == 11. |tna| and |tnb| are within one
478 // of each other, so if |tna| is larger and tna > i, then we know
479 // tnb >= i, and this call is valid.
480 bn_mul_part_recursive(&r[n2], &a[n], &b[n], i, tna - i, tnb - i, p);
481 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800482 }
Robert Sloanab8b8882018-03-26 11:39:51 -0700483 if (i == tna || i == tnb) {
484 // If there is only a bottom half to the number, just do it. We know
485 // the larger of |tna - i| and |tnb - i| is zero. The other is zero or
486 // -1 by because of |tna| and |tnb| differ by at most one.
487 bn_mul_recursive(&r[n2], &a[n], &b[n], i, tna - i, tnb - i, p);
488 break;
489 }
490
491 // This loop will eventually terminate when |i| falls below
492 // |BN_MUL_RECURSIVE_SIZE_NORMAL| because we know one of |tna| and |tnb|
493 // exceeds that.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800494 }
495 }
496 }
497
Robert Sloanab8b8882018-03-26 11:39:51 -0700498 // t0,t1,c = r0,r1 + r2,r3 = a0*b0 + a1*b1
499 BN_ULONG c = bn_add_words(t, r, &r[n2], n2);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800500
Robert Sloanab8b8882018-03-26 11:39:51 -0700501 // t2,t3,c = t0,t1,c + neg*t2,t3 = (a0 - a1)*(b1 - b0) + a1*b1 + a0*b0.
502 // The second term is stored as the absolute value, so we do this with a
503 // constant-time select.
504 BN_ULONG c_neg = c - bn_sub_words(&t[n2 * 2], t, &t[n2], n2);
505 BN_ULONG c_pos = c + bn_add_words(&t[n2], t, &t[n2], n2);
506 bn_select_words(&t[n2], neg, &t[n2 * 2], &t[n2], n2);
507 OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
508 crypto_word_t_too_small);
509 c = constant_time_select_w(neg, c_neg, c_pos);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800510
Robert Sloanab8b8882018-03-26 11:39:51 -0700511 // We now have our three components. Add them together.
512 // r1,r2,c = r1,r2 + t2,t3,c
513 c += bn_add_words(&r[n], &r[n], &t[n2], n2);
514
515 // Propagate the carry bit to the end.
516 for (int i = n + n2; i < n2 + n2; i++) {
517 BN_ULONG old = r[i];
518 r[i] = old + c;
519 c = r[i] < old;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800520 }
521
Robert Sloanab8b8882018-03-26 11:39:51 -0700522 // The product should fit without carries.
523 assert(c == 0);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800524}
525
Robert Sloanab8b8882018-03-26 11:39:51 -0700526// bn_mul_impl implements |BN_mul| and |bn_mul_fixed|. Note this function breaks
527// |BIGNUM| invariants and may return a negative zero. This is handled by the
528// callers.
529static int bn_mul_impl(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
530 BN_CTX *ctx) {
531 int al = a->width;
532 int bl = b->width;
533 if (al == 0 || bl == 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800534 BN_zero(r);
535 return 1;
536 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800537
Robert Sloanab8b8882018-03-26 11:39:51 -0700538 int ret = 0;
539 BIGNUM *rr;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800540 BN_CTX_start(ctx);
Robert Sloanab8b8882018-03-26 11:39:51 -0700541 if (r == a || r == b) {
542 rr = BN_CTX_get(ctx);
543 if (r == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800544 goto err;
545 }
546 } else {
547 rr = r;
548 }
549 rr->neg = a->neg ^ b->neg;
550
Robert Sloanab8b8882018-03-26 11:39:51 -0700551 int i = al - bl;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800552 if (i == 0) {
553 if (al == 8) {
Robert Sloan9254e682017-04-24 09:42:06 -0700554 if (!bn_wexpand(rr, 16)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800555 goto err;
556 }
Robert Sloanab8b8882018-03-26 11:39:51 -0700557 rr->width = 16;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800558 bn_mul_comba8(rr->d, a->d, b->d);
559 goto end;
560 }
561 }
562
Robert Sloanab8b8882018-03-26 11:39:51 -0700563 int top = al + bl;
David Benjamin4969cc92016-04-22 15:02:23 -0400564 static const int kMulNormalSize = 16;
565 if (al >= kMulNormalSize && bl >= kMulNormalSize) {
Robert Sloanab8b8882018-03-26 11:39:51 -0700566 if (-1 <= i && i <= 1) {
567 // Find the larger power of two less than or equal to the larger length.
568 int j;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800569 if (i >= 0) {
570 j = BN_num_bits_word((BN_ULONG)al);
Robert Sloanab8b8882018-03-26 11:39:51 -0700571 } else {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800572 j = BN_num_bits_word((BN_ULONG)bl);
573 }
574 j = 1 << (j - 1);
575 assert(j <= al || j <= bl);
Robert Sloanab8b8882018-03-26 11:39:51 -0700576 BIGNUM *t = BN_CTX_get(ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800577 if (t == NULL) {
578 goto err;
579 }
580 if (al > j || bl > j) {
Robert Sloanab8b8882018-03-26 11:39:51 -0700581 // We know |al| and |bl| are at most one from each other, so if al > j,
582 // bl >= j, and vice versa. Thus we can use |bn_mul_part_recursive|.
583 assert(al >= j && bl >= j);
584 if (!bn_wexpand(t, j * 8) ||
585 !bn_wexpand(rr, j * 4)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800586 goto err;
587 }
588 bn_mul_part_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
589 } else {
Robert Sloanab8b8882018-03-26 11:39:51 -0700590 // al <= j && bl <= j. Additionally, we know j <= al or j <= bl, so one
591 // of al - j or bl - j is zero. The other, by the bound on |i| above, is
592 // zero or -1. Thus, we can use |bn_mul_recursive|.
593 if (!bn_wexpand(t, j * 4) ||
594 !bn_wexpand(rr, j * 2)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800595 goto err;
596 }
597 bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
598 }
Robert Sloanab8b8882018-03-26 11:39:51 -0700599 rr->width = top;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800600 goto end;
601 }
602 }
603
Robert Sloan9254e682017-04-24 09:42:06 -0700604 if (!bn_wexpand(rr, top)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800605 goto err;
606 }
Robert Sloanab8b8882018-03-26 11:39:51 -0700607 rr->width = top;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800608 bn_mul_normal(rr->d, a->d, al, b->d, bl);
609
610end:
Kenny Rootb8494592015-09-25 02:29:14 +0000611 if (r != rr && !BN_copy(r, rr)) {
612 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800613 }
614 ret = 1;
615
616err:
617 BN_CTX_end(ctx);
618 return ret;
619}
620
Robert Sloanab8b8882018-03-26 11:39:51 -0700621int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
622 if (!bn_mul_impl(r, a, b, ctx)) {
623 return 0;
624 }
625
626 // This additionally fixes any negative zeros created by |bn_mul_impl|.
627 bn_set_minimal_width(r);
628 return 1;
629}
630
631int bn_mul_fixed(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
632 // Prevent negative zeros.
633 if (a->neg || b->neg) {
634 OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
635 return 0;
636 }
637
638 return bn_mul_impl(r, a, b, ctx);
639}
640
Robert Sloan99319a12017-11-27 10:32:46 -0800641int bn_mul_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a,
642 const BN_ULONG *b, size_t num_b) {
643 if (num_r != num_a + num_b) {
644 OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
645 return 0;
646 }
647 // TODO(davidben): Should this call |bn_mul_comba4| too? |BN_mul| does not
648 // hit that code.
649 if (num_a == 8 && num_b == 8) {
650 bn_mul_comba8(r, a, b);
651 } else {
652 bn_mul_normal(r, a, num_a, b, num_b);
653 }
654 return 1;
655}
656
Robert Sloan8f860b12017-08-28 07:37:06 -0700657// tmp must have 2*n words
Robert Sloan99319a12017-11-27 10:32:46 -0800658static void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, size_t n,
Robert Sloand5c22152017-11-13 09:22:12 -0800659 BN_ULONG *tmp) {
Robert Sloan99319a12017-11-27 10:32:46 -0800660 if (n == 0) {
661 return;
662 }
663
664 size_t max = n * 2;
Robert Sloand5c22152017-11-13 09:22:12 -0800665 const BN_ULONG *ap = a;
666 BN_ULONG *rp = r;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800667 rp[0] = rp[max - 1] = 0;
668 rp++;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800669
Robert Sloand5c22152017-11-13 09:22:12 -0800670 // Compute the contribution of a[i] * a[j] for all i < j.
Robert Sloan99319a12017-11-27 10:32:46 -0800671 if (n > 1) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800672 ap++;
Robert Sloan99319a12017-11-27 10:32:46 -0800673 rp[n - 1] = bn_mul_words(rp, ap, n - 1, ap[-1]);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800674 rp += 2;
675 }
Robert Sloan99319a12017-11-27 10:32:46 -0800676 if (n > 2) {
677 for (size_t i = n - 2; i > 0; i--) {
678 ap++;
679 rp[i] = bn_mul_add_words(rp, ap, i, ap[-1]);
680 rp += 2;
681 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800682 }
683
Robert Sloand5c22152017-11-13 09:22:12 -0800684 // The final result fits in |max| words, so none of the following operations
685 // will overflow.
686
687 // Double |r|, giving the contribution of a[i] * a[j] for all i != j.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800688 bn_add_words(r, r, r, max);
689
Robert Sloand5c22152017-11-13 09:22:12 -0800690 // Add in the contribution of a[i] * a[i] for all i.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800691 bn_sqr_words(tmp, a, n);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800692 bn_add_words(r, r, tmp, max);
693}
694
Robert Sloanab8b8882018-03-26 11:39:51 -0700695// bn_sqr_recursive sets |r| to |a|^2, using |t| as scratch space. |r| has
696// length 2*|n2|, |a| has length |n2|, and |t| has length 4*|n2|. |n2| must be
697// a power of two.
698static void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, size_t n2,
Robert Sloand5c22152017-11-13 09:22:12 -0800699 BN_ULONG *t) {
Robert Sloanab8b8882018-03-26 11:39:51 -0700700 // |n2| is a power of two.
701 assert(n2 != 0 && (n2 & (n2 - 1)) == 0);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800702
703 if (n2 == 4) {
704 bn_sqr_comba4(r, a);
705 return;
Robert Sloanab8b8882018-03-26 11:39:51 -0700706 }
707 if (n2 == 8) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800708 bn_sqr_comba8(r, a);
709 return;
710 }
711 if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL) {
712 bn_sqr_normal(r, a, n2, t);
713 return;
714 }
Robert Sloanab8b8882018-03-26 11:39:51 -0700715
716 // Split |a| into a0,a1, each of size |n|.
717 // Split |t| into t0,t1,t2,t3, each of size |n|, with the remaining 4*|n| used
718 // for recursive calls.
719 // Split |r| into r0,r1,r2,r3. We must contribute a0^2 to r0,r1, 2*a0*a1 to
720 // r1,r2, and a1^2 to r2,r3.
721 size_t n = n2 / 2;
722 BN_ULONG *t_recursive = &t[n2 * 2];
723
724 // t0 = |a0 - a1|.
725 bn_abs_sub_words(t, a, &a[n], n, &t[n]);
726 // t2,t3 = t0^2 = |a0 - a1|^2 = a0^2 - 2*a0*a1 + a1^2
727 bn_sqr_recursive(&t[n2], t, n, t_recursive);
728
729 // r0,r1 = a0^2
730 bn_sqr_recursive(r, a, n, t_recursive);
731
732 // r2,r3 = a1^2
733 bn_sqr_recursive(&r[n2], &a[n], n, t_recursive);
734
735 // t0,t1,c = r0,r1 + r2,r3 = a0^2 + a1^2
736 BN_ULONG c = bn_add_words(t, r, &r[n2], n2);
737 // t2,t3,c = t0,t1,c - t2,t3 = 2*a0*a1
738 c -= bn_sub_words(&t[n2], t, &t[n2], n2);
739
740 // We now have our three components. Add them together.
741 // r1,r2,c = r1,r2 + t2,t3,c
742 c += bn_add_words(&r[n], &r[n], &t[n2], n2);
743
744 // Propagate the carry bit to the end.
745 for (size_t i = n + n2; i < n2 + n2; i++) {
746 BN_ULONG old = r[i];
747 r[i] = old + c;
748 c = r[i] < old;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800749 }
750
Robert Sloanab8b8882018-03-26 11:39:51 -0700751 // The square should fit without carries.
752 assert(c == 0);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800753}
754
755int BN_mul_word(BIGNUM *bn, BN_ULONG w) {
Robert Sloanab8b8882018-03-26 11:39:51 -0700756 if (!bn->width) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800757 return 1;
758 }
759
760 if (w == 0) {
761 BN_zero(bn);
762 return 1;
763 }
764
Robert Sloanab8b8882018-03-26 11:39:51 -0700765 BN_ULONG ll = bn_mul_words(bn->d, bn->d, bn->width, w);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800766 if (ll) {
Robert Sloanab8b8882018-03-26 11:39:51 -0700767 if (!bn_wexpand(bn, bn->width + 1)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800768 return 0;
769 }
Robert Sloanab8b8882018-03-26 11:39:51 -0700770 bn->d[bn->width++] = ll;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800771 }
772
773 return 1;
774}
775
Robert Sloanab8b8882018-03-26 11:39:51 -0700776int bn_sqr_fixed(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) {
777 int al = a->width;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800778 if (al <= 0) {
Robert Sloanab8b8882018-03-26 11:39:51 -0700779 r->width = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800780 r->neg = 0;
781 return 1;
782 }
783
Robert Sloanab8b8882018-03-26 11:39:51 -0700784 int ret = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800785 BN_CTX_start(ctx);
Robert Sloanab8b8882018-03-26 11:39:51 -0700786 BIGNUM *rr = (a != r) ? r : BN_CTX_get(ctx);
787 BIGNUM *tmp = BN_CTX_get(ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800788 if (!rr || !tmp) {
789 goto err;
790 }
791
Robert Sloanab8b8882018-03-26 11:39:51 -0700792 int max = 2 * al; // Non-zero (from above)
Robert Sloan9254e682017-04-24 09:42:06 -0700793 if (!bn_wexpand(rr, max)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800794 goto err;
795 }
796
797 if (al == 4) {
798 bn_sqr_comba4(rr->d, a->d);
799 } else if (al == 8) {
800 bn_sqr_comba8(rr->d, a->d);
801 } else {
802 if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {
803 BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];
804 bn_sqr_normal(rr->d, a->d, al, t);
805 } else {
Robert Sloanab8b8882018-03-26 11:39:51 -0700806 // If |al| is a power of two, we can use |bn_sqr_recursive|.
807 if (al != 0 && (al & (al - 1)) == 0) {
808 if (!bn_wexpand(tmp, al * 4)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800809 goto err;
810 }
811 bn_sqr_recursive(rr->d, a->d, al, tmp->d);
812 } else {
Robert Sloan9254e682017-04-24 09:42:06 -0700813 if (!bn_wexpand(tmp, max)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800814 goto err;
815 }
816 bn_sqr_normal(rr->d, a->d, al, tmp->d);
817 }
818 }
819 }
820
821 rr->neg = 0;
Robert Sloanab8b8882018-03-26 11:39:51 -0700822 rr->width = max;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800823
Kenny Rootb8494592015-09-25 02:29:14 +0000824 if (rr != r && !BN_copy(r, rr)) {
825 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800826 }
827 ret = 1;
828
829err:
830 BN_CTX_end(ctx);
831 return ret;
832}
Robert Sloan99319a12017-11-27 10:32:46 -0800833
Robert Sloanab8b8882018-03-26 11:39:51 -0700834int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) {
835 if (!bn_sqr_fixed(r, a, ctx)) {
836 return 0;
837 }
838
839 bn_set_minimal_width(r);
840 return 1;
841}
842
Robert Sloan99319a12017-11-27 10:32:46 -0800843int bn_sqr_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a) {
844 if (num_r != 2 * num_a || num_a > BN_SMALL_MAX_WORDS) {
845 OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
846 return 0;
847 }
848 if (num_a == 4) {
849 bn_sqr_comba4(r, a);
850 } else if (num_a == 8) {
851 bn_sqr_comba8(r, a);
852 } else {
853 BN_ULONG tmp[2 * BN_SMALL_MAX_WORDS];
854 bn_sqr_normal(r, a, num_a, tmp);
855 OPENSSL_cleanse(tmp, 2 * num_a * sizeof(BN_ULONG));
856 }
857 return 1;
858}