blob: 70da0fd5687d38b68c6d8b4a3ff5b8e4966b51f8 [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"
41#include "mbedtls/bn_mul.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050042#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000043#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Rich Evans00ab4702015-02-06 13:43:58 +000045#include <string.h>
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020049#else
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <stdio.h>
51#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#define mbedtls_printf printf
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020053#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020055#endif
56
Hanno Becker73d7d792018-12-11 10:35:51 +000057#define MPI_VALIDATE_RET( cond ) \
58 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA )
59#define MPI_VALIDATE( cond ) \
60 MBEDTLS_INTERNAL_VALIDATE( cond )
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
Paul Bakker5121ce52009-01-03 21:22:43 +000063#define biL (ciL << 3) /* bits in limb */
64#define biH (ciL << 2) /* half limb size */
65
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +010066#define MPI_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
67
Paul Bakker5121ce52009-01-03 21:22:43 +000068/*
69 * Convert between bits/chars and number of limbs
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +020070 * Divide first in order to avoid potential overflows
Paul Bakker5121ce52009-01-03 21:22:43 +000071 */
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +020072#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
73#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050075/* Implementation that should never be optimized out by the compiler */
Andres Amaya Garcia6698d2f2018-04-24 08:39:07 -050076static void mbedtls_mpi_zeroize( mbedtls_mpi_uint *v, size_t n )
77{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050078 mbedtls_platform_zeroize( v, ciL * n );
79}
80
Paul Bakker5121ce52009-01-03 21:22:43 +000081/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000082 * Initialize one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084void mbedtls_mpi_init( mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +000085{
Hanno Becker73d7d792018-12-11 10:35:51 +000086 MPI_VALIDATE( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +000087
Paul Bakker6c591fa2011-05-05 11:49:20 +000088 X->s = 1;
89 X->n = 0;
90 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000091}
92
93/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000094 * Unallocate one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096void mbedtls_mpi_free( mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +000097{
Paul Bakker6c591fa2011-05-05 11:49:20 +000098 if( X == NULL )
99 return;
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Paul Bakker6c591fa2011-05-05 11:49:20 +0000101 if( X->p != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 {
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200103 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 }
106
Paul Bakker6c591fa2011-05-05 11:49:20 +0000107 X->s = 1;
108 X->n = 0;
109 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000110}
111
112/*
113 * Enlarge to the specified number of limbs
114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000116{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_mpi_uint *p;
Hanno Becker73d7d792018-12-11 10:35:51 +0000118 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200121 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Paul Bakkerf9688572011-05-05 10:00:45 +0000122
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 if( X->n < nblimbs )
124 {
Simon Butcher29176892016-05-20 00:19:09 +0100125 if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( nblimbs, ciL ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200126 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 if( X->p != NULL )
129 {
130 memcpy( p, X->p, X->n * ciL );
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200131 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 }
134
135 X->n = nblimbs;
136 X->p = p;
137 }
138
139 return( 0 );
140}
141
142/*
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100143 * Resize down as much as possible,
144 * while keeping at least the specified number of limbs
145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100147{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_mpi_uint *p;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100149 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000150 MPI_VALIDATE_RET( X != NULL );
151
152 if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
153 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100154
Gilles Peskinee2f563e2020-01-20 21:17:43 +0100155 /* Actually resize up if there are currently fewer than nblimbs limbs. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100156 if( X->n <= nblimbs )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 return( mbedtls_mpi_grow( X, nblimbs ) );
Gilles Peskine322752b2020-01-21 13:59:51 +0100158 /* After this point, then X->n > nblimbs and in particular X->n > 0. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100159
160 for( i = X->n - 1; i > 0; i-- )
161 if( X->p[i] != 0 )
162 break;
163 i++;
164
165 if( i < nblimbs )
166 i = nblimbs;
167
Simon Butcher29176892016-05-20 00:19:09 +0100168 if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( i, ciL ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200169 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100170
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100171 if( X->p != NULL )
172 {
173 memcpy( p, X->p, i * ciL );
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200174 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_free( X->p );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100176 }
177
178 X->n = i;
179 X->p = p;
180
181 return( 0 );
182}
183
Gilles Peskine3130ce22021-06-02 22:17:52 +0200184/* Resize X to have exactly n limbs and set it to 0. */
185static int mbedtls_mpi_resize_clear( mbedtls_mpi *X, size_t limbs )
186{
187 if( limbs == 0 )
188 {
189 mbedtls_mpi_free( X );
190 return( 0 );
191 }
192 else if( X->n == limbs )
193 {
194 memset( X->p, 0, limbs * ciL );
195 X->s = 1;
196 return( 0 );
197 }
198 else
199 {
200 mbedtls_mpi_free( X );
201 return( mbedtls_mpi_grow( X, limbs ) );
202 }
203}
204
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100205/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000206 * Copy the contents of Y into X
207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000209{
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100210 int ret = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000211 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000212 MPI_VALIDATE_RET( X != NULL );
213 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
215 if( X == Y )
216 return( 0 );
217
Gilles Peskinedb420622020-01-20 21:12:50 +0100218 if( Y->n == 0 )
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_mpi_free( X );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200221 return( 0 );
222 }
223
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 for( i = Y->n - 1; i > 0; i-- )
225 if( Y->p[i] != 0 )
226 break;
227 i++;
228
229 X->s = Y->s;
230
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100231 if( X->n < i )
232 {
233 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i ) );
234 }
235 else
236 {
237 memset( X->p + i, 0, ( X->n - i ) * ciL );
238 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 memcpy( X->p, Y->p, i * ciL );
241
242cleanup:
243
244 return( ret );
245}
246
247/*
248 * Swap the contents of X and Y
249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000251{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000253 MPI_VALIDATE( X != NULL );
254 MPI_VALIDATE( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 memcpy( &T, X, sizeof( mbedtls_mpi ) );
257 memcpy( X, Y, sizeof( mbedtls_mpi ) );
258 memcpy( Y, &T, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000259}
260
Manuel Pégourié-Gonnard5325b972021-06-07 09:51:00 +0200261/**
262 * Select between two sign values in constant-time.
263 *
264 * This is functionally equivalent to second ? a : b but uses only bit
265 * operations in order to avoid branches.
266 *
267 * \param[in] a The first sign; must be either +1 or -1.
268 * \param[in] b The second sign; must be either +1 or -1.
269 * \param[in] second Must be either 1 (return b) or 0 (return a).
270 *
271 * \return The selected sign value.
272 */
273static int mpi_safe_cond_select_sign( int a, int b, unsigned char second )
274{
275 /* In order to avoid questions about what we can reasonnably assume about
276 * the representations of signed integers, move everything to unsigned
277 * by taking advantage of the fact that a and b are either +1 or -1. */
278 unsigned ua = a + 1;
279 unsigned ub = b + 1;
280
281 /* MSVC has a warning about unary minus on unsigned integer types,
282 * but this is well-defined and precisely what we want to do here. */
283#if defined(_MSC_VER)
284#pragma warning( push )
285#pragma warning( disable : 4146 )
286#endif
287 /* all-bits 1 if assign is 1, all-bits 0 if assign is 0 */
288 const unsigned mask = -second;
289#if defined(_MSC_VER)
290#pragma warning( pop )
291#endif
292
293 /* select ua or ub */
294 unsigned ur = ( ua & ~mask ) | ( ub & mask );
295
296 /* ur is now 0 or 2, convert back to -1 or +1 */
297 return( (int) ur - 1 );
298}
299
Paul Bakker5121ce52009-01-03 21:22:43 +0000300/*
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200301 * Conditionally assign dest = src, without leaking information
302 * about whether the assignment was made or not.
303 * dest and src must be arrays of limbs of size n.
304 * assign must be 0 or 1.
305 */
306static void mpi_safe_cond_assign( size_t n,
307 mbedtls_mpi_uint *dest,
308 const mbedtls_mpi_uint *src,
309 unsigned char assign )
310{
311 size_t i;
Manuel Pégourié-Gonnardc3be3992021-05-31 11:48:45 +0200312
313 /* MSVC has a warning about unary minus on unsigned integer types,
314 * but this is well-defined and precisely what we want to do here. */
315#if defined(_MSC_VER)
316#pragma warning( push )
317#pragma warning( disable : 4146 )
318#endif
319
320 /* all-bits 1 if assign is 1, all-bits 0 if assign is 0 */
321 const mbedtls_mpi_uint mask = -assign;
322
323#if defined(_MSC_VER)
324#pragma warning( pop )
325#endif
326
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200327 for( i = 0; i < n; i++ )
Manuel Pégourié-Gonnardc3be3992021-05-31 11:48:45 +0200328 dest[i] = ( src[i] & mask ) | ( dest[i] & ~mask );
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200329}
330
331/*
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100332 * Conditionally assign X = Y, without leaking information
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +0100333 * about whether the assignment was made or not.
334 * (Leaking information about the respective sizes of X and Y is ok however.)
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100337{
338 int ret = 0;
339 size_t i;
Manuel Pégourié-Gonnardc3be3992021-05-31 11:48:45 +0200340 mbedtls_mpi_uint limb_mask;
Hanno Becker73d7d792018-12-11 10:35:51 +0000341 MPI_VALIDATE_RET( X != NULL );
342 MPI_VALIDATE_RET( Y != NULL );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100343
Manuel Pégourié-Gonnardc3be3992021-05-31 11:48:45 +0200344 /* MSVC has a warning about unary minus on unsigned integer types,
345 * but this is well-defined and precisely what we want to do here. */
346#if defined(_MSC_VER)
347#pragma warning( push )
348#pragma warning( disable : 4146 )
349#endif
350
Pascal Junodb99183d2015-03-11 16:49:45 +0100351 /* make sure assign is 0 or 1 in a time-constant manner */
352 assign = (assign | (unsigned char)-assign) >> 7;
Manuel Pégourié-Gonnardc3be3992021-05-31 11:48:45 +0200353 /* all-bits 1 if assign is 1, all-bits 0 if assign is 0 */
Manuel Pégourié-Gonnardc3be3992021-05-31 11:48:45 +0200354 limb_mask = -assign;
355
356#if defined(_MSC_VER)
357#pragma warning( pop )
358#endif
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100361
Manuel Pégourié-Gonnard5325b972021-06-07 09:51:00 +0200362 X->s = mpi_safe_cond_select_sign( X->s, Y->s, assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100363
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200364 mpi_safe_cond_assign( Y->n, X->p, Y->p, assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100365
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200366 for( i = Y->n; i < X->n; i++ )
Manuel Pégourié-Gonnardc3be3992021-05-31 11:48:45 +0200367 X->p[i] &= ~limb_mask;
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100368
369cleanup:
370 return( ret );
371}
372
373/*
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100374 * Conditionally swap X and Y, without leaking information
375 * about whether the swap was made or not.
376 * Here it is not ok to simply swap the pointers, which whould lead to
377 * different memory access patterns when X and Y are used afterwards.
378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100380{
381 int ret, s;
382 size_t i;
Manuel Pégourié-Gonnard464fe6a2021-06-03 10:54:01 +0200383 mbedtls_mpi_uint limb_mask;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 mbedtls_mpi_uint tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +0000385 MPI_VALIDATE_RET( X != NULL );
386 MPI_VALIDATE_RET( Y != NULL );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100387
388 if( X == Y )
389 return( 0 );
390
Manuel Pégourié-Gonnard464fe6a2021-06-03 10:54:01 +0200391 /* MSVC has a warning about unary minus on unsigned integer types,
392 * but this is well-defined and precisely what we want to do here. */
393#if defined(_MSC_VER)
394#pragma warning( push )
395#pragma warning( disable : 4146 )
396#endif
397
Pascal Junodb99183d2015-03-11 16:49:45 +0100398 /* make sure swap is 0 or 1 in a time-constant manner */
399 swap = (swap | (unsigned char)-swap) >> 7;
Manuel Pégourié-Gonnard464fe6a2021-06-03 10:54:01 +0200400 /* all-bits 1 if swap is 1, all-bits 0 if swap is 0 */
Manuel Pégourié-Gonnard464fe6a2021-06-03 10:54:01 +0200401 limb_mask = -swap;
402
403#if defined(_MSC_VER)
404#pragma warning( pop )
405#endif
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
408 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( Y, X->n ) );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100409
410 s = X->s;
Manuel Pégourié-Gonnard5325b972021-06-07 09:51:00 +0200411 X->s = mpi_safe_cond_select_sign( X->s, Y->s, swap );
412 Y->s = mpi_safe_cond_select_sign( Y->s, s, swap );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100413
414
415 for( i = 0; i < X->n; i++ )
416 {
417 tmp = X->p[i];
Manuel Pégourié-Gonnard464fe6a2021-06-03 10:54:01 +0200418 X->p[i] = ( X->p[i] & ~limb_mask ) | ( Y->p[i] & limb_mask );
419 Y->p[i] = ( Y->p[i] & ~limb_mask ) | ( tmp & limb_mask );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100420 }
421
422cleanup:
423 return( ret );
424}
425
426/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 * Set value from integer
428 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +0000430{
Janos Follath24eed8d2019-11-22 13:21:35 +0000431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker73d7d792018-12-11 10:35:51 +0000432 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435 memset( X->p, 0, X->n * ciL );
436
437 X->p[0] = ( z < 0 ) ? -z : z;
438 X->s = ( z < 0 ) ? -1 : 1;
439
440cleanup:
441
442 return( ret );
443}
444
445/*
Paul Bakker2f5947e2011-05-18 15:47:11 +0000446 * Get a specific bit
447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000449{
Hanno Becker73d7d792018-12-11 10:35:51 +0000450 MPI_VALIDATE_RET( X != NULL );
451
Paul Bakker2f5947e2011-05-18 15:47:11 +0000452 if( X->n * biL <= pos )
453 return( 0 );
454
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200455 return( ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000456}
457
Gilles Peskine11cdb052018-11-20 16:47:47 +0100458/* Get a specific byte, without range checks. */
459#define GET_BYTE( X, i ) \
460 ( ( ( X )->p[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff )
461
Paul Bakker2f5947e2011-05-18 15:47:11 +0000462/*
463 * Set a bit to a specific value of 0 or 1
464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000466{
467 int ret = 0;
468 size_t off = pos / biL;
469 size_t idx = pos % biL;
Hanno Becker73d7d792018-12-11 10:35:51 +0000470 MPI_VALIDATE_RET( X != NULL );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000471
472 if( val != 0 && val != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker9af723c2014-05-01 13:03:14 +0200474
Paul Bakker2f5947e2011-05-18 15:47:11 +0000475 if( X->n * biL <= pos )
476 {
477 if( val == 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200478 return( 0 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, off + 1 ) );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000481 }
482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 X->p[off] &= ~( (mbedtls_mpi_uint) 0x01 << idx );
484 X->p[off] |= (mbedtls_mpi_uint) val << idx;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000485
486cleanup:
Paul Bakker9af723c2014-05-01 13:03:14 +0200487
Paul Bakker2f5947e2011-05-18 15:47:11 +0000488 return( ret );
489}
490
491/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200492 * Return the number of less significant zero-bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494size_t mbedtls_mpi_lsb( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000495{
Paul Bakker23986e52011-04-24 08:57:21 +0000496 size_t i, j, count = 0;
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000497 MBEDTLS_INTERNAL_VALIDATE_RET( X != NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000498
499 for( i = 0; i < X->n; i++ )
Paul Bakkerf9688572011-05-05 10:00:45 +0000500 for( j = 0; j < biL; j++, count++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 if( ( ( X->p[i] >> j ) & 1 ) != 0 )
502 return( count );
503
504 return( 0 );
505}
506
507/*
Simon Butcher15b15d12015-11-26 19:35:03 +0000508 * Count leading zero bits in a given integer
509 */
510static size_t mbedtls_clz( const mbedtls_mpi_uint x )
511{
512 size_t j;
Manuel Pégourié-Gonnarde3e8edf2015-12-01 09:31:52 +0100513 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
Simon Butcher15b15d12015-11-26 19:35:03 +0000514
515 for( j = 0; j < biL; j++ )
516 {
517 if( x & mask ) break;
518
519 mask >>= 1;
520 }
521
522 return j;
523}
524
525/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200526 * Return the number of bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000527 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200528size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000529{
Paul Bakker23986e52011-04-24 08:57:21 +0000530 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200532 if( X->n == 0 )
533 return( 0 );
534
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 for( i = X->n - 1; i > 0; i-- )
536 if( X->p[i] != 0 )
537 break;
538
Simon Butcher15b15d12015-11-26 19:35:03 +0000539 j = biL - mbedtls_clz( X->p[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Paul Bakker23986e52011-04-24 08:57:21 +0000541 return( ( i * biL ) + j );
Paul Bakker5121ce52009-01-03 21:22:43 +0000542}
543
544/*
545 * Return the total size in bytes
546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547size_t mbedtls_mpi_size( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000548{
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200549 return( ( mbedtls_mpi_bitlen( X ) + 7 ) >> 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000550}
551
552/*
553 * Convert an ASCII character to digit value
554 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555static int mpi_get_digit( mbedtls_mpi_uint *d, int radix, char c )
Paul Bakker5121ce52009-01-03 21:22:43 +0000556{
557 *d = 255;
558
559 if( c >= 0x30 && c <= 0x39 ) *d = c - 0x30;
560 if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37;
561 if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57;
562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 if( *d >= (mbedtls_mpi_uint) radix )
564 return( MBEDTLS_ERR_MPI_INVALID_CHARACTER );
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
566 return( 0 );
567}
568
569/*
570 * Import from an ASCII string
571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000573{
Janos Follath24eed8d2019-11-22 13:21:35 +0000574 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000575 size_t i, j, slen, n;
Gilles Peskine80f56732021-04-03 18:26:13 +0200576 int sign = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_mpi_uint d;
578 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000579 MPI_VALIDATE_RET( X != NULL );
580 MPI_VALIDATE_RET( s != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000581
582 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000583 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
Gilles Peskine80f56732021-04-03 18:26:13 +0200587 if( s[0] == '-' )
588 {
589 ++s;
590 sign = -1;
591 }
592
Paul Bakkerff60ee62010-03-16 21:09:09 +0000593 slen = strlen( s );
594
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 if( radix == 16 )
596 {
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +0100597 if( slen > MPI_SIZE_T_MAX >> 2 )
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +0200598 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
599
Paul Bakkerff60ee62010-03-16 21:09:09 +0000600 n = BITS_TO_LIMBS( slen << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n ) );
603 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000604
Paul Bakker23986e52011-04-24 08:57:21 +0000605 for( i = slen, j = 0; i > 0; i--, j++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200608 X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000609 }
610 }
611 else
612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000614
Paul Bakkerff60ee62010-03-16 21:09:09 +0000615 for( i = 0; i < slen; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
618 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T, X, radix ) );
Gilles Peskine80f56732021-04-03 18:26:13 +0200619 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, &T, d ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 }
621 }
622
Gilles Peskine80f56732021-04-03 18:26:13 +0200623 if( sign < 0 && mbedtls_mpi_bitlen( X ) != 0 )
624 X->s = -1;
625
Paul Bakker5121ce52009-01-03 21:22:43 +0000626cleanup:
627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
630 return( ret );
631}
632
633/*
Ron Eldora16fa292018-11-20 14:07:01 +0200634 * Helper to write the digits high-order first.
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 */
Ron Eldora16fa292018-11-20 14:07:01 +0200636static int mpi_write_hlp( mbedtls_mpi *X, int radix,
637 char **p, const size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000638{
Janos Follath24eed8d2019-11-22 13:21:35 +0000639 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 mbedtls_mpi_uint r;
Ron Eldora16fa292018-11-20 14:07:01 +0200641 size_t length = 0;
642 char *p_end = *p + buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000643
Ron Eldora16fa292018-11-20 14:07:01 +0200644 do
645 {
646 if( length >= buflen )
647 {
648 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
649 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000650
Ron Eldora16fa292018-11-20 14:07:01 +0200651 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) );
652 MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) );
653 /*
654 * Write the residue in the current position, as an ASCII character.
655 */
656 if( r < 0xA )
657 *(--p_end) = (char)( '0' + r );
658 else
659 *(--p_end) = (char)( 'A' + ( r - 0xA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000660
Ron Eldora16fa292018-11-20 14:07:01 +0200661 length++;
662 } while( mbedtls_mpi_cmp_int( X, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
Ron Eldora16fa292018-11-20 14:07:01 +0200664 memmove( *p, p_end, length );
665 *p += length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000666
667cleanup:
668
669 return( ret );
670}
671
672/*
673 * Export into an ASCII string
674 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100675int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
676 char *buf, size_t buflen, size_t *olen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000677{
Paul Bakker23986e52011-04-24 08:57:21 +0000678 int ret = 0;
679 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000680 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000682 MPI_VALIDATE_RET( X != NULL );
683 MPI_VALIDATE_RET( olen != NULL );
684 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000685
686 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000687 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
Hanno Becker23cfea02019-02-04 09:45:07 +0000689 n = mbedtls_mpi_bitlen( X ); /* Number of bits necessary to present `n`. */
690 if( radix >= 4 ) n >>= 1; /* Number of 4-adic digits necessary to present
691 * `n`. If radix > 4, this might be a strict
692 * overapproximation of the number of
693 * radix-adic digits needed to present `n`. */
694 if( radix >= 16 ) n >>= 1; /* Number of hexadecimal digits necessary to
695 * present `n`. */
696
Janos Follath80470622019-03-06 13:43:02 +0000697 n += 1; /* Terminating null byte */
Hanno Becker23cfea02019-02-04 09:45:07 +0000698 n += 1; /* Compensate for the divisions above, which round down `n`
699 * in case it's not even. */
700 n += 1; /* Potential '-'-sign. */
701 n += ( n & 1 ); /* Make n even to have enough space for hexadecimal writing,
702 * which always uses an even number of hex-digits. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100704 if( buflen < n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000705 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100706 *olen = n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000708 }
709
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100710 p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000712
713 if( X->s == -1 )
Hanno Beckerc983c812019-02-01 16:41:30 +0000714 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000715 *p++ = '-';
Hanno Beckerc983c812019-02-01 16:41:30 +0000716 buflen--;
717 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000718
719 if( radix == 16 )
720 {
Paul Bakker23986e52011-04-24 08:57:21 +0000721 int c;
722 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000723
Paul Bakker23986e52011-04-24 08:57:21 +0000724 for( i = X->n, k = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000725 {
Paul Bakker23986e52011-04-24 08:57:21 +0000726 for( j = ciL; j > 0; j-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000727 {
Paul Bakker23986e52011-04-24 08:57:21 +0000728 c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000729
Paul Bakker6c343d72014-07-10 14:36:19 +0200730 if( c == 0 && k == 0 && ( i + j ) != 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000731 continue;
732
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000733 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000734 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000735 k = 1;
736 }
737 }
738 }
739 else
740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T, X ) );
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000742
743 if( T.s == -1 )
744 T.s = 1;
745
Ron Eldora16fa292018-11-20 14:07:01 +0200746 MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p, buflen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000747 }
748
749 *p++ = '\0';
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100750 *olen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
752cleanup:
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
756 return( ret );
757}
758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000760/*
761 * Read X from an opened file
762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin )
Paul Bakker5121ce52009-01-03 21:22:43 +0000764{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 mbedtls_mpi_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000766 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000767 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000768 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000769 * Buffer should have space for (short) label and decimal formatted MPI,
770 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000771 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Paul Bakker5121ce52009-01-03 21:22:43 +0000773
Hanno Becker73d7d792018-12-11 10:35:51 +0000774 MPI_VALIDATE_RET( X != NULL );
775 MPI_VALIDATE_RET( fin != NULL );
776
777 if( radix < 2 || radix > 16 )
778 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
779
Paul Bakker5121ce52009-01-03 21:22:43 +0000780 memset( s, 0, sizeof( s ) );
781 if( fgets( s, sizeof( s ) - 1, fin ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000783
784 slen = strlen( s );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000785 if( slen == sizeof( s ) - 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000787
Hanno Beckerb2034b72017-04-26 11:46:46 +0100788 if( slen > 0 && s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
789 if( slen > 0 && s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
Paul Bakker5121ce52009-01-03 21:22:43 +0000790
791 p = s + slen;
Hanno Beckerb2034b72017-04-26 11:46:46 +0100792 while( p-- > s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000793 if( mpi_get_digit( &d, radix, *p ) != 0 )
794 break;
795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 return( mbedtls_mpi_read_string( X, radix, p + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000797}
798
799/*
800 * Write X into an opened file (or stdout if fout == NULL)
801 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout )
Paul Bakker5121ce52009-01-03 21:22:43 +0000803{
Janos Follath24eed8d2019-11-22 13:21:35 +0000804 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000805 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000806 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000807 * Buffer should have space for (short) label and decimal formatted MPI,
808 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Hanno Becker73d7d792018-12-11 10:35:51 +0000811 MPI_VALIDATE_RET( X != NULL );
812
813 if( radix < 2 || radix > 16 )
814 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000815
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100816 memset( s, 0, sizeof( s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000817
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100818 MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, sizeof( s ) - 2, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000819
820 if( p == NULL ) p = "";
821
822 plen = strlen( p );
823 slen = strlen( s );
824 s[slen++] = '\r';
825 s[slen++] = '\n';
826
827 if( fout != NULL )
828 {
829 if( fwrite( p, 1, plen, fout ) != plen ||
830 fwrite( s, 1, slen, fout ) != slen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000832 }
833 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 mbedtls_printf( "%s%s", p, s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000835
836cleanup:
837
838 return( ret );
839}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000841
Hanno Beckerda1655a2017-10-18 14:21:44 +0100842
843/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
844 * into the storage form used by mbedtls_mpi. */
Hanno Beckerf8720072018-11-08 11:53:49 +0000845
846static mbedtls_mpi_uint mpi_uint_bigendian_to_host_c( mbedtls_mpi_uint x )
847{
848 uint8_t i;
Hanno Becker031d6332019-05-01 17:09:11 +0100849 unsigned char *x_ptr;
Hanno Beckerf8720072018-11-08 11:53:49 +0000850 mbedtls_mpi_uint tmp = 0;
Hanno Becker031d6332019-05-01 17:09:11 +0100851
852 for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
853 {
854 tmp <<= CHAR_BIT;
855 tmp |= (mbedtls_mpi_uint) *x_ptr;
856 }
857
Hanno Beckerf8720072018-11-08 11:53:49 +0000858 return( tmp );
859}
860
861static mbedtls_mpi_uint mpi_uint_bigendian_to_host( mbedtls_mpi_uint x )
862{
863#if defined(__BYTE_ORDER__)
864
865/* Nothing to do on bigendian systems. */
866#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
867 return( x );
868#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
869
870#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
871
872/* For GCC and Clang, have builtins for byte swapping. */
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000873#if defined(__GNUC__) && defined(__GNUC_PREREQ)
874#if __GNUC_PREREQ(4,3)
Hanno Beckerf8720072018-11-08 11:53:49 +0000875#define have_bswap
876#endif
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000877#endif
878
879#if defined(__clang__) && defined(__has_builtin)
880#if __has_builtin(__builtin_bswap32) && \
881 __has_builtin(__builtin_bswap64)
882#define have_bswap
883#endif
884#endif
885
Hanno Beckerf8720072018-11-08 11:53:49 +0000886#if defined(have_bswap)
887 /* The compiler is hopefully able to statically evaluate this! */
888 switch( sizeof(mbedtls_mpi_uint) )
889 {
890 case 4:
891 return( __builtin_bswap32(x) );
892 case 8:
893 return( __builtin_bswap64(x) );
894 }
895#endif
896#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
897#endif /* __BYTE_ORDER__ */
898
899 /* Fall back to C-based reordering if we don't know the byte order
900 * or we couldn't use a compiler-specific builtin. */
901 return( mpi_uint_bigendian_to_host_c( x ) );
902}
903
Hanno Becker2be8a552018-10-25 12:40:09 +0100904static void mpi_bigendian_to_host( mbedtls_mpi_uint * const p, size_t limbs )
Hanno Beckerda1655a2017-10-18 14:21:44 +0100905{
Hanno Beckerda1655a2017-10-18 14:21:44 +0100906 mbedtls_mpi_uint *cur_limb_left;
907 mbedtls_mpi_uint *cur_limb_right;
Hanno Becker2be8a552018-10-25 12:40:09 +0100908 if( limbs == 0 )
909 return;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100910
911 /*
912 * Traverse limbs and
913 * - adapt byte-order in each limb
914 * - swap the limbs themselves.
915 * For that, simultaneously traverse the limbs from left to right
916 * and from right to left, as long as the left index is not bigger
917 * than the right index (it's not a problem if limbs is odd and the
918 * indices coincide in the last iteration).
919 */
Hanno Beckerda1655a2017-10-18 14:21:44 +0100920 for( cur_limb_left = p, cur_limb_right = p + ( limbs - 1 );
921 cur_limb_left <= cur_limb_right;
922 cur_limb_left++, cur_limb_right-- )
923 {
Hanno Beckerf8720072018-11-08 11:53:49 +0000924 mbedtls_mpi_uint tmp;
925 /* Note that if cur_limb_left == cur_limb_right,
926 * this code effectively swaps the bytes only once. */
927 tmp = mpi_uint_bigendian_to_host( *cur_limb_left );
928 *cur_limb_left = mpi_uint_bigendian_to_host( *cur_limb_right );
929 *cur_limb_right = tmp;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100930 }
Hanno Beckerda1655a2017-10-18 14:21:44 +0100931}
932
Paul Bakker5121ce52009-01-03 21:22:43 +0000933/*
Janos Follatha778a942019-02-13 10:28:28 +0000934 * Import X from unsigned binary data, little endian
935 */
936int mbedtls_mpi_read_binary_le( mbedtls_mpi *X,
937 const unsigned char *buf, size_t buflen )
938{
Janos Follath24eed8d2019-11-22 13:21:35 +0000939 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follatha778a942019-02-13 10:28:28 +0000940 size_t i;
941 size_t const limbs = CHARS_TO_LIMBS( buflen );
942
943 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine3130ce22021-06-02 22:17:52 +0200944 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Janos Follatha778a942019-02-13 10:28:28 +0000945
946 for( i = 0; i < buflen; i++ )
947 X->p[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
948
949cleanup:
950
Janos Follath171a7ef2019-02-15 16:17:45 +0000951 /*
952 * This function is also used to import keys. However, wiping the buffers
953 * upon failure is not necessary because failure only can happen before any
954 * input is copied.
955 */
Janos Follatha778a942019-02-13 10:28:28 +0000956 return( ret );
957}
958
959/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000960 * Import X from unsigned binary data, big endian
961 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000963{
Janos Follath24eed8d2019-11-22 13:21:35 +0000964 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100965 size_t const limbs = CHARS_TO_LIMBS( buflen );
966 size_t const overhead = ( limbs * ciL ) - buflen;
967 unsigned char *Xp;
Paul Bakker5121ce52009-01-03 21:22:43 +0000968
Hanno Becker8ce11a32018-12-19 16:18:52 +0000969 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +0000970 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
971
Hanno Becker073c1992017-10-17 15:17:27 +0100972 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine3130ce22021-06-02 22:17:52 +0200973 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000974
Gilles Peskine3130ce22021-06-02 22:17:52 +0200975 /* Avoid calling `memcpy` with NULL source or destination argument,
Hanno Becker0e810b92019-01-03 17:13:11 +0000976 * even if buflen is 0. */
Gilles Peskine3130ce22021-06-02 22:17:52 +0200977 if( buflen != 0 )
Hanno Becker0e810b92019-01-03 17:13:11 +0000978 {
979 Xp = (unsigned char*) X->p;
980 memcpy( Xp + overhead, buf, buflen );
Hanno Beckerda1655a2017-10-18 14:21:44 +0100981
Hanno Becker0e810b92019-01-03 17:13:11 +0000982 mpi_bigendian_to_host( X->p, limbs );
983 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000984
985cleanup:
986
Janos Follath171a7ef2019-02-15 16:17:45 +0000987 /*
988 * This function is also used to import keys. However, wiping the buffers
989 * upon failure is not necessary because failure only can happen before any
990 * input is copied.
991 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000992 return( ret );
993}
994
995/*
Janos Follathe344d0f2019-02-19 16:17:40 +0000996 * Export X into unsigned binary data, little endian
997 */
998int mbedtls_mpi_write_binary_le( const mbedtls_mpi *X,
999 unsigned char *buf, size_t buflen )
1000{
1001 size_t stored_bytes = X->n * ciL;
1002 size_t bytes_to_copy;
1003 size_t i;
1004
1005 if( stored_bytes < buflen )
1006 {
1007 bytes_to_copy = stored_bytes;
1008 }
1009 else
1010 {
1011 bytes_to_copy = buflen;
1012
1013 /* The output buffer is smaller than the allocated size of X.
1014 * However X may fit if its leading bytes are zero. */
1015 for( i = bytes_to_copy; i < stored_bytes; i++ )
1016 {
1017 if( GET_BYTE( X, i ) != 0 )
1018 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
1019 }
1020 }
1021
1022 for( i = 0; i < bytes_to_copy; i++ )
1023 buf[i] = GET_BYTE( X, i );
1024
1025 if( stored_bytes < buflen )
1026 {
1027 /* Write trailing 0 bytes */
1028 memset( buf + stored_bytes, 0, buflen - stored_bytes );
1029 }
1030
1031 return( 0 );
1032}
1033
1034/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 * Export X into unsigned binary data, big endian
1036 */
Gilles Peskine11cdb052018-11-20 16:47:47 +01001037int mbedtls_mpi_write_binary( const mbedtls_mpi *X,
1038 unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001039{
Hanno Becker73d7d792018-12-11 10:35:51 +00001040 size_t stored_bytes;
Gilles Peskine11cdb052018-11-20 16:47:47 +01001041 size_t bytes_to_copy;
1042 unsigned char *p;
1043 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +00001044
Hanno Becker73d7d792018-12-11 10:35:51 +00001045 MPI_VALIDATE_RET( X != NULL );
1046 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
1047
1048 stored_bytes = X->n * ciL;
1049
Gilles Peskine11cdb052018-11-20 16:47:47 +01001050 if( stored_bytes < buflen )
1051 {
1052 /* There is enough space in the output buffer. Write initial
1053 * null bytes and record the position at which to start
1054 * writing the significant bytes. In this case, the execution
1055 * trace of this function does not depend on the value of the
1056 * number. */
1057 bytes_to_copy = stored_bytes;
1058 p = buf + buflen - stored_bytes;
1059 memset( buf, 0, buflen - stored_bytes );
1060 }
1061 else
1062 {
1063 /* The output buffer is smaller than the allocated size of X.
1064 * However X may fit if its leading bytes are zero. */
1065 bytes_to_copy = buflen;
1066 p = buf;
1067 for( i = bytes_to_copy; i < stored_bytes; i++ )
1068 {
1069 if( GET_BYTE( X, i ) != 0 )
1070 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
1071 }
1072 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001073
Gilles Peskine11cdb052018-11-20 16:47:47 +01001074 for( i = 0; i < bytes_to_copy; i++ )
1075 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
Paul Bakker5121ce52009-01-03 21:22:43 +00001076
1077 return( 0 );
1078}
1079
1080/*
1081 * Left-shift: X <<= count
1082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +00001084{
Janos Follath24eed8d2019-11-22 13:21:35 +00001085 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001086 size_t i, v0, t1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +00001088 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001089
1090 v0 = count / (biL );
1091 t1 = count & (biL - 1);
1092
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001093 i = mbedtls_mpi_bitlen( X ) + count;
Paul Bakker5121ce52009-01-03 21:22:43 +00001094
Paul Bakkerf9688572011-05-05 10:00:45 +00001095 if( X->n * biL < i )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001097
1098 ret = 0;
1099
1100 /*
1101 * shift by count / limb_size
1102 */
1103 if( v0 > 0 )
1104 {
Paul Bakker23986e52011-04-24 08:57:21 +00001105 for( i = X->n; i > v0; i-- )
1106 X->p[i - 1] = X->p[i - v0 - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001107
Paul Bakker23986e52011-04-24 08:57:21 +00001108 for( ; i > 0; i-- )
1109 X->p[i - 1] = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 }
1111
1112 /*
1113 * shift by count % limb_size
1114 */
1115 if( t1 > 0 )
1116 {
1117 for( i = v0; i < X->n; i++ )
1118 {
1119 r1 = X->p[i] >> (biL - t1);
1120 X->p[i] <<= t1;
1121 X->p[i] |= r0;
1122 r0 = r1;
1123 }
1124 }
1125
1126cleanup:
1127
1128 return( ret );
1129}
1130
1131/*
1132 * Right-shift: X >>= count
1133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +00001135{
Paul Bakker23986e52011-04-24 08:57:21 +00001136 size_t i, v0, v1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +00001138 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001139
1140 v0 = count / biL;
1141 v1 = count & (biL - 1);
1142
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001143 if( v0 > X->n || ( v0 == X->n && v1 > 0 ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 return mbedtls_mpi_lset( X, 0 );
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001145
Paul Bakker5121ce52009-01-03 21:22:43 +00001146 /*
1147 * shift by count / limb_size
1148 */
1149 if( v0 > 0 )
1150 {
1151 for( i = 0; i < X->n - v0; i++ )
1152 X->p[i] = X->p[i + v0];
1153
1154 for( ; i < X->n; i++ )
1155 X->p[i] = 0;
1156 }
1157
1158 /*
1159 * shift by count % limb_size
1160 */
1161 if( v1 > 0 )
1162 {
Paul Bakker23986e52011-04-24 08:57:21 +00001163 for( i = X->n; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001164 {
Paul Bakker23986e52011-04-24 08:57:21 +00001165 r1 = X->p[i - 1] << (biL - v1);
1166 X->p[i - 1] >>= v1;
1167 X->p[i - 1] |= r0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001168 r0 = r1;
1169 }
1170 }
1171
1172 return( 0 );
1173}
1174
1175/*
1176 * Compare unsigned values
1177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001179{
Paul Bakker23986e52011-04-24 08:57:21 +00001180 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001181 MPI_VALIDATE_RET( X != NULL );
1182 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001183
Paul Bakker23986e52011-04-24 08:57:21 +00001184 for( i = X->n; i > 0; i-- )
1185 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001186 break;
1187
Paul Bakker23986e52011-04-24 08:57:21 +00001188 for( j = Y->n; j > 0; j-- )
1189 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001190 break;
1191
Paul Bakker23986e52011-04-24 08:57:21 +00001192 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 return( 0 );
1194
1195 if( i > j ) return( 1 );
1196 if( j > i ) return( -1 );
1197
Paul Bakker23986e52011-04-24 08:57:21 +00001198 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001199 {
Paul Bakker23986e52011-04-24 08:57:21 +00001200 if( X->p[i - 1] > Y->p[i - 1] ) return( 1 );
1201 if( X->p[i - 1] < Y->p[i - 1] ) return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001202 }
1203
1204 return( 0 );
1205}
1206
1207/*
1208 * Compare signed values
1209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001211{
Paul Bakker23986e52011-04-24 08:57:21 +00001212 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001213 MPI_VALIDATE_RET( X != NULL );
1214 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001215
Paul Bakker23986e52011-04-24 08:57:21 +00001216 for( i = X->n; i > 0; i-- )
1217 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001218 break;
1219
Paul Bakker23986e52011-04-24 08:57:21 +00001220 for( j = Y->n; j > 0; j-- )
1221 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001222 break;
1223
Paul Bakker23986e52011-04-24 08:57:21 +00001224 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001225 return( 0 );
1226
1227 if( i > j ) return( X->s );
Paul Bakker0c8f73b2012-03-22 14:08:57 +00001228 if( j > i ) return( -Y->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001229
1230 if( X->s > 0 && Y->s < 0 ) return( 1 );
1231 if( Y->s > 0 && X->s < 0 ) return( -1 );
1232
Paul Bakker23986e52011-04-24 08:57:21 +00001233 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001234 {
Paul Bakker23986e52011-04-24 08:57:21 +00001235 if( X->p[i - 1] > Y->p[i - 1] ) return( X->s );
1236 if( X->p[i - 1] < Y->p[i - 1] ) return( -X->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 }
1238
1239 return( 0 );
1240}
1241
Janos Follath3f6f0e42019-10-14 09:09:32 +01001242/** Decide if an integer is less than the other, without branches.
1243 *
1244 * \param x First integer.
1245 * \param y Second integer.
1246 *
1247 * \return 1 if \p x is less than \p y, 0 otherwise
1248 */
Janos Follath0e5532d2019-10-11 14:21:53 +01001249static unsigned ct_lt_mpi_uint( const mbedtls_mpi_uint x,
1250 const mbedtls_mpi_uint y )
Janos Follathee6abce2019-09-05 14:47:19 +01001251{
1252 mbedtls_mpi_uint ret;
1253 mbedtls_mpi_uint cond;
1254
1255 /*
1256 * Check if the most significant bits (MSB) of the operands are different.
1257 */
1258 cond = ( x ^ y );
1259 /*
1260 * If the MSB are the same then the difference x-y will be negative (and
1261 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
1262 */
1263 ret = ( x - y ) & ~cond;
1264 /*
1265 * If the MSB are different, then the operand with the MSB of 1 is the
1266 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
1267 * the MSB of y is 0.)
1268 */
1269 ret |= y & cond;
1270
1271
Janos Follatha0f732b2019-10-14 08:59:14 +01001272 ret = ret >> ( biL - 1 );
Janos Follathee6abce2019-09-05 14:47:19 +01001273
Janos Follath67ce6472019-10-29 15:08:46 +00001274 return (unsigned) ret;
Janos Follathee6abce2019-09-05 14:47:19 +01001275}
1276
1277/*
1278 * Compare signed values in constant time
1279 */
Janos Follath0e5532d2019-10-11 14:21:53 +01001280int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y,
1281 unsigned *ret )
Janos Follathee6abce2019-09-05 14:47:19 +01001282{
1283 size_t i;
Janos Follathbb5147f2019-10-28 12:23:18 +00001284 /* The value of any of these variables is either 0 or 1 at all times. */
Janos Follath5e614ce2019-10-28 12:31:34 +00001285 unsigned cond, done, X_is_negative, Y_is_negative;
Janos Follathee6abce2019-09-05 14:47:19 +01001286
1287 MPI_VALIDATE_RET( X != NULL );
1288 MPI_VALIDATE_RET( Y != NULL );
1289 MPI_VALIDATE_RET( ret != NULL );
1290
1291 if( X->n != Y->n )
1292 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1293
1294 /*
Janos Follath73ba9ec2019-10-28 12:12:15 +00001295 * Set sign_N to 1 if N >= 0, 0 if N < 0.
1296 * We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0.
Janos Follathee6abce2019-09-05 14:47:19 +01001297 */
Janos Follath5e614ce2019-10-28 12:31:34 +00001298 X_is_negative = ( X->s & 2 ) >> 1;
1299 Y_is_negative = ( Y->s & 2 ) >> 1;
Janos Follath0e5532d2019-10-11 14:21:53 +01001300
1301 /*
1302 * If the signs are different, then the positive operand is the bigger.
Janos Follath5e614ce2019-10-28 12:31:34 +00001303 * That is if X is negative (X_is_negative == 1), then X < Y is true and it
1304 * is false if X is positive (X_is_negative == 0).
Janos Follath0e5532d2019-10-11 14:21:53 +01001305 */
Janos Follath5e614ce2019-10-28 12:31:34 +00001306 cond = ( X_is_negative ^ Y_is_negative );
1307 *ret = cond & X_is_negative;
Janos Follath0e5532d2019-10-11 14:21:53 +01001308
1309 /*
Janos Follathbb5147f2019-10-28 12:23:18 +00001310 * This is a constant-time function. We might have the result, but we still
Janos Follath0e5532d2019-10-11 14:21:53 +01001311 * need to go through the loop. Record if we have the result already.
1312 */
Janos Follathee6abce2019-09-05 14:47:19 +01001313 done = cond;
1314
1315 for( i = X->n; i > 0; i-- )
1316 {
1317 /*
Janos Follath30702422019-11-05 12:24:52 +00001318 * If Y->p[i - 1] < X->p[i - 1] then X < Y is true if and only if both
1319 * X and Y are negative.
Janos Follath0e5532d2019-10-11 14:21:53 +01001320 *
1321 * Again even if we can make a decision, we just mark the result and
1322 * the fact that we are done and continue looping.
Janos Follathee6abce2019-09-05 14:47:19 +01001323 */
Janos Follath30702422019-11-05 12:24:52 +00001324 cond = ct_lt_mpi_uint( Y->p[i - 1], X->p[i - 1] );
1325 *ret |= cond & ( 1 - done ) & X_is_negative;
Janos Follathc50e6d52019-10-28 12:37:21 +00001326 done |= cond;
Janos Follathee6abce2019-09-05 14:47:19 +01001327
1328 /*
Janos Follath30702422019-11-05 12:24:52 +00001329 * If X->p[i - 1] < Y->p[i - 1] then X < Y is true if and only if both
1330 * X and Y are positive.
Janos Follath0e5532d2019-10-11 14:21:53 +01001331 *
1332 * Again even if we can make a decision, we just mark the result and
1333 * the fact that we are done and continue looping.
Janos Follathee6abce2019-09-05 14:47:19 +01001334 */
Janos Follath30702422019-11-05 12:24:52 +00001335 cond = ct_lt_mpi_uint( X->p[i - 1], Y->p[i - 1] );
1336 *ret |= cond & ( 1 - done ) & ( 1 - X_is_negative );
Janos Follathc50e6d52019-10-28 12:37:21 +00001337 done |= cond;
Janos Follathee6abce2019-09-05 14:47:19 +01001338 }
1339
1340 return( 0 );
1341}
1342
Paul Bakker5121ce52009-01-03 21:22:43 +00001343/*
1344 * Compare signed values
1345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +00001347{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 mbedtls_mpi Y;
1349 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001350 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001351
1352 *p = ( z < 0 ) ? -z : z;
1353 Y.s = ( z < 0 ) ? -1 : 1;
1354 Y.n = 1;
1355 Y.p = p;
1356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 return( mbedtls_mpi_cmp_mpi( X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001358}
1359
1360/*
1361 * Unsigned addition: X = |A| + |B| (HAC 14.7)
1362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001364{
Janos Follath24eed8d2019-11-22 13:21:35 +00001365 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001366 size_t i, j;
Janos Follath6c922682015-10-30 17:43:11 +01001367 mbedtls_mpi_uint *o, *p, c, tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +00001368 MPI_VALIDATE_RET( X != NULL );
1369 MPI_VALIDATE_RET( A != NULL );
1370 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001371
1372 if( X == B )
1373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 const mbedtls_mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001375 }
1376
1377 if( X != A )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
Paul Bakker9af723c2014-05-01 13:03:14 +02001379
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001380 /*
1381 * X should always be positive as a result of unsigned additions.
1382 */
1383 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001384
Paul Bakker23986e52011-04-24 08:57:21 +00001385 for( j = B->n; j > 0; j-- )
1386 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001387 break;
1388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
1391 o = B->p; p = X->p; c = 0;
1392
Janos Follath6c922682015-10-30 17:43:11 +01001393 /*
1394 * tmp is used because it might happen that p == o
1395 */
Paul Bakker23986e52011-04-24 08:57:21 +00001396 for( i = 0; i < j; i++, o++, p++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001397 {
Janos Follath6c922682015-10-30 17:43:11 +01001398 tmp= *o;
Paul Bakker5121ce52009-01-03 21:22:43 +00001399 *p += c; c = ( *p < c );
Janos Follath6c922682015-10-30 17:43:11 +01001400 *p += tmp; c += ( *p < tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 }
1402
1403 while( c != 0 )
1404 {
1405 if( i >= X->n )
1406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001408 p = X->p + i;
1409 }
1410
Paul Bakker2d319fd2012-09-16 21:34:26 +00001411 *p += c; c = ( *p < c ); i++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001412 }
1413
1414cleanup:
1415
1416 return( ret );
1417}
1418
Gilles Peskine09ec10a2020-06-09 10:39:38 +02001419/**
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001420 * Helper for mbedtls_mpi subtraction.
1421 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001422 * Calculate l - r where l and r have the same size.
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001423 * This function operates modulo (2^ciL)^n and returns the carry
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001424 * (1 if there was a wraparound, i.e. if `l < r`, and 0 otherwise).
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001425 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001426 * d may be aliased to l or r.
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001427 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001428 * \param n Number of limbs of \p d, \p l and \p r.
1429 * \param[out] d The result of the subtraction.
1430 * \param[in] l The left operand.
1431 * \param[in] r The right operand.
1432 *
1433 * \return 1 if `l < r`.
1434 * 0 if `l >= r`.
Paul Bakker5121ce52009-01-03 21:22:43 +00001435 */
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001436static mbedtls_mpi_uint mpi_sub_hlp( size_t n,
1437 mbedtls_mpi_uint *d,
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001438 const mbedtls_mpi_uint *l,
1439 const mbedtls_mpi_uint *r )
Paul Bakker5121ce52009-01-03 21:22:43 +00001440{
Paul Bakker23986e52011-04-24 08:57:21 +00001441 size_t i;
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001442 mbedtls_mpi_uint c = 0, t, z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001443
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001444 for( i = 0; i < n; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001445 {
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001446 z = ( l[i] < c ); t = l[i] - c;
1447 c = ( t < r[i] ) + z; d[i] = t - r[i];
Paul Bakker5121ce52009-01-03 21:22:43 +00001448 }
1449
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001450 return( c );
Paul Bakker5121ce52009-01-03 21:22:43 +00001451}
1452
1453/*
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001454 * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
Paul Bakker5121ce52009-01-03 21:22:43 +00001455 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001457{
Janos Follath24eed8d2019-11-22 13:21:35 +00001458 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001459 size_t n;
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001460 mbedtls_mpi_uint carry;
Hanno Becker73d7d792018-12-11 10:35:51 +00001461 MPI_VALIDATE_RET( X != NULL );
1462 MPI_VALIDATE_RET( A != NULL );
1463 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001464
Paul Bakker23986e52011-04-24 08:57:21 +00001465 for( n = B->n; n > 0; n-- )
1466 if( B->p[n - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001467 break;
Gilles Peskinec8a91772021-01-27 22:30:43 +01001468 if( n > A->n )
1469 {
1470 /* B >= (2^ciL)^n > A */
1471 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1472 goto cleanup;
1473 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001474
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001475 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, A->n ) );
1476
1477 /* Set the high limbs of X to match A. Don't touch the lower limbs
1478 * because X might be aliased to B, and we must not overwrite the
1479 * significant digits of B. */
1480 if( A->n > n )
1481 memcpy( X->p + n, A->p + n, ( A->n - n ) * ciL );
1482 if( X->n > A->n )
1483 memset( X->p + A->n, 0, ( X->n - A->n ) * ciL );
1484
1485 carry = mpi_sub_hlp( n, X->p, A->p, B->p );
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001486 if( carry != 0 )
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001487 {
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001488 /* Propagate the carry to the first nonzero limb of X. */
1489 for( ; n < X->n && X->p[n] == 0; n++ )
1490 --X->p[n];
1491 /* If we ran out of space for the carry, it means that the result
1492 * is negative. */
1493 if( n == X->n )
Gilles Peskine89b41302020-07-23 01:16:46 +02001494 {
1495 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1496 goto cleanup;
1497 }
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001498 --X->p[n];
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001499 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001500
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001501 /* X should always be positive as a result of unsigned subtractions. */
1502 X->s = 1;
1503
Paul Bakker5121ce52009-01-03 21:22:43 +00001504cleanup:
Paul Bakker5121ce52009-01-03 21:22:43 +00001505 return( ret );
1506}
1507
1508/*
1509 * Signed addition: X = A + B
1510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001512{
Hanno Becker73d7d792018-12-11 10:35:51 +00001513 int ret, s;
1514 MPI_VALIDATE_RET( X != NULL );
1515 MPI_VALIDATE_RET( A != NULL );
1516 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001517
Hanno Becker73d7d792018-12-11 10:35:51 +00001518 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001519 if( A->s * B->s < 0 )
1520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001524 X->s = s;
1525 }
1526 else
1527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001529 X->s = -s;
1530 }
1531 }
1532 else
1533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001535 X->s = s;
1536 }
1537
1538cleanup:
1539
1540 return( ret );
1541}
1542
1543/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001544 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001547{
Hanno Becker73d7d792018-12-11 10:35:51 +00001548 int ret, s;
1549 MPI_VALIDATE_RET( X != NULL );
1550 MPI_VALIDATE_RET( A != NULL );
1551 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001552
Hanno Becker73d7d792018-12-11 10:35:51 +00001553 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001554 if( A->s * B->s > 0 )
1555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001559 X->s = s;
1560 }
1561 else
1562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001564 X->s = -s;
1565 }
1566 }
1567 else
1568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001570 X->s = s;
1571 }
1572
1573cleanup:
1574
1575 return( ret );
1576}
1577
1578/*
1579 * Signed addition: X = A + b
1580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001582{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 mbedtls_mpi _B;
1584 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001585 MPI_VALIDATE_RET( X != NULL );
1586 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001587
1588 p[0] = ( b < 0 ) ? -b : b;
1589 _B.s = ( b < 0 ) ? -1 : 1;
1590 _B.n = 1;
1591 _B.p = p;
1592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 return( mbedtls_mpi_add_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001594}
1595
1596/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001597 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001600{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 mbedtls_mpi _B;
1602 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001603 MPI_VALIDATE_RET( X != NULL );
1604 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001605
1606 p[0] = ( b < 0 ) ? -b : b;
1607 _B.s = ( b < 0 ) ? -1 : 1;
1608 _B.n = 1;
1609 _B.p = p;
1610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 return( mbedtls_mpi_sub_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001612}
1613
Gilles Peskinea5d8d892020-07-23 21:27:15 +02001614/** Helper for mbedtls_mpi multiplication.
1615 *
1616 * Add \p b * \p s to \p d.
1617 *
1618 * \param i The number of limbs of \p s.
1619 * \param[in] s A bignum to multiply, of size \p i.
1620 * It may overlap with \p d, but only if
1621 * \p d <= \p s.
1622 * Its leading limb must not be \c 0.
1623 * \param[in,out] d The bignum to add to.
1624 * It must be sufficiently large to store the
1625 * result of the multiplication. This means
1626 * \p i + 1 limbs if \p d[\p i - 1] started as 0 and \p b
1627 * is not known a priori.
1628 * \param b A scalar to multiply.
Paul Bakkerfc4f46f2013-06-24 19:23:56 +02001629 */
1630static
1631#if defined(__APPLE__) && defined(__arm__)
1632/*
1633 * Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
1634 * appears to need this to prevent bad ARM code generation at -O3.
1635 */
1636__attribute__ ((noinline))
1637#endif
Gilles Peskinea5d8d892020-07-23 21:27:15 +02001638void mpi_mul_hlp( size_t i,
1639 const mbedtls_mpi_uint *s,
1640 mbedtls_mpi_uint *d,
1641 mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001642{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 mbedtls_mpi_uint c = 0, t = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001644
1645#if defined(MULADDC_HUIT)
1646 for( ; i >= 8; i -= 8 )
1647 {
1648 MULADDC_INIT
1649 MULADDC_HUIT
1650 MULADDC_STOP
1651 }
1652
1653 for( ; i > 0; i-- )
1654 {
1655 MULADDC_INIT
1656 MULADDC_CORE
1657 MULADDC_STOP
1658 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001659#else /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001660 for( ; i >= 16; i -= 16 )
1661 {
1662 MULADDC_INIT
1663 MULADDC_CORE MULADDC_CORE
1664 MULADDC_CORE MULADDC_CORE
1665 MULADDC_CORE MULADDC_CORE
1666 MULADDC_CORE MULADDC_CORE
1667
1668 MULADDC_CORE MULADDC_CORE
1669 MULADDC_CORE MULADDC_CORE
1670 MULADDC_CORE MULADDC_CORE
1671 MULADDC_CORE MULADDC_CORE
1672 MULADDC_STOP
1673 }
1674
1675 for( ; i >= 8; i -= 8 )
1676 {
1677 MULADDC_INIT
1678 MULADDC_CORE MULADDC_CORE
1679 MULADDC_CORE MULADDC_CORE
1680
1681 MULADDC_CORE MULADDC_CORE
1682 MULADDC_CORE MULADDC_CORE
1683 MULADDC_STOP
1684 }
1685
1686 for( ; i > 0; i-- )
1687 {
1688 MULADDC_INIT
1689 MULADDC_CORE
1690 MULADDC_STOP
1691 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001692#endif /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001693
1694 t++;
1695
Gilles Peskine8e464c42020-07-24 00:08:38 +02001696 while( c != 0 )
1697 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001698 *d += c; c = ( *d < c ); d++;
1699 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001700}
1701
1702/*
1703 * Baseline multiplication: X = A * B (HAC 14.12)
1704 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001706{
Janos Follath24eed8d2019-11-22 13:21:35 +00001707 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001708 size_t i, j;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 mbedtls_mpi TA, TB;
Hanno Becker73d7d792018-12-11 10:35:51 +00001710 MPI_VALIDATE_RET( X != NULL );
1711 MPI_VALIDATE_RET( A != NULL );
1712 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 if( X == A ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); A = &TA; }
1717 if( X == B ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); B = &TB; }
Paul Bakker5121ce52009-01-03 21:22:43 +00001718
Paul Bakker23986e52011-04-24 08:57:21 +00001719 for( i = A->n; i > 0; i-- )
1720 if( A->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001721 break;
1722
Paul Bakker23986e52011-04-24 08:57:21 +00001723 for( j = B->n; j > 0; j-- )
1724 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001725 break;
1726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + j ) );
1728 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001729
Alexey Skalozub8e75e682016-01-13 21:59:27 +02001730 for( ; j > 0; j-- )
1731 mpi_mul_hlp( i, A->p, X->p + j - 1, B->p[j - 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00001732
1733 X->s = A->s * B->s;
1734
1735cleanup:
1736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001738
1739 return( ret );
1740}
1741
1742/*
1743 * Baseline multiplication: X = A * b
1744 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001746{
Hanno Becker73d7d792018-12-11 10:35:51 +00001747 MPI_VALIDATE_RET( X != NULL );
1748 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001749
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001750 /* mpi_mul_hlp can't deal with a leading 0. */
1751 size_t n = A->n;
1752 while( n > 0 && A->p[n - 1] == 0 )
1753 --n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001754
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001755 /* The general method below doesn't work if n==0 or b==0. By chance
1756 * calculating the result is trivial in those cases. */
1757 if( b == 0 || n == 0 )
1758 {
Paul Elliott986b55a2021-04-20 21:46:29 +01001759 return( mbedtls_mpi_lset( X, 0 ) );
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001760 }
1761
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001762 /* Calculate A*b as A + A*(b-1) to take advantage of mpi_mul_hlp */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001763 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001764 /* In general, A * b requires 1 limb more than b. If
1765 * A->p[n - 1] * b / b == A->p[n - 1], then A * b fits in the same
1766 * number of limbs as A and the call to grow() is not required since
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001767 * copy() will take care of the growth if needed. However, experimentally,
1768 * making the call to grow() unconditional causes slightly fewer
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001769 * calls to calloc() in ECP code, presumably because it reuses the
1770 * same mpi for a while and this way the mpi is more likely to directly
1771 * grow to its final size. */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001772 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n + 1 ) );
1773 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
1774 mpi_mul_hlp( n, A->p, X->p, b - 1 );
1775
1776cleanup:
1777 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001778}
1779
1780/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001781 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1782 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001783 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001784static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1,
1785 mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r )
Simon Butcher15b15d12015-11-26 19:35:03 +00001786{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001787#if defined(MBEDTLS_HAVE_UDBL)
1788 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001789#else
Simon Butcher9803d072016-01-03 00:24:34 +00001790 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
1791 const mbedtls_mpi_uint uint_halfword_mask = ( (mbedtls_mpi_uint) 1 << biH ) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001792 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1793 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001794 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001795#endif
1796
Simon Butcher15b15d12015-11-26 19:35:03 +00001797 /*
1798 * Check for overflow
1799 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001800 if( 0 == d || u1 >= d )
Simon Butcher15b15d12015-11-26 19:35:03 +00001801 {
Simon Butcherf5ba0452015-12-27 23:01:55 +00001802 if (r != NULL) *r = ~0;
Simon Butcher15b15d12015-11-26 19:35:03 +00001803
Simon Butcherf5ba0452015-12-27 23:01:55 +00001804 return ( ~0 );
Simon Butcher15b15d12015-11-26 19:35:03 +00001805 }
1806
1807#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001808 dividend = (mbedtls_t_udbl) u1 << biL;
1809 dividend |= (mbedtls_t_udbl) u0;
1810 quotient = dividend / d;
1811 if( quotient > ( (mbedtls_t_udbl) 1 << biL ) - 1 )
1812 quotient = ( (mbedtls_t_udbl) 1 << biL ) - 1;
1813
1814 if( r != NULL )
Simon Butcher9803d072016-01-03 00:24:34 +00001815 *r = (mbedtls_mpi_uint)( dividend - (quotient * d ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001816
1817 return (mbedtls_mpi_uint) quotient;
1818#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001819
1820 /*
1821 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1822 * Vol. 2 - Seminumerical Algorithms, Knuth
1823 */
1824
1825 /*
1826 * Normalize the divisor, d, and dividend, u0, u1
1827 */
1828 s = mbedtls_clz( d );
1829 d = d << s;
1830
1831 u1 = u1 << s;
Simon Butcher9803d072016-01-03 00:24:34 +00001832 u1 |= ( u0 >> ( biL - s ) ) & ( -(mbedtls_mpi_sint)s >> ( biL - 1 ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001833 u0 = u0 << s;
1834
1835 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001836 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001837
1838 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001839 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001840
1841 /*
1842 * Find the first quotient and remainder
1843 */
1844 q1 = u1 / d1;
1845 r0 = u1 - d1 * q1;
1846
1847 while( q1 >= radix || ( q1 * d0 > radix * r0 + u0_msw ) )
1848 {
1849 q1 -= 1;
1850 r0 += d1;
1851
1852 if ( r0 >= radix ) break;
1853 }
1854
Simon Butcherf5ba0452015-12-27 23:01:55 +00001855 rAX = ( u1 * radix ) + ( u0_msw - q1 * d );
Simon Butcher15b15d12015-11-26 19:35:03 +00001856 q0 = rAX / d1;
1857 r0 = rAX - q0 * d1;
1858
1859 while( q0 >= radix || ( q0 * d0 > radix * r0 + u0_lsw ) )
1860 {
1861 q0 -= 1;
1862 r0 += d1;
1863
1864 if ( r0 >= radix ) break;
1865 }
1866
1867 if (r != NULL)
Simon Butcherf5ba0452015-12-27 23:01:55 +00001868 *r = ( rAX * radix + u0_lsw - q0 * d ) >> s;
Simon Butcher15b15d12015-11-26 19:35:03 +00001869
1870 quotient = q1 * radix + q0;
1871
1872 return quotient;
1873#endif
1874}
1875
1876/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001878 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001879int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1880 const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001881{
Janos Follath24eed8d2019-11-22 13:21:35 +00001882 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001883 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 mbedtls_mpi X, Y, Z, T1, T2;
Alexander Kd19a1932019-11-01 18:20:42 +03001885 mbedtls_mpi_uint TP2[3];
Hanno Becker73d7d792018-12-11 10:35:51 +00001886 MPI_VALIDATE_RET( A != NULL );
1887 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889 if( mbedtls_mpi_cmp_int( B, 0 ) == 0 )
1890 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Alexander K35d6d462019-10-31 14:46:45 +03001893 mbedtls_mpi_init( &T1 );
Alexander Kd19a1932019-11-01 18:20:42 +03001894 /*
1895 * Avoid dynamic memory allocations for constant-size T2.
1896 *
1897 * T2 is used for comparison only and the 3 limbs are assigned explicitly,
1898 * so nobody increase the size of the MPI and we're safe to use an on-stack
1899 * buffer.
1900 */
Alexander K35d6d462019-10-31 14:46:45 +03001901 T2.s = 1;
Alexander Kd19a1932019-11-01 18:20:42 +03001902 T2.n = sizeof( TP2 ) / sizeof( *TP2 );
1903 T2.p = TP2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 if( Q != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_lset( Q, 0 ) );
1908 if( R != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001909 return( 0 );
1910 }
1911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &X, A ) );
1913 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001914 X.s = Y.s = 1;
1915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &Z, A->n + 2 ) );
1917 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Z, 0 ) );
Gilles Peskine2536aa72020-07-24 00:12:59 +02001918 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, A->n + 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001919
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001920 k = mbedtls_mpi_bitlen( &Y ) % biL;
Paul Bakkerf9688572011-05-05 10:00:45 +00001921 if( k < biL - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001922 {
1923 k = biL - 1 - k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &X, k ) );
1925 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, k ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001926 }
1927 else k = 0;
1928
1929 n = X.n - 1;
1930 t = Y.n - 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933 while( mbedtls_mpi_cmp_mpi( &X, &Y ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001934 {
1935 Z.p[n - t]++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001937 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001939
1940 for( i = n; i > t ; i-- )
1941 {
1942 if( X.p[i] >= Y.p[t] )
1943 Z.p[i - t - 1] = ~0;
1944 else
1945 {
Simon Butcher15b15d12015-11-26 19:35:03 +00001946 Z.p[i - t - 1] = mbedtls_int_div_int( X.p[i], X.p[i - 1],
1947 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001948 }
1949
Alexander K35d6d462019-10-31 14:46:45 +03001950 T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2];
1951 T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1];
1952 T2.p[2] = X.p[i];
1953
Paul Bakker5121ce52009-01-03 21:22:43 +00001954 Z.p[i - t - 1]++;
1955 do
1956 {
1957 Z.p[i - t - 1]--;
1958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T1, 0 ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001960 T1.p[0] = ( t < 1 ) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001961 T1.p[1] = Y.p[t];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001963 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964 while( mbedtls_mpi_cmp_mpi( &T1, &T2 ) > 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
1967 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1968 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970 if( mbedtls_mpi_cmp_int( &X, 0 ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &Y ) );
1973 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1974 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001975 Z.p[i - t - 1]--;
1976 }
1977 }
1978
1979 if( Q != NULL )
1980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( Q, &Z ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001982 Q->s = A->s * B->s;
1983 }
1984
1985 if( R != NULL )
1986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &X, k ) );
Paul Bakkerf02c5642012-11-13 10:25:21 +00001988 X.s = A->s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, &X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 if( mbedtls_mpi_cmp_int( R, 0 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001992 R->s = 1;
1993 }
1994
1995cleanup:
1996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Alexander K35d6d462019-10-31 14:46:45 +03001998 mbedtls_mpi_free( &T1 );
Alexander Kd19a1932019-11-01 18:20:42 +03001999 mbedtls_platform_zeroize( TP2, sizeof( TP2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002000
2001 return( ret );
2002}
2003
2004/*
2005 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00002006 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002007int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R,
2008 const mbedtls_mpi *A,
2009 mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00002010{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 mbedtls_mpi _B;
2012 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00002013 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002014
2015 p[0] = ( b < 0 ) ? -b : b;
2016 _B.s = ( b < 0 ) ? -1 : 1;
2017 _B.n = 1;
2018 _B.p = p;
2019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 return( mbedtls_mpi_div_mpi( Q, R, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002021}
2022
2023/*
2024 * Modulo: R = A mod B
2025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00002027{
Janos Follath24eed8d2019-11-22 13:21:35 +00002028 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker73d7d792018-12-11 10:35:51 +00002029 MPI_VALIDATE_RET( R != NULL );
2030 MPI_VALIDATE_RET( A != NULL );
2031 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033 if( mbedtls_mpi_cmp_int( B, 0 ) < 0 )
2034 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakkerce40a6d2009-06-23 19:46:08 +00002035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( NULL, R, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038 while( mbedtls_mpi_cmp_int( R, 0 ) < 0 )
2039 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 while( mbedtls_mpi_cmp_mpi( R, B ) >= 0 )
2042 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002043
2044cleanup:
2045
2046 return( ret );
2047}
2048
2049/*
2050 * Modulo: r = A mod b
2051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00002053{
Paul Bakker23986e52011-04-24 08:57:21 +00002054 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055 mbedtls_mpi_uint x, y, z;
Hanno Becker73d7d792018-12-11 10:35:51 +00002056 MPI_VALIDATE_RET( r != NULL );
2057 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002058
2059 if( b == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00002061
2062 if( b < 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002064
2065 /*
2066 * handle trivial cases
2067 */
2068 if( b == 1 )
2069 {
2070 *r = 0;
2071 return( 0 );
2072 }
2073
2074 if( b == 2 )
2075 {
2076 *r = A->p[0] & 1;
2077 return( 0 );
2078 }
2079
2080 /*
2081 * general case
2082 */
Paul Bakker23986e52011-04-24 08:57:21 +00002083 for( i = A->n, y = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00002084 {
Paul Bakker23986e52011-04-24 08:57:21 +00002085 x = A->p[i - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 y = ( y << biH ) | ( x >> biH );
2087 z = y / b;
2088 y -= z * b;
2089
2090 x <<= biH;
2091 y = ( y << biH ) | ( x >> biH );
2092 z = y / b;
2093 y -= z * b;
2094 }
2095
Paul Bakkerce40a6d2009-06-23 19:46:08 +00002096 /*
2097 * If A is negative, then the current y represents a negative value.
2098 * Flipping it to the positive side.
2099 */
2100 if( A->s < 0 && y != 0 )
2101 y = b - y;
2102
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 *r = y;
2104
2105 return( 0 );
2106}
2107
2108/*
2109 * Fast Montgomery initialization (thanks to Tom St Denis)
2110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111static void mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002112{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 mbedtls_mpi_uint x, m0 = N->p[0];
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01002114 unsigned int i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002115
2116 x = m0;
2117 x += ( ( m0 + 2 ) & 4 ) << 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002118
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01002119 for( i = biL; i >= 8; i /= 2 )
2120 x *= ( 2 - ( m0 * x ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002121
2122 *mm = ~x + 1;
2123}
2124
Gilles Peskine2a82f722020-06-04 15:00:49 +02002125/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
2126 *
2127 * \param[in,out] A One of the numbers to multiply.
Gilles Peskine221626f2020-06-08 22:37:50 +02002128 * It must have at least as many limbs as N
2129 * (A->n >= N->n), and any limbs beyond n are ignored.
Gilles Peskine2a82f722020-06-04 15:00:49 +02002130 * On successful completion, A contains the result of
2131 * the multiplication A * B * R^-1 mod N where
2132 * R = (2^ciL)^n.
2133 * \param[in] B One of the numbers to multiply.
2134 * It must be nonzero and must not have more limbs than N
2135 * (B->n <= N->n).
2136 * \param[in] N The modulo. N must be odd.
2137 * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
2138 * This is -N^-1 mod 2^ciL.
2139 * \param[in,out] T A bignum for temporary storage.
2140 * It must be at least twice the limb size of N plus 2
2141 * (T->n >= 2 * (N->n + 1)).
2142 * Its initial content is unused and
2143 * its final content is indeterminate.
2144 * Note that unlike the usual convention in the library
2145 * for `const mbedtls_mpi*`, the content of T can change.
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002147static void mpi_montmul( mbedtls_mpi *A, const mbedtls_mpi *B, const mbedtls_mpi *N, mbedtls_mpi_uint mm,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00002149{
Paul Bakker23986e52011-04-24 08:57:21 +00002150 size_t i, n, m;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 mbedtls_mpi_uint u0, u1, *d;
Paul Bakker5121ce52009-01-03 21:22:43 +00002152
2153 memset( T->p, 0, T->n * ciL );
2154
2155 d = T->p;
2156 n = N->n;
2157 m = ( B->n < n ) ? B->n : n;
2158
2159 for( i = 0; i < n; i++ )
2160 {
2161 /*
2162 * T = (T + u0*B + u1*N) / 2^biL
2163 */
2164 u0 = A->p[i];
2165 u1 = ( d[0] + u0 * B->p[0] ) * mm;
2166
2167 mpi_mul_hlp( m, B->p, d, u0 );
2168 mpi_mul_hlp( n, N->p, d, u1 );
2169
2170 *d++ = u0; d[n + 1] = 0;
2171 }
2172
Gilles Peskine221626f2020-06-08 22:37:50 +02002173 /* At this point, d is either the desired result or the desired result
2174 * plus N. We now potentially subtract N, avoiding leaking whether the
2175 * subtraction is performed through side channels. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002176
Gilles Peskine221626f2020-06-08 22:37:50 +02002177 /* Copy the n least significant limbs of d to A, so that
2178 * A = d if d < N (recall that N has n limbs). */
2179 memcpy( A->p, d, n * ciL );
Gilles Peskine09ec10a2020-06-09 10:39:38 +02002180 /* If d >= N then we want to set A to d - N. To prevent timing attacks,
Gilles Peskine221626f2020-06-08 22:37:50 +02002181 * do the calculation without using conditional tests. */
2182 /* Set d to d0 + (2^biL)^n - N where d0 is the current value of d. */
Gilles Peskine132c0972020-06-04 21:05:24 +02002183 d[n] += 1;
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02002184 d[n] -= mpi_sub_hlp( n, d, d, N->p );
Gilles Peskine221626f2020-06-08 22:37:50 +02002185 /* If d0 < N then d < (2^biL)^n
2186 * so d[n] == 0 and we want to keep A as it is.
2187 * If d0 >= N then d >= (2^biL)^n, and d <= (2^biL)^n + N < 2 * (2^biL)^n
2188 * so d[n] == 1 and we want to set A to the result of the subtraction
2189 * which is d - (2^biL)^n, i.e. the n least significant limbs of d.
2190 * This exactly corresponds to a conditional assignment. */
2191 mpi_safe_cond_assign( n, A->p, d, (unsigned char) d[n] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002192}
2193
2194/*
2195 * Montgomery reduction: A = A * R^-1 mod N
Gilles Peskine2a82f722020-06-04 15:00:49 +02002196 *
2197 * See mpi_montmul() regarding constraints and guarantees on the parameters.
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002199static void mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N,
2200 mbedtls_mpi_uint mm, const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00002201{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 mbedtls_mpi_uint z = 1;
2203 mbedtls_mpi U;
Paul Bakker5121ce52009-01-03 21:22:43 +00002204
Paul Bakker8ddb6452013-02-27 14:56:33 +01002205 U.n = U.s = (int) z;
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 U.p = &z;
2207
Gilles Peskine4e91d472020-06-04 20:55:15 +02002208 mpi_montmul( A, &U, N, mm, T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002209}
2210
Manuel Pégourié-Gonnardeaafa492021-06-03 10:42:46 +02002211/*
2212 * Constant-flow boolean "equal" comparison:
2213 * return x == y
2214 *
2215 * This function can be used to write constant-time code by replacing branches
2216 * with bit operations - it can be used in conjunction with
2217 * mbedtls_ssl_cf_mask_from_bit().
2218 *
2219 * This function is implemented without using comparison operators, as those
2220 * might be translated to branches by some compilers on some platforms.
2221 */
2222static size_t mbedtls_mpi_cf_bool_eq( size_t x, size_t y )
2223{
2224 /* diff = 0 if x == y, non-zero otherwise */
2225 const size_t diff = x ^ y;
2226
2227 /* MSVC has a warning about unary minus on unsigned integer types,
2228 * but this is well-defined and precisely what we want to do here. */
2229#if defined(_MSC_VER)
2230#pragma warning( push )
2231#pragma warning( disable : 4146 )
2232#endif
2233
2234 /* diff_msb's most significant bit is equal to x != y */
2235 const size_t diff_msb = ( diff | -diff );
2236
2237#if defined(_MSC_VER)
2238#pragma warning( pop )
2239#endif
2240
2241 /* diff1 = (x != y) ? 1 : 0 */
2242 const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 );
2243
2244 return( 1 ^ diff1 );
2245}
2246
Manuel Pégourié-Gonnarde10e8db2021-03-09 11:22:20 +01002247/**
2248 * Select an MPI from a table without leaking the index.
2249 *
2250 * This is functionally equivalent to mbedtls_mpi_copy(R, T[idx]) except it
2251 * reads the entire table in order to avoid leaking the value of idx to an
2252 * attacker able to observe memory access patterns.
2253 *
2254 * \param[out] R Where to write the selected MPI.
2255 * \param[in] T The table to read from.
2256 * \param[in] T_size The number of elements in the table.
2257 * \param[in] idx The index of the element to select;
2258 * this must satisfy 0 <= idx < T_size.
2259 *
2260 * \return \c 0 on success, or a negative error code.
2261 */
2262static int mpi_select( mbedtls_mpi *R, const mbedtls_mpi *T, size_t T_size, size_t idx )
2263{
2264 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2265
2266 for( size_t i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnardeaafa492021-06-03 10:42:46 +02002267 {
2268 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( R, &T[i],
2269 mbedtls_mpi_cf_bool_eq( i, idx ) ) );
2270 }
Manuel Pégourié-Gonnarde10e8db2021-03-09 11:22:20 +01002271
2272cleanup:
2273 return( ret );
2274}
2275
Paul Bakker5121ce52009-01-03 21:22:43 +00002276/*
2277 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
2278 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002279int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
2280 const mbedtls_mpi *E, const mbedtls_mpi *N,
2281 mbedtls_mpi *_RR )
Paul Bakker5121ce52009-01-03 21:22:43 +00002282{
Janos Follath24eed8d2019-11-22 13:21:35 +00002283 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002284 size_t wbits, wsize, one = 1;
2285 size_t i, j, nblimbs;
2286 size_t bufsize, nbits;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 mbedtls_mpi_uint ei, mm, state;
Manuel Pégourié-Gonnarde10e8db2021-03-09 11:22:20 +01002288 mbedtls_mpi RR, T, W[ 1 << MBEDTLS_MPI_WINDOW_SIZE ], WW, Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00002289 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002290
Hanno Becker73d7d792018-12-11 10:35:51 +00002291 MPI_VALIDATE_RET( X != NULL );
2292 MPI_VALIDATE_RET( A != NULL );
2293 MPI_VALIDATE_RET( E != NULL );
2294 MPI_VALIDATE_RET( N != NULL );
2295
Hanno Becker8d1dd1b2017-09-28 11:02:24 +01002296 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 || ( N->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 if( mbedtls_mpi_cmp_int( E, 0 ) < 0 )
2300 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002301
Chris Jones9246d042020-11-25 15:12:39 +00002302 if( mbedtls_mpi_bitlen( E ) > MBEDTLS_MPI_MAX_BITS ||
2303 mbedtls_mpi_bitlen( N ) > MBEDTLS_MPI_MAX_BITS )
2304 return ( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2305
Paul Bakkerf6198c12012-05-16 08:02:29 +00002306 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002307 * Init temps and window size
2308 */
2309 mpi_montg_init( &mm, N );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &T );
2311 mbedtls_mpi_init( &Apos );
Manuel Pégourié-Gonnarde10e8db2021-03-09 11:22:20 +01002312 mbedtls_mpi_init( &WW );
Paul Bakker5121ce52009-01-03 21:22:43 +00002313 memset( W, 0, sizeof( W ) );
2314
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002315 i = mbedtls_mpi_bitlen( E );
Paul Bakker5121ce52009-01-03 21:22:43 +00002316
2317 wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
2318 ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1;
2319
Peter Kolbuse6bcad32018-12-11 14:01:44 -06002320#if( MBEDTLS_MPI_WINDOW_SIZE < 6 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 if( wsize > MBEDTLS_MPI_WINDOW_SIZE )
2322 wsize = MBEDTLS_MPI_WINDOW_SIZE;
Peter Kolbuse6bcad32018-12-11 14:01:44 -06002323#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00002324
Paul Bakker5121ce52009-01-03 21:22:43 +00002325 j = N->n + 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
2327 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], j ) );
2328 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T, j * 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002329
2330 /*
Paul Bakker50546922012-05-19 08:40:49 +00002331 * Compensate for negative A (and correct at the end)
2332 */
2333 neg = ( A->s == -1 );
Paul Bakker50546922012-05-19 08:40:49 +00002334 if( neg )
2335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Apos, A ) );
Paul Bakker50546922012-05-19 08:40:49 +00002337 Apos.s = 1;
2338 A = &Apos;
2339 }
2340
2341 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002342 * If 1st call, pre-compute R^2 mod N
2343 */
2344 if( _RR == NULL || _RR->p == NULL )
2345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &RR, 1 ) );
2347 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &RR, N->n * 2 * biL ) );
2348 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &RR, &RR, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002349
2350 if( _RR != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 memcpy( _RR, &RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002352 }
2353 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 memcpy( &RR, _RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002355
2356 /*
2357 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
2358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 if( mbedtls_mpi_cmp_mpi( A, N ) >= 0 )
2360 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &W[1], A, N ) );
Paul Bakkerc2024f42014-01-23 20:38:35 +01002361 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[1], A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002363
Gilles Peskine4e91d472020-06-04 20:55:15 +02002364 mpi_montmul( &W[1], &RR, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002365
2366 /*
2367 * X = R^2 * R^-1 mod N = R mod N
2368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &RR ) );
Gilles Peskine4e91d472020-06-04 20:55:15 +02002370 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002371
2372 if( wsize > 1 )
2373 {
2374 /*
2375 * W[1 << (wsize - 1)] = W[1] ^ (wsize - 1)
2376 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002377 j = one << ( wsize - 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[j], N->n + 1 ) );
2380 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[j], &W[1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002381
2382 for( i = 0; i < wsize - 1; i++ )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002383 mpi_montmul( &W[j], &W[j], N, mm, &T );
Paul Bakker0d7702c2013-10-29 16:18:35 +01002384
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 /*
2386 * W[i] = W[i - 1] * W[1]
2387 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002388 for( i = j + 1; i < ( one << wsize ); i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[i], N->n + 1 ) );
2391 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[i], &W[i - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002392
Gilles Peskine4e91d472020-06-04 20:55:15 +02002393 mpi_montmul( &W[i], &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002394 }
2395 }
2396
2397 nblimbs = E->n;
2398 bufsize = 0;
2399 nbits = 0;
2400 wbits = 0;
2401 state = 0;
2402
2403 while( 1 )
2404 {
2405 if( bufsize == 0 )
2406 {
Paul Bakker0d7702c2013-10-29 16:18:35 +01002407 if( nblimbs == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 break;
2409
Paul Bakker0d7702c2013-10-29 16:18:35 +01002410 nblimbs--;
2411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 bufsize = sizeof( mbedtls_mpi_uint ) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00002413 }
2414
2415 bufsize--;
2416
2417 ei = (E->p[nblimbs] >> bufsize) & 1;
2418
2419 /*
2420 * skip leading 0s
2421 */
2422 if( ei == 0 && state == 0 )
2423 continue;
2424
2425 if( ei == 0 && state == 1 )
2426 {
2427 /*
2428 * out of window, square X
2429 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002430 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 continue;
2432 }
2433
2434 /*
2435 * add ei to current window
2436 */
2437 state = 2;
2438
2439 nbits++;
Paul Bakker66d5d072014-06-17 16:39:18 +02002440 wbits |= ( ei << ( wsize - nbits ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002441
2442 if( nbits == wsize )
2443 {
2444 /*
2445 * X = X^wsize R^-1 mod N
2446 */
2447 for( i = 0; i < wsize; i++ )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002448 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002449
2450 /*
2451 * X = X * W[wbits] R^-1 mod N
2452 */
Manuel Pégourié-Gonnarde10e8db2021-03-09 11:22:20 +01002453 MBEDTLS_MPI_CHK( mpi_select( &WW, W, 1 << wsize, wbits ) );
2454 mpi_montmul( X, &WW, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002455
2456 state--;
2457 nbits = 0;
2458 wbits = 0;
2459 }
2460 }
2461
2462 /*
2463 * process the remaining bits
2464 */
2465 for( i = 0; i < nbits; i++ )
2466 {
Gilles Peskine4e91d472020-06-04 20:55:15 +02002467 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
2469 wbits <<= 1;
2470
Paul Bakker66d5d072014-06-17 16:39:18 +02002471 if( ( wbits & ( one << wsize ) ) != 0 )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002472 mpi_montmul( X, &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002473 }
2474
2475 /*
2476 * X = A^E * R * R^-1 mod N = A^E mod N
2477 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002478 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002479
Hanno Beckera4af1c42017-04-18 09:07:45 +01002480 if( neg && E->n != 0 && ( E->p[0] & 1 ) != 0 )
Paul Bakkerf6198c12012-05-16 08:02:29 +00002481 {
2482 X->s = -1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, N, X ) );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002484 }
2485
Paul Bakker5121ce52009-01-03 21:22:43 +00002486cleanup:
2487
Paul Bakker66d5d072014-06-17 16:39:18 +02002488 for( i = ( one << ( wsize - 1 ) ); i < ( one << wsize ); i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489 mbedtls_mpi_free( &W[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 mbedtls_mpi_free( &W[1] ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &Apos );
Manuel Pégourié-Gonnarde10e8db2021-03-09 11:22:20 +01002492 mbedtls_mpi_free( &WW );
Paul Bakker6c591fa2011-05-05 11:49:20 +00002493
Paul Bakker75a28602014-03-31 12:08:17 +02002494 if( _RR == NULL || _RR->p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002496
2497 return( ret );
2498}
2499
Paul Bakker5121ce52009-01-03 21:22:43 +00002500/*
2501 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
2502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00002504{
Janos Follath24eed8d2019-11-22 13:21:35 +00002505 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002506 size_t lz, lzt;
Alexander Ke8ad49f2019-08-16 16:16:07 +03002507 mbedtls_mpi TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00002508
Hanno Becker73d7d792018-12-11 10:35:51 +00002509 MPI_VALIDATE_RET( G != NULL );
2510 MPI_VALIDATE_RET( A != NULL );
2511 MPI_VALIDATE_RET( B != NULL );
2512
Alexander Ke8ad49f2019-08-16 16:16:07 +03002513 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) );
2516 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 lz = mbedtls_mpi_lsb( &TA );
2519 lzt = mbedtls_mpi_lsb( &TB );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002520
Paul Bakker66d5d072014-06-17 16:39:18 +02002521 if( lzt < lz )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002522 lz = lzt;
2523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, lz ) );
2525 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, lz ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002526
Paul Bakker5121ce52009-01-03 21:22:43 +00002527 TA.s = TB.s = 1;
2528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529 while( mbedtls_mpi_cmp_int( &TA, 0 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, mbedtls_mpi_lsb( &TA ) ) );
2532 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, mbedtls_mpi_lsb( &TB ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534 if( mbedtls_mpi_cmp_mpi( &TA, &TB ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TA, &TA, &TB ) );
2537 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002538 }
2539 else
2540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TB, &TB, &TA ) );
2542 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002543 }
2544 }
2545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &TB, lz ) );
2547 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( G, &TB ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002548
2549cleanup:
2550
Alexander Ke8ad49f2019-08-16 16:16:07 +03002551 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002552
2553 return( ret );
2554}
2555
Gilles Peskine8f454702021-04-01 15:57:18 +02002556/* Fill X with n_bytes random bytes.
2557 * X must already have room for those bytes.
Gilles Peskine23422e42021-06-03 11:51:09 +02002558 * The ordering of the bytes returned from the RNG is suitable for
2559 * deterministic ECDSA (see RFC 6979 §3.3 and mbedtls_mpi_random()).
Gilles Peskinea16001e2021-04-13 21:55:35 +02002560 * The size and sign of X are unchanged.
Gilles Peskine8f454702021-04-01 15:57:18 +02002561 * n_bytes must not be 0.
2562 */
2563static int mpi_fill_random_internal(
2564 mbedtls_mpi *X, size_t n_bytes,
2565 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
2566{
2567 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2568 const size_t limbs = CHARS_TO_LIMBS( n_bytes );
2569 const size_t overhead = ( limbs * ciL ) - n_bytes;
2570
2571 if( X->n < limbs )
2572 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Gilles Peskine8f454702021-04-01 15:57:18 +02002573
Gilles Peskinea16001e2021-04-13 21:55:35 +02002574 memset( X->p, 0, overhead );
2575 memset( (unsigned char *) X->p + limbs * ciL, 0, ( X->n - limbs ) * ciL );
Gilles Peskine8f454702021-04-01 15:57:18 +02002576 MBEDTLS_MPI_CHK( f_rng( p_rng, (unsigned char *) X->p + overhead, n_bytes ) );
2577 mpi_bigendian_to_host( X->p, limbs );
2578
2579cleanup:
2580 return( ret );
2581}
2582
Paul Bakker33dc46b2014-04-30 16:11:39 +02002583/*
2584 * Fill X with size bytes of random.
2585 *
2586 * Use a temporary bytes representation to make sure the result is the same
Paul Bakkerc37b0ac2014-05-01 14:19:23 +02002587 * regardless of the platform endianness (useful when f_rng is actually
Paul Bakker33dc46b2014-04-30 16:11:39 +02002588 * deterministic, eg for tests).
2589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002591 int (*f_rng)(void *, unsigned char *, size_t),
2592 void *p_rng )
Paul Bakker287781a2011-03-26 13:18:49 +00002593{
Janos Follath24eed8d2019-11-22 13:21:35 +00002594 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6dab6202019-01-02 16:42:29 +00002595 size_t const limbs = CHARS_TO_LIMBS( size );
Hanno Beckerda1655a2017-10-18 14:21:44 +01002596
Hanno Becker8ce11a32018-12-19 16:18:52 +00002597 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002598 MPI_VALIDATE_RET( f_rng != NULL );
Paul Bakker33dc46b2014-04-30 16:11:39 +02002599
Hanno Beckerda1655a2017-10-18 14:21:44 +01002600 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine3130ce22021-06-02 22:17:52 +02002601 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Gilles Peskine8f454702021-04-01 15:57:18 +02002602 if( size == 0 )
2603 return( 0 );
Paul Bakker287781a2011-03-26 13:18:49 +00002604
Gilles Peskine8f454702021-04-01 15:57:18 +02002605 ret = mpi_fill_random_internal( X, size, f_rng, p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +00002606
2607cleanup:
2608 return( ret );
2609}
2610
Gilles Peskine4699fa42021-03-29 22:02:55 +02002611int mbedtls_mpi_random( mbedtls_mpi *X,
2612 mbedtls_mpi_sint min,
2613 const mbedtls_mpi *N,
2614 int (*f_rng)(void *, unsigned char *, size_t),
2615 void *p_rng )
2616{
Gilles Peskine4699fa42021-03-29 22:02:55 +02002617 int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskinee39ee8e2021-04-13 21:23:25 +02002618 int count;
Gilles Peskine74f66bb2021-04-13 21:09:10 +02002619 unsigned lt_lower = 1, lt_upper = 0;
Gilles Peskine4699fa42021-03-29 22:02:55 +02002620 size_t n_bits = mbedtls_mpi_bitlen( N );
2621 size_t n_bytes = ( n_bits + 7 ) / 8;
Gilles Peskine74f66bb2021-04-13 21:09:10 +02002622 mbedtls_mpi lower_bound;
Gilles Peskine4699fa42021-03-29 22:02:55 +02002623
Gilles Peskine9312ba52021-03-29 22:14:51 +02002624 if( min < 0 )
2625 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2626 if( mbedtls_mpi_cmp_int( N, min ) <= 0 )
2627 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2628
Gilles Peskinee39ee8e2021-04-13 21:23:25 +02002629 /*
2630 * When min == 0, each try has at worst a probability 1/2 of failing
2631 * (the msb has a probability 1/2 of being 0, and then the result will
2632 * be < N), so after 30 tries failure probability is a most 2**(-30).
2633 *
2634 * When N is just below a power of 2, as is the case when generating
Gilles Peskine3f613632021-04-15 11:45:19 +02002635 * a random scalar on most elliptic curves, 1 try is enough with
Gilles Peskinee39ee8e2021-04-13 21:23:25 +02002636 * overwhelming probability. When N is just above a power of 2,
Gilles Peskine3f613632021-04-15 11:45:19 +02002637 * as when generating a random scalar on secp224k1, each try has
Gilles Peskinee39ee8e2021-04-13 21:23:25 +02002638 * a probability of failing that is almost 1/2.
2639 *
2640 * The probabilities are almost the same if min is nonzero but negligible
2641 * compared to N. This is always the case when N is crypto-sized, but
2642 * it's convenient to support small N for testing purposes. When N
2643 * is small, use a higher repeat count, otherwise the probability of
2644 * failure is macroscopic.
2645 */
Gilles Peskine11779072021-06-02 21:18:59 +02002646 count = ( n_bytes > 4 ? 30 : 250 );
Gilles Peskinee39ee8e2021-04-13 21:23:25 +02002647
Gilles Peskine74f66bb2021-04-13 21:09:10 +02002648 mbedtls_mpi_init( &lower_bound );
2649
Gilles Peskine8f454702021-04-01 15:57:18 +02002650 /* Ensure that target MPI has exactly the same number of limbs
2651 * as the upper bound, even if the upper bound has leading zeros.
2652 * This is necessary for the mbedtls_mpi_lt_mpi_ct() check. */
Gilles Peskine3130ce22021-06-02 22:17:52 +02002653 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, N->n ) );
Gilles Peskine74f66bb2021-04-13 21:09:10 +02002654 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &lower_bound, N->n ) );
2655 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &lower_bound, min ) );
Gilles Peskine8f454702021-04-01 15:57:18 +02002656
Gilles Peskine4699fa42021-03-29 22:02:55 +02002657 /*
2658 * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA)
2659 * when f_rng is a suitably parametrized instance of HMAC_DRBG:
2660 * - use the same byte ordering;
2661 * - keep the leftmost n_bits bits of the generated octet string;
2662 * - try until result is in the desired range.
2663 * This also avoids any bias, which is especially important for ECDSA.
2664 */
2665 do
2666 {
Gilles Peskine8f454702021-04-01 15:57:18 +02002667 MBEDTLS_MPI_CHK( mpi_fill_random_internal( X, n_bytes, f_rng, p_rng ) );
Gilles Peskine4699fa42021-03-29 22:02:55 +02002668 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, 8 * n_bytes - n_bits ) );
2669
Gilles Peskinee39ee8e2021-04-13 21:23:25 +02002670 if( --count == 0 )
Gilles Peskine4699fa42021-03-29 22:02:55 +02002671 {
2672 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2673 goto cleanup;
2674 }
2675
Gilles Peskine74f66bb2021-04-13 21:09:10 +02002676 MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( X, &lower_bound, &lt_lower ) );
2677 MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( X, N, &lt_upper ) );
Gilles Peskine4699fa42021-03-29 22:02:55 +02002678 }
Gilles Peskine74f66bb2021-04-13 21:09:10 +02002679 while( lt_lower != 0 || lt_upper == 0 );
Gilles Peskine4699fa42021-03-29 22:02:55 +02002680
2681cleanup:
Gilles Peskine74f66bb2021-04-13 21:09:10 +02002682 mbedtls_mpi_free( &lower_bound );
Gilles Peskine4699fa42021-03-29 22:02:55 +02002683 return( ret );
2684}
2685
Paul Bakker5121ce52009-01-03 21:22:43 +00002686/*
2687 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002690{
Janos Follath24eed8d2019-11-22 13:21:35 +00002691 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Hanno Becker73d7d792018-12-11 10:35:51 +00002693 MPI_VALIDATE_RET( X != NULL );
2694 MPI_VALIDATE_RET( A != NULL );
2695 MPI_VALIDATE_RET( N != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002696
Hanno Becker4bcb4912017-04-18 15:49:39 +01002697 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002698 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TU ); mbedtls_mpi_init( &U1 ); mbedtls_mpi_init( &U2 );
2701 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &TB ); mbedtls_mpi_init( &TV );
2702 mbedtls_mpi_init( &V1 ); mbedtls_mpi_init( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, A, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002709 goto cleanup;
2710 }
2711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &TA, A, N ) );
2713 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TU, &TA ) );
2714 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, N ) );
2715 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TV, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U1, 1 ) );
2718 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U2, 0 ) );
2719 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V1, 0 ) );
2720 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002721
2722 do
2723 {
2724 while( ( TU.p[0] & 1 ) == 0 )
2725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TU, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002727
2728 if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
2729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &U1, &U1, &TB ) );
2731 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002732 }
2733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U1, 1 ) );
2735 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002736 }
2737
2738 while( ( TV.p[0] & 1 ) == 0 )
2739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TV, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002741
2742 if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
2743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002744 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, &TB ) );
2745 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002746 }
2747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V1, 1 ) );
2749 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002750 }
2751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752 if( mbedtls_mpi_cmp_mpi( &TU, &TV ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TU, &TU, &TV ) );
2755 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U1, &U1, &V1 ) );
2756 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &V2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002757 }
2758 else
2759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TV, &TV, &TU ) );
2761 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, &U1 ) );
2762 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &U2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002763 }
2764 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765 while( mbedtls_mpi_cmp_int( &TU, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767 while( mbedtls_mpi_cmp_int( &V1, 0 ) < 0 )
2768 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 while( mbedtls_mpi_cmp_mpi( &V1, N ) >= 0 )
2771 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &V1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002774
2775cleanup:
2776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TU ); mbedtls_mpi_free( &U1 ); mbedtls_mpi_free( &U2 );
2778 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TV );
2779 mbedtls_mpi_free( &V1 ); mbedtls_mpi_free( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002780
2781 return( ret );
2782}
2783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002785
Paul Bakker5121ce52009-01-03 21:22:43 +00002786static const int small_prime[] =
2787{
2788 3, 5, 7, 11, 13, 17, 19, 23,
2789 29, 31, 37, 41, 43, 47, 53, 59,
2790 61, 67, 71, 73, 79, 83, 89, 97,
2791 101, 103, 107, 109, 113, 127, 131, 137,
2792 139, 149, 151, 157, 163, 167, 173, 179,
2793 181, 191, 193, 197, 199, 211, 223, 227,
2794 229, 233, 239, 241, 251, 257, 263, 269,
2795 271, 277, 281, 283, 293, 307, 311, 313,
2796 317, 331, 337, 347, 349, 353, 359, 367,
2797 373, 379, 383, 389, 397, 401, 409, 419,
2798 421, 431, 433, 439, 443, 449, 457, 461,
2799 463, 467, 479, 487, 491, 499, 503, 509,
2800 521, 523, 541, 547, 557, 563, 569, 571,
2801 577, 587, 593, 599, 601, 607, 613, 617,
2802 619, 631, 641, 643, 647, 653, 659, 661,
2803 673, 677, 683, 691, 701, 709, 719, 727,
2804 733, 739, 743, 751, 757, 761, 769, 773,
2805 787, 797, 809, 811, 821, 823, 827, 829,
2806 839, 853, 857, 859, 863, 877, 881, 883,
2807 887, 907, 911, 919, 929, 937, 941, 947,
2808 953, 967, 971, 977, 983, 991, 997, -103
2809};
2810
2811/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002812 * Small divisors test (X must be positive)
2813 *
2814 * Return values:
2815 * 0: no small factor (possible prime, more tests needed)
2816 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002818 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002819 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820static int mpi_check_small_factors( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +00002821{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002822 int ret = 0;
2823 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824 mbedtls_mpi_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +00002825
Paul Bakker5121ce52009-01-03 21:22:43 +00002826 if( ( X->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002828
2829 for( i = 0; small_prime[i] > 0; i++ )
2830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831 if( mbedtls_mpi_cmp_int( X, small_prime[i] ) <= 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002832 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002834 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, small_prime[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
2836 if( r == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002838 }
2839
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002840cleanup:
2841 return( ret );
2842}
2843
2844/*
2845 * Miller-Rabin pseudo-primality test (HAC 4.24)
2846 */
Janos Follathda31fa12018-09-03 14:45:23 +01002847static int mpi_miller_rabin( const mbedtls_mpi *X, size_t rounds,
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002848 int (*f_rng)(void *, unsigned char *, size_t),
2849 void *p_rng )
2850{
Pascal Junodb99183d2015-03-11 16:49:45 +01002851 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002852 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002853 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002854
Hanno Becker8ce11a32018-12-19 16:18:52 +00002855 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002856 MPI_VALIDATE_RET( f_rng != NULL );
2857
2858 mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R );
2859 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860 mbedtls_mpi_init( &RR );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002861
Paul Bakker5121ce52009-01-03 21:22:43 +00002862 /*
2863 * W = |X| - 1
2864 * R = W >> lsb( W )
2865 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &W, X, 1 ) );
2867 s = mbedtls_mpi_lsb( &W );
2868 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R, &W ) );
2869 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &R, s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002870
Janos Follathda31fa12018-09-03 14:45:23 +01002871 for( i = 0; i < rounds; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002872 {
2873 /*
2874 * pick a random A, 1 < A < |X| - 1
2875 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002876 count = 0;
2877 do {
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002878 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
Pascal Junodb99183d2015-03-11 16:49:45 +01002879
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002880 j = mbedtls_mpi_bitlen( &A );
2881 k = mbedtls_mpi_bitlen( &W );
Pascal Junodb99183d2015-03-11 16:49:45 +01002882 if (j > k) {
Darryl Greene3f95ed2018-10-02 13:21:35 +01002883 A.p[A.n - 1] &= ( (mbedtls_mpi_uint) 1 << ( k - ( A.n - 1 ) * biL - 1 ) ) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002884 }
2885
2886 if (count++ > 30) {
Jens Wiklanderf08aa3e2019-01-17 13:30:57 +01002887 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2888 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002889 }
2890
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002891 } while ( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 ||
2892 mbedtls_mpi_cmp_int( &A, 1 ) <= 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002893
2894 /*
2895 * A = A^R mod |X|
2896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &A, &A, &R, X, &RR ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 if( mbedtls_mpi_cmp_mpi( &A, &W ) == 0 ||
2900 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002901 continue;
2902
2903 j = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 while( j < s && mbedtls_mpi_cmp_mpi( &A, &W ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002905 {
2906 /*
2907 * A = A * A mod |X|
2908 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &A, &A ) );
2910 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &A, &T, X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 if( mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002913 break;
2914
2915 j++;
2916 }
2917
2918 /*
2919 * not prime if A != |X| - 1 or A == 1
2920 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 if( mbedtls_mpi_cmp_mpi( &A, &W ) != 0 ||
2922 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002925 break;
2926 }
2927 }
2928
2929cleanup:
Hanno Becker73d7d792018-12-11 10:35:51 +00002930 mbedtls_mpi_free( &W ); mbedtls_mpi_free( &R );
2931 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002933
2934 return( ret );
2935}
2936
2937/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002938 * Pseudo-primality test: small factors, then Miller-Rabin
2939 */
Janos Follatha0b67c22018-09-18 14:48:23 +01002940int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds,
2941 int (*f_rng)(void *, unsigned char *, size_t),
2942 void *p_rng )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002943{
Janos Follath24eed8d2019-11-22 13:21:35 +00002944 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 mbedtls_mpi XX;
Hanno Becker8ce11a32018-12-19 16:18:52 +00002946 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002947 MPI_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002948
2949 XX.s = 1;
2950 XX.n = X->n;
2951 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953 if( mbedtls_mpi_cmp_int( &XX, 0 ) == 0 ||
2954 mbedtls_mpi_cmp_int( &XX, 1 ) == 0 )
2955 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957 if( mbedtls_mpi_cmp_int( &XX, 2 ) == 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002958 return( 0 );
2959
2960 if( ( ret = mpi_check_small_factors( &XX ) ) != 0 )
2961 {
2962 if( ret == 1 )
2963 return( 0 );
2964
2965 return( ret );
2966 }
2967
Janos Follathda31fa12018-09-03 14:45:23 +01002968 return( mpi_miller_rabin( &XX, rounds, f_rng, p_rng ) );
Janos Follathf301d232018-08-14 13:34:01 +01002969}
2970
Janos Follatha0b67c22018-09-18 14:48:23 +01002971#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Janos Follathf301d232018-08-14 13:34:01 +01002972/*
2973 * Pseudo-primality test, error probability 2^-80
2974 */
2975int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
2976 int (*f_rng)(void *, unsigned char *, size_t),
2977 void *p_rng )
2978{
Hanno Becker8ce11a32018-12-19 16:18:52 +00002979 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002980 MPI_VALIDATE_RET( f_rng != NULL );
2981
Janos Follatha0b67c22018-09-18 14:48:23 +01002982 /*
2983 * In the past our key generation aimed for an error rate of at most
2984 * 2^-80. Since this function is deprecated, aim for the same certainty
2985 * here as well.
2986 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002987 return( mbedtls_mpi_is_prime_ext( X, 40, f_rng, p_rng ) );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002988}
Janos Follatha0b67c22018-09-18 14:48:23 +01002989#endif
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002990
2991/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002992 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002993 *
Janos Follathf301d232018-08-14 13:34:01 +01002994 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2995 * be either 1024 bits or 1536 bits long, and flags must contain
2996 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002997 */
Janos Follath7c025a92018-08-14 11:08:41 +01002998int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002999 int (*f_rng)(void *, unsigned char *, size_t),
3000 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00003001{
Jethro Beekman66689272018-02-14 19:24:10 -08003002#ifdef MBEDTLS_HAVE_INT64
3003// ceil(2^63.5)
3004#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
3005#else
3006// ceil(2^31.5)
3007#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
3008#endif
3009 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003010 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01003011 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012 mbedtls_mpi_uint r;
3013 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00003014
Hanno Becker8ce11a32018-12-19 16:18:52 +00003015 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00003016 MPI_VALIDATE_RET( f_rng != NULL );
3017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018 if( nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS )
3019 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 mbedtls_mpi_init( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00003022
3023 n = BITS_TO_LIMBS( nbits );
3024
Janos Follathda31fa12018-09-03 14:45:23 +01003025 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR ) == 0 )
3026 {
3027 /*
3028 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
3029 */
3030 rounds = ( ( nbits >= 1300 ) ? 2 : ( nbits >= 850 ) ? 3 :
3031 ( nbits >= 650 ) ? 4 : ( nbits >= 350 ) ? 8 :
3032 ( nbits >= 250 ) ? 12 : ( nbits >= 150 ) ? 18 : 27 );
3033 }
3034 else
3035 {
3036 /*
3037 * 2^-100 error probability, number of rounds computed based on HAC,
3038 * fact 4.48
3039 */
3040 rounds = ( ( nbits >= 1450 ) ? 4 : ( nbits >= 1150 ) ? 5 :
3041 ( nbits >= 1000 ) ? 6 : ( nbits >= 850 ) ? 7 :
3042 ( nbits >= 750 ) ? 8 : ( nbits >= 500 ) ? 13 :
3043 ( nbits >= 250 ) ? 28 : ( nbits >= 150 ) ? 40 : 51 );
3044 }
3045
Jethro Beekman66689272018-02-14 19:24:10 -08003046 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 {
Jethro Beekman66689272018-02-14 19:24:10 -08003048 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
3049 /* make sure generated number is at least (nbits-1)+0.5 bits (FIPS 186-4 §B.3.3 steps 4.4, 5.5) */
3050 if( X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2 ) continue;
3051
3052 k = n * biL;
3053 if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits ) );
3054 X->p[0] |= 1;
3055
Janos Follath7c025a92018-08-14 11:08:41 +01003056 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003057 {
Janos Follatha0b67c22018-09-18 14:48:23 +01003058 ret = mbedtls_mpi_is_prime_ext( X, rounds, f_rng, p_rng );
Jethro Beekman66689272018-02-14 19:24:10 -08003059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003061 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003062 }
Jethro Beekman66689272018-02-14 19:24:10 -08003063 else
Paul Bakker5121ce52009-01-03 21:22:43 +00003064 {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01003065 /*
Jethro Beekman66689272018-02-14 19:24:10 -08003066 * An necessary condition for Y and X = 2Y + 1 to be prime
3067 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
3068 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01003069 */
Jethro Beekman66689272018-02-14 19:24:10 -08003070
3071 X->p[0] |= 2;
3072
3073 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, 3 ) );
3074 if( r == 0 )
3075 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 8 ) );
3076 else if( r == 1 )
3077 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 4 ) );
3078
3079 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
3080 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, X ) );
3081 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, 1 ) );
3082
3083 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003084 {
Jethro Beekman66689272018-02-14 19:24:10 -08003085 /*
3086 * First, check small factors for X and Y
3087 * before doing Miller-Rabin on any of them
3088 */
3089 if( ( ret = mpi_check_small_factors( X ) ) == 0 &&
3090 ( ret = mpi_check_small_factors( &Y ) ) == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01003091 ( ret = mpi_miller_rabin( X, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01003092 == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01003093 ( ret = mpi_miller_rabin( &Y, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01003094 == 0 )
Jethro Beekman66689272018-02-14 19:24:10 -08003095 goto cleanup;
3096
3097 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
3098 goto cleanup;
3099
3100 /*
3101 * Next candidates. We want to preserve Y = (X-1) / 2 and
3102 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
3103 * so up Y by 6 and X by 12.
3104 */
3105 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 12 ) );
3106 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &Y, &Y, 6 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003107 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003108 }
3109 }
3110
3111cleanup:
3112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003113 mbedtls_mpi_free( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00003114
3115 return( ret );
3116}
3117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00003119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003121
Paul Bakker23986e52011-04-24 08:57:21 +00003122#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003123
3124static const int gcd_pairs[GCD_PAIR_COUNT][3] =
3125{
3126 { 693, 609, 21 },
3127 { 1764, 868, 28 },
3128 { 768454923, 542167814, 1 }
3129};
3130
Paul Bakker5121ce52009-01-03 21:22:43 +00003131/*
3132 * Checkup routine
3133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134int mbedtls_mpi_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00003135{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003136 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00003138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); mbedtls_mpi_init( &X );
3140 mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &U ); mbedtls_mpi_init( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00003141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &A, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003143 "EFE021C2645FD1DC586E69184AF4A31E" \
3144 "D5F53E93B5F123FA41680867BA110131" \
3145 "944FE7952E2517337780CB0DB80E61AA" \
3146 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6" ) );
3147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003149 "B2E7EFD37075B9F03FF989C7C5051C20" \
3150 "34D2A323810251127E7BF8625A4F49A5" \
3151 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
3152 "5B5C25763222FEFCCFC38B832366C29E" ) );
3153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &N, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003155 "0066A198186C18C10B2F5ED9B522752A" \
3156 "9830B69916E535C8F047518A889A43A5" \
3157 "94B6BED27A168D31D4A52F88925AA8F5" ) );
3158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003162 "602AB7ECA597A3D6B56FF9829A5E8B85" \
3163 "9E857EA95A03512E2BAE7391688D264A" \
3164 "A5663B0341DB9CCFD2C4C5F421FEC814" \
3165 "8001B72E848A38CAE1C65F78E56ABDEF" \
3166 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
3167 "ECF677152EF804370C1A305CAF3B5BF1" \
3168 "30879B56C61DE584A0F53A2447A51E" ) );
3169
3170 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171 mbedtls_printf( " MPI test #1 (mul_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003174 {
3175 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003177
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003178 ret = 1;
3179 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003180 }
3181
3182 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003183 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &X, &Y, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003188 "256567336059E52CAE22925474705F39A94" ) );
3189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &V, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003191 "6613F26162223DF488E9CD48CC132C7A" \
3192 "0AC93C701B001B092E4E5B9F73BCD27B" \
3193 "9EE50D0657C77F374E903CDFA4C642" ) );
3194
3195 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196 mbedtls_printf( " MPI test #2 (div_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ||
3199 mbedtls_mpi_cmp_mpi( &Y, &V ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003200 {
3201 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003203
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003204 ret = 1;
3205 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003206 }
3207
3208 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003211 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &X, &A, &E, &N, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003214 "36E139AEA55215609D2816998ED020BB" \
3215 "BD96C37890F65171D948E9BC7CBAA4D9" \
3216 "325D24D6A3C12710F10A09FA08AB87" ) );
3217
3218 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219 mbedtls_printf( " MPI test #3 (exp_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003222 {
3223 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003225
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003226 ret = 1;
3227 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003228 }
3229
3230 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003236 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
3237 "C3DBA76456363A10869622EAC2DD84EC" \
3238 "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) );
3239
3240 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241 mbedtls_printf( " MPI test #4 (inv_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003244 {
3245 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003247
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003248 ret = 1;
3249 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003250 }
3251
3252 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003254
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003255 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003256 mbedtls_printf( " MPI test #5 (simple gcd): " );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003257
Paul Bakker66d5d072014-06-17 16:39:18 +02003258 for( i = 0; i < GCD_PAIR_COUNT; i++ )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &X, gcd_pairs[i][0] ) );
3261 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Y, gcd_pairs[i][1] ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003263 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &A, &X, &Y ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003265 if( mbedtls_mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 )
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003266 {
3267 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003268 mbedtls_printf( "failed at %d\n", i );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003269
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003270 ret = 1;
3271 goto cleanup;
3272 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003273 }
3274
3275 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276 mbedtls_printf( "passed\n" );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003277
Paul Bakker5121ce52009-01-03 21:22:43 +00003278cleanup:
3279
3280 if( ret != 0 && verbose != 0 )
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003281 mbedtls_printf( "Unexpected error, return code = %08X\n", (unsigned int) ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &X );
3284 mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &U ); mbedtls_mpi_free( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00003285
3286 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003287 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003288
3289 return( ret );
3290}
3291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294#endif /* MBEDTLS_BIGNUM_C */