Coverage Report

Created: 2024-07-27 06:39

/src/openssl32/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 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 "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
49.7M
{
31
49.7M
    BN_ULONG c, t;
32
33
49.7M
    assert(cl >= 0);
34
49.7M
    c = bn_sub_words(r, a, b, cl);
35
36
49.7M
    if (dl == 0)
37
48.8M
        return c;
38
39
873k
    r += cl;
40
873k
    a += cl;
41
873k
    b += cl;
42
43
873k
    if (dl < 0) {
44
255k
        for (;;) {
45
255k
            t = b[0];
46
255k
            r[0] = (0 - t - c) & BN_MASK2;
47
255k
            if (t != 0)
48
0
                c = 1;
49
255k
            if (++dl >= 0)
50
25.2k
                break;
51
52
229k
            t = b[1];
53
229k
            r[1] = (0 - t - c) & BN_MASK2;
54
229k
            if (t != 0)
55
0
                c = 1;
56
229k
            if (++dl >= 0)
57
16.1k
                break;
58
59
213k
            t = b[2];
60
213k
            r[2] = (0 - t - c) & BN_MASK2;
61
213k
            if (t != 0)
62
0
                c = 1;
63
213k
            if (++dl >= 0)
64
33.4k
                break;
65
66
180k
            t = b[3];
67
180k
            r[3] = (0 - t - c) & BN_MASK2;
68
180k
            if (t != 0)
69
0
                c = 1;
70
180k
            if (++dl >= 0)
71
6.27k
                break;
72
73
174k
            b += 4;
74
174k
            r += 4;
75
174k
        }
76
792k
    } else {
77
792k
        int save_dl = dl;
78
951k
        while (c) {
79
220k
            t = a[0];
80
220k
            r[0] = (t - c) & BN_MASK2;
81
220k
            if (t != 0)
82
96.3k
                c = 0;
83
220k
            if (--dl <= 0)
84
21.5k
                break;
85
86
199k
            t = a[1];
87
199k
            r[1] = (t - c) & BN_MASK2;
88
199k
            if (t != 0)
89
75.2k
                c = 0;
90
199k
            if (--dl <= 0)
91
11.2k
                break;
92
93
187k
            t = a[2];
94
187k
            r[2] = (t - c) & BN_MASK2;
95
187k
            if (t != 0)
96
85.8k
                c = 0;
97
187k
            if (--dl <= 0)
98
16.5k
                break;
99
100
171k
            t = a[3];
101
171k
            r[3] = (t - c) & BN_MASK2;
102
171k
            if (t != 0)
103
80.5k
                c = 0;
104
171k
            if (--dl <= 0)
105
12.8k
                break;
106
107
158k
            save_dl = dl;
108
158k
            a += 4;
109
158k
            r += 4;
110
158k
        }
111
792k
        if (dl > 0) {
112
730k
            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 through */
119
0
                case 2:
120
0
                    r[2] = a[2];
121
0
                    if (--dl <= 0)
122
0
                        break;
123
                    /* fall through */
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
730k
        }
133
792k
        if (dl > 0) {
134
6.13M
            for (;;) {
135
6.13M
                r[0] = a[0];
136
6.13M
                if (--dl <= 0)
137
141k
                    break;
138
5.99M
                r[1] = a[1];
139
5.99M
                if (--dl <= 0)
140
87.2k
                    break;
141
5.90M
                r[2] = a[2];
142
5.90M
                if (--dl <= 0)
143
296k
                    break;
144
5.61M
                r[3] = a[3];
145
5.61M
                if (--dl <= 0)
146
204k
                    break;
147
148
5.40M
                a += 4;
149
5.40M
                r += 4;
150
5.40M
            }
151
730k
        }
152
792k
    }
153
873k
    return c;
154
873k
}
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
25.8M
{
178
25.8M
    int n = n2 / 2, c1, c2;
179
25.8M
    int tna = n + dna, tnb = n + dnb;
180
25.8M
    unsigned int neg, zero;
181
25.8M
    BN_ULONG ln, lo, *p;
182
183
25.8M
# 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
25.8M
    if (n2 == 8 && dna == 0 && dnb == 0) {
195
35.9k
        bn_mul_comba8(r, a, b);
196
35.9k
        return;
197
35.9k
    }
198
25.8M
# endif                         /* BN_MUL_COMBA */
199
    /* Else do normal multiply */
200
25.8M
    if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) {
201
22.2k
        bn_mul_normal(r, a, n2 + dna, b, n2 + dnb);
202
22.2k
        if ((dna + dnb) < 0)
203
22.2k
            memset(&r[2 * n2 + dna + dnb], 0,
204
22.2k
                   sizeof(BN_ULONG) * -(dna + dnb));
205
22.2k
        return;
206
22.2k
    }
207
    /* r=(a[0]-a[1])*(b[1]-b[0]) */
208
25.7M
    c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
209
25.7M
    c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
210
25.7M
    zero = neg = 0;
211
25.7M
    switch (c1 * 3 + c2) {
212
5.28M
    case -4:
213
5.28M
        bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
214
5.28M
        bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
215
5.28M
        break;
216
429k
    case -3:
217
429k
        zero = 1;
218
429k
        break;
219
5.14M
    case -2:
220
5.14M
        bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
221
5.14M
        bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
222
5.14M
        neg = 1;
223
5.14M
        break;
224
51.8k
    case -1:
225
366k
    case 0:
226
417k
    case 1:
227
417k
        zero = 1;
228
417k
        break;
229
8.63M
    case 2:
230
8.63M
        bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
231
8.63M
        bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
232
8.63M
        neg = 1;
233
8.63M
        break;
234
471k
    case 3:
235
471k
        zero = 1;
236
471k
        break;
237
5.39M
    case 4:
238
5.39M
        bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
239
5.39M
        bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
240
5.39M
        break;
241
25.7M
    }
242
243
25.7M
# ifdef BN_MUL_COMBA
244
25.7M
    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
25.7M
    } else if (n == 8 && dna == 0 && dnb == 0) { /* XXX: bn_mul_comba8 could
254
                                                  * take extra args to do
255
                                                  * this well */
256
17.3M
        if (!zero)
257
16.3M
            bn_mul_comba8(&(t[n2]), t, &(t[n]));
258
940k
        else
259
940k
            memset(&t[n2], 0, sizeof(*t) * 16);
260
261
17.3M
        bn_mul_comba8(r, a, b);
262
17.3M
        bn_mul_comba8(&(r[n2]), &(a[n]), &(b[n]));
263
17.3M
    } else
264
8.45M
# endif                         /* BN_MUL_COMBA */
265
8.45M
    {
266
8.45M
        p = &(t[n2 * 2]);
267
8.45M
        if (!zero)
268
8.07M
            bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
269
377k
        else
270
377k
            memset(&t[n2], 0, sizeof(*t) * n2);
271
8.45M
        bn_mul_recursive(r, a, b, n, 0, 0, p);
272
8.45M
        bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), n, dna, dnb, p);
273
8.45M
    }
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
25.7M
    c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
282
283
25.7M
    if (neg) {                  /* if t[32] is negative */
284
13.7M
        c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
285
13.7M
    } else {
286
        /* Might have a carry */
287
12.0M
        c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
288
12.0M
    }
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
25.7M
    c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
297
25.7M
    if (c1) {
298
9.97M
        p = &(r[n + n2]);
299
9.97M
        lo = *p;
300
9.97M
        ln = (lo + c1) & BN_MASK2;
301
9.97M
        *p = ln;
302
303
        /*
304
         * The overflow will stop before we over write words we should not
305
         * overwrite
306
         */
307
9.97M
        if (ln < (BN_ULONG)c1) {
308
148k
            do {
309
148k
                p++;
310
148k
                lo = *p;
311
148k
                ln = (lo + 1) & BN_MASK2;
312
148k
                *p = ln;
313
148k
            } while (ln == 0);
314
35.1k
        }
315
9.97M
    }
316
25.7M
}
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
422k
{
325
422k
    int i, j, n2 = n * 2;
326
422k
    int c1, c2, neg;
327
422k
    BN_ULONG ln, lo, *p;
328
329
422k
    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
422k
    c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
336
422k
    c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
337
422k
    neg = 0;
338
422k
    switch (c1 * 3 + c2) {
339
39.4k
    case -4:
340
39.4k
        bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
341
39.4k
        bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
342
39.4k
        break;
343
1.67k
    case -3:
344
10.2k
    case -2:
345
10.2k
        bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
346
10.2k
        bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
347
10.2k
        neg = 1;
348
10.2k
        break;
349
6.05k
    case -1:
350
7.65k
    case 0:
351
9.00k
    case 1:
352
354k
    case 2:
353
354k
        bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
354
354k
        bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
355
354k
        neg = 1;
356
354k
        break;
357
5.14k
    case 3:
358
18.0k
    case 4:
359
18.0k
        bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
360
18.0k
        bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
361
18.0k
        break;
362
422k
    }
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
422k
    if (n == 8) {
376
70.0k
        bn_mul_comba8(&(t[n2]), t, &(t[n]));
377
70.0k
        bn_mul_comba8(r, a, b);
378
70.0k
        bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
379
70.0k
        memset(&r[n2 + tna + tnb], 0, sizeof(*r) * (n2 - tna - tnb));
380
352k
    } else {
381
352k
        p = &(t[n2 * 2]);
382
352k
        bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
383
352k
        bn_mul_recursive(r, a, b, n, 0, 0, p);
384
352k
        i = n / 2;
385
        /*
386
         * If there is only a bottom half to the number, just do it
387
         */
388
352k
        if (tna > tnb)
389
32.7k
            j = tna - i;
390
319k
        else
391
319k
            j = tnb - i;
392
352k
        if (j == 0) {
393
12.6k
            bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]),
394
12.6k
                             i, tna - i, tnb - i, p);
395
12.6k
            memset(&r[n2 + i * 2], 0, sizeof(*r) * (n2 - i * 2));
396
339k
        } else if (j > 0) {     /* eg, n == 16, i == 8 and tn == 11 */
397
98.8k
            bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]),
398
98.8k
                                  i, tna - i, tnb - i, p);
399
98.8k
            memset(&(r[n2 + tna + tnb]), 0,
400
98.8k
                   sizeof(BN_ULONG) * (n2 - tna - tnb));
401
240k
        } else {                /* (j < 0) eg, n == 16, i == 8 and tn == 5 */
402
403
240k
            memset(&r[n2], 0, sizeof(*r) * n2);
404
240k
            if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL
405
240k
                && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {
406
181k
                bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
407
181k
            } else {
408
77.1k
                for (;;) {
409
77.1k
                    i /= 2;
410
                    /*
411
                     * these simplified conditions work exclusively because
412
                     * difference between tna and tnb is 1 or 0
413
                     */
414
77.1k
                    if (i < tna || i < tnb) {
415
48.5k
                        bn_mul_part_recursive(&(r[n2]),
416
48.5k
                                              &(a[n]), &(b[n]),
417
48.5k
                                              i, tna - i, tnb - i, p);
418
48.5k
                        break;
419
48.5k
                    } else if (i == tna || i == tnb) {
420
10.4k
                        bn_mul_recursive(&(r[n2]),
421
10.4k
                                         &(a[n]), &(b[n]),
422
10.4k
                                         i, tna - i, tnb - i, p);
423
10.4k
                        break;
424
10.4k
                    }
425
77.1k
                }
426
58.9k
            }
427
240k
        }
428
352k
    }
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
422k
    c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
437
438
422k
    if (neg) {                  /* if t[32] is negative */
439
364k
        c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
440
364k
    } else {
441
        /* Might have a carry */
442
57.4k
        c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
443
57.4k
    }
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
422k
    c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
452
422k
    if (c1) {
453
9.00k
        p = &(r[n + n2]);
454
9.00k
        lo = *p;
455
9.00k
        ln = (lo + c1) & BN_MASK2;
456
9.00k
        *p = ln;
457
458
        /*
459
         * The overflow will stop before we over write words we should not
460
         * overwrite
461
         */
462
9.00k
        if (ln < (BN_ULONG)c1) {
463
16.3k
            do {
464
16.3k
                p++;
465
16.3k
                lo = *p;
466
16.3k
                ln = (lo + 1) & BN_MASK2;
467
16.3k
                *p = ln;
468
16.3k
            } while (ln == 0);
469
3.12k
        }
470
9.00k
    }
471
422k
}
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
14.1M
{
499
14.1M
    int ret = bn_mul_fixed_top(r, a, b, ctx);
500
501
14.1M
    bn_correct_top(r);
502
14.1M
    bn_check_top(r);
503
504
14.1M
    return ret;
505
14.1M
}
506
507
int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
508
17.2M
{
509
17.2M
    int ret = 0;
510
17.2M
    int top, al, bl;
511
17.2M
    BIGNUM *rr;
512
17.2M
#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
513
17.2M
    int i;
514
17.2M
#endif
515
17.2M
#ifdef BN_RECURSION
516
17.2M
    BIGNUM *t = NULL;
517
17.2M
    int j = 0, k;
518
17.2M
#endif
519
520
17.2M
    bn_check_top(a);
521
17.2M
    bn_check_top(b);
522
17.2M
    bn_check_top(r);
523
524
17.2M
    al = a->top;
525
17.2M
    bl = b->top;
526
527
17.2M
    if ((al == 0) || (bl == 0)) {
528
1.16M
        BN_zero(r);
529
1.16M
        return 1;
530
1.16M
    }
531
16.0M
    top = al + bl;
532
533
16.0M
    BN_CTX_start(ctx);
534
16.0M
    if ((r == a) || (r == b)) {
535
81.2k
        if ((rr = BN_CTX_get(ctx)) == NULL)
536
0
            goto err;
537
81.2k
    } else
538
15.9M
        rr = r;
539
540
16.0M
#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
541
16.0M
    i = al - bl;
542
16.0M
#endif
543
16.0M
#ifdef BN_MUL_COMBA
544
16.0M
    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
5.02M
        if (al == 8) {
555
10.0k
            if (bn_wexpand(rr, 16) == NULL)
556
0
                goto err;
557
10.0k
            rr->top = 16;
558
10.0k
            bn_mul_comba8(rr->d, a->d, b->d);
559
10.0k
            goto end;
560
10.0k
        }
561
5.02M
    }
562
16.0M
#endif                          /* BN_MUL_COMBA */
563
16.0M
#ifdef BN_RECURSION
564
16.0M
    if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {
565
441k
        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
401k
            if (i >= 0) {
571
332k
                j = BN_num_bits_word((BN_ULONG)al);
572
332k
            }
573
401k
            if (i == -1) {
574
68.8k
                j = BN_num_bits_word((BN_ULONG)bl);
575
68.8k
            }
576
401k
            j = 1 << (j - 1);
577
401k
            assert(j <= al || j <= bl);
578
401k
            k = j + j;
579
401k
            t = BN_CTX_get(ctx);
580
401k
            if (t == NULL)
581
0
                goto err;
582
401k
            if (al > j || bl > j) {
583
274k
                if (bn_wexpand(t, k * 4) == NULL)
584
0
                    goto err;
585
274k
                if (bn_wexpand(rr, k * 4) == NULL)
586
0
                    goto err;
587
274k
                bn_mul_part_recursive(rr->d, a->d, b->d,
588
274k
                                      j, al - j, bl - j, t->d);
589
274k
            } else {            /* al <= j || bl <= j */
590
591
126k
                if (bn_wexpand(t, k * 2) == NULL)
592
0
                    goto err;
593
126k
                if (bn_wexpand(rr, k * 2) == NULL)
594
0
                    goto err;
595
126k
                bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
596
126k
            }
597
401k
            rr->top = top;
598
401k
            goto end;
599
401k
        }
600
441k
    }
601
15.6M
#endif                          /* BN_RECURSION */
602
15.6M
    if (bn_wexpand(rr, top) == NULL)
603
0
        goto err;
604
15.6M
    rr->top = top;
605
15.6M
    bn_mul_normal(rr->d, a->d, al, b->d, bl);
606
607
15.6M
#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
608
16.0M
 end:
609
16.0M
#endif
610
16.0M
    rr->neg = a->neg ^ b->neg;
611
16.0M
    rr->flags |= BN_FLG_FIXED_TOP;
612
16.0M
    if (r != rr && BN_copy(r, rr) == NULL)
613
0
        goto err;
614
615
16.0M
    ret = 1;
616
16.0M
 err:
617
16.0M
    bn_check_top(r);
618
16.0M
    BN_CTX_end(ctx);
619
16.0M
    return ret;
620
16.0M
}
621
622
void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
623
15.9M
{
624
15.9M
    BN_ULONG *rr;
625
626
15.9M
    if (na < nb) {
627
10.3M
        int itmp;
628
10.3M
        BN_ULONG *ltmp;
629
630
10.3M
        itmp = na;
631
10.3M
        na = nb;
632
10.3M
        nb = itmp;
633
10.3M
        ltmp = a;
634
10.3M
        a = b;
635
10.3M
        b = ltmp;
636
637
10.3M
    }
638
15.9M
    rr = &(r[na]);
639
15.9M
    if (nb <= 0) {
640
62.6k
        (void)bn_mul_words(r, a, na, 0);
641
62.6k
        return;
642
62.6k
    } else
643
15.8M
        rr[0] = bn_mul_words(r, a, na, b[0]);
644
645
16.6M
    for (;;) {
646
16.6M
        if (--nb <= 0)
647
13.0M
            return;
648
3.62M
        rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);
649
3.62M
        if (--nb <= 0)
650
253k
            return;
651
3.37M
        rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);
652
3.37M
        if (--nb <= 0)
653
142k
            return;
654
3.22M
        rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);
655
3.22M
        if (--nb <= 0)
656
2.41M
            return;
657
814k
        rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);
658
814k
        rr += 4;
659
814k
        r += 4;
660
814k
        b += 4;
661
814k
    }
662
15.8M
}
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
}