xref: /openssl/crypto/bn/bn_asm.c (revision 17cca0e8)
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #include <assert.h>
11 #include <openssl/crypto.h>
12 #include "internal/cryptlib.h"
13 #include "bn_local.h"
14 
15 #if defined(BN_LLONG) || defined(BN_UMULT_HIGH)
16 
bn_mul_add_words(BN_ULONG * rp,const BN_ULONG * ap,int num,BN_ULONG w)17 BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,
18                           BN_ULONG w)
19 {
20     BN_ULONG c1 = 0;
21 
22     assert(num >= 0);
23     if (num <= 0)
24         return c1;
25 
26 # ifndef OPENSSL_SMALL_FOOTPRINT
27     while (num & ~3) {
28         mul_add(rp[0], ap[0], w, c1);
29         mul_add(rp[1], ap[1], w, c1);
30         mul_add(rp[2], ap[2], w, c1);
31         mul_add(rp[3], ap[3], w, c1);
32         ap += 4;
33         rp += 4;
34         num -= 4;
35     }
36 # endif
37     while (num) {
38         mul_add(rp[0], ap[0], w, c1);
39         ap++;
40         rp++;
41         num--;
42     }
43 
44     return c1;
45 }
46 
bn_mul_words(BN_ULONG * rp,const BN_ULONG * ap,int num,BN_ULONG w)47 BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
48 {
49     BN_ULONG c1 = 0;
50 
51     assert(num >= 0);
52     if (num <= 0)
53         return c1;
54 
55 # ifndef OPENSSL_SMALL_FOOTPRINT
56     while (num & ~3) {
57         mul(rp[0], ap[0], w, c1);
58         mul(rp[1], ap[1], w, c1);
59         mul(rp[2], ap[2], w, c1);
60         mul(rp[3], ap[3], w, c1);
61         ap += 4;
62         rp += 4;
63         num -= 4;
64     }
65 # endif
66     while (num) {
67         mul(rp[0], ap[0], w, c1);
68         ap++;
69         rp++;
70         num--;
71     }
72     return c1;
73 }
74 
bn_sqr_words(BN_ULONG * r,const BN_ULONG * a,int n)75 void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n)
76 {
77     assert(n >= 0);
78     if (n <= 0)
79         return;
80 
81 # ifndef OPENSSL_SMALL_FOOTPRINT
82     while (n & ~3) {
83         sqr(r[0], r[1], a[0]);
84         sqr(r[2], r[3], a[1]);
85         sqr(r[4], r[5], a[2]);
86         sqr(r[6], r[7], a[3]);
87         a += 4;
88         r += 8;
89         n -= 4;
90     }
91 # endif
92     while (n) {
93         sqr(r[0], r[1], a[0]);
94         a++;
95         r += 2;
96         n--;
97     }
98 }
99 
100 #else                           /* !(defined(BN_LLONG) ||
101                                  * defined(BN_UMULT_HIGH)) */
102 
bn_mul_add_words(BN_ULONG * rp,const BN_ULONG * ap,int num,BN_ULONG w)103 BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,
104                           BN_ULONG w)
105 {
106     BN_ULONG c = 0;
107     BN_ULONG bl, bh;
108 
109     assert(num >= 0);
110     if (num <= 0)
111         return (BN_ULONG)0;
112 
113     bl = LBITS(w);
114     bh = HBITS(w);
115 
116 # ifndef OPENSSL_SMALL_FOOTPRINT
117     while (num & ~3) {
118         mul_add(rp[0], ap[0], bl, bh, c);
119         mul_add(rp[1], ap[1], bl, bh, c);
120         mul_add(rp[2], ap[2], bl, bh, c);
121         mul_add(rp[3], ap[3], bl, bh, c);
122         ap += 4;
123         rp += 4;
124         num -= 4;
125     }
126 # endif
127     while (num) {
128         mul_add(rp[0], ap[0], bl, bh, c);
129         ap++;
130         rp++;
131         num--;
132     }
133     return c;
134 }
135 
bn_mul_words(BN_ULONG * rp,const BN_ULONG * ap,int num,BN_ULONG w)136 BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
137 {
138     BN_ULONG carry = 0;
139     BN_ULONG bl, bh;
140 
141     assert(num >= 0);
142     if (num <= 0)
143         return (BN_ULONG)0;
144 
145     bl = LBITS(w);
146     bh = HBITS(w);
147 
148 # ifndef OPENSSL_SMALL_FOOTPRINT
149     while (num & ~3) {
150         mul(rp[0], ap[0], bl, bh, carry);
151         mul(rp[1], ap[1], bl, bh, carry);
152         mul(rp[2], ap[2], bl, bh, carry);
153         mul(rp[3], ap[3], bl, bh, carry);
154         ap += 4;
155         rp += 4;
156         num -= 4;
157     }
158 # endif
159     while (num) {
160         mul(rp[0], ap[0], bl, bh, carry);
161         ap++;
162         rp++;
163         num--;
164     }
165     return carry;
166 }
167 
bn_sqr_words(BN_ULONG * r,const BN_ULONG * a,int n)168 void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n)
169 {
170     assert(n >= 0);
171     if (n <= 0)
172         return;
173 
174 # ifndef OPENSSL_SMALL_FOOTPRINT
175     while (n & ~3) {
176         sqr64(r[0], r[1], a[0]);
177         sqr64(r[2], r[3], a[1]);
178         sqr64(r[4], r[5], a[2]);
179         sqr64(r[6], r[7], a[3]);
180         a += 4;
181         r += 8;
182         n -= 4;
183     }
184 # endif
185     while (n) {
186         sqr64(r[0], r[1], a[0]);
187         a++;
188         r += 2;
189         n--;
190     }
191 }
192 
193 #endif                          /* !(defined(BN_LLONG) ||
194                                  * defined(BN_UMULT_HIGH)) */
195 
196 #if defined(BN_LLONG) && defined(BN_DIV2W)
197 
bn_div_words(BN_ULONG h,BN_ULONG l,BN_ULONG d)198 BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
199 {
200     return ((BN_ULONG)(((((BN_ULLONG) h) << BN_BITS2) | l) / (BN_ULLONG) d));
201 }
202 
203 #else
204 
205 /* Divide h,l by d and return the result. */
206 /* I need to test this some more :-( */
bn_div_words(BN_ULONG h,BN_ULONG l,BN_ULONG d)207 BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
208 {
209     BN_ULONG dh, dl, q, ret = 0, th, tl, t;
210     int i, count = 2;
211 
212     if (d == 0)
213         return BN_MASK2;
214 
215     i = BN_num_bits_word(d);
216     assert((i == BN_BITS2) || (h <= (BN_ULONG)1 << i));
217 
218     i = BN_BITS2 - i;
219     if (h >= d)
220         h -= d;
221 
222     if (i) {
223         d <<= i;
224         h = (h << i) | (l >> (BN_BITS2 - i));
225         l <<= i;
226     }
227     dh = (d & BN_MASK2h) >> BN_BITS4;
228     dl = (d & BN_MASK2l);
229     for (;;) {
230         if ((h >> BN_BITS4) == dh)
231             q = BN_MASK2l;
232         else
233             q = h / dh;
234 
235         th = q * dh;
236         tl = dl * q;
237         for (;;) {
238             t = h - th;
239             if ((t & BN_MASK2h) ||
240                 ((tl) <= ((t << BN_BITS4) | ((l & BN_MASK2h) >> BN_BITS4))))
241                 break;
242             q--;
243             th -= dh;
244             tl -= dl;
245         }
246         t = (tl >> BN_BITS4);
247         tl = (tl << BN_BITS4) & BN_MASK2h;
248         th += t;
249 
250         if (l < tl)
251             th++;
252         l -= tl;
253         if (h < th) {
254             h += d;
255             q--;
256         }
257         h -= th;
258 
259         if (--count == 0)
260             break;
261 
262         ret = q << BN_BITS4;
263         h = ((h << BN_BITS4) | (l >> BN_BITS4)) & BN_MASK2;
264         l = (l & BN_MASK2l) << BN_BITS4;
265     }
266     ret |= q;
267     return ret;
268 }
269 #endif                          /* !defined(BN_LLONG) && defined(BN_DIV2W) */
270 
271 #ifdef BN_LLONG
bn_add_words(BN_ULONG * r,const BN_ULONG * a,const BN_ULONG * b,int n)272 BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
273                       int n)
274 {
275     BN_ULLONG ll = 0;
276 
277     assert(n >= 0);
278     if (n <= 0)
279         return (BN_ULONG)0;
280 
281 # ifndef OPENSSL_SMALL_FOOTPRINT
282     while (n & ~3) {
283         ll += (BN_ULLONG) a[0] + b[0];
284         r[0] = (BN_ULONG)ll & BN_MASK2;
285         ll >>= BN_BITS2;
286         ll += (BN_ULLONG) a[1] + b[1];
287         r[1] = (BN_ULONG)ll & BN_MASK2;
288         ll >>= BN_BITS2;
289         ll += (BN_ULLONG) a[2] + b[2];
290         r[2] = (BN_ULONG)ll & BN_MASK2;
291         ll >>= BN_BITS2;
292         ll += (BN_ULLONG) a[3] + b[3];
293         r[3] = (BN_ULONG)ll & BN_MASK2;
294         ll >>= BN_BITS2;
295         a += 4;
296         b += 4;
297         r += 4;
298         n -= 4;
299     }
300 # endif
301     while (n) {
302         ll += (BN_ULLONG) a[0] + b[0];
303         r[0] = (BN_ULONG)ll & BN_MASK2;
304         ll >>= BN_BITS2;
305         a++;
306         b++;
307         r++;
308         n--;
309     }
310     return (BN_ULONG)ll;
311 }
312 #else                           /* !BN_LLONG */
bn_add_words(BN_ULONG * r,const BN_ULONG * a,const BN_ULONG * b,int n)313 BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
314                       int n)
315 {
316     BN_ULONG c, l, t;
317 
318     assert(n >= 0);
319     if (n <= 0)
320         return (BN_ULONG)0;
321 
322     c = 0;
323 # ifndef OPENSSL_SMALL_FOOTPRINT
324     while (n & ~3) {
325         t = a[0];
326         t = (t + c) & BN_MASK2;
327         c = (t < c);
328         l = (t + b[0]) & BN_MASK2;
329         c += (l < t);
330         r[0] = l;
331         t = a[1];
332         t = (t + c) & BN_MASK2;
333         c = (t < c);
334         l = (t + b[1]) & BN_MASK2;
335         c += (l < t);
336         r[1] = l;
337         t = a[2];
338         t = (t + c) & BN_MASK2;
339         c = (t < c);
340         l = (t + b[2]) & BN_MASK2;
341         c += (l < t);
342         r[2] = l;
343         t = a[3];
344         t = (t + c) & BN_MASK2;
345         c = (t < c);
346         l = (t + b[3]) & BN_MASK2;
347         c += (l < t);
348         r[3] = l;
349         a += 4;
350         b += 4;
351         r += 4;
352         n -= 4;
353     }
354 # endif
355     while (n) {
356         t = a[0];
357         t = (t + c) & BN_MASK2;
358         c = (t < c);
359         l = (t + b[0]) & BN_MASK2;
360         c += (l < t);
361         r[0] = l;
362         a++;
363         b++;
364         r++;
365         n--;
366     }
367     return (BN_ULONG)c;
368 }
369 #endif                          /* !BN_LLONG */
370 
bn_sub_words(BN_ULONG * r,const BN_ULONG * a,const BN_ULONG * b,int n)371 BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
372                       int n)
373 {
374     BN_ULONG t1, t2;
375     int c = 0;
376 
377     assert(n >= 0);
378     if (n <= 0)
379         return (BN_ULONG)0;
380 
381 #ifndef OPENSSL_SMALL_FOOTPRINT
382     while (n & ~3) {
383         t1 = a[0];
384         t2 = b[0];
385         r[0] = (t1 - t2 - c) & BN_MASK2;
386         if (t1 != t2)
387             c = (t1 < t2);
388         t1 = a[1];
389         t2 = b[1];
390         r[1] = (t1 - t2 - c) & BN_MASK2;
391         if (t1 != t2)
392             c = (t1 < t2);
393         t1 = a[2];
394         t2 = b[2];
395         r[2] = (t1 - t2 - c) & BN_MASK2;
396         if (t1 != t2)
397             c = (t1 < t2);
398         t1 = a[3];
399         t2 = b[3];
400         r[3] = (t1 - t2 - c) & BN_MASK2;
401         if (t1 != t2)
402             c = (t1 < t2);
403         a += 4;
404         b += 4;
405         r += 4;
406         n -= 4;
407     }
408 #endif
409     while (n) {
410         t1 = a[0];
411         t2 = b[0];
412         r[0] = (t1 - t2 - c) & BN_MASK2;
413         if (t1 != t2)
414             c = (t1 < t2);
415         a++;
416         b++;
417         r++;
418         n--;
419     }
420     return c;
421 }
422 
423 #if defined(BN_MUL_COMBA) && !defined(OPENSSL_SMALL_FOOTPRINT)
424 
425 /* mul_add_c(a,b,c0,c1,c2)  -- c+=a*b for three word number c=(c2,c1,c0) */
426 /* mul_add_c2(a,b,c0,c1,c2) -- c+=2*a*b for three word number c=(c2,c1,c0) */
427 /* sqr_add_c(a,i,c0,c1,c2)  -- c+=a[i]^2 for three word number c=(c2,c1,c0) */
428 /*
429  * sqr_add_c2(a,i,c0,c1,c2) -- c+=2*a[i]*a[j] for three word number
430  * c=(c2,c1,c0)
431  */
432 
433 # ifdef BN_LLONG
434 /*
435  * Keep in mind that additions to multiplication result can not
436  * overflow, because its high half cannot be all-ones.
437  */
438 #  define mul_add_c(a,b,c0,c1,c2)       do {    \
439         BN_ULONG hi;                            \
440         BN_ULLONG t = (BN_ULLONG)(a)*(b);       \
441         t += c0;                /* no carry */  \
442         c0 = (BN_ULONG)Lw(t);                   \
443         hi = (BN_ULONG)Hw(t);                   \
444         c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
445         } while(0)
446 
447 #  define mul_add_c2(a,b,c0,c1,c2)      do {    \
448         BN_ULONG hi;                            \
449         BN_ULLONG t = (BN_ULLONG)(a)*(b);       \
450         BN_ULLONG tt = t+c0;    /* no carry */  \
451         c0 = (BN_ULONG)Lw(tt);                  \
452         hi = (BN_ULONG)Hw(tt);                  \
453         c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
454         t += c0;                /* no carry */  \
455         c0 = (BN_ULONG)Lw(t);                   \
456         hi = (BN_ULONG)Hw(t);                   \
457         c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
458         } while(0)
459 
460 #  define sqr_add_c(a,i,c0,c1,c2)       do {    \
461         BN_ULONG hi;                            \
462         BN_ULLONG t = (BN_ULLONG)a[i]*a[i];     \
463         t += c0;                /* no carry */  \
464         c0 = (BN_ULONG)Lw(t);                   \
465         hi = (BN_ULONG)Hw(t);                   \
466         c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
467         } while(0)
468 
469 #  define sqr_add_c2(a,i,j,c0,c1,c2) \
470         mul_add_c2((a)[i],(a)[j],c0,c1,c2)
471 
472 # elif defined(BN_UMULT_LOHI)
473 /*
474  * Keep in mind that additions to hi can not overflow, because
475  * the high word of a multiplication result cannot be all-ones.
476  */
477 #  define mul_add_c(a,b,c0,c1,c2)       do {    \
478         BN_ULONG ta = (a), tb = (b);            \
479         BN_ULONG lo, hi;                        \
480         BN_UMULT_LOHI(lo,hi,ta,tb);             \
481         c0 += lo; hi += (c0<lo)?1:0;            \
482         c1 += hi; c2 += (c1<hi)?1:0;            \
483         } while(0)
484 
485 #  define mul_add_c2(a,b,c0,c1,c2)      do {    \
486         BN_ULONG ta = (a), tb = (b);            \
487         BN_ULONG lo, hi, tt;                    \
488         BN_UMULT_LOHI(lo,hi,ta,tb);             \
489         c0 += lo; tt = hi+((c0<lo)?1:0);        \
490         c1 += tt; c2 += (c1<tt)?1:0;            \
491         c0 += lo; hi += (c0<lo)?1:0;            \
492         c1 += hi; c2 += (c1<hi)?1:0;            \
493         } while(0)
494 
495 #  define sqr_add_c(a,i,c0,c1,c2)       do {    \
496         BN_ULONG ta = (a)[i];                   \
497         BN_ULONG lo, hi;                        \
498         BN_UMULT_LOHI(lo,hi,ta,ta);             \
499         c0 += lo; hi += (c0<lo)?1:0;            \
500         c1 += hi; c2 += (c1<hi)?1:0;            \
501         } while(0)
502 
503 #  define sqr_add_c2(a,i,j,c0,c1,c2)    \
504         mul_add_c2((a)[i],(a)[j],c0,c1,c2)
505 
506 # elif defined(BN_UMULT_HIGH)
507 /*
508  * Keep in mind that additions to hi can not overflow, because
509  * the high word of a multiplication result cannot be all-ones.
510  */
511 #  define mul_add_c(a,b,c0,c1,c2)       do {    \
512         BN_ULONG ta = (a), tb = (b);            \
513         BN_ULONG lo = ta * tb;                  \
514         BN_ULONG hi = BN_UMULT_HIGH(ta,tb);     \
515         c0 += lo; hi += (c0<lo)?1:0;            \
516         c1 += hi; c2 += (c1<hi)?1:0;            \
517         } while(0)
518 
519 #  define mul_add_c2(a,b,c0,c1,c2)      do {    \
520         BN_ULONG ta = (a), tb = (b), tt;        \
521         BN_ULONG lo = ta * tb;                  \
522         BN_ULONG hi = BN_UMULT_HIGH(ta,tb);     \
523         c0 += lo; tt = hi + ((c0<lo)?1:0);      \
524         c1 += tt; c2 += (c1<tt)?1:0;            \
525         c0 += lo; hi += (c0<lo)?1:0;            \
526         c1 += hi; c2 += (c1<hi)?1:0;            \
527         } while(0)
528 
529 #  define sqr_add_c(a,i,c0,c1,c2)       do {    \
530         BN_ULONG ta = (a)[i];                   \
531         BN_ULONG lo = ta * ta;                  \
532         BN_ULONG hi = BN_UMULT_HIGH(ta,ta);     \
533         c0 += lo; hi += (c0<lo)?1:0;            \
534         c1 += hi; c2 += (c1<hi)?1:0;            \
535         } while(0)
536 
537 #  define sqr_add_c2(a,i,j,c0,c1,c2)      \
538         mul_add_c2((a)[i],(a)[j],c0,c1,c2)
539 
540 # else                          /* !BN_LLONG */
541 /*
542  * Keep in mind that additions to hi can not overflow, because
543  * the high word of a multiplication result cannot be all-ones.
544  */
545 #  define mul_add_c(a,b,c0,c1,c2)       do {    \
546         BN_ULONG lo = LBITS(a), hi = HBITS(a);  \
547         BN_ULONG bl = LBITS(b), bh = HBITS(b);  \
548         mul64(lo,hi,bl,bh);                     \
549         c0 = (c0+lo)&BN_MASK2; if (c0<lo) hi++; \
550         c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
551         } while(0)
552 
553 #  define mul_add_c2(a,b,c0,c1,c2)      do {    \
554         BN_ULONG tt;                            \
555         BN_ULONG lo = LBITS(a), hi = HBITS(a);  \
556         BN_ULONG bl = LBITS(b), bh = HBITS(b);  \
557         mul64(lo,hi,bl,bh);                     \
558         tt = hi;                                \
559         c0 = (c0+lo)&BN_MASK2; if (c0<lo) tt++; \
560         c1 = (c1+tt)&BN_MASK2; if (c1<tt) c2++; \
561         c0 = (c0+lo)&BN_MASK2; if (c0<lo) hi++; \
562         c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
563         } while(0)
564 
565 #  define sqr_add_c(a,i,c0,c1,c2)       do {    \
566         BN_ULONG lo, hi;                        \
567         sqr64(lo,hi,(a)[i]);                    \
568         c0 = (c0+lo)&BN_MASK2; if (c0<lo) hi++; \
569         c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
570         } while(0)
571 
572 #  define sqr_add_c2(a,i,j,c0,c1,c2) \
573         mul_add_c2((a)[i],(a)[j],c0,c1,c2)
574 # endif                         /* !BN_LLONG */
575 
bn_mul_comba8(BN_ULONG * r,BN_ULONG * a,BN_ULONG * b)576 void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
577 {
578     BN_ULONG c1, c2, c3;
579 
580     c1 = 0;
581     c2 = 0;
582     c3 = 0;
583     mul_add_c(a[0], b[0], c1, c2, c3);
584     r[0] = c1;
585     c1 = 0;
586     mul_add_c(a[0], b[1], c2, c3, c1);
587     mul_add_c(a[1], b[0], c2, c3, c1);
588     r[1] = c2;
589     c2 = 0;
590     mul_add_c(a[2], b[0], c3, c1, c2);
591     mul_add_c(a[1], b[1], c3, c1, c2);
592     mul_add_c(a[0], b[2], c3, c1, c2);
593     r[2] = c3;
594     c3 = 0;
595     mul_add_c(a[0], b[3], c1, c2, c3);
596     mul_add_c(a[1], b[2], c1, c2, c3);
597     mul_add_c(a[2], b[1], c1, c2, c3);
598     mul_add_c(a[3], b[0], c1, c2, c3);
599     r[3] = c1;
600     c1 = 0;
601     mul_add_c(a[4], b[0], c2, c3, c1);
602     mul_add_c(a[3], b[1], c2, c3, c1);
603     mul_add_c(a[2], b[2], c2, c3, c1);
604     mul_add_c(a[1], b[3], c2, c3, c1);
605     mul_add_c(a[0], b[4], c2, c3, c1);
606     r[4] = c2;
607     c2 = 0;
608     mul_add_c(a[0], b[5], c3, c1, c2);
609     mul_add_c(a[1], b[4], c3, c1, c2);
610     mul_add_c(a[2], b[3], c3, c1, c2);
611     mul_add_c(a[3], b[2], c3, c1, c2);
612     mul_add_c(a[4], b[1], c3, c1, c2);
613     mul_add_c(a[5], b[0], c3, c1, c2);
614     r[5] = c3;
615     c3 = 0;
616     mul_add_c(a[6], b[0], c1, c2, c3);
617     mul_add_c(a[5], b[1], c1, c2, c3);
618     mul_add_c(a[4], b[2], c1, c2, c3);
619     mul_add_c(a[3], b[3], c1, c2, c3);
620     mul_add_c(a[2], b[4], c1, c2, c3);
621     mul_add_c(a[1], b[5], c1, c2, c3);
622     mul_add_c(a[0], b[6], c1, c2, c3);
623     r[6] = c1;
624     c1 = 0;
625     mul_add_c(a[0], b[7], c2, c3, c1);
626     mul_add_c(a[1], b[6], c2, c3, c1);
627     mul_add_c(a[2], b[5], c2, c3, c1);
628     mul_add_c(a[3], b[4], c2, c3, c1);
629     mul_add_c(a[4], b[3], c2, c3, c1);
630     mul_add_c(a[5], b[2], c2, c3, c1);
631     mul_add_c(a[6], b[1], c2, c3, c1);
632     mul_add_c(a[7], b[0], c2, c3, c1);
633     r[7] = c2;
634     c2 = 0;
635     mul_add_c(a[7], b[1], c3, c1, c2);
636     mul_add_c(a[6], b[2], c3, c1, c2);
637     mul_add_c(a[5], b[3], c3, c1, c2);
638     mul_add_c(a[4], b[4], c3, c1, c2);
639     mul_add_c(a[3], b[5], c3, c1, c2);
640     mul_add_c(a[2], b[6], c3, c1, c2);
641     mul_add_c(a[1], b[7], c3, c1, c2);
642     r[8] = c3;
643     c3 = 0;
644     mul_add_c(a[2], b[7], c1, c2, c3);
645     mul_add_c(a[3], b[6], c1, c2, c3);
646     mul_add_c(a[4], b[5], c1, c2, c3);
647     mul_add_c(a[5], b[4], c1, c2, c3);
648     mul_add_c(a[6], b[3], c1, c2, c3);
649     mul_add_c(a[7], b[2], c1, c2, c3);
650     r[9] = c1;
651     c1 = 0;
652     mul_add_c(a[7], b[3], c2, c3, c1);
653     mul_add_c(a[6], b[4], c2, c3, c1);
654     mul_add_c(a[5], b[5], c2, c3, c1);
655     mul_add_c(a[4], b[6], c2, c3, c1);
656     mul_add_c(a[3], b[7], c2, c3, c1);
657     r[10] = c2;
658     c2 = 0;
659     mul_add_c(a[4], b[7], c3, c1, c2);
660     mul_add_c(a[5], b[6], c3, c1, c2);
661     mul_add_c(a[6], b[5], c3, c1, c2);
662     mul_add_c(a[7], b[4], c3, c1, c2);
663     r[11] = c3;
664     c3 = 0;
665     mul_add_c(a[7], b[5], c1, c2, c3);
666     mul_add_c(a[6], b[6], c1, c2, c3);
667     mul_add_c(a[5], b[7], c1, c2, c3);
668     r[12] = c1;
669     c1 = 0;
670     mul_add_c(a[6], b[7], c2, c3, c1);
671     mul_add_c(a[7], b[6], c2, c3, c1);
672     r[13] = c2;
673     c2 = 0;
674     mul_add_c(a[7], b[7], c3, c1, c2);
675     r[14] = c3;
676     r[15] = c1;
677 }
678 
bn_mul_comba4(BN_ULONG * r,BN_ULONG * a,BN_ULONG * b)679 void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
680 {
681     BN_ULONG c1, c2, c3;
682 
683     c1 = 0;
684     c2 = 0;
685     c3 = 0;
686     mul_add_c(a[0], b[0], c1, c2, c3);
687     r[0] = c1;
688     c1 = 0;
689     mul_add_c(a[0], b[1], c2, c3, c1);
690     mul_add_c(a[1], b[0], c2, c3, c1);
691     r[1] = c2;
692     c2 = 0;
693     mul_add_c(a[2], b[0], c3, c1, c2);
694     mul_add_c(a[1], b[1], c3, c1, c2);
695     mul_add_c(a[0], b[2], c3, c1, c2);
696     r[2] = c3;
697     c3 = 0;
698     mul_add_c(a[0], b[3], c1, c2, c3);
699     mul_add_c(a[1], b[2], c1, c2, c3);
700     mul_add_c(a[2], b[1], c1, c2, c3);
701     mul_add_c(a[3], b[0], c1, c2, c3);
702     r[3] = c1;
703     c1 = 0;
704     mul_add_c(a[3], b[1], c2, c3, c1);
705     mul_add_c(a[2], b[2], c2, c3, c1);
706     mul_add_c(a[1], b[3], c2, c3, c1);
707     r[4] = c2;
708     c2 = 0;
709     mul_add_c(a[2], b[3], c3, c1, c2);
710     mul_add_c(a[3], b[2], c3, c1, c2);
711     r[5] = c3;
712     c3 = 0;
713     mul_add_c(a[3], b[3], c1, c2, c3);
714     r[6] = c1;
715     r[7] = c2;
716 }
717 
bn_sqr_comba8(BN_ULONG * r,const BN_ULONG * a)718 void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a)
719 {
720     BN_ULONG c1, c2, c3;
721 
722     c1 = 0;
723     c2 = 0;
724     c3 = 0;
725     sqr_add_c(a, 0, c1, c2, c3);
726     r[0] = c1;
727     c1 = 0;
728     sqr_add_c2(a, 1, 0, c2, c3, c1);
729     r[1] = c2;
730     c2 = 0;
731     sqr_add_c(a, 1, c3, c1, c2);
732     sqr_add_c2(a, 2, 0, c3, c1, c2);
733     r[2] = c3;
734     c3 = 0;
735     sqr_add_c2(a, 3, 0, c1, c2, c3);
736     sqr_add_c2(a, 2, 1, c1, c2, c3);
737     r[3] = c1;
738     c1 = 0;
739     sqr_add_c(a, 2, c2, c3, c1);
740     sqr_add_c2(a, 3, 1, c2, c3, c1);
741     sqr_add_c2(a, 4, 0, c2, c3, c1);
742     r[4] = c2;
743     c2 = 0;
744     sqr_add_c2(a, 5, 0, c3, c1, c2);
745     sqr_add_c2(a, 4, 1, c3, c1, c2);
746     sqr_add_c2(a, 3, 2, c3, c1, c2);
747     r[5] = c3;
748     c3 = 0;
749     sqr_add_c(a, 3, c1, c2, c3);
750     sqr_add_c2(a, 4, 2, c1, c2, c3);
751     sqr_add_c2(a, 5, 1, c1, c2, c3);
752     sqr_add_c2(a, 6, 0, c1, c2, c3);
753     r[6] = c1;
754     c1 = 0;
755     sqr_add_c2(a, 7, 0, c2, c3, c1);
756     sqr_add_c2(a, 6, 1, c2, c3, c1);
757     sqr_add_c2(a, 5, 2, c2, c3, c1);
758     sqr_add_c2(a, 4, 3, c2, c3, c1);
759     r[7] = c2;
760     c2 = 0;
761     sqr_add_c(a, 4, c3, c1, c2);
762     sqr_add_c2(a, 5, 3, c3, c1, c2);
763     sqr_add_c2(a, 6, 2, c3, c1, c2);
764     sqr_add_c2(a, 7, 1, c3, c1, c2);
765     r[8] = c3;
766     c3 = 0;
767     sqr_add_c2(a, 7, 2, c1, c2, c3);
768     sqr_add_c2(a, 6, 3, c1, c2, c3);
769     sqr_add_c2(a, 5, 4, c1, c2, c3);
770     r[9] = c1;
771     c1 = 0;
772     sqr_add_c(a, 5, c2, c3, c1);
773     sqr_add_c2(a, 6, 4, c2, c3, c1);
774     sqr_add_c2(a, 7, 3, c2, c3, c1);
775     r[10] = c2;
776     c2 = 0;
777     sqr_add_c2(a, 7, 4, c3, c1, c2);
778     sqr_add_c2(a, 6, 5, c3, c1, c2);
779     r[11] = c3;
780     c3 = 0;
781     sqr_add_c(a, 6, c1, c2, c3);
782     sqr_add_c2(a, 7, 5, c1, c2, c3);
783     r[12] = c1;
784     c1 = 0;
785     sqr_add_c2(a, 7, 6, c2, c3, c1);
786     r[13] = c2;
787     c2 = 0;
788     sqr_add_c(a, 7, c3, c1, c2);
789     r[14] = c3;
790     r[15] = c1;
791 }
792 
bn_sqr_comba4(BN_ULONG * r,const BN_ULONG * a)793 void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a)
794 {
795     BN_ULONG c1, c2, c3;
796 
797     c1 = 0;
798     c2 = 0;
799     c3 = 0;
800     sqr_add_c(a, 0, c1, c2, c3);
801     r[0] = c1;
802     c1 = 0;
803     sqr_add_c2(a, 1, 0, c2, c3, c1);
804     r[1] = c2;
805     c2 = 0;
806     sqr_add_c(a, 1, c3, c1, c2);
807     sqr_add_c2(a, 2, 0, c3, c1, c2);
808     r[2] = c3;
809     c3 = 0;
810     sqr_add_c2(a, 3, 0, c1, c2, c3);
811     sqr_add_c2(a, 2, 1, c1, c2, c3);
812     r[3] = c1;
813     c1 = 0;
814     sqr_add_c(a, 2, c2, c3, c1);
815     sqr_add_c2(a, 3, 1, c2, c3, c1);
816     r[4] = c2;
817     c2 = 0;
818     sqr_add_c2(a, 3, 2, c3, c1, c2);
819     r[5] = c3;
820     c3 = 0;
821     sqr_add_c(a, 3, c1, c2, c3);
822     r[6] = c1;
823     r[7] = c2;
824 }
825 
826 # ifdef OPENSSL_NO_ASM
827 #  ifdef OPENSSL_BN_ASM_MONT
828 #   include <alloca.h>
829 /*
830  * This is essentially reference implementation, which may or may not
831  * result in performance improvement. E.g. on IA-32 this routine was
832  * observed to give 40% faster rsa1024 private key operations and 10%
833  * faster rsa4096 ones, while on AMD64 it improves rsa1024 sign only
834  * by 10% and *worsens* rsa4096 sign by 15%. Once again, it's a
835  * reference implementation, one to be used as starting point for
836  * platform-specific assembler. Mentioned numbers apply to compiler
837  * generated code compiled with and without -DOPENSSL_BN_ASM_MONT and
838  * can vary not only from platform to platform, but even for compiler
839  * versions. Assembler vs. assembler improvement coefficients can
840  * [and are known to] differ and are to be documented elsewhere.
841  */
bn_mul_mont(BN_ULONG * rp,const BN_ULONG * ap,const BN_ULONG * bp,const BN_ULONG * np,const BN_ULONG * n0p,int num)842 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
843                 const BN_ULONG *np, const BN_ULONG *n0p, int num)
844 {
845     BN_ULONG c0, c1, ml, *tp, n0;
846 #   ifdef mul64
847     BN_ULONG mh;
848 #   endif
849     volatile BN_ULONG *vp;
850     int i = 0, j;
851 
852 #   if 0                        /* template for platform-specific
853                                  * implementation */
854     if (ap == bp)
855         return bn_sqr_mont(rp, ap, np, n0p, num);
856 #   endif
857     vp = tp = alloca((num + 2) * sizeof(BN_ULONG));
858 
859     n0 = *n0p;
860 
861     c0 = 0;
862     ml = bp[0];
863 #   ifdef mul64
864     mh = HBITS(ml);
865     ml = LBITS(ml);
866     for (j = 0; j < num; ++j)
867         mul(tp[j], ap[j], ml, mh, c0);
868 #   else
869     for (j = 0; j < num; ++j)
870         mul(tp[j], ap[j], ml, c0);
871 #   endif
872 
873     tp[num] = c0;
874     tp[num + 1] = 0;
875     goto enter;
876 
877     for (i = 0; i < num; i++) {
878         c0 = 0;
879         ml = bp[i];
880 #   ifdef mul64
881         mh = HBITS(ml);
882         ml = LBITS(ml);
883         for (j = 0; j < num; ++j)
884             mul_add(tp[j], ap[j], ml, mh, c0);
885 #   else
886         for (j = 0; j < num; ++j)
887             mul_add(tp[j], ap[j], ml, c0);
888 #   endif
889         c1 = (tp[num] + c0) & BN_MASK2;
890         tp[num] = c1;
891         tp[num + 1] = (c1 < c0 ? 1 : 0);
892  enter:
893         c1 = tp[0];
894         ml = (c1 * n0) & BN_MASK2;
895         c0 = 0;
896 #   ifdef mul64
897         mh = HBITS(ml);
898         ml = LBITS(ml);
899         mul_add(c1, np[0], ml, mh, c0);
900 #   else
901         mul_add(c1, ml, np[0], c0);
902 #   endif
903         for (j = 1; j < num; j++) {
904             c1 = tp[j];
905 #   ifdef mul64
906             mul_add(c1, np[j], ml, mh, c0);
907 #   else
908             mul_add(c1, ml, np[j], c0);
909 #   endif
910             tp[j - 1] = c1 & BN_MASK2;
911         }
912         c1 = (tp[num] + c0) & BN_MASK2;
913         tp[num - 1] = c1;
914         tp[num] = tp[num + 1] + (c1 < c0 ? 1 : 0);
915     }
916 
917     if (tp[num] != 0 || tp[num - 1] >= np[num - 1]) {
918         c0 = bn_sub_words(rp, tp, np, num);
919         if (tp[num] != 0 || c0 == 0) {
920             for (i = 0; i < num + 2; i++)
921                 vp[i] = 0;
922             return 1;
923         }
924     }
925     for (i = 0; i < num; i++)
926         rp[i] = tp[i], vp[i] = 0;
927     vp[num] = 0;
928     vp[num + 1] = 0;
929     return 1;
930 }
931 #  else
932 /*
933  * Return value of 0 indicates that multiplication/convolution was not
934  * performed to signal the caller to fall down to alternative/original
935  * code-path.
936  */
bn_mul_mont(BN_ULONG * rp,const BN_ULONG * ap,const BN_ULONG * bp,const BN_ULONG * np,const BN_ULONG * n0,int num)937 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
938                 const BN_ULONG *np, const BN_ULONG *n0, int num)
939 {
940     return 0;
941 }
942 #  endif                        /* OPENSSL_BN_ASM_MONT */
943 # endif
944 
945 #else                           /* !BN_MUL_COMBA */
946 
947 /* hmm... is it faster just to do a multiply? */
bn_sqr_comba4(BN_ULONG * r,const BN_ULONG * a)948 void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a)
949 {
950     BN_ULONG t[8];
951     bn_sqr_normal(r, a, 4, t);
952 }
953 
bn_sqr_comba8(BN_ULONG * r,const BN_ULONG * a)954 void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a)
955 {
956     BN_ULONG t[16];
957     bn_sqr_normal(r, a, 8, t);
958 }
959 
bn_mul_comba4(BN_ULONG * r,BN_ULONG * a,BN_ULONG * b)960 void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
961 {
962     r[4] = bn_mul_words(&(r[0]), a, 4, b[0]);
963     r[5] = bn_mul_add_words(&(r[1]), a, 4, b[1]);
964     r[6] = bn_mul_add_words(&(r[2]), a, 4, b[2]);
965     r[7] = bn_mul_add_words(&(r[3]), a, 4, b[3]);
966 }
967 
bn_mul_comba8(BN_ULONG * r,BN_ULONG * a,BN_ULONG * b)968 void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
969 {
970     r[8] = bn_mul_words(&(r[0]), a, 8, b[0]);
971     r[9] = bn_mul_add_words(&(r[1]), a, 8, b[1]);
972     r[10] = bn_mul_add_words(&(r[2]), a, 8, b[2]);
973     r[11] = bn_mul_add_words(&(r[3]), a, 8, b[3]);
974     r[12] = bn_mul_add_words(&(r[4]), a, 8, b[4]);
975     r[13] = bn_mul_add_words(&(r[5]), a, 8, b[5]);
976     r[14] = bn_mul_add_words(&(r[6]), a, 8, b[6]);
977     r[15] = bn_mul_add_words(&(r[7]), a, 8, b[7]);
978 }
979 
980 # ifdef OPENSSL_NO_ASM
981 #  ifdef OPENSSL_BN_ASM_MONT
982 #   include <alloca.h>
bn_mul_mont(BN_ULONG * rp,const BN_ULONG * ap,const BN_ULONG * bp,const BN_ULONG * np,const BN_ULONG * n0p,int num)983 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
984                 const BN_ULONG *np, const BN_ULONG *n0p, int num)
985 {
986     BN_ULONG c0, c1, *tp, n0 = *n0p;
987     volatile BN_ULONG *vp;
988     int i = 0, j;
989 
990     vp = tp = alloca((num + 2) * sizeof(BN_ULONG));
991 
992     for (i = 0; i <= num; i++)
993         tp[i] = 0;
994 
995     for (i = 0; i < num; i++) {
996         c0 = bn_mul_add_words(tp, ap, num, bp[i]);
997         c1 = (tp[num] + c0) & BN_MASK2;
998         tp[num] = c1;
999         tp[num + 1] = (c1 < c0 ? 1 : 0);
1000 
1001         c0 = bn_mul_add_words(tp, np, num, tp[0] * n0);
1002         c1 = (tp[num] + c0) & BN_MASK2;
1003         tp[num] = c1;
1004         tp[num + 1] += (c1 < c0 ? 1 : 0);
1005         for (j = 0; j <= num; j++)
1006             tp[j] = tp[j + 1];
1007     }
1008 
1009     if (tp[num] != 0 || tp[num - 1] >= np[num - 1]) {
1010         c0 = bn_sub_words(rp, tp, np, num);
1011         if (tp[num] != 0 || c0 == 0) {
1012             for (i = 0; i < num + 2; i++)
1013                 vp[i] = 0;
1014             return 1;
1015         }
1016     }
1017     for (i = 0; i < num; i++)
1018         rp[i] = tp[i], vp[i] = 0;
1019     vp[num] = 0;
1020     vp[num + 1] = 0;
1021     return 1;
1022 }
1023 #  else
bn_mul_mont(BN_ULONG * rp,const BN_ULONG * ap,const BN_ULONG * bp,const BN_ULONG * np,const BN_ULONG * n0,int num)1024 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
1025                 const BN_ULONG *np, const BN_ULONG *n0, int num)
1026 {
1027     return 0;
1028 }
1029 #  endif                        /* OPENSSL_BN_ASM_MONT */
1030 # endif
1031 
1032 #endif                          /* !BN_MUL_COMBA */
1033