Coverage Report

Created: 2023-06-08 06:40

/src/openssl111/crypto/bn/bn_mul.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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 "internal/cryptlib.h"
12
#include "bn_local.h"
13
14
#if defined(OPENSSL_NO_ASM) || !defined(OPENSSL_BN_ASM_PART_WORDS)
15
/*
16
 * Here follows specialised variants of bn_add_words() and bn_sub_words().
17
 * They have the property performing operations on arrays of different sizes.
18
 * The sizes of those arrays is expressed through cl, which is the common
19
 * length ( basically, min(len(a),len(b)) ), and dl, which is the delta
20
 * between the two lengths, calculated as len(a)-len(b). All lengths are the
21
 * number of BN_ULONGs...  For the operations that require a result array as
22
 * parameter, it must have the length cl+abs(dl). These functions should
23
 * probably end up in bn_asm.c as soon as there are assembler counterparts
24
 * for the systems that use assembler files.
25
 */
26
27
BN_ULONG bn_sub_part_words(BN_ULONG *r,
28
                           const BN_ULONG *a, const BN_ULONG *b,
29
                           int cl, int dl)
30
1.15M
{
31
1.15M
    BN_ULONG c, t;
32
33
1.15M
    assert(cl >= 0);
34
1.15M
    c = bn_sub_words(r, a, b, cl);
35
36
1.15M
    if (dl == 0)
37
1.05M
        return c;
38
39
95.7k
    r += cl;
40
95.7k
    a += cl;
41
95.7k
    b += cl;
42
43
95.7k
    if (dl < 0) {
44
28.6k
        for (;;) {
45
28.6k
            t = b[0];
46
28.6k
            r[0] = (0 - t - c) & BN_MASK2;
47
28.6k
            if (t != 0)
48
0
                c = 1;
49
28.6k
            if (++dl >= 0)
50
2.02k
                break;
51
52
26.5k
            t = b[1];
53
26.5k
            r[1] = (0 - t - c) & BN_MASK2;
54
26.5k
            if (t != 0)
55
0
                c = 1;
56
26.5k
            if (++dl >= 0)
57
1.25k
                break;
58
59
25.3k
            t = b[2];
60
25.3k
            r[2] = (0 - t - c) & BN_MASK2;
61
25.3k
            if (t != 0)
62
0
                c = 1;
63
25.3k
            if (++dl >= 0)
64
5.47k
                break;
65
66
19.8k
            t = b[3];
67
19.8k
            r[3] = (0 - t - c) & BN_MASK2;
68
19.8k
            if (t != 0)
69
0
                c = 1;
70
19.8k
            if (++dl >= 0)
71
1.17k
                break;
72
73
18.6k
            b += 4;
74
18.6k
            r += 4;
75
18.6k
        }
76
85.7k
    } else {
77
85.7k
        int save_dl = dl;
78
104k
        while (c) {
79
25.6k
            t = a[0];
80
25.6k
            r[0] = (t - c) & BN_MASK2;
81
25.6k
            if (t != 0)
82
11.2k
                c = 0;
83
25.6k
            if (--dl <= 0)
84
2.29k
                break;
85
86
23.3k
            t = a[1];
87
23.3k
            r[1] = (t - c) & BN_MASK2;
88
23.3k
            if (t != 0)
89
9.76k
                c = 0;
90
23.3k
            if (--dl <= 0)
91
1.20k
                break;
92
93
22.1k
            t = a[2];
94
22.1k
            r[2] = (t - c) & BN_MASK2;
95
22.1k
            if (t != 0)
96
10.6k
                c = 0;
97
22.1k
            if (--dl <= 0)
98
2.38k
                break;
99
100
19.7k
            t = a[3];
101
19.7k
            r[3] = (t - c) & BN_MASK2;
102
19.7k
            if (t != 0)
103
9.50k
                c = 0;
104
19.7k
            if (--dl <= 0)
105
1.37k
                break;
106
107
18.3k
            save_dl = dl;
108
18.3k
            a += 4;
109
18.3k
            r += 4;
110
18.3k
        }
111
85.7k
        if (dl > 0) {
112
78.5k
            if (save_dl > dl) {
113
0
                switch (save_dl - dl) {
114
0
                case 1:
115
0
                    r[1] = a[1];
116
0
                    if (--dl <= 0)
117
0
                        break;
118
                    /* fall thru */
119
0
                case 2:
120
0
                    r[2] = a[2];
121
0
                    if (--dl <= 0)
122
0
                        break;
123
                    /* fall thru */
124
0
                case 3:
125
0
                    r[3] = a[3];
126
0
                    if (--dl <= 0)
127
0
                        break;
128
0
                }
129
0
                a += 4;
130
0
                r += 4;
131
0
            }
132
78.5k
        }
133
85.7k
        if (dl > 0) {
134
456k
            for (;;) {
135
456k
                r[0] = a[0];
136
456k
                if (--dl <= 0)
137
13.6k
                    break;
138
442k
                r[1] = a[1];
139
442k
                if (--dl <= 0)
140
6.96k
                    break;
141
435k
                r[2] = a[2];
142
435k
                if (--dl <= 0)
143
39.6k
                    break;
144
395k
                r[3] = a[3];
145
395k
                if (--dl <= 0)
146
18.2k
                    break;
147
148
377k
                a += 4;
149
377k
                r += 4;
150
377k
            }
151
78.5k
        }
152
85.7k
    }
153
95.7k
    return c;
154
95.7k
}
155
#endif
156
157
#ifdef BN_RECURSION
158
/*
159
 * Karatsuba recursive multiplication algorithm (cf. Knuth, The Art of
160
 * Computer Programming, Vol. 2)
161
 */
162
163
/*-
164
 * r is 2*n2 words in size,
165
 * a and b are both n2 words in size.
166
 * n2 must be a power of 2.
167
 * We multiply and return the result.
168
 * t must be 2*n2 words in size
169
 * We calculate
170
 * a[0]*b[0]
171
 * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
172
 * a[1]*b[1]
173
 */
174
/* dnX may not be positive, but n2/2+dnX has to be */
175
void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
176
                      int dna, int dnb, BN_ULONG *t)
177
558k
{
178
558k
    int n = n2 / 2, c1, c2;
179
558k
    int tna = n + dna, tnb = n + dnb;
180
558k
    unsigned int neg, zero;
181
558k
    BN_ULONG ln, lo, *p;
182
183
558k
# ifdef BN_MUL_COMBA
184
#  if 0
185
    if (n2 == 4) {
186
        bn_mul_comba4(r, a, b);
187
        return;
188
    }
189
#  endif
190
    /*
191
     * Only call bn_mul_comba 8 if n2 == 8 and the two arrays are complete
192
     * [steve]
193
     */
194
558k
    if (n2 == 8 && dna == 0 && dnb == 0) {
195
3.92k
        bn_mul_comba8(r, a, b);
196
3.92k
        return;
197
3.92k
    }
198
554k
# endif                         /* BN_MUL_COMBA */
199
    /* Else do normal multiply */
200
554k
    if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) {
201
2.89k
        bn_mul_normal(r, a, n2 + dna, b, n2 + dnb);
202
2.89k
        if ((dna + dnb) < 0)
203
2.89k
            memset(&r[2 * n2 + dna + dnb], 0,
204
2.89k
                   sizeof(BN_ULONG) * -(dna + dnb));
205
2.89k
        return;
206
2.89k
    }
207
    /* r=(a[0]-a[1])*(b[1]-b[0]) */
208
552k
    c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
209
552k
    c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
210
552k
    zero = neg = 0;
211
552k
    switch (c1 * 3 + c2) {
212
128k
    case -4:
213
128k
        bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
214
128k
        bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
215
128k
        break;
216
4.85k
    case -3:
217
4.85k
        zero = 1;
218
4.85k
        break;
219
105k
    case -2:
220
105k
        bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
221
105k
        bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
222
105k
        neg = 1;
223
105k
        break;
224
4.44k
    case -1:
225
6.79k
    case 0:
226
12.0k
    case 1:
227
12.0k
        zero = 1;
228
12.0k
        break;
229
180k
    case 2:
230
180k
        bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
231
180k
        bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
232
180k
        neg = 1;
233
180k
        break;
234
5.53k
    case 3:
235
5.53k
        zero = 1;
236
5.53k
        break;
237
114k
    case 4:
238
114k
        bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
239
114k
        bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
240
114k
        break;
241
552k
    }
242
243
552k
# ifdef BN_MUL_COMBA
244
552k
    if (n == 4 && dna == 0 && dnb == 0) { /* XXX: bn_mul_comba4 could take
245
                                           * extra args to do this well */
246
0
        if (!zero)
247
0
            bn_mul_comba4(&(t[n2]), t, &(t[n]));
248
0
        else
249
0
            memset(&t[n2], 0, sizeof(*t) * 8);
250
251
0
        bn_mul_comba4(r, a, b);
252
0
        bn_mul_comba4(&(r[n2]), &(a[n]), &(b[n]));
253
552k
    } else if (n == 8 && dna == 0 && dnb == 0) { /* XXX: bn_mul_comba8 could
254
                                                  * take extra args to do
255
                                                  * this well */
256
396k
        if (!zero)
257
378k
            bn_mul_comba8(&(t[n2]), t, &(t[n]));
258
18.2k
        else
259
18.2k
            memset(&t[n2], 0, sizeof(*t) * 16);
260
261
396k
        bn_mul_comba8(r, a, b);
262
396k
        bn_mul_comba8(&(r[n2]), &(a[n]), &(b[n]));
263
396k
    } else
264
155k
# endif                         /* BN_MUL_COMBA */
265
155k
    {
266
155k
        p = &(t[n2 * 2]);
267
155k
        if (!zero)
268
151k
            bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
269
4.17k
        else
270
4.17k
            memset(&t[n2], 0, sizeof(*t) * n2);
271
155k
        bn_mul_recursive(r, a, b, n, 0, 0, p);
272
155k
        bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), n, dna, dnb, p);
273
155k
    }
274
275
    /*-
276
     * t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
277
     * r[10] holds (a[0]*b[0])
278
     * r[32] holds (b[1]*b[1])
279
     */
280
281
552k
    c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
282
283
552k
    if (neg) {                  /* if t[32] is negative */
284
285k
        c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
285
285k
    } else {
286
        /* Might have a carry */
287
266k
        c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
288
266k
    }
289
290
    /*-
291
     * t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
292
     * r[10] holds (a[0]*b[0])
293
     * r[32] holds (b[1]*b[1])
294
     * c1 holds the carry bits
295
     */
296
552k
    c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
297
552k
    if (c1) {
298
209k
        p = &(r[n + n2]);
299
209k
        lo = *p;
300
209k
        ln = (lo + c1) & BN_MASK2;
301
209k
        *p = ln;
302
303
        /*
304
         * The overflow will stop before we over write words we should not
305
         * overwrite
306
         */
307
209k
        if (ln < (BN_ULONG)c1) {
308
4.99k
            do {
309
4.99k
                p++;
310
4.99k
                lo = *p;
311
4.99k
                ln = (lo + 1) & BN_MASK2;
312
4.99k
                *p = ln;
313
4.99k
            } while (ln == 0);
314
1.65k
        }
315
209k
    }
316
552k
}
317
318
/*
319
 * n+tn is the word length t needs to be n*4 is size, as does r
320
 */
321
/* tnX may not be negative but less than n */
322
void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,
323
                           int tna, int tnb, BN_ULONG *t)
324
46.2k
{
325
46.2k
    int i, j, n2 = n * 2;
326
46.2k
    int c1, c2, neg;
327
46.2k
    BN_ULONG ln, lo, *p;
328
329
46.2k
    if (n < 8) {
330
0
        bn_mul_normal(r, a, n + tna, b, n + tnb);
331
0
        return;
332
0
    }
333
334
    /* r=(a[0]-a[1])*(b[1]-b[0]) */
335
46.2k
    c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
336
46.2k
    c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
337
46.2k
    neg = 0;
338
46.2k
    switch (c1 * 3 + c2) {
339
4.75k
    case -4:
340
4.75k
        bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
341
4.75k
        bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
342
4.75k
        break;
343
291
    case -3:
344
1.63k
    case -2:
345
1.63k
        bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
346
1.63k
        bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
347
1.63k
        neg = 1;
348
1.63k
        break;
349
890
    case -1:
350
1.09k
    case 0:
351
1.32k
    case 1:
352
38.0k
    case 2:
353
38.0k
        bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
354
38.0k
        bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
355
38.0k
        neg = 1;
356
38.0k
        break;
357
518
    case 3:
358
1.83k
    case 4:
359
1.83k
        bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
360
1.83k
        bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
361
1.83k
        break;
362
46.2k
    }
363
    /*
364
     * The zero case isn't yet implemented here. The speedup would probably
365
     * be negligible.
366
     */
367
# if 0
368
    if (n == 4) {
369
        bn_mul_comba4(&(t[n2]), t, &(t[n]));
370
        bn_mul_comba4(r, a, b);
371
        bn_mul_normal(&(r[n2]), &(a[n]), tn, &(b[n]), tn);
372
        memset(&r[n2 + tn * 2], 0, sizeof(*r) * (n2 - tn * 2));
373
    } else
374
# endif
375
46.2k
    if (n == 8) {
376
5.97k
        bn_mul_comba8(&(t[n2]), t, &(t[n]));
377
5.97k
        bn_mul_comba8(r, a, b);
378
5.97k
        bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
379
5.97k
        memset(&r[n2 + tna + tnb], 0, sizeof(*r) * (n2 - tna - tnb));
380
40.3k
    } else {
381
40.3k
        p = &(t[n2 * 2]);
382
40.3k
        bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
383
40.3k
        bn_mul_recursive(r, a, b, n, 0, 0, p);
384
40.3k
        i = n / 2;
385
        /*
386
         * If there is only a bottom half to the number, just do it
387
         */
388
40.3k
        if (tna > tnb)
389
4.24k
            j = tna - i;
390
36.0k
        else
391
36.0k
            j = tnb - i;
392
40.3k
        if (j == 0) {
393
1.81k
            bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]),
394
1.81k
                             i, tna - i, tnb - i, p);
395
1.81k
            memset(&r[n2 + i * 2], 0, sizeof(*r) * (n2 - i * 2));
396
38.5k
        } else if (j > 0) {     /* eg, n == 16, i == 8 and tn == 11 */
397
9.51k
            bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]),
398
9.51k
                                  i, tna - i, tnb - i, p);
399
9.51k
            memset(&(r[n2 + tna + tnb]), 0,
400
9.51k
                   sizeof(BN_ULONG) * (n2 - tna - tnb));
401
28.9k
        } else {                /* (j < 0) eg, n == 16, i == 8 and tn == 5 */
402
403
28.9k
            memset(&r[n2], 0, sizeof(*r) * n2);
404
28.9k
            if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL
405
28.9k
                && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {
406
22.4k
                bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
407
22.4k
            } else {
408
7.11k
                for (;;) {
409
7.11k
                    i /= 2;
410
                    /*
411
                     * these simplified conditions work exclusively because
412
                     * difference between tna and tnb is 1 or 0
413
                     */
414
7.11k
                    if (i < tna || i < tnb) {
415
5.50k
                        bn_mul_part_recursive(&(r[n2]),
416
5.50k
                                              &(a[n]), &(b[n]),
417
5.50k
                                              i, tna - i, tnb - i, p);
418
5.50k
                        break;
419
5.50k
                    } else if (i == tna || i == tnb) {
420
996
                        bn_mul_recursive(&(r[n2]),
421
996
                                         &(a[n]), &(b[n]),
422
996
                                         i, tna - i, tnb - i, p);
423
996
                        break;
424
996
                    }
425
7.11k
                }
426
6.49k
            }
427
28.9k
        }
428
40.3k
    }
429
430
    /*-
431
     * t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
432
     * r[10] holds (a[0]*b[0])
433
     * r[32] holds (b[1]*b[1])
434
     */
435
436
46.2k
    c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
437
438
46.2k
    if (neg) {                  /* if t[32] is negative */
439
39.6k
        c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
440
39.6k
    } else {
441
        /* Might have a carry */
442
6.59k
        c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
443
6.59k
    }
444
445
    /*-
446
     * t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
447
     * r[10] holds (a[0]*b[0])
448
     * r[32] holds (b[1]*b[1])
449
     * c1 holds the carry bits
450
     */
451
46.2k
    c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
452
46.2k
    if (c1) {
453
1.07k
        p = &(r[n + n2]);
454
1.07k
        lo = *p;
455
1.07k
        ln = (lo + c1) & BN_MASK2;
456
1.07k
        *p = ln;
457
458
        /*
459
         * The overflow will stop before we over write words we should not
460
         * overwrite
461
         */
462
1.07k
        if (ln < (BN_ULONG)c1) {
463
956
            do {
464
956
                p++;
465
956
                lo = *p;
466
956
                ln = (lo + 1) & BN_MASK2;
467
956
                *p = ln;
468
956
            } while (ln == 0);
469
336
        }
470
1.07k
    }
471
46.2k
}
472
473
/*-
474
 * a and b must be the same size, which is n2.
475
 * r needs to be n2 words and t needs to be n2*2
476
 */
477
void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
478
                          BN_ULONG *t)
479
0
{
480
0
    int n = n2 / 2;
481
482
0
    bn_mul_recursive(r, a, b, n, 0, 0, &(t[0]));
483
0
    if (n >= BN_MUL_LOW_RECURSIVE_SIZE_NORMAL) {
484
0
        bn_mul_low_recursive(&(t[0]), &(a[0]), &(b[n]), n, &(t[n2]));
485
0
        bn_add_words(&(r[n]), &(r[n]), &(t[0]), n);
486
0
        bn_mul_low_recursive(&(t[0]), &(a[n]), &(b[0]), n, &(t[n2]));
487
0
        bn_add_words(&(r[n]), &(r[n]), &(t[0]), n);
488
0
    } else {
489
0
        bn_mul_low_normal(&(t[0]), &(a[0]), &(b[n]), n);
490
0
        bn_mul_low_normal(&(t[n]), &(a[n]), &(b[0]), n);
491
0
        bn_add_words(&(r[n]), &(r[n]), &(t[0]), n);
492
0
        bn_add_words(&(r[n]), &(r[n]), &(t[n]), n);
493
0
    }
494
0
}
495
#endif                          /* BN_RECURSION */
496
497
int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
498
241k
{
499
241k
    int ret = bn_mul_fixed_top(r, a, b, ctx);
500
501
241k
    bn_correct_top(r);
502
241k
    bn_check_top(r);
503
504
241k
    return ret;
505
241k
}
506
507
int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
508
260k
{
509
260k
    int ret = 0;
510
260k
    int top, al, bl;
511
260k
    BIGNUM *rr;
512
260k
#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
513
260k
    int i;
514
260k
#endif
515
260k
#ifdef BN_RECURSION
516
260k
    BIGNUM *t = NULL;
517
260k
    int j = 0, k;
518
260k
#endif
519
520
260k
    bn_check_top(a);
521
260k
    bn_check_top(b);
522
260k
    bn_check_top(r);
523
524
260k
    al = a->top;
525
260k
    bl = b->top;
526
527
260k
    if ((al == 0) || (bl == 0)) {
528
4.92k
        BN_zero(r);
529
4.92k
        return 1;
530
4.92k
    }
531
256k
    top = al + bl;
532
533
256k
    BN_CTX_start(ctx);
534
256k
    if ((r == a) || (r == b)) {
535
0
        if ((rr = BN_CTX_get(ctx)) == NULL)
536
0
            goto err;
537
0
    } else
538
256k
        rr = r;
539
540
256k
#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
541
256k
    i = al - bl;
542
256k
#endif
543
256k
#ifdef BN_MUL_COMBA
544
256k
    if (i == 0) {
545
# if 0
546
        if (al == 4) {
547
            if (bn_wexpand(rr, 8) == NULL)
548
                goto err;
549
            rr->top = 8;
550
            bn_mul_comba4(rr->d, a->d, b->d);
551
            goto end;
552
        }
553
# endif
554
192k
        if (al == 8) {
555
556
            if (bn_wexpand(rr, 16) == NULL)
556
0
                goto err;
557
556
            rr->top = 16;
558
556
            bn_mul_comba8(rr->d, a->d, b->d);
559
556
            goto end;
560
556
        }
561
192k
    }
562
255k
#endif                          /* BN_MUL_COMBA */
563
255k
#ifdef BN_RECURSION
564
255k
    if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {
565
46.6k
        if (i >= -1 && i <= 1) {
566
            /*
567
             * Find out the power of two lower or equal to the longest of the
568
             * two numbers
569
             */
570
44.4k
            if (i >= 0) {
571
35.7k
                j = BN_num_bits_word((BN_ULONG)al);
572
35.7k
            }
573
44.4k
            if (i == -1) {
574
8.69k
                j = BN_num_bits_word((BN_ULONG)bl);
575
8.69k
            }
576
44.4k
            j = 1 << (j - 1);
577
44.4k
            assert(j <= al || j <= bl);
578
44.4k
            k = j + j;
579
44.4k
            t = BN_CTX_get(ctx);
580
44.4k
            if (t == NULL)
581
0
                goto err;
582
44.4k
            if (al > j || bl > j) {
583
31.2k
                if (bn_wexpand(t, k * 4) == NULL)
584
0
                    goto err;
585
31.2k
                if (bn_wexpand(rr, k * 4) == NULL)
586
0
                    goto err;
587
31.2k
                bn_mul_part_recursive(rr->d, a->d, b->d,
588
31.2k
                                      j, al - j, bl - j, t->d);
589
31.2k
            } else {            /* al <= j || bl <= j */
590
591
13.1k
                if (bn_wexpand(t, k * 2) == NULL)
592
0
                    goto err;
593
13.1k
                if (bn_wexpand(rr, k * 2) == NULL)
594
0
                    goto err;
595
13.1k
                bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
596
13.1k
            }
597
44.4k
            rr->top = top;
598
44.4k
            goto end;
599
44.4k
        }
600
46.6k
    }
601
211k
#endif                          /* BN_RECURSION */
602
211k
    if (bn_wexpand(rr, top) == NULL)
603
0
        goto err;
604
211k
    rr->top = top;
605
211k
    bn_mul_normal(rr->d, a->d, al, b->d, bl);
606
607
211k
#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
608
256k
 end:
609
256k
#endif
610
256k
    rr->neg = a->neg ^ b->neg;
611
256k
    rr->flags |= BN_FLG_FIXED_TOP;
612
256k
    if (r != rr && BN_copy(r, rr) == NULL)
613
0
        goto err;
614
615
256k
    ret = 1;
616
256k
 err:
617
256k
    bn_check_top(r);
618
256k
    BN_CTX_end(ctx);
619
256k
    return ret;
620
256k
}
621
622
void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
623
242k
{
624
242k
    BN_ULONG *rr;
625
626
242k
    if (na < nb) {
627
49.4k
        int itmp;
628
49.4k
        BN_ULONG *ltmp;
629
630
49.4k
        itmp = na;
631
49.4k
        na = nb;
632
49.4k
        nb = itmp;
633
49.4k
        ltmp = a;
634
49.4k
        a = b;
635
49.4k
        b = ltmp;
636
637
49.4k
    }
638
242k
    rr = &(r[na]);
639
242k
    if (nb <= 0) {
640
6.68k
        (void)bn_mul_words(r, a, na, 0);
641
6.68k
        return;
642
6.68k
    } else
643
235k
        rr[0] = bn_mul_words(r, a, na, b[0]);
644
645
272k
    for (;;) {
646
272k
        if (--nb <= 0)
647
204k
            return;
648
67.6k
        rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);
649
67.6k
        if (--nb <= 0)
650
11.9k
            return;
651
55.6k
        rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);
652
55.6k
        if (--nb <= 0)
653
11.5k
            return;
654
44.0k
        rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);
655
44.0k
        if (--nb <= 0)
656
7.79k
            return;
657
36.2k
        rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);
658
36.2k
        rr += 4;
659
36.2k
        r += 4;
660
36.2k
        b += 4;
661
36.2k
    }
662
235k
}
663
664
void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n)
665
0
{
666
0
    bn_mul_words(r, a, n, b[0]);
667
668
0
    for (;;) {
669
0
        if (--n <= 0)
670
0
            return;
671
0
        bn_mul_add_words(&(r[1]), a, n, b[1]);
672
0
        if (--n <= 0)
673
0
            return;
674
0
        bn_mul_add_words(&(r[2]), a, n, b[2]);
675
0
        if (--n <= 0)
676
0
            return;
677
0
        bn_mul_add_words(&(r[3]), a, n, b[3]);
678
0
        if (--n <= 0)
679
0
            return;
680
0
        bn_mul_add_words(&(r[4]), a, n, b[4]);
681
0
        r += 4;
682
0
        b += 4;
683
0
    }
684
0
}