blob: 8658ba395b664863464a9546c68eb322278271a0 [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"
Chris Jones4c5819c2021-03-03 17:45:34 +000041#include "bn_mul.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050042#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000043#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Rich Evans00ab4702015-02-06 13:43:58 +000045#include <string.h>
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020049#else
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <stdio.h>
51#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#define mbedtls_printf printf
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020053#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020055#endif
56
Hanno Becker73d7d792018-12-11 10:35:51 +000057#define MPI_VALIDATE_RET( cond ) \
58 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA )
59#define MPI_VALIDATE( cond ) \
60 MBEDTLS_INTERNAL_VALIDATE( cond )
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
Paul Bakker5121ce52009-01-03 21:22:43 +000063#define biL (ciL << 3) /* bits in limb */
64#define biH (ciL << 2) /* half limb size */
65
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +010066#define MPI_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
67
Paul Bakker5121ce52009-01-03 21:22:43 +000068/*
69 * Convert between bits/chars and number of limbs
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +020070 * Divide first in order to avoid potential overflows
Paul Bakker5121ce52009-01-03 21:22:43 +000071 */
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +020072#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
73#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050075/* Implementation that should never be optimized out by the compiler */
Andres Amaya Garcia6698d2f2018-04-24 08:39:07 -050076static void mbedtls_mpi_zeroize( mbedtls_mpi_uint *v, size_t n )
77{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050078 mbedtls_platform_zeroize( v, ciL * n );
79}
80
Paul Bakker5121ce52009-01-03 21:22:43 +000081/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000082 * Initialize one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084void mbedtls_mpi_init( mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +000085{
Hanno Becker73d7d792018-12-11 10:35:51 +000086 MPI_VALIDATE( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +000087
Paul Bakker6c591fa2011-05-05 11:49:20 +000088 X->s = 1;
89 X->n = 0;
90 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000091}
92
93/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000094 * Unallocate one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096void mbedtls_mpi_free( mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +000097{
Paul Bakker6c591fa2011-05-05 11:49:20 +000098 if( X == NULL )
99 return;
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Paul Bakker6c591fa2011-05-05 11:49:20 +0000101 if( X->p != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 {
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200103 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 }
106
Paul Bakker6c591fa2011-05-05 11:49:20 +0000107 X->s = 1;
108 X->n = 0;
109 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000110}
111
112/*
113 * Enlarge to the specified number of limbs
114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000116{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_mpi_uint *p;
Hanno Becker73d7d792018-12-11 10:35:51 +0000118 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200121 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Paul Bakkerf9688572011-05-05 10:00:45 +0000122
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 if( X->n < nblimbs )
124 {
Simon Butcher29176892016-05-20 00:19:09 +0100125 if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( nblimbs, ciL ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200126 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 if( X->p != NULL )
129 {
130 memcpy( p, X->p, X->n * ciL );
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200131 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 }
134
135 X->n = nblimbs;
136 X->p = p;
137 }
138
139 return( 0 );
140}
141
142/*
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100143 * Resize down as much as possible,
144 * while keeping at least the specified number of limbs
145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100147{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_mpi_uint *p;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100149 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000150 MPI_VALIDATE_RET( X != NULL );
151
152 if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
153 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100154
Gilles Peskinee2f563e2020-01-20 21:17:43 +0100155 /* Actually resize up if there are currently fewer than nblimbs limbs. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100156 if( X->n <= nblimbs )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 return( mbedtls_mpi_grow( X, nblimbs ) );
Gilles Peskine322752b2020-01-21 13:59:51 +0100158 /* After this point, then X->n > nblimbs and in particular X->n > 0. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100159
160 for( i = X->n - 1; i > 0; i-- )
161 if( X->p[i] != 0 )
162 break;
163 i++;
164
165 if( i < nblimbs )
166 i = nblimbs;
167
Simon Butcher29176892016-05-20 00:19:09 +0100168 if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( i, ciL ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200169 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100170
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100171 if( X->p != NULL )
172 {
173 memcpy( p, X->p, i * ciL );
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200174 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_free( X->p );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100176 }
177
178 X->n = i;
179 X->p = p;
180
181 return( 0 );
182}
183
Gilles Peskineed32b572021-06-02 22:17:52 +0200184/* Resize X to have exactly n limbs and set it to 0. */
185static int mbedtls_mpi_resize_clear( mbedtls_mpi *X, size_t limbs )
186{
187 if( limbs == 0 )
188 {
189 mbedtls_mpi_free( X );
190 return( 0 );
191 }
192 else if( X->n == limbs )
193 {
194 memset( X->p, 0, limbs * ciL );
195 X->s = 1;
196 return( 0 );
197 }
198 else
199 {
200 mbedtls_mpi_free( X );
201 return( mbedtls_mpi_grow( X, limbs ) );
202 }
203}
204
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100205/*
Gilles Peskine3da1a8f2021-06-08 23:17:42 +0200206 * Copy the contents of Y into X.
207 *
208 * This function is not constant-time. Leading zeros in Y may be removed.
209 *
210 * Ensure that X does not shrink. This is not guaranteed by the public API,
211 * but some code in the bignum module relies on this property, for example
212 * in mbedtls_mpi_exp_mod().
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000215{
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100216 int ret = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000217 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000218 MPI_VALIDATE_RET( X != NULL );
219 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
221 if( X == Y )
222 return( 0 );
223
Gilles Peskinedb420622020-01-20 21:12:50 +0100224 if( Y->n == 0 )
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200225 {
Gilles Peskine3da1a8f2021-06-08 23:17:42 +0200226 if( X->n != 0 )
227 {
228 X->s = 1;
229 memset( X->p, 0, X->n * ciL );
230 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200231 return( 0 );
232 }
233
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 for( i = Y->n - 1; i > 0; i-- )
235 if( Y->p[i] != 0 )
236 break;
237 i++;
238
239 X->s = Y->s;
240
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100241 if( X->n < i )
242 {
243 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i ) );
244 }
245 else
246 {
247 memset( X->p + i, 0, ( X->n - i ) * ciL );
248 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000249
Paul Bakker5121ce52009-01-03 21:22:43 +0000250 memcpy( X->p, Y->p, i * ciL );
251
252cleanup:
253
254 return( ret );
255}
256
257/*
258 * Swap the contents of X and Y
259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000261{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000263 MPI_VALIDATE( X != NULL );
264 MPI_VALIDATE( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 memcpy( &T, X, sizeof( mbedtls_mpi ) );
267 memcpy( X, Y, sizeof( mbedtls_mpi ) );
268 memcpy( Y, &T, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000269}
270
Manuel Pégourié-Gonnard3ae4ae42021-06-07 09:51:00 +0200271/**
272 * Select between two sign values in constant-time.
273 *
274 * This is functionally equivalent to second ? a : b but uses only bit
275 * operations in order to avoid branches.
276 *
277 * \param[in] a The first sign; must be either +1 or -1.
278 * \param[in] b The second sign; must be either +1 or -1.
279 * \param[in] second Must be either 1 (return b) or 0 (return a).
280 *
281 * \return The selected sign value.
282 */
283static int mpi_safe_cond_select_sign( int a, int b, unsigned char second )
284{
285 /* In order to avoid questions about what we can reasonnably assume about
286 * the representations of signed integers, move everything to unsigned
287 * by taking advantage of the fact that a and b are either +1 or -1. */
288 unsigned ua = a + 1;
289 unsigned ub = b + 1;
290
Manuel Pégourié-Gonnard31ec1d72021-06-10 09:36:41 +0200291 /* second was 0 or 1, mask is 0 or 2 as are ua and ub */
292 const unsigned mask = second << 1;
Manuel Pégourié-Gonnard3ae4ae42021-06-07 09:51:00 +0200293
294 /* select ua or ub */
295 unsigned ur = ( ua & ~mask ) | ( ub & mask );
296
297 /* ur is now 0 or 2, convert back to -1 or +1 */
298 return( (int) ur - 1 );
299}
300
Paul Bakker5121ce52009-01-03 21:22:43 +0000301/*
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200302 * Conditionally assign dest = src, without leaking information
303 * about whether the assignment was made or not.
304 * dest and src must be arrays of limbs of size n.
305 * assign must be 0 or 1.
306 */
307static void mpi_safe_cond_assign( size_t n,
308 mbedtls_mpi_uint *dest,
309 const mbedtls_mpi_uint *src,
310 unsigned char assign )
311{
312 size_t i;
Manuel Pégourié-Gonnard5ada7a82021-05-31 11:48:45 +0200313
314 /* MSVC has a warning about unary minus on unsigned integer types,
315 * but this is well-defined and precisely what we want to do here. */
316#if defined(_MSC_VER)
317#pragma warning( push )
318#pragma warning( disable : 4146 )
319#endif
320
321 /* all-bits 1 if assign is 1, all-bits 0 if assign is 0 */
322 const mbedtls_mpi_uint mask = -assign;
323
324#if defined(_MSC_VER)
325#pragma warning( pop )
326#endif
327
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200328 for( i = 0; i < n; i++ )
Manuel Pégourié-Gonnard5ada7a82021-05-31 11:48:45 +0200329 dest[i] = ( src[i] & mask ) | ( dest[i] & ~mask );
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200330}
331
332/*
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100333 * Conditionally assign X = Y, without leaking information
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +0100334 * about whether the assignment was made or not.
335 * (Leaking information about the respective sizes of X and Y is ok however.)
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100338{
339 int ret = 0;
340 size_t i;
Manuel Pégourié-Gonnard5ada7a82021-05-31 11:48:45 +0200341 mbedtls_mpi_uint limb_mask;
Hanno Becker73d7d792018-12-11 10:35:51 +0000342 MPI_VALIDATE_RET( X != NULL );
343 MPI_VALIDATE_RET( Y != NULL );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100344
Manuel Pégourié-Gonnard5ada7a82021-05-31 11:48:45 +0200345 /* MSVC has a warning about unary minus on unsigned integer types,
346 * but this is well-defined and precisely what we want to do here. */
347#if defined(_MSC_VER)
348#pragma warning( push )
349#pragma warning( disable : 4146 )
350#endif
351
Pascal Junodb99183d2015-03-11 16:49:45 +0100352 /* make sure assign is 0 or 1 in a time-constant manner */
Manuel Pégourié-Gonnarda48b16a2021-06-17 13:25:03 +0200353 assign = (assign | (unsigned char)-assign) >> (sizeof( assign ) * 8 - 1);
Manuel Pégourié-Gonnard5ada7a82021-05-31 11:48:45 +0200354 /* all-bits 1 if assign is 1, all-bits 0 if assign is 0 */
Manuel Pégourié-Gonnard5ada7a82021-05-31 11:48:45 +0200355 limb_mask = -assign;
356
357#if defined(_MSC_VER)
358#pragma warning( pop )
359#endif
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100362
Manuel Pégourié-Gonnard3ae4ae42021-06-07 09:51:00 +0200363 X->s = mpi_safe_cond_select_sign( X->s, Y->s, assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100364
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200365 mpi_safe_cond_assign( Y->n, X->p, Y->p, assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100366
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200367 for( i = Y->n; i < X->n; i++ )
Manuel Pégourié-Gonnard5ada7a82021-05-31 11:48:45 +0200368 X->p[i] &= ~limb_mask;
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100369
370cleanup:
371 return( ret );
372}
373
374/*
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100375 * Conditionally swap X and Y, without leaking information
376 * about whether the swap was made or not.
377 * Here it is not ok to simply swap the pointers, which whould lead to
378 * different memory access patterns when X and Y are used afterwards.
379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100381{
382 int ret, s;
383 size_t i;
Manuel Pégourié-Gonnard448f1352021-06-03 10:54:01 +0200384 mbedtls_mpi_uint limb_mask;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_mpi_uint tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +0000386 MPI_VALIDATE_RET( X != NULL );
387 MPI_VALIDATE_RET( Y != NULL );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100388
389 if( X == Y )
390 return( 0 );
391
Manuel Pégourié-Gonnard448f1352021-06-03 10:54:01 +0200392 /* MSVC has a warning about unary minus on unsigned integer types,
393 * but this is well-defined and precisely what we want to do here. */
394#if defined(_MSC_VER)
395#pragma warning( push )
396#pragma warning( disable : 4146 )
397#endif
398
Pascal Junodb99183d2015-03-11 16:49:45 +0100399 /* make sure swap is 0 or 1 in a time-constant manner */
Manuel Pégourié-Gonnarda48b16a2021-06-17 13:25:03 +0200400 swap = (swap | (unsigned char)-swap) >> (sizeof( swap ) * 8 - 1);
Manuel Pégourié-Gonnard448f1352021-06-03 10:54:01 +0200401 /* all-bits 1 if swap is 1, all-bits 0 if swap is 0 */
Manuel Pégourié-Gonnard448f1352021-06-03 10:54:01 +0200402 limb_mask = -swap;
403
404#if defined(_MSC_VER)
405#pragma warning( pop )
406#endif
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
409 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( Y, X->n ) );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100410
411 s = X->s;
Manuel Pégourié-Gonnard3ae4ae42021-06-07 09:51:00 +0200412 X->s = mpi_safe_cond_select_sign( X->s, Y->s, swap );
413 Y->s = mpi_safe_cond_select_sign( Y->s, s, swap );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100414
415
416 for( i = 0; i < X->n; i++ )
417 {
418 tmp = X->p[i];
Manuel Pégourié-Gonnard448f1352021-06-03 10:54:01 +0200419 X->p[i] = ( X->p[i] & ~limb_mask ) | ( Y->p[i] & limb_mask );
420 Y->p[i] = ( Y->p[i] & ~limb_mask ) | ( tmp & limb_mask );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100421 }
422
423cleanup:
424 return( ret );
425}
426
427/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 * Set value from integer
429 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +0000431{
Janos Follath24eed8d2019-11-22 13:21:35 +0000432 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker73d7d792018-12-11 10:35:51 +0000433 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000436 memset( X->p, 0, X->n * ciL );
437
438 X->p[0] = ( z < 0 ) ? -z : z;
439 X->s = ( z < 0 ) ? -1 : 1;
440
441cleanup:
442
443 return( ret );
444}
445
446/*
Paul Bakker2f5947e2011-05-18 15:47:11 +0000447 * Get a specific bit
448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000450{
Hanno Becker73d7d792018-12-11 10:35:51 +0000451 MPI_VALIDATE_RET( X != NULL );
452
Paul Bakker2f5947e2011-05-18 15:47:11 +0000453 if( X->n * biL <= pos )
454 return( 0 );
455
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200456 return( ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000457}
458
Gilles Peskine11cdb052018-11-20 16:47:47 +0100459/* Get a specific byte, without range checks. */
460#define GET_BYTE( X, i ) \
461 ( ( ( X )->p[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff )
462
Paul Bakker2f5947e2011-05-18 15:47:11 +0000463/*
464 * Set a bit to a specific value of 0 or 1
465 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000467{
468 int ret = 0;
469 size_t off = pos / biL;
470 size_t idx = pos % biL;
Hanno Becker73d7d792018-12-11 10:35:51 +0000471 MPI_VALIDATE_RET( X != NULL );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000472
473 if( val != 0 && val != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker9af723c2014-05-01 13:03:14 +0200475
Paul Bakker2f5947e2011-05-18 15:47:11 +0000476 if( X->n * biL <= pos )
477 {
478 if( val == 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200479 return( 0 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, off + 1 ) );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000482 }
483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 X->p[off] &= ~( (mbedtls_mpi_uint) 0x01 << idx );
485 X->p[off] |= (mbedtls_mpi_uint) val << idx;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000486
487cleanup:
Paul Bakker9af723c2014-05-01 13:03:14 +0200488
Paul Bakker2f5947e2011-05-18 15:47:11 +0000489 return( ret );
490}
491
492/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200493 * Return the number of less significant zero-bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495size_t mbedtls_mpi_lsb( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000496{
Paul Bakker23986e52011-04-24 08:57:21 +0000497 size_t i, j, count = 0;
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000498 MBEDTLS_INTERNAL_VALIDATE_RET( X != NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500 for( i = 0; i < X->n; i++ )
Paul Bakkerf9688572011-05-05 10:00:45 +0000501 for( j = 0; j < biL; j++, count++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000502 if( ( ( X->p[i] >> j ) & 1 ) != 0 )
503 return( count );
504
505 return( 0 );
506}
507
508/*
Simon Butcher15b15d12015-11-26 19:35:03 +0000509 * Count leading zero bits in a given integer
510 */
511static size_t mbedtls_clz( const mbedtls_mpi_uint x )
512{
513 size_t j;
Manuel Pégourié-Gonnarde3e8edf2015-12-01 09:31:52 +0100514 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
Simon Butcher15b15d12015-11-26 19:35:03 +0000515
516 for( j = 0; j < biL; j++ )
517 {
518 if( x & mask ) break;
519
520 mask >>= 1;
521 }
522
523 return j;
524}
525
526/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200527 * Return the number of bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200529size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000530{
Paul Bakker23986e52011-04-24 08:57:21 +0000531 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +0000532
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200533 if( X->n == 0 )
534 return( 0 );
535
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 for( i = X->n - 1; i > 0; i-- )
537 if( X->p[i] != 0 )
538 break;
539
Simon Butcher15b15d12015-11-26 19:35:03 +0000540 j = biL - mbedtls_clz( X->p[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
Paul Bakker23986e52011-04-24 08:57:21 +0000542 return( ( i * biL ) + j );
Paul Bakker5121ce52009-01-03 21:22:43 +0000543}
544
545/*
546 * Return the total size in bytes
547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548size_t mbedtls_mpi_size( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000549{
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200550 return( ( mbedtls_mpi_bitlen( X ) + 7 ) >> 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000551}
552
553/*
554 * Convert an ASCII character to digit value
555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556static int mpi_get_digit( mbedtls_mpi_uint *d, int radix, char c )
Paul Bakker5121ce52009-01-03 21:22:43 +0000557{
558 *d = 255;
559
560 if( c >= 0x30 && c <= 0x39 ) *d = c - 0x30;
561 if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37;
562 if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57;
563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 if( *d >= (mbedtls_mpi_uint) radix )
565 return( MBEDTLS_ERR_MPI_INVALID_CHARACTER );
Paul Bakker5121ce52009-01-03 21:22:43 +0000566
567 return( 0 );
568}
569
570/*
571 * Import from an ASCII string
572 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000574{
Janos Follath24eed8d2019-11-22 13:21:35 +0000575 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000576 size_t i, j, slen, n;
Gilles Peskine80f56732021-04-03 18:26:13 +0200577 int sign = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_mpi_uint d;
579 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000580 MPI_VALIDATE_RET( X != NULL );
581 MPI_VALIDATE_RET( s != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
583 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000584 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000587
Gilles Peskine7cba8592021-06-08 18:32:34 +0200588 if( s[0] == 0 )
589 {
590 mbedtls_mpi_free( X );
591 return( 0 );
592 }
593
Gilles Peskine80f56732021-04-03 18:26:13 +0200594 if( s[0] == '-' )
595 {
596 ++s;
597 sign = -1;
598 }
599
Paul Bakkerff60ee62010-03-16 21:09:09 +0000600 slen = strlen( s );
601
Paul Bakker5121ce52009-01-03 21:22:43 +0000602 if( radix == 16 )
603 {
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +0100604 if( slen > MPI_SIZE_T_MAX >> 2 )
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +0200605 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
606
Paul Bakkerff60ee62010-03-16 21:09:09 +0000607 n = BITS_TO_LIMBS( slen << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n ) );
610 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000611
Paul Bakker23986e52011-04-24 08:57:21 +0000612 for( i = slen, j = 0; i > 0; i--, j++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200615 X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000616 }
617 }
618 else
619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
Paul Bakkerff60ee62010-03-16 21:09:09 +0000622 for( i = 0; i < slen; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
625 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T, X, radix ) );
Gilles Peskine80f56732021-04-03 18:26:13 +0200626 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, &T, d ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000627 }
628 }
629
Gilles Peskine80f56732021-04-03 18:26:13 +0200630 if( sign < 0 && mbedtls_mpi_bitlen( X ) != 0 )
631 X->s = -1;
632
Paul Bakker5121ce52009-01-03 21:22:43 +0000633cleanup:
634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000636
637 return( ret );
638}
639
640/*
Ron Eldora16fa292018-11-20 14:07:01 +0200641 * Helper to write the digits high-order first.
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 */
Ron Eldora16fa292018-11-20 14:07:01 +0200643static int mpi_write_hlp( mbedtls_mpi *X, int radix,
644 char **p, const size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000645{
Janos Follath24eed8d2019-11-22 13:21:35 +0000646 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_mpi_uint r;
Ron Eldora16fa292018-11-20 14:07:01 +0200648 size_t length = 0;
649 char *p_end = *p + buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000650
Ron Eldora16fa292018-11-20 14:07:01 +0200651 do
652 {
653 if( length >= buflen )
654 {
655 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
656 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000657
Ron Eldora16fa292018-11-20 14:07:01 +0200658 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) );
659 MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) );
660 /*
661 * Write the residue in the current position, as an ASCII character.
662 */
663 if( r < 0xA )
664 *(--p_end) = (char)( '0' + r );
665 else
666 *(--p_end) = (char)( 'A' + ( r - 0xA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000667
Ron Eldora16fa292018-11-20 14:07:01 +0200668 length++;
669 } while( mbedtls_mpi_cmp_int( X, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000670
Ron Eldora16fa292018-11-20 14:07:01 +0200671 memmove( *p, p_end, length );
672 *p += length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000673
674cleanup:
675
676 return( ret );
677}
678
679/*
680 * Export into an ASCII string
681 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100682int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
683 char *buf, size_t buflen, size_t *olen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000684{
Paul Bakker23986e52011-04-24 08:57:21 +0000685 int ret = 0;
686 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000687 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000689 MPI_VALIDATE_RET( X != NULL );
690 MPI_VALIDATE_RET( olen != NULL );
691 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
693 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000694 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000695
Hanno Becker23cfea02019-02-04 09:45:07 +0000696 n = mbedtls_mpi_bitlen( X ); /* Number of bits necessary to present `n`. */
697 if( radix >= 4 ) n >>= 1; /* Number of 4-adic digits necessary to present
698 * `n`. If radix > 4, this might be a strict
699 * overapproximation of the number of
700 * radix-adic digits needed to present `n`. */
701 if( radix >= 16 ) n >>= 1; /* Number of hexadecimal digits necessary to
702 * present `n`. */
703
Janos Follath80470622019-03-06 13:43:02 +0000704 n += 1; /* Terminating null byte */
Hanno Becker23cfea02019-02-04 09:45:07 +0000705 n += 1; /* Compensate for the divisions above, which round down `n`
706 * in case it's not even. */
707 n += 1; /* Potential '-'-sign. */
708 n += ( n & 1 ); /* Make n even to have enough space for hexadecimal writing,
709 * which always uses an even number of hex-digits. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100711 if( buflen < n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100713 *olen = n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000715 }
716
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100717 p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000719
720 if( X->s == -1 )
Hanno Beckerc983c812019-02-01 16:41:30 +0000721 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000722 *p++ = '-';
Hanno Beckerc983c812019-02-01 16:41:30 +0000723 buflen--;
724 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000725
726 if( radix == 16 )
727 {
Paul Bakker23986e52011-04-24 08:57:21 +0000728 int c;
729 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000730
Paul Bakker23986e52011-04-24 08:57:21 +0000731 for( i = X->n, k = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000732 {
Paul Bakker23986e52011-04-24 08:57:21 +0000733 for( j = ciL; j > 0; j-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000734 {
Paul Bakker23986e52011-04-24 08:57:21 +0000735 c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000736
Paul Bakker6c343d72014-07-10 14:36:19 +0200737 if( c == 0 && k == 0 && ( i + j ) != 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000738 continue;
739
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000740 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000741 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000742 k = 1;
743 }
744 }
745 }
746 else
747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T, X ) );
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000749
750 if( T.s == -1 )
751 T.s = 1;
752
Ron Eldora16fa292018-11-20 14:07:01 +0200753 MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p, buflen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000754 }
755
756 *p++ = '\0';
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100757 *olen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000758
759cleanup:
760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000762
763 return( ret );
764}
765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000767/*
768 * Read X from an opened file
769 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin )
Paul Bakker5121ce52009-01-03 21:22:43 +0000771{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_mpi_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000773 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000774 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000775 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000776 * Buffer should have space for (short) label and decimal formatted MPI,
777 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000778 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Paul Bakker5121ce52009-01-03 21:22:43 +0000780
Hanno Becker73d7d792018-12-11 10:35:51 +0000781 MPI_VALIDATE_RET( X != NULL );
782 MPI_VALIDATE_RET( fin != NULL );
783
784 if( radix < 2 || radix > 16 )
785 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
786
Paul Bakker5121ce52009-01-03 21:22:43 +0000787 memset( s, 0, sizeof( s ) );
788 if( fgets( s, sizeof( s ) - 1, fin ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000790
791 slen = strlen( s );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000792 if( slen == sizeof( s ) - 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000794
Hanno Beckerb2034b72017-04-26 11:46:46 +0100795 if( slen > 0 && s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
796 if( slen > 0 && s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
Paul Bakker5121ce52009-01-03 21:22:43 +0000797
798 p = s + slen;
Hanno Beckerb2034b72017-04-26 11:46:46 +0100799 while( p-- > s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000800 if( mpi_get_digit( &d, radix, *p ) != 0 )
801 break;
802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 return( mbedtls_mpi_read_string( X, radix, p + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000804}
805
806/*
807 * Write X into an opened file (or stdout if fout == NULL)
808 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout )
Paul Bakker5121ce52009-01-03 21:22:43 +0000810{
Janos Follath24eed8d2019-11-22 13:21:35 +0000811 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000812 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000813 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000814 * Buffer should have space for (short) label and decimal formatted MPI,
815 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000816 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Hanno Becker73d7d792018-12-11 10:35:51 +0000818 MPI_VALIDATE_RET( X != NULL );
819
820 if( radix < 2 || radix > 16 )
821 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000822
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100823 memset( s, 0, sizeof( s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000824
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100825 MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, sizeof( s ) - 2, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000826
827 if( p == NULL ) p = "";
828
829 plen = strlen( p );
830 slen = strlen( s );
831 s[slen++] = '\r';
832 s[slen++] = '\n';
833
834 if( fout != NULL )
835 {
836 if( fwrite( p, 1, plen, fout ) != plen ||
837 fwrite( s, 1, slen, fout ) != slen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000839 }
840 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 mbedtls_printf( "%s%s", p, s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000842
843cleanup:
844
845 return( ret );
846}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000848
Hanno Beckerda1655a2017-10-18 14:21:44 +0100849
850/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
851 * into the storage form used by mbedtls_mpi. */
Hanno Beckerf8720072018-11-08 11:53:49 +0000852
853static mbedtls_mpi_uint mpi_uint_bigendian_to_host_c( mbedtls_mpi_uint x )
854{
855 uint8_t i;
Hanno Becker031d6332019-05-01 17:09:11 +0100856 unsigned char *x_ptr;
Hanno Beckerf8720072018-11-08 11:53:49 +0000857 mbedtls_mpi_uint tmp = 0;
Hanno Becker031d6332019-05-01 17:09:11 +0100858
859 for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
860 {
861 tmp <<= CHAR_BIT;
862 tmp |= (mbedtls_mpi_uint) *x_ptr;
863 }
864
Hanno Beckerf8720072018-11-08 11:53:49 +0000865 return( tmp );
866}
867
868static mbedtls_mpi_uint mpi_uint_bigendian_to_host( mbedtls_mpi_uint x )
869{
870#if defined(__BYTE_ORDER__)
871
872/* Nothing to do on bigendian systems. */
873#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
874 return( x );
875#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
876
877#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
878
879/* For GCC and Clang, have builtins for byte swapping. */
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000880#if defined(__GNUC__) && defined(__GNUC_PREREQ)
881#if __GNUC_PREREQ(4,3)
Hanno Beckerf8720072018-11-08 11:53:49 +0000882#define have_bswap
883#endif
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000884#endif
885
886#if defined(__clang__) && defined(__has_builtin)
887#if __has_builtin(__builtin_bswap32) && \
888 __has_builtin(__builtin_bswap64)
889#define have_bswap
890#endif
891#endif
892
Hanno Beckerf8720072018-11-08 11:53:49 +0000893#if defined(have_bswap)
894 /* The compiler is hopefully able to statically evaluate this! */
895 switch( sizeof(mbedtls_mpi_uint) )
896 {
897 case 4:
898 return( __builtin_bswap32(x) );
899 case 8:
900 return( __builtin_bswap64(x) );
901 }
902#endif
903#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
904#endif /* __BYTE_ORDER__ */
905
906 /* Fall back to C-based reordering if we don't know the byte order
907 * or we couldn't use a compiler-specific builtin. */
908 return( mpi_uint_bigendian_to_host_c( x ) );
909}
910
Hanno Becker2be8a552018-10-25 12:40:09 +0100911static void mpi_bigendian_to_host( mbedtls_mpi_uint * const p, size_t limbs )
Hanno Beckerda1655a2017-10-18 14:21:44 +0100912{
Hanno Beckerda1655a2017-10-18 14:21:44 +0100913 mbedtls_mpi_uint *cur_limb_left;
914 mbedtls_mpi_uint *cur_limb_right;
Hanno Becker2be8a552018-10-25 12:40:09 +0100915 if( limbs == 0 )
916 return;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100917
918 /*
919 * Traverse limbs and
920 * - adapt byte-order in each limb
921 * - swap the limbs themselves.
922 * For that, simultaneously traverse the limbs from left to right
923 * and from right to left, as long as the left index is not bigger
924 * than the right index (it's not a problem if limbs is odd and the
925 * indices coincide in the last iteration).
926 */
Hanno Beckerda1655a2017-10-18 14:21:44 +0100927 for( cur_limb_left = p, cur_limb_right = p + ( limbs - 1 );
928 cur_limb_left <= cur_limb_right;
929 cur_limb_left++, cur_limb_right-- )
930 {
Hanno Beckerf8720072018-11-08 11:53:49 +0000931 mbedtls_mpi_uint tmp;
932 /* Note that if cur_limb_left == cur_limb_right,
933 * this code effectively swaps the bytes only once. */
934 tmp = mpi_uint_bigendian_to_host( *cur_limb_left );
935 *cur_limb_left = mpi_uint_bigendian_to_host( *cur_limb_right );
936 *cur_limb_right = tmp;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100937 }
Hanno Beckerda1655a2017-10-18 14:21:44 +0100938}
939
Paul Bakker5121ce52009-01-03 21:22:43 +0000940/*
Janos Follatha778a942019-02-13 10:28:28 +0000941 * Import X from unsigned binary data, little endian
942 */
943int mbedtls_mpi_read_binary_le( mbedtls_mpi *X,
944 const unsigned char *buf, size_t buflen )
945{
Janos Follath24eed8d2019-11-22 13:21:35 +0000946 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follatha778a942019-02-13 10:28:28 +0000947 size_t i;
948 size_t const limbs = CHARS_TO_LIMBS( buflen );
949
950 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskineed32b572021-06-02 22:17:52 +0200951 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Janos Follatha778a942019-02-13 10:28:28 +0000952
953 for( i = 0; i < buflen; i++ )
954 X->p[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
955
956cleanup:
957
Janos Follath171a7ef2019-02-15 16:17:45 +0000958 /*
959 * This function is also used to import keys. However, wiping the buffers
960 * upon failure is not necessary because failure only can happen before any
961 * input is copied.
962 */
Janos Follatha778a942019-02-13 10:28:28 +0000963 return( ret );
964}
965
966/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000967 * Import X from unsigned binary data, big endian
968 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000970{
Janos Follath24eed8d2019-11-22 13:21:35 +0000971 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100972 size_t const limbs = CHARS_TO_LIMBS( buflen );
973 size_t const overhead = ( limbs * ciL ) - buflen;
974 unsigned char *Xp;
Paul Bakker5121ce52009-01-03 21:22:43 +0000975
Hanno Becker8ce11a32018-12-19 16:18:52 +0000976 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +0000977 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
978
Hanno Becker073c1992017-10-17 15:17:27 +0100979 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskineed32b572021-06-02 22:17:52 +0200980 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000981
Gilles Peskineed32b572021-06-02 22:17:52 +0200982 /* Avoid calling `memcpy` with NULL source or destination argument,
Hanno Becker0e810b92019-01-03 17:13:11 +0000983 * even if buflen is 0. */
Gilles Peskineed32b572021-06-02 22:17:52 +0200984 if( buflen != 0 )
Hanno Becker0e810b92019-01-03 17:13:11 +0000985 {
986 Xp = (unsigned char*) X->p;
987 memcpy( Xp + overhead, buf, buflen );
Hanno Beckerda1655a2017-10-18 14:21:44 +0100988
Hanno Becker0e810b92019-01-03 17:13:11 +0000989 mpi_bigendian_to_host( X->p, limbs );
990 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000991
992cleanup:
993
Janos Follath171a7ef2019-02-15 16:17:45 +0000994 /*
995 * This function is also used to import keys. However, wiping the buffers
996 * upon failure is not necessary because failure only can happen before any
997 * input is copied.
998 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 return( ret );
1000}
1001
1002/*
Janos Follathe344d0f2019-02-19 16:17:40 +00001003 * Export X into unsigned binary data, little endian
1004 */
1005int mbedtls_mpi_write_binary_le( const mbedtls_mpi *X,
1006 unsigned char *buf, size_t buflen )
1007{
1008 size_t stored_bytes = X->n * ciL;
1009 size_t bytes_to_copy;
1010 size_t i;
1011
1012 if( stored_bytes < buflen )
1013 {
1014 bytes_to_copy = stored_bytes;
1015 }
1016 else
1017 {
1018 bytes_to_copy = buflen;
1019
1020 /* The output buffer is smaller than the allocated size of X.
1021 * However X may fit if its leading bytes are zero. */
1022 for( i = bytes_to_copy; i < stored_bytes; i++ )
1023 {
1024 if( GET_BYTE( X, i ) != 0 )
1025 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
1026 }
1027 }
1028
1029 for( i = 0; i < bytes_to_copy; i++ )
1030 buf[i] = GET_BYTE( X, i );
1031
1032 if( stored_bytes < buflen )
1033 {
1034 /* Write trailing 0 bytes */
1035 memset( buf + stored_bytes, 0, buflen - stored_bytes );
1036 }
1037
1038 return( 0 );
1039}
1040
1041/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001042 * Export X into unsigned binary data, big endian
1043 */
Gilles Peskine11cdb052018-11-20 16:47:47 +01001044int mbedtls_mpi_write_binary( const mbedtls_mpi *X,
1045 unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001046{
Hanno Becker73d7d792018-12-11 10:35:51 +00001047 size_t stored_bytes;
Gilles Peskine11cdb052018-11-20 16:47:47 +01001048 size_t bytes_to_copy;
1049 unsigned char *p;
1050 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +00001051
Hanno Becker73d7d792018-12-11 10:35:51 +00001052 MPI_VALIDATE_RET( X != NULL );
1053 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
1054
1055 stored_bytes = X->n * ciL;
1056
Gilles Peskine11cdb052018-11-20 16:47:47 +01001057 if( stored_bytes < buflen )
1058 {
1059 /* There is enough space in the output buffer. Write initial
1060 * null bytes and record the position at which to start
1061 * writing the significant bytes. In this case, the execution
1062 * trace of this function does not depend on the value of the
1063 * number. */
1064 bytes_to_copy = stored_bytes;
1065 p = buf + buflen - stored_bytes;
1066 memset( buf, 0, buflen - stored_bytes );
1067 }
1068 else
1069 {
1070 /* The output buffer is smaller than the allocated size of X.
1071 * However X may fit if its leading bytes are zero. */
1072 bytes_to_copy = buflen;
1073 p = buf;
1074 for( i = bytes_to_copy; i < stored_bytes; i++ )
1075 {
1076 if( GET_BYTE( X, i ) != 0 )
1077 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
1078 }
1079 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001080
Gilles Peskine11cdb052018-11-20 16:47:47 +01001081 for( i = 0; i < bytes_to_copy; i++ )
1082 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
Paul Bakker5121ce52009-01-03 21:22:43 +00001083
1084 return( 0 );
1085}
1086
1087/*
1088 * Left-shift: X <<= count
1089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +00001091{
Janos Follath24eed8d2019-11-22 13:21:35 +00001092 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001093 size_t i, v0, t1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +00001095 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001096
1097 v0 = count / (biL );
1098 t1 = count & (biL - 1);
1099
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001100 i = mbedtls_mpi_bitlen( X ) + count;
Paul Bakker5121ce52009-01-03 21:22:43 +00001101
Paul Bakkerf9688572011-05-05 10:00:45 +00001102 if( X->n * biL < i )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001104
1105 ret = 0;
1106
1107 /*
1108 * shift by count / limb_size
1109 */
1110 if( v0 > 0 )
1111 {
Paul Bakker23986e52011-04-24 08:57:21 +00001112 for( i = X->n; i > v0; i-- )
1113 X->p[i - 1] = X->p[i - v0 - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001114
Paul Bakker23986e52011-04-24 08:57:21 +00001115 for( ; i > 0; i-- )
1116 X->p[i - 1] = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001117 }
1118
1119 /*
1120 * shift by count % limb_size
1121 */
1122 if( t1 > 0 )
1123 {
1124 for( i = v0; i < X->n; i++ )
1125 {
1126 r1 = X->p[i] >> (biL - t1);
1127 X->p[i] <<= t1;
1128 X->p[i] |= r0;
1129 r0 = r1;
1130 }
1131 }
1132
1133cleanup:
1134
1135 return( ret );
1136}
1137
1138/*
1139 * Right-shift: X >>= count
1140 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +00001142{
Paul Bakker23986e52011-04-24 08:57:21 +00001143 size_t i, v0, v1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +00001145 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001146
1147 v0 = count / biL;
1148 v1 = count & (biL - 1);
1149
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001150 if( v0 > X->n || ( v0 == X->n && v1 > 0 ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 return mbedtls_mpi_lset( X, 0 );
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001152
Paul Bakker5121ce52009-01-03 21:22:43 +00001153 /*
1154 * shift by count / limb_size
1155 */
1156 if( v0 > 0 )
1157 {
1158 for( i = 0; i < X->n - v0; i++ )
1159 X->p[i] = X->p[i + v0];
1160
1161 for( ; i < X->n; i++ )
1162 X->p[i] = 0;
1163 }
1164
1165 /*
1166 * shift by count % limb_size
1167 */
1168 if( v1 > 0 )
1169 {
Paul Bakker23986e52011-04-24 08:57:21 +00001170 for( i = X->n; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001171 {
Paul Bakker23986e52011-04-24 08:57:21 +00001172 r1 = X->p[i - 1] << (biL - v1);
1173 X->p[i - 1] >>= v1;
1174 X->p[i - 1] |= r0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001175 r0 = r1;
1176 }
1177 }
1178
1179 return( 0 );
1180}
1181
1182/*
1183 * Compare unsigned values
1184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001186{
Paul Bakker23986e52011-04-24 08:57:21 +00001187 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001188 MPI_VALIDATE_RET( X != NULL );
1189 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001190
Paul Bakker23986e52011-04-24 08:57:21 +00001191 for( i = X->n; i > 0; i-- )
1192 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 break;
1194
Paul Bakker23986e52011-04-24 08:57:21 +00001195 for( j = Y->n; j > 0; j-- )
1196 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001197 break;
1198
Paul Bakker23986e52011-04-24 08:57:21 +00001199 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001200 return( 0 );
1201
1202 if( i > j ) return( 1 );
1203 if( j > i ) return( -1 );
1204
Paul Bakker23986e52011-04-24 08:57:21 +00001205 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 {
Paul Bakker23986e52011-04-24 08:57:21 +00001207 if( X->p[i - 1] > Y->p[i - 1] ) return( 1 );
1208 if( X->p[i - 1] < Y->p[i - 1] ) return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001209 }
1210
1211 return( 0 );
1212}
1213
1214/*
1215 * Compare signed values
1216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001218{
Paul Bakker23986e52011-04-24 08:57:21 +00001219 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001220 MPI_VALIDATE_RET( X != NULL );
1221 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001222
Paul Bakker23986e52011-04-24 08:57:21 +00001223 for( i = X->n; i > 0; i-- )
1224 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001225 break;
1226
Paul Bakker23986e52011-04-24 08:57:21 +00001227 for( j = Y->n; j > 0; j-- )
1228 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001229 break;
1230
Paul Bakker23986e52011-04-24 08:57:21 +00001231 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001232 return( 0 );
1233
1234 if( i > j ) return( X->s );
Paul Bakker0c8f73b2012-03-22 14:08:57 +00001235 if( j > i ) return( -Y->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001236
1237 if( X->s > 0 && Y->s < 0 ) return( 1 );
1238 if( Y->s > 0 && X->s < 0 ) return( -1 );
1239
Paul Bakker23986e52011-04-24 08:57:21 +00001240 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001241 {
Paul Bakker23986e52011-04-24 08:57:21 +00001242 if( X->p[i - 1] > Y->p[i - 1] ) return( X->s );
1243 if( X->p[i - 1] < Y->p[i - 1] ) return( -X->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 }
1245
1246 return( 0 );
1247}
1248
Janos Follath3f6f0e42019-10-14 09:09:32 +01001249/** Decide if an integer is less than the other, without branches.
1250 *
1251 * \param x First integer.
1252 * \param y Second integer.
1253 *
1254 * \return 1 if \p x is less than \p y, 0 otherwise
1255 */
Janos Follath0e5532d2019-10-11 14:21:53 +01001256static unsigned ct_lt_mpi_uint( const mbedtls_mpi_uint x,
1257 const mbedtls_mpi_uint y )
Janos Follathee6abce2019-09-05 14:47:19 +01001258{
1259 mbedtls_mpi_uint ret;
1260 mbedtls_mpi_uint cond;
1261
1262 /*
1263 * Check if the most significant bits (MSB) of the operands are different.
1264 */
1265 cond = ( x ^ y );
1266 /*
1267 * If the MSB are the same then the difference x-y will be negative (and
1268 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
1269 */
1270 ret = ( x - y ) & ~cond;
1271 /*
1272 * If the MSB are different, then the operand with the MSB of 1 is the
1273 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
1274 * the MSB of y is 0.)
1275 */
1276 ret |= y & cond;
1277
1278
Janos Follatha0f732b2019-10-14 08:59:14 +01001279 ret = ret >> ( biL - 1 );
Janos Follathee6abce2019-09-05 14:47:19 +01001280
Janos Follath67ce6472019-10-29 15:08:46 +00001281 return (unsigned) ret;
Janos Follathee6abce2019-09-05 14:47:19 +01001282}
1283
1284/*
1285 * Compare signed values in constant time
1286 */
Janos Follath0e5532d2019-10-11 14:21:53 +01001287int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y,
1288 unsigned *ret )
Janos Follathee6abce2019-09-05 14:47:19 +01001289{
1290 size_t i;
Janos Follathbb5147f2019-10-28 12:23:18 +00001291 /* The value of any of these variables is either 0 or 1 at all times. */
Janos Follath5e614ce2019-10-28 12:31:34 +00001292 unsigned cond, done, X_is_negative, Y_is_negative;
Janos Follathee6abce2019-09-05 14:47:19 +01001293
1294 MPI_VALIDATE_RET( X != NULL );
1295 MPI_VALIDATE_RET( Y != NULL );
1296 MPI_VALIDATE_RET( ret != NULL );
1297
1298 if( X->n != Y->n )
1299 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1300
1301 /*
Janos Follath73ba9ec2019-10-28 12:12:15 +00001302 * Set sign_N to 1 if N >= 0, 0 if N < 0.
1303 * We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0.
Janos Follathee6abce2019-09-05 14:47:19 +01001304 */
Janos Follath5e614ce2019-10-28 12:31:34 +00001305 X_is_negative = ( X->s & 2 ) >> 1;
1306 Y_is_negative = ( Y->s & 2 ) >> 1;
Janos Follath0e5532d2019-10-11 14:21:53 +01001307
1308 /*
1309 * If the signs are different, then the positive operand is the bigger.
Janos Follath5e614ce2019-10-28 12:31:34 +00001310 * That is if X is negative (X_is_negative == 1), then X < Y is true and it
1311 * is false if X is positive (X_is_negative == 0).
Janos Follath0e5532d2019-10-11 14:21:53 +01001312 */
Janos Follath5e614ce2019-10-28 12:31:34 +00001313 cond = ( X_is_negative ^ Y_is_negative );
1314 *ret = cond & X_is_negative;
Janos Follath0e5532d2019-10-11 14:21:53 +01001315
1316 /*
Janos Follathbb5147f2019-10-28 12:23:18 +00001317 * This is a constant-time function. We might have the result, but we still
Janos Follath0e5532d2019-10-11 14:21:53 +01001318 * need to go through the loop. Record if we have the result already.
1319 */
Janos Follathee6abce2019-09-05 14:47:19 +01001320 done = cond;
1321
1322 for( i = X->n; i > 0; i-- )
1323 {
1324 /*
Janos Follath30702422019-11-05 12:24:52 +00001325 * If Y->p[i - 1] < X->p[i - 1] then X < Y is true if and only if both
1326 * X and Y are negative.
Janos Follath0e5532d2019-10-11 14:21:53 +01001327 *
1328 * Again even if we can make a decision, we just mark the result and
1329 * the fact that we are done and continue looping.
Janos Follathee6abce2019-09-05 14:47:19 +01001330 */
Janos Follath30702422019-11-05 12:24:52 +00001331 cond = ct_lt_mpi_uint( Y->p[i - 1], X->p[i - 1] );
1332 *ret |= cond & ( 1 - done ) & X_is_negative;
Janos Follathc50e6d52019-10-28 12:37:21 +00001333 done |= cond;
Janos Follathee6abce2019-09-05 14:47:19 +01001334
1335 /*
Janos Follath30702422019-11-05 12:24:52 +00001336 * If X->p[i - 1] < Y->p[i - 1] then X < Y is true if and only if both
1337 * X and Y are positive.
Janos Follath0e5532d2019-10-11 14:21:53 +01001338 *
1339 * Again even if we can make a decision, we just mark the result and
1340 * the fact that we are done and continue looping.
Janos Follathee6abce2019-09-05 14:47:19 +01001341 */
Janos Follath30702422019-11-05 12:24:52 +00001342 cond = ct_lt_mpi_uint( X->p[i - 1], Y->p[i - 1] );
1343 *ret |= cond & ( 1 - done ) & ( 1 - X_is_negative );
Janos Follathc50e6d52019-10-28 12:37:21 +00001344 done |= cond;
Janos Follathee6abce2019-09-05 14:47:19 +01001345 }
1346
1347 return( 0 );
1348}
1349
Paul Bakker5121ce52009-01-03 21:22:43 +00001350/*
1351 * Compare signed values
1352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +00001354{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 mbedtls_mpi Y;
1356 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001357 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001358
1359 *p = ( z < 0 ) ? -z : z;
1360 Y.s = ( z < 0 ) ? -1 : 1;
1361 Y.n = 1;
1362 Y.p = p;
1363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 return( mbedtls_mpi_cmp_mpi( X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001365}
1366
1367/*
1368 * Unsigned addition: X = |A| + |B| (HAC 14.7)
1369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001371{
Janos Follath24eed8d2019-11-22 13:21:35 +00001372 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001373 size_t i, j;
Janos Follath6c922682015-10-30 17:43:11 +01001374 mbedtls_mpi_uint *o, *p, c, tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +00001375 MPI_VALIDATE_RET( X != NULL );
1376 MPI_VALIDATE_RET( A != NULL );
1377 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001378
1379 if( X == B )
1380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 const mbedtls_mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 }
1383
1384 if( X != A )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
Paul Bakker9af723c2014-05-01 13:03:14 +02001386
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001387 /*
1388 * X should always be positive as a result of unsigned additions.
1389 */
1390 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001391
Paul Bakker23986e52011-04-24 08:57:21 +00001392 for( j = B->n; j > 0; j-- )
1393 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 break;
1395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001397
1398 o = B->p; p = X->p; c = 0;
1399
Janos Follath6c922682015-10-30 17:43:11 +01001400 /*
1401 * tmp is used because it might happen that p == o
1402 */
Paul Bakker23986e52011-04-24 08:57:21 +00001403 for( i = 0; i < j; i++, o++, p++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 {
Janos Follath6c922682015-10-30 17:43:11 +01001405 tmp= *o;
Paul Bakker5121ce52009-01-03 21:22:43 +00001406 *p += c; c = ( *p < c );
Janos Follath6c922682015-10-30 17:43:11 +01001407 *p += tmp; c += ( *p < tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +00001408 }
1409
1410 while( c != 0 )
1411 {
1412 if( i >= X->n )
1413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001415 p = X->p + i;
1416 }
1417
Paul Bakker2d319fd2012-09-16 21:34:26 +00001418 *p += c; c = ( *p < c ); i++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001419 }
1420
1421cleanup:
1422
1423 return( ret );
1424}
1425
Gilles Peskine09ec10a2020-06-09 10:39:38 +02001426/**
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001427 * Helper for mbedtls_mpi subtraction.
1428 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001429 * Calculate l - r where l and r have the same size.
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001430 * This function operates modulo (2^ciL)^n and returns the carry
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001431 * (1 if there was a wraparound, i.e. if `l < r`, and 0 otherwise).
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001432 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001433 * d may be aliased to l or r.
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001434 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001435 * \param n Number of limbs of \p d, \p l and \p r.
1436 * \param[out] d The result of the subtraction.
1437 * \param[in] l The left operand.
1438 * \param[in] r The right operand.
1439 *
1440 * \return 1 if `l < r`.
1441 * 0 if `l >= r`.
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 */
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001443static mbedtls_mpi_uint mpi_sub_hlp( size_t n,
1444 mbedtls_mpi_uint *d,
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001445 const mbedtls_mpi_uint *l,
1446 const mbedtls_mpi_uint *r )
Paul Bakker5121ce52009-01-03 21:22:43 +00001447{
Paul Bakker23986e52011-04-24 08:57:21 +00001448 size_t i;
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001449 mbedtls_mpi_uint c = 0, t, z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001450
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001451 for( i = 0; i < n; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001452 {
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001453 z = ( l[i] < c ); t = l[i] - c;
1454 c = ( t < r[i] ) + z; d[i] = t - r[i];
Paul Bakker5121ce52009-01-03 21:22:43 +00001455 }
1456
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001457 return( c );
Paul Bakker5121ce52009-01-03 21:22:43 +00001458}
1459
1460/*
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001461 * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
Paul Bakker5121ce52009-01-03 21:22:43 +00001462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001464{
Janos Follath24eed8d2019-11-22 13:21:35 +00001465 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001466 size_t n;
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001467 mbedtls_mpi_uint carry;
Hanno Becker73d7d792018-12-11 10:35:51 +00001468 MPI_VALIDATE_RET( X != NULL );
1469 MPI_VALIDATE_RET( A != NULL );
1470 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001471
Paul Bakker23986e52011-04-24 08:57:21 +00001472 for( n = B->n; n > 0; n-- )
1473 if( B->p[n - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001474 break;
Gilles Peskinec8a91772021-01-27 22:30:43 +01001475 if( n > A->n )
1476 {
1477 /* B >= (2^ciL)^n > A */
1478 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1479 goto cleanup;
1480 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001481
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001482 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, A->n ) );
1483
1484 /* Set the high limbs of X to match A. Don't touch the lower limbs
1485 * because X might be aliased to B, and we must not overwrite the
1486 * significant digits of B. */
1487 if( A->n > n )
1488 memcpy( X->p + n, A->p + n, ( A->n - n ) * ciL );
1489 if( X->n > A->n )
1490 memset( X->p + A->n, 0, ( X->n - A->n ) * ciL );
1491
1492 carry = mpi_sub_hlp( n, X->p, A->p, B->p );
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001493 if( carry != 0 )
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001494 {
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001495 /* Propagate the carry to the first nonzero limb of X. */
1496 for( ; n < X->n && X->p[n] == 0; n++ )
1497 --X->p[n];
1498 /* If we ran out of space for the carry, it means that the result
1499 * is negative. */
1500 if( n == X->n )
Gilles Peskine89b41302020-07-23 01:16:46 +02001501 {
1502 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1503 goto cleanup;
1504 }
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001505 --X->p[n];
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001506 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001507
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001508 /* X should always be positive as a result of unsigned subtractions. */
1509 X->s = 1;
1510
Paul Bakker5121ce52009-01-03 21:22:43 +00001511cleanup:
Paul Bakker5121ce52009-01-03 21:22:43 +00001512 return( ret );
1513}
1514
1515/*
1516 * Signed addition: X = A + B
1517 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001519{
Hanno Becker73d7d792018-12-11 10:35:51 +00001520 int ret, s;
1521 MPI_VALIDATE_RET( X != NULL );
1522 MPI_VALIDATE_RET( A != NULL );
1523 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001524
Hanno Becker73d7d792018-12-11 10:35:51 +00001525 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 if( A->s * B->s < 0 )
1527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001531 X->s = s;
1532 }
1533 else
1534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001536 X->s = -s;
1537 }
1538 }
1539 else
1540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001542 X->s = s;
1543 }
1544
1545cleanup:
1546
1547 return( ret );
1548}
1549
1550/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001551 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001552 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001554{
Hanno Becker73d7d792018-12-11 10:35:51 +00001555 int ret, s;
1556 MPI_VALIDATE_RET( X != NULL );
1557 MPI_VALIDATE_RET( A != NULL );
1558 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001559
Hanno Becker73d7d792018-12-11 10:35:51 +00001560 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001561 if( A->s * B->s > 0 )
1562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 X->s = s;
1567 }
1568 else
1569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001571 X->s = -s;
1572 }
1573 }
1574 else
1575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001577 X->s = s;
1578 }
1579
1580cleanup:
1581
1582 return( ret );
1583}
1584
1585/*
1586 * Signed addition: X = A + b
1587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001589{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001590 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001592 MPI_VALIDATE_RET( X != NULL );
1593 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001594
1595 p[0] = ( b < 0 ) ? -b : b;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001596 B.s = ( b < 0 ) ? -1 : 1;
1597 B.n = 1;
1598 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001599
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001600 return( mbedtls_mpi_add_mpi( X, A, &B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001601}
1602
1603/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001604 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001605 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001607{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001608 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001610 MPI_VALIDATE_RET( X != NULL );
1611 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001612
1613 p[0] = ( b < 0 ) ? -b : b;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001614 B.s = ( b < 0 ) ? -1 : 1;
1615 B.n = 1;
1616 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001617
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001618 return( mbedtls_mpi_sub_mpi( X, A, &B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001619}
1620
Gilles Peskinea5d8d892020-07-23 21:27:15 +02001621/** Helper for mbedtls_mpi multiplication.
1622 *
1623 * Add \p b * \p s to \p d.
1624 *
1625 * \param i The number of limbs of \p s.
1626 * \param[in] s A bignum to multiply, of size \p i.
1627 * It may overlap with \p d, but only if
1628 * \p d <= \p s.
1629 * Its leading limb must not be \c 0.
1630 * \param[in,out] d The bignum to add to.
1631 * It must be sufficiently large to store the
1632 * result of the multiplication. This means
1633 * \p i + 1 limbs if \p d[\p i - 1] started as 0 and \p b
1634 * is not known a priori.
1635 * \param b A scalar to multiply.
Paul Bakkerfc4f46f2013-06-24 19:23:56 +02001636 */
1637static
1638#if defined(__APPLE__) && defined(__arm__)
1639/*
1640 * Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
1641 * appears to need this to prevent bad ARM code generation at -O3.
1642 */
1643__attribute__ ((noinline))
1644#endif
Gilles Peskinea5d8d892020-07-23 21:27:15 +02001645void mpi_mul_hlp( size_t i,
1646 const mbedtls_mpi_uint *s,
1647 mbedtls_mpi_uint *d,
1648 mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001649{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650 mbedtls_mpi_uint c = 0, t = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001651
1652#if defined(MULADDC_HUIT)
1653 for( ; i >= 8; i -= 8 )
1654 {
1655 MULADDC_INIT
1656 MULADDC_HUIT
1657 MULADDC_STOP
1658 }
1659
1660 for( ; i > 0; i-- )
1661 {
1662 MULADDC_INIT
1663 MULADDC_CORE
1664 MULADDC_STOP
1665 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001666#else /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001667 for( ; i >= 16; i -= 16 )
1668 {
1669 MULADDC_INIT
1670 MULADDC_CORE MULADDC_CORE
1671 MULADDC_CORE MULADDC_CORE
1672 MULADDC_CORE MULADDC_CORE
1673 MULADDC_CORE MULADDC_CORE
1674
1675 MULADDC_CORE MULADDC_CORE
1676 MULADDC_CORE MULADDC_CORE
1677 MULADDC_CORE MULADDC_CORE
1678 MULADDC_CORE MULADDC_CORE
1679 MULADDC_STOP
1680 }
1681
1682 for( ; i >= 8; i -= 8 )
1683 {
1684 MULADDC_INIT
1685 MULADDC_CORE MULADDC_CORE
1686 MULADDC_CORE MULADDC_CORE
1687
1688 MULADDC_CORE MULADDC_CORE
1689 MULADDC_CORE MULADDC_CORE
1690 MULADDC_STOP
1691 }
1692
1693 for( ; i > 0; i-- )
1694 {
1695 MULADDC_INIT
1696 MULADDC_CORE
1697 MULADDC_STOP
1698 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001699#endif /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001700
1701 t++;
1702
Gilles Peskine8e464c42020-07-24 00:08:38 +02001703 while( c != 0 )
1704 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001705 *d += c; c = ( *d < c ); d++;
1706 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001707}
1708
1709/*
1710 * Baseline multiplication: X = A * B (HAC 14.12)
1711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001713{
Janos Follath24eed8d2019-11-22 13:21:35 +00001714 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001715 size_t i, j;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 mbedtls_mpi TA, TB;
Gilles Peskine997be0a2021-06-15 21:44:32 +02001717 int result_is_zero = 0;
Hanno Becker73d7d792018-12-11 10:35:51 +00001718 MPI_VALIDATE_RET( X != NULL );
1719 MPI_VALIDATE_RET( A != NULL );
1720 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 if( X == A ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); A = &TA; }
1725 if( X == B ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); B = &TB; }
Paul Bakker5121ce52009-01-03 21:22:43 +00001726
Paul Bakker23986e52011-04-24 08:57:21 +00001727 for( i = A->n; i > 0; i-- )
1728 if( A->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001729 break;
Gilles Peskine4169c322021-06-17 14:35:25 +02001730 if( i == 0 )
Gilles Peskine997be0a2021-06-15 21:44:32 +02001731 result_is_zero = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001732
Paul Bakker23986e52011-04-24 08:57:21 +00001733 for( j = B->n; j > 0; j-- )
1734 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001735 break;
Gilles Peskine4169c322021-06-17 14:35:25 +02001736 if( j == 0 )
Gilles Peskine997be0a2021-06-15 21:44:32 +02001737 result_is_zero = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + j ) );
1740 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001741
Alexey Skalozub8e75e682016-01-13 21:59:27 +02001742 for( ; j > 0; j-- )
1743 mpi_mul_hlp( i, A->p, X->p + j - 1, B->p[j - 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00001744
Gilles Peskinef4998b02021-06-10 15:51:54 +02001745 /* If the result is 0, we don't shortcut the operation, which reduces
1746 * but does not eliminate side channels leaking the zero-ness. We do
1747 * need to take care to set the sign bit properly since the library does
1748 * not fully support an MPI object with a value of 0 and s == -1. */
Gilles Peskine997be0a2021-06-15 21:44:32 +02001749 if( result_is_zero )
Gilles Peskinef4998b02021-06-10 15:51:54 +02001750 X->s = 1;
Gilles Peskinef4998b02021-06-10 15:51:54 +02001751 else
1752 X->s = A->s * B->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001753
1754cleanup:
1755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756 mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001757
1758 return( ret );
1759}
1760
1761/*
1762 * Baseline multiplication: X = A * b
1763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001765{
Hanno Becker73d7d792018-12-11 10:35:51 +00001766 MPI_VALIDATE_RET( X != NULL );
1767 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001768
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001769 /* mpi_mul_hlp can't deal with a leading 0. */
1770 size_t n = A->n;
1771 while( n > 0 && A->p[n - 1] == 0 )
1772 --n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001773
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001774 /* The general method below doesn't work if n==0 or b==0. By chance
1775 * calculating the result is trivial in those cases. */
1776 if( b == 0 || n == 0 )
1777 {
Paul Elliott986b55a2021-04-20 21:46:29 +01001778 return( mbedtls_mpi_lset( X, 0 ) );
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001779 }
1780
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001781 /* Calculate A*b as A + A*(b-1) to take advantage of mpi_mul_hlp */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001782 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001783 /* In general, A * b requires 1 limb more than b. If
1784 * A->p[n - 1] * b / b == A->p[n - 1], then A * b fits in the same
1785 * number of limbs as A and the call to grow() is not required since
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001786 * copy() will take care of the growth if needed. However, experimentally,
1787 * making the call to grow() unconditional causes slightly fewer
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001788 * calls to calloc() in ECP code, presumably because it reuses the
1789 * same mpi for a while and this way the mpi is more likely to directly
1790 * grow to its final size. */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001791 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n + 1 ) );
1792 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
1793 mpi_mul_hlp( n, A->p, X->p, b - 1 );
1794
1795cleanup:
1796 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001797}
1798
1799/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001800 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1801 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001802 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001803static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1,
1804 mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r )
Simon Butcher15b15d12015-11-26 19:35:03 +00001805{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001806#if defined(MBEDTLS_HAVE_UDBL)
1807 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001808#else
Simon Butcher9803d072016-01-03 00:24:34 +00001809 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
1810 const mbedtls_mpi_uint uint_halfword_mask = ( (mbedtls_mpi_uint) 1 << biH ) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001811 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1812 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001813 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001814#endif
1815
Simon Butcher15b15d12015-11-26 19:35:03 +00001816 /*
1817 * Check for overflow
1818 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001819 if( 0 == d || u1 >= d )
Simon Butcher15b15d12015-11-26 19:35:03 +00001820 {
Simon Butcherf5ba0452015-12-27 23:01:55 +00001821 if (r != NULL) *r = ~0;
Simon Butcher15b15d12015-11-26 19:35:03 +00001822
Simon Butcherf5ba0452015-12-27 23:01:55 +00001823 return ( ~0 );
Simon Butcher15b15d12015-11-26 19:35:03 +00001824 }
1825
1826#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001827 dividend = (mbedtls_t_udbl) u1 << biL;
1828 dividend |= (mbedtls_t_udbl) u0;
1829 quotient = dividend / d;
1830 if( quotient > ( (mbedtls_t_udbl) 1 << biL ) - 1 )
1831 quotient = ( (mbedtls_t_udbl) 1 << biL ) - 1;
1832
1833 if( r != NULL )
Simon Butcher9803d072016-01-03 00:24:34 +00001834 *r = (mbedtls_mpi_uint)( dividend - (quotient * d ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001835
1836 return (mbedtls_mpi_uint) quotient;
1837#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001838
1839 /*
1840 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1841 * Vol. 2 - Seminumerical Algorithms, Knuth
1842 */
1843
1844 /*
1845 * Normalize the divisor, d, and dividend, u0, u1
1846 */
1847 s = mbedtls_clz( d );
1848 d = d << s;
1849
1850 u1 = u1 << s;
Simon Butcher9803d072016-01-03 00:24:34 +00001851 u1 |= ( u0 >> ( biL - s ) ) & ( -(mbedtls_mpi_sint)s >> ( biL - 1 ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001852 u0 = u0 << s;
1853
1854 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001855 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001856
1857 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001858 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001859
1860 /*
1861 * Find the first quotient and remainder
1862 */
1863 q1 = u1 / d1;
1864 r0 = u1 - d1 * q1;
1865
1866 while( q1 >= radix || ( q1 * d0 > radix * r0 + u0_msw ) )
1867 {
1868 q1 -= 1;
1869 r0 += d1;
1870
1871 if ( r0 >= radix ) break;
1872 }
1873
Simon Butcherf5ba0452015-12-27 23:01:55 +00001874 rAX = ( u1 * radix ) + ( u0_msw - q1 * d );
Simon Butcher15b15d12015-11-26 19:35:03 +00001875 q0 = rAX / d1;
1876 r0 = rAX - q0 * d1;
1877
1878 while( q0 >= radix || ( q0 * d0 > radix * r0 + u0_lsw ) )
1879 {
1880 q0 -= 1;
1881 r0 += d1;
1882
1883 if ( r0 >= radix ) break;
1884 }
1885
1886 if (r != NULL)
Simon Butcherf5ba0452015-12-27 23:01:55 +00001887 *r = ( rAX * radix + u0_lsw - q0 * d ) >> s;
Simon Butcher15b15d12015-11-26 19:35:03 +00001888
1889 quotient = q1 * radix + q0;
1890
1891 return quotient;
1892#endif
1893}
1894
1895/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001897 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001898int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1899 const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001900{
Janos Follath24eed8d2019-11-22 13:21:35 +00001901 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001902 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 mbedtls_mpi X, Y, Z, T1, T2;
Alexander Kd19a1932019-11-01 18:20:42 +03001904 mbedtls_mpi_uint TP2[3];
Hanno Becker73d7d792018-12-11 10:35:51 +00001905 MPI_VALIDATE_RET( A != NULL );
1906 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 if( mbedtls_mpi_cmp_int( B, 0 ) == 0 )
1909 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Alexander K35d6d462019-10-31 14:46:45 +03001912 mbedtls_mpi_init( &T1 );
Alexander Kd19a1932019-11-01 18:20:42 +03001913 /*
1914 * Avoid dynamic memory allocations for constant-size T2.
1915 *
1916 * T2 is used for comparison only and the 3 limbs are assigned explicitly,
1917 * so nobody increase the size of the MPI and we're safe to use an on-stack
1918 * buffer.
1919 */
Alexander K35d6d462019-10-31 14:46:45 +03001920 T2.s = 1;
Alexander Kd19a1932019-11-01 18:20:42 +03001921 T2.n = sizeof( TP2 ) / sizeof( *TP2 );
1922 T2.p = TP2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 if( Q != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_lset( Q, 0 ) );
1927 if( R != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001928 return( 0 );
1929 }
1930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &X, A ) );
1932 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001933 X.s = Y.s = 1;
1934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &Z, A->n + 2 ) );
1936 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Z, 0 ) );
Gilles Peskine2536aa72020-07-24 00:12:59 +02001937 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, A->n + 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001938
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001939 k = mbedtls_mpi_bitlen( &Y ) % biL;
Paul Bakkerf9688572011-05-05 10:00:45 +00001940 if( k < biL - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001941 {
1942 k = biL - 1 - k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &X, k ) );
1944 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, k ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001945 }
1946 else k = 0;
1947
1948 n = X.n - 1;
1949 t = Y.n - 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952 while( mbedtls_mpi_cmp_mpi( &X, &Y ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001953 {
1954 Z.p[n - t]++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001956 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001958
1959 for( i = n; i > t ; i-- )
1960 {
1961 if( X.p[i] >= Y.p[t] )
1962 Z.p[i - t - 1] = ~0;
1963 else
1964 {
Simon Butcher15b15d12015-11-26 19:35:03 +00001965 Z.p[i - t - 1] = mbedtls_int_div_int( X.p[i], X.p[i - 1],
1966 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001967 }
1968
Alexander K35d6d462019-10-31 14:46:45 +03001969 T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2];
1970 T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1];
1971 T2.p[2] = X.p[i];
1972
Paul Bakker5121ce52009-01-03 21:22:43 +00001973 Z.p[i - t - 1]++;
1974 do
1975 {
1976 Z.p[i - t - 1]--;
1977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T1, 0 ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001979 T1.p[0] = ( t < 1 ) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001980 T1.p[1] = Y.p[t];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001982 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 while( mbedtls_mpi_cmp_mpi( &T1, &T2 ) > 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
1986 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1987 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 if( mbedtls_mpi_cmp_int( &X, 0 ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &Y ) );
1992 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1993 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001994 Z.p[i - t - 1]--;
1995 }
1996 }
1997
1998 if( Q != NULL )
1999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( Q, &Z ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002001 Q->s = A->s * B->s;
2002 }
2003
2004 if( R != NULL )
2005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &X, k ) );
Paul Bakkerf02c5642012-11-13 10:25:21 +00002007 X.s = A->s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, &X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 if( mbedtls_mpi_cmp_int( R, 0 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002011 R->s = 1;
2012 }
2013
2014cleanup:
2015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Alexander K35d6d462019-10-31 14:46:45 +03002017 mbedtls_mpi_free( &T1 );
Alexander Kd19a1932019-11-01 18:20:42 +03002018 mbedtls_platform_zeroize( TP2, sizeof( TP2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002019
2020 return( ret );
2021}
2022
2023/*
2024 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00002025 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002026int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R,
2027 const mbedtls_mpi *A,
2028 mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00002029{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01002030 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00002032 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002033
2034 p[0] = ( b < 0 ) ? -b : b;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01002035 B.s = ( b < 0 ) ? -1 : 1;
2036 B.n = 1;
2037 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002038
Yuto Takano36c8ddc2021-07-05 09:10:52 +01002039 return( mbedtls_mpi_div_mpi( Q, R, A, &B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002040}
2041
2042/*
2043 * Modulo: R = A mod B
2044 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00002046{
Janos Follath24eed8d2019-11-22 13:21:35 +00002047 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker73d7d792018-12-11 10:35:51 +00002048 MPI_VALIDATE_RET( R != NULL );
2049 MPI_VALIDATE_RET( A != NULL );
2050 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 if( mbedtls_mpi_cmp_int( B, 0 ) < 0 )
2053 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakkerce40a6d2009-06-23 19:46:08 +00002054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( NULL, R, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057 while( mbedtls_mpi_cmp_int( R, 0 ) < 0 )
2058 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060 while( mbedtls_mpi_cmp_mpi( R, B ) >= 0 )
2061 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002062
2063cleanup:
2064
2065 return( ret );
2066}
2067
2068/*
2069 * Modulo: r = A mod b
2070 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00002072{
Paul Bakker23986e52011-04-24 08:57:21 +00002073 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 mbedtls_mpi_uint x, y, z;
Hanno Becker73d7d792018-12-11 10:35:51 +00002075 MPI_VALIDATE_RET( r != NULL );
2076 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002077
2078 if( b == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00002080
2081 if( b < 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002083
2084 /*
2085 * handle trivial cases
2086 */
2087 if( b == 1 )
2088 {
2089 *r = 0;
2090 return( 0 );
2091 }
2092
2093 if( b == 2 )
2094 {
2095 *r = A->p[0] & 1;
2096 return( 0 );
2097 }
2098
2099 /*
2100 * general case
2101 */
Paul Bakker23986e52011-04-24 08:57:21 +00002102 for( i = A->n, y = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 {
Paul Bakker23986e52011-04-24 08:57:21 +00002104 x = A->p[i - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 y = ( y << biH ) | ( x >> biH );
2106 z = y / b;
2107 y -= z * b;
2108
2109 x <<= biH;
2110 y = ( y << biH ) | ( x >> biH );
2111 z = y / b;
2112 y -= z * b;
2113 }
2114
Paul Bakkerce40a6d2009-06-23 19:46:08 +00002115 /*
2116 * If A is negative, then the current y represents a negative value.
2117 * Flipping it to the positive side.
2118 */
2119 if( A->s < 0 && y != 0 )
2120 y = b - y;
2121
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 *r = y;
2123
2124 return( 0 );
2125}
2126
2127/*
2128 * Fast Montgomery initialization (thanks to Tom St Denis)
2129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130static void mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002131{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 mbedtls_mpi_uint x, m0 = N->p[0];
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01002133 unsigned int i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002134
2135 x = m0;
2136 x += ( ( m0 + 2 ) & 4 ) << 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002137
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01002138 for( i = biL; i >= 8; i /= 2 )
2139 x *= ( 2 - ( m0 * x ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002140
2141 *mm = ~x + 1;
2142}
2143
Gilles Peskine2a82f722020-06-04 15:00:49 +02002144/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
2145 *
2146 * \param[in,out] A One of the numbers to multiply.
Gilles Peskine221626f2020-06-08 22:37:50 +02002147 * It must have at least as many limbs as N
2148 * (A->n >= N->n), and any limbs beyond n are ignored.
Gilles Peskine2a82f722020-06-04 15:00:49 +02002149 * On successful completion, A contains the result of
2150 * the multiplication A * B * R^-1 mod N where
2151 * R = (2^ciL)^n.
2152 * \param[in] B One of the numbers to multiply.
2153 * It must be nonzero and must not have more limbs than N
2154 * (B->n <= N->n).
2155 * \param[in] N The modulo. N must be odd.
2156 * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
2157 * This is -N^-1 mod 2^ciL.
2158 * \param[in,out] T A bignum for temporary storage.
2159 * It must be at least twice the limb size of N plus 2
2160 * (T->n >= 2 * (N->n + 1)).
2161 * Its initial content is unused and
2162 * its final content is indeterminate.
2163 * Note that unlike the usual convention in the library
2164 * for `const mbedtls_mpi*`, the content of T can change.
Paul Bakker5121ce52009-01-03 21:22:43 +00002165 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002166static 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 +02002167 const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00002168{
Paul Bakker23986e52011-04-24 08:57:21 +00002169 size_t i, n, m;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 mbedtls_mpi_uint u0, u1, *d;
Paul Bakker5121ce52009-01-03 21:22:43 +00002171
2172 memset( T->p, 0, T->n * ciL );
2173
2174 d = T->p;
2175 n = N->n;
2176 m = ( B->n < n ) ? B->n : n;
2177
2178 for( i = 0; i < n; i++ )
2179 {
2180 /*
2181 * T = (T + u0*B + u1*N) / 2^biL
2182 */
2183 u0 = A->p[i];
2184 u1 = ( d[0] + u0 * B->p[0] ) * mm;
2185
2186 mpi_mul_hlp( m, B->p, d, u0 );
2187 mpi_mul_hlp( n, N->p, d, u1 );
2188
2189 *d++ = u0; d[n + 1] = 0;
2190 }
2191
Gilles Peskine221626f2020-06-08 22:37:50 +02002192 /* At this point, d is either the desired result or the desired result
2193 * plus N. We now potentially subtract N, avoiding leaking whether the
2194 * subtraction is performed through side channels. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002195
Gilles Peskine221626f2020-06-08 22:37:50 +02002196 /* Copy the n least significant limbs of d to A, so that
2197 * A = d if d < N (recall that N has n limbs). */
2198 memcpy( A->p, d, n * ciL );
Gilles Peskine09ec10a2020-06-09 10:39:38 +02002199 /* If d >= N then we want to set A to d - N. To prevent timing attacks,
Gilles Peskine221626f2020-06-08 22:37:50 +02002200 * do the calculation without using conditional tests. */
2201 /* Set d to d0 + (2^biL)^n - N where d0 is the current value of d. */
Gilles Peskine132c0972020-06-04 21:05:24 +02002202 d[n] += 1;
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02002203 d[n] -= mpi_sub_hlp( n, d, d, N->p );
Gilles Peskine221626f2020-06-08 22:37:50 +02002204 /* If d0 < N then d < (2^biL)^n
2205 * so d[n] == 0 and we want to keep A as it is.
2206 * If d0 >= N then d >= (2^biL)^n, and d <= (2^biL)^n + N < 2 * (2^biL)^n
2207 * so d[n] == 1 and we want to set A to the result of the subtraction
2208 * which is d - (2^biL)^n, i.e. the n least significant limbs of d.
2209 * This exactly corresponds to a conditional assignment. */
2210 mpi_safe_cond_assign( n, A->p, d, (unsigned char) d[n] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002211}
2212
2213/*
2214 * Montgomery reduction: A = A * R^-1 mod N
Gilles Peskine2a82f722020-06-04 15:00:49 +02002215 *
2216 * See mpi_montmul() regarding constraints and guarantees on the parameters.
Paul Bakker5121ce52009-01-03 21:22:43 +00002217 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002218static void mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N,
2219 mbedtls_mpi_uint mm, const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00002220{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 mbedtls_mpi_uint z = 1;
2222 mbedtls_mpi U;
Paul Bakker5121ce52009-01-03 21:22:43 +00002223
Paul Bakker8ddb6452013-02-27 14:56:33 +01002224 U.n = U.s = (int) z;
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 U.p = &z;
2226
Gilles Peskine4e91d472020-06-04 20:55:15 +02002227 mpi_montmul( A, &U, N, mm, T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002228}
2229
2230/*
Manuel Pégourié-Gonnard92413ef2021-06-03 10:42:46 +02002231 * Constant-flow boolean "equal" comparison:
2232 * return x == y
2233 *
2234 * This function can be used to write constant-time code by replacing branches
2235 * with bit operations - it can be used in conjunction with
2236 * mbedtls_ssl_cf_mask_from_bit().
2237 *
2238 * This function is implemented without using comparison operators, as those
2239 * might be translated to branches by some compilers on some platforms.
2240 */
2241static size_t mbedtls_mpi_cf_bool_eq( size_t x, size_t y )
2242{
2243 /* diff = 0 if x == y, non-zero otherwise */
2244 const size_t diff = x ^ y;
2245
2246 /* MSVC has a warning about unary minus on unsigned integer types,
2247 * but this is well-defined and precisely what we want to do here. */
2248#if defined(_MSC_VER)
2249#pragma warning( push )
2250#pragma warning( disable : 4146 )
2251#endif
2252
2253 /* diff_msb's most significant bit is equal to x != y */
Manuel Pégourié-Gonnarda48b16a2021-06-17 13:25:03 +02002254 const size_t diff_msb = ( diff | (size_t) -diff );
Manuel Pégourié-Gonnard92413ef2021-06-03 10:42:46 +02002255
2256#if defined(_MSC_VER)
2257#pragma warning( pop )
2258#endif
2259
2260 /* diff1 = (x != y) ? 1 : 0 */
2261 const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 );
2262
2263 return( 1 ^ diff1 );
2264}
2265
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01002266/**
2267 * Select an MPI from a table without leaking the index.
2268 *
2269 * This is functionally equivalent to mbedtls_mpi_copy(R, T[idx]) except it
2270 * reads the entire table in order to avoid leaking the value of idx to an
2271 * attacker able to observe memory access patterns.
2272 *
2273 * \param[out] R Where to write the selected MPI.
2274 * \param[in] T The table to read from.
2275 * \param[in] T_size The number of elements in the table.
2276 * \param[in] idx The index of the element to select;
2277 * this must satisfy 0 <= idx < T_size.
2278 *
2279 * \return \c 0 on success, or a negative error code.
2280 */
2281static int mpi_select( mbedtls_mpi *R, const mbedtls_mpi *T, size_t T_size, size_t idx )
2282{
2283 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2284
2285 for( size_t i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard92413ef2021-06-03 10:42:46 +02002286 {
2287 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( R, &T[i],
Manuel Pégourié-Gonnarde22176e2021-06-10 09:34:00 +02002288 (unsigned char) mbedtls_mpi_cf_bool_eq( i, idx ) ) );
Manuel Pégourié-Gonnard92413ef2021-06-03 10:42:46 +02002289 }
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01002290
2291cleanup:
2292 return( ret );
2293}
2294
Paul Bakker5121ce52009-01-03 21:22:43 +00002295/*
2296 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
2297 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002298int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
2299 const mbedtls_mpi *E, const mbedtls_mpi *N,
2300 mbedtls_mpi *_RR )
Paul Bakker5121ce52009-01-03 21:22:43 +00002301{
Janos Follath24eed8d2019-11-22 13:21:35 +00002302 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002303 size_t wbits, wsize, one = 1;
2304 size_t i, j, nblimbs;
2305 size_t bufsize, nbits;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 mbedtls_mpi_uint ei, mm, state;
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01002307 mbedtls_mpi RR, T, W[ 1 << MBEDTLS_MPI_WINDOW_SIZE ], WW, Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00002308 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002309
Hanno Becker73d7d792018-12-11 10:35:51 +00002310 MPI_VALIDATE_RET( X != NULL );
2311 MPI_VALIDATE_RET( A != NULL );
2312 MPI_VALIDATE_RET( E != NULL );
2313 MPI_VALIDATE_RET( N != NULL );
2314
Hanno Becker8d1dd1b2017-09-28 11:02:24 +01002315 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 || ( N->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 if( mbedtls_mpi_cmp_int( E, 0 ) < 0 )
2319 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002320
Chris Jones9246d042020-11-25 15:12:39 +00002321 if( mbedtls_mpi_bitlen( E ) > MBEDTLS_MPI_MAX_BITS ||
2322 mbedtls_mpi_bitlen( N ) > MBEDTLS_MPI_MAX_BITS )
2323 return ( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2324
Paul Bakkerf6198c12012-05-16 08:02:29 +00002325 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002326 * Init temps and window size
2327 */
2328 mpi_montg_init( &mm, N );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &T );
2330 mbedtls_mpi_init( &Apos );
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01002331 mbedtls_mpi_init( &WW );
Paul Bakker5121ce52009-01-03 21:22:43 +00002332 memset( W, 0, sizeof( W ) );
2333
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002334 i = mbedtls_mpi_bitlen( E );
Paul Bakker5121ce52009-01-03 21:22:43 +00002335
2336 wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
2337 ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1;
2338
Peter Kolbuse6bcad32018-12-11 14:01:44 -06002339#if( MBEDTLS_MPI_WINDOW_SIZE < 6 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 if( wsize > MBEDTLS_MPI_WINDOW_SIZE )
2341 wsize = MBEDTLS_MPI_WINDOW_SIZE;
Peter Kolbuse6bcad32018-12-11 14:01:44 -06002342#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00002343
Paul Bakker5121ce52009-01-03 21:22:43 +00002344 j = N->n + 1;
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02002345 /* All W[i] and X must have at least N->n limbs for the mpi_montmul()
2346 * and mpi_montred() calls later. Here we ensure that W[1] and X are
2347 * large enough, and later we'll grow other W[i] to the same length.
2348 * They must not be shrunk midway through this function!
2349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
2351 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], j ) );
2352 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T, j * 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002353
2354 /*
Paul Bakker50546922012-05-19 08:40:49 +00002355 * Compensate for negative A (and correct at the end)
2356 */
2357 neg = ( A->s == -1 );
Paul Bakker50546922012-05-19 08:40:49 +00002358 if( neg )
2359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Apos, A ) );
Paul Bakker50546922012-05-19 08:40:49 +00002361 Apos.s = 1;
2362 A = &Apos;
2363 }
2364
2365 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002366 * If 1st call, pre-compute R^2 mod N
2367 */
2368 if( _RR == NULL || _RR->p == NULL )
2369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &RR, 1 ) );
2371 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &RR, N->n * 2 * biL ) );
2372 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &RR, &RR, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002373
2374 if( _RR != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 memcpy( _RR, &RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002376 }
2377 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378 memcpy( &RR, _RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002379
2380 /*
2381 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
2382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 if( mbedtls_mpi_cmp_mpi( A, N ) >= 0 )
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02002384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &W[1], A, N ) );
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02002386 /* This should be a no-op because W[1] is already that large before
2387 * mbedtls_mpi_mod_mpi(), but it's necessary to avoid an overflow
2388 * in mpi_montmul() below, so let's make sure. */
Gilles Peskine2aa3f162021-06-15 21:22:48 +02002389 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], N->n + 1 ) );
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02002390 }
Paul Bakkerc2024f42014-01-23 20:38:35 +01002391 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[1], A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002393
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02002394 /* Note that this is safe because W[1] always has at least N->n limbs
2395 * (it grew above and was preserved by mbedtls_mpi_copy()). */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002396 mpi_montmul( &W[1], &RR, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002397
2398 /*
2399 * X = R^2 * R^-1 mod N = R mod N
2400 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &RR ) );
Gilles Peskine4e91d472020-06-04 20:55:15 +02002402 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002403
2404 if( wsize > 1 )
2405 {
2406 /*
2407 * W[1 << (wsize - 1)] = W[1] ^ (wsize - 1)
2408 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002409 j = one << ( wsize - 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[j], N->n + 1 ) );
2412 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[j], &W[1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002413
2414 for( i = 0; i < wsize - 1; i++ )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002415 mpi_montmul( &W[j], &W[j], N, mm, &T );
Paul Bakker0d7702c2013-10-29 16:18:35 +01002416
Paul Bakker5121ce52009-01-03 21:22:43 +00002417 /*
2418 * W[i] = W[i - 1] * W[1]
2419 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002420 for( i = j + 1; i < ( one << wsize ); i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[i], N->n + 1 ) );
2423 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[i], &W[i - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002424
Gilles Peskine4e91d472020-06-04 20:55:15 +02002425 mpi_montmul( &W[i], &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 }
2427 }
2428
2429 nblimbs = E->n;
2430 bufsize = 0;
2431 nbits = 0;
2432 wbits = 0;
2433 state = 0;
2434
2435 while( 1 )
2436 {
2437 if( bufsize == 0 )
2438 {
Paul Bakker0d7702c2013-10-29 16:18:35 +01002439 if( nblimbs == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002440 break;
2441
Paul Bakker0d7702c2013-10-29 16:18:35 +01002442 nblimbs--;
2443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 bufsize = sizeof( mbedtls_mpi_uint ) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00002445 }
2446
2447 bufsize--;
2448
2449 ei = (E->p[nblimbs] >> bufsize) & 1;
2450
2451 /*
2452 * skip leading 0s
2453 */
2454 if( ei == 0 && state == 0 )
2455 continue;
2456
2457 if( ei == 0 && state == 1 )
2458 {
2459 /*
2460 * out of window, square X
2461 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002462 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002463 continue;
2464 }
2465
2466 /*
2467 * add ei to current window
2468 */
2469 state = 2;
2470
2471 nbits++;
Paul Bakker66d5d072014-06-17 16:39:18 +02002472 wbits |= ( ei << ( wsize - nbits ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002473
2474 if( nbits == wsize )
2475 {
2476 /*
2477 * X = X^wsize R^-1 mod N
2478 */
2479 for( i = 0; i < wsize; i++ )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002480 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002481
2482 /*
2483 * X = X * W[wbits] R^-1 mod N
2484 */
Manuel Pégourié-Gonnarde22176e2021-06-10 09:34:00 +02002485 MBEDTLS_MPI_CHK( mpi_select( &WW, W, (size_t) 1 << wsize, wbits ) );
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01002486 mpi_montmul( X, &WW, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002487
2488 state--;
2489 nbits = 0;
2490 wbits = 0;
2491 }
2492 }
2493
2494 /*
2495 * process the remaining bits
2496 */
2497 for( i = 0; i < nbits; i++ )
2498 {
Gilles Peskine4e91d472020-06-04 20:55:15 +02002499 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002500
2501 wbits <<= 1;
2502
Paul Bakker66d5d072014-06-17 16:39:18 +02002503 if( ( wbits & ( one << wsize ) ) != 0 )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002504 mpi_montmul( X, &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002505 }
2506
2507 /*
2508 * X = A^E * R * R^-1 mod N = A^E mod N
2509 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002510 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002511
Hanno Beckera4af1c42017-04-18 09:07:45 +01002512 if( neg && E->n != 0 && ( E->p[0] & 1 ) != 0 )
Paul Bakkerf6198c12012-05-16 08:02:29 +00002513 {
2514 X->s = -1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, N, X ) );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002516 }
2517
Paul Bakker5121ce52009-01-03 21:22:43 +00002518cleanup:
2519
Paul Bakker66d5d072014-06-17 16:39:18 +02002520 for( i = ( one << ( wsize - 1 ) ); i < ( one << wsize ); i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521 mbedtls_mpi_free( &W[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 mbedtls_mpi_free( &W[1] ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &Apos );
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01002524 mbedtls_mpi_free( &WW );
Paul Bakker6c591fa2011-05-05 11:49:20 +00002525
Paul Bakker75a28602014-03-31 12:08:17 +02002526 if( _RR == NULL || _RR->p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002528
2529 return( ret );
2530}
2531
Paul Bakker5121ce52009-01-03 21:22:43 +00002532/*
2533 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
2534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00002536{
Janos Follath24eed8d2019-11-22 13:21:35 +00002537 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002538 size_t lz, lzt;
Alexander Ke8ad49f2019-08-16 16:16:07 +03002539 mbedtls_mpi TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00002540
Hanno Becker73d7d792018-12-11 10:35:51 +00002541 MPI_VALIDATE_RET( G != NULL );
2542 MPI_VALIDATE_RET( A != NULL );
2543 MPI_VALIDATE_RET( B != NULL );
2544
Alexander Ke8ad49f2019-08-16 16:16:07 +03002545 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) );
2548 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550 lz = mbedtls_mpi_lsb( &TA );
2551 lzt = mbedtls_mpi_lsb( &TB );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002552
Gilles Peskine27253bc2021-06-09 13:26:43 +02002553 /* The loop below gives the correct result when A==0 but not when B==0.
2554 * So have a special case for B==0. Leverage the fact that we just
2555 * calculated the lsb and lsb(B)==0 iff B is odd or 0 to make the test
2556 * slightly more efficient than cmp_int(). */
2557 if( lzt == 0 && mbedtls_mpi_get_bit( &TB, 0 ) == 0 )
2558 {
2559 ret = mbedtls_mpi_copy( G, A );
2560 goto cleanup;
2561 }
2562
Paul Bakker66d5d072014-06-17 16:39:18 +02002563 if( lzt < lz )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002564 lz = lzt;
2565
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 TA.s = TB.s = 1;
2567
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002568 /* We mostly follow the procedure described in HAC 14.54, but with some
2569 * minor differences:
2570 * - Sequences of multiplications or divisions by 2 are grouped into a
2571 * single shift operation.
Gilles Peskineb09c7ee2021-06-21 18:58:39 +02002572 * - The procedure in HAC assumes that 0 < TB <= TA.
2573 * - The condition TB <= TA is not actually necessary for correctness.
2574 * TA and TB have symmetric roles except for the loop termination
2575 * condition, and the shifts at the beginning of the loop body
2576 * remove any significance from the ordering of TA vs TB before
2577 * the shifts.
2578 * - If TA = 0, the loop goes through 0 iterations and the result is
2579 * correctly TB.
2580 * - The case TB = 0 was short-circuited above.
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002581 *
2582 * For the correctness proof below, decompose the original values of
2583 * A and B as
2584 * A = sa * 2^a * A' with A'=0 or A' odd, and sa = +-1
2585 * B = sb * 2^b * B' with B'=0 or B' odd, and sb = +-1
2586 * Then gcd(A, B) = 2^{min(a,b)} * gcd(A',B'),
2587 * and gcd(A',B') is odd or 0.
2588 *
2589 * At the beginning, we have TA = |A| and TB = |B| so gcd(A,B) = gcd(TA,TB).
2590 * The code maintains the following invariant:
2591 * gcd(A,B) = 2^k * gcd(TA,TB) for some k (I)
Gilles Peskine4df3f1f2021-06-15 22:09:39 +02002592 */
2593
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002594 /* Proof that the loop terminates:
2595 * At each iteration, either the right-shift by 1 is made on a nonzero
2596 * value and the nonnegative integer bitlen(TA) + bitlen(TB) decreases
2597 * by at least 1, or the right-shift by 1 is made on zero and then
2598 * TA becomes 0 which ends the loop (TB cannot be 0 if it is right-shifted
2599 * since in that case TB is calculated from TB-TA with the condition TB>TA).
2600 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601 while( mbedtls_mpi_cmp_int( &TA, 0 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002602 {
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002603 /* Divisions by 2 preserve the invariant (I). */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, mbedtls_mpi_lsb( &TA ) ) );
2605 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, mbedtls_mpi_lsb( &TB ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002606
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002607 /* Set either TA or TB to |TA-TB|/2. Since TA and TB are both odd,
2608 * TA-TB is even so the division by 2 has an integer result.
2609 * Invariant (I) is preserved since any odd divisor of both TA and TB
2610 * also divides |TA-TB|/2, and any odd divisor of both TA and |TA-TB|/2
2611 * also divides TB, and any odd divisior of both TB and |TA-TB|/2 also
2612 * divides TA.
2613 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614 if( mbedtls_mpi_cmp_mpi( &TA, &TB ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TA, &TA, &TB ) );
2617 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 }
2619 else
2620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TB, &TB, &TA ) );
2622 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002623 }
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002624 /* Note that one of TA or TB is still odd. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 }
2626
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002627 /* By invariant (I), gcd(A,B) = 2^k * gcd(TA,TB) for some k.
2628 * At the loop exit, TA = 0, so gcd(TA,TB) = TB.
2629 * - If there was at least one loop iteration, then one of TA or TB is odd,
2630 * and TA = 0, so TB is odd and gcd(TA,TB) = gcd(A',B'). In this case,
2631 * lz = min(a,b) so gcd(A,B) = 2^lz * TB.
2632 * - If there was no loop iteration, then A was 0, and gcd(A,B) = B.
Gilles Peskine4d3fd362021-06-21 11:40:38 +02002633 * In this case, lz = 0 and B = TB so gcd(A,B) = B = 2^lz * TB as well.
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002634 */
2635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &TB, lz ) );
2637 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( G, &TB ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
2639cleanup:
2640
Alexander Ke8ad49f2019-08-16 16:16:07 +03002641 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002642
2643 return( ret );
2644}
2645
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002646/* Fill X with n_bytes random bytes.
2647 * X must already have room for those bytes.
Gilles Peskineafb2bd22021-06-03 11:51:09 +02002648 * The ordering of the bytes returned from the RNG is suitable for
2649 * deterministic ECDSA (see RFC 6979 §3.3 and mbedtls_mpi_random()).
Gilles Peskineebe9b6a2021-04-13 21:55:35 +02002650 * The size and sign of X are unchanged.
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002651 * n_bytes must not be 0.
2652 */
2653static int mpi_fill_random_internal(
2654 mbedtls_mpi *X, size_t n_bytes,
2655 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
2656{
2657 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2658 const size_t limbs = CHARS_TO_LIMBS( n_bytes );
2659 const size_t overhead = ( limbs * ciL ) - n_bytes;
2660
2661 if( X->n < limbs )
2662 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002663
Gilles Peskineebe9b6a2021-04-13 21:55:35 +02002664 memset( X->p, 0, overhead );
2665 memset( (unsigned char *) X->p + limbs * ciL, 0, ( X->n - limbs ) * ciL );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002666 MBEDTLS_MPI_CHK( f_rng( p_rng, (unsigned char *) X->p + overhead, n_bytes ) );
2667 mpi_bigendian_to_host( X->p, limbs );
2668
2669cleanup:
2670 return( ret );
2671}
2672
Paul Bakker33dc46b2014-04-30 16:11:39 +02002673/*
2674 * Fill X with size bytes of random.
2675 *
2676 * Use a temporary bytes representation to make sure the result is the same
Paul Bakkerc37b0ac2014-05-01 14:19:23 +02002677 * regardless of the platform endianness (useful when f_rng is actually
Paul Bakker33dc46b2014-04-30 16:11:39 +02002678 * deterministic, eg for tests).
2679 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002681 int (*f_rng)(void *, unsigned char *, size_t),
2682 void *p_rng )
Paul Bakker287781a2011-03-26 13:18:49 +00002683{
Janos Follath24eed8d2019-11-22 13:21:35 +00002684 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6dab6202019-01-02 16:42:29 +00002685 size_t const limbs = CHARS_TO_LIMBS( size );
Hanno Beckerda1655a2017-10-18 14:21:44 +01002686
Hanno Becker8ce11a32018-12-19 16:18:52 +00002687 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002688 MPI_VALIDATE_RET( f_rng != NULL );
Paul Bakker33dc46b2014-04-30 16:11:39 +02002689
Hanno Beckerda1655a2017-10-18 14:21:44 +01002690 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskineed32b572021-06-02 22:17:52 +02002691 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002692 if( size == 0 )
2693 return( 0 );
Paul Bakker287781a2011-03-26 13:18:49 +00002694
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002695 ret = mpi_fill_random_internal( X, size, f_rng, p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +00002696
2697cleanup:
2698 return( ret );
2699}
2700
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002701int mbedtls_mpi_random( mbedtls_mpi *X,
2702 mbedtls_mpi_sint min,
2703 const mbedtls_mpi *N,
2704 int (*f_rng)(void *, unsigned char *, size_t),
2705 void *p_rng )
2706{
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002707 int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskinee5381682021-04-13 21:23:25 +02002708 int count;
Gilles Peskine5b0589e2021-04-13 21:09:10 +02002709 unsigned lt_lower = 1, lt_upper = 0;
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002710 size_t n_bits = mbedtls_mpi_bitlen( N );
2711 size_t n_bytes = ( n_bits + 7 ) / 8;
Gilles Peskine5b0589e2021-04-13 21:09:10 +02002712 mbedtls_mpi lower_bound;
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002713
Gilles Peskine1e918f42021-03-29 22:14:51 +02002714 if( min < 0 )
2715 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2716 if( mbedtls_mpi_cmp_int( N, min ) <= 0 )
2717 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2718
Gilles Peskinee5381682021-04-13 21:23:25 +02002719 /*
2720 * When min == 0, each try has at worst a probability 1/2 of failing
2721 * (the msb has a probability 1/2 of being 0, and then the result will
2722 * be < N), so after 30 tries failure probability is a most 2**(-30).
2723 *
2724 * When N is just below a power of 2, as is the case when generating
Gilles Peskinee842e582021-04-15 11:45:19 +02002725 * a random scalar on most elliptic curves, 1 try is enough with
Gilles Peskinee5381682021-04-13 21:23:25 +02002726 * overwhelming probability. When N is just above a power of 2,
Gilles Peskinee842e582021-04-15 11:45:19 +02002727 * as when generating a random scalar on secp224k1, each try has
Gilles Peskinee5381682021-04-13 21:23:25 +02002728 * a probability of failing that is almost 1/2.
2729 *
2730 * The probabilities are almost the same if min is nonzero but negligible
2731 * compared to N. This is always the case when N is crypto-sized, but
2732 * it's convenient to support small N for testing purposes. When N
2733 * is small, use a higher repeat count, otherwise the probability of
2734 * failure is macroscopic.
2735 */
Gilles Peskine87823d72021-06-02 21:18:59 +02002736 count = ( n_bytes > 4 ? 30 : 250 );
Gilles Peskinee5381682021-04-13 21:23:25 +02002737
Gilles Peskine5b0589e2021-04-13 21:09:10 +02002738 mbedtls_mpi_init( &lower_bound );
2739
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002740 /* Ensure that target MPI has exactly the same number of limbs
2741 * as the upper bound, even if the upper bound has leading zeros.
2742 * This is necessary for the mbedtls_mpi_lt_mpi_ct() check. */
Gilles Peskineed32b572021-06-02 22:17:52 +02002743 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, N->n ) );
Gilles Peskine5b0589e2021-04-13 21:09:10 +02002744 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &lower_bound, N->n ) );
2745 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &lower_bound, min ) );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002746
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002747 /*
2748 * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA)
2749 * when f_rng is a suitably parametrized instance of HMAC_DRBG:
2750 * - use the same byte ordering;
2751 * - keep the leftmost n_bits bits of the generated octet string;
2752 * - try until result is in the desired range.
2753 * This also avoids any bias, which is especially important for ECDSA.
2754 */
2755 do
2756 {
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002757 MBEDTLS_MPI_CHK( mpi_fill_random_internal( X, n_bytes, f_rng, p_rng ) );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002758 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, 8 * n_bytes - n_bits ) );
2759
Gilles Peskinee5381682021-04-13 21:23:25 +02002760 if( --count == 0 )
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002761 {
2762 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2763 goto cleanup;
2764 }
2765
Gilles Peskine5b0589e2021-04-13 21:09:10 +02002766 MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( X, &lower_bound, &lt_lower ) );
2767 MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( X, N, &lt_upper ) );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002768 }
Gilles Peskine5b0589e2021-04-13 21:09:10 +02002769 while( lt_lower != 0 || lt_upper == 0 );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002770
2771cleanup:
Gilles Peskine5b0589e2021-04-13 21:09:10 +02002772 mbedtls_mpi_free( &lower_bound );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002773 return( ret );
2774}
2775
Paul Bakker5121ce52009-01-03 21:22:43 +00002776/*
2777 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2778 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002780{
Janos Follath24eed8d2019-11-22 13:21:35 +00002781 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Hanno Becker73d7d792018-12-11 10:35:51 +00002783 MPI_VALIDATE_RET( X != NULL );
2784 MPI_VALIDATE_RET( A != NULL );
2785 MPI_VALIDATE_RET( N != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002786
Hanno Becker4bcb4912017-04-18 15:49:39 +01002787 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TU ); mbedtls_mpi_init( &U1 ); mbedtls_mpi_init( &U2 );
2791 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &TB ); mbedtls_mpi_init( &TV );
2792 mbedtls_mpi_init( &V1 ); mbedtls_mpi_init( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, A, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002799 goto cleanup;
2800 }
2801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &TA, A, N ) );
2803 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TU, &TA ) );
2804 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, N ) );
2805 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TV, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U1, 1 ) );
2808 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U2, 0 ) );
2809 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V1, 0 ) );
2810 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002811
2812 do
2813 {
2814 while( ( TU.p[0] & 1 ) == 0 )
2815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TU, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002817
2818 if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
2819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &U1, &U1, &TB ) );
2821 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002822 }
2823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U1, 1 ) );
2825 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002826 }
2827
2828 while( ( TV.p[0] & 1 ) == 0 )
2829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TV, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002831
2832 if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
2833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002834 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, &TB ) );
2835 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002836 }
2837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V1, 1 ) );
2839 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002840 }
2841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842 if( mbedtls_mpi_cmp_mpi( &TU, &TV ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TU, &TU, &TV ) );
2845 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U1, &U1, &V1 ) );
2846 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &V2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002847 }
2848 else
2849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TV, &TV, &TU ) );
2851 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, &U1 ) );
2852 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &U2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002853 }
2854 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855 while( mbedtls_mpi_cmp_int( &TU, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857 while( mbedtls_mpi_cmp_int( &V1, 0 ) < 0 )
2858 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860 while( mbedtls_mpi_cmp_mpi( &V1, N ) >= 0 )
2861 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &V1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002864
2865cleanup:
2866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TU ); mbedtls_mpi_free( &U1 ); mbedtls_mpi_free( &U2 );
2868 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TV );
2869 mbedtls_mpi_free( &V1 ); mbedtls_mpi_free( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002870
2871 return( ret );
2872}
2873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002875
Paul Bakker5121ce52009-01-03 21:22:43 +00002876static const int small_prime[] =
2877{
2878 3, 5, 7, 11, 13, 17, 19, 23,
2879 29, 31, 37, 41, 43, 47, 53, 59,
2880 61, 67, 71, 73, 79, 83, 89, 97,
2881 101, 103, 107, 109, 113, 127, 131, 137,
2882 139, 149, 151, 157, 163, 167, 173, 179,
2883 181, 191, 193, 197, 199, 211, 223, 227,
2884 229, 233, 239, 241, 251, 257, 263, 269,
2885 271, 277, 281, 283, 293, 307, 311, 313,
2886 317, 331, 337, 347, 349, 353, 359, 367,
2887 373, 379, 383, 389, 397, 401, 409, 419,
2888 421, 431, 433, 439, 443, 449, 457, 461,
2889 463, 467, 479, 487, 491, 499, 503, 509,
2890 521, 523, 541, 547, 557, 563, 569, 571,
2891 577, 587, 593, 599, 601, 607, 613, 617,
2892 619, 631, 641, 643, 647, 653, 659, 661,
2893 673, 677, 683, 691, 701, 709, 719, 727,
2894 733, 739, 743, 751, 757, 761, 769, 773,
2895 787, 797, 809, 811, 821, 823, 827, 829,
2896 839, 853, 857, 859, 863, 877, 881, 883,
2897 887, 907, 911, 919, 929, 937, 941, 947,
2898 953, 967, 971, 977, 983, 991, 997, -103
2899};
2900
2901/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002902 * Small divisors test (X must be positive)
2903 *
2904 * Return values:
2905 * 0: no small factor (possible prime, more tests needed)
2906 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002908 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002909 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910static int mpi_check_small_factors( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +00002911{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002912 int ret = 0;
2913 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914 mbedtls_mpi_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +00002915
Paul Bakker5121ce52009-01-03 21:22:43 +00002916 if( ( X->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002918
2919 for( i = 0; small_prime[i] > 0; i++ )
2920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 if( mbedtls_mpi_cmp_int( X, small_prime[i] ) <= 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002922 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, small_prime[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002925
2926 if( r == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002928 }
2929
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002930cleanup:
2931 return( ret );
2932}
2933
2934/*
2935 * Miller-Rabin pseudo-primality test (HAC 4.24)
2936 */
Janos Follathda31fa12018-09-03 14:45:23 +01002937static int mpi_miller_rabin( const mbedtls_mpi *X, size_t rounds,
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002938 int (*f_rng)(void *, unsigned char *, size_t),
2939 void *p_rng )
2940{
Pascal Junodb99183d2015-03-11 16:49:45 +01002941 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002942 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002944
Hanno Becker8ce11a32018-12-19 16:18:52 +00002945 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002946 MPI_VALIDATE_RET( f_rng != NULL );
2947
2948 mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R );
2949 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950 mbedtls_mpi_init( &RR );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002951
Paul Bakker5121ce52009-01-03 21:22:43 +00002952 /*
2953 * W = |X| - 1
2954 * R = W >> lsb( W )
2955 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002956 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &W, X, 1 ) );
2957 s = mbedtls_mpi_lsb( &W );
2958 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R, &W ) );
2959 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &R, s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002960
Janos Follathda31fa12018-09-03 14:45:23 +01002961 for( i = 0; i < rounds; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002962 {
2963 /*
2964 * pick a random A, 1 < A < |X| - 1
2965 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002966 count = 0;
2967 do {
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002968 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
Pascal Junodb99183d2015-03-11 16:49:45 +01002969
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002970 j = mbedtls_mpi_bitlen( &A );
2971 k = mbedtls_mpi_bitlen( &W );
Pascal Junodb99183d2015-03-11 16:49:45 +01002972 if (j > k) {
Darryl Greene3f95ed2018-10-02 13:21:35 +01002973 A.p[A.n - 1] &= ( (mbedtls_mpi_uint) 1 << ( k - ( A.n - 1 ) * biL - 1 ) ) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002974 }
2975
2976 if (count++ > 30) {
Jens Wiklanderf08aa3e2019-01-17 13:30:57 +01002977 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2978 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002979 }
2980
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002981 } while ( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 ||
2982 mbedtls_mpi_cmp_int( &A, 1 ) <= 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002983
2984 /*
2985 * A = A^R mod |X|
2986 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &A, &A, &R, X, &RR ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989 if( mbedtls_mpi_cmp_mpi( &A, &W ) == 0 ||
2990 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002991 continue;
2992
2993 j = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994 while( j < s && mbedtls_mpi_cmp_mpi( &A, &W ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002995 {
2996 /*
2997 * A = A * A mod |X|
2998 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &A, &A ) );
3000 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &A, &T, X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002 if( mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003003 break;
3004
3005 j++;
3006 }
3007
3008 /*
3009 * not prime if A != |X| - 1 or A == 1
3010 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011 if( mbedtls_mpi_cmp_mpi( &A, &W ) != 0 ||
3012 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003015 break;
3016 }
3017 }
3018
3019cleanup:
Hanno Becker73d7d792018-12-11 10:35:51 +00003020 mbedtls_mpi_free( &W ); mbedtls_mpi_free( &R );
3021 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00003023
3024 return( ret );
3025}
3026
3027/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01003028 * Pseudo-primality test: small factors, then Miller-Rabin
3029 */
Janos Follatha0b67c22018-09-18 14:48:23 +01003030int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds,
3031 int (*f_rng)(void *, unsigned char *, size_t),
3032 void *p_rng )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01003033{
Janos Follath24eed8d2019-11-22 13:21:35 +00003034 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035 mbedtls_mpi XX;
Hanno Becker8ce11a32018-12-19 16:18:52 +00003036 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00003037 MPI_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02003038
3039 XX.s = 1;
3040 XX.n = X->n;
3041 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01003042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043 if( mbedtls_mpi_cmp_int( &XX, 0 ) == 0 ||
3044 mbedtls_mpi_cmp_int( &XX, 1 ) == 0 )
3045 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01003046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003047 if( mbedtls_mpi_cmp_int( &XX, 2 ) == 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01003048 return( 0 );
3049
3050 if( ( ret = mpi_check_small_factors( &XX ) ) != 0 )
3051 {
3052 if( ret == 1 )
3053 return( 0 );
3054
3055 return( ret );
3056 }
3057
Janos Follathda31fa12018-09-03 14:45:23 +01003058 return( mpi_miller_rabin( &XX, rounds, f_rng, p_rng ) );
Janos Follathf301d232018-08-14 13:34:01 +01003059}
3060
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01003061/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003062 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08003063 *
Janos Follathf301d232018-08-14 13:34:01 +01003064 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
3065 * be either 1024 bits or 1536 bits long, and flags must contain
3066 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00003067 */
Janos Follath7c025a92018-08-14 11:08:41 +01003068int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003069 int (*f_rng)(void *, unsigned char *, size_t),
3070 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00003071{
Jethro Beekman66689272018-02-14 19:24:10 -08003072#ifdef MBEDTLS_HAVE_INT64
3073// ceil(2^63.5)
3074#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
3075#else
3076// ceil(2^31.5)
3077#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
3078#endif
3079 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003080 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01003081 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082 mbedtls_mpi_uint r;
3083 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00003084
Hanno Becker8ce11a32018-12-19 16:18:52 +00003085 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00003086 MPI_VALIDATE_RET( f_rng != NULL );
3087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088 if( nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS )
3089 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 mbedtls_mpi_init( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00003092
3093 n = BITS_TO_LIMBS( nbits );
3094
Janos Follathda31fa12018-09-03 14:45:23 +01003095 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR ) == 0 )
3096 {
3097 /*
3098 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
3099 */
3100 rounds = ( ( nbits >= 1300 ) ? 2 : ( nbits >= 850 ) ? 3 :
3101 ( nbits >= 650 ) ? 4 : ( nbits >= 350 ) ? 8 :
3102 ( nbits >= 250 ) ? 12 : ( nbits >= 150 ) ? 18 : 27 );
3103 }
3104 else
3105 {
3106 /*
3107 * 2^-100 error probability, number of rounds computed based on HAC,
3108 * fact 4.48
3109 */
3110 rounds = ( ( nbits >= 1450 ) ? 4 : ( nbits >= 1150 ) ? 5 :
3111 ( nbits >= 1000 ) ? 6 : ( nbits >= 850 ) ? 7 :
3112 ( nbits >= 750 ) ? 8 : ( nbits >= 500 ) ? 13 :
3113 ( nbits >= 250 ) ? 28 : ( nbits >= 150 ) ? 40 : 51 );
3114 }
3115
Jethro Beekman66689272018-02-14 19:24:10 -08003116 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003117 {
Jethro Beekman66689272018-02-14 19:24:10 -08003118 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
3119 /* make sure generated number is at least (nbits-1)+0.5 bits (FIPS 186-4 §B.3.3 steps 4.4, 5.5) */
3120 if( X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2 ) continue;
3121
3122 k = n * biL;
3123 if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits ) );
3124 X->p[0] |= 1;
3125
Janos Follath7c025a92018-08-14 11:08:41 +01003126 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003127 {
Janos Follatha0b67c22018-09-18 14:48:23 +01003128 ret = mbedtls_mpi_is_prime_ext( X, rounds, f_rng, p_rng );
Jethro Beekman66689272018-02-14 19:24:10 -08003129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003130 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003131 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003132 }
Jethro Beekman66689272018-02-14 19:24:10 -08003133 else
Paul Bakker5121ce52009-01-03 21:22:43 +00003134 {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01003135 /*
Jethro Beekman66689272018-02-14 19:24:10 -08003136 * An necessary condition for Y and X = 2Y + 1 to be prime
3137 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
3138 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01003139 */
Jethro Beekman66689272018-02-14 19:24:10 -08003140
3141 X->p[0] |= 2;
3142
3143 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, 3 ) );
3144 if( r == 0 )
3145 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 8 ) );
3146 else if( r == 1 )
3147 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 4 ) );
3148
3149 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
3150 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, X ) );
3151 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, 1 ) );
3152
3153 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003154 {
Jethro Beekman66689272018-02-14 19:24:10 -08003155 /*
3156 * First, check small factors for X and Y
3157 * before doing Miller-Rabin on any of them
3158 */
3159 if( ( ret = mpi_check_small_factors( X ) ) == 0 &&
3160 ( ret = mpi_check_small_factors( &Y ) ) == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01003161 ( ret = mpi_miller_rabin( X, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01003162 == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01003163 ( ret = mpi_miller_rabin( &Y, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01003164 == 0 )
Jethro Beekman66689272018-02-14 19:24:10 -08003165 goto cleanup;
3166
3167 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
3168 goto cleanup;
3169
3170 /*
3171 * Next candidates. We want to preserve Y = (X-1) / 2 and
3172 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
3173 * so up Y by 6 and X by 12.
3174 */
3175 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 12 ) );
3176 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &Y, &Y, 6 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003177 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003178 }
3179 }
3180
3181cleanup:
3182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003183 mbedtls_mpi_free( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00003184
3185 return( ret );
3186}
3187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00003189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003191
Paul Bakker23986e52011-04-24 08:57:21 +00003192#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003193
3194static const int gcd_pairs[GCD_PAIR_COUNT][3] =
3195{
3196 { 693, 609, 21 },
3197 { 1764, 868, 28 },
3198 { 768454923, 542167814, 1 }
3199};
3200
Paul Bakker5121ce52009-01-03 21:22:43 +00003201/*
3202 * Checkup routine
3203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204int mbedtls_mpi_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00003205{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003206 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00003208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); mbedtls_mpi_init( &X );
3210 mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &U ); mbedtls_mpi_init( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00003211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003212 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &A, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003213 "EFE021C2645FD1DC586E69184AF4A31E" \
3214 "D5F53E93B5F123FA41680867BA110131" \
3215 "944FE7952E2517337780CB0DB80E61AA" \
3216 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6" ) );
3217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003219 "B2E7EFD37075B9F03FF989C7C5051C20" \
3220 "34D2A323810251127E7BF8625A4F49A5" \
3221 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
3222 "5B5C25763222FEFCCFC38B832366C29E" ) );
3223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &N, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003225 "0066A198186C18C10B2F5ED9B522752A" \
3226 "9830B69916E535C8F047518A889A43A5" \
3227 "94B6BED27A168D31D4A52F88925AA8F5" ) );
3228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003232 "602AB7ECA597A3D6B56FF9829A5E8B85" \
3233 "9E857EA95A03512E2BAE7391688D264A" \
3234 "A5663B0341DB9CCFD2C4C5F421FEC814" \
3235 "8001B72E848A38CAE1C65F78E56ABDEF" \
3236 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
3237 "ECF677152EF804370C1A305CAF3B5BF1" \
3238 "30879B56C61DE584A0F53A2447A51E" ) );
3239
3240 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241 mbedtls_printf( " MPI test #1 (mul_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003244 {
3245 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003247
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003248 ret = 1;
3249 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003250 }
3251
3252 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &X, &Y, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003258 "256567336059E52CAE22925474705F39A94" ) );
3259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &V, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003261 "6613F26162223DF488E9CD48CC132C7A" \
3262 "0AC93C701B001B092E4E5B9F73BCD27B" \
3263 "9EE50D0657C77F374E903CDFA4C642" ) );
3264
3265 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266 mbedtls_printf( " MPI test #2 (div_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003268 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ||
3269 mbedtls_mpi_cmp_mpi( &Y, &V ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003270 {
3271 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003273
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003274 ret = 1;
3275 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003276 }
3277
3278 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003279 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &X, &A, &E, &N, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003284 "36E139AEA55215609D2816998ED020BB" \
3285 "BD96C37890F65171D948E9BC7CBAA4D9" \
3286 "325D24D6A3C12710F10A09FA08AB87" ) );
3287
3288 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289 mbedtls_printf( " MPI test #3 (exp_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003292 {
3293 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003295
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003296 ret = 1;
3297 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003298 }
3299
3300 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003305 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003306 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
3307 "C3DBA76456363A10869622EAC2DD84EC" \
3308 "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) );
3309
3310 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003311 mbedtls_printf( " MPI test #4 (inv_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003314 {
3315 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003316 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003317
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003318 ret = 1;
3319 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003320 }
3321
3322 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003323 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003324
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003325 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326 mbedtls_printf( " MPI test #5 (simple gcd): " );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003327
Paul Bakker66d5d072014-06-17 16:39:18 +02003328 for( i = 0; i < GCD_PAIR_COUNT; i++ )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003330 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &X, gcd_pairs[i][0] ) );
3331 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Y, gcd_pairs[i][1] ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003333 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &A, &X, &Y ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 if( mbedtls_mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 )
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003336 {
3337 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338 mbedtls_printf( "failed at %d\n", i );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003339
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003340 ret = 1;
3341 goto cleanup;
3342 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003343 }
3344
3345 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346 mbedtls_printf( "passed\n" );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003347
Paul Bakker5121ce52009-01-03 21:22:43 +00003348cleanup:
3349
3350 if( ret != 0 && verbose != 0 )
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003351 mbedtls_printf( "Unexpected error, return code = %08X\n", (unsigned int) ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003353 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &X );
3354 mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &U ); mbedtls_mpi_free( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00003355
3356 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003358
3359 return( ret );
3360}
3361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364#endif /* MBEDTLS_BIGNUM_C */