blob: d3d72ab7473f2479d8599651d0d6f8914b07bb26 [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 Follath74601202022-11-21 15:54:20 +00001686 size_t window_bitsize;
Paul Bakker23986e52011-04-24 08:57:21 +00001687 size_t i, j, nblimbs;
1688 size_t bufsize, nbits;
Paul Elliott1748de12023-02-13 15:35:35 +00001689 size_t exponent_bits_in_window = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 mbedtls_mpi_uint ei, mm, state;
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 mbedtls_mpi RR, T, W[(size_t) 1 << MBEDTLS_MPI_WINDOW_SIZE], WW, Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00001692 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 if (mbedtls_mpi_cmp_int(N, 0) <= 0 || (N->p[0] & 1) == 0) {
1695 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1696 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 if (mbedtls_mpi_cmp_int(E, 0) < 0) {
1699 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1700 }
Paul Bakkerf6198c12012-05-16 08:02:29 +00001701
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 if (mbedtls_mpi_bitlen(E) > MBEDTLS_MPI_MAX_BITS ||
1703 mbedtls_mpi_bitlen(N) > MBEDTLS_MPI_MAX_BITS) {
1704 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1705 }
Chris Jones9246d042020-11-25 15:12:39 +00001706
Paul Bakkerf6198c12012-05-16 08:02:29 +00001707 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001708 * Init temps and window size
1709 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 mpi_montg_init(&mm, N);
1711 mbedtls_mpi_init(&RR); mbedtls_mpi_init(&T);
1712 mbedtls_mpi_init(&Apos);
1713 mbedtls_mpi_init(&WW);
1714 memset(W, 0, sizeof(W));
Paul Bakker5121ce52009-01-03 21:22:43 +00001715
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 i = mbedtls_mpi_bitlen(E);
Paul Bakker5121ce52009-01-03 21:22:43 +00001717
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 window_bitsize = (i > 671) ? 6 : (i > 239) ? 5 :
1719 (i > 79) ? 4 : (i > 23) ? 3 : 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001720
Gilles Peskine449bd832023-01-11 14:50:10 +01001721#if (MBEDTLS_MPI_WINDOW_SIZE < 6)
1722 if (window_bitsize > MBEDTLS_MPI_WINDOW_SIZE) {
Janos Follath7fa11b82022-11-21 14:48:02 +00001723 window_bitsize = MBEDTLS_MPI_WINDOW_SIZE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 }
Peter Kolbuse6bcad32018-12-11 14:01:44 -06001725#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00001726
Janos Follathc8d66d52022-11-22 10:47:10 +00001727 const size_t w_table_used_size = (size_t) 1 << window_bitsize;
Janos Follath06000952022-11-22 10:18:06 +00001728
Paul Bakker5121ce52009-01-03 21:22:43 +00001729 /*
Janos Follathbe54ca72022-11-21 16:14:54 +00001730 * This function is not constant-trace: its memory accesses depend on the
1731 * exponent value. To defend against timing attacks, callers (such as RSA
1732 * and DHM) should use exponent blinding. However this is not enough if the
1733 * adversary can find the exponent in a single trace, so this function
1734 * takes extra precautions against adversaries who can observe memory
1735 * access patterns.
Janos Follathf08b40e2022-11-11 15:56:38 +00001736 *
Janos Follathbe54ca72022-11-21 16:14:54 +00001737 * This function performs a series of multiplications by table elements and
1738 * squarings, and we want the prevent the adversary from finding out which
1739 * table element was used, and from distinguishing between multiplications
1740 * and squarings. Firstly, when multiplying by an element of the window
1741 * W[i], we do a constant-trace table lookup to obfuscate i. This leaves
1742 * squarings as having a different memory access patterns from other
Gilles Peskinee6cb45e2023-08-10 15:59:28 +02001743 * multiplications. So secondly, we put the accumulator in the table as
1744 * well, and also do a constant-trace table lookup to multiply by the
1745 * accumulator which is W[x_index].
Janos Follathbe54ca72022-11-21 16:14:54 +00001746 *
1747 * This way, all multiplications take the form of a lookup-and-multiply.
1748 * The number of lookup-and-multiply operations inside each iteration of
1749 * the main loop still depends on the bits of the exponent, but since the
1750 * other operations in the loop don't have an easily recognizable memory
1751 * trace, an adversary is unlikely to be able to observe the exact
1752 * patterns.
1753 *
1754 * An adversary may still be able to recover the exponent if they can
1755 * observe both memory accesses and branches. However, branch prediction
1756 * exploitation typically requires many traces of execution over the same
1757 * data, which is defeated by randomized blinding.
Janos Follath8e7d6a02022-10-04 13:27:40 +01001758 */
Janos Follathc8d66d52022-11-22 10:47:10 +00001759 const size_t x_index = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 mbedtls_mpi_init(&W[x_index]);
Janos Follath84461482022-11-21 14:31:22 +00001761
Paul Bakker5121ce52009-01-03 21:22:43 +00001762 j = N->n + 1;
Gilles Peskinee6cb45e2023-08-10 15:59:28 +02001763 /* All W[i] including the accumulator must have at least N->n limbs for
1764 * the mpi_montmul() and mpi_montred() calls later. Here we ensure that
1765 * W[1] and the accumulator W[x_index] are large enough. later we'll grow
1766 * other W[i] to the same length. They must not be shrunk midway through
1767 * this function!
Paul Bakker5121ce52009-01-03 21:22:43 +00001768 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[x_index], j));
1770 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[1], j));
1771 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T, j * 2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001772
1773 /*
Paul Bakker50546922012-05-19 08:40:49 +00001774 * Compensate for negative A (and correct at the end)
1775 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 neg = (A->s == -1);
1777 if (neg) {
1778 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Apos, A));
Paul Bakker50546922012-05-19 08:40:49 +00001779 Apos.s = 1;
1780 A = &Apos;
1781 }
1782
1783 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001784 * If 1st call, pre-compute R^2 mod N
1785 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 if (prec_RR == NULL || prec_RR->p == NULL) {
1787 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&RR, 1));
1788 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&RR, N->n * 2 * biL));
1789 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&RR, &RR, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001790
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 if (prec_RR != NULL) {
1792 memcpy(prec_RR, &RR, sizeof(mbedtls_mpi));
1793 }
1794 } else {
1795 memcpy(&RR, prec_RR, sizeof(mbedtls_mpi));
Paul Bakker5121ce52009-01-03 21:22:43 +00001796 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001797
1798 /*
1799 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
1800 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 if (mbedtls_mpi_cmp_mpi(A, N) >= 0) {
1802 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&W[1], A, N));
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001803 /* This should be a no-op because W[1] is already that large before
1804 * mbedtls_mpi_mod_mpi(), but it's necessary to avoid an overflow
Tom Cosgrove93842842022-08-05 16:59:43 +01001805 * in mpi_montmul() below, so let's make sure. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[1], N->n + 1));
1807 } else {
1808 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[1], A));
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001809 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001810
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001811 /* Note that this is safe because W[1] always has at least N->n limbs
1812 * (it grew above and was preserved by mbedtls_mpi_copy()). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 mpi_montmul(&W[1], &RR, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001814
1815 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01001816 * W[x_index] = R^2 * R^-1 mod N = R mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001817 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[x_index], &RR));
1819 mpi_montred(&W[x_index], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001820
Janos Follathc8d66d52022-11-22 10:47:10 +00001821
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 if (window_bitsize > 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001823 /*
Janos Follath74601202022-11-21 15:54:20 +00001824 * W[i] = W[1] ^ i
1825 *
1826 * The first bit of the sliding window is always 1 and therefore we
1827 * only need to store the second half of the table.
Janos Follathc8d66d52022-11-22 10:47:10 +00001828 *
1829 * (There are two special elements in the table: W[0] for the
1830 * accumulator/result and W[1] for A in Montgomery form. Both of these
1831 * are already set at this point.)
Paul Bakker5121ce52009-01-03 21:22:43 +00001832 */
Janos Follath74601202022-11-21 15:54:20 +00001833 j = w_table_used_size / 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001834
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[j], N->n + 1));
1836 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[j], &W[1]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001837
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 for (i = 0; i < window_bitsize - 1; i++) {
1839 mpi_montmul(&W[j], &W[j], N, mm, &T);
1840 }
Paul Bakker0d7702c2013-10-29 16:18:35 +01001841
Paul Bakker5121ce52009-01-03 21:22:43 +00001842 /*
1843 * W[i] = W[i - 1] * W[1]
1844 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 for (i = j + 1; i < w_table_used_size; i++) {
1846 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[i], N->n + 1));
1847 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[i], &W[i - 1]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001848
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 mpi_montmul(&W[i], &W[1], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001850 }
1851 }
1852
1853 nblimbs = E->n;
1854 bufsize = 0;
1855 nbits = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001856 state = 0;
1857
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 while (1) {
1859 if (bufsize == 0) {
1860 if (nblimbs == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001861 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001863
Paul Bakker0d7702c2013-10-29 16:18:35 +01001864 nblimbs--;
1865
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 bufsize = sizeof(mbedtls_mpi_uint) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00001867 }
1868
1869 bufsize--;
1870
1871 ei = (E->p[nblimbs] >> bufsize) & 1;
1872
1873 /*
1874 * skip leading 0s
1875 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 if (ei == 0 && state == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001877 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 if (ei == 0 && state == 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001881 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01001882 * out of window, square W[x_index]
Paul Bakker5121ce52009-01-03 21:22:43 +00001883 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, x_index));
1885 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001886 continue;
1887 }
1888
1889 /*
1890 * add ei to current window
1891 */
1892 state = 2;
1893
1894 nbits++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 exponent_bits_in_window |= (ei << (window_bitsize - nbits));
Paul Bakker5121ce52009-01-03 21:22:43 +00001896
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 if (nbits == window_bitsize) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001898 /*
Janos Follath7fa11b82022-11-21 14:48:02 +00001899 * W[x_index] = W[x_index]^window_bitsize R^-1 mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001900 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 for (i = 0; i < window_bitsize; i++) {
1902 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size,
1903 x_index));
1904 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Janos Follathb764ee12022-10-04 14:00:09 +01001905 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001906
1907 /*
Janos Follath7fa11b82022-11-21 14:48:02 +00001908 * W[x_index] = W[x_index] * W[exponent_bits_in_window] R^-1 mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001909 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size,
1911 exponent_bits_in_window));
1912 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001913
1914 state--;
1915 nbits = 0;
Janos Follath7fa11b82022-11-21 14:48:02 +00001916 exponent_bits_in_window = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001917 }
1918 }
1919
1920 /*
1921 * process the remaining bits
1922 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 for (i = 0; i < nbits; i++) {
1924 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, x_index));
1925 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001926
Janos Follath7fa11b82022-11-21 14:48:02 +00001927 exponent_bits_in_window <<= 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001928
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 if ((exponent_bits_in_window & ((size_t) 1 << window_bitsize)) != 0) {
1930 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, 1));
1931 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Janos Follathb764ee12022-10-04 14:00:09 +01001932 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001933 }
1934
1935 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01001936 * W[x_index] = A^E * R * R^-1 mod N = A^E mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001937 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 mpi_montred(&W[x_index], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 if (neg && E->n != 0 && (E->p[0] & 1) != 0) {
Janos Follath8e7d6a02022-10-04 13:27:40 +01001941 W[x_index].s = -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&W[x_index], N, &W[x_index]));
Paul Bakkerf6198c12012-05-16 08:02:29 +00001943 }
1944
Janos Follath8e7d6a02022-10-04 13:27:40 +01001945 /*
1946 * Load the result in the output variable.
1947 */
Chien Wonge2caf412023-08-01 21:38:46 +08001948 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, &W[x_index]));
Janos Follath8e7d6a02022-10-04 13:27:40 +01001949
Paul Bakker5121ce52009-01-03 21:22:43 +00001950cleanup:
1951
Janos Follathb2c2fca2022-11-21 15:05:31 +00001952 /* The first bit of the sliding window is always 1 and therefore the first
1953 * half of the table was unused. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 for (i = w_table_used_size/2; i < w_table_used_size; i++) {
1955 mbedtls_mpi_free(&W[i]);
1956 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001957
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 mbedtls_mpi_free(&W[x_index]);
1959 mbedtls_mpi_free(&W[1]);
1960 mbedtls_mpi_free(&T);
1961 mbedtls_mpi_free(&Apos);
1962 mbedtls_mpi_free(&WW);
Paul Bakker6c591fa2011-05-05 11:49:20 +00001963
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 if (prec_RR == NULL || prec_RR->p == NULL) {
1965 mbedtls_mpi_free(&RR);
1966 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001967
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001969}
1970
Paul Bakker5121ce52009-01-03 21:22:43 +00001971/*
1972 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
1973 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001974int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001975{
Janos Follath24eed8d2019-11-22 13:21:35 +00001976 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001977 size_t lz, lzt;
Alexander Ke8ad49f2019-08-16 16:16:07 +03001978 mbedtls_mpi TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00001979
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00001981
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A));
1983 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001984
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 lz = mbedtls_mpi_lsb(&TA);
1986 lzt = mbedtls_mpi_lsb(&TB);
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00001987
Gilles Peskine27253bc2021-06-09 13:26:43 +02001988 /* The loop below gives the correct result when A==0 but not when B==0.
1989 * So have a special case for B==0. Leverage the fact that we just
1990 * calculated the lsb and lsb(B)==0 iff B is odd or 0 to make the test
1991 * slightly more efficient than cmp_int(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 if (lzt == 0 && mbedtls_mpi_get_bit(&TB, 0) == 0) {
1993 ret = mbedtls_mpi_copy(G, A);
Gilles Peskine27253bc2021-06-09 13:26:43 +02001994 goto cleanup;
1995 }
1996
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 if (lzt < lz) {
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00001998 lz = lzt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002000
Paul Bakker5121ce52009-01-03 21:22:43 +00002001 TA.s = TB.s = 1;
2002
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002003 /* We mostly follow the procedure described in HAC 14.54, but with some
2004 * minor differences:
2005 * - Sequences of multiplications or divisions by 2 are grouped into a
2006 * single shift operation.
Gilles Peskineb09c7ee2021-06-21 18:58:39 +02002007 * - The procedure in HAC assumes that 0 < TB <= TA.
2008 * - The condition TB <= TA is not actually necessary for correctness.
2009 * TA and TB have symmetric roles except for the loop termination
2010 * condition, and the shifts at the beginning of the loop body
2011 * remove any significance from the ordering of TA vs TB before
2012 * the shifts.
2013 * - If TA = 0, the loop goes through 0 iterations and the result is
2014 * correctly TB.
2015 * - The case TB = 0 was short-circuited above.
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002016 *
2017 * For the correctness proof below, decompose the original values of
2018 * A and B as
2019 * A = sa * 2^a * A' with A'=0 or A' odd, and sa = +-1
2020 * B = sb * 2^b * B' with B'=0 or B' odd, and sb = +-1
2021 * Then gcd(A, B) = 2^{min(a,b)} * gcd(A',B'),
2022 * and gcd(A',B') is odd or 0.
2023 *
2024 * At the beginning, we have TA = |A| and TB = |B| so gcd(A,B) = gcd(TA,TB).
2025 * The code maintains the following invariant:
2026 * gcd(A,B) = 2^k * gcd(TA,TB) for some k (I)
Gilles Peskine4df3f1f2021-06-15 22:09:39 +02002027 */
2028
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002029 /* Proof that the loop terminates:
2030 * At each iteration, either the right-shift by 1 is made on a nonzero
2031 * value and the nonnegative integer bitlen(TA) + bitlen(TB) decreases
2032 * by at least 1, or the right-shift by 1 is made on zero and then
2033 * TA becomes 0 which ends the loop (TB cannot be 0 if it is right-shifted
2034 * since in that case TB is calculated from TB-TA with the condition TB>TA).
2035 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 while (mbedtls_mpi_cmp_int(&TA, 0) != 0) {
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002037 /* Divisions by 2 preserve the invariant (I). */
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, mbedtls_mpi_lsb(&TA)));
2039 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, mbedtls_mpi_lsb(&TB)));
Paul Bakker5121ce52009-01-03 21:22:43 +00002040
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002041 /* Set either TA or TB to |TA-TB|/2. Since TA and TB are both odd,
2042 * TA-TB is even so the division by 2 has an integer result.
2043 * Invariant (I) is preserved since any odd divisor of both TA and TB
2044 * also divides |TA-TB|/2, and any odd divisor of both TA and |TA-TB|/2
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002045 * also divides TB, and any odd divisor of both TB and |TA-TB|/2 also
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002046 * divides TA.
2047 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 if (mbedtls_mpi_cmp_mpi(&TA, &TB) >= 0) {
2049 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TA, &TA, &TB));
2050 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, 1));
2051 } else {
2052 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TB, &TB, &TA));
2053 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002054 }
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002055 /* Note that one of TA or TB is still odd. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002056 }
2057
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002058 /* By invariant (I), gcd(A,B) = 2^k * gcd(TA,TB) for some k.
2059 * At the loop exit, TA = 0, so gcd(TA,TB) = TB.
2060 * - If there was at least one loop iteration, then one of TA or TB is odd,
2061 * and TA = 0, so TB is odd and gcd(TA,TB) = gcd(A',B'). In this case,
2062 * lz = min(a,b) so gcd(A,B) = 2^lz * TB.
2063 * - If there was no loop iteration, then A was 0, and gcd(A,B) = B.
Gilles Peskine4d3fd362021-06-21 11:40:38 +02002064 * 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 +02002065 */
2066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&TB, lz));
2068 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(G, &TB));
Paul Bakker5121ce52009-01-03 21:22:43 +00002069
2070cleanup:
2071
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00002073
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002075}
2076
Paul Bakker33dc46b2014-04-30 16:11:39 +02002077/*
2078 * Fill X with size bytes of random.
Gilles Peskine22cdd0c2022-10-27 20:15:13 +02002079 * The bytes returned from the RNG are used in a specific order which
2080 * is suitable for deterministic ECDSA (see the specification of
2081 * mbedtls_mpi_random() and the implementation in mbedtls_mpi_fill_random()).
Paul Bakker33dc46b2014-04-30 16:11:39 +02002082 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002083int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
2084 int (*f_rng)(void *, unsigned char *, size_t),
2085 void *p_rng)
Paul Bakker287781a2011-03-26 13:18:49 +00002086{
Janos Follath24eed8d2019-11-22 13:21:35 +00002087 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 const size_t limbs = CHARS_TO_LIMBS(size);
Hanno Beckerda1655a2017-10-18 14:21:44 +01002089
Hanno Beckerda1655a2017-10-18 14:21:44 +01002090 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
2092 if (size == 0) {
2093 return 0;
2094 }
Paul Bakker287781a2011-03-26 13:18:49 +00002095
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 ret = mbedtls_mpi_core_fill_random(X->p, X->n, size, f_rng, p_rng);
Paul Bakker287781a2011-03-26 13:18:49 +00002097
2098cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 return ret;
Paul Bakker287781a2011-03-26 13:18:49 +00002100}
2101
Gilles Peskine449bd832023-01-11 14:50:10 +01002102int mbedtls_mpi_random(mbedtls_mpi *X,
2103 mbedtls_mpi_sint min,
2104 const mbedtls_mpi *N,
2105 int (*f_rng)(void *, unsigned char *, size_t),
2106 void *p_rng)
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002107{
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 if (min < 0) {
2109 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2110 }
2111 if (mbedtls_mpi_cmp_int(N, min) <= 0) {
2112 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2113 }
Gilles Peskine1e918f42021-03-29 22:14:51 +02002114
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002115 /* Ensure that target MPI has exactly the same number of limbs
2116 * as the upper bound, even if the upper bound has leading zeros.
Gilles Peskine6b7ce962022-12-15 15:04:33 +01002117 * This is necessary for mbedtls_mpi_core_random. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 int ret = mbedtls_mpi_resize_clear(X, N->n);
2119 if (ret != 0) {
2120 return ret;
2121 }
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 return mbedtls_mpi_core_random(X->p, min, N->p, X->n, f_rng, p_rng);
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002124}
2125
Paul Bakker5121ce52009-01-03 21:22:43 +00002126/*
2127 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2128 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002129int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
Paul Bakker5121ce52009-01-03 21:22:43 +00002130{
Janos Follath24eed8d2019-11-22 13:21:35 +00002131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002133
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 if (mbedtls_mpi_cmp_int(N, 1) <= 0) {
2135 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2136 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TU); mbedtls_mpi_init(&U1); mbedtls_mpi_init(&U2);
2139 mbedtls_mpi_init(&G); mbedtls_mpi_init(&TB); mbedtls_mpi_init(&TV);
2140 mbedtls_mpi_init(&V1); mbedtls_mpi_init(&V2);
Paul Bakker5121ce52009-01-03 21:22:43 +00002141
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, A, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002143
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 goto cleanup;
2147 }
2148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&TA, A, N));
2150 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TU, &TA));
2151 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, N));
2152 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TV, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U1, 1));
2155 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U2, 0));
2156 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V1, 0));
2157 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002158
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 do {
2160 while ((TU.p[0] & 1) == 0) {
2161 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TU, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 if ((U1.p[0] & 1) != 0 || (U2.p[0] & 1) != 0) {
2164 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&U1, &U1, &TB));
2165 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &TA));
Paul Bakker5121ce52009-01-03 21:22:43 +00002166 }
2167
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U1, 1));
2169 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 }
2171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 while ((TV.p[0] & 1) == 0) {
2173 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TV, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002174
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 if ((V1.p[0] & 1) != 0 || (V2.p[0] & 1) != 0) {
2176 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, &TB));
2177 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &TA));
Paul Bakker5121ce52009-01-03 21:22:43 +00002178 }
2179
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V1, 1));
2181 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002182 }
2183
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 if (mbedtls_mpi_cmp_mpi(&TU, &TV) >= 0) {
2185 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TU, &TU, &TV));
2186 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U1, &U1, &V1));
2187 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &V2));
2188 } else {
2189 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TV, &TV, &TU));
2190 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, &U1));
2191 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &U2));
Paul Bakker5121ce52009-01-03 21:22:43 +00002192 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 } while (mbedtls_mpi_cmp_int(&TU, 0) != 0);
2194
2195 while (mbedtls_mpi_cmp_int(&V1, 0) < 0) {
2196 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002197 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002198
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 while (mbedtls_mpi_cmp_mpi(&V1, N) >= 0) {
2200 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, N));
2201 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, &V1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002204
2205cleanup:
2206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TU); mbedtls_mpi_free(&U1); mbedtls_mpi_free(&U2);
2208 mbedtls_mpi_free(&G); mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TV);
2209 mbedtls_mpi_free(&V1); mbedtls_mpi_free(&V2);
Paul Bakker5121ce52009-01-03 21:22:43 +00002210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002212}
2213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002215
Gilles Peskineb2bc1712019-02-08 17:27:11 +01002216/* Gaps between primes, starting at 3. https://oeis.org/A001223 */
2217static const unsigned char small_prime_gaps[] = {
2218 2, 2, 4, 2, 4, 2, 4, 6,
2219 2, 6, 4, 2, 4, 6, 6, 2,
2220 6, 4, 2, 6, 4, 6, 8, 4,
2221 2, 4, 2, 4, 14, 4, 6, 2,
2222 10, 2, 6, 6, 4, 6, 6, 2,
2223 10, 2, 4, 2, 12, 12, 4, 2,
2224 4, 6, 2, 10, 6, 6, 6, 2,
2225 6, 4, 2, 10, 14, 4, 2, 4,
2226 14, 6, 10, 2, 4, 6, 8, 6,
2227 6, 4, 6, 8, 4, 8, 10, 2,
2228 10, 2, 6, 4, 6, 8, 4, 2,
2229 4, 12, 8, 4, 8, 4, 6, 12,
2230 2, 18, 6, 10, 6, 6, 2, 6,
2231 10, 6, 6, 2, 6, 6, 4, 2,
2232 12, 10, 2, 4, 6, 6, 2, 12,
2233 4, 6, 8, 10, 8, 10, 8, 6,
2234 6, 4, 8, 6, 4, 8, 4, 14,
2235 10, 12, 2, 10, 2, 4, 2, 10,
2236 14, 4, 2, 4, 14, 4, 2, 4,
2237 20, 4, 8, 10, 8, 4, 6, 6,
2238 14, 4, 6, 6, 8, 6, /*reaches 997*/
Gilles Peskine30b03782023-08-22 11:06:47 +02002239 0 /* the last entry is effectively unused */
Paul Bakker5121ce52009-01-03 21:22:43 +00002240};
2241
2242/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002243 * Small divisors test (X must be positive)
2244 *
2245 * Return values:
2246 * 0: no small factor (possible prime, more tests needed)
2247 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002249 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002251static int mpi_check_small_factors(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +00002252{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002253 int ret = 0;
2254 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255 mbedtls_mpi_uint r;
Gilles Peskineb2bc1712019-02-08 17:27:11 +01002256 unsigned p = 3; /* The first odd prime */
Paul Bakker5121ce52009-01-03 21:22:43 +00002257
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 if ((X->p[0] & 1) == 0) {
2259 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2260 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
Gilles Peskineb2bc1712019-02-08 17:27:11 +01002262 for (i = 0; i < sizeof(small_prime_gaps); p += small_prime_gaps[i], i++) {
2263 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, p));
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 if (r == 0) {
Gilles Peskineb2bc1712019-02-08 17:27:11 +01002265 if (mbedtls_mpi_cmp_int(X, p) == 0) {
2266 return 1;
2267 } else {
2268 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2269 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002271 }
2272
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002273cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 return ret;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002275}
2276
2277/*
2278 * Miller-Rabin pseudo-primality test (HAC 4.24)
2279 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002280static int mpi_miller_rabin(const mbedtls_mpi *X, size_t rounds,
2281 int (*f_rng)(void *, unsigned char *, size_t),
2282 void *p_rng)
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002283{
Pascal Junodb99183d2015-03-11 16:49:45 +01002284 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002285 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002287
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 mbedtls_mpi_init(&W); mbedtls_mpi_init(&R);
2289 mbedtls_mpi_init(&T); mbedtls_mpi_init(&A);
2290 mbedtls_mpi_init(&RR);
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002291
Paul Bakker5121ce52009-01-03 21:22:43 +00002292 /*
2293 * W = |X| - 1
2294 * R = W >> lsb( W )
2295 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&W, X, 1));
2297 s = mbedtls_mpi_lsb(&W);
2298 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R, &W));
2299 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&R, s));
Paul Bakker5121ce52009-01-03 21:22:43 +00002300
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 for (i = 0; i < rounds; i++) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002302 /*
2303 * pick a random A, 1 < A < |X| - 1
2304 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002305 count = 0;
2306 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&A, X->n * ciL, f_rng, p_rng));
Pascal Junodb99183d2015-03-11 16:49:45 +01002308
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 j = mbedtls_mpi_bitlen(&A);
2310 k = mbedtls_mpi_bitlen(&W);
Pascal Junodb99183d2015-03-11 16:49:45 +01002311 if (j > k) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 A.p[A.n - 1] &= ((mbedtls_mpi_uint) 1 << (k - (A.n - 1) * biL - 1)) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002313 }
2314
2315 if (count++ > 30) {
Jens Wiklanderf08aa3e2019-01-17 13:30:57 +01002316 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2317 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002318 }
2319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 } while (mbedtls_mpi_cmp_mpi(&A, &W) >= 0 ||
2321 mbedtls_mpi_cmp_int(&A, 1) <= 0);
Paul Bakker5121ce52009-01-03 21:22:43 +00002322
2323 /*
2324 * A = A^R mod |X|
2325 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&A, &A, &R, X, &RR));
Paul Bakker5121ce52009-01-03 21:22:43 +00002327
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 if (mbedtls_mpi_cmp_mpi(&A, &W) == 0 ||
2329 mbedtls_mpi_cmp_int(&A, 1) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002330 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002332
2333 j = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 while (j < s && mbedtls_mpi_cmp_mpi(&A, &W) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002335 /*
2336 * A = A * A mod |X|
2337 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &A, &A));
2339 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&A, &T, X));
Paul Bakker5121ce52009-01-03 21:22:43 +00002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 if (mbedtls_mpi_cmp_int(&A, 1) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002342 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002344
2345 j++;
2346 }
2347
2348 /*
2349 * not prime if A != |X| - 1 or A == 1
2350 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 if (mbedtls_mpi_cmp_mpi(&A, &W) != 0 ||
2352 mbedtls_mpi_cmp_int(&A, 1) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002354 break;
2355 }
2356 }
2357
2358cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 mbedtls_mpi_free(&W); mbedtls_mpi_free(&R);
2360 mbedtls_mpi_free(&T); mbedtls_mpi_free(&A);
2361 mbedtls_mpi_free(&RR);
Paul Bakker5121ce52009-01-03 21:22:43 +00002362
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002364}
2365
2366/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002367 * Pseudo-primality test: small factors, then Miller-Rabin
2368 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002369int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
2370 int (*f_rng)(void *, unsigned char *, size_t),
2371 void *p_rng)
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002372{
Janos Follath24eed8d2019-11-22 13:21:35 +00002373 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374 mbedtls_mpi XX;
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002375
2376 XX.s = 1;
2377 XX.n = X->n;
2378 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 if (mbedtls_mpi_cmp_int(&XX, 0) == 0 ||
2381 mbedtls_mpi_cmp_int(&XX, 1) == 0) {
2382 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002383 }
2384
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 if (mbedtls_mpi_cmp_int(&XX, 2) == 0) {
2386 return 0;
2387 }
2388
2389 if ((ret = mpi_check_small_factors(&XX)) != 0) {
2390 if (ret == 1) {
2391 return 0;
2392 }
2393
2394 return ret;
2395 }
2396
2397 return mpi_miller_rabin(&XX, rounds, f_rng, p_rng);
Janos Follathf301d232018-08-14 13:34:01 +01002398}
2399
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002400/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002402 *
Janos Follathf301d232018-08-14 13:34:01 +01002403 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2404 * be either 1024 bits or 1536 bits long, and flags must contain
2405 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002406 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002407int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
2408 int (*f_rng)(void *, unsigned char *, size_t),
2409 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00002410{
Jethro Beekman66689272018-02-14 19:24:10 -08002411#ifdef MBEDTLS_HAVE_INT64
2412// ceil(2^63.5)
2413#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2414#else
2415// ceil(2^31.5)
2416#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2417#endif
2418 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002419 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002420 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421 mbedtls_mpi_uint r;
2422 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 if (nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS) {
2425 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2426 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002427
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 mbedtls_mpi_init(&Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00002429
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 n = BITS_TO_LIMBS(nbits);
Paul Bakker5121ce52009-01-03 21:22:43 +00002431
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR) == 0) {
Janos Follathda31fa12018-09-03 14:45:23 +01002433 /*
2434 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2435 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 rounds = ((nbits >= 1300) ? 2 : (nbits >= 850) ? 3 :
2437 (nbits >= 650) ? 4 : (nbits >= 350) ? 8 :
2438 (nbits >= 250) ? 12 : (nbits >= 150) ? 18 : 27);
2439 } else {
Janos Follathda31fa12018-09-03 14:45:23 +01002440 /*
2441 * 2^-100 error probability, number of rounds computed based on HAC,
2442 * fact 4.48
2443 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 rounds = ((nbits >= 1450) ? 4 : (nbits >= 1150) ? 5 :
2445 (nbits >= 1000) ? 6 : (nbits >= 850) ? 7 :
2446 (nbits >= 750) ? 8 : (nbits >= 500) ? 13 :
2447 (nbits >= 250) ? 28 : (nbits >= 150) ? 40 : 51);
Janos Follathda31fa12018-09-03 14:45:23 +01002448 }
2449
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 while (1) {
2451 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(X, n * ciL, f_rng, p_rng));
Jethro Beekman66689272018-02-14 19:24:10 -08002452 /* 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 +01002453 if (X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2) {
2454 continue;
2455 }
Jethro Beekman66689272018-02-14 19:24:10 -08002456
2457 k = n * biL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 if (k > nbits) {
2459 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(X, k - nbits));
2460 }
Jethro Beekman66689272018-02-14 19:24:10 -08002461 X->p[0] |= 1;
2462
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH) == 0) {
2464 ret = mbedtls_mpi_is_prime_ext(X, rounds, f_rng, p_rng);
Jethro Beekman66689272018-02-14 19:24:10 -08002465
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002467 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 }
2469 } else {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002470 /*
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01002471 * A necessary condition for Y and X = 2Y + 1 to be prime
Jethro Beekman66689272018-02-14 19:24:10 -08002472 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2473 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002474 */
Jethro Beekman66689272018-02-14 19:24:10 -08002475
2476 X->p[0] |= 2;
2477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, 3));
2479 if (r == 0) {
2480 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 8));
2481 } else if (r == 1) {
2482 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 4));
2483 }
Jethro Beekman66689272018-02-14 19:24:10 -08002484
2485 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, X));
2487 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, 1));
Jethro Beekman66689272018-02-14 19:24:10 -08002488
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 while (1) {
Jethro Beekman66689272018-02-14 19:24:10 -08002490 /*
2491 * First, check small factors for X and Y
2492 * before doing Miller-Rabin on any of them
2493 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 if ((ret = mpi_check_small_factors(X)) == 0 &&
2495 (ret = mpi_check_small_factors(&Y)) == 0 &&
2496 (ret = mpi_miller_rabin(X, rounds, f_rng, p_rng))
2497 == 0 &&
2498 (ret = mpi_miller_rabin(&Y, rounds, f_rng, p_rng))
2499 == 0) {
Jethro Beekman66689272018-02-14 19:24:10 -08002500 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 }
Jethro Beekman66689272018-02-14 19:24:10 -08002502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Jethro Beekman66689272018-02-14 19:24:10 -08002504 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 }
Jethro Beekman66689272018-02-14 19:24:10 -08002506
2507 /*
2508 * Next candidates. We want to preserve Y = (X-1) / 2 and
2509 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2510 * so up Y by 6 and X by 12.
2511 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 12));
2513 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&Y, &Y, 6));
Paul Bakker5121ce52009-01-03 21:22:43 +00002514 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002515 }
2516 }
2517
2518cleanup:
2519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 mbedtls_mpi_free(&Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002523}
2524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002528
Paul Bakker23986e52011-04-24 08:57:21 +00002529#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002530
2531static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2532{
2533 { 693, 609, 21 },
2534 { 1764, 868, 28 },
2535 { 768454923, 542167814, 1 }
2536};
2537
Paul Bakker5121ce52009-01-03 21:22:43 +00002538/*
2539 * Checkup routine
2540 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002541int mbedtls_mpi_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002542{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002543 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 mbedtls_mpi_init(&A); mbedtls_mpi_init(&E); mbedtls_mpi_init(&N); mbedtls_mpi_init(&X);
2547 mbedtls_mpi_init(&Y); mbedtls_mpi_init(&U); mbedtls_mpi_init(&V);
Paul Bakker5121ce52009-01-03 21:22:43 +00002548
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&A, 16,
2550 "EFE021C2645FD1DC586E69184AF4A31E" \
2551 "D5F53E93B5F123FA41680867BA110131" \
2552 "944FE7952E2517337780CB0DB80E61AA" \
2553 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&E, 16,
2556 "B2E7EFD37075B9F03FF989C7C5051C20" \
2557 "34D2A323810251127E7BF8625A4F49A5" \
2558 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
2559 "5B5C25763222FEFCCFC38B832366C29E"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002560
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&N, 16,
2562 "0066A198186C18C10B2F5ED9B522752A" \
2563 "9830B69916E535C8F047518A889A43A5" \
2564 "94B6BED27A168D31D4A52F88925AA8F5"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002565
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&X, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2569 "602AB7ECA597A3D6B56FF9829A5E8B85" \
2570 "9E857EA95A03512E2BAE7391688D264A" \
2571 "A5663B0341DB9CCFD2C4C5F421FEC814" \
2572 "8001B72E848A38CAE1C65F78E56ABDEF" \
2573 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
2574 "ECF677152EF804370C1A305CAF3B5BF1" \
2575 "30879B56C61DE584A0F53A2447A51E"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002576
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 if (verbose != 0) {
2578 mbedtls_printf(" MPI test #1 (mul_mpi): ");
2579 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002580
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2582 if (verbose != 0) {
2583 mbedtls_printf("failed\n");
2584 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002585
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002586 ret = 1;
2587 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 }
2589
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 if (verbose != 0) {
2591 mbedtls_printf("passed\n");
2592 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002593
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&X, &Y, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2597 "256567336059E52CAE22925474705F39A94"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002598
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&V, 16,
2600 "6613F26162223DF488E9CD48CC132C7A" \
2601 "0AC93C701B001B092E4E5B9F73BCD27B" \
2602 "9EE50D0657C77F374E903CDFA4C642"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002603
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 if (verbose != 0) {
2605 mbedtls_printf(" MPI test #2 (div_mpi): ");
2606 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002607
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0 ||
2609 mbedtls_mpi_cmp_mpi(&Y, &V) != 0) {
2610 if (verbose != 0) {
2611 mbedtls_printf("failed\n");
2612 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002613
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002614 ret = 1;
2615 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002616 }
2617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 if (verbose != 0) {
2619 mbedtls_printf("passed\n");
2620 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002621
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&X, &A, &E, &N, NULL));
Paul Bakker5121ce52009-01-03 21:22:43 +00002623
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2625 "36E139AEA55215609D2816998ED020BB" \
2626 "BD96C37890F65171D948E9BC7CBAA4D9" \
2627 "325D24D6A3C12710F10A09FA08AB87"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 if (verbose != 0) {
2630 mbedtls_printf(" MPI test #3 (exp_mod): ");
2631 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002632
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2634 if (verbose != 0) {
2635 mbedtls_printf("failed\n");
2636 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002637
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002638 ret = 1;
2639 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002640 }
2641
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 if (verbose != 0) {
2643 mbedtls_printf("passed\n");
2644 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&X, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002647
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2649 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
2650 "C3DBA76456363A10869622EAC2DD84EC" \
2651 "C5B8A74DAC4D09E03B5E0BE779F2DF61"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 if (verbose != 0) {
2654 mbedtls_printf(" MPI test #4 (inv_mod): ");
2655 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002656
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2658 if (verbose != 0) {
2659 mbedtls_printf("failed\n");
2660 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002661
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002662 ret = 1;
2663 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 }
2665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 if (verbose != 0) {
2667 mbedtls_printf("passed\n");
2668 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002669
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 if (verbose != 0) {
2671 mbedtls_printf(" MPI test #5 (simple gcd): ");
2672 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 for (i = 0; i < GCD_PAIR_COUNT; i++) {
2675 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&X, gcd_pairs[i][0]));
2676 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Y, gcd_pairs[i][1]));
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002677
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&A, &X, &Y));
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002679
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 if (mbedtls_mpi_cmp_int(&A, gcd_pairs[i][2]) != 0) {
2681 if (verbose != 0) {
2682 mbedtls_printf("failed at %d\n", i);
2683 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002684
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002685 ret = 1;
2686 goto cleanup;
2687 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002688 }
2689
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 if (verbose != 0) {
2691 mbedtls_printf("passed\n");
2692 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002693
Paul Bakker5121ce52009-01-03 21:22:43 +00002694cleanup:
2695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 if (ret != 0 && verbose != 0) {
2697 mbedtls_printf("Unexpected error, return code = %08X\n", (unsigned int) ret);
2698 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 mbedtls_mpi_free(&A); mbedtls_mpi_free(&E); mbedtls_mpi_free(&N); mbedtls_mpi_free(&X);
2701 mbedtls_mpi_free(&Y); mbedtls_mpi_free(&U); mbedtls_mpi_free(&V);
Paul Bakker5121ce52009-01-03 21:22:43 +00002702
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 if (verbose != 0) {
2704 mbedtls_printf("\n");
2705 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002706
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002708}
2709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712#endif /* MBEDTLS_BIGNUM_C */