Coverage Report

Created: 2023-06-08 06:41

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