blob: 641049ba784e76c4a5c155ae8759201382125955 [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
184/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 * Copy the contents of Y into X
186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000188{
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100189 int ret = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000190 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000191 MPI_VALIDATE_RET( X != NULL );
192 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
194 if( X == Y )
195 return( 0 );
196
Gilles Peskinedb420622020-01-20 21:12:50 +0100197 if( Y->n == 0 )
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_mpi_free( X );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200200 return( 0 );
201 }
202
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 for( i = Y->n - 1; i > 0; i-- )
204 if( Y->p[i] != 0 )
205 break;
206 i++;
207
208 X->s = Y->s;
209
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100210 if( X->n < i )
211 {
212 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i ) );
213 }
214 else
215 {
216 memset( X->p + i, 0, ( X->n - i ) * ciL );
217 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000218
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 memcpy( X->p, Y->p, i * ciL );
220
221cleanup:
222
223 return( ret );
224}
225
226/*
227 * Swap the contents of X and Y
228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000230{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000232 MPI_VALIDATE( X != NULL );
233 MPI_VALIDATE( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 memcpy( &T, X, sizeof( mbedtls_mpi ) );
236 memcpy( X, Y, sizeof( mbedtls_mpi ) );
237 memcpy( Y, &T, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000238}
239
Manuel Pégourié-Gonnard3ae4ae42021-06-07 09:51:00 +0200240/**
241 * Select between two sign values in constant-time.
242 *
243 * This is functionally equivalent to second ? a : b but uses only bit
244 * operations in order to avoid branches.
245 *
246 * \param[in] a The first sign; must be either +1 or -1.
247 * \param[in] b The second sign; must be either +1 or -1.
248 * \param[in] second Must be either 1 (return b) or 0 (return a).
249 *
250 * \return The selected sign value.
251 */
252static int mpi_safe_cond_select_sign( int a, int b, unsigned char second )
253{
254 /* In order to avoid questions about what we can reasonnably assume about
255 * the representations of signed integers, move everything to unsigned
256 * by taking advantage of the fact that a and b are either +1 or -1. */
257 unsigned ua = a + 1;
258 unsigned ub = b + 1;
259
260 /* MSVC has a warning about unary minus on unsigned integer types,
261 * but this is well-defined and precisely what we want to do here. */
262#if defined(_MSC_VER)
263#pragma warning( push )
264#pragma warning( disable : 4146 )
265#endif
266 /* all-bits 1 if assign is 1, all-bits 0 if assign is 0 */
267 const unsigned mask = -second;
268#if defined(_MSC_VER)
269#pragma warning( pop )
270#endif
271
272 /* select ua or ub */
273 unsigned ur = ( ua & ~mask ) | ( ub & mask );
274
275 /* ur is now 0 or 2, convert back to -1 or +1 */
276 return( (int) ur - 1 );
277}
278
Paul Bakker5121ce52009-01-03 21:22:43 +0000279/*
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200280 * Conditionally assign dest = src, without leaking information
281 * about whether the assignment was made or not.
282 * dest and src must be arrays of limbs of size n.
283 * assign must be 0 or 1.
284 */
285static void mpi_safe_cond_assign( size_t n,
286 mbedtls_mpi_uint *dest,
287 const mbedtls_mpi_uint *src,
288 unsigned char assign )
289{
290 size_t i;
Manuel Pégourié-Gonnard5ada7a82021-05-31 11:48:45 +0200291
292 /* MSVC has a warning about unary minus on unsigned integer types,
293 * but this is well-defined and precisely what we want to do here. */
294#if defined(_MSC_VER)
295#pragma warning( push )
296#pragma warning( disable : 4146 )
297#endif
298
299 /* all-bits 1 if assign is 1, all-bits 0 if assign is 0 */
300 const mbedtls_mpi_uint mask = -assign;
301
302#if defined(_MSC_VER)
303#pragma warning( pop )
304#endif
305
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200306 for( i = 0; i < n; i++ )
Manuel Pégourié-Gonnard5ada7a82021-05-31 11:48:45 +0200307 dest[i] = ( src[i] & mask ) | ( dest[i] & ~mask );
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200308}
309
310/*
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100311 * Conditionally assign X = Y, without leaking information
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +0100312 * about whether the assignment was made or not.
313 * (Leaking information about the respective sizes of X and Y is ok however.)
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100316{
317 int ret = 0;
318 size_t i;
Manuel Pégourié-Gonnard5ada7a82021-05-31 11:48:45 +0200319 mbedtls_mpi_uint limb_mask;
Hanno Becker73d7d792018-12-11 10:35:51 +0000320 MPI_VALIDATE_RET( X != NULL );
321 MPI_VALIDATE_RET( Y != NULL );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100322
Manuel Pégourié-Gonnard5ada7a82021-05-31 11:48:45 +0200323 /* MSVC has a warning about unary minus on unsigned integer types,
324 * but this is well-defined and precisely what we want to do here. */
325#if defined(_MSC_VER)
326#pragma warning( push )
327#pragma warning( disable : 4146 )
328#endif
329
Pascal Junodb99183d2015-03-11 16:49:45 +0100330 /* make sure assign is 0 or 1 in a time-constant manner */
331 assign = (assign | (unsigned char)-assign) >> 7;
Manuel Pégourié-Gonnard5ada7a82021-05-31 11:48:45 +0200332 /* all-bits 1 if assign is 1, all-bits 0 if assign is 0 */
Manuel Pégourié-Gonnard5ada7a82021-05-31 11:48:45 +0200333 limb_mask = -assign;
334
335#if defined(_MSC_VER)
336#pragma warning( pop )
337#endif
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100340
Manuel Pégourié-Gonnard3ae4ae42021-06-07 09:51:00 +0200341 X->s = mpi_safe_cond_select_sign( X->s, Y->s, assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100342
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200343 mpi_safe_cond_assign( Y->n, X->p, Y->p, assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100344
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200345 for( i = Y->n; i < X->n; i++ )
Manuel Pégourié-Gonnard5ada7a82021-05-31 11:48:45 +0200346 X->p[i] &= ~limb_mask;
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100347
348cleanup:
349 return( ret );
350}
351
352/*
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100353 * Conditionally swap X and Y, without leaking information
354 * about whether the swap was made or not.
355 * Here it is not ok to simply swap the pointers, which whould lead to
356 * different memory access patterns when X and Y are used afterwards.
357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100359{
360 int ret, s;
361 size_t i;
Manuel Pégourié-Gonnard448f1352021-06-03 10:54:01 +0200362 mbedtls_mpi_uint limb_mask;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_mpi_uint tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +0000364 MPI_VALIDATE_RET( X != NULL );
365 MPI_VALIDATE_RET( Y != NULL );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100366
367 if( X == Y )
368 return( 0 );
369
Manuel Pégourié-Gonnard448f1352021-06-03 10:54:01 +0200370 /* MSVC has a warning about unary minus on unsigned integer types,
371 * but this is well-defined and precisely what we want to do here. */
372#if defined(_MSC_VER)
373#pragma warning( push )
374#pragma warning( disable : 4146 )
375#endif
376
Pascal Junodb99183d2015-03-11 16:49:45 +0100377 /* make sure swap is 0 or 1 in a time-constant manner */
378 swap = (swap | (unsigned char)-swap) >> 7;
Manuel Pégourié-Gonnard448f1352021-06-03 10:54:01 +0200379 /* all-bits 1 if swap is 1, all-bits 0 if swap is 0 */
Manuel Pégourié-Gonnard448f1352021-06-03 10:54:01 +0200380 limb_mask = -swap;
381
382#if defined(_MSC_VER)
383#pragma warning( pop )
384#endif
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
387 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( Y, X->n ) );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100388
389 s = X->s;
Manuel Pégourié-Gonnard3ae4ae42021-06-07 09:51:00 +0200390 X->s = mpi_safe_cond_select_sign( X->s, Y->s, swap );
391 Y->s = mpi_safe_cond_select_sign( Y->s, s, swap );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100392
393
394 for( i = 0; i < X->n; i++ )
395 {
396 tmp = X->p[i];
Manuel Pégourié-Gonnard448f1352021-06-03 10:54:01 +0200397 X->p[i] = ( X->p[i] & ~limb_mask ) | ( Y->p[i] & limb_mask );
398 Y->p[i] = ( Y->p[i] & ~limb_mask ) | ( tmp & limb_mask );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100399 }
400
401cleanup:
402 return( ret );
403}
404
405/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000406 * Set value from integer
407 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +0000409{
Janos Follath24eed8d2019-11-22 13:21:35 +0000410 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker73d7d792018-12-11 10:35:51 +0000411 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000414 memset( X->p, 0, X->n * ciL );
415
416 X->p[0] = ( z < 0 ) ? -z : z;
417 X->s = ( z < 0 ) ? -1 : 1;
418
419cleanup:
420
421 return( ret );
422}
423
424/*
Paul Bakker2f5947e2011-05-18 15:47:11 +0000425 * Get a specific bit
426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000428{
Hanno Becker73d7d792018-12-11 10:35:51 +0000429 MPI_VALIDATE_RET( X != NULL );
430
Paul Bakker2f5947e2011-05-18 15:47:11 +0000431 if( X->n * biL <= pos )
432 return( 0 );
433
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200434 return( ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000435}
436
Gilles Peskine11cdb052018-11-20 16:47:47 +0100437/* Get a specific byte, without range checks. */
438#define GET_BYTE( X, i ) \
439 ( ( ( X )->p[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff )
440
Paul Bakker2f5947e2011-05-18 15:47:11 +0000441/*
442 * Set a bit to a specific value of 0 or 1
443 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000445{
446 int ret = 0;
447 size_t off = pos / biL;
448 size_t idx = pos % biL;
Hanno Becker73d7d792018-12-11 10:35:51 +0000449 MPI_VALIDATE_RET( X != NULL );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000450
451 if( val != 0 && val != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker9af723c2014-05-01 13:03:14 +0200453
Paul Bakker2f5947e2011-05-18 15:47:11 +0000454 if( X->n * biL <= pos )
455 {
456 if( val == 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200457 return( 0 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, off + 1 ) );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000460 }
461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 X->p[off] &= ~( (mbedtls_mpi_uint) 0x01 << idx );
463 X->p[off] |= (mbedtls_mpi_uint) val << idx;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000464
465cleanup:
Paul Bakker9af723c2014-05-01 13:03:14 +0200466
Paul Bakker2f5947e2011-05-18 15:47:11 +0000467 return( ret );
468}
469
470/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200471 * Return the number of less significant zero-bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473size_t mbedtls_mpi_lsb( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000474{
Paul Bakker23986e52011-04-24 08:57:21 +0000475 size_t i, j, count = 0;
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000476 MBEDTLS_INTERNAL_VALIDATE_RET( X != NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000477
478 for( i = 0; i < X->n; i++ )
Paul Bakkerf9688572011-05-05 10:00:45 +0000479 for( j = 0; j < biL; j++, count++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000480 if( ( ( X->p[i] >> j ) & 1 ) != 0 )
481 return( count );
482
483 return( 0 );
484}
485
486/*
Simon Butcher15b15d12015-11-26 19:35:03 +0000487 * Count leading zero bits in a given integer
488 */
489static size_t mbedtls_clz( const mbedtls_mpi_uint x )
490{
491 size_t j;
Manuel Pégourié-Gonnarde3e8edf2015-12-01 09:31:52 +0100492 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
Simon Butcher15b15d12015-11-26 19:35:03 +0000493
494 for( j = 0; j < biL; j++ )
495 {
496 if( x & mask ) break;
497
498 mask >>= 1;
499 }
500
501 return j;
502}
503
504/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200505 * Return the number of bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200507size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000508{
Paul Bakker23986e52011-04-24 08:57:21 +0000509 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200511 if( X->n == 0 )
512 return( 0 );
513
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 for( i = X->n - 1; i > 0; i-- )
515 if( X->p[i] != 0 )
516 break;
517
Simon Butcher15b15d12015-11-26 19:35:03 +0000518 j = biL - mbedtls_clz( X->p[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000519
Paul Bakker23986e52011-04-24 08:57:21 +0000520 return( ( i * biL ) + j );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521}
522
523/*
524 * Return the total size in bytes
525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526size_t mbedtls_mpi_size( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000527{
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200528 return( ( mbedtls_mpi_bitlen( X ) + 7 ) >> 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000529}
530
531/*
532 * Convert an ASCII character to digit value
533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534static int mpi_get_digit( mbedtls_mpi_uint *d, int radix, char c )
Paul Bakker5121ce52009-01-03 21:22:43 +0000535{
536 *d = 255;
537
538 if( c >= 0x30 && c <= 0x39 ) *d = c - 0x30;
539 if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37;
540 if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57;
541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 if( *d >= (mbedtls_mpi_uint) radix )
543 return( MBEDTLS_ERR_MPI_INVALID_CHARACTER );
Paul Bakker5121ce52009-01-03 21:22:43 +0000544
545 return( 0 );
546}
547
548/*
549 * Import from an ASCII string
550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000552{
Janos Follath24eed8d2019-11-22 13:21:35 +0000553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000554 size_t i, j, slen, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_mpi_uint d;
556 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000557 MPI_VALIDATE_RET( X != NULL );
558 MPI_VALIDATE_RET( s != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
560 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000561 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000564
Paul Bakkerff60ee62010-03-16 21:09:09 +0000565 slen = strlen( s );
566
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 if( radix == 16 )
568 {
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +0100569 if( slen > MPI_SIZE_T_MAX >> 2 )
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +0200570 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
571
Paul Bakkerff60ee62010-03-16 21:09:09 +0000572 n = BITS_TO_LIMBS( slen << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n ) );
575 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
Paul Bakker23986e52011-04-24 08:57:21 +0000577 for( i = slen, j = 0; i > 0; i--, j++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 {
Paul Bakker23986e52011-04-24 08:57:21 +0000579 if( i == 1 && s[i - 1] == '-' )
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 {
581 X->s = -1;
582 break;
583 }
584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200586 X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000587 }
588 }
589 else
590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000592
Paul Bakkerff60ee62010-03-16 21:09:09 +0000593 for( i = 0; i < slen; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 {
595 if( i == 0 && s[i] == '-' )
596 {
597 X->s = -1;
598 continue;
599 }
600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
602 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T, X, radix ) );
Paul Bakker05feca62009-06-20 08:22:43 +0000603
604 if( X->s == 1 )
605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, &T, d ) );
Paul Bakker05feca62009-06-20 08:22:43 +0000607 }
608 else
609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( X, &T, d ) );
Paul Bakker05feca62009-06-20 08:22:43 +0000611 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000612 }
613 }
614
615cleanup:
616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000618
619 return( ret );
620}
621
622/*
Ron Eldora16fa292018-11-20 14:07:01 +0200623 * Helper to write the digits high-order first.
Paul Bakker5121ce52009-01-03 21:22:43 +0000624 */
Ron Eldora16fa292018-11-20 14:07:01 +0200625static int mpi_write_hlp( mbedtls_mpi *X, int radix,
626 char **p, const size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000627{
Janos Follath24eed8d2019-11-22 13:21:35 +0000628 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_mpi_uint r;
Ron Eldora16fa292018-11-20 14:07:01 +0200630 size_t length = 0;
631 char *p_end = *p + buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000632
Ron Eldora16fa292018-11-20 14:07:01 +0200633 do
634 {
635 if( length >= buflen )
636 {
637 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
638 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000639
Ron Eldora16fa292018-11-20 14:07:01 +0200640 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) );
641 MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) );
642 /*
643 * Write the residue in the current position, as an ASCII character.
644 */
645 if( r < 0xA )
646 *(--p_end) = (char)( '0' + r );
647 else
648 *(--p_end) = (char)( 'A' + ( r - 0xA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000649
Ron Eldora16fa292018-11-20 14:07:01 +0200650 length++;
651 } while( mbedtls_mpi_cmp_int( X, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
Ron Eldora16fa292018-11-20 14:07:01 +0200653 memmove( *p, p_end, length );
654 *p += length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
656cleanup:
657
658 return( ret );
659}
660
661/*
662 * Export into an ASCII string
663 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100664int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
665 char *buf, size_t buflen, size_t *olen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000666{
Paul Bakker23986e52011-04-24 08:57:21 +0000667 int ret = 0;
668 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000669 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000671 MPI_VALIDATE_RET( X != NULL );
672 MPI_VALIDATE_RET( olen != NULL );
673 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
675 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000676 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000677
Hanno Becker23cfea02019-02-04 09:45:07 +0000678 n = mbedtls_mpi_bitlen( X ); /* Number of bits necessary to present `n`. */
679 if( radix >= 4 ) n >>= 1; /* Number of 4-adic digits necessary to present
680 * `n`. If radix > 4, this might be a strict
681 * overapproximation of the number of
682 * radix-adic digits needed to present `n`. */
683 if( radix >= 16 ) n >>= 1; /* Number of hexadecimal digits necessary to
684 * present `n`. */
685
Janos Follath80470622019-03-06 13:43:02 +0000686 n += 1; /* Terminating null byte */
Hanno Becker23cfea02019-02-04 09:45:07 +0000687 n += 1; /* Compensate for the divisions above, which round down `n`
688 * in case it's not even. */
689 n += 1; /* Potential '-'-sign. */
690 n += ( n & 1 ); /* Make n even to have enough space for hexadecimal writing,
691 * which always uses an even number of hex-digits. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100693 if( buflen < n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000694 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100695 *olen = n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000697 }
698
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100699 p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000701
702 if( X->s == -1 )
Hanno Beckerc983c812019-02-01 16:41:30 +0000703 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000704 *p++ = '-';
Hanno Beckerc983c812019-02-01 16:41:30 +0000705 buflen--;
706 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000707
708 if( radix == 16 )
709 {
Paul Bakker23986e52011-04-24 08:57:21 +0000710 int c;
711 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000712
Paul Bakker23986e52011-04-24 08:57:21 +0000713 for( i = X->n, k = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000714 {
Paul Bakker23986e52011-04-24 08:57:21 +0000715 for( j = ciL; j > 0; j-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000716 {
Paul Bakker23986e52011-04-24 08:57:21 +0000717 c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000718
Paul Bakker6c343d72014-07-10 14:36:19 +0200719 if( c == 0 && k == 0 && ( i + j ) != 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000720 continue;
721
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000722 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000723 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000724 k = 1;
725 }
726 }
727 }
728 else
729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T, X ) );
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000731
732 if( T.s == -1 )
733 T.s = 1;
734
Ron Eldora16fa292018-11-20 14:07:01 +0200735 MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p, buflen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000736 }
737
738 *p++ = '\0';
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100739 *olen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000740
741cleanup:
742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000744
745 return( ret );
746}
747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000749/*
750 * Read X from an opened file
751 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin )
Paul Bakker5121ce52009-01-03 21:22:43 +0000753{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_mpi_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000755 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000756 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000757 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000758 * Buffer should have space for (short) label and decimal formatted MPI,
759 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000760 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Paul Bakker5121ce52009-01-03 21:22:43 +0000762
Hanno Becker73d7d792018-12-11 10:35:51 +0000763 MPI_VALIDATE_RET( X != NULL );
764 MPI_VALIDATE_RET( fin != NULL );
765
766 if( radix < 2 || radix > 16 )
767 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
768
Paul Bakker5121ce52009-01-03 21:22:43 +0000769 memset( s, 0, sizeof( s ) );
770 if( fgets( s, sizeof( s ) - 1, fin ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000772
773 slen = strlen( s );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000774 if( slen == sizeof( s ) - 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000776
Hanno Beckerb2034b72017-04-26 11:46:46 +0100777 if( slen > 0 && s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
778 if( slen > 0 && s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
Paul Bakker5121ce52009-01-03 21:22:43 +0000779
780 p = s + slen;
Hanno Beckerb2034b72017-04-26 11:46:46 +0100781 while( p-- > s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000782 if( mpi_get_digit( &d, radix, *p ) != 0 )
783 break;
784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 return( mbedtls_mpi_read_string( X, radix, p + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000786}
787
788/*
789 * Write X into an opened file (or stdout if fout == NULL)
790 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout )
Paul Bakker5121ce52009-01-03 21:22:43 +0000792{
Janos Follath24eed8d2019-11-22 13:21:35 +0000793 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000794 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000795 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000796 * Buffer should have space for (short) label and decimal formatted MPI,
797 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000798 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Hanno Becker73d7d792018-12-11 10:35:51 +0000800 MPI_VALIDATE_RET( X != NULL );
801
802 if( radix < 2 || radix > 16 )
803 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000804
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100805 memset( s, 0, sizeof( s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000806
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100807 MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, sizeof( s ) - 2, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000808
809 if( p == NULL ) p = "";
810
811 plen = strlen( p );
812 slen = strlen( s );
813 s[slen++] = '\r';
814 s[slen++] = '\n';
815
816 if( fout != NULL )
817 {
818 if( fwrite( p, 1, plen, fout ) != plen ||
819 fwrite( s, 1, slen, fout ) != slen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000821 }
822 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 mbedtls_printf( "%s%s", p, s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000824
825cleanup:
826
827 return( ret );
828}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000830
Hanno Beckerda1655a2017-10-18 14:21:44 +0100831
832/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
833 * into the storage form used by mbedtls_mpi. */
Hanno Beckerf8720072018-11-08 11:53:49 +0000834
835static mbedtls_mpi_uint mpi_uint_bigendian_to_host_c( mbedtls_mpi_uint x )
836{
837 uint8_t i;
Hanno Becker031d6332019-05-01 17:09:11 +0100838 unsigned char *x_ptr;
Hanno Beckerf8720072018-11-08 11:53:49 +0000839 mbedtls_mpi_uint tmp = 0;
Hanno Becker031d6332019-05-01 17:09:11 +0100840
841 for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
842 {
843 tmp <<= CHAR_BIT;
844 tmp |= (mbedtls_mpi_uint) *x_ptr;
845 }
846
Hanno Beckerf8720072018-11-08 11:53:49 +0000847 return( tmp );
848}
849
850static mbedtls_mpi_uint mpi_uint_bigendian_to_host( mbedtls_mpi_uint x )
851{
852#if defined(__BYTE_ORDER__)
853
854/* Nothing to do on bigendian systems. */
855#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
856 return( x );
857#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
858
859#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
860
861/* For GCC and Clang, have builtins for byte swapping. */
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000862#if defined(__GNUC__) && defined(__GNUC_PREREQ)
863#if __GNUC_PREREQ(4,3)
Hanno Beckerf8720072018-11-08 11:53:49 +0000864#define have_bswap
865#endif
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000866#endif
867
868#if defined(__clang__) && defined(__has_builtin)
869#if __has_builtin(__builtin_bswap32) && \
870 __has_builtin(__builtin_bswap64)
871#define have_bswap
872#endif
873#endif
874
Hanno Beckerf8720072018-11-08 11:53:49 +0000875#if defined(have_bswap)
876 /* The compiler is hopefully able to statically evaluate this! */
877 switch( sizeof(mbedtls_mpi_uint) )
878 {
879 case 4:
880 return( __builtin_bswap32(x) );
881 case 8:
882 return( __builtin_bswap64(x) );
883 }
884#endif
885#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
886#endif /* __BYTE_ORDER__ */
887
888 /* Fall back to C-based reordering if we don't know the byte order
889 * or we couldn't use a compiler-specific builtin. */
890 return( mpi_uint_bigendian_to_host_c( x ) );
891}
892
Hanno Becker2be8a552018-10-25 12:40:09 +0100893static void mpi_bigendian_to_host( mbedtls_mpi_uint * const p, size_t limbs )
Hanno Beckerda1655a2017-10-18 14:21:44 +0100894{
Hanno Beckerda1655a2017-10-18 14:21:44 +0100895 mbedtls_mpi_uint *cur_limb_left;
896 mbedtls_mpi_uint *cur_limb_right;
Hanno Becker2be8a552018-10-25 12:40:09 +0100897 if( limbs == 0 )
898 return;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100899
900 /*
901 * Traverse limbs and
902 * - adapt byte-order in each limb
903 * - swap the limbs themselves.
904 * For that, simultaneously traverse the limbs from left to right
905 * and from right to left, as long as the left index is not bigger
906 * than the right index (it's not a problem if limbs is odd and the
907 * indices coincide in the last iteration).
908 */
Hanno Beckerda1655a2017-10-18 14:21:44 +0100909 for( cur_limb_left = p, cur_limb_right = p + ( limbs - 1 );
910 cur_limb_left <= cur_limb_right;
911 cur_limb_left++, cur_limb_right-- )
912 {
Hanno Beckerf8720072018-11-08 11:53:49 +0000913 mbedtls_mpi_uint tmp;
914 /* Note that if cur_limb_left == cur_limb_right,
915 * this code effectively swaps the bytes only once. */
916 tmp = mpi_uint_bigendian_to_host( *cur_limb_left );
917 *cur_limb_left = mpi_uint_bigendian_to_host( *cur_limb_right );
918 *cur_limb_right = tmp;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100919 }
Hanno Beckerda1655a2017-10-18 14:21:44 +0100920}
921
Paul Bakker5121ce52009-01-03 21:22:43 +0000922/*
Janos Follatha778a942019-02-13 10:28:28 +0000923 * Import X from unsigned binary data, little endian
924 */
925int mbedtls_mpi_read_binary_le( mbedtls_mpi *X,
926 const unsigned char *buf, size_t buflen )
927{
Janos Follath24eed8d2019-11-22 13:21:35 +0000928 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follatha778a942019-02-13 10:28:28 +0000929 size_t i;
930 size_t const limbs = CHARS_TO_LIMBS( buflen );
931
932 /* Ensure that target MPI has exactly the necessary number of limbs */
933 if( X->n != limbs )
934 {
935 mbedtls_mpi_free( X );
936 mbedtls_mpi_init( X );
937 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, limbs ) );
938 }
939
940 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
941
942 for( i = 0; i < buflen; i++ )
943 X->p[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
944
945cleanup:
946
Janos Follath171a7ef2019-02-15 16:17:45 +0000947 /*
948 * This function is also used to import keys. However, wiping the buffers
949 * upon failure is not necessary because failure only can happen before any
950 * input is copied.
951 */
Janos Follatha778a942019-02-13 10:28:28 +0000952 return( ret );
953}
954
955/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000956 * Import X from unsigned binary data, big endian
957 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000959{
Janos Follath24eed8d2019-11-22 13:21:35 +0000960 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100961 size_t const limbs = CHARS_TO_LIMBS( buflen );
962 size_t const overhead = ( limbs * ciL ) - buflen;
963 unsigned char *Xp;
Paul Bakker5121ce52009-01-03 21:22:43 +0000964
Hanno Becker8ce11a32018-12-19 16:18:52 +0000965 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +0000966 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
967
Hanno Becker073c1992017-10-17 15:17:27 +0100968 /* Ensure that target MPI has exactly the necessary number of limbs */
969 if( X->n != limbs )
970 {
971 mbedtls_mpi_free( X );
972 mbedtls_mpi_init( X );
973 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, limbs ) );
974 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000976
Hanno Becker0e810b92019-01-03 17:13:11 +0000977 /* Avoid calling `memcpy` with NULL source argument,
978 * even if buflen is 0. */
979 if( buf != NULL )
980 {
981 Xp = (unsigned char*) X->p;
982 memcpy( Xp + overhead, buf, buflen );
Hanno Beckerda1655a2017-10-18 14:21:44 +0100983
Hanno Becker0e810b92019-01-03 17:13:11 +0000984 mpi_bigendian_to_host( X->p, limbs );
985 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000986
987cleanup:
988
Janos Follath171a7ef2019-02-15 16:17:45 +0000989 /*
990 * This function is also used to import keys. However, wiping the buffers
991 * upon failure is not necessary because failure only can happen before any
992 * input is copied.
993 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000994 return( ret );
995}
996
997/*
Janos Follathe344d0f2019-02-19 16:17:40 +0000998 * Export X into unsigned binary data, little endian
999 */
1000int mbedtls_mpi_write_binary_le( const mbedtls_mpi *X,
1001 unsigned char *buf, size_t buflen )
1002{
1003 size_t stored_bytes = X->n * ciL;
1004 size_t bytes_to_copy;
1005 size_t i;
1006
1007 if( stored_bytes < buflen )
1008 {
1009 bytes_to_copy = stored_bytes;
1010 }
1011 else
1012 {
1013 bytes_to_copy = buflen;
1014
1015 /* The output buffer is smaller than the allocated size of X.
1016 * However X may fit if its leading bytes are zero. */
1017 for( i = bytes_to_copy; i < stored_bytes; i++ )
1018 {
1019 if( GET_BYTE( X, i ) != 0 )
1020 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
1021 }
1022 }
1023
1024 for( i = 0; i < bytes_to_copy; i++ )
1025 buf[i] = GET_BYTE( X, i );
1026
1027 if( stored_bytes < buflen )
1028 {
1029 /* Write trailing 0 bytes */
1030 memset( buf + stored_bytes, 0, buflen - stored_bytes );
1031 }
1032
1033 return( 0 );
1034}
1035
1036/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001037 * Export X into unsigned binary data, big endian
1038 */
Gilles Peskine11cdb052018-11-20 16:47:47 +01001039int mbedtls_mpi_write_binary( const mbedtls_mpi *X,
1040 unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001041{
Hanno Becker73d7d792018-12-11 10:35:51 +00001042 size_t stored_bytes;
Gilles Peskine11cdb052018-11-20 16:47:47 +01001043 size_t bytes_to_copy;
1044 unsigned char *p;
1045 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +00001046
Hanno Becker73d7d792018-12-11 10:35:51 +00001047 MPI_VALIDATE_RET( X != NULL );
1048 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
1049
1050 stored_bytes = X->n * ciL;
1051
Gilles Peskine11cdb052018-11-20 16:47:47 +01001052 if( stored_bytes < buflen )
1053 {
1054 /* There is enough space in the output buffer. Write initial
1055 * null bytes and record the position at which to start
1056 * writing the significant bytes. In this case, the execution
1057 * trace of this function does not depend on the value of the
1058 * number. */
1059 bytes_to_copy = stored_bytes;
1060 p = buf + buflen - stored_bytes;
1061 memset( buf, 0, buflen - stored_bytes );
1062 }
1063 else
1064 {
1065 /* The output buffer is smaller than the allocated size of X.
1066 * However X may fit if its leading bytes are zero. */
1067 bytes_to_copy = buflen;
1068 p = buf;
1069 for( i = bytes_to_copy; i < stored_bytes; i++ )
1070 {
1071 if( GET_BYTE( X, i ) != 0 )
1072 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
1073 }
1074 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001075
Gilles Peskine11cdb052018-11-20 16:47:47 +01001076 for( i = 0; i < bytes_to_copy; i++ )
1077 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
Paul Bakker5121ce52009-01-03 21:22:43 +00001078
1079 return( 0 );
1080}
1081
1082/*
1083 * Left-shift: X <<= count
1084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +00001086{
Janos Follath24eed8d2019-11-22 13:21:35 +00001087 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001088 size_t i, v0, t1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +00001090 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001091
1092 v0 = count / (biL );
1093 t1 = count & (biL - 1);
1094
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001095 i = mbedtls_mpi_bitlen( X ) + count;
Paul Bakker5121ce52009-01-03 21:22:43 +00001096
Paul Bakkerf9688572011-05-05 10:00:45 +00001097 if( X->n * biL < i )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001099
1100 ret = 0;
1101
1102 /*
1103 * shift by count / limb_size
1104 */
1105 if( v0 > 0 )
1106 {
Paul Bakker23986e52011-04-24 08:57:21 +00001107 for( i = X->n; i > v0; i-- )
1108 X->p[i - 1] = X->p[i - v0 - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001109
Paul Bakker23986e52011-04-24 08:57:21 +00001110 for( ; i > 0; i-- )
1111 X->p[i - 1] = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 }
1113
1114 /*
1115 * shift by count % limb_size
1116 */
1117 if( t1 > 0 )
1118 {
1119 for( i = v0; i < X->n; i++ )
1120 {
1121 r1 = X->p[i] >> (biL - t1);
1122 X->p[i] <<= t1;
1123 X->p[i] |= r0;
1124 r0 = r1;
1125 }
1126 }
1127
1128cleanup:
1129
1130 return( ret );
1131}
1132
1133/*
1134 * Right-shift: X >>= count
1135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +00001137{
Paul Bakker23986e52011-04-24 08:57:21 +00001138 size_t i, v0, v1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +00001140 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001141
1142 v0 = count / biL;
1143 v1 = count & (biL - 1);
1144
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001145 if( v0 > X->n || ( v0 == X->n && v1 > 0 ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 return mbedtls_mpi_lset( X, 0 );
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001147
Paul Bakker5121ce52009-01-03 21:22:43 +00001148 /*
1149 * shift by count / limb_size
1150 */
1151 if( v0 > 0 )
1152 {
1153 for( i = 0; i < X->n - v0; i++ )
1154 X->p[i] = X->p[i + v0];
1155
1156 for( ; i < X->n; i++ )
1157 X->p[i] = 0;
1158 }
1159
1160 /*
1161 * shift by count % limb_size
1162 */
1163 if( v1 > 0 )
1164 {
Paul Bakker23986e52011-04-24 08:57:21 +00001165 for( i = X->n; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001166 {
Paul Bakker23986e52011-04-24 08:57:21 +00001167 r1 = X->p[i - 1] << (biL - v1);
1168 X->p[i - 1] >>= v1;
1169 X->p[i - 1] |= r0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 r0 = r1;
1171 }
1172 }
1173
1174 return( 0 );
1175}
1176
1177/*
1178 * Compare unsigned values
1179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001181{
Paul Bakker23986e52011-04-24 08:57:21 +00001182 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001183 MPI_VALIDATE_RET( X != NULL );
1184 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001185
Paul Bakker23986e52011-04-24 08:57:21 +00001186 for( i = X->n; i > 0; i-- )
1187 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 break;
1189
Paul Bakker23986e52011-04-24 08:57:21 +00001190 for( j = Y->n; j > 0; j-- )
1191 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001192 break;
1193
Paul Bakker23986e52011-04-24 08:57:21 +00001194 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001195 return( 0 );
1196
1197 if( i > j ) return( 1 );
1198 if( j > i ) return( -1 );
1199
Paul Bakker23986e52011-04-24 08:57:21 +00001200 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 {
Paul Bakker23986e52011-04-24 08:57:21 +00001202 if( X->p[i - 1] > Y->p[i - 1] ) return( 1 );
1203 if( X->p[i - 1] < Y->p[i - 1] ) return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001204 }
1205
1206 return( 0 );
1207}
1208
1209/*
1210 * Compare signed values
1211 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001213{
Paul Bakker23986e52011-04-24 08:57:21 +00001214 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001215 MPI_VALIDATE_RET( X != NULL );
1216 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001217
Paul Bakker23986e52011-04-24 08:57:21 +00001218 for( i = X->n; i > 0; i-- )
1219 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 break;
1221
Paul Bakker23986e52011-04-24 08:57:21 +00001222 for( j = Y->n; j > 0; j-- )
1223 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001224 break;
1225
Paul Bakker23986e52011-04-24 08:57:21 +00001226 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001227 return( 0 );
1228
1229 if( i > j ) return( X->s );
Paul Bakker0c8f73b2012-03-22 14:08:57 +00001230 if( j > i ) return( -Y->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001231
1232 if( X->s > 0 && Y->s < 0 ) return( 1 );
1233 if( Y->s > 0 && X->s < 0 ) return( -1 );
1234
Paul Bakker23986e52011-04-24 08:57:21 +00001235 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001236 {
Paul Bakker23986e52011-04-24 08:57:21 +00001237 if( X->p[i - 1] > Y->p[i - 1] ) return( X->s );
1238 if( X->p[i - 1] < Y->p[i - 1] ) return( -X->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001239 }
1240
1241 return( 0 );
1242}
1243
Janos Follath3f6f0e42019-10-14 09:09:32 +01001244/** Decide if an integer is less than the other, without branches.
1245 *
1246 * \param x First integer.
1247 * \param y Second integer.
1248 *
1249 * \return 1 if \p x is less than \p y, 0 otherwise
1250 */
Janos Follath0e5532d2019-10-11 14:21:53 +01001251static unsigned ct_lt_mpi_uint( const mbedtls_mpi_uint x,
1252 const mbedtls_mpi_uint y )
Janos Follathee6abce2019-09-05 14:47:19 +01001253{
1254 mbedtls_mpi_uint ret;
1255 mbedtls_mpi_uint cond;
1256
1257 /*
1258 * Check if the most significant bits (MSB) of the operands are different.
1259 */
1260 cond = ( x ^ y );
1261 /*
1262 * If the MSB are the same then the difference x-y will be negative (and
1263 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
1264 */
1265 ret = ( x - y ) & ~cond;
1266 /*
1267 * If the MSB are different, then the operand with the MSB of 1 is the
1268 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
1269 * the MSB of y is 0.)
1270 */
1271 ret |= y & cond;
1272
1273
Janos Follatha0f732b2019-10-14 08:59:14 +01001274 ret = ret >> ( biL - 1 );
Janos Follathee6abce2019-09-05 14:47:19 +01001275
Janos Follath67ce6472019-10-29 15:08:46 +00001276 return (unsigned) ret;
Janos Follathee6abce2019-09-05 14:47:19 +01001277}
1278
1279/*
1280 * Compare signed values in constant time
1281 */
Janos Follath0e5532d2019-10-11 14:21:53 +01001282int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y,
1283 unsigned *ret )
Janos Follathee6abce2019-09-05 14:47:19 +01001284{
1285 size_t i;
Janos Follathbb5147f2019-10-28 12:23:18 +00001286 /* The value of any of these variables is either 0 or 1 at all times. */
Janos Follath5e614ce2019-10-28 12:31:34 +00001287 unsigned cond, done, X_is_negative, Y_is_negative;
Janos Follathee6abce2019-09-05 14:47:19 +01001288
1289 MPI_VALIDATE_RET( X != NULL );
1290 MPI_VALIDATE_RET( Y != NULL );
1291 MPI_VALIDATE_RET( ret != NULL );
1292
1293 if( X->n != Y->n )
1294 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1295
1296 /*
Janos Follath73ba9ec2019-10-28 12:12:15 +00001297 * Set sign_N to 1 if N >= 0, 0 if N < 0.
1298 * We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0.
Janos Follathee6abce2019-09-05 14:47:19 +01001299 */
Janos Follath5e614ce2019-10-28 12:31:34 +00001300 X_is_negative = ( X->s & 2 ) >> 1;
1301 Y_is_negative = ( Y->s & 2 ) >> 1;
Janos Follath0e5532d2019-10-11 14:21:53 +01001302
1303 /*
1304 * If the signs are different, then the positive operand is the bigger.
Janos Follath5e614ce2019-10-28 12:31:34 +00001305 * That is if X is negative (X_is_negative == 1), then X < Y is true and it
1306 * is false if X is positive (X_is_negative == 0).
Janos Follath0e5532d2019-10-11 14:21:53 +01001307 */
Janos Follath5e614ce2019-10-28 12:31:34 +00001308 cond = ( X_is_negative ^ Y_is_negative );
1309 *ret = cond & X_is_negative;
Janos Follath0e5532d2019-10-11 14:21:53 +01001310
1311 /*
Janos Follathbb5147f2019-10-28 12:23:18 +00001312 * This is a constant-time function. We might have the result, but we still
Janos Follath0e5532d2019-10-11 14:21:53 +01001313 * need to go through the loop. Record if we have the result already.
1314 */
Janos Follathee6abce2019-09-05 14:47:19 +01001315 done = cond;
1316
1317 for( i = X->n; i > 0; i-- )
1318 {
1319 /*
Janos Follath30702422019-11-05 12:24:52 +00001320 * If Y->p[i - 1] < X->p[i - 1] then X < Y is true if and only if both
1321 * X and Y are negative.
Janos Follath0e5532d2019-10-11 14:21:53 +01001322 *
1323 * Again even if we can make a decision, we just mark the result and
1324 * the fact that we are done and continue looping.
Janos Follathee6abce2019-09-05 14:47:19 +01001325 */
Janos Follath30702422019-11-05 12:24:52 +00001326 cond = ct_lt_mpi_uint( Y->p[i - 1], X->p[i - 1] );
1327 *ret |= cond & ( 1 - done ) & X_is_negative;
Janos Follathc50e6d52019-10-28 12:37:21 +00001328 done |= cond;
Janos Follathee6abce2019-09-05 14:47:19 +01001329
1330 /*
Janos Follath30702422019-11-05 12:24:52 +00001331 * If X->p[i - 1] < Y->p[i - 1] then X < Y is true if and only if both
1332 * X and Y are positive.
Janos Follath0e5532d2019-10-11 14:21:53 +01001333 *
1334 * Again even if we can make a decision, we just mark the result and
1335 * the fact that we are done and continue looping.
Janos Follathee6abce2019-09-05 14:47:19 +01001336 */
Janos Follath30702422019-11-05 12:24:52 +00001337 cond = ct_lt_mpi_uint( X->p[i - 1], Y->p[i - 1] );
1338 *ret |= cond & ( 1 - done ) & ( 1 - X_is_negative );
Janos Follathc50e6d52019-10-28 12:37:21 +00001339 done |= cond;
Janos Follathee6abce2019-09-05 14:47:19 +01001340 }
1341
1342 return( 0 );
1343}
1344
Paul Bakker5121ce52009-01-03 21:22:43 +00001345/*
1346 * Compare signed values
1347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +00001349{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 mbedtls_mpi Y;
1351 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001352 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001353
1354 *p = ( z < 0 ) ? -z : z;
1355 Y.s = ( z < 0 ) ? -1 : 1;
1356 Y.n = 1;
1357 Y.p = p;
1358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 return( mbedtls_mpi_cmp_mpi( X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001360}
1361
1362/*
1363 * Unsigned addition: X = |A| + |B| (HAC 14.7)
1364 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001366{
Janos Follath24eed8d2019-11-22 13:21:35 +00001367 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001368 size_t i, j;
Janos Follath6c922682015-10-30 17:43:11 +01001369 mbedtls_mpi_uint *o, *p, c, tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +00001370 MPI_VALIDATE_RET( X != NULL );
1371 MPI_VALIDATE_RET( A != NULL );
1372 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001373
1374 if( X == B )
1375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 const mbedtls_mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001377 }
1378
1379 if( X != A )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
Paul Bakker9af723c2014-05-01 13:03:14 +02001381
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001382 /*
1383 * X should always be positive as a result of unsigned additions.
1384 */
1385 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001386
Paul Bakker23986e52011-04-24 08:57:21 +00001387 for( j = B->n; j > 0; j-- )
1388 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001389 break;
1390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001392
1393 o = B->p; p = X->p; c = 0;
1394
Janos Follath6c922682015-10-30 17:43:11 +01001395 /*
1396 * tmp is used because it might happen that p == o
1397 */
Paul Bakker23986e52011-04-24 08:57:21 +00001398 for( i = 0; i < j; i++, o++, p++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001399 {
Janos Follath6c922682015-10-30 17:43:11 +01001400 tmp= *o;
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 *p += c; c = ( *p < c );
Janos Follath6c922682015-10-30 17:43:11 +01001402 *p += tmp; c += ( *p < tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +00001403 }
1404
1405 while( c != 0 )
1406 {
1407 if( i >= X->n )
1408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001410 p = X->p + i;
1411 }
1412
Paul Bakker2d319fd2012-09-16 21:34:26 +00001413 *p += c; c = ( *p < c ); i++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001414 }
1415
1416cleanup:
1417
1418 return( ret );
1419}
1420
Gilles Peskine09ec10a2020-06-09 10:39:38 +02001421/**
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001422 * Helper for mbedtls_mpi subtraction.
1423 *
1424 * Calculate d - s where d and s have the same size.
1425 * This function operates modulo (2^ciL)^n and returns the carry
1426 * (1 if there was a wraparound, i.e. if `d < s`, and 0 otherwise).
1427 *
1428 * \param n Number of limbs of \p d and \p s.
1429 * \param[in,out] d On input, the left operand.
1430 * On output, the result of the subtraction:
Gilles Peskine09ec10a2020-06-09 10:39:38 +02001431 * \param[in] s The right operand.
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001432 *
1433 * \return 1 if `d < s`.
1434 * 0 if `d >= s`.
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,
1438 const mbedtls_mpi_uint *s )
Paul Bakker5121ce52009-01-03 21:22:43 +00001439{
Paul Bakker23986e52011-04-24 08:57:21 +00001440 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 mbedtls_mpi_uint c, z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001442
1443 for( i = c = 0; i < n; i++, s++, d++ )
1444 {
1445 z = ( *d < c ); *d -= c;
1446 c = ( *d < *s ) + z; *d -= *s;
1447 }
1448
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001449 return( c );
Paul Bakker5121ce52009-01-03 21:22:43 +00001450}
1451
1452/*
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001453 * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
Paul Bakker5121ce52009-01-03 21:22:43 +00001454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001456{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 mbedtls_mpi TB;
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001466
1467 if( X == B )
1468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001470 B = &TB;
1471 }
1472
1473 if( X != A )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001475
Paul Bakker1ef7a532009-06-20 10:50:55 +00001476 /*
Paul Bakker60b1d102013-10-29 10:02:51 +01001477 * X should always be positive as a result of unsigned subtractions.
Paul Bakker1ef7a532009-06-20 10:50:55 +00001478 */
1479 X->s = 1;
1480
Paul Bakker5121ce52009-01-03 21:22:43 +00001481 ret = 0;
1482
Paul Bakker23986e52011-04-24 08:57:21 +00001483 for( n = B->n; n > 0; n-- )
1484 if( B->p[n - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001485 break;
Gilles Peskinec8a91772021-01-27 22:30:43 +01001486 if( n > A->n )
1487 {
1488 /* B >= (2^ciL)^n > A */
1489 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1490 goto cleanup;
1491 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001492
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001493 carry = mpi_sub_hlp( n, X->p, B->p );
1494 if( carry != 0 )
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001495 {
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001496 /* Propagate the carry to the first nonzero limb of X. */
1497 for( ; n < X->n && X->p[n] == 0; n++ )
1498 --X->p[n];
1499 /* If we ran out of space for the carry, it means that the result
1500 * is negative. */
1501 if( n == X->n )
Gilles Peskine89b41302020-07-23 01:16:46 +02001502 {
1503 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1504 goto cleanup;
1505 }
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001506 --X->p[n];
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001507 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001508
1509cleanup:
1510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511 mbedtls_mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001512
1513 return( ret );
1514}
1515
1516/*
1517 * Signed addition: X = A + B
1518 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001520{
Hanno Becker73d7d792018-12-11 10:35:51 +00001521 int ret, s;
1522 MPI_VALIDATE_RET( X != NULL );
1523 MPI_VALIDATE_RET( A != NULL );
1524 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001525
Hanno Becker73d7d792018-12-11 10:35:51 +00001526 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001527 if( A->s * B->s < 0 )
1528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001532 X->s = s;
1533 }
1534 else
1535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001537 X->s = -s;
1538 }
1539 }
1540 else
1541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001543 X->s = s;
1544 }
1545
1546cleanup:
1547
1548 return( ret );
1549}
1550
1551/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001552 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001555{
Hanno Becker73d7d792018-12-11 10:35:51 +00001556 int ret, s;
1557 MPI_VALIDATE_RET( X != NULL );
1558 MPI_VALIDATE_RET( A != NULL );
1559 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001560
Hanno Becker73d7d792018-12-11 10:35:51 +00001561 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001562 if( A->s * B->s > 0 )
1563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001567 X->s = s;
1568 }
1569 else
1570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001572 X->s = -s;
1573 }
1574 }
1575 else
1576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001578 X->s = s;
1579 }
1580
1581cleanup:
1582
1583 return( ret );
1584}
1585
1586/*
1587 * Signed addition: X = A + b
1588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001590{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 mbedtls_mpi _B;
1592 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001593 MPI_VALIDATE_RET( X != NULL );
1594 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001595
1596 p[0] = ( b < 0 ) ? -b : b;
1597 _B.s = ( b < 0 ) ? -1 : 1;
1598 _B.n = 1;
1599 _B.p = p;
1600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 return( mbedtls_mpi_add_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001602}
1603
1604/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001605 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001606 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001608{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 mbedtls_mpi _B;
1610 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001611 MPI_VALIDATE_RET( X != NULL );
1612 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001613
1614 p[0] = ( b < 0 ) ? -b : b;
1615 _B.s = ( b < 0 ) ? -1 : 1;
1616 _B.n = 1;
1617 _B.p = p;
1618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 return( mbedtls_mpi_sub_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001620}
1621
1622/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 * Helper for mbedtls_mpi multiplication
Paul Bakkerfc4f46f2013-06-24 19:23:56 +02001624 */
1625static
1626#if defined(__APPLE__) && defined(__arm__)
1627/*
1628 * Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
1629 * appears to need this to prevent bad ARM code generation at -O3.
1630 */
1631__attribute__ ((noinline))
1632#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633void mpi_mul_hlp( size_t i, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d, mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001634{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635 mbedtls_mpi_uint c = 0, t = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001636
1637#if defined(MULADDC_HUIT)
1638 for( ; i >= 8; i -= 8 )
1639 {
1640 MULADDC_INIT
1641 MULADDC_HUIT
1642 MULADDC_STOP
1643 }
1644
1645 for( ; i > 0; i-- )
1646 {
1647 MULADDC_INIT
1648 MULADDC_CORE
1649 MULADDC_STOP
1650 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001651#else /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001652 for( ; i >= 16; i -= 16 )
1653 {
1654 MULADDC_INIT
1655 MULADDC_CORE MULADDC_CORE
1656 MULADDC_CORE MULADDC_CORE
1657 MULADDC_CORE MULADDC_CORE
1658 MULADDC_CORE MULADDC_CORE
1659
1660 MULADDC_CORE MULADDC_CORE
1661 MULADDC_CORE MULADDC_CORE
1662 MULADDC_CORE MULADDC_CORE
1663 MULADDC_CORE MULADDC_CORE
1664 MULADDC_STOP
1665 }
1666
1667 for( ; i >= 8; i -= 8 )
1668 {
1669 MULADDC_INIT
1670 MULADDC_CORE MULADDC_CORE
1671 MULADDC_CORE MULADDC_CORE
1672
1673 MULADDC_CORE MULADDC_CORE
1674 MULADDC_CORE MULADDC_CORE
1675 MULADDC_STOP
1676 }
1677
1678 for( ; i > 0; i-- )
1679 {
1680 MULADDC_INIT
1681 MULADDC_CORE
1682 MULADDC_STOP
1683 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001684#endif /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001685
1686 t++;
1687
1688 do {
1689 *d += c; c = ( *d < c ); d++;
1690 }
1691 while( c != 0 );
1692}
1693
1694/*
1695 * Baseline multiplication: X = A * B (HAC 14.12)
1696 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001698{
Janos Follath24eed8d2019-11-22 13:21:35 +00001699 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001700 size_t i, j;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 mbedtls_mpi TA, TB;
Hanno Becker73d7d792018-12-11 10:35:51 +00001702 MPI_VALIDATE_RET( X != NULL );
1703 MPI_VALIDATE_RET( A != NULL );
1704 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 if( X == A ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); A = &TA; }
1709 if( X == B ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); B = &TB; }
Paul Bakker5121ce52009-01-03 21:22:43 +00001710
Paul Bakker23986e52011-04-24 08:57:21 +00001711 for( i = A->n; i > 0; i-- )
1712 if( A->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001713 break;
1714
Paul Bakker23986e52011-04-24 08:57:21 +00001715 for( j = B->n; j > 0; j-- )
1716 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001717 break;
1718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + j ) );
1720 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001721
Alexey Skalozub8e75e682016-01-13 21:59:27 +02001722 for( ; j > 0; j-- )
1723 mpi_mul_hlp( i, A->p, X->p + j - 1, B->p[j - 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00001724
1725 X->s = A->s * B->s;
1726
1727cleanup:
1728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001730
1731 return( ret );
1732}
1733
1734/*
1735 * Baseline multiplication: X = A * b
1736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001738{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 mbedtls_mpi _B;
1740 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001741 MPI_VALIDATE_RET( X != NULL );
1742 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001743
1744 _B.s = 1;
1745 _B.n = 1;
1746 _B.p = p;
1747 p[0] = b;
1748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 return( mbedtls_mpi_mul_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001750}
1751
1752/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001753 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1754 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001755 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001756static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1,
1757 mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r )
Simon Butcher15b15d12015-11-26 19:35:03 +00001758{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001759#if defined(MBEDTLS_HAVE_UDBL)
1760 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001761#else
Simon Butcher9803d072016-01-03 00:24:34 +00001762 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
1763 const mbedtls_mpi_uint uint_halfword_mask = ( (mbedtls_mpi_uint) 1 << biH ) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001764 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1765 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001766 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001767#endif
1768
Simon Butcher15b15d12015-11-26 19:35:03 +00001769 /*
1770 * Check for overflow
1771 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001772 if( 0 == d || u1 >= d )
Simon Butcher15b15d12015-11-26 19:35:03 +00001773 {
Simon Butcherf5ba0452015-12-27 23:01:55 +00001774 if (r != NULL) *r = ~0;
Simon Butcher15b15d12015-11-26 19:35:03 +00001775
Simon Butcherf5ba0452015-12-27 23:01:55 +00001776 return ( ~0 );
Simon Butcher15b15d12015-11-26 19:35:03 +00001777 }
1778
1779#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001780 dividend = (mbedtls_t_udbl) u1 << biL;
1781 dividend |= (mbedtls_t_udbl) u0;
1782 quotient = dividend / d;
1783 if( quotient > ( (mbedtls_t_udbl) 1 << biL ) - 1 )
1784 quotient = ( (mbedtls_t_udbl) 1 << biL ) - 1;
1785
1786 if( r != NULL )
Simon Butcher9803d072016-01-03 00:24:34 +00001787 *r = (mbedtls_mpi_uint)( dividend - (quotient * d ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001788
1789 return (mbedtls_mpi_uint) quotient;
1790#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001791
1792 /*
1793 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1794 * Vol. 2 - Seminumerical Algorithms, Knuth
1795 */
1796
1797 /*
1798 * Normalize the divisor, d, and dividend, u0, u1
1799 */
1800 s = mbedtls_clz( d );
1801 d = d << s;
1802
1803 u1 = u1 << s;
Simon Butcher9803d072016-01-03 00:24:34 +00001804 u1 |= ( u0 >> ( biL - s ) ) & ( -(mbedtls_mpi_sint)s >> ( biL - 1 ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001805 u0 = u0 << s;
1806
1807 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001808 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001809
1810 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001811 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001812
1813 /*
1814 * Find the first quotient and remainder
1815 */
1816 q1 = u1 / d1;
1817 r0 = u1 - d1 * q1;
1818
1819 while( q1 >= radix || ( q1 * d0 > radix * r0 + u0_msw ) )
1820 {
1821 q1 -= 1;
1822 r0 += d1;
1823
1824 if ( r0 >= radix ) break;
1825 }
1826
Simon Butcherf5ba0452015-12-27 23:01:55 +00001827 rAX = ( u1 * radix ) + ( u0_msw - q1 * d );
Simon Butcher15b15d12015-11-26 19:35:03 +00001828 q0 = rAX / d1;
1829 r0 = rAX - q0 * d1;
1830
1831 while( q0 >= radix || ( q0 * d0 > radix * r0 + u0_lsw ) )
1832 {
1833 q0 -= 1;
1834 r0 += d1;
1835
1836 if ( r0 >= radix ) break;
1837 }
1838
1839 if (r != NULL)
Simon Butcherf5ba0452015-12-27 23:01:55 +00001840 *r = ( rAX * radix + u0_lsw - q0 * d ) >> s;
Simon Butcher15b15d12015-11-26 19:35:03 +00001841
1842 quotient = q1 * radix + q0;
1843
1844 return quotient;
1845#endif
1846}
1847
1848/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001850 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001851int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1852 const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001853{
Janos Follath24eed8d2019-11-22 13:21:35 +00001854 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001855 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 mbedtls_mpi X, Y, Z, T1, T2;
Alexander Kd19a1932019-11-01 18:20:42 +03001857 mbedtls_mpi_uint TP2[3];
Hanno Becker73d7d792018-12-11 10:35:51 +00001858 MPI_VALIDATE_RET( A != NULL );
1859 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 if( mbedtls_mpi_cmp_int( B, 0 ) == 0 )
1862 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Alexander K35d6d462019-10-31 14:46:45 +03001865 mbedtls_mpi_init( &T1 );
Alexander Kd19a1932019-11-01 18:20:42 +03001866 /*
1867 * Avoid dynamic memory allocations for constant-size T2.
1868 *
1869 * T2 is used for comparison only and the 3 limbs are assigned explicitly,
1870 * so nobody increase the size of the MPI and we're safe to use an on-stack
1871 * buffer.
1872 */
Alexander K35d6d462019-10-31 14:46:45 +03001873 T2.s = 1;
Alexander Kd19a1932019-11-01 18:20:42 +03001874 T2.n = sizeof( TP2 ) / sizeof( *TP2 );
1875 T2.p = TP2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 if( Q != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_lset( Q, 0 ) );
1880 if( R != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001881 return( 0 );
1882 }
1883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &X, A ) );
1885 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001886 X.s = Y.s = 1;
1887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &Z, A->n + 2 ) );
1889 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Z, 0 ) );
1890 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001891
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001892 k = mbedtls_mpi_bitlen( &Y ) % biL;
Paul Bakkerf9688572011-05-05 10:00:45 +00001893 if( k < biL - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001894 {
1895 k = biL - 1 - k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &X, k ) );
1897 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, k ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001898 }
1899 else k = 0;
1900
1901 n = X.n - 1;
1902 t = Y.n - 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 while( mbedtls_mpi_cmp_mpi( &X, &Y ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001906 {
1907 Z.p[n - t]++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001909 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001911
1912 for( i = n; i > t ; i-- )
1913 {
1914 if( X.p[i] >= Y.p[t] )
1915 Z.p[i - t - 1] = ~0;
1916 else
1917 {
Simon Butcher15b15d12015-11-26 19:35:03 +00001918 Z.p[i - t - 1] = mbedtls_int_div_int( X.p[i], X.p[i - 1],
1919 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001920 }
1921
Alexander K35d6d462019-10-31 14:46:45 +03001922 T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2];
1923 T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1];
1924 T2.p[2] = X.p[i];
1925
Paul Bakker5121ce52009-01-03 21:22:43 +00001926 Z.p[i - t - 1]++;
1927 do
1928 {
1929 Z.p[i - t - 1]--;
1930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T1, 0 ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001932 T1.p[0] = ( t < 1 ) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001933 T1.p[1] = Y.p[t];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001935 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936 while( mbedtls_mpi_cmp_mpi( &T1, &T2 ) > 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
1939 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1940 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942 if( mbedtls_mpi_cmp_int( &X, 0 ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &Y ) );
1945 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1946 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001947 Z.p[i - t - 1]--;
1948 }
1949 }
1950
1951 if( Q != NULL )
1952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( Q, &Z ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001954 Q->s = A->s * B->s;
1955 }
1956
1957 if( R != NULL )
1958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &X, k ) );
Paul Bakkerf02c5642012-11-13 10:25:21 +00001960 X.s = A->s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, &X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963 if( mbedtls_mpi_cmp_int( R, 0 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001964 R->s = 1;
1965 }
1966
1967cleanup:
1968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Alexander K35d6d462019-10-31 14:46:45 +03001970 mbedtls_mpi_free( &T1 );
Alexander Kd19a1932019-11-01 18:20:42 +03001971 mbedtls_platform_zeroize( TP2, sizeof( TP2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001972
1973 return( ret );
1974}
1975
1976/*
1977 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00001978 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001979int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R,
1980 const mbedtls_mpi *A,
1981 mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001982{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 mbedtls_mpi _B;
1984 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001985 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001986
1987 p[0] = ( b < 0 ) ? -b : b;
1988 _B.s = ( b < 0 ) ? -1 : 1;
1989 _B.n = 1;
1990 _B.p = p;
1991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 return( mbedtls_mpi_div_mpi( Q, R, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001993}
1994
1995/*
1996 * Modulo: R = A mod B
1997 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001999{
Janos Follath24eed8d2019-11-22 13:21:35 +00002000 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker73d7d792018-12-11 10:35:51 +00002001 MPI_VALIDATE_RET( R != NULL );
2002 MPI_VALIDATE_RET( A != NULL );
2003 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 if( mbedtls_mpi_cmp_int( B, 0 ) < 0 )
2006 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakkerce40a6d2009-06-23 19:46:08 +00002007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( NULL, R, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 while( mbedtls_mpi_cmp_int( R, 0 ) < 0 )
2011 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013 while( mbedtls_mpi_cmp_mpi( R, B ) >= 0 )
2014 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002015
2016cleanup:
2017
2018 return( ret );
2019}
2020
2021/*
2022 * Modulo: r = A mod b
2023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00002025{
Paul Bakker23986e52011-04-24 08:57:21 +00002026 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 mbedtls_mpi_uint x, y, z;
Hanno Becker73d7d792018-12-11 10:35:51 +00002028 MPI_VALIDATE_RET( r != NULL );
2029 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002030
2031 if( b == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00002033
2034 if( b < 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002036
2037 /*
2038 * handle trivial cases
2039 */
2040 if( b == 1 )
2041 {
2042 *r = 0;
2043 return( 0 );
2044 }
2045
2046 if( b == 2 )
2047 {
2048 *r = A->p[0] & 1;
2049 return( 0 );
2050 }
2051
2052 /*
2053 * general case
2054 */
Paul Bakker23986e52011-04-24 08:57:21 +00002055 for( i = A->n, y = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00002056 {
Paul Bakker23986e52011-04-24 08:57:21 +00002057 x = A->p[i - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 y = ( y << biH ) | ( x >> biH );
2059 z = y / b;
2060 y -= z * b;
2061
2062 x <<= biH;
2063 y = ( y << biH ) | ( x >> biH );
2064 z = y / b;
2065 y -= z * b;
2066 }
2067
Paul Bakkerce40a6d2009-06-23 19:46:08 +00002068 /*
2069 * If A is negative, then the current y represents a negative value.
2070 * Flipping it to the positive side.
2071 */
2072 if( A->s < 0 && y != 0 )
2073 y = b - y;
2074
Paul Bakker5121ce52009-01-03 21:22:43 +00002075 *r = y;
2076
2077 return( 0 );
2078}
2079
2080/*
2081 * Fast Montgomery initialization (thanks to Tom St Denis)
2082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083static void mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002084{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 mbedtls_mpi_uint x, m0 = N->p[0];
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01002086 unsigned int i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002087
2088 x = m0;
2089 x += ( ( m0 + 2 ) & 4 ) << 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002090
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01002091 for( i = biL; i >= 8; i /= 2 )
2092 x *= ( 2 - ( m0 * x ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002093
2094 *mm = ~x + 1;
2095}
2096
Gilles Peskine2a82f722020-06-04 15:00:49 +02002097/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
2098 *
2099 * \param[in,out] A One of the numbers to multiply.
Gilles Peskine221626f2020-06-08 22:37:50 +02002100 * It must have at least as many limbs as N
2101 * (A->n >= N->n), and any limbs beyond n are ignored.
Gilles Peskine2a82f722020-06-04 15:00:49 +02002102 * On successful completion, A contains the result of
2103 * the multiplication A * B * R^-1 mod N where
2104 * R = (2^ciL)^n.
2105 * \param[in] B One of the numbers to multiply.
2106 * It must be nonzero and must not have more limbs than N
2107 * (B->n <= N->n).
2108 * \param[in] N The modulo. N must be odd.
2109 * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
2110 * This is -N^-1 mod 2^ciL.
2111 * \param[in,out] T A bignum for temporary storage.
2112 * It must be at least twice the limb size of N plus 2
2113 * (T->n >= 2 * (N->n + 1)).
2114 * Its initial content is unused and
2115 * its final content is indeterminate.
2116 * Note that unlike the usual convention in the library
2117 * for `const mbedtls_mpi*`, the content of T can change.
Paul Bakker5121ce52009-01-03 21:22:43 +00002118 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002119static 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 +02002120 const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00002121{
Paul Bakker23986e52011-04-24 08:57:21 +00002122 size_t i, n, m;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 mbedtls_mpi_uint u0, u1, *d;
Paul Bakker5121ce52009-01-03 21:22:43 +00002124
2125 memset( T->p, 0, T->n * ciL );
2126
2127 d = T->p;
2128 n = N->n;
2129 m = ( B->n < n ) ? B->n : n;
2130
2131 for( i = 0; i < n; i++ )
2132 {
2133 /*
2134 * T = (T + u0*B + u1*N) / 2^biL
2135 */
2136 u0 = A->p[i];
2137 u1 = ( d[0] + u0 * B->p[0] ) * mm;
2138
2139 mpi_mul_hlp( m, B->p, d, u0 );
2140 mpi_mul_hlp( n, N->p, d, u1 );
2141
2142 *d++ = u0; d[n + 1] = 0;
2143 }
2144
Gilles Peskine221626f2020-06-08 22:37:50 +02002145 /* At this point, d is either the desired result or the desired result
2146 * plus N. We now potentially subtract N, avoiding leaking whether the
2147 * subtraction is performed through side channels. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002148
Gilles Peskine221626f2020-06-08 22:37:50 +02002149 /* Copy the n least significant limbs of d to A, so that
2150 * A = d if d < N (recall that N has n limbs). */
2151 memcpy( A->p, d, n * ciL );
Gilles Peskine09ec10a2020-06-09 10:39:38 +02002152 /* If d >= N then we want to set A to d - N. To prevent timing attacks,
Gilles Peskine221626f2020-06-08 22:37:50 +02002153 * do the calculation without using conditional tests. */
2154 /* Set d to d0 + (2^biL)^n - N where d0 is the current value of d. */
Gilles Peskine132c0972020-06-04 21:05:24 +02002155 d[n] += 1;
Gilles Peskinec097e9e2020-06-08 21:58:22 +02002156 d[n] -= mpi_sub_hlp( n, d, N->p );
Gilles Peskine221626f2020-06-08 22:37:50 +02002157 /* If d0 < N then d < (2^biL)^n
2158 * so d[n] == 0 and we want to keep A as it is.
2159 * If d0 >= N then d >= (2^biL)^n, and d <= (2^biL)^n + N < 2 * (2^biL)^n
2160 * so d[n] == 1 and we want to set A to the result of the subtraction
2161 * which is d - (2^biL)^n, i.e. the n least significant limbs of d.
2162 * This exactly corresponds to a conditional assignment. */
2163 mpi_safe_cond_assign( n, A->p, d, (unsigned char) d[n] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002164}
2165
2166/*
2167 * Montgomery reduction: A = A * R^-1 mod N
Gilles Peskine2a82f722020-06-04 15:00:49 +02002168 *
2169 * See mpi_montmul() regarding constraints and guarantees on the parameters.
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002171static void mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N,
2172 mbedtls_mpi_uint mm, const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00002173{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 mbedtls_mpi_uint z = 1;
2175 mbedtls_mpi U;
Paul Bakker5121ce52009-01-03 21:22:43 +00002176
Paul Bakker8ddb6452013-02-27 14:56:33 +01002177 U.n = U.s = (int) z;
Paul Bakker5121ce52009-01-03 21:22:43 +00002178 U.p = &z;
2179
Gilles Peskine4e91d472020-06-04 20:55:15 +02002180 mpi_montmul( A, &U, N, mm, T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002181}
2182
Manuel Pégourié-Gonnard92413ef2021-06-03 10:42:46 +02002183/*
2184 * Constant-flow boolean "equal" comparison:
2185 * return x == y
2186 *
2187 * This function can be used to write constant-time code by replacing branches
2188 * with bit operations - it can be used in conjunction with
2189 * mbedtls_ssl_cf_mask_from_bit().
2190 *
2191 * This function is implemented without using comparison operators, as those
2192 * might be translated to branches by some compilers on some platforms.
2193 */
2194static size_t mbedtls_mpi_cf_bool_eq( size_t x, size_t y )
2195{
2196 /* diff = 0 if x == y, non-zero otherwise */
2197 const size_t diff = x ^ y;
2198
2199 /* MSVC has a warning about unary minus on unsigned integer types,
2200 * but this is well-defined and precisely what we want to do here. */
2201#if defined(_MSC_VER)
2202#pragma warning( push )
2203#pragma warning( disable : 4146 )
2204#endif
2205
2206 /* diff_msb's most significant bit is equal to x != y */
2207 const size_t diff_msb = ( diff | -diff );
2208
2209#if defined(_MSC_VER)
2210#pragma warning( pop )
2211#endif
2212
2213 /* diff1 = (x != y) ? 1 : 0 */
2214 const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 );
2215
2216 return( 1 ^ diff1 );
2217}
2218
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01002219/**
2220 * Select an MPI from a table without leaking the index.
2221 *
2222 * This is functionally equivalent to mbedtls_mpi_copy(R, T[idx]) except it
2223 * reads the entire table in order to avoid leaking the value of idx to an
2224 * attacker able to observe memory access patterns.
2225 *
2226 * \param[out] R Where to write the selected MPI.
2227 * \param[in] T The table to read from.
2228 * \param[in] T_size The number of elements in the table.
2229 * \param[in] idx The index of the element to select;
2230 * this must satisfy 0 <= idx < T_size.
2231 *
2232 * \return \c 0 on success, or a negative error code.
2233 */
2234static int mpi_select( mbedtls_mpi *R, const mbedtls_mpi *T, size_t T_size, size_t idx )
2235{
2236 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2237
2238 for( size_t i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard92413ef2021-06-03 10:42:46 +02002239 {
2240 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( R, &T[i],
2241 mbedtls_mpi_cf_bool_eq( i, idx ) ) );
2242 }
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01002243
2244cleanup:
2245 return( ret );
2246}
2247
Paul Bakker5121ce52009-01-03 21:22:43 +00002248/*
2249 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
2250 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002251int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
2252 const mbedtls_mpi *E, const mbedtls_mpi *N,
2253 mbedtls_mpi *_RR )
Paul Bakker5121ce52009-01-03 21:22:43 +00002254{
Janos Follath24eed8d2019-11-22 13:21:35 +00002255 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002256 size_t wbits, wsize, one = 1;
2257 size_t i, j, nblimbs;
2258 size_t bufsize, nbits;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 mbedtls_mpi_uint ei, mm, state;
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01002260 mbedtls_mpi RR, T, W[ 1 << MBEDTLS_MPI_WINDOW_SIZE ], WW, Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00002261 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002262
Hanno Becker73d7d792018-12-11 10:35:51 +00002263 MPI_VALIDATE_RET( X != NULL );
2264 MPI_VALIDATE_RET( A != NULL );
2265 MPI_VALIDATE_RET( E != NULL );
2266 MPI_VALIDATE_RET( N != NULL );
2267
Hanno Becker8d1dd1b2017-09-28 11:02:24 +01002268 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 || ( N->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 if( mbedtls_mpi_cmp_int( E, 0 ) < 0 )
2272 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002273
Chris Jones9246d042020-11-25 15:12:39 +00002274 if( mbedtls_mpi_bitlen( E ) > MBEDTLS_MPI_MAX_BITS ||
2275 mbedtls_mpi_bitlen( N ) > MBEDTLS_MPI_MAX_BITS )
2276 return ( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2277
Paul Bakkerf6198c12012-05-16 08:02:29 +00002278 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002279 * Init temps and window size
2280 */
2281 mpi_montg_init( &mm, N );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &T );
2283 mbedtls_mpi_init( &Apos );
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01002284 mbedtls_mpi_init( &WW );
Paul Bakker5121ce52009-01-03 21:22:43 +00002285 memset( W, 0, sizeof( W ) );
2286
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002287 i = mbedtls_mpi_bitlen( E );
Paul Bakker5121ce52009-01-03 21:22:43 +00002288
2289 wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
2290 ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1;
2291
Peter Kolbuse6bcad32018-12-11 14:01:44 -06002292#if( MBEDTLS_MPI_WINDOW_SIZE < 6 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 if( wsize > MBEDTLS_MPI_WINDOW_SIZE )
2294 wsize = MBEDTLS_MPI_WINDOW_SIZE;
Peter Kolbuse6bcad32018-12-11 14:01:44 -06002295#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00002296
Paul Bakker5121ce52009-01-03 21:22:43 +00002297 j = N->n + 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
2299 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], j ) );
2300 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T, j * 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002301
2302 /*
Paul Bakker50546922012-05-19 08:40:49 +00002303 * Compensate for negative A (and correct at the end)
2304 */
2305 neg = ( A->s == -1 );
Paul Bakker50546922012-05-19 08:40:49 +00002306 if( neg )
2307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Apos, A ) );
Paul Bakker50546922012-05-19 08:40:49 +00002309 Apos.s = 1;
2310 A = &Apos;
2311 }
2312
2313 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002314 * If 1st call, pre-compute R^2 mod N
2315 */
2316 if( _RR == NULL || _RR->p == NULL )
2317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &RR, 1 ) );
2319 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &RR, N->n * 2 * biL ) );
2320 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &RR, &RR, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002321
2322 if( _RR != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 memcpy( _RR, &RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002324 }
2325 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 memcpy( &RR, _RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002327
2328 /*
2329 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
2330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 if( mbedtls_mpi_cmp_mpi( A, N ) >= 0 )
2332 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &W[1], A, N ) );
Paul Bakkerc2024f42014-01-23 20:38:35 +01002333 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[1], A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002335
Gilles Peskine4e91d472020-06-04 20:55:15 +02002336 mpi_montmul( &W[1], &RR, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002337
2338 /*
2339 * X = R^2 * R^-1 mod N = R mod N
2340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &RR ) );
Gilles Peskine4e91d472020-06-04 20:55:15 +02002342 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002343
2344 if( wsize > 1 )
2345 {
2346 /*
2347 * W[1 << (wsize - 1)] = W[1] ^ (wsize - 1)
2348 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002349 j = one << ( wsize - 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[j], N->n + 1 ) );
2352 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[j], &W[1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002353
2354 for( i = 0; i < wsize - 1; i++ )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002355 mpi_montmul( &W[j], &W[j], N, mm, &T );
Paul Bakker0d7702c2013-10-29 16:18:35 +01002356
Paul Bakker5121ce52009-01-03 21:22:43 +00002357 /*
2358 * W[i] = W[i - 1] * W[1]
2359 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002360 for( i = j + 1; i < ( one << wsize ); i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[i], N->n + 1 ) );
2363 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[i], &W[i - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002364
Gilles Peskine4e91d472020-06-04 20:55:15 +02002365 mpi_montmul( &W[i], &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002366 }
2367 }
2368
2369 nblimbs = E->n;
2370 bufsize = 0;
2371 nbits = 0;
2372 wbits = 0;
2373 state = 0;
2374
2375 while( 1 )
2376 {
2377 if( bufsize == 0 )
2378 {
Paul Bakker0d7702c2013-10-29 16:18:35 +01002379 if( nblimbs == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 break;
2381
Paul Bakker0d7702c2013-10-29 16:18:35 +01002382 nblimbs--;
2383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 bufsize = sizeof( mbedtls_mpi_uint ) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 }
2386
2387 bufsize--;
2388
2389 ei = (E->p[nblimbs] >> bufsize) & 1;
2390
2391 /*
2392 * skip leading 0s
2393 */
2394 if( ei == 0 && state == 0 )
2395 continue;
2396
2397 if( ei == 0 && state == 1 )
2398 {
2399 /*
2400 * out of window, square X
2401 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002402 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002403 continue;
2404 }
2405
2406 /*
2407 * add ei to current window
2408 */
2409 state = 2;
2410
2411 nbits++;
Paul Bakker66d5d072014-06-17 16:39:18 +02002412 wbits |= ( ei << ( wsize - nbits ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002413
2414 if( nbits == wsize )
2415 {
2416 /*
2417 * X = X^wsize R^-1 mod N
2418 */
2419 for( i = 0; i < wsize; i++ )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002420 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002421
2422 /*
2423 * X = X * W[wbits] R^-1 mod N
2424 */
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01002425 MBEDTLS_MPI_CHK( mpi_select( &WW, W, 1 << wsize, wbits ) );
2426 mpi_montmul( X, &WW, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002427
2428 state--;
2429 nbits = 0;
2430 wbits = 0;
2431 }
2432 }
2433
2434 /*
2435 * process the remaining bits
2436 */
2437 for( i = 0; i < nbits; i++ )
2438 {
Gilles Peskine4e91d472020-06-04 20:55:15 +02002439 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002440
2441 wbits <<= 1;
2442
Paul Bakker66d5d072014-06-17 16:39:18 +02002443 if( ( wbits & ( one << wsize ) ) != 0 )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002444 mpi_montmul( X, &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002445 }
2446
2447 /*
2448 * X = A^E * R * R^-1 mod N = A^E mod N
2449 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002450 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002451
Hanno Beckera4af1c42017-04-18 09:07:45 +01002452 if( neg && E->n != 0 && ( E->p[0] & 1 ) != 0 )
Paul Bakkerf6198c12012-05-16 08:02:29 +00002453 {
2454 X->s = -1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, N, X ) );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002456 }
2457
Paul Bakker5121ce52009-01-03 21:22:43 +00002458cleanup:
2459
Paul Bakker66d5d072014-06-17 16:39:18 +02002460 for( i = ( one << ( wsize - 1 ) ); i < ( one << wsize ); i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 mbedtls_mpi_free( &W[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 mbedtls_mpi_free( &W[1] ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &Apos );
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01002464 mbedtls_mpi_free( &WW );
Paul Bakker6c591fa2011-05-05 11:49:20 +00002465
Paul Bakker75a28602014-03-31 12:08:17 +02002466 if( _RR == NULL || _RR->p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
2469 return( ret );
2470}
2471
Paul Bakker5121ce52009-01-03 21:22:43 +00002472/*
2473 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
2474 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00002476{
Janos Follath24eed8d2019-11-22 13:21:35 +00002477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002478 size_t lz, lzt;
Alexander Ke8ad49f2019-08-16 16:16:07 +03002479 mbedtls_mpi TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
Hanno Becker73d7d792018-12-11 10:35:51 +00002481 MPI_VALIDATE_RET( G != NULL );
2482 MPI_VALIDATE_RET( A != NULL );
2483 MPI_VALIDATE_RET( B != NULL );
2484
Alexander Ke8ad49f2019-08-16 16:16:07 +03002485 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) );
2488 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 lz = mbedtls_mpi_lsb( &TA );
2491 lzt = mbedtls_mpi_lsb( &TB );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002492
Paul Bakker66d5d072014-06-17 16:39:18 +02002493 if( lzt < lz )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002494 lz = lzt;
2495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, lz ) );
2497 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, lz ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002498
Paul Bakker5121ce52009-01-03 21:22:43 +00002499 TA.s = TB.s = 1;
2500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 while( mbedtls_mpi_cmp_int( &TA, 0 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, mbedtls_mpi_lsb( &TA ) ) );
2504 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, mbedtls_mpi_lsb( &TB ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506 if( mbedtls_mpi_cmp_mpi( &TA, &TB ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TA, &TA, &TB ) );
2509 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 }
2511 else
2512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TB, &TB, &TA ) );
2514 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002515 }
2516 }
2517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &TB, lz ) );
2519 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( G, &TB ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002520
2521cleanup:
2522
Alexander Ke8ad49f2019-08-16 16:16:07 +03002523 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002524
2525 return( ret );
2526}
2527
Paul Bakker33dc46b2014-04-30 16:11:39 +02002528/*
2529 * Fill X with size bytes of random.
2530 *
2531 * Use a temporary bytes representation to make sure the result is the same
Paul Bakkerc37b0ac2014-05-01 14:19:23 +02002532 * regardless of the platform endianness (useful when f_rng is actually
Paul Bakker33dc46b2014-04-30 16:11:39 +02002533 * deterministic, eg for tests).
2534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002536 int (*f_rng)(void *, unsigned char *, size_t),
2537 void *p_rng )
Paul Bakker287781a2011-03-26 13:18:49 +00002538{
Janos Follath24eed8d2019-11-22 13:21:35 +00002539 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6dab6202019-01-02 16:42:29 +00002540 size_t const limbs = CHARS_TO_LIMBS( size );
Hanno Beckerda1655a2017-10-18 14:21:44 +01002541 size_t const overhead = ( limbs * ciL ) - size;
2542 unsigned char *Xp;
2543
Hanno Becker8ce11a32018-12-19 16:18:52 +00002544 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002545 MPI_VALIDATE_RET( f_rng != NULL );
Paul Bakker33dc46b2014-04-30 16:11:39 +02002546
Hanno Beckerda1655a2017-10-18 14:21:44 +01002547 /* Ensure that target MPI has exactly the necessary number of limbs */
2548 if( X->n != limbs )
2549 {
2550 mbedtls_mpi_free( X );
2551 mbedtls_mpi_init( X );
2552 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, limbs ) );
2553 }
2554 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker287781a2011-03-26 13:18:49 +00002555
Hanno Beckerda1655a2017-10-18 14:21:44 +01002556 Xp = (unsigned char*) X->p;
Gilles Peskine436400e2020-11-25 16:15:14 +01002557 MBEDTLS_MPI_CHK( f_rng( p_rng, Xp + overhead, size ) );
Hanno Beckerda1655a2017-10-18 14:21:44 +01002558
Hanno Becker2be8a552018-10-25 12:40:09 +01002559 mpi_bigendian_to_host( X->p, limbs );
Paul Bakker287781a2011-03-26 13:18:49 +00002560
2561cleanup:
2562 return( ret );
2563}
2564
Paul Bakker5121ce52009-01-03 21:22:43 +00002565/*
2566 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2567 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002569{
Janos Follath24eed8d2019-11-22 13:21:35 +00002570 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Hanno Becker73d7d792018-12-11 10:35:51 +00002572 MPI_VALIDATE_RET( X != NULL );
2573 MPI_VALIDATE_RET( A != NULL );
2574 MPI_VALIDATE_RET( N != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002575
Hanno Becker4bcb4912017-04-18 15:49:39 +01002576 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TU ); mbedtls_mpi_init( &U1 ); mbedtls_mpi_init( &U2 );
2580 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &TB ); mbedtls_mpi_init( &TV );
2581 mbedtls_mpi_init( &V1 ); mbedtls_mpi_init( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, A, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 goto cleanup;
2589 }
2590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &TA, A, N ) );
2592 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TU, &TA ) );
2593 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, N ) );
2594 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TV, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U1, 1 ) );
2597 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U2, 0 ) );
2598 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V1, 0 ) );
2599 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002600
2601 do
2602 {
2603 while( ( TU.p[0] & 1 ) == 0 )
2604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TU, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002606
2607 if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
2608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &U1, &U1, &TB ) );
2610 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002611 }
2612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U1, 1 ) );
2614 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 }
2616
2617 while( ( TV.p[0] & 1 ) == 0 )
2618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TV, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002620
2621 if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
2622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, &TB ) );
2624 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 }
2626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V1, 1 ) );
2628 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002629 }
2630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 if( mbedtls_mpi_cmp_mpi( &TU, &TV ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TU, &TU, &TV ) );
2634 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U1, &U1, &V1 ) );
2635 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &V2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002636 }
2637 else
2638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TV, &TV, &TU ) );
2640 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, &U1 ) );
2641 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &U2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002642 }
2643 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644 while( mbedtls_mpi_cmp_int( &TU, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 while( mbedtls_mpi_cmp_int( &V1, 0 ) < 0 )
2647 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649 while( mbedtls_mpi_cmp_mpi( &V1, N ) >= 0 )
2650 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &V1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002653
2654cleanup:
2655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TU ); mbedtls_mpi_free( &U1 ); mbedtls_mpi_free( &U2 );
2657 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TV );
2658 mbedtls_mpi_free( &V1 ); mbedtls_mpi_free( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
2660 return( ret );
2661}
2662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002663#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002664
Paul Bakker5121ce52009-01-03 21:22:43 +00002665static const int small_prime[] =
2666{
2667 3, 5, 7, 11, 13, 17, 19, 23,
2668 29, 31, 37, 41, 43, 47, 53, 59,
2669 61, 67, 71, 73, 79, 83, 89, 97,
2670 101, 103, 107, 109, 113, 127, 131, 137,
2671 139, 149, 151, 157, 163, 167, 173, 179,
2672 181, 191, 193, 197, 199, 211, 223, 227,
2673 229, 233, 239, 241, 251, 257, 263, 269,
2674 271, 277, 281, 283, 293, 307, 311, 313,
2675 317, 331, 337, 347, 349, 353, 359, 367,
2676 373, 379, 383, 389, 397, 401, 409, 419,
2677 421, 431, 433, 439, 443, 449, 457, 461,
2678 463, 467, 479, 487, 491, 499, 503, 509,
2679 521, 523, 541, 547, 557, 563, 569, 571,
2680 577, 587, 593, 599, 601, 607, 613, 617,
2681 619, 631, 641, 643, 647, 653, 659, 661,
2682 673, 677, 683, 691, 701, 709, 719, 727,
2683 733, 739, 743, 751, 757, 761, 769, 773,
2684 787, 797, 809, 811, 821, 823, 827, 829,
2685 839, 853, 857, 859, 863, 877, 881, 883,
2686 887, 907, 911, 919, 929, 937, 941, 947,
2687 953, 967, 971, 977, 983, 991, 997, -103
2688};
2689
2690/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002691 * Small divisors test (X must be positive)
2692 *
2693 * Return values:
2694 * 0: no small factor (possible prime, more tests needed)
2695 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002697 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002698 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699static int mpi_check_small_factors( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +00002700{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002701 int ret = 0;
2702 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703 mbedtls_mpi_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +00002704
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 if( ( X->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
2708 for( i = 0; small_prime[i] > 0; i++ )
2709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710 if( mbedtls_mpi_cmp_int( X, small_prime[i] ) <= 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002711 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, small_prime[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002714
2715 if( r == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002717 }
2718
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002719cleanup:
2720 return( ret );
2721}
2722
2723/*
2724 * Miller-Rabin pseudo-primality test (HAC 4.24)
2725 */
Janos Follathda31fa12018-09-03 14:45:23 +01002726static int mpi_miller_rabin( const mbedtls_mpi *X, size_t rounds,
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002727 int (*f_rng)(void *, unsigned char *, size_t),
2728 void *p_rng )
2729{
Pascal Junodb99183d2015-03-11 16:49:45 +01002730 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002731 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002733
Hanno Becker8ce11a32018-12-19 16:18:52 +00002734 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002735 MPI_VALIDATE_RET( f_rng != NULL );
2736
2737 mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R );
2738 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 mbedtls_mpi_init( &RR );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002740
Paul Bakker5121ce52009-01-03 21:22:43 +00002741 /*
2742 * W = |X| - 1
2743 * R = W >> lsb( W )
2744 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &W, X, 1 ) );
2746 s = mbedtls_mpi_lsb( &W );
2747 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R, &W ) );
2748 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &R, s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002749
Janos Follathda31fa12018-09-03 14:45:23 +01002750 for( i = 0; i < rounds; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002751 {
2752 /*
2753 * pick a random A, 1 < A < |X| - 1
2754 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002755 count = 0;
2756 do {
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002757 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
Pascal Junodb99183d2015-03-11 16:49:45 +01002758
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002759 j = mbedtls_mpi_bitlen( &A );
2760 k = mbedtls_mpi_bitlen( &W );
Pascal Junodb99183d2015-03-11 16:49:45 +01002761 if (j > k) {
Darryl Greene3f95ed2018-10-02 13:21:35 +01002762 A.p[A.n - 1] &= ( (mbedtls_mpi_uint) 1 << ( k - ( A.n - 1 ) * biL - 1 ) ) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002763 }
2764
2765 if (count++ > 30) {
Jens Wiklanderf08aa3e2019-01-17 13:30:57 +01002766 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2767 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002768 }
2769
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002770 } while ( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 ||
2771 mbedtls_mpi_cmp_int( &A, 1 ) <= 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002772
2773 /*
2774 * A = A^R mod |X|
2775 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &A, &A, &R, X, &RR ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778 if( mbedtls_mpi_cmp_mpi( &A, &W ) == 0 ||
2779 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002780 continue;
2781
2782 j = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 while( j < s && mbedtls_mpi_cmp_mpi( &A, &W ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002784 {
2785 /*
2786 * A = A * A mod |X|
2787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &A, &A ) );
2789 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &A, &T, X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791 if( mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002792 break;
2793
2794 j++;
2795 }
2796
2797 /*
2798 * not prime if A != |X| - 1 or A == 1
2799 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800 if( mbedtls_mpi_cmp_mpi( &A, &W ) != 0 ||
2801 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002804 break;
2805 }
2806 }
2807
2808cleanup:
Hanno Becker73d7d792018-12-11 10:35:51 +00002809 mbedtls_mpi_free( &W ); mbedtls_mpi_free( &R );
2810 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002812
2813 return( ret );
2814}
2815
2816/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002817 * Pseudo-primality test: small factors, then Miller-Rabin
2818 */
Janos Follatha0b67c22018-09-18 14:48:23 +01002819int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds,
2820 int (*f_rng)(void *, unsigned char *, size_t),
2821 void *p_rng )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002822{
Janos Follath24eed8d2019-11-22 13:21:35 +00002823 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824 mbedtls_mpi XX;
Hanno Becker8ce11a32018-12-19 16:18:52 +00002825 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002826 MPI_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002827
2828 XX.s = 1;
2829 XX.n = X->n;
2830 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832 if( mbedtls_mpi_cmp_int( &XX, 0 ) == 0 ||
2833 mbedtls_mpi_cmp_int( &XX, 1 ) == 0 )
2834 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 if( mbedtls_mpi_cmp_int( &XX, 2 ) == 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002837 return( 0 );
2838
2839 if( ( ret = mpi_check_small_factors( &XX ) ) != 0 )
2840 {
2841 if( ret == 1 )
2842 return( 0 );
2843
2844 return( ret );
2845 }
2846
Janos Follathda31fa12018-09-03 14:45:23 +01002847 return( mpi_miller_rabin( &XX, rounds, f_rng, p_rng ) );
Janos Follathf301d232018-08-14 13:34:01 +01002848}
2849
Janos Follatha0b67c22018-09-18 14:48:23 +01002850#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Janos Follathf301d232018-08-14 13:34:01 +01002851/*
2852 * Pseudo-primality test, error probability 2^-80
2853 */
2854int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
2855 int (*f_rng)(void *, unsigned char *, size_t),
2856 void *p_rng )
2857{
Hanno Becker8ce11a32018-12-19 16:18:52 +00002858 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002859 MPI_VALIDATE_RET( f_rng != NULL );
2860
Janos Follatha0b67c22018-09-18 14:48:23 +01002861 /*
2862 * In the past our key generation aimed for an error rate of at most
2863 * 2^-80. Since this function is deprecated, aim for the same certainty
2864 * here as well.
2865 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002866 return( mbedtls_mpi_is_prime_ext( X, 40, f_rng, p_rng ) );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002867}
Janos Follatha0b67c22018-09-18 14:48:23 +01002868#endif
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002869
2870/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002871 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002872 *
Janos Follathf301d232018-08-14 13:34:01 +01002873 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2874 * be either 1024 bits or 1536 bits long, and flags must contain
2875 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002876 */
Janos Follath7c025a92018-08-14 11:08:41 +01002877int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002878 int (*f_rng)(void *, unsigned char *, size_t),
2879 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002880{
Jethro Beekman66689272018-02-14 19:24:10 -08002881#ifdef MBEDTLS_HAVE_INT64
2882// ceil(2^63.5)
2883#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2884#else
2885// ceil(2^31.5)
2886#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2887#endif
2888 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002889 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002890 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891 mbedtls_mpi_uint r;
2892 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002893
Hanno Becker8ce11a32018-12-19 16:18:52 +00002894 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002895 MPI_VALIDATE_RET( f_rng != NULL );
2896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 if( nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS )
2898 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900 mbedtls_mpi_init( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002901
2902 n = BITS_TO_LIMBS( nbits );
2903
Janos Follathda31fa12018-09-03 14:45:23 +01002904 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR ) == 0 )
2905 {
2906 /*
2907 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2908 */
2909 rounds = ( ( nbits >= 1300 ) ? 2 : ( nbits >= 850 ) ? 3 :
2910 ( nbits >= 650 ) ? 4 : ( nbits >= 350 ) ? 8 :
2911 ( nbits >= 250 ) ? 12 : ( nbits >= 150 ) ? 18 : 27 );
2912 }
2913 else
2914 {
2915 /*
2916 * 2^-100 error probability, number of rounds computed based on HAC,
2917 * fact 4.48
2918 */
2919 rounds = ( ( nbits >= 1450 ) ? 4 : ( nbits >= 1150 ) ? 5 :
2920 ( nbits >= 1000 ) ? 6 : ( nbits >= 850 ) ? 7 :
2921 ( nbits >= 750 ) ? 8 : ( nbits >= 500 ) ? 13 :
2922 ( nbits >= 250 ) ? 28 : ( nbits >= 150 ) ? 40 : 51 );
2923 }
2924
Jethro Beekman66689272018-02-14 19:24:10 -08002925 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002926 {
Jethro Beekman66689272018-02-14 19:24:10 -08002927 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
2928 /* make sure generated number is at least (nbits-1)+0.5 bits (FIPS 186-4 §B.3.3 steps 4.4, 5.5) */
2929 if( X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2 ) continue;
2930
2931 k = n * biL;
2932 if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits ) );
2933 X->p[0] |= 1;
2934
Janos Follath7c025a92018-08-14 11:08:41 +01002935 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002936 {
Janos Follatha0b67c22018-09-18 14:48:23 +01002937 ret = mbedtls_mpi_is_prime_ext( X, rounds, f_rng, p_rng );
Jethro Beekman66689272018-02-14 19:24:10 -08002938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002940 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002941 }
Jethro Beekman66689272018-02-14 19:24:10 -08002942 else
Paul Bakker5121ce52009-01-03 21:22:43 +00002943 {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002944 /*
Jethro Beekman66689272018-02-14 19:24:10 -08002945 * An necessary condition for Y and X = 2Y + 1 to be prime
2946 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2947 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002948 */
Jethro Beekman66689272018-02-14 19:24:10 -08002949
2950 X->p[0] |= 2;
2951
2952 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, 3 ) );
2953 if( r == 0 )
2954 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 8 ) );
2955 else if( r == 1 )
2956 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 4 ) );
2957
2958 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
2959 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, X ) );
2960 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, 1 ) );
2961
2962 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002963 {
Jethro Beekman66689272018-02-14 19:24:10 -08002964 /*
2965 * First, check small factors for X and Y
2966 * before doing Miller-Rabin on any of them
2967 */
2968 if( ( ret = mpi_check_small_factors( X ) ) == 0 &&
2969 ( ret = mpi_check_small_factors( &Y ) ) == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002970 ( ret = mpi_miller_rabin( X, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002971 == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002972 ( ret = mpi_miller_rabin( &Y, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002973 == 0 )
Jethro Beekman66689272018-02-14 19:24:10 -08002974 goto cleanup;
2975
2976 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
2977 goto cleanup;
2978
2979 /*
2980 * Next candidates. We want to preserve Y = (X-1) / 2 and
2981 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2982 * so up Y by 6 and X by 12.
2983 */
2984 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 12 ) );
2985 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &Y, &Y, 6 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002986 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002987 }
2988 }
2989
2990cleanup:
2991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 mbedtls_mpi_free( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002993
2994 return( ret );
2995}
2996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003000
Paul Bakker23986e52011-04-24 08:57:21 +00003001#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003002
3003static const int gcd_pairs[GCD_PAIR_COUNT][3] =
3004{
3005 { 693, 609, 21 },
3006 { 1764, 868, 28 },
3007 { 768454923, 542167814, 1 }
3008};
3009
Paul Bakker5121ce52009-01-03 21:22:43 +00003010/*
3011 * Checkup routine
3012 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013int mbedtls_mpi_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00003014{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003015 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00003017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); mbedtls_mpi_init( &X );
3019 mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &U ); mbedtls_mpi_init( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00003020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &A, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003022 "EFE021C2645FD1DC586E69184AF4A31E" \
3023 "D5F53E93B5F123FA41680867BA110131" \
3024 "944FE7952E2517337780CB0DB80E61AA" \
3025 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6" ) );
3026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003027 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003028 "B2E7EFD37075B9F03FF989C7C5051C20" \
3029 "34D2A323810251127E7BF8625A4F49A5" \
3030 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
3031 "5B5C25763222FEFCCFC38B832366C29E" ) );
3032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003033 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &N, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003034 "0066A198186C18C10B2F5ED9B522752A" \
3035 "9830B69916E535C8F047518A889A43A5" \
3036 "94B6BED27A168D31D4A52F88925AA8F5" ) );
3037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003038 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003041 "602AB7ECA597A3D6B56FF9829A5E8B85" \
3042 "9E857EA95A03512E2BAE7391688D264A" \
3043 "A5663B0341DB9CCFD2C4C5F421FEC814" \
3044 "8001B72E848A38CAE1C65F78E56ABDEF" \
3045 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
3046 "ECF677152EF804370C1A305CAF3B5BF1" \
3047 "30879B56C61DE584A0F53A2447A51E" ) );
3048
3049 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003050 mbedtls_printf( " MPI test #1 (mul_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003053 {
3054 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003056
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003057 ret = 1;
3058 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003059 }
3060
3061 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &X, &Y, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003067 "256567336059E52CAE22925474705F39A94" ) );
3068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003069 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &V, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003070 "6613F26162223DF488E9CD48CC132C7A" \
3071 "0AC93C701B001B092E4E5B9F73BCD27B" \
3072 "9EE50D0657C77F374E903CDFA4C642" ) );
3073
3074 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075 mbedtls_printf( " MPI test #2 (div_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ||
3078 mbedtls_mpi_cmp_mpi( &Y, &V ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003079 {
3080 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003082
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003083 ret = 1;
3084 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003085 }
3086
3087 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &X, &A, &E, &N, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003093 "36E139AEA55215609D2816998ED020BB" \
3094 "BD96C37890F65171D948E9BC7CBAA4D9" \
3095 "325D24D6A3C12710F10A09FA08AB87" ) );
3096
3097 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098 mbedtls_printf( " MPI test #3 (exp_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003100 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003101 {
3102 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003104
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003105 ret = 1;
3106 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003107 }
3108
3109 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003115 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
3116 "C3DBA76456363A10869622EAC2DD84EC" \
3117 "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) );
3118
3119 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120 mbedtls_printf( " MPI test #4 (inv_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003123 {
3124 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003126
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003127 ret = 1;
3128 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003129 }
3130
3131 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003133
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003134 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135 mbedtls_printf( " MPI test #5 (simple gcd): " );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003136
Paul Bakker66d5d072014-06-17 16:39:18 +02003137 for( i = 0; i < GCD_PAIR_COUNT; i++ )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &X, gcd_pairs[i][0] ) );
3140 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Y, gcd_pairs[i][1] ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &A, &X, &Y ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144 if( mbedtls_mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 )
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003145 {
3146 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147 mbedtls_printf( "failed at %d\n", i );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003148
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003149 ret = 1;
3150 goto cleanup;
3151 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003152 }
3153
3154 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 mbedtls_printf( "passed\n" );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003156
Paul Bakker5121ce52009-01-03 21:22:43 +00003157cleanup:
3158
3159 if( ret != 0 && verbose != 0 )
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003160 mbedtls_printf( "Unexpected error, return code = %08X\n", (unsigned int) ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &X );
3163 mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &U ); mbedtls_mpi_free( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00003164
3165 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003166 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003167
3168 return( ret );
3169}
3170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173#endif /* MBEDTLS_BIGNUM_C */