Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/bn/bn_mul.c
Line
Count
Source
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
124M
{
31
124M
    BN_ULONG c, t;
32
33
124M
    assert(cl >= 0);
34
124M
    c = bn_sub_words(r, a, b, cl);
35
36
124M
    if (dl == 0)
37
120M
        return c;
38
39
4.40M
    r += cl;
40
4.40M
    a += cl;
41
4.40M
    b += cl;
42
43
4.40M
    if (dl < 0) {
44
899k
        for (;;) {
45
899k
            t = b[0];
46
899k
            r[0] = (0 - t - c) & BN_MASK2;
47
899k
            if (t != 0)
48
0
                c = 1;
49
899k
            if (++dl >= 0)
50
94.6k
                break;
51
52
805k
            t = b[1];
53
805k
            r[1] = (0 - t - c) & BN_MASK2;
54
805k
            if (t != 0)
55
0
                c = 1;
56
805k
            if (++dl >= 0)
57
58.4k
                break;
58
59
746k
            t = b[2];
60
746k
            r[2] = (0 - t - c) & BN_MASK2;
61
746k
            if (t != 0)
62
0
                c = 1;
63
746k
            if (++dl >= 0)
64
184k
                break;
65
66
562k
            t = b[3];
67
562k
            r[3] = (0 - t - c) & BN_MASK2;
68
562k
            if (t != 0)
69
0
                c = 1;
70
562k
            if (++dl >= 0)
71
23.6k
                break;
72
73
538k
            b += 4;
74
538k
            r += 4;
75
538k
        }
76
4.04M
    } else {
77
4.04M
        int save_dl = dl;
78
4.62M
        while (c) {
79
888k
            t = a[0];
80
888k
            r[0] = (t - c) & BN_MASK2;
81
888k
            if (t != 0)
82
440k
                c = 0;
83
888k
            if (--dl <= 0)
84
129k
                break;
85
86
758k
            t = a[1];
87
758k
            r[1] = (t - c) & BN_MASK2;
88
758k
            if (t != 0)
89
356k
                c = 0;
90
758k
            if (--dl <= 0)
91
42.9k
                break;
92
93
715k
            t = a[2];
94
715k
            r[2] = (t - c) & BN_MASK2;
95
715k
            if (t != 0)
96
342k
                c = 0;
97
715k
            if (--dl <= 0)
98
98.7k
                break;
99
100
616k
            t = a[3];
101
616k
            r[3] = (t - c) & BN_MASK2;
102
616k
            if (t != 0)
103
283k
                c = 0;
104
616k
            if (--dl <= 0)
105
37.8k
                break;
106
107
578k
            save_dl = dl;
108
578k
            a += 4;
109
578k
            r += 4;
110
578k
        }
111
4.04M
        if (dl > 0) {
112
3.73M
            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
3.73M
        }
133
4.04M
        if (dl > 0) {
134
21.5M
            for (;;) {
135
21.5M
                r[0] = a[0];
136
21.5M
                if (--dl <= 0)
137
839k
                    break;
138
20.6M
                r[1] = a[1];
139
20.6M
                if (--dl <= 0)
140
430k
                    break;
141
20.2M
                r[2] = a[2];
142
20.2M
                if (--dl <= 0)
143
1.68M
                    break;
144
18.5M
                r[3] = a[3];
145
18.5M
                if (--dl <= 0)
146
785k
                    break;
147
148
17.7M
                a += 4;
149
17.7M
                r += 4;
150
17.7M
            }
151
3.73M
        }
152
4.04M
    }
153
4.40M
    return c;
154
4.40M
}
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
65.2M
{
178
65.2M
    int n = n2 / 2, c1, c2;
179
65.2M
    int tna = n + dna, tnb = n + dnb;
180
65.2M
    unsigned int neg, zero;
181
65.2M
    BN_ULONG ln, lo, *p;
182
183
65.2M
#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
65.2M
    if (n2 == 8 && dna == 0 && dnb == 0) {
195
213k
        bn_mul_comba8(r, a, b);
196
213k
        return;
197
213k
    }
198
65.0M
#endif /* BN_MUL_COMBA */
199
    /* Else do normal multiply */
200
65.0M
    if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) {
201
107k
        bn_mul_normal(r, a, n2 + dna, b, n2 + dnb);
202
107k
        if ((dna + dnb) < 0)
203
107k
            memset(&r[2 * n2 + dna + dnb], 0,
204
107k
                sizeof(BN_ULONG) * -(dna + dnb));
205
107k
        return;
206
107k
    }
207
    /* r=(a[0]-a[1])*(b[1]-b[0]) */
208
64.9M
    c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
209
64.9M
    c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
210
64.9M
    zero = neg = 0;
211
64.9M
    switch (c1 * 3 + c2) {
212
12.6M
    case -4:
213
12.6M
        bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
214
12.6M
        bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
215
12.6M
        break;
216
1.21M
    case -3:
217
1.21M
        zero = 1;
218
1.21M
        break;
219
13.0M
    case -2:
220
13.0M
        bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
221
13.0M
        bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
222
13.0M
        neg = 1;
223
13.0M
        break;
224
222k
    case -1:
225
1.90M
    case 0:
226
2.10M
    case 1:
227
2.10M
        zero = 1;
228
2.10M
        break;
229
21.6M
    case 2:
230
21.6M
        bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
231
21.6M
        bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
232
21.6M
        neg = 1;
233
21.6M
        break;
234
1.34M
    case 3:
235
1.34M
        zero = 1;
236
1.34M
        break;
237
13.0M
    case 4:
238
13.0M
        bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
239
13.0M
        bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
240
13.0M
        break;
241
64.9M
    }
242
243
64.9M
#ifdef BN_MUL_COMBA
244
64.9M
    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
64.9M
    } else if (n == 8 && dna == 0 && dnb == 0) { /* XXX: bn_mul_comba8 could
254
                                                  * take extra args to do
255
                                                  * this well */
256
44.2M
        if (!zero)
257
40.8M
            bn_mul_comba8(&(t[n2]), t, &(t[n]));
258
3.36M
        else
259
3.36M
            memset(&t[n2], 0, sizeof(*t) * 16);
260
261
44.2M
        bn_mul_comba8(r, a, b);
262
44.2M
        bn_mul_comba8(&(r[n2]), &(a[n]), &(b[n]));
263
44.2M
    } else
264
20.7M
#endif /* BN_MUL_COMBA */
265
20.7M
    {
266
20.7M
        p = &(t[n2 * 2]);
267
20.7M
        if (!zero)
268
19.4M
            bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
269
1.29M
        else
270
1.29M
            memset(&t[n2], 0, sizeof(*t) * n2);
271
20.7M
        bn_mul_recursive(r, a, b, n, 0, 0, p);
272
20.7M
        bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), n, dna, dnb, p);
273
20.7M
    }
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
64.9M
    c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
282
283
64.9M
    if (neg) { /* if t[32] is negative */
284
34.6M
        c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
285
34.6M
    } else {
286
        /* Might have a carry */
287
30.3M
        c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
288
30.3M
    }
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
64.9M
    c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
297
64.9M
    if (c1) {
298
25.0M
        p = &(r[n + n2]);
299
25.0M
        lo = *p;
300
25.0M
        ln = (lo + c1) & BN_MASK2;
301
25.0M
        *p = ln;
302
303
        /*
304
         * The overflow will stop before we over write words we should not
305
         * overwrite
306
         */
307
25.0M
        if (ln < (BN_ULONG)c1) {
308
798k
            do {
309
798k
                p++;
310
798k
                lo = *p;
311
798k
                ln = (lo + 1) & BN_MASK2;
312
798k
                *p = ln;
313
798k
            } while (ln == 0);
314
156k
        }
315
25.0M
    }
316
64.9M
}
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
2.10M
{
325
2.10M
    int i, j, n2 = n * 2;
326
2.10M
    int c1, c2, neg;
327
2.10M
    BN_ULONG ln, lo, *p;
328
329
2.10M
    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
2.10M
    c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
336
2.10M
    c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
337
2.10M
    neg = 0;
338
2.10M
    switch (c1 * 3 + c2) {
339
229k
    case -4:
340
229k
        bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
341
229k
        bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
342
229k
        break;
343
7.25k
    case -3:
344
25.8k
    case -2:
345
25.8k
        bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
346
25.8k
        bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
347
25.8k
        neg = 1;
348
25.8k
        break;
349
25.1k
    case -1:
350
29.4k
    case 0:
351
32.3k
    case 1:
352
1.78M
    case 2:
353
1.78M
        bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
354
1.78M
        bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
355
1.78M
        neg = 1;
356
1.78M
        break;
357
34.6k
    case 3:
358
70.1k
    case 4:
359
70.1k
        bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
360
70.1k
        bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
361
70.1k
        break;
362
2.10M
    }
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
2.10M
    if (n == 8) {
376
388k
        bn_mul_comba8(&(t[n2]), t, &(t[n]));
377
388k
        bn_mul_comba8(r, a, b);
378
388k
        bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
379
388k
        memset(&r[n2 + tna + tnb], 0, sizeof(*r) * (n2 - tna - tnb));
380
1.72M
    } else {
381
1.72M
        p = &(t[n2 * 2]);
382
1.72M
        bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
383
1.72M
        bn_mul_recursive(r, a, b, n, 0, 0, p);
384
1.72M
        i = n / 2;
385
        /*
386
         * If there is only a bottom half to the number, just do it
387
         */
388
1.72M
        if (tna > tnb)
389
135k
            j = tna - i;
390
1.58M
        else
391
1.58M
            j = tnb - i;
392
1.72M
        if (j == 0) {
393
43.3k
            bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]),
394
43.3k
                i, tna - i, tnb - i, p);
395
43.3k
            memset(&r[n2 + i * 2], 0, sizeof(*r) * (n2 - i * 2));
396
1.67M
        } else if (j > 0) { /* eg, n == 16, i == 8 and tn == 11 */
397
479k
            bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]),
398
479k
                i, tna - i, tnb - i, p);
399
479k
            memset(&(r[n2 + tna + tnb]), 0,
400
479k
                sizeof(BN_ULONG) * (n2 - tna - tnb));
401
1.19M
        } else { /* (j < 0) eg, n == 16, i == 8 and tn == 5 */
402
403
1.19M
            memset(&r[n2], 0, sizeof(*r) * n2);
404
1.19M
            if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL
405
1.09M
                && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {
406
1.09M
                bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
407
1.09M
            } else {
408
121k
                for (;;) {
409
121k
                    i /= 2;
410
                    /*
411
                     * these simplified conditions work exclusively because
412
                     * difference between tna and tnb is 1 or 0
413
                     */
414
121k
                    if (i < tna || i < tnb) {
415
61.1k
                        bn_mul_part_recursive(&(r[n2]),
416
61.1k
                            &(a[n]), &(b[n]),
417
61.1k
                            i, tna - i, tnb - i, p);
418
61.1k
                        break;
419
61.1k
                    } else if (i == tna || i == tnb) {
420
44.0k
                        bn_mul_recursive(&(r[n2]),
421
44.0k
                            &(a[n]), &(b[n]),
422
44.0k
                            i, tna - i, tnb - i, p);
423
44.0k
                        break;
424
44.0k
                    }
425
121k
                }
426
105k
            }
427
1.19M
        }
428
1.72M
    }
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
2.10M
    c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
437
438
2.10M
    if (neg) { /* if t[32] is negative */
439
1.80M
        c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
440
1.80M
    } else {
441
        /* Might have a carry */
442
299k
        c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
443
299k
    }
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
2.10M
    c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
452
2.10M
    if (c1) {
453
27.6k
        p = &(r[n + n2]);
454
27.6k
        lo = *p;
455
27.6k
        ln = (lo + c1) & BN_MASK2;
456
27.6k
        *p = ln;
457
458
        /*
459
         * The overflow will stop before we over write words we should not
460
         * overwrite
461
         */
462
27.6k
        if (ln < (BN_ULONG)c1) {
463
75.2k
            do {
464
75.2k
                p++;
465
75.2k
                lo = *p;
466
75.2k
                ln = (lo + 1) & BN_MASK2;
467
75.2k
                *p = ln;
468
75.2k
            } while (ln == 0);
469
13.5k
        }
470
27.6k
    }
471
2.10M
}
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
29.1M
{
499
29.1M
    int ret = bn_mul_fixed_top(r, a, b, ctx);
500
501
29.1M
    bn_correct_top(r);
502
29.1M
    bn_check_top(r);
503
504
29.1M
    return ret;
505
29.1M
}
506
507
int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
508
34.1M
{
509
34.1M
    int ret = 0;
510
34.1M
    int top, al, bl;
511
34.1M
    BIGNUM *rr;
512
34.1M
#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
513
34.1M
    int i;
514
34.1M
#endif
515
34.1M
#ifdef BN_RECURSION
516
34.1M
    BIGNUM *t = NULL;
517
34.1M
    int j = 0, k;
518
34.1M
#endif
519
520
34.1M
    bn_check_top(a);
521
34.1M
    bn_check_top(b);
522
34.1M
    bn_check_top(r);
523
524
34.1M
    al = a->top;
525
34.1M
    bl = b->top;
526
527
34.1M
    if ((al == 0) || (bl == 0)) {
528
1.50M
        BN_zero(r);
529
1.50M
        return 1;
530
1.50M
    }
531
32.6M
    top = al + bl;
532
533
32.6M
    BN_CTX_start(ctx);
534
32.6M
    if ((r == a) || (r == b)) {
535
198k
        if ((rr = BN_CTX_get(ctx)) == NULL)
536
0
            goto err;
537
198k
    } else
538
32.4M
        rr = r;
539
540
32.6M
#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
541
32.6M
    i = al - bl;
542
32.6M
#endif
543
32.6M
#ifdef BN_MUL_COMBA
544
32.6M
    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
10.0M
        if (al == 8) {
555
35.0k
            if (bn_wexpand(rr, 16) == NULL)
556
0
                goto err;
557
35.0k
            rr->top = 16;
558
35.0k
            bn_mul_comba8(rr->d, a->d, b->d);
559
35.0k
            goto end;
560
35.0k
        }
561
10.0M
    }
562
32.6M
#endif /* BN_MUL_COMBA */
563
32.6M
#ifdef BN_RECURSION
564
32.6M
    if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {
565
2.37M
        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
2.31M
            if (i >= 0) {
571
1.91M
                j = BN_num_bits_word((BN_ULONG)al);
572
1.91M
            }
573
2.31M
            if (i == -1) {
574
398k
                j = BN_num_bits_word((BN_ULONG)bl);
575
398k
            }
576
2.31M
            j = 1 << (j - 1);
577
2.31M
            assert(j <= al || j <= bl);
578
2.31M
            k = j + j;
579
2.31M
            t = BN_CTX_get(ctx);
580
2.31M
            if (t == NULL)
581
0
                goto err;
582
2.31M
            if (al > j || bl > j) {
583
1.56M
                if (bn_wexpand(t, k * 4) == NULL)
584
0
                    goto err;
585
1.56M
                if (bn_wexpand(rr, k * 4) == NULL)
586
0
                    goto err;
587
1.56M
                bn_mul_part_recursive(rr->d, a->d, b->d,
588
1.56M
                    j, al - j, bl - j, t->d);
589
1.56M
            } else { /* al <= j || bl <= j */
590
591
747k
                if (bn_wexpand(t, k * 2) == NULL)
592
0
                    goto err;
593
747k
                if (bn_wexpand(rr, k * 2) == NULL)
594
0
                    goto err;
595
747k
                bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
596
747k
            }
597
2.31M
            rr->top = top;
598
2.31M
            goto end;
599
2.31M
        }
600
2.37M
    }
601
30.3M
#endif /* BN_RECURSION */
602
30.3M
    if (bn_wexpand(rr, top) == NULL)
603
0
        goto err;
604
30.3M
    rr->top = top;
605
30.3M
    bn_mul_normal(rr->d, a->d, al, b->d, bl);
606
607
30.3M
#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
608
32.6M
end:
609
32.6M
#endif
610
32.6M
    rr->neg = a->neg ^ b->neg;
611
32.6M
    rr->flags |= BN_FLG_FIXED_TOP;
612
32.6M
    if (r != rr && BN_copy(r, rr) == NULL)
613
0
        goto err;
614
615
32.6M
    ret = 1;
616
32.6M
err:
617
32.6M
    bn_check_top(r);
618
32.6M
    BN_CTX_end(ctx);
619
32.6M
    return ret;
620
32.6M
}
621
622
void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
623
31.8M
{
624
31.8M
    BN_ULONG *rr;
625
626
31.8M
    if (na < nb) {
627
21.6M
        int itmp;
628
21.6M
        BN_ULONG *ltmp;
629
630
21.6M
        itmp = na;
631
21.6M
        na = nb;
632
21.6M
        nb = itmp;
633
21.6M
        ltmp = a;
634
21.6M
        a = b;
635
21.6M
        b = ltmp;
636
21.6M
    }
637
31.8M
    rr = &(r[na]);
638
31.8M
    if (nb <= 0) {
639
368k
        (void)bn_mul_words(r, a, na, 0);
640
368k
        return;
641
368k
    } else
642
31.5M
        rr[0] = bn_mul_words(r, a, na, b[0]);
643
644
33.1M
    for (;;) {
645
33.1M
        if (--nb <= 0)
646
25.9M
            return;
647
7.17M
        rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);
648
7.17M
        if (--nb <= 0)
649
693k
            return;
650
6.48M
        rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);
651
6.48M
        if (--nb <= 0)
652
478k
            return;
653
6.00M
        rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);
654
6.00M
        if (--nb <= 0)
655
4.38M
            return;
656
1.62M
        rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);
657
1.62M
        rr += 4;
658
1.62M
        r += 4;
659
1.62M
        b += 4;
660
1.62M
    }
661
31.5M
}
662
663
void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n)
664
0
{
665
0
    bn_mul_words(r, a, n, b[0]);
666
667
0
    for (;;) {
668
0
        if (--n <= 0)
669
0
            return;
670
0
        bn_mul_add_words(&(r[1]), a, n, b[1]);
671
0
        if (--n <= 0)
672
0
            return;
673
0
        bn_mul_add_words(&(r[2]), a, n, b[2]);
674
0
        if (--n <= 0)
675
0
            return;
676
0
        bn_mul_add_words(&(r[3]), a, n, b[3]);
677
0
        if (--n <= 0)
678
0
            return;
679
0
        bn_mul_add_words(&(r[4]), a, n, b[4]);
680
0
        r += 4;
681
0
        b += 4;
682
0
    }
683
0
}