Coverage Report

Created: 2026-10-17 06:10

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