blob: 3926da418aa897bb4c230805842c4a5e4363b5ff [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
Gilles Peskine449bd832023-01-11 14:50:10 +01001601static void mpi_montg_init(mbedtls_mpi_uint *mm, const mbedtls_mpi *N)
Paul Bakker5121ce52009-01-03 21:22:43 +00001602{
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 *mm = mbedtls_mpi_core_montmul_init(N->p);
Paul Bakker5121ce52009-01-03 21:22:43 +00001604}
1605
Tom Cosgrove93842842022-08-05 16:59:43 +01001606/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
1607 *
1608 * \param[in,out] A One of the numbers to multiply.
1609 * It must have at least as many limbs as N
1610 * (A->n >= N->n), and any limbs beyond n are ignored.
1611 * On successful completion, A contains the result of
1612 * the multiplication A * B * R^-1 mod N where
1613 * R = (2^ciL)^n.
1614 * \param[in] B One of the numbers to multiply.
1615 * It must be nonzero and must not have more limbs than N
1616 * (B->n <= N->n).
Tom Cosgrove40d22942022-08-17 06:42:44 +01001617 * \param[in] N The modulus. \p N must be odd.
Tom Cosgrove93842842022-08-05 16:59:43 +01001618 * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
1619 * This is -N^-1 mod 2^ciL.
1620 * \param[in,out] T A bignum for temporary storage.
1621 * It must be at least twice the limb size of N plus 1
1622 * (T->n >= 2 * N->n + 1).
1623 * Its initial content is unused and
1624 * its final content is indeterminate.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +01001625 * It does not get reallocated.
Tom Cosgrove93842842022-08-05 16:59:43 +01001626 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001627static void mpi_montmul(mbedtls_mpi *A, const mbedtls_mpi *B,
1628 const mbedtls_mpi *N, mbedtls_mpi_uint mm,
1629 mbedtls_mpi *T)
Paul Bakker5121ce52009-01-03 21:22:43 +00001630{
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 mbedtls_mpi_core_montmul(A->p, A->p, B->p, B->n, N->p, N->n, mm, T->p);
Paul Bakker5121ce52009-01-03 21:22:43 +00001632}
1633
1634/*
1635 * Montgomery reduction: A = A * R^-1 mod N
Gilles Peskine2a82f722020-06-04 15:00:49 +02001636 *
Tom Cosgrove93842842022-08-05 16:59:43 +01001637 * See mpi_montmul() regarding constraints and guarantees on the parameters.
Paul Bakker5121ce52009-01-03 21:22:43 +00001638 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001639static void mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N,
1640 mbedtls_mpi_uint mm, mbedtls_mpi *T)
Paul Bakker5121ce52009-01-03 21:22:43 +00001641{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 mbedtls_mpi_uint z = 1;
1643 mbedtls_mpi U;
Gilles Peskine053022f2023-06-29 19:26:48 +02001644 U.n = 1;
1645 U.s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001646 U.p = &z;
1647
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 mpi_montmul(A, &U, N, mm, T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001649}
1650
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001651/**
1652 * Select an MPI from a table without leaking the index.
1653 *
1654 * This is functionally equivalent to mbedtls_mpi_copy(R, T[idx]) except it
1655 * reads the entire table in order to avoid leaking the value of idx to an
1656 * attacker able to observe memory access patterns.
1657 *
1658 * \param[out] R Where to write the selected MPI.
1659 * \param[in] T The table to read from.
1660 * \param[in] T_size The number of elements in the table.
1661 * \param[in] idx The index of the element to select;
1662 * this must satisfy 0 <= idx < T_size.
1663 *
1664 * \return \c 0 on success, or a negative error code.
1665 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001666static int mpi_select(mbedtls_mpi *R, const mbedtls_mpi *T, size_t T_size, size_t idx)
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001667{
1668 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 for (size_t i = 0; i < T_size; i++) {
1671 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(R, &T[i],
Dave Rodgmanb7825ce2023-08-10 11:58:18 +01001672 (unsigned char) mbedtls_ct_uint_eq(i, idx)));
Manuel Pégourié-Gonnard92413ef2021-06-03 10:42:46 +02001673 }
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001674cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 return ret;
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001676}
1677
Paul Bakker5121ce52009-01-03 21:22:43 +00001678/*
1679 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
1680 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001681int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
1682 const mbedtls_mpi *E, const mbedtls_mpi *N,
1683 mbedtls_mpi *prec_RR)
Paul Bakker5121ce52009-01-03 21:22:43 +00001684{
Janos Follath24eed8d2019-11-22 13:21:35 +00001685 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath1ba40582024-02-13 12:36:13 +00001686 mbedtls_mpi RR, T, E_core;
Paul Bakker5121ce52009-01-03 21:22:43 +00001687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 if (mbedtls_mpi_cmp_int(N, 0) <= 0 || (N->p[0] & 1) == 0) {
1689 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1690 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001691
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 if (mbedtls_mpi_cmp_int(E, 0) < 0) {
1693 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1694 }
Paul Bakkerf6198c12012-05-16 08:02:29 +00001695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 if (mbedtls_mpi_bitlen(E) > MBEDTLS_MPI_MAX_BITS ||
1697 mbedtls_mpi_bitlen(N) > MBEDTLS_MPI_MAX_BITS) {
1698 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1699 }
Chris Jones9246d042020-11-25 15:12:39 +00001700
Janos Follath1ba40582024-02-13 12:36:13 +00001701 mbedtls_mpi_init(&RR);
1702 mbedtls_mpi_init(&T);
1703 mbedtls_mpi_init(&E_core);
Paul Bakker50546922012-05-19 08:40:49 +00001704
1705 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001706 * If 1st call, pre-compute R^2 mod N
1707 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 if (prec_RR == NULL || prec_RR->p == NULL) {
Janos Follath1ba40582024-02-13 12:36:13 +00001709 MBEDTLS_MPI_CHK(mbedtls_mpi_core_get_mont_r2_unsafe(&RR, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001710
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 if (prec_RR != NULL) {
1712 memcpy(prec_RR, &RR, sizeof(mbedtls_mpi));
1713 }
1714 } else {
1715 memcpy(&RR, prec_RR, sizeof(mbedtls_mpi));
Paul Bakker5121ce52009-01-03 21:22:43 +00001716 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001717
1718 /*
Janos Follath1ba40582024-02-13 12:36:13 +00001719 * Ensure that the exponent that we are passing to the core is not NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +00001720 */
Janos Follath1ba40582024-02-13 12:36:13 +00001721 if (E->n == 0) {
1722 mbedtls_mpi_lset(&E_core, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 } else {
Janos Follath1ba40582024-02-13 12:36:13 +00001724 memcpy(&E_core, E, sizeof(mbedtls_mpi));
Paul Bakker5121ce52009-01-03 21:22:43 +00001725 }
1726
1727 /*
Janos Follath1ba40582024-02-13 12:36:13 +00001728 * To preserve constness we need to make a copy of A. Using X for this to
1729 * save memory.
Paul Bakker5121ce52009-01-03 21:22:43 +00001730 */
Janos Follath1ba40582024-02-13 12:36:13 +00001731 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
Paul Bakker5121ce52009-01-03 21:22:43 +00001732
1733 /*
Janos Follath1ba40582024-02-13 12:36:13 +00001734 * Compensate for negative A (and correct at the end).
Paul Bakker5121ce52009-01-03 21:22:43 +00001735 */
Janos Follath1ba40582024-02-13 12:36:13 +00001736 X->s = 1;
Paul Bakkerf6198c12012-05-16 08:02:29 +00001737
Janos Follath8e7d6a02022-10-04 13:27:40 +01001738 /*
Janos Follath1ba40582024-02-13 12:36:13 +00001739 * Make sure that A has exactly as many limbs as N.
Janos Follath8e7d6a02022-10-04 13:27:40 +01001740 */
Janos Follath1ba40582024-02-13 12:36:13 +00001741 if (mbedtls_mpi_cmp_mpi(X, N) >= 0) {
1742 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(X, X, N));
1743 }
1744 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, N->n));
1745
1746 /*
1747 * Allocate working memory for mbedtls_mpi_core_exp_mod()
1748 */
1749 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T,
1750 mbedtls_mpi_core_exp_mod_working_limbs(N->n, E_core.n)));
1751
1752 /*
1753 * Convert to and from Montgomery around mbedtls_mpi_core_exp_mod().
1754 */
1755 mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N->p);
1756 mbedtls_mpi_core_to_mont_rep(X->p, X->p, N->p, N->n, mm, RR.p, T.p);
1757 mbedtls_mpi_core_exp_mod(X->p, X->p, N->p, N->n, E_core.p, E_core.n, RR.p,
1758 T.p);
1759 mbedtls_mpi_core_from_mont_rep(X->p, X->p, N->p, N->n, mm, T.p);
1760
1761 /*
1762 * Correct for negative A.
1763 */
1764 if (A->s == -1 && (E_core.p[0] & 1) != 0) {
1765 X->s = -1;
1766 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(X, N, X));
1767 }
Janos Follath8e7d6a02022-10-04 13:27:40 +01001768
Paul Bakker5121ce52009-01-03 21:22:43 +00001769cleanup:
1770
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 mbedtls_mpi_free(&T);
Paul Bakker6c591fa2011-05-05 11:49:20 +00001772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 if (prec_RR == NULL || prec_RR->p == NULL) {
1774 mbedtls_mpi_free(&RR);
1775 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001776
Janos Follath1ba40582024-02-13 12:36:13 +00001777 if (E->n == 0) {
1778 mbedtls_mpi_free(&E_core);
1779 }
1780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001782}
1783
Paul Bakker5121ce52009-01-03 21:22:43 +00001784/*
1785 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
1786 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001787int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001788{
Janos Follath24eed8d2019-11-22 13:21:35 +00001789 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001790 size_t lz, lzt;
Alexander Ke8ad49f2019-08-16 16:16:07 +03001791 mbedtls_mpi TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00001792
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00001794
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A));
1796 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001797
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 lz = mbedtls_mpi_lsb(&TA);
1799 lzt = mbedtls_mpi_lsb(&TB);
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00001800
Gilles Peskine27253bc2021-06-09 13:26:43 +02001801 /* The loop below gives the correct result when A==0 but not when B==0.
1802 * So have a special case for B==0. Leverage the fact that we just
1803 * calculated the lsb and lsb(B)==0 iff B is odd or 0 to make the test
1804 * slightly more efficient than cmp_int(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 if (lzt == 0 && mbedtls_mpi_get_bit(&TB, 0) == 0) {
1806 ret = mbedtls_mpi_copy(G, A);
Gilles Peskine27253bc2021-06-09 13:26:43 +02001807 goto cleanup;
1808 }
1809
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 if (lzt < lz) {
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00001811 lz = lzt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00001813
Paul Bakker5121ce52009-01-03 21:22:43 +00001814 TA.s = TB.s = 1;
1815
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001816 /* We mostly follow the procedure described in HAC 14.54, but with some
1817 * minor differences:
1818 * - Sequences of multiplications or divisions by 2 are grouped into a
1819 * single shift operation.
Gilles Peskineb09c7ee2021-06-21 18:58:39 +02001820 * - The procedure in HAC assumes that 0 < TB <= TA.
1821 * - The condition TB <= TA is not actually necessary for correctness.
1822 * TA and TB have symmetric roles except for the loop termination
1823 * condition, and the shifts at the beginning of the loop body
1824 * remove any significance from the ordering of TA vs TB before
1825 * the shifts.
1826 * - If TA = 0, the loop goes through 0 iterations and the result is
1827 * correctly TB.
1828 * - The case TB = 0 was short-circuited above.
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001829 *
1830 * For the correctness proof below, decompose the original values of
1831 * A and B as
1832 * A = sa * 2^a * A' with A'=0 or A' odd, and sa = +-1
1833 * B = sb * 2^b * B' with B'=0 or B' odd, and sb = +-1
1834 * Then gcd(A, B) = 2^{min(a,b)} * gcd(A',B'),
1835 * and gcd(A',B') is odd or 0.
1836 *
1837 * At the beginning, we have TA = |A| and TB = |B| so gcd(A,B) = gcd(TA,TB).
1838 * The code maintains the following invariant:
1839 * gcd(A,B) = 2^k * gcd(TA,TB) for some k (I)
Gilles Peskine4df3f1f2021-06-15 22:09:39 +02001840 */
1841
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001842 /* Proof that the loop terminates:
1843 * At each iteration, either the right-shift by 1 is made on a nonzero
1844 * value and the nonnegative integer bitlen(TA) + bitlen(TB) decreases
1845 * by at least 1, or the right-shift by 1 is made on zero and then
1846 * TA becomes 0 which ends the loop (TB cannot be 0 if it is right-shifted
1847 * since in that case TB is calculated from TB-TA with the condition TB>TA).
1848 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 while (mbedtls_mpi_cmp_int(&TA, 0) != 0) {
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001850 /* Divisions by 2 preserve the invariant (I). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, mbedtls_mpi_lsb(&TA)));
1852 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, mbedtls_mpi_lsb(&TB)));
Paul Bakker5121ce52009-01-03 21:22:43 +00001853
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001854 /* Set either TA or TB to |TA-TB|/2. Since TA and TB are both odd,
1855 * TA-TB is even so the division by 2 has an integer result.
1856 * Invariant (I) is preserved since any odd divisor of both TA and TB
1857 * also divides |TA-TB|/2, and any odd divisor of both TA and |TA-TB|/2
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001858 * also divides TB, and any odd divisor of both TB and |TA-TB|/2 also
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001859 * divides TA.
1860 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 if (mbedtls_mpi_cmp_mpi(&TA, &TB) >= 0) {
1862 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TA, &TA, &TB));
1863 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, 1));
1864 } else {
1865 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TB, &TB, &TA));
1866 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001867 }
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001868 /* Note that one of TA or TB is still odd. */
Paul Bakker5121ce52009-01-03 21:22:43 +00001869 }
1870
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02001871 /* By invariant (I), gcd(A,B) = 2^k * gcd(TA,TB) for some k.
1872 * At the loop exit, TA = 0, so gcd(TA,TB) = TB.
1873 * - If there was at least one loop iteration, then one of TA or TB is odd,
1874 * and TA = 0, so TB is odd and gcd(TA,TB) = gcd(A',B'). In this case,
1875 * lz = min(a,b) so gcd(A,B) = 2^lz * TB.
1876 * - If there was no loop iteration, then A was 0, and gcd(A,B) = B.
Gilles Peskine4d3fd362021-06-21 11:40:38 +02001877 * 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 +02001878 */
1879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&TB, lz));
1881 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(G, &TB));
Paul Bakker5121ce52009-01-03 21:22:43 +00001882
1883cleanup:
1884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00001886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001888}
1889
Paul Bakker33dc46b2014-04-30 16:11:39 +02001890/*
1891 * Fill X with size bytes of random.
Gilles Peskine22cdd0c2022-10-27 20:15:13 +02001892 * The bytes returned from the RNG are used in a specific order which
1893 * is suitable for deterministic ECDSA (see the specification of
1894 * mbedtls_mpi_random() and the implementation in mbedtls_mpi_fill_random()).
Paul Bakker33dc46b2014-04-30 16:11:39 +02001895 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001896int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
1897 int (*f_rng)(void *, unsigned char *, size_t),
1898 void *p_rng)
Paul Bakker287781a2011-03-26 13:18:49 +00001899{
Janos Follath24eed8d2019-11-22 13:21:35 +00001900 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 const size_t limbs = CHARS_TO_LIMBS(size);
Hanno Beckerda1655a2017-10-18 14:21:44 +01001902
Hanno Beckerda1655a2017-10-18 14:21:44 +01001903 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
1905 if (size == 0) {
1906 return 0;
1907 }
Paul Bakker287781a2011-03-26 13:18:49 +00001908
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 ret = mbedtls_mpi_core_fill_random(X->p, X->n, size, f_rng, p_rng);
Paul Bakker287781a2011-03-26 13:18:49 +00001910
1911cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 return ret;
Paul Bakker287781a2011-03-26 13:18:49 +00001913}
1914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915int mbedtls_mpi_random(mbedtls_mpi *X,
1916 mbedtls_mpi_sint min,
1917 const mbedtls_mpi *N,
1918 int (*f_rng)(void *, unsigned char *, size_t),
1919 void *p_rng)
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001920{
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 if (min < 0) {
1922 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1923 }
1924 if (mbedtls_mpi_cmp_int(N, min) <= 0) {
1925 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1926 }
Gilles Peskine1e918f42021-03-29 22:14:51 +02001927
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001928 /* Ensure that target MPI has exactly the same number of limbs
1929 * as the upper bound, even if the upper bound has leading zeros.
Gilles Peskine6b7ce962022-12-15 15:04:33 +01001930 * This is necessary for mbedtls_mpi_core_random. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 int ret = mbedtls_mpi_resize_clear(X, N->n);
1932 if (ret != 0) {
1933 return ret;
1934 }
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 return mbedtls_mpi_core_random(X->p, min, N->p, X->n, f_rng, p_rng);
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001937}
1938
Paul Bakker5121ce52009-01-03 21:22:43 +00001939/*
1940 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
1941 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001942int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
Paul Bakker5121ce52009-01-03 21:22:43 +00001943{
Janos Follath24eed8d2019-11-22 13:21:35 +00001944 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001946
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 if (mbedtls_mpi_cmp_int(N, 1) <= 0) {
1948 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1949 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001950
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TU); mbedtls_mpi_init(&U1); mbedtls_mpi_init(&U2);
1952 mbedtls_mpi_init(&G); mbedtls_mpi_init(&TB); mbedtls_mpi_init(&TV);
1953 mbedtls_mpi_init(&V1); mbedtls_mpi_init(&V2);
Paul Bakker5121ce52009-01-03 21:22:43 +00001954
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, A, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001956
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001959 goto cleanup;
1960 }
1961
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&TA, A, N));
1963 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TU, &TA));
1964 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, N));
1965 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TV, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001966
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U1, 1));
1968 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U2, 0));
1969 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V1, 0));
1970 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001971
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 do {
1973 while ((TU.p[0] & 1) == 0) {
1974 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TU, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001975
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 if ((U1.p[0] & 1) != 0 || (U2.p[0] & 1) != 0) {
1977 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&U1, &U1, &TB));
1978 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &TA));
Paul Bakker5121ce52009-01-03 21:22:43 +00001979 }
1980
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U1, 1));
1982 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001983 }
1984
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 while ((TV.p[0] & 1) == 0) {
1986 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TV, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001987
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 if ((V1.p[0] & 1) != 0 || (V2.p[0] & 1) != 0) {
1989 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, &TB));
1990 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &TA));
Paul Bakker5121ce52009-01-03 21:22:43 +00001991 }
1992
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V1, 1));
1994 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001995 }
1996
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 if (mbedtls_mpi_cmp_mpi(&TU, &TV) >= 0) {
1998 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TU, &TU, &TV));
1999 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U1, &U1, &V1));
2000 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &V2));
2001 } else {
2002 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TV, &TV, &TU));
2003 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, &U1));
2004 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &U2));
Paul Bakker5121ce52009-01-03 21:22:43 +00002005 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 } while (mbedtls_mpi_cmp_int(&TU, 0) != 0);
2007
2008 while (mbedtls_mpi_cmp_int(&V1, 0) < 0) {
2009 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002010 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002011
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 while (mbedtls_mpi_cmp_mpi(&V1, N) >= 0) {
2013 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, N));
2014 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002015
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, &V1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002017
2018cleanup:
2019
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TU); mbedtls_mpi_free(&U1); mbedtls_mpi_free(&U2);
2021 mbedtls_mpi_free(&G); mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TV);
2022 mbedtls_mpi_free(&V1); mbedtls_mpi_free(&V2);
Paul Bakker5121ce52009-01-03 21:22:43 +00002023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002025}
2026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002028
Gilles Peskineb2bc1712019-02-08 17:27:11 +01002029/* Gaps between primes, starting at 3. https://oeis.org/A001223 */
2030static const unsigned char small_prime_gaps[] = {
2031 2, 2, 4, 2, 4, 2, 4, 6,
2032 2, 6, 4, 2, 4, 6, 6, 2,
2033 6, 4, 2, 6, 4, 6, 8, 4,
2034 2, 4, 2, 4, 14, 4, 6, 2,
2035 10, 2, 6, 6, 4, 6, 6, 2,
2036 10, 2, 4, 2, 12, 12, 4, 2,
2037 4, 6, 2, 10, 6, 6, 6, 2,
2038 6, 4, 2, 10, 14, 4, 2, 4,
2039 14, 6, 10, 2, 4, 6, 8, 6,
2040 6, 4, 6, 8, 4, 8, 10, 2,
2041 10, 2, 6, 4, 6, 8, 4, 2,
2042 4, 12, 8, 4, 8, 4, 6, 12,
2043 2, 18, 6, 10, 6, 6, 2, 6,
2044 10, 6, 6, 2, 6, 6, 4, 2,
2045 12, 10, 2, 4, 6, 6, 2, 12,
2046 4, 6, 8, 10, 8, 10, 8, 6,
2047 6, 4, 8, 6, 4, 8, 4, 14,
2048 10, 12, 2, 10, 2, 4, 2, 10,
2049 14, 4, 2, 4, 14, 4, 2, 4,
2050 20, 4, 8, 10, 8, 4, 6, 6,
2051 14, 4, 6, 6, 8, 6, /*reaches 997*/
Gilles Peskine30b03782023-08-22 11:06:47 +02002052 0 /* the last entry is effectively unused */
Paul Bakker5121ce52009-01-03 21:22:43 +00002053};
2054
2055/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002056 * Small divisors test (X must be positive)
2057 *
2058 * Return values:
2059 * 0: no small factor (possible prime, more tests needed)
2060 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002062 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002063 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002064static int mpi_check_small_factors(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +00002065{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002066 int ret = 0;
2067 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068 mbedtls_mpi_uint r;
Gilles Peskineb2bc1712019-02-08 17:27:11 +01002069 unsigned p = 3; /* The first odd prime */
Paul Bakker5121ce52009-01-03 21:22:43 +00002070
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 if ((X->p[0] & 1) == 0) {
2072 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2073 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002074
Gilles Peskineb2bc1712019-02-08 17:27:11 +01002075 for (i = 0; i < sizeof(small_prime_gaps); p += small_prime_gaps[i], i++) {
2076 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, p));
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 if (r == 0) {
Gilles Peskineb2bc1712019-02-08 17:27:11 +01002078 if (mbedtls_mpi_cmp_int(X, p) == 0) {
2079 return 1;
2080 } else {
2081 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2082 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002084 }
2085
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002086cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 return ret;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002088}
2089
2090/*
2091 * Miller-Rabin pseudo-primality test (HAC 4.24)
2092 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002093static int mpi_miller_rabin(const mbedtls_mpi *X, size_t rounds,
2094 int (*f_rng)(void *, unsigned char *, size_t),
2095 void *p_rng)
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002096{
Pascal Junodb99183d2015-03-11 16:49:45 +01002097 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002098 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002100
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 mbedtls_mpi_init(&W); mbedtls_mpi_init(&R);
2102 mbedtls_mpi_init(&T); mbedtls_mpi_init(&A);
2103 mbedtls_mpi_init(&RR);
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002104
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 /*
2106 * W = |X| - 1
2107 * R = W >> lsb( W )
2108 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&W, X, 1));
2110 s = mbedtls_mpi_lsb(&W);
2111 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R, &W));
2112 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&R, s));
Paul Bakker5121ce52009-01-03 21:22:43 +00002113
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 for (i = 0; i < rounds; i++) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 /*
2116 * pick a random A, 1 < A < |X| - 1
2117 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002118 count = 0;
2119 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&A, X->n * ciL, f_rng, p_rng));
Pascal Junodb99183d2015-03-11 16:49:45 +01002121
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 j = mbedtls_mpi_bitlen(&A);
2123 k = mbedtls_mpi_bitlen(&W);
Pascal Junodb99183d2015-03-11 16:49:45 +01002124 if (j > k) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 A.p[A.n - 1] &= ((mbedtls_mpi_uint) 1 << (k - (A.n - 1) * biL - 1)) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002126 }
2127
2128 if (count++ > 30) {
Jens Wiklanderf08aa3e2019-01-17 13:30:57 +01002129 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2130 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002131 }
2132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 } while (mbedtls_mpi_cmp_mpi(&A, &W) >= 0 ||
2134 mbedtls_mpi_cmp_int(&A, 1) <= 0);
Paul Bakker5121ce52009-01-03 21:22:43 +00002135
2136 /*
2137 * A = A^R mod |X|
2138 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&A, &A, &R, X, &RR));
Paul Bakker5121ce52009-01-03 21:22:43 +00002140
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 if (mbedtls_mpi_cmp_mpi(&A, &W) == 0 ||
2142 mbedtls_mpi_cmp_int(&A, 1) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002143 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002145
2146 j = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 while (j < s && mbedtls_mpi_cmp_mpi(&A, &W) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 /*
2149 * A = A * A mod |X|
2150 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &A, &A));
2152 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&A, &T, X));
Paul Bakker5121ce52009-01-03 21:22:43 +00002153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 if (mbedtls_mpi_cmp_int(&A, 1) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002155 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002157
2158 j++;
2159 }
2160
2161 /*
2162 * not prime if A != |X| - 1 or A == 1
2163 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 if (mbedtls_mpi_cmp_mpi(&A, &W) != 0 ||
2165 mbedtls_mpi_cmp_int(&A, 1) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002167 break;
2168 }
2169 }
2170
2171cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 mbedtls_mpi_free(&W); mbedtls_mpi_free(&R);
2173 mbedtls_mpi_free(&T); mbedtls_mpi_free(&A);
2174 mbedtls_mpi_free(&RR);
Paul Bakker5121ce52009-01-03 21:22:43 +00002175
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002177}
2178
2179/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002180 * Pseudo-primality test: small factors, then Miller-Rabin
2181 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002182int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
2183 int (*f_rng)(void *, unsigned char *, size_t),
2184 void *p_rng)
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002185{
Janos Follath24eed8d2019-11-22 13:21:35 +00002186 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 mbedtls_mpi XX;
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002188
2189 XX.s = 1;
2190 XX.n = X->n;
2191 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002192
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 if (mbedtls_mpi_cmp_int(&XX, 0) == 0 ||
2194 mbedtls_mpi_cmp_int(&XX, 1) == 0) {
2195 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002196 }
2197
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 if (mbedtls_mpi_cmp_int(&XX, 2) == 0) {
2199 return 0;
2200 }
2201
2202 if ((ret = mpi_check_small_factors(&XX)) != 0) {
2203 if (ret == 1) {
2204 return 0;
2205 }
2206
2207 return ret;
2208 }
2209
2210 return mpi_miller_rabin(&XX, rounds, f_rng, p_rng);
Janos Follathf301d232018-08-14 13:34:01 +01002211}
2212
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002213/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002214 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002215 *
Janos Follathf301d232018-08-14 13:34:01 +01002216 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2217 * be either 1024 bits or 1536 bits long, and flags must contain
2218 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002220int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
2221 int (*f_rng)(void *, unsigned char *, size_t),
2222 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00002223{
Jethro Beekman66689272018-02-14 19:24:10 -08002224#ifdef MBEDTLS_HAVE_INT64
2225// ceil(2^63.5)
2226#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2227#else
2228// ceil(2^31.5)
2229#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2230#endif
2231 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002232 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002233 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 mbedtls_mpi_uint r;
2235 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002236
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 if (nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS) {
2238 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2239 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 mbedtls_mpi_init(&Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00002242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 n = BITS_TO_LIMBS(nbits);
Paul Bakker5121ce52009-01-03 21:22:43 +00002244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR) == 0) {
Janos Follathda31fa12018-09-03 14:45:23 +01002246 /*
2247 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2248 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 rounds = ((nbits >= 1300) ? 2 : (nbits >= 850) ? 3 :
2250 (nbits >= 650) ? 4 : (nbits >= 350) ? 8 :
2251 (nbits >= 250) ? 12 : (nbits >= 150) ? 18 : 27);
2252 } else {
Janos Follathda31fa12018-09-03 14:45:23 +01002253 /*
2254 * 2^-100 error probability, number of rounds computed based on HAC,
2255 * fact 4.48
2256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 rounds = ((nbits >= 1450) ? 4 : (nbits >= 1150) ? 5 :
2258 (nbits >= 1000) ? 6 : (nbits >= 850) ? 7 :
2259 (nbits >= 750) ? 8 : (nbits >= 500) ? 13 :
2260 (nbits >= 250) ? 28 : (nbits >= 150) ? 40 : 51);
Janos Follathda31fa12018-09-03 14:45:23 +01002261 }
2262
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 while (1) {
2264 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(X, n * ciL, f_rng, p_rng));
Jethro Beekman66689272018-02-14 19:24:10 -08002265 /* 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 +01002266 if (X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2) {
2267 continue;
2268 }
Jethro Beekman66689272018-02-14 19:24:10 -08002269
2270 k = n * biL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 if (k > nbits) {
2272 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(X, k - nbits));
2273 }
Jethro Beekman66689272018-02-14 19:24:10 -08002274 X->p[0] |= 1;
2275
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH) == 0) {
2277 ret = mbedtls_mpi_is_prime_ext(X, rounds, f_rng, p_rng);
Jethro Beekman66689272018-02-14 19:24:10 -08002278
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002280 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 }
2282 } else {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002283 /*
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01002284 * A necessary condition for Y and X = 2Y + 1 to be prime
Jethro Beekman66689272018-02-14 19:24:10 -08002285 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2286 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002287 */
Jethro Beekman66689272018-02-14 19:24:10 -08002288
2289 X->p[0] |= 2;
2290
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, 3));
2292 if (r == 0) {
2293 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 8));
2294 } else if (r == 1) {
2295 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 4));
2296 }
Jethro Beekman66689272018-02-14 19:24:10 -08002297
2298 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, X));
2300 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, 1));
Jethro Beekman66689272018-02-14 19:24:10 -08002301
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 while (1) {
Jethro Beekman66689272018-02-14 19:24:10 -08002303 /*
2304 * First, check small factors for X and Y
2305 * before doing Miller-Rabin on any of them
2306 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 if ((ret = mpi_check_small_factors(X)) == 0 &&
2308 (ret = mpi_check_small_factors(&Y)) == 0 &&
2309 (ret = mpi_miller_rabin(X, rounds, f_rng, p_rng))
2310 == 0 &&
2311 (ret = mpi_miller_rabin(&Y, rounds, f_rng, p_rng))
2312 == 0) {
Jethro Beekman66689272018-02-14 19:24:10 -08002313 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 }
Jethro Beekman66689272018-02-14 19:24:10 -08002315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Jethro Beekman66689272018-02-14 19:24:10 -08002317 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 }
Jethro Beekman66689272018-02-14 19:24:10 -08002319
2320 /*
2321 * Next candidates. We want to preserve Y = (X-1) / 2 and
2322 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2323 * so up Y by 6 and X by 12.
2324 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 12));
2326 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&Y, &Y, 6));
Paul Bakker5121ce52009-01-03 21:22:43 +00002327 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 }
2329 }
2330
2331cleanup:
2332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 mbedtls_mpi_free(&Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00002334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002336}
2337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002341
Paul Bakker23986e52011-04-24 08:57:21 +00002342#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002343
2344static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2345{
2346 { 693, 609, 21 },
2347 { 1764, 868, 28 },
2348 { 768454923, 542167814, 1 }
2349};
2350
Paul Bakker5121ce52009-01-03 21:22:43 +00002351/*
2352 * Checkup routine
2353 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002354int mbedtls_mpi_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002355{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002356 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00002358
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 mbedtls_mpi_init(&A); mbedtls_mpi_init(&E); mbedtls_mpi_init(&N); mbedtls_mpi_init(&X);
2360 mbedtls_mpi_init(&Y); mbedtls_mpi_init(&U); mbedtls_mpi_init(&V);
Paul Bakker5121ce52009-01-03 21:22:43 +00002361
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&A, 16,
2363 "EFE021C2645FD1DC586E69184AF4A31E" \
2364 "D5F53E93B5F123FA41680867BA110131" \
2365 "944FE7952E2517337780CB0DB80E61AA" \
2366 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&E, 16,
2369 "B2E7EFD37075B9F03FF989C7C5051C20" \
2370 "34D2A323810251127E7BF8625A4F49A5" \
2371 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
2372 "5B5C25763222FEFCCFC38B832366C29E"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002373
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&N, 16,
2375 "0066A198186C18C10B2F5ED9B522752A" \
2376 "9830B69916E535C8F047518A889A43A5" \
2377 "94B6BED27A168D31D4A52F88925AA8F5"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002378
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&X, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2382 "602AB7ECA597A3D6B56FF9829A5E8B85" \
2383 "9E857EA95A03512E2BAE7391688D264A" \
2384 "A5663B0341DB9CCFD2C4C5F421FEC814" \
2385 "8001B72E848A38CAE1C65F78E56ABDEF" \
2386 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
2387 "ECF677152EF804370C1A305CAF3B5BF1" \
2388 "30879B56C61DE584A0F53A2447A51E"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 if (verbose != 0) {
2391 mbedtls_printf(" MPI test #1 (mul_mpi): ");
2392 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002393
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2395 if (verbose != 0) {
2396 mbedtls_printf("failed\n");
2397 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002398
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002399 ret = 1;
2400 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 }
2402
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 if (verbose != 0) {
2404 mbedtls_printf("passed\n");
2405 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002406
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&X, &Y, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002408
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2410 "256567336059E52CAE22925474705F39A94"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002411
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&V, 16,
2413 "6613F26162223DF488E9CD48CC132C7A" \
2414 "0AC93C701B001B092E4E5B9F73BCD27B" \
2415 "9EE50D0657C77F374E903CDFA4C642"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 if (verbose != 0) {
2418 mbedtls_printf(" MPI test #2 (div_mpi): ");
2419 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0 ||
2422 mbedtls_mpi_cmp_mpi(&Y, &V) != 0) {
2423 if (verbose != 0) {
2424 mbedtls_printf("failed\n");
2425 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002426
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002427 ret = 1;
2428 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 }
2430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 if (verbose != 0) {
2432 mbedtls_printf("passed\n");
2433 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002434
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&X, &A, &E, &N, NULL));
Paul Bakker5121ce52009-01-03 21:22:43 +00002436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2438 "36E139AEA55215609D2816998ED020BB" \
2439 "BD96C37890F65171D948E9BC7CBAA4D9" \
2440 "325D24D6A3C12710F10A09FA08AB87"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002441
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 if (verbose != 0) {
2443 mbedtls_printf(" MPI test #3 (exp_mod): ");
2444 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002445
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2447 if (verbose != 0) {
2448 mbedtls_printf("failed\n");
2449 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002450
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002451 ret = 1;
2452 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 }
2454
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 if (verbose != 0) {
2456 mbedtls_printf("passed\n");
2457 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002458
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&X, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002460
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2462 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
2463 "C3DBA76456363A10869622EAC2DD84EC" \
2464 "C5B8A74DAC4D09E03B5E0BE779F2DF61"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002465
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 if (verbose != 0) {
2467 mbedtls_printf(" MPI test #4 (inv_mod): ");
2468 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002469
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2471 if (verbose != 0) {
2472 mbedtls_printf("failed\n");
2473 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002474
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002475 ret = 1;
2476 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002477 }
2478
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 if (verbose != 0) {
2480 mbedtls_printf("passed\n");
2481 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 if (verbose != 0) {
2484 mbedtls_printf(" MPI test #5 (simple gcd): ");
2485 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002486
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 for (i = 0; i < GCD_PAIR_COUNT; i++) {
2488 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&X, gcd_pairs[i][0]));
2489 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Y, gcd_pairs[i][1]));
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002490
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&A, &X, &Y));
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002492
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 if (mbedtls_mpi_cmp_int(&A, gcd_pairs[i][2]) != 0) {
2494 if (verbose != 0) {
2495 mbedtls_printf("failed at %d\n", i);
2496 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002497
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002498 ret = 1;
2499 goto cleanup;
2500 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002501 }
2502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 if (verbose != 0) {
2504 mbedtls_printf("passed\n");
2505 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002506
Paul Bakker5121ce52009-01-03 21:22:43 +00002507cleanup:
2508
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 if (ret != 0 && verbose != 0) {
2510 mbedtls_printf("Unexpected error, return code = %08X\n", (unsigned int) ret);
2511 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 mbedtls_mpi_free(&A); mbedtls_mpi_free(&E); mbedtls_mpi_free(&N); mbedtls_mpi_free(&X);
2514 mbedtls_mpi_free(&Y); mbedtls_mpi_free(&U); mbedtls_mpi_free(&V);
Paul Bakker5121ce52009-01-03 21:22:43 +00002515
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 if (verbose != 0) {
2517 mbedtls_printf("\n");
2518 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002521}
2522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525#endif /* MBEDTLS_BIGNUM_C */