blob: 24b6abe634061d63060f7cb8ae9789880c257a4f [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"
Hanno Beckeraef9cc42022-04-11 06:36:29 +010041#include "bignum_internal.h"
Janos Follath4614b9a2022-07-21 15:34:47 +010042#include "bignum_core.h"
Chris Jones4c5819c2021-03-03 17:45:34 +000043#include "bn_mul.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050044#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000045#include "mbedtls/error.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020046#include "constant_time_internal.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Dave Rodgman351c71b2021-12-06 17:50:53 +000048#include <limits.h>
Rich Evans00ab4702015-02-06 13:43:58 +000049#include <string.h>
50
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052
Gabor Mezei66669142022-08-03 12:52:26 +020053#define MPI_VALIDATE_RET( cond ) \
54 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA )
55#define MPI_VALIDATE( cond ) \
56 MBEDTLS_INTERNAL_VALIDATE( cond )
57
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +010058#define MPI_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
59
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050060/* Implementation that should never be optimized out by the compiler */
Andres Amaya Garcia6698d2f2018-04-24 08:39:07 -050061static void mbedtls_mpi_zeroize( mbedtls_mpi_uint *v, size_t n )
62{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050063 mbedtls_platform_zeroize( v, ciL * n );
64}
65
Paul Bakker5121ce52009-01-03 21:22:43 +000066/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000067 * Initialize one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000068 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069void mbedtls_mpi_init( mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +000070{
Hanno Becker73d7d792018-12-11 10:35:51 +000071 MPI_VALIDATE( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Paul Bakker6c591fa2011-05-05 11:49:20 +000073 X->s = 1;
74 X->n = 0;
75 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000076}
77
78/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000079 * Unallocate one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081void mbedtls_mpi_free( mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +000082{
Paul Bakker6c591fa2011-05-05 11:49:20 +000083 if( X == NULL )
84 return;
Paul Bakker5121ce52009-01-03 21:22:43 +000085
Paul Bakker6c591fa2011-05-05 11:49:20 +000086 if( X->p != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000087 {
Alexey Skalozube17a8da2016-01-13 17:19:33 +020088 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +000090 }
91
Paul Bakker6c591fa2011-05-05 11:49:20 +000092 X->s = 1;
93 X->n = 0;
94 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000095}
96
97/*
98 * Enlarge to the specified number of limbs
99 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000101{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_mpi_uint *p;
Hanno Becker73d7d792018-12-11 10:35:51 +0000103 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200106 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Paul Bakkerf9688572011-05-05 10:00:45 +0000107
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 if( X->n < nblimbs )
109 {
Simon Butcher29176892016-05-20 00:19:09 +0100110 if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( nblimbs, ciL ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200111 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 if( X->p != NULL )
114 {
115 memcpy( p, X->p, X->n * ciL );
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200116 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 }
119
120 X->n = nblimbs;
121 X->p = p;
122 }
123
124 return( 0 );
125}
126
127/*
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100128 * Resize down as much as possible,
129 * while keeping at least the specified number of limbs
130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_mpi_uint *p;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100134 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000135 MPI_VALIDATE_RET( X != NULL );
136
137 if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
138 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100139
Gilles Peskinee2f563e2020-01-20 21:17:43 +0100140 /* Actually resize up if there are currently fewer than nblimbs limbs. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100141 if( X->n <= nblimbs )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 return( mbedtls_mpi_grow( X, nblimbs ) );
Gilles Peskine322752b2020-01-21 13:59:51 +0100143 /* After this point, then X->n > nblimbs and in particular X->n > 0. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100144
145 for( i = X->n - 1; i > 0; i-- )
146 if( X->p[i] != 0 )
147 break;
148 i++;
149
150 if( i < nblimbs )
151 i = nblimbs;
152
Simon Butcher29176892016-05-20 00:19:09 +0100153 if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( i, ciL ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200154 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100155
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100156 if( X->p != NULL )
157 {
158 memcpy( p, X->p, i * ciL );
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200159 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_free( X->p );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100161 }
162
163 X->n = i;
164 X->p = p;
165
166 return( 0 );
167}
168
Gilles Peskineed32b572021-06-02 22:17:52 +0200169/* Resize X to have exactly n limbs and set it to 0. */
170static int mbedtls_mpi_resize_clear( mbedtls_mpi *X, size_t limbs )
171{
172 if( limbs == 0 )
173 {
174 mbedtls_mpi_free( X );
175 return( 0 );
176 }
177 else if( X->n == limbs )
178 {
179 memset( X->p, 0, limbs * ciL );
180 X->s = 1;
181 return( 0 );
182 }
183 else
184 {
185 mbedtls_mpi_free( X );
186 return( mbedtls_mpi_grow( X, limbs ) );
187 }
188}
189
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100190/*
Gilles Peskine3da1a8f2021-06-08 23:17:42 +0200191 * Copy the contents of Y into X.
192 *
193 * This function is not constant-time. Leading zeros in Y may be removed.
194 *
195 * Ensure that X does not shrink. This is not guaranteed by the public API,
196 * but some code in the bignum module relies on this property, for example
197 * in mbedtls_mpi_exp_mod().
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000200{
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100201 int ret = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000202 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000203 MPI_VALIDATE_RET( X != NULL );
204 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000205
206 if( X == Y )
207 return( 0 );
208
Gilles Peskinedb420622020-01-20 21:12:50 +0100209 if( Y->n == 0 )
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200210 {
Gilles Peskine3da1a8f2021-06-08 23:17:42 +0200211 if( X->n != 0 )
212 {
213 X->s = 1;
214 memset( X->p, 0, X->n * ciL );
215 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200216 return( 0 );
217 }
218
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 for( i = Y->n - 1; i > 0; i-- )
220 if( Y->p[i] != 0 )
221 break;
222 i++;
223
224 X->s = Y->s;
225
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100226 if( X->n < i )
227 {
228 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i ) );
229 }
230 else
231 {
232 memset( X->p + i, 0, ( X->n - i ) * ciL );
233 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 memcpy( X->p, Y->p, i * ciL );
236
237cleanup:
238
239 return( ret );
240}
241
242/*
243 * Swap the contents of X and Y
244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000246{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000248 MPI_VALIDATE( X != NULL );
249 MPI_VALIDATE( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 memcpy( &T, X, sizeof( mbedtls_mpi ) );
252 memcpy( X, Y, sizeof( mbedtls_mpi ) );
253 memcpy( Y, &T, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254}
255
256/*
257 * Set value from integer
258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +0000260{
Janos Follath24eed8d2019-11-22 13:21:35 +0000261 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker73d7d792018-12-11 10:35:51 +0000262 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 memset( X->p, 0, X->n * ciL );
266
267 X->p[0] = ( z < 0 ) ? -z : z;
268 X->s = ( z < 0 ) ? -1 : 1;
269
270cleanup:
271
272 return( ret );
273}
274
275/*
Paul Bakker2f5947e2011-05-18 15:47:11 +0000276 * Get a specific bit
277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000279{
Hanno Becker73d7d792018-12-11 10:35:51 +0000280 MPI_VALIDATE_RET( X != NULL );
281
Paul Bakker2f5947e2011-05-18 15:47:11 +0000282 if( X->n * biL <= pos )
283 return( 0 );
284
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200285 return( ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000286}
287
288/*
289 * Set a bit to a specific value of 0 or 1
290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000292{
293 int ret = 0;
294 size_t off = pos / biL;
295 size_t idx = pos % biL;
Hanno Becker73d7d792018-12-11 10:35:51 +0000296 MPI_VALIDATE_RET( X != NULL );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000297
298 if( val != 0 && val != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker9af723c2014-05-01 13:03:14 +0200300
Paul Bakker2f5947e2011-05-18 15:47:11 +0000301 if( X->n * biL <= pos )
302 {
303 if( val == 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200304 return( 0 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, off + 1 ) );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000307 }
308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 X->p[off] &= ~( (mbedtls_mpi_uint) 0x01 << idx );
310 X->p[off] |= (mbedtls_mpi_uint) val << idx;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000311
312cleanup:
Paul Bakker9af723c2014-05-01 13:03:14 +0200313
Paul Bakker2f5947e2011-05-18 15:47:11 +0000314 return( ret );
315}
316
317/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200318 * Return the number of less significant zero-bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320size_t mbedtls_mpi_lsb( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000321{
Paul Bakker23986e52011-04-24 08:57:21 +0000322 size_t i, j, count = 0;
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000323 MBEDTLS_INTERNAL_VALIDATE_RET( X != NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000324
325 for( i = 0; i < X->n; i++ )
Paul Bakkerf9688572011-05-05 10:00:45 +0000326 for( j = 0; j < biL; j++, count++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 if( ( ( X->p[i] >> j ) & 1 ) != 0 )
328 return( count );
329
330 return( 0 );
331}
332
333/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200334 * Return the number of bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000335 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200336size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000337{
Gabor Mezei89e31462022-08-12 15:36:56 +0200338 return( mbedtls_mpi_core_bitlen( X->p, X->n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339}
340
341/*
342 * Return the total size in bytes
343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344size_t mbedtls_mpi_size( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000345{
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200346 return( ( mbedtls_mpi_bitlen( X ) + 7 ) >> 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000347}
348
349/*
350 * Convert an ASCII character to digit value
351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352static int mpi_get_digit( mbedtls_mpi_uint *d, int radix, char c )
Paul Bakker5121ce52009-01-03 21:22:43 +0000353{
354 *d = 255;
355
356 if( c >= 0x30 && c <= 0x39 ) *d = c - 0x30;
357 if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37;
358 if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57;
359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 if( *d >= (mbedtls_mpi_uint) radix )
361 return( MBEDTLS_ERR_MPI_INVALID_CHARACTER );
Paul Bakker5121ce52009-01-03 21:22:43 +0000362
363 return( 0 );
364}
365
366/*
367 * Import from an ASCII string
368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000370{
Janos Follath24eed8d2019-11-22 13:21:35 +0000371 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000372 size_t i, j, slen, n;
Gilles Peskine80f56732021-04-03 18:26:13 +0200373 int sign = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_mpi_uint d;
375 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000376 MPI_VALIDATE_RET( X != NULL );
377 MPI_VALIDATE_RET( s != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000378
379 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000380 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000383
Gilles Peskine7cba8592021-06-08 18:32:34 +0200384 if( s[0] == 0 )
385 {
386 mbedtls_mpi_free( X );
387 return( 0 );
388 }
389
Gilles Peskine80f56732021-04-03 18:26:13 +0200390 if( s[0] == '-' )
391 {
392 ++s;
393 sign = -1;
394 }
395
Paul Bakkerff60ee62010-03-16 21:09:09 +0000396 slen = strlen( s );
397
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 if( radix == 16 )
399 {
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +0100400 if( slen > MPI_SIZE_T_MAX >> 2 )
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +0200401 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
402
Paul Bakkerff60ee62010-03-16 21:09:09 +0000403 n = BITS_TO_LIMBS( slen << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n ) );
406 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000407
Paul Bakker23986e52011-04-24 08:57:21 +0000408 for( i = slen, j = 0; i > 0; i--, j++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200411 X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000412 }
413 }
414 else
415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000417
Paul Bakkerff60ee62010-03-16 21:09:09 +0000418 for( i = 0; i < slen; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
421 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T, X, radix ) );
Gilles Peskine80f56732021-04-03 18:26:13 +0200422 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, &T, d ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000423 }
424 }
425
Gilles Peskine80f56732021-04-03 18:26:13 +0200426 if( sign < 0 && mbedtls_mpi_bitlen( X ) != 0 )
427 X->s = -1;
428
Paul Bakker5121ce52009-01-03 21:22:43 +0000429cleanup:
430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000432
433 return( ret );
434}
435
436/*
Ron Eldora16fa292018-11-20 14:07:01 +0200437 * Helper to write the digits high-order first.
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 */
Ron Eldora16fa292018-11-20 14:07:01 +0200439static int mpi_write_hlp( mbedtls_mpi *X, int radix,
440 char **p, const size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000441{
Janos Follath24eed8d2019-11-22 13:21:35 +0000442 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_mpi_uint r;
Ron Eldora16fa292018-11-20 14:07:01 +0200444 size_t length = 0;
445 char *p_end = *p + buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
Ron Eldora16fa292018-11-20 14:07:01 +0200447 do
448 {
449 if( length >= buflen )
450 {
451 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
452 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000453
Ron Eldora16fa292018-11-20 14:07:01 +0200454 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) );
455 MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) );
456 /*
457 * Write the residue in the current position, as an ASCII character.
458 */
459 if( r < 0xA )
460 *(--p_end) = (char)( '0' + r );
461 else
462 *(--p_end) = (char)( 'A' + ( r - 0xA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
Ron Eldora16fa292018-11-20 14:07:01 +0200464 length++;
465 } while( mbedtls_mpi_cmp_int( X, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000466
Ron Eldora16fa292018-11-20 14:07:01 +0200467 memmove( *p, p_end, length );
468 *p += length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
470cleanup:
471
472 return( ret );
473}
474
475/*
476 * Export into an ASCII string
477 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100478int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
479 char *buf, size_t buflen, size_t *olen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000480{
Paul Bakker23986e52011-04-24 08:57:21 +0000481 int ret = 0;
482 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000485 MPI_VALIDATE_RET( X != NULL );
486 MPI_VALIDATE_RET( olen != NULL );
487 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000488
489 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000490 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
Hanno Becker23cfea02019-02-04 09:45:07 +0000492 n = mbedtls_mpi_bitlen( X ); /* Number of bits necessary to present `n`. */
493 if( radix >= 4 ) n >>= 1; /* Number of 4-adic digits necessary to present
494 * `n`. If radix > 4, this might be a strict
495 * overapproximation of the number of
496 * radix-adic digits needed to present `n`. */
497 if( radix >= 16 ) n >>= 1; /* Number of hexadecimal digits necessary to
498 * present `n`. */
499
Janos Follath80470622019-03-06 13:43:02 +0000500 n += 1; /* Terminating null byte */
Hanno Becker23cfea02019-02-04 09:45:07 +0000501 n += 1; /* Compensate for the divisions above, which round down `n`
502 * in case it's not even. */
503 n += 1; /* Potential '-'-sign. */
504 n += ( n & 1 ); /* Make n even to have enough space for hexadecimal writing,
505 * which always uses an even number of hex-digits. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000506
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100507 if( buflen < n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100509 *olen = n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000511 }
512
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100513 p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
516 if( X->s == -1 )
Hanno Beckerc983c812019-02-01 16:41:30 +0000517 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 *p++ = '-';
Hanno Beckerc983c812019-02-01 16:41:30 +0000519 buflen--;
520 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
522 if( radix == 16 )
523 {
Paul Bakker23986e52011-04-24 08:57:21 +0000524 int c;
525 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
Paul Bakker23986e52011-04-24 08:57:21 +0000527 for( i = X->n, k = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 {
Paul Bakker23986e52011-04-24 08:57:21 +0000529 for( j = ciL; j > 0; j-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000530 {
Paul Bakker23986e52011-04-24 08:57:21 +0000531 c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000532
Paul Bakker6c343d72014-07-10 14:36:19 +0200533 if( c == 0 && k == 0 && ( i + j ) != 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 continue;
535
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000536 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000537 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 k = 1;
539 }
540 }
541 }
542 else
543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T, X ) );
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000545
546 if( T.s == -1 )
547 T.s = 1;
548
Ron Eldora16fa292018-11-20 14:07:01 +0200549 MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p, buflen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000550 }
551
552 *p++ = '\0';
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100553 *olen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
555cleanup:
556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
559 return( ret );
560}
561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000563/*
564 * Read X from an opened file
565 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin )
Paul Bakker5121ce52009-01-03 21:22:43 +0000567{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_mpi_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000569 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000570 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000571 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000572 * Buffer should have space for (short) label and decimal formatted MPI,
573 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
Hanno Becker73d7d792018-12-11 10:35:51 +0000577 MPI_VALIDATE_RET( X != NULL );
578 MPI_VALIDATE_RET( fin != NULL );
579
580 if( radix < 2 || radix > 16 )
581 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
582
Paul Bakker5121ce52009-01-03 21:22:43 +0000583 memset( s, 0, sizeof( s ) );
584 if( fgets( s, sizeof( s ) - 1, fin ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
587 slen = strlen( s );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000588 if( slen == sizeof( s ) - 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000590
Hanno Beckerb2034b72017-04-26 11:46:46 +0100591 if( slen > 0 && s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
592 if( slen > 0 && s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
Paul Bakker5121ce52009-01-03 21:22:43 +0000593
594 p = s + slen;
Hanno Beckerb2034b72017-04-26 11:46:46 +0100595 while( p-- > s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000596 if( mpi_get_digit( &d, radix, *p ) != 0 )
597 break;
598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 return( mbedtls_mpi_read_string( X, radix, p + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000600}
601
602/*
603 * Write X into an opened file (or stdout if fout == NULL)
604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout )
Paul Bakker5121ce52009-01-03 21:22:43 +0000606{
Janos Follath24eed8d2019-11-22 13:21:35 +0000607 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000608 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000609 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000610 * Buffer should have space for (short) label and decimal formatted MPI,
611 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Hanno Becker73d7d792018-12-11 10:35:51 +0000614 MPI_VALIDATE_RET( X != NULL );
615
616 if( radix < 2 || radix > 16 )
617 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000618
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100619 memset( s, 0, sizeof( s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100621 MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, sizeof( s ) - 2, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000622
623 if( p == NULL ) p = "";
624
625 plen = strlen( p );
626 slen = strlen( s );
627 s[slen++] = '\r';
628 s[slen++] = '\n';
629
630 if( fout != NULL )
631 {
632 if( fwrite( p, 1, plen, fout ) != plen ||
633 fwrite( s, 1, slen, fout ) != slen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 }
636 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_printf( "%s%s", p, s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000638
639cleanup:
640
641 return( ret );
642}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000644
645/*
Janos Follatha778a942019-02-13 10:28:28 +0000646 * Import X from unsigned binary data, little endian
Przemyslaw Stekiel76960a72022-02-21 13:42:09 +0100647 *
648 * This function is guaranteed to return an MPI with exactly the necessary
649 * number of limbs (in particular, it does not skip 0s in the input).
Janos Follatha778a942019-02-13 10:28:28 +0000650 */
651int mbedtls_mpi_read_binary_le( mbedtls_mpi *X,
652 const unsigned char *buf, size_t buflen )
653{
Janos Follath24eed8d2019-11-22 13:21:35 +0000654 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath620c58c2022-08-15 11:58:42 +0100655 const size_t limbs = CHARS_TO_LIMBS( buflen );
Janos Follatha778a942019-02-13 10:28:28 +0000656
657 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskineed32b572021-06-02 22:17:52 +0200658 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Janos Follatha778a942019-02-13 10:28:28 +0000659
Janos Follath5f016652022-07-22 16:18:41 +0100660 MBEDTLS_MPI_CHK( mbedtls_mpi_core_read_le( X->p, X->n, buf, buflen ) );
Janos Follatha778a942019-02-13 10:28:28 +0000661
662cleanup:
663
Janos Follath171a7ef2019-02-15 16:17:45 +0000664 /*
665 * This function is also used to import keys. However, wiping the buffers
666 * upon failure is not necessary because failure only can happen before any
667 * input is copied.
668 */
Janos Follatha778a942019-02-13 10:28:28 +0000669 return( ret );
670}
671
672/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000673 * Import X from unsigned binary data, big endian
Przemyslaw Stekiel76960a72022-02-21 13:42:09 +0100674 *
675 * This function is guaranteed to return an MPI with exactly the necessary
676 * number of limbs (in particular, it does not skip 0s in the input).
Paul Bakker5121ce52009-01-03 21:22:43 +0000677 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000679{
Janos Follath24eed8d2019-11-22 13:21:35 +0000680 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath620c58c2022-08-15 11:58:42 +0100681 const size_t limbs = CHARS_TO_LIMBS( buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000682
Hanno Becker8ce11a32018-12-19 16:18:52 +0000683 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +0000684 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
685
Hanno Becker073c1992017-10-17 15:17:27 +0100686 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskineed32b572021-06-02 22:17:52 +0200687 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
Janos Follath5f016652022-07-22 16:18:41 +0100689 MBEDTLS_MPI_CHK( mbedtls_mpi_core_read_be( X->p, X->n, buf, buflen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000690
691cleanup:
692
Janos Follath171a7ef2019-02-15 16:17:45 +0000693 /*
694 * This function is also used to import keys. However, wiping the buffers
695 * upon failure is not necessary because failure only can happen before any
696 * input is copied.
697 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 return( ret );
699}
700
701/*
Janos Follathe344d0f2019-02-19 16:17:40 +0000702 * Export X into unsigned binary data, little endian
703 */
704int mbedtls_mpi_write_binary_le( const mbedtls_mpi *X,
705 unsigned char *buf, size_t buflen )
706{
Janos Follathca5688e2022-08-19 12:05:28 +0100707 return( mbedtls_mpi_core_write_le( X->p, X->n, buf, buflen ) );
Janos Follathe344d0f2019-02-19 16:17:40 +0000708}
709
710/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000711 * Export X into unsigned binary data, big endian
712 */
Gilles Peskine11cdb052018-11-20 16:47:47 +0100713int mbedtls_mpi_write_binary( const mbedtls_mpi *X,
714 unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000715{
Janos Follath5f016652022-07-22 16:18:41 +0100716 return( mbedtls_mpi_core_write_be( X->p, X->n, buf, buflen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000717}
718
719/*
720 * Left-shift: X <<= count
721 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +0000723{
Janos Follath24eed8d2019-11-22 13:21:35 +0000724 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000725 size_t i, v0, t1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +0000727 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000728
729 v0 = count / (biL );
730 t1 = count & (biL - 1);
731
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200732 i = mbedtls_mpi_bitlen( X ) + count;
Paul Bakker5121ce52009-01-03 21:22:43 +0000733
Paul Bakkerf9688572011-05-05 10:00:45 +0000734 if( X->n * biL < i )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000736
737 ret = 0;
738
739 /*
740 * shift by count / limb_size
741 */
742 if( v0 > 0 )
743 {
Paul Bakker23986e52011-04-24 08:57:21 +0000744 for( i = X->n; i > v0; i-- )
745 X->p[i - 1] = X->p[i - v0 - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +0000746
Paul Bakker23986e52011-04-24 08:57:21 +0000747 for( ; i > 0; i-- )
748 X->p[i - 1] = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000749 }
750
751 /*
752 * shift by count % limb_size
753 */
754 if( t1 > 0 )
755 {
756 for( i = v0; i < X->n; i++ )
757 {
758 r1 = X->p[i] >> (biL - t1);
759 X->p[i] <<= t1;
760 X->p[i] |= r0;
761 r0 = r1;
762 }
763 }
764
765cleanup:
766
767 return( ret );
768}
769
770/*
771 * Right-shift: X >>= count
772 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +0000774{
Paul Bakker23986e52011-04-24 08:57:21 +0000775 size_t i, v0, v1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +0000777 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000778
779 v0 = count / biL;
780 v1 = count & (biL - 1);
781
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +0100782 if( v0 > X->n || ( v0 == X->n && v1 > 0 ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 return mbedtls_mpi_lset( X, 0 );
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +0100784
Paul Bakker5121ce52009-01-03 21:22:43 +0000785 /*
786 * shift by count / limb_size
787 */
788 if( v0 > 0 )
789 {
790 for( i = 0; i < X->n - v0; i++ )
791 X->p[i] = X->p[i + v0];
792
793 for( ; i < X->n; i++ )
794 X->p[i] = 0;
795 }
796
797 /*
798 * shift by count % limb_size
799 */
800 if( v1 > 0 )
801 {
Paul Bakker23986e52011-04-24 08:57:21 +0000802 for( i = X->n; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000803 {
Paul Bakker23986e52011-04-24 08:57:21 +0000804 r1 = X->p[i - 1] << (biL - v1);
805 X->p[i - 1] >>= v1;
806 X->p[i - 1] |= r0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000807 r0 = r1;
808 }
809 }
810
811 return( 0 );
812}
813
814/*
815 * Compare unsigned values
816 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000818{
Paul Bakker23986e52011-04-24 08:57:21 +0000819 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +0000820 MPI_VALIDATE_RET( X != NULL );
821 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000822
Paul Bakker23986e52011-04-24 08:57:21 +0000823 for( i = X->n; i > 0; i-- )
824 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000825 break;
826
Paul Bakker23986e52011-04-24 08:57:21 +0000827 for( j = Y->n; j > 0; j-- )
828 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000829 break;
830
Paul Bakker23986e52011-04-24 08:57:21 +0000831 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000832 return( 0 );
833
834 if( i > j ) return( 1 );
835 if( j > i ) return( -1 );
836
Paul Bakker23986e52011-04-24 08:57:21 +0000837 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000838 {
Paul Bakker23986e52011-04-24 08:57:21 +0000839 if( X->p[i - 1] > Y->p[i - 1] ) return( 1 );
840 if( X->p[i - 1] < Y->p[i - 1] ) return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000841 }
842
843 return( 0 );
844}
845
846/*
847 * Compare signed values
848 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000850{
Paul Bakker23986e52011-04-24 08:57:21 +0000851 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +0000852 MPI_VALIDATE_RET( X != NULL );
853 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000854
Paul Bakker23986e52011-04-24 08:57:21 +0000855 for( i = X->n; i > 0; i-- )
856 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000857 break;
858
Paul Bakker23986e52011-04-24 08:57:21 +0000859 for( j = Y->n; j > 0; j-- )
860 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000861 break;
862
Paul Bakker23986e52011-04-24 08:57:21 +0000863 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000864 return( 0 );
865
866 if( i > j ) return( X->s );
Paul Bakker0c8f73b2012-03-22 14:08:57 +0000867 if( j > i ) return( -Y->s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000868
869 if( X->s > 0 && Y->s < 0 ) return( 1 );
870 if( Y->s > 0 && X->s < 0 ) return( -1 );
871
Paul Bakker23986e52011-04-24 08:57:21 +0000872 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000873 {
Paul Bakker23986e52011-04-24 08:57:21 +0000874 if( X->p[i - 1] > Y->p[i - 1] ) return( X->s );
875 if( X->p[i - 1] < Y->p[i - 1] ) return( -X->s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000876 }
877
878 return( 0 );
879}
880
Janos Follathee6abce2019-09-05 14:47:19 +0100881/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000882 * Compare signed values
883 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +0000885{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 mbedtls_mpi Y;
887 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +0000888 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000889
890 *p = ( z < 0 ) ? -z : z;
891 Y.s = ( z < 0 ) ? -1 : 1;
892 Y.n = 1;
893 Y.p = p;
894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 return( mbedtls_mpi_cmp_mpi( X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000896}
897
898/*
899 * Unsigned addition: X = |A| + |B| (HAC 14.7)
900 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +0000902{
Janos Follath24eed8d2019-11-22 13:21:35 +0000903 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000904 size_t i, j;
Janos Follath6c922682015-10-30 17:43:11 +0100905 mbedtls_mpi_uint *o, *p, c, tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +0000906 MPI_VALIDATE_RET( X != NULL );
907 MPI_VALIDATE_RET( A != NULL );
908 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000909
910 if( X == B )
911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 const mbedtls_mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000913 }
914
915 if( X != A )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
Paul Bakker9af723c2014-05-01 13:03:14 +0200917
Paul Bakkerf7ca7b92009-06-20 10:31:06 +0000918 /*
919 * X should always be positive as a result of unsigned additions.
920 */
921 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000922
Paul Bakker23986e52011-04-24 08:57:21 +0000923 for( j = B->n; j > 0; j-- )
924 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000925 break;
926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000928
929 o = B->p; p = X->p; c = 0;
930
Janos Follath6c922682015-10-30 17:43:11 +0100931 /*
932 * tmp is used because it might happen that p == o
933 */
Paul Bakker23986e52011-04-24 08:57:21 +0000934 for( i = 0; i < j; i++, o++, p++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000935 {
Janos Follath6c922682015-10-30 17:43:11 +0100936 tmp= *o;
Paul Bakker5121ce52009-01-03 21:22:43 +0000937 *p += c; c = ( *p < c );
Janos Follath6c922682015-10-30 17:43:11 +0100938 *p += tmp; c += ( *p < tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000939 }
940
941 while( c != 0 )
942 {
943 if( i >= X->n )
944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000946 p = X->p + i;
947 }
948
Paul Bakker2d319fd2012-09-16 21:34:26 +0000949 *p += c; c = ( *p < c ); i++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000950 }
951
952cleanup:
953
954 return( ret );
955}
956
Gilles Peskine09ec10a2020-06-09 10:39:38 +0200957/**
Gilles Peskinec097e9e2020-06-08 21:58:22 +0200958 * Helper for mbedtls_mpi subtraction.
959 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +0200960 * Calculate l - r where l and r have the same size.
Gilles Peskinec097e9e2020-06-08 21:58:22 +0200961 * This function operates modulo (2^ciL)^n and returns the carry
Gilles Peskine1acf7cb2020-07-23 01:03:22 +0200962 * (1 if there was a wraparound, i.e. if `l < r`, and 0 otherwise).
Gilles Peskinec097e9e2020-06-08 21:58:22 +0200963 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +0200964 * d may be aliased to l or r.
Gilles Peskinec097e9e2020-06-08 21:58:22 +0200965 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +0200966 * \param n Number of limbs of \p d, \p l and \p r.
967 * \param[out] d The result of the subtraction.
968 * \param[in] l The left operand.
969 * \param[in] r The right operand.
970 *
971 * \return 1 if `l < r`.
972 * 0 if `l >= r`.
Paul Bakker5121ce52009-01-03 21:22:43 +0000973 */
Gilles Peskinec097e9e2020-06-08 21:58:22 +0200974static mbedtls_mpi_uint mpi_sub_hlp( size_t n,
975 mbedtls_mpi_uint *d,
Gilles Peskine1acf7cb2020-07-23 01:03:22 +0200976 const mbedtls_mpi_uint *l,
977 const mbedtls_mpi_uint *r )
Paul Bakker5121ce52009-01-03 21:22:43 +0000978{
Paul Bakker23986e52011-04-24 08:57:21 +0000979 size_t i;
Gilles Peskine1acf7cb2020-07-23 01:03:22 +0200980 mbedtls_mpi_uint c = 0, t, z;
Paul Bakker5121ce52009-01-03 21:22:43 +0000981
Gilles Peskine1acf7cb2020-07-23 01:03:22 +0200982 for( i = 0; i < n; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000983 {
Gilles Peskine1acf7cb2020-07-23 01:03:22 +0200984 z = ( l[i] < c ); t = l[i] - c;
985 c = ( t < r[i] ) + z; d[i] = t - r[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000986 }
987
Gilles Peskinec097e9e2020-06-08 21:58:22 +0200988 return( c );
Paul Bakker5121ce52009-01-03 21:22:43 +0000989}
990
991/*
Gilles Peskine0e5faf62020-06-08 22:50:35 +0200992 * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
Paul Bakker5121ce52009-01-03 21:22:43 +0000993 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +0000995{
Janos Follath24eed8d2019-11-22 13:21:35 +0000996 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000997 size_t n;
Gilles Peskine0e5faf62020-06-08 22:50:35 +0200998 mbedtls_mpi_uint carry;
Hanno Becker73d7d792018-12-11 10:35:51 +0000999 MPI_VALIDATE_RET( X != NULL );
1000 MPI_VALIDATE_RET( A != NULL );
1001 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001002
Paul Bakker23986e52011-04-24 08:57:21 +00001003 for( n = B->n; n > 0; n-- )
1004 if( B->p[n - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001005 break;
Gilles Peskinec8a91772021-01-27 22:30:43 +01001006 if( n > A->n )
1007 {
1008 /* B >= (2^ciL)^n > A */
1009 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1010 goto cleanup;
1011 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001012
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001013 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, A->n ) );
1014
1015 /* Set the high limbs of X to match A. Don't touch the lower limbs
1016 * because X might be aliased to B, and we must not overwrite the
1017 * significant digits of B. */
1018 if( A->n > n )
1019 memcpy( X->p + n, A->p + n, ( A->n - n ) * ciL );
1020 if( X->n > A->n )
1021 memset( X->p + A->n, 0, ( X->n - A->n ) * ciL );
1022
1023 carry = mpi_sub_hlp( n, X->p, A->p, B->p );
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001024 if( carry != 0 )
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001025 {
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001026 /* Propagate the carry to the first nonzero limb of X. */
1027 for( ; n < X->n && X->p[n] == 0; n++ )
1028 --X->p[n];
1029 /* If we ran out of space for the carry, it means that the result
1030 * is negative. */
1031 if( n == X->n )
Gilles Peskine89b41302020-07-23 01:16:46 +02001032 {
1033 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1034 goto cleanup;
1035 }
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001036 --X->p[n];
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001037 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001038
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001039 /* X should always be positive as a result of unsigned subtractions. */
1040 X->s = 1;
1041
Paul Bakker5121ce52009-01-03 21:22:43 +00001042cleanup:
Paul Bakker5121ce52009-01-03 21:22:43 +00001043 return( ret );
1044}
1045
1046/*
1047 * Signed addition: X = A + B
1048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001050{
Hanno Becker73d7d792018-12-11 10:35:51 +00001051 int ret, s;
1052 MPI_VALIDATE_RET( X != NULL );
1053 MPI_VALIDATE_RET( A != NULL );
1054 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001055
Hanno Becker73d7d792018-12-11 10:35:51 +00001056 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001057 if( A->s * B->s < 0 )
1058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001062 X->s = s;
1063 }
1064 else
1065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001067 X->s = -s;
1068 }
1069 }
1070 else
1071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001073 X->s = s;
1074 }
1075
1076cleanup:
1077
1078 return( ret );
1079}
1080
1081/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001082 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001085{
Hanno Becker73d7d792018-12-11 10:35:51 +00001086 int ret, s;
1087 MPI_VALIDATE_RET( X != NULL );
1088 MPI_VALIDATE_RET( A != NULL );
1089 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
Hanno Becker73d7d792018-12-11 10:35:51 +00001091 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001092 if( A->s * B->s > 0 )
1093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001097 X->s = s;
1098 }
1099 else
1100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001102 X->s = -s;
1103 }
1104 }
1105 else
1106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001108 X->s = s;
1109 }
1110
1111cleanup:
1112
1113 return( ret );
1114}
1115
1116/*
1117 * Signed addition: X = A + b
1118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001120{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001121 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001123 MPI_VALIDATE_RET( X != NULL );
1124 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001125
1126 p[0] = ( b < 0 ) ? -b : b;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001127 B.s = ( b < 0 ) ? -1 : 1;
1128 B.n = 1;
1129 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001130
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001131 return( mbedtls_mpi_add_mpi( X, A, &B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001132}
1133
1134/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001135 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001138{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001139 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001141 MPI_VALIDATE_RET( X != NULL );
1142 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001143
1144 p[0] = ( b < 0 ) ? -b : b;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001145 B.s = ( b < 0 ) ? -1 : 1;
1146 B.n = 1;
1147 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001148
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001149 return( mbedtls_mpi_sub_mpi( X, A, &B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001150}
1151
Hanno Becker284d7782022-04-11 09:19:24 +01001152mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len,
Hanno Beckeraef9cc42022-04-11 06:36:29 +01001153 const mbedtls_mpi_uint *s, size_t s_len,
1154 mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001155{
Hanno Beckere7f14a32022-04-06 06:11:26 +01001156 mbedtls_mpi_uint c = 0; /* carry */
Hanno Becker5d4ceeb2022-04-11 09:46:47 +01001157 size_t excess_len = d_len - s_len;
Hanno Beckerdefe5692022-04-06 06:12:09 +01001158
Hanno Becker63eb28c2022-04-06 11:30:51 +01001159 size_t steps_x8 = s_len / 8;
1160 size_t steps_x1 = s_len & 7;
1161
1162 while( steps_x8-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001163 {
Hanno Beckereacf3b92022-04-06 11:25:22 +01001164 MULADDC_X8_INIT
1165 MULADDC_X8_CORE
1166 MULADDC_X8_STOP
Paul Bakker5121ce52009-01-03 21:22:43 +00001167 }
1168
Hanno Becker63eb28c2022-04-06 11:30:51 +01001169 while( steps_x1-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 {
Hanno Beckereacf3b92022-04-06 11:25:22 +01001171 MULADDC_X1_INIT
1172 MULADDC_X1_CORE
1173 MULADDC_X1_STOP
Paul Bakker5121ce52009-01-03 21:22:43 +00001174 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001175
Hanno Becker284d7782022-04-11 09:19:24 +01001176 while( excess_len-- )
Gilles Peskine8e464c42020-07-24 00:08:38 +02001177 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001178 *d += c; c = ( *d < c ); d++;
1179 }
Hanno Beckerdefe5692022-04-06 06:12:09 +01001180
1181 return( c );
Paul Bakker5121ce52009-01-03 21:22:43 +00001182}
1183
1184/*
1185 * Baseline multiplication: X = A * B (HAC 14.12)
1186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001188{
Janos Follath24eed8d2019-11-22 13:21:35 +00001189 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker1772e052022-04-13 06:51:40 +01001190 size_t i, j;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 mbedtls_mpi TA, TB;
Hanno Beckerda763de2022-04-13 06:50:02 +01001192 int result_is_zero = 0;
Hanno Becker73d7d792018-12-11 10:35:51 +00001193 MPI_VALIDATE_RET( X != NULL );
1194 MPI_VALIDATE_RET( A != NULL );
1195 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 if( X == A ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); A = &TA; }
1200 if( X == B ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); B = &TB; }
Paul Bakker5121ce52009-01-03 21:22:43 +00001201
Hanno Beckerda763de2022-04-13 06:50:02 +01001202 for( i = A->n; i > 0; i-- )
1203 if( A->p[i - 1] != 0 )
1204 break;
1205 if( i == 0 )
1206 result_is_zero = 1;
1207
1208 for( j = B->n; j > 0; j-- )
1209 if( B->p[j - 1] != 0 )
1210 break;
1211 if( j == 0 )
1212 result_is_zero = 1;
1213
1214 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + j ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001216
Hanno Becker1772e052022-04-13 06:51:40 +01001217 for( size_t k = 0; k < j; k++ )
Hanno Beckerfee261a2022-04-06 06:20:22 +01001218 {
1219 /* We know that there cannot be any carry-out since we're
1220 * iterating from bottom to top. */
Hanno Beckerda763de2022-04-13 06:50:02 +01001221 (void) mbedtls_mpi_core_mla( X->p + k, i + 1,
1222 A->p, i,
Hanno Beckeraef9cc42022-04-11 06:36:29 +01001223 B->p[k] );
Hanno Beckerfee261a2022-04-06 06:20:22 +01001224 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001225
Hanno Beckerda763de2022-04-13 06:50:02 +01001226 /* If the result is 0, we don't shortcut the operation, which reduces
1227 * but does not eliminate side channels leaking the zero-ness. We do
1228 * need to take care to set the sign bit properly since the library does
1229 * not fully support an MPI object with a value of 0 and s == -1. */
1230 if( result_is_zero )
1231 X->s = 1;
1232 else
1233 X->s = A->s * B->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001234
1235cleanup:
1236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001238
1239 return( ret );
1240}
1241
1242/*
1243 * Baseline multiplication: X = A * b
1244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001246{
Hanno Becker73d7d792018-12-11 10:35:51 +00001247 MPI_VALIDATE_RET( X != NULL );
1248 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
Hanno Becker35771312022-04-14 11:52:11 +01001250 size_t n = A->n;
1251 while( n > 0 && A->p[n - 1] == 0 )
1252 --n;
1253
Hanno Becker74a11a32022-04-06 06:27:00 +01001254 /* The general method below doesn't work if b==0. */
Hanno Becker35771312022-04-14 11:52:11 +01001255 if( b == 0 || n == 0 )
Paul Elliott986b55a2021-04-20 21:46:29 +01001256 return( mbedtls_mpi_lset( X, 0 ) );
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001257
Hanno Beckeraef9cc42022-04-11 06:36:29 +01001258 /* Calculate A*b as A + A*(b-1) to take advantage of mbedtls_mpi_core_mla */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001259 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001260 /* In general, A * b requires 1 limb more than b. If
1261 * A->p[n - 1] * b / b == A->p[n - 1], then A * b fits in the same
1262 * number of limbs as A and the call to grow() is not required since
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001263 * copy() will take care of the growth if needed. However, experimentally,
1264 * making the call to grow() unconditional causes slightly fewer
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001265 * calls to calloc() in ECP code, presumably because it reuses the
1266 * same mpi for a while and this way the mpi is more likely to directly
Hanno Becker9137b9c2022-04-12 10:51:54 +01001267 * grow to its final size.
1268 *
1269 * Note that calculating A*b as 0 + A*b doesn't work as-is because
1270 * A,X can be the same. */
Hanno Becker35771312022-04-14 11:52:11 +01001271 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n + 1 ) );
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001272 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
Hanno Becker35771312022-04-14 11:52:11 +01001273 mbedtls_mpi_core_mla( X->p, X->n, A->p, n, b - 1 );
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001274
1275cleanup:
1276 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001277}
1278
1279/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001280 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1281 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001282 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001283static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1,
1284 mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r )
Simon Butcher15b15d12015-11-26 19:35:03 +00001285{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001286#if defined(MBEDTLS_HAVE_UDBL)
1287 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001288#else
Simon Butcher9803d072016-01-03 00:24:34 +00001289 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
1290 const mbedtls_mpi_uint uint_halfword_mask = ( (mbedtls_mpi_uint) 1 << biH ) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001291 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1292 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001293 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001294#endif
1295
Simon Butcher15b15d12015-11-26 19:35:03 +00001296 /*
1297 * Check for overflow
1298 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001299 if( 0 == d || u1 >= d )
Simon Butcher15b15d12015-11-26 19:35:03 +00001300 {
Simon Butcherf5ba0452015-12-27 23:01:55 +00001301 if (r != NULL) *r = ~0;
Simon Butcher15b15d12015-11-26 19:35:03 +00001302
Simon Butcherf5ba0452015-12-27 23:01:55 +00001303 return ( ~0 );
Simon Butcher15b15d12015-11-26 19:35:03 +00001304 }
1305
1306#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001307 dividend = (mbedtls_t_udbl) u1 << biL;
1308 dividend |= (mbedtls_t_udbl) u0;
1309 quotient = dividend / d;
1310 if( quotient > ( (mbedtls_t_udbl) 1 << biL ) - 1 )
1311 quotient = ( (mbedtls_t_udbl) 1 << biL ) - 1;
1312
1313 if( r != NULL )
Simon Butcher9803d072016-01-03 00:24:34 +00001314 *r = (mbedtls_mpi_uint)( dividend - (quotient * d ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001315
1316 return (mbedtls_mpi_uint) quotient;
1317#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001318
1319 /*
1320 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1321 * Vol. 2 - Seminumerical Algorithms, Knuth
1322 */
1323
1324 /*
1325 * Normalize the divisor, d, and dividend, u0, u1
1326 */
Janos Follath4670f882022-07-21 18:25:42 +01001327 s = mbedtls_mpi_core_clz( d );
Simon Butcher15b15d12015-11-26 19:35:03 +00001328 d = d << s;
1329
1330 u1 = u1 << s;
Simon Butcher9803d072016-01-03 00:24:34 +00001331 u1 |= ( u0 >> ( biL - s ) ) & ( -(mbedtls_mpi_sint)s >> ( biL - 1 ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001332 u0 = u0 << s;
1333
1334 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001335 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001336
1337 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001338 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001339
1340 /*
1341 * Find the first quotient and remainder
1342 */
1343 q1 = u1 / d1;
1344 r0 = u1 - d1 * q1;
1345
1346 while( q1 >= radix || ( q1 * d0 > radix * r0 + u0_msw ) )
1347 {
1348 q1 -= 1;
1349 r0 += d1;
1350
1351 if ( r0 >= radix ) break;
1352 }
1353
Simon Butcherf5ba0452015-12-27 23:01:55 +00001354 rAX = ( u1 * radix ) + ( u0_msw - q1 * d );
Simon Butcher15b15d12015-11-26 19:35:03 +00001355 q0 = rAX / d1;
1356 r0 = rAX - q0 * d1;
1357
1358 while( q0 >= radix || ( q0 * d0 > radix * r0 + u0_lsw ) )
1359 {
1360 q0 -= 1;
1361 r0 += d1;
1362
1363 if ( r0 >= radix ) break;
1364 }
1365
1366 if (r != NULL)
Simon Butcherf5ba0452015-12-27 23:01:55 +00001367 *r = ( rAX * radix + u0_lsw - q0 * d ) >> s;
Simon Butcher15b15d12015-11-26 19:35:03 +00001368
1369 quotient = q1 * radix + q0;
1370
1371 return quotient;
1372#endif
1373}
1374
1375/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001377 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001378int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1379 const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001380{
Janos Follath24eed8d2019-11-22 13:21:35 +00001381 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001382 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 mbedtls_mpi X, Y, Z, T1, T2;
Alexander Kd19a1932019-11-01 18:20:42 +03001384 mbedtls_mpi_uint TP2[3];
Hanno Becker73d7d792018-12-11 10:35:51 +00001385 MPI_VALIDATE_RET( A != NULL );
1386 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 if( mbedtls_mpi_cmp_int( B, 0 ) == 0 )
1389 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Alexander K35d6d462019-10-31 14:46:45 +03001392 mbedtls_mpi_init( &T1 );
Alexander Kd19a1932019-11-01 18:20:42 +03001393 /*
1394 * Avoid dynamic memory allocations for constant-size T2.
1395 *
1396 * T2 is used for comparison only and the 3 limbs are assigned explicitly,
1397 * so nobody increase the size of the MPI and we're safe to use an on-stack
1398 * buffer.
1399 */
Alexander K35d6d462019-10-31 14:46:45 +03001400 T2.s = 1;
Alexander Kd19a1932019-11-01 18:20:42 +03001401 T2.n = sizeof( TP2 ) / sizeof( *TP2 );
1402 T2.p = TP2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 if( Q != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_lset( Q, 0 ) );
1407 if( R != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001408 return( 0 );
1409 }
1410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &X, A ) );
1412 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001413 X.s = Y.s = 1;
1414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &Z, A->n + 2 ) );
1416 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Z, 0 ) );
Gilles Peskine2536aa72020-07-24 00:12:59 +02001417 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, A->n + 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001418
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001419 k = mbedtls_mpi_bitlen( &Y ) % biL;
Paul Bakkerf9688572011-05-05 10:00:45 +00001420 if( k < biL - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 {
1422 k = biL - 1 - k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &X, k ) );
1424 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, k ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001425 }
1426 else k = 0;
1427
1428 n = X.n - 1;
1429 t = Y.n - 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 while( mbedtls_mpi_cmp_mpi( &X, &Y ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001433 {
1434 Z.p[n - t]++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001436 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001438
1439 for( i = n; i > t ; i-- )
1440 {
1441 if( X.p[i] >= Y.p[t] )
1442 Z.p[i - t - 1] = ~0;
1443 else
1444 {
Simon Butcher15b15d12015-11-26 19:35:03 +00001445 Z.p[i - t - 1] = mbedtls_int_div_int( X.p[i], X.p[i - 1],
1446 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 }
1448
Alexander K35d6d462019-10-31 14:46:45 +03001449 T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2];
1450 T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1];
1451 T2.p[2] = X.p[i];
1452
Paul Bakker5121ce52009-01-03 21:22:43 +00001453 Z.p[i - t - 1]++;
1454 do
1455 {
1456 Z.p[i - t - 1]--;
1457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T1, 0 ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001459 T1.p[0] = ( t < 1 ) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 T1.p[1] = Y.p[t];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001462 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 while( mbedtls_mpi_cmp_mpi( &T1, &T2 ) > 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
1466 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1467 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 if( mbedtls_mpi_cmp_int( &X, 0 ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &Y ) );
1472 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1473 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001474 Z.p[i - t - 1]--;
1475 }
1476 }
1477
1478 if( Q != NULL )
1479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( Q, &Z ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001481 Q->s = A->s * B->s;
1482 }
1483
1484 if( R != NULL )
1485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &X, k ) );
Paul Bakkerf02c5642012-11-13 10:25:21 +00001487 X.s = A->s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, &X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 if( mbedtls_mpi_cmp_int( R, 0 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001491 R->s = 1;
1492 }
1493
1494cleanup:
1495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Alexander K35d6d462019-10-31 14:46:45 +03001497 mbedtls_mpi_free( &T1 );
Alexander Kd19a1932019-11-01 18:20:42 +03001498 mbedtls_platform_zeroize( TP2, sizeof( TP2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001499
1500 return( ret );
1501}
1502
1503/*
1504 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00001505 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001506int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R,
1507 const mbedtls_mpi *A,
1508 mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001509{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001510 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001512 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001513
1514 p[0] = ( b < 0 ) ? -b : b;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001515 B.s = ( b < 0 ) ? -1 : 1;
1516 B.n = 1;
1517 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001518
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001519 return( mbedtls_mpi_div_mpi( Q, R, A, &B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001520}
1521
1522/*
1523 * Modulo: R = A mod B
1524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001526{
Janos Follath24eed8d2019-11-22 13:21:35 +00001527 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker73d7d792018-12-11 10:35:51 +00001528 MPI_VALIDATE_RET( R != NULL );
1529 MPI_VALIDATE_RET( A != NULL );
1530 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 if( mbedtls_mpi_cmp_int( B, 0 ) < 0 )
1533 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( NULL, R, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 while( mbedtls_mpi_cmp_int( R, 0 ) < 0 )
1538 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 while( mbedtls_mpi_cmp_mpi( R, B ) >= 0 )
1541 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001542
1543cleanup:
1544
1545 return( ret );
1546}
1547
1548/*
1549 * Modulo: r = A mod b
1550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001552{
Paul Bakker23986e52011-04-24 08:57:21 +00001553 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 mbedtls_mpi_uint x, y, z;
Hanno Becker73d7d792018-12-11 10:35:51 +00001555 MPI_VALIDATE_RET( r != NULL );
1556 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001557
1558 if( b == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001560
1561 if( b < 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001563
1564 /*
1565 * handle trivial cases
1566 */
Gilles Peskineae25bb02022-06-09 19:32:46 +02001567 if( b == 1 || A->n == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001568 {
1569 *r = 0;
1570 return( 0 );
1571 }
1572
1573 if( b == 2 )
1574 {
1575 *r = A->p[0] & 1;
1576 return( 0 );
1577 }
1578
1579 /*
1580 * general case
1581 */
Paul Bakker23986e52011-04-24 08:57:21 +00001582 for( i = A->n, y = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001583 {
Paul Bakker23986e52011-04-24 08:57:21 +00001584 x = A->p[i - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001585 y = ( y << biH ) | ( x >> biH );
1586 z = y / b;
1587 y -= z * b;
1588
1589 x <<= biH;
1590 y = ( y << biH ) | ( x >> biH );
1591 z = y / b;
1592 y -= z * b;
1593 }
1594
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001595 /*
1596 * If A is negative, then the current y represents a negative value.
1597 * Flipping it to the positive side.
1598 */
1599 if( A->s < 0 && y != 0 )
1600 y = b - y;
1601
Paul Bakker5121ce52009-01-03 21:22:43 +00001602 *r = y;
1603
1604 return( 0 );
1605}
1606
1607/*
1608 * Fast Montgomery initialization (thanks to Tom St Denis)
1609 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610static void mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00001611{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 mbedtls_mpi_uint x, m0 = N->p[0];
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01001613 unsigned int i;
Paul Bakker5121ce52009-01-03 21:22:43 +00001614
1615 x = m0;
1616 x += ( ( m0 + 2 ) & 4 ) << 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001617
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01001618 for( i = biL; i >= 8; i /= 2 )
1619 x *= ( 2 - ( m0 * x ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001620
1621 *mm = ~x + 1;
1622}
1623
Gilles Peskine2a82f722020-06-04 15:00:49 +02001624/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
1625 *
1626 * \param[in,out] A One of the numbers to multiply.
Gilles Peskine221626f2020-06-08 22:37:50 +02001627 * It must have at least as many limbs as N
1628 * (A->n >= N->n), and any limbs beyond n are ignored.
Gilles Peskine2a82f722020-06-04 15:00:49 +02001629 * On successful completion, A contains the result of
1630 * the multiplication A * B * R^-1 mod N where
1631 * R = (2^ciL)^n.
1632 * \param[in] B One of the numbers to multiply.
1633 * It must be nonzero and must not have more limbs than N
1634 * (B->n <= N->n).
1635 * \param[in] N The modulo. N must be odd.
1636 * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
1637 * This is -N^-1 mod 2^ciL.
1638 * \param[in,out] T A bignum for temporary storage.
Hanno Beckere1417022022-04-06 06:45:45 +01001639 * It must be at least twice the limb size of N plus 1
1640 * (T->n >= 2 * N->n + 1).
Gilles Peskine2a82f722020-06-04 15:00:49 +02001641 * Its initial content is unused and
1642 * its final content is indeterminate.
1643 * Note that unlike the usual convention in the library
1644 * for `const mbedtls_mpi*`, the content of T can change.
Paul Bakker5121ce52009-01-03 21:22:43 +00001645 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02001646static 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 +02001647 const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00001648{
Hanno Becker0235f752022-04-12 10:54:46 +01001649 size_t n, m;
1650 mbedtls_mpi_uint *d;
Paul Bakker5121ce52009-01-03 21:22:43 +00001651
1652 memset( T->p, 0, T->n * ciL );
1653
1654 d = T->p;
1655 n = N->n;
1656 m = ( B->n < n ) ? B->n : n;
1657
Hanno Becker0235f752022-04-12 10:54:46 +01001658 for( size_t i = 0; i < n; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001659 {
Hanno Becker0235f752022-04-12 10:54:46 +01001660 mbedtls_mpi_uint u0, u1;
1661
Paul Bakker5121ce52009-01-03 21:22:43 +00001662 /*
1663 * T = (T + u0*B + u1*N) / 2^biL
1664 */
1665 u0 = A->p[i];
1666 u1 = ( d[0] + u0 * B->p[0] ) * mm;
1667
Hanno Beckeraef9cc42022-04-11 06:36:29 +01001668 (void) mbedtls_mpi_core_mla( d, n + 2,
1669 B->p, m,
1670 u0 );
1671 (void) mbedtls_mpi_core_mla( d, n + 2,
1672 N->p, n,
1673 u1 );
Hanno Beckere1417022022-04-06 06:45:45 +01001674 d++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001675 }
1676
Gilles Peskine221626f2020-06-08 22:37:50 +02001677 /* At this point, d is either the desired result or the desired result
1678 * plus N. We now potentially subtract N, avoiding leaking whether the
1679 * subtraction is performed through side channels. */
Paul Bakker5121ce52009-01-03 21:22:43 +00001680
Gilles Peskine221626f2020-06-08 22:37:50 +02001681 /* Copy the n least significant limbs of d to A, so that
1682 * A = d if d < N (recall that N has n limbs). */
1683 memcpy( A->p, d, n * ciL );
Gilles Peskine09ec10a2020-06-09 10:39:38 +02001684 /* If d >= N then we want to set A to d - N. To prevent timing attacks,
Gilles Peskine221626f2020-06-08 22:37:50 +02001685 * do the calculation without using conditional tests. */
1686 /* Set d to d0 + (2^biL)^n - N where d0 is the current value of d. */
Gilles Peskine132c0972020-06-04 21:05:24 +02001687 d[n] += 1;
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001688 d[n] -= mpi_sub_hlp( n, d, d, N->p );
Gilles Peskine221626f2020-06-08 22:37:50 +02001689 /* If d0 < N then d < (2^biL)^n
1690 * so d[n] == 0 and we want to keep A as it is.
1691 * If d0 >= N then d >= (2^biL)^n, and d <= (2^biL)^n + N < 2 * (2^biL)^n
1692 * so d[n] == 1 and we want to set A to the result of the subtraction
1693 * which is d - (2^biL)^n, i.e. the n least significant limbs of d.
1694 * This exactly corresponds to a conditional assignment. */
Gabor Mezei90437e32021-10-20 11:59:27 +02001695 mbedtls_ct_mpi_uint_cond_assign( n, A->p, d, (unsigned char) d[n] );
Paul Bakker5121ce52009-01-03 21:22:43 +00001696}
1697
1698/*
1699 * Montgomery reduction: A = A * R^-1 mod N
Gilles Peskine2a82f722020-06-04 15:00:49 +02001700 *
1701 * See mpi_montmul() regarding constraints and guarantees on the parameters.
Paul Bakker5121ce52009-01-03 21:22:43 +00001702 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02001703static void mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N,
1704 mbedtls_mpi_uint mm, const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00001705{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 mbedtls_mpi_uint z = 1;
1707 mbedtls_mpi U;
Paul Bakker5121ce52009-01-03 21:22:43 +00001708
Paul Bakker8ddb6452013-02-27 14:56:33 +01001709 U.n = U.s = (int) z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001710 U.p = &z;
1711
Gilles Peskine4e91d472020-06-04 20:55:15 +02001712 mpi_montmul( A, &U, N, mm, T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001713}
1714
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001715/**
1716 * Select an MPI from a table without leaking the index.
1717 *
1718 * This is functionally equivalent to mbedtls_mpi_copy(R, T[idx]) except it
1719 * reads the entire table in order to avoid leaking the value of idx to an
1720 * attacker able to observe memory access patterns.
1721 *
1722 * \param[out] R Where to write the selected MPI.
1723 * \param[in] T The table to read from.
1724 * \param[in] T_size The number of elements in the table.
1725 * \param[in] idx The index of the element to select;
1726 * this must satisfy 0 <= idx < T_size.
1727 *
1728 * \return \c 0 on success, or a negative error code.
1729 */
1730static int mpi_select( mbedtls_mpi *R, const mbedtls_mpi *T, size_t T_size, size_t idx )
1731{
1732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1733
1734 for( size_t i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard92413ef2021-06-03 10:42:46 +02001735 {
1736 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( R, &T[i],
Gabor Mezei90437e32021-10-20 11:59:27 +02001737 (unsigned char) mbedtls_ct_size_bool_eq( i, idx ) ) );
Manuel Pégourié-Gonnard92413ef2021-06-03 10:42:46 +02001738 }
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001739
1740cleanup:
1741 return( ret );
1742}
1743
Paul Bakker5121ce52009-01-03 21:22:43 +00001744/*
1745 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
1746 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001747int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
1748 const mbedtls_mpi *E, const mbedtls_mpi *N,
Yuto Takano538a0cb2021-07-14 10:20:09 +01001749 mbedtls_mpi *prec_RR )
Paul Bakker5121ce52009-01-03 21:22:43 +00001750{
Janos Follath24eed8d2019-11-22 13:21:35 +00001751 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001752 size_t wbits, wsize, one = 1;
1753 size_t i, j, nblimbs;
1754 size_t bufsize, nbits;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 mbedtls_mpi_uint ei, mm, state;
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001756 mbedtls_mpi RR, T, W[ 1 << MBEDTLS_MPI_WINDOW_SIZE ], WW, Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00001757 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001758
Hanno Becker73d7d792018-12-11 10:35:51 +00001759 MPI_VALIDATE_RET( X != NULL );
1760 MPI_VALIDATE_RET( A != NULL );
1761 MPI_VALIDATE_RET( E != NULL );
1762 MPI_VALIDATE_RET( N != NULL );
1763
Hanno Becker8d1dd1b2017-09-28 11:02:24 +01001764 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 || ( N->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 if( mbedtls_mpi_cmp_int( E, 0 ) < 0 )
1768 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakkerf6198c12012-05-16 08:02:29 +00001769
Chris Jones9246d042020-11-25 15:12:39 +00001770 if( mbedtls_mpi_bitlen( E ) > MBEDTLS_MPI_MAX_BITS ||
1771 mbedtls_mpi_bitlen( N ) > MBEDTLS_MPI_MAX_BITS )
1772 return ( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
1773
Paul Bakkerf6198c12012-05-16 08:02:29 +00001774 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001775 * Init temps and window size
1776 */
1777 mpi_montg_init( &mm, N );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &T );
1779 mbedtls_mpi_init( &Apos );
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001780 mbedtls_mpi_init( &WW );
Paul Bakker5121ce52009-01-03 21:22:43 +00001781 memset( W, 0, sizeof( W ) );
1782
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001783 i = mbedtls_mpi_bitlen( E );
Paul Bakker5121ce52009-01-03 21:22:43 +00001784
1785 wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
1786 ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1;
1787
Peter Kolbuse6bcad32018-12-11 14:01:44 -06001788#if( MBEDTLS_MPI_WINDOW_SIZE < 6 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 if( wsize > MBEDTLS_MPI_WINDOW_SIZE )
1790 wsize = MBEDTLS_MPI_WINDOW_SIZE;
Peter Kolbuse6bcad32018-12-11 14:01:44 -06001791#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00001792
Paul Bakker5121ce52009-01-03 21:22:43 +00001793 j = N->n + 1;
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001794 /* All W[i] and X must have at least N->n limbs for the mpi_montmul()
1795 * and mpi_montred() calls later. Here we ensure that W[1] and X are
1796 * large enough, and later we'll grow other W[i] to the same length.
1797 * They must not be shrunk midway through this function!
1798 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
1800 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], j ) );
1801 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T, j * 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001802
1803 /*
Paul Bakker50546922012-05-19 08:40:49 +00001804 * Compensate for negative A (and correct at the end)
1805 */
1806 neg = ( A->s == -1 );
Paul Bakker50546922012-05-19 08:40:49 +00001807 if( neg )
1808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Apos, A ) );
Paul Bakker50546922012-05-19 08:40:49 +00001810 Apos.s = 1;
1811 A = &Apos;
1812 }
1813
1814 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001815 * If 1st call, pre-compute R^2 mod N
1816 */
Yuto Takano538a0cb2021-07-14 10:20:09 +01001817 if( prec_RR == NULL || prec_RR->p == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &RR, 1 ) );
1820 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &RR, N->n * 2 * biL ) );
1821 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &RR, &RR, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001822
Yuto Takano538a0cb2021-07-14 10:20:09 +01001823 if( prec_RR != NULL )
1824 memcpy( prec_RR, &RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001825 }
1826 else
Yuto Takano538a0cb2021-07-14 10:20:09 +01001827 memcpy( &RR, prec_RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001828
1829 /*
1830 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
1831 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 if( mbedtls_mpi_cmp_mpi( A, N ) >= 0 )
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &W[1], A, N ) );
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001835 /* This should be a no-op because W[1] is already that large before
1836 * mbedtls_mpi_mod_mpi(), but it's necessary to avoid an overflow
1837 * in mpi_montmul() below, so let's make sure. */
Gilles Peskine2aa3f162021-06-15 21:22:48 +02001838 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], N->n + 1 ) );
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001839 }
Paul Bakkerc2024f42014-01-23 20:38:35 +01001840 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[1], A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001842
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001843 /* Note that this is safe because W[1] always has at least N->n limbs
1844 * (it grew above and was preserved by mbedtls_mpi_copy()). */
Gilles Peskine4e91d472020-06-04 20:55:15 +02001845 mpi_montmul( &W[1], &RR, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001846
1847 /*
1848 * X = R^2 * R^-1 mod N = R mod N
1849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &RR ) );
Gilles Peskine4e91d472020-06-04 20:55:15 +02001851 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001852
1853 if( wsize > 1 )
1854 {
1855 /*
1856 * W[1 << (wsize - 1)] = W[1] ^ (wsize - 1)
1857 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001858 j = one << ( wsize - 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[j], N->n + 1 ) );
1861 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[j], &W[1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001862
1863 for( i = 0; i < wsize - 1; i++ )
Gilles Peskine4e91d472020-06-04 20:55:15 +02001864 mpi_montmul( &W[j], &W[j], N, mm, &T );
Paul Bakker0d7702c2013-10-29 16:18:35 +01001865
Paul Bakker5121ce52009-01-03 21:22:43 +00001866 /*
1867 * W[i] = W[i - 1] * W[1]
1868 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001869 for( i = j + 1; i < ( one << wsize ); i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[i], N->n + 1 ) );
1872 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[i], &W[i - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001873
Gilles Peskine4e91d472020-06-04 20:55:15 +02001874 mpi_montmul( &W[i], &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001875 }
1876 }
1877
1878 nblimbs = E->n;
1879 bufsize = 0;
1880 nbits = 0;
1881 wbits = 0;
1882 state = 0;
1883
1884 while( 1 )
1885 {
1886 if( bufsize == 0 )
1887 {
Paul Bakker0d7702c2013-10-29 16:18:35 +01001888 if( nblimbs == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001889 break;
1890
Paul Bakker0d7702c2013-10-29 16:18:35 +01001891 nblimbs--;
1892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 bufsize = sizeof( mbedtls_mpi_uint ) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00001894 }
1895
1896 bufsize--;
1897
1898 ei = (E->p[nblimbs] >> bufsize) & 1;
1899
1900 /*
1901 * skip leading 0s
1902 */
1903 if( ei == 0 && state == 0 )
1904 continue;
1905
1906 if( ei == 0 && state == 1 )
1907 {
1908 /*
1909 * out of window, square X
1910 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02001911 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001912 continue;
1913 }
1914
1915 /*
1916 * add ei to current window
1917 */
1918 state = 2;
1919
1920 nbits++;
Paul Bakker66d5d072014-06-17 16:39:18 +02001921 wbits |= ( ei << ( wsize - nbits ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001922
1923 if( nbits == wsize )
1924 {
1925 /*
1926 * X = X^wsize R^-1 mod N
1927 */
1928 for( i = 0; i < wsize; i++ )
Gilles Peskine4e91d472020-06-04 20:55:15 +02001929 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001930
1931 /*
1932 * X = X * W[wbits] R^-1 mod N
1933 */
Manuel Pégourié-Gonnarde22176e2021-06-10 09:34:00 +02001934 MBEDTLS_MPI_CHK( mpi_select( &WW, W, (size_t) 1 << wsize, wbits ) );
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001935 mpi_montmul( X, &WW, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001936
1937 state--;
1938 nbits = 0;
1939 wbits = 0;
1940 }
1941 }
1942
1943 /*
1944 * process the remaining bits
1945 */
1946 for( i = 0; i < nbits; i++ )
1947 {
Gilles Peskine4e91d472020-06-04 20:55:15 +02001948 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001949
1950 wbits <<= 1;
1951
Paul Bakker66d5d072014-06-17 16:39:18 +02001952 if( ( wbits & ( one << wsize ) ) != 0 )
Gilles Peskine4e91d472020-06-04 20:55:15 +02001953 mpi_montmul( X, &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001954 }
1955
1956 /*
1957 * X = A^E * R * R^-1 mod N = A^E mod N
1958 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02001959 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001960
Hanno Beckera4af1c42017-04-18 09:07:45 +01001961 if( neg && E->n != 0 && ( E->p[0] & 1 ) != 0 )
Paul Bakkerf6198c12012-05-16 08:02:29 +00001962 {
1963 X->s = -1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, N, X ) );
Paul Bakkerf6198c12012-05-16 08:02:29 +00001965 }
1966
Paul Bakker5121ce52009-01-03 21:22:43 +00001967cleanup:
1968
Paul Bakker66d5d072014-06-17 16:39:18 +02001969 for( i = ( one << ( wsize - 1 ) ); i < ( one << wsize ); i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970 mbedtls_mpi_free( &W[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +00001971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972 mbedtls_mpi_free( &W[1] ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &Apos );
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001973 mbedtls_mpi_free( &WW );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001974
Yuto Takano538a0cb2021-07-14 10:20:09 +01001975 if( prec_RR == NULL || prec_RR->p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00001977
1978 return( ret );
1979}
1980
Paul Bakker5121ce52009-01-03 21:22:43 +00001981/*
1982 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
1983 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001985{
Janos Follath24eed8d2019-11-22 13:21:35 +00001986 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001987 size_t lz, lzt;
Alexander Ke8ad49f2019-08-16 16:16:07 +03001988 mbedtls_mpi TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00001989
Hanno Becker73d7d792018-12-11 10:35:51 +00001990 MPI_VALIDATE_RET( G != NULL );
1991 MPI_VALIDATE_RET( A != NULL );
1992 MPI_VALIDATE_RET( B != NULL );
1993
Alexander Ke8ad49f2019-08-16 16:16:07 +03001994 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) );
1997 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 lz = mbedtls_mpi_lsb( &TA );
2000 lzt = mbedtls_mpi_lsb( &TB );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002001
Gilles Peskine27253bc2021-06-09 13:26:43 +02002002 /* The loop below gives the correct result when A==0 but not when B==0.
2003 * So have a special case for B==0. Leverage the fact that we just
2004 * calculated the lsb and lsb(B)==0 iff B is odd or 0 to make the test
2005 * slightly more efficient than cmp_int(). */
2006 if( lzt == 0 && mbedtls_mpi_get_bit( &TB, 0 ) == 0 )
2007 {
2008 ret = mbedtls_mpi_copy( G, A );
2009 goto cleanup;
2010 }
2011
Paul Bakker66d5d072014-06-17 16:39:18 +02002012 if( lzt < lz )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002013 lz = lzt;
2014
Paul Bakker5121ce52009-01-03 21:22:43 +00002015 TA.s = TB.s = 1;
2016
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002017 /* We mostly follow the procedure described in HAC 14.54, but with some
2018 * minor differences:
2019 * - Sequences of multiplications or divisions by 2 are grouped into a
2020 * single shift operation.
Gilles Peskineb09c7ee2021-06-21 18:58:39 +02002021 * - The procedure in HAC assumes that 0 < TB <= TA.
2022 * - The condition TB <= TA is not actually necessary for correctness.
2023 * TA and TB have symmetric roles except for the loop termination
2024 * condition, and the shifts at the beginning of the loop body
2025 * remove any significance from the ordering of TA vs TB before
2026 * the shifts.
2027 * - If TA = 0, the loop goes through 0 iterations and the result is
2028 * correctly TB.
2029 * - The case TB = 0 was short-circuited above.
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002030 *
2031 * For the correctness proof below, decompose the original values of
2032 * A and B as
2033 * A = sa * 2^a * A' with A'=0 or A' odd, and sa = +-1
2034 * B = sb * 2^b * B' with B'=0 or B' odd, and sb = +-1
2035 * Then gcd(A, B) = 2^{min(a,b)} * gcd(A',B'),
2036 * and gcd(A',B') is odd or 0.
2037 *
2038 * At the beginning, we have TA = |A| and TB = |B| so gcd(A,B) = gcd(TA,TB).
2039 * The code maintains the following invariant:
2040 * gcd(A,B) = 2^k * gcd(TA,TB) for some k (I)
Gilles Peskine4df3f1f2021-06-15 22:09:39 +02002041 */
2042
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002043 /* Proof that the loop terminates:
2044 * At each iteration, either the right-shift by 1 is made on a nonzero
2045 * value and the nonnegative integer bitlen(TA) + bitlen(TB) decreases
2046 * by at least 1, or the right-shift by 1 is made on zero and then
2047 * TA becomes 0 which ends the loop (TB cannot be 0 if it is right-shifted
2048 * since in that case TB is calculated from TB-TA with the condition TB>TA).
2049 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050 while( mbedtls_mpi_cmp_int( &TA, 0 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002051 {
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002052 /* Divisions by 2 preserve the invariant (I). */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, mbedtls_mpi_lsb( &TA ) ) );
2054 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, mbedtls_mpi_lsb( &TB ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002055
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002056 /* Set either TA or TB to |TA-TB|/2. Since TA and TB are both odd,
2057 * TA-TB is even so the division by 2 has an integer result.
2058 * Invariant (I) is preserved since any odd divisor of both TA and TB
2059 * also divides |TA-TB|/2, and any odd divisor of both TA and |TA-TB|/2
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002060 * also divides TB, and any odd divisor of both TB and |TA-TB|/2 also
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002061 * divides TA.
2062 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063 if( mbedtls_mpi_cmp_mpi( &TA, &TB ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TA, &TA, &TB ) );
2066 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002067 }
2068 else
2069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TB, &TB, &TA ) );
2071 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 }
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002073 /* Note that one of TA or TB is still odd. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 }
2075
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002076 /* By invariant (I), gcd(A,B) = 2^k * gcd(TA,TB) for some k.
2077 * At the loop exit, TA = 0, so gcd(TA,TB) = TB.
2078 * - If there was at least one loop iteration, then one of TA or TB is odd,
2079 * and TA = 0, so TB is odd and gcd(TA,TB) = gcd(A',B'). In this case,
2080 * lz = min(a,b) so gcd(A,B) = 2^lz * TB.
2081 * - If there was no loop iteration, then A was 0, and gcd(A,B) = B.
Gilles Peskine4d3fd362021-06-21 11:40:38 +02002082 * 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 +02002083 */
2084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &TB, lz ) );
2086 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( G, &TB ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002087
2088cleanup:
2089
Alexander Ke8ad49f2019-08-16 16:16:07 +03002090 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002091
2092 return( ret );
2093}
2094
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002095/* Fill X with n_bytes random bytes.
2096 * X must already have room for those bytes.
Gilles Peskineafb2bd22021-06-03 11:51:09 +02002097 * The ordering of the bytes returned from the RNG is suitable for
2098 * deterministic ECDSA (see RFC 6979 §3.3 and mbedtls_mpi_random()).
Gilles Peskineebe9b6a2021-04-13 21:55:35 +02002099 * The size and sign of X are unchanged.
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002100 * n_bytes must not be 0.
2101 */
2102static int mpi_fill_random_internal(
2103 mbedtls_mpi *X, size_t n_bytes,
2104 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
2105{
2106 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2107 const size_t limbs = CHARS_TO_LIMBS( n_bytes );
2108 const size_t overhead = ( limbs * ciL ) - n_bytes;
2109
2110 if( X->n < limbs )
2111 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002112
Gilles Peskineebe9b6a2021-04-13 21:55:35 +02002113 memset( X->p, 0, overhead );
2114 memset( (unsigned char *) X->p + limbs * ciL, 0, ( X->n - limbs ) * ciL );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002115 MBEDTLS_MPI_CHK( f_rng( p_rng, (unsigned char *) X->p + overhead, n_bytes ) );
Janos Follath4670f882022-07-21 18:25:42 +01002116 mbedtls_mpi_core_bigendian_to_host( X->p, limbs );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002117
2118cleanup:
2119 return( ret );
2120}
2121
Paul Bakker33dc46b2014-04-30 16:11:39 +02002122/*
2123 * Fill X with size bytes of random.
2124 *
2125 * Use a temporary bytes representation to make sure the result is the same
Paul Bakkerc37b0ac2014-05-01 14:19:23 +02002126 * regardless of the platform endianness (useful when f_rng is actually
Paul Bakker33dc46b2014-04-30 16:11:39 +02002127 * deterministic, eg for tests).
2128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002130 int (*f_rng)(void *, unsigned char *, size_t),
2131 void *p_rng )
Paul Bakker287781a2011-03-26 13:18:49 +00002132{
Janos Follath24eed8d2019-11-22 13:21:35 +00002133 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath620c58c2022-08-15 11:58:42 +01002134 const size_t limbs = CHARS_TO_LIMBS( size );
Hanno Beckerda1655a2017-10-18 14:21:44 +01002135
Hanno Becker8ce11a32018-12-19 16:18:52 +00002136 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002137 MPI_VALIDATE_RET( f_rng != NULL );
Paul Bakker33dc46b2014-04-30 16:11:39 +02002138
Hanno Beckerda1655a2017-10-18 14:21:44 +01002139 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskineed32b572021-06-02 22:17:52 +02002140 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002141 if( size == 0 )
2142 return( 0 );
Paul Bakker287781a2011-03-26 13:18:49 +00002143
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002144 ret = mpi_fill_random_internal( X, size, f_rng, p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +00002145
2146cleanup:
2147 return( ret );
2148}
2149
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002150int mbedtls_mpi_random( mbedtls_mpi *X,
2151 mbedtls_mpi_sint min,
2152 const mbedtls_mpi *N,
2153 int (*f_rng)(void *, unsigned char *, size_t),
2154 void *p_rng )
2155{
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002156 int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskinee5381682021-04-13 21:23:25 +02002157 int count;
Gilles Peskine5b0589e2021-04-13 21:09:10 +02002158 unsigned lt_lower = 1, lt_upper = 0;
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002159 size_t n_bits = mbedtls_mpi_bitlen( N );
2160 size_t n_bytes = ( n_bits + 7 ) / 8;
Gilles Peskine5b0589e2021-04-13 21:09:10 +02002161 mbedtls_mpi lower_bound;
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002162
Gilles Peskine1e918f42021-03-29 22:14:51 +02002163 if( min < 0 )
2164 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2165 if( mbedtls_mpi_cmp_int( N, min ) <= 0 )
2166 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2167
Gilles Peskinee5381682021-04-13 21:23:25 +02002168 /*
2169 * When min == 0, each try has at worst a probability 1/2 of failing
2170 * (the msb has a probability 1/2 of being 0, and then the result will
2171 * be < N), so after 30 tries failure probability is a most 2**(-30).
2172 *
2173 * When N is just below a power of 2, as is the case when generating
Gilles Peskinee842e582021-04-15 11:45:19 +02002174 * a random scalar on most elliptic curves, 1 try is enough with
Gilles Peskinee5381682021-04-13 21:23:25 +02002175 * overwhelming probability. When N is just above a power of 2,
Gilles Peskinee842e582021-04-15 11:45:19 +02002176 * as when generating a random scalar on secp224k1, each try has
Gilles Peskinee5381682021-04-13 21:23:25 +02002177 * a probability of failing that is almost 1/2.
2178 *
2179 * The probabilities are almost the same if min is nonzero but negligible
2180 * compared to N. This is always the case when N is crypto-sized, but
2181 * it's convenient to support small N for testing purposes. When N
2182 * is small, use a higher repeat count, otherwise the probability of
2183 * failure is macroscopic.
2184 */
Gilles Peskine87823d72021-06-02 21:18:59 +02002185 count = ( n_bytes > 4 ? 30 : 250 );
Gilles Peskinee5381682021-04-13 21:23:25 +02002186
Gilles Peskine5b0589e2021-04-13 21:09:10 +02002187 mbedtls_mpi_init( &lower_bound );
2188
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002189 /* Ensure that target MPI has exactly the same number of limbs
2190 * as the upper bound, even if the upper bound has leading zeros.
2191 * This is necessary for the mbedtls_mpi_lt_mpi_ct() check. */
Gilles Peskineed32b572021-06-02 22:17:52 +02002192 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, N->n ) );
Gilles Peskine5b0589e2021-04-13 21:09:10 +02002193 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &lower_bound, N->n ) );
2194 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &lower_bound, min ) );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002195
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002196 /*
2197 * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA)
2198 * when f_rng is a suitably parametrized instance of HMAC_DRBG:
2199 * - use the same byte ordering;
2200 * - keep the leftmost n_bits bits of the generated octet string;
2201 * - try until result is in the desired range.
2202 * This also avoids any bias, which is especially important for ECDSA.
2203 */
2204 do
2205 {
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002206 MBEDTLS_MPI_CHK( mpi_fill_random_internal( X, n_bytes, f_rng, p_rng ) );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002207 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, 8 * n_bytes - n_bits ) );
2208
Gilles Peskinee5381682021-04-13 21:23:25 +02002209 if( --count == 0 )
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002210 {
2211 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2212 goto cleanup;
2213 }
2214
Gilles Peskine5b0589e2021-04-13 21:09:10 +02002215 MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( X, &lower_bound, &lt_lower ) );
2216 MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( X, N, &lt_upper ) );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002217 }
Gilles Peskine5b0589e2021-04-13 21:09:10 +02002218 while( lt_lower != 0 || lt_upper == 0 );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002219
2220cleanup:
Gilles Peskine5b0589e2021-04-13 21:09:10 +02002221 mbedtls_mpi_free( &lower_bound );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002222 return( ret );
2223}
2224
Paul Bakker5121ce52009-01-03 21:22:43 +00002225/*
2226 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002229{
Janos Follath24eed8d2019-11-22 13:21:35 +00002230 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Hanno Becker73d7d792018-12-11 10:35:51 +00002232 MPI_VALIDATE_RET( X != NULL );
2233 MPI_VALIDATE_RET( A != NULL );
2234 MPI_VALIDATE_RET( N != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002235
Hanno Becker4bcb4912017-04-18 15:49:39 +01002236 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TU ); mbedtls_mpi_init( &U1 ); mbedtls_mpi_init( &U2 );
2240 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &TB ); mbedtls_mpi_init( &TV );
2241 mbedtls_mpi_init( &V1 ); mbedtls_mpi_init( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, A, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 goto cleanup;
2249 }
2250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &TA, A, N ) );
2252 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TU, &TA ) );
2253 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, N ) );
2254 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TV, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U1, 1 ) );
2257 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U2, 0 ) );
2258 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V1, 0 ) );
2259 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002260
2261 do
2262 {
2263 while( ( TU.p[0] & 1 ) == 0 )
2264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TU, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002266
2267 if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
2268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &U1, &U1, &TB ) );
2270 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002271 }
2272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U1, 1 ) );
2274 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002275 }
2276
2277 while( ( TV.p[0] & 1 ) == 0 )
2278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TV, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002280
2281 if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
2282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, &TB ) );
2284 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002285 }
2286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V1, 1 ) );
2288 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002289 }
2290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 if( mbedtls_mpi_cmp_mpi( &TU, &TV ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TU, &TU, &TV ) );
2294 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U1, &U1, &V1 ) );
2295 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &V2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002296 }
2297 else
2298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TV, &TV, &TU ) );
2300 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, &U1 ) );
2301 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &U2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002302 }
2303 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 while( mbedtls_mpi_cmp_int( &TU, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 while( mbedtls_mpi_cmp_int( &V1, 0 ) < 0 )
2307 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 while( mbedtls_mpi_cmp_mpi( &V1, N ) >= 0 )
2310 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &V1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002313
2314cleanup:
2315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TU ); mbedtls_mpi_free( &U1 ); mbedtls_mpi_free( &U2 );
2317 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TV );
2318 mbedtls_mpi_free( &V1 ); mbedtls_mpi_free( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002319
2320 return( ret );
2321}
2322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002324
Paul Bakker5121ce52009-01-03 21:22:43 +00002325static const int small_prime[] =
2326{
2327 3, 5, 7, 11, 13, 17, 19, 23,
2328 29, 31, 37, 41, 43, 47, 53, 59,
2329 61, 67, 71, 73, 79, 83, 89, 97,
2330 101, 103, 107, 109, 113, 127, 131, 137,
2331 139, 149, 151, 157, 163, 167, 173, 179,
2332 181, 191, 193, 197, 199, 211, 223, 227,
2333 229, 233, 239, 241, 251, 257, 263, 269,
2334 271, 277, 281, 283, 293, 307, 311, 313,
2335 317, 331, 337, 347, 349, 353, 359, 367,
2336 373, 379, 383, 389, 397, 401, 409, 419,
2337 421, 431, 433, 439, 443, 449, 457, 461,
2338 463, 467, 479, 487, 491, 499, 503, 509,
2339 521, 523, 541, 547, 557, 563, 569, 571,
2340 577, 587, 593, 599, 601, 607, 613, 617,
2341 619, 631, 641, 643, 647, 653, 659, 661,
2342 673, 677, 683, 691, 701, 709, 719, 727,
2343 733, 739, 743, 751, 757, 761, 769, 773,
2344 787, 797, 809, 811, 821, 823, 827, 829,
2345 839, 853, 857, 859, 863, 877, 881, 883,
2346 887, 907, 911, 919, 929, 937, 941, 947,
2347 953, 967, 971, 977, 983, 991, 997, -103
2348};
2349
2350/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002351 * Small divisors test (X must be positive)
2352 *
2353 * Return values:
2354 * 0: no small factor (possible prime, more tests needed)
2355 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002357 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359static int mpi_check_small_factors( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +00002360{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002361 int ret = 0;
2362 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 mbedtls_mpi_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +00002364
Paul Bakker5121ce52009-01-03 21:22:43 +00002365 if( ( X->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002367
2368 for( i = 0; small_prime[i] > 0; i++ )
2369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 if( mbedtls_mpi_cmp_int( X, small_prime[i] ) <= 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002371 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, small_prime[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002374
2375 if( r == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002377 }
2378
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002379cleanup:
2380 return( ret );
2381}
2382
2383/*
2384 * Miller-Rabin pseudo-primality test (HAC 4.24)
2385 */
Janos Follathda31fa12018-09-03 14:45:23 +01002386static int mpi_miller_rabin( const mbedtls_mpi *X, size_t rounds,
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002387 int (*f_rng)(void *, unsigned char *, size_t),
2388 void *p_rng )
2389{
Pascal Junodb99183d2015-03-11 16:49:45 +01002390 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002391 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002393
Hanno Becker8ce11a32018-12-19 16:18:52 +00002394 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002395 MPI_VALIDATE_RET( f_rng != NULL );
2396
2397 mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R );
2398 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 mbedtls_mpi_init( &RR );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002400
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 /*
2402 * W = |X| - 1
2403 * R = W >> lsb( W )
2404 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &W, X, 1 ) );
2406 s = mbedtls_mpi_lsb( &W );
2407 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R, &W ) );
2408 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &R, s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002409
Janos Follathda31fa12018-09-03 14:45:23 +01002410 for( i = 0; i < rounds; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002411 {
2412 /*
2413 * pick a random A, 1 < A < |X| - 1
2414 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002415 count = 0;
2416 do {
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002417 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
Pascal Junodb99183d2015-03-11 16:49:45 +01002418
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002419 j = mbedtls_mpi_bitlen( &A );
2420 k = mbedtls_mpi_bitlen( &W );
Pascal Junodb99183d2015-03-11 16:49:45 +01002421 if (j > k) {
Darryl Greene3f95ed2018-10-02 13:21:35 +01002422 A.p[A.n - 1] &= ( (mbedtls_mpi_uint) 1 << ( k - ( A.n - 1 ) * biL - 1 ) ) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002423 }
2424
2425 if (count++ > 30) {
Jens Wiklanderf08aa3e2019-01-17 13:30:57 +01002426 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2427 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002428 }
2429
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002430 } while ( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 ||
2431 mbedtls_mpi_cmp_int( &A, 1 ) <= 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002432
2433 /*
2434 * A = A^R mod |X|
2435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &A, &A, &R, X, &RR ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 if( mbedtls_mpi_cmp_mpi( &A, &W ) == 0 ||
2439 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002440 continue;
2441
2442 j = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 while( j < s && mbedtls_mpi_cmp_mpi( &A, &W ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002444 {
2445 /*
2446 * A = A * A mod |X|
2447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &A, &A ) );
2449 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &A, &T, X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 if( mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002452 break;
2453
2454 j++;
2455 }
2456
2457 /*
2458 * not prime if A != |X| - 1 or A == 1
2459 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460 if( mbedtls_mpi_cmp_mpi( &A, &W ) != 0 ||
2461 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002464 break;
2465 }
2466 }
2467
2468cleanup:
Hanno Becker73d7d792018-12-11 10:35:51 +00002469 mbedtls_mpi_free( &W ); mbedtls_mpi_free( &R );
2470 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002472
2473 return( ret );
2474}
2475
2476/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002477 * Pseudo-primality test: small factors, then Miller-Rabin
2478 */
Janos Follatha0b67c22018-09-18 14:48:23 +01002479int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds,
2480 int (*f_rng)(void *, unsigned char *, size_t),
2481 void *p_rng )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002482{
Janos Follath24eed8d2019-11-22 13:21:35 +00002483 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484 mbedtls_mpi XX;
Hanno Becker8ce11a32018-12-19 16:18:52 +00002485 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002486 MPI_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002487
2488 XX.s = 1;
2489 XX.n = X->n;
2490 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492 if( mbedtls_mpi_cmp_int( &XX, 0 ) == 0 ||
2493 mbedtls_mpi_cmp_int( &XX, 1 ) == 0 )
2494 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496 if( mbedtls_mpi_cmp_int( &XX, 2 ) == 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002497 return( 0 );
2498
2499 if( ( ret = mpi_check_small_factors( &XX ) ) != 0 )
2500 {
2501 if( ret == 1 )
2502 return( 0 );
2503
2504 return( ret );
2505 }
2506
Janos Follathda31fa12018-09-03 14:45:23 +01002507 return( mpi_miller_rabin( &XX, rounds, f_rng, p_rng ) );
Janos Follathf301d232018-08-14 13:34:01 +01002508}
2509
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002510/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002511 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002512 *
Janos Follathf301d232018-08-14 13:34:01 +01002513 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2514 * be either 1024 bits or 1536 bits long, and flags must contain
2515 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 */
Janos Follath7c025a92018-08-14 11:08:41 +01002517int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002518 int (*f_rng)(void *, unsigned char *, size_t),
2519 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002520{
Jethro Beekman66689272018-02-14 19:24:10 -08002521#ifdef MBEDTLS_HAVE_INT64
2522// ceil(2^63.5)
2523#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2524#else
2525// ceil(2^31.5)
2526#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2527#endif
2528 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002529 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002530 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 mbedtls_mpi_uint r;
2532 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002533
Hanno Becker8ce11a32018-12-19 16:18:52 +00002534 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002535 MPI_VALIDATE_RET( f_rng != NULL );
2536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 if( nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS )
2538 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 mbedtls_mpi_init( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002541
2542 n = BITS_TO_LIMBS( nbits );
2543
Janos Follathda31fa12018-09-03 14:45:23 +01002544 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR ) == 0 )
2545 {
2546 /*
2547 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2548 */
2549 rounds = ( ( nbits >= 1300 ) ? 2 : ( nbits >= 850 ) ? 3 :
2550 ( nbits >= 650 ) ? 4 : ( nbits >= 350 ) ? 8 :
2551 ( nbits >= 250 ) ? 12 : ( nbits >= 150 ) ? 18 : 27 );
2552 }
2553 else
2554 {
2555 /*
2556 * 2^-100 error probability, number of rounds computed based on HAC,
2557 * fact 4.48
2558 */
2559 rounds = ( ( nbits >= 1450 ) ? 4 : ( nbits >= 1150 ) ? 5 :
2560 ( nbits >= 1000 ) ? 6 : ( nbits >= 850 ) ? 7 :
2561 ( nbits >= 750 ) ? 8 : ( nbits >= 500 ) ? 13 :
2562 ( nbits >= 250 ) ? 28 : ( nbits >= 150 ) ? 40 : 51 );
2563 }
2564
Jethro Beekman66689272018-02-14 19:24:10 -08002565 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 {
Jethro Beekman66689272018-02-14 19:24:10 -08002567 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
2568 /* make sure generated number is at least (nbits-1)+0.5 bits (FIPS 186-4 §B.3.3 steps 4.4, 5.5) */
2569 if( X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2 ) continue;
2570
2571 k = n * biL;
2572 if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits ) );
2573 X->p[0] |= 1;
2574
Janos Follath7c025a92018-08-14 11:08:41 +01002575 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002576 {
Janos Follatha0b67c22018-09-18 14:48:23 +01002577 ret = mbedtls_mpi_is_prime_ext( X, rounds, f_rng, p_rng );
Jethro Beekman66689272018-02-14 19:24:10 -08002578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002581 }
Jethro Beekman66689272018-02-14 19:24:10 -08002582 else
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002584 /*
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01002585 * A necessary condition for Y and X = 2Y + 1 to be prime
Jethro Beekman66689272018-02-14 19:24:10 -08002586 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2587 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002588 */
Jethro Beekman66689272018-02-14 19:24:10 -08002589
2590 X->p[0] |= 2;
2591
2592 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, 3 ) );
2593 if( r == 0 )
2594 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 8 ) );
2595 else if( r == 1 )
2596 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 4 ) );
2597
2598 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
2599 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, X ) );
2600 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, 1 ) );
2601
2602 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002603 {
Jethro Beekman66689272018-02-14 19:24:10 -08002604 /*
2605 * First, check small factors for X and Y
2606 * before doing Miller-Rabin on any of them
2607 */
2608 if( ( ret = mpi_check_small_factors( X ) ) == 0 &&
2609 ( ret = mpi_check_small_factors( &Y ) ) == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002610 ( ret = mpi_miller_rabin( X, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002611 == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002612 ( ret = mpi_miller_rabin( &Y, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002613 == 0 )
Jethro Beekman66689272018-02-14 19:24:10 -08002614 goto cleanup;
2615
2616 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
2617 goto cleanup;
2618
2619 /*
2620 * Next candidates. We want to preserve Y = (X-1) / 2 and
2621 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2622 * so up Y by 6 and X by 12.
2623 */
2624 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 12 ) );
2625 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &Y, &Y, 6 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002626 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002627 }
2628 }
2629
2630cleanup:
2631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632 mbedtls_mpi_free( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002633
2634 return( ret );
2635}
2636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002640
Paul Bakker23986e52011-04-24 08:57:21 +00002641#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002642
2643static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2644{
2645 { 693, 609, 21 },
2646 { 1764, 868, 28 },
2647 { 768454923, 542167814, 1 }
2648};
2649
Paul Bakker5121ce52009-01-03 21:22:43 +00002650/*
2651 * Checkup routine
2652 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653int mbedtls_mpi_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002654{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002655 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00002657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); mbedtls_mpi_init( &X );
2659 mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &U ); mbedtls_mpi_init( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &A, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002662 "EFE021C2645FD1DC586E69184AF4A31E" \
2663 "D5F53E93B5F123FA41680867BA110131" \
2664 "944FE7952E2517337780CB0DB80E61AA" \
2665 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6" ) );
2666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002668 "B2E7EFD37075B9F03FF989C7C5051C20" \
2669 "34D2A323810251127E7BF8625A4F49A5" \
2670 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
2671 "5B5C25763222FEFCCFC38B832366C29E" ) );
2672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &N, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 "0066A198186C18C10B2F5ED9B522752A" \
2675 "9830B69916E535C8F047518A889A43A5" \
2676 "94B6BED27A168D31D4A52F88925AA8F5" ) );
2677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002681 "602AB7ECA597A3D6B56FF9829A5E8B85" \
2682 "9E857EA95A03512E2BAE7391688D264A" \
2683 "A5663B0341DB9CCFD2C4C5F421FEC814" \
2684 "8001B72E848A38CAE1C65F78E56ABDEF" \
2685 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
2686 "ECF677152EF804370C1A305CAF3B5BF1" \
2687 "30879B56C61DE584A0F53A2447A51E" ) );
2688
2689 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690 mbedtls_printf( " MPI test #1 (mul_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002693 {
2694 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002696
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002697 ret = 1;
2698 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 }
2700
2701 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &X, &Y, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 "256567336059E52CAE22925474705F39A94" ) );
2708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &V, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002710 "6613F26162223DF488E9CD48CC132C7A" \
2711 "0AC93C701B001B092E4E5B9F73BCD27B" \
2712 "9EE50D0657C77F374E903CDFA4C642" ) );
2713
2714 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 mbedtls_printf( " MPI test #2 (div_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ||
2718 mbedtls_mpi_cmp_mpi( &Y, &V ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002719 {
2720 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002722
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002723 ret = 1;
2724 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002725 }
2726
2727 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &X, &A, &E, &N, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 "36E139AEA55215609D2816998ED020BB" \
2734 "BD96C37890F65171D948E9BC7CBAA4D9" \
2735 "325D24D6A3C12710F10A09FA08AB87" ) );
2736
2737 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738 mbedtls_printf( " MPI test #3 (exp_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002741 {
2742 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002744
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002745 ret = 1;
2746 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002747 }
2748
2749 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002755 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
2756 "C3DBA76456363A10869622EAC2DD84EC" \
2757 "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) );
2758
2759 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760 mbedtls_printf( " MPI test #4 (inv_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002763 {
2764 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002766
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002767 ret = 1;
2768 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002769 }
2770
2771 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002773
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002774 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775 mbedtls_printf( " MPI test #5 (simple gcd): " );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002776
Paul Bakker66d5d072014-06-17 16:39:18 +02002777 for( i = 0; i < GCD_PAIR_COUNT; i++ )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &X, gcd_pairs[i][0] ) );
2780 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Y, gcd_pairs[i][1] ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &A, &X, &Y ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784 if( mbedtls_mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 )
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002785 {
2786 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 mbedtls_printf( "failed at %d\n", i );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002788
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002789 ret = 1;
2790 goto cleanup;
2791 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002792 }
2793
2794 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795 mbedtls_printf( "passed\n" );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002796
Paul Bakker5121ce52009-01-03 21:22:43 +00002797cleanup:
2798
2799 if( ret != 0 && verbose != 0 )
Kenneth Soerensen518d4352020-04-01 17:22:45 +02002800 mbedtls_printf( "Unexpected error, return code = %08X\n", (unsigned int) ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &X );
2803 mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &U ); mbedtls_mpi_free( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00002804
2805 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002807
2808 return( ret );
2809}
2810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813#endif /* MBEDTLS_BIGNUM_C */