blob: 9c19fc9fb6c72e1ccf2c9a63f7c2ce7141c5df78 [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 Rodgman1cfc43c2023-09-19 16:18:59 +010086 different_sign = mbedtls_ct_bool_ne(X_is_negative, Y_is_negative); // true if different sign
Dave Rodgman1f39f032023-08-01 09:19:16 +010087 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 Rodgman98ddc012023-08-10 12:11:31 +010097 size_t i = mbedtls_ct_size_if_else_0(X_is_negative, 1);
Dave Rodgman2c764842023-05-18 13:28:21 +010098 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
Dave Rodgman98ddc012023-08-10 12:11:31 +0100107 *ret = mbedtls_ct_uint_if_else_0(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 Rodgman98ddc012023-08-10 12:11:31 +0100142 X->p[i] = mbedtls_ct_mpi_uint_if_else_0(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 */
Tom Cosgrovebc345e82023-07-25 15:17:39 +0100184#define mbedtls_mpi_zeroize_and_free(v, n) mbedtls_zeroize_and_free(v, ciL * (n))
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500185
Paul Bakker5121ce52009-01-03 21:22:43 +0000186/*
Paul Bakker6c591fa2011-05-05 11:49:20 +0000187 * Initialize one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189void mbedtls_mpi_init(mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000190{
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 MPI_VALIDATE(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000192
Paul Bakker6c591fa2011-05-05 11:49:20 +0000193 X->s = 1;
194 X->n = 0;
195 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000196}
197
198/*
Paul Bakker6c591fa2011-05-05 11:49:20 +0000199 * Unallocate one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +0000200 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100201void mbedtls_mpi_free(mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000202{
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (X == NULL) {
Paul Bakker6c591fa2011-05-05 11:49:20 +0000204 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 if (X->p != NULL) {
Tom Cosgrove46259f62023-07-18 16:44:14 +0100208 mbedtls_mpi_zeroize_and_free(X->p, X->n);
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 }
210
Paul Bakker6c591fa2011-05-05 11:49:20 +0000211 X->s = 1;
212 X->n = 0;
213 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000214}
215
216/*
217 * Enlarge to the specified number of limbs
218 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100219int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs)
Paul Bakker5121ce52009-01-03 21:22:43 +0000220{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 mbedtls_mpi_uint *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
225 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
226 }
Paul Bakkerf9688572011-05-05 10:00:45 +0000227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 if (X->n < nblimbs) {
229 if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(nblimbs, ciL)) == NULL) {
230 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
231 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 if (X->p != NULL) {
234 memcpy(p, X->p, X->n * ciL);
Tom Cosgrove46259f62023-07-18 16:44:14 +0100235 mbedtls_mpi_zeroize_and_free(X->p, X->n);
Paul Bakker5121ce52009-01-03 21:22:43 +0000236 }
237
Gilles Peskine053022f2023-06-29 19:26:48 +0200238 /* nblimbs fits in n because we ensure that MBEDTLS_MPI_MAX_LIMBS
239 * fits, and we've checked that nblimbs <= MBEDTLS_MPI_MAX_LIMBS. */
240 X->n = (unsigned short) nblimbs;
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 X->p = p;
242 }
243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000245}
246
247/*
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100248 * Resize down as much as possible,
249 * while keeping at least the specified number of limbs
250 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100251int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs)
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100252{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_mpi_uint *p;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100254 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 MPI_VALIDATE_RET(X != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
258 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
259 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100260
Gilles Peskinee2f563e2020-01-20 21:17:43 +0100261 /* Actually resize up if there are currently fewer than nblimbs limbs. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 if (X->n <= nblimbs) {
263 return mbedtls_mpi_grow(X, nblimbs);
264 }
Gilles Peskine322752b2020-01-21 13:59:51 +0100265 /* After this point, then X->n > nblimbs and in particular X->n > 0. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 for (i = X->n - 1; i > 0; i--) {
268 if (X->p[i] != 0) {
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100269 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 }
271 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100272 i++;
273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 if (i < nblimbs) {
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100275 i = nblimbs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(i, ciL)) == NULL) {
279 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
280 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if (X->p != NULL) {
283 memcpy(p, X->p, i * ciL);
Tom Cosgrove46259f62023-07-18 16:44:14 +0100284 mbedtls_mpi_zeroize_and_free(X->p, X->n);
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100285 }
286
Gilles Peskine053022f2023-06-29 19:26:48 +0200287 /* i fits in n because we ensure that MBEDTLS_MPI_MAX_LIMBS
288 * fits, and we've checked that i <= nblimbs <= MBEDTLS_MPI_MAX_LIMBS. */
289 X->n = (unsigned short) i;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100290 X->p = p;
291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 return 0;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100293}
294
Gilles Peskineed32b572021-06-02 22:17:52 +0200295/* Resize X to have exactly n limbs and set it to 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100296static int mbedtls_mpi_resize_clear(mbedtls_mpi *X, size_t limbs)
Gilles Peskineed32b572021-06-02 22:17:52 +0200297{
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 if (limbs == 0) {
299 mbedtls_mpi_free(X);
300 return 0;
301 } else if (X->n == limbs) {
302 memset(X->p, 0, limbs * ciL);
Gilles Peskineed32b572021-06-02 22:17:52 +0200303 X->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return 0;
305 } else {
306 mbedtls_mpi_free(X);
307 return mbedtls_mpi_grow(X, limbs);
Gilles Peskineed32b572021-06-02 22:17:52 +0200308 }
309}
310
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100311/*
Gilles Peskine3da1a8f2021-06-08 23:17:42 +0200312 * Copy the contents of Y into X.
313 *
314 * This function is not constant-time. Leading zeros in Y may be removed.
315 *
316 * Ensure that X does not shrink. This is not guaranteed by the public API,
317 * but some code in the bignum module relies on this property, for example
318 * in mbedtls_mpi_exp_mod().
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100320int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000321{
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100322 int ret = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000323 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 MPI_VALIDATE_RET(X != NULL);
325 MPI_VALIDATE_RET(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 if (X == Y) {
328 return 0;
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200329 }
330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 if (Y->n == 0) {
332 if (X->n != 0) {
333 X->s = 1;
334 memset(X->p, 0, X->n * ciL);
335 }
336 return 0;
337 }
338
339 for (i = Y->n - 1; i > 0; i--) {
340 if (Y->p[i] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000341 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 }
343 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 i++;
345
346 X->s = Y->s;
347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (X->n < i) {
349 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i));
350 } else {
351 memset(X->p + i, 0, (X->n - i) * ciL);
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100352 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 memcpy(X->p, Y->p, i * ciL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000355
356cleanup:
357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000359}
360
361/*
362 * Swap the contents of X and Y
363 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100364void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000365{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 MPI_VALIDATE(X != NULL);
368 MPI_VALIDATE(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 memcpy(&T, X, sizeof(mbedtls_mpi));
371 memcpy(X, Y, sizeof(mbedtls_mpi));
372 memcpy(Y, &T, sizeof(mbedtls_mpi));
Paul Bakker5121ce52009-01-03 21:22:43 +0000373}
374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375static inline mbedtls_mpi_uint mpi_sint_abs(mbedtls_mpi_sint z)
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100376{
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 if (z >= 0) {
378 return z;
379 }
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100380 /* Take care to handle the most negative value (-2^(biL-1)) correctly.
381 * A naive -z would have undefined behavior.
382 * Write this in a way that makes popular compilers happy (GCC, Clang,
383 * MSVC). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 return (mbedtls_mpi_uint) 0 - (mbedtls_mpi_uint) z;
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100385}
386
Dave Rodgmanf3df1052023-08-09 18:55:41 +0100387/* Convert x to a sign, i.e. to 1, if x is positive, or -1, if x is negative.
388 * This looks awkward but generates smaller code than (x < 0 ? -1 : 1) */
Dave Rodgman960eca92023-08-09 20:43:18 +0100389#define TO_SIGN(x) ((((mbedtls_mpi_uint) x) >> (biL - 1)) * -2 + 1)
Dave Rodgmanf3df1052023-08-09 18:55:41 +0100390
Paul Bakker5121ce52009-01-03 21:22:43 +0000391/*
392 * Set value from integer
393 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100394int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z)
Paul Bakker5121ce52009-01-03 21:22:43 +0000395{
Janos Follath24eed8d2019-11-22 13:21:35 +0000396 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, 1));
400 memset(X->p, 0, X->n * ciL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 X->p[0] = mpi_sint_abs(z);
Dave Rodgmanf3df1052023-08-09 18:55:41 +0100403 X->s = TO_SIGN(z);
Paul Bakker5121ce52009-01-03 21:22:43 +0000404
405cleanup:
406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000408}
409
410/*
Paul Bakker2f5947e2011-05-18 15:47:11 +0000411 * Get a specific bit
412 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100413int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
Paul Bakker2f5947e2011-05-18 15:47:11 +0000414{
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 MPI_VALIDATE_RET(X != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if (X->n * biL <= pos) {
418 return 0;
419 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 return (X->p[pos / biL] >> (pos % biL)) & 0x01;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000422}
423
424/*
425 * Set a bit to a specific value of 0 or 1
426 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100427int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val)
Paul Bakker2f5947e2011-05-18 15:47:11 +0000428{
429 int ret = 0;
430 size_t off = pos / biL;
431 size_t idx = pos % biL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 MPI_VALIDATE_RET(X != NULL);
Paul Bakker2f5947e2011-05-18 15:47:11 +0000433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 if (val != 0 && val != 1) {
435 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000436 }
437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 if (X->n * biL <= pos) {
439 if (val == 0) {
440 return 0;
441 }
442
443 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, off + 1));
444 }
445
446 X->p[off] &= ~((mbedtls_mpi_uint) 0x01 << idx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 X->p[off] |= (mbedtls_mpi_uint) val << idx;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000448
449cleanup:
Paul Bakker9af723c2014-05-01 13:03:14 +0200450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 return ret;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000452}
453
454/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200455 * Return the number of less significant zero-bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100457size_t mbedtls_mpi_lsb(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000458{
Dave Rodgmanfa703e32023-08-09 18:56:07 +0100459 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 MBEDTLS_INTERNAL_VALIDATE_RET(X != NULL, 0);
Paul Bakker5121ce52009-01-03 21:22:43 +0000461
Dave Rodgmanfa703e32023-08-09 18:56:07 +0100462#if defined(__has_builtin)
463#if (MBEDTLS_MPI_UINT_MAX == UINT_MAX) && __has_builtin(__builtin_ctz)
464 #define mbedtls_mpi_uint_ctz __builtin_ctz
465#elif (MBEDTLS_MPI_UINT_MAX == ULONG_MAX) && __has_builtin(__builtin_ctzl)
466 #define mbedtls_mpi_uint_ctz __builtin_ctzl
467#elif (MBEDTLS_MPI_UINT_MAX == ULLONG_MAX) && __has_builtin(__builtin_ctzll)
468 #define mbedtls_mpi_uint_ctz __builtin_ctzll
469#endif
470#endif
471
472#if defined(mbedtls_mpi_uint_ctz)
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 for (i = 0; i < X->n; i++) {
Dave Rodgman960eca92023-08-09 20:43:18 +0100474 if (X->p[i] != 0) {
475 return i * biL + mbedtls_mpi_uint_ctz(X->p[i]);
476 }
Dave Rodgmanfa703e32023-08-09 18:56:07 +0100477 }
478#else
479 size_t count = 0;
480 for (i = 0; i < X->n; i++) {
481 for (size_t j = 0; j < biL; j++, count++) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 if (((X->p[i] >> j) & 1) != 0) {
483 return count;
484 }
485 }
486 }
Dave Rodgmanfa703e32023-08-09 18:56:07 +0100487#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000490}
491
492/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200493 * Return the number of bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000494 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100495size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000496{
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 return mbedtls_mpi_core_bitlen(X->p, X->n);
Paul Bakker5121ce52009-01-03 21:22:43 +0000498}
499
500/*
501 * Return the total size in bytes
502 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100503size_t mbedtls_mpi_size(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000504{
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 return (mbedtls_mpi_bitlen(X) + 7) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000506}
507
508/*
509 * Convert an ASCII character to digit value
510 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100511static int mpi_get_digit(mbedtls_mpi_uint *d, int radix, char c)
Paul Bakker5121ce52009-01-03 21:22:43 +0000512{
513 *d = 255;
514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 if (c >= 0x30 && c <= 0x39) {
516 *d = c - 0x30;
517 }
518 if (c >= 0x41 && c <= 0x46) {
519 *d = c - 0x37;
520 }
521 if (c >= 0x61 && c <= 0x66) {
522 *d = c - 0x57;
523 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 if (*d >= (mbedtls_mpi_uint) radix) {
526 return MBEDTLS_ERR_MPI_INVALID_CHARACTER;
527 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000530}
531
532/*
533 * Import from an ASCII string
534 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100535int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s)
Paul Bakker5121ce52009-01-03 21:22:43 +0000536{
Janos Follath24eed8d2019-11-22 13:21:35 +0000537 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000538 size_t i, j, slen, n;
Gilles Peskine80f56732021-04-03 18:26:13 +0200539 int sign = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_mpi_uint d;
541 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 MPI_VALIDATE_RET(X != NULL);
543 MPI_VALIDATE_RET(s != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if (radix < 2 || radix > 16) {
546 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskine7cba8592021-06-08 18:32:34 +0200547 }
548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 mbedtls_mpi_init(&T);
550
551 if (s[0] == 0) {
552 mbedtls_mpi_free(X);
553 return 0;
554 }
555
556 if (s[0] == '-') {
Gilles Peskine80f56732021-04-03 18:26:13 +0200557 ++s;
558 sign = -1;
559 }
560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 slen = strlen(s);
Paul Bakkerff60ee62010-03-16 21:09:09 +0000562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 if (radix == 16) {
Dave Rodgman68ef1d62023-05-18 20:49:03 +0100564 if (slen > SIZE_MAX >> 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 n = BITS_TO_LIMBS(slen << 2);
569
570 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n));
571 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
572
573 for (i = slen, j = 0; i > 0; i--, j++) {
574 MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i - 1]));
575 X->p[j / (2 * ciL)] |= d << ((j % (2 * ciL)) << 2);
576 }
577 } else {
578 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
579
580 for (i = 0; i < slen; i++) {
581 MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i]));
582 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T, X, radix));
583 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, &T, d));
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 }
585 }
586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 if (sign < 0 && mbedtls_mpi_bitlen(X) != 0) {
Gilles Peskine80f56732021-04-03 18:26:13 +0200588 X->s = -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 }
Gilles Peskine80f56732021-04-03 18:26:13 +0200590
Paul Bakker5121ce52009-01-03 21:22:43 +0000591cleanup:
592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000596}
597
598/*
Ron Eldora16fa292018-11-20 14:07:01 +0200599 * Helper to write the digits high-order first.
Paul Bakker5121ce52009-01-03 21:22:43 +0000600 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100601static int mpi_write_hlp(mbedtls_mpi *X, int radix,
602 char **p, const size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000603{
Janos Follath24eed8d2019-11-22 13:21:35 +0000604 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_mpi_uint r;
Ron Eldora16fa292018-11-20 14:07:01 +0200606 size_t length = 0;
607 char *p_end = *p + buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 do {
610 if (length >= buflen) {
611 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
Ron Eldora16fa292018-11-20 14:07:01 +0200612 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, radix));
615 MBEDTLS_MPI_CHK(mbedtls_mpi_div_int(X, NULL, X, radix));
Ron Eldora16fa292018-11-20 14:07:01 +0200616 /*
617 * Write the residue in the current position, as an ASCII character.
618 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 if (r < 0xA) {
620 *(--p_end) = (char) ('0' + r);
621 } else {
622 *(--p_end) = (char) ('A' + (r - 0xA));
623 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000624
Ron Eldora16fa292018-11-20 14:07:01 +0200625 length++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 } while (mbedtls_mpi_cmp_int(X, 0) != 0);
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 memmove(*p, p_end, length);
Ron Eldora16fa292018-11-20 14:07:01 +0200629 *p += length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000630
631cleanup:
632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000634}
635
636/*
637 * Export into an ASCII string
638 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100639int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
640 char *buf, size_t buflen, size_t *olen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000641{
Paul Bakker23986e52011-04-24 08:57:21 +0000642 int ret = 0;
643 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 MPI_VALIDATE_RET(X != NULL);
647 MPI_VALIDATE_RET(olen != NULL);
648 MPI_VALIDATE_RET(buflen == 0 || buf != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 if (radix < 2 || radix > 16) {
651 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
652 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 n = mbedtls_mpi_bitlen(X); /* Number of bits necessary to present `n`. */
655 if (radix >= 4) {
656 n >>= 1; /* Number of 4-adic digits necessary to present
Hanno Becker23cfea02019-02-04 09:45:07 +0000657 * `n`. If radix > 4, this might be a strict
658 * overapproximation of the number of
659 * radix-adic digits needed to present `n`. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 }
661 if (radix >= 16) {
662 n >>= 1; /* Number of hexadecimal digits necessary to
Hanno Becker23cfea02019-02-04 09:45:07 +0000663 * present `n`. */
664
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 }
Janos Follath80470622019-03-06 13:43:02 +0000666 n += 1; /* Terminating null byte */
Hanno Becker23cfea02019-02-04 09:45:07 +0000667 n += 1; /* Compensate for the divisions above, which round down `n`
668 * in case it's not even. */
669 n += 1; /* Potential '-'-sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 n += (n & 1); /* Make n even to have enough space for hexadecimal writing,
Hanno Becker23cfea02019-02-04 09:45:07 +0000671 * which always uses an even number of hex-digits. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000672
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 if (buflen < n) {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100674 *olen = n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000676 }
677
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100678 p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 if (X->s == -1) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000682 *p++ = '-';
Hanno Beckerc983c812019-02-01 16:41:30 +0000683 buflen--;
684 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 if (radix == 16) {
Paul Bakker23986e52011-04-24 08:57:21 +0000687 int c;
688 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 for (i = X->n, k = 0; i > 0; i--) {
691 for (j = ciL; j > 0; j--) {
692 c = (X->p[i - 1] >> ((j - 1) << 3)) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 if (c == 0 && k == 0 && (i + j) != 2) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000695 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000697
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000698 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000699 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000700 k = 1;
701 }
702 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 } else {
704 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T, X));
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000705
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if (T.s == -1) {
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000707 T.s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 MBEDTLS_MPI_CHK(mpi_write_hlp(&T, radix, &p, buflen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000711 }
712
713 *p++ = '\0';
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100714 *olen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000715
716cleanup:
717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000721}
722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000724/*
725 * Read X from an opened file
726 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100727int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin)
Paul Bakker5121ce52009-01-03 21:22:43 +0000728{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_mpi_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000730 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000731 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000732 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000733 * Buffer should have space for (short) label and decimal formatted MPI,
734 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000735 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 MPI_VALIDATE_RET(X != NULL);
739 MPI_VALIDATE_RET(fin != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if (radix < 2 || radix > 16) {
742 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
743 }
Hanno Becker73d7d792018-12-11 10:35:51 +0000744
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 memset(s, 0, sizeof(s));
746 if (fgets(s, sizeof(s) - 1, fin) == NULL) {
747 return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
748 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 slen = strlen(s);
751 if (slen == sizeof(s) - 2) {
752 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
753 }
Paul Bakkercb37aa52011-11-30 16:00:20 +0000754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 if (slen > 0 && s[slen - 1] == '\n') {
756 slen--; s[slen] = '\0';
757 }
758 if (slen > 0 && s[slen - 1] == '\r') {
759 slen--; s[slen] = '\0';
760 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000761
762 p = s + slen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 while (p-- > s) {
764 if (mpi_get_digit(&d, radix, *p) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000765 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 }
767 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 return mbedtls_mpi_read_string(X, radix, p + 1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000770}
771
772/*
773 * Write X into an opened file (or stdout if fout == NULL)
774 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100775int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE *fout)
Paul Bakker5121ce52009-01-03 21:22:43 +0000776{
Janos Follath24eed8d2019-11-22 13:21:35 +0000777 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000778 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000779 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000780 * Buffer should have space for (short) label and decimal formatted MPI,
781 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000782 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
784 MPI_VALIDATE_RET(X != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if (radix < 2 || radix > 16) {
787 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
788 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 memset(s, 0, sizeof(s));
Paul Bakker5121ce52009-01-03 21:22:43 +0000791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 MBEDTLS_MPI_CHK(mbedtls_mpi_write_string(X, radix, s, sizeof(s) - 2, &n));
Paul Bakker5121ce52009-01-03 21:22:43 +0000793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if (p == NULL) {
795 p = "";
796 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000797
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 plen = strlen(p);
799 slen = strlen(s);
Paul Bakker5121ce52009-01-03 21:22:43 +0000800 s[slen++] = '\r';
801 s[slen++] = '\n';
802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if (fout != NULL) {
804 if (fwrite(p, 1, plen, fout) != plen ||
805 fwrite(s, 1, slen, fout) != slen) {
806 return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
807 }
808 } else {
809 mbedtls_printf("%s%s", p, s);
Paul Bakker5121ce52009-01-03 21:22:43 +0000810 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000811
812cleanup:
813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000815}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000817
818/*
Janos Follatha778a942019-02-13 10:28:28 +0000819 * Import X from unsigned binary data, little endian
Przemyslaw Stekiel76960a72022-02-21 13:42:09 +0100820 *
821 * This function is guaranteed to return an MPI with exactly the necessary
822 * number of limbs (in particular, it does not skip 0s in the input).
Janos Follatha778a942019-02-13 10:28:28 +0000823 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100824int mbedtls_mpi_read_binary_le(mbedtls_mpi *X,
825 const unsigned char *buf, size_t buflen)
Janos Follatha778a942019-02-13 10:28:28 +0000826{
Janos Follath24eed8d2019-11-22 13:21:35 +0000827 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 const size_t limbs = CHARS_TO_LIMBS(buflen);
Janos Follatha778a942019-02-13 10:28:28 +0000829
830 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
Janos Follatha778a942019-02-13 10:28:28 +0000832
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_le(X->p, X->n, buf, buflen));
Janos Follatha778a942019-02-13 10:28:28 +0000834
835cleanup:
836
Janos Follath171a7ef2019-02-15 16:17:45 +0000837 /*
838 * This function is also used to import keys. However, wiping the buffers
839 * upon failure is not necessary because failure only can happen before any
840 * input is copied.
841 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 return ret;
Janos Follatha778a942019-02-13 10:28:28 +0000843}
844
845/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000846 * Import X from unsigned binary data, big endian
Przemyslaw Stekiel76960a72022-02-21 13:42:09 +0100847 *
848 * This function is guaranteed to return an MPI with exactly the necessary
849 * number of limbs (in particular, it does not skip 0s in the input).
Paul Bakker5121ce52009-01-03 21:22:43 +0000850 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100851int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000852{
Janos Follath24eed8d2019-11-22 13:21:35 +0000853 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 const size_t limbs = CHARS_TO_LIMBS(buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000855
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 MPI_VALIDATE_RET(X != NULL);
857 MPI_VALIDATE_RET(buflen == 0 || buf != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000858
Hanno Becker073c1992017-10-17 15:17:27 +0100859 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
Paul Bakker5121ce52009-01-03 21:22:43 +0000861
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_be(X->p, X->n, buf, buflen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000863
864cleanup:
865
Janos Follath171a7ef2019-02-15 16:17:45 +0000866 /*
867 * This function is also used to import keys. However, wiping the buffers
868 * upon failure is not necessary because failure only can happen before any
869 * input is copied.
870 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000872}
873
874/*
Janos Follathe344d0f2019-02-19 16:17:40 +0000875 * Export X into unsigned binary data, little endian
876 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100877int mbedtls_mpi_write_binary_le(const mbedtls_mpi *X,
878 unsigned char *buf, size_t buflen)
Janos Follathe344d0f2019-02-19 16:17:40 +0000879{
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 return mbedtls_mpi_core_write_le(X->p, X->n, buf, buflen);
Janos Follathe344d0f2019-02-19 16:17:40 +0000881}
882
883/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000884 * Export X into unsigned binary data, big endian
885 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100886int mbedtls_mpi_write_binary(const mbedtls_mpi *X,
887 unsigned char *buf, size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000888{
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 return mbedtls_mpi_core_write_be(X->p, X->n, buf, buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000890}
891
892/*
893 * Left-shift: X <<= count
894 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100895int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
Paul Bakker5121ce52009-01-03 21:22:43 +0000896{
Janos Follath24eed8d2019-11-22 13:21:35 +0000897 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Minos Galanakis0144b352023-05-02 14:02:32 +0100898 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 i = mbedtls_mpi_bitlen(X) + count;
Paul Bakker5121ce52009-01-03 21:22:43 +0000902
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 if (X->n * biL < i) {
904 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, BITS_TO_LIMBS(i)));
905 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000906
907 ret = 0;
908
Minos Galanakis0144b352023-05-02 14:02:32 +0100909 mbedtls_mpi_core_shift_l(X->p, X->n, count);
Paul Bakker5121ce52009-01-03 21:22:43 +0000910cleanup:
911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000913}
914
915/*
916 * Right-shift: X >>= count
917 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100918int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
Paul Bakker5121ce52009-01-03 21:22:43 +0000919{
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 MPI_VALIDATE_RET(X != NULL);
921 if (X->n != 0) {
922 mbedtls_mpi_core_shift_r(X->p, X->n, count);
923 }
924 return 0;
Gilles Peskine66414202022-09-21 15:36:16 +0200925}
926
Paul Bakker5121ce52009-01-03 21:22:43 +0000927/*
928 * Compare unsigned values
929 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100930int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000931{
Paul Bakker23986e52011-04-24 08:57:21 +0000932 size_t i, j;
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 MPI_VALIDATE_RET(X != NULL);
934 MPI_VALIDATE_RET(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000935
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 for (i = X->n; i > 0; i--) {
937 if (X->p[i - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000938 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000940 }
941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 for (j = Y->n; j > 0; j--) {
943 if (Y->p[j - 1] != 0) {
944 break;
945 }
946 }
947
Dave Rodgmanebcd7852023-08-09 18:56:42 +0100948 /* If i == j == 0, i.e. abs(X) == abs(Y),
949 * we end up returning 0 at the end of the function. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100950
951 if (i > j) {
952 return 1;
953 }
954 if (j > i) {
955 return -1;
956 }
957
958 for (; i > 0; i--) {
959 if (X->p[i - 1] > Y->p[i - 1]) {
960 return 1;
961 }
962 if (X->p[i - 1] < Y->p[i - 1]) {
963 return -1;
964 }
965 }
966
967 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000968}
969
970/*
971 * Compare signed values
972 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100973int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000974{
Paul Bakker23986e52011-04-24 08:57:21 +0000975 size_t i, j;
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 MPI_VALIDATE_RET(X != NULL);
977 MPI_VALIDATE_RET(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 for (i = X->n; i > 0; i--) {
980 if (X->p[i - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000981 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000983 }
984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 for (j = Y->n; j > 0; j--) {
986 if (Y->p[j - 1] != 0) {
987 break;
988 }
989 }
990
991 if (i == 0 && j == 0) {
992 return 0;
993 }
994
995 if (i > j) {
996 return X->s;
997 }
998 if (j > i) {
999 return -Y->s;
1000 }
1001
1002 if (X->s > 0 && Y->s < 0) {
1003 return 1;
1004 }
1005 if (Y->s > 0 && X->s < 0) {
1006 return -1;
1007 }
1008
1009 for (; i > 0; i--) {
1010 if (X->p[i - 1] > Y->p[i - 1]) {
1011 return X->s;
1012 }
1013 if (X->p[i - 1] < Y->p[i - 1]) {
1014 return -X->s;
1015 }
1016 }
1017
1018 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001019}
1020
Janos Follathee6abce2019-09-05 14:47:19 +01001021/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001022 * Compare signed values
1023 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001024int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
Paul Bakker5121ce52009-01-03 21:22:43 +00001025{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 mbedtls_mpi Y;
1027 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 *p = mpi_sint_abs(z);
Dave Rodgmanf3df1052023-08-09 18:55:41 +01001031 Y.s = TO_SIGN(z);
Paul Bakker5121ce52009-01-03 21:22:43 +00001032 Y.n = 1;
1033 Y.p = p;
1034
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 return mbedtls_mpi_cmp_mpi(X, &Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00001036}
1037
1038/*
1039 * Unsigned addition: X = |A| + |B| (HAC 14.7)
1040 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001041int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001042{
Janos Follath24eed8d2019-11-22 13:21:35 +00001043 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001044 size_t j;
Agathiyan Bragadeeshc99840a2023-07-12 11:15:46 +01001045 mbedtls_mpi_uint *p;
1046 mbedtls_mpi_uint c;
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 MPI_VALIDATE_RET(X != NULL);
1048 MPI_VALIDATE_RET(A != NULL);
1049 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 if (X == B) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 const mbedtls_mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001053 }
1054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if (X != A) {
1056 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
1057 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001058
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001059 /*
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001060 * X must always be positive as a result of unsigned additions.
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001061 */
1062 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001063
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 for (j = B->n; j > 0; j--) {
1065 if (B->p[j - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001066 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 }
1068 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001069
Gilles Peskinedb14a9d2022-11-15 22:59:00 +01001070 /* Exit early to avoid undefined behavior on NULL+0 when X->n == 0
1071 * and B is 0 (of any size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if (j == 0) {
1073 return 0;
1074 }
Gilles Peskinedb14a9d2022-11-15 22:59:00 +01001075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j));
Paul Bakker5121ce52009-01-03 21:22:43 +00001077
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001078 /* j is the number of non-zero limbs of B. Add those to X. */
Paul Bakker5121ce52009-01-03 21:22:43 +00001079
Agathiyan Bragadeeshc99840a2023-07-12 11:15:46 +01001080 p = X->p;
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001081
Agathiyan Bragadeeshc99840a2023-07-12 11:15:46 +01001082 c = mbedtls_mpi_core_add(p, p, B->p, j);
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001083
1084 p += j;
1085
1086 /* Now propagate any carry */
Paul Bakker5121ce52009-01-03 21:22:43 +00001087
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 while (c != 0) {
1089 if (j >= X->n) {
1090 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j + 1));
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001091 p = X->p + j;
Paul Bakker5121ce52009-01-03 21:22:43 +00001092 }
1093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 *p += c; c = (*p < c); j++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001095 }
1096
1097cleanup:
1098
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001100}
1101
Paul Bakker5121ce52009-01-03 21:22:43 +00001102/*
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001103 * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001105int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001106{
Janos Follath24eed8d2019-11-22 13:21:35 +00001107 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001108 size_t n;
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001109 mbedtls_mpi_uint carry;
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 MPI_VALIDATE_RET(X != NULL);
1111 MPI_VALIDATE_RET(A != NULL);
1112 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 for (n = B->n; n > 0; n--) {
1115 if (B->p[n - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001116 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 }
1118 }
1119 if (n > A->n) {
Gilles Peskinec8a91772021-01-27 22:30:43 +01001120 /* B >= (2^ciL)^n > A */
1121 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1122 goto cleanup;
1123 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, A->n));
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001126
1127 /* Set the high limbs of X to match A. Don't touch the lower limbs
1128 * because X might be aliased to B, and we must not overwrite the
1129 * significant digits of B. */
Aaron M. Uckoaf67d2c2023-01-17 11:52:22 -05001130 if (A->n > n && A != X) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 memcpy(X->p + n, A->p + n, (A->n - n) * ciL);
1132 }
1133 if (X->n > A->n) {
1134 memset(X->p + A->n, 0, (X->n - A->n) * ciL);
1135 }
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 carry = mbedtls_mpi_core_sub(X->p, A->p, B->p, n);
1138 if (carry != 0) {
Tom Cosgrove452c99c2022-08-25 10:07:07 +01001139 /* Propagate the carry through the rest of X. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 carry = mbedtls_mpi_core_sub_int(X->p + n, X->p + n, carry, X->n - n);
Tom Cosgrove452c99c2022-08-25 10:07:07 +01001141
1142 /* If we have further carry/borrow, the result is negative. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 if (carry != 0) {
Gilles Peskine89b41302020-07-23 01:16:46 +02001144 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1145 goto cleanup;
1146 }
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001147 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001148
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001149 /* X should always be positive as a result of unsigned subtractions. */
1150 X->s = 1;
1151
Paul Bakker5121ce52009-01-03 21:22:43 +00001152cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001154}
1155
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001156/* Common function for signed addition and subtraction.
1157 * Calculate A + B * flip_B where flip_B is 1 or -1.
Paul Bakker5121ce52009-01-03 21:22:43 +00001158 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001159static int add_sub_mpi(mbedtls_mpi *X,
1160 const mbedtls_mpi *A, const mbedtls_mpi *B,
1161 int flip_B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001162{
Hanno Becker73d7d792018-12-11 10:35:51 +00001163 int ret, s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 MPI_VALIDATE_RET(X != NULL);
1165 MPI_VALIDATE_RET(A != NULL);
1166 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001167
Hanno Becker73d7d792018-12-11 10:35:51 +00001168 s = A->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 if (A->s * B->s * flip_B < 0) {
1170 int cmp = mbedtls_mpi_cmp_abs(A, B);
1171 if (cmp >= 0) {
1172 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, A, B));
Gilles Peskine4a768dd2022-11-09 22:02:16 +01001173 /* If |A| = |B|, the result is 0 and we must set the sign bit
1174 * to +1 regardless of which of A or B was negative. Otherwise,
1175 * since |A| > |B|, the sign is the sign of A. */
1176 X->s = cmp == 0 ? 1 : s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 } else {
1178 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, B, A));
Gilles Peskine4a768dd2022-11-09 22:02:16 +01001179 /* Since |A| < |B|, the sign is the opposite of A. */
Paul Bakker5121ce52009-01-03 21:22:43 +00001180 X->s = -s;
1181 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 } else {
1183 MBEDTLS_MPI_CHK(mbedtls_mpi_add_abs(X, A, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 X->s = s;
1185 }
1186
1187cleanup:
1188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001190}
1191
1192/*
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001193 * Signed addition: X = A + B
1194 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001195int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001196{
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 return add_sub_mpi(X, A, B, 1);
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001198}
1199
1200/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001201 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001202 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001203int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001204{
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 return add_sub_mpi(X, A, B, -1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001206}
1207
1208/*
1209 * Signed addition: X = A + b
1210 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001211int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001212{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001213 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 MPI_VALIDATE_RET(X != NULL);
1216 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 p[0] = mpi_sint_abs(b);
Dave Rodgmanf3df1052023-08-09 18:55:41 +01001219 B.s = TO_SIGN(b);
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001220 B.n = 1;
1221 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 return mbedtls_mpi_add_mpi(X, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001224}
1225
1226/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001227 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001228 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001229int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001230{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001231 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 MPI_VALIDATE_RET(X != NULL);
1234 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001235
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 p[0] = mpi_sint_abs(b);
Dave Rodgmanf3df1052023-08-09 18:55:41 +01001237 B.s = TO_SIGN(b);
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001238 B.n = 1;
1239 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001240
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 return mbedtls_mpi_sub_mpi(X, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001242}
1243
Paul Bakker5121ce52009-01-03 21:22:43 +00001244/*
1245 * Baseline multiplication: X = A * B (HAC 14.12)
1246 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001247int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001248{
Janos Follath24eed8d2019-11-22 13:21:35 +00001249 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker1772e052022-04-13 06:51:40 +01001250 size_t i, j;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 mbedtls_mpi TA, TB;
Hanno Beckerda763de2022-04-13 06:50:02 +01001252 int result_is_zero = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 MPI_VALIDATE_RET(X != NULL);
1254 MPI_VALIDATE_RET(A != NULL);
1255 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001256
Tom Cosgrove6af26f32022-08-24 16:40:55 +01001257 mbedtls_mpi_init(&TA);
1258 mbedtls_mpi_init(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00001259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 if (X == A) {
1261 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A)); A = &TA;
1262 }
1263 if (X == B) {
1264 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B)); B = &TB;
1265 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001266
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 for (i = A->n; i > 0; i--) {
1268 if (A->p[i - 1] != 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001269 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 }
1271 }
1272 if (i == 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001273 result_is_zero = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 }
Hanno Beckerda763de2022-04-13 06:50:02 +01001275
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 for (j = B->n; j > 0; j--) {
1277 if (B->p[j - 1] != 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001278 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 }
1280 }
1281 if (j == 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001282 result_is_zero = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 }
Hanno Beckerda763de2022-04-13 06:50:02 +01001284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i + j));
1286 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
Paul Bakker5121ce52009-01-03 21:22:43 +00001287
Tom Cosgrove6af26f32022-08-24 16:40:55 +01001288 mbedtls_mpi_core_mul(X->p, A->p, i, B->p, j);
Paul Bakker5121ce52009-01-03 21:22:43 +00001289
Hanno Beckerda763de2022-04-13 06:50:02 +01001290 /* If the result is 0, we don't shortcut the operation, which reduces
1291 * but does not eliminate side channels leaking the zero-ness. We do
1292 * need to take care to set the sign bit properly since the library does
1293 * not fully support an MPI object with a value of 0 and s == -1. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 if (result_is_zero) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001295 X->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 } else {
Hanno Beckerda763de2022-04-13 06:50:02 +01001297 X->s = A->s * B->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001299
1300cleanup:
1301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TA);
Paul Bakker5121ce52009-01-03 21:22:43 +00001303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001305}
1306
1307/*
1308 * Baseline multiplication: X = A * b
1309 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001310int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001311{
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 MPI_VALIDATE_RET(X != NULL);
1313 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001314
Hanno Becker35771312022-04-14 11:52:11 +01001315 size_t n = A->n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 while (n > 0 && A->p[n - 1] == 0) {
Hanno Becker35771312022-04-14 11:52:11 +01001317 --n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 }
Hanno Becker35771312022-04-14 11:52:11 +01001319
Hanno Becker74a11a32022-04-06 06:27:00 +01001320 /* The general method below doesn't work if b==0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 if (b == 0 || n == 0) {
1322 return mbedtls_mpi_lset(X, 0);
1323 }
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001324
Hanno Beckeraef9cc42022-04-11 06:36:29 +01001325 /* Calculate A*b as A + A*(b-1) to take advantage of mbedtls_mpi_core_mla */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001326 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001327 /* In general, A * b requires 1 limb more than b. If
1328 * A->p[n - 1] * b / b == A->p[n - 1], then A * b fits in the same
1329 * number of limbs as A and the call to grow() is not required since
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001330 * copy() will take care of the growth if needed. However, experimentally,
1331 * making the call to grow() unconditional causes slightly fewer
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001332 * calls to calloc() in ECP code, presumably because it reuses the
1333 * same mpi for a while and this way the mpi is more likely to directly
Hanno Becker9137b9c2022-04-12 10:51:54 +01001334 * grow to its final size.
1335 *
1336 * Note that calculating A*b as 0 + A*b doesn't work as-is because
1337 * A,X can be the same. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n + 1));
1339 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
1340 mbedtls_mpi_core_mla(X->p, X->n, A->p, n, b - 1);
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001341
1342cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001344}
1345
1346/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001347 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1348 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001349 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001350static mbedtls_mpi_uint mbedtls_int_div_int(mbedtls_mpi_uint u1,
1351 mbedtls_mpi_uint u0,
1352 mbedtls_mpi_uint d,
1353 mbedtls_mpi_uint *r)
Simon Butcher15b15d12015-11-26 19:35:03 +00001354{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001355#if defined(MBEDTLS_HAVE_UDBL)
1356 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001357#else
Simon Butcher9803d072016-01-03 00:24:34 +00001358 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 const mbedtls_mpi_uint uint_halfword_mask = ((mbedtls_mpi_uint) 1 << biH) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001360 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1361 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001362 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001363#endif
1364
Simon Butcher15b15d12015-11-26 19:35:03 +00001365 /*
1366 * Check for overflow
1367 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 if (0 == d || u1 >= d) {
1369 if (r != NULL) {
1370 *r = ~(mbedtls_mpi_uint) 0u;
1371 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001372
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 return ~(mbedtls_mpi_uint) 0u;
Simon Butcher15b15d12015-11-26 19:35:03 +00001374 }
1375
1376#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001377 dividend = (mbedtls_t_udbl) u1 << biL;
1378 dividend |= (mbedtls_t_udbl) u0;
1379 quotient = dividend / d;
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 if (quotient > ((mbedtls_t_udbl) 1 << biL) - 1) {
1381 quotient = ((mbedtls_t_udbl) 1 << biL) - 1;
1382 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 if (r != NULL) {
1385 *r = (mbedtls_mpi_uint) (dividend - (quotient * d));
1386 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001387
1388 return (mbedtls_mpi_uint) quotient;
1389#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001390
1391 /*
1392 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1393 * Vol. 2 - Seminumerical Algorithms, Knuth
1394 */
1395
1396 /*
1397 * Normalize the divisor, d, and dividend, u0, u1
1398 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 s = mbedtls_mpi_core_clz(d);
Simon Butcher15b15d12015-11-26 19:35:03 +00001400 d = d << s;
1401
1402 u1 = u1 << s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 u1 |= (u0 >> (biL - s)) & (-(mbedtls_mpi_sint) s >> (biL - 1));
Simon Butcher15b15d12015-11-26 19:35:03 +00001404 u0 = u0 << s;
1405
1406 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001407 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001408
1409 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001410 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001411
1412 /*
1413 * Find the first quotient and remainder
1414 */
1415 q1 = u1 / d1;
1416 r0 = u1 - d1 * q1;
1417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 while (q1 >= radix || (q1 * d0 > radix * r0 + u0_msw)) {
Simon Butcher15b15d12015-11-26 19:35:03 +00001419 q1 -= 1;
1420 r0 += d1;
1421
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 if (r0 >= radix) {
1423 break;
1424 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001425 }
1426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 rAX = (u1 * radix) + (u0_msw - q1 * d);
Simon Butcher15b15d12015-11-26 19:35:03 +00001428 q0 = rAX / d1;
1429 r0 = rAX - q0 * d1;
1430
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 while (q0 >= radix || (q0 * d0 > radix * r0 + u0_lsw)) {
Simon Butcher15b15d12015-11-26 19:35:03 +00001432 q0 -= 1;
1433 r0 += d1;
1434
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 if (r0 >= radix) {
1436 break;
1437 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001438 }
1439
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 if (r != NULL) {
1441 *r = (rAX * radix + u0_lsw - q0 * d) >> s;
1442 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001443
1444 quotient = q1 * radix + q0;
1445
1446 return quotient;
1447#endif
1448}
1449
1450/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001452 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001453int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1454 const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001455{
Janos Follath24eed8d2019-11-22 13:21:35 +00001456 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001457 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 mbedtls_mpi X, Y, Z, T1, T2;
Alexander Kd19a1932019-11-01 18:20:42 +03001459 mbedtls_mpi_uint TP2[3];
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 MPI_VALIDATE_RET(A != NULL);
1461 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 if (mbedtls_mpi_cmp_int(B, 0) == 0) {
1464 return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
1465 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001466
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z);
1468 mbedtls_mpi_init(&T1);
Alexander Kd19a1932019-11-01 18:20:42 +03001469 /*
1470 * Avoid dynamic memory allocations for constant-size T2.
1471 *
1472 * T2 is used for comparison only and the 3 limbs are assigned explicitly,
1473 * so nobody increase the size of the MPI and we're safe to use an on-stack
1474 * buffer.
1475 */
Alexander K35d6d462019-10-31 14:46:45 +03001476 T2.s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 T2.n = sizeof(TP2) / sizeof(*TP2);
Alexander Kd19a1932019-11-01 18:20:42 +03001478 T2.p = TP2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 if (mbedtls_mpi_cmp_abs(A, B) < 0) {
1481 if (Q != NULL) {
1482 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(Q, 0));
1483 }
1484 if (R != NULL) {
1485 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, A));
1486 }
1487 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001488 }
1489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&X, A));
1491 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 X.s = Y.s = 1;
1493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&Z, A->n + 2));
1495 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Z, 0));
1496 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T1, A->n + 2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001497
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 k = mbedtls_mpi_bitlen(&Y) % biL;
1499 if (k < biL - 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001500 k = biL - 1 - k;
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&X, k));
1502 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, k));
1503 } else {
1504 k = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001505 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001506
1507 n = X.n - 1;
1508 t = Y.n - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, biL * (n - t)));
Paul Bakker5121ce52009-01-03 21:22:43 +00001510
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 while (mbedtls_mpi_cmp_mpi(&X, &Y) >= 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001512 Z.p[n - t]++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &Y));
Paul Bakker5121ce52009-01-03 21:22:43 +00001514 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, biL * (n - t)));
Paul Bakker5121ce52009-01-03 21:22:43 +00001516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 for (i = n; i > t; i--) {
1518 if (X.p[i] >= Y.p[t]) {
1519 Z.p[i - t - 1] = ~(mbedtls_mpi_uint) 0u;
1520 } else {
1521 Z.p[i - t - 1] = mbedtls_int_div_int(X.p[i], X.p[i - 1],
1522 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001523 }
1524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 T2.p[0] = (i < 2) ? 0 : X.p[i - 2];
1526 T2.p[1] = (i < 1) ? 0 : X.p[i - 1];
Alexander K35d6d462019-10-31 14:46:45 +03001527 T2.p[2] = X.p[i];
1528
Paul Bakker5121ce52009-01-03 21:22:43 +00001529 Z.p[i - t - 1]++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 do {
Paul Bakker5121ce52009-01-03 21:22:43 +00001531 Z.p[i - t - 1]--;
1532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&T1, 0));
1534 T1.p[0] = (t < 1) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001535 T1.p[1] = Y.p[t];
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &T1, Z.p[i - t - 1]));
1537 } while (mbedtls_mpi_cmp_mpi(&T1, &T2) > 0);
Paul Bakker5121ce52009-01-03 21:22:43 +00001538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &Y, Z.p[i - t - 1]));
1540 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
1541 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &T1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001542
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 if (mbedtls_mpi_cmp_int(&X, 0) < 0) {
1544 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T1, &Y));
1545 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
1546 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&X, &X, &T1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001547 Z.p[i - t - 1]--;
1548 }
1549 }
1550
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 if (Q != NULL) {
1552 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(Q, &Z));
Paul Bakker5121ce52009-01-03 21:22:43 +00001553 Q->s = A->s * B->s;
1554 }
1555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 if (R != NULL) {
1557 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&X, k));
Paul Bakkerf02c5642012-11-13 10:25:21 +00001558 X.s = A->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, &X));
Paul Bakker5121ce52009-01-03 21:22:43 +00001560
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 if (mbedtls_mpi_cmp_int(R, 0) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001562 R->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001564 }
1565
1566cleanup:
1567
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z);
1569 mbedtls_mpi_free(&T1);
1570 mbedtls_platform_zeroize(TP2, sizeof(TP2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001571
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001573}
1574
1575/*
1576 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00001577 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001578int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R,
1579 const mbedtls_mpi *A,
1580 mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001581{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001582 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001585
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 p[0] = mpi_sint_abs(b);
Dave Rodgmanf3df1052023-08-09 18:55:41 +01001587 B.s = TO_SIGN(b);
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001588 B.n = 1;
1589 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 return mbedtls_mpi_div_mpi(Q, R, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001592}
1593
1594/*
1595 * Modulo: R = A mod B
1596 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001597int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001598{
Janos Follath24eed8d2019-11-22 13:21:35 +00001599 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 MPI_VALIDATE_RET(R != NULL);
1601 MPI_VALIDATE_RET(A != NULL);
1602 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001603
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 if (mbedtls_mpi_cmp_int(B, 0) < 0) {
1605 return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1606 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001607
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(NULL, R, A, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001609
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 while (mbedtls_mpi_cmp_int(R, 0) < 0) {
1611 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(R, R, B));
1612 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 while (mbedtls_mpi_cmp_mpi(R, B) >= 0) {
1615 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(R, R, B));
1616 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001617
1618cleanup:
1619
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001621}
1622
1623/*
1624 * Modulo: r = A mod b
1625 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001626int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001627{
Paul Bakker23986e52011-04-24 08:57:21 +00001628 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 mbedtls_mpi_uint x, y, z;
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 MPI_VALIDATE_RET(r != NULL);
1631 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 if (b == 0) {
1634 return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
1635 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001636
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 if (b < 0) {
1638 return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1639 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001640
1641 /*
1642 * handle trivial cases
1643 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 if (b == 1 || A->n == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001645 *r = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001647 }
1648
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 if (b == 2) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001650 *r = A->p[0] & 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001652 }
1653
1654 /*
1655 * general case
1656 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 for (i = A->n, y = 0; i > 0; i--) {
Paul Bakker23986e52011-04-24 08:57:21 +00001658 x = A->p[i - 1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 y = (y << biH) | (x >> biH);
Paul Bakker5121ce52009-01-03 21:22:43 +00001660 z = y / b;
1661 y -= z * b;
1662
1663 x <<= biH;
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 y = (y << biH) | (x >> biH);
Paul Bakker5121ce52009-01-03 21:22:43 +00001665 z = y / b;
1666 y -= z * b;
1667 }
1668
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001669 /*
1670 * If A is negative, then the current y represents a negative value.
1671 * Flipping it to the positive side.
1672 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 if (A->s < 0 && y != 0) {
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001674 y = b - y;
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001676
Paul Bakker5121ce52009-01-03 21:22:43 +00001677 *r = y;
1678
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001680}
1681
Gilles Peskine449bd832023-01-11 14:50:10 +01001682static void mpi_montg_init(mbedtls_mpi_uint *mm, const mbedtls_mpi *N)
Paul Bakker5121ce52009-01-03 21:22:43 +00001683{
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 *mm = mbedtls_mpi_core_montmul_init(N->p);
Paul Bakker5121ce52009-01-03 21:22:43 +00001685}
1686
Tom Cosgrove93842842022-08-05 16:59:43 +01001687/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
1688 *
1689 * \param[in,out] A One of the numbers to multiply.
1690 * It must have at least as many limbs as N
1691 * (A->n >= N->n), and any limbs beyond n are ignored.
1692 * On successful completion, A contains the result of
1693 * the multiplication A * B * R^-1 mod N where
1694 * R = (2^ciL)^n.
1695 * \param[in] B One of the numbers to multiply.
1696 * It must be nonzero and must not have more limbs than N
1697 * (B->n <= N->n).
Tom Cosgrove40d22942022-08-17 06:42:44 +01001698 * \param[in] N The modulus. \p N must be odd.
Tom Cosgrove93842842022-08-05 16:59:43 +01001699 * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
1700 * This is -N^-1 mod 2^ciL.
1701 * \param[in,out] T A bignum for temporary storage.
1702 * It must be at least twice the limb size of N plus 1
1703 * (T->n >= 2 * N->n + 1).
1704 * Its initial content is unused and
1705 * its final content is indeterminate.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +01001706 * It does not get reallocated.
Tom Cosgrove93842842022-08-05 16:59:43 +01001707 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001708static void mpi_montmul(mbedtls_mpi *A, const mbedtls_mpi *B,
1709 const mbedtls_mpi *N, mbedtls_mpi_uint mm,
1710 mbedtls_mpi *T)
Paul Bakker5121ce52009-01-03 21:22:43 +00001711{
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 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 +00001713}
1714
1715/*
1716 * Montgomery reduction: A = A * R^-1 mod N
Gilles Peskine2a82f722020-06-04 15:00:49 +02001717 *
Tom Cosgrove93842842022-08-05 16:59:43 +01001718 * See mpi_montmul() regarding constraints and guarantees on the parameters.
Paul Bakker5121ce52009-01-03 21:22:43 +00001719 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001720static void mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N,
1721 mbedtls_mpi_uint mm, mbedtls_mpi *T)
Paul Bakker5121ce52009-01-03 21:22:43 +00001722{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 mbedtls_mpi_uint z = 1;
1724 mbedtls_mpi U;
Gilles Peskine053022f2023-06-29 19:26:48 +02001725 U.n = 1;
1726 U.s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001727 U.p = &z;
1728
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 mpi_montmul(A, &U, N, mm, T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001730}
1731
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001732/**
1733 * Select an MPI from a table without leaking the index.
1734 *
1735 * This is functionally equivalent to mbedtls_mpi_copy(R, T[idx]) except it
1736 * reads the entire table in order to avoid leaking the value of idx to an
1737 * attacker able to observe memory access patterns.
1738 *
1739 * \param[out] R Where to write the selected MPI.
1740 * \param[in] T The table to read from.
1741 * \param[in] T_size The number of elements in the table.
1742 * \param[in] idx The index of the element to select;
1743 * this must satisfy 0 <= idx < T_size.
1744 *
1745 * \return \c 0 on success, or a negative error code.
1746 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001747static 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 +01001748{
1749 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1750
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 for (size_t i = 0; i < T_size; i++) {
1752 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(R, &T[i],
Dave Rodgmanb7825ce2023-08-10 11:58:18 +01001753 (unsigned char) mbedtls_ct_uint_eq(i, idx)));
Manuel Pégourié-Gonnard92413ef2021-06-03 10:42:46 +02001754 }
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001755cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 return ret;
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001757}
1758
Paul Bakker5121ce52009-01-03 21:22:43 +00001759/*
1760 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
1761 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001762int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
1763 const mbedtls_mpi *E, const mbedtls_mpi *N,
1764 mbedtls_mpi *prec_RR)
Paul Bakker5121ce52009-01-03 21:22:43 +00001765{
Janos Follath24eed8d2019-11-22 13:21:35 +00001766 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath74601202022-11-21 15:54:20 +00001767 size_t window_bitsize;
Paul Bakker23986e52011-04-24 08:57:21 +00001768 size_t i, j, nblimbs;
1769 size_t bufsize, nbits;
Paul Elliott1748de12023-02-13 15:35:35 +00001770 size_t exponent_bits_in_window = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 mbedtls_mpi_uint ei, mm, state;
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 mbedtls_mpi RR, T, W[(size_t) 1 << MBEDTLS_MPI_WINDOW_SIZE], WW, Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00001773 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 MPI_VALIDATE_RET(X != NULL);
1776 MPI_VALIDATE_RET(A != NULL);
1777 MPI_VALIDATE_RET(E != NULL);
1778 MPI_VALIDATE_RET(N != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00001779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 if (mbedtls_mpi_cmp_int(N, 0) <= 0 || (N->p[0] & 1) == 0) {
1781 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1782 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001783
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 if (mbedtls_mpi_cmp_int(E, 0) < 0) {
1785 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1786 }
Paul Bakkerf6198c12012-05-16 08:02:29 +00001787
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 if (mbedtls_mpi_bitlen(E) > MBEDTLS_MPI_MAX_BITS ||
1789 mbedtls_mpi_bitlen(N) > MBEDTLS_MPI_MAX_BITS) {
1790 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1791 }
Chris Jones9246d042020-11-25 15:12:39 +00001792
Paul Bakkerf6198c12012-05-16 08:02:29 +00001793 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001794 * Init temps and window size
1795 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 mpi_montg_init(&mm, N);
1797 mbedtls_mpi_init(&RR); mbedtls_mpi_init(&T);
1798 mbedtls_mpi_init(&Apos);
1799 mbedtls_mpi_init(&WW);
1800 memset(W, 0, sizeof(W));
Paul Bakker5121ce52009-01-03 21:22:43 +00001801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 i = mbedtls_mpi_bitlen(E);
Paul Bakker5121ce52009-01-03 21:22:43 +00001803
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 window_bitsize = (i > 671) ? 6 : (i > 239) ? 5 :
1805 (i > 79) ? 4 : (i > 23) ? 3 : 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001806
Gilles Peskine449bd832023-01-11 14:50:10 +01001807#if (MBEDTLS_MPI_WINDOW_SIZE < 6)
1808 if (window_bitsize > MBEDTLS_MPI_WINDOW_SIZE) {
Janos Follath7fa11b82022-11-21 14:48:02 +00001809 window_bitsize = MBEDTLS_MPI_WINDOW_SIZE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 }
Peter Kolbuse6bcad32018-12-11 14:01:44 -06001811#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00001812
Janos Follathc8d66d52022-11-22 10:47:10 +00001813 const size_t w_table_used_size = (size_t) 1 << window_bitsize;
Janos Follath06000952022-11-22 10:18:06 +00001814
Paul Bakker5121ce52009-01-03 21:22:43 +00001815 /*
Janos Follathbe54ca72022-11-21 16:14:54 +00001816 * This function is not constant-trace: its memory accesses depend on the
1817 * exponent value. To defend against timing attacks, callers (such as RSA
1818 * and DHM) should use exponent blinding. However this is not enough if the
1819 * adversary can find the exponent in a single trace, so this function
1820 * takes extra precautions against adversaries who can observe memory
1821 * access patterns.
Janos Follathf08b40e2022-11-11 15:56:38 +00001822 *
Janos Follathbe54ca72022-11-21 16:14:54 +00001823 * This function performs a series of multiplications by table elements and
1824 * squarings, and we want the prevent the adversary from finding out which
1825 * table element was used, and from distinguishing between multiplications
1826 * and squarings. Firstly, when multiplying by an element of the window
1827 * W[i], we do a constant-trace table lookup to obfuscate i. This leaves
1828 * squarings as having a different memory access patterns from other
Gilles Peskinee6cb45e2023-08-10 15:59:28 +02001829 * multiplications. So secondly, we put the accumulator in the table as
1830 * well, and also do a constant-trace table lookup to multiply by the
1831 * accumulator which is W[x_index].
Janos Follathbe54ca72022-11-21 16:14:54 +00001832 *
1833 * This way, all multiplications take the form of a lookup-and-multiply.
1834 * The number of lookup-and-multiply operations inside each iteration of
1835 * the main loop still depends on the bits of the exponent, but since the
1836 * other operations in the loop don't have an easily recognizable memory
1837 * trace, an adversary is unlikely to be able to observe the exact
1838 * patterns.
1839 *
1840 * An adversary may still be able to recover the exponent if they can
1841 * observe both memory accesses and branches. However, branch prediction
1842 * exploitation typically requires many traces of execution over the same
1843 * data, which is defeated by randomized blinding.
Janos Follath8e7d6a02022-10-04 13:27:40 +01001844 */
Janos Follathc8d66d52022-11-22 10:47:10 +00001845 const size_t x_index = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 mbedtls_mpi_init(&W[x_index]);
Janos Follath84461482022-11-21 14:31:22 +00001847
Paul Bakker5121ce52009-01-03 21:22:43 +00001848 j = N->n + 1;
Gilles Peskinee6cb45e2023-08-10 15:59:28 +02001849 /* All W[i] including the accumulator must have at least N->n limbs for
1850 * the mpi_montmul() and mpi_montred() calls later. Here we ensure that
1851 * W[1] and the accumulator W[x_index] are large enough. later we'll grow
1852 * other W[i] to the same length. They must not be shrunk midway through
1853 * this function!
Paul Bakker5121ce52009-01-03 21:22:43 +00001854 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[x_index], j));
1856 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[1], j));
1857 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T, j * 2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001858
1859 /*
Paul Bakker50546922012-05-19 08:40:49 +00001860 * Compensate for negative A (and correct at the end)
1861 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 neg = (A->s == -1);
1863 if (neg) {
1864 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Apos, A));
Paul Bakker50546922012-05-19 08:40:49 +00001865 Apos.s = 1;
1866 A = &Apos;
1867 }
1868
1869 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001870 * If 1st call, pre-compute R^2 mod N
1871 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 if (prec_RR == NULL || prec_RR->p == NULL) {
1873 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&RR, 1));
1874 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&RR, N->n * 2 * biL));
1875 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&RR, &RR, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001876
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 if (prec_RR != NULL) {
1878 memcpy(prec_RR, &RR, sizeof(mbedtls_mpi));
1879 }
1880 } else {
1881 memcpy(&RR, prec_RR, sizeof(mbedtls_mpi));
Paul Bakker5121ce52009-01-03 21:22:43 +00001882 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001883
1884 /*
1885 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
1886 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 if (mbedtls_mpi_cmp_mpi(A, N) >= 0) {
1888 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&W[1], A, N));
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001889 /* This should be a no-op because W[1] is already that large before
1890 * mbedtls_mpi_mod_mpi(), but it's necessary to avoid an overflow
Tom Cosgrove93842842022-08-05 16:59:43 +01001891 * in mpi_montmul() below, so let's make sure. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[1], N->n + 1));
1893 } else {
1894 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[1], A));
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001895 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001896
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001897 /* Note that this is safe because W[1] always has at least N->n limbs
1898 * (it grew above and was preserved by mbedtls_mpi_copy()). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 mpi_montmul(&W[1], &RR, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001900
1901 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01001902 * W[x_index] = R^2 * R^-1 mod N = R mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001903 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[x_index], &RR));
1905 mpi_montred(&W[x_index], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001906
Janos Follathc8d66d52022-11-22 10:47:10 +00001907
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 if (window_bitsize > 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001909 /*
Janos Follath74601202022-11-21 15:54:20 +00001910 * W[i] = W[1] ^ i
1911 *
1912 * The first bit of the sliding window is always 1 and therefore we
1913 * only need to store the second half of the table.
Janos Follathc8d66d52022-11-22 10:47:10 +00001914 *
1915 * (There are two special elements in the table: W[0] for the
1916 * accumulator/result and W[1] for A in Montgomery form. Both of these
1917 * are already set at this point.)
Paul Bakker5121ce52009-01-03 21:22:43 +00001918 */
Janos Follath74601202022-11-21 15:54:20 +00001919 j = w_table_used_size / 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001920
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[j], N->n + 1));
1922 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[j], &W[1]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001923
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 for (i = 0; i < window_bitsize - 1; i++) {
1925 mpi_montmul(&W[j], &W[j], N, mm, &T);
1926 }
Paul Bakker0d7702c2013-10-29 16:18:35 +01001927
Paul Bakker5121ce52009-01-03 21:22:43 +00001928 /*
1929 * W[i] = W[i - 1] * W[1]
1930 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 for (i = j + 1; i < w_table_used_size; i++) {
1932 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[i], N->n + 1));
1933 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[i], &W[i - 1]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001934
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 mpi_montmul(&W[i], &W[1], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001936 }
1937 }
1938
1939 nblimbs = E->n;
1940 bufsize = 0;
1941 nbits = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001942 state = 0;
1943
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 while (1) {
1945 if (bufsize == 0) {
1946 if (nblimbs == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001947 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001949
Paul Bakker0d7702c2013-10-29 16:18:35 +01001950 nblimbs--;
1951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 bufsize = sizeof(mbedtls_mpi_uint) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00001953 }
1954
1955 bufsize--;
1956
1957 ei = (E->p[nblimbs] >> bufsize) & 1;
1958
1959 /*
1960 * skip leading 0s
1961 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 if (ei == 0 && state == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001963 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001965
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 if (ei == 0 && state == 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001967 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01001968 * out of window, square W[x_index]
Paul Bakker5121ce52009-01-03 21:22:43 +00001969 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, x_index));
1971 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 continue;
1973 }
1974
1975 /*
1976 * add ei to current window
1977 */
1978 state = 2;
1979
1980 nbits++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 exponent_bits_in_window |= (ei << (window_bitsize - nbits));
Paul Bakker5121ce52009-01-03 21:22:43 +00001982
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 if (nbits == window_bitsize) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001984 /*
Janos Follath7fa11b82022-11-21 14:48:02 +00001985 * W[x_index] = W[x_index]^window_bitsize R^-1 mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001986 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 for (i = 0; i < window_bitsize; i++) {
1988 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size,
1989 x_index));
1990 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Janos Follathb764ee12022-10-04 14:00:09 +01001991 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001992
1993 /*
Janos Follath7fa11b82022-11-21 14:48:02 +00001994 * W[x_index] = W[x_index] * W[exponent_bits_in_window] R^-1 mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001995 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size,
1997 exponent_bits_in_window));
1998 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001999
2000 state--;
2001 nbits = 0;
Janos Follath7fa11b82022-11-21 14:48:02 +00002002 exponent_bits_in_window = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002003 }
2004 }
2005
2006 /*
2007 * process the remaining bits
2008 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 for (i = 0; i < nbits; i++) {
2010 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, x_index));
2011 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00002012
Janos Follath7fa11b82022-11-21 14:48:02 +00002013 exponent_bits_in_window <<= 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002014
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 if ((exponent_bits_in_window & ((size_t) 1 << window_bitsize)) != 0) {
2016 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, 1));
2017 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Janos Follathb764ee12022-10-04 14:00:09 +01002018 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002019 }
2020
2021 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01002022 * W[x_index] = A^E * R * R^-1 mod N = A^E mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00002023 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 mpi_montred(&W[x_index], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00002025
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 if (neg && E->n != 0 && (E->p[0] & 1) != 0) {
Janos Follath8e7d6a02022-10-04 13:27:40 +01002027 W[x_index].s = -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&W[x_index], N, &W[x_index]));
Paul Bakkerf6198c12012-05-16 08:02:29 +00002029 }
2030
Janos Follath8e7d6a02022-10-04 13:27:40 +01002031 /*
2032 * Load the result in the output variable.
2033 */
Chien Wonge2caf412023-08-01 21:38:46 +08002034 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, &W[x_index]));
Janos Follath8e7d6a02022-10-04 13:27:40 +01002035
Paul Bakker5121ce52009-01-03 21:22:43 +00002036cleanup:
2037
Janos Follathb2c2fca2022-11-21 15:05:31 +00002038 /* The first bit of the sliding window is always 1 and therefore the first
2039 * half of the table was unused. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 for (i = w_table_used_size/2; i < w_table_used_size; i++) {
2041 mbedtls_mpi_free(&W[i]);
2042 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 mbedtls_mpi_free(&W[x_index]);
2045 mbedtls_mpi_free(&W[1]);
2046 mbedtls_mpi_free(&T);
2047 mbedtls_mpi_free(&Apos);
2048 mbedtls_mpi_free(&WW);
Paul Bakker6c591fa2011-05-05 11:49:20 +00002049
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 if (prec_RR == NULL || prec_RR->p == NULL) {
2051 mbedtls_mpi_free(&RR);
2052 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002053
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002055}
2056
Paul Bakker5121ce52009-01-03 21:22:43 +00002057/*
2058 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
2059 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002060int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00002061{
Janos Follath24eed8d2019-11-22 13:21:35 +00002062 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002063 size_t lz, lzt;
Alexander Ke8ad49f2019-08-16 16:16:07 +03002064 mbedtls_mpi TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00002065
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 MPI_VALIDATE_RET(G != NULL);
2067 MPI_VALIDATE_RET(A != NULL);
2068 MPI_VALIDATE_RET(B != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00002069
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00002071
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A));
2073 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00002074
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 lz = mbedtls_mpi_lsb(&TA);
2076 lzt = mbedtls_mpi_lsb(&TB);
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002077
Gilles Peskine27253bc2021-06-09 13:26:43 +02002078 /* The loop below gives the correct result when A==0 but not when B==0.
2079 * So have a special case for B==0. Leverage the fact that we just
2080 * calculated the lsb and lsb(B)==0 iff B is odd or 0 to make the test
2081 * slightly more efficient than cmp_int(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 if (lzt == 0 && mbedtls_mpi_get_bit(&TB, 0) == 0) {
2083 ret = mbedtls_mpi_copy(G, A);
Gilles Peskine27253bc2021-06-09 13:26:43 +02002084 goto cleanup;
2085 }
2086
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 if (lzt < lz) {
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002088 lz = lzt;
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002090
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 TA.s = TB.s = 1;
2092
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002093 /* We mostly follow the procedure described in HAC 14.54, but with some
2094 * minor differences:
2095 * - Sequences of multiplications or divisions by 2 are grouped into a
2096 * single shift operation.
Gilles Peskineb09c7ee2021-06-21 18:58:39 +02002097 * - The procedure in HAC assumes that 0 < TB <= TA.
2098 * - The condition TB <= TA is not actually necessary for correctness.
2099 * TA and TB have symmetric roles except for the loop termination
2100 * condition, and the shifts at the beginning of the loop body
2101 * remove any significance from the ordering of TA vs TB before
2102 * the shifts.
2103 * - If TA = 0, the loop goes through 0 iterations and the result is
2104 * correctly TB.
2105 * - The case TB = 0 was short-circuited above.
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002106 *
2107 * For the correctness proof below, decompose the original values of
2108 * A and B as
2109 * A = sa * 2^a * A' with A'=0 or A' odd, and sa = +-1
2110 * B = sb * 2^b * B' with B'=0 or B' odd, and sb = +-1
2111 * Then gcd(A, B) = 2^{min(a,b)} * gcd(A',B'),
2112 * and gcd(A',B') is odd or 0.
2113 *
2114 * At the beginning, we have TA = |A| and TB = |B| so gcd(A,B) = gcd(TA,TB).
2115 * The code maintains the following invariant:
2116 * gcd(A,B) = 2^k * gcd(TA,TB) for some k (I)
Gilles Peskine4df3f1f2021-06-15 22:09:39 +02002117 */
2118
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002119 /* Proof that the loop terminates:
2120 * At each iteration, either the right-shift by 1 is made on a nonzero
2121 * value and the nonnegative integer bitlen(TA) + bitlen(TB) decreases
2122 * by at least 1, or the right-shift by 1 is made on zero and then
2123 * TA becomes 0 which ends the loop (TB cannot be 0 if it is right-shifted
2124 * since in that case TB is calculated from TB-TA with the condition TB>TA).
2125 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 while (mbedtls_mpi_cmp_int(&TA, 0) != 0) {
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002127 /* Divisions by 2 preserve the invariant (I). */
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, mbedtls_mpi_lsb(&TA)));
2129 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, mbedtls_mpi_lsb(&TB)));
Paul Bakker5121ce52009-01-03 21:22:43 +00002130
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002131 /* Set either TA or TB to |TA-TB|/2. Since TA and TB are both odd,
2132 * TA-TB is even so the division by 2 has an integer result.
2133 * Invariant (I) is preserved since any odd divisor of both TA and TB
2134 * also divides |TA-TB|/2, and any odd divisor of both TA and |TA-TB|/2
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002135 * also divides TB, and any odd divisor of both TB and |TA-TB|/2 also
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002136 * divides TA.
2137 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 if (mbedtls_mpi_cmp_mpi(&TA, &TB) >= 0) {
2139 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TA, &TA, &TB));
2140 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, 1));
2141 } else {
2142 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TB, &TB, &TA));
2143 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 }
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002145 /* Note that one of TA or TB is still odd. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 }
2147
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002148 /* By invariant (I), gcd(A,B) = 2^k * gcd(TA,TB) for some k.
2149 * At the loop exit, TA = 0, so gcd(TA,TB) = TB.
2150 * - If there was at least one loop iteration, then one of TA or TB is odd,
2151 * and TA = 0, so TB is odd and gcd(TA,TB) = gcd(A',B'). In this case,
2152 * lz = min(a,b) so gcd(A,B) = 2^lz * TB.
2153 * - If there was no loop iteration, then A was 0, and gcd(A,B) = B.
Gilles Peskine4d3fd362021-06-21 11:40:38 +02002154 * 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 +02002155 */
2156
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&TB, lz));
2158 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(G, &TB));
Paul Bakker5121ce52009-01-03 21:22:43 +00002159
2160cleanup:
2161
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00002163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002165}
2166
Paul Bakker33dc46b2014-04-30 16:11:39 +02002167/*
2168 * Fill X with size bytes of random.
Gilles Peskine22cdd0c2022-10-27 20:15:13 +02002169 * The bytes returned from the RNG are used in a specific order which
2170 * is suitable for deterministic ECDSA (see the specification of
2171 * mbedtls_mpi_random() and the implementation in mbedtls_mpi_fill_random()).
Paul Bakker33dc46b2014-04-30 16:11:39 +02002172 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002173int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
2174 int (*f_rng)(void *, unsigned char *, size_t),
2175 void *p_rng)
Paul Bakker287781a2011-03-26 13:18:49 +00002176{
Janos Follath24eed8d2019-11-22 13:21:35 +00002177 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 const size_t limbs = CHARS_TO_LIMBS(size);
Hanno Beckerda1655a2017-10-18 14:21:44 +01002179
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 MPI_VALIDATE_RET(X != NULL);
2181 MPI_VALIDATE_RET(f_rng != NULL);
Paul Bakker33dc46b2014-04-30 16:11:39 +02002182
Hanno Beckerda1655a2017-10-18 14:21:44 +01002183 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
2185 if (size == 0) {
2186 return 0;
2187 }
Paul Bakker287781a2011-03-26 13:18:49 +00002188
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 ret = mbedtls_mpi_core_fill_random(X->p, X->n, size, f_rng, p_rng);
Paul Bakker287781a2011-03-26 13:18:49 +00002190
2191cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 return ret;
Paul Bakker287781a2011-03-26 13:18:49 +00002193}
2194
Gilles Peskine449bd832023-01-11 14:50:10 +01002195int mbedtls_mpi_random(mbedtls_mpi *X,
2196 mbedtls_mpi_sint min,
2197 const mbedtls_mpi *N,
2198 int (*f_rng)(void *, unsigned char *, size_t),
2199 void *p_rng)
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002200{
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 if (min < 0) {
2202 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2203 }
2204 if (mbedtls_mpi_cmp_int(N, min) <= 0) {
2205 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2206 }
Gilles Peskine1e918f42021-03-29 22:14:51 +02002207
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002208 /* Ensure that target MPI has exactly the same number of limbs
2209 * as the upper bound, even if the upper bound has leading zeros.
Gilles Peskine6b7ce962022-12-15 15:04:33 +01002210 * This is necessary for mbedtls_mpi_core_random. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 int ret = mbedtls_mpi_resize_clear(X, N->n);
2212 if (ret != 0) {
2213 return ret;
2214 }
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 return mbedtls_mpi_core_random(X->p, min, N->p, X->n, f_rng, p_rng);
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002217}
2218
Paul Bakker5121ce52009-01-03 21:22:43 +00002219/*
2220 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2221 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002222int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
Paul Bakker5121ce52009-01-03 21:22:43 +00002223{
Janos Follath24eed8d2019-11-22 13:21:35 +00002224 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 MPI_VALIDATE_RET(X != NULL);
2227 MPI_VALIDATE_RET(A != NULL);
2228 MPI_VALIDATE_RET(N != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 if (mbedtls_mpi_cmp_int(N, 1) <= 0) {
2231 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2232 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TU); mbedtls_mpi_init(&U1); mbedtls_mpi_init(&U2);
2235 mbedtls_mpi_init(&G); mbedtls_mpi_init(&TB); mbedtls_mpi_init(&TV);
2236 mbedtls_mpi_init(&V1); mbedtls_mpi_init(&V2);
Paul Bakker5121ce52009-01-03 21:22:43 +00002237
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, A, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002239
Gilles Peskine449bd832023-01-11 14:50:10 +01002240 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 goto cleanup;
2243 }
2244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&TA, A, N));
2246 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TU, &TA));
2247 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, N));
2248 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TV, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U1, 1));
2251 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U2, 0));
2252 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V1, 0));
2253 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002254
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 do {
2256 while ((TU.p[0] & 1) == 0) {
2257 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TU, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002258
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 if ((U1.p[0] & 1) != 0 || (U2.p[0] & 1) != 0) {
2260 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&U1, &U1, &TB));
2261 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &TA));
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 }
2263
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U1, 1));
2265 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 }
2267
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 while ((TV.p[0] & 1) == 0) {
2269 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TV, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002270
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 if ((V1.p[0] & 1) != 0 || (V2.p[0] & 1) != 0) {
2272 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, &TB));
2273 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &TA));
Paul Bakker5121ce52009-01-03 21:22:43 +00002274 }
2275
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V1, 1));
2277 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002278 }
2279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 if (mbedtls_mpi_cmp_mpi(&TU, &TV) >= 0) {
2281 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TU, &TU, &TV));
2282 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U1, &U1, &V1));
2283 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &V2));
2284 } else {
2285 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TV, &TV, &TU));
2286 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, &U1));
2287 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &U2));
Paul Bakker5121ce52009-01-03 21:22:43 +00002288 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 } while (mbedtls_mpi_cmp_int(&TU, 0) != 0);
2290
2291 while (mbedtls_mpi_cmp_int(&V1, 0) < 0) {
2292 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002293 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 while (mbedtls_mpi_cmp_mpi(&V1, N) >= 0) {
2296 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, N));
2297 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, &V1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002300
2301cleanup:
2302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TU); mbedtls_mpi_free(&U1); mbedtls_mpi_free(&U2);
2304 mbedtls_mpi_free(&G); mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TV);
2305 mbedtls_mpi_free(&V1); mbedtls_mpi_free(&V2);
Paul Bakker5121ce52009-01-03 21:22:43 +00002306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002308}
2309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002311
Gilles Peskineb2bc1712019-02-08 17:27:11 +01002312/* Gaps between primes, starting at 3. https://oeis.org/A001223 */
2313static const unsigned char small_prime_gaps[] = {
2314 2, 2, 4, 2, 4, 2, 4, 6,
2315 2, 6, 4, 2, 4, 6, 6, 2,
2316 6, 4, 2, 6, 4, 6, 8, 4,
2317 2, 4, 2, 4, 14, 4, 6, 2,
2318 10, 2, 6, 6, 4, 6, 6, 2,
2319 10, 2, 4, 2, 12, 12, 4, 2,
2320 4, 6, 2, 10, 6, 6, 6, 2,
2321 6, 4, 2, 10, 14, 4, 2, 4,
2322 14, 6, 10, 2, 4, 6, 8, 6,
2323 6, 4, 6, 8, 4, 8, 10, 2,
2324 10, 2, 6, 4, 6, 8, 4, 2,
2325 4, 12, 8, 4, 8, 4, 6, 12,
2326 2, 18, 6, 10, 6, 6, 2, 6,
2327 10, 6, 6, 2, 6, 6, 4, 2,
2328 12, 10, 2, 4, 6, 6, 2, 12,
2329 4, 6, 8, 10, 8, 10, 8, 6,
2330 6, 4, 8, 6, 4, 8, 4, 14,
2331 10, 12, 2, 10, 2, 4, 2, 10,
2332 14, 4, 2, 4, 14, 4, 2, 4,
2333 20, 4, 8, 10, 8, 4, 6, 6,
2334 14, 4, 6, 6, 8, 6, /*reaches 997*/
Gilles Peskine30b03782023-08-22 11:06:47 +02002335 0 /* the last entry is effectively unused */
Paul Bakker5121ce52009-01-03 21:22:43 +00002336};
2337
2338/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002339 * Small divisors test (X must be positive)
2340 *
2341 * Return values:
2342 * 0: no small factor (possible prime, more tests needed)
2343 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002345 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002346 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002347static int mpi_check_small_factors(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +00002348{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002349 int ret = 0;
2350 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 mbedtls_mpi_uint r;
Gilles Peskineb2bc1712019-02-08 17:27:11 +01002352 unsigned p = 3; /* The first odd prime */
Paul Bakker5121ce52009-01-03 21:22:43 +00002353
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 if ((X->p[0] & 1) == 0) {
2355 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2356 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002357
Gilles Peskineb2bc1712019-02-08 17:27:11 +01002358 for (i = 0; i < sizeof(small_prime_gaps); p += small_prime_gaps[i], i++) {
2359 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, p));
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 if (r == 0) {
Gilles Peskineb2bc1712019-02-08 17:27:11 +01002361 if (mbedtls_mpi_cmp_int(X, p) == 0) {
2362 return 1;
2363 } else {
2364 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2365 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002367 }
2368
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002369cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 return ret;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002371}
2372
2373/*
2374 * Miller-Rabin pseudo-primality test (HAC 4.24)
2375 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002376static int mpi_miller_rabin(const mbedtls_mpi *X, size_t rounds,
2377 int (*f_rng)(void *, unsigned char *, size_t),
2378 void *p_rng)
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002379{
Pascal Junodb99183d2015-03-11 16:49:45 +01002380 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002381 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002383
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 MPI_VALIDATE_RET(X != NULL);
2385 MPI_VALIDATE_RET(f_rng != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00002386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 mbedtls_mpi_init(&W); mbedtls_mpi_init(&R);
2388 mbedtls_mpi_init(&T); mbedtls_mpi_init(&A);
2389 mbedtls_mpi_init(&RR);
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002390
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 /*
2392 * W = |X| - 1
2393 * R = W >> lsb( W )
2394 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&W, X, 1));
2396 s = mbedtls_mpi_lsb(&W);
2397 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R, &W));
2398 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&R, s));
Paul Bakker5121ce52009-01-03 21:22:43 +00002399
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 for (i = 0; i < rounds; i++) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 /*
2402 * pick a random A, 1 < A < |X| - 1
2403 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002404 count = 0;
2405 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&A, X->n * ciL, f_rng, p_rng));
Pascal Junodb99183d2015-03-11 16:49:45 +01002407
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 j = mbedtls_mpi_bitlen(&A);
2409 k = mbedtls_mpi_bitlen(&W);
Pascal Junodb99183d2015-03-11 16:49:45 +01002410 if (j > k) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 A.p[A.n - 1] &= ((mbedtls_mpi_uint) 1 << (k - (A.n - 1) * biL - 1)) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002412 }
2413
2414 if (count++ > 30) {
Jens Wiklanderf08aa3e2019-01-17 13:30:57 +01002415 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2416 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002417 }
2418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 } while (mbedtls_mpi_cmp_mpi(&A, &W) >= 0 ||
2420 mbedtls_mpi_cmp_int(&A, 1) <= 0);
Paul Bakker5121ce52009-01-03 21:22:43 +00002421
2422 /*
2423 * A = A^R mod |X|
2424 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&A, &A, &R, X, &RR));
Paul Bakker5121ce52009-01-03 21:22:43 +00002426
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 if (mbedtls_mpi_cmp_mpi(&A, &W) == 0 ||
2428 mbedtls_mpi_cmp_int(&A, 1) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002431
2432 j = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 while (j < s && mbedtls_mpi_cmp_mpi(&A, &W) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002434 /*
2435 * A = A * A mod |X|
2436 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &A, &A));
2438 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&A, &T, X));
Paul Bakker5121ce52009-01-03 21:22:43 +00002439
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 if (mbedtls_mpi_cmp_int(&A, 1) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002441 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002443
2444 j++;
2445 }
2446
2447 /*
2448 * not prime if A != |X| - 1 or A == 1
2449 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 if (mbedtls_mpi_cmp_mpi(&A, &W) != 0 ||
2451 mbedtls_mpi_cmp_int(&A, 1) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 break;
2454 }
2455 }
2456
2457cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 mbedtls_mpi_free(&W); mbedtls_mpi_free(&R);
2459 mbedtls_mpi_free(&T); mbedtls_mpi_free(&A);
2460 mbedtls_mpi_free(&RR);
Paul Bakker5121ce52009-01-03 21:22:43 +00002461
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002463}
2464
2465/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002466 * Pseudo-primality test: small factors, then Miller-Rabin
2467 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002468int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
2469 int (*f_rng)(void *, unsigned char *, size_t),
2470 void *p_rng)
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002471{
Janos Follath24eed8d2019-11-22 13:21:35 +00002472 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473 mbedtls_mpi XX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 MPI_VALIDATE_RET(X != NULL);
2475 MPI_VALIDATE_RET(f_rng != NULL);
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002476
2477 XX.s = 1;
2478 XX.n = X->n;
2479 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002480
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 if (mbedtls_mpi_cmp_int(&XX, 0) == 0 ||
2482 mbedtls_mpi_cmp_int(&XX, 1) == 0) {
2483 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002484 }
2485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 if (mbedtls_mpi_cmp_int(&XX, 2) == 0) {
2487 return 0;
2488 }
2489
2490 if ((ret = mpi_check_small_factors(&XX)) != 0) {
2491 if (ret == 1) {
2492 return 0;
2493 }
2494
2495 return ret;
2496 }
2497
2498 return mpi_miller_rabin(&XX, rounds, f_rng, p_rng);
Janos Follathf301d232018-08-14 13:34:01 +01002499}
2500
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002501/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002503 *
Janos Follathf301d232018-08-14 13:34:01 +01002504 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2505 * be either 1024 bits or 1536 bits long, and flags must contain
2506 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002507 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002508int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
2509 int (*f_rng)(void *, unsigned char *, size_t),
2510 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00002511{
Jethro Beekman66689272018-02-14 19:24:10 -08002512#ifdef MBEDTLS_HAVE_INT64
2513// ceil(2^63.5)
2514#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2515#else
2516// ceil(2^31.5)
2517#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2518#endif
2519 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002520 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002521 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 mbedtls_mpi_uint r;
2523 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002524
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 MPI_VALIDATE_RET(X != NULL);
2526 MPI_VALIDATE_RET(f_rng != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00002527
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 if (nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS) {
2529 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2530 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002531
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 mbedtls_mpi_init(&Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00002533
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 n = BITS_TO_LIMBS(nbits);
Paul Bakker5121ce52009-01-03 21:22:43 +00002535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR) == 0) {
Janos Follathda31fa12018-09-03 14:45:23 +01002537 /*
2538 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2539 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 rounds = ((nbits >= 1300) ? 2 : (nbits >= 850) ? 3 :
2541 (nbits >= 650) ? 4 : (nbits >= 350) ? 8 :
2542 (nbits >= 250) ? 12 : (nbits >= 150) ? 18 : 27);
2543 } else {
Janos Follathda31fa12018-09-03 14:45:23 +01002544 /*
2545 * 2^-100 error probability, number of rounds computed based on HAC,
2546 * fact 4.48
2547 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 rounds = ((nbits >= 1450) ? 4 : (nbits >= 1150) ? 5 :
2549 (nbits >= 1000) ? 6 : (nbits >= 850) ? 7 :
2550 (nbits >= 750) ? 8 : (nbits >= 500) ? 13 :
2551 (nbits >= 250) ? 28 : (nbits >= 150) ? 40 : 51);
Janos Follathda31fa12018-09-03 14:45:23 +01002552 }
2553
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 while (1) {
2555 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(X, n * ciL, f_rng, p_rng));
Jethro Beekman66689272018-02-14 19:24:10 -08002556 /* 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 +01002557 if (X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2) {
2558 continue;
2559 }
Jethro Beekman66689272018-02-14 19:24:10 -08002560
2561 k = n * biL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 if (k > nbits) {
2563 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(X, k - nbits));
2564 }
Jethro Beekman66689272018-02-14 19:24:10 -08002565 X->p[0] |= 1;
2566
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH) == 0) {
2568 ret = mbedtls_mpi_is_prime_ext(X, rounds, f_rng, p_rng);
Jethro Beekman66689272018-02-14 19:24:10 -08002569
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002571 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 }
2573 } else {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002574 /*
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01002575 * A necessary condition for Y and X = 2Y + 1 to be prime
Jethro Beekman66689272018-02-14 19:24:10 -08002576 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2577 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002578 */
Jethro Beekman66689272018-02-14 19:24:10 -08002579
2580 X->p[0] |= 2;
2581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, 3));
2583 if (r == 0) {
2584 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 8));
2585 } else if (r == 1) {
2586 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 4));
2587 }
Jethro Beekman66689272018-02-14 19:24:10 -08002588
2589 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, X));
2591 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, 1));
Jethro Beekman66689272018-02-14 19:24:10 -08002592
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 while (1) {
Jethro Beekman66689272018-02-14 19:24:10 -08002594 /*
2595 * First, check small factors for X and Y
2596 * before doing Miller-Rabin on any of them
2597 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 if ((ret = mpi_check_small_factors(X)) == 0 &&
2599 (ret = mpi_check_small_factors(&Y)) == 0 &&
2600 (ret = mpi_miller_rabin(X, rounds, f_rng, p_rng))
2601 == 0 &&
2602 (ret = mpi_miller_rabin(&Y, rounds, f_rng, p_rng))
2603 == 0) {
Jethro Beekman66689272018-02-14 19:24:10 -08002604 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 }
Jethro Beekman66689272018-02-14 19:24:10 -08002606
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Jethro Beekman66689272018-02-14 19:24:10 -08002608 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 }
Jethro Beekman66689272018-02-14 19:24:10 -08002610
2611 /*
2612 * Next candidates. We want to preserve Y = (X-1) / 2 and
2613 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2614 * so up Y by 6 and X by 12.
2615 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 12));
2617 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&Y, &Y, 6));
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 }
2620 }
2621
2622cleanup:
2623
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 mbedtls_mpi_free(&Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00002625
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002627}
2628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002632
Paul Bakker23986e52011-04-24 08:57:21 +00002633#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002634
2635static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2636{
2637 { 693, 609, 21 },
2638 { 1764, 868, 28 },
2639 { 768454923, 542167814, 1 }
2640};
2641
Paul Bakker5121ce52009-01-03 21:22:43 +00002642/*
2643 * Checkup routine
2644 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002645int mbedtls_mpi_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002646{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002647 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00002649
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 mbedtls_mpi_init(&A); mbedtls_mpi_init(&E); mbedtls_mpi_init(&N); mbedtls_mpi_init(&X);
2651 mbedtls_mpi_init(&Y); mbedtls_mpi_init(&U); mbedtls_mpi_init(&V);
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&A, 16,
2654 "EFE021C2645FD1DC586E69184AF4A31E" \
2655 "D5F53E93B5F123FA41680867BA110131" \
2656 "944FE7952E2517337780CB0DB80E61AA" \
2657 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002658
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&E, 16,
2660 "B2E7EFD37075B9F03FF989C7C5051C20" \
2661 "34D2A323810251127E7BF8625A4F49A5" \
2662 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
2663 "5B5C25763222FEFCCFC38B832366C29E"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002664
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&N, 16,
2666 "0066A198186C18C10B2F5ED9B522752A" \
2667 "9830B69916E535C8F047518A889A43A5" \
2668 "94B6BED27A168D31D4A52F88925AA8F5"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002669
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&X, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2673 "602AB7ECA597A3D6B56FF9829A5E8B85" \
2674 "9E857EA95A03512E2BAE7391688D264A" \
2675 "A5663B0341DB9CCFD2C4C5F421FEC814" \
2676 "8001B72E848A38CAE1C65F78E56ABDEF" \
2677 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
2678 "ECF677152EF804370C1A305CAF3B5BF1" \
2679 "30879B56C61DE584A0F53A2447A51E"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 if (verbose != 0) {
2682 mbedtls_printf(" MPI test #1 (mul_mpi): ");
2683 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2686 if (verbose != 0) {
2687 mbedtls_printf("failed\n");
2688 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002689
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002690 ret = 1;
2691 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 }
2693
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 if (verbose != 0) {
2695 mbedtls_printf("passed\n");
2696 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&X, &Y, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2701 "256567336059E52CAE22925474705F39A94"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002702
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&V, 16,
2704 "6613F26162223DF488E9CD48CC132C7A" \
2705 "0AC93C701B001B092E4E5B9F73BCD27B" \
2706 "9EE50D0657C77F374E903CDFA4C642"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 if (verbose != 0) {
2709 mbedtls_printf(" MPI test #2 (div_mpi): ");
2710 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002711
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0 ||
2713 mbedtls_mpi_cmp_mpi(&Y, &V) != 0) {
2714 if (verbose != 0) {
2715 mbedtls_printf("failed\n");
2716 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002717
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002718 ret = 1;
2719 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 }
2721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 if (verbose != 0) {
2723 mbedtls_printf("passed\n");
2724 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002725
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&X, &A, &E, &N, NULL));
Paul Bakker5121ce52009-01-03 21:22:43 +00002727
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2729 "36E139AEA55215609D2816998ED020BB" \
2730 "BD96C37890F65171D948E9BC7CBAA4D9" \
2731 "325D24D6A3C12710F10A09FA08AB87"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002732
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 if (verbose != 0) {
2734 mbedtls_printf(" MPI test #3 (exp_mod): ");
2735 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002736
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2738 if (verbose != 0) {
2739 mbedtls_printf("failed\n");
2740 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002741
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002742 ret = 1;
2743 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002744 }
2745
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 if (verbose != 0) {
2747 mbedtls_printf("passed\n");
2748 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002749
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&X, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002751
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2753 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
2754 "C3DBA76456363A10869622EAC2DD84EC" \
2755 "C5B8A74DAC4D09E03B5E0BE779F2DF61"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002756
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 if (verbose != 0) {
2758 mbedtls_printf(" MPI test #4 (inv_mod): ");
2759 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002760
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2762 if (verbose != 0) {
2763 mbedtls_printf("failed\n");
2764 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002765
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002766 ret = 1;
2767 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002768 }
2769
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 if (verbose != 0) {
2771 mbedtls_printf("passed\n");
2772 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002773
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 if (verbose != 0) {
2775 mbedtls_printf(" MPI test #5 (simple gcd): ");
2776 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002777
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 for (i = 0; i < GCD_PAIR_COUNT; i++) {
2779 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&X, gcd_pairs[i][0]));
2780 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Y, gcd_pairs[i][1]));
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002781
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&A, &X, &Y));
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002783
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 if (mbedtls_mpi_cmp_int(&A, gcd_pairs[i][2]) != 0) {
2785 if (verbose != 0) {
2786 mbedtls_printf("failed at %d\n", i);
2787 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002788
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002789 ret = 1;
2790 goto cleanup;
2791 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002792 }
2793
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 if (verbose != 0) {
2795 mbedtls_printf("passed\n");
2796 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002797
Paul Bakker5121ce52009-01-03 21:22:43 +00002798cleanup:
2799
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 if (ret != 0 && verbose != 0) {
2801 mbedtls_printf("Unexpected error, return code = %08X\n", (unsigned int) ret);
2802 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002803
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 mbedtls_mpi_free(&A); mbedtls_mpi_free(&E); mbedtls_mpi_free(&N); mbedtls_mpi_free(&X);
2805 mbedtls_mpi_free(&Y); mbedtls_mpi_free(&U); mbedtls_mpi_free(&V);
Paul Bakker5121ce52009-01-03 21:22:43 +00002806
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 if (verbose != 0) {
2808 mbedtls_printf("\n");
2809 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002810
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002812}
2813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816#endif /* MBEDTLS_BIGNUM_C */