blob: de652c30900d4cf90b4d5a989a87ea59e31f6a40 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
Simon Butcher15b15d12015-11-26 19:35:03 +000019
Paul Bakker5121ce52009-01-03 21:22:43 +000020/*
Simon Butcher15b15d12015-11-26 19:35:03 +000021 * The following sources were referenced in the design of this Multi-precision
22 * Integer library:
Paul Bakker5121ce52009-01-03 21:22:43 +000023 *
Simon Butcher15b15d12015-11-26 19:35:03 +000024 * [1] Handbook of Applied Cryptography - 1997
25 * Menezes, van Oorschot and Vanstone
26 *
27 * [2] Multi-Precision Math
28 * Tom St Denis
29 * https://github.com/libtom/libtommath/blob/develop/tommath.pdf
30 *
31 * [3] GNU Multi-Precision Arithmetic Library
32 * https://gmplib.org/manual/index.html
33 *
Simon Butcherf5ba0452015-12-27 23:01:55 +000034 */
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Gilles Peskinedb09ef62020-06-03 01:43:33 +020036#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/bignum.h"
Janos Follath4614b9a2022-07-21 15:34:47 +010041#include "bignum_core.h"
Chris Jones4c5819c2021-03-03 17:45:34 +000042#include "bn_mul.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050043#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000044#include "mbedtls/error.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020045#include "constant_time_internal.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000046
Dave Rodgman351c71b2021-12-06 17:50:53 +000047#include <limits.h>
Rich Evans00ab4702015-02-06 13:43:58 +000048#include <string.h>
49
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020051
Gilles Peskine449bd832023-01-11 14:50:10 +010052#define MPI_VALIDATE_RET(cond) \
53 MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA)
54#define MPI_VALIDATE(cond) \
55 MBEDTLS_INTERNAL_VALIDATE(cond)
Gabor Mezei66669142022-08-03 12:52:26 +020056
Dave Rodgman7d4f0192023-05-09 14:01:05 +010057/*
58 * Compare signed values in constant time
59 */
60int mbedtls_mpi_lt_mpi_ct(const mbedtls_mpi *X,
61 const mbedtls_mpi *Y,
62 unsigned *ret)
63{
Dave Rodgman1f39f032023-08-01 09:19:16 +010064 mbedtls_ct_condition_t different_sign, X_is_negative, Y_is_negative, result;
Dave Rodgman7d4f0192023-05-09 14:01:05 +010065
66 MPI_VALIDATE_RET(X != NULL);
67 MPI_VALIDATE_RET(Y != NULL);
68 MPI_VALIDATE_RET(ret != NULL);
69
70 if (X->n != Y->n) {
71 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
72 }
73
74 /*
75 * Set sign_N to 1 if N >= 0, 0 if N < 0.
76 * We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0.
77 */
Dave Rodgman1a7a5622023-05-17 13:47:56 +010078 X_is_negative = mbedtls_ct_bool((X->s & 2) >> 1);
79 Y_is_negative = mbedtls_ct_bool((Y->s & 2) >> 1);
Dave Rodgman7d4f0192023-05-09 14:01:05 +010080
81 /*
82 * If the signs are different, then the positive operand is the bigger.
83 * That is if X is negative (X_is_negative == 1), then X < Y is true and it
84 * is false if X is positive (X_is_negative == 0).
85 */
Dave Rodgman1f39f032023-08-01 09:19:16 +010086 different_sign = mbedtls_ct_bool_xor(X_is_negative, Y_is_negative); // non-zero if different sign
87 result = mbedtls_ct_bool_and(different_sign, X_is_negative);
Dave Rodgman7d4f0192023-05-09 14:01:05 +010088
Dave Rodgman32d72602023-07-31 12:28:05 +010089 /*
90 * Assuming signs are the same, compare X and Y. We switch the comparison
Dave Rodgman1a7a5622023-05-17 13:47:56 +010091 * order if they are negative so that we get the right result, regardles of
92 * sign.
Dave Rodgman7d4f0192023-05-09 14:01:05 +010093 */
Dave Rodgman7d4f0192023-05-09 14:01:05 +010094
Dave Rodgman32d72602023-07-31 12:28:05 +010095 /* This array is used to conditionally swap the pointers in const time */
Dave Rodgman1a7a5622023-05-17 13:47:56 +010096 void * const p[2] = { X->p, Y->p };
Dave Rodgman2c764842023-05-18 13:28:21 +010097 size_t i = mbedtls_ct_size_if0(X_is_negative, 1);
98 mbedtls_ct_condition_t lt = mbedtls_mpi_core_lt_ct(p[i], p[i ^ 1], X->n);
Dave Rodgman7d4f0192023-05-09 14:01:05 +010099
Dave Rodgman32d72602023-07-31 12:28:05 +0100100 /*
Dave Rodgman1f39f032023-08-01 09:19:16 +0100101 * Store in result iff the signs are the same (i.e., iff different_sign == false). If
Dave Rodgman32d72602023-07-31 12:28:05 +0100102 * the signs differ, result has already been set, so we don't change it.
103 */
Dave Rodgman1f39f032023-08-01 09:19:16 +0100104 result = mbedtls_ct_bool_or(result,
105 mbedtls_ct_bool_and(mbedtls_ct_bool_not(different_sign), lt));
Dave Rodgman1a7a5622023-05-17 13:47:56 +0100106
107 *ret = mbedtls_ct_uint_if0(result, 1);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100108
109 return 0;
110}
111
112/*
113 * Conditionally assign X = Y, without leaking information
114 * about whether the assignment was made or not.
115 * (Leaking information about the respective sizes of X and Y is ok however.)
116 */
117#if defined(_MSC_VER) && defined(_M_ARM64) && (_MSC_FULL_VER < 193131103)
118/*
119 * MSVC miscompiles this function if it's inlined prior to Visual Studio 2022 version 17.1. See:
120 * https://developercommunity.visualstudio.com/t/c-compiler-miscompiles-part-of-mbedtls-library-on/1646989
121 */
122__declspec(noinline)
123#endif
124int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X,
125 const mbedtls_mpi *Y,
126 unsigned char assign)
127{
128 int ret = 0;
129 MPI_VALIDATE_RET(X != NULL);
130 MPI_VALIDATE_RET(Y != NULL);
131
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100132 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, Y->n));
133
Dave Rodgman589ccb82023-05-17 13:55:01 +0100134 mbedtls_ct_condition_t do_assign = mbedtls_ct_bool(assign);
135
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100136 X->s = (int) mbedtls_ct_uint_if(do_assign, Y->s, X->s);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100137
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100138 mbedtls_mpi_core_cond_assign(X->p, Y->p, Y->n, do_assign);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100139
Dave Rodgman589ccb82023-05-17 13:55:01 +0100140 mbedtls_ct_condition_t do_not_assign = mbedtls_ct_bool_not(do_assign);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100141 for (size_t i = Y->n; i < X->n; i++) {
Dave Rodgman589ccb82023-05-17 13:55:01 +0100142 X->p[i] = mbedtls_ct_mpi_uint_if0(do_not_assign, X->p[i]);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100143 }
144
145cleanup:
146 return ret;
147}
148
149/*
150 * Conditionally swap X and Y, without leaking information
151 * about whether the swap was made or not.
152 * Here it is not ok to simply swap the pointers, which would lead to
153 * different memory access patterns when X and Y are used afterwards.
154 */
155int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X,
156 mbedtls_mpi *Y,
157 unsigned char swap)
158{
159 int ret = 0;
160 int s;
161 MPI_VALIDATE_RET(X != NULL);
162 MPI_VALIDATE_RET(Y != NULL);
163
164 if (X == Y) {
165 return 0;
166 }
167
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100168 mbedtls_ct_condition_t do_swap = mbedtls_ct_bool(swap);
169
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100170 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, Y->n));
171 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Y, X->n));
172
173 s = X->s;
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100174 X->s = (int) mbedtls_ct_uint_if(do_swap, Y->s, X->s);
175 Y->s = (int) mbedtls_ct_uint_if(do_swap, s, Y->s);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100176
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100177 mbedtls_mpi_core_cond_swap(X->p, Y->p, X->n, do_swap);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100178
179cleanup:
180 return ret;
181}
182
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500183/* Implementation that should never be optimized out by the compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +0100184static void mbedtls_mpi_zeroize(mbedtls_mpi_uint *v, size_t n)
Andres Amaya Garcia6698d2f2018-04-24 08:39:07 -0500185{
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 mbedtls_platform_zeroize(v, ciL * n);
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500187}
188
Paul Bakker5121ce52009-01-03 21:22:43 +0000189/*
Paul Bakker6c591fa2011-05-05 11:49:20 +0000190 * Initialize one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192void mbedtls_mpi_init(mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000193{
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 MPI_VALIDATE(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000195
Paul Bakker6c591fa2011-05-05 11:49:20 +0000196 X->s = 1;
197 X->n = 0;
198 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000199}
200
201/*
Paul Bakker6c591fa2011-05-05 11:49:20 +0000202 * Unallocate one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100204void mbedtls_mpi_free(mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000205{
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 if (X == NULL) {
Paul Bakker6c591fa2011-05-05 11:49:20 +0000207 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 if (X->p != NULL) {
211 mbedtls_mpi_zeroize(X->p, X->n);
212 mbedtls_free(X->p);
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 }
214
Paul Bakker6c591fa2011-05-05 11:49:20 +0000215 X->s = 1;
216 X->n = 0;
217 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000218}
219
220/*
221 * Enlarge to the specified number of limbs
222 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100223int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs)
Paul Bakker5121ce52009-01-03 21:22:43 +0000224{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 mbedtls_mpi_uint *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
229 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
230 }
Paul Bakkerf9688572011-05-05 10:00:45 +0000231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 if (X->n < nblimbs) {
233 if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(nblimbs, ciL)) == NULL) {
234 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
235 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 if (X->p != NULL) {
238 memcpy(p, X->p, X->n * ciL);
239 mbedtls_mpi_zeroize(X->p, X->n);
240 mbedtls_free(X->p);
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 }
242
243 X->n = nblimbs;
244 X->p = p;
245 }
246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000248}
249
250/*
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100251 * Resize down as much as possible,
252 * while keeping at least the specified number of limbs
253 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100254int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs)
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100255{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_mpi_uint *p;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100257 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 MPI_VALIDATE_RET(X != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
261 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
262 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100263
Gilles Peskinee2f563e2020-01-20 21:17:43 +0100264 /* Actually resize up if there are currently fewer than nblimbs limbs. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 if (X->n <= nblimbs) {
266 return mbedtls_mpi_grow(X, nblimbs);
267 }
Gilles Peskine322752b2020-01-21 13:59:51 +0100268 /* After this point, then X->n > nblimbs and in particular X->n > 0. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 for (i = X->n - 1; i > 0; i--) {
271 if (X->p[i] != 0) {
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100272 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 }
274 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100275 i++;
276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 if (i < nblimbs) {
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100278 i = nblimbs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(i, ciL)) == NULL) {
282 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
283 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if (X->p != NULL) {
286 memcpy(p, X->p, i * ciL);
287 mbedtls_mpi_zeroize(X->p, X->n);
288 mbedtls_free(X->p);
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100289 }
290
291 X->n = i;
292 X->p = p;
293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 return 0;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100295}
296
Gilles Peskineed32b572021-06-02 22:17:52 +0200297/* Resize X to have exactly n limbs and set it to 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100298static int mbedtls_mpi_resize_clear(mbedtls_mpi *X, size_t limbs)
Gilles Peskineed32b572021-06-02 22:17:52 +0200299{
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (limbs == 0) {
301 mbedtls_mpi_free(X);
302 return 0;
303 } else if (X->n == limbs) {
304 memset(X->p, 0, limbs * ciL);
Gilles Peskineed32b572021-06-02 22:17:52 +0200305 X->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 return 0;
307 } else {
308 mbedtls_mpi_free(X);
309 return mbedtls_mpi_grow(X, limbs);
Gilles Peskineed32b572021-06-02 22:17:52 +0200310 }
311}
312
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100313/*
Gilles Peskine3da1a8f2021-06-08 23:17:42 +0200314 * Copy the contents of Y into X.
315 *
316 * This function is not constant-time. Leading zeros in Y may be removed.
317 *
318 * Ensure that X does not shrink. This is not guaranteed by the public API,
319 * but some code in the bignum module relies on this property, for example
320 * in mbedtls_mpi_exp_mod().
Paul Bakker5121ce52009-01-03 21:22:43 +0000321 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100322int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000323{
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100324 int ret = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000325 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 MPI_VALIDATE_RET(X != NULL);
327 MPI_VALIDATE_RET(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 if (X == Y) {
330 return 0;
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200331 }
332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if (Y->n == 0) {
334 if (X->n != 0) {
335 X->s = 1;
336 memset(X->p, 0, X->n * ciL);
337 }
338 return 0;
339 }
340
341 for (i = Y->n - 1; i > 0; i--) {
342 if (Y->p[i] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000343 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 }
345 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000346 i++;
347
348 X->s = Y->s;
349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (X->n < i) {
351 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i));
352 } else {
353 memset(X->p + i, 0, (X->n - i) * ciL);
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100354 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 memcpy(X->p, Y->p, i * ciL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000357
358cleanup:
359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000361}
362
363/*
364 * Swap the contents of X and Y
365 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100366void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000367{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 MPI_VALIDATE(X != NULL);
370 MPI_VALIDATE(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 memcpy(&T, X, sizeof(mbedtls_mpi));
373 memcpy(X, Y, sizeof(mbedtls_mpi));
374 memcpy(Y, &T, sizeof(mbedtls_mpi));
Paul Bakker5121ce52009-01-03 21:22:43 +0000375}
376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377static inline mbedtls_mpi_uint mpi_sint_abs(mbedtls_mpi_sint z)
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100378{
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 if (z >= 0) {
380 return z;
381 }
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100382 /* Take care to handle the most negative value (-2^(biL-1)) correctly.
383 * A naive -z would have undefined behavior.
384 * Write this in a way that makes popular compilers happy (GCC, Clang,
385 * MSVC). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 return (mbedtls_mpi_uint) 0 - (mbedtls_mpi_uint) z;
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100387}
388
Paul Bakker5121ce52009-01-03 21:22:43 +0000389/*
390 * Set value from integer
391 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100392int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z)
Paul Bakker5121ce52009-01-03 21:22:43 +0000393{
Janos Follath24eed8d2019-11-22 13:21:35 +0000394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, 1));
398 memset(X->p, 0, X->n * ciL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 X->p[0] = mpi_sint_abs(z);
401 X->s = (z < 0) ? -1 : 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000402
403cleanup:
404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000406}
407
408/*
Paul Bakker2f5947e2011-05-18 15:47:11 +0000409 * Get a specific bit
410 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100411int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
Paul Bakker2f5947e2011-05-18 15:47:11 +0000412{
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 MPI_VALIDATE_RET(X != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if (X->n * biL <= pos) {
416 return 0;
417 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 return (X->p[pos / biL] >> (pos % biL)) & 0x01;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000420}
421
422/*
423 * Set a bit to a specific value of 0 or 1
424 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100425int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val)
Paul Bakker2f5947e2011-05-18 15:47:11 +0000426{
427 int ret = 0;
428 size_t off = pos / biL;
429 size_t idx = pos % biL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 MPI_VALIDATE_RET(X != NULL);
Paul Bakker2f5947e2011-05-18 15:47:11 +0000431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if (val != 0 && val != 1) {
433 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000434 }
435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 if (X->n * biL <= pos) {
437 if (val == 0) {
438 return 0;
439 }
440
441 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, off + 1));
442 }
443
444 X->p[off] &= ~((mbedtls_mpi_uint) 0x01 << idx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 X->p[off] |= (mbedtls_mpi_uint) val << idx;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000446
447cleanup:
Paul Bakker9af723c2014-05-01 13:03:14 +0200448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 return ret;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000450}
451
452/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200453 * Return the number of less significant zero-bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100455size_t mbedtls_mpi_lsb(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000456{
Paul Bakker23986e52011-04-24 08:57:21 +0000457 size_t i, j, count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 MBEDTLS_INTERNAL_VALIDATE_RET(X != NULL, 0);
Paul Bakker5121ce52009-01-03 21:22:43 +0000459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 for (i = 0; i < X->n; i++) {
461 for (j = 0; j < biL; j++, count++) {
462 if (((X->p[i] >> j) & 1) != 0) {
463 return count;
464 }
465 }
466 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000469}
470
471/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200472 * Return the number of bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000473 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100474size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000475{
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 return mbedtls_mpi_core_bitlen(X->p, X->n);
Paul Bakker5121ce52009-01-03 21:22:43 +0000477}
478
479/*
480 * Return the total size in bytes
481 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100482size_t mbedtls_mpi_size(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000483{
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 return (mbedtls_mpi_bitlen(X) + 7) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000485}
486
487/*
488 * Convert an ASCII character to digit value
489 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100490static int mpi_get_digit(mbedtls_mpi_uint *d, int radix, char c)
Paul Bakker5121ce52009-01-03 21:22:43 +0000491{
492 *d = 255;
493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (c >= 0x30 && c <= 0x39) {
495 *d = c - 0x30;
496 }
497 if (c >= 0x41 && c <= 0x46) {
498 *d = c - 0x37;
499 }
500 if (c >= 0x61 && c <= 0x66) {
501 *d = c - 0x57;
502 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (*d >= (mbedtls_mpi_uint) radix) {
505 return MBEDTLS_ERR_MPI_INVALID_CHARACTER;
506 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000509}
510
511/*
512 * Import from an ASCII string
513 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100514int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s)
Paul Bakker5121ce52009-01-03 21:22:43 +0000515{
Janos Follath24eed8d2019-11-22 13:21:35 +0000516 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000517 size_t i, j, slen, n;
Gilles Peskine80f56732021-04-03 18:26:13 +0200518 int sign = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_mpi_uint d;
520 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 MPI_VALIDATE_RET(X != NULL);
522 MPI_VALIDATE_RET(s != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (radix < 2 || radix > 16) {
525 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskine7cba8592021-06-08 18:32:34 +0200526 }
527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 mbedtls_mpi_init(&T);
529
530 if (s[0] == 0) {
531 mbedtls_mpi_free(X);
532 return 0;
533 }
534
535 if (s[0] == '-') {
Gilles Peskine80f56732021-04-03 18:26:13 +0200536 ++s;
537 sign = -1;
538 }
539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 slen = strlen(s);
Paul Bakkerff60ee62010-03-16 21:09:09 +0000541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if (radix == 16) {
Dave Rodgman68ef1d62023-05-18 20:49:03 +0100543 if (slen > SIZE_MAX >> 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 n = BITS_TO_LIMBS(slen << 2);
548
549 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n));
550 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
551
552 for (i = slen, j = 0; i > 0; i--, j++) {
553 MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i - 1]));
554 X->p[j / (2 * ciL)] |= d << ((j % (2 * ciL)) << 2);
555 }
556 } else {
557 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
558
559 for (i = 0; i < slen; i++) {
560 MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i]));
561 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T, X, radix));
562 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, &T, d));
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 }
564 }
565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 if (sign < 0 && mbedtls_mpi_bitlen(X) != 0) {
Gilles Peskine80f56732021-04-03 18:26:13 +0200567 X->s = -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 }
Gilles Peskine80f56732021-04-03 18:26:13 +0200569
Paul Bakker5121ce52009-01-03 21:22:43 +0000570cleanup:
571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000575}
576
577/*
Ron Eldora16fa292018-11-20 14:07:01 +0200578 * Helper to write the digits high-order first.
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100580static int mpi_write_hlp(mbedtls_mpi *X, int radix,
581 char **p, const size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000582{
Janos Follath24eed8d2019-11-22 13:21:35 +0000583 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_mpi_uint r;
Ron Eldora16fa292018-11-20 14:07:01 +0200585 size_t length = 0;
586 char *p_end = *p + buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 do {
589 if (length >= buflen) {
590 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
Ron Eldora16fa292018-11-20 14:07:01 +0200591 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, radix));
594 MBEDTLS_MPI_CHK(mbedtls_mpi_div_int(X, NULL, X, radix));
Ron Eldora16fa292018-11-20 14:07:01 +0200595 /*
596 * Write the residue in the current position, as an ASCII character.
597 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 if (r < 0xA) {
599 *(--p_end) = (char) ('0' + r);
600 } else {
601 *(--p_end) = (char) ('A' + (r - 0xA));
602 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000603
Ron Eldora16fa292018-11-20 14:07:01 +0200604 length++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 } while (mbedtls_mpi_cmp_int(X, 0) != 0);
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 memmove(*p, p_end, length);
Ron Eldora16fa292018-11-20 14:07:01 +0200608 *p += length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000609
610cleanup:
611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000613}
614
615/*
616 * Export into an ASCII string
617 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100618int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
619 char *buf, size_t buflen, size_t *olen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000620{
Paul Bakker23986e52011-04-24 08:57:21 +0000621 int ret = 0;
622 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 MPI_VALIDATE_RET(X != NULL);
626 MPI_VALIDATE_RET(olen != NULL);
627 MPI_VALIDATE_RET(buflen == 0 || buf != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 if (radix < 2 || radix > 16) {
630 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
631 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 n = mbedtls_mpi_bitlen(X); /* Number of bits necessary to present `n`. */
634 if (radix >= 4) {
635 n >>= 1; /* Number of 4-adic digits necessary to present
Hanno Becker23cfea02019-02-04 09:45:07 +0000636 * `n`. If radix > 4, this might be a strict
637 * overapproximation of the number of
638 * radix-adic digits needed to present `n`. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 }
640 if (radix >= 16) {
641 n >>= 1; /* Number of hexadecimal digits necessary to
Hanno Becker23cfea02019-02-04 09:45:07 +0000642 * present `n`. */
643
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 }
Janos Follath80470622019-03-06 13:43:02 +0000645 n += 1; /* Terminating null byte */
Hanno Becker23cfea02019-02-04 09:45:07 +0000646 n += 1; /* Compensate for the divisions above, which round down `n`
647 * in case it's not even. */
648 n += 1; /* Potential '-'-sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 n += (n & 1); /* Make n even to have enough space for hexadecimal writing,
Hanno Becker23cfea02019-02-04 09:45:07 +0000650 * which always uses an even number of hex-digits. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (buflen < n) {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100653 *olen = n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 }
656
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100657 p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000659
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 if (X->s == -1) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 *p++ = '-';
Hanno Beckerc983c812019-02-01 16:41:30 +0000662 buflen--;
663 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000664
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (radix == 16) {
Paul Bakker23986e52011-04-24 08:57:21 +0000666 int c;
667 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 for (i = X->n, k = 0; i > 0; i--) {
670 for (j = ciL; j > 0; j--) {
671 c = (X->p[i - 1] >> ((j - 1) << 3)) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000672
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 if (c == 0 && k == 0 && (i + j) != 2) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000674 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000676
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000677 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000678 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000679 k = 1;
680 }
681 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 } else {
683 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T, X));
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (T.s == -1) {
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000686 T.s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 MBEDTLS_MPI_CHK(mpi_write_hlp(&T, radix, &p, buflen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000690 }
691
692 *p++ = '\0';
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100693 *olen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000694
695cleanup:
696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000700}
701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000703/*
704 * Read X from an opened file
705 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100706int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin)
Paul Bakker5121ce52009-01-03 21:22:43 +0000707{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_mpi_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000709 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000710 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000711 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000712 * Buffer should have space for (short) label and decimal formatted MPI,
713 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000714 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 MPI_VALIDATE_RET(X != NULL);
718 MPI_VALIDATE_RET(fin != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if (radix < 2 || radix > 16) {
721 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
722 }
Hanno Becker73d7d792018-12-11 10:35:51 +0000723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 memset(s, 0, sizeof(s));
725 if (fgets(s, sizeof(s) - 1, fin) == NULL) {
726 return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
727 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 slen = strlen(s);
730 if (slen == sizeof(s) - 2) {
731 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
732 }
Paul Bakkercb37aa52011-11-30 16:00:20 +0000733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 if (slen > 0 && s[slen - 1] == '\n') {
735 slen--; s[slen] = '\0';
736 }
737 if (slen > 0 && s[slen - 1] == '\r') {
738 slen--; s[slen] = '\0';
739 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000740
741 p = s + slen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 while (p-- > s) {
743 if (mpi_get_digit(&d, radix, *p) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000744 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 }
746 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 return mbedtls_mpi_read_string(X, radix, p + 1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000749}
750
751/*
752 * Write X into an opened file (or stdout if fout == NULL)
753 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE *fout)
Paul Bakker5121ce52009-01-03 21:22:43 +0000755{
Janos Follath24eed8d2019-11-22 13:21:35 +0000756 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000757 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000758 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000759 * Buffer should have space for (short) label and decimal formatted MPI,
760 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000761 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
763 MPI_VALIDATE_RET(X != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 if (radix < 2 || radix > 16) {
766 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
767 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 memset(s, 0, sizeof(s));
Paul Bakker5121ce52009-01-03 21:22:43 +0000770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 MBEDTLS_MPI_CHK(mbedtls_mpi_write_string(X, radix, s, sizeof(s) - 2, &n));
Paul Bakker5121ce52009-01-03 21:22:43 +0000772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (p == NULL) {
774 p = "";
775 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 plen = strlen(p);
778 slen = strlen(s);
Paul Bakker5121ce52009-01-03 21:22:43 +0000779 s[slen++] = '\r';
780 s[slen++] = '\n';
781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (fout != NULL) {
783 if (fwrite(p, 1, plen, fout) != plen ||
784 fwrite(s, 1, slen, fout) != slen) {
785 return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
786 }
787 } else {
788 mbedtls_printf("%s%s", p, s);
Paul Bakker5121ce52009-01-03 21:22:43 +0000789 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000790
791cleanup:
792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000794}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000796
797/*
Janos Follatha778a942019-02-13 10:28:28 +0000798 * Import X from unsigned binary data, little endian
Przemyslaw Stekiel76960a72022-02-21 13:42:09 +0100799 *
800 * This function is guaranteed to return an MPI with exactly the necessary
801 * number of limbs (in particular, it does not skip 0s in the input).
Janos Follatha778a942019-02-13 10:28:28 +0000802 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100803int mbedtls_mpi_read_binary_le(mbedtls_mpi *X,
804 const unsigned char *buf, size_t buflen)
Janos Follatha778a942019-02-13 10:28:28 +0000805{
Janos Follath24eed8d2019-11-22 13:21:35 +0000806 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 const size_t limbs = CHARS_TO_LIMBS(buflen);
Janos Follatha778a942019-02-13 10:28:28 +0000808
809 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
Janos Follatha778a942019-02-13 10:28:28 +0000811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_le(X->p, X->n, buf, buflen));
Janos Follatha778a942019-02-13 10:28:28 +0000813
814cleanup:
815
Janos Follath171a7ef2019-02-15 16:17:45 +0000816 /*
817 * This function is also used to import keys. However, wiping the buffers
818 * upon failure is not necessary because failure only can happen before any
819 * input is copied.
820 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 return ret;
Janos Follatha778a942019-02-13 10:28:28 +0000822}
823
824/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000825 * Import X from unsigned binary data, big endian
Przemyslaw Stekiel76960a72022-02-21 13:42:09 +0100826 *
827 * This function is guaranteed to return an MPI with exactly the necessary
828 * number of limbs (in particular, it does not skip 0s in the input).
Paul Bakker5121ce52009-01-03 21:22:43 +0000829 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100830int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000831{
Janos Follath24eed8d2019-11-22 13:21:35 +0000832 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 const size_t limbs = CHARS_TO_LIMBS(buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 MPI_VALIDATE_RET(X != NULL);
836 MPI_VALIDATE_RET(buflen == 0 || buf != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000837
Hanno Becker073c1992017-10-17 15:17:27 +0100838 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
Paul Bakker5121ce52009-01-03 21:22:43 +0000840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_be(X->p, X->n, buf, buflen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000842
843cleanup:
844
Janos Follath171a7ef2019-02-15 16:17:45 +0000845 /*
846 * This function is also used to import keys. However, wiping the buffers
847 * upon failure is not necessary because failure only can happen before any
848 * input is copied.
849 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000851}
852
853/*
Janos Follathe344d0f2019-02-19 16:17:40 +0000854 * Export X into unsigned binary data, little endian
855 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100856int mbedtls_mpi_write_binary_le(const mbedtls_mpi *X,
857 unsigned char *buf, size_t buflen)
Janos Follathe344d0f2019-02-19 16:17:40 +0000858{
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 return mbedtls_mpi_core_write_le(X->p, X->n, buf, buflen);
Janos Follathe344d0f2019-02-19 16:17:40 +0000860}
861
862/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000863 * Export X into unsigned binary data, big endian
864 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100865int mbedtls_mpi_write_binary(const mbedtls_mpi *X,
866 unsigned char *buf, size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000867{
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 return mbedtls_mpi_core_write_be(X->p, X->n, buf, buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000869}
870
871/*
872 * Left-shift: X <<= count
873 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100874int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
Paul Bakker5121ce52009-01-03 21:22:43 +0000875{
Janos Follath24eed8d2019-11-22 13:21:35 +0000876 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Minos Galanakis0144b352023-05-02 14:02:32 +0100877 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000879
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 i = mbedtls_mpi_bitlen(X) + count;
Paul Bakker5121ce52009-01-03 21:22:43 +0000881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 if (X->n * biL < i) {
883 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, BITS_TO_LIMBS(i)));
884 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000885
886 ret = 0;
887
Minos Galanakis0144b352023-05-02 14:02:32 +0100888 mbedtls_mpi_core_shift_l(X->p, X->n, count);
Paul Bakker5121ce52009-01-03 21:22:43 +0000889cleanup:
890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000892}
893
894/*
895 * Right-shift: X >>= count
896 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100897int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
Paul Bakker5121ce52009-01-03 21:22:43 +0000898{
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 MPI_VALIDATE_RET(X != NULL);
900 if (X->n != 0) {
901 mbedtls_mpi_core_shift_r(X->p, X->n, count);
902 }
903 return 0;
Gilles Peskine66414202022-09-21 15:36:16 +0200904}
905
Paul Bakker5121ce52009-01-03 21:22:43 +0000906/*
907 * Compare unsigned values
908 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100909int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000910{
Paul Bakker23986e52011-04-24 08:57:21 +0000911 size_t i, j;
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 MPI_VALIDATE_RET(X != NULL);
913 MPI_VALIDATE_RET(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 for (i = X->n; i > 0; i--) {
916 if (X->p[i - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000917 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000919 }
920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 for (j = Y->n; j > 0; j--) {
922 if (Y->p[j - 1] != 0) {
923 break;
924 }
925 }
926
927 if (i == 0 && j == 0) {
928 return 0;
929 }
930
931 if (i > j) {
932 return 1;
933 }
934 if (j > i) {
935 return -1;
936 }
937
938 for (; i > 0; i--) {
939 if (X->p[i - 1] > Y->p[i - 1]) {
940 return 1;
941 }
942 if (X->p[i - 1] < Y->p[i - 1]) {
943 return -1;
944 }
945 }
946
947 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000948}
949
950/*
951 * Compare signed values
952 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100953int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000954{
Paul Bakker23986e52011-04-24 08:57:21 +0000955 size_t i, j;
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 MPI_VALIDATE_RET(X != NULL);
957 MPI_VALIDATE_RET(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000958
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 for (i = X->n; i > 0; i--) {
960 if (X->p[i - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000961 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000963 }
964
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 for (j = Y->n; j > 0; j--) {
966 if (Y->p[j - 1] != 0) {
967 break;
968 }
969 }
970
971 if (i == 0 && j == 0) {
972 return 0;
973 }
974
975 if (i > j) {
976 return X->s;
977 }
978 if (j > i) {
979 return -Y->s;
980 }
981
982 if (X->s > 0 && Y->s < 0) {
983 return 1;
984 }
985 if (Y->s > 0 && X->s < 0) {
986 return -1;
987 }
988
989 for (; i > 0; i--) {
990 if (X->p[i - 1] > Y->p[i - 1]) {
991 return X->s;
992 }
993 if (X->p[i - 1] < Y->p[i - 1]) {
994 return -X->s;
995 }
996 }
997
998 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000999}
1000
Janos Follathee6abce2019-09-05 14:47:19 +01001001/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001002 * Compare signed values
1003 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001004int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
Paul Bakker5121ce52009-01-03 21:22:43 +00001005{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 mbedtls_mpi Y;
1007 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001009
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 *p = mpi_sint_abs(z);
1011 Y.s = (z < 0) ? -1 : 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001012 Y.n = 1;
1013 Y.p = p;
1014
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 return mbedtls_mpi_cmp_mpi(X, &Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00001016}
1017
1018/*
1019 * Unsigned addition: X = |A| + |B| (HAC 14.7)
1020 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001021int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001022{
Janos Follath24eed8d2019-11-22 13:21:35 +00001023 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001024 size_t j;
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 MPI_VALIDATE_RET(X != NULL);
1026 MPI_VALIDATE_RET(A != NULL);
1027 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 if (X == B) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 const mbedtls_mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001031 }
1032
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 if (X != A) {
1034 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
1035 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001036
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001037 /*
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001038 * X must always be positive as a result of unsigned additions.
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001039 */
1040 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 for (j = B->n; j > 0; j--) {
1043 if (B->p[j - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001044 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 }
1046 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001047
Gilles Peskinedb14a9d2022-11-15 22:59:00 +01001048 /* Exit early to avoid undefined behavior on NULL+0 when X->n == 0
1049 * and B is 0 (of any size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 if (j == 0) {
1051 return 0;
1052 }
Gilles Peskinedb14a9d2022-11-15 22:59:00 +01001053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j));
Paul Bakker5121ce52009-01-03 21:22:43 +00001055
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001056 /* j is the number of non-zero limbs of B. Add those to X. */
Paul Bakker5121ce52009-01-03 21:22:43 +00001057
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001058 mbedtls_mpi_uint *p = X->p;
1059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 mbedtls_mpi_uint c = mbedtls_mpi_core_add(p, p, B->p, j);
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001061
1062 p += j;
1063
1064 /* Now propagate any carry */
Paul Bakker5121ce52009-01-03 21:22:43 +00001065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 while (c != 0) {
1067 if (j >= X->n) {
1068 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j + 1));
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001069 p = X->p + j;
Paul Bakker5121ce52009-01-03 21:22:43 +00001070 }
1071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 *p += c; c = (*p < c); j++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001073 }
1074
1075cleanup:
1076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001078}
1079
Paul Bakker5121ce52009-01-03 21:22:43 +00001080/*
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001081 * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
Paul Bakker5121ce52009-01-03 21:22:43 +00001082 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001083int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001084{
Janos Follath24eed8d2019-11-22 13:21:35 +00001085 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001086 size_t n;
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001087 mbedtls_mpi_uint carry;
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 MPI_VALIDATE_RET(X != NULL);
1089 MPI_VALIDATE_RET(A != NULL);
1090 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001091
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 for (n = B->n; n > 0; n--) {
1093 if (B->p[n - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001094 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 }
1096 }
1097 if (n > A->n) {
Gilles Peskinec8a91772021-01-27 22:30:43 +01001098 /* B >= (2^ciL)^n > A */
1099 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1100 goto cleanup;
1101 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001102
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, A->n));
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001104
1105 /* Set the high limbs of X to match A. Don't touch the lower limbs
1106 * because X might be aliased to B, and we must not overwrite the
1107 * significant digits of B. */
Aaron M. Uckoaf67d2c2023-01-17 11:52:22 -05001108 if (A->n > n && A != X) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 memcpy(X->p + n, A->p + n, (A->n - n) * ciL);
1110 }
1111 if (X->n > A->n) {
1112 memset(X->p + A->n, 0, (X->n - A->n) * ciL);
1113 }
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001114
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 carry = mbedtls_mpi_core_sub(X->p, A->p, B->p, n);
1116 if (carry != 0) {
Tom Cosgrove452c99c2022-08-25 10:07:07 +01001117 /* Propagate the carry through the rest of X. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 carry = mbedtls_mpi_core_sub_int(X->p + n, X->p + n, carry, X->n - n);
Tom Cosgrove452c99c2022-08-25 10:07:07 +01001119
1120 /* If we have further carry/borrow, the result is negative. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 if (carry != 0) {
Gilles Peskine89b41302020-07-23 01:16:46 +02001122 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1123 goto cleanup;
1124 }
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001125 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001126
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001127 /* X should always be positive as a result of unsigned subtractions. */
1128 X->s = 1;
1129
Paul Bakker5121ce52009-01-03 21:22:43 +00001130cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001132}
1133
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001134/* Common function for signed addition and subtraction.
1135 * Calculate A + B * flip_B where flip_B is 1 or -1.
Paul Bakker5121ce52009-01-03 21:22:43 +00001136 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001137static int add_sub_mpi(mbedtls_mpi *X,
1138 const mbedtls_mpi *A, const mbedtls_mpi *B,
1139 int flip_B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001140{
Hanno Becker73d7d792018-12-11 10:35:51 +00001141 int ret, s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 MPI_VALIDATE_RET(X != NULL);
1143 MPI_VALIDATE_RET(A != NULL);
1144 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001145
Hanno Becker73d7d792018-12-11 10:35:51 +00001146 s = A->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 if (A->s * B->s * flip_B < 0) {
1148 int cmp = mbedtls_mpi_cmp_abs(A, B);
1149 if (cmp >= 0) {
1150 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, A, B));
Gilles Peskine4a768dd2022-11-09 22:02:16 +01001151 /* If |A| = |B|, the result is 0 and we must set the sign bit
1152 * to +1 regardless of which of A or B was negative. Otherwise,
1153 * since |A| > |B|, the sign is the sign of A. */
1154 X->s = cmp == 0 ? 1 : s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 } else {
1156 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, B, A));
Gilles Peskine4a768dd2022-11-09 22:02:16 +01001157 /* Since |A| < |B|, the sign is the opposite of A. */
Paul Bakker5121ce52009-01-03 21:22:43 +00001158 X->s = -s;
1159 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 } else {
1161 MBEDTLS_MPI_CHK(mbedtls_mpi_add_abs(X, A, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001162 X->s = s;
1163 }
1164
1165cleanup:
1166
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001168}
1169
1170/*
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001171 * Signed addition: X = A + B
1172 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001173int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001174{
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 return add_sub_mpi(X, A, B, 1);
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001176}
1177
1178/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001179 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001180 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001181int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001182{
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 return add_sub_mpi(X, A, B, -1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001184}
1185
1186/*
1187 * Signed addition: X = A + b
1188 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001189int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001190{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001191 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 MPI_VALIDATE_RET(X != NULL);
1194 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001195
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 p[0] = mpi_sint_abs(b);
1197 B.s = (b < 0) ? -1 : 1;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001198 B.n = 1;
1199 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001200
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 return mbedtls_mpi_add_mpi(X, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001202}
1203
1204/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001205 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001207int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001208{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001209 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 MPI_VALIDATE_RET(X != NULL);
1212 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001213
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 p[0] = mpi_sint_abs(b);
1215 B.s = (b < 0) ? -1 : 1;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001216 B.n = 1;
1217 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001218
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 return mbedtls_mpi_sub_mpi(X, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001220}
1221
Paul Bakker5121ce52009-01-03 21:22:43 +00001222/*
1223 * Baseline multiplication: X = A * B (HAC 14.12)
1224 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001225int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001226{
Janos Follath24eed8d2019-11-22 13:21:35 +00001227 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker1772e052022-04-13 06:51:40 +01001228 size_t i, j;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 mbedtls_mpi TA, TB;
Hanno Beckerda763de2022-04-13 06:50:02 +01001230 int result_is_zero = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 MPI_VALIDATE_RET(X != NULL);
1232 MPI_VALIDATE_RET(A != NULL);
1233 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001234
Tom Cosgrove6af26f32022-08-24 16:40:55 +01001235 mbedtls_mpi_init(&TA);
1236 mbedtls_mpi_init(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00001237
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 if (X == A) {
1239 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A)); A = &TA;
1240 }
1241 if (X == B) {
1242 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B)); B = &TB;
1243 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001244
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 for (i = A->n; i > 0; i--) {
1246 if (A->p[i - 1] != 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001247 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 }
1249 }
1250 if (i == 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001251 result_is_zero = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 }
Hanno Beckerda763de2022-04-13 06:50:02 +01001253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 for (j = B->n; j > 0; j--) {
1255 if (B->p[j - 1] != 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001256 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 }
1258 }
1259 if (j == 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001260 result_is_zero = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 }
Hanno Beckerda763de2022-04-13 06:50:02 +01001262
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i + j));
1264 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
Paul Bakker5121ce52009-01-03 21:22:43 +00001265
Tom Cosgrove6af26f32022-08-24 16:40:55 +01001266 mbedtls_mpi_core_mul(X->p, A->p, i, B->p, j);
Paul Bakker5121ce52009-01-03 21:22:43 +00001267
Hanno Beckerda763de2022-04-13 06:50:02 +01001268 /* If the result is 0, we don't shortcut the operation, which reduces
1269 * but does not eliminate side channels leaking the zero-ness. We do
1270 * need to take care to set the sign bit properly since the library does
1271 * not fully support an MPI object with a value of 0 and s == -1. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 if (result_is_zero) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001273 X->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 } else {
Hanno Beckerda763de2022-04-13 06:50:02 +01001275 X->s = A->s * B->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001277
1278cleanup:
1279
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TA);
Paul Bakker5121ce52009-01-03 21:22:43 +00001281
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001283}
1284
1285/*
1286 * Baseline multiplication: X = A * b
1287 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001288int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001289{
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 MPI_VALIDATE_RET(X != NULL);
1291 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001292
Hanno Becker35771312022-04-14 11:52:11 +01001293 size_t n = A->n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 while (n > 0 && A->p[n - 1] == 0) {
Hanno Becker35771312022-04-14 11:52:11 +01001295 --n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 }
Hanno Becker35771312022-04-14 11:52:11 +01001297
Hanno Becker74a11a32022-04-06 06:27:00 +01001298 /* The general method below doesn't work if b==0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 if (b == 0 || n == 0) {
1300 return mbedtls_mpi_lset(X, 0);
1301 }
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001302
Hanno Beckeraef9cc42022-04-11 06:36:29 +01001303 /* Calculate A*b as A + A*(b-1) to take advantage of mbedtls_mpi_core_mla */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001304 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001305 /* In general, A * b requires 1 limb more than b. If
1306 * A->p[n - 1] * b / b == A->p[n - 1], then A * b fits in the same
1307 * number of limbs as A and the call to grow() is not required since
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001308 * copy() will take care of the growth if needed. However, experimentally,
1309 * making the call to grow() unconditional causes slightly fewer
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001310 * calls to calloc() in ECP code, presumably because it reuses the
1311 * same mpi for a while and this way the mpi is more likely to directly
Hanno Becker9137b9c2022-04-12 10:51:54 +01001312 * grow to its final size.
1313 *
1314 * Note that calculating A*b as 0 + A*b doesn't work as-is because
1315 * A,X can be the same. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n + 1));
1317 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
1318 mbedtls_mpi_core_mla(X->p, X->n, A->p, n, b - 1);
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001319
1320cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001322}
1323
1324/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001325 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1326 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001327 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001328static mbedtls_mpi_uint mbedtls_int_div_int(mbedtls_mpi_uint u1,
1329 mbedtls_mpi_uint u0,
1330 mbedtls_mpi_uint d,
1331 mbedtls_mpi_uint *r)
Simon Butcher15b15d12015-11-26 19:35:03 +00001332{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001333#if defined(MBEDTLS_HAVE_UDBL)
1334 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001335#else
Simon Butcher9803d072016-01-03 00:24:34 +00001336 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 const mbedtls_mpi_uint uint_halfword_mask = ((mbedtls_mpi_uint) 1 << biH) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001338 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1339 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001340 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001341#endif
1342
Simon Butcher15b15d12015-11-26 19:35:03 +00001343 /*
1344 * Check for overflow
1345 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 if (0 == d || u1 >= d) {
1347 if (r != NULL) {
1348 *r = ~(mbedtls_mpi_uint) 0u;
1349 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 return ~(mbedtls_mpi_uint) 0u;
Simon Butcher15b15d12015-11-26 19:35:03 +00001352 }
1353
1354#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001355 dividend = (mbedtls_t_udbl) u1 << biL;
1356 dividend |= (mbedtls_t_udbl) u0;
1357 quotient = dividend / d;
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 if (quotient > ((mbedtls_t_udbl) 1 << biL) - 1) {
1359 quotient = ((mbedtls_t_udbl) 1 << biL) - 1;
1360 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001361
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 if (r != NULL) {
1363 *r = (mbedtls_mpi_uint) (dividend - (quotient * d));
1364 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001365
1366 return (mbedtls_mpi_uint) quotient;
1367#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001368
1369 /*
1370 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1371 * Vol. 2 - Seminumerical Algorithms, Knuth
1372 */
1373
1374 /*
1375 * Normalize the divisor, d, and dividend, u0, u1
1376 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 s = mbedtls_mpi_core_clz(d);
Simon Butcher15b15d12015-11-26 19:35:03 +00001378 d = d << s;
1379
1380 u1 = u1 << s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 u1 |= (u0 >> (biL - s)) & (-(mbedtls_mpi_sint) s >> (biL - 1));
Simon Butcher15b15d12015-11-26 19:35:03 +00001382 u0 = u0 << s;
1383
1384 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001385 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001386
1387 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001388 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001389
1390 /*
1391 * Find the first quotient and remainder
1392 */
1393 q1 = u1 / d1;
1394 r0 = u1 - d1 * q1;
1395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 while (q1 >= radix || (q1 * d0 > radix * r0 + u0_msw)) {
Simon Butcher15b15d12015-11-26 19:35:03 +00001397 q1 -= 1;
1398 r0 += d1;
1399
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 if (r0 >= radix) {
1401 break;
1402 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001403 }
1404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 rAX = (u1 * radix) + (u0_msw - q1 * d);
Simon Butcher15b15d12015-11-26 19:35:03 +00001406 q0 = rAX / d1;
1407 r0 = rAX - q0 * d1;
1408
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 while (q0 >= radix || (q0 * d0 > radix * r0 + u0_lsw)) {
Simon Butcher15b15d12015-11-26 19:35:03 +00001410 q0 -= 1;
1411 r0 += d1;
1412
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 if (r0 >= radix) {
1414 break;
1415 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001416 }
1417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 if (r != NULL) {
1419 *r = (rAX * radix + u0_lsw - q0 * d) >> s;
1420 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001421
1422 quotient = q1 * radix + q0;
1423
1424 return quotient;
1425#endif
1426}
1427
1428/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001430 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001431int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1432 const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001433{
Janos Follath24eed8d2019-11-22 13:21:35 +00001434 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001435 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 mbedtls_mpi X, Y, Z, T1, T2;
Alexander Kd19a1932019-11-01 18:20:42 +03001437 mbedtls_mpi_uint TP2[3];
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 MPI_VALIDATE_RET(A != NULL);
1439 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001440
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 if (mbedtls_mpi_cmp_int(B, 0) == 0) {
1442 return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
1443 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001444
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z);
1446 mbedtls_mpi_init(&T1);
Alexander Kd19a1932019-11-01 18:20:42 +03001447 /*
1448 * Avoid dynamic memory allocations for constant-size T2.
1449 *
1450 * T2 is used for comparison only and the 3 limbs are assigned explicitly,
1451 * so nobody increase the size of the MPI and we're safe to use an on-stack
1452 * buffer.
1453 */
Alexander K35d6d462019-10-31 14:46:45 +03001454 T2.s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 T2.n = sizeof(TP2) / sizeof(*TP2);
Alexander Kd19a1932019-11-01 18:20:42 +03001456 T2.p = TP2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if (mbedtls_mpi_cmp_abs(A, B) < 0) {
1459 if (Q != NULL) {
1460 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(Q, 0));
1461 }
1462 if (R != NULL) {
1463 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, A));
1464 }
1465 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001466 }
1467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&X, A));
1469 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001470 X.s = Y.s = 1;
1471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&Z, A->n + 2));
1473 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Z, 0));
1474 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T1, A->n + 2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001475
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 k = mbedtls_mpi_bitlen(&Y) % biL;
1477 if (k < biL - 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001478 k = biL - 1 - k;
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&X, k));
1480 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, k));
1481 } else {
1482 k = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001483 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001484
1485 n = X.n - 1;
1486 t = Y.n - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, biL * (n - t)));
Paul Bakker5121ce52009-01-03 21:22:43 +00001488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 while (mbedtls_mpi_cmp_mpi(&X, &Y) >= 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001490 Z.p[n - t]++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &Y));
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, biL * (n - t)));
Paul Bakker5121ce52009-01-03 21:22:43 +00001494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 for (i = n; i > t; i--) {
1496 if (X.p[i] >= Y.p[t]) {
1497 Z.p[i - t - 1] = ~(mbedtls_mpi_uint) 0u;
1498 } else {
1499 Z.p[i - t - 1] = mbedtls_int_div_int(X.p[i], X.p[i - 1],
1500 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001501 }
1502
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 T2.p[0] = (i < 2) ? 0 : X.p[i - 2];
1504 T2.p[1] = (i < 1) ? 0 : X.p[i - 1];
Alexander K35d6d462019-10-31 14:46:45 +03001505 T2.p[2] = X.p[i];
1506
Paul Bakker5121ce52009-01-03 21:22:43 +00001507 Z.p[i - t - 1]++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 do {
Paul Bakker5121ce52009-01-03 21:22:43 +00001509 Z.p[i - t - 1]--;
1510
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&T1, 0));
1512 T1.p[0] = (t < 1) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001513 T1.p[1] = Y.p[t];
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &T1, Z.p[i - t - 1]));
1515 } while (mbedtls_mpi_cmp_mpi(&T1, &T2) > 0);
Paul Bakker5121ce52009-01-03 21:22:43 +00001516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &Y, Z.p[i - t - 1]));
1518 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
1519 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &T1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 if (mbedtls_mpi_cmp_int(&X, 0) < 0) {
1522 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T1, &Y));
1523 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
1524 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&X, &X, &T1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001525 Z.p[i - t - 1]--;
1526 }
1527 }
1528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 if (Q != NULL) {
1530 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(Q, &Z));
Paul Bakker5121ce52009-01-03 21:22:43 +00001531 Q->s = A->s * B->s;
1532 }
1533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 if (R != NULL) {
1535 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&X, k));
Paul Bakkerf02c5642012-11-13 10:25:21 +00001536 X.s = A->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, &X));
Paul Bakker5121ce52009-01-03 21:22:43 +00001538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 if (mbedtls_mpi_cmp_int(R, 0) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001540 R->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001542 }
1543
1544cleanup:
1545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z);
1547 mbedtls_mpi_free(&T1);
1548 mbedtls_platform_zeroize(TP2, sizeof(TP2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001551}
1552
1553/*
1554 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00001555 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001556int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R,
1557 const mbedtls_mpi *A,
1558 mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001559{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001560 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001563
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 p[0] = mpi_sint_abs(b);
1565 B.s = (b < 0) ? -1 : 1;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001566 B.n = 1;
1567 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001568
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 return mbedtls_mpi_div_mpi(Q, R, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001570}
1571
1572/*
1573 * Modulo: R = A mod B
1574 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001575int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001576{
Janos Follath24eed8d2019-11-22 13:21:35 +00001577 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 MPI_VALIDATE_RET(R != NULL);
1579 MPI_VALIDATE_RET(A != NULL);
1580 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001581
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 if (mbedtls_mpi_cmp_int(B, 0) < 0) {
1583 return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1584 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001585
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(NULL, R, A, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 while (mbedtls_mpi_cmp_int(R, 0) < 0) {
1589 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(R, R, B));
1590 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001591
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 while (mbedtls_mpi_cmp_mpi(R, B) >= 0) {
1593 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(R, R, B));
1594 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001595
1596cleanup:
1597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001599}
1600
1601/*
1602 * Modulo: r = A mod b
1603 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001604int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001605{
Paul Bakker23986e52011-04-24 08:57:21 +00001606 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 mbedtls_mpi_uint x, y, z;
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 MPI_VALIDATE_RET(r != NULL);
1609 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001610
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 if (b == 0) {
1612 return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
1613 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001614
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 if (b < 0) {
1616 return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1617 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001618
1619 /*
1620 * handle trivial cases
1621 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 if (b == 1 || A->n == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001623 *r = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001625 }
1626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 if (b == 2) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001628 *r = A->p[0] & 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001630 }
1631
1632 /*
1633 * general case
1634 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 for (i = A->n, y = 0; i > 0; i--) {
Paul Bakker23986e52011-04-24 08:57:21 +00001636 x = A->p[i - 1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 y = (y << biH) | (x >> biH);
Paul Bakker5121ce52009-01-03 21:22:43 +00001638 z = y / b;
1639 y -= z * b;
1640
1641 x <<= biH;
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 y = (y << biH) | (x >> biH);
Paul Bakker5121ce52009-01-03 21:22:43 +00001643 z = y / b;
1644 y -= z * b;
1645 }
1646
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001647 /*
1648 * If A is negative, then the current y represents a negative value.
1649 * Flipping it to the positive side.
1650 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 if (A->s < 0 && y != 0) {
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001652 y = b - y;
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001654
Paul Bakker5121ce52009-01-03 21:22:43 +00001655 *r = y;
1656
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001658}
1659
Gilles Peskine449bd832023-01-11 14:50:10 +01001660static void mpi_montg_init(mbedtls_mpi_uint *mm, const mbedtls_mpi *N)
Paul Bakker5121ce52009-01-03 21:22:43 +00001661{
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 *mm = mbedtls_mpi_core_montmul_init(N->p);
Paul Bakker5121ce52009-01-03 21:22:43 +00001663}
1664
Tom Cosgrove93842842022-08-05 16:59:43 +01001665/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
1666 *
1667 * \param[in,out] A One of the numbers to multiply.
1668 * It must have at least as many limbs as N
1669 * (A->n >= N->n), and any limbs beyond n are ignored.
1670 * On successful completion, A contains the result of
1671 * the multiplication A * B * R^-1 mod N where
1672 * R = (2^ciL)^n.
1673 * \param[in] B One of the numbers to multiply.
1674 * It must be nonzero and must not have more limbs than N
1675 * (B->n <= N->n).
Tom Cosgrove40d22942022-08-17 06:42:44 +01001676 * \param[in] N The modulus. \p N must be odd.
Tom Cosgrove93842842022-08-05 16:59:43 +01001677 * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
1678 * This is -N^-1 mod 2^ciL.
1679 * \param[in,out] T A bignum for temporary storage.
1680 * It must be at least twice the limb size of N plus 1
1681 * (T->n >= 2 * N->n + 1).
1682 * Its initial content is unused and
1683 * its final content is indeterminate.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +01001684 * It does not get reallocated.
Tom Cosgrove93842842022-08-05 16:59:43 +01001685 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001686static void mpi_montmul(mbedtls_mpi *A, const mbedtls_mpi *B,
1687 const mbedtls_mpi *N, mbedtls_mpi_uint mm,
1688 mbedtls_mpi *T)
Paul Bakker5121ce52009-01-03 21:22:43 +00001689{
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 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 +00001691}
1692
1693/*
1694 * Montgomery reduction: A = A * R^-1 mod N
Gilles Peskine2a82f722020-06-04 15:00:49 +02001695 *
Tom Cosgrove93842842022-08-05 16:59:43 +01001696 * See mpi_montmul() regarding constraints and guarantees on the parameters.
Paul Bakker5121ce52009-01-03 21:22:43 +00001697 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001698static void mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N,
1699 mbedtls_mpi_uint mm, mbedtls_mpi *T)
Paul Bakker5121ce52009-01-03 21:22:43 +00001700{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 mbedtls_mpi_uint z = 1;
1702 mbedtls_mpi U;
Paul Bakker5121ce52009-01-03 21:22:43 +00001703
Paul Bakker8ddb6452013-02-27 14:56:33 +01001704 U.n = U.s = (int) z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001705 U.p = &z;
1706
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 mpi_montmul(A, &U, N, mm, T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001708}
1709
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001710/**
1711 * Select an MPI from a table without leaking the index.
1712 *
1713 * This is functionally equivalent to mbedtls_mpi_copy(R, T[idx]) except it
1714 * reads the entire table in order to avoid leaking the value of idx to an
1715 * attacker able to observe memory access patterns.
1716 *
1717 * \param[out] R Where to write the selected MPI.
1718 * \param[in] T The table to read from.
1719 * \param[in] T_size The number of elements in the table.
1720 * \param[in] idx The index of the element to select;
1721 * this must satisfy 0 <= idx < T_size.
1722 *
1723 * \return \c 0 on success, or a negative error code.
1724 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001725static 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 +01001726{
1727 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1728
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 for (size_t i = 0; i < T_size; i++) {
1730 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(R, &T[i],
Dave Rodgmanee54faf2023-05-17 13:56:33 +01001731 (unsigned char) mbedtls_ct_bool_eq(i, idx)));
Manuel Pégourié-Gonnard92413ef2021-06-03 10:42:46 +02001732 }
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001733cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 return ret;
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001735}
1736
Paul Bakker5121ce52009-01-03 21:22:43 +00001737/*
1738 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
1739 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001740int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
1741 const mbedtls_mpi *E, const mbedtls_mpi *N,
1742 mbedtls_mpi *prec_RR)
Paul Bakker5121ce52009-01-03 21:22:43 +00001743{
Janos Follath24eed8d2019-11-22 13:21:35 +00001744 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath74601202022-11-21 15:54:20 +00001745 size_t window_bitsize;
Paul Bakker23986e52011-04-24 08:57:21 +00001746 size_t i, j, nblimbs;
1747 size_t bufsize, nbits;
Paul Elliott1748de12023-02-13 15:35:35 +00001748 size_t exponent_bits_in_window = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 mbedtls_mpi_uint ei, mm, state;
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 mbedtls_mpi RR, T, W[(size_t) 1 << MBEDTLS_MPI_WINDOW_SIZE], WW, Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00001751 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001752
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 MPI_VALIDATE_RET(X != NULL);
1754 MPI_VALIDATE_RET(A != NULL);
1755 MPI_VALIDATE_RET(E != NULL);
1756 MPI_VALIDATE_RET(N != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00001757
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 if (mbedtls_mpi_cmp_int(N, 0) <= 0 || (N->p[0] & 1) == 0) {
1759 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1760 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001761
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 if (mbedtls_mpi_cmp_int(E, 0) < 0) {
1763 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1764 }
Paul Bakkerf6198c12012-05-16 08:02:29 +00001765
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 if (mbedtls_mpi_bitlen(E) > MBEDTLS_MPI_MAX_BITS ||
1767 mbedtls_mpi_bitlen(N) > MBEDTLS_MPI_MAX_BITS) {
1768 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1769 }
Chris Jones9246d042020-11-25 15:12:39 +00001770
Paul Bakkerf6198c12012-05-16 08:02:29 +00001771 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001772 * Init temps and window size
1773 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 mpi_montg_init(&mm, N);
1775 mbedtls_mpi_init(&RR); mbedtls_mpi_init(&T);
1776 mbedtls_mpi_init(&Apos);
1777 mbedtls_mpi_init(&WW);
1778 memset(W, 0, sizeof(W));
Paul Bakker5121ce52009-01-03 21:22:43 +00001779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 i = mbedtls_mpi_bitlen(E);
Paul Bakker5121ce52009-01-03 21:22:43 +00001781
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 window_bitsize = (i > 671) ? 6 : (i > 239) ? 5 :
1783 (i > 79) ? 4 : (i > 23) ? 3 : 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001784
Gilles Peskine449bd832023-01-11 14:50:10 +01001785#if (MBEDTLS_MPI_WINDOW_SIZE < 6)
1786 if (window_bitsize > MBEDTLS_MPI_WINDOW_SIZE) {
Janos Follath7fa11b82022-11-21 14:48:02 +00001787 window_bitsize = MBEDTLS_MPI_WINDOW_SIZE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 }
Peter Kolbuse6bcad32018-12-11 14:01:44 -06001789#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00001790
Janos Follathc8d66d52022-11-22 10:47:10 +00001791 const size_t w_table_used_size = (size_t) 1 << window_bitsize;
Janos Follath06000952022-11-22 10:18:06 +00001792
Paul Bakker5121ce52009-01-03 21:22:43 +00001793 /*
Janos Follathbe54ca72022-11-21 16:14:54 +00001794 * This function is not constant-trace: its memory accesses depend on the
1795 * exponent value. To defend against timing attacks, callers (such as RSA
1796 * and DHM) should use exponent blinding. However this is not enough if the
1797 * adversary can find the exponent in a single trace, so this function
1798 * takes extra precautions against adversaries who can observe memory
1799 * access patterns.
Janos Follathf08b40e2022-11-11 15:56:38 +00001800 *
Janos Follathbe54ca72022-11-21 16:14:54 +00001801 * This function performs a series of multiplications by table elements and
1802 * squarings, and we want the prevent the adversary from finding out which
1803 * table element was used, and from distinguishing between multiplications
1804 * and squarings. Firstly, when multiplying by an element of the window
1805 * W[i], we do a constant-trace table lookup to obfuscate i. This leaves
1806 * squarings as having a different memory access patterns from other
1807 * multiplications. So secondly, we put the accumulator X in the table as
1808 * well, and also do a constant-trace table lookup to multiply by X.
1809 *
1810 * This way, all multiplications take the form of a lookup-and-multiply.
1811 * The number of lookup-and-multiply operations inside each iteration of
1812 * the main loop still depends on the bits of the exponent, but since the
1813 * other operations in the loop don't have an easily recognizable memory
1814 * trace, an adversary is unlikely to be able to observe the exact
1815 * patterns.
1816 *
1817 * An adversary may still be able to recover the exponent if they can
1818 * observe both memory accesses and branches. However, branch prediction
1819 * exploitation typically requires many traces of execution over the same
1820 * data, which is defeated by randomized blinding.
Janos Follath84461482022-11-21 14:31:22 +00001821 *
1822 * To achieve this, we make a copy of X and we use the table entry in each
1823 * calculation from this point on.
Janos Follath8e7d6a02022-10-04 13:27:40 +01001824 */
Janos Follathc8d66d52022-11-22 10:47:10 +00001825 const size_t x_index = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 mbedtls_mpi_init(&W[x_index]);
1827 mbedtls_mpi_copy(&W[x_index], X);
Janos Follath84461482022-11-21 14:31:22 +00001828
Paul Bakker5121ce52009-01-03 21:22:43 +00001829 j = N->n + 1;
Tom Cosgrove93842842022-08-05 16:59:43 +01001830 /* All W[i] and X must have at least N->n limbs for the mpi_montmul()
Paul Bakker5121ce52009-01-03 21:22:43 +00001831 * and mpi_montred() calls later. Here we ensure that W[1] and X are
1832 * large enough, and later we'll grow other W[i] to the same length.
1833 * They must not be shrunk midway through this function!
1834 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[x_index], j));
1836 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[1], j));
1837 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T, j * 2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001838
1839 /*
Paul Bakker50546922012-05-19 08:40:49 +00001840 * Compensate for negative A (and correct at the end)
1841 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 neg = (A->s == -1);
1843 if (neg) {
1844 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Apos, A));
Paul Bakker50546922012-05-19 08:40:49 +00001845 Apos.s = 1;
1846 A = &Apos;
1847 }
1848
1849 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001850 * If 1st call, pre-compute R^2 mod N
1851 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 if (prec_RR == NULL || prec_RR->p == NULL) {
1853 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&RR, 1));
1854 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&RR, N->n * 2 * biL));
1855 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&RR, &RR, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001856
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 if (prec_RR != NULL) {
1858 memcpy(prec_RR, &RR, sizeof(mbedtls_mpi));
1859 }
1860 } else {
1861 memcpy(&RR, prec_RR, sizeof(mbedtls_mpi));
Paul Bakker5121ce52009-01-03 21:22:43 +00001862 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001863
1864 /*
1865 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
1866 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 if (mbedtls_mpi_cmp_mpi(A, N) >= 0) {
1868 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&W[1], A, N));
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001869 /* This should be a no-op because W[1] is already that large before
1870 * mbedtls_mpi_mod_mpi(), but it's necessary to avoid an overflow
Tom Cosgrove93842842022-08-05 16:59:43 +01001871 * in mpi_montmul() below, so let's make sure. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[1], N->n + 1));
1873 } else {
1874 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[1], A));
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001875 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001876
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001877 /* Note that this is safe because W[1] always has at least N->n limbs
1878 * (it grew above and was preserved by mbedtls_mpi_copy()). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 mpi_montmul(&W[1], &RR, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001880
1881 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01001882 * W[x_index] = R^2 * R^-1 mod N = R mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001883 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[x_index], &RR));
1885 mpi_montred(&W[x_index], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001886
Janos Follathc8d66d52022-11-22 10:47:10 +00001887
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 if (window_bitsize > 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001889 /*
Janos Follath74601202022-11-21 15:54:20 +00001890 * W[i] = W[1] ^ i
1891 *
1892 * The first bit of the sliding window is always 1 and therefore we
1893 * only need to store the second half of the table.
Janos Follathc8d66d52022-11-22 10:47:10 +00001894 *
1895 * (There are two special elements in the table: W[0] for the
1896 * accumulator/result and W[1] for A in Montgomery form. Both of these
1897 * are already set at this point.)
Paul Bakker5121ce52009-01-03 21:22:43 +00001898 */
Janos Follath74601202022-11-21 15:54:20 +00001899 j = w_table_used_size / 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001900
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[j], N->n + 1));
1902 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[j], &W[1]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001903
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 for (i = 0; i < window_bitsize - 1; i++) {
1905 mpi_montmul(&W[j], &W[j], N, mm, &T);
1906 }
Paul Bakker0d7702c2013-10-29 16:18:35 +01001907
Paul Bakker5121ce52009-01-03 21:22:43 +00001908 /*
1909 * W[i] = W[i - 1] * W[1]
1910 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 for (i = j + 1; i < w_table_used_size; i++) {
1912 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[i], N->n + 1));
1913 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[i], &W[i - 1]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 mpi_montmul(&W[i], &W[1], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001916 }
1917 }
1918
1919 nblimbs = E->n;
1920 bufsize = 0;
1921 nbits = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001922 state = 0;
1923
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 while (1) {
1925 if (bufsize == 0) {
1926 if (nblimbs == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001929
Paul Bakker0d7702c2013-10-29 16:18:35 +01001930 nblimbs--;
1931
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 bufsize = sizeof(mbedtls_mpi_uint) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00001933 }
1934
1935 bufsize--;
1936
1937 ei = (E->p[nblimbs] >> bufsize) & 1;
1938
1939 /*
1940 * skip leading 0s
1941 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 if (ei == 0 && state == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001943 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 if (ei == 0 && state == 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001947 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01001948 * out of window, square W[x_index]
Paul Bakker5121ce52009-01-03 21:22:43 +00001949 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, x_index));
1951 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001952 continue;
1953 }
1954
1955 /*
1956 * add ei to current window
1957 */
1958 state = 2;
1959
1960 nbits++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 exponent_bits_in_window |= (ei << (window_bitsize - nbits));
Paul Bakker5121ce52009-01-03 21:22:43 +00001962
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 if (nbits == window_bitsize) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001964 /*
Janos Follath7fa11b82022-11-21 14:48:02 +00001965 * W[x_index] = W[x_index]^window_bitsize R^-1 mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001966 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 for (i = 0; i < window_bitsize; i++) {
1968 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size,
1969 x_index));
1970 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Janos Follathb764ee12022-10-04 14:00:09 +01001971 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001972
1973 /*
Janos Follath7fa11b82022-11-21 14:48:02 +00001974 * W[x_index] = W[x_index] * W[exponent_bits_in_window] R^-1 mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001975 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size,
1977 exponent_bits_in_window));
1978 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001979
1980 state--;
1981 nbits = 0;
Janos Follath7fa11b82022-11-21 14:48:02 +00001982 exponent_bits_in_window = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001983 }
1984 }
1985
1986 /*
1987 * process the remaining bits
1988 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 for (i = 0; i < nbits; i++) {
1990 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, x_index));
1991 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001992
Janos Follath7fa11b82022-11-21 14:48:02 +00001993 exponent_bits_in_window <<= 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001994
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 if ((exponent_bits_in_window & ((size_t) 1 << window_bitsize)) != 0) {
1996 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, 1));
1997 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Janos Follathb764ee12022-10-04 14:00:09 +01001998 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001999 }
2000
2001 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01002002 * W[x_index] = A^E * R * R^-1 mod N = A^E mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00002003 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 mpi_montred(&W[x_index], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00002005
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 if (neg && E->n != 0 && (E->p[0] & 1) != 0) {
Janos Follath8e7d6a02022-10-04 13:27:40 +01002007 W[x_index].s = -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&W[x_index], N, &W[x_index]));
Paul Bakkerf6198c12012-05-16 08:02:29 +00002009 }
2010
Janos Follath8e7d6a02022-10-04 13:27:40 +01002011 /*
2012 * Load the result in the output variable.
2013 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 mbedtls_mpi_copy(X, &W[x_index]);
Janos Follath8e7d6a02022-10-04 13:27:40 +01002015
Paul Bakker5121ce52009-01-03 21:22:43 +00002016cleanup:
2017
Janos Follathb2c2fca2022-11-21 15:05:31 +00002018 /* The first bit of the sliding window is always 1 and therefore the first
2019 * half of the table was unused. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 for (i = w_table_used_size/2; i < w_table_used_size; i++) {
2021 mbedtls_mpi_free(&W[i]);
2022 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 mbedtls_mpi_free(&W[x_index]);
2025 mbedtls_mpi_free(&W[1]);
2026 mbedtls_mpi_free(&T);
2027 mbedtls_mpi_free(&Apos);
2028 mbedtls_mpi_free(&WW);
Paul Bakker6c591fa2011-05-05 11:49:20 +00002029
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 if (prec_RR == NULL || prec_RR->p == NULL) {
2031 mbedtls_mpi_free(&RR);
2032 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002035}
2036
Paul Bakker5121ce52009-01-03 21:22:43 +00002037/*
2038 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
2039 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002040int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00002041{
Janos Follath24eed8d2019-11-22 13:21:35 +00002042 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002043 size_t lz, lzt;
Alexander Ke8ad49f2019-08-16 16:16:07 +03002044 mbedtls_mpi TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00002045
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 MPI_VALIDATE_RET(G != NULL);
2047 MPI_VALIDATE_RET(A != NULL);
2048 MPI_VALIDATE_RET(B != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00002049
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00002051
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A));
2053 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00002054
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 lz = mbedtls_mpi_lsb(&TA);
2056 lzt = mbedtls_mpi_lsb(&TB);
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002057
Gilles Peskine27253bc2021-06-09 13:26:43 +02002058 /* The loop below gives the correct result when A==0 but not when B==0.
2059 * So have a special case for B==0. Leverage the fact that we just
2060 * calculated the lsb and lsb(B)==0 iff B is odd or 0 to make the test
2061 * slightly more efficient than cmp_int(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 if (lzt == 0 && mbedtls_mpi_get_bit(&TB, 0) == 0) {
2063 ret = mbedtls_mpi_copy(G, A);
Gilles Peskine27253bc2021-06-09 13:26:43 +02002064 goto cleanup;
2065 }
2066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 if (lzt < lz) {
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002068 lz = lzt;
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002070
Paul Bakker5121ce52009-01-03 21:22:43 +00002071 TA.s = TB.s = 1;
2072
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002073 /* We mostly follow the procedure described in HAC 14.54, but with some
2074 * minor differences:
2075 * - Sequences of multiplications or divisions by 2 are grouped into a
2076 * single shift operation.
Gilles Peskineb09c7ee2021-06-21 18:58:39 +02002077 * - The procedure in HAC assumes that 0 < TB <= TA.
2078 * - The condition TB <= TA is not actually necessary for correctness.
2079 * TA and TB have symmetric roles except for the loop termination
2080 * condition, and the shifts at the beginning of the loop body
2081 * remove any significance from the ordering of TA vs TB before
2082 * the shifts.
2083 * - If TA = 0, the loop goes through 0 iterations and the result is
2084 * correctly TB.
2085 * - The case TB = 0 was short-circuited above.
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002086 *
2087 * For the correctness proof below, decompose the original values of
2088 * A and B as
2089 * A = sa * 2^a * A' with A'=0 or A' odd, and sa = +-1
2090 * B = sb * 2^b * B' with B'=0 or B' odd, and sb = +-1
2091 * Then gcd(A, B) = 2^{min(a,b)} * gcd(A',B'),
2092 * and gcd(A',B') is odd or 0.
2093 *
2094 * At the beginning, we have TA = |A| and TB = |B| so gcd(A,B) = gcd(TA,TB).
2095 * The code maintains the following invariant:
2096 * gcd(A,B) = 2^k * gcd(TA,TB) for some k (I)
Gilles Peskine4df3f1f2021-06-15 22:09:39 +02002097 */
2098
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002099 /* Proof that the loop terminates:
2100 * At each iteration, either the right-shift by 1 is made on a nonzero
2101 * value and the nonnegative integer bitlen(TA) + bitlen(TB) decreases
2102 * by at least 1, or the right-shift by 1 is made on zero and then
2103 * TA becomes 0 which ends the loop (TB cannot be 0 if it is right-shifted
2104 * since in that case TB is calculated from TB-TA with the condition TB>TA).
2105 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 while (mbedtls_mpi_cmp_int(&TA, 0) != 0) {
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002107 /* Divisions by 2 preserve the invariant (I). */
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, mbedtls_mpi_lsb(&TA)));
2109 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, mbedtls_mpi_lsb(&TB)));
Paul Bakker5121ce52009-01-03 21:22:43 +00002110
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002111 /* Set either TA or TB to |TA-TB|/2. Since TA and TB are both odd,
2112 * TA-TB is even so the division by 2 has an integer result.
2113 * Invariant (I) is preserved since any odd divisor of both TA and TB
2114 * also divides |TA-TB|/2, and any odd divisor of both TA and |TA-TB|/2
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002115 * also divides TB, and any odd divisor of both TB and |TA-TB|/2 also
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002116 * divides TA.
2117 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 if (mbedtls_mpi_cmp_mpi(&TA, &TB) >= 0) {
2119 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TA, &TA, &TB));
2120 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, 1));
2121 } else {
2122 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TB, &TB, &TA));
2123 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 }
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002125 /* Note that one of TA or TB is still odd. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 }
2127
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002128 /* By invariant (I), gcd(A,B) = 2^k * gcd(TA,TB) for some k.
2129 * At the loop exit, TA = 0, so gcd(TA,TB) = TB.
2130 * - If there was at least one loop iteration, then one of TA or TB is odd,
2131 * and TA = 0, so TB is odd and gcd(TA,TB) = gcd(A',B'). In this case,
2132 * lz = min(a,b) so gcd(A,B) = 2^lz * TB.
2133 * - If there was no loop iteration, then A was 0, and gcd(A,B) = B.
Gilles Peskine4d3fd362021-06-21 11:40:38 +02002134 * 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 +02002135 */
2136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&TB, lz));
2138 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(G, &TB));
Paul Bakker5121ce52009-01-03 21:22:43 +00002139
2140cleanup:
2141
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00002143
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002145}
2146
Paul Bakker33dc46b2014-04-30 16:11:39 +02002147/*
2148 * Fill X with size bytes of random.
Gilles Peskine22cdd0c2022-10-27 20:15:13 +02002149 * The bytes returned from the RNG are used in a specific order which
2150 * is suitable for deterministic ECDSA (see the specification of
2151 * mbedtls_mpi_random() and the implementation in mbedtls_mpi_fill_random()).
Paul Bakker33dc46b2014-04-30 16:11:39 +02002152 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002153int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
2154 int (*f_rng)(void *, unsigned char *, size_t),
2155 void *p_rng)
Paul Bakker287781a2011-03-26 13:18:49 +00002156{
Janos Follath24eed8d2019-11-22 13:21:35 +00002157 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 const size_t limbs = CHARS_TO_LIMBS(size);
Hanno Beckerda1655a2017-10-18 14:21:44 +01002159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 MPI_VALIDATE_RET(X != NULL);
2161 MPI_VALIDATE_RET(f_rng != NULL);
Paul Bakker33dc46b2014-04-30 16:11:39 +02002162
Hanno Beckerda1655a2017-10-18 14:21:44 +01002163 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
2165 if (size == 0) {
2166 return 0;
2167 }
Paul Bakker287781a2011-03-26 13:18:49 +00002168
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 ret = mbedtls_mpi_core_fill_random(X->p, X->n, size, f_rng, p_rng);
Paul Bakker287781a2011-03-26 13:18:49 +00002170
2171cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 return ret;
Paul Bakker287781a2011-03-26 13:18:49 +00002173}
2174
Gilles Peskine449bd832023-01-11 14:50:10 +01002175int mbedtls_mpi_random(mbedtls_mpi *X,
2176 mbedtls_mpi_sint min,
2177 const mbedtls_mpi *N,
2178 int (*f_rng)(void *, unsigned char *, size_t),
2179 void *p_rng)
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002180{
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 if (min < 0) {
2182 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2183 }
2184 if (mbedtls_mpi_cmp_int(N, min) <= 0) {
2185 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2186 }
Gilles Peskine1e918f42021-03-29 22:14:51 +02002187
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002188 /* Ensure that target MPI has exactly the same number of limbs
2189 * as the upper bound, even if the upper bound has leading zeros.
Gilles Peskine6b7ce962022-12-15 15:04:33 +01002190 * This is necessary for mbedtls_mpi_core_random. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 int ret = mbedtls_mpi_resize_clear(X, N->n);
2192 if (ret != 0) {
2193 return ret;
2194 }
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 return mbedtls_mpi_core_random(X->p, min, N->p, X->n, f_rng, p_rng);
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002197}
2198
Paul Bakker5121ce52009-01-03 21:22:43 +00002199/*
2200 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2201 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002202int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
Paul Bakker5121ce52009-01-03 21:22:43 +00002203{
Janos Follath24eed8d2019-11-22 13:21:35 +00002204 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 MPI_VALIDATE_RET(X != NULL);
2207 MPI_VALIDATE_RET(A != NULL);
2208 MPI_VALIDATE_RET(N != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00002209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 if (mbedtls_mpi_cmp_int(N, 1) <= 0) {
2211 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2212 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TU); mbedtls_mpi_init(&U1); mbedtls_mpi_init(&U2);
2215 mbedtls_mpi_init(&G); mbedtls_mpi_init(&TB); mbedtls_mpi_init(&TV);
2216 mbedtls_mpi_init(&V1); mbedtls_mpi_init(&V2);
Paul Bakker5121ce52009-01-03 21:22:43 +00002217
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, A, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 goto cleanup;
2223 }
2224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&TA, A, N));
2226 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TU, &TA));
2227 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, N));
2228 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TV, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U1, 1));
2231 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U2, 0));
2232 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V1, 0));
2233 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002234
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 do {
2236 while ((TU.p[0] & 1) == 0) {
2237 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TU, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002238
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 if ((U1.p[0] & 1) != 0 || (U2.p[0] & 1) != 0) {
2240 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&U1, &U1, &TB));
2241 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &TA));
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 }
2243
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U1, 1));
2245 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002246 }
2247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 while ((TV.p[0] & 1) == 0) {
2249 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TV, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 if ((V1.p[0] & 1) != 0 || (V2.p[0] & 1) != 0) {
2252 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, &TB));
2253 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &TA));
Paul Bakker5121ce52009-01-03 21:22:43 +00002254 }
2255
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V1, 1));
2257 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002258 }
2259
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 if (mbedtls_mpi_cmp_mpi(&TU, &TV) >= 0) {
2261 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TU, &TU, &TV));
2262 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U1, &U1, &V1));
2263 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &V2));
2264 } else {
2265 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TV, &TV, &TU));
2266 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, &U1));
2267 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &U2));
Paul Bakker5121ce52009-01-03 21:22:43 +00002268 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 } while (mbedtls_mpi_cmp_int(&TU, 0) != 0);
2270
2271 while (mbedtls_mpi_cmp_int(&V1, 0) < 0) {
2272 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002273 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002274
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 while (mbedtls_mpi_cmp_mpi(&V1, N) >= 0) {
2276 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, N));
2277 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002278
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, &V1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002280
2281cleanup:
2282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TU); mbedtls_mpi_free(&U1); mbedtls_mpi_free(&U2);
2284 mbedtls_mpi_free(&G); mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TV);
2285 mbedtls_mpi_free(&V1); mbedtls_mpi_free(&V2);
Paul Bakker5121ce52009-01-03 21:22:43 +00002286
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002288}
2289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002291
Paul Bakker5121ce52009-01-03 21:22:43 +00002292static const int small_prime[] =
2293{
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 3, 5, 7, 11, 13, 17, 19, 23,
2295 29, 31, 37, 41, 43, 47, 53, 59,
2296 61, 67, 71, 73, 79, 83, 89, 97,
2297 101, 103, 107, 109, 113, 127, 131, 137,
2298 139, 149, 151, 157, 163, 167, 173, 179,
2299 181, 191, 193, 197, 199, 211, 223, 227,
2300 229, 233, 239, 241, 251, 257, 263, 269,
2301 271, 277, 281, 283, 293, 307, 311, 313,
2302 317, 331, 337, 347, 349, 353, 359, 367,
2303 373, 379, 383, 389, 397, 401, 409, 419,
2304 421, 431, 433, 439, 443, 449, 457, 461,
2305 463, 467, 479, 487, 491, 499, 503, 509,
2306 521, 523, 541, 547, 557, 563, 569, 571,
2307 577, 587, 593, 599, 601, 607, 613, 617,
2308 619, 631, 641, 643, 647, 653, 659, 661,
2309 673, 677, 683, 691, 701, 709, 719, 727,
2310 733, 739, 743, 751, 757, 761, 769, 773,
2311 787, 797, 809, 811, 821, 823, 827, 829,
2312 839, 853, 857, 859, 863, 877, 881, 883,
2313 887, 907, 911, 919, 929, 937, 941, 947,
2314 953, 967, 971, 977, 983, 991, 997, -103
Paul Bakker5121ce52009-01-03 21:22:43 +00002315};
2316
2317/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002318 * Small divisors test (X must be positive)
2319 *
2320 * Return values:
2321 * 0: no small factor (possible prime, more tests needed)
2322 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002324 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002325 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002326static int mpi_check_small_factors(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +00002327{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002328 int ret = 0;
2329 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 mbedtls_mpi_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +00002331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 if ((X->p[0] & 1) == 0) {
2333 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2334 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002335
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 for (i = 0; small_prime[i] > 0; i++) {
2337 if (mbedtls_mpi_cmp_int(X, small_prime[i]) <= 0) {
2338 return 1;
2339 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, small_prime[i]));
Paul Bakker5121ce52009-01-03 21:22:43 +00002342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 if (r == 0) {
2344 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2345 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002346 }
2347
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002348cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 return ret;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002350}
2351
2352/*
2353 * Miller-Rabin pseudo-primality test (HAC 4.24)
2354 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002355static int mpi_miller_rabin(const mbedtls_mpi *X, size_t rounds,
2356 int (*f_rng)(void *, unsigned char *, size_t),
2357 void *p_rng)
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002358{
Pascal Junodb99183d2015-03-11 16:49:45 +01002359 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002360 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002362
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 MPI_VALIDATE_RET(X != NULL);
2364 MPI_VALIDATE_RET(f_rng != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00002365
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 mbedtls_mpi_init(&W); mbedtls_mpi_init(&R);
2367 mbedtls_mpi_init(&T); mbedtls_mpi_init(&A);
2368 mbedtls_mpi_init(&RR);
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002369
Paul Bakker5121ce52009-01-03 21:22:43 +00002370 /*
2371 * W = |X| - 1
2372 * R = W >> lsb( W )
2373 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&W, X, 1));
2375 s = mbedtls_mpi_lsb(&W);
2376 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R, &W));
2377 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&R, s));
Paul Bakker5121ce52009-01-03 21:22:43 +00002378
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 for (i = 0; i < rounds; i++) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 /*
2381 * pick a random A, 1 < A < |X| - 1
2382 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002383 count = 0;
2384 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&A, X->n * ciL, f_rng, p_rng));
Pascal Junodb99183d2015-03-11 16:49:45 +01002386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 j = mbedtls_mpi_bitlen(&A);
2388 k = mbedtls_mpi_bitlen(&W);
Pascal Junodb99183d2015-03-11 16:49:45 +01002389 if (j > k) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 A.p[A.n - 1] &= ((mbedtls_mpi_uint) 1 << (k - (A.n - 1) * biL - 1)) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002391 }
2392
2393 if (count++ > 30) {
Jens Wiklanderf08aa3e2019-01-17 13:30:57 +01002394 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2395 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002396 }
2397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 } while (mbedtls_mpi_cmp_mpi(&A, &W) >= 0 ||
2399 mbedtls_mpi_cmp_int(&A, 1) <= 0);
Paul Bakker5121ce52009-01-03 21:22:43 +00002400
2401 /*
2402 * A = A^R mod |X|
2403 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&A, &A, &R, X, &RR));
Paul Bakker5121ce52009-01-03 21:22:43 +00002405
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 if (mbedtls_mpi_cmp_mpi(&A, &W) == 0 ||
2407 mbedtls_mpi_cmp_int(&A, 1) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002410
2411 j = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 while (j < s && mbedtls_mpi_cmp_mpi(&A, &W) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002413 /*
2414 * A = A * A mod |X|
2415 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &A, &A));
2417 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&A, &T, X));
Paul Bakker5121ce52009-01-03 21:22:43 +00002418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 if (mbedtls_mpi_cmp_int(&A, 1) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002420 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002422
2423 j++;
2424 }
2425
2426 /*
2427 * not prime if A != |X| - 1 or A == 1
2428 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 if (mbedtls_mpi_cmp_mpi(&A, &W) != 0 ||
2430 mbedtls_mpi_cmp_int(&A, 1) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002432 break;
2433 }
2434 }
2435
2436cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 mbedtls_mpi_free(&W); mbedtls_mpi_free(&R);
2438 mbedtls_mpi_free(&T); mbedtls_mpi_free(&A);
2439 mbedtls_mpi_free(&RR);
Paul Bakker5121ce52009-01-03 21:22:43 +00002440
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002442}
2443
2444/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002445 * Pseudo-primality test: small factors, then Miller-Rabin
2446 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002447int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
2448 int (*f_rng)(void *, unsigned char *, size_t),
2449 void *p_rng)
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002450{
Janos Follath24eed8d2019-11-22 13:21:35 +00002451 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 mbedtls_mpi XX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 MPI_VALIDATE_RET(X != NULL);
2454 MPI_VALIDATE_RET(f_rng != NULL);
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002455
2456 XX.s = 1;
2457 XX.n = X->n;
2458 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002459
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 if (mbedtls_mpi_cmp_int(&XX, 0) == 0 ||
2461 mbedtls_mpi_cmp_int(&XX, 1) == 0) {
2462 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002463 }
2464
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 if (mbedtls_mpi_cmp_int(&XX, 2) == 0) {
2466 return 0;
2467 }
2468
2469 if ((ret = mpi_check_small_factors(&XX)) != 0) {
2470 if (ret == 1) {
2471 return 0;
2472 }
2473
2474 return ret;
2475 }
2476
2477 return mpi_miller_rabin(&XX, rounds, f_rng, p_rng);
Janos Follathf301d232018-08-14 13:34:01 +01002478}
2479
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002480/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002482 *
Janos Follathf301d232018-08-14 13:34:01 +01002483 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2484 * be either 1024 bits or 1536 bits long, and flags must contain
2485 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002487int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
2488 int (*f_rng)(void *, unsigned char *, size_t),
2489 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00002490{
Jethro Beekman66689272018-02-14 19:24:10 -08002491#ifdef MBEDTLS_HAVE_INT64
2492// ceil(2^63.5)
2493#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2494#else
2495// ceil(2^31.5)
2496#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2497#endif
2498 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002499 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002500 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 mbedtls_mpi_uint r;
2502 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002503
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 MPI_VALIDATE_RET(X != NULL);
2505 MPI_VALIDATE_RET(f_rng != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00002506
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 if (nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS) {
2508 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2509 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002510
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 mbedtls_mpi_init(&Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00002512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 n = BITS_TO_LIMBS(nbits);
Paul Bakker5121ce52009-01-03 21:22:43 +00002514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR) == 0) {
Janos Follathda31fa12018-09-03 14:45:23 +01002516 /*
2517 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2518 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 rounds = ((nbits >= 1300) ? 2 : (nbits >= 850) ? 3 :
2520 (nbits >= 650) ? 4 : (nbits >= 350) ? 8 :
2521 (nbits >= 250) ? 12 : (nbits >= 150) ? 18 : 27);
2522 } else {
Janos Follathda31fa12018-09-03 14:45:23 +01002523 /*
2524 * 2^-100 error probability, number of rounds computed based on HAC,
2525 * fact 4.48
2526 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 rounds = ((nbits >= 1450) ? 4 : (nbits >= 1150) ? 5 :
2528 (nbits >= 1000) ? 6 : (nbits >= 850) ? 7 :
2529 (nbits >= 750) ? 8 : (nbits >= 500) ? 13 :
2530 (nbits >= 250) ? 28 : (nbits >= 150) ? 40 : 51);
Janos Follathda31fa12018-09-03 14:45:23 +01002531 }
2532
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 while (1) {
2534 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(X, n * ciL, f_rng, p_rng));
Jethro Beekman66689272018-02-14 19:24:10 -08002535 /* 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 +01002536 if (X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2) {
2537 continue;
2538 }
Jethro Beekman66689272018-02-14 19:24:10 -08002539
2540 k = n * biL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 if (k > nbits) {
2542 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(X, k - nbits));
2543 }
Jethro Beekman66689272018-02-14 19:24:10 -08002544 X->p[0] |= 1;
2545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH) == 0) {
2547 ret = mbedtls_mpi_is_prime_ext(X, rounds, f_rng, p_rng);
Jethro Beekman66689272018-02-14 19:24:10 -08002548
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 }
2552 } else {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002553 /*
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01002554 * A necessary condition for Y and X = 2Y + 1 to be prime
Jethro Beekman66689272018-02-14 19:24:10 -08002555 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2556 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002557 */
Jethro Beekman66689272018-02-14 19:24:10 -08002558
2559 X->p[0] |= 2;
2560
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, 3));
2562 if (r == 0) {
2563 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 8));
2564 } else if (r == 1) {
2565 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 4));
2566 }
Jethro Beekman66689272018-02-14 19:24:10 -08002567
2568 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, X));
2570 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, 1));
Jethro Beekman66689272018-02-14 19:24:10 -08002571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 while (1) {
Jethro Beekman66689272018-02-14 19:24:10 -08002573 /*
2574 * First, check small factors for X and Y
2575 * before doing Miller-Rabin on any of them
2576 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 if ((ret = mpi_check_small_factors(X)) == 0 &&
2578 (ret = mpi_check_small_factors(&Y)) == 0 &&
2579 (ret = mpi_miller_rabin(X, rounds, f_rng, p_rng))
2580 == 0 &&
2581 (ret = mpi_miller_rabin(&Y, rounds, f_rng, p_rng))
2582 == 0) {
Jethro Beekman66689272018-02-14 19:24:10 -08002583 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 }
Jethro Beekman66689272018-02-14 19:24:10 -08002585
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Jethro Beekman66689272018-02-14 19:24:10 -08002587 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 }
Jethro Beekman66689272018-02-14 19:24:10 -08002589
2590 /*
2591 * Next candidates. We want to preserve Y = (X-1) / 2 and
2592 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2593 * so up Y by 6 and X by 12.
2594 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 12));
2596 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&Y, &Y, 6));
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 }
2599 }
2600
2601cleanup:
2602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 mbedtls_mpi_free(&Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00002604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002606}
2607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002611
Paul Bakker23986e52011-04-24 08:57:21 +00002612#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002613
2614static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2615{
2616 { 693, 609, 21 },
2617 { 1764, 868, 28 },
2618 { 768454923, 542167814, 1 }
2619};
2620
Paul Bakker5121ce52009-01-03 21:22:43 +00002621/*
2622 * Checkup routine
2623 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002624int mbedtls_mpi_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002625{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002626 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00002628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 mbedtls_mpi_init(&A); mbedtls_mpi_init(&E); mbedtls_mpi_init(&N); mbedtls_mpi_init(&X);
2630 mbedtls_mpi_init(&Y); mbedtls_mpi_init(&U); mbedtls_mpi_init(&V);
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&A, 16,
2633 "EFE021C2645FD1DC586E69184AF4A31E" \
2634 "D5F53E93B5F123FA41680867BA110131" \
2635 "944FE7952E2517337780CB0DB80E61AA" \
2636 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002637
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&E, 16,
2639 "B2E7EFD37075B9F03FF989C7C5051C20" \
2640 "34D2A323810251127E7BF8625A4F49A5" \
2641 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
2642 "5B5C25763222FEFCCFC38B832366C29E"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002643
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&N, 16,
2645 "0066A198186C18C10B2F5ED9B522752A" \
2646 "9830B69916E535C8F047518A889A43A5" \
2647 "94B6BED27A168D31D4A52F88925AA8F5"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002648
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&X, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002650
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2652 "602AB7ECA597A3D6B56FF9829A5E8B85" \
2653 "9E857EA95A03512E2BAE7391688D264A" \
2654 "A5663B0341DB9CCFD2C4C5F421FEC814" \
2655 "8001B72E848A38CAE1C65F78E56ABDEF" \
2656 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
2657 "ECF677152EF804370C1A305CAF3B5BF1" \
2658 "30879B56C61DE584A0F53A2447A51E"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 if (verbose != 0) {
2661 mbedtls_printf(" MPI test #1 (mul_mpi): ");
2662 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2665 if (verbose != 0) {
2666 mbedtls_printf("failed\n");
2667 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002668
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002669 ret = 1;
2670 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002671 }
2672
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 if (verbose != 0) {
2674 mbedtls_printf("passed\n");
2675 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002676
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&X, &Y, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002678
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2680 "256567336059E52CAE22925474705F39A94"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002681
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&V, 16,
2683 "6613F26162223DF488E9CD48CC132C7A" \
2684 "0AC93C701B001B092E4E5B9F73BCD27B" \
2685 "9EE50D0657C77F374E903CDFA4C642"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002686
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 if (verbose != 0) {
2688 mbedtls_printf(" MPI test #2 (div_mpi): ");
2689 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002690
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0 ||
2692 mbedtls_mpi_cmp_mpi(&Y, &V) != 0) {
2693 if (verbose != 0) {
2694 mbedtls_printf("failed\n");
2695 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002696
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002697 ret = 1;
2698 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 }
2700
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 if (verbose != 0) {
2702 mbedtls_printf("passed\n");
2703 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002704
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&X, &A, &E, &N, NULL));
Paul Bakker5121ce52009-01-03 21:22:43 +00002706
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2708 "36E139AEA55215609D2816998ED020BB" \
2709 "BD96C37890F65171D948E9BC7CBAA4D9" \
2710 "325D24D6A3C12710F10A09FA08AB87"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002711
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 if (verbose != 0) {
2713 mbedtls_printf(" MPI test #3 (exp_mod): ");
2714 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2717 if (verbose != 0) {
2718 mbedtls_printf("failed\n");
2719 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002720
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002721 ret = 1;
2722 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002723 }
2724
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 if (verbose != 0) {
2726 mbedtls_printf("passed\n");
2727 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&X, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002730
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2732 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
2733 "C3DBA76456363A10869622EAC2DD84EC" \
2734 "C5B8A74DAC4D09E03B5E0BE779F2DF61"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002735
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 if (verbose != 0) {
2737 mbedtls_printf(" MPI test #4 (inv_mod): ");
2738 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002739
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2741 if (verbose != 0) {
2742 mbedtls_printf("failed\n");
2743 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002744
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002745 ret = 1;
2746 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002747 }
2748
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 if (verbose != 0) {
2750 mbedtls_printf("passed\n");
2751 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002752
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 if (verbose != 0) {
2754 mbedtls_printf(" MPI test #5 (simple gcd): ");
2755 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002756
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 for (i = 0; i < GCD_PAIR_COUNT; i++) {
2758 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&X, gcd_pairs[i][0]));
2759 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Y, gcd_pairs[i][1]));
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002760
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&A, &X, &Y));
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002762
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 if (mbedtls_mpi_cmp_int(&A, gcd_pairs[i][2]) != 0) {
2764 if (verbose != 0) {
2765 mbedtls_printf("failed at %d\n", i);
2766 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002767
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002768 ret = 1;
2769 goto cleanup;
2770 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002771 }
2772
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 if (verbose != 0) {
2774 mbedtls_printf("passed\n");
2775 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002776
Paul Bakker5121ce52009-01-03 21:22:43 +00002777cleanup:
2778
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 if (ret != 0 && verbose != 0) {
2780 mbedtls_printf("Unexpected error, return code = %08X\n", (unsigned int) ret);
2781 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002782
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 mbedtls_mpi_free(&A); mbedtls_mpi_free(&E); mbedtls_mpi_free(&N); mbedtls_mpi_free(&X);
2784 mbedtls_mpi_free(&Y); mbedtls_mpi_free(&U); mbedtls_mpi_free(&V);
Paul Bakker5121ce52009-01-03 21:22:43 +00002785
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 if (verbose != 0) {
2787 mbedtls_printf("\n");
2788 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002789
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002791}
2792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795#endif /* MBEDTLS_BIGNUM_C */