scorpiox-bot-plugin-webhook
←bignum.cc
2583 lines · 68 KBHEADBlameDownload
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 */
48static inline signed short mbedtls_ct_mpi_sign_if(mbedtls_ct_condition_t cond,
49 signed short sign1, signed short sign2)
50{
51 return (signed short) mbedtls_ct_uint_if(cond, sign1 + 1, sign2 + 1) - 1;
52}
53
54/*
55 * Compare signed values in constant time
56 */
57int mbedtls_mpi_lt_mpi_ct(const mbedtls_mpi *X,
58 const mbedtls_mpi *Y,
59 unsigned *ret)
60{
61 mbedtls_ct_condition_t different_sign, X_is_negative, Y_is_negative, result;
62
63 if (X->n != Y->n) {
64 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
65 }
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 X_is_negative = mbedtls_ct_bool((X->s & 2) >> 1);
72 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 different_sign = mbedtls_ct_bool_ne(X_is_negative, Y_is_negative); // true if different sign
80 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 void * const p[2] = { X->p, Y->p };
90 size_t i = mbedtls_ct_size_if_else_0(X_is_negative, 1);
91 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 result = mbedtls_ct_bool_or(result,
98 mbedtls_ct_bool_and(mbedtls_ct_bool_not(different_sign), lt));
99
100 *ret = mbedtls_ct_uint_if_else_0(result, 1);
101
102 return 0;
103}
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
118int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X,
119 const mbedtls_mpi *Y,
120 unsigned char assign)
121{
122 int ret = 0;
123
124 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, Y->n));
125
126 {
127 mbedtls_ct_condition_t do_assign = mbedtls_ct_bool(assign);
128
129 X->s = mbedtls_ct_mpi_sign_if(do_assign, Y->s, X->s);
130
131 mbedtls_mpi_core_cond_assign(X->p, Y->p, Y->n, do_assign);
132
133 mbedtls_ct_condition_t do_not_assign = mbedtls_ct_bool_not(do_assign);
134 for (size_t i = Y->n; i < X->n; i++) {
135 X->p[i] = mbedtls_ct_mpi_uint_if_else_0(do_not_assign, X->p[i]);
136 }
137 }
138
139cleanup:
140 return ret;
141}
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 */
149int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X,
150 mbedtls_mpi *Y,
151 unsigned char swap)
152{
153 int ret = 0;
154 int s;
155
156 if (X == Y) {
157 return 0;
158 }
159
160 mbedtls_ct_condition_t do_swap = mbedtls_ct_bool(swap);
161
162 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, Y->n));
163 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Y, X->n));
164
165 s = X->s;
166 X->s = mbedtls_ct_mpi_sign_if(do_swap, Y->s, X->s);
167 Y->s = mbedtls_ct_mpi_sign_if(do_swap, s, Y->s);
168
169 mbedtls_mpi_core_cond_swap(X->p, Y->p, X->n, do_swap);
170
171cleanup:
172 return ret;
173}
174
175/* Implementation that should never be optimized out by the compiler */
176#define mbedtls_mpi_zeroize_and_free(v, n) mbedtls_zeroize_and_free(v, ciL * (n))
177
178/*
179 * Initialize one MPI
180 */
181void mbedtls_mpi_init(mbedtls_mpi *X)
182{
183 X->s = 1;
184 X->n = 0;
185 X->p = NULL;
186}
187
188/*
189 * Unallocate one MPI
190 */
191void mbedtls_mpi_free(mbedtls_mpi *X)
192{
193 if (X == NULL) {
194 return;
195 }
196
197 if (X->p != NULL) {
198 mbedtls_mpi_zeroize_and_free(X->p, X->n);
199 }
200
201 X->s = 1;
202 X->n = 0;
203 X->p = NULL;
204}
205
206/*
207 * Enlarge to the specified number of limbs
208 */
209int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs)
210{
211 mbedtls_mpi_uint *p;
212
213 if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
214 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
215 }
216
217 if (X->n < nblimbs) {
218 if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(nblimbs, ciL)) == NULL) {
219 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
220 }
221
222 if (X->p != NULL) {
223 memcpy(p, X->p, X->n * ciL);
224 mbedtls_mpi_zeroize_and_free(X->p, X->n);
225 }
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 X->n = (unsigned short) nblimbs;
230 X->p = p;
231 }
232
233 return 0;
234}
235
236/*
237 * Resize down as much as possible,
238 * while keeping at least the specified number of limbs
239 */
240int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs)
241{
242 mbedtls_mpi_uint *p;
243 size_t i;
244
245 if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
246 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
247 }
248
249 /* Actually resize up if there are currently fewer than nblimbs limbs. */
250 if (X->n <= nblimbs) {
251 return mbedtls_mpi_grow(X, nblimbs);
252 }
253 /* After this point, then X->n > nblimbs and in particular X->n > 0. */
254
255 for (i = X->n - 1; i > 0; i--) {
256 if (X->p[i] != 0) {
257 break;
258 }
259 }
260 i++;
261
262 if (i < nblimbs) {
263 i = nblimbs;
264 }
265
266 if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(i, ciL)) == NULL) {
267 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
268 }
269
270 if (X->p != NULL) {
271 memcpy(p, X->p, i * ciL);
272 mbedtls_mpi_zeroize_and_free(X->p, X->n);
273 }
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 X->n = (unsigned short) i;
278 X->p = p;
279
280 return 0;
281}
282
283/* Resize X to have exactly n limbs and set it to 0. */
284static int mbedtls_mpi_resize_clear(mbedtls_mpi *X, size_t limbs)
285{
286 if (limbs == 0) {
287 mbedtls_mpi_free(X);
288 return 0;
289 } else if (X->n == limbs) {
290 memset(X->p, 0, limbs * ciL);
291 X->s = 1;
292 return 0;
293 } else {
294 mbedtls_mpi_free(X);
295 return mbedtls_mpi_grow(X, limbs);
296 }
297}
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 */
307int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
308{
309 int ret = 0;
310 size_t i;
311
312 if (X == Y) {
313 return 0;
314 }
315
316 if (Y->n == 0) {
317 if (X->n != 0) {
318 X->s = 1;
319 memset(X->p, 0, X->n * ciL);
320 }
321 return 0;
322 }
323
324 for (i = Y->n - 1; i > 0; i--) {
325 if (Y->p[i] != 0) {
326 break;
327 }
328 }
329 i++;
330
331 X->s = Y->s;
332
333 if (X->n < i) {
334 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i));
335 } else {
336 memset(X->p + i, 0, (X->n - i) * ciL);
337 }
338
339 memcpy(X->p, Y->p, i * ciL);
340
341cleanup:
342
343 return ret;
344}
345
346/*
347 * Swap the contents of X and Y
348 */
349void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
350{
351 mbedtls_mpi T;
352
353 memcpy(&T, X, sizeof(mbedtls_mpi));
354 memcpy(X, Y, sizeof(mbedtls_mpi));
355 memcpy(Y, &T, sizeof(mbedtls_mpi));
356}
357
358static inline mbedtls_mpi_uint mpi_sint_abs(mbedtls_mpi_sint z)
359{
360 if (z >= 0) {
361 return z;
362 }
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 return (mbedtls_mpi_uint) 0 - (mbedtls_mpi_uint) z;
368}
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#define TO_SIGN(x) ((mbedtls_mpi_sint) (((mbedtls_mpi_uint) x) >> (biL - 1)) * -2 + 1)
373
374/*
375 * Set value from integer
376 */
377int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z)
378{
379 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
380
381 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, 1));
382 memset(X->p, 0, X->n * ciL);
383
384 X->p[0] = mpi_sint_abs(z);
385 X->s = TO_SIGN(z);
386
387cleanup:
388
389 return ret;
390}
391
392/*
393 * Get a specific bit
394 */
395int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
396{
397 if (X->n * biL <= pos) {
398 return 0;
399 }
400
401 return (X->p[pos / biL] >> (pos % biL)) & 0x01;
402}
403
404/*
405 * Set a bit to a specific value of 0 or 1
406 */
407int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val)
408{
409 int ret = 0;
410 size_t off = pos / biL;
411 size_t idx = pos % biL;
412
413 if (val != 0 && val != 1) {
414 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
415 }
416
417 if (X->n * biL <= pos) {
418 if (val == 0) {
419 return 0;
420 }
421
422 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, off + 1));
423 }
424
425 X->p[off] &= ~((mbedtls_mpi_uint) 0x01 << idx);
426 X->p[off] |= (mbedtls_mpi_uint) val << idx;
427
428cleanup:
429
430 return ret;
431}
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 #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)
444static 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 */
462size_t mbedtls_mpi_lsb(const mbedtls_mpi *X)
463{
464 size_t i;
465
466 for (i = 0; i < X->n; i++) {
467 if (X->p[i] != 0) {
468 return i * biL + mbedtls_mpi_uint_ctz(X->p[i]);
469 }
470 }
471
472 return 0;
473}
474
475/*
476 * Return the number of bits
477 */
478size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
479{
480 return mbedtls_mpi_core_bitlen(X->p, X->n);
481}
482
483/*
484 * Return the total size in bytes
485 */
486size_t mbedtls_mpi_size(const mbedtls_mpi *X)
487{
488 return (mbedtls_mpi_bitlen(X) + 7) >> 3;
489}
490
491/*
492 * Convert an ASCII character to digit value
493 */
494static int mpi_get_digit(mbedtls_mpi_uint *d, int radix, char c)
495{
496 *d = 255;
497
498 if (c >= 0x30 && c <= 0x39) {
499 *d = c - 0x30;
500 }
501 if (c >= 0x41 && c <= 0x46) {
502 *d = c - 0x37;
503 }
504 if (c >= 0x61 && c <= 0x66) {
505 *d = c - 0x57;
506 }
507
508 if (*d >= (mbedtls_mpi_uint) radix) {
509 return MBEDTLS_ERR_MPI_INVALID_CHARACTER;
510 }
511
512 return 0;
513}
514
515/*
516 * Import from an ASCII string
517 */
518int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s)
519{
520 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
521 size_t i, j, slen, n;
522 int sign = 1;
523 mbedtls_mpi_uint d;
524 mbedtls_mpi T;
525
526 if (radix < 2 || radix > 16) {
527 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
528 }
529
530 mbedtls_mpi_init(&T);
531
532 if (s[0] == 0) {
533 mbedtls_mpi_free(X);
534 return 0;
535 }
536
537 if (s[0] == '-') {
538 ++s;
539 sign = -1;
540 }
541
542 slen = strlen(s);
543
544 if (radix == 16) {
545 if (slen > SIZE_MAX >> 2) {
546 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
547 }
548
549 n = BITS_TO_LIMBS(slen << 2);
550
551 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n));
552 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
553
554 for (i = slen, j = 0; i > 0; i--, j++) {
555 MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i - 1]));
556 X->p[j / (2 * ciL)] |= d << ((j % (2 * ciL)) << 2);
557 }
558 } else {
559 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
560
561 for (i = 0; i < slen; i++) {
562 MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i]));
563 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T, X, radix));
564 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, &T, d));
565 }
566 }
567
568 if (sign < 0 && mbedtls_mpi_bitlen(X) != 0) {
569 X->s = -1;
570 }
571
572cleanup:
573
574 mbedtls_mpi_free(&T);
575
576 return ret;
577}
578
579/*
580 * Helper to write the digits high-order first.
581 */
582static int mpi_write_hlp(mbedtls_mpi *X, int radix,
583 char **p, const size_t buflen)
584{
585 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
586 mbedtls_mpi_uint r;
587 size_t length = 0;
588 char *p_end = *p + buflen;
589
590 do {
591 if (length >= buflen) {
592 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
593 }
594
595 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, radix));
596 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 if (r < 0xA) {
601 *(--p_end) = (char) ('0' + r);
602 } else {
603 *(--p_end) = (char) ('A' + (r - 0xA));
604 }
605
606 length++;
607 } while (mbedtls_mpi_cmp_int(X, 0) != 0);
608
609 memmove(*p, p_end, length);
610 *p += length;
611
612cleanup:
613
614 return ret;
615}
616
617/*
618 * Export into an ASCII string
619 */
620int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
621 char *buf, size_t buflen, size_t *olen)
622{
623 int ret = 0;
624 size_t n;
625 char *p;
626 mbedtls_mpi T;
627
628 if (radix < 2 || radix > 16) {
629 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
630 }
631
632 n = mbedtls_mpi_bitlen(X); /* Number of bits necessary to present `n`. */
633 if (radix >= 4) {
634 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 }
639 if (radix >= 16) {
640 n >>= 1; /* Number of hexadecimal digits necessary to
641 * present `n`. */
642
643 }
644 n += 1; /* Terminating null byte */
645 n += 1; /* Compensate for the divisions above, which round down `n`
646 * in case it's not even. */
647 n += 1; /* Potential '-'-sign. */
648 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 if (buflen < n) {
652 *olen = n;
653 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
654 }
655
656 p = buf;
657 mbedtls_mpi_init(&T);
658
659 if (X->s == -1) {
660 *p++ = '-';
661 buflen--;
662 }
663
664 if (radix == 16) {
665 int c;
666 size_t i, j, k;
667
668 for (i = X->n, k = 0; i > 0; i--) {
669 for (j = ciL; j > 0; j--) {
670 c = (X->p[i - 1] >> ((j - 1) << 3)) & 0xFF;
671
672 if (c == 0 && k == 0 && (i + j) != 2) {
673 continue;
674 }
675
676 *(p++) = "0123456789ABCDEF" [c / 16];
677 *(p++) = "0123456789ABCDEF" [c % 16];
678 k = 1;
679 }
680 }
681 } else {
682 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T, X));
683
684 if (T.s == -1) {
685 T.s = 1;
686 }
687
688 MBEDTLS_MPI_CHK(mpi_write_hlp(&T, radix, &p, buflen));
689 }
690
691 *p++ = '\0';
692 *olen = (size_t) (p - buf);
693
694cleanup:
695
696 mbedtls_mpi_free(&T);
697
698 return ret;
699}
700
701#if defined(MBEDTLS_FS_IO)
702/*
703 * Read X from an opened file
704 */
705int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin)
706{
707 mbedtls_mpi_uint d;
708 size_t slen;
709 char *p;
710 /*
711 * Buffer should have space for (short) label and decimal formatted MPI,
712 * newline characters and '\0'
713 */
714 char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
715
716 if (radix < 2 || radix > 16) {
717 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
718 }
719
720 memset(s, 0, sizeof(s));
721 if (fgets(s, sizeof(s) - 1, fin) == NULL) {
722 return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
723 }
724
725 slen = strlen(s);
726 if (slen == sizeof(s) - 2) {
727 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
728 }
729
730 if (slen > 0 && s[slen - 1] == '\n') {
731 slen--; s[slen] = '\0';
732 }
733 if (slen > 0 && s[slen - 1] == '\r') {
734 slen--; s[slen] = '\0';
735 }
736
737 p = s + slen;
738 while (p-- > s) {
739 if (mpi_get_digit(&d, radix, *p) != 0) {
740 break;
741 }
742 }
743
744 return mbedtls_mpi_read_string(X, radix, p + 1);
745}
746
747/*
748 * Write X into an opened file (or stdout if fout == NULL)
749 */
750int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE *fout)
751{
752 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
753 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 char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
759
760 if (radix < 2 || radix > 16) {
761 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
762 }
763
764 memset(s, 0, sizeof(s));
765
766 MBEDTLS_MPI_CHK(mbedtls_mpi_write_string(X, radix, s, sizeof(s) - 2, &n));
767
768 if (p == NULL) {
769 p = "";
770 }
771
772 plen = strlen(p);
773 slen = strlen(s);
774 s[slen++] = '\r';
775 s[slen++] = '\n';
776
777 if (fout != NULL) {
778 if (fwrite(p, 1, plen, fout) != plen ||
779 fwrite(s, 1, slen, fout) != slen) {
780 return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
781 }
782 } else {
783 mbedtls_printf("%s%s", p, s);
784 }
785
786cleanup:
787
788 return ret;
789}
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 */
798int mbedtls_mpi_read_binary_le(mbedtls_mpi *X,
799 const unsigned char *buf, size_t buflen)
800{
801 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
802 const size_t limbs = CHARS_TO_LIMBS(buflen);
803
804 /* Ensure that target MPI has exactly the necessary number of limbs */
805 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
806
807 MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_le(X->p, X->n, buf, buflen));
808
809cleanup:
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 return ret;
817}
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 */
825int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
826{
827 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
828 const size_t limbs = CHARS_TO_LIMBS(buflen);
829
830 /* Ensure that target MPI has exactly the necessary number of limbs */
831 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
832
833 MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_be(X->p, X->n, buf, buflen));
834
835cleanup:
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 return ret;
843}
844
845/*
846 * Export X into unsigned binary data, little endian
847 */
848int mbedtls_mpi_write_binary_le(const mbedtls_mpi *X,
849 unsigned char *buf, size_t buflen)
850{
851 return mbedtls_mpi_core_write_le(X->p, X->n, buf, buflen);
852}
853
854/*
855 * Export X into unsigned binary data, big endian
856 */
857int mbedtls_mpi_write_binary(const mbedtls_mpi *X,
858 unsigned char *buf, size_t buflen)
859{
860 return mbedtls_mpi_core_write_be(X->p, X->n, buf, buflen);
861}
862
863/*
864 * Left-shift: X <<= count
865 */
866int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
867{
868 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
869 size_t i;
870
871 i = mbedtls_mpi_bitlen(X) + count;
872
873 if (X->n * biL < i) {
874 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, BITS_TO_LIMBS(i)));
875 }
876
877 ret = 0;
878
879 mbedtls_mpi_core_shift_l(X->p, X->n, count);
880cleanup:
881
882 return ret;
883}
884
885/*
886 * Right-shift: X >>= count
887 */
888int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
889{
890 if (X->n != 0) {
891 mbedtls_mpi_core_shift_r(X->p, X->n, count);
892 }
893 return 0;
894}
895
896/*
897 * Compare unsigned values
898 */
899int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
900{
901 size_t i, j;
902
903 for (i = X->n; i > 0; i--) {
904 if (X->p[i - 1] != 0) {
905 break;
906 }
907 }
908
909 for (j = Y->n; j > 0; j--) {
910 if (Y->p[j - 1] != 0) {
911 break;
912 }
913 }
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 if (i > j) {
919 return 1;
920 }
921 if (j > i) {
922 return -1;
923 }
924
925 for (; i > 0; i--) {
926 if (X->p[i - 1] > Y->p[i - 1]) {
927 return 1;
928 }
929 if (X->p[i - 1] < Y->p[i - 1]) {
930 return -1;
931 }
932 }
933
934 return 0;
935}
936
937/*
938 * Compare signed values
939 */
940int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
941{
942 size_t i, j;
943
944 for (i = X->n; i > 0; i--) {
945 if (X->p[i - 1] != 0) {
946 break;
947 }
948 }
949
950 for (j = Y->n; j > 0; j--) {
951 if (Y->p[j - 1] != 0) {
952 break;
953 }
954 }
955
956 if (i == 0 && j == 0) {
957 return 0;
958 }
959
960 if (i > j) {
961 return X->s;
962 }
963 if (j > i) {
964 return -Y->s;
965 }
966
967 if (X->s > 0 && Y->s < 0) {
968 return 1;
969 }
970 if (Y->s > 0 && X->s < 0) {
971 return -1;
972 }
973
974 for (; i > 0; i--) {
975 if (X->p[i - 1] > Y->p[i - 1]) {
976 return X->s;
977 }
978 if (X->p[i - 1] < Y->p[i - 1]) {
979 return -X->s;
980 }
981 }
982
983 return 0;
984}
985
986/*
987 * Compare signed values
988 */
989int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
990{
991 mbedtls_mpi Y;
992 mbedtls_mpi_uint p[1];
993
994 *p = mpi_sint_abs(z);
995 Y.s = TO_SIGN(z);
996 Y.n = 1;
997 Y.p = p;
998
999 return mbedtls_mpi_cmp_mpi(X, &Y);
1000}
1001
1002/*
1003 * Unsigned addition: X = |A| + |B| (HAC 14.7)
1004 */
1005int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
1006{
1007 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1008 size_t j;
1009 mbedtls_mpi_uint *p;
1010 mbedtls_mpi_uint c;
1011
1012 if (X == B) {
1013 const mbedtls_mpi *T = A; A = X; B = T;
1014 }
1015
1016 if (X != A) {
1017 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
1018 }
1019
1020 /*
1021 * X must always be positive as a result of unsigned additions.
1022 */
1023 X->s = 1;
1024
1025 for (j = B->n; j > 0; j--) {
1026 if (B->p[j - 1] != 0) {
1027 break;
1028 }
1029 }
1030
1031 /* Exit early to avoid undefined behavior on NULL+0 when X->n == 0
1032 * and B is 0 (of any size). */
1033 if (j == 0) {
1034 return 0;
1035 }
1036
1037 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 p = X->p;
1042
1043 c = mbedtls_mpi_core_add(p, p, B->p, j);
1044
1045 p += j;
1046
1047 /* Now propagate any carry */
1048
1049 while (c != 0) {
1050 if (j >= X->n) {
1051 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j + 1));
1052 p = X->p + j;
1053 }
1054
1055 *p += c; c = (*p < c); j++; p++;
1056 }
1057
1058cleanup:
1059
1060 return ret;
1061}
1062
1063/*
1064 * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
1065 */
1066int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
1067{
1068 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1069 size_t n;
1070 mbedtls_mpi_uint carry;
1071
1072 for (n = B->n; n > 0; n--) {
1073 if (B->p[n - 1] != 0) {
1074 break;
1075 }
1076 }
1077 if (n > A->n) {
1078 /* B >= (2^ciL)^n > A */
1079 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1080 goto cleanup;
1081 }
1082
1083 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 if (A->n > n && A != X) {
1089 memcpy(X->p + n, A->p + n, (A->n - n) * ciL);
1090 }
1091 if (X->n > A->n) {
1092 memset(X->p + A->n, 0, (X->n - A->n) * ciL);
1093 }
1094
1095 carry = mbedtls_mpi_core_sub(X->p, A->p, B->p, n);
1096 if (carry != 0) {
1097 /* Propagate the carry through the rest of X. */
1098 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 if (carry != 0) {
1102 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1103 goto cleanup;
1104 }
1105 }
1106
1107 /* X should always be positive as a result of unsigned subtractions. */
1108 X->s = 1;
1109
1110cleanup:
1111 return ret;
1112}
1113
1114/* Common function for signed addition and subtraction.
1115 * Calculate A + B * flip_B where flip_B is 1 or -1.
1116 */
1117static int add_sub_mpi(mbedtls_mpi *X,
1118 const mbedtls_mpi *A, const mbedtls_mpi *B,
1119 int flip_B)
1120{
1121 int ret, s;
1122
1123 s = A->s;
1124 if (A->s * B->s * flip_B < 0) {
1125 int cmp = mbedtls_mpi_cmp_abs(A, B);
1126 if (cmp >= 0) {
1127 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 X->s = cmp == 0 ? 1 : s;
1132 } else {
1133 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, B, A));
1134 /* Since |A| < |B|, the sign is the opposite of A. */
1135 X->s = -s;
1136 }
1137 } else {
1138 MBEDTLS_MPI_CHK(mbedtls_mpi_add_abs(X, A, B));
1139 X->s = s;
1140 }
1141
1142cleanup:
1143
1144 return ret;
1145}
1146
1147/*
1148 * Signed addition: X = A + B
1149 */
1150int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
1151{
1152 return add_sub_mpi(X, A, B, 1);
1153}
1154
1155/*
1156 * Signed subtraction: X = A - B
1157 */
1158int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
1159{
1160 return add_sub_mpi(X, A, B, -1);
1161}
1162
1163/*
1164 * Signed addition: X = A + b
1165 */
1166int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
1167{
1168 mbedtls_mpi B;
1169 mbedtls_mpi_uint p[1];
1170
1171 p[0] = mpi_sint_abs(b);
1172 B.s = TO_SIGN(b);
1173 B.n = 1;
1174 B.p = p;
1175
1176 return mbedtls_mpi_add_mpi(X, A, &B);
1177}
1178
1179/*
1180 * Signed subtraction: X = A - b
1181 */
1182int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
1183{
1184 mbedtls_mpi B;
1185 mbedtls_mpi_uint p[1];
1186
1187 p[0] = mpi_sint_abs(b);
1188 B.s = TO_SIGN(b);
1189 B.n = 1;
1190 B.p = p;
1191
1192 return mbedtls_mpi_sub_mpi(X, A, &B);
1193}
1194
1195/*
1196 * Baseline multiplication: X = A * B (HAC 14.12)
1197 */
1198int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
1199{
1200 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1201 size_t i, j;
1202 mbedtls_mpi TA, TB;
1203 int result_is_zero = 0;
1204
1205 mbedtls_mpi_init(&TA);
1206 mbedtls_mpi_init(&TB);
1207
1208 if (X == A) {
1209 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A)); A = &TA;
1210 }
1211 if (X == B) {
1212 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B)); B = &TB;
1213 }
1214
1215 for (i = A->n; i > 0; i--) {
1216 if (A->p[i - 1] != 0) {
1217 break;
1218 }
1219 }
1220 if (i == 0) {
1221 result_is_zero = 1;
1222 }
1223
1224 for (j = B->n; j > 0; j--) {
1225 if (B->p[j - 1] != 0) {
1226 break;
1227 }
1228 }
1229 if (j == 0) {
1230 result_is_zero = 1;
1231 }
1232
1233 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i + j));
1234 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
1235
1236 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 if (result_is_zero) {
1243 X->s = 1;
1244 } else {
1245 X->s = A->s * B->s;
1246 }
1247
1248cleanup:
1249
1250 mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TA);
1251
1252 return ret;
1253}
1254
1255/*
1256 * Baseline multiplication: X = A * b
1257 */
1258int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b)
1259{
1260 size_t n = A->n;
1261 while (n > 0 && A->p[n - 1] == 0) {
1262 --n;
1263 }
1264
1265 /* The general method below doesn't work if b==0. */
1266 if (b == 0 || n == 0) {
1267 return mbedtls_mpi_lset(X, 0);
1268 }
1269
1270 /* Calculate A*b as A + A*(b-1) to take advantage of mbedtls_mpi_core_mla */
1271 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 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n + 1));
1284 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
1285 mbedtls_mpi_core_mla(X->p, X->n, A->p, n, b - 1);
1286
1287cleanup:
1288 return ret;
1289}
1290
1291/*
1292 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1293 * mbedtls_mpi_uint divisor, d
1294 */
1295static 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{
1300#if defined(MBEDTLS_HAVE_UDBL)
1301 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 if (0 == d || u1 >= d) {
1314 if (r != NULL) {
1315 *r = ~(mbedtls_mpi_uint) 0u;
1316 }
1317
1318 return ~(mbedtls_mpi_uint) 0u;
1319 }
1320
1321#if defined(MBEDTLS_HAVE_UDBL)
1322 dividend = (mbedtls_t_udbl) u1 << biL;
1323 dividend |= (mbedtls_t_udbl) u0;
1324 quotient = dividend / d;
1325 if (quotient > ((mbedtls_t_udbl) 1 << biL) - 1) {
1326 quotient = ((mbedtls_t_udbl) 1 << biL) - 1;
1327 }
1328
1329 if (r != NULL) {
1330 *r = (mbedtls_mpi_uint) (dividend - (quotient * d));
1331 }
1332
1333 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}
1394
1395/*
1396 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
1397 */
1398int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1399 const mbedtls_mpi *B)
1400{
1401 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1402 size_t i, n, t, k;
1403 mbedtls_mpi X, Y, Z, T1, T2;
1404 mbedtls_mpi_uint TP2[3];
1405
1406 if (mbedtls_mpi_cmp_int(B, 0) == 0) {
1407 return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
1408 }
1409
1410 mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z);
1411 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 T2.s = 1;
1420 T2.n = sizeof(TP2) / sizeof(*TP2);
1421 T2.p = TP2;
1422
1423 if (mbedtls_mpi_cmp_abs(A, B) < 0) {
1424 if (Q != NULL) {
1425 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(Q, 0));
1426 }
1427 if (R != NULL) {
1428 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, A));
1429 }
1430 return 0;
1431 }
1432
1433 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&X, A));
1434 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, B));
1435 X.s = Y.s = 1;
1436
1437 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&Z, A->n + 2));
1438 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Z, 0));
1439 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T1, A->n + 2));
1440
1441 k = mbedtls_mpi_bitlen(&Y) % biL;
1442 if (k < biL - 1) {
1443 k = biL - 1 - k;
1444 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&X, k));
1445 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, k));
1446 } else {
1447 k = 0;
1448 }
1449
1450 n = X.n - 1;
1451 t = Y.n - 1;
1452 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, biL * (n - t)));
1453
1454 while (mbedtls_mpi_cmp_mpi(&X, &Y) >= 0) {
1455 Z.p[n - t]++;
1456 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &Y));
1457 }
1458 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, biL * (n - t)));
1459
1460 for (i = n; i > t; i--) {
1461 if (X.p[i] >= Y.p[t]) {
1462 Z.p[i - t - 1] = ~(mbedtls_mpi_uint) 0u;
1463 } else {
1464 Z.p[i - t - 1] = mbedtls_int_div_int(X.p[i], X.p[i - 1],
1465 Y.p[t], NULL);
1466 }
1467
1468 T2.p[0] = (i < 2) ? 0 : X.p[i - 2];
1469 T2.p[1] = (i < 1) ? 0 : X.p[i - 1];
1470 T2.p[2] = X.p[i];
1471
1472 Z.p[i - t - 1]++;
1473 do {
1474 Z.p[i - t - 1]--;
1475
1476 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&T1, 0));
1477 T1.p[0] = (t < 1) ? 0 : Y.p[t - 1];
1478 T1.p[1] = Y.p[t];
1479 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &T1, Z.p[i - t - 1]));
1480 } while (mbedtls_mpi_cmp_mpi(&T1, &T2) > 0);
1481
1482 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &Y, Z.p[i - t - 1]));
1483 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
1484 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &T1));
1485
1486 if (mbedtls_mpi_cmp_int(&X, 0) < 0) {
1487 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T1, &Y));
1488 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
1489 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&X, &X, &T1));
1490 Z.p[i - t - 1]--;
1491 }
1492 }
1493
1494 if (Q != NULL) {
1495 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(Q, &Z));
1496 Q->s = A->s * B->s;
1497 }
1498
1499 if (R != NULL) {
1500 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&X, k));
1501 X.s = A->s;
1502 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, &X));
1503
1504 if (mbedtls_mpi_cmp_int(R, 0) == 0) {
1505 R->s = 1;
1506 }
1507 }
1508
1509cleanup:
1510
1511 mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z);
1512 mbedtls_mpi_free(&T1);
1513 mbedtls_platform_zeroize(TP2, sizeof(TP2));
1514
1515 return ret;
1516}
1517
1518/*
1519 * Division by int: A = Q * b + R
1520 */
1521int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R,
1522 const mbedtls_mpi *A,
1523 mbedtls_mpi_sint b)
1524{
1525 mbedtls_mpi B;
1526 mbedtls_mpi_uint p[1];
1527
1528 p[0] = mpi_sint_abs(b);
1529 B.s = TO_SIGN(b);
1530 B.n = 1;
1531 B.p = p;
1532
1533 return mbedtls_mpi_div_mpi(Q, R, A, &B);
1534}
1535
1536/*
1537 * Modulo: R = A mod B
1538 */
1539int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
1540{
1541 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1542
1543 if (mbedtls_mpi_cmp_int(B, 0) < 0) {
1544 return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1545 }
1546
1547 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(NULL, R, A, B));
1548
1549 while (mbedtls_mpi_cmp_int(R, 0) < 0) {
1550 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(R, R, B));
1551 }
1552
1553 while (mbedtls_mpi_cmp_mpi(R, B) >= 0) {
1554 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(R, R, B));
1555 }
1556
1557cleanup:
1558
1559 return ret;
1560}
1561
1562/*
1563 * Modulo: r = A mod b
1564 */
1565int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b)
1566{
1567 size_t i;
1568 mbedtls_mpi_uint x, y, z;
1569
1570 if (b == 0) {
1571 return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
1572 }
1573
1574 if (b < 0) {
1575 return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1576 }
1577
1578 /*
1579 * handle trivial cases
1580 */
1581 if (b == 1 || A->n == 0) {
1582 *r = 0;
1583 return 0;
1584 }
1585
1586 if (b == 2) {
1587 *r = A->p[0] & 1;
1588 return 0;
1589 }
1590
1591 /*
1592 * general case
1593 */
1594 for (i = A->n, y = 0; i > 0; i--) {
1595 x = A->p[i - 1];
1596 y = (y << biH) | (x >> biH);
1597 z = y / b;
1598 y -= z * b;
1599
1600 x <<= biH;
1601 y = (y << biH) | (x >> biH);
1602 z = y / b;
1603 y -= z * b;
1604 }
1605
1606 /*
1607 * If A is negative, then the current y represents a negative value.
1608 * Flipping it to the positive side.
1609 */
1610 if (A->s < 0 && y != 0) {
1611 y = b - y;
1612 }
1613
1614 *r = y;
1615
1616 return 0;
1617}
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 */
1623static 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{
1627 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1628
1629 if (mbedtls_mpi_cmp_int(N, 0) <= 0 || (N->p[0] & 1) == 0) {
1630 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1631 }
1632
1633 if (mbedtls_mpi_cmp_int(E, 0) < 0) {
1634 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1635 }
1636
1637 if (mbedtls_mpi_bitlen(E) > MBEDTLS_MPI_MAX_BITS ||
1638 mbedtls_mpi_bitlen(N) > MBEDTLS_MPI_MAX_BITS) {
1639 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1640 }
1641
1642 /*
1643 * Ensure that the exponent that we are passing to the core is not NULL.
1644 */
1645 if (E->n == 0) {
1646 ret = mbedtls_mpi_lset(X, 1);
1647 return ret;
1648 }
1649
1650 /*
1651 * Allocate working memory for mbedtls_mpi_core_exp_mod()
1652 */
1653 size_t T_limbs = mbedtls_mpi_core_exp_mod_working_limbs(N->n, E->n);
1654 mbedtls_mpi_uint *T = (mbedtls_mpi_uint *) mbedtls_calloc(T_limbs, sizeof(mbedtls_mpi_uint));
1655 if (T == NULL) {
1656 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
1657 }
1658
1659 mbedtls_mpi RR;
1660 mbedtls_mpi_init(&RR);
1661
1662 /*
1663 * If 1st call, pre-compute R^2 mod N
1664 */
1665 if (prec_RR == NULL || prec_RR->p == NULL) {
1666 MBEDTLS_MPI_CHK(mbedtls_mpi_core_get_mont_r2_unsafe(&RR, N));
1667
1668 if (prec_RR != NULL) {
1669 *prec_RR = RR;
1670 }
1671 } else {
1672 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(prec_RR, N->n));
1673 RR = *prec_RR;
1674 }
1675
1676 /*
1677 * To preserve constness we need to make a copy of A. Using X for this to
1678 * save memory.
1679 */
1680 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
1681
1682 /*
1683 * Compensate for negative A (and correct at the end).
1684 */
1685 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 if (mbedtls_mpi_cmp_mpi(X, N) >= 0) {
1698 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(X, X, N));
1699 }
1700 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 {
1706 mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N->p);
1707 mbedtls_mpi_core_to_mont_rep(X->p, X->p, N->p, N->n, mm, RR.p, T);
1708 if (E_public == MBEDTLS_MPI_IS_PUBLIC) {
1709 mbedtls_mpi_core_exp_mod_unsafe(X->p, X->p, N->p, N->n, E->p, E->n, RR.p, T);
1710 } else {
1711 mbedtls_mpi_core_exp_mod(X->p, X->p, N->p, N->n, E->p, E->n, RR.p, T);
1712 }
1713 mbedtls_mpi_core_from_mont_rep(X->p, X->p, N->p, N->n, mm, T);
1714 }
1715
1716 /*
1717 * Correct for negative A.
1718 */
1719 if (A->s == -1 && (E->p[0] & 1) != 0) {
1720 mbedtls_ct_condition_t is_x_non_zero = mbedtls_mpi_core_check_zero_ct(X->p, X->n);
1721 X->s = mbedtls_ct_mpi_sign_if(is_x_non_zero, -1, 1);
1722
1723 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(X, N, X));
1724 }
1725
1726cleanup:
1727
1728 mbedtls_mpi_zeroize_and_free(T, T_limbs);
1729
1730 if (prec_RR == NULL || prec_RR->p == NULL) {
1731 mbedtls_mpi_free(&RR);
1732 }
1733
1734 return ret;
1735}
1736
1737int 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{
1741 return mbedtls_mpi_exp_mod_optionally_safe(X, A, E, MBEDTLS_MPI_IS_SECRET, N, prec_RR);
1742}
1743
1744int 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{
1748 return mbedtls_mpi_exp_mod_optionally_safe(X, A, E, MBEDTLS_MPI_IS_PUBLIC, N, prec_RR);
1749}
1750
1751/* Constant-time GCD and/or modinv with odd modulus and A <= N */
1752int mbedtls_mpi_gcd_modinv_odd(mbedtls_mpi *G,
1753 mbedtls_mpi *I,
1754 const mbedtls_mpi *A,
1755 const mbedtls_mpi *N)
1756{
1757 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1758 mbedtls_mpi local_g;
1759 mbedtls_mpi_uint *T = NULL;
1760 const size_t T_factor = I != NULL ? 5 : 4;
1761 const mbedtls_mpi_uint zero = 0;
1762
1763 /* Check requirements on A and N */
1764 if (mbedtls_mpi_cmp_int(A, 0) < 0 ||
1765 mbedtls_mpi_cmp_mpi(A, N) > 0 ||
1766 mbedtls_mpi_get_bit(N, 0) != 1 ||
1767 (I != NULL && mbedtls_mpi_cmp_int(N, 1) == 0)) {
1768 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1769 }
1770
1771 /* Check aliasing requirements */
1772 if (A == N || (I != NULL && (I == N || G == N))) {
1773 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1774 }
1775
1776 mbedtls_mpi_init(&local_g);
1777
1778 if (G == NULL) {
1779 G = &local_g;
1780 }
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 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(G, N->n));
1785 if (I != NULL) {
1786 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(I, N->n));
1787 }
1788
1789 T = mbedtls_calloc(sizeof(mbedtls_mpi_uint) * N->n, T_factor);
1790 if (T == NULL) {
1791 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1792 goto cleanup;
1793 }
1794
1795 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 const mbedtls_mpi_uint *Ap = A->p != NULL ? A->p : &zero;
1800 size_t An = A->n >= N->n ? N->n : A->p != NULL ? A->n : 1;
1801 mbedtls_mpi_core_gcd_modinv_odd(G->p, Ip, Ap, An, N->p, N->n, T);
1802
1803 G->s = 1;
1804 if (I != NULL) {
1805 I->s = 1;
1806 }
1807
1808 if (G->n > N->n) {
1809 memset(G->p + N->n, 0, ciL * (G->n - N->n));
1810 }
1811 if (I != NULL && I->n > N->n) {
1812 memset(I->p + N->n, 0, ciL * (I->n - N->n));
1813 }
1814
1815cleanup:
1816 mbedtls_mpi_free(&local_g);
1817 mbedtls_free(T);
1818 return ret;
1819}
1820
1821/*
1822 * Greatest common divisor: G = gcd(A, B)
1823 * Wrapper around mbedtls_mpi_gcd_modinv() that removes its restrictions.
1824 */
1825int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
1826{
1827 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1828 mbedtls_mpi TA, TB;
1829
1830 mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TB);
1831
1832 /* Make copies and take absolute values */
1833 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A));
1834 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B));
1835 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 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&TA, TB.n != 0 ? TB.n : 1));
1840 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 if (mbedtls_mpi_core_check_zero_ct(TA.p, TA.n) == MBEDTLS_CT_FALSE) {
1844 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(G, &TB)); // GCD(0, B) = abs(B)
1845 goto cleanup;
1846 }
1847 if (mbedtls_mpi_core_check_zero_ct(TB.p, TB.n) == MBEDTLS_CT_FALSE) {
1848 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(G, &TA)); // GCD(A, 0) = abs(A)
1849 goto cleanup;
1850 }
1851
1852 /* Make boths inputs odd by putting powers of 2 on the side */
1853 const size_t za = mbedtls_mpi_lsb(&TA);
1854 const size_t zb = mbedtls_mpi_lsb(&TB);
1855 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, za));
1856 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, zb));
1857
1858 /* Ensure A <= B: if B < A, swap them */
1859 mbedtls_ct_condition_t swap = mbedtls_mpi_core_lt_ct(TB.p, TA.p, TA.n);
1860 mbedtls_mpi_core_cond_swap(TA.p, TB.p, TA.n, swap);
1861
1862 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 size_t zg = za > zb ? zb : za; // zg = min(za, zb)
1866 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(G, zg));
1867
1868cleanup:
1869
1870 mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TB);
1871
1872 return ret;
1873}
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 */
1881int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
1882 int (*f_rng)(void *, unsigned char *, size_t),
1883 void *p_rng)
1884{
1885 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1886 const size_t limbs = CHARS_TO_LIMBS(size);
1887
1888 /* Ensure that target MPI has exactly the necessary number of limbs */
1889 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
1890 if (size == 0) {
1891 return 0;
1892 }
1893
1894 ret = mbedtls_mpi_core_fill_random(X->p, X->n, size, f_rng, p_rng);
1895
1896cleanup:
1897 return ret;
1898}
1899
1900int 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{
1906 if (min < 0) {
1907 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1908 }
1909 if (mbedtls_mpi_cmp_int(N, min) <= 0) {
1910 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1911 }
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 int ret = mbedtls_mpi_resize_clear(X, N->n);
1917 if (ret != 0) {
1918 return ret;
1919 }
1920
1921 return mbedtls_mpi_core_random(X->p, min, N->p, X->n, f_rng, p_rng);
1922}
1923
1924/*
1925 * Modular inverse: X = A^-1 mod N with N odd (and A any range)
1926 */
1927int mbedtls_mpi_inv_mod_odd(mbedtls_mpi *X,
1928 const mbedtls_mpi *A,
1929 const mbedtls_mpi *N)
1930{
1931 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1932 mbedtls_mpi T, G;
1933
1934 mbedtls_mpi_init(&T);
1935 mbedtls_mpi_init(&G);
1936
1937 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, A, N));
1938 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd_modinv_odd(&G, &T, &T, N));
1939 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
1940 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
1941 goto cleanup;
1942 }
1943
1944 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, &T));
1945
1946cleanup:
1947 mbedtls_mpi_free(&T);
1948 mbedtls_mpi_free(&G);
1949
1950 return ret;
1951}
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 */
1966int mbedtls_mpi_inv_mod_even_in_range(mbedtls_mpi *X,
1967 mbedtls_mpi const *A,
1968 mbedtls_mpi const *N)
1969{
1970 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1971 mbedtls_mpi I, G;
1972
1973 mbedtls_mpi_init(&I);
1974 mbedtls_mpi_init(&G);
1975
1976 /* Set I = N^-1 mod A */
1977 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&I, N, A));
1978 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd_modinv_odd(&G, &I, &I, A));
1979 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
1980 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
1981 goto cleanup;
1982 }
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 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&I, &I, N));
1987 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&I, &I, 1));
1988 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 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(X, N, &G));
1994
1995cleanup:
1996 mbedtls_mpi_free(&I);
1997 mbedtls_mpi_free(&G);
1998 return ret;
1999}
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 */
2006static int mbedtls_mpi_inv_mod_even(mbedtls_mpi *X,
2007 mbedtls_mpi const *A,
2008 mbedtls_mpi const *N)
2009{
2010 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2011 mbedtls_mpi AA;
2012
2013 mbedtls_mpi_init(&AA);
2014
2015 /* Bring A in the range [0, N). */
2016 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&AA, A, N));
2017
2018 /* We know A >= 0 but the next function wants A > 1 */
2019 int cmp = mbedtls_mpi_cmp_int(&AA, 1);
2020 if (cmp < 0) { // AA == 0
2021 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2022 goto cleanup;
2023 }
2024 if (cmp == 0) { // AA = 1
2025 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 1));
2026 goto cleanup;
2027 }
2028
2029 /* Now we know 1 < A < N, N is even and AA is still odd */
2030 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod_even_in_range(X, &AA, N));
2031
2032cleanup:
2033 mbedtls_mpi_free(&AA);
2034 return ret;
2035}
2036
2037/*
2038 * Modular inverse: X = A^-1 mod N
2039 *
2040 * Wrapper around mbedtls_mpi_gcd_modinv_odd() that lifts its limitations.
2041 */
2042int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
2043{
2044 if (mbedtls_mpi_cmp_int(N, 1) <= 0) {
2045 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2046 }
2047
2048 if (mbedtls_mpi_get_bit(N, 0) == 1) {
2049 return mbedtls_mpi_inv_mod_odd(X, A, N);
2050 }
2051
2052 if (mbedtls_mpi_get_bit(A, 0) == 1) {
2053 return mbedtls_mpi_inv_mod_even(X, A, N);
2054 }
2055
2056 /* If A and N are both even, 2 divides their GCD, so no inverse. */
2057 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2058}
2059
2060#if defined(MBEDTLS_GENPRIME)
2061
2062static const mbedtls_mpi_sint small_primes_limit = 997;
2063/* Product of small primes up to small_primes_limit included */
2064static const mbedtls_mpi_uint small_primes_product_limbs[] = {
2065 MBEDTLS_BYTES_TO_T_UINT_8(0x4b, 0x13, 0x6a, 0x97, 0xbb, 0xd0, 0xdf, 0x95),
2066 MBEDTLS_BYTES_TO_T_UINT_8(0xa7, 0x2c, 0x10, 0xa4, 0x20, 0xa4, 0x9f, 0x7b),
2067 MBEDTLS_BYTES_TO_T_UINT_8(0x9d, 0x18, 0xd6, 0xdf, 0xc0, 0xf5, 0x61, 0x65),
2068 MBEDTLS_BYTES_TO_T_UINT_8(0xfc, 0x35, 0x79, 0xfb, 0x30, 0xa8, 0xd5, 0xbf),
2069 MBEDTLS_BYTES_TO_T_UINT_8(0xdb, 0x37, 0xba, 0x2c, 0xfb, 0xbb, 0x89, 0xfb),
2070 MBEDTLS_BYTES_TO_T_UINT_8(0xad, 0xc2, 0x8c, 0x1d, 0x99, 0x18, 0xe8, 0xe5),
2071 MBEDTLS_BYTES_TO_T_UINT_8(0x8d, 0x77, 0xc9, 0x5d, 0x96, 0x8a, 0x61, 0x9d),
2072 MBEDTLS_BYTES_TO_T_UINT_8(0x39, 0x41, 0x3e, 0xf4, 0x34, 0x07, 0x57, 0xe0),
2073 MBEDTLS_BYTES_TO_T_UINT_8(0x4a, 0xf1, 0x54, 0x3a, 0x43, 0x67, 0x46, 0xa2),
2074 MBEDTLS_BYTES_TO_T_UINT_8(0x83, 0x0c, 0xe5, 0x31, 0xa2, 0xfc, 0x05, 0x45),
2075 MBEDTLS_BYTES_TO_T_UINT_8(0xf0, 0x1d, 0x66, 0xfc, 0x7a, 0x85, 0x37, 0xe1),
2076 MBEDTLS_BYTES_TO_T_UINT_8(0x17, 0xe0, 0x80, 0x62, 0x0d, 0xa2, 0xbc, 0x32),
2077 MBEDTLS_BYTES_TO_T_UINT_8(0x6d, 0xce, 0x84, 0x68, 0x00, 0xb5, 0xe3, 0x35),
2078 MBEDTLS_BYTES_TO_T_UINT_8(0x14, 0x19, 0x0d, 0xe4, 0x92, 0xd5, 0xd8, 0xdf),
2079 MBEDTLS_BYTES_TO_T_UINT_8(0x20, 0x1c, 0x7d, 0x38, 0x3b, 0xe8, 0xd9, 0xa8),
2080 MBEDTLS_BYTES_TO_T_UINT_8(0xf6, 0xac, 0x11, 0xe6, 0xb4, 0x03, 0xf7, 0x6c),
2081 MBEDTLS_BYTES_TO_T_UINT_8(0x78, 0x3d, 0xf2, 0x3a, 0x8f, 0xf9, 0x2f, 0x6a),
2082 MBEDTLS_BYTES_TO_T_UINT_8(0x7b, 0x72, 0x66, 0xa9, 0x48, 0xe4, 0x6d, 0x02),
2083 MBEDTLS_BYTES_TO_T_UINT_8(0x69, 0xc7, 0x32, 0xcb, 0xf2, 0xf7, 0xa9, 0x0b),
2084 MBEDTLS_BYTES_TO_T_UINT_8(0xd3, 0x52, 0x72, 0x9d, 0xbf, 0x54, 0xea, 0xc7),
2085 MBEDTLS_BYTES_TO_T_UINT_8(0xf5, 0xf3, 0x8a, 0xc5, 0xf0, 0xe1, 0x21, 0x81),
2086 MBEDTLS_BYTES_TO_T_UINT_8(0x8e, 0x61, 0x78, 0xf0, 0x05, 0x00, 0x00, 0x00),
2087};
2088/* Could make ECP_MPI_INIT_ARRAY() available outside ecp, but not doing it now
2089 * as it would lead to conflicts with other in-flight PRs. */
2090static const mbedtls_mpi small_primes_product = {
2091 .p = (mbedtls_mpi_uint *) small_primes_product_limbs,
2092 .s = 1,
2093 .n = sizeof(small_primes_product_limbs) / sizeof(mbedtls_mpi_uint),
2094};
2095
2096/*
2097 * Small divisors test (X must be positive)
2098 *
2099 * Return values:
2100 * 0: no small factor (possible prime, more tests needed)
2101 * 1: certain prime
2102 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
2103 * other negative: error
2104 */
2105static int mpi_check_small_factors(const mbedtls_mpi *X)
2106{
2107 int ret = 0;
2108 mbedtls_mpi g;
2109
2110 mbedtls_mpi_init(&g);
2111
2112 if ((X->p[0] & 1) == 0) {
2113 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2114 }
2115
2116 /* The GCD test below only works if X > small_primes_limit.
2117 * Below this limit, use trial division: numbers that small are of no
2118 * interest for cryptography, so we don't care about performance or side
2119 * channels. We're supporting them only for backwards compatibility, so
2120 * let's not waste code size on those. */
2121 if (mbedtls_mpi_cmp_int(X, small_primes_limit) <= 0) {
2122 mbedtls_mpi_uint x = X->p[0];
2123 mbedtls_mpi_uint d = 2;
2124 while (x % d != 0) {
2125 ++d;
2126 }
2127 return x == d ? 1 : MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2128 }
2129
2130 /* We can't directly use mbedtls_mpi_gcd_modinv_odd() because we don't know
2131 * if X is larger than prod or not (prod is 1380 bits). So, use this generic
2132 * wrapper - it does a bit more than what we need (handles even inputs as
2133 * well, while we know our inputs are both odd), but that's OK. */
2134 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&g, &small_primes_product, X));
2135
2136 if (mbedtls_mpi_cmp_int(&g, 1) == 0) {
2137 /* X is not divisible by a small prime */
2138 ret = 0;
2139 } else {
2140 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2141 }
2142
2143cleanup:
2144 mbedtls_mpi_free(&g);
2145 return ret;
2146}
2147
2148/*
2149 * Miller-Rabin pseudo-primality test (HAC 4.24)
2150 */
2151static int mpi_miller_rabin(const mbedtls_mpi *X, size_t rounds,
2152 int (*f_rng)(void *, unsigned char *, size_t),
2153 void *p_rng)
2154{
2155 int ret, count;
2156 size_t i, j, k, s;
2157 mbedtls_mpi W, R, T, A, RR;
2158
2159 mbedtls_mpi_init(&W); mbedtls_mpi_init(&R);
2160 mbedtls_mpi_init(&T); mbedtls_mpi_init(&A);
2161 mbedtls_mpi_init(&RR);
2162
2163 /*
2164 * W = |X| - 1
2165 * R = W >> lsb( W )
2166 */
2167 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&W, X, 1));
2168 s = mbedtls_mpi_lsb(&W);
2169 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R, &W));
2170 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&R, s));
2171
2172 for (i = 0; i < rounds; i++) {
2173 /*
2174 * pick a random A, 1 < A < |X| - 1
2175 */
2176 count = 0;
2177 do {
2178 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&A, X->n * ciL, f_rng, p_rng));
2179
2180 j = mbedtls_mpi_bitlen(&A);
2181 k = mbedtls_mpi_bitlen(&W);
2182 if (j > k) {
2183 A.p[A.n - 1] &= ((mbedtls_mpi_uint) 1 << (k - (A.n - 1) * biL - 1)) - 1;
2184 }
2185
2186 if (count++ > 30) {
2187 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2188 goto cleanup;
2189 }
2190
2191 } while (mbedtls_mpi_cmp_mpi(&A, &W) >= 0 ||
2192 mbedtls_mpi_cmp_int(&A, 1) <= 0);
2193
2194 /*
2195 * A = A^R mod |X|
2196 */
2197 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&A, &A, &R, X, &RR));
2198
2199 if (mbedtls_mpi_cmp_mpi(&A, &W) == 0 ||
2200 mbedtls_mpi_cmp_int(&A, 1) == 0) {
2201 continue;
2202 }
2203
2204 j = 1;
2205 while (j < s && mbedtls_mpi_cmp_mpi(&A, &W) != 0) {
2206 /*
2207 * A = A * A mod |X|
2208 */
2209 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &A, &A));
2210 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&A, &T, X));
2211
2212 if (mbedtls_mpi_cmp_int(&A, 1) == 0) {
2213 break;
2214 }
2215
2216 j++;
2217 }
2218
2219 /*
2220 * not prime if A != |X| - 1 or A == 1
2221 */
2222 if (mbedtls_mpi_cmp_mpi(&A, &W) != 0 ||
2223 mbedtls_mpi_cmp_int(&A, 1) == 0) {
2224 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2225 break;
2226 }
2227 }
2228
2229cleanup:
2230 mbedtls_mpi_free(&W); mbedtls_mpi_free(&R);
2231 mbedtls_mpi_free(&T); mbedtls_mpi_free(&A);
2232 mbedtls_mpi_free(&RR);
2233
2234 return ret;
2235}
2236
2237/*
2238 * Pseudo-primality test: small factors, then Miller-Rabin
2239 */
2240int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
2241 int (*f_rng)(void *, unsigned char *, size_t),
2242 void *p_rng)
2243{
2244 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2245 mbedtls_mpi XX;
2246
2247 XX.s = 1;
2248 XX.n = X->n;
2249 XX.p = X->p;
2250
2251 if (mbedtls_mpi_cmp_int(&XX, 0) == 0 ||
2252 mbedtls_mpi_cmp_int(&XX, 1) == 0) {
2253 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2254 }
2255
2256 if (mbedtls_mpi_cmp_int(&XX, 2) == 0) {
2257 return 0;
2258 }
2259
2260 if ((ret = mpi_check_small_factors(&XX)) != 0) {
2261 if (ret == 1) {
2262 return 0;
2263 }
2264
2265 return ret;
2266 }
2267
2268 return mpi_miller_rabin(&XX, rounds, f_rng, p_rng);
2269}
2270
2271/*
2272 * Prime number generation
2273 *
2274 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2275 * be either 1024 bits or 1536 bits long, and flags must contain
2276 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
2277 */
2278int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
2279 int (*f_rng)(void *, unsigned char *, size_t),
2280 void *p_rng)
2281{
2282#ifdef MBEDTLS_HAVE_INT64
2283// ceil(2^63.5)
2284#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2285#else
2286// ceil(2^31.5)
2287#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2288#endif
2289 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2290 size_t k, n;
2291 int rounds;
2292 mbedtls_mpi_uint r;
2293 mbedtls_mpi Y;
2294
2295 if (nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS) {
2296 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2297 }
2298
2299 mbedtls_mpi_init(&Y);
2300
2301 n = BITS_TO_LIMBS(nbits);
2302
2303 if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR) == 0) {
2304 /*
2305 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2306 */
2307 rounds = ((nbits >= 1300) ? 2 : (nbits >= 850) ? 3 :
2308 (nbits >= 650) ? 4 : (nbits >= 350) ? 8 :
2309 (nbits >= 250) ? 12 : (nbits >= 150) ? 18 : 27);
2310 } else {
2311 /*
2312 * 2^-100 error probability, number of rounds computed based on HAC,
2313 * fact 4.48
2314 */
2315 rounds = ((nbits >= 1450) ? 4 : (nbits >= 1150) ? 5 :
2316 (nbits >= 1000) ? 6 : (nbits >= 850) ? 7 :
2317 (nbits >= 750) ? 8 : (nbits >= 500) ? 13 :
2318 (nbits >= 250) ? 28 : (nbits >= 150) ? 40 : 51);
2319 }
2320
2321 while (1) {
2322 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(X, n * ciL, f_rng, p_rng));
2323 /* make sure generated number is at least (nbits-1)+0.5 bits (FIPS 186-4 §B.3.3 steps 4.4, 5.5) */
2324 if (X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2) {
2325 continue;
2326 }
2327
2328 k = n * biL;
2329 if (k > nbits) {
2330 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(X, k - nbits));
2331 }
2332 X->p[0] |= 1;
2333
2334 if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH) == 0) {
2335 ret = mbedtls_mpi_is_prime_ext(X, rounds, f_rng, p_rng);
2336
2337 if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
2338 goto cleanup;
2339 }
2340 } else {
2341 /*
2342 * A necessary condition for Y and X = 2Y + 1 to be prime
2343 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2344 * Make sure it is satisfied, while keeping X = 3 mod 4
2345 */
2346
2347 X->p[0] |= 2;
2348
2349 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, 3));
2350 if (r == 0) {
2351 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 8));
2352 } else if (r == 1) {
2353 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 4));
2354 }
2355
2356 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
2357 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, X));
2358 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, 1));
2359
2360 while (1) {
2361 /*
2362 * First, check small factors for X and Y
2363 * before doing Miller-Rabin on any of them
2364 */
2365 if ((ret = mpi_check_small_factors(X)) == 0 &&
2366 (ret = mpi_check_small_factors(&Y)) == 0 &&
2367 (ret = mpi_miller_rabin(X, rounds, f_rng, p_rng))
2368 == 0 &&
2369 (ret = mpi_miller_rabin(&Y, rounds, f_rng, p_rng))
2370 == 0) {
2371 goto cleanup;
2372 }
2373
2374 if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
2375 goto cleanup;
2376 }
2377
2378 /*
2379 * Next candidates. We want to preserve Y = (X-1) / 2 and
2380 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2381 * so up Y by 6 and X by 12.
2382 */
2383 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 12));
2384 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&Y, &Y, 6));
2385 }
2386 }
2387 }
2388
2389cleanup:
2390
2391 mbedtls_mpi_free(&Y);
2392
2393 return ret;
2394}
2395
2396#endif /* MBEDTLS_GENPRIME */
2397
2398#if defined(MBEDTLS_SELF_TEST)
2399
2400#define GCD_PAIR_COUNT 3
2401
2402static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2403{
2404 { 693, 609, 21 },
2405 { 1764, 868, 28 },
2406 { 768454923, 542167814, 1 }
2407};
2408
2409/*
2410 * Checkup routine
2411 */
2412int mbedtls_mpi_self_test(int verbose)
2413{
2414 int ret, i;
2415 mbedtls_mpi A, E, N, X, Y, U, V;
2416
2417 mbedtls_mpi_init(&A); mbedtls_mpi_init(&E); mbedtls_mpi_init(&N); mbedtls_mpi_init(&X);
2418 mbedtls_mpi_init(&Y); mbedtls_mpi_init(&U); mbedtls_mpi_init(&V);
2419
2420 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&A, 16,
2421 "EFE021C2645FD1DC586E69184AF4A31E" \
2422 "D5F53E93B5F123FA41680867BA110131" \
2423 "944FE7952E2517337780CB0DB80E61AA" \
2424 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6"));
2425
2426 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&E, 16,
2427 "B2E7EFD37075B9F03FF989C7C5051C20" \
2428 "34D2A323810251127E7BF8625A4F49A5" \
2429 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
2430 "5B5C25763222FEFCCFC38B832366C29E"));
2431
2432 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&N, 16,
2433 "0066A198186C18C10B2F5ED9B522752A" \
2434 "9830B69916E535C8F047518A889A43A5" \
2435 "94B6BED27A168D31D4A52F88925AA8F5"));
2436
2437 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&X, &A, &N));
2438
2439 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2440 "602AB7ECA597A3D6B56FF9829A5E8B85" \
2441 "9E857EA95A03512E2BAE7391688D264A" \
2442 "A5663B0341DB9CCFD2C4C5F421FEC814" \
2443 "8001B72E848A38CAE1C65F78E56ABDEF" \
2444 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
2445 "ECF677152EF804370C1A305CAF3B5BF1" \
2446 "30879B56C61DE584A0F53A2447A51E"));
2447
2448 if (verbose != 0) {
2449 mbedtls_printf(" MPI test #1 (mul_mpi): ");
2450 }
2451
2452 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2453 if (verbose != 0) {
2454 mbedtls_printf("failed\n");
2455 }
2456
2457 ret = 1;
2458 goto cleanup;
2459 }
2460
2461 if (verbose != 0) {
2462 mbedtls_printf("passed\n");
2463 }
2464
2465 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&X, &Y, &A, &N));
2466
2467 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2468 "256567336059E52CAE22925474705F39A94"));
2469
2470 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&V, 16,
2471 "6613F26162223DF488E9CD48CC132C7A" \
2472 "0AC93C701B001B092E4E5B9F73BCD27B" \
2473 "9EE50D0657C77F374E903CDFA4C642"));
2474
2475 if (verbose != 0) {
2476 mbedtls_printf(" MPI test #2 (div_mpi): ");
2477 }
2478
2479 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0 ||
2480 mbedtls_mpi_cmp_mpi(&Y, &V) != 0) {
2481 if (verbose != 0) {
2482 mbedtls_printf("failed\n");
2483 }
2484
2485 ret = 1;
2486 goto cleanup;
2487 }
2488
2489 if (verbose != 0) {
2490 mbedtls_printf("passed\n");
2491 }
2492
2493 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&X, &A, &E, &N, NULL));
2494
2495 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2496 "36E139AEA55215609D2816998ED020BB" \
2497 "BD96C37890F65171D948E9BC7CBAA4D9" \
2498 "325D24D6A3C12710F10A09FA08AB87"));
2499
2500 if (verbose != 0) {
2501 mbedtls_printf(" MPI test #3 (exp_mod): ");
2502 }
2503
2504 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2505 if (verbose != 0) {
2506 mbedtls_printf("failed\n");
2507 }
2508
2509 ret = 1;
2510 goto cleanup;
2511 }
2512
2513 if (verbose != 0) {
2514 mbedtls_printf("passed\n");
2515 }
2516
2517 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&X, &A, &N));
2518
2519 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2520 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
2521 "C3DBA76456363A10869622EAC2DD84EC" \
2522 "C5B8A74DAC4D09E03B5E0BE779F2DF61"));
2523
2524 if (verbose != 0) {
2525 mbedtls_printf(" MPI test #4 (inv_mod): ");
2526 }
2527
2528 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2529 if (verbose != 0) {
2530 mbedtls_printf("failed\n");
2531 }
2532
2533 ret = 1;
2534 goto cleanup;
2535 }
2536
2537 if (verbose != 0) {
2538 mbedtls_printf("passed\n");
2539 }
2540
2541 if (verbose != 0) {
2542 mbedtls_printf(" MPI test #5 (simple gcd): ");
2543 }
2544
2545 for (i = 0; i < GCD_PAIR_COUNT; i++) {
2546 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&X, gcd_pairs[i][0]));
2547 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Y, gcd_pairs[i][1]));
2548
2549 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&A, &X, &Y));
2550
2551 if (mbedtls_mpi_cmp_int(&A, gcd_pairs[i][2]) != 0) {
2552 if (verbose != 0) {
2553 mbedtls_printf("failed at %d\n", i);
2554 }
2555
2556 ret = 1;
2557 goto cleanup;
2558 }
2559 }
2560
2561 if (verbose != 0) {
2562 mbedtls_printf("passed\n");
2563 }
2564
2565cleanup:
2566
2567 if (ret != 0 && verbose != 0) {
2568 mbedtls_printf("Unexpected error, return code = %08X\n", (unsigned int) ret);
2569 }
2570
2571 mbedtls_mpi_free(&A); mbedtls_mpi_free(&E); mbedtls_mpi_free(&N); mbedtls_mpi_free(&X);
2572 mbedtls_mpi_free(&Y); mbedtls_mpi_free(&U); mbedtls_mpi_free(&V);
2573
2574 if (verbose != 0) {
2575 mbedtls_printf("\n");
2576 }
2577
2578 return ret;
2579}
2580
2581#endif /* MBEDTLS_SELF_TEST */
2582
2583#endif /* MBEDTLS_BIGNUM_C */