Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/security/nss/lib/freebl/mpi/mpmontg.c
Line
Count
Source (jump to first uncovered line)
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
/* This file implements moduluar exponentiation using Montgomery's
6
 * method for modular reduction.  This file implements the method
7
 * described as "Improvement 2" in the paper "A Cryptogrpahic Library for
8
 * the Motorola DSP56000" by Stephen R. Dusse' and Burton S. Kaliski Jr.
9
 * published in "Advances in Cryptology: Proceedings of EUROCRYPT '90"
10
 * "Lecture Notes in Computer Science" volume 473, 1991, pg 230-244,
11
 * published by Springer Verlag.
12
 */
13
14
#define MP_USING_CACHE_SAFE_MOD_EXP 1
15
#include <string.h>
16
#include "mpi-priv.h"
17
#include "mplogic.h"
18
#include "mpprime.h"
19
#ifdef MP_USING_MONT_MULF
20
#include "montmulf.h"
21
#endif
22
#include <stddef.h> /* ptrdiff_t */
23
#include <assert.h>
24
25
#define STATIC
26
27
0
#define MAX_ODD_INTS 32 /* 2 ** (WINDOW_BITS - 1) */
28
29
/*! computes T = REDC(T), 2^b == R
30
    \param T < RN
31
*/
32
mp_err
33
s_mp_redc(mp_int *T, mp_mont_modulus *mmm)
34
0
{
35
0
    mp_err res;
36
0
    mp_size i;
37
0
38
0
    i = (MP_USED(&mmm->N) << 1) + 1;
39
0
    MP_CHECKOK(s_mp_pad(T, i));
40
0
    for (i = 0; i < MP_USED(&mmm->N); ++i) {
41
0
        mp_digit m_i = MP_DIGIT(T, i) * mmm->n0prime;
42
0
        /* T += N * m_i * (MP_RADIX ** i); */
43
0
        s_mp_mul_d_add_offset(&mmm->N, m_i, T, i);
44
0
    }
45
0
    s_mp_clamp(T);
46
0
47
0
    /* T /= R */
48
0
    s_mp_rshd(T, MP_USED(&mmm->N));
49
0
50
0
    if ((res = s_mp_cmp(T, &mmm->N)) >= 0) {
51
0
        /* T = T - N */
52
0
        MP_CHECKOK(s_mp_sub(T, &mmm->N));
53
#ifdef DEBUG
54
        if ((res = mp_cmp(T, &mmm->N)) >= 0) {
55
            res = MP_UNDEF;
56
            goto CLEANUP;
57
        }
58
#endif
59
    }
60
0
    res = MP_OKAY;
61
0
CLEANUP:
62
0
    return res;
63
0
}
64
65
#if !defined(MP_MONT_USE_MP_MUL)
66
67
/*! c <- REDC( a * b ) mod N
68
    \param a < N  i.e. "reduced"
69
    \param b < N  i.e. "reduced"
70
    \param mmm modulus N and n0' of N
71
*/
72
mp_err
73
s_mp_mul_mont(const mp_int *a, const mp_int *b, mp_int *c,
74
              mp_mont_modulus *mmm)
75
0
{
76
0
    mp_digit *pb;
77
0
    mp_digit m_i;
78
0
    mp_err res;
79
0
    mp_size ib; /* "index b": index of current digit of B */
80
0
    mp_size useda, usedb;
81
0
82
0
    ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
83
0
84
0
    if (MP_USED(a) < MP_USED(b)) {
85
0
        const mp_int *xch = b; /* switch a and b, to do fewer outer loops */
86
0
        b = a;
87
0
        a = xch;
88
0
    }
89
0
90
0
    MP_USED(c) = 1;
91
0
    MP_DIGIT(c, 0) = 0;
92
0
    ib = (MP_USED(&mmm->N) << 1) + 1;
93
0
    if ((res = s_mp_pad(c, ib)) != MP_OKAY)
94
0
        goto CLEANUP;
95
0
96
0
    useda = MP_USED(a);
97
0
    pb = MP_DIGITS(b);
98
0
    s_mpv_mul_d(MP_DIGITS(a), useda, *pb++, MP_DIGITS(c));
99
0
    s_mp_setz(MP_DIGITS(c) + useda + 1, ib - (useda + 1));
100
0
    m_i = MP_DIGIT(c, 0) * mmm->n0prime;
101
0
    s_mp_mul_d_add_offset(&mmm->N, m_i, c, 0);
102
0
103
0
    /* Outer loop:  Digits of b */
104
0
    usedb = MP_USED(b);
105
0
    for (ib = 1; ib < usedb; ib++) {
106
0
        mp_digit b_i = *pb++;
107
0
108
0
        /* Inner product:  Digits of a */
109
0
        if (b_i)
110
0
            s_mpv_mul_d_add_prop(MP_DIGITS(a), useda, b_i, MP_DIGITS(c) + ib);
111
0
        m_i = MP_DIGIT(c, ib) * mmm->n0prime;
112
0
        s_mp_mul_d_add_offset(&mmm->N, m_i, c, ib);
113
0
    }
114
0
    if (usedb < MP_USED(&mmm->N)) {
115
0
        for (usedb = MP_USED(&mmm->N); ib < usedb; ++ib) {
116
0
            m_i = MP_DIGIT(c, ib) * mmm->n0prime;
117
0
            s_mp_mul_d_add_offset(&mmm->N, m_i, c, ib);
118
0
        }
119
0
    }
120
0
    s_mp_clamp(c);
121
0
    s_mp_rshd(c, MP_USED(&mmm->N)); /* c /= R */
122
0
    if (s_mp_cmp(c, &mmm->N) >= 0) {
123
0
        MP_CHECKOK(s_mp_sub(c, &mmm->N));
124
0
    }
125
0
    res = MP_OKAY;
126
0
127
0
CLEANUP:
128
0
    return res;
129
0
}
130
#endif
131
132
STATIC
133
mp_err
134
s_mp_to_mont(const mp_int *x, mp_mont_modulus *mmm, mp_int *xMont)
135
0
{
136
0
    mp_err res;
137
0
138
0
    /* xMont = x * R mod N   where  N is modulus */
139
0
    MP_CHECKOK(mp_copy(x, xMont));
140
0
    MP_CHECKOK(s_mp_lshd(xMont, MP_USED(&mmm->N))); /* xMont = x << b */
141
0
    MP_CHECKOK(mp_div(xMont, &mmm->N, 0, xMont));   /*         mod N */
142
0
CLEANUP:
143
0
    return res;
144
0
}
145
146
#ifdef MP_USING_MONT_MULF
147
148
/* the floating point multiply is already cache safe,
149
 * don't turn on cache safe unless we specifically
150
 * force it */
151
#ifndef MP_FORCE_CACHE_SAFE
152
#undef MP_USING_CACHE_SAFE_MOD_EXP
153
#endif
154
155
unsigned int mp_using_mont_mulf = 1;
156
157
/* computes montgomery square of the integer in mResult */
158
#define SQR                                              \
159
    conv_i32_to_d32_and_d16(dm1, d16Tmp, mResult, nLen); \
160
    mont_mulf_noconv(mResult, dm1, d16Tmp,               \
161
                     dTmp, dn, MP_DIGITS(modulus), nLen, dn0)
162
163
/* computes montgomery product of x and the integer in mResult */
164
#define MUL(x)                                   \
165
    conv_i32_to_d32(dm1, mResult, nLen);         \
166
    mont_mulf_noconv(mResult, dm1, oddPowers[x], \
167
                     dTmp, dn, MP_DIGITS(modulus), nLen, dn0)
168
169
/* Do modular exponentiation using floating point multiply code. */
170
mp_err
171
mp_exptmod_f(const mp_int *montBase,
172
             const mp_int *exponent,
173
             const mp_int *modulus,
174
             mp_int *result,
175
             mp_mont_modulus *mmm,
176
             int nLen,
177
             mp_size bits_in_exponent,
178
             mp_size window_bits,
179
             mp_size odd_ints)
180
{
181
    mp_digit *mResult;
182
    double *dBuf = 0, *dm1, *dn, *dSqr, *d16Tmp, *dTmp;
183
    double dn0;
184
    mp_size i;
185
    mp_err res;
186
    int expOff;
187
    int dSize = 0, oddPowSize, dTmpSize;
188
    mp_int accum1;
189
    double *oddPowers[MAX_ODD_INTS];
190
191
    /* function for computing n0prime only works if n0 is odd */
192
193
    MP_DIGITS(&accum1) = 0;
194
195
    for (i = 0; i < MAX_ODD_INTS; ++i)
196
        oddPowers[i] = 0;
197
198
    MP_CHECKOK(mp_init_size(&accum1, 3 * nLen + 2));
199
200
    mp_set(&accum1, 1);
201
    MP_CHECKOK(s_mp_to_mont(&accum1, mmm, &accum1));
202
    MP_CHECKOK(s_mp_pad(&accum1, nLen));
203
204
    oddPowSize = 2 * nLen + 1;
205
    dTmpSize = 2 * oddPowSize;
206
    dSize = sizeof(double) * (nLen * 4 + 1 +
207
                              ((odd_ints + 1) * oddPowSize) + dTmpSize);
208
    dBuf = malloc(dSize);
209
    if (!dBuf) {
210
        res = MP_MEM;
211
        goto CLEANUP;
212
    }
213
    dm1 = dBuf;           /* array of d32 */
214
    dn = dBuf + nLen;     /* array of d32 */
215
    dSqr = dn + nLen;     /* array of d32 */
216
    d16Tmp = dSqr + nLen; /* array of d16 */
217
    dTmp = d16Tmp + oddPowSize;
218
219
    for (i = 0; i < odd_ints; ++i) {
220
        oddPowers[i] = dTmp;
221
        dTmp += oddPowSize;
222
    }
223
    mResult = (mp_digit *)(dTmp + dTmpSize); /* size is nLen + 1 */
224
225
    /* Make dn and dn0 */
226
    conv_i32_to_d32(dn, MP_DIGITS(modulus), nLen);
227
    dn0 = (double)(mmm->n0prime & 0xffff);
228
229
    /* Make dSqr */
230
    conv_i32_to_d32_and_d16(dm1, oddPowers[0], MP_DIGITS(montBase), nLen);
231
    mont_mulf_noconv(mResult, dm1, oddPowers[0],
232
                     dTmp, dn, MP_DIGITS(modulus), nLen, dn0);
233
    conv_i32_to_d32(dSqr, mResult, nLen);
234
235
    for (i = 1; i < odd_ints; ++i) {
236
        mont_mulf_noconv(mResult, dSqr, oddPowers[i - 1],
237
                         dTmp, dn, MP_DIGITS(modulus), nLen, dn0);
238
        conv_i32_to_d16(oddPowers[i], mResult, nLen);
239
    }
240
241
    s_mp_copy(MP_DIGITS(&accum1), mResult, nLen); /* from, to, len */
242
243
    for (expOff = bits_in_exponent - window_bits; expOff >= 0; expOff -= window_bits) {
244
        mp_size smallExp;
245
        MP_CHECKOK(mpl_get_bits(exponent, expOff, window_bits));
246
        smallExp = (mp_size)res;
247
248
        if (window_bits == 1) {
249
            if (!smallExp) {
250
                SQR;
251
            } else if (smallExp & 1) {
252
                SQR;
253
                MUL(0);
254
            } else {
255
                abort();
256
            }
257
        } else if (window_bits == 4) {
258
            if (!smallExp) {
259
                SQR;
260
                SQR;
261
                SQR;
262
                SQR;
263
            } else if (smallExp & 1) {
264
                SQR;
265
                SQR;
266
                SQR;
267
                SQR;
268
                MUL(smallExp / 2);
269
            } else if (smallExp & 2) {
270
                SQR;
271
                SQR;
272
                SQR;
273
                MUL(smallExp / 4);
274
                SQR;
275
            } else if (smallExp & 4) {
276
                SQR;
277
                SQR;
278
                MUL(smallExp / 8);
279
                SQR;
280
                SQR;
281
            } else if (smallExp & 8) {
282
                SQR;
283
                MUL(smallExp / 16);
284
                SQR;
285
                SQR;
286
                SQR;
287
            } else {
288
                abort();
289
            }
290
        } else if (window_bits == 5) {
291
            if (!smallExp) {
292
                SQR;
293
                SQR;
294
                SQR;
295
                SQR;
296
                SQR;
297
            } else if (smallExp & 1) {
298
                SQR;
299
                SQR;
300
                SQR;
301
                SQR;
302
                SQR;
303
                MUL(smallExp / 2);
304
            } else if (smallExp & 2) {
305
                SQR;
306
                SQR;
307
                SQR;
308
                SQR;
309
                MUL(smallExp / 4);
310
                SQR;
311
            } else if (smallExp & 4) {
312
                SQR;
313
                SQR;
314
                SQR;
315
                MUL(smallExp / 8);
316
                SQR;
317
                SQR;
318
            } else if (smallExp & 8) {
319
                SQR;
320
                SQR;
321
                MUL(smallExp / 16);
322
                SQR;
323
                SQR;
324
                SQR;
325
            } else if (smallExp & 0x10) {
326
                SQR;
327
                MUL(smallExp / 32);
328
                SQR;
329
                SQR;
330
                SQR;
331
                SQR;
332
            } else {
333
                abort();
334
            }
335
        } else if (window_bits == 6) {
336
            if (!smallExp) {
337
                SQR;
338
                SQR;
339
                SQR;
340
                SQR;
341
                SQR;
342
                SQR;
343
            } else if (smallExp & 1) {
344
                SQR;
345
                SQR;
346
                SQR;
347
                SQR;
348
                SQR;
349
                SQR;
350
                MUL(smallExp / 2);
351
            } else if (smallExp & 2) {
352
                SQR;
353
                SQR;
354
                SQR;
355
                SQR;
356
                SQR;
357
                MUL(smallExp / 4);
358
                SQR;
359
            } else if (smallExp & 4) {
360
                SQR;
361
                SQR;
362
                SQR;
363
                SQR;
364
                MUL(smallExp / 8);
365
                SQR;
366
                SQR;
367
            } else if (smallExp & 8) {
368
                SQR;
369
                SQR;
370
                SQR;
371
                MUL(smallExp / 16);
372
                SQR;
373
                SQR;
374
                SQR;
375
            } else if (smallExp & 0x10) {
376
                SQR;
377
                SQR;
378
                MUL(smallExp / 32);
379
                SQR;
380
                SQR;
381
                SQR;
382
                SQR;
383
            } else if (smallExp & 0x20) {
384
                SQR;
385
                MUL(smallExp / 64);
386
                SQR;
387
                SQR;
388
                SQR;
389
                SQR;
390
                SQR;
391
            } else {
392
                abort();
393
            }
394
        } else {
395
            abort();
396
        }
397
    }
398
399
    s_mp_copy(mResult, MP_DIGITS(&accum1), nLen); /* from, to, len */
400
401
    res = s_mp_redc(&accum1, mmm);
402
    mp_exch(&accum1, result);
403
404
CLEANUP:
405
    mp_clear(&accum1);
406
    if (dBuf) {
407
        if (dSize)
408
            memset(dBuf, 0, dSize);
409
        free(dBuf);
410
    }
411
412
    return res;
413
}
414
#undef SQR
415
#undef MUL
416
#endif
417
418
#define SQR(a, b)             \
419
0
    MP_CHECKOK(mp_sqr(a, b)); \
420
0
    MP_CHECKOK(s_mp_redc(b, mmm))
421
422
#if defined(MP_MONT_USE_MP_MUL)
423
#define MUL(x, a, b)                           \
424
    MP_CHECKOK(mp_mul(a, oddPowers + (x), b)); \
425
    MP_CHECKOK(s_mp_redc(b, mmm))
426
#else
427
#define MUL(x, a, b) \
428
0
    MP_CHECKOK(s_mp_mul_mont(a, oddPowers + (x), b, mmm))
429
#endif
430
431
#define SWAPPA  \
432
0
    ptmp = pa1; \
433
0
    pa1 = pa2;  \
434
0
    pa2 = ptmp
435
436
/* Do modular exponentiation using integer multiply code. */
437
mp_err
438
mp_exptmod_i(const mp_int *montBase,
439
             const mp_int *exponent,
440
             const mp_int *modulus,
441
             mp_int *result,
442
             mp_mont_modulus *mmm,
443
             int nLen,
444
             mp_size bits_in_exponent,
445
             mp_size window_bits,
446
             mp_size odd_ints)
447
0
{
448
0
    mp_int *pa1, *pa2, *ptmp;
449
0
    mp_size i;
450
0
    mp_err res;
451
0
    int expOff;
452
0
    mp_int accum1, accum2, power2, oddPowers[MAX_ODD_INTS];
453
0
454
0
    /* power2 = base ** 2; oddPowers[i] = base ** (2*i + 1); */
455
0
    /* oddPowers[i] = base ** (2*i + 1); */
456
0
457
0
    MP_DIGITS(&accum1) = 0;
458
0
    MP_DIGITS(&accum2) = 0;
459
0
    MP_DIGITS(&power2) = 0;
460
0
    for (i = 0; i < MAX_ODD_INTS; ++i) {
461
0
        MP_DIGITS(oddPowers + i) = 0;
462
0
    }
463
0
464
0
    MP_CHECKOK(mp_init_size(&accum1, 3 * nLen + 2));
465
0
    MP_CHECKOK(mp_init_size(&accum2, 3 * nLen + 2));
466
0
467
0
    MP_CHECKOK(mp_init_copy(&oddPowers[0], montBase));
468
0
469
0
    MP_CHECKOK(mp_init_size(&power2, nLen + 2 * MP_USED(montBase) + 2));
470
0
    MP_CHECKOK(mp_sqr(montBase, &power2)); /* power2 = montBase ** 2 */
471
0
    MP_CHECKOK(s_mp_redc(&power2, mmm));
472
0
473
0
    for (i = 1; i < odd_ints; ++i) {
474
0
        MP_CHECKOK(mp_init_size(oddPowers + i, nLen + 2 * MP_USED(&power2) + 2));
475
0
        MP_CHECKOK(mp_mul(oddPowers + (i - 1), &power2, oddPowers + i));
476
0
        MP_CHECKOK(s_mp_redc(oddPowers + i, mmm));
477
0
    }
478
0
479
0
    /* set accumulator to montgomery residue of 1 */
480
0
    mp_set(&accum1, 1);
481
0
    MP_CHECKOK(s_mp_to_mont(&accum1, mmm, &accum1));
482
0
    pa1 = &accum1;
483
0
    pa2 = &accum2;
484
0
485
0
    for (expOff = bits_in_exponent - window_bits; expOff >= 0; expOff -= window_bits) {
486
0
        mp_size smallExp;
487
0
        MP_CHECKOK(mpl_get_bits(exponent, expOff, window_bits));
488
0
        smallExp = (mp_size)res;
489
0
490
0
        if (window_bits == 1) {
491
0
            if (!smallExp) {
492
0
                SQR(pa1, pa2);
493
0
                SWAPPA;
494
0
            } else if (smallExp & 1) {
495
0
                SQR(pa1, pa2);
496
0
                MUL(0, pa2, pa1);
497
0
            } else {
498
0
                abort();
499
0
            }
500
0
        } else if (window_bits == 4) {
501
0
            if (!smallExp) {
502
0
                SQR(pa1, pa2);
503
0
                SQR(pa2, pa1);
504
0
                SQR(pa1, pa2);
505
0
                SQR(pa2, pa1);
506
0
            } else if (smallExp & 1) {
507
0
                SQR(pa1, pa2);
508
0
                SQR(pa2, pa1);
509
0
                SQR(pa1, pa2);
510
0
                SQR(pa2, pa1);
511
0
                MUL(smallExp / 2, pa1, pa2);
512
0
                SWAPPA;
513
0
            } else if (smallExp & 2) {
514
0
                SQR(pa1, pa2);
515
0
                SQR(pa2, pa1);
516
0
                SQR(pa1, pa2);
517
0
                MUL(smallExp / 4, pa2, pa1);
518
0
                SQR(pa1, pa2);
519
0
                SWAPPA;
520
0
            } else if (smallExp & 4) {
521
0
                SQR(pa1, pa2);
522
0
                SQR(pa2, pa1);
523
0
                MUL(smallExp / 8, pa1, pa2);
524
0
                SQR(pa2, pa1);
525
0
                SQR(pa1, pa2);
526
0
                SWAPPA;
527
0
            } else if (smallExp & 8) {
528
0
                SQR(pa1, pa2);
529
0
                MUL(smallExp / 16, pa2, pa1);
530
0
                SQR(pa1, pa2);
531
0
                SQR(pa2, pa1);
532
0
                SQR(pa1, pa2);
533
0
                SWAPPA;
534
0
            } else {
535
0
                abort();
536
0
            }
537
0
        } else if (window_bits == 5) {
538
0
            if (!smallExp) {
539
0
                SQR(pa1, pa2);
540
0
                SQR(pa2, pa1);
541
0
                SQR(pa1, pa2);
542
0
                SQR(pa2, pa1);
543
0
                SQR(pa1, pa2);
544
0
                SWAPPA;
545
0
            } else if (smallExp & 1) {
546
0
                SQR(pa1, pa2);
547
0
                SQR(pa2, pa1);
548
0
                SQR(pa1, pa2);
549
0
                SQR(pa2, pa1);
550
0
                SQR(pa1, pa2);
551
0
                MUL(smallExp / 2, pa2, pa1);
552
0
            } else if (smallExp & 2) {
553
0
                SQR(pa1, pa2);
554
0
                SQR(pa2, pa1);
555
0
                SQR(pa1, pa2);
556
0
                SQR(pa2, pa1);
557
0
                MUL(smallExp / 4, pa1, pa2);
558
0
                SQR(pa2, pa1);
559
0
            } else if (smallExp & 4) {
560
0
                SQR(pa1, pa2);
561
0
                SQR(pa2, pa1);
562
0
                SQR(pa1, pa2);
563
0
                MUL(smallExp / 8, pa2, pa1);
564
0
                SQR(pa1, pa2);
565
0
                SQR(pa2, pa1);
566
0
            } else if (smallExp & 8) {
567
0
                SQR(pa1, pa2);
568
0
                SQR(pa2, pa1);
569
0
                MUL(smallExp / 16, pa1, pa2);
570
0
                SQR(pa2, pa1);
571
0
                SQR(pa1, pa2);
572
0
                SQR(pa2, pa1);
573
0
            } else if (smallExp & 0x10) {
574
0
                SQR(pa1, pa2);
575
0
                MUL(smallExp / 32, pa2, pa1);
576
0
                SQR(pa1, pa2);
577
0
                SQR(pa2, pa1);
578
0
                SQR(pa1, pa2);
579
0
                SQR(pa2, pa1);
580
0
            } else {
581
0
                abort();
582
0
            }
583
0
        } else if (window_bits == 6) {
584
0
            if (!smallExp) {
585
0
                SQR(pa1, pa2);
586
0
                SQR(pa2, pa1);
587
0
                SQR(pa1, pa2);
588
0
                SQR(pa2, pa1);
589
0
                SQR(pa1, pa2);
590
0
                SQR(pa2, pa1);
591
0
            } else if (smallExp & 1) {
592
0
                SQR(pa1, pa2);
593
0
                SQR(pa2, pa1);
594
0
                SQR(pa1, pa2);
595
0
                SQR(pa2, pa1);
596
0
                SQR(pa1, pa2);
597
0
                SQR(pa2, pa1);
598
0
                MUL(smallExp / 2, pa1, pa2);
599
0
                SWAPPA;
600
0
            } else if (smallExp & 2) {
601
0
                SQR(pa1, pa2);
602
0
                SQR(pa2, pa1);
603
0
                SQR(pa1, pa2);
604
0
                SQR(pa2, pa1);
605
0
                SQR(pa1, pa2);
606
0
                MUL(smallExp / 4, pa2, pa1);
607
0
                SQR(pa1, pa2);
608
0
                SWAPPA;
609
0
            } else if (smallExp & 4) {
610
0
                SQR(pa1, pa2);
611
0
                SQR(pa2, pa1);
612
0
                SQR(pa1, pa2);
613
0
                SQR(pa2, pa1);
614
0
                MUL(smallExp / 8, pa1, pa2);
615
0
                SQR(pa2, pa1);
616
0
                SQR(pa1, pa2);
617
0
                SWAPPA;
618
0
            } else if (smallExp & 8) {
619
0
                SQR(pa1, pa2);
620
0
                SQR(pa2, pa1);
621
0
                SQR(pa1, pa2);
622
0
                MUL(smallExp / 16, pa2, pa1);
623
0
                SQR(pa1, pa2);
624
0
                SQR(pa2, pa1);
625
0
                SQR(pa1, pa2);
626
0
                SWAPPA;
627
0
            } else if (smallExp & 0x10) {
628
0
                SQR(pa1, pa2);
629
0
                SQR(pa2, pa1);
630
0
                MUL(smallExp / 32, pa1, pa2);
631
0
                SQR(pa2, pa1);
632
0
                SQR(pa1, pa2);
633
0
                SQR(pa2, pa1);
634
0
                SQR(pa1, pa2);
635
0
                SWAPPA;
636
0
            } else if (smallExp & 0x20) {
637
0
                SQR(pa1, pa2);
638
0
                MUL(smallExp / 64, pa2, pa1);
639
0
                SQR(pa1, pa2);
640
0
                SQR(pa2, pa1);
641
0
                SQR(pa1, pa2);
642
0
                SQR(pa2, pa1);
643
0
                SQR(pa1, pa2);
644
0
                SWAPPA;
645
0
            } else {
646
0
                abort();
647
0
            }
648
0
        } else {
649
0
            abort();
650
0
        }
651
0
    }
652
0
653
0
    res = s_mp_redc(pa1, mmm);
654
0
    mp_exch(pa1, result);
655
0
656
0
CLEANUP:
657
0
    mp_clear(&accum1);
658
0
    mp_clear(&accum2);
659
0
    mp_clear(&power2);
660
0
    for (i = 0; i < odd_ints; ++i) {
661
0
        mp_clear(oddPowers + i);
662
0
    }
663
0
    return res;
664
0
}
665
#undef SQR
666
#undef MUL
667
668
#ifdef MP_USING_CACHE_SAFE_MOD_EXP
669
unsigned int mp_using_cache_safe_exp = 1;
670
#endif
671
672
mp_err
673
mp_set_safe_modexp(int value)
674
0
{
675
0
#ifdef MP_USING_CACHE_SAFE_MOD_EXP
676
0
    mp_using_cache_safe_exp = value;
677
0
    return MP_OKAY;
678
#else
679
    if (value == 0) {
680
        return MP_OKAY;
681
    }
682
    return MP_BADARG;
683
#endif
684
}
685
686
#ifdef MP_USING_CACHE_SAFE_MOD_EXP
687
0
#define WEAVE_WORD_SIZE 4
688
689
/*
690
 * mpi_to_weave takes an array of bignums, a matrix in which each bignum
691
 * occupies all the columns of a row, and transposes it into a matrix in
692
 * which each bignum occupies a column of every row.  The first row of the
693
 * input matrix becomes the first column of the output matrix.  The n'th
694
 * row of input becomes the n'th column of output.  The input data is said
695
 * to be "interleaved" or "woven" into the output matrix.
696
 *
697
 * The array of bignums is left in this woven form.  Each time a single
698
 * bignum value is needed, it is recreated by fetching the n'th column,
699
 * forming a single row which is the new bignum.
700
 *
701
 * The purpose of this interleaving is make it impossible to determine which
702
 * of the bignums is being used in any one operation by examining the pattern
703
 * of cache misses.
704
 *
705
 * The weaving function does not transpose the entire input matrix in one call.
706
 * It transposes 4 rows of mp_ints into their respective columns of output.
707
 *
708
 * This implementation treats each mp_int bignum as an array of mp_digits,
709
 * It stores those bytes as a column of mp_digits in the output matrix.  It
710
 * doesn't care if the machine uses big-endian or little-endian byte ordering
711
 * within mp_digits.
712
 *
713
 * "bignums" is an array of mp_ints.
714
 * It points to four rows, four mp_ints, a subset of a larger array of mp_ints.
715
 *
716
 * "weaved" is the weaved output matrix.
717
 * The first byte of bignums[0] is stored in weaved[0].
718
 *
719
 * "nBignums" is the total number of bignums in the array of which "bignums"
720
 * is a part.
721
 *
722
 * "nDigits" is the size in mp_digits of each mp_int in the "bignums" array.
723
 * mp_ints that use less than nDigits digits are logically padded with zeros
724
 * while being stored in the weaved array.
725
 */
726
mp_err mpi_to_weave(const mp_int *bignums,
727
                    mp_digit *weaved,
728
                    mp_size nDigits,  /* in each mp_int of input */
729
                    mp_size nBignums) /* in the entire source array */
730
0
{
731
0
    mp_size i;
732
0
    mp_digit *endDest = weaved + (nDigits * nBignums);
733
0
734
0
    for (i = 0; i < WEAVE_WORD_SIZE; i++) {
735
0
        mp_size used = MP_USED(&bignums[i]);
736
0
        mp_digit *pSrc = MP_DIGITS(&bignums[i]);
737
0
        mp_digit *endSrc = pSrc + used;
738
0
        mp_digit *pDest = weaved + i;
739
0
740
0
        ARGCHK(MP_SIGN(&bignums[i]) == MP_ZPOS, MP_BADARG);
741
0
        ARGCHK(used <= nDigits, MP_BADARG);
742
0
743
0
        for (; pSrc < endSrc; pSrc++) {
744
0
            *pDest = *pSrc;
745
0
            pDest += nBignums;
746
0
        }
747
0
        while (pDest < endDest) {
748
0
            *pDest = 0;
749
0
            pDest += nBignums;
750
0
        }
751
0
    }
752
0
753
0
    return MP_OKAY;
754
0
}
755
756
/*
757
 * These functions return 0xffffffff if the output is true, and 0 otherwise.
758
 */
759
0
#define CONST_TIME_MSB(x) (0L - ((x) >> (8 * sizeof(x) - 1)))
760
0
#define CONST_TIME_EQ_Z(x) CONST_TIME_MSB(~(x) & ((x)-1))
761
0
#define CONST_TIME_EQ(a, b) CONST_TIME_EQ_Z((a) ^ (b))
762
763
/* Reverse the operation above for one mp_int.
764
 * Reconstruct one mp_int from its column in the weaved array.
765
 * Every read accesses every element of the weaved array, in order to
766
 * avoid timing attacks based on patterns of memory accesses.
767
 */
768
mp_err weave_to_mpi(mp_int *a,              /* out, result */
769
                    const mp_digit *weaved, /* in, byte matrix */
770
                    mp_size index,          /* which column to read */
771
                    mp_size nDigits,        /* number of mp_digits in each bignum */
772
                    mp_size nBignums)       /* width of the matrix */
773
0
{
774
0
    /* these are indices, but need to be the same size as mp_digit
775
0
     * because of the CONST_TIME operations */
776
0
    mp_digit i, j;
777
0
    mp_digit d;
778
0
    mp_digit *pDest = MP_DIGITS(a);
779
0
780
0
    MP_SIGN(a) = MP_ZPOS;
781
0
    MP_USED(a) = nDigits;
782
0
783
0
    assert(weaved != NULL);
784
0
785
0
    /* Fetch the proper column in constant time, indexing over the whole array */
786
0
    for (i = 0; i < nDigits; ++i) {
787
0
        d = 0;
788
0
        for (j = 0; j < nBignums; ++j) {
789
0
            d |= weaved[i * nBignums + j] & CONST_TIME_EQ(j, index);
790
0
        }
791
0
        pDest[i] = d;
792
0
    }
793
0
794
0
    s_mp_clamp(a);
795
0
    return MP_OKAY;
796
0
}
797
798
#define SQR(a, b)             \
799
0
    MP_CHECKOK(mp_sqr(a, b)); \
800
0
    MP_CHECKOK(s_mp_redc(b, mmm))
801
802
#if defined(MP_MONT_USE_MP_MUL)
803
#define MUL_NOWEAVE(x, a, b)     \
804
    MP_CHECKOK(mp_mul(a, x, b)); \
805
    MP_CHECKOK(s_mp_redc(b, mmm))
806
#else
807
#define MUL_NOWEAVE(x, a, b) \
808
0
    MP_CHECKOK(s_mp_mul_mont(a, x, b, mmm))
809
#endif
810
811
#define MUL(x, a, b)                                               \
812
0
    MP_CHECKOK(weave_to_mpi(&tmp, powers, (x), nLen, num_powers)); \
813
0
    MUL_NOWEAVE(&tmp, a, b)
814
815
#define SWAPPA  \
816
0
    ptmp = pa1; \
817
0
    pa1 = pa2;  \
818
0
    pa2 = ptmp
819
0
#define MP_ALIGN(x, y) ((((ptrdiff_t)(x)) + ((y)-1)) & (((ptrdiff_t)0) - (y)))
820
821
/* Do modular exponentiation using integer multiply code. */
822
mp_err
823
mp_exptmod_safe_i(const mp_int *montBase,
824
                  const mp_int *exponent,
825
                  const mp_int *modulus,
826
                  mp_int *result,
827
                  mp_mont_modulus *mmm,
828
                  int nLen,
829
                  mp_size bits_in_exponent,
830
                  mp_size window_bits,
831
                  mp_size num_powers)
832
0
{
833
0
    mp_int *pa1, *pa2, *ptmp;
834
0
    mp_size i;
835
0
    mp_size first_window;
836
0
    mp_err res;
837
0
    int expOff;
838
0
    mp_int accum1, accum2, accum[WEAVE_WORD_SIZE];
839
0
    mp_int tmp;
840
0
    mp_digit *powersArray = NULL;
841
0
    mp_digit *powers = NULL;
842
0
843
0
    MP_DIGITS(&accum1) = 0;
844
0
    MP_DIGITS(&accum2) = 0;
845
0
    MP_DIGITS(&accum[0]) = 0;
846
0
    MP_DIGITS(&accum[1]) = 0;
847
0
    MP_DIGITS(&accum[2]) = 0;
848
0
    MP_DIGITS(&accum[3]) = 0;
849
0
    MP_DIGITS(&tmp) = 0;
850
0
851
0
    /* grab the first window value. This allows us to preload accumulator1
852
0
   * and save a conversion, some squares and a multiple*/
853
0
    MP_CHECKOK(mpl_get_bits(exponent,
854
0
                            bits_in_exponent - window_bits, window_bits));
855
0
    first_window = (mp_size)res;
856
0
857
0
    MP_CHECKOK(mp_init_size(&accum1, 3 * nLen + 2));
858
0
    MP_CHECKOK(mp_init_size(&accum2, 3 * nLen + 2));
859
0
860
0
    /* build the first WEAVE_WORD powers inline */
861
0
    /* if WEAVE_WORD_SIZE is not 4, this code will have to change */
862
0
    if (num_powers > 2) {
863
0
        MP_CHECKOK(mp_init_size(&accum[0], 3 * nLen + 2));
864
0
        MP_CHECKOK(mp_init_size(&accum[1], 3 * nLen + 2));
865
0
        MP_CHECKOK(mp_init_size(&accum[2], 3 * nLen + 2));
866
0
        MP_CHECKOK(mp_init_size(&accum[3], 3 * nLen + 2));
867
0
        mp_set(&accum[0], 1);
868
0
        MP_CHECKOK(s_mp_to_mont(&accum[0], mmm, &accum[0]));
869
0
        MP_CHECKOK(mp_copy(montBase, &accum[1]));
870
0
        SQR(montBase, &accum[2]);
871
0
        MUL_NOWEAVE(montBase, &accum[2], &accum[3]);
872
0
        powersArray = (mp_digit *)malloc(num_powers * (nLen * sizeof(mp_digit) + 1));
873
0
        if (!powersArray) {
874
0
            res = MP_MEM;
875
0
            goto CLEANUP;
876
0
        }
877
0
        /* powers[i] = base ** (i); */
878
0
        powers = (mp_digit *)MP_ALIGN(powersArray, num_powers);
879
0
        MP_CHECKOK(mpi_to_weave(accum, powers, nLen, num_powers));
880
0
        if (first_window < 4) {
881
0
            MP_CHECKOK(mp_copy(&accum[first_window], &accum1));
882
0
            first_window = num_powers;
883
0
        }
884
0
    } else {
885
0
        if (first_window == 0) {
886
0
            mp_set(&accum1, 1);
887
0
            MP_CHECKOK(s_mp_to_mont(&accum1, mmm, &accum1));
888
0
        } else {
889
0
            /* assert first_window == 1? */
890
0
            MP_CHECKOK(mp_copy(montBase, &accum1));
891
0
        }
892
0
    }
893
0
894
0
    /*
895
0
     * calculate all the powers in the powers array.
896
0
     * this adds 2**(k-1)-2 square operations over just calculating the
897
0
     * odd powers where k is the window size in the two other mp_modexpt
898
0
     * implementations in this file. We will get some of that
899
0
     * back by not needing the first 'k' squares and one multiply for the
900
0
     * first window.
901
0
     * Given the value of 4 for WEAVE_WORD_SIZE, this loop will only execute if
902
0
     * num_powers > 2, in which case powers will have been allocated.
903
0
     */
904
0
    for (i = WEAVE_WORD_SIZE; i < num_powers; i++) {
905
0
        int acc_index = i & (WEAVE_WORD_SIZE - 1); /* i % WEAVE_WORD_SIZE */
906
0
        if (i & 1) {
907
0
            MUL_NOWEAVE(montBase, &accum[acc_index - 1], &accum[acc_index]);
908
0
            /* we've filled the array do our 'per array' processing */
909
0
            if (acc_index == (WEAVE_WORD_SIZE - 1)) {
910
0
                MP_CHECKOK(mpi_to_weave(accum, powers + i - (WEAVE_WORD_SIZE - 1),
911
0
                                        nLen, num_powers));
912
0
913
0
                if (first_window <= i) {
914
0
                    MP_CHECKOK(mp_copy(&accum[first_window & (WEAVE_WORD_SIZE - 1)],
915
0
                                       &accum1));
916
0
                    first_window = num_powers;
917
0
                }
918
0
            }
919
0
        } else {
920
0
            /* up to 8 we can find 2^i-1 in the accum array, but at 8 we our source
921
0
             * and target are the same so we need to copy.. After that, the
922
0
             * value is overwritten, so we need to fetch it from the stored
923
0
             * weave array */
924
0
            if (i > 2 * WEAVE_WORD_SIZE) {
925
0
                MP_CHECKOK(weave_to_mpi(&accum2, powers, i / 2, nLen, num_powers));
926
0
                SQR(&accum2, &accum[acc_index]);
927
0
            } else {
928
0
                int half_power_index = (i / 2) & (WEAVE_WORD_SIZE - 1);
929
0
                if (half_power_index == acc_index) {
930
0
                    /* copy is cheaper than weave_to_mpi */
931
0
                    MP_CHECKOK(mp_copy(&accum[half_power_index], &accum2));
932
0
                    SQR(&accum2, &accum[acc_index]);
933
0
                } else {
934
0
                    SQR(&accum[half_power_index], &accum[acc_index]);
935
0
                }
936
0
            }
937
0
        }
938
0
    }
939
0
/* if the accum1 isn't set, Then there is something wrong with our logic
940
0
   * above and is an internal programming error.
941
0
   */
942
#if MP_ARGCHK == 2
943
    assert(MP_USED(&accum1) != 0);
944
#endif
945
946
0
    /* set accumulator to montgomery residue of 1 */
947
0
    pa1 = &accum1;
948
0
    pa2 = &accum2;
949
0
950
0
    /* tmp is not used if window_bits == 1. */
951
0
    if (window_bits != 1) {
952
0
        MP_CHECKOK(mp_init_size(&tmp, 3 * nLen + 2));
953
0
    }
954
0
955
0
    for (expOff = bits_in_exponent - window_bits * 2; expOff >= 0; expOff -= window_bits) {
956
0
        mp_size smallExp;
957
0
        MP_CHECKOK(mpl_get_bits(exponent, expOff, window_bits));
958
0
        smallExp = (mp_size)res;
959
0
960
0
        /* handle unroll the loops */
961
0
        switch (window_bits) {
962
0
            case 1:
963
0
                if (!smallExp) {
964
0
                    SQR(pa1, pa2);
965
0
                    SWAPPA;
966
0
                } else if (smallExp & 1) {
967
0
                    SQR(pa1, pa2);
968
0
                    MUL_NOWEAVE(montBase, pa2, pa1);
969
0
                } else {
970
0
                    abort();
971
0
                }
972
0
                break;
973
0
            case 6:
974
0
                SQR(pa1, pa2);
975
0
                SQR(pa2, pa1);
976
0
            /* fall through */
977
0
            case 4:
978
0
                SQR(pa1, pa2);
979
0
                SQR(pa2, pa1);
980
0
                SQR(pa1, pa2);
981
0
                SQR(pa2, pa1);
982
0
                MUL(smallExp, pa1, pa2);
983
0
                SWAPPA;
984
0
                break;
985
0
            case 5:
986
0
                SQR(pa1, pa2);
987
0
                SQR(pa2, pa1);
988
0
                SQR(pa1, pa2);
989
0
                SQR(pa2, pa1);
990
0
                SQR(pa1, pa2);
991
0
                MUL(smallExp, pa2, pa1);
992
0
                break;
993
0
            default:
994
0
                abort(); /* could do a loop? */
995
0
        }
996
0
    }
997
0
998
0
    res = s_mp_redc(pa1, mmm);
999
0
    mp_exch(pa1, result);
1000
0
1001
0
CLEANUP:
1002
0
    mp_clear(&accum1);
1003
0
    mp_clear(&accum2);
1004
0
    mp_clear(&accum[0]);
1005
0
    mp_clear(&accum[1]);
1006
0
    mp_clear(&accum[2]);
1007
0
    mp_clear(&accum[3]);
1008
0
    mp_clear(&tmp);
1009
0
    /* PORT_Memset(powers,0,num_powers*nLen*sizeof(mp_digit)); */
1010
0
    free(powersArray);
1011
0
    return res;
1012
0
}
1013
#undef SQR
1014
#undef MUL
1015
#endif
1016
1017
mp_err
1018
mp_exptmod(const mp_int *inBase, const mp_int *exponent,
1019
           const mp_int *modulus, mp_int *result)
1020
0
{
1021
0
    const mp_int *base;
1022
0
    mp_size bits_in_exponent, i, window_bits, odd_ints;
1023
0
    mp_err res;
1024
0
    int nLen;
1025
0
    mp_int montBase, goodBase;
1026
0
    mp_mont_modulus mmm;
1027
0
#ifdef MP_USING_CACHE_SAFE_MOD_EXP
1028
0
    static unsigned int max_window_bits;
1029
0
#endif
1030
0
1031
0
    /* function for computing n0prime only works if n0 is odd */
1032
0
    if (!mp_isodd(modulus))
1033
0
        return s_mp_exptmod(inBase, exponent, modulus, result);
1034
0
1035
0
    MP_DIGITS(&montBase) = 0;
1036
0
    MP_DIGITS(&goodBase) = 0;
1037
0
1038
0
    if (mp_cmp(inBase, modulus) < 0) {
1039
0
        base = inBase;
1040
0
    } else {
1041
0
        MP_CHECKOK(mp_init(&goodBase));
1042
0
        base = &goodBase;
1043
0
        MP_CHECKOK(mp_mod(inBase, modulus, &goodBase));
1044
0
    }
1045
0
1046
0
    nLen = MP_USED(modulus);
1047
0
    MP_CHECKOK(mp_init_size(&montBase, 2 * nLen + 2));
1048
0
1049
0
    mmm.N = *modulus; /* a copy of the mp_int struct */
1050
0
1051
0
    /* compute n0', given n0, n0' = -(n0 ** -1) mod MP_RADIX
1052
0
    **        where n0 = least significant mp_digit of N, the modulus.
1053
0
    */
1054
0
    mmm.n0prime = 0 - s_mp_invmod_radix(MP_DIGIT(modulus, 0));
1055
0
1056
0
    MP_CHECKOK(s_mp_to_mont(base, &mmm, &montBase));
1057
0
1058
0
    bits_in_exponent = mpl_significant_bits(exponent);
1059
0
#ifdef MP_USING_CACHE_SAFE_MOD_EXP
1060
0
    if (mp_using_cache_safe_exp) {
1061
0
        if (bits_in_exponent > 780)
1062
0
            window_bits = 6;
1063
0
        else if (bits_in_exponent > 256)
1064
0
            window_bits = 5;
1065
0
        else if (bits_in_exponent > 20)
1066
0
            window_bits = 4;
1067
0
        /* RSA public key exponents are typically under 20 bits (common values
1068
0
         * are: 3, 17, 65537) and a 4-bit window is inefficient
1069
0
         */
1070
0
        else
1071
0
            window_bits = 1;
1072
0
    } else
1073
0
#endif
1074
0
        if (bits_in_exponent > 480)
1075
0
        window_bits = 6;
1076
0
    else if (bits_in_exponent > 160)
1077
0
        window_bits = 5;
1078
0
    else if (bits_in_exponent > 20)
1079
0
        window_bits = 4;
1080
0
    /* RSA public key exponents are typically under 20 bits (common values
1081
0
     * are: 3, 17, 65537) and a 4-bit window is inefficient
1082
0
     */
1083
0
    else
1084
0
        window_bits = 1;
1085
0
1086
0
#ifdef MP_USING_CACHE_SAFE_MOD_EXP
1087
0
    /*
1088
0
     * clamp the window size based on
1089
0
     * the cache line size.
1090
0
     */
1091
0
    if (!max_window_bits) {
1092
0
        unsigned long cache_size = s_mpi_getProcessorLineSize();
1093
0
        /* processor has no cache, use 'fast' code always */
1094
0
        if (cache_size == 0) {
1095
0
            mp_using_cache_safe_exp = 0;
1096
0
        }
1097
0
        if ((cache_size == 0) || (cache_size >= 64)) {
1098
0
            max_window_bits = 6;
1099
0
        } else if (cache_size >= 32) {
1100
0
            max_window_bits = 5;
1101
0
        } else if (cache_size >= 16) {
1102
0
            max_window_bits = 4;
1103
0
        } else
1104
0
            max_window_bits = 1; /* should this be an assert? */
1105
0
    }
1106
0
1107
0
    /* clamp the window size down before we caclulate bits_in_exponent */
1108
0
    if (mp_using_cache_safe_exp) {
1109
0
        if (window_bits > max_window_bits) {
1110
0
            window_bits = max_window_bits;
1111
0
        }
1112
0
    }
1113
0
#endif
1114
0
1115
0
    odd_ints = 1 << (window_bits - 1);
1116
0
    i = bits_in_exponent % window_bits;
1117
0
    if (i != 0) {
1118
0
        bits_in_exponent += window_bits - i;
1119
0
    }
1120
0
1121
#ifdef MP_USING_MONT_MULF
1122
    if (mp_using_mont_mulf) {
1123
        MP_CHECKOK(s_mp_pad(&montBase, nLen));
1124
        res = mp_exptmod_f(&montBase, exponent, modulus, result, &mmm, nLen,
1125
                           bits_in_exponent, window_bits, odd_ints);
1126
    } else
1127
#endif
1128
#ifdef MP_USING_CACHE_SAFE_MOD_EXP
1129
0
        if (mp_using_cache_safe_exp) {
1130
0
        res = mp_exptmod_safe_i(&montBase, exponent, modulus, result, &mmm, nLen,
1131
0
                                bits_in_exponent, window_bits, 1 << window_bits);
1132
0
    } else
1133
0
#endif
1134
0
        res = mp_exptmod_i(&montBase, exponent, modulus, result, &mmm, nLen,
1135
0
                           bits_in_exponent, window_bits, odd_ints);
1136
0
1137
0
CLEANUP:
1138
0
    mp_clear(&montBase);
1139
0
    mp_clear(&goodBase);
1140
0
    /* Don't mp_clear mmm.N because it is merely a copy of modulus.
1141
0
    ** Just zap it.
1142
0
    */
1143
0
    memset(&mmm, 0, sizeof mmm);
1144
0
    return res;
1145
0
}