blob: 9b8591bbe7e9800b2ef232bc640bc23f3ef1c152 [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
Dave Rodgman7d4f0192023-05-09 14:01:05 +010040/*
41 * Compare signed values in constant time
42 */
43int mbedtls_mpi_lt_mpi_ct(const mbedtls_mpi *X,
44 const mbedtls_mpi *Y,
45 unsigned *ret)
46{
Dave Rodgman1f39f032023-08-01 09:19:16 +010047 mbedtls_ct_condition_t different_sign, X_is_negative, Y_is_negative, result;
Dave Rodgman7d4f0192023-05-09 14:01:05 +010048
Dave Rodgman7d4f0192023-05-09 14:01:05 +010049 if (X->n != Y->n) {
50 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
51 }
52
53 /*
Dave Rodgmanb69239c2023-08-09 14:53:18 +010054 * Set N_is_negative to MBEDTLS_CT_FALSE if N >= 0, MBEDTLS_CT_TRUE if N < 0.
Dave Rodgman7d4f0192023-05-09 14:01:05 +010055 * We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0.
56 */
Dave Rodgman1a7a5622023-05-17 13:47:56 +010057 X_is_negative = mbedtls_ct_bool((X->s & 2) >> 1);
58 Y_is_negative = mbedtls_ct_bool((Y->s & 2) >> 1);
Dave Rodgman7d4f0192023-05-09 14:01:05 +010059
60 /*
61 * If the signs are different, then the positive operand is the bigger.
62 * That is if X is negative (X_is_negative == 1), then X < Y is true and it
63 * is false if X is positive (X_is_negative == 0).
64 */
Dave Rodgman1cfc43c2023-09-19 16:18:59 +010065 different_sign = mbedtls_ct_bool_ne(X_is_negative, Y_is_negative); // true if different sign
Dave Rodgman1f39f032023-08-01 09:19:16 +010066 result = mbedtls_ct_bool_and(different_sign, X_is_negative);
Dave Rodgman7d4f0192023-05-09 14:01:05 +010067
Dave Rodgman32d72602023-07-31 12:28:05 +010068 /*
69 * Assuming signs are the same, compare X and Y. We switch the comparison
Dave Rodgman1a7a5622023-05-17 13:47:56 +010070 * order if they are negative so that we get the right result, regardles of
71 * sign.
Dave Rodgman7d4f0192023-05-09 14:01:05 +010072 */
Dave Rodgman7d4f0192023-05-09 14:01:05 +010073
Dave Rodgman32d72602023-07-31 12:28:05 +010074 /* This array is used to conditionally swap the pointers in const time */
Dave Rodgman1a7a5622023-05-17 13:47:56 +010075 void * const p[2] = { X->p, Y->p };
Dave Rodgman98ddc012023-08-10 12:11:31 +010076 size_t i = mbedtls_ct_size_if_else_0(X_is_negative, 1);
Dave Rodgman2c764842023-05-18 13:28:21 +010077 mbedtls_ct_condition_t lt = mbedtls_mpi_core_lt_ct(p[i], p[i ^ 1], X->n);
Dave Rodgman7d4f0192023-05-09 14:01:05 +010078
Dave Rodgman32d72602023-07-31 12:28:05 +010079 /*
Dave Rodgman1f39f032023-08-01 09:19:16 +010080 * Store in result iff the signs are the same (i.e., iff different_sign == false). If
Dave Rodgman32d72602023-07-31 12:28:05 +010081 * the signs differ, result has already been set, so we don't change it.
82 */
Dave Rodgman1f39f032023-08-01 09:19:16 +010083 result = mbedtls_ct_bool_or(result,
84 mbedtls_ct_bool_and(mbedtls_ct_bool_not(different_sign), lt));
Dave Rodgman1a7a5622023-05-17 13:47:56 +010085
Dave Rodgman98ddc012023-08-10 12:11:31 +010086 *ret = mbedtls_ct_uint_if_else_0(result, 1);
Dave Rodgman7d4f0192023-05-09 14:01:05 +010087
88 return 0;
89}
90
91/*
92 * Conditionally assign X = Y, without leaking information
93 * about whether the assignment was made or not.
94 * (Leaking information about the respective sizes of X and Y is ok however.)
95 */
Dave Rodgman0a487172023-09-15 11:52:06 +010096#if defined(_MSC_VER) && defined(MBEDTLS_PLATFORM_IS_WINDOWS_ON_ARM64) && \
97 (_MSC_FULL_VER < 193131103)
Dave Rodgman7d4f0192023-05-09 14:01:05 +010098/*
99 * MSVC miscompiles this function if it's inlined prior to Visual Studio 2022 version 17.1. See:
100 * https://developercommunity.visualstudio.com/t/c-compiler-miscompiles-part-of-mbedtls-library-on/1646989
101 */
102__declspec(noinline)
103#endif
104int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X,
105 const mbedtls_mpi *Y,
106 unsigned char assign)
107{
108 int ret = 0;
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100109
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100110 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, Y->n));
111
Dave Rodgman7e9af052023-09-28 17:08:16 +0100112 {
113 mbedtls_ct_condition_t do_assign = mbedtls_ct_bool(assign);
Dave Rodgman589ccb82023-05-17 13:55:01 +0100114
Dave Rodgman7e9af052023-09-28 17:08:16 +0100115 X->s = (int) mbedtls_ct_uint_if(do_assign, Y->s, X->s);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100116
Dave Rodgman7e9af052023-09-28 17:08:16 +0100117 mbedtls_mpi_core_cond_assign(X->p, Y->p, Y->n, do_assign);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100118
Dave Rodgman7e9af052023-09-28 17:08:16 +0100119 mbedtls_ct_condition_t do_not_assign = mbedtls_ct_bool_not(do_assign);
120 for (size_t i = Y->n; i < X->n; i++) {
121 X->p[i] = mbedtls_ct_mpi_uint_if_else_0(do_not_assign, X->p[i]);
122 }
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100123 }
124
125cleanup:
126 return ret;
127}
128
129/*
130 * Conditionally swap X and Y, without leaking information
131 * about whether the swap was made or not.
132 * Here it is not ok to simply swap the pointers, which would lead to
133 * different memory access patterns when X and Y are used afterwards.
134 */
135int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X,
136 mbedtls_mpi *Y,
137 unsigned char swap)
138{
139 int ret = 0;
140 int s;
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100141
142 if (X == Y) {
143 return 0;
144 }
145
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100146 mbedtls_ct_condition_t do_swap = mbedtls_ct_bool(swap);
147
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100148 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, Y->n));
149 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Y, X->n));
150
151 s = X->s;
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100152 X->s = (int) mbedtls_ct_uint_if(do_swap, Y->s, X->s);
153 Y->s = (int) mbedtls_ct_uint_if(do_swap, s, Y->s);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100154
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100155 mbedtls_mpi_core_cond_swap(X->p, Y->p, X->n, do_swap);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100156
157cleanup:
158 return ret;
159}
160
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500161/* Implementation that should never be optimized out by the compiler */
Tom Cosgrovebc345e82023-07-25 15:17:39 +0100162#define mbedtls_mpi_zeroize_and_free(v, n) mbedtls_zeroize_and_free(v, ciL * (n))
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500163
Paul Bakker5121ce52009-01-03 21:22:43 +0000164/*
Paul Bakker6c591fa2011-05-05 11:49:20 +0000165 * Initialize one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100167void mbedtls_mpi_init(mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000168{
Paul Bakker6c591fa2011-05-05 11:49:20 +0000169 X->s = 1;
170 X->n = 0;
171 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000172}
173
174/*
Paul Bakker6c591fa2011-05-05 11:49:20 +0000175 * Unallocate one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100177void mbedtls_mpi_free(mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000178{
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 if (X == NULL) {
Paul Bakker6c591fa2011-05-05 11:49:20 +0000180 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 if (X->p != NULL) {
Tom Cosgrove46259f62023-07-18 16:44:14 +0100184 mbedtls_mpi_zeroize_and_free(X->p, X->n);
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 }
186
Paul Bakker6c591fa2011-05-05 11:49:20 +0000187 X->s = 1;
188 X->n = 0;
189 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000190}
191
192/*
193 * Enlarge to the specified number of limbs
194 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100195int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs)
Paul Bakker5121ce52009-01-03 21:22:43 +0000196{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_mpi_uint *p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
200 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
201 }
Paul Bakkerf9688572011-05-05 10:00:45 +0000202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (X->n < nblimbs) {
204 if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(nblimbs, ciL)) == NULL) {
205 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
206 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 if (X->p != NULL) {
209 memcpy(p, X->p, X->n * ciL);
Tom Cosgrove46259f62023-07-18 16:44:14 +0100210 mbedtls_mpi_zeroize_and_free(X->p, X->n);
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 }
212
Gilles Peskine053022f2023-06-29 19:26:48 +0200213 /* nblimbs fits in n because we ensure that MBEDTLS_MPI_MAX_LIMBS
214 * fits, and we've checked that nblimbs <= MBEDTLS_MPI_MAX_LIMBS. */
215 X->n = (unsigned short) nblimbs;
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 X->p = p;
217 }
218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000220}
221
222/*
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100223 * Resize down as much as possible,
224 * while keeping at least the specified number of limbs
225 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100226int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs)
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 mbedtls_mpi_uint *p;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100229 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
232 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
233 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100234
Gilles Peskinee2f563e2020-01-20 21:17:43 +0100235 /* Actually resize up if there are currently fewer than nblimbs limbs. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 if (X->n <= nblimbs) {
237 return mbedtls_mpi_grow(X, nblimbs);
238 }
Gilles Peskine322752b2020-01-21 13:59:51 +0100239 /* After this point, then X->n > nblimbs and in particular X->n > 0. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 for (i = X->n - 1; i > 0; i--) {
242 if (X->p[i] != 0) {
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100243 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 }
245 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100246 i++;
247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 if (i < nblimbs) {
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100249 i = nblimbs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(i, ciL)) == NULL) {
253 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
254 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 if (X->p != NULL) {
257 memcpy(p, X->p, i * ciL);
Tom Cosgrove46259f62023-07-18 16:44:14 +0100258 mbedtls_mpi_zeroize_and_free(X->p, X->n);
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100259 }
260
Gilles Peskine053022f2023-06-29 19:26:48 +0200261 /* i fits in n because we ensure that MBEDTLS_MPI_MAX_LIMBS
262 * fits, and we've checked that i <= nblimbs <= MBEDTLS_MPI_MAX_LIMBS. */
263 X->n = (unsigned short) i;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100264 X->p = p;
265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 return 0;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100267}
268
Gilles Peskineed32b572021-06-02 22:17:52 +0200269/* Resize X to have exactly n limbs and set it to 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100270static int mbedtls_mpi_resize_clear(mbedtls_mpi *X, size_t limbs)
Gilles Peskineed32b572021-06-02 22:17:52 +0200271{
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (limbs == 0) {
273 mbedtls_mpi_free(X);
274 return 0;
275 } else if (X->n == limbs) {
276 memset(X->p, 0, limbs * ciL);
Gilles Peskineed32b572021-06-02 22:17:52 +0200277 X->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 return 0;
279 } else {
280 mbedtls_mpi_free(X);
281 return mbedtls_mpi_grow(X, limbs);
Gilles Peskineed32b572021-06-02 22:17:52 +0200282 }
283}
284
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100285/*
Gilles Peskine3da1a8f2021-06-08 23:17:42 +0200286 * Copy the contents of Y into X.
287 *
288 * This function is not constant-time. Leading zeros in Y may be removed.
289 *
290 * Ensure that X does not shrink. This is not guaranteed by the public API,
291 * but some code in the bignum module relies on this property, for example
292 * in mbedtls_mpi_exp_mod().
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100294int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000295{
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100296 int ret = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000297 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 if (X == Y) {
300 return 0;
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200301 }
302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 if (Y->n == 0) {
304 if (X->n != 0) {
305 X->s = 1;
306 memset(X->p, 0, X->n * ciL);
307 }
308 return 0;
309 }
310
311 for (i = Y->n - 1; i > 0; i--) {
312 if (Y->p[i] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 }
315 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 i++;
317
318 X->s = Y->s;
319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if (X->n < i) {
321 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i));
322 } else {
323 memset(X->p + i, 0, (X->n - i) * ciL);
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100324 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 memcpy(X->p, Y->p, i * ciL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000327
328cleanup:
329
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000331}
332
333/*
334 * Swap the contents of X and Y
335 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100336void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000337{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 memcpy(&T, X, sizeof(mbedtls_mpi));
341 memcpy(X, Y, sizeof(mbedtls_mpi));
342 memcpy(Y, &T, sizeof(mbedtls_mpi));
Paul Bakker5121ce52009-01-03 21:22:43 +0000343}
344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345static inline mbedtls_mpi_uint mpi_sint_abs(mbedtls_mpi_sint z)
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100346{
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (z >= 0) {
348 return z;
349 }
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100350 /* Take care to handle the most negative value (-2^(biL-1)) correctly.
351 * A naive -z would have undefined behavior.
352 * Write this in a way that makes popular compilers happy (GCC, Clang,
353 * MSVC). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 return (mbedtls_mpi_uint) 0 - (mbedtls_mpi_uint) z;
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100355}
356
Dave Rodgmanf3df1052023-08-09 18:55:41 +0100357/* Convert x to a sign, i.e. to 1, if x is positive, or -1, if x is negative.
358 * This looks awkward but generates smaller code than (x < 0 ? -1 : 1) */
Dave Rodgman73d85912023-09-28 17:00:50 +0100359#define TO_SIGN(x) ((mbedtls_mpi_sint) (((mbedtls_mpi_uint) x) >> (biL - 1)) * -2 + 1)
Dave Rodgmanf3df1052023-08-09 18:55:41 +0100360
Paul Bakker5121ce52009-01-03 21:22:43 +0000361/*
362 * Set value from integer
363 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100364int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z)
Paul Bakker5121ce52009-01-03 21:22:43 +0000365{
Janos Follath24eed8d2019-11-22 13:21:35 +0000366 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, 1));
369 memset(X->p, 0, X->n * ciL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 X->p[0] = mpi_sint_abs(z);
Dave Rodgmanf3df1052023-08-09 18:55:41 +0100372 X->s = TO_SIGN(z);
Paul Bakker5121ce52009-01-03 21:22:43 +0000373
374cleanup:
375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000377}
378
379/*
Paul Bakker2f5947e2011-05-18 15:47:11 +0000380 * Get a specific bit
381 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100382int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
Paul Bakker2f5947e2011-05-18 15:47:11 +0000383{
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if (X->n * biL <= pos) {
385 return 0;
386 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 return (X->p[pos / biL] >> (pos % biL)) & 0x01;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000389}
390
391/*
392 * Set a bit to a specific value of 0 or 1
393 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100394int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val)
Paul Bakker2f5947e2011-05-18 15:47:11 +0000395{
396 int ret = 0;
397 size_t off = pos / biL;
398 size_t idx = pos % biL;
399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if (val != 0 && val != 1) {
401 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000402 }
403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 if (X->n * biL <= pos) {
405 if (val == 0) {
406 return 0;
407 }
408
409 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, off + 1));
410 }
411
412 X->p[off] &= ~((mbedtls_mpi_uint) 0x01 << idx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 X->p[off] |= (mbedtls_mpi_uint) val << idx;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000414
415cleanup:
Paul Bakker9af723c2014-05-01 13:03:14 +0200416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 return ret;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000418}
419
420/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200421 * Return the number of less significant zero-bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100423size_t mbedtls_mpi_lsb(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000424{
Dave Rodgmanfa703e32023-08-09 18:56:07 +0100425 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
Dave Rodgmanfa703e32023-08-09 18:56:07 +0100427#if defined(__has_builtin)
428#if (MBEDTLS_MPI_UINT_MAX == UINT_MAX) && __has_builtin(__builtin_ctz)
429 #define mbedtls_mpi_uint_ctz __builtin_ctz
430#elif (MBEDTLS_MPI_UINT_MAX == ULONG_MAX) && __has_builtin(__builtin_ctzl)
431 #define mbedtls_mpi_uint_ctz __builtin_ctzl
432#elif (MBEDTLS_MPI_UINT_MAX == ULLONG_MAX) && __has_builtin(__builtin_ctzll)
433 #define mbedtls_mpi_uint_ctz __builtin_ctzll
434#endif
435#endif
436
437#if defined(mbedtls_mpi_uint_ctz)
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 for (i = 0; i < X->n; i++) {
Dave Rodgman960eca92023-08-09 20:43:18 +0100439 if (X->p[i] != 0) {
440 return i * biL + mbedtls_mpi_uint_ctz(X->p[i]);
441 }
Dave Rodgmanfa703e32023-08-09 18:56:07 +0100442 }
443#else
444 size_t count = 0;
445 for (i = 0; i < X->n; i++) {
446 for (size_t j = 0; j < biL; j++, count++) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if (((X->p[i] >> j) & 1) != 0) {
448 return count;
449 }
450 }
451 }
Dave Rodgmanfa703e32023-08-09 18:56:07 +0100452#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000455}
456
457/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200458 * Return the number of bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100460size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000461{
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 return mbedtls_mpi_core_bitlen(X->p, X->n);
Paul Bakker5121ce52009-01-03 21:22:43 +0000463}
464
465/*
466 * Return the total size in bytes
467 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100468size_t mbedtls_mpi_size(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000469{
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 return (mbedtls_mpi_bitlen(X) + 7) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000471}
472
473/*
474 * Convert an ASCII character to digit value
475 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100476static int mpi_get_digit(mbedtls_mpi_uint *d, int radix, char c)
Paul Bakker5121ce52009-01-03 21:22:43 +0000477{
478 *d = 255;
479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 if (c >= 0x30 && c <= 0x39) {
481 *d = c - 0x30;
482 }
483 if (c >= 0x41 && c <= 0x46) {
484 *d = c - 0x37;
485 }
486 if (c >= 0x61 && c <= 0x66) {
487 *d = c - 0x57;
488 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if (*d >= (mbedtls_mpi_uint) radix) {
491 return MBEDTLS_ERR_MPI_INVALID_CHARACTER;
492 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000495}
496
497/*
498 * Import from an ASCII string
499 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100500int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s)
Paul Bakker5121ce52009-01-03 21:22:43 +0000501{
Janos Follath24eed8d2019-11-22 13:21:35 +0000502 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000503 size_t i, j, slen, n;
Gilles Peskine80f56732021-04-03 18:26:13 +0200504 int sign = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_mpi_uint d;
506 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 if (radix < 2 || radix > 16) {
509 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskine7cba8592021-06-08 18:32:34 +0200510 }
511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 mbedtls_mpi_init(&T);
513
514 if (s[0] == 0) {
515 mbedtls_mpi_free(X);
516 return 0;
517 }
518
519 if (s[0] == '-') {
Gilles Peskine80f56732021-04-03 18:26:13 +0200520 ++s;
521 sign = -1;
522 }
523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 slen = strlen(s);
Paul Bakkerff60ee62010-03-16 21:09:09 +0000525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 if (radix == 16) {
Dave Rodgman68ef1d62023-05-18 20:49:03 +0100527 if (slen > SIZE_MAX >> 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 n = BITS_TO_LIMBS(slen << 2);
532
533 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n));
534 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
535
536 for (i = slen, j = 0; i > 0; i--, j++) {
537 MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i - 1]));
538 X->p[j / (2 * ciL)] |= d << ((j % (2 * ciL)) << 2);
539 }
540 } else {
541 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
542
543 for (i = 0; i < slen; i++) {
544 MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i]));
545 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T, X, radix));
546 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, &T, d));
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 }
548 }
549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 if (sign < 0 && mbedtls_mpi_bitlen(X) != 0) {
Gilles Peskine80f56732021-04-03 18:26:13 +0200551 X->s = -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 }
Gilles Peskine80f56732021-04-03 18:26:13 +0200553
Paul Bakker5121ce52009-01-03 21:22:43 +0000554cleanup:
555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000559}
560
561/*
Ron Eldora16fa292018-11-20 14:07:01 +0200562 * Helper to write the digits high-order first.
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100564static int mpi_write_hlp(mbedtls_mpi *X, int radix,
565 char **p, const size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000566{
Janos Follath24eed8d2019-11-22 13:21:35 +0000567 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_mpi_uint r;
Ron Eldora16fa292018-11-20 14:07:01 +0200569 size_t length = 0;
570 char *p_end = *p + buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 do {
573 if (length >= buflen) {
574 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
Ron Eldora16fa292018-11-20 14:07:01 +0200575 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, radix));
578 MBEDTLS_MPI_CHK(mbedtls_mpi_div_int(X, NULL, X, radix));
Ron Eldora16fa292018-11-20 14:07:01 +0200579 /*
580 * Write the residue in the current position, as an ASCII character.
581 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if (r < 0xA) {
583 *(--p_end) = (char) ('0' + r);
584 } else {
585 *(--p_end) = (char) ('A' + (r - 0xA));
586 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000587
Ron Eldora16fa292018-11-20 14:07:01 +0200588 length++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 } while (mbedtls_mpi_cmp_int(X, 0) != 0);
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 memmove(*p, p_end, length);
Ron Eldora16fa292018-11-20 14:07:01 +0200592 *p += length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000593
594cleanup:
595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000597}
598
599/*
600 * Export into an ASCII string
601 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100602int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
603 char *buf, size_t buflen, size_t *olen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000604{
Paul Bakker23986e52011-04-24 08:57:21 +0000605 int ret = 0;
606 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000607 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 if (radix < 2 || radix > 16) {
611 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
612 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 n = mbedtls_mpi_bitlen(X); /* Number of bits necessary to present `n`. */
615 if (radix >= 4) {
616 n >>= 1; /* Number of 4-adic digits necessary to present
Hanno Becker23cfea02019-02-04 09:45:07 +0000617 * `n`. If radix > 4, this might be a strict
618 * overapproximation of the number of
619 * radix-adic digits needed to present `n`. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 }
621 if (radix >= 16) {
622 n >>= 1; /* Number of hexadecimal digits necessary to
Hanno Becker23cfea02019-02-04 09:45:07 +0000623 * present `n`. */
624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 }
Janos Follath80470622019-03-06 13:43:02 +0000626 n += 1; /* Terminating null byte */
Hanno Becker23cfea02019-02-04 09:45:07 +0000627 n += 1; /* Compensate for the divisions above, which round down `n`
628 * in case it's not even. */
629 n += 1; /* Potential '-'-sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 n += (n & 1); /* Make n even to have enough space for hexadecimal writing,
Hanno Becker23cfea02019-02-04 09:45:07 +0000631 * which always uses an even number of hex-digits. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if (buflen < n) {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100634 *olen = n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000636 }
637
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100638 p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if (X->s == -1) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 *p++ = '-';
Hanno Beckerc983c812019-02-01 16:41:30 +0000643 buflen--;
644 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if (radix == 16) {
Paul Bakker23986e52011-04-24 08:57:21 +0000647 int c;
648 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 for (i = X->n, k = 0; i > 0; i--) {
651 for (j = ciL; j > 0; j--) {
652 c = (X->p[i - 1] >> ((j - 1) << 3)) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (c == 0 && k == 0 && (i + j) != 2) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000657
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000658 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000659 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000660 k = 1;
661 }
662 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 } else {
664 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T, X));
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (T.s == -1) {
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000667 T.s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 MBEDTLS_MPI_CHK(mpi_write_hlp(&T, radix, &p, buflen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 }
672
673 *p++ = '\0';
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000674 *olen = (size_t) (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000675
676cleanup:
677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000681}
682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000684/*
685 * Read X from an opened file
686 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100687int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin)
Paul Bakker5121ce52009-01-03 21:22:43 +0000688{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 mbedtls_mpi_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000690 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000691 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000692 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000693 * Buffer should have space for (short) label and decimal formatted MPI,
694 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000695 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (radix < 2 || radix > 16) {
699 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
700 }
Hanno Becker73d7d792018-12-11 10:35:51 +0000701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 memset(s, 0, sizeof(s));
703 if (fgets(s, sizeof(s) - 1, fin) == NULL) {
704 return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
705 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 slen = strlen(s);
708 if (slen == sizeof(s) - 2) {
709 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
710 }
Paul Bakkercb37aa52011-11-30 16:00:20 +0000711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (slen > 0 && s[slen - 1] == '\n') {
713 slen--; s[slen] = '\0';
714 }
715 if (slen > 0 && s[slen - 1] == '\r') {
716 slen--; s[slen] = '\0';
717 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000718
719 p = s + slen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 while (p-- > s) {
721 if (mpi_get_digit(&d, radix, *p) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000722 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 }
724 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000725
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 return mbedtls_mpi_read_string(X, radix, p + 1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000727}
728
729/*
730 * Write X into an opened file (or stdout if fout == NULL)
731 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100732int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE *fout)
Paul Bakker5121ce52009-01-03 21:22:43 +0000733{
Janos Follath24eed8d2019-11-22 13:21:35 +0000734 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000735 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000736 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000737 * Buffer should have space for (short) label and decimal formatted MPI,
738 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000739 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
Hanno Becker73d7d792018-12-11 10:35:51 +0000741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 if (radix < 2 || radix > 16) {
743 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
744 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 memset(s, 0, sizeof(s));
Paul Bakker5121ce52009-01-03 21:22:43 +0000747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 MBEDTLS_MPI_CHK(mbedtls_mpi_write_string(X, radix, s, sizeof(s) - 2, &n));
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 if (p == NULL) {
751 p = "";
752 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 plen = strlen(p);
755 slen = strlen(s);
Paul Bakker5121ce52009-01-03 21:22:43 +0000756 s[slen++] = '\r';
757 s[slen++] = '\n';
758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 if (fout != NULL) {
760 if (fwrite(p, 1, plen, fout) != plen ||
761 fwrite(s, 1, slen, fout) != slen) {
762 return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
763 }
764 } else {
765 mbedtls_printf("%s%s", p, s);
Paul Bakker5121ce52009-01-03 21:22:43 +0000766 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000767
768cleanup:
769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000771}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000773
774/*
Janos Follatha778a942019-02-13 10:28:28 +0000775 * Import X from unsigned binary data, little endian
Przemyslaw Stekiel76960a72022-02-21 13:42:09 +0100776 *
777 * This function is guaranteed to return an MPI with exactly the necessary
778 * number of limbs (in particular, it does not skip 0s in the input).
Janos Follatha778a942019-02-13 10:28:28 +0000779 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100780int mbedtls_mpi_read_binary_le(mbedtls_mpi *X,
781 const unsigned char *buf, size_t buflen)
Janos Follatha778a942019-02-13 10:28:28 +0000782{
Janos Follath24eed8d2019-11-22 13:21:35 +0000783 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 const size_t limbs = CHARS_TO_LIMBS(buflen);
Janos Follatha778a942019-02-13 10:28:28 +0000785
786 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
Janos Follatha778a942019-02-13 10:28:28 +0000788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_le(X->p, X->n, buf, buflen));
Janos Follatha778a942019-02-13 10:28:28 +0000790
791cleanup:
792
Janos Follath171a7ef2019-02-15 16:17:45 +0000793 /*
794 * This function is also used to import keys. However, wiping the buffers
795 * upon failure is not necessary because failure only can happen before any
796 * input is copied.
797 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 return ret;
Janos Follatha778a942019-02-13 10:28:28 +0000799}
800
801/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000802 * Import X from unsigned binary data, big endian
Przemyslaw Stekiel76960a72022-02-21 13:42:09 +0100803 *
804 * This function is guaranteed to return an MPI with exactly the necessary
805 * number of limbs (in particular, it does not skip 0s in the input).
Paul Bakker5121ce52009-01-03 21:22:43 +0000806 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100807int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000808{
Janos Follath24eed8d2019-11-22 13:21:35 +0000809 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 const size_t limbs = CHARS_TO_LIMBS(buflen);
Hanno Becker73d7d792018-12-11 10:35:51 +0000811
Hanno Becker073c1992017-10-17 15:17:27 +0100812 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
Paul Bakker5121ce52009-01-03 21:22:43 +0000814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_be(X->p, X->n, buf, buflen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000816
817cleanup:
818
Janos Follath171a7ef2019-02-15 16:17:45 +0000819 /*
820 * This function is also used to import keys. However, wiping the buffers
821 * upon failure is not necessary because failure only can happen before any
822 * input is copied.
823 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000825}
826
827/*
Janos Follathe344d0f2019-02-19 16:17:40 +0000828 * Export X into unsigned binary data, little endian
829 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100830int mbedtls_mpi_write_binary_le(const mbedtls_mpi *X,
831 unsigned char *buf, size_t buflen)
Janos Follathe344d0f2019-02-19 16:17:40 +0000832{
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 return mbedtls_mpi_core_write_le(X->p, X->n, buf, buflen);
Janos Follathe344d0f2019-02-19 16:17:40 +0000834}
835
836/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000837 * Export X into unsigned binary data, big endian
838 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100839int mbedtls_mpi_write_binary(const mbedtls_mpi *X,
840 unsigned char *buf, size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000841{
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 return mbedtls_mpi_core_write_be(X->p, X->n, buf, buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000843}
844
845/*
846 * Left-shift: X <<= count
847 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100848int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
Paul Bakker5121ce52009-01-03 21:22:43 +0000849{
Janos Follath24eed8d2019-11-22 13:21:35 +0000850 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Minos Galanakis0144b352023-05-02 14:02:32 +0100851 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000852
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 i = mbedtls_mpi_bitlen(X) + count;
Paul Bakker5121ce52009-01-03 21:22:43 +0000854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if (X->n * biL < i) {
856 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, BITS_TO_LIMBS(i)));
857 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000858
859 ret = 0;
860
Minos Galanakis0144b352023-05-02 14:02:32 +0100861 mbedtls_mpi_core_shift_l(X->p, X->n, count);
Paul Bakker5121ce52009-01-03 21:22:43 +0000862cleanup:
863
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000865}
866
867/*
868 * Right-shift: X >>= count
869 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100870int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
Paul Bakker5121ce52009-01-03 21:22:43 +0000871{
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 if (X->n != 0) {
873 mbedtls_mpi_core_shift_r(X->p, X->n, count);
874 }
875 return 0;
Gilles Peskine66414202022-09-21 15:36:16 +0200876}
877
Paul Bakker5121ce52009-01-03 21:22:43 +0000878/*
879 * Compare unsigned values
880 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100881int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000882{
Paul Bakker23986e52011-04-24 08:57:21 +0000883 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +0000884
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 for (i = X->n; i > 0; i--) {
886 if (X->p[i - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000887 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000889 }
890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 for (j = Y->n; j > 0; j--) {
892 if (Y->p[j - 1] != 0) {
893 break;
894 }
895 }
896
Dave Rodgmanebcd7852023-08-09 18:56:42 +0100897 /* If i == j == 0, i.e. abs(X) == abs(Y),
898 * we end up returning 0 at the end of the function. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100899
900 if (i > j) {
901 return 1;
902 }
903 if (j > i) {
904 return -1;
905 }
906
907 for (; i > 0; i--) {
908 if (X->p[i - 1] > Y->p[i - 1]) {
909 return 1;
910 }
911 if (X->p[i - 1] < Y->p[i - 1]) {
912 return -1;
913 }
914 }
915
916 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000917}
918
919/*
920 * Compare signed values
921 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100922int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000923{
Paul Bakker23986e52011-04-24 08:57:21 +0000924 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +0000925
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 for (i = X->n; i > 0; i--) {
927 if (X->p[i - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000928 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000930 }
931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 for (j = Y->n; j > 0; j--) {
933 if (Y->p[j - 1] != 0) {
934 break;
935 }
936 }
937
938 if (i == 0 && j == 0) {
939 return 0;
940 }
941
942 if (i > j) {
943 return X->s;
944 }
945 if (j > i) {
946 return -Y->s;
947 }
948
949 if (X->s > 0 && Y->s < 0) {
950 return 1;
951 }
952 if (Y->s > 0 && X->s < 0) {
953 return -1;
954 }
955
956 for (; i > 0; i--) {
957 if (X->p[i - 1] > Y->p[i - 1]) {
958 return X->s;
959 }
960 if (X->p[i - 1] < Y->p[i - 1]) {
961 return -X->s;
962 }
963 }
964
965 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000966}
967
Janos Follathee6abce2019-09-05 14:47:19 +0100968/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000969 * Compare signed values
970 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100971int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
Paul Bakker5121ce52009-01-03 21:22:43 +0000972{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 mbedtls_mpi Y;
974 mbedtls_mpi_uint p[1];
Paul Bakker5121ce52009-01-03 21:22:43 +0000975
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 *p = mpi_sint_abs(z);
Dave Rodgmanf3df1052023-08-09 18:55:41 +0100977 Y.s = TO_SIGN(z);
Paul Bakker5121ce52009-01-03 21:22:43 +0000978 Y.n = 1;
979 Y.p = p;
980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 return mbedtls_mpi_cmp_mpi(X, &Y);
Paul Bakker5121ce52009-01-03 21:22:43 +0000982}
983
984/*
985 * Unsigned addition: X = |A| + |B| (HAC 14.7)
986 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100987int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +0000988{
Janos Follath24eed8d2019-11-22 13:21:35 +0000989 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100990 size_t j;
Agathiyan Bragadeeshc99840a2023-07-12 11:15:46 +0100991 mbedtls_mpi_uint *p;
992 mbedtls_mpi_uint c;
Paul Bakker5121ce52009-01-03 21:22:43 +0000993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 if (X == B) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 const mbedtls_mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000996 }
997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 if (X != A) {
999 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
1000 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001001
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001002 /*
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001003 * X must always be positive as a result of unsigned additions.
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001004 */
1005 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 for (j = B->n; j > 0; j--) {
1008 if (B->p[j - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001009 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 }
1011 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001012
Gilles Peskinedb14a9d2022-11-15 22:59:00 +01001013 /* Exit early to avoid undefined behavior on NULL+0 when X->n == 0
1014 * and B is 0 (of any size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 if (j == 0) {
1016 return 0;
1017 }
Gilles Peskinedb14a9d2022-11-15 22:59:00 +01001018
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j));
Paul Bakker5121ce52009-01-03 21:22:43 +00001020
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001021 /* j is the number of non-zero limbs of B. Add those to X. */
Paul Bakker5121ce52009-01-03 21:22:43 +00001022
Agathiyan Bragadeeshc99840a2023-07-12 11:15:46 +01001023 p = X->p;
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001024
Agathiyan Bragadeeshc99840a2023-07-12 11:15:46 +01001025 c = mbedtls_mpi_core_add(p, p, B->p, j);
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001026
1027 p += j;
1028
1029 /* Now propagate any carry */
Paul Bakker5121ce52009-01-03 21:22:43 +00001030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 while (c != 0) {
1032 if (j >= X->n) {
1033 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j + 1));
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001034 p = X->p + j;
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 }
1036
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 *p += c; c = (*p < c); j++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001038 }
1039
1040cleanup:
1041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001043}
1044
Paul Bakker5121ce52009-01-03 21:22:43 +00001045/*
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001046 * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
Paul Bakker5121ce52009-01-03 21:22:43 +00001047 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001048int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001049{
Janos Follath24eed8d2019-11-22 13:21:35 +00001050 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001051 size_t n;
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001052 mbedtls_mpi_uint carry;
Paul Bakker5121ce52009-01-03 21:22:43 +00001053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 for (n = B->n; n > 0; n--) {
1055 if (B->p[n - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 }
1058 }
1059 if (n > A->n) {
Gilles Peskinec8a91772021-01-27 22:30:43 +01001060 /* B >= (2^ciL)^n > A */
1061 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1062 goto cleanup;
1063 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001064
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, A->n));
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001066
1067 /* Set the high limbs of X to match A. Don't touch the lower limbs
1068 * because X might be aliased to B, and we must not overwrite the
1069 * significant digits of B. */
Aaron M. Uckoaf67d2c2023-01-17 11:52:22 -05001070 if (A->n > n && A != X) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 memcpy(X->p + n, A->p + n, (A->n - n) * ciL);
1072 }
1073 if (X->n > A->n) {
1074 memset(X->p + A->n, 0, (X->n - A->n) * ciL);
1075 }
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 carry = mbedtls_mpi_core_sub(X->p, A->p, B->p, n);
1078 if (carry != 0) {
Tom Cosgrove452c99c2022-08-25 10:07:07 +01001079 /* Propagate the carry through the rest of X. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 carry = mbedtls_mpi_core_sub_int(X->p + n, X->p + n, carry, X->n - n);
Tom Cosgrove452c99c2022-08-25 10:07:07 +01001081
1082 /* If we have further carry/borrow, the result is negative. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 if (carry != 0) {
Gilles Peskine89b41302020-07-23 01:16:46 +02001084 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1085 goto cleanup;
1086 }
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001087 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001088
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001089 /* X should always be positive as a result of unsigned subtractions. */
1090 X->s = 1;
1091
Paul Bakker5121ce52009-01-03 21:22:43 +00001092cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001094}
1095
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001096/* Common function for signed addition and subtraction.
1097 * Calculate A + B * flip_B where flip_B is 1 or -1.
Paul Bakker5121ce52009-01-03 21:22:43 +00001098 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001099static int add_sub_mpi(mbedtls_mpi *X,
1100 const mbedtls_mpi *A, const mbedtls_mpi *B,
1101 int flip_B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001102{
Hanno Becker73d7d792018-12-11 10:35:51 +00001103 int ret, s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001104
Hanno Becker73d7d792018-12-11 10:35:51 +00001105 s = A->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 if (A->s * B->s * flip_B < 0) {
1107 int cmp = mbedtls_mpi_cmp_abs(A, B);
1108 if (cmp >= 0) {
1109 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, A, B));
Gilles Peskine4a768dd2022-11-09 22:02:16 +01001110 /* If |A| = |B|, the result is 0 and we must set the sign bit
1111 * to +1 regardless of which of A or B was negative. Otherwise,
1112 * since |A| > |B|, the sign is the sign of A. */
1113 X->s = cmp == 0 ? 1 : s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 } else {
1115 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, B, A));
Gilles Peskine4a768dd2022-11-09 22:02:16 +01001116 /* Since |A| < |B|, the sign is the opposite of A. */
Paul Bakker5121ce52009-01-03 21:22:43 +00001117 X->s = -s;
1118 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 } else {
1120 MBEDTLS_MPI_CHK(mbedtls_mpi_add_abs(X, A, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001121 X->s = s;
1122 }
1123
1124cleanup:
1125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001127}
1128
1129/*
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001130 * Signed addition: X = A + B
1131 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001132int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001133{
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 return add_sub_mpi(X, A, B, 1);
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001135}
1136
1137/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001138 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001139 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001140int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001141{
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 return add_sub_mpi(X, A, B, -1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001143}
1144
1145/*
1146 * Signed addition: X = A + b
1147 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001148int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001149{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001150 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 mbedtls_mpi_uint p[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001152
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 p[0] = mpi_sint_abs(b);
Dave Rodgmanf3df1052023-08-09 18:55:41 +01001154 B.s = TO_SIGN(b);
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001155 B.n = 1;
1156 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 return mbedtls_mpi_add_mpi(X, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001159}
1160
1161/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001162 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001163 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001164int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001165{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001166 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 mbedtls_mpi_uint p[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 p[0] = mpi_sint_abs(b);
Dave Rodgmanf3df1052023-08-09 18:55:41 +01001170 B.s = TO_SIGN(b);
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001171 B.n = 1;
1172 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 return mbedtls_mpi_sub_mpi(X, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001175}
1176
Paul Bakker5121ce52009-01-03 21:22:43 +00001177/*
1178 * Baseline multiplication: X = A * B (HAC 14.12)
1179 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001180int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001181{
Janos Follath24eed8d2019-11-22 13:21:35 +00001182 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker1772e052022-04-13 06:51:40 +01001183 size_t i, j;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 mbedtls_mpi TA, TB;
Hanno Beckerda763de2022-04-13 06:50:02 +01001185 int result_is_zero = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001186
Tom Cosgrove6af26f32022-08-24 16:40:55 +01001187 mbedtls_mpi_init(&TA);
1188 mbedtls_mpi_init(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00001189
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 if (X == A) {
1191 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A)); A = &TA;
1192 }
1193 if (X == B) {
1194 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B)); B = &TB;
1195 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 for (i = A->n; i > 0; i--) {
1198 if (A->p[i - 1] != 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001199 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 }
1201 }
1202 if (i == 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001203 result_is_zero = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 }
Hanno Beckerda763de2022-04-13 06:50:02 +01001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 for (j = B->n; j > 0; j--) {
1207 if (B->p[j - 1] != 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001208 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 }
1210 }
1211 if (j == 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001212 result_is_zero = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 }
Hanno Beckerda763de2022-04-13 06:50:02 +01001214
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i + j));
1216 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
Paul Bakker5121ce52009-01-03 21:22:43 +00001217
Tom Cosgrove6af26f32022-08-24 16:40:55 +01001218 mbedtls_mpi_core_mul(X->p, A->p, i, B->p, j);
Paul Bakker5121ce52009-01-03 21:22:43 +00001219
Hanno Beckerda763de2022-04-13 06:50:02 +01001220 /* If the result is 0, we don't shortcut the operation, which reduces
1221 * but does not eliminate side channels leaking the zero-ness. We do
1222 * need to take care to set the sign bit properly since the library does
1223 * not fully support an MPI object with a value of 0 and s == -1. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if (result_is_zero) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001225 X->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 } else {
Hanno Beckerda763de2022-04-13 06:50:02 +01001227 X->s = A->s * B->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001229
1230cleanup:
1231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TA);
Paul Bakker5121ce52009-01-03 21:22:43 +00001233
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001235}
1236
1237/*
1238 * Baseline multiplication: X = A * b
1239 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001240int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001241{
Hanno Becker35771312022-04-14 11:52:11 +01001242 size_t n = A->n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 while (n > 0 && A->p[n - 1] == 0) {
Hanno Becker35771312022-04-14 11:52:11 +01001244 --n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 }
Hanno Becker35771312022-04-14 11:52:11 +01001246
Hanno Becker74a11a32022-04-06 06:27:00 +01001247 /* The general method below doesn't work if b==0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 if (b == 0 || n == 0) {
1249 return mbedtls_mpi_lset(X, 0);
1250 }
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001251
Hanno Beckeraef9cc42022-04-11 06:36:29 +01001252 /* Calculate A*b as A + A*(b-1) to take advantage of mbedtls_mpi_core_mla */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001253 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001254 /* In general, A * b requires 1 limb more than b. If
1255 * A->p[n - 1] * b / b == A->p[n - 1], then A * b fits in the same
1256 * number of limbs as A and the call to grow() is not required since
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001257 * copy() will take care of the growth if needed. However, experimentally,
1258 * making the call to grow() unconditional causes slightly fewer
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001259 * calls to calloc() in ECP code, presumably because it reuses the
1260 * same mpi for a while and this way the mpi is more likely to directly
Hanno Becker9137b9c2022-04-12 10:51:54 +01001261 * grow to its final size.
1262 *
1263 * Note that calculating A*b as 0 + A*b doesn't work as-is because
1264 * A,X can be the same. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n + 1));
1266 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
1267 mbedtls_mpi_core_mla(X->p, X->n, A->p, n, b - 1);
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001268
1269cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001271}
1272
1273/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001274 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1275 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001276 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001277static mbedtls_mpi_uint mbedtls_int_div_int(mbedtls_mpi_uint u1,
1278 mbedtls_mpi_uint u0,
1279 mbedtls_mpi_uint d,
1280 mbedtls_mpi_uint *r)
Simon Butcher15b15d12015-11-26 19:35:03 +00001281{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001282#if defined(MBEDTLS_HAVE_UDBL)
1283 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001284#else
Simon Butcher9803d072016-01-03 00:24:34 +00001285 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 const mbedtls_mpi_uint uint_halfword_mask = ((mbedtls_mpi_uint) 1 << biH) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001287 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1288 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001289 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001290#endif
1291
Simon Butcher15b15d12015-11-26 19:35:03 +00001292 /*
1293 * Check for overflow
1294 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 if (0 == d || u1 >= d) {
1296 if (r != NULL) {
1297 *r = ~(mbedtls_mpi_uint) 0u;
1298 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 return ~(mbedtls_mpi_uint) 0u;
Simon Butcher15b15d12015-11-26 19:35:03 +00001301 }
1302
1303#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001304 dividend = (mbedtls_t_udbl) u1 << biL;
1305 dividend |= (mbedtls_t_udbl) u0;
1306 quotient = dividend / d;
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if (quotient > ((mbedtls_t_udbl) 1 << biL) - 1) {
1308 quotient = ((mbedtls_t_udbl) 1 << biL) - 1;
1309 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 if (r != NULL) {
1312 *r = (mbedtls_mpi_uint) (dividend - (quotient * d));
1313 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001314
1315 return (mbedtls_mpi_uint) quotient;
1316#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001317
1318 /*
1319 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1320 * Vol. 2 - Seminumerical Algorithms, Knuth
1321 */
1322
1323 /*
1324 * Normalize the divisor, d, and dividend, u0, u1
1325 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 s = mbedtls_mpi_core_clz(d);
Simon Butcher15b15d12015-11-26 19:35:03 +00001327 d = d << s;
1328
1329 u1 = u1 << s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 u1 |= (u0 >> (biL - s)) & (-(mbedtls_mpi_sint) s >> (biL - 1));
Simon Butcher15b15d12015-11-26 19:35:03 +00001331 u0 = u0 << s;
1332
1333 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001334 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001335
1336 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001337 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001338
1339 /*
1340 * Find the first quotient and remainder
1341 */
1342 q1 = u1 / d1;
1343 r0 = u1 - d1 * q1;
1344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 while (q1 >= radix || (q1 * d0 > radix * r0 + u0_msw)) {
Simon Butcher15b15d12015-11-26 19:35:03 +00001346 q1 -= 1;
1347 r0 += d1;
1348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 if (r0 >= radix) {
1350 break;
1351 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001352 }
1353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 rAX = (u1 * radix) + (u0_msw - q1 * d);
Simon Butcher15b15d12015-11-26 19:35:03 +00001355 q0 = rAX / d1;
1356 r0 = rAX - q0 * d1;
1357
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 while (q0 >= radix || (q0 * d0 > radix * r0 + u0_lsw)) {
Simon Butcher15b15d12015-11-26 19:35:03 +00001359 q0 -= 1;
1360 r0 += d1;
1361
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 if (r0 >= radix) {
1363 break;
1364 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001365 }
1366
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 if (r != NULL) {
1368 *r = (rAX * radix + u0_lsw - q0 * d) >> s;
1369 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001370
1371 quotient = q1 * radix + q0;
1372
1373 return quotient;
1374#endif
1375}
1376
1377/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001380int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1381 const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001382{
Janos Follath24eed8d2019-11-22 13:21:35 +00001383 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001384 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 mbedtls_mpi X, Y, Z, T1, T2;
Alexander Kd19a1932019-11-01 18:20:42 +03001386 mbedtls_mpi_uint TP2[3];
Paul Bakker5121ce52009-01-03 21:22:43 +00001387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 if (mbedtls_mpi_cmp_int(B, 0) == 0) {
1389 return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
1390 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001391
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z);
1393 mbedtls_mpi_init(&T1);
Alexander Kd19a1932019-11-01 18:20:42 +03001394 /*
1395 * Avoid dynamic memory allocations for constant-size T2.
1396 *
1397 * T2 is used for comparison only and the 3 limbs are assigned explicitly,
1398 * so nobody increase the size of the MPI and we're safe to use an on-stack
1399 * buffer.
1400 */
Alexander K35d6d462019-10-31 14:46:45 +03001401 T2.s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 T2.n = sizeof(TP2) / sizeof(*TP2);
Alexander Kd19a1932019-11-01 18:20:42 +03001403 T2.p = TP2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 if (mbedtls_mpi_cmp_abs(A, B) < 0) {
1406 if (Q != NULL) {
1407 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(Q, 0));
1408 }
1409 if (R != NULL) {
1410 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, A));
1411 }
1412 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001413 }
1414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&X, A));
1416 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001417 X.s = Y.s = 1;
1418
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&Z, A->n + 2));
1420 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Z, 0));
1421 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T1, A->n + 2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 k = mbedtls_mpi_bitlen(&Y) % biL;
1424 if (k < biL - 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001425 k = biL - 1 - k;
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&X, k));
1427 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, k));
1428 } else {
1429 k = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001430 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001431
1432 n = X.n - 1;
1433 t = Y.n - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, biL * (n - t)));
Paul Bakker5121ce52009-01-03 21:22:43 +00001435
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 while (mbedtls_mpi_cmp_mpi(&X, &Y) >= 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001437 Z.p[n - t]++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &Y));
Paul Bakker5121ce52009-01-03 21:22:43 +00001439 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, biL * (n - t)));
Paul Bakker5121ce52009-01-03 21:22:43 +00001441
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 for (i = n; i > t; i--) {
1443 if (X.p[i] >= Y.p[t]) {
1444 Z.p[i - t - 1] = ~(mbedtls_mpi_uint) 0u;
1445 } else {
1446 Z.p[i - t - 1] = mbedtls_int_div_int(X.p[i], X.p[i - 1],
1447 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001448 }
1449
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 T2.p[0] = (i < 2) ? 0 : X.p[i - 2];
1451 T2.p[1] = (i < 1) ? 0 : X.p[i - 1];
Alexander K35d6d462019-10-31 14:46:45 +03001452 T2.p[2] = X.p[i];
1453
Paul Bakker5121ce52009-01-03 21:22:43 +00001454 Z.p[i - t - 1]++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 do {
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 Z.p[i - t - 1]--;
1457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&T1, 0));
1459 T1.p[0] = (t < 1) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 T1.p[1] = Y.p[t];
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &T1, Z.p[i - t - 1]));
1462 } while (mbedtls_mpi_cmp_mpi(&T1, &T2) > 0);
Paul Bakker5121ce52009-01-03 21:22:43 +00001463
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &Y, Z.p[i - t - 1]));
1465 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
1466 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &T1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 if (mbedtls_mpi_cmp_int(&X, 0) < 0) {
1469 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T1, &Y));
1470 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
1471 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&X, &X, &T1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001472 Z.p[i - t - 1]--;
1473 }
1474 }
1475
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 if (Q != NULL) {
1477 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(Q, &Z));
Paul Bakker5121ce52009-01-03 21:22:43 +00001478 Q->s = A->s * B->s;
1479 }
1480
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 if (R != NULL) {
1482 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&X, k));
Paul Bakkerf02c5642012-11-13 10:25:21 +00001483 X.s = A->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, &X));
Paul Bakker5121ce52009-01-03 21:22:43 +00001485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 if (mbedtls_mpi_cmp_int(R, 0) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001487 R->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001489 }
1490
1491cleanup:
1492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z);
1494 mbedtls_mpi_free(&T1);
1495 mbedtls_platform_zeroize(TP2, sizeof(TP2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001496
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001498}
1499
1500/*
1501 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00001502 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001503int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R,
1504 const mbedtls_mpi *A,
1505 mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001506{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001507 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 mbedtls_mpi_uint p[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001509
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 p[0] = mpi_sint_abs(b);
Dave Rodgmanf3df1052023-08-09 18:55:41 +01001511 B.s = TO_SIGN(b);
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001512 B.n = 1;
1513 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001514
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 return mbedtls_mpi_div_mpi(Q, R, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001516}
1517
1518/*
1519 * Modulo: R = A mod B
1520 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001521int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001522{
Janos Follath24eed8d2019-11-22 13:21:35 +00001523 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00001524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 if (mbedtls_mpi_cmp_int(B, 0) < 0) {
1526 return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1527 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(NULL, R, A, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001530
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 while (mbedtls_mpi_cmp_int(R, 0) < 0) {
1532 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(R, R, B));
1533 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001534
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 while (mbedtls_mpi_cmp_mpi(R, B) >= 0) {
1536 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(R, R, B));
1537 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001538
1539cleanup:
1540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001542}
1543
1544/*
1545 * Modulo: r = A mod b
1546 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001547int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001548{
Paul Bakker23986e52011-04-24 08:57:21 +00001549 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 mbedtls_mpi_uint x, y, z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001551
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 if (b == 0) {
1553 return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
1554 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 if (b < 0) {
1557 return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1558 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001559
1560 /*
1561 * handle trivial cases
1562 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 if (b == 1 || A->n == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001564 *r = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 }
1567
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 if (b == 2) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001569 *r = A->p[0] & 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001571 }
1572
1573 /*
1574 * general case
1575 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 for (i = A->n, y = 0; i > 0; i--) {
Paul Bakker23986e52011-04-24 08:57:21 +00001577 x = A->p[i - 1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 y = (y << biH) | (x >> biH);
Paul Bakker5121ce52009-01-03 21:22:43 +00001579 z = y / b;
1580 y -= z * b;
1581
1582 x <<= biH;
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 y = (y << biH) | (x >> biH);
Paul Bakker5121ce52009-01-03 21:22:43 +00001584 z = y / b;
1585 y -= z * b;
1586 }
1587
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001588 /*
1589 * If A is negative, then the current y represents a negative value.
1590 * Flipping it to the positive side.
1591 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 if (A->s < 0 && y != 0) {
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001593 y = b - y;
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001595
Paul Bakker5121ce52009-01-03 21:22:43 +00001596 *r = y;
1597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001599}
1600
Paul Bakker5121ce52009-01-03 21:22:43 +00001601/*
1602 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
1603 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001604int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
1605 const mbedtls_mpi *E, const mbedtls_mpi *N,
1606 mbedtls_mpi *prec_RR)
Paul Bakker5121ce52009-01-03 21:22:43 +00001607{
Janos Follath24eed8d2019-11-22 13:21:35 +00001608 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath1ba40582024-02-13 12:36:13 +00001609 mbedtls_mpi RR, T, E_core;
Paul Bakker5121ce52009-01-03 21:22:43 +00001610
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 if (mbedtls_mpi_cmp_int(N, 0) <= 0 || (N->p[0] & 1) == 0) {
1612 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1613 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001614
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 if (mbedtls_mpi_cmp_int(E, 0) < 0) {
1616 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1617 }
Paul Bakkerf6198c12012-05-16 08:02:29 +00001618
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 if (mbedtls_mpi_bitlen(E) > MBEDTLS_MPI_MAX_BITS ||
1620 mbedtls_mpi_bitlen(N) > MBEDTLS_MPI_MAX_BITS) {
1621 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1622 }
Chris Jones9246d042020-11-25 15:12:39 +00001623
Janos Follath1ba40582024-02-13 12:36:13 +00001624 mbedtls_mpi_init(&RR);
1625 mbedtls_mpi_init(&T);
1626 mbedtls_mpi_init(&E_core);
Paul Bakker50546922012-05-19 08:40:49 +00001627
1628 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001629 * If 1st call, pre-compute R^2 mod N
1630 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 if (prec_RR == NULL || prec_RR->p == NULL) {
Janos Follath1ba40582024-02-13 12:36:13 +00001632 MBEDTLS_MPI_CHK(mbedtls_mpi_core_get_mont_r2_unsafe(&RR, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 if (prec_RR != NULL) {
1635 memcpy(prec_RR, &RR, sizeof(mbedtls_mpi));
1636 }
1637 } else {
1638 memcpy(&RR, prec_RR, sizeof(mbedtls_mpi));
Paul Bakker5121ce52009-01-03 21:22:43 +00001639 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001640
1641 /*
Janos Follath1ba40582024-02-13 12:36:13 +00001642 * Ensure that the exponent that we are passing to the core is not NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +00001643 */
Janos Follath1ba40582024-02-13 12:36:13 +00001644 if (E->n == 0) {
1645 mbedtls_mpi_lset(&E_core, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 } else {
Janos Follath1ba40582024-02-13 12:36:13 +00001647 memcpy(&E_core, E, sizeof(mbedtls_mpi));
Paul Bakker5121ce52009-01-03 21:22:43 +00001648 }
1649
1650 /*
Janos Follath1ba40582024-02-13 12:36:13 +00001651 * To preserve constness we need to make a copy of A. Using X for this to
1652 * save memory.
Paul Bakker5121ce52009-01-03 21:22:43 +00001653 */
Janos Follath1ba40582024-02-13 12:36:13 +00001654 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
Paul Bakker5121ce52009-01-03 21:22:43 +00001655
1656 /*
Janos Follath1ba40582024-02-13 12:36:13 +00001657 * Compensate for negative A (and correct at the end).
Paul Bakker5121ce52009-01-03 21:22:43 +00001658 */
Janos Follath1ba40582024-02-13 12:36:13 +00001659 X->s = 1;
Paul Bakkerf6198c12012-05-16 08:02:29 +00001660
Janos Follath8e7d6a02022-10-04 13:27:40 +01001661 /*
Janos Follath1ba40582024-02-13 12:36:13 +00001662 * Make sure that A has exactly as many limbs as N.
Janos Follath8e7d6a02022-10-04 13:27:40 +01001663 */
Janos Follath1ba40582024-02-13 12:36:13 +00001664 if (mbedtls_mpi_cmp_mpi(X, N) >= 0) {
1665 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(X, X, N));
1666 }
1667 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, N->n));
1668
1669 /*
1670 * Allocate working memory for mbedtls_mpi_core_exp_mod()
1671 */
1672 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T,
1673 mbedtls_mpi_core_exp_mod_working_limbs(N->n, E_core.n)));
1674
1675 /*
1676 * Convert to and from Montgomery around mbedtls_mpi_core_exp_mod().
1677 */
1678 mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N->p);
1679 mbedtls_mpi_core_to_mont_rep(X->p, X->p, N->p, N->n, mm, RR.p, T.p);
1680 mbedtls_mpi_core_exp_mod(X->p, X->p, N->p, N->n, E_core.p, E_core.n, RR.p,
1681 T.p);
1682 mbedtls_mpi_core_from_mont_rep(X->p, X->p, N->p, N->n, mm, T.p);
1683
1684 /*
1685 * Correct for negative A.
1686 */
1687 if (A->s == -1 && (E_core.p[0] & 1) != 0) {
1688 X->s = -1;
1689 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(X, N, X));
1690 }
Janos Follath8e7d6a02022-10-04 13:27:40 +01001691
Paul Bakker5121ce52009-01-03 21:22:43 +00001692cleanup:
1693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 mbedtls_mpi_free(&T);
Paul Bakker6c591fa2011-05-05 11:49:20 +00001695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 if (prec_RR == NULL || prec_RR->p == NULL) {
1697 mbedtls_mpi_free(&RR);
1698 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001699
Janos Follath1ba40582024-02-13 12:36:13 +00001700 if (E->n == 0) {
1701 mbedtls_mpi_free(&E_core);
1702 }
1703
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001705}
1706
Paul Bakker5121ce52009-01-03 21:22:43 +00001707/*
1708 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
1709 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001710int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001711{
Janos Follath24eed8d2019-11-22 13:21:35 +00001712 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001713 size_t lz, lzt;
Alexander Ke8ad49f2019-08-16 16:16:07 +03001714 mbedtls_mpi TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00001715
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00001717
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A));
1719 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001720
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 lz = mbedtls_mpi_lsb(&TA);
1722 lzt = mbedtls_mpi_lsb(&TB);
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00001723
Gilles Peskine27253bc2021-06-09 13:26:43 +02001724 /* The loop below gives the correct result when A==0 but not when B==0.
1725 * So have a special case for B==0. Leverage the fact that we just
1726 * calculated the lsb and lsb(B)==0 iff B is odd or 0 to make the test
1727 * slightly more efficient than cmp_int(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 if (lzt == 0 && mbedtls_mpi_get_bit(&TB, 0) == 0) {
1729 ret = mbedtls_mpi_copy(G, A);
Gilles Peskine27253bc2021-06-09 13:26:43 +02001730 goto cleanup;
1731 }
1732
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 if (lzt < lz) {
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00001734 lz = lzt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00001736
Paul Bakker5121ce52009-01-03 21:22:43 +00001737 TA.s = TB.s = 1;
1738
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001739 /* We mostly follow the procedure described in HAC 14.54, but with some
1740 * minor differences:
1741 * - Sequences of multiplications or divisions by 2 are grouped into a
1742 * single shift operation.
Gilles Peskineb09c7ee2021-06-21 18:58:39 +02001743 * - The procedure in HAC assumes that 0 < TB <= TA.
1744 * - The condition TB <= TA is not actually necessary for correctness.
1745 * TA and TB have symmetric roles except for the loop termination
1746 * condition, and the shifts at the beginning of the loop body
1747 * remove any significance from the ordering of TA vs TB before
1748 * the shifts.
1749 * - If TA = 0, the loop goes through 0 iterations and the result is
1750 * correctly TB.
1751 * - The case TB = 0 was short-circuited above.
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001752 *
1753 * For the correctness proof below, decompose the original values of
1754 * A and B as
1755 * A = sa * 2^a * A' with A'=0 or A' odd, and sa = +-1
1756 * B = sb * 2^b * B' with B'=0 or B' odd, and sb = +-1
1757 * Then gcd(A, B) = 2^{min(a,b)} * gcd(A',B'),
1758 * and gcd(A',B') is odd or 0.
1759 *
1760 * At the beginning, we have TA = |A| and TB = |B| so gcd(A,B) = gcd(TA,TB).
1761 * The code maintains the following invariant:
1762 * gcd(A,B) = 2^k * gcd(TA,TB) for some k (I)
Gilles Peskine4df3f1f2021-06-15 22:09:39 +02001763 */
1764
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001765 /* Proof that the loop terminates:
1766 * At each iteration, either the right-shift by 1 is made on a nonzero
1767 * value and the nonnegative integer bitlen(TA) + bitlen(TB) decreases
1768 * by at least 1, or the right-shift by 1 is made on zero and then
1769 * TA becomes 0 which ends the loop (TB cannot be 0 if it is right-shifted
1770 * since in that case TB is calculated from TB-TA with the condition TB>TA).
1771 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 while (mbedtls_mpi_cmp_int(&TA, 0) != 0) {
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001773 /* Divisions by 2 preserve the invariant (I). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, mbedtls_mpi_lsb(&TA)));
1775 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, mbedtls_mpi_lsb(&TB)));
Paul Bakker5121ce52009-01-03 21:22:43 +00001776
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001777 /* Set either TA or TB to |TA-TB|/2. Since TA and TB are both odd,
1778 * TA-TB is even so the division by 2 has an integer result.
1779 * Invariant (I) is preserved since any odd divisor of both TA and TB
1780 * also divides |TA-TB|/2, and any odd divisor of both TA and |TA-TB|/2
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001781 * also divides TB, and any odd divisor of both TB and |TA-TB|/2 also
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001782 * divides TA.
1783 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 if (mbedtls_mpi_cmp_mpi(&TA, &TB) >= 0) {
1785 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TA, &TA, &TB));
1786 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, 1));
1787 } else {
1788 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TB, &TB, &TA));
1789 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001790 }
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001791 /* Note that one of TA or TB is still odd. */
Paul Bakker5121ce52009-01-03 21:22:43 +00001792 }
1793
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001794 /* By invariant (I), gcd(A,B) = 2^k * gcd(TA,TB) for some k.
1795 * At the loop exit, TA = 0, so gcd(TA,TB) = TB.
1796 * - If there was at least one loop iteration, then one of TA or TB is odd,
1797 * and TA = 0, so TB is odd and gcd(TA,TB) = gcd(A',B'). In this case,
1798 * lz = min(a,b) so gcd(A,B) = 2^lz * TB.
1799 * - If there was no loop iteration, then A was 0, and gcd(A,B) = B.
Gilles Peskine4d3fd362021-06-21 11:40:38 +02001800 * 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 +02001801 */
1802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&TB, lz));
1804 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(G, &TB));
Paul Bakker5121ce52009-01-03 21:22:43 +00001805
1806cleanup:
1807
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00001809
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001811}
1812
Paul Bakker33dc46b2014-04-30 16:11:39 +02001813/*
1814 * Fill X with size bytes of random.
Gilles Peskine22cdd0c2022-10-27 20:15:13 +02001815 * The bytes returned from the RNG are used in a specific order which
1816 * is suitable for deterministic ECDSA (see the specification of
1817 * mbedtls_mpi_random() and the implementation in mbedtls_mpi_fill_random()).
Paul Bakker33dc46b2014-04-30 16:11:39 +02001818 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001819int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
1820 int (*f_rng)(void *, unsigned char *, size_t),
1821 void *p_rng)
Paul Bakker287781a2011-03-26 13:18:49 +00001822{
Janos Follath24eed8d2019-11-22 13:21:35 +00001823 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 const size_t limbs = CHARS_TO_LIMBS(size);
Hanno Beckerda1655a2017-10-18 14:21:44 +01001825
Hanno Beckerda1655a2017-10-18 14:21:44 +01001826 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
1828 if (size == 0) {
1829 return 0;
1830 }
Paul Bakker287781a2011-03-26 13:18:49 +00001831
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 ret = mbedtls_mpi_core_fill_random(X->p, X->n, size, f_rng, p_rng);
Paul Bakker287781a2011-03-26 13:18:49 +00001833
1834cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 return ret;
Paul Bakker287781a2011-03-26 13:18:49 +00001836}
1837
Gilles Peskine449bd832023-01-11 14:50:10 +01001838int mbedtls_mpi_random(mbedtls_mpi *X,
1839 mbedtls_mpi_sint min,
1840 const mbedtls_mpi *N,
1841 int (*f_rng)(void *, unsigned char *, size_t),
1842 void *p_rng)
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001843{
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 if (min < 0) {
1845 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1846 }
1847 if (mbedtls_mpi_cmp_int(N, min) <= 0) {
1848 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1849 }
Gilles Peskine1e918f42021-03-29 22:14:51 +02001850
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001851 /* Ensure that target MPI has exactly the same number of limbs
1852 * as the upper bound, even if the upper bound has leading zeros.
Gilles Peskine6b7ce962022-12-15 15:04:33 +01001853 * This is necessary for mbedtls_mpi_core_random. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 int ret = mbedtls_mpi_resize_clear(X, N->n);
1855 if (ret != 0) {
1856 return ret;
1857 }
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001858
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 return mbedtls_mpi_core_random(X->p, min, N->p, X->n, f_rng, p_rng);
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001860}
1861
Paul Bakker5121ce52009-01-03 21:22:43 +00001862/*
1863 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
1864 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001865int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
Paul Bakker5121ce52009-01-03 21:22:43 +00001866{
Janos Follath24eed8d2019-11-22 13:21:35 +00001867 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001869
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 if (mbedtls_mpi_cmp_int(N, 1) <= 0) {
1871 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1872 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001873
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TU); mbedtls_mpi_init(&U1); mbedtls_mpi_init(&U2);
1875 mbedtls_mpi_init(&G); mbedtls_mpi_init(&TB); mbedtls_mpi_init(&TV);
1876 mbedtls_mpi_init(&V1); mbedtls_mpi_init(&V2);
Paul Bakker5121ce52009-01-03 21:22:43 +00001877
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, A, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001882 goto cleanup;
1883 }
1884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&TA, A, N));
1886 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TU, &TA));
1887 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, N));
1888 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TV, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001889
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U1, 1));
1891 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U2, 0));
1892 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V1, 0));
1893 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001894
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 do {
1896 while ((TU.p[0] & 1) == 0) {
1897 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TU, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001898
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 if ((U1.p[0] & 1) != 0 || (U2.p[0] & 1) != 0) {
1900 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&U1, &U1, &TB));
1901 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &TA));
Paul Bakker5121ce52009-01-03 21:22:43 +00001902 }
1903
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U1, 1));
1905 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001906 }
1907
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 while ((TV.p[0] & 1) == 0) {
1909 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TV, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001910
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 if ((V1.p[0] & 1) != 0 || (V2.p[0] & 1) != 0) {
1912 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, &TB));
1913 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &TA));
Paul Bakker5121ce52009-01-03 21:22:43 +00001914 }
1915
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V1, 1));
1917 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001918 }
1919
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 if (mbedtls_mpi_cmp_mpi(&TU, &TV) >= 0) {
1921 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TU, &TU, &TV));
1922 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U1, &U1, &V1));
1923 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &V2));
1924 } else {
1925 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TV, &TV, &TU));
1926 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, &U1));
1927 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &U2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001928 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 } while (mbedtls_mpi_cmp_int(&TU, 0) != 0);
1930
1931 while (mbedtls_mpi_cmp_int(&V1, 0) < 0) {
1932 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001933 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001934
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 while (mbedtls_mpi_cmp_mpi(&V1, N) >= 0) {
1936 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, N));
1937 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001938
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, &V1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001940
1941cleanup:
1942
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TU); mbedtls_mpi_free(&U1); mbedtls_mpi_free(&U2);
1944 mbedtls_mpi_free(&G); mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TV);
1945 mbedtls_mpi_free(&V1); mbedtls_mpi_free(&V2);
Paul Bakker5121ce52009-01-03 21:22:43 +00001946
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001948}
1949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00001951
Gilles Peskineb2bc1712019-02-08 17:27:11 +01001952/* Gaps between primes, starting at 3. https://oeis.org/A001223 */
1953static const unsigned char small_prime_gaps[] = {
1954 2, 2, 4, 2, 4, 2, 4, 6,
1955 2, 6, 4, 2, 4, 6, 6, 2,
1956 6, 4, 2, 6, 4, 6, 8, 4,
1957 2, 4, 2, 4, 14, 4, 6, 2,
1958 10, 2, 6, 6, 4, 6, 6, 2,
1959 10, 2, 4, 2, 12, 12, 4, 2,
1960 4, 6, 2, 10, 6, 6, 6, 2,
1961 6, 4, 2, 10, 14, 4, 2, 4,
1962 14, 6, 10, 2, 4, 6, 8, 6,
1963 6, 4, 6, 8, 4, 8, 10, 2,
1964 10, 2, 6, 4, 6, 8, 4, 2,
1965 4, 12, 8, 4, 8, 4, 6, 12,
1966 2, 18, 6, 10, 6, 6, 2, 6,
1967 10, 6, 6, 2, 6, 6, 4, 2,
1968 12, 10, 2, 4, 6, 6, 2, 12,
1969 4, 6, 8, 10, 8, 10, 8, 6,
1970 6, 4, 8, 6, 4, 8, 4, 14,
1971 10, 12, 2, 10, 2, 4, 2, 10,
1972 14, 4, 2, 4, 14, 4, 2, 4,
1973 20, 4, 8, 10, 8, 4, 6, 6,
1974 14, 4, 6, 6, 8, 6, /*reaches 997*/
Gilles Peskine30b03782023-08-22 11:06:47 +02001975 0 /* the last entry is effectively unused */
Paul Bakker5121ce52009-01-03 21:22:43 +00001976};
1977
1978/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01001979 * Small divisors test (X must be positive)
1980 *
1981 * Return values:
1982 * 0: no small factor (possible prime, more tests needed)
1983 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01001985 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00001986 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001987static int mpi_check_small_factors(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +00001988{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01001989 int ret = 0;
1990 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 mbedtls_mpi_uint r;
Gilles Peskineb2bc1712019-02-08 17:27:11 +01001992 unsigned p = 3; /* The first odd prime */
Paul Bakker5121ce52009-01-03 21:22:43 +00001993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 if ((X->p[0] & 1) == 0) {
1995 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
1996 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001997
Gilles Peskineb2bc1712019-02-08 17:27:11 +01001998 for (i = 0; i < sizeof(small_prime_gaps); p += small_prime_gaps[i], i++) {
1999 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, p));
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 if (r == 0) {
Gilles Peskineb2bc1712019-02-08 17:27:11 +01002001 if (mbedtls_mpi_cmp_int(X, p) == 0) {
2002 return 1;
2003 } else {
2004 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2005 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002007 }
2008
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002009cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 return ret;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002011}
2012
2013/*
2014 * Miller-Rabin pseudo-primality test (HAC 4.24)
2015 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002016static int mpi_miller_rabin(const mbedtls_mpi *X, size_t rounds,
2017 int (*f_rng)(void *, unsigned char *, size_t),
2018 void *p_rng)
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002019{
Pascal Junodb99183d2015-03-11 16:49:45 +01002020 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002021 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 mbedtls_mpi_init(&W); mbedtls_mpi_init(&R);
2025 mbedtls_mpi_init(&T); mbedtls_mpi_init(&A);
2026 mbedtls_mpi_init(&RR);
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002027
Paul Bakker5121ce52009-01-03 21:22:43 +00002028 /*
2029 * W = |X| - 1
2030 * R = W >> lsb( W )
2031 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&W, X, 1));
2033 s = mbedtls_mpi_lsb(&W);
2034 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R, &W));
2035 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&R, s));
Paul Bakker5121ce52009-01-03 21:22:43 +00002036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 for (i = 0; i < rounds; i++) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002038 /*
2039 * pick a random A, 1 < A < |X| - 1
2040 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002041 count = 0;
2042 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&A, X->n * ciL, f_rng, p_rng));
Pascal Junodb99183d2015-03-11 16:49:45 +01002044
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 j = mbedtls_mpi_bitlen(&A);
2046 k = mbedtls_mpi_bitlen(&W);
Pascal Junodb99183d2015-03-11 16:49:45 +01002047 if (j > k) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 A.p[A.n - 1] &= ((mbedtls_mpi_uint) 1 << (k - (A.n - 1) * biL - 1)) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002049 }
2050
2051 if (count++ > 30) {
Jens Wiklanderf08aa3e2019-01-17 13:30:57 +01002052 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2053 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002054 }
2055
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 } while (mbedtls_mpi_cmp_mpi(&A, &W) >= 0 ||
2057 mbedtls_mpi_cmp_int(&A, 1) <= 0);
Paul Bakker5121ce52009-01-03 21:22:43 +00002058
2059 /*
2060 * A = A^R mod |X|
2061 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&A, &A, &R, X, &RR));
Paul Bakker5121ce52009-01-03 21:22:43 +00002063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 if (mbedtls_mpi_cmp_mpi(&A, &W) == 0 ||
2065 mbedtls_mpi_cmp_int(&A, 1) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002066 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002068
2069 j = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 while (j < s && mbedtls_mpi_cmp_mpi(&A, &W) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002071 /*
2072 * A = A * A mod |X|
2073 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &A, &A));
2075 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&A, &T, X));
Paul Bakker5121ce52009-01-03 21:22:43 +00002076
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 if (mbedtls_mpi_cmp_int(&A, 1) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002078 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002080
2081 j++;
2082 }
2083
2084 /*
2085 * not prime if A != |X| - 1 or A == 1
2086 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 if (mbedtls_mpi_cmp_mpi(&A, &W) != 0 ||
2088 mbedtls_mpi_cmp_int(&A, 1) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002090 break;
2091 }
2092 }
2093
2094cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 mbedtls_mpi_free(&W); mbedtls_mpi_free(&R);
2096 mbedtls_mpi_free(&T); mbedtls_mpi_free(&A);
2097 mbedtls_mpi_free(&RR);
Paul Bakker5121ce52009-01-03 21:22:43 +00002098
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002100}
2101
2102/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002103 * Pseudo-primality test: small factors, then Miller-Rabin
2104 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002105int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
2106 int (*f_rng)(void *, unsigned char *, size_t),
2107 void *p_rng)
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002108{
Janos Follath24eed8d2019-11-22 13:21:35 +00002109 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 mbedtls_mpi XX;
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002111
2112 XX.s = 1;
2113 XX.n = X->n;
2114 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 if (mbedtls_mpi_cmp_int(&XX, 0) == 0 ||
2117 mbedtls_mpi_cmp_int(&XX, 1) == 0) {
2118 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002119 }
2120
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 if (mbedtls_mpi_cmp_int(&XX, 2) == 0) {
2122 return 0;
2123 }
2124
2125 if ((ret = mpi_check_small_factors(&XX)) != 0) {
2126 if (ret == 1) {
2127 return 0;
2128 }
2129
2130 return ret;
2131 }
2132
2133 return mpi_miller_rabin(&XX, rounds, f_rng, p_rng);
Janos Follathf301d232018-08-14 13:34:01 +01002134}
2135
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002136/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002137 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002138 *
Janos Follathf301d232018-08-14 13:34:01 +01002139 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2140 * be either 1024 bits or 1536 bits long, and flags must contain
2141 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002142 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002143int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
2144 int (*f_rng)(void *, unsigned char *, size_t),
2145 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00002146{
Jethro Beekman66689272018-02-14 19:24:10 -08002147#ifdef MBEDTLS_HAVE_INT64
2148// ceil(2^63.5)
2149#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2150#else
2151// ceil(2^31.5)
2152#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2153#endif
2154 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002155 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002156 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 mbedtls_mpi_uint r;
2158 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 if (nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS) {
2161 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2162 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 mbedtls_mpi_init(&Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00002165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 n = BITS_TO_LIMBS(nbits);
Paul Bakker5121ce52009-01-03 21:22:43 +00002167
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR) == 0) {
Janos Follathda31fa12018-09-03 14:45:23 +01002169 /*
2170 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2171 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 rounds = ((nbits >= 1300) ? 2 : (nbits >= 850) ? 3 :
2173 (nbits >= 650) ? 4 : (nbits >= 350) ? 8 :
2174 (nbits >= 250) ? 12 : (nbits >= 150) ? 18 : 27);
2175 } else {
Janos Follathda31fa12018-09-03 14:45:23 +01002176 /*
2177 * 2^-100 error probability, number of rounds computed based on HAC,
2178 * fact 4.48
2179 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 rounds = ((nbits >= 1450) ? 4 : (nbits >= 1150) ? 5 :
2181 (nbits >= 1000) ? 6 : (nbits >= 850) ? 7 :
2182 (nbits >= 750) ? 8 : (nbits >= 500) ? 13 :
2183 (nbits >= 250) ? 28 : (nbits >= 150) ? 40 : 51);
Janos Follathda31fa12018-09-03 14:45:23 +01002184 }
2185
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 while (1) {
2187 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(X, n * ciL, f_rng, p_rng));
Jethro Beekman66689272018-02-14 19:24:10 -08002188 /* 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 +01002189 if (X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2) {
2190 continue;
2191 }
Jethro Beekman66689272018-02-14 19:24:10 -08002192
2193 k = n * biL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 if (k > nbits) {
2195 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(X, k - nbits));
2196 }
Jethro Beekman66689272018-02-14 19:24:10 -08002197 X->p[0] |= 1;
2198
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH) == 0) {
2200 ret = mbedtls_mpi_is_prime_ext(X, rounds, f_rng, p_rng);
Jethro Beekman66689272018-02-14 19:24:10 -08002201
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002203 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 }
2205 } else {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002206 /*
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01002207 * A necessary condition for Y and X = 2Y + 1 to be prime
Jethro Beekman66689272018-02-14 19:24:10 -08002208 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2209 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002210 */
Jethro Beekman66689272018-02-14 19:24:10 -08002211
2212 X->p[0] |= 2;
2213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, 3));
2215 if (r == 0) {
2216 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 8));
2217 } else if (r == 1) {
2218 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 4));
2219 }
Jethro Beekman66689272018-02-14 19:24:10 -08002220
2221 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, X));
2223 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, 1));
Jethro Beekman66689272018-02-14 19:24:10 -08002224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 while (1) {
Jethro Beekman66689272018-02-14 19:24:10 -08002226 /*
2227 * First, check small factors for X and Y
2228 * before doing Miller-Rabin on any of them
2229 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 if ((ret = mpi_check_small_factors(X)) == 0 &&
2231 (ret = mpi_check_small_factors(&Y)) == 0 &&
2232 (ret = mpi_miller_rabin(X, rounds, f_rng, p_rng))
2233 == 0 &&
2234 (ret = mpi_miller_rabin(&Y, rounds, f_rng, p_rng))
2235 == 0) {
Jethro Beekman66689272018-02-14 19:24:10 -08002236 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 }
Jethro Beekman66689272018-02-14 19:24:10 -08002238
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Jethro Beekman66689272018-02-14 19:24:10 -08002240 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 }
Jethro Beekman66689272018-02-14 19:24:10 -08002242
2243 /*
2244 * Next candidates. We want to preserve Y = (X-1) / 2 and
2245 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2246 * so up Y by 6 and X by 12.
2247 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 12));
2249 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&Y, &Y, 6));
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 }
2252 }
2253
2254cleanup:
2255
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 mbedtls_mpi_free(&Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00002257
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002259}
2260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002264
Paul Bakker23986e52011-04-24 08:57:21 +00002265#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002266
2267static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2268{
2269 { 693, 609, 21 },
2270 { 1764, 868, 28 },
2271 { 768454923, 542167814, 1 }
2272};
2273
Paul Bakker5121ce52009-01-03 21:22:43 +00002274/*
2275 * Checkup routine
2276 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002277int mbedtls_mpi_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002278{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002279 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00002281
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 mbedtls_mpi_init(&A); mbedtls_mpi_init(&E); mbedtls_mpi_init(&N); mbedtls_mpi_init(&X);
2283 mbedtls_mpi_init(&Y); mbedtls_mpi_init(&U); mbedtls_mpi_init(&V);
Paul Bakker5121ce52009-01-03 21:22:43 +00002284
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&A, 16,
2286 "EFE021C2645FD1DC586E69184AF4A31E" \
2287 "D5F53E93B5F123FA41680867BA110131" \
2288 "944FE7952E2517337780CB0DB80E61AA" \
2289 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002290
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&E, 16,
2292 "B2E7EFD37075B9F03FF989C7C5051C20" \
2293 "34D2A323810251127E7BF8625A4F49A5" \
2294 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
2295 "5B5C25763222FEFCCFC38B832366C29E"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&N, 16,
2298 "0066A198186C18C10B2F5ED9B522752A" \
2299 "9830B69916E535C8F047518A889A43A5" \
2300 "94B6BED27A168D31D4A52F88925AA8F5"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002301
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&X, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2305 "602AB7ECA597A3D6B56FF9829A5E8B85" \
2306 "9E857EA95A03512E2BAE7391688D264A" \
2307 "A5663B0341DB9CCFD2C4C5F421FEC814" \
2308 "8001B72E848A38CAE1C65F78E56ABDEF" \
2309 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
2310 "ECF677152EF804370C1A305CAF3B5BF1" \
2311 "30879B56C61DE584A0F53A2447A51E"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002312
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 if (verbose != 0) {
2314 mbedtls_printf(" MPI test #1 (mul_mpi): ");
2315 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002316
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2318 if (verbose != 0) {
2319 mbedtls_printf("failed\n");
2320 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002321
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002322 ret = 1;
2323 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002324 }
2325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 if (verbose != 0) {
2327 mbedtls_printf("passed\n");
2328 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&X, &Y, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2333 "256567336059E52CAE22925474705F39A94"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&V, 16,
2336 "6613F26162223DF488E9CD48CC132C7A" \
2337 "0AC93C701B001B092E4E5B9F73BCD27B" \
2338 "9EE50D0657C77F374E903CDFA4C642"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002339
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 if (verbose != 0) {
2341 mbedtls_printf(" MPI test #2 (div_mpi): ");
2342 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0 ||
2345 mbedtls_mpi_cmp_mpi(&Y, &V) != 0) {
2346 if (verbose != 0) {
2347 mbedtls_printf("failed\n");
2348 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002349
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002350 ret = 1;
2351 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002352 }
2353
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 if (verbose != 0) {
2355 mbedtls_printf("passed\n");
2356 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002357
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&X, &A, &E, &N, NULL));
Paul Bakker5121ce52009-01-03 21:22:43 +00002359
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2361 "36E139AEA55215609D2816998ED020BB" \
2362 "BD96C37890F65171D948E9BC7CBAA4D9" \
2363 "325D24D6A3C12710F10A09FA08AB87"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002364
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 if (verbose != 0) {
2366 mbedtls_printf(" MPI test #3 (exp_mod): ");
2367 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2370 if (verbose != 0) {
2371 mbedtls_printf("failed\n");
2372 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002373
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002374 ret = 1;
2375 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002376 }
2377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 if (verbose != 0) {
2379 mbedtls_printf("passed\n");
2380 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002381
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&X, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002383
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2385 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
2386 "C3DBA76456363A10869622EAC2DD84EC" \
2387 "C5B8A74DAC4D09E03B5E0BE779F2DF61"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 if (verbose != 0) {
2390 mbedtls_printf(" MPI test #4 (inv_mod): ");
2391 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002392
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2394 if (verbose != 0) {
2395 mbedtls_printf("failed\n");
2396 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002397
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002398 ret = 1;
2399 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 }
2401
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 if (verbose != 0) {
2403 mbedtls_printf("passed\n");
2404 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002405
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 if (verbose != 0) {
2407 mbedtls_printf(" MPI test #5 (simple gcd): ");
2408 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002409
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 for (i = 0; i < GCD_PAIR_COUNT; i++) {
2411 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&X, gcd_pairs[i][0]));
2412 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Y, gcd_pairs[i][1]));
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&A, &X, &Y));
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002415
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 if (mbedtls_mpi_cmp_int(&A, gcd_pairs[i][2]) != 0) {
2417 if (verbose != 0) {
2418 mbedtls_printf("failed at %d\n", i);
2419 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002420
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002421 ret = 1;
2422 goto cleanup;
2423 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002424 }
2425
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 if (verbose != 0) {
2427 mbedtls_printf("passed\n");
2428 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002429
Paul Bakker5121ce52009-01-03 21:22:43 +00002430cleanup:
2431
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 if (ret != 0 && verbose != 0) {
2433 mbedtls_printf("Unexpected error, return code = %08X\n", (unsigned int) ret);
2434 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002435
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 mbedtls_mpi_free(&A); mbedtls_mpi_free(&E); mbedtls_mpi_free(&N); mbedtls_mpi_free(&X);
2437 mbedtls_mpi_free(&Y); mbedtls_mpi_free(&U); mbedtls_mpi_free(&V);
Paul Bakker5121ce52009-01-03 21:22:43 +00002438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 if (verbose != 0) {
2440 mbedtls_printf("\n");
2441 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002442
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002444}
2445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448#endif /* MBEDTLS_BIGNUM_C */