blob: 1869137c4dc0ebc7fe095e52ea78af48cb927047 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Multi-precision integer library
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
Simon Butcher15b15d12015-11-26 19:35:03 +00007
Paul Bakker5121ce52009-01-03 21:22:43 +00008/*
Simon Butcher15b15d12015-11-26 19:35:03 +00009 * The following sources were referenced in the design of this Multi-precision
10 * Integer library:
Paul Bakker5121ce52009-01-03 21:22:43 +000011 *
Simon Butcher15b15d12015-11-26 19:35:03 +000012 * [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 *
Simon Butcherf5ba0452015-12-27 23:01:55 +000022 */
Paul Bakker5121ce52009-01-03 21:22:43 +000023
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/bignum.h"
Janos Follath4614b9a2022-07-21 15:34:47 +010029#include "bignum_core.h"
Chris Jones4c5819c2021-03-03 17:45:34 +000030#include "bn_mul.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050031#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000032#include "mbedtls/error.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020033#include "constant_time_internal.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Dave Rodgman351c71b2021-12-06 17:50:53 +000035#include <limits.h>
Rich Evans00ab4702015-02-06 13:43:58 +000036#include <string.h>
37
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020039
Gilles Peskine449bd832023-01-11 14:50:10 +010040#define MPI_VALIDATE_RET(cond) \
41 MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA)
42#define MPI_VALIDATE(cond) \
43 MBEDTLS_INTERNAL_VALIDATE(cond)
Gabor Mezei66669142022-08-03 12:52:26 +020044
Dave Rodgman7d4f0192023-05-09 14:01:05 +010045/*
46 * Compare signed values in constant time
47 */
48int mbedtls_mpi_lt_mpi_ct(const mbedtls_mpi *X,
49 const mbedtls_mpi *Y,
50 unsigned *ret)
51{
Dave Rodgman1f39f032023-08-01 09:19:16 +010052 mbedtls_ct_condition_t different_sign, X_is_negative, Y_is_negative, result;
Dave Rodgman7d4f0192023-05-09 14:01:05 +010053
54 MPI_VALIDATE_RET(X != NULL);
55 MPI_VALIDATE_RET(Y != NULL);
56 MPI_VALIDATE_RET(ret != NULL);
57
58 if (X->n != Y->n) {
59 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
60 }
61
62 /*
Dave Rodgmanb69239c2023-08-09 14:53:18 +010063 * Set N_is_negative to MBEDTLS_CT_FALSE if N >= 0, MBEDTLS_CT_TRUE if N < 0.
Dave Rodgman7d4f0192023-05-09 14:01:05 +010064 * We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0.
65 */
Dave Rodgman1a7a5622023-05-17 13:47:56 +010066 X_is_negative = mbedtls_ct_bool((X->s & 2) >> 1);
67 Y_is_negative = mbedtls_ct_bool((Y->s & 2) >> 1);
Dave Rodgman7d4f0192023-05-09 14:01:05 +010068
69 /*
70 * If the signs are different, then the positive operand is the bigger.
71 * That is if X is negative (X_is_negative == 1), then X < Y is true and it
72 * is false if X is positive (X_is_negative == 0).
73 */
Dave Rodgman1cfc43c2023-09-19 16:18:59 +010074 different_sign = mbedtls_ct_bool_ne(X_is_negative, Y_is_negative); // true if different sign
Dave Rodgman1f39f032023-08-01 09:19:16 +010075 result = mbedtls_ct_bool_and(different_sign, X_is_negative);
Dave Rodgman7d4f0192023-05-09 14:01:05 +010076
Dave Rodgman32d72602023-07-31 12:28:05 +010077 /*
78 * Assuming signs are the same, compare X and Y. We switch the comparison
Dave Rodgman1a7a5622023-05-17 13:47:56 +010079 * order if they are negative so that we get the right result, regardles of
80 * sign.
Dave Rodgman7d4f0192023-05-09 14:01:05 +010081 */
Dave Rodgman7d4f0192023-05-09 14:01:05 +010082
Dave Rodgman32d72602023-07-31 12:28:05 +010083 /* This array is used to conditionally swap the pointers in const time */
Dave Rodgman1a7a5622023-05-17 13:47:56 +010084 void * const p[2] = { X->p, Y->p };
Dave Rodgman98ddc012023-08-10 12:11:31 +010085 size_t i = mbedtls_ct_size_if_else_0(X_is_negative, 1);
Dave Rodgman2c764842023-05-18 13:28:21 +010086 mbedtls_ct_condition_t lt = mbedtls_mpi_core_lt_ct(p[i], p[i ^ 1], X->n);
Dave Rodgman7d4f0192023-05-09 14:01:05 +010087
Dave Rodgman32d72602023-07-31 12:28:05 +010088 /*
Dave Rodgman1f39f032023-08-01 09:19:16 +010089 * Store in result iff the signs are the same (i.e., iff different_sign == false). If
Dave Rodgman32d72602023-07-31 12:28:05 +010090 * the signs differ, result has already been set, so we don't change it.
91 */
Dave Rodgman1f39f032023-08-01 09:19:16 +010092 result = mbedtls_ct_bool_or(result,
93 mbedtls_ct_bool_and(mbedtls_ct_bool_not(different_sign), lt));
Dave Rodgman1a7a5622023-05-17 13:47:56 +010094
Dave Rodgman98ddc012023-08-10 12:11:31 +010095 *ret = mbedtls_ct_uint_if_else_0(result, 1);
Dave Rodgman7d4f0192023-05-09 14:01:05 +010096
97 return 0;
98}
99
100/*
101 * Conditionally assign X = Y, without leaking information
102 * about whether the assignment was made or not.
103 * (Leaking information about the respective sizes of X and Y is ok however.)
104 */
Dave Rodgman0a487172023-09-15 11:52:06 +0100105#if defined(_MSC_VER) && defined(MBEDTLS_PLATFORM_IS_WINDOWS_ON_ARM64) && \
106 (_MSC_FULL_VER < 193131103)
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100107/*
108 * MSVC miscompiles this function if it's inlined prior to Visual Studio 2022 version 17.1. See:
109 * https://developercommunity.visualstudio.com/t/c-compiler-miscompiles-part-of-mbedtls-library-on/1646989
110 */
111__declspec(noinline)
112#endif
113int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X,
114 const mbedtls_mpi *Y,
115 unsigned char assign)
116{
117 int ret = 0;
118 MPI_VALIDATE_RET(X != NULL);
119 MPI_VALIDATE_RET(Y != NULL);
120
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100121 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, Y->n));
122
Dave Rodgman7e9af052023-09-28 17:08:16 +0100123 {
124 mbedtls_ct_condition_t do_assign = mbedtls_ct_bool(assign);
Dave Rodgman589ccb82023-05-17 13:55:01 +0100125
Dave Rodgman7e9af052023-09-28 17:08:16 +0100126 X->s = (int) mbedtls_ct_uint_if(do_assign, Y->s, X->s);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100127
Dave Rodgman7e9af052023-09-28 17:08:16 +0100128 mbedtls_mpi_core_cond_assign(X->p, Y->p, Y->n, do_assign);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100129
Dave Rodgman7e9af052023-09-28 17:08:16 +0100130 mbedtls_ct_condition_t do_not_assign = mbedtls_ct_bool_not(do_assign);
131 for (size_t i = Y->n; i < X->n; i++) {
132 X->p[i] = mbedtls_ct_mpi_uint_if_else_0(do_not_assign, X->p[i]);
133 }
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100134 }
135
136cleanup:
137 return ret;
138}
139
140/*
141 * Conditionally swap X and Y, without leaking information
142 * about whether the swap was made or not.
143 * Here it is not ok to simply swap the pointers, which would lead to
144 * different memory access patterns when X and Y are used afterwards.
145 */
146int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X,
147 mbedtls_mpi *Y,
148 unsigned char swap)
149{
150 int ret = 0;
151 int s;
152 MPI_VALIDATE_RET(X != NULL);
153 MPI_VALIDATE_RET(Y != NULL);
154
155 if (X == Y) {
156 return 0;
157 }
158
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100159 mbedtls_ct_condition_t do_swap = mbedtls_ct_bool(swap);
160
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100161 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, Y->n));
162 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Y, X->n));
163
164 s = X->s;
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100165 X->s = (int) mbedtls_ct_uint_if(do_swap, Y->s, X->s);
166 Y->s = (int) mbedtls_ct_uint_if(do_swap, s, Y->s);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100167
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100168 mbedtls_mpi_core_cond_swap(X->p, Y->p, X->n, do_swap);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100169
170cleanup:
171 return ret;
172}
173
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500174/* Implementation that should never be optimized out by the compiler */
Tom Cosgrovebc345e82023-07-25 15:17:39 +0100175#define mbedtls_mpi_zeroize_and_free(v, n) mbedtls_zeroize_and_free(v, ciL * (n))
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500176
Paul Bakker5121ce52009-01-03 21:22:43 +0000177/*
Paul Bakker6c591fa2011-05-05 11:49:20 +0000178 * Initialize one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180void mbedtls_mpi_init(mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000181{
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 MPI_VALIDATE(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
Paul Bakker6c591fa2011-05-05 11:49:20 +0000184 X->s = 1;
185 X->n = 0;
186 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000187}
188
189/*
Paul Bakker6c591fa2011-05-05 11:49:20 +0000190 * Unallocate one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192void mbedtls_mpi_free(mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000193{
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 if (X == NULL) {
Paul Bakker6c591fa2011-05-05 11:49:20 +0000195 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if (X->p != NULL) {
Tom Cosgrove46259f62023-07-18 16:44:14 +0100199 mbedtls_mpi_zeroize_and_free(X->p, X->n);
Paul Bakker5121ce52009-01-03 21:22:43 +0000200 }
201
Paul Bakker6c591fa2011-05-05 11:49:20 +0000202 X->s = 1;
203 X->n = 0;
204 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000205}
206
207/*
208 * Enlarge to the specified number of limbs
209 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100210int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs)
Paul Bakker5121ce52009-01-03 21:22:43 +0000211{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 mbedtls_mpi_uint *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
216 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
217 }
Paul Bakkerf9688572011-05-05 10:00:45 +0000218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 if (X->n < nblimbs) {
220 if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(nblimbs, ciL)) == NULL) {
221 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
222 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 if (X->p != NULL) {
225 memcpy(p, X->p, X->n * ciL);
Tom Cosgrove46259f62023-07-18 16:44:14 +0100226 mbedtls_mpi_zeroize_and_free(X->p, X->n);
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 }
228
Gilles Peskine053022f2023-06-29 19:26:48 +0200229 /* nblimbs fits in n because we ensure that MBEDTLS_MPI_MAX_LIMBS
230 * fits, and we've checked that nblimbs <= MBEDTLS_MPI_MAX_LIMBS. */
231 X->n = (unsigned short) nblimbs;
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 X->p = p;
233 }
234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000236}
237
238/*
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100239 * Resize down as much as possible,
240 * while keeping at least the specified number of limbs
241 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100242int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs)
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100243{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_mpi_uint *p;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100245 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 MPI_VALIDATE_RET(X != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
249 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
250 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100251
Gilles Peskinee2f563e2020-01-20 21:17:43 +0100252 /* Actually resize up if there are currently fewer than nblimbs limbs. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if (X->n <= nblimbs) {
254 return mbedtls_mpi_grow(X, nblimbs);
255 }
Gilles Peskine322752b2020-01-21 13:59:51 +0100256 /* After this point, then X->n > nblimbs and in particular X->n > 0. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 for (i = X->n - 1; i > 0; i--) {
259 if (X->p[i] != 0) {
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100260 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 }
262 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100263 i++;
264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 if (i < nblimbs) {
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100266 i = nblimbs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(i, ciL)) == NULL) {
270 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
271 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (X->p != NULL) {
274 memcpy(p, X->p, i * ciL);
Tom Cosgrove46259f62023-07-18 16:44:14 +0100275 mbedtls_mpi_zeroize_and_free(X->p, X->n);
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100276 }
277
Gilles Peskine053022f2023-06-29 19:26:48 +0200278 /* i fits in n because we ensure that MBEDTLS_MPI_MAX_LIMBS
279 * fits, and we've checked that i <= nblimbs <= MBEDTLS_MPI_MAX_LIMBS. */
280 X->n = (unsigned short) i;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100281 X->p = p;
282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return 0;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100284}
285
Gilles Peskineed32b572021-06-02 22:17:52 +0200286/* Resize X to have exactly n limbs and set it to 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100287static int mbedtls_mpi_resize_clear(mbedtls_mpi *X, size_t limbs)
Gilles Peskineed32b572021-06-02 22:17:52 +0200288{
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (limbs == 0) {
290 mbedtls_mpi_free(X);
291 return 0;
292 } else if (X->n == limbs) {
293 memset(X->p, 0, limbs * ciL);
Gilles Peskineed32b572021-06-02 22:17:52 +0200294 X->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return 0;
296 } else {
297 mbedtls_mpi_free(X);
298 return mbedtls_mpi_grow(X, limbs);
Gilles Peskineed32b572021-06-02 22:17:52 +0200299 }
300}
301
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100302/*
Gilles Peskine3da1a8f2021-06-08 23:17:42 +0200303 * Copy the contents of Y into X.
304 *
305 * This function is not constant-time. Leading zeros in Y may be removed.
306 *
307 * Ensure that X does not shrink. This is not guaranteed by the public API,
308 * but some code in the bignum module relies on this property, for example
309 * in mbedtls_mpi_exp_mod().
Paul Bakker5121ce52009-01-03 21:22:43 +0000310 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100311int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000312{
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100313 int ret = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000314 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 MPI_VALIDATE_RET(X != NULL);
316 MPI_VALIDATE_RET(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if (X == Y) {
319 return 0;
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200320 }
321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if (Y->n == 0) {
323 if (X->n != 0) {
324 X->s = 1;
325 memset(X->p, 0, X->n * ciL);
326 }
327 return 0;
328 }
329
330 for (i = Y->n - 1; i > 0; i--) {
331 if (Y->p[i] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 }
334 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000335 i++;
336
337 X->s = Y->s;
338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if (X->n < i) {
340 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i));
341 } else {
342 memset(X->p + i, 0, (X->n - i) * ciL);
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100343 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 memcpy(X->p, Y->p, i * ciL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000346
347cleanup:
348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000350}
351
352/*
353 * Swap the contents of X and Y
354 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100355void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000356{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 MPI_VALIDATE(X != NULL);
359 MPI_VALIDATE(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 memcpy(&T, X, sizeof(mbedtls_mpi));
362 memcpy(X, Y, sizeof(mbedtls_mpi));
363 memcpy(Y, &T, sizeof(mbedtls_mpi));
Paul Bakker5121ce52009-01-03 21:22:43 +0000364}
365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366static inline mbedtls_mpi_uint mpi_sint_abs(mbedtls_mpi_sint z)
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100367{
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 if (z >= 0) {
369 return z;
370 }
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100371 /* Take care to handle the most negative value (-2^(biL-1)) correctly.
372 * A naive -z would have undefined behavior.
373 * Write this in a way that makes popular compilers happy (GCC, Clang,
374 * MSVC). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 return (mbedtls_mpi_uint) 0 - (mbedtls_mpi_uint) z;
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100376}
377
Dave Rodgmanf3df1052023-08-09 18:55:41 +0100378/* Convert x to a sign, i.e. to 1, if x is positive, or -1, if x is negative.
379 * This looks awkward but generates smaller code than (x < 0 ? -1 : 1) */
Dave Rodgman73d85912023-09-28 17:00:50 +0100380#define TO_SIGN(x) ((mbedtls_mpi_sint) (((mbedtls_mpi_uint) x) >> (biL - 1)) * -2 + 1)
Dave Rodgmanf3df1052023-08-09 18:55:41 +0100381
Paul Bakker5121ce52009-01-03 21:22:43 +0000382/*
383 * Set value from integer
384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100385int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z)
Paul Bakker5121ce52009-01-03 21:22:43 +0000386{
Janos Follath24eed8d2019-11-22 13:21:35 +0000387 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, 1));
391 memset(X->p, 0, X->n * ciL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 X->p[0] = mpi_sint_abs(z);
Dave Rodgmanf3df1052023-08-09 18:55:41 +0100394 X->s = TO_SIGN(z);
Paul Bakker5121ce52009-01-03 21:22:43 +0000395
396cleanup:
397
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000399}
400
401/*
Paul Bakker2f5947e2011-05-18 15:47:11 +0000402 * Get a specific bit
403 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100404int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
Paul Bakker2f5947e2011-05-18 15:47:11 +0000405{
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 MPI_VALIDATE_RET(X != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if (X->n * biL <= pos) {
409 return 0;
410 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 return (X->p[pos / biL] >> (pos % biL)) & 0x01;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000413}
414
415/*
416 * Set a bit to a specific value of 0 or 1
417 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100418int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val)
Paul Bakker2f5947e2011-05-18 15:47:11 +0000419{
420 int ret = 0;
421 size_t off = pos / biL;
422 size_t idx = pos % biL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 MPI_VALIDATE_RET(X != NULL);
Paul Bakker2f5947e2011-05-18 15:47:11 +0000424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (val != 0 && val != 1) {
426 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000427 }
428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if (X->n * biL <= pos) {
430 if (val == 0) {
431 return 0;
432 }
433
434 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, off + 1));
435 }
436
437 X->p[off] &= ~((mbedtls_mpi_uint) 0x01 << idx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 X->p[off] |= (mbedtls_mpi_uint) val << idx;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000439
440cleanup:
Paul Bakker9af723c2014-05-01 13:03:14 +0200441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 return ret;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000443}
444
445/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200446 * Return the number of less significant zero-bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000447 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100448size_t mbedtls_mpi_lsb(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000449{
Dave Rodgmanfa703e32023-08-09 18:56:07 +0100450 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 MBEDTLS_INTERNAL_VALIDATE_RET(X != NULL, 0);
Paul Bakker5121ce52009-01-03 21:22:43 +0000452
Dave Rodgmanfa703e32023-08-09 18:56:07 +0100453#if defined(__has_builtin)
454#if (MBEDTLS_MPI_UINT_MAX == UINT_MAX) && __has_builtin(__builtin_ctz)
455 #define mbedtls_mpi_uint_ctz __builtin_ctz
456#elif (MBEDTLS_MPI_UINT_MAX == ULONG_MAX) && __has_builtin(__builtin_ctzl)
457 #define mbedtls_mpi_uint_ctz __builtin_ctzl
458#elif (MBEDTLS_MPI_UINT_MAX == ULLONG_MAX) && __has_builtin(__builtin_ctzll)
459 #define mbedtls_mpi_uint_ctz __builtin_ctzll
460#endif
461#endif
462
463#if defined(mbedtls_mpi_uint_ctz)
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 for (i = 0; i < X->n; i++) {
Dave Rodgman960eca92023-08-09 20:43:18 +0100465 if (X->p[i] != 0) {
466 return i * biL + mbedtls_mpi_uint_ctz(X->p[i]);
467 }
Dave Rodgmanfa703e32023-08-09 18:56:07 +0100468 }
469#else
470 size_t count = 0;
471 for (i = 0; i < X->n; i++) {
472 for (size_t j = 0; j < biL; j++, count++) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if (((X->p[i] >> j) & 1) != 0) {
474 return count;
475 }
476 }
477 }
Dave Rodgmanfa703e32023-08-09 18:56:07 +0100478#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000481}
482
483/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200484 * Return the number of bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000485 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100486size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000487{
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 return mbedtls_mpi_core_bitlen(X->p, X->n);
Paul Bakker5121ce52009-01-03 21:22:43 +0000489}
490
491/*
492 * Return the total size in bytes
493 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100494size_t mbedtls_mpi_size(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000495{
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 return (mbedtls_mpi_bitlen(X) + 7) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000497}
498
499/*
500 * Convert an ASCII character to digit value
501 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100502static int mpi_get_digit(mbedtls_mpi_uint *d, int radix, char c)
Paul Bakker5121ce52009-01-03 21:22:43 +0000503{
504 *d = 255;
505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 if (c >= 0x30 && c <= 0x39) {
507 *d = c - 0x30;
508 }
509 if (c >= 0x41 && c <= 0x46) {
510 *d = c - 0x37;
511 }
512 if (c >= 0x61 && c <= 0x66) {
513 *d = c - 0x57;
514 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 if (*d >= (mbedtls_mpi_uint) radix) {
517 return MBEDTLS_ERR_MPI_INVALID_CHARACTER;
518 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000521}
522
523/*
524 * Import from an ASCII string
525 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100526int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s)
Paul Bakker5121ce52009-01-03 21:22:43 +0000527{
Janos Follath24eed8d2019-11-22 13:21:35 +0000528 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000529 size_t i, j, slen, n;
Gilles Peskine80f56732021-04-03 18:26:13 +0200530 int sign = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 mbedtls_mpi_uint d;
532 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 MPI_VALIDATE_RET(X != NULL);
534 MPI_VALIDATE_RET(s != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 if (radix < 2 || radix > 16) {
537 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskine7cba8592021-06-08 18:32:34 +0200538 }
539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 mbedtls_mpi_init(&T);
541
542 if (s[0] == 0) {
543 mbedtls_mpi_free(X);
544 return 0;
545 }
546
547 if (s[0] == '-') {
Gilles Peskine80f56732021-04-03 18:26:13 +0200548 ++s;
549 sign = -1;
550 }
551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 slen = strlen(s);
Paul Bakkerff60ee62010-03-16 21:09:09 +0000553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 if (radix == 16) {
Dave Rodgman68ef1d62023-05-18 20:49:03 +0100555 if (slen > SIZE_MAX >> 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 n = BITS_TO_LIMBS(slen << 2);
560
561 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n));
562 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
563
564 for (i = slen, j = 0; i > 0; i--, j++) {
565 MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i - 1]));
566 X->p[j / (2 * ciL)] |= d << ((j % (2 * ciL)) << 2);
567 }
568 } else {
569 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
570
571 for (i = 0; i < slen; i++) {
572 MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i]));
573 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T, X, radix));
574 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, &T, d));
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 }
576 }
577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 if (sign < 0 && mbedtls_mpi_bitlen(X) != 0) {
Gilles Peskine80f56732021-04-03 18:26:13 +0200579 X->s = -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 }
Gilles Peskine80f56732021-04-03 18:26:13 +0200581
Paul Bakker5121ce52009-01-03 21:22:43 +0000582cleanup:
583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000587}
588
589/*
Ron Eldora16fa292018-11-20 14:07:01 +0200590 * Helper to write the digits high-order first.
Paul Bakker5121ce52009-01-03 21:22:43 +0000591 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100592static int mpi_write_hlp(mbedtls_mpi *X, int radix,
593 char **p, const size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000594{
Janos Follath24eed8d2019-11-22 13:21:35 +0000595 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_mpi_uint r;
Ron Eldora16fa292018-11-20 14:07:01 +0200597 size_t length = 0;
598 char *p_end = *p + buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 do {
601 if (length >= buflen) {
602 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
Ron Eldora16fa292018-11-20 14:07:01 +0200603 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, radix));
606 MBEDTLS_MPI_CHK(mbedtls_mpi_div_int(X, NULL, X, radix));
Ron Eldora16fa292018-11-20 14:07:01 +0200607 /*
608 * Write the residue in the current position, as an ASCII character.
609 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 if (r < 0xA) {
611 *(--p_end) = (char) ('0' + r);
612 } else {
613 *(--p_end) = (char) ('A' + (r - 0xA));
614 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000615
Ron Eldora16fa292018-11-20 14:07:01 +0200616 length++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 } while (mbedtls_mpi_cmp_int(X, 0) != 0);
Paul Bakker5121ce52009-01-03 21:22:43 +0000618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 memmove(*p, p_end, length);
Ron Eldora16fa292018-11-20 14:07:01 +0200620 *p += length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
622cleanup:
623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000625}
626
627/*
628 * Export into an ASCII string
629 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100630int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
631 char *buf, size_t buflen, size_t *olen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000632{
Paul Bakker23986e52011-04-24 08:57:21 +0000633 int ret = 0;
634 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 MPI_VALIDATE_RET(X != NULL);
638 MPI_VALIDATE_RET(olen != NULL);
639 MPI_VALIDATE_RET(buflen == 0 || buf != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if (radix < 2 || radix > 16) {
642 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
643 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 n = mbedtls_mpi_bitlen(X); /* Number of bits necessary to present `n`. */
646 if (radix >= 4) {
647 n >>= 1; /* Number of 4-adic digits necessary to present
Hanno Becker23cfea02019-02-04 09:45:07 +0000648 * `n`. If radix > 4, this might be a strict
649 * overapproximation of the number of
650 * radix-adic digits needed to present `n`. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 }
652 if (radix >= 16) {
653 n >>= 1; /* Number of hexadecimal digits necessary to
Hanno Becker23cfea02019-02-04 09:45:07 +0000654 * present `n`. */
655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 }
Janos Follath80470622019-03-06 13:43:02 +0000657 n += 1; /* Terminating null byte */
Hanno Becker23cfea02019-02-04 09:45:07 +0000658 n += 1; /* Compensate for the divisions above, which round down `n`
659 * in case it's not even. */
660 n += 1; /* Potential '-'-sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 n += (n & 1); /* Make n even to have enough space for hexadecimal writing,
Hanno Becker23cfea02019-02-04 09:45:07 +0000662 * which always uses an even number of hex-digits. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 if (buflen < n) {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100665 *olen = n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 }
668
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100669 p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if (X->s == -1) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000673 *p++ = '-';
Hanno Beckerc983c812019-02-01 16:41:30 +0000674 buflen--;
675 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 if (radix == 16) {
Paul Bakker23986e52011-04-24 08:57:21 +0000678 int c;
679 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 for (i = X->n, k = 0; i > 0; i--) {
682 for (j = ciL; j > 0; j--) {
683 c = (X->p[i - 1] >> ((j - 1) << 3)) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (c == 0 && k == 0 && (i + j) != 2) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000686 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000689 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000690 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000691 k = 1;
692 }
693 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 } else {
695 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T, X));
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (T.s == -1) {
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000698 T.s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 MBEDTLS_MPI_CHK(mpi_write_hlp(&T, radix, &p, buflen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000702 }
703
704 *p++ = '\0';
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000705 *olen = (size_t) (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000706
707cleanup:
708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000712}
713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000715/*
716 * Read X from an opened file
717 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100718int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin)
Paul Bakker5121ce52009-01-03 21:22:43 +0000719{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 mbedtls_mpi_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000721 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000722 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000723 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000724 * Buffer should have space for (short) label and decimal formatted MPI,
725 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000726 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 MPI_VALIDATE_RET(X != NULL);
730 MPI_VALIDATE_RET(fin != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (radix < 2 || radix > 16) {
733 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
734 }
Hanno Becker73d7d792018-12-11 10:35:51 +0000735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 memset(s, 0, sizeof(s));
737 if (fgets(s, sizeof(s) - 1, fin) == NULL) {
738 return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
739 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 slen = strlen(s);
742 if (slen == sizeof(s) - 2) {
743 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
744 }
Paul Bakkercb37aa52011-11-30 16:00:20 +0000745
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if (slen > 0 && s[slen - 1] == '\n') {
747 slen--; s[slen] = '\0';
748 }
749 if (slen > 0 && s[slen - 1] == '\r') {
750 slen--; s[slen] = '\0';
751 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000752
753 p = s + slen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 while (p-- > s) {
755 if (mpi_get_digit(&d, radix, *p) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000756 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 }
758 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 return mbedtls_mpi_read_string(X, radix, p + 1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000761}
762
763/*
764 * Write X into an opened file (or stdout if fout == NULL)
765 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100766int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE *fout)
Paul Bakker5121ce52009-01-03 21:22:43 +0000767{
Janos Follath24eed8d2019-11-22 13:21:35 +0000768 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000769 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000770 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000771 * Buffer should have space for (short) label and decimal formatted MPI,
772 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000773 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
775 MPI_VALIDATE_RET(X != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 if (radix < 2 || radix > 16) {
778 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
779 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 memset(s, 0, sizeof(s));
Paul Bakker5121ce52009-01-03 21:22:43 +0000782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 MBEDTLS_MPI_CHK(mbedtls_mpi_write_string(X, radix, s, sizeof(s) - 2, &n));
Paul Bakker5121ce52009-01-03 21:22:43 +0000784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 if (p == NULL) {
786 p = "";
787 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 plen = strlen(p);
790 slen = strlen(s);
Paul Bakker5121ce52009-01-03 21:22:43 +0000791 s[slen++] = '\r';
792 s[slen++] = '\n';
793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if (fout != NULL) {
795 if (fwrite(p, 1, plen, fout) != plen ||
796 fwrite(s, 1, slen, fout) != slen) {
797 return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
798 }
799 } else {
800 mbedtls_printf("%s%s", p, s);
Paul Bakker5121ce52009-01-03 21:22:43 +0000801 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000802
803cleanup:
804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000806}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000808
809/*
Janos Follatha778a942019-02-13 10:28:28 +0000810 * Import X from unsigned binary data, little endian
Przemyslaw Stekiel76960a72022-02-21 13:42:09 +0100811 *
812 * This function is guaranteed to return an MPI with exactly the necessary
813 * number of limbs (in particular, it does not skip 0s in the input).
Janos Follatha778a942019-02-13 10:28:28 +0000814 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815int mbedtls_mpi_read_binary_le(mbedtls_mpi *X,
816 const unsigned char *buf, size_t buflen)
Janos Follatha778a942019-02-13 10:28:28 +0000817{
Janos Follath24eed8d2019-11-22 13:21:35 +0000818 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 const size_t limbs = CHARS_TO_LIMBS(buflen);
Janos Follatha778a942019-02-13 10:28:28 +0000820
821 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
Janos Follatha778a942019-02-13 10:28:28 +0000823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_le(X->p, X->n, buf, buflen));
Janos Follatha778a942019-02-13 10:28:28 +0000825
826cleanup:
827
Janos Follath171a7ef2019-02-15 16:17:45 +0000828 /*
829 * This function is also used to import keys. However, wiping the buffers
830 * upon failure is not necessary because failure only can happen before any
831 * input is copied.
832 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 return ret;
Janos Follatha778a942019-02-13 10:28:28 +0000834}
835
836/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000837 * Import X from unsigned binary data, big endian
Przemyslaw Stekiel76960a72022-02-21 13:42:09 +0100838 *
839 * This function is guaranteed to return an MPI with exactly the necessary
840 * number of limbs (in particular, it does not skip 0s in the input).
Paul Bakker5121ce52009-01-03 21:22:43 +0000841 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100842int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000843{
Janos Follath24eed8d2019-11-22 13:21:35 +0000844 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 const size_t limbs = CHARS_TO_LIMBS(buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 MPI_VALIDATE_RET(X != NULL);
848 MPI_VALIDATE_RET(buflen == 0 || buf != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000849
Hanno Becker073c1992017-10-17 15:17:27 +0100850 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
Paul Bakker5121ce52009-01-03 21:22:43 +0000852
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_be(X->p, X->n, buf, buflen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000854
855cleanup:
856
Janos Follath171a7ef2019-02-15 16:17:45 +0000857 /*
858 * This function is also used to import keys. However, wiping the buffers
859 * upon failure is not necessary because failure only can happen before any
860 * input is copied.
861 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000863}
864
865/*
Janos Follathe344d0f2019-02-19 16:17:40 +0000866 * Export X into unsigned binary data, little endian
867 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100868int mbedtls_mpi_write_binary_le(const mbedtls_mpi *X,
869 unsigned char *buf, size_t buflen)
Janos Follathe344d0f2019-02-19 16:17:40 +0000870{
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 return mbedtls_mpi_core_write_le(X->p, X->n, buf, buflen);
Janos Follathe344d0f2019-02-19 16:17:40 +0000872}
873
874/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000875 * Export X into unsigned binary data, big endian
876 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100877int mbedtls_mpi_write_binary(const mbedtls_mpi *X,
878 unsigned char *buf, size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000879{
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 return mbedtls_mpi_core_write_be(X->p, X->n, buf, buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000881}
882
883/*
884 * Left-shift: X <<= count
885 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100886int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
Paul Bakker5121ce52009-01-03 21:22:43 +0000887{
Janos Follath24eed8d2019-11-22 13:21:35 +0000888 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Minos Galanakis0144b352023-05-02 14:02:32 +0100889 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 i = mbedtls_mpi_bitlen(X) + count;
Paul Bakker5121ce52009-01-03 21:22:43 +0000893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 if (X->n * biL < i) {
895 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, BITS_TO_LIMBS(i)));
896 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000897
898 ret = 0;
899
Minos Galanakis0144b352023-05-02 14:02:32 +0100900 mbedtls_mpi_core_shift_l(X->p, X->n, count);
Paul Bakker5121ce52009-01-03 21:22:43 +0000901cleanup:
902
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000904}
905
906/*
907 * Right-shift: X >>= count
908 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100909int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
Paul Bakker5121ce52009-01-03 21:22:43 +0000910{
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 MPI_VALIDATE_RET(X != NULL);
912 if (X->n != 0) {
913 mbedtls_mpi_core_shift_r(X->p, X->n, count);
914 }
915 return 0;
Gilles Peskine66414202022-09-21 15:36:16 +0200916}
917
Paul Bakker5121ce52009-01-03 21:22:43 +0000918/*
919 * Compare unsigned values
920 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100921int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000922{
Paul Bakker23986e52011-04-24 08:57:21 +0000923 size_t i, j;
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 MPI_VALIDATE_RET(X != NULL);
925 MPI_VALIDATE_RET(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 for (i = X->n; i > 0; i--) {
928 if (X->p[i - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000931 }
932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 for (j = Y->n; j > 0; j--) {
934 if (Y->p[j - 1] != 0) {
935 break;
936 }
937 }
938
Dave Rodgmanebcd7852023-08-09 18:56:42 +0100939 /* If i == j == 0, i.e. abs(X) == abs(Y),
940 * we end up returning 0 at the end of the function. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100941
942 if (i > j) {
943 return 1;
944 }
945 if (j > i) {
946 return -1;
947 }
948
949 for (; i > 0; i--) {
950 if (X->p[i - 1] > Y->p[i - 1]) {
951 return 1;
952 }
953 if (X->p[i - 1] < Y->p[i - 1]) {
954 return -1;
955 }
956 }
957
958 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000959}
960
961/*
962 * Compare signed values
963 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000965{
Paul Bakker23986e52011-04-24 08:57:21 +0000966 size_t i, j;
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 MPI_VALIDATE_RET(X != NULL);
968 MPI_VALIDATE_RET(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 for (i = X->n; i > 0; i--) {
971 if (X->p[i - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000972 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000974 }
975
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 for (j = Y->n; j > 0; j--) {
977 if (Y->p[j - 1] != 0) {
978 break;
979 }
980 }
981
982 if (i == 0 && j == 0) {
983 return 0;
984 }
985
986 if (i > j) {
987 return X->s;
988 }
989 if (j > i) {
990 return -Y->s;
991 }
992
993 if (X->s > 0 && Y->s < 0) {
994 return 1;
995 }
996 if (Y->s > 0 && X->s < 0) {
997 return -1;
998 }
999
1000 for (; i > 0; i--) {
1001 if (X->p[i - 1] > Y->p[i - 1]) {
1002 return X->s;
1003 }
1004 if (X->p[i - 1] < Y->p[i - 1]) {
1005 return -X->s;
1006 }
1007 }
1008
1009 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001010}
1011
Janos Follathee6abce2019-09-05 14:47:19 +01001012/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001013 * Compare signed values
1014 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001015int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
Paul Bakker5121ce52009-01-03 21:22:43 +00001016{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 mbedtls_mpi Y;
1018 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 *p = mpi_sint_abs(z);
Dave Rodgmanf3df1052023-08-09 18:55:41 +01001022 Y.s = TO_SIGN(z);
Paul Bakker5121ce52009-01-03 21:22:43 +00001023 Y.n = 1;
1024 Y.p = p;
1025
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 return mbedtls_mpi_cmp_mpi(X, &Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00001027}
1028
1029/*
1030 * Unsigned addition: X = |A| + |B| (HAC 14.7)
1031 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001032int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001033{
Janos Follath24eed8d2019-11-22 13:21:35 +00001034 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001035 size_t j;
Agathiyan Bragadeeshc99840a2023-07-12 11:15:46 +01001036 mbedtls_mpi_uint *p;
1037 mbedtls_mpi_uint c;
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 MPI_VALIDATE_RET(X != NULL);
1039 MPI_VALIDATE_RET(A != NULL);
1040 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 if (X == B) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 const mbedtls_mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001044 }
1045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 if (X != A) {
1047 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
1048 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001049
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001050 /*
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001051 * X must always be positive as a result of unsigned additions.
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001052 */
1053 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 for (j = B->n; j > 0; j--) {
1056 if (B->p[j - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001057 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 }
1059 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001060
Gilles Peskinedb14a9d2022-11-15 22:59:00 +01001061 /* Exit early to avoid undefined behavior on NULL+0 when X->n == 0
1062 * and B is 0 (of any size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 if (j == 0) {
1064 return 0;
1065 }
Gilles Peskinedb14a9d2022-11-15 22:59:00 +01001066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j));
Paul Bakker5121ce52009-01-03 21:22:43 +00001068
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001069 /* j is the number of non-zero limbs of B. Add those to X. */
Paul Bakker5121ce52009-01-03 21:22:43 +00001070
Agathiyan Bragadeeshc99840a2023-07-12 11:15:46 +01001071 p = X->p;
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001072
Agathiyan Bragadeeshc99840a2023-07-12 11:15:46 +01001073 c = mbedtls_mpi_core_add(p, p, B->p, j);
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001074
1075 p += j;
1076
1077 /* Now propagate any carry */
Paul Bakker5121ce52009-01-03 21:22:43 +00001078
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 while (c != 0) {
1080 if (j >= X->n) {
1081 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j + 1));
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001082 p = X->p + j;
Paul Bakker5121ce52009-01-03 21:22:43 +00001083 }
1084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 *p += c; c = (*p < c); j++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001086 }
1087
1088cleanup:
1089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001091}
1092
Paul Bakker5121ce52009-01-03 21:22:43 +00001093/*
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001094 * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
Paul Bakker5121ce52009-01-03 21:22:43 +00001095 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001096int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001097{
Janos Follath24eed8d2019-11-22 13:21:35 +00001098 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001099 size_t n;
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001100 mbedtls_mpi_uint carry;
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 MPI_VALIDATE_RET(X != NULL);
1102 MPI_VALIDATE_RET(A != NULL);
1103 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 for (n = B->n; n > 0; n--) {
1106 if (B->p[n - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001107 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 }
1109 }
1110 if (n > A->n) {
Gilles Peskinec8a91772021-01-27 22:30:43 +01001111 /* B >= (2^ciL)^n > A */
1112 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1113 goto cleanup;
1114 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, A->n));
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001117
1118 /* Set the high limbs of X to match A. Don't touch the lower limbs
1119 * because X might be aliased to B, and we must not overwrite the
1120 * significant digits of B. */
Aaron M. Uckoaf67d2c2023-01-17 11:52:22 -05001121 if (A->n > n && A != X) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 memcpy(X->p + n, A->p + n, (A->n - n) * ciL);
1123 }
1124 if (X->n > A->n) {
1125 memset(X->p + A->n, 0, (X->n - A->n) * ciL);
1126 }
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 carry = mbedtls_mpi_core_sub(X->p, A->p, B->p, n);
1129 if (carry != 0) {
Tom Cosgrove452c99c2022-08-25 10:07:07 +01001130 /* Propagate the carry through the rest of X. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 carry = mbedtls_mpi_core_sub_int(X->p + n, X->p + n, carry, X->n - n);
Tom Cosgrove452c99c2022-08-25 10:07:07 +01001132
1133 /* If we have further carry/borrow, the result is negative. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 if (carry != 0) {
Gilles Peskine89b41302020-07-23 01:16:46 +02001135 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1136 goto cleanup;
1137 }
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001138 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001139
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001140 /* X should always be positive as a result of unsigned subtractions. */
1141 X->s = 1;
1142
Paul Bakker5121ce52009-01-03 21:22:43 +00001143cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001145}
1146
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001147/* Common function for signed addition and subtraction.
1148 * Calculate A + B * flip_B where flip_B is 1 or -1.
Paul Bakker5121ce52009-01-03 21:22:43 +00001149 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001150static int add_sub_mpi(mbedtls_mpi *X,
1151 const mbedtls_mpi *A, const mbedtls_mpi *B,
1152 int flip_B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001153{
Hanno Becker73d7d792018-12-11 10:35:51 +00001154 int ret, s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 MPI_VALIDATE_RET(X != NULL);
1156 MPI_VALIDATE_RET(A != NULL);
1157 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001158
Hanno Becker73d7d792018-12-11 10:35:51 +00001159 s = A->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 if (A->s * B->s * flip_B < 0) {
1161 int cmp = mbedtls_mpi_cmp_abs(A, B);
1162 if (cmp >= 0) {
1163 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, A, B));
Gilles Peskine4a768dd2022-11-09 22:02:16 +01001164 /* If |A| = |B|, the result is 0 and we must set the sign bit
1165 * to +1 regardless of which of A or B was negative. Otherwise,
1166 * since |A| > |B|, the sign is the sign of A. */
1167 X->s = cmp == 0 ? 1 : s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 } else {
1169 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, B, A));
Gilles Peskine4a768dd2022-11-09 22:02:16 +01001170 /* Since |A| < |B|, the sign is the opposite of A. */
Paul Bakker5121ce52009-01-03 21:22:43 +00001171 X->s = -s;
1172 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 } else {
1174 MBEDTLS_MPI_CHK(mbedtls_mpi_add_abs(X, A, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001175 X->s = s;
1176 }
1177
1178cleanup:
1179
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001181}
1182
1183/*
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001184 * Signed addition: X = A + B
1185 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001186int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001187{
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 return add_sub_mpi(X, A, B, 1);
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001189}
1190
1191/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001192 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001194int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001195{
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 return add_sub_mpi(X, A, B, -1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001197}
1198
1199/*
1200 * Signed addition: X = A + b
1201 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001202int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001203{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001204 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 MPI_VALIDATE_RET(X != NULL);
1207 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 p[0] = mpi_sint_abs(b);
Dave Rodgmanf3df1052023-08-09 18:55:41 +01001210 B.s = TO_SIGN(b);
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001211 B.n = 1;
1212 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001213
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 return mbedtls_mpi_add_mpi(X, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001215}
1216
1217/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001218 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001219 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001220int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001221{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001222 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 MPI_VALIDATE_RET(X != NULL);
1225 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 p[0] = mpi_sint_abs(b);
Dave Rodgmanf3df1052023-08-09 18:55:41 +01001228 B.s = TO_SIGN(b);
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001229 B.n = 1;
1230 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 return mbedtls_mpi_sub_mpi(X, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001233}
1234
Paul Bakker5121ce52009-01-03 21:22:43 +00001235/*
1236 * Baseline multiplication: X = A * B (HAC 14.12)
1237 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001238int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001239{
Janos Follath24eed8d2019-11-22 13:21:35 +00001240 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker1772e052022-04-13 06:51:40 +01001241 size_t i, j;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 mbedtls_mpi TA, TB;
Hanno Beckerda763de2022-04-13 06:50:02 +01001243 int result_is_zero = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 MPI_VALIDATE_RET(X != NULL);
1245 MPI_VALIDATE_RET(A != NULL);
1246 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001247
Tom Cosgrove6af26f32022-08-24 16:40:55 +01001248 mbedtls_mpi_init(&TA);
1249 mbedtls_mpi_init(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00001250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 if (X == A) {
1252 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A)); A = &TA;
1253 }
1254 if (X == B) {
1255 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B)); B = &TB;
1256 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 for (i = A->n; i > 0; i--) {
1259 if (A->p[i - 1] != 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001260 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 }
1262 }
1263 if (i == 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001264 result_is_zero = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 }
Hanno Beckerda763de2022-04-13 06:50:02 +01001266
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 for (j = B->n; j > 0; j--) {
1268 if (B->p[j - 1] != 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001269 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 }
1271 }
1272 if (j == 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001273 result_is_zero = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 }
Hanno Beckerda763de2022-04-13 06:50:02 +01001275
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i + j));
1277 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
Paul Bakker5121ce52009-01-03 21:22:43 +00001278
Tom Cosgrove6af26f32022-08-24 16:40:55 +01001279 mbedtls_mpi_core_mul(X->p, A->p, i, B->p, j);
Paul Bakker5121ce52009-01-03 21:22:43 +00001280
Hanno Beckerda763de2022-04-13 06:50:02 +01001281 /* If the result is 0, we don't shortcut the operation, which reduces
1282 * but does not eliminate side channels leaking the zero-ness. We do
1283 * need to take care to set the sign bit properly since the library does
1284 * not fully support an MPI object with a value of 0 and s == -1. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 if (result_is_zero) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001286 X->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 } else {
Hanno Beckerda763de2022-04-13 06:50:02 +01001288 X->s = A->s * B->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001290
1291cleanup:
1292
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TA);
Paul Bakker5121ce52009-01-03 21:22:43 +00001294
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001296}
1297
1298/*
1299 * Baseline multiplication: X = A * b
1300 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001301int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001302{
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 MPI_VALIDATE_RET(X != NULL);
1304 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001305
Hanno Becker35771312022-04-14 11:52:11 +01001306 size_t n = A->n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 while (n > 0 && A->p[n - 1] == 0) {
Hanno Becker35771312022-04-14 11:52:11 +01001308 --n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 }
Hanno Becker35771312022-04-14 11:52:11 +01001310
Hanno Becker74a11a32022-04-06 06:27:00 +01001311 /* The general method below doesn't work if b==0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 if (b == 0 || n == 0) {
1313 return mbedtls_mpi_lset(X, 0);
1314 }
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001315
Hanno Beckeraef9cc42022-04-11 06:36:29 +01001316 /* Calculate A*b as A + A*(b-1) to take advantage of mbedtls_mpi_core_mla */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001317 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001318 /* In general, A * b requires 1 limb more than b. If
1319 * A->p[n - 1] * b / b == A->p[n - 1], then A * b fits in the same
1320 * number of limbs as A and the call to grow() is not required since
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001321 * copy() will take care of the growth if needed. However, experimentally,
1322 * making the call to grow() unconditional causes slightly fewer
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001323 * calls to calloc() in ECP code, presumably because it reuses the
1324 * same mpi for a while and this way the mpi is more likely to directly
Hanno Becker9137b9c2022-04-12 10:51:54 +01001325 * grow to its final size.
1326 *
1327 * Note that calculating A*b as 0 + A*b doesn't work as-is because
1328 * A,X can be the same. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n + 1));
1330 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
1331 mbedtls_mpi_core_mla(X->p, X->n, A->p, n, b - 1);
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001332
1333cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001335}
1336
1337/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001338 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1339 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001340 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001341static mbedtls_mpi_uint mbedtls_int_div_int(mbedtls_mpi_uint u1,
1342 mbedtls_mpi_uint u0,
1343 mbedtls_mpi_uint d,
1344 mbedtls_mpi_uint *r)
Simon Butcher15b15d12015-11-26 19:35:03 +00001345{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001346#if defined(MBEDTLS_HAVE_UDBL)
1347 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001348#else
Simon Butcher9803d072016-01-03 00:24:34 +00001349 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 const mbedtls_mpi_uint uint_halfword_mask = ((mbedtls_mpi_uint) 1 << biH) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001351 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1352 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001353 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001354#endif
1355
Simon Butcher15b15d12015-11-26 19:35:03 +00001356 /*
1357 * Check for overflow
1358 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 if (0 == d || u1 >= d) {
1360 if (r != NULL) {
1361 *r = ~(mbedtls_mpi_uint) 0u;
1362 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001363
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 return ~(mbedtls_mpi_uint) 0u;
Simon Butcher15b15d12015-11-26 19:35:03 +00001365 }
1366
1367#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001368 dividend = (mbedtls_t_udbl) u1 << biL;
1369 dividend |= (mbedtls_t_udbl) u0;
1370 quotient = dividend / d;
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 if (quotient > ((mbedtls_t_udbl) 1 << biL) - 1) {
1372 quotient = ((mbedtls_t_udbl) 1 << biL) - 1;
1373 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 if (r != NULL) {
1376 *r = (mbedtls_mpi_uint) (dividend - (quotient * d));
1377 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001378
1379 return (mbedtls_mpi_uint) quotient;
1380#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001381
1382 /*
1383 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1384 * Vol. 2 - Seminumerical Algorithms, Knuth
1385 */
1386
1387 /*
1388 * Normalize the divisor, d, and dividend, u0, u1
1389 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 s = mbedtls_mpi_core_clz(d);
Simon Butcher15b15d12015-11-26 19:35:03 +00001391 d = d << s;
1392
1393 u1 = u1 << s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 u1 |= (u0 >> (biL - s)) & (-(mbedtls_mpi_sint) s >> (biL - 1));
Simon Butcher15b15d12015-11-26 19:35:03 +00001395 u0 = u0 << s;
1396
1397 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001398 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001399
1400 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001401 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001402
1403 /*
1404 * Find the first quotient and remainder
1405 */
1406 q1 = u1 / d1;
1407 r0 = u1 - d1 * q1;
1408
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 while (q1 >= radix || (q1 * d0 > radix * r0 + u0_msw)) {
Simon Butcher15b15d12015-11-26 19:35:03 +00001410 q1 -= 1;
1411 r0 += d1;
1412
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 if (r0 >= radix) {
1414 break;
1415 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001416 }
1417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 rAX = (u1 * radix) + (u0_msw - q1 * d);
Simon Butcher15b15d12015-11-26 19:35:03 +00001419 q0 = rAX / d1;
1420 r0 = rAX - q0 * d1;
1421
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 while (q0 >= radix || (q0 * d0 > radix * r0 + u0_lsw)) {
Simon Butcher15b15d12015-11-26 19:35:03 +00001423 q0 -= 1;
1424 r0 += d1;
1425
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 if (r0 >= radix) {
1427 break;
1428 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001429 }
1430
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 if (r != NULL) {
1432 *r = (rAX * radix + u0_lsw - q0 * d) >> s;
1433 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001434
1435 quotient = q1 * radix + q0;
1436
1437 return quotient;
1438#endif
1439}
1440
1441/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001443 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001444int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1445 const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001446{
Janos Follath24eed8d2019-11-22 13:21:35 +00001447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001448 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 mbedtls_mpi X, Y, Z, T1, T2;
Alexander Kd19a1932019-11-01 18:20:42 +03001450 mbedtls_mpi_uint TP2[3];
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 MPI_VALIDATE_RET(A != NULL);
1452 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001453
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 if (mbedtls_mpi_cmp_int(B, 0) == 0) {
1455 return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
1456 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z);
1459 mbedtls_mpi_init(&T1);
Alexander Kd19a1932019-11-01 18:20:42 +03001460 /*
1461 * Avoid dynamic memory allocations for constant-size T2.
1462 *
1463 * T2 is used for comparison only and the 3 limbs are assigned explicitly,
1464 * so nobody increase the size of the MPI and we're safe to use an on-stack
1465 * buffer.
1466 */
Alexander K35d6d462019-10-31 14:46:45 +03001467 T2.s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 T2.n = sizeof(TP2) / sizeof(*TP2);
Alexander Kd19a1932019-11-01 18:20:42 +03001469 T2.p = TP2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if (mbedtls_mpi_cmp_abs(A, B) < 0) {
1472 if (Q != NULL) {
1473 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(Q, 0));
1474 }
1475 if (R != NULL) {
1476 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, A));
1477 }
1478 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001479 }
1480
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&X, A));
1482 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001483 X.s = Y.s = 1;
1484
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&Z, A->n + 2));
1486 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Z, 0));
1487 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T1, A->n + 2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 k = mbedtls_mpi_bitlen(&Y) % biL;
1490 if (k < biL - 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001491 k = biL - 1 - k;
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&X, k));
1493 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, k));
1494 } else {
1495 k = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001496 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001497
1498 n = X.n - 1;
1499 t = Y.n - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, biL * (n - t)));
Paul Bakker5121ce52009-01-03 21:22:43 +00001501
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 while (mbedtls_mpi_cmp_mpi(&X, &Y) >= 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001503 Z.p[n - t]++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &Y));
Paul Bakker5121ce52009-01-03 21:22:43 +00001505 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, biL * (n - t)));
Paul Bakker5121ce52009-01-03 21:22:43 +00001507
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 for (i = n; i > t; i--) {
1509 if (X.p[i] >= Y.p[t]) {
1510 Z.p[i - t - 1] = ~(mbedtls_mpi_uint) 0u;
1511 } else {
1512 Z.p[i - t - 1] = mbedtls_int_div_int(X.p[i], X.p[i - 1],
1513 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001514 }
1515
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 T2.p[0] = (i < 2) ? 0 : X.p[i - 2];
1517 T2.p[1] = (i < 1) ? 0 : X.p[i - 1];
Alexander K35d6d462019-10-31 14:46:45 +03001518 T2.p[2] = X.p[i];
1519
Paul Bakker5121ce52009-01-03 21:22:43 +00001520 Z.p[i - t - 1]++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 do {
Paul Bakker5121ce52009-01-03 21:22:43 +00001522 Z.p[i - t - 1]--;
1523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&T1, 0));
1525 T1.p[0] = (t < 1) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 T1.p[1] = Y.p[t];
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &T1, Z.p[i - t - 1]));
1528 } while (mbedtls_mpi_cmp_mpi(&T1, &T2) > 0);
Paul Bakker5121ce52009-01-03 21:22:43 +00001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &Y, Z.p[i - t - 1]));
1531 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
1532 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &T1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 if (mbedtls_mpi_cmp_int(&X, 0) < 0) {
1535 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T1, &Y));
1536 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
1537 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&X, &X, &T1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001538 Z.p[i - t - 1]--;
1539 }
1540 }
1541
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 if (Q != NULL) {
1543 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(Q, &Z));
Paul Bakker5121ce52009-01-03 21:22:43 +00001544 Q->s = A->s * B->s;
1545 }
1546
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 if (R != NULL) {
1548 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&X, k));
Paul Bakkerf02c5642012-11-13 10:25:21 +00001549 X.s = A->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, &X));
Paul Bakker5121ce52009-01-03 21:22:43 +00001551
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 if (mbedtls_mpi_cmp_int(R, 0) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001553 R->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001555 }
1556
1557cleanup:
1558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z);
1560 mbedtls_mpi_free(&T1);
1561 mbedtls_platform_zeroize(TP2, sizeof(TP2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001564}
1565
1566/*
1567 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00001568 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001569int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R,
1570 const mbedtls_mpi *A,
1571 mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001572{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001573 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001576
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 p[0] = mpi_sint_abs(b);
Dave Rodgmanf3df1052023-08-09 18:55:41 +01001578 B.s = TO_SIGN(b);
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001579 B.n = 1;
1580 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001581
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 return mbedtls_mpi_div_mpi(Q, R, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001583}
1584
1585/*
1586 * Modulo: R = A mod B
1587 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001588int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001589{
Janos Follath24eed8d2019-11-22 13:21:35 +00001590 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 MPI_VALIDATE_RET(R != NULL);
1592 MPI_VALIDATE_RET(A != NULL);
1593 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001594
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 if (mbedtls_mpi_cmp_int(B, 0) < 0) {
1596 return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1597 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(NULL, R, A, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001600
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 while (mbedtls_mpi_cmp_int(R, 0) < 0) {
1602 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(R, R, B));
1603 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001604
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 while (mbedtls_mpi_cmp_mpi(R, B) >= 0) {
1606 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(R, R, B));
1607 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001608
1609cleanup:
1610
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001612}
1613
1614/*
1615 * Modulo: r = A mod b
1616 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001617int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001618{
Paul Bakker23986e52011-04-24 08:57:21 +00001619 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 mbedtls_mpi_uint x, y, z;
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 MPI_VALIDATE_RET(r != NULL);
1622 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001623
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 if (b == 0) {
1625 return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
1626 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001627
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 if (b < 0) {
1629 return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1630 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001631
1632 /*
1633 * handle trivial cases
1634 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 if (b == 1 || A->n == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001636 *r = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001638 }
1639
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 if (b == 2) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001641 *r = A->p[0] & 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001643 }
1644
1645 /*
1646 * general case
1647 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 for (i = A->n, y = 0; i > 0; i--) {
Paul Bakker23986e52011-04-24 08:57:21 +00001649 x = A->p[i - 1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 y = (y << biH) | (x >> biH);
Paul Bakker5121ce52009-01-03 21:22:43 +00001651 z = y / b;
1652 y -= z * b;
1653
1654 x <<= biH;
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 y = (y << biH) | (x >> biH);
Paul Bakker5121ce52009-01-03 21:22:43 +00001656 z = y / b;
1657 y -= z * b;
1658 }
1659
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001660 /*
1661 * If A is negative, then the current y represents a negative value.
1662 * Flipping it to the positive side.
1663 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 if (A->s < 0 && y != 0) {
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001665 y = b - y;
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001667
Paul Bakker5121ce52009-01-03 21:22:43 +00001668 *r = y;
1669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001671}
1672
Gilles Peskine449bd832023-01-11 14:50:10 +01001673static void mpi_montg_init(mbedtls_mpi_uint *mm, const mbedtls_mpi *N)
Paul Bakker5121ce52009-01-03 21:22:43 +00001674{
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 *mm = mbedtls_mpi_core_montmul_init(N->p);
Paul Bakker5121ce52009-01-03 21:22:43 +00001676}
1677
Tom Cosgrove93842842022-08-05 16:59:43 +01001678/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
1679 *
1680 * \param[in,out] A One of the numbers to multiply.
1681 * It must have at least as many limbs as N
1682 * (A->n >= N->n), and any limbs beyond n are ignored.
1683 * On successful completion, A contains the result of
1684 * the multiplication A * B * R^-1 mod N where
1685 * R = (2^ciL)^n.
1686 * \param[in] B One of the numbers to multiply.
1687 * It must be nonzero and must not have more limbs than N
1688 * (B->n <= N->n).
Tom Cosgrove40d22942022-08-17 06:42:44 +01001689 * \param[in] N The modulus. \p N must be odd.
Tom Cosgrove93842842022-08-05 16:59:43 +01001690 * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
1691 * This is -N^-1 mod 2^ciL.
1692 * \param[in,out] T A bignum for temporary storage.
1693 * It must be at least twice the limb size of N plus 1
1694 * (T->n >= 2 * N->n + 1).
1695 * Its initial content is unused and
1696 * its final content is indeterminate.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +01001697 * It does not get reallocated.
Tom Cosgrove93842842022-08-05 16:59:43 +01001698 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001699static void mpi_montmul(mbedtls_mpi *A, const mbedtls_mpi *B,
1700 const mbedtls_mpi *N, mbedtls_mpi_uint mm,
1701 mbedtls_mpi *T)
Paul Bakker5121ce52009-01-03 21:22:43 +00001702{
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 mbedtls_mpi_core_montmul(A->p, A->p, B->p, B->n, N->p, N->n, mm, T->p);
Paul Bakker5121ce52009-01-03 21:22:43 +00001704}
1705
1706/*
1707 * Montgomery reduction: A = A * R^-1 mod N
Gilles Peskine2a82f722020-06-04 15:00:49 +02001708 *
Tom Cosgrove93842842022-08-05 16:59:43 +01001709 * See mpi_montmul() regarding constraints and guarantees on the parameters.
Paul Bakker5121ce52009-01-03 21:22:43 +00001710 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001711static void mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N,
1712 mbedtls_mpi_uint mm, mbedtls_mpi *T)
Paul Bakker5121ce52009-01-03 21:22:43 +00001713{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 mbedtls_mpi_uint z = 1;
1715 mbedtls_mpi U;
Gilles Peskine053022f2023-06-29 19:26:48 +02001716 U.n = 1;
1717 U.s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001718 U.p = &z;
1719
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 mpi_montmul(A, &U, N, mm, T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001721}
1722
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001723/**
1724 * Select an MPI from a table without leaking the index.
1725 *
1726 * This is functionally equivalent to mbedtls_mpi_copy(R, T[idx]) except it
1727 * reads the entire table in order to avoid leaking the value of idx to an
1728 * attacker able to observe memory access patterns.
1729 *
1730 * \param[out] R Where to write the selected MPI.
1731 * \param[in] T The table to read from.
1732 * \param[in] T_size The number of elements in the table.
1733 * \param[in] idx The index of the element to select;
1734 * this must satisfy 0 <= idx < T_size.
1735 *
1736 * \return \c 0 on success, or a negative error code.
1737 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001738static int mpi_select(mbedtls_mpi *R, const mbedtls_mpi *T, size_t T_size, size_t idx)
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001739{
1740 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1741
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 for (size_t i = 0; i < T_size; i++) {
1743 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(R, &T[i],
Dave Rodgmanb7825ce2023-08-10 11:58:18 +01001744 (unsigned char) mbedtls_ct_uint_eq(i, idx)));
Manuel Pégourié-Gonnard92413ef2021-06-03 10:42:46 +02001745 }
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001746cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 return ret;
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001748}
1749
Paul Bakker5121ce52009-01-03 21:22:43 +00001750/*
1751 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
1752 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001753int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
1754 const mbedtls_mpi *E, const mbedtls_mpi *N,
1755 mbedtls_mpi *prec_RR)
Paul Bakker5121ce52009-01-03 21:22:43 +00001756{
Janos Follath24eed8d2019-11-22 13:21:35 +00001757 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath74601202022-11-21 15:54:20 +00001758 size_t window_bitsize;
Paul Bakker23986e52011-04-24 08:57:21 +00001759 size_t i, j, nblimbs;
1760 size_t bufsize, nbits;
Paul Elliott1748de12023-02-13 15:35:35 +00001761 size_t exponent_bits_in_window = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 mbedtls_mpi_uint ei, mm, state;
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 mbedtls_mpi RR, T, W[(size_t) 1 << MBEDTLS_MPI_WINDOW_SIZE], WW, Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00001764 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 MPI_VALIDATE_RET(X != NULL);
1767 MPI_VALIDATE_RET(A != NULL);
1768 MPI_VALIDATE_RET(E != NULL);
1769 MPI_VALIDATE_RET(N != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00001770
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 if (mbedtls_mpi_cmp_int(N, 0) <= 0 || (N->p[0] & 1) == 0) {
1772 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1773 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 if (mbedtls_mpi_cmp_int(E, 0) < 0) {
1776 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1777 }
Paul Bakkerf6198c12012-05-16 08:02:29 +00001778
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 if (mbedtls_mpi_bitlen(E) > MBEDTLS_MPI_MAX_BITS ||
1780 mbedtls_mpi_bitlen(N) > MBEDTLS_MPI_MAX_BITS) {
1781 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1782 }
Chris Jones9246d042020-11-25 15:12:39 +00001783
Paul Bakkerf6198c12012-05-16 08:02:29 +00001784 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001785 * Init temps and window size
1786 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 mpi_montg_init(&mm, N);
1788 mbedtls_mpi_init(&RR); mbedtls_mpi_init(&T);
1789 mbedtls_mpi_init(&Apos);
1790 mbedtls_mpi_init(&WW);
1791 memset(W, 0, sizeof(W));
Paul Bakker5121ce52009-01-03 21:22:43 +00001792
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 i = mbedtls_mpi_bitlen(E);
Paul Bakker5121ce52009-01-03 21:22:43 +00001794
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 window_bitsize = (i > 671) ? 6 : (i > 239) ? 5 :
1796 (i > 79) ? 4 : (i > 23) ? 3 : 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001797
Gilles Peskine449bd832023-01-11 14:50:10 +01001798#if (MBEDTLS_MPI_WINDOW_SIZE < 6)
1799 if (window_bitsize > MBEDTLS_MPI_WINDOW_SIZE) {
Janos Follath7fa11b82022-11-21 14:48:02 +00001800 window_bitsize = MBEDTLS_MPI_WINDOW_SIZE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 }
Peter Kolbuse6bcad32018-12-11 14:01:44 -06001802#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00001803
Janos Follathc8d66d52022-11-22 10:47:10 +00001804 const size_t w_table_used_size = (size_t) 1 << window_bitsize;
Janos Follath06000952022-11-22 10:18:06 +00001805
Paul Bakker5121ce52009-01-03 21:22:43 +00001806 /*
Janos Follathbe54ca72022-11-21 16:14:54 +00001807 * This function is not constant-trace: its memory accesses depend on the
1808 * exponent value. To defend against timing attacks, callers (such as RSA
1809 * and DHM) should use exponent blinding. However this is not enough if the
1810 * adversary can find the exponent in a single trace, so this function
1811 * takes extra precautions against adversaries who can observe memory
1812 * access patterns.
Janos Follathf08b40e2022-11-11 15:56:38 +00001813 *
Janos Follathbe54ca72022-11-21 16:14:54 +00001814 * This function performs a series of multiplications by table elements and
1815 * squarings, and we want the prevent the adversary from finding out which
1816 * table element was used, and from distinguishing between multiplications
1817 * and squarings. Firstly, when multiplying by an element of the window
1818 * W[i], we do a constant-trace table lookup to obfuscate i. This leaves
1819 * squarings as having a different memory access patterns from other
Gilles Peskinee6cb45e2023-08-10 15:59:28 +02001820 * multiplications. So secondly, we put the accumulator in the table as
1821 * well, and also do a constant-trace table lookup to multiply by the
1822 * accumulator which is W[x_index].
Janos Follathbe54ca72022-11-21 16:14:54 +00001823 *
1824 * This way, all multiplications take the form of a lookup-and-multiply.
1825 * The number of lookup-and-multiply operations inside each iteration of
1826 * the main loop still depends on the bits of the exponent, but since the
1827 * other operations in the loop don't have an easily recognizable memory
1828 * trace, an adversary is unlikely to be able to observe the exact
1829 * patterns.
1830 *
1831 * An adversary may still be able to recover the exponent if they can
1832 * observe both memory accesses and branches. However, branch prediction
1833 * exploitation typically requires many traces of execution over the same
1834 * data, which is defeated by randomized blinding.
Janos Follath8e7d6a02022-10-04 13:27:40 +01001835 */
Janos Follathc8d66d52022-11-22 10:47:10 +00001836 const size_t x_index = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 mbedtls_mpi_init(&W[x_index]);
Janos Follath84461482022-11-21 14:31:22 +00001838
Paul Bakker5121ce52009-01-03 21:22:43 +00001839 j = N->n + 1;
Gilles Peskinee6cb45e2023-08-10 15:59:28 +02001840 /* All W[i] including the accumulator must have at least N->n limbs for
1841 * the mpi_montmul() and mpi_montred() calls later. Here we ensure that
1842 * W[1] and the accumulator W[x_index] are large enough. later we'll grow
1843 * other W[i] to the same length. They must not be shrunk midway through
1844 * this function!
Paul Bakker5121ce52009-01-03 21:22:43 +00001845 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[x_index], j));
1847 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[1], j));
1848 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T, j * 2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001849
1850 /*
Paul Bakker50546922012-05-19 08:40:49 +00001851 * Compensate for negative A (and correct at the end)
1852 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 neg = (A->s == -1);
1854 if (neg) {
1855 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Apos, A));
Paul Bakker50546922012-05-19 08:40:49 +00001856 Apos.s = 1;
1857 A = &Apos;
1858 }
1859
1860 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001861 * If 1st call, pre-compute R^2 mod N
1862 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 if (prec_RR == NULL || prec_RR->p == NULL) {
1864 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&RR, 1));
1865 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&RR, N->n * 2 * biL));
1866 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&RR, &RR, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001867
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 if (prec_RR != NULL) {
1869 memcpy(prec_RR, &RR, sizeof(mbedtls_mpi));
1870 }
1871 } else {
1872 memcpy(&RR, prec_RR, sizeof(mbedtls_mpi));
Paul Bakker5121ce52009-01-03 21:22:43 +00001873 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001874
1875 /*
1876 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
1877 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 if (mbedtls_mpi_cmp_mpi(A, N) >= 0) {
1879 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&W[1], A, N));
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001880 /* This should be a no-op because W[1] is already that large before
1881 * mbedtls_mpi_mod_mpi(), but it's necessary to avoid an overflow
Tom Cosgrove93842842022-08-05 16:59:43 +01001882 * in mpi_montmul() below, so let's make sure. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[1], N->n + 1));
1884 } else {
1885 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[1], A));
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001886 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001887
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001888 /* Note that this is safe because W[1] always has at least N->n limbs
1889 * (it grew above and was preserved by mbedtls_mpi_copy()). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 mpi_montmul(&W[1], &RR, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001891
1892 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01001893 * W[x_index] = R^2 * R^-1 mod N = R mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001894 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[x_index], &RR));
1896 mpi_montred(&W[x_index], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001897
Janos Follathc8d66d52022-11-22 10:47:10 +00001898
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 if (window_bitsize > 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001900 /*
Janos Follath74601202022-11-21 15:54:20 +00001901 * W[i] = W[1] ^ i
1902 *
1903 * The first bit of the sliding window is always 1 and therefore we
1904 * only need to store the second half of the table.
Janos Follathc8d66d52022-11-22 10:47:10 +00001905 *
1906 * (There are two special elements in the table: W[0] for the
1907 * accumulator/result and W[1] for A in Montgomery form. Both of these
1908 * are already set at this point.)
Paul Bakker5121ce52009-01-03 21:22:43 +00001909 */
Janos Follath74601202022-11-21 15:54:20 +00001910 j = w_table_used_size / 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001911
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[j], N->n + 1));
1913 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[j], &W[1]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 for (i = 0; i < window_bitsize - 1; i++) {
1916 mpi_montmul(&W[j], &W[j], N, mm, &T);
1917 }
Paul Bakker0d7702c2013-10-29 16:18:35 +01001918
Paul Bakker5121ce52009-01-03 21:22:43 +00001919 /*
1920 * W[i] = W[i - 1] * W[1]
1921 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 for (i = j + 1; i < w_table_used_size; i++) {
1923 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[i], N->n + 1));
1924 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[i], &W[i - 1]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 mpi_montmul(&W[i], &W[1], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 }
1928 }
1929
1930 nblimbs = E->n;
1931 bufsize = 0;
1932 nbits = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001933 state = 0;
1934
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 while (1) {
1936 if (bufsize == 0) {
1937 if (nblimbs == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001938 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001940
Paul Bakker0d7702c2013-10-29 16:18:35 +01001941 nblimbs--;
1942
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 bufsize = sizeof(mbedtls_mpi_uint) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00001944 }
1945
1946 bufsize--;
1947
1948 ei = (E->p[nblimbs] >> bufsize) & 1;
1949
1950 /*
1951 * skip leading 0s
1952 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 if (ei == 0 && state == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001954 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001956
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 if (ei == 0 && state == 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001958 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01001959 * out of window, square W[x_index]
Paul Bakker5121ce52009-01-03 21:22:43 +00001960 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, x_index));
1962 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001963 continue;
1964 }
1965
1966 /*
1967 * add ei to current window
1968 */
1969 state = 2;
1970
1971 nbits++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 exponent_bits_in_window |= (ei << (window_bitsize - nbits));
Paul Bakker5121ce52009-01-03 21:22:43 +00001973
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 if (nbits == window_bitsize) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001975 /*
Janos Follath7fa11b82022-11-21 14:48:02 +00001976 * W[x_index] = W[x_index]^window_bitsize R^-1 mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001977 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 for (i = 0; i < window_bitsize; i++) {
1979 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size,
1980 x_index));
1981 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Janos Follathb764ee12022-10-04 14:00:09 +01001982 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001983
1984 /*
Janos Follath7fa11b82022-11-21 14:48:02 +00001985 * W[x_index] = W[x_index] * W[exponent_bits_in_window] R^-1 mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001986 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size,
1988 exponent_bits_in_window));
1989 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001990
1991 state--;
1992 nbits = 0;
Janos Follath7fa11b82022-11-21 14:48:02 +00001993 exponent_bits_in_window = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001994 }
1995 }
1996
1997 /*
1998 * process the remaining bits
1999 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 for (i = 0; i < nbits; i++) {
2001 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, x_index));
2002 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00002003
Janos Follath7fa11b82022-11-21 14:48:02 +00002004 exponent_bits_in_window <<= 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002005
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 if ((exponent_bits_in_window & ((size_t) 1 << window_bitsize)) != 0) {
2007 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, 1));
2008 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Janos Follathb764ee12022-10-04 14:00:09 +01002009 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002010 }
2011
2012 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01002013 * W[x_index] = A^E * R * R^-1 mod N = A^E mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00002014 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 mpi_montred(&W[x_index], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00002016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 if (neg && E->n != 0 && (E->p[0] & 1) != 0) {
Janos Follath8e7d6a02022-10-04 13:27:40 +01002018 W[x_index].s = -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&W[x_index], N, &W[x_index]));
Paul Bakkerf6198c12012-05-16 08:02:29 +00002020 }
2021
Janos Follath8e7d6a02022-10-04 13:27:40 +01002022 /*
2023 * Load the result in the output variable.
2024 */
Chien Wonge2caf412023-08-01 21:38:46 +08002025 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, &W[x_index]));
Janos Follath8e7d6a02022-10-04 13:27:40 +01002026
Paul Bakker5121ce52009-01-03 21:22:43 +00002027cleanup:
2028
Janos Follathb2c2fca2022-11-21 15:05:31 +00002029 /* The first bit of the sliding window is always 1 and therefore the first
2030 * half of the table was unused. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 for (i = w_table_used_size/2; i < w_table_used_size; i++) {
2032 mbedtls_mpi_free(&W[i]);
2033 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002034
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 mbedtls_mpi_free(&W[x_index]);
2036 mbedtls_mpi_free(&W[1]);
2037 mbedtls_mpi_free(&T);
2038 mbedtls_mpi_free(&Apos);
2039 mbedtls_mpi_free(&WW);
Paul Bakker6c591fa2011-05-05 11:49:20 +00002040
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 if (prec_RR == NULL || prec_RR->p == NULL) {
2042 mbedtls_mpi_free(&RR);
2043 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002044
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002046}
2047
Paul Bakker5121ce52009-01-03 21:22:43 +00002048/*
2049 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
2050 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002051int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00002052{
Janos Follath24eed8d2019-11-22 13:21:35 +00002053 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002054 size_t lz, lzt;
Alexander Ke8ad49f2019-08-16 16:16:07 +03002055 mbedtls_mpi TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00002056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 MPI_VALIDATE_RET(G != NULL);
2058 MPI_VALIDATE_RET(A != NULL);
2059 MPI_VALIDATE_RET(B != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00002060
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00002062
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A));
2064 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00002065
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 lz = mbedtls_mpi_lsb(&TA);
2067 lzt = mbedtls_mpi_lsb(&TB);
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002068
Gilles Peskine27253bc2021-06-09 13:26:43 +02002069 /* The loop below gives the correct result when A==0 but not when B==0.
2070 * So have a special case for B==0. Leverage the fact that we just
2071 * calculated the lsb and lsb(B)==0 iff B is odd or 0 to make the test
2072 * slightly more efficient than cmp_int(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 if (lzt == 0 && mbedtls_mpi_get_bit(&TB, 0) == 0) {
2074 ret = mbedtls_mpi_copy(G, A);
Gilles Peskine27253bc2021-06-09 13:26:43 +02002075 goto cleanup;
2076 }
2077
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 if (lzt < lz) {
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002079 lz = lzt;
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002081
Paul Bakker5121ce52009-01-03 21:22:43 +00002082 TA.s = TB.s = 1;
2083
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002084 /* We mostly follow the procedure described in HAC 14.54, but with some
2085 * minor differences:
2086 * - Sequences of multiplications or divisions by 2 are grouped into a
2087 * single shift operation.
Gilles Peskineb09c7ee2021-06-21 18:58:39 +02002088 * - The procedure in HAC assumes that 0 < TB <= TA.
2089 * - The condition TB <= TA is not actually necessary for correctness.
2090 * TA and TB have symmetric roles except for the loop termination
2091 * condition, and the shifts at the beginning of the loop body
2092 * remove any significance from the ordering of TA vs TB before
2093 * the shifts.
2094 * - If TA = 0, the loop goes through 0 iterations and the result is
2095 * correctly TB.
2096 * - The case TB = 0 was short-circuited above.
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002097 *
2098 * For the correctness proof below, decompose the original values of
2099 * A and B as
2100 * A = sa * 2^a * A' with A'=0 or A' odd, and sa = +-1
2101 * B = sb * 2^b * B' with B'=0 or B' odd, and sb = +-1
2102 * Then gcd(A, B) = 2^{min(a,b)} * gcd(A',B'),
2103 * and gcd(A',B') is odd or 0.
2104 *
2105 * At the beginning, we have TA = |A| and TB = |B| so gcd(A,B) = gcd(TA,TB).
2106 * The code maintains the following invariant:
2107 * gcd(A,B) = 2^k * gcd(TA,TB) for some k (I)
Gilles Peskine4df3f1f2021-06-15 22:09:39 +02002108 */
2109
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002110 /* Proof that the loop terminates:
2111 * At each iteration, either the right-shift by 1 is made on a nonzero
2112 * value and the nonnegative integer bitlen(TA) + bitlen(TB) decreases
2113 * by at least 1, or the right-shift by 1 is made on zero and then
2114 * TA becomes 0 which ends the loop (TB cannot be 0 if it is right-shifted
2115 * since in that case TB is calculated from TB-TA with the condition TB>TA).
2116 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 while (mbedtls_mpi_cmp_int(&TA, 0) != 0) {
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002118 /* Divisions by 2 preserve the invariant (I). */
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, mbedtls_mpi_lsb(&TA)));
2120 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, mbedtls_mpi_lsb(&TB)));
Paul Bakker5121ce52009-01-03 21:22:43 +00002121
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002122 /* Set either TA or TB to |TA-TB|/2. Since TA and TB are both odd,
2123 * TA-TB is even so the division by 2 has an integer result.
2124 * Invariant (I) is preserved since any odd divisor of both TA and TB
2125 * also divides |TA-TB|/2, and any odd divisor of both TA and |TA-TB|/2
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002126 * also divides TB, and any odd divisor of both TB and |TA-TB|/2 also
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002127 * divides TA.
2128 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 if (mbedtls_mpi_cmp_mpi(&TA, &TB) >= 0) {
2130 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TA, &TA, &TB));
2131 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, 1));
2132 } else {
2133 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TB, &TB, &TA));
2134 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 }
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002136 /* Note that one of TA or TB is still odd. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002137 }
2138
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002139 /* By invariant (I), gcd(A,B) = 2^k * gcd(TA,TB) for some k.
2140 * At the loop exit, TA = 0, so gcd(TA,TB) = TB.
2141 * - If there was at least one loop iteration, then one of TA or TB is odd,
2142 * and TA = 0, so TB is odd and gcd(TA,TB) = gcd(A',B'). In this case,
2143 * lz = min(a,b) so gcd(A,B) = 2^lz * TB.
2144 * - If there was no loop iteration, then A was 0, and gcd(A,B) = B.
Gilles Peskine4d3fd362021-06-21 11:40:38 +02002145 * In this case, lz = 0 and B = TB so gcd(A,B) = B = 2^lz * TB as well.
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002146 */
2147
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&TB, lz));
2149 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(G, &TB));
Paul Bakker5121ce52009-01-03 21:22:43 +00002150
2151cleanup:
2152
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00002154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002156}
2157
Paul Bakker33dc46b2014-04-30 16:11:39 +02002158/*
2159 * Fill X with size bytes of random.
Gilles Peskine22cdd0c2022-10-27 20:15:13 +02002160 * The bytes returned from the RNG are used in a specific order which
2161 * is suitable for deterministic ECDSA (see the specification of
2162 * mbedtls_mpi_random() and the implementation in mbedtls_mpi_fill_random()).
Paul Bakker33dc46b2014-04-30 16:11:39 +02002163 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002164int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
2165 int (*f_rng)(void *, unsigned char *, size_t),
2166 void *p_rng)
Paul Bakker287781a2011-03-26 13:18:49 +00002167{
Janos Follath24eed8d2019-11-22 13:21:35 +00002168 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 const size_t limbs = CHARS_TO_LIMBS(size);
Hanno Beckerda1655a2017-10-18 14:21:44 +01002170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 MPI_VALIDATE_RET(X != NULL);
2172 MPI_VALIDATE_RET(f_rng != NULL);
Paul Bakker33dc46b2014-04-30 16:11:39 +02002173
Hanno Beckerda1655a2017-10-18 14:21:44 +01002174 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
2176 if (size == 0) {
2177 return 0;
2178 }
Paul Bakker287781a2011-03-26 13:18:49 +00002179
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 ret = mbedtls_mpi_core_fill_random(X->p, X->n, size, f_rng, p_rng);
Paul Bakker287781a2011-03-26 13:18:49 +00002181
2182cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 return ret;
Paul Bakker287781a2011-03-26 13:18:49 +00002184}
2185
Gilles Peskine449bd832023-01-11 14:50:10 +01002186int mbedtls_mpi_random(mbedtls_mpi *X,
2187 mbedtls_mpi_sint min,
2188 const mbedtls_mpi *N,
2189 int (*f_rng)(void *, unsigned char *, size_t),
2190 void *p_rng)
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002191{
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 if (min < 0) {
2193 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2194 }
2195 if (mbedtls_mpi_cmp_int(N, min) <= 0) {
2196 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2197 }
Gilles Peskine1e918f42021-03-29 22:14:51 +02002198
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002199 /* Ensure that target MPI has exactly the same number of limbs
2200 * as the upper bound, even if the upper bound has leading zeros.
Gilles Peskine6b7ce962022-12-15 15:04:33 +01002201 * This is necessary for mbedtls_mpi_core_random. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 int ret = mbedtls_mpi_resize_clear(X, N->n);
2203 if (ret != 0) {
2204 return ret;
2205 }
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 return mbedtls_mpi_core_random(X->p, min, N->p, X->n, f_rng, p_rng);
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002208}
2209
Paul Bakker5121ce52009-01-03 21:22:43 +00002210/*
2211 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2212 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002213int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
Paul Bakker5121ce52009-01-03 21:22:43 +00002214{
Janos Follath24eed8d2019-11-22 13:21:35 +00002215 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 MPI_VALIDATE_RET(X != NULL);
2218 MPI_VALIDATE_RET(A != NULL);
2219 MPI_VALIDATE_RET(N != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00002220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 if (mbedtls_mpi_cmp_int(N, 1) <= 0) {
2222 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2223 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TU); mbedtls_mpi_init(&U1); mbedtls_mpi_init(&U2);
2226 mbedtls_mpi_init(&G); mbedtls_mpi_init(&TB); mbedtls_mpi_init(&TV);
2227 mbedtls_mpi_init(&V1); mbedtls_mpi_init(&V2);
Paul Bakker5121ce52009-01-03 21:22:43 +00002228
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, A, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002230
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002233 goto cleanup;
2234 }
2235
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&TA, A, N));
2237 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TU, &TA));
2238 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, N));
2239 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TV, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U1, 1));
2242 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U2, 0));
2243 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V1, 0));
2244 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 do {
2247 while ((TU.p[0] & 1) == 0) {
2248 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TU, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 if ((U1.p[0] & 1) != 0 || (U2.p[0] & 1) != 0) {
2251 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&U1, &U1, &TB));
2252 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &TA));
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 }
2254
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U1, 1));
2256 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 }
2258
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 while ((TV.p[0] & 1) == 0) {
2260 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TV, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 if ((V1.p[0] & 1) != 0 || (V2.p[0] & 1) != 0) {
2263 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, &TB));
2264 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &TA));
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 }
2266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V1, 1));
2268 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002269 }
2270
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 if (mbedtls_mpi_cmp_mpi(&TU, &TV) >= 0) {
2272 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TU, &TU, &TV));
2273 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U1, &U1, &V1));
2274 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &V2));
2275 } else {
2276 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TV, &TV, &TU));
2277 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, &U1));
2278 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &U2));
Paul Bakker5121ce52009-01-03 21:22:43 +00002279 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 } while (mbedtls_mpi_cmp_int(&TU, 0) != 0);
2281
2282 while (mbedtls_mpi_cmp_int(&V1, 0) < 0) {
2283 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 while (mbedtls_mpi_cmp_mpi(&V1, N) >= 0) {
2287 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, N));
2288 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, &V1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002291
2292cleanup:
2293
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TU); mbedtls_mpi_free(&U1); mbedtls_mpi_free(&U2);
2295 mbedtls_mpi_free(&G); mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TV);
2296 mbedtls_mpi_free(&V1); mbedtls_mpi_free(&V2);
Paul Bakker5121ce52009-01-03 21:22:43 +00002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002299}
2300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002302
Gilles Peskineb2bc1712019-02-08 17:27:11 +01002303/* Gaps between primes, starting at 3. https://oeis.org/A001223 */
2304static const unsigned char small_prime_gaps[] = {
2305 2, 2, 4, 2, 4, 2, 4, 6,
2306 2, 6, 4, 2, 4, 6, 6, 2,
2307 6, 4, 2, 6, 4, 6, 8, 4,
2308 2, 4, 2, 4, 14, 4, 6, 2,
2309 10, 2, 6, 6, 4, 6, 6, 2,
2310 10, 2, 4, 2, 12, 12, 4, 2,
2311 4, 6, 2, 10, 6, 6, 6, 2,
2312 6, 4, 2, 10, 14, 4, 2, 4,
2313 14, 6, 10, 2, 4, 6, 8, 6,
2314 6, 4, 6, 8, 4, 8, 10, 2,
2315 10, 2, 6, 4, 6, 8, 4, 2,
2316 4, 12, 8, 4, 8, 4, 6, 12,
2317 2, 18, 6, 10, 6, 6, 2, 6,
2318 10, 6, 6, 2, 6, 6, 4, 2,
2319 12, 10, 2, 4, 6, 6, 2, 12,
2320 4, 6, 8, 10, 8, 10, 8, 6,
2321 6, 4, 8, 6, 4, 8, 4, 14,
2322 10, 12, 2, 10, 2, 4, 2, 10,
2323 14, 4, 2, 4, 14, 4, 2, 4,
2324 20, 4, 8, 10, 8, 4, 6, 6,
2325 14, 4, 6, 6, 8, 6, /*reaches 997*/
Gilles Peskine30b03782023-08-22 11:06:47 +02002326 0 /* the last entry is effectively unused */
Paul Bakker5121ce52009-01-03 21:22:43 +00002327};
2328
2329/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002330 * Small divisors test (X must be positive)
2331 *
2332 * Return values:
2333 * 0: no small factor (possible prime, more tests needed)
2334 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002336 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002337 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002338static int mpi_check_small_factors(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +00002339{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002340 int ret = 0;
2341 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 mbedtls_mpi_uint r;
Gilles Peskineb2bc1712019-02-08 17:27:11 +01002343 unsigned p = 3; /* The first odd prime */
Paul Bakker5121ce52009-01-03 21:22:43 +00002344
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 if ((X->p[0] & 1) == 0) {
2346 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2347 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002348
Gilles Peskineb2bc1712019-02-08 17:27:11 +01002349 for (i = 0; i < sizeof(small_prime_gaps); p += small_prime_gaps[i], i++) {
2350 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, p));
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 if (r == 0) {
Gilles Peskineb2bc1712019-02-08 17:27:11 +01002352 if (mbedtls_mpi_cmp_int(X, p) == 0) {
2353 return 1;
2354 } else {
2355 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2356 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002358 }
2359
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002360cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 return ret;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002362}
2363
2364/*
2365 * Miller-Rabin pseudo-primality test (HAC 4.24)
2366 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002367static int mpi_miller_rabin(const mbedtls_mpi *X, size_t rounds,
2368 int (*f_rng)(void *, unsigned char *, size_t),
2369 void *p_rng)
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002370{
Pascal Junodb99183d2015-03-11 16:49:45 +01002371 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002372 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002374
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 MPI_VALIDATE_RET(X != NULL);
2376 MPI_VALIDATE_RET(f_rng != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00002377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 mbedtls_mpi_init(&W); mbedtls_mpi_init(&R);
2379 mbedtls_mpi_init(&T); mbedtls_mpi_init(&A);
2380 mbedtls_mpi_init(&RR);
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002381
Paul Bakker5121ce52009-01-03 21:22:43 +00002382 /*
2383 * W = |X| - 1
2384 * R = W >> lsb( W )
2385 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&W, X, 1));
2387 s = mbedtls_mpi_lsb(&W);
2388 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R, &W));
2389 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&R, s));
Paul Bakker5121ce52009-01-03 21:22:43 +00002390
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 for (i = 0; i < rounds; i++) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002392 /*
2393 * pick a random A, 1 < A < |X| - 1
2394 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002395 count = 0;
2396 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&A, X->n * ciL, f_rng, p_rng));
Pascal Junodb99183d2015-03-11 16:49:45 +01002398
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 j = mbedtls_mpi_bitlen(&A);
2400 k = mbedtls_mpi_bitlen(&W);
Pascal Junodb99183d2015-03-11 16:49:45 +01002401 if (j > k) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 A.p[A.n - 1] &= ((mbedtls_mpi_uint) 1 << (k - (A.n - 1) * biL - 1)) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002403 }
2404
2405 if (count++ > 30) {
Jens Wiklanderf08aa3e2019-01-17 13:30:57 +01002406 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2407 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002408 }
2409
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 } while (mbedtls_mpi_cmp_mpi(&A, &W) >= 0 ||
2411 mbedtls_mpi_cmp_int(&A, 1) <= 0);
Paul Bakker5121ce52009-01-03 21:22:43 +00002412
2413 /*
2414 * A = A^R mod |X|
2415 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&A, &A, &R, X, &RR));
Paul Bakker5121ce52009-01-03 21:22:43 +00002417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 if (mbedtls_mpi_cmp_mpi(&A, &W) == 0 ||
2419 mbedtls_mpi_cmp_int(&A, 1) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002420 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002422
2423 j = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 while (j < s && mbedtls_mpi_cmp_mpi(&A, &W) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002425 /*
2426 * A = A * A mod |X|
2427 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &A, &A));
2429 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&A, &T, X));
Paul Bakker5121ce52009-01-03 21:22:43 +00002430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 if (mbedtls_mpi_cmp_int(&A, 1) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002432 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002434
2435 j++;
2436 }
2437
2438 /*
2439 * not prime if A != |X| - 1 or A == 1
2440 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 if (mbedtls_mpi_cmp_mpi(&A, &W) != 0 ||
2442 mbedtls_mpi_cmp_int(&A, 1) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002444 break;
2445 }
2446 }
2447
2448cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 mbedtls_mpi_free(&W); mbedtls_mpi_free(&R);
2450 mbedtls_mpi_free(&T); mbedtls_mpi_free(&A);
2451 mbedtls_mpi_free(&RR);
Paul Bakker5121ce52009-01-03 21:22:43 +00002452
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002454}
2455
2456/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002457 * Pseudo-primality test: small factors, then Miller-Rabin
2458 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002459int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
2460 int (*f_rng)(void *, unsigned char *, size_t),
2461 void *p_rng)
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002462{
Janos Follath24eed8d2019-11-22 13:21:35 +00002463 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 mbedtls_mpi XX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 MPI_VALIDATE_RET(X != NULL);
2466 MPI_VALIDATE_RET(f_rng != NULL);
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002467
2468 XX.s = 1;
2469 XX.n = X->n;
2470 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002471
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 if (mbedtls_mpi_cmp_int(&XX, 0) == 0 ||
2473 mbedtls_mpi_cmp_int(&XX, 1) == 0) {
2474 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002475 }
2476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 if (mbedtls_mpi_cmp_int(&XX, 2) == 0) {
2478 return 0;
2479 }
2480
2481 if ((ret = mpi_check_small_factors(&XX)) != 0) {
2482 if (ret == 1) {
2483 return 0;
2484 }
2485
2486 return ret;
2487 }
2488
2489 return mpi_miller_rabin(&XX, rounds, f_rng, p_rng);
Janos Follathf301d232018-08-14 13:34:01 +01002490}
2491
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002492/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002494 *
Janos Follathf301d232018-08-14 13:34:01 +01002495 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2496 * be either 1024 bits or 1536 bits long, and flags must contain
2497 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002499int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
2500 int (*f_rng)(void *, unsigned char *, size_t),
2501 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00002502{
Jethro Beekman66689272018-02-14 19:24:10 -08002503#ifdef MBEDTLS_HAVE_INT64
2504// ceil(2^63.5)
2505#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2506#else
2507// ceil(2^31.5)
2508#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2509#endif
2510 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002511 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002512 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 mbedtls_mpi_uint r;
2514 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002515
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 MPI_VALIDATE_RET(X != NULL);
2517 MPI_VALIDATE_RET(f_rng != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00002518
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 if (nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS) {
2520 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2521 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 mbedtls_mpi_init(&Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00002524
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 n = BITS_TO_LIMBS(nbits);
Paul Bakker5121ce52009-01-03 21:22:43 +00002526
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR) == 0) {
Janos Follathda31fa12018-09-03 14:45:23 +01002528 /*
2529 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2530 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 rounds = ((nbits >= 1300) ? 2 : (nbits >= 850) ? 3 :
2532 (nbits >= 650) ? 4 : (nbits >= 350) ? 8 :
2533 (nbits >= 250) ? 12 : (nbits >= 150) ? 18 : 27);
2534 } else {
Janos Follathda31fa12018-09-03 14:45:23 +01002535 /*
2536 * 2^-100 error probability, number of rounds computed based on HAC,
2537 * fact 4.48
2538 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 rounds = ((nbits >= 1450) ? 4 : (nbits >= 1150) ? 5 :
2540 (nbits >= 1000) ? 6 : (nbits >= 850) ? 7 :
2541 (nbits >= 750) ? 8 : (nbits >= 500) ? 13 :
2542 (nbits >= 250) ? 28 : (nbits >= 150) ? 40 : 51);
Janos Follathda31fa12018-09-03 14:45:23 +01002543 }
2544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 while (1) {
2546 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(X, n * ciL, f_rng, p_rng));
Jethro Beekman66689272018-02-14 19:24:10 -08002547 /* make sure generated number is at least (nbits-1)+0.5 bits (FIPS 186-4 §B.3.3 steps 4.4, 5.5) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 if (X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2) {
2549 continue;
2550 }
Jethro Beekman66689272018-02-14 19:24:10 -08002551
2552 k = n * biL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 if (k > nbits) {
2554 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(X, k - nbits));
2555 }
Jethro Beekman66689272018-02-14 19:24:10 -08002556 X->p[0] |= 1;
2557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH) == 0) {
2559 ret = mbedtls_mpi_is_prime_ext(X, rounds, f_rng, p_rng);
Jethro Beekman66689272018-02-14 19:24:10 -08002560
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002562 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 }
2564 } else {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002565 /*
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01002566 * A necessary condition for Y and X = 2Y + 1 to be prime
Jethro Beekman66689272018-02-14 19:24:10 -08002567 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2568 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002569 */
Jethro Beekman66689272018-02-14 19:24:10 -08002570
2571 X->p[0] |= 2;
2572
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, 3));
2574 if (r == 0) {
2575 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 8));
2576 } else if (r == 1) {
2577 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 4));
2578 }
Jethro Beekman66689272018-02-14 19:24:10 -08002579
2580 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, X));
2582 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, 1));
Jethro Beekman66689272018-02-14 19:24:10 -08002583
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 while (1) {
Jethro Beekman66689272018-02-14 19:24:10 -08002585 /*
2586 * First, check small factors for X and Y
2587 * before doing Miller-Rabin on any of them
2588 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 if ((ret = mpi_check_small_factors(X)) == 0 &&
2590 (ret = mpi_check_small_factors(&Y)) == 0 &&
2591 (ret = mpi_miller_rabin(X, rounds, f_rng, p_rng))
2592 == 0 &&
2593 (ret = mpi_miller_rabin(&Y, rounds, f_rng, p_rng))
2594 == 0) {
Jethro Beekman66689272018-02-14 19:24:10 -08002595 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 }
Jethro Beekman66689272018-02-14 19:24:10 -08002597
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Jethro Beekman66689272018-02-14 19:24:10 -08002599 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 }
Jethro Beekman66689272018-02-14 19:24:10 -08002601
2602 /*
2603 * Next candidates. We want to preserve Y = (X-1) / 2 and
2604 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2605 * so up Y by 6 and X by 12.
2606 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 12));
2608 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&Y, &Y, 6));
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002610 }
2611 }
2612
2613cleanup:
2614
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 mbedtls_mpi_free(&Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00002616
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002618}
2619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002623
Paul Bakker23986e52011-04-24 08:57:21 +00002624#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002625
2626static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2627{
2628 { 693, 609, 21 },
2629 { 1764, 868, 28 },
2630 { 768454923, 542167814, 1 }
2631};
2632
Paul Bakker5121ce52009-01-03 21:22:43 +00002633/*
2634 * Checkup routine
2635 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002636int mbedtls_mpi_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002637{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002638 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00002640
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 mbedtls_mpi_init(&A); mbedtls_mpi_init(&E); mbedtls_mpi_init(&N); mbedtls_mpi_init(&X);
2642 mbedtls_mpi_init(&Y); mbedtls_mpi_init(&U); mbedtls_mpi_init(&V);
Paul Bakker5121ce52009-01-03 21:22:43 +00002643
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&A, 16,
2645 "EFE021C2645FD1DC586E69184AF4A31E" \
2646 "D5F53E93B5F123FA41680867BA110131" \
2647 "944FE7952E2517337780CB0DB80E61AA" \
2648 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002649
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&E, 16,
2651 "B2E7EFD37075B9F03FF989C7C5051C20" \
2652 "34D2A323810251127E7BF8625A4F49A5" \
2653 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
2654 "5B5C25763222FEFCCFC38B832366C29E"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002655
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&N, 16,
2657 "0066A198186C18C10B2F5ED9B522752A" \
2658 "9830B69916E535C8F047518A889A43A5" \
2659 "94B6BED27A168D31D4A52F88925AA8F5"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&X, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002662
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2664 "602AB7ECA597A3D6B56FF9829A5E8B85" \
2665 "9E857EA95A03512E2BAE7391688D264A" \
2666 "A5663B0341DB9CCFD2C4C5F421FEC814" \
2667 "8001B72E848A38CAE1C65F78E56ABDEF" \
2668 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
2669 "ECF677152EF804370C1A305CAF3B5BF1" \
2670 "30879B56C61DE584A0F53A2447A51E"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 if (verbose != 0) {
2673 mbedtls_printf(" MPI test #1 (mul_mpi): ");
2674 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002675
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2677 if (verbose != 0) {
2678 mbedtls_printf("failed\n");
2679 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002681 ret = 1;
2682 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002683 }
2684
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 if (verbose != 0) {
2686 mbedtls_printf("passed\n");
2687 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002688
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&X, &Y, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002690
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2692 "256567336059E52CAE22925474705F39A94"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002693
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&V, 16,
2695 "6613F26162223DF488E9CD48CC132C7A" \
2696 "0AC93C701B001B092E4E5B9F73BCD27B" \
2697 "9EE50D0657C77F374E903CDFA4C642"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002698
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 if (verbose != 0) {
2700 mbedtls_printf(" MPI test #2 (div_mpi): ");
2701 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002702
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0 ||
2704 mbedtls_mpi_cmp_mpi(&Y, &V) != 0) {
2705 if (verbose != 0) {
2706 mbedtls_printf("failed\n");
2707 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002709 ret = 1;
2710 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002711 }
2712
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 if (verbose != 0) {
2714 mbedtls_printf("passed\n");
2715 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&X, &A, &E, &N, NULL));
Paul Bakker5121ce52009-01-03 21:22:43 +00002718
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2720 "36E139AEA55215609D2816998ED020BB" \
2721 "BD96C37890F65171D948E9BC7CBAA4D9" \
2722 "325D24D6A3C12710F10A09FA08AB87"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 if (verbose != 0) {
2725 mbedtls_printf(" MPI test #3 (exp_mod): ");
2726 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002727
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2729 if (verbose != 0) {
2730 mbedtls_printf("failed\n");
2731 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002732
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002733 ret = 1;
2734 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002735 }
2736
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 if (verbose != 0) {
2738 mbedtls_printf("passed\n");
2739 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002740
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&X, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002742
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2744 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
2745 "C3DBA76456363A10869622EAC2DD84EC" \
2746 "C5B8A74DAC4D09E03B5E0BE779F2DF61"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002747
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 if (verbose != 0) {
2749 mbedtls_printf(" MPI test #4 (inv_mod): ");
2750 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002751
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2753 if (verbose != 0) {
2754 mbedtls_printf("failed\n");
2755 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002756
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002757 ret = 1;
2758 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002759 }
2760
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 if (verbose != 0) {
2762 mbedtls_printf("passed\n");
2763 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002764
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 if (verbose != 0) {
2766 mbedtls_printf(" MPI test #5 (simple gcd): ");
2767 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002768
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 for (i = 0; i < GCD_PAIR_COUNT; i++) {
2770 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&X, gcd_pairs[i][0]));
2771 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Y, gcd_pairs[i][1]));
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002772
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&A, &X, &Y));
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002774
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 if (mbedtls_mpi_cmp_int(&A, gcd_pairs[i][2]) != 0) {
2776 if (verbose != 0) {
2777 mbedtls_printf("failed at %d\n", i);
2778 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002779
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002780 ret = 1;
2781 goto cleanup;
2782 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002783 }
2784
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 if (verbose != 0) {
2786 mbedtls_printf("passed\n");
2787 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002788
Paul Bakker5121ce52009-01-03 21:22:43 +00002789cleanup:
2790
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 if (ret != 0 && verbose != 0) {
2792 mbedtls_printf("Unexpected error, return code = %08X\n", (unsigned int) ret);
2793 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002794
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 mbedtls_mpi_free(&A); mbedtls_mpi_free(&E); mbedtls_mpi_free(&N); mbedtls_mpi_free(&X);
2796 mbedtls_mpi_free(&Y); mbedtls_mpi_free(&U); mbedtls_mpi_free(&V);
Paul Bakker5121ce52009-01-03 21:22:43 +00002797
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 if (verbose != 0) {
2799 mbedtls_printf("\n");
2800 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002801
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002803}
2804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807#endif /* MBEDTLS_BIGNUM_C */