blob: 8ad7258582cf32cd6373a2e2ccd81f68a708688e [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 Rodgman1a7a5622023-05-17 13:47:56 +010064 mbedtls_ct_condition_t cond, 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 Rodgman1a7a5622023-05-17 13:47:56 +010086 cond = mbedtls_ct_bool_xor(X_is_negative, Y_is_negative); // non-zero if different sign
87 result = mbedtls_ct_bool_and(cond, X_is_negative);
Dave Rodgman7d4f0192023-05-09 14:01:05 +010088
Dave Rodgman1a7a5622023-05-17 13:47:56 +010089 /* Assuming signs are the same, compare X and Y. We switch the comparison
90 * order if they are negative so that we get the right result, regardles of
91 * sign.
92 *
93 * Store in ret iff the signs are the same (i.e., iff cond == 0). If
94 * the signs differ, done has already been set.
Dave Rodgman7d4f0192023-05-09 14:01:05 +010095 */
Dave Rodgman7d4f0192023-05-09 14:01:05 +010096
Dave Rodgman1a7a5622023-05-17 13:47:56 +010097 /* This is used to conditionally swap the pointers in const time */
98 void * const p[2] = { X->p, Y->p };
Dave Rodgman2c764842023-05-18 13:28:21 +010099 size_t i = mbedtls_ct_size_if0(X_is_negative, 1);
100 mbedtls_ct_condition_t lt = mbedtls_mpi_core_lt_ct(p[i], p[i ^ 1], X->n);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100101
Dave Rodgman1a7a5622023-05-17 13:47:56 +0100102 result = mbedtls_ct_bool_or(result, mbedtls_ct_bool_and(mbedtls_ct_bool_not(cond), lt));
103
104 *ret = mbedtls_ct_uint_if0(result, 1);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100105
106 return 0;
107}
108
109/*
110 * Conditionally assign X = Y, without leaking information
111 * about whether the assignment was made or not.
112 * (Leaking information about the respective sizes of X and Y is ok however.)
113 */
114#if defined(_MSC_VER) && defined(_M_ARM64) && (_MSC_FULL_VER < 193131103)
115/*
116 * MSVC miscompiles this function if it's inlined prior to Visual Studio 2022 version 17.1. See:
117 * https://developercommunity.visualstudio.com/t/c-compiler-miscompiles-part-of-mbedtls-library-on/1646989
118 */
119__declspec(noinline)
120#endif
121int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X,
122 const mbedtls_mpi *Y,
123 unsigned char assign)
124{
125 int ret = 0;
126 MPI_VALIDATE_RET(X != NULL);
127 MPI_VALIDATE_RET(Y != NULL);
128
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100129 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, Y->n));
130
Dave Rodgman589ccb82023-05-17 13:55:01 +0100131 mbedtls_ct_condition_t do_assign = mbedtls_ct_bool(assign);
132
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100133 X->s = (int) mbedtls_ct_uint_if(do_assign, Y->s, X->s);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100134
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100135 mbedtls_mpi_core_cond_assign(X->p, Y->p, Y->n, do_assign);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100136
Dave Rodgman589ccb82023-05-17 13:55:01 +0100137 mbedtls_ct_condition_t do_not_assign = mbedtls_ct_bool_not(do_assign);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100138 for (size_t i = Y->n; i < X->n; i++) {
Dave Rodgman589ccb82023-05-17 13:55:01 +0100139 X->p[i] = mbedtls_ct_mpi_uint_if0(do_not_assign, X->p[i]);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100140 }
141
142cleanup:
143 return ret;
144}
145
146/*
147 * Conditionally swap X and Y, without leaking information
148 * about whether the swap was made or not.
149 * Here it is not ok to simply swap the pointers, which would lead to
150 * different memory access patterns when X and Y are used afterwards.
151 */
152int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X,
153 mbedtls_mpi *Y,
154 unsigned char swap)
155{
156 int ret = 0;
157 int s;
158 MPI_VALIDATE_RET(X != NULL);
159 MPI_VALIDATE_RET(Y != NULL);
160
161 if (X == Y) {
162 return 0;
163 }
164
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100165 mbedtls_ct_condition_t do_swap = mbedtls_ct_bool(swap);
166
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100167 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, Y->n));
168 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Y, X->n));
169
170 s = X->s;
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100171 X->s = (int) mbedtls_ct_uint_if(do_swap, Y->s, X->s);
172 Y->s = (int) mbedtls_ct_uint_if(do_swap, s, Y->s);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100173
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100174 mbedtls_mpi_core_cond_swap(X->p, Y->p, X->n, do_swap);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100175
176cleanup:
177 return ret;
178}
179
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500180/* Implementation that should never be optimized out by the compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +0100181static void mbedtls_mpi_zeroize(mbedtls_mpi_uint *v, size_t n)
Andres Amaya Garcia6698d2f2018-04-24 08:39:07 -0500182{
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 mbedtls_platform_zeroize(v, ciL * n);
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500184}
185
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) {
208 mbedtls_mpi_zeroize(X->p, X->n);
209 mbedtls_free(X->p);
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 }
211
Paul Bakker6c591fa2011-05-05 11:49:20 +0000212 X->s = 1;
213 X->n = 0;
214 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000215}
216
217/*
218 * Enlarge to the specified number of limbs
219 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100220int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs)
Paul Bakker5121ce52009-01-03 21:22:43 +0000221{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 mbedtls_mpi_uint *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
226 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
227 }
Paul Bakkerf9688572011-05-05 10:00:45 +0000228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 if (X->n < nblimbs) {
230 if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(nblimbs, ciL)) == NULL) {
231 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
232 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 if (X->p != NULL) {
235 memcpy(p, X->p, X->n * ciL);
236 mbedtls_mpi_zeroize(X->p, X->n);
237 mbedtls_free(X->p);
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 }
239
240 X->n = nblimbs;
241 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);
284 mbedtls_mpi_zeroize(X->p, X->n);
285 mbedtls_free(X->p);
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100286 }
287
288 X->n = i;
289 X->p = p;
290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return 0;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100292}
293
Gilles Peskineed32b572021-06-02 22:17:52 +0200294/* Resize X to have exactly n limbs and set it to 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100295static int mbedtls_mpi_resize_clear(mbedtls_mpi *X, size_t limbs)
Gilles Peskineed32b572021-06-02 22:17:52 +0200296{
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (limbs == 0) {
298 mbedtls_mpi_free(X);
299 return 0;
300 } else if (X->n == limbs) {
301 memset(X->p, 0, limbs * ciL);
Gilles Peskineed32b572021-06-02 22:17:52 +0200302 X->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return 0;
304 } else {
305 mbedtls_mpi_free(X);
306 return mbedtls_mpi_grow(X, limbs);
Gilles Peskineed32b572021-06-02 22:17:52 +0200307 }
308}
309
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100310/*
Gilles Peskine3da1a8f2021-06-08 23:17:42 +0200311 * Copy the contents of Y into X.
312 *
313 * This function is not constant-time. Leading zeros in Y may be removed.
314 *
315 * Ensure that X does not shrink. This is not guaranteed by the public API,
316 * but some code in the bignum module relies on this property, for example
317 * in mbedtls_mpi_exp_mod().
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100319int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000320{
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100321 int ret = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000322 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 MPI_VALIDATE_RET(X != NULL);
324 MPI_VALIDATE_RET(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if (X == Y) {
327 return 0;
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200328 }
329
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 if (Y->n == 0) {
331 if (X->n != 0) {
332 X->s = 1;
333 memset(X->p, 0, X->n * ciL);
334 }
335 return 0;
336 }
337
338 for (i = Y->n - 1; i > 0; i--) {
339 if (Y->p[i] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 }
342 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000343 i++;
344
345 X->s = Y->s;
346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (X->n < i) {
348 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i));
349 } else {
350 memset(X->p + i, 0, (X->n - i) * ciL);
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100351 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 memcpy(X->p, Y->p, i * ciL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000354
355cleanup:
356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000358}
359
360/*
361 * Swap the contents of X and Y
362 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100363void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000364{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 MPI_VALIDATE(X != NULL);
367 MPI_VALIDATE(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 memcpy(&T, X, sizeof(mbedtls_mpi));
370 memcpy(X, Y, sizeof(mbedtls_mpi));
371 memcpy(Y, &T, sizeof(mbedtls_mpi));
Paul Bakker5121ce52009-01-03 21:22:43 +0000372}
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374static inline mbedtls_mpi_uint mpi_sint_abs(mbedtls_mpi_sint z)
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100375{
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if (z >= 0) {
377 return z;
378 }
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100379 /* Take care to handle the most negative value (-2^(biL-1)) correctly.
380 * A naive -z would have undefined behavior.
381 * Write this in a way that makes popular compilers happy (GCC, Clang,
382 * MSVC). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 return (mbedtls_mpi_uint) 0 - (mbedtls_mpi_uint) z;
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100384}
385
Paul Bakker5121ce52009-01-03 21:22:43 +0000386/*
387 * Set value from integer
388 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100389int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z)
Paul Bakker5121ce52009-01-03 21:22:43 +0000390{
Janos Follath24eed8d2019-11-22 13:21:35 +0000391 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, 1));
395 memset(X->p, 0, X->n * ciL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 X->p[0] = mpi_sint_abs(z);
398 X->s = (z < 0) ? -1 : 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000399
400cleanup:
401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000403}
404
405/*
Paul Bakker2f5947e2011-05-18 15:47:11 +0000406 * Get a specific bit
407 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100408int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
Paul Bakker2f5947e2011-05-18 15:47:11 +0000409{
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 MPI_VALIDATE_RET(X != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if (X->n * biL <= pos) {
413 return 0;
414 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 return (X->p[pos / biL] >> (pos % biL)) & 0x01;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000417}
418
419/*
420 * Set a bit to a specific value of 0 or 1
421 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100422int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val)
Paul Bakker2f5947e2011-05-18 15:47:11 +0000423{
424 int ret = 0;
425 size_t off = pos / biL;
426 size_t idx = pos % biL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 MPI_VALIDATE_RET(X != NULL);
Paul Bakker2f5947e2011-05-18 15:47:11 +0000428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if (val != 0 && val != 1) {
430 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000431 }
432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if (X->n * biL <= pos) {
434 if (val == 0) {
435 return 0;
436 }
437
438 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, off + 1));
439 }
440
441 X->p[off] &= ~((mbedtls_mpi_uint) 0x01 << idx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 X->p[off] |= (mbedtls_mpi_uint) val << idx;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000443
444cleanup:
Paul Bakker9af723c2014-05-01 13:03:14 +0200445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 return ret;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000447}
448
449/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200450 * Return the number of less significant zero-bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100452size_t mbedtls_mpi_lsb(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000453{
Paul Bakker23986e52011-04-24 08:57:21 +0000454 size_t i, j, count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 MBEDTLS_INTERNAL_VALIDATE_RET(X != NULL, 0);
Paul Bakker5121ce52009-01-03 21:22:43 +0000456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 for (i = 0; i < X->n; i++) {
458 for (j = 0; j < biL; j++, count++) {
459 if (((X->p[i] >> j) & 1) != 0) {
460 return count;
461 }
462 }
463 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000466}
467
468/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200469 * Return the number of bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000470 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100471size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000472{
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 return mbedtls_mpi_core_bitlen(X->p, X->n);
Paul Bakker5121ce52009-01-03 21:22:43 +0000474}
475
476/*
477 * Return the total size in bytes
478 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100479size_t mbedtls_mpi_size(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000480{
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 return (mbedtls_mpi_bitlen(X) + 7) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000482}
483
484/*
485 * Convert an ASCII character to digit value
486 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100487static int mpi_get_digit(mbedtls_mpi_uint *d, int radix, char c)
Paul Bakker5121ce52009-01-03 21:22:43 +0000488{
489 *d = 255;
490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 if (c >= 0x30 && c <= 0x39) {
492 *d = c - 0x30;
493 }
494 if (c >= 0x41 && c <= 0x46) {
495 *d = c - 0x37;
496 }
497 if (c >= 0x61 && c <= 0x66) {
498 *d = c - 0x57;
499 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 if (*d >= (mbedtls_mpi_uint) radix) {
502 return MBEDTLS_ERR_MPI_INVALID_CHARACTER;
503 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000506}
507
508/*
509 * Import from an ASCII string
510 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100511int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s)
Paul Bakker5121ce52009-01-03 21:22:43 +0000512{
Janos Follath24eed8d2019-11-22 13:21:35 +0000513 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000514 size_t i, j, slen, n;
Gilles Peskine80f56732021-04-03 18:26:13 +0200515 int sign = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 mbedtls_mpi_uint d;
517 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 MPI_VALIDATE_RET(X != NULL);
519 MPI_VALIDATE_RET(s != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 if (radix < 2 || radix > 16) {
522 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskine7cba8592021-06-08 18:32:34 +0200523 }
524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 mbedtls_mpi_init(&T);
526
527 if (s[0] == 0) {
528 mbedtls_mpi_free(X);
529 return 0;
530 }
531
532 if (s[0] == '-') {
Gilles Peskine80f56732021-04-03 18:26:13 +0200533 ++s;
534 sign = -1;
535 }
536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 slen = strlen(s);
Paul Bakkerff60ee62010-03-16 21:09:09 +0000538
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 if (radix == 16) {
Dave Rodgman68ef1d62023-05-18 20:49:03 +0100540 if (slen > SIZE_MAX >> 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Paul Bakker5121ce52009-01-03 21:22:43 +0000542 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 n = BITS_TO_LIMBS(slen << 2);
545
546 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n));
547 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
548
549 for (i = slen, j = 0; i > 0; i--, j++) {
550 MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i - 1]));
551 X->p[j / (2 * ciL)] |= d << ((j % (2 * ciL)) << 2);
552 }
553 } else {
554 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
555
556 for (i = 0; i < slen; i++) {
557 MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i]));
558 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T, X, radix));
559 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, &T, d));
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 }
561 }
562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 if (sign < 0 && mbedtls_mpi_bitlen(X) != 0) {
Gilles Peskine80f56732021-04-03 18:26:13 +0200564 X->s = -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 }
Gilles Peskine80f56732021-04-03 18:26:13 +0200566
Paul Bakker5121ce52009-01-03 21:22:43 +0000567cleanup:
568
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000572}
573
574/*
Ron Eldora16fa292018-11-20 14:07:01 +0200575 * Helper to write the digits high-order first.
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100577static int mpi_write_hlp(mbedtls_mpi *X, int radix,
578 char **p, const size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000579{
Janos Follath24eed8d2019-11-22 13:21:35 +0000580 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_mpi_uint r;
Ron Eldora16fa292018-11-20 14:07:01 +0200582 size_t length = 0;
583 char *p_end = *p + buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 do {
586 if (length >= buflen) {
587 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
Ron Eldora16fa292018-11-20 14:07:01 +0200588 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, radix));
591 MBEDTLS_MPI_CHK(mbedtls_mpi_div_int(X, NULL, X, radix));
Ron Eldora16fa292018-11-20 14:07:01 +0200592 /*
593 * Write the residue in the current position, as an ASCII character.
594 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 if (r < 0xA) {
596 *(--p_end) = (char) ('0' + r);
597 } else {
598 *(--p_end) = (char) ('A' + (r - 0xA));
599 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
Ron Eldora16fa292018-11-20 14:07:01 +0200601 length++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 } while (mbedtls_mpi_cmp_int(X, 0) != 0);
Paul Bakker5121ce52009-01-03 21:22:43 +0000603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 memmove(*p, p_end, length);
Ron Eldora16fa292018-11-20 14:07:01 +0200605 *p += length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
607cleanup:
608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000610}
611
612/*
613 * Export into an ASCII string
614 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100615int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
616 char *buf, size_t buflen, size_t *olen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000617{
Paul Bakker23986e52011-04-24 08:57:21 +0000618 int ret = 0;
619 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 MPI_VALIDATE_RET(X != NULL);
623 MPI_VALIDATE_RET(olen != NULL);
624 MPI_VALIDATE_RET(buflen == 0 || buf != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 if (radix < 2 || radix > 16) {
627 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
628 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 n = mbedtls_mpi_bitlen(X); /* Number of bits necessary to present `n`. */
631 if (radix >= 4) {
632 n >>= 1; /* Number of 4-adic digits necessary to present
Hanno Becker23cfea02019-02-04 09:45:07 +0000633 * `n`. If radix > 4, this might be a strict
634 * overapproximation of the number of
635 * radix-adic digits needed to present `n`. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 }
637 if (radix >= 16) {
638 n >>= 1; /* Number of hexadecimal digits necessary to
Hanno Becker23cfea02019-02-04 09:45:07 +0000639 * present `n`. */
640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 }
Janos Follath80470622019-03-06 13:43:02 +0000642 n += 1; /* Terminating null byte */
Hanno Becker23cfea02019-02-04 09:45:07 +0000643 n += 1; /* Compensate for the divisions above, which round down `n`
644 * in case it's not even. */
645 n += 1; /* Potential '-'-sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 n += (n & 1); /* Make n even to have enough space for hexadecimal writing,
Hanno Becker23cfea02019-02-04 09:45:07 +0000647 * which always uses an even number of hex-digits. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000648
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if (buflen < n) {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100650 *olen = n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000652 }
653
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100654 p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000656
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 if (X->s == -1) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000658 *p++ = '-';
Hanno Beckerc983c812019-02-01 16:41:30 +0000659 buflen--;
660 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 if (radix == 16) {
Paul Bakker23986e52011-04-24 08:57:21 +0000663 int c;
664 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 for (i = X->n, k = 0; i > 0; i--) {
667 for (j = ciL; j > 0; j--) {
668 c = (X->p[i - 1] >> ((j - 1) << 3)) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (c == 0 && k == 0 && (i + j) != 2) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000673
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000674 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000675 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000676 k = 1;
677 }
678 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 } else {
680 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T, X));
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 if (T.s == -1) {
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000683 T.s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 MBEDTLS_MPI_CHK(mpi_write_hlp(&T, radix, &p, buflen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000687 }
688
689 *p++ = '\0';
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100690 *olen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000691
692cleanup:
693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000697}
698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000700/*
701 * Read X from an opened file
702 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100703int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin)
Paul Bakker5121ce52009-01-03 21:22:43 +0000704{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 mbedtls_mpi_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000706 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000707 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000708 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000709 * Buffer should have space for (short) label and decimal formatted MPI,
710 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000711 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 MPI_VALIDATE_RET(X != NULL);
715 MPI_VALIDATE_RET(fin != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 if (radix < 2 || radix > 16) {
718 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
719 }
Hanno Becker73d7d792018-12-11 10:35:51 +0000720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 memset(s, 0, sizeof(s));
722 if (fgets(s, sizeof(s) - 1, fin) == NULL) {
723 return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
724 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000725
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 slen = strlen(s);
727 if (slen == sizeof(s) - 2) {
728 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
729 }
Paul Bakkercb37aa52011-11-30 16:00:20 +0000730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 if (slen > 0 && s[slen - 1] == '\n') {
732 slen--; s[slen] = '\0';
733 }
734 if (slen > 0 && s[slen - 1] == '\r') {
735 slen--; s[slen] = '\0';
736 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000737
738 p = s + slen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 while (p-- > s) {
740 if (mpi_get_digit(&d, radix, *p) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000741 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 }
743 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000744
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 return mbedtls_mpi_read_string(X, radix, p + 1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000746}
747
748/*
749 * Write X into an opened file (or stdout if fout == NULL)
750 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100751int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE *fout)
Paul Bakker5121ce52009-01-03 21:22:43 +0000752{
Janos Follath24eed8d2019-11-22 13:21:35 +0000753 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000754 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000755 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000756 * Buffer should have space for (short) label and decimal formatted MPI,
757 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000758 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
760 MPI_VALIDATE_RET(X != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 if (radix < 2 || radix > 16) {
763 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
764 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000765
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 memset(s, 0, sizeof(s));
Paul Bakker5121ce52009-01-03 21:22:43 +0000767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 MBEDTLS_MPI_CHK(mbedtls_mpi_write_string(X, radix, s, sizeof(s) - 2, &n));
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 if (p == NULL) {
771 p = "";
772 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 plen = strlen(p);
775 slen = strlen(s);
Paul Bakker5121ce52009-01-03 21:22:43 +0000776 s[slen++] = '\r';
777 s[slen++] = '\n';
778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 if (fout != NULL) {
780 if (fwrite(p, 1, plen, fout) != plen ||
781 fwrite(s, 1, slen, fout) != slen) {
782 return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
783 }
784 } else {
785 mbedtls_printf("%s%s", p, s);
Paul Bakker5121ce52009-01-03 21:22:43 +0000786 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000787
788cleanup:
789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000791}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000793
794/*
Janos Follatha778a942019-02-13 10:28:28 +0000795 * Import X from unsigned binary data, little endian
Przemyslaw Stekiel76960a72022-02-21 13:42:09 +0100796 *
797 * This function is guaranteed to return an MPI with exactly the necessary
798 * number of limbs (in particular, it does not skip 0s in the input).
Janos Follatha778a942019-02-13 10:28:28 +0000799 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800int mbedtls_mpi_read_binary_le(mbedtls_mpi *X,
801 const unsigned char *buf, size_t buflen)
Janos Follatha778a942019-02-13 10:28:28 +0000802{
Janos Follath24eed8d2019-11-22 13:21:35 +0000803 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 const size_t limbs = CHARS_TO_LIMBS(buflen);
Janos Follatha778a942019-02-13 10:28:28 +0000805
806 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
Janos Follatha778a942019-02-13 10:28:28 +0000808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_le(X->p, X->n, buf, buflen));
Janos Follatha778a942019-02-13 10:28:28 +0000810
811cleanup:
812
Janos Follath171a7ef2019-02-15 16:17:45 +0000813 /*
814 * This function is also used to import keys. However, wiping the buffers
815 * upon failure is not necessary because failure only can happen before any
816 * input is copied.
817 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 return ret;
Janos Follatha778a942019-02-13 10:28:28 +0000819}
820
821/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000822 * Import X from unsigned binary data, big endian
Przemyslaw Stekiel76960a72022-02-21 13:42:09 +0100823 *
824 * This function is guaranteed to return an MPI with exactly the necessary
825 * number of limbs (in particular, it does not skip 0s in the input).
Paul Bakker5121ce52009-01-03 21:22:43 +0000826 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100827int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000828{
Janos Follath24eed8d2019-11-22 13:21:35 +0000829 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 const size_t limbs = CHARS_TO_LIMBS(buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 MPI_VALIDATE_RET(X != NULL);
833 MPI_VALIDATE_RET(buflen == 0 || buf != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000834
Hanno Becker073c1992017-10-17 15:17:27 +0100835 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
Paul Bakker5121ce52009-01-03 21:22:43 +0000837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_be(X->p, X->n, buf, buflen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000839
840cleanup:
841
Janos Follath171a7ef2019-02-15 16:17:45 +0000842 /*
843 * This function is also used to import keys. However, wiping the buffers
844 * upon failure is not necessary because failure only can happen before any
845 * input is copied.
846 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000848}
849
850/*
Janos Follathe344d0f2019-02-19 16:17:40 +0000851 * Export X into unsigned binary data, little endian
852 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100853int mbedtls_mpi_write_binary_le(const mbedtls_mpi *X,
854 unsigned char *buf, size_t buflen)
Janos Follathe344d0f2019-02-19 16:17:40 +0000855{
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 return mbedtls_mpi_core_write_le(X->p, X->n, buf, buflen);
Janos Follathe344d0f2019-02-19 16:17:40 +0000857}
858
859/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000860 * Export X into unsigned binary data, big endian
861 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862int mbedtls_mpi_write_binary(const mbedtls_mpi *X,
863 unsigned char *buf, size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000864{
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 return mbedtls_mpi_core_write_be(X->p, X->n, buf, buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000866}
867
868/*
869 * Left-shift: X <<= count
870 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100871int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
Paul Bakker5121ce52009-01-03 21:22:43 +0000872{
Janos Follath24eed8d2019-11-22 13:21:35 +0000873 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Minos Galanakis0144b352023-05-02 14:02:32 +0100874 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000876
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 i = mbedtls_mpi_bitlen(X) + count;
Paul Bakker5121ce52009-01-03 21:22:43 +0000878
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 if (X->n * biL < i) {
880 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, BITS_TO_LIMBS(i)));
881 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000882
883 ret = 0;
884
Minos Galanakis0144b352023-05-02 14:02:32 +0100885 mbedtls_mpi_core_shift_l(X->p, X->n, count);
Paul Bakker5121ce52009-01-03 21:22:43 +0000886cleanup:
887
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000889}
890
891/*
892 * Right-shift: X >>= count
893 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100894int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
Paul Bakker5121ce52009-01-03 21:22:43 +0000895{
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 MPI_VALIDATE_RET(X != NULL);
897 if (X->n != 0) {
898 mbedtls_mpi_core_shift_r(X->p, X->n, count);
899 }
900 return 0;
Gilles Peskine66414202022-09-21 15:36:16 +0200901}
902
Paul Bakker5121ce52009-01-03 21:22:43 +0000903/*
904 * Compare unsigned values
905 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100906int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000907{
Paul Bakker23986e52011-04-24 08:57:21 +0000908 size_t i, j;
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 MPI_VALIDATE_RET(X != NULL);
910 MPI_VALIDATE_RET(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 for (i = X->n; i > 0; i--) {
913 if (X->p[i - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000914 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000916 }
917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 for (j = Y->n; j > 0; j--) {
919 if (Y->p[j - 1] != 0) {
920 break;
921 }
922 }
923
924 if (i == 0 && j == 0) {
925 return 0;
926 }
927
928 if (i > j) {
929 return 1;
930 }
931 if (j > i) {
932 return -1;
933 }
934
935 for (; i > 0; i--) {
936 if (X->p[i - 1] > Y->p[i - 1]) {
937 return 1;
938 }
939 if (X->p[i - 1] < Y->p[i - 1]) {
940 return -1;
941 }
942 }
943
944 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000945}
946
947/*
948 * Compare signed values
949 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100950int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000951{
Paul Bakker23986e52011-04-24 08:57:21 +0000952 size_t i, j;
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 MPI_VALIDATE_RET(X != NULL);
954 MPI_VALIDATE_RET(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 for (i = X->n; i > 0; i--) {
957 if (X->p[i - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000958 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000960 }
961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 for (j = Y->n; j > 0; j--) {
963 if (Y->p[j - 1] != 0) {
964 break;
965 }
966 }
967
968 if (i == 0 && j == 0) {
969 return 0;
970 }
971
972 if (i > j) {
973 return X->s;
974 }
975 if (j > i) {
976 return -Y->s;
977 }
978
979 if (X->s > 0 && Y->s < 0) {
980 return 1;
981 }
982 if (Y->s > 0 && X->s < 0) {
983 return -1;
984 }
985
986 for (; i > 0; i--) {
987 if (X->p[i - 1] > Y->p[i - 1]) {
988 return X->s;
989 }
990 if (X->p[i - 1] < Y->p[i - 1]) {
991 return -X->s;
992 }
993 }
994
995 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000996}
997
Janos Follathee6abce2019-09-05 14:47:19 +0100998/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 * Compare signed values
1000 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001001int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
Paul Bakker5121ce52009-01-03 21:22:43 +00001002{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 mbedtls_mpi Y;
1004 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 *p = mpi_sint_abs(z);
1008 Y.s = (z < 0) ? -1 : 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001009 Y.n = 1;
1010 Y.p = p;
1011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 return mbedtls_mpi_cmp_mpi(X, &Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00001013}
1014
1015/*
1016 * Unsigned addition: X = |A| + |B| (HAC 14.7)
1017 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001018int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001019{
Janos Follath24eed8d2019-11-22 13:21:35 +00001020 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001021 size_t j;
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 MPI_VALIDATE_RET(X != NULL);
1023 MPI_VALIDATE_RET(A != NULL);
1024 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001025
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 if (X == B) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 const mbedtls_mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001028 }
1029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 if (X != A) {
1031 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
1032 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001033
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001034 /*
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001035 * X must always be positive as a result of unsigned additions.
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001036 */
1037 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001038
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 for (j = B->n; j > 0; j--) {
1040 if (B->p[j - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001041 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 }
1043 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001044
Gilles Peskinedb14a9d2022-11-15 22:59:00 +01001045 /* Exit early to avoid undefined behavior on NULL+0 when X->n == 0
1046 * and B is 0 (of any size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 if (j == 0) {
1048 return 0;
1049 }
Gilles Peskinedb14a9d2022-11-15 22:59:00 +01001050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j));
Paul Bakker5121ce52009-01-03 21:22:43 +00001052
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001053 /* j is the number of non-zero limbs of B. Add those to X. */
Paul Bakker5121ce52009-01-03 21:22:43 +00001054
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001055 mbedtls_mpi_uint *p = X->p;
1056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 mbedtls_mpi_uint c = mbedtls_mpi_core_add(p, p, B->p, j);
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001058
1059 p += j;
1060
1061 /* Now propagate any carry */
Paul Bakker5121ce52009-01-03 21:22:43 +00001062
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 while (c != 0) {
1064 if (j >= X->n) {
1065 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j + 1));
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001066 p = X->p + j;
Paul Bakker5121ce52009-01-03 21:22:43 +00001067 }
1068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 *p += c; c = (*p < c); j++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001070 }
1071
1072cleanup:
1073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001075}
1076
Paul Bakker5121ce52009-01-03 21:22:43 +00001077/*
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001078 * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
Paul Bakker5121ce52009-01-03 21:22:43 +00001079 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001080int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001081{
Janos Follath24eed8d2019-11-22 13:21:35 +00001082 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001083 size_t n;
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001084 mbedtls_mpi_uint carry;
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 MPI_VALIDATE_RET(X != NULL);
1086 MPI_VALIDATE_RET(A != NULL);
1087 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 for (n = B->n; n > 0; n--) {
1090 if (B->p[n - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 }
1093 }
1094 if (n > A->n) {
Gilles Peskinec8a91772021-01-27 22:30:43 +01001095 /* B >= (2^ciL)^n > A */
1096 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1097 goto cleanup;
1098 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001099
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, A->n));
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001101
1102 /* Set the high limbs of X to match A. Don't touch the lower limbs
1103 * because X might be aliased to B, and we must not overwrite the
1104 * significant digits of B. */
Aaron M. Uckoaf67d2c2023-01-17 11:52:22 -05001105 if (A->n > n && A != X) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 memcpy(X->p + n, A->p + n, (A->n - n) * ciL);
1107 }
1108 if (X->n > A->n) {
1109 memset(X->p + A->n, 0, (X->n - A->n) * ciL);
1110 }
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 carry = mbedtls_mpi_core_sub(X->p, A->p, B->p, n);
1113 if (carry != 0) {
Tom Cosgrove452c99c2022-08-25 10:07:07 +01001114 /* Propagate the carry through the rest of X. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 carry = mbedtls_mpi_core_sub_int(X->p + n, X->p + n, carry, X->n - n);
Tom Cosgrove452c99c2022-08-25 10:07:07 +01001116
1117 /* If we have further carry/borrow, the result is negative. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 if (carry != 0) {
Gilles Peskine89b41302020-07-23 01:16:46 +02001119 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1120 goto cleanup;
1121 }
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001122 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001123
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001124 /* X should always be positive as a result of unsigned subtractions. */
1125 X->s = 1;
1126
Paul Bakker5121ce52009-01-03 21:22:43 +00001127cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001129}
1130
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001131/* Common function for signed addition and subtraction.
1132 * Calculate A + B * flip_B where flip_B is 1 or -1.
Paul Bakker5121ce52009-01-03 21:22:43 +00001133 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001134static int add_sub_mpi(mbedtls_mpi *X,
1135 const mbedtls_mpi *A, const mbedtls_mpi *B,
1136 int flip_B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001137{
Hanno Becker73d7d792018-12-11 10:35:51 +00001138 int ret, s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 MPI_VALIDATE_RET(X != NULL);
1140 MPI_VALIDATE_RET(A != NULL);
1141 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001142
Hanno Becker73d7d792018-12-11 10:35:51 +00001143 s = A->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 if (A->s * B->s * flip_B < 0) {
1145 int cmp = mbedtls_mpi_cmp_abs(A, B);
1146 if (cmp >= 0) {
1147 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, A, B));
Gilles Peskine4a768dd2022-11-09 22:02:16 +01001148 /* If |A| = |B|, the result is 0 and we must set the sign bit
1149 * to +1 regardless of which of A or B was negative. Otherwise,
1150 * since |A| > |B|, the sign is the sign of A. */
1151 X->s = cmp == 0 ? 1 : s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 } else {
1153 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, B, A));
Gilles Peskine4a768dd2022-11-09 22:02:16 +01001154 /* Since |A| < |B|, the sign is the opposite of A. */
Paul Bakker5121ce52009-01-03 21:22:43 +00001155 X->s = -s;
1156 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 } else {
1158 MBEDTLS_MPI_CHK(mbedtls_mpi_add_abs(X, A, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001159 X->s = s;
1160 }
1161
1162cleanup:
1163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001165}
1166
1167/*
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001168 * Signed addition: X = A + B
1169 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001170int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001171{
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 return add_sub_mpi(X, A, B, 1);
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001173}
1174
1175/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001176 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001177 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001178int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001179{
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 return add_sub_mpi(X, A, B, -1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001181}
1182
1183/*
1184 * Signed addition: X = A + b
1185 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001186int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001187{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001188 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 MPI_VALIDATE_RET(X != NULL);
1191 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 p[0] = mpi_sint_abs(b);
1194 B.s = (b < 0) ? -1 : 1;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001195 B.n = 1;
1196 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001197
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 return mbedtls_mpi_add_mpi(X, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001199}
1200
1201/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001202 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001203 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001204int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001205{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001206 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 MPI_VALIDATE_RET(X != NULL);
1209 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001210
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 p[0] = mpi_sint_abs(b);
1212 B.s = (b < 0) ? -1 : 1;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001213 B.n = 1;
1214 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 return mbedtls_mpi_sub_mpi(X, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001217}
1218
Paul Bakker5121ce52009-01-03 21:22:43 +00001219/*
1220 * Baseline multiplication: X = A * B (HAC 14.12)
1221 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001222int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001223{
Janos Follath24eed8d2019-11-22 13:21:35 +00001224 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker1772e052022-04-13 06:51:40 +01001225 size_t i, j;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 mbedtls_mpi TA, TB;
Hanno Beckerda763de2022-04-13 06:50:02 +01001227 int result_is_zero = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 MPI_VALIDATE_RET(X != NULL);
1229 MPI_VALIDATE_RET(A != NULL);
1230 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001231
Tom Cosgrove6af26f32022-08-24 16:40:55 +01001232 mbedtls_mpi_init(&TA);
1233 mbedtls_mpi_init(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00001234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 if (X == A) {
1236 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A)); A = &TA;
1237 }
1238 if (X == B) {
1239 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B)); B = &TB;
1240 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 for (i = A->n; i > 0; i--) {
1243 if (A->p[i - 1] != 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001244 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 }
1246 }
1247 if (i == 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001248 result_is_zero = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 }
Hanno Beckerda763de2022-04-13 06:50:02 +01001250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 for (j = B->n; j > 0; j--) {
1252 if (B->p[j - 1] != 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001253 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 }
1255 }
1256 if (j == 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001257 result_is_zero = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 }
Hanno Beckerda763de2022-04-13 06:50:02 +01001259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i + j));
1261 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
Paul Bakker5121ce52009-01-03 21:22:43 +00001262
Tom Cosgrove6af26f32022-08-24 16:40:55 +01001263 mbedtls_mpi_core_mul(X->p, A->p, i, B->p, j);
Paul Bakker5121ce52009-01-03 21:22:43 +00001264
Hanno Beckerda763de2022-04-13 06:50:02 +01001265 /* If the result is 0, we don't shortcut the operation, which reduces
1266 * but does not eliminate side channels leaking the zero-ness. We do
1267 * need to take care to set the sign bit properly since the library does
1268 * not fully support an MPI object with a value of 0 and s == -1. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 if (result_is_zero) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001270 X->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 } else {
Hanno Beckerda763de2022-04-13 06:50:02 +01001272 X->s = A->s * B->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001274
1275cleanup:
1276
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TA);
Paul Bakker5121ce52009-01-03 21:22:43 +00001278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001280}
1281
1282/*
1283 * Baseline multiplication: X = A * b
1284 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001285int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001286{
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 MPI_VALIDATE_RET(X != NULL);
1288 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001289
Hanno Becker35771312022-04-14 11:52:11 +01001290 size_t n = A->n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 while (n > 0 && A->p[n - 1] == 0) {
Hanno Becker35771312022-04-14 11:52:11 +01001292 --n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 }
Hanno Becker35771312022-04-14 11:52:11 +01001294
Hanno Becker74a11a32022-04-06 06:27:00 +01001295 /* The general method below doesn't work if b==0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 if (b == 0 || n == 0) {
1297 return mbedtls_mpi_lset(X, 0);
1298 }
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001299
Hanno Beckeraef9cc42022-04-11 06:36:29 +01001300 /* Calculate A*b as A + A*(b-1) to take advantage of mbedtls_mpi_core_mla */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001301 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001302 /* In general, A * b requires 1 limb more than b. If
1303 * A->p[n - 1] * b / b == A->p[n - 1], then A * b fits in the same
1304 * number of limbs as A and the call to grow() is not required since
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001305 * copy() will take care of the growth if needed. However, experimentally,
1306 * making the call to grow() unconditional causes slightly fewer
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001307 * calls to calloc() in ECP code, presumably because it reuses the
1308 * same mpi for a while and this way the mpi is more likely to directly
Hanno Becker9137b9c2022-04-12 10:51:54 +01001309 * grow to its final size.
1310 *
1311 * Note that calculating A*b as 0 + A*b doesn't work as-is because
1312 * A,X can be the same. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n + 1));
1314 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
1315 mbedtls_mpi_core_mla(X->p, X->n, A->p, n, b - 1);
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001316
1317cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001319}
1320
1321/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001322 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1323 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001324 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001325static mbedtls_mpi_uint mbedtls_int_div_int(mbedtls_mpi_uint u1,
1326 mbedtls_mpi_uint u0,
1327 mbedtls_mpi_uint d,
1328 mbedtls_mpi_uint *r)
Simon Butcher15b15d12015-11-26 19:35:03 +00001329{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001330#if defined(MBEDTLS_HAVE_UDBL)
1331 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001332#else
Simon Butcher9803d072016-01-03 00:24:34 +00001333 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 const mbedtls_mpi_uint uint_halfword_mask = ((mbedtls_mpi_uint) 1 << biH) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001335 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1336 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001337 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001338#endif
1339
Simon Butcher15b15d12015-11-26 19:35:03 +00001340 /*
1341 * Check for overflow
1342 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 if (0 == d || u1 >= d) {
1344 if (r != NULL) {
1345 *r = ~(mbedtls_mpi_uint) 0u;
1346 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 return ~(mbedtls_mpi_uint) 0u;
Simon Butcher15b15d12015-11-26 19:35:03 +00001349 }
1350
1351#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001352 dividend = (mbedtls_t_udbl) u1 << biL;
1353 dividend |= (mbedtls_t_udbl) u0;
1354 quotient = dividend / d;
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 if (quotient > ((mbedtls_t_udbl) 1 << biL) - 1) {
1356 quotient = ((mbedtls_t_udbl) 1 << biL) - 1;
1357 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 if (r != NULL) {
1360 *r = (mbedtls_mpi_uint) (dividend - (quotient * d));
1361 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001362
1363 return (mbedtls_mpi_uint) quotient;
1364#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001365
1366 /*
1367 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1368 * Vol. 2 - Seminumerical Algorithms, Knuth
1369 */
1370
1371 /*
1372 * Normalize the divisor, d, and dividend, u0, u1
1373 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 s = mbedtls_mpi_core_clz(d);
Simon Butcher15b15d12015-11-26 19:35:03 +00001375 d = d << s;
1376
1377 u1 = u1 << s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 u1 |= (u0 >> (biL - s)) & (-(mbedtls_mpi_sint) s >> (biL - 1));
Simon Butcher15b15d12015-11-26 19:35:03 +00001379 u0 = u0 << s;
1380
1381 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001382 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001383
1384 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001385 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001386
1387 /*
1388 * Find the first quotient and remainder
1389 */
1390 q1 = u1 / d1;
1391 r0 = u1 - d1 * q1;
1392
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 while (q1 >= radix || (q1 * d0 > radix * r0 + u0_msw)) {
Simon Butcher15b15d12015-11-26 19:35:03 +00001394 q1 -= 1;
1395 r0 += d1;
1396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 if (r0 >= radix) {
1398 break;
1399 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001400 }
1401
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 rAX = (u1 * radix) + (u0_msw - q1 * d);
Simon Butcher15b15d12015-11-26 19:35:03 +00001403 q0 = rAX / d1;
1404 r0 = rAX - q0 * d1;
1405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 while (q0 >= radix || (q0 * d0 > radix * r0 + u0_lsw)) {
Simon Butcher15b15d12015-11-26 19:35:03 +00001407 q0 -= 1;
1408 r0 += d1;
1409
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 if (r0 >= radix) {
1411 break;
1412 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001413 }
1414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 if (r != NULL) {
1416 *r = (rAX * radix + u0_lsw - q0 * d) >> s;
1417 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001418
1419 quotient = q1 * radix + q0;
1420
1421 return quotient;
1422#endif
1423}
1424
1425/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001428int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1429 const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001430{
Janos Follath24eed8d2019-11-22 13:21:35 +00001431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001432 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 mbedtls_mpi X, Y, Z, T1, T2;
Alexander Kd19a1932019-11-01 18:20:42 +03001434 mbedtls_mpi_uint TP2[3];
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 MPI_VALIDATE_RET(A != NULL);
1436 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 if (mbedtls_mpi_cmp_int(B, 0) == 0) {
1439 return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
1440 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001441
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z);
1443 mbedtls_mpi_init(&T1);
Alexander Kd19a1932019-11-01 18:20:42 +03001444 /*
1445 * Avoid dynamic memory allocations for constant-size T2.
1446 *
1447 * T2 is used for comparison only and the 3 limbs are assigned explicitly,
1448 * so nobody increase the size of the MPI and we're safe to use an on-stack
1449 * buffer.
1450 */
Alexander K35d6d462019-10-31 14:46:45 +03001451 T2.s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 T2.n = sizeof(TP2) / sizeof(*TP2);
Alexander Kd19a1932019-11-01 18:20:42 +03001453 T2.p = TP2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 if (mbedtls_mpi_cmp_abs(A, B) < 0) {
1456 if (Q != NULL) {
1457 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(Q, 0));
1458 }
1459 if (R != NULL) {
1460 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, A));
1461 }
1462 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001463 }
1464
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&X, A));
1466 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001467 X.s = Y.s = 1;
1468
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&Z, A->n + 2));
1470 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Z, 0));
1471 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T1, A->n + 2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001472
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 k = mbedtls_mpi_bitlen(&Y) % biL;
1474 if (k < biL - 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001475 k = biL - 1 - k;
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&X, k));
1477 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, k));
1478 } else {
1479 k = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001480 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001481
1482 n = X.n - 1;
1483 t = Y.n - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, biL * (n - t)));
Paul Bakker5121ce52009-01-03 21:22:43 +00001485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 while (mbedtls_mpi_cmp_mpi(&X, &Y) >= 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001487 Z.p[n - t]++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &Y));
Paul Bakker5121ce52009-01-03 21:22:43 +00001489 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, biL * (n - t)));
Paul Bakker5121ce52009-01-03 21:22:43 +00001491
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 for (i = n; i > t; i--) {
1493 if (X.p[i] >= Y.p[t]) {
1494 Z.p[i - t - 1] = ~(mbedtls_mpi_uint) 0u;
1495 } else {
1496 Z.p[i - t - 1] = mbedtls_int_div_int(X.p[i], X.p[i - 1],
1497 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001498 }
1499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 T2.p[0] = (i < 2) ? 0 : X.p[i - 2];
1501 T2.p[1] = (i < 1) ? 0 : X.p[i - 1];
Alexander K35d6d462019-10-31 14:46:45 +03001502 T2.p[2] = X.p[i];
1503
Paul Bakker5121ce52009-01-03 21:22:43 +00001504 Z.p[i - t - 1]++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 do {
Paul Bakker5121ce52009-01-03 21:22:43 +00001506 Z.p[i - t - 1]--;
1507
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&T1, 0));
1509 T1.p[0] = (t < 1) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001510 T1.p[1] = Y.p[t];
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &T1, Z.p[i - t - 1]));
1512 } while (mbedtls_mpi_cmp_mpi(&T1, &T2) > 0);
Paul Bakker5121ce52009-01-03 21:22:43 +00001513
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &Y, Z.p[i - t - 1]));
1515 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
1516 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &T1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001517
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 if (mbedtls_mpi_cmp_int(&X, 0) < 0) {
1519 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T1, &Y));
1520 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
1521 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&X, &X, &T1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001522 Z.p[i - t - 1]--;
1523 }
1524 }
1525
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 if (Q != NULL) {
1527 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(Q, &Z));
Paul Bakker5121ce52009-01-03 21:22:43 +00001528 Q->s = A->s * B->s;
1529 }
1530
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 if (R != NULL) {
1532 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&X, k));
Paul Bakkerf02c5642012-11-13 10:25:21 +00001533 X.s = A->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, &X));
Paul Bakker5121ce52009-01-03 21:22:43 +00001535
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 if (mbedtls_mpi_cmp_int(R, 0) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001537 R->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001539 }
1540
1541cleanup:
1542
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z);
1544 mbedtls_mpi_free(&T1);
1545 mbedtls_platform_zeroize(TP2, sizeof(TP2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001546
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001548}
1549
1550/*
1551 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00001552 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001553int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R,
1554 const mbedtls_mpi *A,
1555 mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001556{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001557 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001560
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 p[0] = mpi_sint_abs(b);
1562 B.s = (b < 0) ? -1 : 1;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001563 B.n = 1;
1564 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 return mbedtls_mpi_div_mpi(Q, R, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001567}
1568
1569/*
1570 * Modulo: R = A mod B
1571 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001572int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001573{
Janos Follath24eed8d2019-11-22 13:21:35 +00001574 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 MPI_VALIDATE_RET(R != NULL);
1576 MPI_VALIDATE_RET(A != NULL);
1577 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001578
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 if (mbedtls_mpi_cmp_int(B, 0) < 0) {
1580 return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1581 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001582
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(NULL, R, A, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001584
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 while (mbedtls_mpi_cmp_int(R, 0) < 0) {
1586 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(R, R, B));
1587 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001588
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 while (mbedtls_mpi_cmp_mpi(R, B) >= 0) {
1590 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(R, R, B));
1591 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001592
1593cleanup:
1594
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001596}
1597
1598/*
1599 * Modulo: r = A mod b
1600 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001601int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001602{
Paul Bakker23986e52011-04-24 08:57:21 +00001603 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 mbedtls_mpi_uint x, y, z;
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 MPI_VALIDATE_RET(r != NULL);
1606 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001607
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 if (b == 0) {
1609 return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
1610 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001611
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 if (b < 0) {
1613 return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1614 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001615
1616 /*
1617 * handle trivial cases
1618 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 if (b == 1 || A->n == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001620 *r = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001622 }
1623
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 if (b == 2) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001625 *r = A->p[0] & 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001627 }
1628
1629 /*
1630 * general case
1631 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 for (i = A->n, y = 0; i > 0; i--) {
Paul Bakker23986e52011-04-24 08:57:21 +00001633 x = A->p[i - 1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 y = (y << biH) | (x >> biH);
Paul Bakker5121ce52009-01-03 21:22:43 +00001635 z = y / b;
1636 y -= z * b;
1637
1638 x <<= biH;
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 y = (y << biH) | (x >> biH);
Paul Bakker5121ce52009-01-03 21:22:43 +00001640 z = y / b;
1641 y -= z * b;
1642 }
1643
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001644 /*
1645 * If A is negative, then the current y represents a negative value.
1646 * Flipping it to the positive side.
1647 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 if (A->s < 0 && y != 0) {
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001649 y = b - y;
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001651
Paul Bakker5121ce52009-01-03 21:22:43 +00001652 *r = y;
1653
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001655}
1656
Gilles Peskine449bd832023-01-11 14:50:10 +01001657static void mpi_montg_init(mbedtls_mpi_uint *mm, const mbedtls_mpi *N)
Paul Bakker5121ce52009-01-03 21:22:43 +00001658{
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 *mm = mbedtls_mpi_core_montmul_init(N->p);
Paul Bakker5121ce52009-01-03 21:22:43 +00001660}
1661
Tom Cosgrove93842842022-08-05 16:59:43 +01001662/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
1663 *
1664 * \param[in,out] A One of the numbers to multiply.
1665 * It must have at least as many limbs as N
1666 * (A->n >= N->n), and any limbs beyond n are ignored.
1667 * On successful completion, A contains the result of
1668 * the multiplication A * B * R^-1 mod N where
1669 * R = (2^ciL)^n.
1670 * \param[in] B One of the numbers to multiply.
1671 * It must be nonzero and must not have more limbs than N
1672 * (B->n <= N->n).
Tom Cosgrove40d22942022-08-17 06:42:44 +01001673 * \param[in] N The modulus. \p N must be odd.
Tom Cosgrove93842842022-08-05 16:59:43 +01001674 * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
1675 * This is -N^-1 mod 2^ciL.
1676 * \param[in,out] T A bignum for temporary storage.
1677 * It must be at least twice the limb size of N plus 1
1678 * (T->n >= 2 * N->n + 1).
1679 * Its initial content is unused and
1680 * its final content is indeterminate.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +01001681 * It does not get reallocated.
Tom Cosgrove93842842022-08-05 16:59:43 +01001682 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001683static void mpi_montmul(mbedtls_mpi *A, const mbedtls_mpi *B,
1684 const mbedtls_mpi *N, mbedtls_mpi_uint mm,
1685 mbedtls_mpi *T)
Paul Bakker5121ce52009-01-03 21:22:43 +00001686{
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 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 +00001688}
1689
1690/*
1691 * Montgomery reduction: A = A * R^-1 mod N
Gilles Peskine2a82f722020-06-04 15:00:49 +02001692 *
Tom Cosgrove93842842022-08-05 16:59:43 +01001693 * See mpi_montmul() regarding constraints and guarantees on the parameters.
Paul Bakker5121ce52009-01-03 21:22:43 +00001694 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001695static void mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N,
1696 mbedtls_mpi_uint mm, mbedtls_mpi *T)
Paul Bakker5121ce52009-01-03 21:22:43 +00001697{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 mbedtls_mpi_uint z = 1;
1699 mbedtls_mpi U;
Paul Bakker5121ce52009-01-03 21:22:43 +00001700
Paul Bakker8ddb6452013-02-27 14:56:33 +01001701 U.n = U.s = (int) z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001702 U.p = &z;
1703
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 mpi_montmul(A, &U, N, mm, T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001705}
1706
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001707/**
1708 * Select an MPI from a table without leaking the index.
1709 *
1710 * This is functionally equivalent to mbedtls_mpi_copy(R, T[idx]) except it
1711 * reads the entire table in order to avoid leaking the value of idx to an
1712 * attacker able to observe memory access patterns.
1713 *
1714 * \param[out] R Where to write the selected MPI.
1715 * \param[in] T The table to read from.
1716 * \param[in] T_size The number of elements in the table.
1717 * \param[in] idx The index of the element to select;
1718 * this must satisfy 0 <= idx < T_size.
1719 *
1720 * \return \c 0 on success, or a negative error code.
1721 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001722static 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 +01001723{
1724 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1725
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 for (size_t i = 0; i < T_size; i++) {
1727 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(R, &T[i],
Dave Rodgmanee54faf2023-05-17 13:56:33 +01001728 (unsigned char) mbedtls_ct_bool_eq(i, idx)));
Manuel Pégourié-Gonnard92413ef2021-06-03 10:42:46 +02001729 }
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001730cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 return ret;
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001732}
1733
Paul Bakker5121ce52009-01-03 21:22:43 +00001734/*
1735 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
1736 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001737int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
1738 const mbedtls_mpi *E, const mbedtls_mpi *N,
1739 mbedtls_mpi *prec_RR)
Paul Bakker5121ce52009-01-03 21:22:43 +00001740{
Janos Follath24eed8d2019-11-22 13:21:35 +00001741 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath74601202022-11-21 15:54:20 +00001742 size_t window_bitsize;
Paul Bakker23986e52011-04-24 08:57:21 +00001743 size_t i, j, nblimbs;
1744 size_t bufsize, nbits;
Paul Elliott1748de12023-02-13 15:35:35 +00001745 size_t exponent_bits_in_window = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 mbedtls_mpi_uint ei, mm, state;
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 mbedtls_mpi RR, T, W[(size_t) 1 << MBEDTLS_MPI_WINDOW_SIZE], WW, Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00001748 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001749
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 MPI_VALIDATE_RET(X != NULL);
1751 MPI_VALIDATE_RET(A != NULL);
1752 MPI_VALIDATE_RET(E != NULL);
1753 MPI_VALIDATE_RET(N != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00001754
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 if (mbedtls_mpi_cmp_int(N, 0) <= 0 || (N->p[0] & 1) == 0) {
1756 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1757 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001758
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 if (mbedtls_mpi_cmp_int(E, 0) < 0) {
1760 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1761 }
Paul Bakkerf6198c12012-05-16 08:02:29 +00001762
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 if (mbedtls_mpi_bitlen(E) > MBEDTLS_MPI_MAX_BITS ||
1764 mbedtls_mpi_bitlen(N) > MBEDTLS_MPI_MAX_BITS) {
1765 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1766 }
Chris Jones9246d042020-11-25 15:12:39 +00001767
Paul Bakkerf6198c12012-05-16 08:02:29 +00001768 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001769 * Init temps and window size
1770 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 mpi_montg_init(&mm, N);
1772 mbedtls_mpi_init(&RR); mbedtls_mpi_init(&T);
1773 mbedtls_mpi_init(&Apos);
1774 mbedtls_mpi_init(&WW);
1775 memset(W, 0, sizeof(W));
Paul Bakker5121ce52009-01-03 21:22:43 +00001776
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 i = mbedtls_mpi_bitlen(E);
Paul Bakker5121ce52009-01-03 21:22:43 +00001778
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 window_bitsize = (i > 671) ? 6 : (i > 239) ? 5 :
1780 (i > 79) ? 4 : (i > 23) ? 3 : 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001781
Gilles Peskine449bd832023-01-11 14:50:10 +01001782#if (MBEDTLS_MPI_WINDOW_SIZE < 6)
1783 if (window_bitsize > MBEDTLS_MPI_WINDOW_SIZE) {
Janos Follath7fa11b82022-11-21 14:48:02 +00001784 window_bitsize = MBEDTLS_MPI_WINDOW_SIZE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 }
Peter Kolbuse6bcad32018-12-11 14:01:44 -06001786#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00001787
Janos Follathc8d66d52022-11-22 10:47:10 +00001788 const size_t w_table_used_size = (size_t) 1 << window_bitsize;
Janos Follath06000952022-11-22 10:18:06 +00001789
Paul Bakker5121ce52009-01-03 21:22:43 +00001790 /*
Janos Follathbe54ca72022-11-21 16:14:54 +00001791 * This function is not constant-trace: its memory accesses depend on the
1792 * exponent value. To defend against timing attacks, callers (such as RSA
1793 * and DHM) should use exponent blinding. However this is not enough if the
1794 * adversary can find the exponent in a single trace, so this function
1795 * takes extra precautions against adversaries who can observe memory
1796 * access patterns.
Janos Follathf08b40e2022-11-11 15:56:38 +00001797 *
Janos Follathbe54ca72022-11-21 16:14:54 +00001798 * This function performs a series of multiplications by table elements and
1799 * squarings, and we want the prevent the adversary from finding out which
1800 * table element was used, and from distinguishing between multiplications
1801 * and squarings. Firstly, when multiplying by an element of the window
1802 * W[i], we do a constant-trace table lookup to obfuscate i. This leaves
1803 * squarings as having a different memory access patterns from other
1804 * multiplications. So secondly, we put the accumulator X in the table as
1805 * well, and also do a constant-trace table lookup to multiply by X.
1806 *
1807 * This way, all multiplications take the form of a lookup-and-multiply.
1808 * The number of lookup-and-multiply operations inside each iteration of
1809 * the main loop still depends on the bits of the exponent, but since the
1810 * other operations in the loop don't have an easily recognizable memory
1811 * trace, an adversary is unlikely to be able to observe the exact
1812 * patterns.
1813 *
1814 * An adversary may still be able to recover the exponent if they can
1815 * observe both memory accesses and branches. However, branch prediction
1816 * exploitation typically requires many traces of execution over the same
1817 * data, which is defeated by randomized blinding.
Janos Follath84461482022-11-21 14:31:22 +00001818 *
1819 * To achieve this, we make a copy of X and we use the table entry in each
1820 * calculation from this point on.
Janos Follath8e7d6a02022-10-04 13:27:40 +01001821 */
Janos Follathc8d66d52022-11-22 10:47:10 +00001822 const size_t x_index = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 mbedtls_mpi_init(&W[x_index]);
1824 mbedtls_mpi_copy(&W[x_index], X);
Janos Follath84461482022-11-21 14:31:22 +00001825
Paul Bakker5121ce52009-01-03 21:22:43 +00001826 j = N->n + 1;
Tom Cosgrove93842842022-08-05 16:59:43 +01001827 /* All W[i] and X must have at least N->n limbs for the mpi_montmul()
Paul Bakker5121ce52009-01-03 21:22:43 +00001828 * and mpi_montred() calls later. Here we ensure that W[1] and X are
1829 * large enough, and later we'll grow other W[i] to the same length.
1830 * They must not be shrunk midway through this function!
1831 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[x_index], j));
1833 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[1], j));
1834 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T, j * 2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001835
1836 /*
Paul Bakker50546922012-05-19 08:40:49 +00001837 * Compensate for negative A (and correct at the end)
1838 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 neg = (A->s == -1);
1840 if (neg) {
1841 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Apos, A));
Paul Bakker50546922012-05-19 08:40:49 +00001842 Apos.s = 1;
1843 A = &Apos;
1844 }
1845
1846 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001847 * If 1st call, pre-compute R^2 mod N
1848 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 if (prec_RR == NULL || prec_RR->p == NULL) {
1850 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&RR, 1));
1851 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&RR, N->n * 2 * biL));
1852 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&RR, &RR, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001853
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 if (prec_RR != NULL) {
1855 memcpy(prec_RR, &RR, sizeof(mbedtls_mpi));
1856 }
1857 } else {
1858 memcpy(&RR, prec_RR, sizeof(mbedtls_mpi));
Paul Bakker5121ce52009-01-03 21:22:43 +00001859 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001860
1861 /*
1862 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
1863 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 if (mbedtls_mpi_cmp_mpi(A, N) >= 0) {
1865 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&W[1], A, N));
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001866 /* This should be a no-op because W[1] is already that large before
1867 * mbedtls_mpi_mod_mpi(), but it's necessary to avoid an overflow
Tom Cosgrove93842842022-08-05 16:59:43 +01001868 * in mpi_montmul() below, so let's make sure. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[1], N->n + 1));
1870 } else {
1871 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[1], A));
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001872 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001873
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001874 /* Note that this is safe because W[1] always has at least N->n limbs
1875 * (it grew above and was preserved by mbedtls_mpi_copy()). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 mpi_montmul(&W[1], &RR, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001877
1878 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01001879 * W[x_index] = R^2 * R^-1 mod N = R mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001880 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[x_index], &RR));
1882 mpi_montred(&W[x_index], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001883
Janos Follathc8d66d52022-11-22 10:47:10 +00001884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 if (window_bitsize > 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001886 /*
Janos Follath74601202022-11-21 15:54:20 +00001887 * W[i] = W[1] ^ i
1888 *
1889 * The first bit of the sliding window is always 1 and therefore we
1890 * only need to store the second half of the table.
Janos Follathc8d66d52022-11-22 10:47:10 +00001891 *
1892 * (There are two special elements in the table: W[0] for the
1893 * accumulator/result and W[1] for A in Montgomery form. Both of these
1894 * are already set at this point.)
Paul Bakker5121ce52009-01-03 21:22:43 +00001895 */
Janos Follath74601202022-11-21 15:54:20 +00001896 j = w_table_used_size / 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001897
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[j], N->n + 1));
1899 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[j], &W[1]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001900
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 for (i = 0; i < window_bitsize - 1; i++) {
1902 mpi_montmul(&W[j], &W[j], N, mm, &T);
1903 }
Paul Bakker0d7702c2013-10-29 16:18:35 +01001904
Paul Bakker5121ce52009-01-03 21:22:43 +00001905 /*
1906 * W[i] = W[i - 1] * W[1]
1907 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 for (i = j + 1; i < w_table_used_size; i++) {
1909 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[i], N->n + 1));
1910 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[i], &W[i - 1]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001911
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 mpi_montmul(&W[i], &W[1], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001913 }
1914 }
1915
1916 nblimbs = E->n;
1917 bufsize = 0;
1918 nbits = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001919 state = 0;
1920
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 while (1) {
1922 if (bufsize == 0) {
1923 if (nblimbs == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001924 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001926
Paul Bakker0d7702c2013-10-29 16:18:35 +01001927 nblimbs--;
1928
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 bufsize = sizeof(mbedtls_mpi_uint) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00001930 }
1931
1932 bufsize--;
1933
1934 ei = (E->p[nblimbs] >> bufsize) & 1;
1935
1936 /*
1937 * skip leading 0s
1938 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 if (ei == 0 && state == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001940 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001942
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 if (ei == 0 && state == 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001944 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01001945 * out of window, square W[x_index]
Paul Bakker5121ce52009-01-03 21:22:43 +00001946 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, x_index));
1948 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001949 continue;
1950 }
1951
1952 /*
1953 * add ei to current window
1954 */
1955 state = 2;
1956
1957 nbits++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 exponent_bits_in_window |= (ei << (window_bitsize - nbits));
Paul Bakker5121ce52009-01-03 21:22:43 +00001959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 if (nbits == window_bitsize) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001961 /*
Janos Follath7fa11b82022-11-21 14:48:02 +00001962 * W[x_index] = W[x_index]^window_bitsize R^-1 mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001963 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 for (i = 0; i < window_bitsize; i++) {
1965 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size,
1966 x_index));
1967 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Janos Follathb764ee12022-10-04 14:00:09 +01001968 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001969
1970 /*
Janos Follath7fa11b82022-11-21 14:48:02 +00001971 * W[x_index] = W[x_index] * W[exponent_bits_in_window] R^-1 mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size,
1974 exponent_bits_in_window));
1975 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001976
1977 state--;
1978 nbits = 0;
Janos Follath7fa11b82022-11-21 14:48:02 +00001979 exponent_bits_in_window = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001980 }
1981 }
1982
1983 /*
1984 * process the remaining bits
1985 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 for (i = 0; i < nbits; i++) {
1987 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, x_index));
1988 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001989
Janos Follath7fa11b82022-11-21 14:48:02 +00001990 exponent_bits_in_window <<= 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001991
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 if ((exponent_bits_in_window & ((size_t) 1 << window_bitsize)) != 0) {
1993 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, 1));
1994 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Janos Follathb764ee12022-10-04 14:00:09 +01001995 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001996 }
1997
1998 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01001999 * W[x_index] = A^E * R * R^-1 mod N = A^E mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00002000 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 mpi_montred(&W[x_index], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00002002
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 if (neg && E->n != 0 && (E->p[0] & 1) != 0) {
Janos Follath8e7d6a02022-10-04 13:27:40 +01002004 W[x_index].s = -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&W[x_index], N, &W[x_index]));
Paul Bakkerf6198c12012-05-16 08:02:29 +00002006 }
2007
Janos Follath8e7d6a02022-10-04 13:27:40 +01002008 /*
2009 * Load the result in the output variable.
2010 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 mbedtls_mpi_copy(X, &W[x_index]);
Janos Follath8e7d6a02022-10-04 13:27:40 +01002012
Paul Bakker5121ce52009-01-03 21:22:43 +00002013cleanup:
2014
Janos Follathb2c2fca2022-11-21 15:05:31 +00002015 /* The first bit of the sliding window is always 1 and therefore the first
2016 * half of the table was unused. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 for (i = w_table_used_size/2; i < w_table_used_size; i++) {
2018 mbedtls_mpi_free(&W[i]);
2019 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002020
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 mbedtls_mpi_free(&W[x_index]);
2022 mbedtls_mpi_free(&W[1]);
2023 mbedtls_mpi_free(&T);
2024 mbedtls_mpi_free(&Apos);
2025 mbedtls_mpi_free(&WW);
Paul Bakker6c591fa2011-05-05 11:49:20 +00002026
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 if (prec_RR == NULL || prec_RR->p == NULL) {
2028 mbedtls_mpi_free(&RR);
2029 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002030
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002032}
2033
Paul Bakker5121ce52009-01-03 21:22:43 +00002034/*
2035 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
2036 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002037int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00002038{
Janos Follath24eed8d2019-11-22 13:21:35 +00002039 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002040 size_t lz, lzt;
Alexander Ke8ad49f2019-08-16 16:16:07 +03002041 mbedtls_mpi TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00002042
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 MPI_VALIDATE_RET(G != NULL);
2044 MPI_VALIDATE_RET(A != NULL);
2045 MPI_VALIDATE_RET(B != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00002046
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00002048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A));
2050 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00002051
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 lz = mbedtls_mpi_lsb(&TA);
2053 lzt = mbedtls_mpi_lsb(&TB);
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002054
Gilles Peskine27253bc2021-06-09 13:26:43 +02002055 /* The loop below gives the correct result when A==0 but not when B==0.
2056 * So have a special case for B==0. Leverage the fact that we just
2057 * calculated the lsb and lsb(B)==0 iff B is odd or 0 to make the test
2058 * slightly more efficient than cmp_int(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 if (lzt == 0 && mbedtls_mpi_get_bit(&TB, 0) == 0) {
2060 ret = mbedtls_mpi_copy(G, A);
Gilles Peskine27253bc2021-06-09 13:26:43 +02002061 goto cleanup;
2062 }
2063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 if (lzt < lz) {
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002065 lz = lzt;
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002067
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 TA.s = TB.s = 1;
2069
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002070 /* We mostly follow the procedure described in HAC 14.54, but with some
2071 * minor differences:
2072 * - Sequences of multiplications or divisions by 2 are grouped into a
2073 * single shift operation.
Gilles Peskineb09c7ee2021-06-21 18:58:39 +02002074 * - The procedure in HAC assumes that 0 < TB <= TA.
2075 * - The condition TB <= TA is not actually necessary for correctness.
2076 * TA and TB have symmetric roles except for the loop termination
2077 * condition, and the shifts at the beginning of the loop body
2078 * remove any significance from the ordering of TA vs TB before
2079 * the shifts.
2080 * - If TA = 0, the loop goes through 0 iterations and the result is
2081 * correctly TB.
2082 * - The case TB = 0 was short-circuited above.
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002083 *
2084 * For the correctness proof below, decompose the original values of
2085 * A and B as
2086 * A = sa * 2^a * A' with A'=0 or A' odd, and sa = +-1
2087 * B = sb * 2^b * B' with B'=0 or B' odd, and sb = +-1
2088 * Then gcd(A, B) = 2^{min(a,b)} * gcd(A',B'),
2089 * and gcd(A',B') is odd or 0.
2090 *
2091 * At the beginning, we have TA = |A| and TB = |B| so gcd(A,B) = gcd(TA,TB).
2092 * The code maintains the following invariant:
2093 * gcd(A,B) = 2^k * gcd(TA,TB) for some k (I)
Gilles Peskine4df3f1f2021-06-15 22:09:39 +02002094 */
2095
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002096 /* Proof that the loop terminates:
2097 * At each iteration, either the right-shift by 1 is made on a nonzero
2098 * value and the nonnegative integer bitlen(TA) + bitlen(TB) decreases
2099 * by at least 1, or the right-shift by 1 is made on zero and then
2100 * TA becomes 0 which ends the loop (TB cannot be 0 if it is right-shifted
2101 * since in that case TB is calculated from TB-TA with the condition TB>TA).
2102 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 while (mbedtls_mpi_cmp_int(&TA, 0) != 0) {
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002104 /* Divisions by 2 preserve the invariant (I). */
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, mbedtls_mpi_lsb(&TA)));
2106 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, mbedtls_mpi_lsb(&TB)));
Paul Bakker5121ce52009-01-03 21:22:43 +00002107
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002108 /* Set either TA or TB to |TA-TB|/2. Since TA and TB are both odd,
2109 * TA-TB is even so the division by 2 has an integer result.
2110 * Invariant (I) is preserved since any odd divisor of both TA and TB
2111 * also divides |TA-TB|/2, and any odd divisor of both TA and |TA-TB|/2
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002112 * also divides TB, and any odd divisor of both TB and |TA-TB|/2 also
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002113 * divides TA.
2114 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 if (mbedtls_mpi_cmp_mpi(&TA, &TB) >= 0) {
2116 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TA, &TA, &TB));
2117 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, 1));
2118 } else {
2119 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TB, &TB, &TA));
2120 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 }
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002122 /* Note that one of TA or TB is still odd. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002123 }
2124
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002125 /* By invariant (I), gcd(A,B) = 2^k * gcd(TA,TB) for some k.
2126 * At the loop exit, TA = 0, so gcd(TA,TB) = TB.
2127 * - If there was at least one loop iteration, then one of TA or TB is odd,
2128 * and TA = 0, so TB is odd and gcd(TA,TB) = gcd(A',B'). In this case,
2129 * lz = min(a,b) so gcd(A,B) = 2^lz * TB.
2130 * - If there was no loop iteration, then A was 0, and gcd(A,B) = B.
Gilles Peskine4d3fd362021-06-21 11:40:38 +02002131 * 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 +02002132 */
2133
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&TB, lz));
2135 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(G, &TB));
Paul Bakker5121ce52009-01-03 21:22:43 +00002136
2137cleanup:
2138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00002140
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002142}
2143
Paul Bakker33dc46b2014-04-30 16:11:39 +02002144/*
2145 * Fill X with size bytes of random.
Gilles Peskine22cdd0c2022-10-27 20:15:13 +02002146 * The bytes returned from the RNG are used in a specific order which
2147 * is suitable for deterministic ECDSA (see the specification of
2148 * mbedtls_mpi_random() and the implementation in mbedtls_mpi_fill_random()).
Paul Bakker33dc46b2014-04-30 16:11:39 +02002149 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002150int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
2151 int (*f_rng)(void *, unsigned char *, size_t),
2152 void *p_rng)
Paul Bakker287781a2011-03-26 13:18:49 +00002153{
Janos Follath24eed8d2019-11-22 13:21:35 +00002154 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 const size_t limbs = CHARS_TO_LIMBS(size);
Hanno Beckerda1655a2017-10-18 14:21:44 +01002156
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 MPI_VALIDATE_RET(X != NULL);
2158 MPI_VALIDATE_RET(f_rng != NULL);
Paul Bakker33dc46b2014-04-30 16:11:39 +02002159
Hanno Beckerda1655a2017-10-18 14:21:44 +01002160 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
2162 if (size == 0) {
2163 return 0;
2164 }
Paul Bakker287781a2011-03-26 13:18:49 +00002165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 ret = mbedtls_mpi_core_fill_random(X->p, X->n, size, f_rng, p_rng);
Paul Bakker287781a2011-03-26 13:18:49 +00002167
2168cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 return ret;
Paul Bakker287781a2011-03-26 13:18:49 +00002170}
2171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172int mbedtls_mpi_random(mbedtls_mpi *X,
2173 mbedtls_mpi_sint min,
2174 const mbedtls_mpi *N,
2175 int (*f_rng)(void *, unsigned char *, size_t),
2176 void *p_rng)
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002177{
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 if (min < 0) {
2179 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2180 }
2181 if (mbedtls_mpi_cmp_int(N, min) <= 0) {
2182 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2183 }
Gilles Peskine1e918f42021-03-29 22:14:51 +02002184
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002185 /* Ensure that target MPI has exactly the same number of limbs
2186 * as the upper bound, even if the upper bound has leading zeros.
Gilles Peskine6b7ce962022-12-15 15:04:33 +01002187 * This is necessary for mbedtls_mpi_core_random. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 int ret = mbedtls_mpi_resize_clear(X, N->n);
2189 if (ret != 0) {
2190 return ret;
2191 }
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002192
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 return mbedtls_mpi_core_random(X->p, min, N->p, X->n, f_rng, p_rng);
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002194}
2195
Paul Bakker5121ce52009-01-03 21:22:43 +00002196/*
2197 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2198 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002199int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
Paul Bakker5121ce52009-01-03 21:22:43 +00002200{
Janos Follath24eed8d2019-11-22 13:21:35 +00002201 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 MPI_VALIDATE_RET(X != NULL);
2204 MPI_VALIDATE_RET(A != NULL);
2205 MPI_VALIDATE_RET(N != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00002206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 if (mbedtls_mpi_cmp_int(N, 1) <= 0) {
2208 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2209 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TU); mbedtls_mpi_init(&U1); mbedtls_mpi_init(&U2);
2212 mbedtls_mpi_init(&G); mbedtls_mpi_init(&TB); mbedtls_mpi_init(&TV);
2213 mbedtls_mpi_init(&V1); mbedtls_mpi_init(&V2);
Paul Bakker5121ce52009-01-03 21:22:43 +00002214
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, A, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 goto cleanup;
2220 }
2221
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&TA, A, N));
2223 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TU, &TA));
2224 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, N));
2225 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TV, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002226
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U1, 1));
2228 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U2, 0));
2229 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V1, 0));
2230 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002231
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 do {
2233 while ((TU.p[0] & 1) == 0) {
2234 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TU, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002235
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 if ((U1.p[0] & 1) != 0 || (U2.p[0] & 1) != 0) {
2237 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&U1, &U1, &TB));
2238 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &TA));
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 }
2240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U1, 1));
2242 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002243 }
2244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 while ((TV.p[0] & 1) == 0) {
2246 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TV, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 if ((V1.p[0] & 1) != 0 || (V2.p[0] & 1) != 0) {
2249 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, &TB));
2250 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &TA));
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 }
2252
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V1, 1));
2254 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 }
2256
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 if (mbedtls_mpi_cmp_mpi(&TU, &TV) >= 0) {
2258 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TU, &TU, &TV));
2259 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U1, &U1, &V1));
2260 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &V2));
2261 } else {
2262 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TV, &TV, &TU));
2263 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, &U1));
2264 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &U2));
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 } while (mbedtls_mpi_cmp_int(&TU, 0) != 0);
2267
2268 while (mbedtls_mpi_cmp_int(&V1, 0) < 0) {
2269 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002270 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002271
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 while (mbedtls_mpi_cmp_mpi(&V1, N) >= 0) {
2273 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, N));
2274 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002275
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, &V1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002277
2278cleanup:
2279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TU); mbedtls_mpi_free(&U1); mbedtls_mpi_free(&U2);
2281 mbedtls_mpi_free(&G); mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TV);
2282 mbedtls_mpi_free(&V1); mbedtls_mpi_free(&V2);
Paul Bakker5121ce52009-01-03 21:22:43 +00002283
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002285}
2286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002288
Paul Bakker5121ce52009-01-03 21:22:43 +00002289static const int small_prime[] =
2290{
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 3, 5, 7, 11, 13, 17, 19, 23,
2292 29, 31, 37, 41, 43, 47, 53, 59,
2293 61, 67, 71, 73, 79, 83, 89, 97,
2294 101, 103, 107, 109, 113, 127, 131, 137,
2295 139, 149, 151, 157, 163, 167, 173, 179,
2296 181, 191, 193, 197, 199, 211, 223, 227,
2297 229, 233, 239, 241, 251, 257, 263, 269,
2298 271, 277, 281, 283, 293, 307, 311, 313,
2299 317, 331, 337, 347, 349, 353, 359, 367,
2300 373, 379, 383, 389, 397, 401, 409, 419,
2301 421, 431, 433, 439, 443, 449, 457, 461,
2302 463, 467, 479, 487, 491, 499, 503, 509,
2303 521, 523, 541, 547, 557, 563, 569, 571,
2304 577, 587, 593, 599, 601, 607, 613, 617,
2305 619, 631, 641, 643, 647, 653, 659, 661,
2306 673, 677, 683, 691, 701, 709, 719, 727,
2307 733, 739, 743, 751, 757, 761, 769, 773,
2308 787, 797, 809, 811, 821, 823, 827, 829,
2309 839, 853, 857, 859, 863, 877, 881, 883,
2310 887, 907, 911, 919, 929, 937, 941, 947,
2311 953, 967, 971, 977, 983, 991, 997, -103
Paul Bakker5121ce52009-01-03 21:22:43 +00002312};
2313
2314/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002315 * Small divisors test (X must be positive)
2316 *
2317 * Return values:
2318 * 0: no small factor (possible prime, more tests needed)
2319 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002321 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002322 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002323static int mpi_check_small_factors(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +00002324{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002325 int ret = 0;
2326 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 mbedtls_mpi_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +00002328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 if ((X->p[0] & 1) == 0) {
2330 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2331 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 for (i = 0; small_prime[i] > 0; i++) {
2334 if (mbedtls_mpi_cmp_int(X, small_prime[i]) <= 0) {
2335 return 1;
2336 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, small_prime[i]));
Paul Bakker5121ce52009-01-03 21:22:43 +00002339
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 if (r == 0) {
2341 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2342 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002343 }
2344
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002345cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 return ret;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002347}
2348
2349/*
2350 * Miller-Rabin pseudo-primality test (HAC 4.24)
2351 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002352static int mpi_miller_rabin(const mbedtls_mpi *X, size_t rounds,
2353 int (*f_rng)(void *, unsigned char *, size_t),
2354 void *p_rng)
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002355{
Pascal Junodb99183d2015-03-11 16:49:45 +01002356 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002357 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002359
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 MPI_VALIDATE_RET(X != NULL);
2361 MPI_VALIDATE_RET(f_rng != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00002362
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 mbedtls_mpi_init(&W); mbedtls_mpi_init(&R);
2364 mbedtls_mpi_init(&T); mbedtls_mpi_init(&A);
2365 mbedtls_mpi_init(&RR);
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002366
Paul Bakker5121ce52009-01-03 21:22:43 +00002367 /*
2368 * W = |X| - 1
2369 * R = W >> lsb( W )
2370 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&W, X, 1));
2372 s = mbedtls_mpi_lsb(&W);
2373 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R, &W));
2374 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&R, s));
Paul Bakker5121ce52009-01-03 21:22:43 +00002375
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 for (i = 0; i < rounds; i++) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002377 /*
2378 * pick a random A, 1 < A < |X| - 1
2379 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002380 count = 0;
2381 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&A, X->n * ciL, f_rng, p_rng));
Pascal Junodb99183d2015-03-11 16:49:45 +01002383
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 j = mbedtls_mpi_bitlen(&A);
2385 k = mbedtls_mpi_bitlen(&W);
Pascal Junodb99183d2015-03-11 16:49:45 +01002386 if (j > k) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 A.p[A.n - 1] &= ((mbedtls_mpi_uint) 1 << (k - (A.n - 1) * biL - 1)) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002388 }
2389
2390 if (count++ > 30) {
Jens Wiklanderf08aa3e2019-01-17 13:30:57 +01002391 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2392 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002393 }
2394
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 } while (mbedtls_mpi_cmp_mpi(&A, &W) >= 0 ||
2396 mbedtls_mpi_cmp_int(&A, 1) <= 0);
Paul Bakker5121ce52009-01-03 21:22:43 +00002397
2398 /*
2399 * A = A^R mod |X|
2400 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&A, &A, &R, X, &RR));
Paul Bakker5121ce52009-01-03 21:22:43 +00002402
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 if (mbedtls_mpi_cmp_mpi(&A, &W) == 0 ||
2404 mbedtls_mpi_cmp_int(&A, 1) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002405 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002407
2408 j = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 while (j < s && mbedtls_mpi_cmp_mpi(&A, &W) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 /*
2411 * A = A * A mod |X|
2412 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &A, &A));
2414 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&A, &T, X));
Paul Bakker5121ce52009-01-03 21:22:43 +00002415
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 if (mbedtls_mpi_cmp_int(&A, 1) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002417 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002419
2420 j++;
2421 }
2422
2423 /*
2424 * not prime if A != |X| - 1 or A == 1
2425 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 if (mbedtls_mpi_cmp_mpi(&A, &W) != 0 ||
2427 mbedtls_mpi_cmp_int(&A, 1) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 break;
2430 }
2431 }
2432
2433cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 mbedtls_mpi_free(&W); mbedtls_mpi_free(&R);
2435 mbedtls_mpi_free(&T); mbedtls_mpi_free(&A);
2436 mbedtls_mpi_free(&RR);
Paul Bakker5121ce52009-01-03 21:22:43 +00002437
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002439}
2440
2441/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002442 * Pseudo-primality test: small factors, then Miller-Rabin
2443 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002444int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
2445 int (*f_rng)(void *, unsigned char *, size_t),
2446 void *p_rng)
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002447{
Janos Follath24eed8d2019-11-22 13:21:35 +00002448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449 mbedtls_mpi XX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 MPI_VALIDATE_RET(X != NULL);
2451 MPI_VALIDATE_RET(f_rng != NULL);
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002452
2453 XX.s = 1;
2454 XX.n = X->n;
2455 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 if (mbedtls_mpi_cmp_int(&XX, 0) == 0 ||
2458 mbedtls_mpi_cmp_int(&XX, 1) == 0) {
2459 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002460 }
2461
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 if (mbedtls_mpi_cmp_int(&XX, 2) == 0) {
2463 return 0;
2464 }
2465
2466 if ((ret = mpi_check_small_factors(&XX)) != 0) {
2467 if (ret == 1) {
2468 return 0;
2469 }
2470
2471 return ret;
2472 }
2473
2474 return mpi_miller_rabin(&XX, rounds, f_rng, p_rng);
Janos Follathf301d232018-08-14 13:34:01 +01002475}
2476
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002477/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002478 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002479 *
Janos Follathf301d232018-08-14 13:34:01 +01002480 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2481 * be either 1024 bits or 1536 bits long, and flags must contain
2482 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002483 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002484int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
2485 int (*f_rng)(void *, unsigned char *, size_t),
2486 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00002487{
Jethro Beekman66689272018-02-14 19:24:10 -08002488#ifdef MBEDTLS_HAVE_INT64
2489// ceil(2^63.5)
2490#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2491#else
2492// ceil(2^31.5)
2493#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2494#endif
2495 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002496 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002497 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 mbedtls_mpi_uint r;
2499 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002500
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 MPI_VALIDATE_RET(X != NULL);
2502 MPI_VALIDATE_RET(f_rng != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00002503
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 if (nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS) {
2505 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2506 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 mbedtls_mpi_init(&Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00002509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 n = BITS_TO_LIMBS(nbits);
Paul Bakker5121ce52009-01-03 21:22:43 +00002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR) == 0) {
Janos Follathda31fa12018-09-03 14:45:23 +01002513 /*
2514 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2515 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 rounds = ((nbits >= 1300) ? 2 : (nbits >= 850) ? 3 :
2517 (nbits >= 650) ? 4 : (nbits >= 350) ? 8 :
2518 (nbits >= 250) ? 12 : (nbits >= 150) ? 18 : 27);
2519 } else {
Janos Follathda31fa12018-09-03 14:45:23 +01002520 /*
2521 * 2^-100 error probability, number of rounds computed based on HAC,
2522 * fact 4.48
2523 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 rounds = ((nbits >= 1450) ? 4 : (nbits >= 1150) ? 5 :
2525 (nbits >= 1000) ? 6 : (nbits >= 850) ? 7 :
2526 (nbits >= 750) ? 8 : (nbits >= 500) ? 13 :
2527 (nbits >= 250) ? 28 : (nbits >= 150) ? 40 : 51);
Janos Follathda31fa12018-09-03 14:45:23 +01002528 }
2529
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 while (1) {
2531 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(X, n * ciL, f_rng, p_rng));
Jethro Beekman66689272018-02-14 19:24:10 -08002532 /* 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 +01002533 if (X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2) {
2534 continue;
2535 }
Jethro Beekman66689272018-02-14 19:24:10 -08002536
2537 k = n * biL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 if (k > nbits) {
2539 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(X, k - nbits));
2540 }
Jethro Beekman66689272018-02-14 19:24:10 -08002541 X->p[0] |= 1;
2542
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH) == 0) {
2544 ret = mbedtls_mpi_is_prime_ext(X, rounds, f_rng, p_rng);
Jethro Beekman66689272018-02-14 19:24:10 -08002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002547 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 }
2549 } else {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002550 /*
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01002551 * A necessary condition for Y and X = 2Y + 1 to be prime
Jethro Beekman66689272018-02-14 19:24:10 -08002552 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2553 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002554 */
Jethro Beekman66689272018-02-14 19:24:10 -08002555
2556 X->p[0] |= 2;
2557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, 3));
2559 if (r == 0) {
2560 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 8));
2561 } else if (r == 1) {
2562 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 4));
2563 }
Jethro Beekman66689272018-02-14 19:24:10 -08002564
2565 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, X));
2567 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, 1));
Jethro Beekman66689272018-02-14 19:24:10 -08002568
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 while (1) {
Jethro Beekman66689272018-02-14 19:24:10 -08002570 /*
2571 * First, check small factors for X and Y
2572 * before doing Miller-Rabin on any of them
2573 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 if ((ret = mpi_check_small_factors(X)) == 0 &&
2575 (ret = mpi_check_small_factors(&Y)) == 0 &&
2576 (ret = mpi_miller_rabin(X, rounds, f_rng, p_rng))
2577 == 0 &&
2578 (ret = mpi_miller_rabin(&Y, rounds, f_rng, p_rng))
2579 == 0) {
Jethro Beekman66689272018-02-14 19:24:10 -08002580 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 }
Jethro Beekman66689272018-02-14 19:24:10 -08002582
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Jethro Beekman66689272018-02-14 19:24:10 -08002584 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 }
Jethro Beekman66689272018-02-14 19:24:10 -08002586
2587 /*
2588 * Next candidates. We want to preserve Y = (X-1) / 2 and
2589 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2590 * so up Y by 6 and X by 12.
2591 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 12));
2593 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&Y, &Y, 6));
Paul Bakker5121ce52009-01-03 21:22:43 +00002594 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002595 }
2596 }
2597
2598cleanup:
2599
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 mbedtls_mpi_free(&Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00002601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002603}
2604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002608
Paul Bakker23986e52011-04-24 08:57:21 +00002609#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002610
2611static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2612{
2613 { 693, 609, 21 },
2614 { 1764, 868, 28 },
2615 { 768454923, 542167814, 1 }
2616};
2617
Paul Bakker5121ce52009-01-03 21:22:43 +00002618/*
2619 * Checkup routine
2620 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002621int mbedtls_mpi_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002622{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002623 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00002625
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 mbedtls_mpi_init(&A); mbedtls_mpi_init(&E); mbedtls_mpi_init(&N); mbedtls_mpi_init(&X);
2627 mbedtls_mpi_init(&Y); mbedtls_mpi_init(&U); mbedtls_mpi_init(&V);
Paul Bakker5121ce52009-01-03 21:22:43 +00002628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&A, 16,
2630 "EFE021C2645FD1DC586E69184AF4A31E" \
2631 "D5F53E93B5F123FA41680867BA110131" \
2632 "944FE7952E2517337780CB0DB80E61AA" \
2633 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002634
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&E, 16,
2636 "B2E7EFD37075B9F03FF989C7C5051C20" \
2637 "34D2A323810251127E7BF8625A4F49A5" \
2638 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
2639 "5B5C25763222FEFCCFC38B832366C29E"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002640
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&N, 16,
2642 "0066A198186C18C10B2F5ED9B522752A" \
2643 "9830B69916E535C8F047518A889A43A5" \
2644 "94B6BED27A168D31D4A52F88925AA8F5"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&X, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002647
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2649 "602AB7ECA597A3D6B56FF9829A5E8B85" \
2650 "9E857EA95A03512E2BAE7391688D264A" \
2651 "A5663B0341DB9CCFD2C4C5F421FEC814" \
2652 "8001B72E848A38CAE1C65F78E56ABDEF" \
2653 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
2654 "ECF677152EF804370C1A305CAF3B5BF1" \
2655 "30879B56C61DE584A0F53A2447A51E"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002656
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 if (verbose != 0) {
2658 mbedtls_printf(" MPI test #1 (mul_mpi): ");
2659 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2662 if (verbose != 0) {
2663 mbedtls_printf("failed\n");
2664 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002666 ret = 1;
2667 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002668 }
2669
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 if (verbose != 0) {
2671 mbedtls_printf("passed\n");
2672 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&X, &Y, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002675
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2677 "256567336059E52CAE22925474705F39A94"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002678
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&V, 16,
2680 "6613F26162223DF488E9CD48CC132C7A" \
2681 "0AC93C701B001B092E4E5B9F73BCD27B" \
2682 "9EE50D0657C77F374E903CDFA4C642"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 if (verbose != 0) {
2685 mbedtls_printf(" MPI test #2 (div_mpi): ");
2686 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002687
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0 ||
2689 mbedtls_mpi_cmp_mpi(&Y, &V) != 0) {
2690 if (verbose != 0) {
2691 mbedtls_printf("failed\n");
2692 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002693
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002694 ret = 1;
2695 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 }
2697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 if (verbose != 0) {
2699 mbedtls_printf("passed\n");
2700 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002701
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&X, &A, &E, &N, NULL));
Paul Bakker5121ce52009-01-03 21:22:43 +00002703
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2705 "36E139AEA55215609D2816998ED020BB" \
2706 "BD96C37890F65171D948E9BC7CBAA4D9" \
2707 "325D24D6A3C12710F10A09FA08AB87"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 if (verbose != 0) {
2710 mbedtls_printf(" MPI test #3 (exp_mod): ");
2711 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002712
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 if (mbedtls_mpi_cmp_mpi(&X, &U) != 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_inv_mod(&X, &A, &N));
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 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
2730 "C3DBA76456363A10869622EAC2DD84EC" \
2731 "C5B8A74DAC4D09E03B5E0BE779F2DF61"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002732
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 if (verbose != 0) {
2734 mbedtls_printf(" MPI test #4 (inv_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 if (verbose != 0) {
2751 mbedtls_printf(" MPI test #5 (simple gcd): ");
2752 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002753
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 for (i = 0; i < GCD_PAIR_COUNT; i++) {
2755 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&X, gcd_pairs[i][0]));
2756 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Y, gcd_pairs[i][1]));
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002757
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&A, &X, &Y));
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002759
Gilles Peskine449bd832023-01-11 14:50:10 +01002760 if (mbedtls_mpi_cmp_int(&A, gcd_pairs[i][2]) != 0) {
2761 if (verbose != 0) {
2762 mbedtls_printf("failed at %d\n", i);
2763 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002764
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002765 ret = 1;
2766 goto cleanup;
2767 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002768 }
2769
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 if (verbose != 0) {
2771 mbedtls_printf("passed\n");
2772 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002773
Paul Bakker5121ce52009-01-03 21:22:43 +00002774cleanup:
2775
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 if (ret != 0 && verbose != 0) {
2777 mbedtls_printf("Unexpected error, return code = %08X\n", (unsigned int) ret);
2778 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002779
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 mbedtls_mpi_free(&A); mbedtls_mpi_free(&E); mbedtls_mpi_free(&N); mbedtls_mpi_free(&X);
2781 mbedtls_mpi_free(&Y); mbedtls_mpi_free(&U); mbedtls_mpi_free(&V);
Paul Bakker5121ce52009-01-03 21:22:43 +00002782
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 if (verbose != 0) {
2784 mbedtls_printf("\n");
2785 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002786
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002788}
2789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792#endif /* MBEDTLS_BIGNUM_C */