Coverage Report

Created: 2023-12-14 14:12

/src/mbedtls/library/bignum.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *  Multi-precision integer library
3
 *
4
 *  Copyright The Mbed TLS Contributors
5
 *  SPDX-License-Identifier: Apache-2.0
6
 *
7
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8
 *  not use this file except in compliance with the License.
9
 *  You may obtain a copy of the License at
10
 *
11
 *  http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 *  Unless required by applicable law or agreed to in writing, software
14
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
 *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 *  See the License for the specific language governing permissions and
17
 *  limitations under the License.
18
 */
19
20
/*
21
 *  The following sources were referenced in the design of this Multi-precision
22
 *  Integer library:
23
 *
24
 *  [1] Handbook of Applied Cryptography - 1997
25
 *      Menezes, van Oorschot and Vanstone
26
 *
27
 *  [2] Multi-Precision Math
28
 *      Tom St Denis
29
 *      https://github.com/libtom/libtommath/blob/develop/tommath.pdf
30
 *
31
 *  [3] GNU Multi-Precision Arithmetic Library
32
 *      https://gmplib.org/manual/index.html
33
 *
34
 */
35
36
#include "common.h"
37
38
#if defined(MBEDTLS_BIGNUM_C)
39
40
#include "mbedtls/bignum.h"
41
#include "bignum_core.h"
42
#include "bn_mul.h"
43
#include "mbedtls/platform_util.h"
44
#include "mbedtls/error.h"
45
#include "constant_time_internal.h"
46
47
#include <limits.h>
48
#include <string.h>
49
50
#include "mbedtls/platform.h"
51
52
#define MPI_VALIDATE_RET(cond)                                       \
53
27.5M
    MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA)
54
#define MPI_VALIDATE(cond)                                           \
55
1.29M
    MBEDTLS_INTERNAL_VALIDATE(cond)
56
57
0
#define MPI_SIZE_T_MAX  ((size_t) -1)   /* SIZE_T_MAX is not standard */
58
59
/* Implementation that should never be optimized out by the compiler */
60
static void mbedtls_mpi_zeroize(mbedtls_mpi_uint *v, size_t n)
61
321k
{
62
321k
    mbedtls_platform_zeroize(v, ciL * n);
63
321k
}
64
65
/*
66
 * Initialize one MPI
67
 */
68
void mbedtls_mpi_init(mbedtls_mpi *X)
69
1.29M
{
70
1.29M
    MPI_VALIDATE(X != NULL);
71
72
1.29M
    X->s = 1;
73
1.29M
    X->n = 0;
74
1.29M
    X->p = NULL;
75
1.29M
}
76
77
/*
78
 * Unallocate one MPI
79
 */
80
void mbedtls_mpi_free(mbedtls_mpi *X)
81
1.87M
{
82
1.87M
    if (X == NULL) {
83
0
        return;
84
0
    }
85
86
1.87M
    if (X->p != NULL) {
87
283k
        mbedtls_mpi_zeroize(X->p, X->n);
88
283k
        mbedtls_free(X->p);
89
283k
    }
90
91
1.87M
    X->s = 1;
92
1.87M
    X->n = 0;
93
1.87M
    X->p = NULL;
94
1.87M
}
95
96
/*
97
 * Enlarge to the specified number of limbs
98
 */
99
int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs)
100
4.89M
{
101
4.89M
    mbedtls_mpi_uint *p;
102
4.89M
    MPI_VALIDATE_RET(X != NULL);
103
104
4.89M
    if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
105
0
        return MBEDTLS_ERR_MPI_ALLOC_FAILED;
106
0
    }
107
108
4.89M
    if (X->n < nblimbs) {
109
332k
        if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(nblimbs, ciL)) == NULL) {
110
0
            return MBEDTLS_ERR_MPI_ALLOC_FAILED;
111
0
        }
112
113
332k
        if (X->p != NULL) {
114
36.3k
            memcpy(p, X->p, X->n * ciL);
115
36.3k
            mbedtls_mpi_zeroize(X->p, X->n);
116
36.3k
            mbedtls_free(X->p);
117
36.3k
        }
118
119
332k
        X->n = nblimbs;
120
332k
        X->p = p;
121
332k
    }
122
123
4.89M
    return 0;
124
4.89M
}
125
126
/*
127
 * Resize down as much as possible,
128
 * while keeping at least the specified number of limbs
129
 */
130
int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs)
131
1.76k
{
132
1.76k
    mbedtls_mpi_uint *p;
133
1.76k
    size_t i;
134
1.76k
    MPI_VALIDATE_RET(X != NULL);
135
136
1.76k
    if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
137
0
        return MBEDTLS_ERR_MPI_ALLOC_FAILED;
138
0
    }
139
140
    /* Actually resize up if there are currently fewer than nblimbs limbs. */
141
1.76k
    if (X->n <= nblimbs) {
142
0
        return mbedtls_mpi_grow(X, nblimbs);
143
0
    }
144
    /* After this point, then X->n > nblimbs and in particular X->n > 0. */
145
146
14.0k
    for (i = X->n - 1; i > 0; i--) {
147
14.0k
        if (X->p[i] != 0) {
148
1.76k
            break;
149
1.76k
        }
150
14.0k
    }
151
1.76k
    i++;
152
153
1.76k
    if (i < nblimbs) {
154
0
        i = nblimbs;
155
0
    }
156
157
1.76k
    if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(i, ciL)) == NULL) {
158
0
        return MBEDTLS_ERR_MPI_ALLOC_FAILED;
159
0
    }
160
161
1.76k
    if (X->p != NULL) {
162
1.76k
        memcpy(p, X->p, i * ciL);
163
1.76k
        mbedtls_mpi_zeroize(X->p, X->n);
164
1.76k
        mbedtls_free(X->p);
165
1.76k
    }
166
167
1.76k
    X->n = i;
168
1.76k
    X->p = p;
169
170
1.76k
    return 0;
171
1.76k
}
172
173
/* Resize X to have exactly n limbs and set it to 0. */
174
static int mbedtls_mpi_resize_clear(mbedtls_mpi *X, size_t limbs)
175
73.0k
{
176
73.0k
    if (limbs == 0) {
177
290
        mbedtls_mpi_free(X);
178
290
        return 0;
179
72.7k
    } else if (X->n == limbs) {
180
0
        memset(X->p, 0, limbs * ciL);
181
0
        X->s = 1;
182
0
        return 0;
183
72.7k
    } else {
184
72.7k
        mbedtls_mpi_free(X);
185
72.7k
        return mbedtls_mpi_grow(X, limbs);
186
72.7k
    }
187
73.0k
}
188
189
/*
190
 * Copy the contents of Y into X.
191
 *
192
 * This function is not constant-time. Leading zeros in Y may be removed.
193
 *
194
 * Ensure that X does not shrink. This is not guaranteed by the public API,
195
 * but some code in the bignum module relies on this property, for example
196
 * in mbedtls_mpi_exp_mod().
197
 */
198
int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
199
687k
{
200
687k
    int ret = 0;
201
687k
    size_t i;
202
687k
    MPI_VALIDATE_RET(X != NULL);
203
687k
    MPI_VALIDATE_RET(Y != NULL);
204
205
687k
    if (X == Y) {
206
179k
        return 0;
207
179k
    }
208
209
508k
    if (Y->n == 0) {
210
15
        if (X->n != 0) {
211
0
            X->s = 1;
212
0
            memset(X->p, 0, X->n * ciL);
213
0
        }
214
15
        return 0;
215
15
    }
216
217
7.95M
    for (i = Y->n - 1; i > 0; i--) {
218
7.95M
        if (Y->p[i] != 0) {
219
506k
            break;
220
506k
        }
221
7.95M
    }
222
508k
    i++;
223
224
508k
    X->s = Y->s;
225
226
508k
    if (X->n < i) {
227
156k
        MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i));
228
351k
    } else {
229
351k
        memset(X->p + i, 0, (X->n - i) * ciL);
230
351k
    }
231
232
508k
    memcpy(X->p, Y->p, i * ciL);
233
234
508k
cleanup:
235
236
508k
    return ret;
237
508k
}
238
239
/*
240
 * Swap the contents of X and Y
241
 */
242
void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
243
0
{
244
0
    mbedtls_mpi T;
245
0
    MPI_VALIDATE(X != NULL);
246
0
    MPI_VALIDATE(Y != NULL);
247
248
0
    memcpy(&T,  X, sizeof(mbedtls_mpi));
249
0
    memcpy(X,  Y, sizeof(mbedtls_mpi));
250
0
    memcpy(Y, &T, sizeof(mbedtls_mpi));
251
0
}
252
253
static inline mbedtls_mpi_uint mpi_sint_abs(mbedtls_mpi_sint z)
254
1.66M
{
255
1.66M
    if (z >= 0) {
256
1.66M
        return z;
257
1.66M
    }
258
    /* Take care to handle the most negative value (-2^(biL-1)) correctly.
259
     * A naive -z would have undefined behavior.
260
     * Write this in a way that makes popular compilers happy (GCC, Clang,
261
     * MSVC). */
262
0
    return (mbedtls_mpi_uint) 0 - (mbedtls_mpi_uint) z;
263
1.66M
}
264
265
/*
266
 * Set value from integer
267
 */
268
int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z)
269
763k
{
270
763k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
271
763k
    MPI_VALIDATE_RET(X != NULL);
272
273
763k
    MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, 1));
274
763k
    memset(X->p, 0, X->n * ciL);
275
276
763k
    X->p[0] = mpi_sint_abs(z);
277
763k
    X->s    = (z < 0) ? -1 : 1;
278
279
763k
cleanup:
280
281
763k
    return ret;
282
763k
}
283
284
/*
285
 * Get a specific bit
286
 */
287
int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
288
143k
{
289
143k
    MPI_VALIDATE_RET(X != NULL);
290
291
143k
    if (X->n * biL <= pos) {
292
0
        return 0;
293
0
    }
294
295
143k
    return (X->p[pos / biL] >> (pos % biL)) & 0x01;
296
143k
}
297
298
/*
299
 * Set a bit to a specific value of 0 or 1
300
 */
301
int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val)
302
10
{
303
10
    int ret = 0;
304
10
    size_t off = pos / biL;
305
10
    size_t idx = pos % biL;
306
10
    MPI_VALIDATE_RET(X != NULL);
307
308
10
    if (val != 0 && val != 1) {
309
0
        return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
310
0
    }
311
312
10
    if (X->n * biL <= pos) {
313
0
        if (val == 0) {
314
0
            return 0;
315
0
        }
316
317
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, off + 1));
318
0
    }
319
320
10
    X->p[off] &= ~((mbedtls_mpi_uint) 0x01 << idx);
321
10
    X->p[off] |= (mbedtls_mpi_uint) val << idx;
322
323
10
cleanup:
324
325
10
    return ret;
326
10
}
327
328
/*
329
 * Return the number of less significant zero-bits
330
 */
331
size_t mbedtls_mpi_lsb(const mbedtls_mpi *X)
332
191k
{
333
191k
    size_t i, j, count = 0;
334
191k
    MBEDTLS_INTERNAL_VALIDATE_RET(X != NULL, 0);
335
336
191k
    for (i = 0; i < X->n; i++) {
337
287k
        for (j = 0; j < biL; j++, count++) {
338
287k
            if (((X->p[i] >> j) & 1) != 0) {
339
191k
                return count;
340
191k
            }
341
287k
        }
342
191k
    }
343
344
0
    return 0;
345
191k
}
346
347
/*
348
 * Return the number of bits
349
 */
350
size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
351
1.00M
{
352
1.00M
    return mbedtls_mpi_core_bitlen(X->p, X->n);
353
1.00M
}
354
355
/*
356
 * Return the total size in bytes
357
 */
358
size_t mbedtls_mpi_size(const mbedtls_mpi *X)
359
86.1k
{
360
86.1k
    return (mbedtls_mpi_bitlen(X) + 7) >> 3;
361
86.1k
}
362
363
/*
364
 * Convert an ASCII character to digit value
365
 */
366
static int mpi_get_digit(mbedtls_mpi_uint *d, int radix, char c)
367
0
{
368
0
    *d = 255;
369
370
0
    if (c >= 0x30 && c <= 0x39) {
371
0
        *d = c - 0x30;
372
0
    }
373
0
    if (c >= 0x41 && c <= 0x46) {
374
0
        *d = c - 0x37;
375
0
    }
376
0
    if (c >= 0x61 && c <= 0x66) {
377
0
        *d = c - 0x57;
378
0
    }
379
380
0
    if (*d >= (mbedtls_mpi_uint) radix) {
381
0
        return MBEDTLS_ERR_MPI_INVALID_CHARACTER;
382
0
    }
383
384
0
    return 0;
385
0
}
386
387
/*
388
 * Import from an ASCII string
389
 */
390
int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s)
391
0
{
392
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
393
0
    size_t i, j, slen, n;
394
0
    int sign = 1;
395
0
    mbedtls_mpi_uint d;
396
0
    mbedtls_mpi T;
397
0
    MPI_VALIDATE_RET(X != NULL);
398
0
    MPI_VALIDATE_RET(s != NULL);
399
400
0
    if (radix < 2 || radix > 16) {
401
0
        return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
402
0
    }
403
404
0
    mbedtls_mpi_init(&T);
405
406
0
    if (s[0] == 0) {
407
0
        mbedtls_mpi_free(X);
408
0
        return 0;
409
0
    }
410
411
0
    if (s[0] == '-') {
412
0
        ++s;
413
0
        sign = -1;
414
0
    }
415
416
0
    slen = strlen(s);
417
418
0
    if (radix == 16) {
419
0
        if (slen > MPI_SIZE_T_MAX >> 2) {
420
0
            return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
421
0
        }
422
423
0
        n = BITS_TO_LIMBS(slen << 2);
424
425
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n));
426
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
427
428
0
        for (i = slen, j = 0; i > 0; i--, j++) {
429
0
            MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i - 1]));
430
0
            X->p[j / (2 * ciL)] |= d << ((j % (2 * ciL)) << 2);
431
0
        }
432
0
    } else {
433
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
434
435
0
        for (i = 0; i < slen; i++) {
436
0
            MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i]));
437
0
            MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T, X, radix));
438
0
            MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, &T, d));
439
0
        }
440
0
    }
441
442
0
    if (sign < 0 && mbedtls_mpi_bitlen(X) != 0) {
443
0
        X->s = -1;
444
0
    }
445
446
0
cleanup:
447
448
0
    mbedtls_mpi_free(&T);
449
450
0
    return ret;
451
0
}
452
453
/*
454
 * Helper to write the digits high-order first.
455
 */
456
static int mpi_write_hlp(mbedtls_mpi *X, int radix,
457
                         char **p, const size_t buflen)
458
0
{
459
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
460
0
    mbedtls_mpi_uint r;
461
0
    size_t length = 0;
462
0
    char *p_end = *p + buflen;
463
464
0
    do {
465
0
        if (length >= buflen) {
466
0
            return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
467
0
        }
468
469
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, radix));
470
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_div_int(X, NULL, X, radix));
471
        /*
472
         * Write the residue in the current position, as an ASCII character.
473
         */
474
0
        if (r < 0xA) {
475
0
            *(--p_end) = (char) ('0' + r);
476
0
        } else {
477
0
            *(--p_end) = (char) ('A' + (r - 0xA));
478
0
        }
479
480
0
        length++;
481
0
    } while (mbedtls_mpi_cmp_int(X, 0) != 0);
482
483
0
    memmove(*p, p_end, length);
484
0
    *p += length;
485
486
0
cleanup:
487
488
0
    return ret;
489
0
}
490
491
/*
492
 * Export into an ASCII string
493
 */
494
int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
495
                             char *buf, size_t buflen, size_t *olen)
496
0
{
497
0
    int ret = 0;
498
0
    size_t n;
499
0
    char *p;
500
0
    mbedtls_mpi T;
501
0
    MPI_VALIDATE_RET(X    != NULL);
502
0
    MPI_VALIDATE_RET(olen != NULL);
503
0
    MPI_VALIDATE_RET(buflen == 0 || buf != NULL);
504
505
0
    if (radix < 2 || radix > 16) {
506
0
        return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
507
0
    }
508
509
0
    n = mbedtls_mpi_bitlen(X);   /* Number of bits necessary to present `n`. */
510
0
    if (radix >=  4) {
511
0
        n >>= 1;                 /* Number of 4-adic digits necessary to present
512
                                  * `n`. If radix > 4, this might be a strict
513
                                  * overapproximation of the number of
514
                                  * radix-adic digits needed to present `n`. */
515
0
    }
516
0
    if (radix >= 16) {
517
0
        n >>= 1;                 /* Number of hexadecimal digits necessary to
518
                                  * present `n`. */
519
520
0
    }
521
0
    n += 1; /* Terminating null byte */
522
0
    n += 1; /* Compensate for the divisions above, which round down `n`
523
             * in case it's not even. */
524
0
    n += 1; /* Potential '-'-sign. */
525
0
    n += (n & 1);   /* Make n even to have enough space for hexadecimal writing,
526
                     * which always uses an even number of hex-digits. */
527
528
0
    if (buflen < n) {
529
0
        *olen = n;
530
0
        return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
531
0
    }
532
533
0
    p = buf;
534
0
    mbedtls_mpi_init(&T);
535
536
0
    if (X->s == -1) {
537
0
        *p++ = '-';
538
0
        buflen--;
539
0
    }
540
541
0
    if (radix == 16) {
542
0
        int c;
543
0
        size_t i, j, k;
544
545
0
        for (i = X->n, k = 0; i > 0; i--) {
546
0
            for (j = ciL; j > 0; j--) {
547
0
                c = (X->p[i - 1] >> ((j - 1) << 3)) & 0xFF;
548
549
0
                if (c == 0 && k == 0 && (i + j) != 2) {
550
0
                    continue;
551
0
                }
552
553
0
                *(p++) = "0123456789ABCDEF" [c / 16];
554
0
                *(p++) = "0123456789ABCDEF" [c % 16];
555
0
                k = 1;
556
0
            }
557
0
        }
558
0
    } else {
559
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T, X));
560
561
0
        if (T.s == -1) {
562
0
            T.s = 1;
563
0
        }
564
565
0
        MBEDTLS_MPI_CHK(mpi_write_hlp(&T, radix, &p, buflen));
566
0
    }
567
568
0
    *p++ = '\0';
569
0
    *olen = p - buf;
570
571
0
cleanup:
572
573
0
    mbedtls_mpi_free(&T);
574
575
0
    return ret;
576
0
}
577
578
#if defined(MBEDTLS_FS_IO)
579
/*
580
 * Read X from an opened file
581
 */
582
int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin)
583
0
{
584
0
    mbedtls_mpi_uint d;
585
0
    size_t slen;
586
0
    char *p;
587
    /*
588
     * Buffer should have space for (short) label and decimal formatted MPI,
589
     * newline characters and '\0'
590
     */
591
0
    char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
592
593
0
    MPI_VALIDATE_RET(X   != NULL);
594
0
    MPI_VALIDATE_RET(fin != NULL);
595
596
0
    if (radix < 2 || radix > 16) {
597
0
        return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
598
0
    }
599
600
0
    memset(s, 0, sizeof(s));
601
0
    if (fgets(s, sizeof(s) - 1, fin) == NULL) {
602
0
        return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
603
0
    }
604
605
0
    slen = strlen(s);
606
0
    if (slen == sizeof(s) - 2) {
607
0
        return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
608
0
    }
609
610
0
    if (slen > 0 && s[slen - 1] == '\n') {
611
0
        slen--; s[slen] = '\0';
612
0
    }
613
0
    if (slen > 0 && s[slen - 1] == '\r') {
614
0
        slen--; s[slen] = '\0';
615
0
    }
616
617
0
    p = s + slen;
618
0
    while (p-- > s) {
619
0
        if (mpi_get_digit(&d, radix, *p) != 0) {
620
0
            break;
621
0
        }
622
0
    }
623
624
0
    return mbedtls_mpi_read_string(X, radix, p + 1);
625
0
}
626
627
/*
628
 * Write X into an opened file (or stdout if fout == NULL)
629
 */
630
int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE *fout)
631
0
{
632
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
633
0
    size_t n, slen, plen;
634
    /*
635
     * Buffer should have space for (short) label and decimal formatted MPI,
636
     * newline characters and '\0'
637
     */
638
0
    char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
639
0
    MPI_VALIDATE_RET(X != NULL);
640
641
0
    if (radix < 2 || radix > 16) {
642
0
        return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
643
0
    }
644
645
0
    memset(s, 0, sizeof(s));
646
647
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_write_string(X, radix, s, sizeof(s) - 2, &n));
648
649
0
    if (p == NULL) {
650
0
        p = "";
651
0
    }
652
653
0
    plen = strlen(p);
654
0
    slen = strlen(s);
655
0
    s[slen++] = '\r';
656
0
    s[slen++] = '\n';
657
658
0
    if (fout != NULL) {
659
0
        if (fwrite(p, 1, plen, fout) != plen ||
660
0
            fwrite(s, 1, slen, fout) != slen) {
661
0
            return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
662
0
        }
663
0
    } else {
664
0
        mbedtls_printf("%s%s", p, s);
665
0
    }
666
667
0
cleanup:
668
669
0
    return ret;
670
0
}
671
#endif /* MBEDTLS_FS_IO */
672
673
/*
674
 * Import X from unsigned binary data, little endian
675
 *
676
 * This function is guaranteed to return an MPI with exactly the necessary
677
 * number of limbs (in particular, it does not skip 0s in the input).
678
 */
679
int mbedtls_mpi_read_binary_le(mbedtls_mpi *X,
680
                               const unsigned char *buf, size_t buflen)
681
11
{
682
11
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
683
11
    const size_t limbs = CHARS_TO_LIMBS(buflen);
684
685
    /* Ensure that target MPI has exactly the necessary number of limbs */
686
11
    MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
687
688
11
    MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_le(X->p, X->n, buf, buflen));
689
690
11
cleanup:
691
692
    /*
693
     * This function is also used to import keys. However, wiping the buffers
694
     * upon failure is not necessary because failure only can happen before any
695
     * input is copied.
696
     */
697
11
    return ret;
698
11
}
699
700
/*
701
 * Import X from unsigned binary data, big endian
702
 *
703
 * This function is guaranteed to return an MPI with exactly the necessary
704
 * number of limbs (in particular, it does not skip 0s in the input).
705
 */
706
int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
707
72.6k
{
708
72.6k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
709
72.6k
    const size_t limbs = CHARS_TO_LIMBS(buflen);
710
711
72.6k
    MPI_VALIDATE_RET(X != NULL);
712
72.6k
    MPI_VALIDATE_RET(buflen == 0 || buf != NULL);
713
714
    /* Ensure that target MPI has exactly the necessary number of limbs */
715
72.6k
    MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
716
717
72.6k
    MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_be(X->p, X->n, buf, buflen));
718
719
72.6k
cleanup:
720
721
    /*
722
     * This function is also used to import keys. However, wiping the buffers
723
     * upon failure is not necessary because failure only can happen before any
724
     * input is copied.
725
     */
726
72.6k
    return ret;
727
72.6k
}
728
729
/*
730
 * Export X into unsigned binary data, little endian
731
 */
732
int mbedtls_mpi_write_binary_le(const mbedtls_mpi *X,
733
                                unsigned char *buf, size_t buflen)
734
0
{
735
0
    return mbedtls_mpi_core_write_le(X->p, X->n, buf, buflen);
736
0
}
737
738
/*
739
 * Export X into unsigned binary data, big endian
740
 */
741
int mbedtls_mpi_write_binary(const mbedtls_mpi *X,
742
                             unsigned char *buf, size_t buflen)
743
3.89k
{
744
3.89k
    return mbedtls_mpi_core_write_be(X->p, X->n, buf, buflen);
745
3.89k
}
746
747
/*
748
 * Left-shift: X <<= count
749
 */
750
int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
751
326k
{
752
326k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
753
326k
    size_t i, v0, t1;
754
326k
    mbedtls_mpi_uint r0 = 0, r1;
755
326k
    MPI_VALIDATE_RET(X != NULL);
756
757
326k
    v0 = count / (biL);
758
326k
    t1 = count & (biL - 1);
759
760
326k
    i = mbedtls_mpi_bitlen(X) + count;
761
762
326k
    if (X->n * biL < i) {
763
8.55k
        MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, BITS_TO_LIMBS(i)));
764
8.55k
    }
765
766
326k
    ret = 0;
767
768
    /*
769
     * shift by count / limb_size
770
     */
771
326k
    if (v0 > 0) {
772
6.95M
        for (i = X->n; i > v0; i--) {
773
6.81M
            X->p[i - 1] = X->p[i - v0 - 1];
774
6.81M
        }
775
776
3.15M
        for (; i > 0; i--) {
777
3.01M
            X->p[i - 1] = 0;
778
3.01M
        }
779
142k
    }
780
781
    /*
782
     * shift by count % limb_size
783
     */
784
326k
    if (t1 > 0) {
785
2.76M
        for (i = v0; i < X->n; i++) {
786
2.58M
            r1 = X->p[i] >> (biL - t1);
787
2.58M
            X->p[i] <<= t1;
788
2.58M
            X->p[i] |= r0;
789
2.58M
            r0 = r1;
790
2.58M
        }
791
179k
    }
792
793
326k
cleanup:
794
795
326k
    return ret;
796
326k
}
797
798
/*
799
 * Right-shift: X >>= count
800
 */
801
int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
802
865k
{
803
865k
    MPI_VALIDATE_RET(X != NULL);
804
865k
    if (X->n != 0) {
805
865k
        mbedtls_mpi_core_shift_r(X->p, X->n, count);
806
865k
    }
807
865k
    return 0;
808
865k
}
809
810
/*
811
 * Compare unsigned values
812
 */
813
int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
814
905k
{
815
905k
    size_t i, j;
816
905k
    MPI_VALIDATE_RET(X != NULL);
817
905k
    MPI_VALIDATE_RET(Y != NULL);
818
819
7.02M
    for (i = X->n; i > 0; i--) {
820
7.02M
        if (X->p[i - 1] != 0) {
821
905k
            break;
822
905k
        }
823
7.02M
    }
824
825
6.92M
    for (j = Y->n; j > 0; j--) {
826
6.91M
        if (Y->p[j - 1] != 0) {
827
890k
            break;
828
890k
        }
829
6.91M
    }
830
831
905k
    if (i == 0 && j == 0) {
832
0
        return 0;
833
0
    }
834
835
905k
    if (i > j) {
836
31.2k
        return 1;
837
31.2k
    }
838
874k
    if (j > i) {
839
3.42k
        return -1;
840
3.42k
    }
841
842
988k
    for (; i > 0; i--) {
843
987k
        if (X->p[i - 1] > Y->p[i - 1]) {
844
434k
            return 1;
845
434k
        }
846
553k
        if (X->p[i - 1] < Y->p[i - 1]) {
847
435k
            return -1;
848
435k
        }
849
553k
    }
850
851
888
    return 0;
852
871k
}
853
854
/*
855
 * Compare signed values
856
 */
857
int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
858
2.65M
{
859
2.65M
    size_t i, j;
860
2.65M
    MPI_VALIDATE_RET(X != NULL);
861
2.65M
    MPI_VALIDATE_RET(Y != NULL);
862
863
29.7M
    for (i = X->n; i > 0; i--) {
864
29.5M
        if (X->p[i - 1] != 0) {
865
2.49M
            break;
866
2.49M
        }
867
29.5M
    }
868
869
4.17M
    for (j = Y->n; j > 0; j--) {
870
3.29M
        if (Y->p[j - 1] != 0) {
871
1.77M
            break;
872
1.77M
        }
873
3.29M
    }
874
875
2.65M
    if (i == 0 && j == 0) {
876
154k
        return 0;
877
154k
    }
878
879
2.50M
    if (i > j) {
880
1.29M
        return X->s;
881
1.29M
    }
882
1.21M
    if (j > i) {
883
35.3k
        return -Y->s;
884
35.3k
    }
885
886
1.17M
    if (X->s > 0 && Y->s < 0) {
887
21
        return 1;
888
21
    }
889
1.17M
    if (Y->s > 0 && X->s < 0) {
890
0
        return -1;
891
0
    }
892
893
1.37M
    for (; i > 0; i--) {
894
1.34M
        if (X->p[i - 1] > Y->p[i - 1]) {
895
152k
            return X->s;
896
152k
        }
897
1.19M
        if (X->p[i - 1] < Y->p[i - 1]) {
898
997k
            return -X->s;
899
997k
        }
900
1.19M
    }
901
902
28.2k
    return 0;
903
1.17M
}
904
905
/*
906
 * Compare signed values
907
 */
908
int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
909
895k
{
910
895k
    mbedtls_mpi Y;
911
895k
    mbedtls_mpi_uint p[1];
912
895k
    MPI_VALIDATE_RET(X != NULL);
913
914
895k
    *p  = mpi_sint_abs(z);
915
895k
    Y.s = (z < 0) ? -1 : 1;
916
895k
    Y.n = 1;
917
895k
    Y.p = p;
918
919
895k
    return mbedtls_mpi_cmp_mpi(X, &Y);
920
895k
}
921
922
/*
923
 * Unsigned addition: X = |A| + |B|  (HAC 14.7)
924
 */
925
int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
926
219k
{
927
219k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
928
219k
    size_t j;
929
219k
    MPI_VALIDATE_RET(X != NULL);
930
219k
    MPI_VALIDATE_RET(A != NULL);
931
219k
    MPI_VALIDATE_RET(B != NULL);
932
933
219k
    if (X == B) {
934
0
        const mbedtls_mpi *T = A; A = X; B = T;
935
0
    }
936
937
219k
    if (X != A) {
938
39.4k
        MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
939
39.4k
    }
940
941
    /*
942
     * X must always be positive as a result of unsigned additions.
943
     */
944
219k
    X->s = 1;
945
946
533k
    for (j = B->n; j > 0; j--) {
947
533k
        if (B->p[j - 1] != 0) {
948
219k
            break;
949
219k
        }
950
533k
    }
951
952
    /* Exit early to avoid undefined behavior on NULL+0 when X->n == 0
953
     * and B is 0 (of any size). */
954
219k
    if (j == 0) {
955
88
        return 0;
956
88
    }
957
958
219k
    MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j));
959
960
    /* j is the number of non-zero limbs of B. Add those to X. */
961
962
219k
    mbedtls_mpi_uint *p = X->p;
963
964
219k
    mbedtls_mpi_uint c = mbedtls_mpi_core_add(p, p, B->p, j);
965
966
219k
    p += j;
967
968
    /* Now propagate any carry */
969
970
317k
    while (c != 0) {
971
98.0k
        if (j >= X->n) {
972
616
            MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j + 1));
973
616
            p = X->p + j;
974
616
        }
975
976
98.0k
        *p += c; c = (*p < c); j++; p++;
977
98.0k
    }
978
979
219k
cleanup:
980
981
219k
    return ret;
982
219k
}
983
984
/*
985
 * Unsigned subtraction: X = |A| - |B|  (HAC 14.9, 14.10)
986
 */
987
int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
988
1.55M
{
989
1.55M
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
990
1.55M
    size_t n;
991
1.55M
    mbedtls_mpi_uint carry;
992
1.55M
    MPI_VALIDATE_RET(X != NULL);
993
1.55M
    MPI_VALIDATE_RET(A != NULL);
994
1.55M
    MPI_VALIDATE_RET(B != NULL);
995
996
8.85M
    for (n = B->n; n > 0; n--) {
997
8.83M
        if (B->p[n - 1] != 0) {
998
1.53M
            break;
999
1.53M
        }
1000
8.83M
    }
1001
1.55M
    if (n > A->n) {
1002
        /* B >= (2^ciL)^n > A */
1003
0
        ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1004
0
        goto cleanup;
1005
0
    }
1006
1007
1.55M
    MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, A->n));
1008
1009
    /* Set the high limbs of X to match A. Don't touch the lower limbs
1010
     * because X might be aliased to B, and we must not overwrite the
1011
     * significant digits of B. */
1012
1.55M
    if (A->n > n) {
1013
1.22M
        memcpy(X->p + n, A->p + n, (A->n - n) * ciL);
1014
1.22M
    }
1015
1.55M
    if (X->n > A->n) {
1016
214k
        memset(X->p + A->n, 0, (X->n - A->n) * ciL);
1017
214k
    }
1018
1019
1.55M
    carry = mbedtls_mpi_core_sub(X->p, A->p, B->p, n);
1020
1.55M
    if (carry != 0) {
1021
        /* Propagate the carry through the rest of X. */
1022
563k
        carry = mbedtls_mpi_core_sub_int(X->p + n, X->p + n, carry, X->n - n);
1023
1024
        /* If we have further carry/borrow, the result is negative. */
1025
563k
        if (carry != 0) {
1026
0
            ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1027
0
            goto cleanup;
1028
0
        }
1029
563k
    }
1030
1031
    /* X should always be positive as a result of unsigned subtractions. */
1032
1.55M
    X->s = 1;
1033
1034
1.55M
cleanup:
1035
1.55M
    return ret;
1036
1.55M
}
1037
1038
/* Common function for signed addition and subtraction.
1039
 * Calculate A + B * flip_B where flip_B is 1 or -1.
1040
 */
1041
static int add_sub_mpi(mbedtls_mpi *X,
1042
                       const mbedtls_mpi *A, const mbedtls_mpi *B,
1043
                       int flip_B)
1044
1.12M
{
1045
1.12M
    int ret, s;
1046
1.12M
    MPI_VALIDATE_RET(X != NULL);
1047
1.12M
    MPI_VALIDATE_RET(A != NULL);
1048
1.12M
    MPI_VALIDATE_RET(B != NULL);
1049
1050
1.12M
    s = A->s;
1051
1.12M
    if (A->s * B->s * flip_B < 0) {
1052
901k
        int cmp = mbedtls_mpi_cmp_abs(A, B);
1053
901k
        if (cmp >= 0) {
1054
463k
            MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, A, B));
1055
            /* If |A| = |B|, the result is 0 and we must set the sign bit
1056
             * to +1 regardless of which of A or B was negative. Otherwise,
1057
             * since |A| > |B|, the sign is the sign of A. */
1058
463k
            X->s = cmp == 0 ? 1 : s;
1059
463k
        } else {
1060
438k
            MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, B, A));
1061
            /* Since |A| < |B|, the sign is the opposite of A. */
1062
438k
            X->s = -s;
1063
438k
        }
1064
901k
    } else {
1065
219k
        MBEDTLS_MPI_CHK(mbedtls_mpi_add_abs(X, A, B));
1066
219k
        X->s = s;
1067
219k
    }
1068
1069
1.12M
cleanup:
1070
1071
1.12M
    return ret;
1072
1.12M
}
1073
1074
/*
1075
 * Signed addition: X = A + B
1076
 */
1077
int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
1078
297k
{
1079
297k
    return add_sub_mpi(X, A, B, 1);
1080
297k
}
1081
1082
/*
1083
 * Signed subtraction: X = A - B
1084
 */
1085
int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
1086
824k
{
1087
824k
    return add_sub_mpi(X, A, B, -1);
1088
824k
}
1089
1090
/*
1091
 * Signed addition: X = A + b
1092
 */
1093
int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
1094
0
{
1095
0
    mbedtls_mpi B;
1096
0
    mbedtls_mpi_uint p[1];
1097
0
    MPI_VALIDATE_RET(X != NULL);
1098
0
    MPI_VALIDATE_RET(A != NULL);
1099
1100
0
    p[0] = mpi_sint_abs(b);
1101
0
    B.s = (b < 0) ? -1 : 1;
1102
0
    B.n = 1;
1103
0
    B.p = p;
1104
1105
0
    return mbedtls_mpi_add_mpi(X, A, &B);
1106
0
}
1107
1108
/*
1109
 * Signed subtraction: X = A - b
1110
 */
1111
int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
1112
6.67k
{
1113
6.67k
    mbedtls_mpi B;
1114
6.67k
    mbedtls_mpi_uint p[1];
1115
6.67k
    MPI_VALIDATE_RET(X != NULL);
1116
6.67k
    MPI_VALIDATE_RET(A != NULL);
1117
1118
6.67k
    p[0] = mpi_sint_abs(b);
1119
6.67k
    B.s = (b < 0) ? -1 : 1;
1120
6.67k
    B.n = 1;
1121
6.67k
    B.p = p;
1122
1123
6.67k
    return mbedtls_mpi_sub_mpi(X, A, &B);
1124
6.67k
}
1125
1126
/*
1127
 * Baseline multiplication: X = A * B  (HAC 14.12)
1128
 */
1129
int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
1130
504k
{
1131
504k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1132
504k
    size_t i, j;
1133
504k
    mbedtls_mpi TA, TB;
1134
504k
    int result_is_zero = 0;
1135
504k
    MPI_VALIDATE_RET(X != NULL);
1136
504k
    MPI_VALIDATE_RET(A != NULL);
1137
504k
    MPI_VALIDATE_RET(B != NULL);
1138
1139
504k
    mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TB);
1140
1141
504k
    if (X == A) {
1142
139k
        MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A)); A = &TA;
1143
139k
    }
1144
504k
    if (X == B) {
1145
1.40k
        MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B)); B = &TB;
1146
1.40k
    }
1147
1148
2.10M
    for (i = A->n; i > 0; i--) {
1149
2.10M
        if (A->p[i - 1] != 0) {
1150
504k
            break;
1151
504k
        }
1152
2.10M
    }
1153
504k
    if (i == 0) {
1154
279
        result_is_zero = 1;
1155
279
    }
1156
1157
2.83M
    for (j = B->n; j > 0; j--) {
1158
2.83M
        if (B->p[j - 1] != 0) {
1159
504k
            break;
1160
504k
        }
1161
2.83M
    }
1162
504k
    if (j == 0) {
1163
421
        result_is_zero = 1;
1164
421
    }
1165
1166
504k
    MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i + j));
1167
504k
    MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
1168
1169
3.52M
    for (size_t k = 0; k < j; k++) {
1170
        /* We know that there cannot be any carry-out since we're
1171
         * iterating from bottom to top. */
1172
3.01M
        (void) mbedtls_mpi_core_mla(X->p + k, i + 1,
1173
3.01M
                                    A->p, i,
1174
3.01M
                                    B->p[k]);
1175
3.01M
    }
1176
1177
    /* If the result is 0, we don't shortcut the operation, which reduces
1178
     * but does not eliminate side channels leaking the zero-ness. We do
1179
     * need to take care to set the sign bit properly since the library does
1180
     * not fully support an MPI object with a value of 0 and s == -1. */
1181
504k
    if (result_is_zero) {
1182
421
        X->s = 1;
1183
504k
    } else {
1184
504k
        X->s = A->s * B->s;
1185
504k
    }
1186
1187
504k
cleanup:
1188
1189
504k
    mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TA);
1190
1191
504k
    return ret;
1192
504k
}
1193
1194
/*
1195
 * Baseline multiplication: X = A * b
1196
 */
1197
int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b)
1198
370k
{
1199
370k
    MPI_VALIDATE_RET(X != NULL);
1200
370k
    MPI_VALIDATE_RET(A != NULL);
1201
1202
370k
    size_t n = A->n;
1203
18.9M
    while (n > 0 && A->p[n - 1] == 0) {
1204
18.6M
        --n;
1205
18.6M
    }
1206
1207
    /* The general method below doesn't work if b==0. */
1208
370k
    if (b == 0 || n == 0) {
1209
30.3k
        return mbedtls_mpi_lset(X, 0);
1210
30.3k
    }
1211
1212
    /* Calculate A*b as A + A*(b-1) to take advantage of mbedtls_mpi_core_mla */
1213
340k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1214
    /* In general, A * b requires 1 limb more than b. If
1215
     * A->p[n - 1] * b / b == A->p[n - 1], then A * b fits in the same
1216
     * number of limbs as A and the call to grow() is not required since
1217
     * copy() will take care of the growth if needed. However, experimentally,
1218
     * making the call to grow() unconditional causes slightly fewer
1219
     * calls to calloc() in ECP code, presumably because it reuses the
1220
     * same mpi for a while and this way the mpi is more likely to directly
1221
     * grow to its final size.
1222
     *
1223
     * Note that calculating A*b as 0 + A*b doesn't work as-is because
1224
     * A,X can be the same. */
1225
340k
    MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n + 1));
1226
340k
    MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
1227
340k
    mbedtls_mpi_core_mla(X->p, X->n, A->p, n, b - 1);
1228
1229
340k
cleanup:
1230
340k
    return ret;
1231
340k
}
1232
1233
/*
1234
 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1235
 * mbedtls_mpi_uint divisor, d
1236
 */
1237
static mbedtls_mpi_uint mbedtls_int_div_int(mbedtls_mpi_uint u1,
1238
                                            mbedtls_mpi_uint u0,
1239
                                            mbedtls_mpi_uint d,
1240
                                            mbedtls_mpi_uint *r)
1241
132k
{
1242
132k
#if defined(MBEDTLS_HAVE_UDBL)
1243
132k
    mbedtls_t_udbl dividend, quotient;
1244
#else
1245
    const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
1246
    const mbedtls_mpi_uint uint_halfword_mask = ((mbedtls_mpi_uint) 1 << biH) - 1;
1247
    mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1248
    mbedtls_mpi_uint u0_msw, u0_lsw;
1249
    size_t s;
1250
#endif
1251
1252
    /*
1253
     * Check for overflow
1254
     */
1255
132k
    if (0 == d || u1 >= d) {
1256
0
        if (r != NULL) {
1257
0
            *r = ~(mbedtls_mpi_uint) 0u;
1258
0
        }
1259
1260
0
        return ~(mbedtls_mpi_uint) 0u;
1261
0
    }
1262
1263
132k
#if defined(MBEDTLS_HAVE_UDBL)
1264
132k
    dividend  = (mbedtls_t_udbl) u1 << biL;
1265
132k
    dividend |= (mbedtls_t_udbl) u0;
1266
132k
    quotient = dividend / d;
1267
132k
    if (quotient > ((mbedtls_t_udbl) 1 << biL) - 1) {
1268
0
        quotient = ((mbedtls_t_udbl) 1 << biL) - 1;
1269
0
    }
1270
1271
132k
    if (r != NULL) {
1272
0
        *r = (mbedtls_mpi_uint) (dividend - (quotient * d));
1273
0
    }
1274
1275
132k
    return (mbedtls_mpi_uint) quotient;
1276
#else
1277
1278
    /*
1279
     * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1280
     *   Vol. 2 - Seminumerical Algorithms, Knuth
1281
     */
1282
1283
    /*
1284
     * Normalize the divisor, d, and dividend, u0, u1
1285
     */
1286
    s = mbedtls_mpi_core_clz(d);
1287
    d = d << s;
1288
1289
    u1 = u1 << s;
1290
    u1 |= (u0 >> (biL - s)) & (-(mbedtls_mpi_sint) s >> (biL - 1));
1291
    u0 =  u0 << s;
1292
1293
    d1 = d >> biH;
1294
    d0 = d & uint_halfword_mask;
1295
1296
    u0_msw = u0 >> biH;
1297
    u0_lsw = u0 & uint_halfword_mask;
1298
1299
    /*
1300
     * Find the first quotient and remainder
1301
     */
1302
    q1 = u1 / d1;
1303
    r0 = u1 - d1 * q1;
1304
1305
    while (q1 >= radix || (q1 * d0 > radix * r0 + u0_msw)) {
1306
        q1 -= 1;
1307
        r0 += d1;
1308
1309
        if (r0 >= radix) {
1310
            break;
1311
        }
1312
    }
1313
1314
    rAX = (u1 * radix) + (u0_msw - q1 * d);
1315
    q0 = rAX / d1;
1316
    r0 = rAX - q0 * d1;
1317
1318
    while (q0 >= radix || (q0 * d0 > radix * r0 + u0_lsw)) {
1319
        q0 -= 1;
1320
        r0 += d1;
1321
1322
        if (r0 >= radix) {
1323
            break;
1324
        }
1325
    }
1326
1327
    if (r != NULL) {
1328
        *r = (rAX * radix + u0_lsw - q0 * d) >> s;
1329
    }
1330
1331
    quotient = q1 * radix + q0;
1332
1333
    return quotient;
1334
#endif
1335
132k
}
1336
1337
/*
1338
 * Division by mbedtls_mpi: A = Q * B + R  (HAC 14.20)
1339
 */
1340
int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1341
                        const mbedtls_mpi *B)
1342
3.98k
{
1343
3.98k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1344
3.98k
    size_t i, n, t, k;
1345
3.98k
    mbedtls_mpi X, Y, Z, T1, T2;
1346
3.98k
    mbedtls_mpi_uint TP2[3];
1347
3.98k
    MPI_VALIDATE_RET(A != NULL);
1348
3.98k
    MPI_VALIDATE_RET(B != NULL);
1349
1350
3.98k
    if (mbedtls_mpi_cmp_int(B, 0) == 0) {
1351
0
        return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
1352
0
    }
1353
1354
3.98k
    mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z);
1355
3.98k
    mbedtls_mpi_init(&T1);
1356
    /*
1357
     * Avoid dynamic memory allocations for constant-size T2.
1358
     *
1359
     * T2 is used for comparison only and the 3 limbs are assigned explicitly,
1360
     * so nobody increase the size of the MPI and we're safe to use an on-stack
1361
     * buffer.
1362
     */
1363
3.98k
    T2.s = 1;
1364
3.98k
    T2.n = sizeof(TP2) / sizeof(*TP2);
1365
3.98k
    T2.p = TP2;
1366
1367
3.98k
    if (mbedtls_mpi_cmp_abs(A, B) < 0) {
1368
352
        if (Q != NULL) {
1369
0
            MBEDTLS_MPI_CHK(mbedtls_mpi_lset(Q, 0));
1370
0
        }
1371
352
        if (R != NULL) {
1372
352
            MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, A));
1373
352
        }
1374
352
        return 0;
1375
352
    }
1376
1377
3.62k
    MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&X, A));
1378
3.62k
    MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, B));
1379
3.62k
    X.s = Y.s = 1;
1380
1381
3.62k
    MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&Z, A->n + 2));
1382
3.62k
    MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Z,  0));
1383
3.62k
    MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T1, A->n + 2));
1384
1385
3.62k
    k = mbedtls_mpi_bitlen(&Y) % biL;
1386
3.62k
    if (k < biL - 1) {
1387
3.53k
        k = biL - 1 - k;
1388
3.53k
        MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&X, k));
1389
3.53k
        MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, k));
1390
3.53k
    } else {
1391
93
        k = 0;
1392
93
    }
1393
1394
3.62k
    n = X.n - 1;
1395
3.62k
    t = Y.n - 1;
1396
3.62k
    MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, biL * (n - t)));
1397
1398
4.92k
    while (mbedtls_mpi_cmp_mpi(&X, &Y) >= 0) {
1399
1.29k
        Z.p[n - t]++;
1400
1.29k
        MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &Y));
1401
1.29k
    }
1402
3.62k
    MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, biL * (n - t)));
1403
1404
140k
    for (i = n; i > t; i--) {
1405
137k
        if (X.p[i] >= Y.p[t]) {
1406
4.72k
            Z.p[i - t - 1] = ~(mbedtls_mpi_uint) 0u;
1407
132k
        } else {
1408
132k
            Z.p[i - t - 1] = mbedtls_int_div_int(X.p[i], X.p[i - 1],
1409
132k
                                                 Y.p[t], NULL);
1410
132k
        }
1411
1412
137k
        T2.p[0] = (i < 2) ? 0 : X.p[i - 2];
1413
137k
        T2.p[1] = (i < 1) ? 0 : X.p[i - 1];
1414
137k
        T2.p[2] = X.p[i];
1415
1416
137k
        Z.p[i - t - 1]++;
1417
194k
        do {
1418
194k
            Z.p[i - t - 1]--;
1419
1420
194k
            MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&T1, 0));
1421
194k
            T1.p[0] = (t < 1) ? 0 : Y.p[t - 1];
1422
194k
            T1.p[1] = Y.p[t];
1423
194k
            MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &T1, Z.p[i - t - 1]));
1424
194k
        } while (mbedtls_mpi_cmp_mpi(&T1, &T2) > 0);
1425
1426
137k
        MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &Y, Z.p[i - t - 1]));
1427
137k
        MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1,  biL * (i - t - 1)));
1428
137k
        MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &T1));
1429
1430
137k
        if (mbedtls_mpi_cmp_int(&X, 0) < 0) {
1431
1.78k
            MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T1, &Y));
1432
1.78k
            MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
1433
1.78k
            MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&X, &X, &T1));
1434
1.78k
            Z.p[i - t - 1]--;
1435
1.78k
        }
1436
137k
    }
1437
1438
3.62k
    if (Q != NULL) {
1439
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_copy(Q, &Z));
1440
0
        Q->s = A->s * B->s;
1441
0
    }
1442
1443
3.62k
    if (R != NULL) {
1444
3.62k
        MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&X, k));
1445
3.62k
        X.s = A->s;
1446
3.62k
        MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, &X));
1447
1448
3.62k
        if (mbedtls_mpi_cmp_int(R, 0) == 0) {
1449
0
            R->s = 1;
1450
0
        }
1451
3.62k
    }
1452
1453
3.62k
cleanup:
1454
1455
3.62k
    mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z);
1456
3.62k
    mbedtls_mpi_free(&T1);
1457
3.62k
    mbedtls_platform_zeroize(TP2, sizeof(TP2));
1458
1459
3.62k
    return ret;
1460
3.62k
}
1461
1462
/*
1463
 * Division by int: A = Q * b + R
1464
 */
1465
int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R,
1466
                        const mbedtls_mpi *A,
1467
                        mbedtls_mpi_sint b)
1468
0
{
1469
0
    mbedtls_mpi B;
1470
0
    mbedtls_mpi_uint p[1];
1471
0
    MPI_VALIDATE_RET(A != NULL);
1472
1473
0
    p[0] = mpi_sint_abs(b);
1474
0
    B.s = (b < 0) ? -1 : 1;
1475
0
    B.n = 1;
1476
0
    B.p = p;
1477
1478
0
    return mbedtls_mpi_div_mpi(Q, R, A, &B);
1479
0
}
1480
1481
/*
1482
 * Modulo: R = A mod B
1483
 */
1484
int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
1485
3.98k
{
1486
3.98k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1487
3.98k
    MPI_VALIDATE_RET(R != NULL);
1488
3.98k
    MPI_VALIDATE_RET(A != NULL);
1489
3.98k
    MPI_VALIDATE_RET(B != NULL);
1490
1491
3.98k
    if (mbedtls_mpi_cmp_int(B, 0) < 0) {
1492
0
        return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1493
0
    }
1494
1495
3.98k
    MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(NULL, R, A, B));
1496
1497
3.98k
    while (mbedtls_mpi_cmp_int(R, 0) < 0) {
1498
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(R, R, B));
1499
0
    }
1500
1501
3.98k
    while (mbedtls_mpi_cmp_mpi(R, B) >= 0) {
1502
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(R, R, B));
1503
0
    }
1504
1505
3.98k
cleanup:
1506
1507
3.98k
    return ret;
1508
3.98k
}
1509
1510
/*
1511
 * Modulo: r = A mod b
1512
 */
1513
int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b)
1514
0
{
1515
0
    size_t i;
1516
0
    mbedtls_mpi_uint x, y, z;
1517
0
    MPI_VALIDATE_RET(r != NULL);
1518
0
    MPI_VALIDATE_RET(A != NULL);
1519
1520
0
    if (b == 0) {
1521
0
        return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
1522
0
    }
1523
1524
0
    if (b < 0) {
1525
0
        return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1526
0
    }
1527
1528
    /*
1529
     * handle trivial cases
1530
     */
1531
0
    if (b == 1 || A->n == 0) {
1532
0
        *r = 0;
1533
0
        return 0;
1534
0
    }
1535
1536
0
    if (b == 2) {
1537
0
        *r = A->p[0] & 1;
1538
0
        return 0;
1539
0
    }
1540
1541
    /*
1542
     * general case
1543
     */
1544
0
    for (i = A->n, y = 0; i > 0; i--) {
1545
0
        x  = A->p[i - 1];
1546
0
        y  = (y << biH) | (x >> biH);
1547
0
        z  = y / b;
1548
0
        y -= z * b;
1549
1550
0
        x <<= biH;
1551
0
        y  = (y << biH) | (x >> biH);
1552
0
        z  = y / b;
1553
0
        y -= z * b;
1554
0
    }
1555
1556
    /*
1557
     * If A is negative, then the current y represents a negative value.
1558
     * Flipping it to the positive side.
1559
     */
1560
0
    if (A->s < 0 && y != 0) {
1561
0
        y = b - y;
1562
0
    }
1563
1564
0
    *r = y;
1565
1566
0
    return 0;
1567
0
}
1568
1569
static void mpi_montg_init(mbedtls_mpi_uint *mm, const mbedtls_mpi *N)
1570
3.62k
{
1571
3.62k
    *mm = mbedtls_mpi_core_montmul_init(N->p);
1572
3.62k
}
1573
1574
/** Montgomery multiplication: A = A * B * R^-1 mod N  (HAC 14.36)
1575
 *
1576
 * \param[in,out]   A   One of the numbers to multiply.
1577
 *                      It must have at least as many limbs as N
1578
 *                      (A->n >= N->n), and any limbs beyond n are ignored.
1579
 *                      On successful completion, A contains the result of
1580
 *                      the multiplication A * B * R^-1 mod N where
1581
 *                      R = (2^ciL)^n.
1582
 * \param[in]       B   One of the numbers to multiply.
1583
 *                      It must be nonzero and must not have more limbs than N
1584
 *                      (B->n <= N->n).
1585
 * \param[in]       N   The modulus. \p N must be odd.
1586
 * \param           mm  The value calculated by `mpi_montg_init(&mm, N)`.
1587
 *                      This is -N^-1 mod 2^ciL.
1588
 * \param[in,out]   T   A bignum for temporary storage.
1589
 *                      It must be at least twice the limb size of N plus 1
1590
 *                      (T->n >= 2 * N->n + 1).
1591
 *                      Its initial content is unused and
1592
 *                      its final content is indeterminate.
1593
 *                      It does not get reallocated.
1594
 */
1595
static void mpi_montmul(mbedtls_mpi *A, const mbedtls_mpi *B,
1596
                        const mbedtls_mpi *N, mbedtls_mpi_uint mm,
1597
                        mbedtls_mpi *T)
1598
93.8k
{
1599
93.8k
    mbedtls_mpi_core_montmul(A->p, A->p, B->p, B->n, N->p, N->n, mm, T->p);
1600
93.8k
}
1601
1602
/*
1603
 * Montgomery reduction: A = A * R^-1 mod N
1604
 *
1605
 * See mpi_montmul() regarding constraints and guarantees on the parameters.
1606
 */
1607
static void mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N,
1608
                        mbedtls_mpi_uint mm, mbedtls_mpi *T)
1609
7.25k
{
1610
7.25k
    mbedtls_mpi_uint z = 1;
1611
7.25k
    mbedtls_mpi U;
1612
1613
7.25k
    U.n = U.s = (int) z;
1614
7.25k
    U.p = &z;
1615
1616
7.25k
    mpi_montmul(A, &U, N, mm, T);
1617
7.25k
}
1618
1619
/**
1620
 * Select an MPI from a table without leaking the index.
1621
 *
1622
 * This is functionally equivalent to mbedtls_mpi_copy(R, T[idx]) except it
1623
 * reads the entire table in order to avoid leaking the value of idx to an
1624
 * attacker able to observe memory access patterns.
1625
 *
1626
 * \param[out] R        Where to write the selected MPI.
1627
 * \param[in] T         The table to read from.
1628
 * \param[in] T_size    The number of elements in the table.
1629
 * \param[in] idx       The index of the element to select;
1630
 *                      this must satisfy 0 <= idx < T_size.
1631
 *
1632
 * \return \c 0 on success, or a negative error code.
1633
 */
1634
static int mpi_select(mbedtls_mpi *R, const mbedtls_mpi *T, size_t T_size, size_t idx)
1635
81.1k
{
1636
81.1k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1637
1638
311k
    for (size_t i = 0; i < T_size; i++) {
1639
230k
        MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(R, &T[i],
1640
230k
                                                     (unsigned char) mbedtls_ct_size_bool_eq(i,
1641
230k
                                                                                             idx)));
1642
230k
    }
1643
1644
81.1k
cleanup:
1645
81.1k
    return ret;
1646
81.1k
}
1647
1648
/*
1649
 * Sliding-window exponentiation: X = A^E mod N  (HAC 14.85)
1650
 */
1651
int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
1652
                        const mbedtls_mpi *E, const mbedtls_mpi *N,
1653
                        mbedtls_mpi *prec_RR)
1654
3.62k
{
1655
3.62k
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1656
3.62k
    size_t window_bitsize;
1657
3.62k
    size_t i, j, nblimbs;
1658
3.62k
    size_t bufsize, nbits;
1659
3.62k
    mbedtls_mpi_uint ei, mm, state;
1660
3.62k
    mbedtls_mpi RR, T, W[(size_t) 1 << MBEDTLS_MPI_WINDOW_SIZE], WW, Apos;
1661
3.62k
    int neg;
1662
1663
3.62k
    MPI_VALIDATE_RET(X != NULL);
1664
3.62k
    MPI_VALIDATE_RET(A != NULL);
1665
3.62k
    MPI_VALIDATE_RET(E != NULL);
1666
3.62k
    MPI_VALIDATE_RET(N != NULL);
1667
1668
3.62k
    if (mbedtls_mpi_cmp_int(N, 0) <= 0 || (N->p[0] & 1) == 0) {
1669
0
        return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1670
0
    }
1671
1672
3.62k
    if (mbedtls_mpi_cmp_int(E, 0) < 0) {
1673
0
        return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1674
0
    }
1675
1676
3.62k
    if (mbedtls_mpi_bitlen(E) > MBEDTLS_MPI_MAX_BITS ||
1677
3.62k
        mbedtls_mpi_bitlen(N) > MBEDTLS_MPI_MAX_BITS) {
1678
0
        return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1679
0
    }
1680
1681
    /*
1682
     * Init temps and window size
1683
     */
1684
3.62k
    mpi_montg_init(&mm, N);
1685
3.62k
    mbedtls_mpi_init(&RR); mbedtls_mpi_init(&T);
1686
3.62k
    mbedtls_mpi_init(&Apos);
1687
3.62k
    mbedtls_mpi_init(&WW);
1688
3.62k
    memset(W, 0, sizeof(W));
1689
1690
3.62k
    i = mbedtls_mpi_bitlen(E);
1691
1692
3.62k
    window_bitsize = (i > 671) ? 6 : (i > 239) ? 5 :
1693
3.62k
                     (i >  79) ? 4 : (i >  23) ? 3 : 1;
1694
1695
#if (MBEDTLS_MPI_WINDOW_SIZE < 6)
1696
    if (window_bitsize > MBEDTLS_MPI_WINDOW_SIZE) {
1697
        window_bitsize = MBEDTLS_MPI_WINDOW_SIZE;
1698
    }
1699
#endif
1700
1701
3.62k
    const size_t w_table_used_size = (size_t) 1 << window_bitsize;
1702
1703
    /*
1704
     * This function is not constant-trace: its memory accesses depend on the
1705
     * exponent value. To defend against timing attacks, callers (such as RSA
1706
     * and DHM) should use exponent blinding. However this is not enough if the
1707
     * adversary can find the exponent in a single trace, so this function
1708
     * takes extra precautions against adversaries who can observe memory
1709
     * access patterns.
1710
     *
1711
     * This function performs a series of multiplications by table elements and
1712
     * squarings, and we want the prevent the adversary from finding out which
1713
     * table element was used, and from distinguishing between multiplications
1714
     * and squarings. Firstly, when multiplying by an element of the window
1715
     * W[i], we do a constant-trace table lookup to obfuscate i. This leaves
1716
     * squarings as having a different memory access patterns from other
1717
     * multiplications. So secondly, we put the accumulator X in the table as
1718
     * well, and also do a constant-trace table lookup to multiply by X.
1719
     *
1720
     * This way, all multiplications take the form of a lookup-and-multiply.
1721
     * The number of lookup-and-multiply operations inside each iteration of
1722
     * the main loop still depends on the bits of the exponent, but since the
1723
     * other operations in the loop don't have an easily recognizable memory
1724
     * trace, an adversary is unlikely to be able to observe the exact
1725
     * patterns.
1726
     *
1727
     * An adversary may still be able to recover the exponent if they can
1728
     * observe both memory accesses and branches. However, branch prediction
1729
     * exploitation typically requires many traces of execution over the same
1730
     * data, which is defeated by randomized blinding.
1731
     *
1732
     * To achieve this, we make a copy of X and we use the table entry in each
1733
     * calculation from this point on.
1734
     */
1735
3.62k
    const size_t x_index = 0;
1736
3.62k
    mbedtls_mpi_init(&W[x_index]);
1737
3.62k
    mbedtls_mpi_copy(&W[x_index], X);
1738
1739
3.62k
    j = N->n + 1;
1740
    /* All W[i] and X must have at least N->n limbs for the mpi_montmul()
1741
     * and mpi_montred() calls later. Here we ensure that W[1] and X are
1742
     * large enough, and later we'll grow other W[i] to the same length.
1743
     * They must not be shrunk midway through this function!
1744
     */
1745
3.62k
    MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[x_index], j));
1746
3.62k
    MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[1],  j));
1747
3.62k
    MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T, j * 2));
1748
1749
    /*
1750
     * Compensate for negative A (and correct at the end)
1751
     */
1752
3.62k
    neg = (A->s == -1);
1753
3.62k
    if (neg) {
1754
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Apos, A));
1755
0
        Apos.s = 1;
1756
0
        A = &Apos;
1757
0
    }
1758
1759
    /*
1760
     * If 1st call, pre-compute R^2 mod N
1761
     */
1762
3.62k
    if (prec_RR == NULL || prec_RR->p == NULL) {
1763
3.62k
        MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&RR, 1));
1764
3.62k
        MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&RR, N->n * 2 * biL));
1765
3.62k
        MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&RR, &RR, N));
1766
1767
3.62k
        if (prec_RR != NULL) {
1768
3.62k
            memcpy(prec_RR, &RR, sizeof(mbedtls_mpi));
1769
3.62k
        }
1770
3.62k
    } else {
1771
0
        memcpy(&RR, prec_RR, sizeof(mbedtls_mpi));
1772
0
    }
1773
1774
    /*
1775
     * W[1] = A * R^2 * R^-1 mod N = A * R mod N
1776
     */
1777
3.62k
    if (mbedtls_mpi_cmp_mpi(A, N) >= 0) {
1778
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&W[1], A, N));
1779
        /* This should be a no-op because W[1] is already that large before
1780
         * mbedtls_mpi_mod_mpi(), but it's necessary to avoid an overflow
1781
         * in mpi_montmul() below, so let's make sure. */
1782
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[1], N->n + 1));
1783
3.62k
    } else {
1784
3.62k
        MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[1], A));
1785
3.62k
    }
1786
1787
    /* Note that this is safe because W[1] always has at least N->n limbs
1788
     * (it grew above and was preserved by mbedtls_mpi_copy()). */
1789
3.62k
    mpi_montmul(&W[1], &RR, N, mm, &T);
1790
1791
    /*
1792
     * W[x_index] = R^2 * R^-1 mod N = R mod N
1793
     */
1794
3.62k
    MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[x_index], &RR));
1795
3.62k
    mpi_montred(&W[x_index], N, mm, &T);
1796
1797
1798
3.62k
    if (window_bitsize > 1) {
1799
        /*
1800
         * W[i] = W[1] ^ i
1801
         *
1802
         * The first bit of the sliding window is always 1 and therefore we
1803
         * only need to store the second half of the table.
1804
         *
1805
         * (There are two special elements in the table: W[0] for the
1806
         * accumulator/result and W[1] for A in Montgomery form. Both of these
1807
         * are already set at this point.)
1808
         */
1809
375
        j = w_table_used_size / 2;
1810
1811
375
        MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[j], N->n + 1));
1812
375
        MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[j], &W[1]));
1813
1814
1.12k
        for (i = 0; i < window_bitsize - 1; i++) {
1815
750
            mpi_montmul(&W[j], &W[j], N, mm, &T);
1816
750
        }
1817
1818
        /*
1819
         * W[i] = W[i - 1] * W[1]
1820
         */
1821
1.50k
        for (i = j + 1; i < w_table_used_size; i++) {
1822
1.12k
            MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[i], N->n + 1));
1823
1.12k
            MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[i], &W[i - 1]));
1824
1825
1.12k
            mpi_montmul(&W[i], &W[1], N, mm, &T);
1826
1.12k
        }
1827
375
    }
1828
1829
3.62k
    nblimbs = E->n;
1830
3.62k
    bufsize = 0;
1831
3.62k
    nbits   = 0;
1832
3.62k
    size_t exponent_bits_in_window = 0;
1833
3.62k
    state   = 0;
1834
1835
235k
    while (1) {
1836
235k
        if (bufsize == 0) {
1837
7.25k
            if (nblimbs == 0) {
1838
3.62k
                break;
1839
3.62k
            }
1840
1841
3.62k
            nblimbs--;
1842
1843
3.62k
            bufsize = sizeof(mbedtls_mpi_uint) << 3;
1844
3.62k
        }
1845
1846
232k
        bufsize--;
1847
1848
232k
        ei = (E->p[nblimbs] >> bufsize) & 1;
1849
1850
        /*
1851
         * skip leading 0s
1852
         */
1853
232k
        if (ei == 0 && state == 0) {
1854
168k
            continue;
1855
168k
        }
1856
1857
63.9k
        if (ei == 0 && state == 1) {
1858
            /*
1859
             * out of window, square W[x_index]
1860
             */
1861
43.3k
            MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, x_index));
1862
43.3k
            mpi_montmul(&W[x_index], &WW, N, mm, &T);
1863
43.3k
            continue;
1864
43.3k
        }
1865
1866
        /*
1867
         * add ei to current window
1868
         */
1869
20.6k
        state = 2;
1870
1871
20.6k
        nbits++;
1872
20.6k
        exponent_bits_in_window |= (ei << (window_bitsize - nbits));
1873
1874
20.6k
        if (nbits == window_bitsize) {
1875
            /*
1876
             * W[x_index] = W[x_index]^window_bitsize R^-1 mod N
1877
             */
1878
36.7k
            for (i = 0; i < window_bitsize; i++) {
1879
20.1k
                MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size,
1880
20.1k
                                           x_index));
1881
20.1k
                mpi_montmul(&W[x_index], &WW, N, mm, &T);
1882
20.1k
            }
1883
1884
            /*
1885
             * W[x_index] = W[x_index] * W[exponent_bits_in_window] R^-1 mod N
1886
             */
1887
16.5k
            MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size,
1888
16.5k
                                       exponent_bits_in_window));
1889
16.5k
            mpi_montmul(&W[x_index], &WW, N, mm, &T);
1890
1891
16.5k
            state--;
1892
16.5k
            nbits = 0;
1893
16.5k
            exponent_bits_in_window = 0;
1894
16.5k
        }
1895
20.6k
    }
1896
1897
    /*
1898
     * process the remaining bits
1899
     */
1900
4.14k
    for (i = 0; i < nbits; i++) {
1901
517
        MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, x_index));
1902
517
        mpi_montmul(&W[x_index], &WW, N, mm, &T);
1903
1904
517
        exponent_bits_in_window <<= 1;
1905
1906
517
        if ((exponent_bits_in_window & ((size_t) 1 << window_bitsize)) != 0) {
1907
517
            MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, 1));
1908
517
            mpi_montmul(&W[x_index], &WW, N, mm, &T);
1909
517
        }
1910
517
    }
1911
1912
    /*
1913
     * W[x_index] = A^E * R * R^-1 mod N = A^E mod N
1914
     */
1915
3.62k
    mpi_montred(&W[x_index], N, mm, &T);
1916
1917
3.62k
    if (neg && E->n != 0 && (E->p[0] & 1) != 0) {
1918
0
        W[x_index].s = -1;
1919
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&W[x_index], N, &W[x_index]));
1920
0
    }
1921
1922
    /*
1923
     * Load the result in the output variable.
1924
     */
1925
3.62k
    mbedtls_mpi_copy(X, &W[x_index]);
1926
1927
3.62k
cleanup:
1928
1929
    /* The first bit of the sliding window is always 1 and therefore the first
1930
     * half of the table was unused. */
1931
8.38k
    for (i = w_table_used_size/2; i < w_table_used_size; i++) {
1932
4.75k
        mbedtls_mpi_free(&W[i]);
1933
4.75k
    }
1934
1935
3.62k
    mbedtls_mpi_free(&W[x_index]);
1936
3.62k
    mbedtls_mpi_free(&W[1]);
1937
3.62k
    mbedtls_mpi_free(&T);
1938
3.62k
    mbedtls_mpi_free(&Apos);
1939
3.62k
    mbedtls_mpi_free(&WW);
1940
1941
3.62k
    if (prec_RR == NULL || prec_RR->p == NULL) {
1942
0
        mbedtls_mpi_free(&RR);
1943
0
    }
1944
1945
3.62k
    return ret;
1946
3.62k
}
1947
1948
/*
1949
 * Greatest common divisor: G = gcd(A, B)  (HAC 14.54)
1950
 */
1951
int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
1952
352
{
1953
352
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1954
352
    size_t lz, lzt;
1955
352
    mbedtls_mpi TA, TB;
1956
1957
352
    MPI_VALIDATE_RET(G != NULL);
1958
352
    MPI_VALIDATE_RET(A != NULL);
1959
352
    MPI_VALIDATE_RET(B != NULL);
1960
1961
352
    mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TB);
1962
1963
352
    MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A));
1964
352
    MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B));
1965
1966
352
    lz = mbedtls_mpi_lsb(&TA);
1967
352
    lzt = mbedtls_mpi_lsb(&TB);
1968
1969
    /* The loop below gives the correct result when A==0 but not when B==0.
1970
     * So have a special case for B==0. Leverage the fact that we just
1971
     * calculated the lsb and lsb(B)==0 iff B is odd or 0 to make the test
1972
     * slightly more efficient than cmp_int(). */
1973
352
    if (lzt == 0 && mbedtls_mpi_get_bit(&TB, 0) == 0) {
1974
0
        ret = mbedtls_mpi_copy(G, A);
1975
0
        goto cleanup;
1976
0
    }
1977
1978
352
    if (lzt < lz) {
1979
88
        lz = lzt;
1980
88
    }
1981
1982
352
    TA.s = TB.s = 1;
1983
1984
    /* We mostly follow the procedure described in HAC 14.54, but with some
1985
     * minor differences:
1986
     * - Sequences of multiplications or divisions by 2 are grouped into a
1987
     *   single shift operation.
1988
     * - The procedure in HAC assumes that 0 < TB <= TA.
1989
     *     - The condition TB <= TA is not actually necessary for correctness.
1990
     *       TA and TB have symmetric roles except for the loop termination
1991
     *       condition, and the shifts at the beginning of the loop body
1992
     *       remove any significance from the ordering of TA vs TB before
1993
     *       the shifts.
1994
     *     - If TA = 0, the loop goes through 0 iterations and the result is
1995
     *       correctly TB.
1996
     *     - The case TB = 0 was short-circuited above.
1997
     *
1998
     * For the correctness proof below, decompose the original values of
1999
     * A and B as
2000
     *   A = sa * 2^a * A' with A'=0 or A' odd, and sa = +-1
2001
     *   B = sb * 2^b * B' with B'=0 or B' odd, and sb = +-1
2002
     * Then gcd(A, B) = 2^{min(a,b)} * gcd(A',B'),
2003
     * and gcd(A',B') is odd or 0.
2004
     *
2005
     * At the beginning, we have TA = |A| and TB = |B| so gcd(A,B) = gcd(TA,TB).
2006
     * The code maintains the following invariant:
2007
     *     gcd(A,B) = 2^k * gcd(TA,TB) for some k   (I)
2008
     */
2009
2010
    /* Proof that the loop terminates:
2011
     * At each iteration, either the right-shift by 1 is made on a nonzero
2012
     * value and the nonnegative integer bitlen(TA) + bitlen(TB) decreases
2013
     * by at least 1, or the right-shift by 1 is made on zero and then
2014
     * TA becomes 0 which ends the loop (TB cannot be 0 if it is right-shifted
2015
     * since in that case TB is calculated from TB-TA with the condition TB>TA).
2016
     */
2017
95.5k
    while (mbedtls_mpi_cmp_int(&TA, 0) != 0) {
2018
        /* Divisions by 2 preserve the invariant (I). */
2019
95.2k
        MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, mbedtls_mpi_lsb(&TA)));
2020
95.2k
        MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, mbedtls_mpi_lsb(&TB)));
2021
2022
        /* Set either TA or TB to |TA-TB|/2. Since TA and TB are both odd,
2023
         * TA-TB is even so the division by 2 has an integer result.
2024
         * Invariant (I) is preserved since any odd divisor of both TA and TB
2025
         * also divides |TA-TB|/2, and any odd divisor of both TA and |TA-TB|/2
2026
         * also divides TB, and any odd divisor of both TB and |TA-TB|/2 also
2027
         * divides TA.
2028
         */
2029
95.2k
        if (mbedtls_mpi_cmp_mpi(&TA, &TB) >= 0) {
2030
49.6k
            MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TA, &TA, &TB));
2031
49.6k
            MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, 1));
2032
49.6k
        } else {
2033
45.5k
            MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TB, &TB, &TA));
2034
45.5k
            MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, 1));
2035
45.5k
        }
2036
        /* Note that one of TA or TB is still odd. */
2037
95.2k
    }
2038
2039
    /* By invariant (I), gcd(A,B) = 2^k * gcd(TA,TB) for some k.
2040
     * At the loop exit, TA = 0, so gcd(TA,TB) = TB.
2041
     * - If there was at least one loop iteration, then one of TA or TB is odd,
2042
     *   and TA = 0, so TB is odd and gcd(TA,TB) = gcd(A',B'). In this case,
2043
     *   lz = min(a,b) so gcd(A,B) = 2^lz * TB.
2044
     * - If there was no loop iteration, then A was 0, and gcd(A,B) = B.
2045
     *   In this case, lz = 0 and B = TB so gcd(A,B) = B = 2^lz * TB as well.
2046
     */
2047
2048
352
    MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&TB, lz));
2049
352
    MBEDTLS_MPI_CHK(mbedtls_mpi_copy(G, &TB));
2050
2051
352
cleanup:
2052
2053
352
    mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TB);
2054
2055
352
    return ret;
2056
352
}
2057
2058
/*
2059
 * Fill X with size bytes of random.
2060
 * The bytes returned from the RNG are used in a specific order which
2061
 * is suitable for deterministic ECDSA (see the specification of
2062
 * mbedtls_mpi_random() and the implementation in mbedtls_mpi_fill_random()).
2063
 */
2064
int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
2065
                            int (*f_rng)(void *, unsigned char *, size_t),
2066
                            void *p_rng)
2067
0
{
2068
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2069
0
    const size_t limbs = CHARS_TO_LIMBS(size);
2070
2071
0
    MPI_VALIDATE_RET(X     != NULL);
2072
0
    MPI_VALIDATE_RET(f_rng != NULL);
2073
2074
    /* Ensure that target MPI has exactly the necessary number of limbs */
2075
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
2076
0
    if (size == 0) {
2077
0
        return 0;
2078
0
    }
2079
2080
0
    ret = mbedtls_mpi_core_fill_random(X->p, X->n, size, f_rng, p_rng);
2081
2082
0
cleanup:
2083
0
    return ret;
2084
0
}
2085
2086
int mbedtls_mpi_random(mbedtls_mpi *X,
2087
                       mbedtls_mpi_sint min,
2088
                       const mbedtls_mpi *N,
2089
                       int (*f_rng)(void *, unsigned char *, size_t),
2090
                       void *p_rng)
2091
440
{
2092
440
    if (min < 0) {
2093
0
        return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2094
0
    }
2095
440
    if (mbedtls_mpi_cmp_int(N, min) <= 0) {
2096
0
        return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2097
0
    }
2098
2099
    /* Ensure that target MPI has exactly the same number of limbs
2100
     * as the upper bound, even if the upper bound has leading zeros.
2101
     * This is necessary for mbedtls_mpi_core_random. */
2102
440
    int ret = mbedtls_mpi_resize_clear(X, N->n);
2103
440
    if (ret != 0) {
2104
0
        return ret;
2105
0
    }
2106
2107
440
    return mbedtls_mpi_core_random(X->p, min, N->p, X->n, f_rng, p_rng);
2108
440
}
2109
2110
/*
2111
 * Modular inverse: X = A^-1 mod N  (HAC 14.61 / 14.64)
2112
 */
2113
int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
2114
352
{
2115
352
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2116
352
    mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
2117
352
    MPI_VALIDATE_RET(X != NULL);
2118
352
    MPI_VALIDATE_RET(A != NULL);
2119
352
    MPI_VALIDATE_RET(N != NULL);
2120
2121
352
    if (mbedtls_mpi_cmp_int(N, 1) <= 0) {
2122
0
        return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2123
0
    }
2124
2125
352
    mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TU); mbedtls_mpi_init(&U1); mbedtls_mpi_init(&U2);
2126
352
    mbedtls_mpi_init(&G); mbedtls_mpi_init(&TB); mbedtls_mpi_init(&TV);
2127
352
    mbedtls_mpi_init(&V1); mbedtls_mpi_init(&V2);
2128
2129
352
    MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, A, N));
2130
2131
352
    if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
2132
0
        ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2133
0
        goto cleanup;
2134
0
    }
2135
2136
352
    MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&TA, A, N));
2137
352
    MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TU, &TA));
2138
352
    MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, N));
2139
352
    MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TV, N));
2140
2141
352
    MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U1, 1));
2142
352
    MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U2, 0));
2143
352
    MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V1, 0));
2144
352
    MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V2, 1));
2145
2146
95.2k
    do {
2147
193k
        while ((TU.p[0] & 1) == 0) {
2148
97.8k
            MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TU, 1));
2149
2150
97.8k
            if ((U1.p[0] & 1) != 0 || (U2.p[0] & 1) != 0) {
2151
46.6k
                MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&U1, &U1, &TB));
2152
46.6k
                MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &TA));
2153
46.6k
            }
2154
2155
97.8k
            MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U1, 1));
2156
97.8k
            MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U2, 1));
2157
97.8k
        }
2158
2159
188k
        while ((TV.p[0] & 1) == 0) {
2160
93.1k
            MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TV, 1));
2161
2162
93.1k
            if ((V1.p[0] & 1) != 0 || (V2.p[0] & 1) != 0) {
2163
46.9k
                MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, &TB));
2164
46.9k
                MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &TA));
2165
46.9k
            }
2166
2167
93.1k
            MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V1, 1));
2168
93.1k
            MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V2, 1));
2169
93.1k
        }
2170
2171
95.2k
        if (mbedtls_mpi_cmp_mpi(&TU, &TV) >= 0) {
2172
49.6k
            MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TU, &TU, &TV));
2173
49.6k
            MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U1, &U1, &V1));
2174
49.6k
            MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &V2));
2175
49.6k
        } else {
2176
45.5k
            MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TV, &TV, &TU));
2177
45.5k
            MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, &U1));
2178
45.5k
            MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &U2));
2179
45.5k
        }
2180
95.2k
    } while (mbedtls_mpi_cmp_int(&TU, 0) != 0);
2181
2182
440
    while (mbedtls_mpi_cmp_int(&V1, 0) < 0) {
2183
88
        MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, N));
2184
88
    }
2185
2186
352
    while (mbedtls_mpi_cmp_mpi(&V1, N) >= 0) {
2187
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, N));
2188
0
    }
2189
2190
352
    MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, &V1));
2191
2192
352
cleanup:
2193
2194
352
    mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TU); mbedtls_mpi_free(&U1); mbedtls_mpi_free(&U2);
2195
352
    mbedtls_mpi_free(&G); mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TV);
2196
352
    mbedtls_mpi_free(&V1); mbedtls_mpi_free(&V2);
2197
2198
352
    return ret;
2199
352
}
2200
2201
#if defined(MBEDTLS_GENPRIME)
2202
2203
static const int small_prime[] =
2204
{
2205
    3,    5,    7,   11,   13,   17,   19,   23,
2206
    29,   31,   37,   41,   43,   47,   53,   59,
2207
    61,   67,   71,   73,   79,   83,   89,   97,
2208
    101,  103,  107,  109,  113,  127,  131,  137,
2209
    139,  149,  151,  157,  163,  167,  173,  179,
2210
    181,  191,  193,  197,  199,  211,  223,  227,
2211
    229,  233,  239,  241,  251,  257,  263,  269,
2212
    271,  277,  281,  283,  293,  307,  311,  313,
2213
    317,  331,  337,  347,  349,  353,  359,  367,
2214
    373,  379,  383,  389,  397,  401,  409,  419,
2215
    421,  431,  433,  439,  443,  449,  457,  461,
2216
    463,  467,  479,  487,  491,  499,  503,  509,
2217
    521,  523,  541,  547,  557,  563,  569,  571,
2218
    577,  587,  593,  599,  601,  607,  613,  617,
2219
    619,  631,  641,  643,  647,  653,  659,  661,
2220
    673,  677,  683,  691,  701,  709,  719,  727,
2221
    733,  739,  743,  751,  757,  761,  769,  773,
2222
    787,  797,  809,  811,  821,  823,  827,  829,
2223
    839,  853,  857,  859,  863,  877,  881,  883,
2224
    887,  907,  911,  919,  929,  937,  941,  947,
2225
    953,  967,  971,  977,  983,  991,  997, -103
2226
};
2227
2228
/*
2229
 * Small divisors test (X must be positive)
2230
 *
2231
 * Return values:
2232
 * 0: no small factor (possible prime, more tests needed)
2233
 * 1: certain prime
2234
 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
2235
 * other negative: error
2236
 */
2237
static int mpi_check_small_factors(const mbedtls_mpi *X)
2238
0
{
2239
0
    int ret = 0;
2240
0
    size_t i;
2241
0
    mbedtls_mpi_uint r;
2242
2243
0
    if ((X->p[0] & 1) == 0) {
2244
0
        return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2245
0
    }
2246
2247
0
    for (i = 0; small_prime[i] > 0; i++) {
2248
0
        if (mbedtls_mpi_cmp_int(X, small_prime[i]) <= 0) {
2249
0
            return 1;
2250
0
        }
2251
2252
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, small_prime[i]));
2253
2254
0
        if (r == 0) {
2255
0
            return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2256
0
        }
2257
0
    }
2258
2259
0
cleanup:
2260
0
    return ret;
2261
0
}
2262
2263
/*
2264
 * Miller-Rabin pseudo-primality test  (HAC 4.24)
2265
 */
2266
static int mpi_miller_rabin(const mbedtls_mpi *X, size_t rounds,
2267
                            int (*f_rng)(void *, unsigned char *, size_t),
2268
                            void *p_rng)
2269
0
{
2270
0
    int ret, count;
2271
0
    size_t i, j, k, s;
2272
0
    mbedtls_mpi W, R, T, A, RR;
2273
2274
0
    MPI_VALIDATE_RET(X     != NULL);
2275
0
    MPI_VALIDATE_RET(f_rng != NULL);
2276
2277
0
    mbedtls_mpi_init(&W); mbedtls_mpi_init(&R);
2278
0
    mbedtls_mpi_init(&T); mbedtls_mpi_init(&A);
2279
0
    mbedtls_mpi_init(&RR);
2280
2281
    /*
2282
     * W = |X| - 1
2283
     * R = W >> lsb( W )
2284
     */
2285
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&W, X, 1));
2286
0
    s = mbedtls_mpi_lsb(&W);
2287
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R, &W));
2288
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&R, s));
2289
2290
0
    for (i = 0; i < rounds; i++) {
2291
        /*
2292
         * pick a random A, 1 < A < |X| - 1
2293
         */
2294
0
        count = 0;
2295
0
        do {
2296
0
            MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&A, X->n * ciL, f_rng, p_rng));
2297
2298
0
            j = mbedtls_mpi_bitlen(&A);
2299
0
            k = mbedtls_mpi_bitlen(&W);
2300
0
            if (j > k) {
2301
0
                A.p[A.n - 1] &= ((mbedtls_mpi_uint) 1 << (k - (A.n - 1) * biL - 1)) - 1;
2302
0
            }
2303
2304
0
            if (count++ > 30) {
2305
0
                ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2306
0
                goto cleanup;
2307
0
            }
2308
2309
0
        } while (mbedtls_mpi_cmp_mpi(&A, &W) >= 0 ||
2310
0
                 mbedtls_mpi_cmp_int(&A, 1)  <= 0);
2311
2312
        /*
2313
         * A = A^R mod |X|
2314
         */
2315
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&A, &A, &R, X, &RR));
2316
2317
0
        if (mbedtls_mpi_cmp_mpi(&A, &W) == 0 ||
2318
0
            mbedtls_mpi_cmp_int(&A,  1) == 0) {
2319
0
            continue;
2320
0
        }
2321
2322
0
        j = 1;
2323
0
        while (j < s && mbedtls_mpi_cmp_mpi(&A, &W) != 0) {
2324
            /*
2325
             * A = A * A mod |X|
2326
             */
2327
0
            MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &A, &A));
2328
0
            MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&A, &T, X));
2329
2330
0
            if (mbedtls_mpi_cmp_int(&A, 1) == 0) {
2331
0
                break;
2332
0
            }
2333
2334
0
            j++;
2335
0
        }
2336
2337
        /*
2338
         * not prime if A != |X| - 1 or A == 1
2339
         */
2340
0
        if (mbedtls_mpi_cmp_mpi(&A, &W) != 0 ||
2341
0
            mbedtls_mpi_cmp_int(&A,  1) == 0) {
2342
0
            ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2343
0
            break;
2344
0
        }
2345
0
    }
2346
2347
0
cleanup:
2348
0
    mbedtls_mpi_free(&W); mbedtls_mpi_free(&R);
2349
0
    mbedtls_mpi_free(&T); mbedtls_mpi_free(&A);
2350
0
    mbedtls_mpi_free(&RR);
2351
2352
0
    return ret;
2353
0
}
2354
2355
/*
2356
 * Pseudo-primality test: small factors, then Miller-Rabin
2357
 */
2358
int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
2359
                             int (*f_rng)(void *, unsigned char *, size_t),
2360
                             void *p_rng)
2361
0
{
2362
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2363
0
    mbedtls_mpi XX;
2364
0
    MPI_VALIDATE_RET(X     != NULL);
2365
0
    MPI_VALIDATE_RET(f_rng != NULL);
2366
2367
0
    XX.s = 1;
2368
0
    XX.n = X->n;
2369
0
    XX.p = X->p;
2370
2371
0
    if (mbedtls_mpi_cmp_int(&XX, 0) == 0 ||
2372
0
        mbedtls_mpi_cmp_int(&XX, 1) == 0) {
2373
0
        return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2374
0
    }
2375
2376
0
    if (mbedtls_mpi_cmp_int(&XX, 2) == 0) {
2377
0
        return 0;
2378
0
    }
2379
2380
0
    if ((ret = mpi_check_small_factors(&XX)) != 0) {
2381
0
        if (ret == 1) {
2382
0
            return 0;
2383
0
        }
2384
2385
0
        return ret;
2386
0
    }
2387
2388
0
    return mpi_miller_rabin(&XX, rounds, f_rng, p_rng);
2389
0
}
2390
2391
/*
2392
 * Prime number generation
2393
 *
2394
 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2395
 * be either 1024 bits or 1536 bits long, and flags must contain
2396
 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
2397
 */
2398
int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
2399
                          int (*f_rng)(void *, unsigned char *, size_t),
2400
                          void *p_rng)
2401
0
{
2402
0
#ifdef MBEDTLS_HAVE_INT64
2403
// ceil(2^63.5)
2404
0
#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2405
#else
2406
// ceil(2^31.5)
2407
#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2408
#endif
2409
0
    int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2410
0
    size_t k, n;
2411
0
    int rounds;
2412
0
    mbedtls_mpi_uint r;
2413
0
    mbedtls_mpi Y;
2414
2415
0
    MPI_VALIDATE_RET(X     != NULL);
2416
0
    MPI_VALIDATE_RET(f_rng != NULL);
2417
2418
0
    if (nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS) {
2419
0
        return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2420
0
    }
2421
2422
0
    mbedtls_mpi_init(&Y);
2423
2424
0
    n = BITS_TO_LIMBS(nbits);
2425
2426
0
    if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR) == 0) {
2427
        /*
2428
         * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2429
         */
2430
0
        rounds = ((nbits >= 1300) ?  2 : (nbits >=  850) ?  3 :
2431
0
                  (nbits >=  650) ?  4 : (nbits >=  350) ?  8 :
2432
0
                  (nbits >=  250) ? 12 : (nbits >=  150) ? 18 : 27);
2433
0
    } else {
2434
        /*
2435
         * 2^-100 error probability, number of rounds computed based on HAC,
2436
         * fact 4.48
2437
         */
2438
0
        rounds = ((nbits >= 1450) ?  4 : (nbits >=  1150) ?  5 :
2439
0
                  (nbits >= 1000) ?  6 : (nbits >=   850) ?  7 :
2440
0
                  (nbits >=  750) ?  8 : (nbits >=   500) ? 13 :
2441
0
                  (nbits >=  250) ? 28 : (nbits >=   150) ? 40 : 51);
2442
0
    }
2443
2444
0
    while (1) {
2445
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(X, n * ciL, f_rng, p_rng));
2446
        /* make sure generated number is at least (nbits-1)+0.5 bits (FIPS 186-4 §B.3.3 steps 4.4, 5.5) */
2447
0
        if (X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2) {
2448
0
            continue;
2449
0
        }
2450
2451
0
        k = n * biL;
2452
0
        if (k > nbits) {
2453
0
            MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(X, k - nbits));
2454
0
        }
2455
0
        X->p[0] |= 1;
2456
2457
0
        if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH) == 0) {
2458
0
            ret = mbedtls_mpi_is_prime_ext(X, rounds, f_rng, p_rng);
2459
2460
0
            if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
2461
0
                goto cleanup;
2462
0
            }
2463
0
        } else {
2464
            /*
2465
             * A necessary condition for Y and X = 2Y + 1 to be prime
2466
             * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2467
             * Make sure it is satisfied, while keeping X = 3 mod 4
2468
             */
2469
2470
0
            X->p[0] |= 2;
2471
2472
0
            MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, 3));
2473
0
            if (r == 0) {
2474
0
                MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 8));
2475
0
            } else if (r == 1) {
2476
0
                MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 4));
2477
0
            }
2478
2479
            /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
2480
0
            MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, X));
2481
0
            MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, 1));
2482
2483
0
            while (1) {
2484
                /*
2485
                 * First, check small factors for X and Y
2486
                 * before doing Miller-Rabin on any of them
2487
                 */
2488
0
                if ((ret = mpi_check_small_factors(X)) == 0 &&
2489
0
                    (ret = mpi_check_small_factors(&Y)) == 0 &&
2490
0
                    (ret = mpi_miller_rabin(X, rounds, f_rng, p_rng))
2491
0
                    == 0 &&
2492
0
                    (ret = mpi_miller_rabin(&Y, rounds, f_rng, p_rng))
2493
0
                    == 0) {
2494
0
                    goto cleanup;
2495
0
                }
2496
2497
0
                if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
2498
0
                    goto cleanup;
2499
0
                }
2500
2501
                /*
2502
                 * Next candidates. We want to preserve Y = (X-1) / 2 and
2503
                 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2504
                 * so up Y by 6 and X by 12.
2505
                 */
2506
0
                MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X,  X, 12));
2507
0
                MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&Y, &Y, 6));
2508
0
            }
2509
0
        }
2510
0
    }
2511
2512
0
cleanup:
2513
2514
0
    mbedtls_mpi_free(&Y);
2515
2516
0
    return ret;
2517
0
}
2518
2519
#endif /* MBEDTLS_GENPRIME */
2520
2521
#if defined(MBEDTLS_SELF_TEST)
2522
2523
0
#define GCD_PAIR_COUNT  3
2524
2525
static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2526
{
2527
    { 693, 609, 21 },
2528
    { 1764, 868, 28 },
2529
    { 768454923, 542167814, 1 }
2530
};
2531
2532
/*
2533
 * Checkup routine
2534
 */
2535
int mbedtls_mpi_self_test(int verbose)
2536
0
{
2537
0
    int ret, i;
2538
0
    mbedtls_mpi A, E, N, X, Y, U, V;
2539
2540
0
    mbedtls_mpi_init(&A); mbedtls_mpi_init(&E); mbedtls_mpi_init(&N); mbedtls_mpi_init(&X);
2541
0
    mbedtls_mpi_init(&Y); mbedtls_mpi_init(&U); mbedtls_mpi_init(&V);
2542
2543
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&A, 16,
2544
0
                                            "EFE021C2645FD1DC586E69184AF4A31E" \
2545
0
                                            "D5F53E93B5F123FA41680867BA110131" \
2546
0
                                            "944FE7952E2517337780CB0DB80E61AA" \
2547
0
                                            "E7C8DDC6C5C6AADEB34EB38A2F40D5E6"));
2548
2549
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&E, 16,
2550
0
                                            "B2E7EFD37075B9F03FF989C7C5051C20" \
2551
0
                                            "34D2A323810251127E7BF8625A4F49A5" \
2552
0
                                            "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
2553
0
                                            "5B5C25763222FEFCCFC38B832366C29E"));
2554
2555
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&N, 16,
2556
0
                                            "0066A198186C18C10B2F5ED9B522752A" \
2557
0
                                            "9830B69916E535C8F047518A889A43A5" \
2558
0
                                            "94B6BED27A168D31D4A52F88925AA8F5"));
2559
2560
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&X, &A, &N));
2561
2562
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2563
0
                                            "602AB7ECA597A3D6B56FF9829A5E8B85" \
2564
0
                                            "9E857EA95A03512E2BAE7391688D264A" \
2565
0
                                            "A5663B0341DB9CCFD2C4C5F421FEC814" \
2566
0
                                            "8001B72E848A38CAE1C65F78E56ABDEF" \
2567
0
                                            "E12D3C039B8A02D6BE593F0BBBDA56F1" \
2568
0
                                            "ECF677152EF804370C1A305CAF3B5BF1" \
2569
0
                                            "30879B56C61DE584A0F53A2447A51E"));
2570
2571
0
    if (verbose != 0) {
2572
0
        mbedtls_printf("  MPI test #1 (mul_mpi): ");
2573
0
    }
2574
2575
0
    if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2576
0
        if (verbose != 0) {
2577
0
            mbedtls_printf("failed\n");
2578
0
        }
2579
2580
0
        ret = 1;
2581
0
        goto cleanup;
2582
0
    }
2583
2584
0
    if (verbose != 0) {
2585
0
        mbedtls_printf("passed\n");
2586
0
    }
2587
2588
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&X, &Y, &A, &N));
2589
2590
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2591
0
                                            "256567336059E52CAE22925474705F39A94"));
2592
2593
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&V, 16,
2594
0
                                            "6613F26162223DF488E9CD48CC132C7A" \
2595
0
                                            "0AC93C701B001B092E4E5B9F73BCD27B" \
2596
0
                                            "9EE50D0657C77F374E903CDFA4C642"));
2597
2598
0
    if (verbose != 0) {
2599
0
        mbedtls_printf("  MPI test #2 (div_mpi): ");
2600
0
    }
2601
2602
0
    if (mbedtls_mpi_cmp_mpi(&X, &U) != 0 ||
2603
0
        mbedtls_mpi_cmp_mpi(&Y, &V) != 0) {
2604
0
        if (verbose != 0) {
2605
0
            mbedtls_printf("failed\n");
2606
0
        }
2607
2608
0
        ret = 1;
2609
0
        goto cleanup;
2610
0
    }
2611
2612
0
    if (verbose != 0) {
2613
0
        mbedtls_printf("passed\n");
2614
0
    }
2615
2616
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&X, &A, &E, &N, NULL));
2617
2618
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2619
0
                                            "36E139AEA55215609D2816998ED020BB" \
2620
0
                                            "BD96C37890F65171D948E9BC7CBAA4D9" \
2621
0
                                            "325D24D6A3C12710F10A09FA08AB87"));
2622
2623
0
    if (verbose != 0) {
2624
0
        mbedtls_printf("  MPI test #3 (exp_mod): ");
2625
0
    }
2626
2627
0
    if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2628
0
        if (verbose != 0) {
2629
0
            mbedtls_printf("failed\n");
2630
0
        }
2631
2632
0
        ret = 1;
2633
0
        goto cleanup;
2634
0
    }
2635
2636
0
    if (verbose != 0) {
2637
0
        mbedtls_printf("passed\n");
2638
0
    }
2639
2640
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&X, &A, &N));
2641
2642
0
    MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2643
0
                                            "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
2644
0
                                            "C3DBA76456363A10869622EAC2DD84EC" \
2645
0
                                            "C5B8A74DAC4D09E03B5E0BE779F2DF61"));
2646
2647
0
    if (verbose != 0) {
2648
0
        mbedtls_printf("  MPI test #4 (inv_mod): ");
2649
0
    }
2650
2651
0
    if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2652
0
        if (verbose != 0) {
2653
0
            mbedtls_printf("failed\n");
2654
0
        }
2655
2656
0
        ret = 1;
2657
0
        goto cleanup;
2658
0
    }
2659
2660
0
    if (verbose != 0) {
2661
0
        mbedtls_printf("passed\n");
2662
0
    }
2663
2664
0
    if (verbose != 0) {
2665
0
        mbedtls_printf("  MPI test #5 (simple gcd): ");
2666
0
    }
2667
2668
0
    for (i = 0; i < GCD_PAIR_COUNT; i++) {
2669
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&X, gcd_pairs[i][0]));
2670
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Y, gcd_pairs[i][1]));
2671
2672
0
        MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&A, &X, &Y));
2673
2674
0
        if (mbedtls_mpi_cmp_int(&A, gcd_pairs[i][2]) != 0) {
2675
0
            if (verbose != 0) {
2676
0
                mbedtls_printf("failed at %d\n", i);
2677
0
            }
2678
2679
0
            ret = 1;
2680
0
            goto cleanup;
2681
0
        }
2682
0
    }
2683
2684
0
    if (verbose != 0) {
2685
0
        mbedtls_printf("passed\n");
2686
0
    }
2687
2688
0
cleanup:
2689
2690
0
    if (ret != 0 && verbose != 0) {
2691
0
        mbedtls_printf("Unexpected error, return code = %08X\n", (unsigned int) ret);
2692
0
    }
2693
2694
0
    mbedtls_mpi_free(&A); mbedtls_mpi_free(&E); mbedtls_mpi_free(&N); mbedtls_mpi_free(&X);
2695
0
    mbedtls_mpi_free(&Y); mbedtls_mpi_free(&U); mbedtls_mpi_free(&V);
2696
2697
0
    if (verbose != 0) {
2698
0
        mbedtls_printf("\n");
2699
0
    }
2700
2701
0
    return ret;
2702
0
}
2703
2704
#endif /* MBEDTLS_SELF_TEST */
2705
2706
#endif /* MBEDTLS_BIGNUM_C */