Debian Codegraph @

external/boringssl/crypto/bn/mul.c

raw
1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young ([email protected]).
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 ([email protected]).
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 ([email protected])"
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 ([email protected])"
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
62 #include "internal.h"
63
64
65 #define BN_MUL_RECURSIVE_SIZE_NORMAL 16
66 #define BN_SQR_RECURSIVE_SIZE_NORMAL BN_MUL_RECURSIVE_SIZE_NORMAL
67
68
69 static void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b,
70 int nb) {
71 BN_ULONG *rr;
72
73 if (na < nb) {
74 int itmp;
75 BN_ULONG *ltmp;
76
77 itmp = na;
78 na = nb;
79 nb = itmp;
80 ltmp = a;
81 a = b;
82 b = ltmp;
83 }
84 rr = &(r[na]);
85 if (nb <= 0) {
86 (void)bn_mul_words(r, a, na, 0);
87 return;
88 } else {
89 rr[0] = bn_mul_words(r, a, na, b[0]);
90 }
91
92 for (;;) {
93 if (--nb <= 0) {
94 return;
95 }
96 rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);
97 if (--nb <= 0) {
98 return;
99 }
100 rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);
101 if (--nb <= 0) {
102 return;
103 }
104 rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);
105 if (--nb <= 0) {
106 return;
107 }
108 rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);
109 rr += 4;
110 r += 4;
111 b += 4;
112 }
113 }
114
115 #if !defined(OPENSSL_X86) || defined(OPENSSL_NO_ASM)
116 /* Here follows specialised variants of bn_add_words() and bn_sub_words(). They
117 * have the property performing operations on arrays of different sizes. The
118 * sizes of those arrays is expressed through cl, which is the common length (
119 * basicall, min(len(a),len(b)) ), and dl, which is the delta between the two
120 * lengths, calculated as len(a)-len(b). All lengths are the number of
121 * BN_ULONGs... For the operations that require a result array as parameter,
122 * it must have the length cl+abs(dl). These functions should probably end up
123 * in bn_asm.c as soon as there are assembler counterparts for the systems that
124 * use assembler files. */
125
126 static BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a,
127 const BN_ULONG *b, int cl, int dl) {
128 BN_ULONG c, t;
129
130 assert(cl >= 0);
131 c = bn_sub_words(r, a, b, cl);
132
133 if (dl == 0) {
134 return c;
135 }
136
137 r += cl;
138 a += cl;
139 b += cl;
140
141 if (dl < 0) {
142 for (;;) {
143 t = b[0];
144 r[0] = (0 - t - c) & BN_MASK2;
145 if (t != 0) {
146 c = 1;
147 }
148 if (++dl >= 0) {
149 break;
150 }
151
152 t = b[1];
153 r[1] = (0 - t - c) & BN_MASK2;
154 if (t != 0) {
155 c = 1;
156 }
157 if (++dl >= 0) {
158 break;
159 }
160
161 t = b[2];
162 r[2] = (0 - t - c) & BN_MASK2;
163 if (t != 0) {
164 c = 1;
165 }
166 if (++dl >= 0) {
167 break;
168 }
169
170 t = b[3];
171 r[3] = (0 - t - c) & BN_MASK2;
172 if (t != 0) {
173 c = 1;
174 }
175 if (++dl >= 0) {
176 break;
177 }
178
179 b += 4;
180 r += 4;
181 }
182 } else {
183 int save_dl = dl;
184 while (c) {
185 t = a[0];
186 r[0] = (t - c) & BN_MASK2;
187 if (t != 0) {
188 c = 0;
189 }
190 if (--dl <= 0) {
191 break;
192 }
193
194 t = a[1];
195 r[1] = (t - c) & BN_MASK2;
196 if (t != 0) {
197 c = 0;
198 }
199 if (--dl <= 0) {
200 break;
201 }
202
203 t = a[2];
204 r[2] = (t - c) & BN_MASK2;
205 if (t != 0) {
206 c = 0;
207 }
208 if (--dl <= 0) {
209 break;
210 }
211
212 t = a[3];
213 r[3] = (t - c) & BN_MASK2;
214 if (t != 0) {
215 c = 0;
216 }
217 if (--dl <= 0) {
218 break;
219 }
220
221 save_dl = dl;
222 a += 4;
223 r += 4;
224 }
225 if (dl > 0) {
226 if (save_dl > dl) {
227 switch (save_dl - dl) {
228 case 1:
229 r[1] = a[1];
230 if (--dl <= 0) {
231 break;
232 }
233 case 2:
234 r[2] = a[2];
235 if (--dl <= 0) {
236 break;
237 }
238 case 3:
239 r[3] = a[3];
240 if (--dl <= 0) {
241 break;
242 }
243 }
244 a += 4;
245 r += 4;
246 }
247 }
248
249 if (dl > 0) {
250 for (;;) {
251 r[0] = a[0];
252 if (--dl <= 0) {
253 break;
254 }
255 r[1] = a[1];
256 if (--dl <= 0) {
257 break;
258 }
259 r[2] = a[2];
260 if (--dl <= 0) {
261 break;
262 }
263 r[3] = a[3];
264 if (--dl <= 0) {
265 break;
266 }
267
268 a += 4;
269 r += 4;
270 }
271 }
272 }
273
274 return c;
275 }
276 #else
277 /* On other platforms the function is defined in asm. */
278 BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
279 int cl, int dl);
280 #endif
281
282 /* Karatsuba recursive multiplication algorithm
283 * (cf. Knuth, The Art of Computer Programming, Vol. 2) */
284
285 /* r is 2*n2 words in size,
286 * a and b are both n2 words in size.
287 * n2 must be a power of 2.
288 * We multiply and return the result.
289 * t must be 2*n2 words in size
290 * We calculate
291 * a[0]*b[0]
292 * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
293 * a[1]*b[1]
294 */
295 /* dnX may not be positive, but n2/2+dnX has to be */
296 static void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
297 int dna, int dnb, BN_ULONG *t) {
298 int n = n2 / 2, c1, c2;
299 int tna = n + dna, tnb = n + dnb;
300 unsigned int neg, zero;
301 BN_ULONG ln, lo, *p;
302
303 /* Only call bn_mul_comba 8 if n2 == 8 and the
304 * two arrays are complete [steve]
305 */
306 if (n2 == 8 && dna == 0 && dnb == 0) {
307 bn_mul_comba8(r, a, b);
308 return;
309 }
310
311 /* Else do normal multiply */
312 if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) {
313 bn_mul_normal(r, a, n2 + dna, b, n2 + dnb);
314 if ((dna + dnb) < 0) {
315 memset(&r[2 * n2 + dna + dnb], 0, sizeof(BN_ULONG) * -(dna + dnb));
316 }
317 return;
318 }
319
320 /* r=(a[0]-a[1])*(b[1]-b[0]) */
321 c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
322 c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
323 zero = neg = 0;
324 switch (c1 * 3 + c2) {
325 case -4:
326 bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
327 bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
328 break;
329 case -3:
330 zero = 1;
331 break;
332 case -2:
333 bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
334 bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
335 neg = 1;
336 break;
337 case -1:
338 case 0:
339 case 1:
340 zero = 1;
341 break;
342 case 2:
343 bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
344 bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
345 neg = 1;
346 break;
347 case 3:
348 zero = 1;
349 break;
350 case 4:
351 bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
352 bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
353 break;
354 }
355
356 if (n == 4 && dna == 0 && dnb == 0) {
357 /* XXX: bn_mul_comba4 could take extra args to do this well */
358 if (!zero) {
359 bn_mul_comba4(&(t[n2]), t, &(t[n]));
360 } else {
361 memset(&(t[n2]), 0, 8 * sizeof(BN_ULONG));
362 }
363
364 bn_mul_comba4(r, a, b);
365 bn_mul_comba4(&(r[n2]), &(a[n]), &(b[n]));
366 } else if (n == 8 && dna == 0 && dnb == 0) {
367 /* XXX: bn_mul_comba8 could take extra args to do this well */
368 if (!zero) {
369 bn_mul_comba8(&(t[n2]), t, &(t[n]));
370 } else {
371 memset(&(t[n2]), 0, 16 * sizeof(BN_ULONG));
372 }
373
374 bn_mul_comba8(r, a, b);
375 bn_mul_comba8(&(r[n2]), &(a[n]), &(b[n]));
376 } else {
377 p = &(t[n2 * 2]);
378 if (!zero) {
379 bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
380 } else {
381 memset(&(t[n2]), 0, n2 * sizeof(BN_ULONG));
382 }
383 bn_mul_recursive(r, a, b, n, 0, 0, p);
384 bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), n, dna, dnb, p);
385 }
386
387 /* t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
388 * r[10] holds (a[0]*b[0])
389 * r[32] holds (b[1]*b[1]) */
390
391 c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
392
393 if (neg) {
394 /* if t[32] is negative */
395 c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
396 } else {
397 /* Might have a carry */
398 c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
399 }
400
401 /* t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
402 * r[10] holds (a[0]*b[0])
403 * r[32] holds (b[1]*b[1])
404 * c1 holds the carry bits */
405 c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
406 if (c1) {
407 p = &(r[n + n2]);
408 lo = *p;
409 ln = (lo + c1) & BN_MASK2;
410 *p = ln;
411
412 /* The overflow will stop before we over write
413 * words we should not overwrite */
414 if (ln < (BN_ULONG)c1) {
415 do {
416 p++;
417 lo = *p;
418 ln = (lo + 1) & BN_MASK2;
419 *p = ln;
420 } while (ln == 0);
421 }
422 }
423 }
424
425 /* n+tn is the word length
426 * t needs to be n*4 is size, as does r */
427 /* tnX may not be negative but less than n */
428 static void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,
429 int tna, int tnb, BN_ULONG *t) {
430 int i, j, n2 = n * 2;
431 int c1, c2, neg;
432 BN_ULONG ln, lo, *p;
433
434 if (n < 8) {
435 bn_mul_normal(r, a, n + tna, b, n + tnb);
436 return;
437 }
438
439 /* r=(a[0]-a[1])*(b[1]-b[0]) */
440 c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
441 c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
442 neg = 0;
443 switch (c1 * 3 + c2) {
444 case -4:
445 bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
446 bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
447 break;
448 case -3:
449 /* break; */
450 case -2:
451 bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
452 bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
453 neg = 1;
454 break;
455 case -1:
456 case 0:
457 case 1:
458 /* break; */
459 case 2:
460 bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
461 bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
462 neg = 1;
463 break;
464 case 3:
465 /* break; */
466 case 4:
467 bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
468 bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
469 break;
470 }
471
472 if (n == 8) {
473 bn_mul_comba8(&(t[n2]), t, &(t[n]));
474 bn_mul_comba8(r, a, b);
475 bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
476 memset(&(r[n2 + tna + tnb]), 0, sizeof(BN_ULONG) * (n2 - tna - tnb));
477 } else {
478 p = &(t[n2 * 2]);
479 bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
480 bn_mul_recursive(r, a, b, n, 0, 0, p);
481 i = n / 2;
482 /* If there is only a bottom half to the number,
483 * just do it */
484 if (tna > tnb) {
485 j = tna - i;
486 } else {
487 j = tnb - i;
488 }
489
490 if (j == 0) {
491 bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), i, tna - i, tnb - i, p);
492 memset(&(r[n2 + i * 2]), 0, sizeof(BN_ULONG) * (n2 - i * 2));
493 } else if (j > 0) {
494 /* eg, n == 16, i == 8 and tn == 11 */
495 bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]), i, tna - i, tnb - i, p);
496 memset(&(r[n2 + tna + tnb]), 0, sizeof(BN_ULONG) * (n2 - tna - tnb));
497 } else {
498 /* (j < 0) eg, n == 16, i == 8 and tn == 5 */
499 memset(&(r[n2]), 0, sizeof(BN_ULONG) * n2);
500 if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL &&
501 tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {
502 bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
503 } else {
504 for (;;) {
505 i /= 2;
506 /* these simplified conditions work
507 * exclusively because difference
508 * between tna and tnb is 1 or 0 */
509 if (i < tna || i < tnb) {
510 bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]), i, tna - i,
511 tnb - i, p);
512 break;
513 } else if (i == tna || i == tnb) {
514 bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), i, tna - i, tnb - i,
515 p);
516 break;
517 }
518 }
519 }
520 }
521 }
522
523 /* t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
524 * r[10] holds (a[0]*b[0])
525 * r[32] holds (b[1]*b[1])
526 */
527
528 c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
529
530 if (neg) {
531 /* if t[32] is negative */
532 c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
533 } else {
534 /* Might have a carry */
535 c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
536 }
537
538 /* t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
539 * r[10] holds (a[0]*b[0])
540 * r[32] holds (b[1]*b[1])
541 * c1 holds the carry bits */
542 c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
543 if (c1) {
544 p = &(r[n + n2]);
545 lo = *p;
546 ln = (lo + c1) & BN_MASK2;
547 *p = ln;
548
549 /* The overflow will stop before we over write
550 * words we should not overwrite */
551 if (ln < (BN_ULONG)c1) {
552 do {
553 p++;
554 lo = *p;
555 ln = (lo + 1) & BN_MASK2;
556 *p = ln;
557 } while (ln == 0);
558 }
559 }
560 }
561
562 int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
563 int ret = 0;
564 int top, al, bl;
565 BIGNUM *rr;
566 int i;
567 BIGNUM *t = NULL;
568 int j = 0, k;
569
570 al = a->top;
571 bl = b->top;
572
573 if ((al == 0) || (bl == 0)) {
574 BN_zero(r);
575 return 1;
576 }
577 top = al + bl;
578
579 BN_CTX_start(ctx);
580 if ((r == a) || (r == b)) {
581 if ((rr = BN_CTX_get(ctx)) == NULL) {
582 goto err;
583 }
584 } else {
585 rr = r;
586 }
587 rr->neg = a->neg ^ b->neg;
588
589 i = al - bl;
590 if (i == 0) {
591 if (al == 8) {
592 if (bn_wexpand(rr, 16) == NULL) {
593 goto err;
594 }
595 rr->top = 16;
596 bn_mul_comba8(rr->d, a->d, b->d);
597 goto end;
598 }
599 }
600
601 static const int kMulNormalSize = 16;
602 if (al >= kMulNormalSize && bl >= kMulNormalSize) {
603 if (i >= -1 && i <= 1) {
604 /* Find out the power of two lower or equal
605 to the longest of the two numbers */
606 if (i >= 0) {
607 j = BN_num_bits_word((BN_ULONG)al);
608 }
609 if (i == -1) {
610 j = BN_num_bits_word((BN_ULONG)bl);
611 }
612 j = 1 << (j - 1);
613 assert(j <= al || j <= bl);
614 k = j + j;
615 t = BN_CTX_get(ctx);
616 if (t == NULL) {
617 goto err;
618 }
619 if (al > j || bl > j) {
620 if (bn_wexpand(t, k * 4) == NULL) {
621 goto err;
622 }
623 if (bn_wexpand(rr, k * 4) == NULL) {
624 goto err;
625 }
626 bn_mul_part_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
627 } else {
628 /* al <= j || bl <= j */
629 if (bn_wexpand(t, k * 2) == NULL) {
630 goto err;
631 }
632 if (bn_wexpand(rr, k * 2) == NULL) {
633 goto err;
634 }
635 bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
636 }
637 rr->top = top;
638 goto end;
639 }
640 }
641
642 if (bn_wexpand(rr, top) == NULL) {
643 goto err;
644 }
645 rr->top = top;
646 bn_mul_normal(rr->d, a->d, al, b->d, bl);
647
648 end:
649 bn_correct_top(rr);
650 if (r != rr && !BN_copy(r, rr)) {
651 goto err;
652 }
653 ret = 1;
654
655 err:
656 BN_CTX_end(ctx);
657 return ret;
658 }
659
660 /* tmp must have 2*n words */
661 static void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp) {
662 int i, j, max;
663 const BN_ULONG *ap;
664 BN_ULONG *rp;
665
666 max = n * 2;
667 ap = a;
668 rp = r;
669 rp[0] = rp[max - 1] = 0;
670 rp++;
671 j = n;
672
673 if (--j > 0) {
674 ap++;
675 rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
676 rp += 2;
677 }
678
679 for (i = n - 2; i > 0; i--) {
680 j--;
681 ap++;
682 rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);
683 rp += 2;
684 }
685
686 bn_add_words(r, r, r, max);
687
688 /* There will not be a carry */
689
690 bn_sqr_words(tmp, a, n);
691
692 bn_add_words(r, r, tmp, max);
693 }
694
695 /* r is 2*n words in size,
696 * a and b are both n words in size. (There's not actually a 'b' here ...)
697 * n must be a power of 2.
698 * We multiply and return the result.
699 * t must be 2*n words in size
700 * We calculate
701 * a[0]*b[0]
702 * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
703 * a[1]*b[1]
704 */
705 static void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t) {
706 int n = n2 / 2;
707 int zero, c1;
708 BN_ULONG ln, lo, *p;
709
710 if (n2 == 4) {
711 bn_sqr_comba4(r, a);
712 return;
713 } else if (n2 == 8) {
714 bn_sqr_comba8(r, a);
715 return;
716 }
717 if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL) {
718 bn_sqr_normal(r, a, n2, t);
719 return;
720 }
721 /* r=(a[0]-a[1])*(a[1]-a[0]) */
722 c1 = bn_cmp_words(a, &(a[n]), n);
723 zero = 0;
724 if (c1 > 0) {
725 bn_sub_words(t, a, &(a[n]), n);
726 } else if (c1 < 0) {
727 bn_sub_words(t, &(a[n]), a, n);
728 } else {
729 zero = 1;
730 }
731
732 /* The result will always be negative unless it is zero */
733 p = &(t[n2 * 2]);
734
735 if (!zero) {
736 bn_sqr_recursive(&(t[n2]), t, n, p);
737 } else {
738 memset(&(t[n2]), 0, n2 * sizeof(BN_ULONG));
739 }
740 bn_sqr_recursive(r, a, n, p);
741 bn_sqr_recursive(&(r[n2]), &(a[n]), n, p);
742
743 /* t[32] holds (a[0]-a[1])*(a[1]-a[0]), it is negative or zero
744 * r[10] holds (a[0]*b[0])
745 * r[32] holds (b[1]*b[1]) */
746
747 c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
748
749 /* t[32] is negative */
750 c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
751
752 /* t[32] holds (a[0]-a[1])*(a[1]-a[0])+(a[0]*a[0])+(a[1]*a[1])
753 * r[10] holds (a[0]*a[0])
754 * r[32] holds (a[1]*a[1])
755 * c1 holds the carry bits */
756 c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
757 if (c1) {
758 p = &(r[n + n2]);
759 lo = *p;
760 ln = (lo + c1) & BN_MASK2;
761 *p = ln;
762
763 /* The overflow will stop before we over write
764 * words we should not overwrite */
765 if (ln < (BN_ULONG)c1) {
766 do {
767 p++;
768 lo = *p;
769 ln = (lo + 1) & BN_MASK2;
770 *p = ln;
771 } while (ln == 0);
772 }
773 }
774 }
775
776 int BN_mul_word(BIGNUM *bn, BN_ULONG w) {
777 BN_ULONG ll;
778
779 w &= BN_MASK2;
780 if (!bn->top) {
781 return 1;
782 }
783
784 if (w == 0) {
785 BN_zero(bn);
786 return 1;
787 }
788
789 ll = bn_mul_words(bn->d, bn->d, bn->top, w);
790 if (ll) {
791 if (bn_wexpand(bn, bn->top + 1) == NULL) {
792 return 0;
793 }
794 bn->d[bn->top++] = ll;
795 }
796
797 return 1;
798 }
799
800 int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) {
801 int max, al;
802 int ret = 0;
803 BIGNUM *tmp, *rr;
804
805 al = a->top;
806 if (al <= 0) {
807 r->top = 0;
808 r->neg = 0;
809 return 1;
810 }
811
812 BN_CTX_start(ctx);
813 rr = (a != r) ? r : BN_CTX_get(ctx);
814 tmp = BN_CTX_get(ctx);
815 if (!rr || !tmp) {
816 goto err;
817 }
818
819 max = 2 * al; /* Non-zero (from above) */
820 if (bn_wexpand(rr, max) == NULL) {
821 goto err;
822 }
823
824 if (al == 4) {
825 bn_sqr_comba4(rr->d, a->d);
826 } else if (al == 8) {
827 bn_sqr_comba8(rr->d, a->d);
828 } else {
829 if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {
830 BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];
831 bn_sqr_normal(rr->d, a->d, al, t);
832 } else {
833 int j, k;
834
835 j = BN_num_bits_word((BN_ULONG)al);
836 j = 1 << (j - 1);
837 k = j + j;
838 if (al == j) {
839 if (bn_wexpand(tmp, k * 2) == NULL) {
840 goto err;
841 }
842 bn_sqr_recursive(rr->d, a->d, al, tmp->d);
843 } else {
844 if (bn_wexpand(tmp, max) == NULL) {
845 goto err;
846 }
847 bn_sqr_normal(rr->d, a->d, al, tmp->d);
848 }
849 }
850 }
851
852 rr->neg = 0;
853 /* If the most-significant half of the top word of 'a' is zero, then
854 * the square of 'a' will max-1 words. */
855 if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l)) {
856 rr->top = max - 1;
857 } else {
858 rr->top = max;
859 }
860
861 if (rr != r && !BN_copy(r, rr)) {
862 goto err;
863 }
864 ret = 1;
865
866 err:
867 BN_CTX_end(ctx);
868 return ret;
869 }