blob: dd77bfc9fc23bb09d5c17d4857d0c312cb246076 [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{
64 size_t i;
65 /* The value of any of these variables is either 0 or 1 at all times. */
66 unsigned cond, done, X_is_negative, Y_is_negative;
67
68 MPI_VALIDATE_RET(X != NULL);
69 MPI_VALIDATE_RET(Y != NULL);
70 MPI_VALIDATE_RET(ret != NULL);
71
72 if (X->n != Y->n) {
73 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
74 }
75
76 /*
77 * Set sign_N to 1 if N >= 0, 0 if N < 0.
78 * We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0.
79 */
80 X_is_negative = (X->s & 2) >> 1;
81 Y_is_negative = (Y->s & 2) >> 1;
82
83 /*
84 * If the signs are different, then the positive operand is the bigger.
85 * That is if X is negative (X_is_negative == 1), then X < Y is true and it
86 * is false if X is positive (X_is_negative == 0).
87 */
88 cond = (X_is_negative ^ Y_is_negative);
89 *ret = cond & X_is_negative;
90
91 /*
92 * This is a constant-time function. We might have the result, but we still
93 * need to go through the loop. Record if we have the result already.
94 */
95 done = cond;
96
97 for (i = X->n; i > 0; i--) {
98 /*
99 * If Y->p[i - 1] < X->p[i - 1] then X < Y is true if and only if both
100 * X and Y are negative.
101 *
102 * Again even if we can make a decision, we just mark the result and
103 * the fact that we are done and continue looping.
104 */
105 cond = mbedtls_ct_mpi_uint_lt(Y->p[i - 1], X->p[i - 1]);
106 *ret |= cond & (1 - done) & X_is_negative;
107 done |= cond;
108
109 /*
110 * If X->p[i - 1] < Y->p[i - 1] then X < Y is true if and only if both
111 * X and Y are positive.
112 *
113 * Again even if we can make a decision, we just mark the result and
114 * the fact that we are done and continue looping.
115 */
116 cond = mbedtls_ct_mpi_uint_lt(X->p[i - 1], Y->p[i - 1]);
117 *ret |= cond & (1 - done) & (1 - X_is_negative);
118 done |= cond;
119 }
120
121 return 0;
122}
123
124/*
125 * Conditionally assign X = Y, without leaking information
126 * about whether the assignment was made or not.
127 * (Leaking information about the respective sizes of X and Y is ok however.)
128 */
129#if defined(_MSC_VER) && defined(_M_ARM64) && (_MSC_FULL_VER < 193131103)
130/*
131 * MSVC miscompiles this function if it's inlined prior to Visual Studio 2022 version 17.1. See:
132 * https://developercommunity.visualstudio.com/t/c-compiler-miscompiles-part-of-mbedtls-library-on/1646989
133 */
134__declspec(noinline)
135#endif
136int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X,
137 const mbedtls_mpi *Y,
138 unsigned char assign)
139{
140 int ret = 0;
141 MPI_VALIDATE_RET(X != NULL);
142 MPI_VALIDATE_RET(Y != NULL);
143
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100144 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, Y->n));
145
Dave Rodgman589ccb82023-05-17 13:55:01 +0100146 mbedtls_ct_condition_t do_assign = mbedtls_ct_bool(assign);
147
148 X->s = (int) mbedtls_ct_uint_if_new(do_assign, Y->s, X->s);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100149
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100150 mbedtls_mpi_core_cond_assign(X->p, Y->p, Y->n, do_assign);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100151
Dave Rodgman589ccb82023-05-17 13:55:01 +0100152 mbedtls_ct_condition_t do_not_assign = mbedtls_ct_bool_not(do_assign);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100153 for (size_t i = Y->n; i < X->n; i++) {
Dave Rodgman589ccb82023-05-17 13:55:01 +0100154 X->p[i] = mbedtls_ct_mpi_uint_if0(do_not_assign, X->p[i]);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100155 }
156
157cleanup:
158 return ret;
159}
160
161/*
162 * Conditionally swap X and Y, without leaking information
163 * about whether the swap was made or not.
164 * Here it is not ok to simply swap the pointers, which would lead to
165 * different memory access patterns when X and Y are used afterwards.
166 */
167int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X,
168 mbedtls_mpi *Y,
169 unsigned char swap)
170{
171 int ret = 0;
172 int s;
173 MPI_VALIDATE_RET(X != NULL);
174 MPI_VALIDATE_RET(Y != NULL);
175
176 if (X == Y) {
177 return 0;
178 }
179
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100180 mbedtls_ct_condition_t do_swap = mbedtls_ct_bool(swap);
181
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100182 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, Y->n));
183 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Y, X->n));
184
185 s = X->s;
186 X->s = (int) mbedtls_ct_uint_if(swap, Y->s, X->s);
187 Y->s = (int) mbedtls_ct_uint_if(swap, s, Y->s);
188
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100189 mbedtls_mpi_core_cond_swap(X->p, Y->p, X->n, do_swap);
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100190
191cleanup:
192 return ret;
193}
194
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500195/* Implementation that should never be optimized out by the compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +0100196static void mbedtls_mpi_zeroize(mbedtls_mpi_uint *v, size_t n)
Andres Amaya Garcia6698d2f2018-04-24 08:39:07 -0500197{
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 mbedtls_platform_zeroize(v, ciL * n);
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500199}
200
Paul Bakker5121ce52009-01-03 21:22:43 +0000201/*
Paul Bakker6c591fa2011-05-05 11:49:20 +0000202 * Initialize one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100204void mbedtls_mpi_init(mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000205{
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 MPI_VALIDATE(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
Paul Bakker6c591fa2011-05-05 11:49:20 +0000208 X->s = 1;
209 X->n = 0;
210 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000211}
212
213/*
Paul Bakker6c591fa2011-05-05 11:49:20 +0000214 * Unallocate one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100216void mbedtls_mpi_free(mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000217{
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 if (X == NULL) {
Paul Bakker6c591fa2011-05-05 11:49:20 +0000219 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 if (X->p != NULL) {
223 mbedtls_mpi_zeroize(X->p, X->n);
224 mbedtls_free(X->p);
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 }
226
Paul Bakker6c591fa2011-05-05 11:49:20 +0000227 X->s = 1;
228 X->n = 0;
229 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000230}
231
232/*
233 * Enlarge to the specified number of limbs
234 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100235int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs)
Paul Bakker5121ce52009-01-03 21:22:43 +0000236{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_mpi_uint *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
241 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
242 }
Paul Bakkerf9688572011-05-05 10:00:45 +0000243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (X->n < nblimbs) {
245 if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(nblimbs, ciL)) == NULL) {
246 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
247 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if (X->p != NULL) {
250 memcpy(p, X->p, X->n * ciL);
251 mbedtls_mpi_zeroize(X->p, X->n);
252 mbedtls_free(X->p);
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 }
254
255 X->n = nblimbs;
256 X->p = p;
257 }
258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000260}
261
262/*
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100263 * Resize down as much as possible,
264 * while keeping at least the specified number of limbs
265 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100266int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs)
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100267{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_mpi_uint *p;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100269 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 MPI_VALIDATE_RET(X != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
273 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
274 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100275
Gilles Peskinee2f563e2020-01-20 21:17:43 +0100276 /* Actually resize up if there are currently fewer than nblimbs limbs. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 if (X->n <= nblimbs) {
278 return mbedtls_mpi_grow(X, nblimbs);
279 }
Gilles Peskine322752b2020-01-21 13:59:51 +0100280 /* After this point, then X->n > nblimbs and in particular X->n > 0. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 for (i = X->n - 1; i > 0; i--) {
283 if (X->p[i] != 0) {
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100284 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 }
286 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100287 i++;
288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (i < nblimbs) {
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100290 i = nblimbs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(i, ciL)) == NULL) {
294 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
295 }
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (X->p != NULL) {
298 memcpy(p, X->p, i * ciL);
299 mbedtls_mpi_zeroize(X->p, X->n);
300 mbedtls_free(X->p);
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100301 }
302
303 X->n = i;
304 X->p = p;
305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 return 0;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100307}
308
Gilles Peskineed32b572021-06-02 22:17:52 +0200309/* Resize X to have exactly n limbs and set it to 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310static int mbedtls_mpi_resize_clear(mbedtls_mpi *X, size_t limbs)
Gilles Peskineed32b572021-06-02 22:17:52 +0200311{
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 if (limbs == 0) {
313 mbedtls_mpi_free(X);
314 return 0;
315 } else if (X->n == limbs) {
316 memset(X->p, 0, limbs * ciL);
Gilles Peskineed32b572021-06-02 22:17:52 +0200317 X->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return 0;
319 } else {
320 mbedtls_mpi_free(X);
321 return mbedtls_mpi_grow(X, limbs);
Gilles Peskineed32b572021-06-02 22:17:52 +0200322 }
323}
324
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100325/*
Gilles Peskine3da1a8f2021-06-08 23:17:42 +0200326 * Copy the contents of Y into X.
327 *
328 * This function is not constant-time. Leading zeros in Y may be removed.
329 *
330 * Ensure that X does not shrink. This is not guaranteed by the public API,
331 * but some code in the bignum module relies on this property, for example
332 * in mbedtls_mpi_exp_mod().
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100334int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000335{
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100336 int ret = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000337 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 MPI_VALIDATE_RET(X != NULL);
339 MPI_VALIDATE_RET(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 if (X == Y) {
342 return 0;
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200343 }
344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if (Y->n == 0) {
346 if (X->n != 0) {
347 X->s = 1;
348 memset(X->p, 0, X->n * ciL);
349 }
350 return 0;
351 }
352
353 for (i = Y->n - 1; i > 0; i--) {
354 if (Y->p[i] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000355 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 }
357 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000358 i++;
359
360 X->s = Y->s;
361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 if (X->n < i) {
363 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i));
364 } else {
365 memset(X->p + i, 0, (X->n - i) * ciL);
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100366 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 memcpy(X->p, Y->p, i * ciL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000369
370cleanup:
371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000373}
374
375/*
376 * Swap the contents of X and Y
377 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100378void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000379{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 MPI_VALIDATE(X != NULL);
382 MPI_VALIDATE(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 memcpy(&T, X, sizeof(mbedtls_mpi));
385 memcpy(X, Y, sizeof(mbedtls_mpi));
386 memcpy(Y, &T, sizeof(mbedtls_mpi));
Paul Bakker5121ce52009-01-03 21:22:43 +0000387}
388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389static inline mbedtls_mpi_uint mpi_sint_abs(mbedtls_mpi_sint z)
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100390{
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if (z >= 0) {
392 return z;
393 }
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100394 /* Take care to handle the most negative value (-2^(biL-1)) correctly.
395 * A naive -z would have undefined behavior.
396 * Write this in a way that makes popular compilers happy (GCC, Clang,
397 * MSVC). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 return (mbedtls_mpi_uint) 0 - (mbedtls_mpi_uint) z;
Gilles Peskineef7f4e42022-11-15 23:25:27 +0100399}
400
Paul Bakker5121ce52009-01-03 21:22:43 +0000401/*
402 * Set value from integer
403 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100404int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z)
Paul Bakker5121ce52009-01-03 21:22:43 +0000405{
Janos Follath24eed8d2019-11-22 13:21:35 +0000406 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, 1));
410 memset(X->p, 0, X->n * ciL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 X->p[0] = mpi_sint_abs(z);
413 X->s = (z < 0) ? -1 : 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000414
415cleanup:
416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000418}
419
420/*
Paul Bakker2f5947e2011-05-18 15:47:11 +0000421 * Get a specific bit
422 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100423int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
Paul Bakker2f5947e2011-05-18 15:47:11 +0000424{
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 MPI_VALIDATE_RET(X != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if (X->n * biL <= pos) {
428 return 0;
429 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 return (X->p[pos / biL] >> (pos % biL)) & 0x01;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000432}
433
434/*
435 * Set a bit to a specific value of 0 or 1
436 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100437int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val)
Paul Bakker2f5947e2011-05-18 15:47:11 +0000438{
439 int ret = 0;
440 size_t off = pos / biL;
441 size_t idx = pos % biL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 MPI_VALIDATE_RET(X != NULL);
Paul Bakker2f5947e2011-05-18 15:47:11 +0000443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if (val != 0 && val != 1) {
445 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000446 }
447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 if (X->n * biL <= pos) {
449 if (val == 0) {
450 return 0;
451 }
452
453 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, off + 1));
454 }
455
456 X->p[off] &= ~((mbedtls_mpi_uint) 0x01 << idx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 X->p[off] |= (mbedtls_mpi_uint) val << idx;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000458
459cleanup:
Paul Bakker9af723c2014-05-01 13:03:14 +0200460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 return ret;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000462}
463
464/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200465 * Return the number of less significant zero-bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100467size_t mbedtls_mpi_lsb(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000468{
Paul Bakker23986e52011-04-24 08:57:21 +0000469 size_t i, j, count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 MBEDTLS_INTERNAL_VALIDATE_RET(X != NULL, 0);
Paul Bakker5121ce52009-01-03 21:22:43 +0000471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 for (i = 0; i < X->n; i++) {
473 for (j = 0; j < biL; j++, count++) {
474 if (((X->p[i] >> j) & 1) != 0) {
475 return count;
476 }
477 }
478 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000481}
482
483/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200484 * Return the number of bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000485 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100486size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000487{
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 return mbedtls_mpi_core_bitlen(X->p, X->n);
Paul Bakker5121ce52009-01-03 21:22:43 +0000489}
490
491/*
492 * Return the total size in bytes
493 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100494size_t mbedtls_mpi_size(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000495{
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 return (mbedtls_mpi_bitlen(X) + 7) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000497}
498
499/*
500 * Convert an ASCII character to digit value
501 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100502static int mpi_get_digit(mbedtls_mpi_uint *d, int radix, char c)
Paul Bakker5121ce52009-01-03 21:22:43 +0000503{
504 *d = 255;
505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 if (c >= 0x30 && c <= 0x39) {
507 *d = c - 0x30;
508 }
509 if (c >= 0x41 && c <= 0x46) {
510 *d = c - 0x37;
511 }
512 if (c >= 0x61 && c <= 0x66) {
513 *d = c - 0x57;
514 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 if (*d >= (mbedtls_mpi_uint) radix) {
517 return MBEDTLS_ERR_MPI_INVALID_CHARACTER;
518 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000521}
522
523/*
524 * Import from an ASCII string
525 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100526int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s)
Paul Bakker5121ce52009-01-03 21:22:43 +0000527{
Janos Follath24eed8d2019-11-22 13:21:35 +0000528 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000529 size_t i, j, slen, n;
Gilles Peskine80f56732021-04-03 18:26:13 +0200530 int sign = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 mbedtls_mpi_uint d;
532 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 MPI_VALIDATE_RET(X != NULL);
534 MPI_VALIDATE_RET(s != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 if (radix < 2 || radix > 16) {
537 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskine7cba8592021-06-08 18:32:34 +0200538 }
539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 mbedtls_mpi_init(&T);
541
542 if (s[0] == 0) {
543 mbedtls_mpi_free(X);
544 return 0;
545 }
546
547 if (s[0] == '-') {
Gilles Peskine80f56732021-04-03 18:26:13 +0200548 ++s;
549 sign = -1;
550 }
551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 slen = strlen(s);
Paul Bakkerff60ee62010-03-16 21:09:09 +0000553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 if (radix == 16) {
Dave Rodgman68ef1d62023-05-18 20:49:03 +0100555 if (slen > SIZE_MAX >> 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 n = BITS_TO_LIMBS(slen << 2);
560
561 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n));
562 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
563
564 for (i = slen, j = 0; i > 0; i--, j++) {
565 MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i - 1]));
566 X->p[j / (2 * ciL)] |= d << ((j % (2 * ciL)) << 2);
567 }
568 } else {
569 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
570
571 for (i = 0; i < slen; i++) {
572 MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i]));
573 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T, X, radix));
574 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, &T, d));
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 }
576 }
577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 if (sign < 0 && mbedtls_mpi_bitlen(X) != 0) {
Gilles Peskine80f56732021-04-03 18:26:13 +0200579 X->s = -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 }
Gilles Peskine80f56732021-04-03 18:26:13 +0200581
Paul Bakker5121ce52009-01-03 21:22:43 +0000582cleanup:
583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000587}
588
589/*
Ron Eldora16fa292018-11-20 14:07:01 +0200590 * Helper to write the digits high-order first.
Paul Bakker5121ce52009-01-03 21:22:43 +0000591 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100592static int mpi_write_hlp(mbedtls_mpi *X, int radix,
593 char **p, const size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000594{
Janos Follath24eed8d2019-11-22 13:21:35 +0000595 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_mpi_uint r;
Ron Eldora16fa292018-11-20 14:07:01 +0200597 size_t length = 0;
598 char *p_end = *p + buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 do {
601 if (length >= buflen) {
602 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
Ron Eldora16fa292018-11-20 14:07:01 +0200603 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, radix));
606 MBEDTLS_MPI_CHK(mbedtls_mpi_div_int(X, NULL, X, radix));
Ron Eldora16fa292018-11-20 14:07:01 +0200607 /*
608 * Write the residue in the current position, as an ASCII character.
609 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 if (r < 0xA) {
611 *(--p_end) = (char) ('0' + r);
612 } else {
613 *(--p_end) = (char) ('A' + (r - 0xA));
614 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000615
Ron Eldora16fa292018-11-20 14:07:01 +0200616 length++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 } while (mbedtls_mpi_cmp_int(X, 0) != 0);
Paul Bakker5121ce52009-01-03 21:22:43 +0000618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 memmove(*p, p_end, length);
Ron Eldora16fa292018-11-20 14:07:01 +0200620 *p += length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
622cleanup:
623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000625}
626
627/*
628 * Export into an ASCII string
629 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100630int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
631 char *buf, size_t buflen, size_t *olen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000632{
Paul Bakker23986e52011-04-24 08:57:21 +0000633 int ret = 0;
634 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_mpi T;
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 MPI_VALIDATE_RET(X != NULL);
638 MPI_VALIDATE_RET(olen != NULL);
639 MPI_VALIDATE_RET(buflen == 0 || buf != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if (radix < 2 || radix > 16) {
642 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
643 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 n = mbedtls_mpi_bitlen(X); /* Number of bits necessary to present `n`. */
646 if (radix >= 4) {
647 n >>= 1; /* Number of 4-adic digits necessary to present
Hanno Becker23cfea02019-02-04 09:45:07 +0000648 * `n`. If radix > 4, this might be a strict
649 * overapproximation of the number of
650 * radix-adic digits needed to present `n`. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 }
652 if (radix >= 16) {
653 n >>= 1; /* Number of hexadecimal digits necessary to
Hanno Becker23cfea02019-02-04 09:45:07 +0000654 * present `n`. */
655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 }
Janos Follath80470622019-03-06 13:43:02 +0000657 n += 1; /* Terminating null byte */
Hanno Becker23cfea02019-02-04 09:45:07 +0000658 n += 1; /* Compensate for the divisions above, which round down `n`
659 * in case it's not even. */
660 n += 1; /* Potential '-'-sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 n += (n & 1); /* Make n even to have enough space for hexadecimal writing,
Hanno Becker23cfea02019-02-04 09:45:07 +0000662 * which always uses an even number of hex-digits. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 if (buflen < n) {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100665 *olen = n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 }
668
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100669 p = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 mbedtls_mpi_init(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if (X->s == -1) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000673 *p++ = '-';
Hanno Beckerc983c812019-02-01 16:41:30 +0000674 buflen--;
675 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 if (radix == 16) {
Paul Bakker23986e52011-04-24 08:57:21 +0000678 int c;
679 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 for (i = X->n, k = 0; i > 0; i--) {
682 for (j = ciL; j > 0; j--) {
683 c = (X->p[i - 1] >> ((j - 1) << 3)) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (c == 0 && k == 0 && (i + j) != 2) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000686 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000689 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000690 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000691 k = 1;
692 }
693 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 } else {
695 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T, X));
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (T.s == -1) {
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000698 T.s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 MBEDTLS_MPI_CHK(mpi_write_hlp(&T, radix, &p, buflen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000702 }
703
704 *p++ = '\0';
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100705 *olen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000706
707cleanup:
708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 mbedtls_mpi_free(&T);
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000712}
713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000715/*
716 * Read X from an opened file
717 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100718int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin)
Paul Bakker5121ce52009-01-03 21:22:43 +0000719{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 mbedtls_mpi_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000721 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000722 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000723 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000724 * Buffer should have space for (short) label and decimal formatted MPI,
725 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000726 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 MPI_VALIDATE_RET(X != NULL);
730 MPI_VALIDATE_RET(fin != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (radix < 2 || radix > 16) {
733 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
734 }
Hanno Becker73d7d792018-12-11 10:35:51 +0000735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 memset(s, 0, sizeof(s));
737 if (fgets(s, sizeof(s) - 1, fin) == NULL) {
738 return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
739 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 slen = strlen(s);
742 if (slen == sizeof(s) - 2) {
743 return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
744 }
Paul Bakkercb37aa52011-11-30 16:00:20 +0000745
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if (slen > 0 && s[slen - 1] == '\n') {
747 slen--; s[slen] = '\0';
748 }
749 if (slen > 0 && s[slen - 1] == '\r') {
750 slen--; s[slen] = '\0';
751 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000752
753 p = s + slen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 while (p-- > s) {
755 if (mpi_get_digit(&d, radix, *p) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000756 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 }
758 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 return mbedtls_mpi_read_string(X, radix, p + 1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000761}
762
763/*
764 * Write X into an opened file (or stdout if fout == NULL)
765 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100766int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE *fout)
Paul Bakker5121ce52009-01-03 21:22:43 +0000767{
Janos Follath24eed8d2019-11-22 13:21:35 +0000768 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000769 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000770 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000771 * Buffer should have space for (short) label and decimal formatted MPI,
772 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000773 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
775 MPI_VALIDATE_RET(X != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 if (radix < 2 || radix > 16) {
778 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
779 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 memset(s, 0, sizeof(s));
Paul Bakker5121ce52009-01-03 21:22:43 +0000782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 MBEDTLS_MPI_CHK(mbedtls_mpi_write_string(X, radix, s, sizeof(s) - 2, &n));
Paul Bakker5121ce52009-01-03 21:22:43 +0000784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 if (p == NULL) {
786 p = "";
787 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 plen = strlen(p);
790 slen = strlen(s);
Paul Bakker5121ce52009-01-03 21:22:43 +0000791 s[slen++] = '\r';
792 s[slen++] = '\n';
793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if (fout != NULL) {
795 if (fwrite(p, 1, plen, fout) != plen ||
796 fwrite(s, 1, slen, fout) != slen) {
797 return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
798 }
799 } else {
800 mbedtls_printf("%s%s", p, s);
Paul Bakker5121ce52009-01-03 21:22:43 +0000801 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000802
803cleanup:
804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000806}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000808
809/*
Janos Follatha778a942019-02-13 10:28:28 +0000810 * Import X from unsigned binary data, little endian
Przemyslaw Stekiel76960a72022-02-21 13:42:09 +0100811 *
812 * This function is guaranteed to return an MPI with exactly the necessary
813 * number of limbs (in particular, it does not skip 0s in the input).
Janos Follatha778a942019-02-13 10:28:28 +0000814 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815int mbedtls_mpi_read_binary_le(mbedtls_mpi *X,
816 const unsigned char *buf, size_t buflen)
Janos Follatha778a942019-02-13 10:28:28 +0000817{
Janos Follath24eed8d2019-11-22 13:21:35 +0000818 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 const size_t limbs = CHARS_TO_LIMBS(buflen);
Janos Follatha778a942019-02-13 10:28:28 +0000820
821 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
Janos Follatha778a942019-02-13 10:28:28 +0000823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_le(X->p, X->n, buf, buflen));
Janos Follatha778a942019-02-13 10:28:28 +0000825
826cleanup:
827
Janos Follath171a7ef2019-02-15 16:17:45 +0000828 /*
829 * This function is also used to import keys. However, wiping the buffers
830 * upon failure is not necessary because failure only can happen before any
831 * input is copied.
832 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 return ret;
Janos Follatha778a942019-02-13 10:28:28 +0000834}
835
836/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000837 * Import X from unsigned binary data, big endian
Przemyslaw Stekiel76960a72022-02-21 13:42:09 +0100838 *
839 * This function is guaranteed to return an MPI with exactly the necessary
840 * number of limbs (in particular, it does not skip 0s in the input).
Paul Bakker5121ce52009-01-03 21:22:43 +0000841 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100842int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000843{
Janos Follath24eed8d2019-11-22 13:21:35 +0000844 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 const size_t limbs = CHARS_TO_LIMBS(buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 MPI_VALIDATE_RET(X != NULL);
848 MPI_VALIDATE_RET(buflen == 0 || buf != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +0000849
Hanno Becker073c1992017-10-17 15:17:27 +0100850 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
Paul Bakker5121ce52009-01-03 21:22:43 +0000852
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 MBEDTLS_MPI_CHK(mbedtls_mpi_core_read_be(X->p, X->n, buf, buflen));
Paul Bakker5121ce52009-01-03 21:22:43 +0000854
855cleanup:
856
Janos Follath171a7ef2019-02-15 16:17:45 +0000857 /*
858 * This function is also used to import keys. However, wiping the buffers
859 * upon failure is not necessary because failure only can happen before any
860 * input is copied.
861 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000863}
864
865/*
Janos Follathe344d0f2019-02-19 16:17:40 +0000866 * Export X into unsigned binary data, little endian
867 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100868int mbedtls_mpi_write_binary_le(const mbedtls_mpi *X,
869 unsigned char *buf, size_t buflen)
Janos Follathe344d0f2019-02-19 16:17:40 +0000870{
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 return mbedtls_mpi_core_write_le(X->p, X->n, buf, buflen);
Janos Follathe344d0f2019-02-19 16:17:40 +0000872}
873
874/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000875 * Export X into unsigned binary data, big endian
876 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100877int mbedtls_mpi_write_binary(const mbedtls_mpi *X,
878 unsigned char *buf, size_t buflen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000879{
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 return mbedtls_mpi_core_write_be(X->p, X->n, buf, buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000881}
882
883/*
884 * Left-shift: X <<= count
885 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100886int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
Paul Bakker5121ce52009-01-03 21:22:43 +0000887{
Janos Follath24eed8d2019-11-22 13:21:35 +0000888 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Minos Galanakis0144b352023-05-02 14:02:32 +0100889 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 i = mbedtls_mpi_bitlen(X) + count;
Paul Bakker5121ce52009-01-03 21:22:43 +0000893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 if (X->n * biL < i) {
895 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, BITS_TO_LIMBS(i)));
896 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000897
898 ret = 0;
899
Minos Galanakis0144b352023-05-02 14:02:32 +0100900 mbedtls_mpi_core_shift_l(X->p, X->n, count);
Paul Bakker5121ce52009-01-03 21:22:43 +0000901cleanup:
902
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000904}
905
906/*
907 * Right-shift: X >>= count
908 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100909int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
Paul Bakker5121ce52009-01-03 21:22:43 +0000910{
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 MPI_VALIDATE_RET(X != NULL);
912 if (X->n != 0) {
913 mbedtls_mpi_core_shift_r(X->p, X->n, count);
914 }
915 return 0;
Gilles Peskine66414202022-09-21 15:36:16 +0200916}
917
Paul Bakker5121ce52009-01-03 21:22:43 +0000918/*
919 * Compare unsigned values
920 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100921int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000922{
Paul Bakker23986e52011-04-24 08:57:21 +0000923 size_t i, j;
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 MPI_VALIDATE_RET(X != NULL);
925 MPI_VALIDATE_RET(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 for (i = X->n; i > 0; i--) {
928 if (X->p[i - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000931 }
932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 for (j = Y->n; j > 0; j--) {
934 if (Y->p[j - 1] != 0) {
935 break;
936 }
937 }
938
939 if (i == 0 && j == 0) {
940 return 0;
941 }
942
943 if (i > j) {
944 return 1;
945 }
946 if (j > i) {
947 return -1;
948 }
949
950 for (; i > 0; i--) {
951 if (X->p[i - 1] > Y->p[i - 1]) {
952 return 1;
953 }
954 if (X->p[i - 1] < Y->p[i - 1]) {
955 return -1;
956 }
957 }
958
959 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000960}
961
962/*
963 * Compare signed values
964 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100965int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Paul Bakker5121ce52009-01-03 21:22:43 +0000966{
Paul Bakker23986e52011-04-24 08:57:21 +0000967 size_t i, j;
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 MPI_VALIDATE_RET(X != NULL);
969 MPI_VALIDATE_RET(Y != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 for (i = X->n; i > 0; i--) {
972 if (X->p[i - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000973 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000975 }
976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 for (j = Y->n; j > 0; j--) {
978 if (Y->p[j - 1] != 0) {
979 break;
980 }
981 }
982
983 if (i == 0 && j == 0) {
984 return 0;
985 }
986
987 if (i > j) {
988 return X->s;
989 }
990 if (j > i) {
991 return -Y->s;
992 }
993
994 if (X->s > 0 && Y->s < 0) {
995 return 1;
996 }
997 if (Y->s > 0 && X->s < 0) {
998 return -1;
999 }
1000
1001 for (; i > 0; i--) {
1002 if (X->p[i - 1] > Y->p[i - 1]) {
1003 return X->s;
1004 }
1005 if (X->p[i - 1] < Y->p[i - 1]) {
1006 return -X->s;
1007 }
1008 }
1009
1010 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001011}
1012
Janos Follathee6abce2019-09-05 14:47:19 +01001013/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001014 * Compare signed values
1015 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001016int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
Paul Bakker5121ce52009-01-03 21:22:43 +00001017{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 mbedtls_mpi Y;
1019 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 MPI_VALIDATE_RET(X != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 *p = mpi_sint_abs(z);
1023 Y.s = (z < 0) ? -1 : 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001024 Y.n = 1;
1025 Y.p = p;
1026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 return mbedtls_mpi_cmp_mpi(X, &Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00001028}
1029
1030/*
1031 * Unsigned addition: X = |A| + |B| (HAC 14.7)
1032 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001033int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001034{
Janos Follath24eed8d2019-11-22 13:21:35 +00001035 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001036 size_t j;
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 MPI_VALIDATE_RET(X != NULL);
1038 MPI_VALIDATE_RET(A != NULL);
1039 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001040
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 if (X == B) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 const mbedtls_mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001043 }
1044
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 if (X != A) {
1046 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
1047 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001048
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001049 /*
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001050 * X must always be positive as a result of unsigned additions.
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001051 */
1052 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 for (j = B->n; j > 0; j--) {
1055 if (B->p[j - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 }
1058 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001059
Gilles Peskinedb14a9d2022-11-15 22:59:00 +01001060 /* Exit early to avoid undefined behavior on NULL+0 when X->n == 0
1061 * and B is 0 (of any size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 if (j == 0) {
1063 return 0;
1064 }
Gilles Peskinedb14a9d2022-11-15 22:59:00 +01001065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j));
Paul Bakker5121ce52009-01-03 21:22:43 +00001067
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001068 /* j is the number of non-zero limbs of B. Add those to X. */
Paul Bakker5121ce52009-01-03 21:22:43 +00001069
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001070 mbedtls_mpi_uint *p = X->p;
1071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 mbedtls_mpi_uint c = mbedtls_mpi_core_add(p, p, B->p, j);
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001073
1074 p += j;
1075
1076 /* Now propagate any carry */
Paul Bakker5121ce52009-01-03 21:22:43 +00001077
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 while (c != 0) {
1079 if (j >= X->n) {
1080 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j + 1));
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +01001081 p = X->p + j;
Paul Bakker5121ce52009-01-03 21:22:43 +00001082 }
1083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 *p += c; c = (*p < c); j++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001085 }
1086
1087cleanup:
1088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001090}
1091
Paul Bakker5121ce52009-01-03 21:22:43 +00001092/*
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001093 * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
Paul Bakker5121ce52009-01-03 21:22:43 +00001094 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001095int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001096{
Janos Follath24eed8d2019-11-22 13:21:35 +00001097 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001098 size_t n;
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001099 mbedtls_mpi_uint carry;
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 MPI_VALIDATE_RET(X != NULL);
1101 MPI_VALIDATE_RET(A != NULL);
1102 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 for (n = B->n; n > 0; n--) {
1105 if (B->p[n - 1] != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001106 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 }
1108 }
1109 if (n > A->n) {
Gilles Peskinec8a91772021-01-27 22:30:43 +01001110 /* B >= (2^ciL)^n > A */
1111 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1112 goto cleanup;
1113 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001114
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, A->n));
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001116
1117 /* Set the high limbs of X to match A. Don't touch the lower limbs
1118 * because X might be aliased to B, and we must not overwrite the
1119 * significant digits of B. */
Aaron M. Uckoaf67d2c2023-01-17 11:52:22 -05001120 if (A->n > n && A != X) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 memcpy(X->p + n, A->p + n, (A->n - n) * ciL);
1122 }
1123 if (X->n > A->n) {
1124 memset(X->p + A->n, 0, (X->n - A->n) * ciL);
1125 }
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 carry = mbedtls_mpi_core_sub(X->p, A->p, B->p, n);
1128 if (carry != 0) {
Tom Cosgrove452c99c2022-08-25 10:07:07 +01001129 /* Propagate the carry through the rest of X. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 carry = mbedtls_mpi_core_sub_int(X->p + n, X->p + n, carry, X->n - n);
Tom Cosgrove452c99c2022-08-25 10:07:07 +01001131
1132 /* If we have further carry/borrow, the result is negative. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 if (carry != 0) {
Gilles Peskine89b41302020-07-23 01:16:46 +02001134 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1135 goto cleanup;
1136 }
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001137 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001138
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001139 /* X should always be positive as a result of unsigned subtractions. */
1140 X->s = 1;
1141
Paul Bakker5121ce52009-01-03 21:22:43 +00001142cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001144}
1145
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001146/* Common function for signed addition and subtraction.
1147 * Calculate A + B * flip_B where flip_B is 1 or -1.
Paul Bakker5121ce52009-01-03 21:22:43 +00001148 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001149static int add_sub_mpi(mbedtls_mpi *X,
1150 const mbedtls_mpi *A, const mbedtls_mpi *B,
1151 int flip_B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001152{
Hanno Becker73d7d792018-12-11 10:35:51 +00001153 int ret, s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 MPI_VALIDATE_RET(X != NULL);
1155 MPI_VALIDATE_RET(A != NULL);
1156 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001157
Hanno Becker73d7d792018-12-11 10:35:51 +00001158 s = A->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 if (A->s * B->s * flip_B < 0) {
1160 int cmp = mbedtls_mpi_cmp_abs(A, B);
1161 if (cmp >= 0) {
1162 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, A, B));
Gilles Peskine4a768dd2022-11-09 22:02:16 +01001163 /* If |A| = |B|, the result is 0 and we must set the sign bit
1164 * to +1 regardless of which of A or B was negative. Otherwise,
1165 * since |A| > |B|, the sign is the sign of A. */
1166 X->s = cmp == 0 ? 1 : s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 } else {
1168 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, B, A));
Gilles Peskine4a768dd2022-11-09 22:02:16 +01001169 /* Since |A| < |B|, the sign is the opposite of A. */
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 X->s = -s;
1171 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 } else {
1173 MBEDTLS_MPI_CHK(mbedtls_mpi_add_abs(X, A, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001174 X->s = s;
1175 }
1176
1177cleanup:
1178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001180}
1181
1182/*
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001183 * Signed addition: X = A + B
1184 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001185int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001186{
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 return add_sub_mpi(X, A, B, 1);
Gilles Peskine72ee1e32022-11-09 21:34:09 +01001188}
1189
1190/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001191 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001192 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001193int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001194{
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 return add_sub_mpi(X, A, B, -1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001196}
1197
1198/*
1199 * Signed addition: X = A + b
1200 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001201int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001202{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001203 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 MPI_VALIDATE_RET(X != NULL);
1206 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 p[0] = mpi_sint_abs(b);
1209 B.s = (b < 0) ? -1 : 1;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001210 B.n = 1;
1211 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 return mbedtls_mpi_add_mpi(X, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001214}
1215
1216/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001217 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001218 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001219int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001220{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001221 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 MPI_VALIDATE_RET(X != NULL);
1224 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001225
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 p[0] = mpi_sint_abs(b);
1227 B.s = (b < 0) ? -1 : 1;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001228 B.n = 1;
1229 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 return mbedtls_mpi_sub_mpi(X, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001232}
1233
Paul Bakker5121ce52009-01-03 21:22:43 +00001234/*
1235 * Baseline multiplication: X = A * B (HAC 14.12)
1236 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001237int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001238{
Janos Follath24eed8d2019-11-22 13:21:35 +00001239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker1772e052022-04-13 06:51:40 +01001240 size_t i, j;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 mbedtls_mpi TA, TB;
Hanno Beckerda763de2022-04-13 06:50:02 +01001242 int result_is_zero = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 MPI_VALIDATE_RET(X != NULL);
1244 MPI_VALIDATE_RET(A != NULL);
1245 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001246
Tom Cosgrove6af26f32022-08-24 16:40:55 +01001247 mbedtls_mpi_init(&TA);
1248 mbedtls_mpi_init(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 if (X == A) {
1251 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A)); A = &TA;
1252 }
1253 if (X == B) {
1254 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B)); B = &TB;
1255 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 for (i = A->n; i > 0; i--) {
1258 if (A->p[i - 1] != 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001259 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 }
1261 }
1262 if (i == 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001263 result_is_zero = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 }
Hanno Beckerda763de2022-04-13 06:50:02 +01001265
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 for (j = B->n; j > 0; j--) {
1267 if (B->p[j - 1] != 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001268 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 }
1270 }
1271 if (j == 0) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001272 result_is_zero = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 }
Hanno Beckerda763de2022-04-13 06:50:02 +01001274
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i + j));
1276 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
Paul Bakker5121ce52009-01-03 21:22:43 +00001277
Tom Cosgrove6af26f32022-08-24 16:40:55 +01001278 mbedtls_mpi_core_mul(X->p, A->p, i, B->p, j);
Paul Bakker5121ce52009-01-03 21:22:43 +00001279
Hanno Beckerda763de2022-04-13 06:50:02 +01001280 /* If the result is 0, we don't shortcut the operation, which reduces
1281 * but does not eliminate side channels leaking the zero-ness. We do
1282 * need to take care to set the sign bit properly since the library does
1283 * not fully support an MPI object with a value of 0 and s == -1. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 if (result_is_zero) {
Hanno Beckerda763de2022-04-13 06:50:02 +01001285 X->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 } else {
Hanno Beckerda763de2022-04-13 06:50:02 +01001287 X->s = A->s * B->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001289
1290cleanup:
1291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TA);
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001295}
1296
1297/*
1298 * Baseline multiplication: X = A * b
1299 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001300int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001301{
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 MPI_VALIDATE_RET(X != NULL);
1303 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001304
Hanno Becker35771312022-04-14 11:52:11 +01001305 size_t n = A->n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 while (n > 0 && A->p[n - 1] == 0) {
Hanno Becker35771312022-04-14 11:52:11 +01001307 --n;
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 }
Hanno Becker35771312022-04-14 11:52:11 +01001309
Hanno Becker74a11a32022-04-06 06:27:00 +01001310 /* The general method below doesn't work if b==0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 if (b == 0 || n == 0) {
1312 return mbedtls_mpi_lset(X, 0);
1313 }
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001314
Hanno Beckeraef9cc42022-04-11 06:36:29 +01001315 /* Calculate A*b as A + A*(b-1) to take advantage of mbedtls_mpi_core_mla */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001316 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001317 /* In general, A * b requires 1 limb more than b. If
1318 * A->p[n - 1] * b / b == A->p[n - 1], then A * b fits in the same
1319 * number of limbs as A and the call to grow() is not required since
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001320 * copy() will take care of the growth if needed. However, experimentally,
1321 * making the call to grow() unconditional causes slightly fewer
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001322 * calls to calloc() in ECP code, presumably because it reuses the
1323 * same mpi for a while and this way the mpi is more likely to directly
Hanno Becker9137b9c2022-04-12 10:51:54 +01001324 * grow to its final size.
1325 *
1326 * Note that calculating A*b as 0 + A*b doesn't work as-is because
1327 * A,X can be the same. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n + 1));
1329 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
1330 mbedtls_mpi_core_mla(X->p, X->n, A->p, n, b - 1);
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001331
1332cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001334}
1335
1336/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001337 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1338 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001339 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001340static mbedtls_mpi_uint mbedtls_int_div_int(mbedtls_mpi_uint u1,
1341 mbedtls_mpi_uint u0,
1342 mbedtls_mpi_uint d,
1343 mbedtls_mpi_uint *r)
Simon Butcher15b15d12015-11-26 19:35:03 +00001344{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001345#if defined(MBEDTLS_HAVE_UDBL)
1346 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001347#else
Simon Butcher9803d072016-01-03 00:24:34 +00001348 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 const mbedtls_mpi_uint uint_halfword_mask = ((mbedtls_mpi_uint) 1 << biH) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001350 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1351 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001352 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001353#endif
1354
Simon Butcher15b15d12015-11-26 19:35:03 +00001355 /*
1356 * Check for overflow
1357 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 if (0 == d || u1 >= d) {
1359 if (r != NULL) {
1360 *r = ~(mbedtls_mpi_uint) 0u;
1361 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 return ~(mbedtls_mpi_uint) 0u;
Simon Butcher15b15d12015-11-26 19:35:03 +00001364 }
1365
1366#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001367 dividend = (mbedtls_t_udbl) u1 << biL;
1368 dividend |= (mbedtls_t_udbl) u0;
1369 quotient = dividend / d;
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 if (quotient > ((mbedtls_t_udbl) 1 << biL) - 1) {
1371 quotient = ((mbedtls_t_udbl) 1 << biL) - 1;
1372 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001373
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 if (r != NULL) {
1375 *r = (mbedtls_mpi_uint) (dividend - (quotient * d));
1376 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001377
1378 return (mbedtls_mpi_uint) quotient;
1379#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001380
1381 /*
1382 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1383 * Vol. 2 - Seminumerical Algorithms, Knuth
1384 */
1385
1386 /*
1387 * Normalize the divisor, d, and dividend, u0, u1
1388 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 s = mbedtls_mpi_core_clz(d);
Simon Butcher15b15d12015-11-26 19:35:03 +00001390 d = d << s;
1391
1392 u1 = u1 << s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 u1 |= (u0 >> (biL - s)) & (-(mbedtls_mpi_sint) s >> (biL - 1));
Simon Butcher15b15d12015-11-26 19:35:03 +00001394 u0 = u0 << s;
1395
1396 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001397 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001398
1399 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001400 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001401
1402 /*
1403 * Find the first quotient and remainder
1404 */
1405 q1 = u1 / d1;
1406 r0 = u1 - d1 * q1;
1407
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 while (q1 >= radix || (q1 * d0 > radix * r0 + u0_msw)) {
Simon Butcher15b15d12015-11-26 19:35:03 +00001409 q1 -= 1;
1410 r0 += d1;
1411
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 if (r0 >= radix) {
1413 break;
1414 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001415 }
1416
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 rAX = (u1 * radix) + (u0_msw - q1 * d);
Simon Butcher15b15d12015-11-26 19:35:03 +00001418 q0 = rAX / d1;
1419 r0 = rAX - q0 * d1;
1420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 while (q0 >= radix || (q0 * d0 > radix * r0 + u0_lsw)) {
Simon Butcher15b15d12015-11-26 19:35:03 +00001422 q0 -= 1;
1423 r0 += d1;
1424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 if (r0 >= radix) {
1426 break;
1427 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001428 }
1429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 if (r != NULL) {
1431 *r = (rAX * radix + u0_lsw - q0 * d) >> s;
1432 }
Simon Butcher15b15d12015-11-26 19:35:03 +00001433
1434 quotient = q1 * radix + q0;
1435
1436 return quotient;
1437#endif
1438}
1439
1440/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001443int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1444 const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001445{
Janos Follath24eed8d2019-11-22 13:21:35 +00001446 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001447 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 mbedtls_mpi X, Y, Z, T1, T2;
Alexander Kd19a1932019-11-01 18:20:42 +03001449 mbedtls_mpi_uint TP2[3];
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 MPI_VALIDATE_RET(A != NULL);
1451 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001452
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 if (mbedtls_mpi_cmp_int(B, 0) == 0) {
1454 return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
1455 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001456
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z);
1458 mbedtls_mpi_init(&T1);
Alexander Kd19a1932019-11-01 18:20:42 +03001459 /*
1460 * Avoid dynamic memory allocations for constant-size T2.
1461 *
1462 * T2 is used for comparison only and the 3 limbs are assigned explicitly,
1463 * so nobody increase the size of the MPI and we're safe to use an on-stack
1464 * buffer.
1465 */
Alexander K35d6d462019-10-31 14:46:45 +03001466 T2.s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 T2.n = sizeof(TP2) / sizeof(*TP2);
Alexander Kd19a1932019-11-01 18:20:42 +03001468 T2.p = TP2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 if (mbedtls_mpi_cmp_abs(A, B) < 0) {
1471 if (Q != NULL) {
1472 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(Q, 0));
1473 }
1474 if (R != NULL) {
1475 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, A));
1476 }
1477 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001478 }
1479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&X, A));
1481 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001482 X.s = Y.s = 1;
1483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&Z, A->n + 2));
1485 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Z, 0));
1486 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T1, A->n + 2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 k = mbedtls_mpi_bitlen(&Y) % biL;
1489 if (k < biL - 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001490 k = biL - 1 - k;
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&X, k));
1492 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, k));
1493 } else {
1494 k = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001495 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001496
1497 n = X.n - 1;
1498 t = Y.n - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, biL * (n - t)));
Paul Bakker5121ce52009-01-03 21:22:43 +00001500
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 while (mbedtls_mpi_cmp_mpi(&X, &Y) >= 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001502 Z.p[n - t]++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &Y));
Paul Bakker5121ce52009-01-03 21:22:43 +00001504 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, biL * (n - t)));
Paul Bakker5121ce52009-01-03 21:22:43 +00001506
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 for (i = n; i > t; i--) {
1508 if (X.p[i] >= Y.p[t]) {
1509 Z.p[i - t - 1] = ~(mbedtls_mpi_uint) 0u;
1510 } else {
1511 Z.p[i - t - 1] = mbedtls_int_div_int(X.p[i], X.p[i - 1],
1512 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001513 }
1514
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 T2.p[0] = (i < 2) ? 0 : X.p[i - 2];
1516 T2.p[1] = (i < 1) ? 0 : X.p[i - 1];
Alexander K35d6d462019-10-31 14:46:45 +03001517 T2.p[2] = X.p[i];
1518
Paul Bakker5121ce52009-01-03 21:22:43 +00001519 Z.p[i - t - 1]++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 do {
Paul Bakker5121ce52009-01-03 21:22:43 +00001521 Z.p[i - t - 1]--;
1522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&T1, 0));
1524 T1.p[0] = (t < 1) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001525 T1.p[1] = Y.p[t];
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &T1, Z.p[i - t - 1]));
1527 } while (mbedtls_mpi_cmp_mpi(&T1, &T2) > 0);
Paul Bakker5121ce52009-01-03 21:22:43 +00001528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &Y, Z.p[i - t - 1]));
1530 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
1531 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &T1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 if (mbedtls_mpi_cmp_int(&X, 0) < 0) {
1534 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T1, &Y));
1535 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
1536 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&X, &X, &T1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001537 Z.p[i - t - 1]--;
1538 }
1539 }
1540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 if (Q != NULL) {
1542 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(Q, &Z));
Paul Bakker5121ce52009-01-03 21:22:43 +00001543 Q->s = A->s * B->s;
1544 }
1545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 if (R != NULL) {
1547 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&X, k));
Paul Bakkerf02c5642012-11-13 10:25:21 +00001548 X.s = A->s;
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, &X));
Paul Bakker5121ce52009-01-03 21:22:43 +00001550
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 if (mbedtls_mpi_cmp_int(R, 0) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001552 R->s = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001554 }
1555
1556cleanup:
1557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z);
1559 mbedtls_mpi_free(&T1);
1560 mbedtls_platform_zeroize(TP2, sizeof(TP2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001563}
1564
1565/*
1566 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00001567 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001568int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R,
1569 const mbedtls_mpi *A,
1570 mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001571{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001572 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 mbedtls_mpi_uint p[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001575
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 p[0] = mpi_sint_abs(b);
1577 B.s = (b < 0) ? -1 : 1;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001578 B.n = 1;
1579 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 return mbedtls_mpi_div_mpi(Q, R, A, &B);
Paul Bakker5121ce52009-01-03 21:22:43 +00001582}
1583
1584/*
1585 * Modulo: R = A mod B
1586 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001587int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00001588{
Janos Follath24eed8d2019-11-22 13:21:35 +00001589 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 MPI_VALIDATE_RET(R != NULL);
1591 MPI_VALIDATE_RET(A != NULL);
1592 MPI_VALIDATE_RET(B != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001593
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 if (mbedtls_mpi_cmp_int(B, 0) < 0) {
1595 return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1596 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(NULL, R, A, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00001599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 while (mbedtls_mpi_cmp_int(R, 0) < 0) {
1601 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(R, R, B));
1602 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001603
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 while (mbedtls_mpi_cmp_mpi(R, B) >= 0) {
1605 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(R, R, B));
1606 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001607
1608cleanup:
1609
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001611}
1612
1613/*
1614 * Modulo: r = A mod b
1615 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001616int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Paul Bakker5121ce52009-01-03 21:22:43 +00001617{
Paul Bakker23986e52011-04-24 08:57:21 +00001618 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 mbedtls_mpi_uint x, y, z;
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 MPI_VALIDATE_RET(r != NULL);
1621 MPI_VALIDATE_RET(A != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001622
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 if (b == 0) {
1624 return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
1625 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 if (b < 0) {
1628 return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1629 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001630
1631 /*
1632 * handle trivial cases
1633 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 if (b == 1 || A->n == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001635 *r = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001637 }
1638
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 if (b == 2) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001640 *r = A->p[0] & 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001642 }
1643
1644 /*
1645 * general case
1646 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 for (i = A->n, y = 0; i > 0; i--) {
Paul Bakker23986e52011-04-24 08:57:21 +00001648 x = A->p[i - 1];
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 y = (y << biH) | (x >> biH);
Paul Bakker5121ce52009-01-03 21:22:43 +00001650 z = y / b;
1651 y -= z * b;
1652
1653 x <<= biH;
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 y = (y << biH) | (x >> biH);
Paul Bakker5121ce52009-01-03 21:22:43 +00001655 z = y / b;
1656 y -= z * b;
1657 }
1658
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001659 /*
1660 * If A is negative, then the current y represents a negative value.
1661 * Flipping it to the positive side.
1662 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 if (A->s < 0 && y != 0) {
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001664 y = b - y;
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 }
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001666
Paul Bakker5121ce52009-01-03 21:22:43 +00001667 *r = y;
1668
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001670}
1671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672static void mpi_montg_init(mbedtls_mpi_uint *mm, const mbedtls_mpi *N)
Paul Bakker5121ce52009-01-03 21:22:43 +00001673{
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 *mm = mbedtls_mpi_core_montmul_init(N->p);
Paul Bakker5121ce52009-01-03 21:22:43 +00001675}
1676
Tom Cosgrove93842842022-08-05 16:59:43 +01001677/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
1678 *
1679 * \param[in,out] A One of the numbers to multiply.
1680 * It must have at least as many limbs as N
1681 * (A->n >= N->n), and any limbs beyond n are ignored.
1682 * On successful completion, A contains the result of
1683 * the multiplication A * B * R^-1 mod N where
1684 * R = (2^ciL)^n.
1685 * \param[in] B One of the numbers to multiply.
1686 * It must be nonzero and must not have more limbs than N
1687 * (B->n <= N->n).
Tom Cosgrove40d22942022-08-17 06:42:44 +01001688 * \param[in] N The modulus. \p N must be odd.
Tom Cosgrove93842842022-08-05 16:59:43 +01001689 * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
1690 * This is -N^-1 mod 2^ciL.
1691 * \param[in,out] T A bignum for temporary storage.
1692 * It must be at least twice the limb size of N plus 1
1693 * (T->n >= 2 * N->n + 1).
1694 * Its initial content is unused and
1695 * its final content is indeterminate.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +01001696 * It does not get reallocated.
Tom Cosgrove93842842022-08-05 16:59:43 +01001697 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001698static void mpi_montmul(mbedtls_mpi *A, const mbedtls_mpi *B,
1699 const mbedtls_mpi *N, mbedtls_mpi_uint mm,
1700 mbedtls_mpi *T)
Paul Bakker5121ce52009-01-03 21:22:43 +00001701{
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 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 +00001703}
1704
1705/*
1706 * Montgomery reduction: A = A * R^-1 mod N
Gilles Peskine2a82f722020-06-04 15:00:49 +02001707 *
Tom Cosgrove93842842022-08-05 16:59:43 +01001708 * See mpi_montmul() regarding constraints and guarantees on the parameters.
Paul Bakker5121ce52009-01-03 21:22:43 +00001709 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001710static void mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N,
1711 mbedtls_mpi_uint mm, mbedtls_mpi *T)
Paul Bakker5121ce52009-01-03 21:22:43 +00001712{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713 mbedtls_mpi_uint z = 1;
1714 mbedtls_mpi U;
Paul Bakker5121ce52009-01-03 21:22:43 +00001715
Paul Bakker8ddb6452013-02-27 14:56:33 +01001716 U.n = U.s = (int) z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001717 U.p = &z;
1718
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 mpi_montmul(A, &U, N, mm, T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001720}
1721
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001722/**
1723 * Select an MPI from a table without leaking the index.
1724 *
1725 * This is functionally equivalent to mbedtls_mpi_copy(R, T[idx]) except it
1726 * reads the entire table in order to avoid leaking the value of idx to an
1727 * attacker able to observe memory access patterns.
1728 *
1729 * \param[out] R Where to write the selected MPI.
1730 * \param[in] T The table to read from.
1731 * \param[in] T_size The number of elements in the table.
1732 * \param[in] idx The index of the element to select;
1733 * this must satisfy 0 <= idx < T_size.
1734 *
1735 * \return \c 0 on success, or a negative error code.
1736 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001737static 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 +01001738{
1739 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1740
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 for (size_t i = 0; i < T_size; i++) {
1742 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(R, &T[i],
1743 (unsigned char) mbedtls_ct_size_bool_eq(i,
1744 idx)));
Manuel Pégourié-Gonnard92413ef2021-06-03 10:42:46 +02001745 }
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001746
1747cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 return ret;
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001749}
1750
Paul Bakker5121ce52009-01-03 21:22:43 +00001751/*
1752 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
1753 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001754int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
1755 const mbedtls_mpi *E, const mbedtls_mpi *N,
1756 mbedtls_mpi *prec_RR)
Paul Bakker5121ce52009-01-03 21:22:43 +00001757{
Janos Follath24eed8d2019-11-22 13:21:35 +00001758 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath74601202022-11-21 15:54:20 +00001759 size_t window_bitsize;
Paul Bakker23986e52011-04-24 08:57:21 +00001760 size_t i, j, nblimbs;
1761 size_t bufsize, nbits;
Paul Elliott1748de12023-02-13 15:35:35 +00001762 size_t exponent_bits_in_window = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763 mbedtls_mpi_uint ei, mm, state;
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 mbedtls_mpi RR, T, W[(size_t) 1 << MBEDTLS_MPI_WINDOW_SIZE], WW, Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00001765 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001766
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 MPI_VALIDATE_RET(X != NULL);
1768 MPI_VALIDATE_RET(A != NULL);
1769 MPI_VALIDATE_RET(E != NULL);
1770 MPI_VALIDATE_RET(N != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00001771
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 if (mbedtls_mpi_cmp_int(N, 0) <= 0 || (N->p[0] & 1) == 0) {
1773 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1774 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001775
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 if (mbedtls_mpi_cmp_int(E, 0) < 0) {
1777 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1778 }
Paul Bakkerf6198c12012-05-16 08:02:29 +00001779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 if (mbedtls_mpi_bitlen(E) > MBEDTLS_MPI_MAX_BITS ||
1781 mbedtls_mpi_bitlen(N) > MBEDTLS_MPI_MAX_BITS) {
1782 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1783 }
Chris Jones9246d042020-11-25 15:12:39 +00001784
Paul Bakkerf6198c12012-05-16 08:02:29 +00001785 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001786 * Init temps and window size
1787 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 mpi_montg_init(&mm, N);
1789 mbedtls_mpi_init(&RR); mbedtls_mpi_init(&T);
1790 mbedtls_mpi_init(&Apos);
1791 mbedtls_mpi_init(&WW);
1792 memset(W, 0, sizeof(W));
Paul Bakker5121ce52009-01-03 21:22:43 +00001793
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 i = mbedtls_mpi_bitlen(E);
Paul Bakker5121ce52009-01-03 21:22:43 +00001795
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 window_bitsize = (i > 671) ? 6 : (i > 239) ? 5 :
1797 (i > 79) ? 4 : (i > 23) ? 3 : 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001798
Gilles Peskine449bd832023-01-11 14:50:10 +01001799#if (MBEDTLS_MPI_WINDOW_SIZE < 6)
1800 if (window_bitsize > MBEDTLS_MPI_WINDOW_SIZE) {
Janos Follath7fa11b82022-11-21 14:48:02 +00001801 window_bitsize = MBEDTLS_MPI_WINDOW_SIZE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 }
Peter Kolbuse6bcad32018-12-11 14:01:44 -06001803#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00001804
Janos Follathc8d66d52022-11-22 10:47:10 +00001805 const size_t w_table_used_size = (size_t) 1 << window_bitsize;
Janos Follath06000952022-11-22 10:18:06 +00001806
Paul Bakker5121ce52009-01-03 21:22:43 +00001807 /*
Janos Follathbe54ca72022-11-21 16:14:54 +00001808 * This function is not constant-trace: its memory accesses depend on the
1809 * exponent value. To defend against timing attacks, callers (such as RSA
1810 * and DHM) should use exponent blinding. However this is not enough if the
1811 * adversary can find the exponent in a single trace, so this function
1812 * takes extra precautions against adversaries who can observe memory
1813 * access patterns.
Janos Follathf08b40e2022-11-11 15:56:38 +00001814 *
Janos Follathbe54ca72022-11-21 16:14:54 +00001815 * This function performs a series of multiplications by table elements and
1816 * squarings, and we want the prevent the adversary from finding out which
1817 * table element was used, and from distinguishing between multiplications
1818 * and squarings. Firstly, when multiplying by an element of the window
1819 * W[i], we do a constant-trace table lookup to obfuscate i. This leaves
1820 * squarings as having a different memory access patterns from other
1821 * multiplications. So secondly, we put the accumulator X in the table as
1822 * well, and also do a constant-trace table lookup to multiply by X.
1823 *
1824 * This way, all multiplications take the form of a lookup-and-multiply.
1825 * The number of lookup-and-multiply operations inside each iteration of
1826 * the main loop still depends on the bits of the exponent, but since the
1827 * other operations in the loop don't have an easily recognizable memory
1828 * trace, an adversary is unlikely to be able to observe the exact
1829 * patterns.
1830 *
1831 * An adversary may still be able to recover the exponent if they can
1832 * observe both memory accesses and branches. However, branch prediction
1833 * exploitation typically requires many traces of execution over the same
1834 * data, which is defeated by randomized blinding.
Janos Follath84461482022-11-21 14:31:22 +00001835 *
1836 * To achieve this, we make a copy of X and we use the table entry in each
1837 * calculation from this point on.
Janos Follath8e7d6a02022-10-04 13:27:40 +01001838 */
Janos Follathc8d66d52022-11-22 10:47:10 +00001839 const size_t x_index = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 mbedtls_mpi_init(&W[x_index]);
1841 mbedtls_mpi_copy(&W[x_index], X);
Janos Follath84461482022-11-21 14:31:22 +00001842
Paul Bakker5121ce52009-01-03 21:22:43 +00001843 j = N->n + 1;
Tom Cosgrove93842842022-08-05 16:59:43 +01001844 /* All W[i] and X must have at least N->n limbs for the mpi_montmul()
Paul Bakker5121ce52009-01-03 21:22:43 +00001845 * and mpi_montred() calls later. Here we ensure that W[1] and X are
1846 * large enough, and later we'll grow other W[i] to the same length.
1847 * They must not be shrunk midway through this function!
1848 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[x_index], j));
1850 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[1], j));
1851 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T, j * 2));
Paul Bakker5121ce52009-01-03 21:22:43 +00001852
1853 /*
Paul Bakker50546922012-05-19 08:40:49 +00001854 * Compensate for negative A (and correct at the end)
1855 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 neg = (A->s == -1);
1857 if (neg) {
1858 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Apos, A));
Paul Bakker50546922012-05-19 08:40:49 +00001859 Apos.s = 1;
1860 A = &Apos;
1861 }
1862
1863 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001864 * If 1st call, pre-compute R^2 mod N
1865 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 if (prec_RR == NULL || prec_RR->p == NULL) {
1867 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&RR, 1));
1868 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&RR, N->n * 2 * biL));
1869 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&RR, &RR, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00001870
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 if (prec_RR != NULL) {
1872 memcpy(prec_RR, &RR, sizeof(mbedtls_mpi));
1873 }
1874 } else {
1875 memcpy(&RR, prec_RR, sizeof(mbedtls_mpi));
Paul Bakker5121ce52009-01-03 21:22:43 +00001876 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001877
1878 /*
1879 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
1880 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 if (mbedtls_mpi_cmp_mpi(A, N) >= 0) {
1882 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&W[1], A, N));
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001883 /* This should be a no-op because W[1] is already that large before
1884 * mbedtls_mpi_mod_mpi(), but it's necessary to avoid an overflow
Tom Cosgrove93842842022-08-05 16:59:43 +01001885 * in mpi_montmul() below, so let's make sure. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[1], N->n + 1));
1887 } else {
1888 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[1], A));
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001889 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001890
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001891 /* Note that this is safe because W[1] always has at least N->n limbs
1892 * (it grew above and was preserved by mbedtls_mpi_copy()). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001893 mpi_montmul(&W[1], &RR, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001894
1895 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01001896 * W[x_index] = R^2 * R^-1 mod N = R mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001897 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[x_index], &RR));
1899 mpi_montred(&W[x_index], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001900
Janos Follathc8d66d52022-11-22 10:47:10 +00001901
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 if (window_bitsize > 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001903 /*
Janos Follath74601202022-11-21 15:54:20 +00001904 * W[i] = W[1] ^ i
1905 *
1906 * The first bit of the sliding window is always 1 and therefore we
1907 * only need to store the second half of the table.
Janos Follathc8d66d52022-11-22 10:47:10 +00001908 *
1909 * (There are two special elements in the table: W[0] for the
1910 * accumulator/result and W[1] for A in Montgomery form. Both of these
1911 * are already set at this point.)
Paul Bakker5121ce52009-01-03 21:22:43 +00001912 */
Janos Follath74601202022-11-21 15:54:20 +00001913 j = w_table_used_size / 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[j], N->n + 1));
1916 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[j], &W[1]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001917
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 for (i = 0; i < window_bitsize - 1; i++) {
1919 mpi_montmul(&W[j], &W[j], N, mm, &T);
1920 }
Paul Bakker0d7702c2013-10-29 16:18:35 +01001921
Paul Bakker5121ce52009-01-03 21:22:43 +00001922 /*
1923 * W[i] = W[i - 1] * W[1]
1924 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 for (i = j + 1; i < w_table_used_size; i++) {
1926 MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[i], N->n + 1));
1927 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[i], &W[i - 1]));
Paul Bakker5121ce52009-01-03 21:22:43 +00001928
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 mpi_montmul(&W[i], &W[1], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001930 }
1931 }
1932
1933 nblimbs = E->n;
1934 bufsize = 0;
1935 nbits = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001936 state = 0;
1937
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 while (1) {
1939 if (bufsize == 0) {
1940 if (nblimbs == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001941 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001943
Paul Bakker0d7702c2013-10-29 16:18:35 +01001944 nblimbs--;
1945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 bufsize = sizeof(mbedtls_mpi_uint) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00001947 }
1948
1949 bufsize--;
1950
1951 ei = (E->p[nblimbs] >> bufsize) & 1;
1952
1953 /*
1954 * skip leading 0s
1955 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 if (ei == 0 && state == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001957 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 if (ei == 0 && state == 1) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001961 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01001962 * out of window, square W[x_index]
Paul Bakker5121ce52009-01-03 21:22:43 +00001963 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, x_index));
1965 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001966 continue;
1967 }
1968
1969 /*
1970 * add ei to current window
1971 */
1972 state = 2;
1973
1974 nbits++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 exponent_bits_in_window |= (ei << (window_bitsize - nbits));
Paul Bakker5121ce52009-01-03 21:22:43 +00001976
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 if (nbits == window_bitsize) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001978 /*
Janos Follath7fa11b82022-11-21 14:48:02 +00001979 * W[x_index] = W[x_index]^window_bitsize R^-1 mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001980 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 for (i = 0; i < window_bitsize; i++) {
1982 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size,
1983 x_index));
1984 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Janos Follathb764ee12022-10-04 14:00:09 +01001985 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001986
1987 /*
Janos Follath7fa11b82022-11-21 14:48:02 +00001988 * W[x_index] = W[x_index] * W[exponent_bits_in_window] R^-1 mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00001989 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size,
1991 exponent_bits_in_window));
1992 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00001993
1994 state--;
1995 nbits = 0;
Janos Follath7fa11b82022-11-21 14:48:02 +00001996 exponent_bits_in_window = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001997 }
1998 }
1999
2000 /*
2001 * process the remaining bits
2002 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 for (i = 0; i < nbits; i++) {
2004 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, x_index));
2005 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00002006
Janos Follath7fa11b82022-11-21 14:48:02 +00002007 exponent_bits_in_window <<= 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002008
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 if ((exponent_bits_in_window & ((size_t) 1 << window_bitsize)) != 0) {
2010 MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, 1));
2011 mpi_montmul(&W[x_index], &WW, N, mm, &T);
Janos Follathb764ee12022-10-04 14:00:09 +01002012 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002013 }
2014
2015 /*
Janos Follath8e7d6a02022-10-04 13:27:40 +01002016 * W[x_index] = A^E * R * R^-1 mod N = A^E mod N
Paul Bakker5121ce52009-01-03 21:22:43 +00002017 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 mpi_montred(&W[x_index], N, mm, &T);
Paul Bakker5121ce52009-01-03 21:22:43 +00002019
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 if (neg && E->n != 0 && (E->p[0] & 1) != 0) {
Janos Follath8e7d6a02022-10-04 13:27:40 +01002021 W[x_index].s = -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&W[x_index], N, &W[x_index]));
Paul Bakkerf6198c12012-05-16 08:02:29 +00002023 }
2024
Janos Follath8e7d6a02022-10-04 13:27:40 +01002025 /*
2026 * Load the result in the output variable.
2027 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 mbedtls_mpi_copy(X, &W[x_index]);
Janos Follath8e7d6a02022-10-04 13:27:40 +01002029
Paul Bakker5121ce52009-01-03 21:22:43 +00002030cleanup:
2031
Janos Follathb2c2fca2022-11-21 15:05:31 +00002032 /* The first bit of the sliding window is always 1 and therefore the first
2033 * half of the table was unused. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 for (i = w_table_used_size/2; i < w_table_used_size; i++) {
2035 mbedtls_mpi_free(&W[i]);
2036 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 mbedtls_mpi_free(&W[x_index]);
2039 mbedtls_mpi_free(&W[1]);
2040 mbedtls_mpi_free(&T);
2041 mbedtls_mpi_free(&Apos);
2042 mbedtls_mpi_free(&WW);
Paul Bakker6c591fa2011-05-05 11:49:20 +00002043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 if (prec_RR == NULL || prec_RR->p == NULL) {
2045 mbedtls_mpi_free(&RR);
2046 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002047
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002049}
2050
Paul Bakker5121ce52009-01-03 21:22:43 +00002051/*
2052 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
2053 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002054int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
Paul Bakker5121ce52009-01-03 21:22:43 +00002055{
Janos Follath24eed8d2019-11-22 13:21:35 +00002056 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002057 size_t lz, lzt;
Alexander Ke8ad49f2019-08-16 16:16:07 +03002058 mbedtls_mpi TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00002059
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 MPI_VALIDATE_RET(G != NULL);
2061 MPI_VALIDATE_RET(A != NULL);
2062 MPI_VALIDATE_RET(B != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00002063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00002065
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A));
2067 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B));
Paul Bakker5121ce52009-01-03 21:22:43 +00002068
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 lz = mbedtls_mpi_lsb(&TA);
2070 lzt = mbedtls_mpi_lsb(&TB);
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002071
Gilles Peskine27253bc2021-06-09 13:26:43 +02002072 /* The loop below gives the correct result when A==0 but not when B==0.
2073 * So have a special case for B==0. Leverage the fact that we just
2074 * calculated the lsb and lsb(B)==0 iff B is odd or 0 to make the test
2075 * slightly more efficient than cmp_int(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 if (lzt == 0 && mbedtls_mpi_get_bit(&TB, 0) == 0) {
2077 ret = mbedtls_mpi_copy(G, A);
Gilles Peskine27253bc2021-06-09 13:26:43 +02002078 goto cleanup;
2079 }
2080
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 if (lzt < lz) {
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002082 lz = lzt;
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002084
Paul Bakker5121ce52009-01-03 21:22:43 +00002085 TA.s = TB.s = 1;
2086
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002087 /* We mostly follow the procedure described in HAC 14.54, but with some
2088 * minor differences:
2089 * - Sequences of multiplications or divisions by 2 are grouped into a
2090 * single shift operation.
Gilles Peskineb09c7ee2021-06-21 18:58:39 +02002091 * - The procedure in HAC assumes that 0 < TB <= TA.
2092 * - The condition TB <= TA is not actually necessary for correctness.
2093 * TA and TB have symmetric roles except for the loop termination
2094 * condition, and the shifts at the beginning of the loop body
2095 * remove any significance from the ordering of TA vs TB before
2096 * the shifts.
2097 * - If TA = 0, the loop goes through 0 iterations and the result is
2098 * correctly TB.
2099 * - The case TB = 0 was short-circuited above.
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002100 *
2101 * For the correctness proof below, decompose the original values of
2102 * A and B as
2103 * A = sa * 2^a * A' with A'=0 or A' odd, and sa = +-1
2104 * B = sb * 2^b * B' with B'=0 or B' odd, and sb = +-1
2105 * Then gcd(A, B) = 2^{min(a,b)} * gcd(A',B'),
2106 * and gcd(A',B') is odd or 0.
2107 *
2108 * At the beginning, we have TA = |A| and TB = |B| so gcd(A,B) = gcd(TA,TB).
2109 * The code maintains the following invariant:
2110 * gcd(A,B) = 2^k * gcd(TA,TB) for some k (I)
Gilles Peskine4df3f1f2021-06-15 22:09:39 +02002111 */
2112
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002113 /* Proof that the loop terminates:
2114 * At each iteration, either the right-shift by 1 is made on a nonzero
2115 * value and the nonnegative integer bitlen(TA) + bitlen(TB) decreases
2116 * by at least 1, or the right-shift by 1 is made on zero and then
2117 * TA becomes 0 which ends the loop (TB cannot be 0 if it is right-shifted
2118 * since in that case TB is calculated from TB-TA with the condition TB>TA).
2119 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 while (mbedtls_mpi_cmp_int(&TA, 0) != 0) {
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002121 /* Divisions by 2 preserve the invariant (I). */
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, mbedtls_mpi_lsb(&TA)));
2123 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, mbedtls_mpi_lsb(&TB)));
Paul Bakker5121ce52009-01-03 21:22:43 +00002124
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002125 /* Set either TA or TB to |TA-TB|/2. Since TA and TB are both odd,
2126 * TA-TB is even so the division by 2 has an integer result.
2127 * Invariant (I) is preserved since any odd divisor of both TA and TB
2128 * also divides |TA-TB|/2, and any odd divisor of both TA and |TA-TB|/2
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002129 * also divides TB, and any odd divisor of both TB and |TA-TB|/2 also
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002130 * divides TA.
2131 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 if (mbedtls_mpi_cmp_mpi(&TA, &TB) >= 0) {
2133 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TA, &TA, &TB));
2134 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, 1));
2135 } else {
2136 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TB, &TB, &TA));
2137 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 }
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002139 /* Note that one of TA or TB is still odd. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 }
2141
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002142 /* By invariant (I), gcd(A,B) = 2^k * gcd(TA,TB) for some k.
2143 * At the loop exit, TA = 0, so gcd(TA,TB) = TB.
2144 * - If there was at least one loop iteration, then one of TA or TB is odd,
2145 * and TA = 0, so TB is odd and gcd(TA,TB) = gcd(A',B'). In this case,
2146 * lz = min(a,b) so gcd(A,B) = 2^lz * TB.
2147 * - If there was no loop iteration, then A was 0, and gcd(A,B) = B.
Gilles Peskine4d3fd362021-06-21 11:40:38 +02002148 * 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 +02002149 */
2150
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&TB, lz));
2152 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(G, &TB));
Paul Bakker5121ce52009-01-03 21:22:43 +00002153
2154cleanup:
2155
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TB);
Paul Bakker5121ce52009-01-03 21:22:43 +00002157
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002159}
2160
Paul Bakker33dc46b2014-04-30 16:11:39 +02002161/*
2162 * Fill X with size bytes of random.
Gilles Peskine22cdd0c2022-10-27 20:15:13 +02002163 * The bytes returned from the RNG are used in a specific order which
2164 * is suitable for deterministic ECDSA (see the specification of
2165 * mbedtls_mpi_random() and the implementation in mbedtls_mpi_fill_random()).
Paul Bakker33dc46b2014-04-30 16:11:39 +02002166 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002167int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
2168 int (*f_rng)(void *, unsigned char *, size_t),
2169 void *p_rng)
Paul Bakker287781a2011-03-26 13:18:49 +00002170{
Janos Follath24eed8d2019-11-22 13:21:35 +00002171 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 const size_t limbs = CHARS_TO_LIMBS(size);
Hanno Beckerda1655a2017-10-18 14:21:44 +01002173
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 MPI_VALIDATE_RET(X != NULL);
2175 MPI_VALIDATE_RET(f_rng != NULL);
Paul Bakker33dc46b2014-04-30 16:11:39 +02002176
Hanno Beckerda1655a2017-10-18 14:21:44 +01002177 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
2179 if (size == 0) {
2180 return 0;
2181 }
Paul Bakker287781a2011-03-26 13:18:49 +00002182
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 ret = mbedtls_mpi_core_fill_random(X->p, X->n, size, f_rng, p_rng);
Paul Bakker287781a2011-03-26 13:18:49 +00002184
2185cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 return ret;
Paul Bakker287781a2011-03-26 13:18:49 +00002187}
2188
Gilles Peskine449bd832023-01-11 14:50:10 +01002189int mbedtls_mpi_random(mbedtls_mpi *X,
2190 mbedtls_mpi_sint min,
2191 const mbedtls_mpi *N,
2192 int (*f_rng)(void *, unsigned char *, size_t),
2193 void *p_rng)
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002194{
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 if (min < 0) {
2196 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2197 }
2198 if (mbedtls_mpi_cmp_int(N, min) <= 0) {
2199 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2200 }
Gilles Peskine1e918f42021-03-29 22:14:51 +02002201
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002202 /* Ensure that target MPI has exactly the same number of limbs
2203 * as the upper bound, even if the upper bound has leading zeros.
Gilles Peskine6b7ce962022-12-15 15:04:33 +01002204 * This is necessary for mbedtls_mpi_core_random. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 int ret = mbedtls_mpi_resize_clear(X, N->n);
2206 if (ret != 0) {
2207 return ret;
2208 }
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 return mbedtls_mpi_core_random(X->p, min, N->p, X->n, f_rng, p_rng);
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002211}
2212
Paul Bakker5121ce52009-01-03 21:22:43 +00002213/*
2214 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2215 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002216int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
Paul Bakker5121ce52009-01-03 21:22:43 +00002217{
Janos Follath24eed8d2019-11-22 13:21:35 +00002218 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 MPI_VALIDATE_RET(X != NULL);
2221 MPI_VALIDATE_RET(A != NULL);
2222 MPI_VALIDATE_RET(N != NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00002223
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 if (mbedtls_mpi_cmp_int(N, 1) <= 0) {
2225 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2226 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002227
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TU); mbedtls_mpi_init(&U1); mbedtls_mpi_init(&U2);
2229 mbedtls_mpi_init(&G); mbedtls_mpi_init(&TB); mbedtls_mpi_init(&TV);
2230 mbedtls_mpi_init(&V1); mbedtls_mpi_init(&V2);
Paul Bakker5121ce52009-01-03 21:22:43 +00002231
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, A, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 goto cleanup;
2237 }
2238
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&TA, A, N));
2240 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TU, &TA));
2241 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, N));
2242 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TV, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002243
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U1, 1));
2245 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U2, 0));
2246 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V1, 0));
2247 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002248
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 do {
2250 while ((TU.p[0] & 1) == 0) {
2251 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TU, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002252
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 if ((U1.p[0] & 1) != 0 || (U2.p[0] & 1) != 0) {
2254 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&U1, &U1, &TB));
2255 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &TA));
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 }
2257
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U1, 1));
2259 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 }
2261
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 while ((TV.p[0] & 1) == 0) {
2263 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TV, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002264
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 if ((V1.p[0] & 1) != 0 || (V2.p[0] & 1) != 0) {
2266 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, &TB));
2267 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &TA));
Paul Bakker5121ce52009-01-03 21:22:43 +00002268 }
2269
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V1, 1));
2271 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V2, 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 }
2273
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 if (mbedtls_mpi_cmp_mpi(&TU, &TV) >= 0) {
2275 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TU, &TU, &TV));
2276 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U1, &U1, &V1));
2277 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &V2));
2278 } else {
2279 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TV, &TV, &TU));
2280 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, &U1));
2281 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &U2));
Paul Bakker5121ce52009-01-03 21:22:43 +00002282 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 } while (mbedtls_mpi_cmp_int(&TU, 0) != 0);
2284
2285 while (mbedtls_mpi_cmp_int(&V1, 0) < 0) {
2286 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 while (mbedtls_mpi_cmp_mpi(&V1, N) >= 0) {
2290 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, N));
2291 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002292
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, &V1));
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
2295cleanup:
2296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TU); mbedtls_mpi_free(&U1); mbedtls_mpi_free(&U2);
2298 mbedtls_mpi_free(&G); mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TV);
2299 mbedtls_mpi_free(&V1); mbedtls_mpi_free(&V2);
Paul Bakker5121ce52009-01-03 21:22:43 +00002300
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002302}
2303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002305
Paul Bakker5121ce52009-01-03 21:22:43 +00002306static const int small_prime[] =
2307{
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 3, 5, 7, 11, 13, 17, 19, 23,
2309 29, 31, 37, 41, 43, 47, 53, 59,
2310 61, 67, 71, 73, 79, 83, 89, 97,
2311 101, 103, 107, 109, 113, 127, 131, 137,
2312 139, 149, 151, 157, 163, 167, 173, 179,
2313 181, 191, 193, 197, 199, 211, 223, 227,
2314 229, 233, 239, 241, 251, 257, 263, 269,
2315 271, 277, 281, 283, 293, 307, 311, 313,
2316 317, 331, 337, 347, 349, 353, 359, 367,
2317 373, 379, 383, 389, 397, 401, 409, 419,
2318 421, 431, 433, 439, 443, 449, 457, 461,
2319 463, 467, 479, 487, 491, 499, 503, 509,
2320 521, 523, 541, 547, 557, 563, 569, 571,
2321 577, 587, 593, 599, 601, 607, 613, 617,
2322 619, 631, 641, 643, 647, 653, 659, 661,
2323 673, 677, 683, 691, 701, 709, 719, 727,
2324 733, 739, 743, 751, 757, 761, 769, 773,
2325 787, 797, 809, 811, 821, 823, 827, 829,
2326 839, 853, 857, 859, 863, 877, 881, 883,
2327 887, 907, 911, 919, 929, 937, 941, 947,
2328 953, 967, 971, 977, 983, 991, 997, -103
Paul Bakker5121ce52009-01-03 21:22:43 +00002329};
2330
2331/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002332 * Small divisors test (X must be positive)
2333 *
2334 * Return values:
2335 * 0: no small factor (possible prime, more tests needed)
2336 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002338 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002339 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002340static int mpi_check_small_factors(const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +00002341{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002342 int ret = 0;
2343 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 mbedtls_mpi_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +00002345
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 if ((X->p[0] & 1) == 0) {
2347 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2348 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002349
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 for (i = 0; small_prime[i] > 0; i++) {
2351 if (mbedtls_mpi_cmp_int(X, small_prime[i]) <= 0) {
2352 return 1;
2353 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, small_prime[i]));
Paul Bakker5121ce52009-01-03 21:22:43 +00002356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 if (r == 0) {
2358 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2359 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002360 }
2361
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002362cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 return ret;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002364}
2365
2366/*
2367 * Miller-Rabin pseudo-primality test (HAC 4.24)
2368 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002369static int mpi_miller_rabin(const mbedtls_mpi *X, size_t rounds,
2370 int (*f_rng)(void *, unsigned char *, size_t),
2371 void *p_rng)
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002372{
Pascal Junodb99183d2015-03-11 16:49:45 +01002373 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002374 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002376
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 MPI_VALIDATE_RET(X != NULL);
2378 MPI_VALIDATE_RET(f_rng != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00002379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 mbedtls_mpi_init(&W); mbedtls_mpi_init(&R);
2381 mbedtls_mpi_init(&T); mbedtls_mpi_init(&A);
2382 mbedtls_mpi_init(&RR);
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002383
Paul Bakker5121ce52009-01-03 21:22:43 +00002384 /*
2385 * W = |X| - 1
2386 * R = W >> lsb( W )
2387 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&W, X, 1));
2389 s = mbedtls_mpi_lsb(&W);
2390 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R, &W));
2391 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&R, s));
Paul Bakker5121ce52009-01-03 21:22:43 +00002392
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 for (i = 0; i < rounds; i++) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002394 /*
2395 * pick a random A, 1 < A < |X| - 1
2396 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002397 count = 0;
2398 do {
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&A, X->n * ciL, f_rng, p_rng));
Pascal Junodb99183d2015-03-11 16:49:45 +01002400
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 j = mbedtls_mpi_bitlen(&A);
2402 k = mbedtls_mpi_bitlen(&W);
Pascal Junodb99183d2015-03-11 16:49:45 +01002403 if (j > k) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 A.p[A.n - 1] &= ((mbedtls_mpi_uint) 1 << (k - (A.n - 1) * biL - 1)) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002405 }
2406
2407 if (count++ > 30) {
Jens Wiklanderf08aa3e2019-01-17 13:30:57 +01002408 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2409 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002410 }
2411
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 } while (mbedtls_mpi_cmp_mpi(&A, &W) >= 0 ||
2413 mbedtls_mpi_cmp_int(&A, 1) <= 0);
Paul Bakker5121ce52009-01-03 21:22:43 +00002414
2415 /*
2416 * A = A^R mod |X|
2417 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&A, &A, &R, X, &RR));
Paul Bakker5121ce52009-01-03 21:22:43 +00002419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 if (mbedtls_mpi_cmp_mpi(&A, &W) == 0 ||
2421 mbedtls_mpi_cmp_int(&A, 1) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002422 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002424
2425 j = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 while (j < s && mbedtls_mpi_cmp_mpi(&A, &W) != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002427 /*
2428 * A = A * A mod |X|
2429 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &A, &A));
2431 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&A, &T, X));
Paul Bakker5121ce52009-01-03 21:22:43 +00002432
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 if (mbedtls_mpi_cmp_int(&A, 1) == 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002434 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002436
2437 j++;
2438 }
2439
2440 /*
2441 * not prime if A != |X| - 1 or A == 1
2442 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 if (mbedtls_mpi_cmp_mpi(&A, &W) != 0 ||
2444 mbedtls_mpi_cmp_int(&A, 1) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002446 break;
2447 }
2448 }
2449
2450cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 mbedtls_mpi_free(&W); mbedtls_mpi_free(&R);
2452 mbedtls_mpi_free(&T); mbedtls_mpi_free(&A);
2453 mbedtls_mpi_free(&RR);
Paul Bakker5121ce52009-01-03 21:22:43 +00002454
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002456}
2457
2458/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002459 * Pseudo-primality test: small factors, then Miller-Rabin
2460 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002461int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
2462 int (*f_rng)(void *, unsigned char *, size_t),
2463 void *p_rng)
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002464{
Janos Follath24eed8d2019-11-22 13:21:35 +00002465 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 mbedtls_mpi XX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 MPI_VALIDATE_RET(X != NULL);
2468 MPI_VALIDATE_RET(f_rng != NULL);
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002469
2470 XX.s = 1;
2471 XX.n = X->n;
2472 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002473
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 if (mbedtls_mpi_cmp_int(&XX, 0) == 0 ||
2475 mbedtls_mpi_cmp_int(&XX, 1) == 0) {
2476 return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002477 }
2478
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 if (mbedtls_mpi_cmp_int(&XX, 2) == 0) {
2480 return 0;
2481 }
2482
2483 if ((ret = mpi_check_small_factors(&XX)) != 0) {
2484 if (ret == 1) {
2485 return 0;
2486 }
2487
2488 return ret;
2489 }
2490
2491 return mpi_miller_rabin(&XX, rounds, f_rng, p_rng);
Janos Follathf301d232018-08-14 13:34:01 +01002492}
2493
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002494/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002495 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002496 *
Janos Follathf301d232018-08-14 13:34:01 +01002497 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2498 * be either 1024 bits or 1536 bits long, and flags must contain
2499 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002500 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002501int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
2502 int (*f_rng)(void *, unsigned char *, size_t),
2503 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00002504{
Jethro Beekman66689272018-02-14 19:24:10 -08002505#ifdef MBEDTLS_HAVE_INT64
2506// ceil(2^63.5)
2507#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2508#else
2509// ceil(2^31.5)
2510#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2511#endif
2512 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002513 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002514 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 mbedtls_mpi_uint r;
2516 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 MPI_VALIDATE_RET(X != NULL);
2519 MPI_VALIDATE_RET(f_rng != NULL);
Hanno Becker73d7d792018-12-11 10:35:51 +00002520
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 if (nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS) {
2522 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
2523 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002524
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 mbedtls_mpi_init(&Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00002526
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 n = BITS_TO_LIMBS(nbits);
Paul Bakker5121ce52009-01-03 21:22:43 +00002528
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR) == 0) {
Janos Follathda31fa12018-09-03 14:45:23 +01002530 /*
2531 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2532 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 rounds = ((nbits >= 1300) ? 2 : (nbits >= 850) ? 3 :
2534 (nbits >= 650) ? 4 : (nbits >= 350) ? 8 :
2535 (nbits >= 250) ? 12 : (nbits >= 150) ? 18 : 27);
2536 } else {
Janos Follathda31fa12018-09-03 14:45:23 +01002537 /*
2538 * 2^-100 error probability, number of rounds computed based on HAC,
2539 * fact 4.48
2540 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 rounds = ((nbits >= 1450) ? 4 : (nbits >= 1150) ? 5 :
2542 (nbits >= 1000) ? 6 : (nbits >= 850) ? 7 :
2543 (nbits >= 750) ? 8 : (nbits >= 500) ? 13 :
2544 (nbits >= 250) ? 28 : (nbits >= 150) ? 40 : 51);
Janos Follathda31fa12018-09-03 14:45:23 +01002545 }
2546
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 while (1) {
2548 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(X, n * ciL, f_rng, p_rng));
Jethro Beekman66689272018-02-14 19:24:10 -08002549 /* 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 +01002550 if (X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2) {
2551 continue;
2552 }
Jethro Beekman66689272018-02-14 19:24:10 -08002553
2554 k = n * biL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 if (k > nbits) {
2556 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(X, k - nbits));
2557 }
Jethro Beekman66689272018-02-14 19:24:10 -08002558 X->p[0] |= 1;
2559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH) == 0) {
2561 ret = mbedtls_mpi_is_prime_ext(X, rounds, f_rng, p_rng);
Jethro Beekman66689272018-02-14 19:24:10 -08002562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002564 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 }
2566 } else {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002567 /*
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01002568 * A necessary condition for Y and X = 2Y + 1 to be prime
Jethro Beekman66689272018-02-14 19:24:10 -08002569 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2570 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002571 */
Jethro Beekman66689272018-02-14 19:24:10 -08002572
2573 X->p[0] |= 2;
2574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, 3));
2576 if (r == 0) {
2577 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 8));
2578 } else if (r == 1) {
2579 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 4));
2580 }
Jethro Beekman66689272018-02-14 19:24:10 -08002581
2582 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, X));
2584 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, 1));
Jethro Beekman66689272018-02-14 19:24:10 -08002585
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 while (1) {
Jethro Beekman66689272018-02-14 19:24:10 -08002587 /*
2588 * First, check small factors for X and Y
2589 * before doing Miller-Rabin on any of them
2590 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 if ((ret = mpi_check_small_factors(X)) == 0 &&
2592 (ret = mpi_check_small_factors(&Y)) == 0 &&
2593 (ret = mpi_miller_rabin(X, rounds, f_rng, p_rng))
2594 == 0 &&
2595 (ret = mpi_miller_rabin(&Y, rounds, f_rng, p_rng))
2596 == 0) {
Jethro Beekman66689272018-02-14 19:24:10 -08002597 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 }
Jethro Beekman66689272018-02-14 19:24:10 -08002599
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
Jethro Beekman66689272018-02-14 19:24:10 -08002601 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 }
Jethro Beekman66689272018-02-14 19:24:10 -08002603
2604 /*
2605 * Next candidates. We want to preserve Y = (X-1) / 2 and
2606 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2607 * so up Y by 6 and X by 12.
2608 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 12));
2610 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&Y, &Y, 6));
Paul Bakker5121ce52009-01-03 21:22:43 +00002611 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 }
2613 }
2614
2615cleanup:
2616
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 mbedtls_mpi_free(&Y);
Paul Bakker5121ce52009-01-03 21:22:43 +00002618
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002620}
2621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002625
Paul Bakker23986e52011-04-24 08:57:21 +00002626#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002627
2628static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2629{
2630 { 693, 609, 21 },
2631 { 1764, 868, 28 },
2632 { 768454923, 542167814, 1 }
2633};
2634
Paul Bakker5121ce52009-01-03 21:22:43 +00002635/*
2636 * Checkup routine
2637 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002638int mbedtls_mpi_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +00002639{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002640 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00002642
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 mbedtls_mpi_init(&A); mbedtls_mpi_init(&E); mbedtls_mpi_init(&N); mbedtls_mpi_init(&X);
2644 mbedtls_mpi_init(&Y); mbedtls_mpi_init(&U); mbedtls_mpi_init(&V);
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&A, 16,
2647 "EFE021C2645FD1DC586E69184AF4A31E" \
2648 "D5F53E93B5F123FA41680867BA110131" \
2649 "944FE7952E2517337780CB0DB80E61AA" \
2650 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002651
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&E, 16,
2653 "B2E7EFD37075B9F03FF989C7C5051C20" \
2654 "34D2A323810251127E7BF8625A4F49A5" \
2655 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
2656 "5B5C25763222FEFCCFC38B832366C29E"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002657
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&N, 16,
2659 "0066A198186C18C10B2F5ED9B522752A" \
2660 "9830B69916E535C8F047518A889A43A5" \
2661 "94B6BED27A168D31D4A52F88925AA8F5"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002662
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&X, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002664
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2666 "602AB7ECA597A3D6B56FF9829A5E8B85" \
2667 "9E857EA95A03512E2BAE7391688D264A" \
2668 "A5663B0341DB9CCFD2C4C5F421FEC814" \
2669 "8001B72E848A38CAE1C65F78E56ABDEF" \
2670 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
2671 "ECF677152EF804370C1A305CAF3B5BF1" \
2672 "30879B56C61DE584A0F53A2447A51E"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 if (verbose != 0) {
2675 mbedtls_printf(" MPI test #1 (mul_mpi): ");
2676 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002677
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2679 if (verbose != 0) {
2680 mbedtls_printf("failed\n");
2681 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002682
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002683 ret = 1;
2684 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002685 }
2686
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 if (verbose != 0) {
2688 mbedtls_printf("passed\n");
2689 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002690
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&X, &Y, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2694 "256567336059E52CAE22925474705F39A94"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&V, 16,
2697 "6613F26162223DF488E9CD48CC132C7A" \
2698 "0AC93C701B001B092E4E5B9F73BCD27B" \
2699 "9EE50D0657C77F374E903CDFA4C642"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 if (verbose != 0) {
2702 mbedtls_printf(" MPI test #2 (div_mpi): ");
2703 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002704
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0 ||
2706 mbedtls_mpi_cmp_mpi(&Y, &V) != 0) {
2707 if (verbose != 0) {
2708 mbedtls_printf("failed\n");
2709 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002711 ret = 1;
2712 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002713 }
2714
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 if (verbose != 0) {
2716 mbedtls_printf("passed\n");
2717 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002718
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&X, &A, &E, &N, NULL));
Paul Bakker5121ce52009-01-03 21:22:43 +00002720
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2722 "36E139AEA55215609D2816998ED020BB" \
2723 "BD96C37890F65171D948E9BC7CBAA4D9" \
2724 "325D24D6A3C12710F10A09FA08AB87"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002725
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 if (verbose != 0) {
2727 mbedtls_printf(" MPI test #3 (exp_mod): ");
2728 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002729
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2731 if (verbose != 0) {
2732 mbedtls_printf("failed\n");
2733 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002734
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002735 ret = 1;
2736 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002737 }
2738
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 if (verbose != 0) {
2740 mbedtls_printf("passed\n");
2741 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002742
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&X, &A, &N));
Paul Bakker5121ce52009-01-03 21:22:43 +00002744
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
2746 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
2747 "C3DBA76456363A10869622EAC2DD84EC" \
2748 "C5B8A74DAC4D09E03B5E0BE779F2DF61"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002749
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 if (verbose != 0) {
2751 mbedtls_printf(" MPI test #4 (inv_mod): ");
2752 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002753
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
2755 if (verbose != 0) {
2756 mbedtls_printf("failed\n");
2757 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002758
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002759 ret = 1;
2760 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002761 }
2762
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 if (verbose != 0) {
2764 mbedtls_printf("passed\n");
2765 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002766
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 if (verbose != 0) {
2768 mbedtls_printf(" MPI test #5 (simple gcd): ");
2769 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002770
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 for (i = 0; i < GCD_PAIR_COUNT; i++) {
2772 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&X, gcd_pairs[i][0]));
2773 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Y, gcd_pairs[i][1]));
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002774
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&A, &X, &Y));
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002776
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 if (mbedtls_mpi_cmp_int(&A, gcd_pairs[i][2]) != 0) {
2778 if (verbose != 0) {
2779 mbedtls_printf("failed at %d\n", i);
2780 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002781
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002782 ret = 1;
2783 goto cleanup;
2784 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002785 }
2786
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 if (verbose != 0) {
2788 mbedtls_printf("passed\n");
2789 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002790
Paul Bakker5121ce52009-01-03 21:22:43 +00002791cleanup:
2792
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 if (ret != 0 && verbose != 0) {
2794 mbedtls_printf("Unexpected error, return code = %08X\n", (unsigned int) ret);
2795 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002796
Gilles Peskine449bd832023-01-11 14:50:10 +01002797 mbedtls_mpi_free(&A); mbedtls_mpi_free(&E); mbedtls_mpi_free(&N); mbedtls_mpi_free(&X);
2798 mbedtls_mpi_free(&Y); mbedtls_mpi_free(&U); mbedtls_mpi_free(&V);
Paul Bakker5121ce52009-01-03 21:22:43 +00002799
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 if (verbose != 0) {
2801 mbedtls_printf("\n");
2802 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002803
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002805}
2806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809#endif /* MBEDTLS_BIGNUM_C */