1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
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)"
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
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.]
59 #include <openssl/crypto.h>
60 #include "internal/cryptlib.h"
63 #if defined(BN_LLONG) || defined(BN_UMULT_HIGH)
65 BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,
74 # ifndef OPENSSL_SMALL_FOOTPRINT
76 mul_add(rp[0], ap[0], w, c1);
77 mul_add(rp[1], ap[1], w, c1);
78 mul_add(rp[2], ap[2], w, c1);
79 mul_add(rp[3], ap[3], w, c1);
86 mul_add(rp[0], ap[0], w, c1);
95 BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
103 # ifndef OPENSSL_SMALL_FOOTPRINT
105 mul(rp[0], ap[0], w, c1);
106 mul(rp[1], ap[1], w, c1);
107 mul(rp[2], ap[2], w, c1);
108 mul(rp[3], ap[3], w, c1);
115 mul(rp[0], ap[0], w, c1);
123 void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n)
129 # ifndef OPENSSL_SMALL_FOOTPRINT
131 sqr(r[0], r[1], a[0]);
132 sqr(r[2], r[3], a[1]);
133 sqr(r[4], r[5], a[2]);
134 sqr(r[6], r[7], a[3]);
141 sqr(r[0], r[1], a[0]);
148 #else /* !(defined(BN_LLONG) ||
149 * defined(BN_UMULT_HIGH)) */
151 BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,
159 return ((BN_ULONG)0);
164 # ifndef OPENSSL_SMALL_FOOTPRINT
166 mul_add(rp[0], ap[0], bl, bh, c);
167 mul_add(rp[1], ap[1], bl, bh, c);
168 mul_add(rp[2], ap[2], bl, bh, c);
169 mul_add(rp[3], ap[3], bl, bh, c);
176 mul_add(rp[0], ap[0], bl, bh, c);
184 BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
191 return ((BN_ULONG)0);
196 # ifndef OPENSSL_SMALL_FOOTPRINT
198 mul(rp[0], ap[0], bl, bh, carry);
199 mul(rp[1], ap[1], bl, bh, carry);
200 mul(rp[2], ap[2], bl, bh, carry);
201 mul(rp[3], ap[3], bl, bh, carry);
208 mul(rp[0], ap[0], bl, bh, carry);
216 void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n)
222 # ifndef OPENSSL_SMALL_FOOTPRINT
224 sqr64(r[0], r[1], a[0]);
225 sqr64(r[2], r[3], a[1]);
226 sqr64(r[4], r[5], a[2]);
227 sqr64(r[6], r[7], a[3]);
234 sqr64(r[0], r[1], a[0]);
241 #endif /* !(defined(BN_LLONG) ||
242 * defined(BN_UMULT_HIGH)) */
244 #if defined(BN_LLONG) && defined(BN_DIV2W)
246 BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
248 return ((BN_ULONG)(((((BN_ULLONG) h) << BN_BITS2) | l) / (BN_ULLONG) d));
253 /* Divide h,l by d and return the result. */
254 /* I need to test this some more :-( */
255 BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
257 BN_ULONG dh, dl, q, ret = 0, th, tl, t;
263 i = BN_num_bits_word(d);
264 assert((i == BN_BITS2) || (h <= (BN_ULONG)1 << i));
272 h = (h << i) | (l >> (BN_BITS2 - i));
275 dh = (d & BN_MASK2h) >> BN_BITS4;
276 dl = (d & BN_MASK2l);
278 if ((h >> BN_BITS4) == dh)
287 if ((t & BN_MASK2h) ||
288 ((tl) <= ((t << BN_BITS4) | ((l & BN_MASK2h) >> BN_BITS4))))
294 t = (tl >> BN_BITS4);
295 tl = (tl << BN_BITS4) & BN_MASK2h;
311 h = ((h << BN_BITS4) | (l >> BN_BITS4)) & BN_MASK2;
312 l = (l & BN_MASK2l) << BN_BITS4;
317 #endif /* !defined(BN_LLONG) && defined(BN_DIV2W) */
320 BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
327 return ((BN_ULONG)0);
329 # ifndef OPENSSL_SMALL_FOOTPRINT
331 ll += (BN_ULLONG) a[0] + b[0];
332 r[0] = (BN_ULONG)ll & BN_MASK2;
334 ll += (BN_ULLONG) a[1] + b[1];
335 r[1] = (BN_ULONG)ll & BN_MASK2;
337 ll += (BN_ULLONG) a[2] + b[2];
338 r[2] = (BN_ULONG)ll & BN_MASK2;
340 ll += (BN_ULLONG) a[3] + b[3];
341 r[3] = (BN_ULONG)ll & BN_MASK2;
350 ll += (BN_ULLONG) a[0] + b[0];
351 r[0] = (BN_ULONG)ll & BN_MASK2;
358 return ((BN_ULONG)ll);
360 #else /* !BN_LLONG */
361 BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
368 return ((BN_ULONG)0);
371 # ifndef OPENSSL_SMALL_FOOTPRINT
374 t = (t + c) & BN_MASK2;
376 l = (t + b[0]) & BN_MASK2;
380 t = (t + c) & BN_MASK2;
382 l = (t + b[1]) & BN_MASK2;
386 t = (t + c) & BN_MASK2;
388 l = (t + b[2]) & BN_MASK2;
392 t = (t + c) & BN_MASK2;
394 l = (t + b[3]) & BN_MASK2;
405 t = (t + c) & BN_MASK2;
407 l = (t + b[0]) & BN_MASK2;
415 return ((BN_ULONG)c);
417 #endif /* !BN_LLONG */
419 BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
427 return ((BN_ULONG)0);
429 #ifndef OPENSSL_SMALL_FOOTPRINT
433 r[0] = (t1 - t2 - c) & BN_MASK2;
438 r[1] = (t1 - t2 - c) & BN_MASK2;
443 r[2] = (t1 - t2 - c) & BN_MASK2;
448 r[3] = (t1 - t2 - c) & BN_MASK2;
460 r[0] = (t1 - t2 - c) & BN_MASK2;
471 #if defined(BN_MUL_COMBA) && !defined(OPENSSL_SMALL_FOOTPRINT)
473 # undef bn_mul_comba8
474 # undef bn_mul_comba4
475 # undef bn_sqr_comba8
476 # undef bn_sqr_comba4
478 /* mul_add_c(a,b,c0,c1,c2) -- c+=a*b for three word number c=(c2,c1,c0) */
479 /* mul_add_c2(a,b,c0,c1,c2) -- c+=2*a*b for three word number c=(c2,c1,c0) */
480 /* sqr_add_c(a,i,c0,c1,c2) -- c+=a[i]^2 for three word number c=(c2,c1,c0) */
482 * sqr_add_c2(a,i,c0,c1,c2) -- c+=2*a[i]*a[j] for three word number
488 * Keep in mind that additions to multiplication result can not
489 * overflow, because its high half cannot be all-ones.
491 # define mul_add_c(a,b,c0,c1,c2) do { \
493 BN_ULLONG t = (BN_ULLONG)(a)*(b); \
494 t += c0; /* no carry */ \
495 c0 = (BN_ULONG)Lw(t); \
496 hi = (BN_ULONG)Hw(t); \
497 c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
500 # define mul_add_c2(a,b,c0,c1,c2) do { \
502 BN_ULLONG t = (BN_ULLONG)(a)*(b); \
503 BN_ULLONG tt = t+c0; /* no carry */ \
504 c0 = (BN_ULONG)Lw(tt); \
505 hi = (BN_ULONG)Hw(tt); \
506 c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
507 t += c0; /* no carry */ \
508 c0 = (BN_ULONG)Lw(t); \
509 hi = (BN_ULONG)Hw(t); \
510 c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
513 # define sqr_add_c(a,i,c0,c1,c2) do { \
515 BN_ULLONG t = (BN_ULLONG)a[i]*a[i]; \
516 t += c0; /* no carry */ \
517 c0 = (BN_ULONG)Lw(t); \
518 hi = (BN_ULONG)Hw(t); \
519 c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
522 # define sqr_add_c2(a,i,j,c0,c1,c2) \
523 mul_add_c2((a)[i],(a)[j],c0,c1,c2)
525 # elif defined(BN_UMULT_LOHI)
527 * Keep in mind that additions to hi can not overflow, because
528 * the high word of a multiplication result cannot be all-ones.
530 # define mul_add_c(a,b,c0,c1,c2) do { \
531 BN_ULONG ta = (a), tb = (b); \
533 BN_UMULT_LOHI(lo,hi,ta,tb); \
534 c0 += lo; hi += (c0<lo)?1:0; \
535 c1 += hi; c2 += (c1<hi)?1:0; \
538 # define mul_add_c2(a,b,c0,c1,c2) do { \
539 BN_ULONG ta = (a), tb = (b); \
540 BN_ULONG lo, hi, tt; \
541 BN_UMULT_LOHI(lo,hi,ta,tb); \
542 c0 += lo; tt = hi+((c0<lo)?1:0); \
543 c1 += tt; c2 += (c1<tt)?1:0; \
544 c0 += lo; hi += (c0<lo)?1:0; \
545 c1 += hi; c2 += (c1<hi)?1:0; \
548 # define sqr_add_c(a,i,c0,c1,c2) do { \
549 BN_ULONG ta = (a)[i]; \
551 BN_UMULT_LOHI(lo,hi,ta,ta); \
552 c0 += lo; hi += (c0<lo)?1:0; \
553 c1 += hi; c2 += (c1<hi)?1:0; \
556 # define sqr_add_c2(a,i,j,c0,c1,c2) \
557 mul_add_c2((a)[i],(a)[j],c0,c1,c2)
559 # elif defined(BN_UMULT_HIGH)
561 * Keep in mind that additions to hi can not overflow, because
562 * the high word of a multiplication result cannot be all-ones.
564 # define mul_add_c(a,b,c0,c1,c2) do { \
565 BN_ULONG ta = (a), tb = (b); \
566 BN_ULONG lo = ta * tb; \
567 BN_ULONG hi = BN_UMULT_HIGH(ta,tb); \
568 c0 += lo; hi += (c0<lo)?1:0; \
569 c1 += hi; c2 += (c1<hi)?1:0; \
572 # define mul_add_c2(a,b,c0,c1,c2) do { \
573 BN_ULONG ta = (a), tb = (b), tt; \
574 BN_ULONG lo = ta * tb; \
575 BN_ULONG hi = BN_UMULT_HIGH(ta,tb); \
576 c0 += lo; tt = hi + ((c0<lo)?1:0); \
577 c1 += tt; c2 += (c1<tt)?1:0; \
578 c0 += lo; hi += (c0<lo)?1:0; \
579 c1 += hi; c2 += (c1<hi)?1:0; \
582 # define sqr_add_c(a,i,c0,c1,c2) do { \
583 BN_ULONG ta = (a)[i]; \
584 BN_ULONG lo = ta * ta; \
585 BN_ULONG hi = BN_UMULT_HIGH(ta,ta); \
586 c0 += lo; hi += (c0<lo)?1:0; \
587 c1 += hi; c2 += (c1<hi)?1:0; \
590 # define sqr_add_c2(a,i,j,c0,c1,c2) \
591 mul_add_c2((a)[i],(a)[j],c0,c1,c2)
593 # else /* !BN_LLONG */
595 * Keep in mind that additions to hi can not overflow, because
596 * the high word of a multiplication result cannot be all-ones.
598 # define mul_add_c(a,b,c0,c1,c2) do { \
599 BN_ULONG lo = LBITS(a), hi = HBITS(a); \
600 BN_ULONG bl = LBITS(b), bh = HBITS(b); \
601 mul64(lo,hi,bl,bh); \
602 c0 = (c0+lo)&BN_MASK2; if (c0<lo) hi++; \
603 c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
606 # define mul_add_c2(a,b,c0,c1,c2) do { \
608 BN_ULONG lo = LBITS(a), hi = HBITS(a); \
609 BN_ULONG bl = LBITS(b), bh = HBITS(b); \
610 mul64(lo,hi,bl,bh); \
612 c0 = (c0+lo)&BN_MASK2; if (c0<lo) tt++; \
613 c1 = (c1+tt)&BN_MASK2; if (c1<tt) c2++; \
614 c0 = (c0+lo)&BN_MASK2; if (c0<lo) hi++; \
615 c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
618 # define sqr_add_c(a,i,c0,c1,c2) do { \
620 sqr64(lo,hi,(a)[i]); \
621 c0 = (c0+lo)&BN_MASK2; if (c0<lo) hi++; \
622 c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
625 # define sqr_add_c2(a,i,j,c0,c1,c2) \
626 mul_add_c2((a)[i],(a)[j],c0,c1,c2)
627 # endif /* !BN_LLONG */
629 void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
636 mul_add_c(a[0], b[0], c1, c2, c3);
639 mul_add_c(a[0], b[1], c2, c3, c1);
640 mul_add_c(a[1], b[0], c2, c3, c1);
643 mul_add_c(a[2], b[0], c3, c1, c2);
644 mul_add_c(a[1], b[1], c3, c1, c2);
645 mul_add_c(a[0], b[2], c3, c1, c2);
648 mul_add_c(a[0], b[3], c1, c2, c3);
649 mul_add_c(a[1], b[2], c1, c2, c3);
650 mul_add_c(a[2], b[1], c1, c2, c3);
651 mul_add_c(a[3], b[0], c1, c2, c3);
654 mul_add_c(a[4], b[0], c2, c3, c1);
655 mul_add_c(a[3], b[1], c2, c3, c1);
656 mul_add_c(a[2], b[2], c2, c3, c1);
657 mul_add_c(a[1], b[3], c2, c3, c1);
658 mul_add_c(a[0], b[4], c2, c3, c1);
661 mul_add_c(a[0], b[5], c3, c1, c2);
662 mul_add_c(a[1], b[4], c3, c1, c2);
663 mul_add_c(a[2], b[3], c3, c1, c2);
664 mul_add_c(a[3], b[2], c3, c1, c2);
665 mul_add_c(a[4], b[1], c3, c1, c2);
666 mul_add_c(a[5], b[0], c3, c1, c2);
669 mul_add_c(a[6], b[0], c1, c2, c3);
670 mul_add_c(a[5], b[1], c1, c2, c3);
671 mul_add_c(a[4], b[2], c1, c2, c3);
672 mul_add_c(a[3], b[3], c1, c2, c3);
673 mul_add_c(a[2], b[4], c1, c2, c3);
674 mul_add_c(a[1], b[5], c1, c2, c3);
675 mul_add_c(a[0], b[6], c1, c2, c3);
678 mul_add_c(a[0], b[7], c2, c3, c1);
679 mul_add_c(a[1], b[6], c2, c3, c1);
680 mul_add_c(a[2], b[5], c2, c3, c1);
681 mul_add_c(a[3], b[4], c2, c3, c1);
682 mul_add_c(a[4], b[3], c2, c3, c1);
683 mul_add_c(a[5], b[2], c2, c3, c1);
684 mul_add_c(a[6], b[1], c2, c3, c1);
685 mul_add_c(a[7], b[0], c2, c3, c1);
688 mul_add_c(a[7], b[1], c3, c1, c2);
689 mul_add_c(a[6], b[2], c3, c1, c2);
690 mul_add_c(a[5], b[3], c3, c1, c2);
691 mul_add_c(a[4], b[4], c3, c1, c2);
692 mul_add_c(a[3], b[5], c3, c1, c2);
693 mul_add_c(a[2], b[6], c3, c1, c2);
694 mul_add_c(a[1], b[7], c3, c1, c2);
697 mul_add_c(a[2], b[7], c1, c2, c3);
698 mul_add_c(a[3], b[6], c1, c2, c3);
699 mul_add_c(a[4], b[5], c1, c2, c3);
700 mul_add_c(a[5], b[4], c1, c2, c3);
701 mul_add_c(a[6], b[3], c1, c2, c3);
702 mul_add_c(a[7], b[2], c1, c2, c3);
705 mul_add_c(a[7], b[3], c2, c3, c1);
706 mul_add_c(a[6], b[4], c2, c3, c1);
707 mul_add_c(a[5], b[5], c2, c3, c1);
708 mul_add_c(a[4], b[6], c2, c3, c1);
709 mul_add_c(a[3], b[7], c2, c3, c1);
712 mul_add_c(a[4], b[7], c3, c1, c2);
713 mul_add_c(a[5], b[6], c3, c1, c2);
714 mul_add_c(a[6], b[5], c3, c1, c2);
715 mul_add_c(a[7], b[4], c3, c1, c2);
718 mul_add_c(a[7], b[5], c1, c2, c3);
719 mul_add_c(a[6], b[6], c1, c2, c3);
720 mul_add_c(a[5], b[7], c1, c2, c3);
723 mul_add_c(a[6], b[7], c2, c3, c1);
724 mul_add_c(a[7], b[6], c2, c3, c1);
727 mul_add_c(a[7], b[7], c3, c1, c2);
732 void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
739 mul_add_c(a[0], b[0], c1, c2, c3);
742 mul_add_c(a[0], b[1], c2, c3, c1);
743 mul_add_c(a[1], b[0], c2, c3, c1);
746 mul_add_c(a[2], b[0], c3, c1, c2);
747 mul_add_c(a[1], b[1], c3, c1, c2);
748 mul_add_c(a[0], b[2], c3, c1, c2);
751 mul_add_c(a[0], b[3], c1, c2, c3);
752 mul_add_c(a[1], b[2], c1, c2, c3);
753 mul_add_c(a[2], b[1], c1, c2, c3);
754 mul_add_c(a[3], b[0], c1, c2, c3);
757 mul_add_c(a[3], b[1], c2, c3, c1);
758 mul_add_c(a[2], b[2], c2, c3, c1);
759 mul_add_c(a[1], b[3], c2, c3, c1);
762 mul_add_c(a[2], b[3], c3, c1, c2);
763 mul_add_c(a[3], b[2], c3, c1, c2);
766 mul_add_c(a[3], b[3], c1, c2, c3);
771 void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a)
778 sqr_add_c(a, 0, c1, c2, c3);
781 sqr_add_c2(a, 1, 0, c2, c3, c1);
784 sqr_add_c(a, 1, c3, c1, c2);
785 sqr_add_c2(a, 2, 0, c3, c1, c2);
788 sqr_add_c2(a, 3, 0, c1, c2, c3);
789 sqr_add_c2(a, 2, 1, c1, c2, c3);
792 sqr_add_c(a, 2, c2, c3, c1);
793 sqr_add_c2(a, 3, 1, c2, c3, c1);
794 sqr_add_c2(a, 4, 0, c2, c3, c1);
797 sqr_add_c2(a, 5, 0, c3, c1, c2);
798 sqr_add_c2(a, 4, 1, c3, c1, c2);
799 sqr_add_c2(a, 3, 2, c3, c1, c2);
802 sqr_add_c(a, 3, c1, c2, c3);
803 sqr_add_c2(a, 4, 2, c1, c2, c3);
804 sqr_add_c2(a, 5, 1, c1, c2, c3);
805 sqr_add_c2(a, 6, 0, c1, c2, c3);
808 sqr_add_c2(a, 7, 0, c2, c3, c1);
809 sqr_add_c2(a, 6, 1, c2, c3, c1);
810 sqr_add_c2(a, 5, 2, c2, c3, c1);
811 sqr_add_c2(a, 4, 3, c2, c3, c1);
814 sqr_add_c(a, 4, c3, c1, c2);
815 sqr_add_c2(a, 5, 3, c3, c1, c2);
816 sqr_add_c2(a, 6, 2, c3, c1, c2);
817 sqr_add_c2(a, 7, 1, c3, c1, c2);
820 sqr_add_c2(a, 7, 2, c1, c2, c3);
821 sqr_add_c2(a, 6, 3, c1, c2, c3);
822 sqr_add_c2(a, 5, 4, c1, c2, c3);
825 sqr_add_c(a, 5, c2, c3, c1);
826 sqr_add_c2(a, 6, 4, c2, c3, c1);
827 sqr_add_c2(a, 7, 3, c2, c3, c1);
830 sqr_add_c2(a, 7, 4, c3, c1, c2);
831 sqr_add_c2(a, 6, 5, c3, c1, c2);
834 sqr_add_c(a, 6, c1, c2, c3);
835 sqr_add_c2(a, 7, 5, c1, c2, c3);
838 sqr_add_c2(a, 7, 6, c2, c3, c1);
841 sqr_add_c(a, 7, c3, c1, c2);
846 void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a)
853 sqr_add_c(a, 0, c1, c2, c3);
856 sqr_add_c2(a, 1, 0, c2, c3, c1);
859 sqr_add_c(a, 1, c3, c1, c2);
860 sqr_add_c2(a, 2, 0, c3, c1, c2);
863 sqr_add_c2(a, 3, 0, c1, c2, c3);
864 sqr_add_c2(a, 2, 1, c1, c2, c3);
867 sqr_add_c(a, 2, c2, c3, c1);
868 sqr_add_c2(a, 3, 1, c2, c3, c1);
871 sqr_add_c2(a, 3, 2, c3, c1, c2);
874 sqr_add_c(a, 3, c1, c2, c3);
879 # ifdef OPENSSL_NO_ASM
880 # ifdef OPENSSL_BN_ASM_MONT
883 * This is essentially reference implementation, which may or may not
884 * result in performance improvement. E.g. on IA-32 this routine was
885 * observed to give 40% faster rsa1024 private key operations and 10%
886 * faster rsa4096 ones, while on AMD64 it improves rsa1024 sign only
887 * by 10% and *worsens* rsa4096 sign by 15%. Once again, it's a
888 * reference implementation, one to be used as starting point for
889 * platform-specific assembler. Mentioned numbers apply to compiler
890 * generated code compiled with and without -DOPENSSL_BN_ASM_MONT and
891 * can vary not only from platform to platform, but even for compiler
892 * versions. Assembler vs. assembler improvement coefficients can
893 * [and are known to] differ and are to be documented elsewhere.
895 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
896 const BN_ULONG *np, const BN_ULONG *n0p, int num)
898 BN_ULONG c0, c1, ml, *tp, n0;
902 volatile BN_ULONG *vp;
905 # if 0 /* template for platform-specific
908 return bn_sqr_mont(rp, ap, np, n0p, num);
910 vp = tp = alloca((num + 2) * sizeof(BN_ULONG));
919 for (j = 0; j < num; ++j)
920 mul(tp[j], ap[j], ml, mh, c0);
922 for (j = 0; j < num; ++j)
923 mul(tp[j], ap[j], ml, c0);
930 for (i = 0; i < num; i++) {
936 for (j = 0; j < num; ++j)
937 mul_add(tp[j], ap[j], ml, mh, c0);
939 for (j = 0; j < num; ++j)
940 mul_add(tp[j], ap[j], ml, c0);
942 c1 = (tp[num] + c0) & BN_MASK2;
944 tp[num + 1] = (c1 < c0 ? 1 : 0);
947 ml = (c1 * n0) & BN_MASK2;
952 mul_add(c1, np[0], ml, mh, c0);
954 mul_add(c1, ml, np[0], c0);
956 for (j = 1; j < num; j++) {
959 mul_add(c1, np[j], ml, mh, c0);
961 mul_add(c1, ml, np[j], c0);
963 tp[j - 1] = c1 & BN_MASK2;
965 c1 = (tp[num] + c0) & BN_MASK2;
967 tp[num] = tp[num + 1] + (c1 < c0 ? 1 : 0);
970 if (tp[num] != 0 || tp[num - 1] >= np[num - 1]) {
971 c0 = bn_sub_words(rp, tp, np, num);
972 if (tp[num] != 0 || c0 == 0) {
973 for (i = 0; i < num + 2; i++)
978 for (i = 0; i < num; i++)
979 rp[i] = tp[i], vp[i] = 0;
986 * Return value of 0 indicates that multiplication/convolution was not
987 * performed to signal the caller to fall down to alternative/original
990 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
991 const BN_ULONG *np, const BN_ULONG *n0, int num)
995 # endif /* OPENSSL_BN_ASM_MONT */
998 #else /* !BN_MUL_COMBA */
1000 /* hmm... is it faster just to do a multiply? */
1001 # undef bn_sqr_comba4
1002 # undef bn_sqr_comba8
1003 void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a)
1006 bn_sqr_normal(r, a, 4, t);
1009 void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a)
1012 bn_sqr_normal(r, a, 8, t);
1015 void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
1017 r[4] = bn_mul_words(&(r[0]), a, 4, b[0]);
1018 r[5] = bn_mul_add_words(&(r[1]), a, 4, b[1]);
1019 r[6] = bn_mul_add_words(&(r[2]), a, 4, b[2]);
1020 r[7] = bn_mul_add_words(&(r[3]), a, 4, b[3]);
1023 void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
1025 r[8] = bn_mul_words(&(r[0]), a, 8, b[0]);
1026 r[9] = bn_mul_add_words(&(r[1]), a, 8, b[1]);
1027 r[10] = bn_mul_add_words(&(r[2]), a, 8, b[2]);
1028 r[11] = bn_mul_add_words(&(r[3]), a, 8, b[3]);
1029 r[12] = bn_mul_add_words(&(r[4]), a, 8, b[4]);
1030 r[13] = bn_mul_add_words(&(r[5]), a, 8, b[5]);
1031 r[14] = bn_mul_add_words(&(r[6]), a, 8, b[6]);
1032 r[15] = bn_mul_add_words(&(r[7]), a, 8, b[7]);
1035 # ifdef OPENSSL_NO_ASM
1036 # ifdef OPENSSL_BN_ASM_MONT
1037 # include <alloca.h>
1038 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
1039 const BN_ULONG *np, const BN_ULONG *n0p, int num)
1041 BN_ULONG c0, c1, *tp, n0 = *n0p;
1042 volatile BN_ULONG *vp;
1045 vp = tp = alloca((num + 2) * sizeof(BN_ULONG));
1047 for (i = 0; i <= num; i++)
1050 for (i = 0; i < num; i++) {
1051 c0 = bn_mul_add_words(tp, ap, num, bp[i]);
1052 c1 = (tp[num] + c0) & BN_MASK2;
1054 tp[num + 1] = (c1 < c0 ? 1 : 0);
1056 c0 = bn_mul_add_words(tp, np, num, tp[0] * n0);
1057 c1 = (tp[num] + c0) & BN_MASK2;
1059 tp[num + 1] += (c1 < c0 ? 1 : 0);
1060 for (j = 0; j <= num; j++)
1064 if (tp[num] != 0 || tp[num - 1] >= np[num - 1]) {
1065 c0 = bn_sub_words(rp, tp, np, num);
1066 if (tp[num] != 0 || c0 == 0) {
1067 for (i = 0; i < num + 2; i++)
1072 for (i = 0; i < num; i++)
1073 rp[i] = tp[i], vp[i] = 0;
1079 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
1080 const BN_ULONG *np, const BN_ULONG *n0, int num)
1084 # endif /* OPENSSL_BN_ASM_MONT */
1087 #endif /* !BN_MUL_COMBA */