blob: a238f8ce8774338de2936b98076e1bddd20babd3 [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020053#else
Rich Evans00ab4702015-02-06 13:43:58 +000054#include <stdio.h>
55#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#define mbedtls_printf printf
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020057#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020059#endif
60
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +010061#define MPI_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
62
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050063/* Implementation that should never be optimized out by the compiler */
Andres Amaya Garcia6698d2f2018-04-24 08:39:07 -050064static void mbedtls_mpi_zeroize( mbedtls_mpi_uint *v, size_t n )
65{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050066 mbedtls_platform_zeroize( v, ciL * n );
67}
68
Paul Bakker5121ce52009-01-03 21:22:43 +000069/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000070 * Initialize one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072void mbedtls_mpi_init( mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +000073{
Hanno Becker73d7d792018-12-11 10:35:51 +000074 MPI_VALIDATE( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +000075
Paul Bakker6c591fa2011-05-05 11:49:20 +000076 X->s = 1;
77 X->n = 0;
78 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000079}
80
81/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000082 * Unallocate one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084void mbedtls_mpi_free( mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +000085{
Paul Bakker6c591fa2011-05-05 11:49:20 +000086 if( X == NULL )
87 return;
Paul Bakker5121ce52009-01-03 21:22:43 +000088
Paul Bakker6c591fa2011-05-05 11:49:20 +000089 if( X->p != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000090 {
Alexey Skalozube17a8da2016-01-13 17:19:33 +020091 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +000093 }
94
Paul Bakker6c591fa2011-05-05 11:49:20 +000095 X->s = 1;
96 X->n = 0;
97 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000098}
99
100/*
101 * Enlarge to the specified number of limbs
102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000104{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_mpi_uint *p;
Hanno Becker73d7d792018-12-11 10:35:51 +0000106 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200109 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Paul Bakkerf9688572011-05-05 10:00:45 +0000110
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 if( X->n < nblimbs )
112 {
Simon Butcher29176892016-05-20 00:19:09 +0100113 if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( nblimbs, ciL ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200114 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 if( X->p != NULL )
117 {
118 memcpy( p, X->p, X->n * ciL );
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200119 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 }
122
123 X->n = nblimbs;
124 X->p = p;
125 }
126
127 return( 0 );
128}
129
130/*
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100131 * Resize down as much as possible,
132 * while keeping at least the specified number of limbs
133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100135{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_mpi_uint *p;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100137 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000138 MPI_VALIDATE_RET( X != NULL );
139
140 if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
141 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100142
Gilles Peskinee2f563e2020-01-20 21:17:43 +0100143 /* Actually resize up if there are currently fewer than nblimbs limbs. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100144 if( X->n <= nblimbs )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 return( mbedtls_mpi_grow( X, nblimbs ) );
Gilles Peskine322752b2020-01-21 13:59:51 +0100146 /* After this point, then X->n > nblimbs and in particular X->n > 0. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100147
148 for( i = X->n - 1; i > 0; i-- )
149 if( X->p[i] != 0 )
150 break;
151 i++;
152
153 if( i < nblimbs )
154 i = nblimbs;
155
Simon Butcher29176892016-05-20 00:19:09 +0100156 if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( i, ciL ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200157 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100158
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100159 if( X->p != NULL )
160 {
161 memcpy( p, X->p, i * ciL );
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200162 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_free( X->p );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100164 }
165
166 X->n = i;
167 X->p = p;
168
169 return( 0 );
170}
171
Gilles Peskineed32b572021-06-02 22:17:52 +0200172/* Resize X to have exactly n limbs and set it to 0. */
173static int mbedtls_mpi_resize_clear( mbedtls_mpi *X, size_t limbs )
174{
175 if( limbs == 0 )
176 {
177 mbedtls_mpi_free( X );
178 return( 0 );
179 }
180 else if( X->n == limbs )
181 {
182 memset( X->p, 0, limbs * ciL );
183 X->s = 1;
184 return( 0 );
185 }
186 else
187 {
188 mbedtls_mpi_free( X );
189 return( mbedtls_mpi_grow( X, limbs ) );
190 }
191}
192
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100193/*
Gilles Peskine3da1a8f2021-06-08 23:17:42 +0200194 * Copy the contents of Y into X.
195 *
196 * This function is not constant-time. Leading zeros in Y may be removed.
197 *
198 * Ensure that X does not shrink. This is not guaranteed by the public API,
199 * but some code in the bignum module relies on this property, for example
200 * in mbedtls_mpi_exp_mod().
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000203{
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100204 int ret = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000205 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000206 MPI_VALIDATE_RET( X != NULL );
207 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000208
209 if( X == Y )
210 return( 0 );
211
Gilles Peskinedb420622020-01-20 21:12:50 +0100212 if( Y->n == 0 )
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200213 {
Gilles Peskine3da1a8f2021-06-08 23:17:42 +0200214 if( X->n != 0 )
215 {
216 X->s = 1;
217 memset( X->p, 0, X->n * ciL );
218 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200219 return( 0 );
220 }
221
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 for( i = Y->n - 1; i > 0; i-- )
223 if( Y->p[i] != 0 )
224 break;
225 i++;
226
227 X->s = Y->s;
228
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100229 if( X->n < i )
230 {
231 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i ) );
232 }
233 else
234 {
235 memset( X->p + i, 0, ( X->n - i ) * ciL );
236 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000237
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 memcpy( X->p, Y->p, i * ciL );
239
240cleanup:
241
242 return( ret );
243}
244
245/*
246 * Swap the contents of X and Y
247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000249{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000251 MPI_VALIDATE( X != NULL );
252 MPI_VALIDATE( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 memcpy( &T, X, sizeof( mbedtls_mpi ) );
255 memcpy( X, Y, sizeof( mbedtls_mpi ) );
256 memcpy( Y, &T, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000257}
258
259/*
260 * Set value from integer
261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +0000263{
Janos Follath24eed8d2019-11-22 13:21:35 +0000264 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker73d7d792018-12-11 10:35:51 +0000265 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 memset( X->p, 0, X->n * ciL );
269
270 X->p[0] = ( z < 0 ) ? -z : z;
271 X->s = ( z < 0 ) ? -1 : 1;
272
273cleanup:
274
275 return( ret );
276}
277
278/*
Paul Bakker2f5947e2011-05-18 15:47:11 +0000279 * Get a specific bit
280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000282{
Hanno Becker73d7d792018-12-11 10:35:51 +0000283 MPI_VALIDATE_RET( X != NULL );
284
Paul Bakker2f5947e2011-05-18 15:47:11 +0000285 if( X->n * biL <= pos )
286 return( 0 );
287
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200288 return( ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000289}
290
Gilles Peskine11cdb052018-11-20 16:47:47 +0100291/* Get a specific byte, without range checks. */
292#define GET_BYTE( X, i ) \
293 ( ( ( X )->p[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff )
294
Paul Bakker2f5947e2011-05-18 15:47:11 +0000295/*
296 * Set a bit to a specific value of 0 or 1
297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000299{
300 int ret = 0;
301 size_t off = pos / biL;
302 size_t idx = pos % biL;
Hanno Becker73d7d792018-12-11 10:35:51 +0000303 MPI_VALIDATE_RET( X != NULL );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000304
305 if( val != 0 && val != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker9af723c2014-05-01 13:03:14 +0200307
Paul Bakker2f5947e2011-05-18 15:47:11 +0000308 if( X->n * biL <= pos )
309 {
310 if( val == 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200311 return( 0 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, off + 1 ) );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000314 }
315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 X->p[off] &= ~( (mbedtls_mpi_uint) 0x01 << idx );
317 X->p[off] |= (mbedtls_mpi_uint) val << idx;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000318
319cleanup:
Paul Bakker9af723c2014-05-01 13:03:14 +0200320
Paul Bakker2f5947e2011-05-18 15:47:11 +0000321 return( ret );
322}
323
324/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200325 * Return the number of less significant zero-bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327size_t mbedtls_mpi_lsb( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000328{
Paul Bakker23986e52011-04-24 08:57:21 +0000329 size_t i, j, count = 0;
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000330 MBEDTLS_INTERNAL_VALIDATE_RET( X != NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000331
332 for( i = 0; i < X->n; i++ )
Paul Bakkerf9688572011-05-05 10:00:45 +0000333 for( j = 0; j < biL; j++, count++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 if( ( ( X->p[i] >> j ) & 1 ) != 0 )
335 return( count );
336
337 return( 0 );
338}
339
340/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200341 * Return the number of bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200343size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000344{
Janos Follath4670f882022-07-21 18:25:42 +0100345 return mbedtls_mpi_core_bitlen( X->p, X->n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000346}
347
348/*
349 * Return the total size in bytes
350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351size_t mbedtls_mpi_size( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000352{
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200353 return( ( mbedtls_mpi_bitlen( X ) + 7 ) >> 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000354}
355
356/*
357 * Convert an ASCII character to digit value
358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359static int mpi_get_digit( mbedtls_mpi_uint *d, int radix, char c )
Paul Bakker5121ce52009-01-03 21:22:43 +0000360{
361 *d = 255;
362
363 if( c >= 0x30 && c <= 0x39 ) *d = c - 0x30;
364 if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37;
365 if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57;
366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 if( *d >= (mbedtls_mpi_uint) radix )
368 return( MBEDTLS_ERR_MPI_INVALID_CHARACTER );
Paul Bakker5121ce52009-01-03 21:22:43 +0000369
370 return( 0 );
371}
372
373/*
374 * Import from an ASCII string
375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000377{
Janos Follath24eed8d2019-11-22 13:21:35 +0000378 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000379 size_t i, j, slen, n;
Gilles Peskine80f56732021-04-03 18:26:13 +0200380 int sign = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_mpi_uint d;
382 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000383 MPI_VALIDATE_RET( X != NULL );
384 MPI_VALIDATE_RET( s != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000385
386 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000387 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000390
Gilles Peskine7cba8592021-06-08 18:32:34 +0200391 if( s[0] == 0 )
392 {
393 mbedtls_mpi_free( X );
394 return( 0 );
395 }
396
Gilles Peskine80f56732021-04-03 18:26:13 +0200397 if( s[0] == '-' )
398 {
399 ++s;
400 sign = -1;
401 }
402
Paul Bakkerff60ee62010-03-16 21:09:09 +0000403 slen = strlen( s );
404
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 if( radix == 16 )
406 {
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +0100407 if( slen > MPI_SIZE_T_MAX >> 2 )
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +0200408 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
409
Paul Bakkerff60ee62010-03-16 21:09:09 +0000410 n = BITS_TO_LIMBS( slen << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n ) );
413 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000414
Paul Bakker23986e52011-04-24 08:57:21 +0000415 for( i = slen, j = 0; i > 0; i--, j++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200418 X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000419 }
420 }
421 else
422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000424
Paul Bakkerff60ee62010-03-16 21:09:09 +0000425 for( i = 0; i < slen; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
428 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T, X, radix ) );
Gilles Peskine80f56732021-04-03 18:26:13 +0200429 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, &T, d ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000430 }
431 }
432
Gilles Peskine80f56732021-04-03 18:26:13 +0200433 if( sign < 0 && mbedtls_mpi_bitlen( X ) != 0 )
434 X->s = -1;
435
Paul Bakker5121ce52009-01-03 21:22:43 +0000436cleanup:
437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000439
440 return( ret );
441}
442
443/*
Ron Eldora16fa292018-11-20 14:07:01 +0200444 * Helper to write the digits high-order first.
Paul Bakker5121ce52009-01-03 21:22:43 +0000445 */
Ron Eldora16fa292018-11-20 14:07:01 +0200446static int mpi_write_hlp( mbedtls_mpi *X, int radix,
447 char **p, const size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000448{
Janos Follath24eed8d2019-11-22 13:21:35 +0000449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_mpi_uint r;
Ron Eldora16fa292018-11-20 14:07:01 +0200451 size_t length = 0;
452 char *p_end = *p + buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000453
Ron Eldora16fa292018-11-20 14:07:01 +0200454 do
455 {
456 if( length >= buflen )
457 {
458 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
459 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
Ron Eldora16fa292018-11-20 14:07:01 +0200461 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) );
462 MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) );
463 /*
464 * Write the residue in the current position, as an ASCII character.
465 */
466 if( r < 0xA )
467 *(--p_end) = (char)( '0' + r );
468 else
469 *(--p_end) = (char)( 'A' + ( r - 0xA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
Ron Eldora16fa292018-11-20 14:07:01 +0200471 length++;
472 } while( mbedtls_mpi_cmp_int( X, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
Ron Eldora16fa292018-11-20 14:07:01 +0200474 memmove( *p, p_end, length );
475 *p += length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
477cleanup:
478
479 return( ret );
480}
481
482/*
483 * Export into an ASCII string
484 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100485int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
486 char *buf, size_t buflen, size_t *olen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000487{
Paul Bakker23986e52011-04-24 08:57:21 +0000488 int ret = 0;
489 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000490 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000492 MPI_VALIDATE_RET( X != NULL );
493 MPI_VALIDATE_RET( olen != NULL );
494 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000497 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000498
Hanno Becker23cfea02019-02-04 09:45:07 +0000499 n = mbedtls_mpi_bitlen( X ); /* Number of bits necessary to present `n`. */
500 if( radix >= 4 ) n >>= 1; /* Number of 4-adic digits necessary to present
501 * `n`. If radix > 4, this might be a strict
502 * overapproximation of the number of
503 * radix-adic digits needed to present `n`. */
504 if( radix >= 16 ) n >>= 1; /* Number of hexadecimal digits necessary to
505 * present `n`. */
506
Janos Follath80470622019-03-06 13:43:02 +0000507 n += 1; /* Terminating null byte */
Hanno Becker23cfea02019-02-04 09:45:07 +0000508 n += 1; /* Compensate for the divisions above, which round down `n`
509 * in case it's not even. */
510 n += 1; /* Potential '-'-sign. */
511 n += ( n & 1 ); /* Make n even to have enough space for hexadecimal writing,
512 * which always uses an even number of hex-digits. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000513
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100514 if( buflen < n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100516 *olen = n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 }
519
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100520 p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000522
523 if( X->s == -1 )
Hanno Beckerc983c812019-02-01 16:41:30 +0000524 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000525 *p++ = '-';
Hanno Beckerc983c812019-02-01 16:41:30 +0000526 buflen--;
527 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529 if( radix == 16 )
530 {
Paul Bakker23986e52011-04-24 08:57:21 +0000531 int c;
532 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000533
Paul Bakker23986e52011-04-24 08:57:21 +0000534 for( i = X->n, k = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 {
Paul Bakker23986e52011-04-24 08:57:21 +0000536 for( j = ciL; j > 0; j-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 {
Paul Bakker23986e52011-04-24 08:57:21 +0000538 c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
Paul Bakker6c343d72014-07-10 14:36:19 +0200540 if( c == 0 && k == 0 && ( i + j ) != 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000541 continue;
542
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000543 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000544 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 k = 1;
546 }
547 }
548 }
549 else
550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T, X ) );
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000552
553 if( T.s == -1 )
554 T.s = 1;
555
Ron Eldora16fa292018-11-20 14:07:01 +0200556 MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p, buflen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 }
558
559 *p++ = '\0';
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100560 *olen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000561
562cleanup:
563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
566 return( ret );
567}
568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000570/*
571 * Read X from an opened file
572 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin )
Paul Bakker5121ce52009-01-03 21:22:43 +0000574{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 mbedtls_mpi_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000576 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000578 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000579 * Buffer should have space for (short) label and decimal formatted MPI,
580 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000581 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Paul Bakker5121ce52009-01-03 21:22:43 +0000583
Hanno Becker73d7d792018-12-11 10:35:51 +0000584 MPI_VALIDATE_RET( X != NULL );
585 MPI_VALIDATE_RET( fin != NULL );
586
587 if( radix < 2 || radix > 16 )
588 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
589
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 memset( s, 0, sizeof( s ) );
591 if( fgets( s, sizeof( s ) - 1, fin ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000593
594 slen = strlen( s );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000595 if( slen == sizeof( s ) - 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000597
Hanno Beckerb2034b72017-04-26 11:46:46 +0100598 if( slen > 0 && s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
599 if( slen > 0 && s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
601 p = s + slen;
Hanno Beckerb2034b72017-04-26 11:46:46 +0100602 while( p-- > s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 if( mpi_get_digit( &d, radix, *p ) != 0 )
604 break;
605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 return( mbedtls_mpi_read_string( X, radix, p + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000607}
608
609/*
610 * Write X into an opened file (or stdout if fout == NULL)
611 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout )
Paul Bakker5121ce52009-01-03 21:22:43 +0000613{
Janos Follath24eed8d2019-11-22 13:21:35 +0000614 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000615 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000616 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000617 * Buffer should have space for (short) label and decimal formatted MPI,
618 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Hanno Becker73d7d792018-12-11 10:35:51 +0000621 MPI_VALIDATE_RET( X != NULL );
622
623 if( radix < 2 || radix > 16 )
624 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100626 memset( s, 0, sizeof( s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100628 MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, sizeof( s ) - 2, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
630 if( p == NULL ) p = "";
631
632 plen = strlen( p );
633 slen = strlen( s );
634 s[slen++] = '\r';
635 s[slen++] = '\n';
636
637 if( fout != NULL )
638 {
639 if( fwrite( p, 1, plen, fout ) != plen ||
640 fwrite( s, 1, slen, fout ) != slen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 }
643 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_printf( "%s%s", p, s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000645
646cleanup:
647
648 return( ret );
649}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000651
652/*
Janos Follatha778a942019-02-13 10:28:28 +0000653 * Import X from unsigned binary data, little endian
Przemyslaw Stekiel76960a72022-02-21 13:42:09 +0100654 *
655 * This function is guaranteed to return an MPI with exactly the necessary
656 * number of limbs (in particular, it does not skip 0s in the input).
Janos Follatha778a942019-02-13 10:28:28 +0000657 */
658int mbedtls_mpi_read_binary_le( mbedtls_mpi *X,
659 const unsigned char *buf, size_t buflen )
660{
Janos Follath24eed8d2019-11-22 13:21:35 +0000661 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follatha778a942019-02-13 10:28:28 +0000662 size_t i;
663 size_t const limbs = CHARS_TO_LIMBS( buflen );
664
665 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskineed32b572021-06-02 22:17:52 +0200666 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Janos Follatha778a942019-02-13 10:28:28 +0000667
668 for( i = 0; i < buflen; i++ )
669 X->p[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
670
671cleanup:
672
Janos Follath171a7ef2019-02-15 16:17:45 +0000673 /*
674 * This function is also used to import keys. However, wiping the buffers
675 * upon failure is not necessary because failure only can happen before any
676 * input is copied.
677 */
Janos Follatha778a942019-02-13 10:28:28 +0000678 return( ret );
679}
680
681/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000682 * Import X from unsigned binary data, big endian
Przemyslaw Stekiel76960a72022-02-21 13:42:09 +0100683 *
684 * This function is guaranteed to return an MPI with exactly the necessary
685 * number of limbs (in particular, it does not skip 0s in the input).
Paul Bakker5121ce52009-01-03 21:22:43 +0000686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000688{
Janos Follath24eed8d2019-11-22 13:21:35 +0000689 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100690 size_t const limbs = CHARS_TO_LIMBS( buflen );
691 size_t const overhead = ( limbs * ciL ) - buflen;
692 unsigned char *Xp;
Paul Bakker5121ce52009-01-03 21:22:43 +0000693
Hanno Becker8ce11a32018-12-19 16:18:52 +0000694 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +0000695 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
696
Hanno Becker073c1992017-10-17 15:17:27 +0100697 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskineed32b572021-06-02 22:17:52 +0200698 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000699
Gilles Peskineed32b572021-06-02 22:17:52 +0200700 /* Avoid calling `memcpy` with NULL source or destination argument,
Hanno Becker0e810b92019-01-03 17:13:11 +0000701 * even if buflen is 0. */
Gilles Peskineed32b572021-06-02 22:17:52 +0200702 if( buflen != 0 )
Hanno Becker0e810b92019-01-03 17:13:11 +0000703 {
704 Xp = (unsigned char*) X->p;
705 memcpy( Xp + overhead, buf, buflen );
Hanno Beckerda1655a2017-10-18 14:21:44 +0100706
Janos Follath4670f882022-07-21 18:25:42 +0100707 mbedtls_mpi_core_bigendian_to_host( X->p, limbs );
Hanno Becker0e810b92019-01-03 17:13:11 +0000708 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000709
710cleanup:
711
Janos Follath171a7ef2019-02-15 16:17:45 +0000712 /*
713 * This function is also used to import keys. However, wiping the buffers
714 * upon failure is not necessary because failure only can happen before any
715 * input is copied.
716 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000717 return( ret );
718}
719
720/*
Janos Follathe344d0f2019-02-19 16:17:40 +0000721 * Export X into unsigned binary data, little endian
722 */
723int mbedtls_mpi_write_binary_le( const mbedtls_mpi *X,
724 unsigned char *buf, size_t buflen )
725{
726 size_t stored_bytes = X->n * ciL;
727 size_t bytes_to_copy;
728 size_t i;
729
730 if( stored_bytes < buflen )
731 {
732 bytes_to_copy = stored_bytes;
733 }
734 else
735 {
736 bytes_to_copy = buflen;
737
738 /* The output buffer is smaller than the allocated size of X.
739 * However X may fit if its leading bytes are zero. */
740 for( i = bytes_to_copy; i < stored_bytes; i++ )
741 {
742 if( GET_BYTE( X, i ) != 0 )
743 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
744 }
745 }
746
747 for( i = 0; i < bytes_to_copy; i++ )
748 buf[i] = GET_BYTE( X, i );
749
750 if( stored_bytes < buflen )
751 {
752 /* Write trailing 0 bytes */
753 memset( buf + stored_bytes, 0, buflen - stored_bytes );
754 }
755
756 return( 0 );
757}
758
759/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000760 * Export X into unsigned binary data, big endian
761 */
Gilles Peskine11cdb052018-11-20 16:47:47 +0100762int mbedtls_mpi_write_binary( const mbedtls_mpi *X,
763 unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000764{
Hanno Becker73d7d792018-12-11 10:35:51 +0000765 size_t stored_bytes;
Gilles Peskine11cdb052018-11-20 16:47:47 +0100766 size_t bytes_to_copy;
767 unsigned char *p;
768 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
Hanno Becker73d7d792018-12-11 10:35:51 +0000770 MPI_VALIDATE_RET( X != NULL );
771 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
772
773 stored_bytes = X->n * ciL;
774
Gilles Peskine11cdb052018-11-20 16:47:47 +0100775 if( stored_bytes < buflen )
776 {
777 /* There is enough space in the output buffer. Write initial
778 * null bytes and record the position at which to start
779 * writing the significant bytes. In this case, the execution
780 * trace of this function does not depend on the value of the
781 * number. */
782 bytes_to_copy = stored_bytes;
783 p = buf + buflen - stored_bytes;
784 memset( buf, 0, buflen - stored_bytes );
785 }
786 else
787 {
788 /* The output buffer is smaller than the allocated size of X.
789 * However X may fit if its leading bytes are zero. */
790 bytes_to_copy = buflen;
791 p = buf;
792 for( i = bytes_to_copy; i < stored_bytes; i++ )
793 {
794 if( GET_BYTE( X, i ) != 0 )
795 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
796 }
797 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000798
Gilles Peskine11cdb052018-11-20 16:47:47 +0100799 for( i = 0; i < bytes_to_copy; i++ )
800 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000801
802 return( 0 );
803}
804
805/*
806 * Left-shift: X <<= count
807 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +0000809{
Janos Follath24eed8d2019-11-22 13:21:35 +0000810 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000811 size_t i, v0, t1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +0000813 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000814
815 v0 = count / (biL );
816 t1 = count & (biL - 1);
817
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200818 i = mbedtls_mpi_bitlen( X ) + count;
Paul Bakker5121ce52009-01-03 21:22:43 +0000819
Paul Bakkerf9688572011-05-05 10:00:45 +0000820 if( X->n * biL < i )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000822
823 ret = 0;
824
825 /*
826 * shift by count / limb_size
827 */
828 if( v0 > 0 )
829 {
Paul Bakker23986e52011-04-24 08:57:21 +0000830 for( i = X->n; i > v0; i-- )
831 X->p[i - 1] = X->p[i - v0 - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +0000832
Paul Bakker23986e52011-04-24 08:57:21 +0000833 for( ; i > 0; i-- )
834 X->p[i - 1] = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000835 }
836
837 /*
838 * shift by count % limb_size
839 */
840 if( t1 > 0 )
841 {
842 for( i = v0; i < X->n; i++ )
843 {
844 r1 = X->p[i] >> (biL - t1);
845 X->p[i] <<= t1;
846 X->p[i] |= r0;
847 r0 = r1;
848 }
849 }
850
851cleanup:
852
853 return( ret );
854}
855
856/*
857 * Right-shift: X >>= count
858 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +0000860{
Paul Bakker23986e52011-04-24 08:57:21 +0000861 size_t i, v0, v1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +0000863 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000864
865 v0 = count / biL;
866 v1 = count & (biL - 1);
867
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +0100868 if( v0 > X->n || ( v0 == X->n && v1 > 0 ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 return mbedtls_mpi_lset( X, 0 );
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +0100870
Paul Bakker5121ce52009-01-03 21:22:43 +0000871 /*
872 * shift by count / limb_size
873 */
874 if( v0 > 0 )
875 {
876 for( i = 0; i < X->n - v0; i++ )
877 X->p[i] = X->p[i + v0];
878
879 for( ; i < X->n; i++ )
880 X->p[i] = 0;
881 }
882
883 /*
884 * shift by count % limb_size
885 */
886 if( v1 > 0 )
887 {
Paul Bakker23986e52011-04-24 08:57:21 +0000888 for( i = X->n; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000889 {
Paul Bakker23986e52011-04-24 08:57:21 +0000890 r1 = X->p[i - 1] << (biL - v1);
891 X->p[i - 1] >>= v1;
892 X->p[i - 1] |= r0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000893 r0 = r1;
894 }
895 }
896
897 return( 0 );
898}
899
900/*
901 * Compare unsigned values
902 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000904{
Paul Bakker23986e52011-04-24 08:57:21 +0000905 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +0000906 MPI_VALIDATE_RET( X != NULL );
907 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
Paul Bakker23986e52011-04-24 08:57:21 +0000909 for( i = X->n; i > 0; i-- )
910 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000911 break;
912
Paul Bakker23986e52011-04-24 08:57:21 +0000913 for( j = Y->n; j > 0; j-- )
914 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000915 break;
916
Paul Bakker23986e52011-04-24 08:57:21 +0000917 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000918 return( 0 );
919
920 if( i > j ) return( 1 );
921 if( j > i ) return( -1 );
922
Paul Bakker23986e52011-04-24 08:57:21 +0000923 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000924 {
Paul Bakker23986e52011-04-24 08:57:21 +0000925 if( X->p[i - 1] > Y->p[i - 1] ) return( 1 );
926 if( X->p[i - 1] < Y->p[i - 1] ) return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000927 }
928
929 return( 0 );
930}
931
932/*
933 * Compare signed values
934 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000936{
Paul Bakker23986e52011-04-24 08:57:21 +0000937 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +0000938 MPI_VALIDATE_RET( X != NULL );
939 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000940
Paul Bakker23986e52011-04-24 08:57:21 +0000941 for( i = X->n; i > 0; i-- )
942 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000943 break;
944
Paul Bakker23986e52011-04-24 08:57:21 +0000945 for( j = Y->n; j > 0; j-- )
946 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000947 break;
948
Paul Bakker23986e52011-04-24 08:57:21 +0000949 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000950 return( 0 );
951
952 if( i > j ) return( X->s );
Paul Bakker0c8f73b2012-03-22 14:08:57 +0000953 if( j > i ) return( -Y->s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000954
955 if( X->s > 0 && Y->s < 0 ) return( 1 );
956 if( Y->s > 0 && X->s < 0 ) return( -1 );
957
Paul Bakker23986e52011-04-24 08:57:21 +0000958 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000959 {
Paul Bakker23986e52011-04-24 08:57:21 +0000960 if( X->p[i - 1] > Y->p[i - 1] ) return( X->s );
961 if( X->p[i - 1] < Y->p[i - 1] ) return( -X->s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000962 }
963
964 return( 0 );
965}
966
Janos Follathee6abce2019-09-05 14:47:19 +0100967/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000968 * Compare signed values
969 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +0000971{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 mbedtls_mpi Y;
973 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +0000974 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000975
976 *p = ( z < 0 ) ? -z : z;
977 Y.s = ( z < 0 ) ? -1 : 1;
978 Y.n = 1;
979 Y.p = p;
980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 return( mbedtls_mpi_cmp_mpi( X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000982}
983
984/*
985 * Unsigned addition: X = |A| + |B| (HAC 14.7)
986 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +0000988{
Janos Follath24eed8d2019-11-22 13:21:35 +0000989 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000990 size_t i, j;
Janos Follath6c922682015-10-30 17:43:11 +0100991 mbedtls_mpi_uint *o, *p, c, tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +0000992 MPI_VALIDATE_RET( X != NULL );
993 MPI_VALIDATE_RET( A != NULL );
994 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000995
996 if( X == B )
997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 const mbedtls_mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 }
1000
1001 if( X != A )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
Paul Bakker9af723c2014-05-01 13:03:14 +02001003
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001004 /*
1005 * X should always be positive as a result of unsigned additions.
1006 */
1007 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001008
Paul Bakker23986e52011-04-24 08:57:21 +00001009 for( j = B->n; j > 0; j-- )
1010 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001011 break;
1012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001014
1015 o = B->p; p = X->p; c = 0;
1016
Janos Follath6c922682015-10-30 17:43:11 +01001017 /*
1018 * tmp is used because it might happen that p == o
1019 */
Paul Bakker23986e52011-04-24 08:57:21 +00001020 for( i = 0; i < j; i++, o++, p++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001021 {
Janos Follath6c922682015-10-30 17:43:11 +01001022 tmp= *o;
Paul Bakker5121ce52009-01-03 21:22:43 +00001023 *p += c; c = ( *p < c );
Janos Follath6c922682015-10-30 17:43:11 +01001024 *p += tmp; c += ( *p < tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +00001025 }
1026
1027 while( c != 0 )
1028 {
1029 if( i >= X->n )
1030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001032 p = X->p + i;
1033 }
1034
Paul Bakker2d319fd2012-09-16 21:34:26 +00001035 *p += c; c = ( *p < c ); i++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001036 }
1037
1038cleanup:
1039
1040 return( ret );
1041}
1042
Gilles Peskine09ec10a2020-06-09 10:39:38 +02001043/**
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001044 * Helper for mbedtls_mpi subtraction.
1045 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001046 * Calculate l - r where l and r have the same size.
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001047 * This function operates modulo (2^ciL)^n and returns the carry
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001048 * (1 if there was a wraparound, i.e. if `l < r`, and 0 otherwise).
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001049 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001050 * d may be aliased to l or r.
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001051 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001052 * \param n Number of limbs of \p d, \p l and \p r.
1053 * \param[out] d The result of the subtraction.
1054 * \param[in] l The left operand.
1055 * \param[in] r The right operand.
1056 *
1057 * \return 1 if `l < r`.
1058 * 0 if `l >= r`.
Paul Bakker5121ce52009-01-03 21:22:43 +00001059 */
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001060static mbedtls_mpi_uint mpi_sub_hlp( size_t n,
1061 mbedtls_mpi_uint *d,
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001062 const mbedtls_mpi_uint *l,
1063 const mbedtls_mpi_uint *r )
Paul Bakker5121ce52009-01-03 21:22:43 +00001064{
Paul Bakker23986e52011-04-24 08:57:21 +00001065 size_t i;
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001066 mbedtls_mpi_uint c = 0, t, z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001067
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001068 for( i = 0; i < n; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001069 {
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001070 z = ( l[i] < c ); t = l[i] - c;
1071 c = ( t < r[i] ) + z; d[i] = t - r[i];
Paul Bakker5121ce52009-01-03 21:22:43 +00001072 }
1073
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001074 return( c );
Paul Bakker5121ce52009-01-03 21:22:43 +00001075}
1076
1077/*
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001078 * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
Paul Bakker5121ce52009-01-03 21:22:43 +00001079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001081{
Janos Follath24eed8d2019-11-22 13:21:35 +00001082 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001083 size_t n;
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001084 mbedtls_mpi_uint carry;
Hanno Becker73d7d792018-12-11 10:35:51 +00001085 MPI_VALIDATE_RET( X != NULL );
1086 MPI_VALIDATE_RET( A != NULL );
1087 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001088
Paul Bakker23986e52011-04-24 08:57:21 +00001089 for( n = B->n; n > 0; n-- )
1090 if( B->p[n - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 break;
Gilles Peskinec8a91772021-01-27 22:30:43 +01001092 if( n > A->n )
1093 {
1094 /* B >= (2^ciL)^n > A */
1095 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1096 goto cleanup;
1097 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001098
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001099 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, A->n ) );
1100
1101 /* Set the high limbs of X to match A. Don't touch the lower limbs
1102 * because X might be aliased to B, and we must not overwrite the
1103 * significant digits of B. */
1104 if( A->n > n )
1105 memcpy( X->p + n, A->p + n, ( A->n - n ) * ciL );
1106 if( X->n > A->n )
1107 memset( X->p + A->n, 0, ( X->n - A->n ) * ciL );
1108
1109 carry = mpi_sub_hlp( n, X->p, A->p, B->p );
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001110 if( carry != 0 )
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001111 {
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001112 /* Propagate the carry to the first nonzero limb of X. */
1113 for( ; n < X->n && X->p[n] == 0; n++ )
1114 --X->p[n];
1115 /* If we ran out of space for the carry, it means that the result
1116 * is negative. */
1117 if( n == X->n )
Gilles Peskine89b41302020-07-23 01:16:46 +02001118 {
1119 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1120 goto cleanup;
1121 }
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001122 --X->p[n];
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001123 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001124
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001125 /* X should always be positive as a result of unsigned subtractions. */
1126 X->s = 1;
1127
Paul Bakker5121ce52009-01-03 21:22:43 +00001128cleanup:
Paul Bakker5121ce52009-01-03 21:22:43 +00001129 return( ret );
1130}
1131
1132/*
1133 * Signed addition: X = A + B
1134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001136{
Hanno Becker73d7d792018-12-11 10:35:51 +00001137 int ret, s;
1138 MPI_VALIDATE_RET( X != NULL );
1139 MPI_VALIDATE_RET( A != NULL );
1140 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001141
Hanno Becker73d7d792018-12-11 10:35:51 +00001142 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001143 if( A->s * B->s < 0 )
1144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001148 X->s = s;
1149 }
1150 else
1151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001153 X->s = -s;
1154 }
1155 }
1156 else
1157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001159 X->s = s;
1160 }
1161
1162cleanup:
1163
1164 return( ret );
1165}
1166
1167/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001168 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001171{
Hanno Becker73d7d792018-12-11 10:35:51 +00001172 int ret, s;
1173 MPI_VALIDATE_RET( X != NULL );
1174 MPI_VALIDATE_RET( A != NULL );
1175 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001176
Hanno Becker73d7d792018-12-11 10:35:51 +00001177 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001178 if( A->s * B->s > 0 )
1179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001183 X->s = s;
1184 }
1185 else
1186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 X->s = -s;
1189 }
1190 }
1191 else
1192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001194 X->s = s;
1195 }
1196
1197cleanup:
1198
1199 return( ret );
1200}
1201
1202/*
1203 * Signed addition: X = A + b
1204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001206{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001207 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001209 MPI_VALIDATE_RET( X != NULL );
1210 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001211
1212 p[0] = ( b < 0 ) ? -b : b;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001213 B.s = ( b < 0 ) ? -1 : 1;
1214 B.n = 1;
1215 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001216
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001217 return( mbedtls_mpi_add_mpi( X, A, &B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001218}
1219
1220/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001221 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001224{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001225 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001227 MPI_VALIDATE_RET( X != NULL );
1228 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001229
1230 p[0] = ( b < 0 ) ? -b : b;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001231 B.s = ( b < 0 ) ? -1 : 1;
1232 B.n = 1;
1233 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001234
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001235 return( mbedtls_mpi_sub_mpi( X, A, &B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001236}
1237
Hanno Becker284d7782022-04-11 09:19:24 +01001238mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len,
Hanno Beckeraef9cc42022-04-11 06:36:29 +01001239 const mbedtls_mpi_uint *s, size_t s_len,
1240 mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001241{
Hanno Beckere7f14a32022-04-06 06:11:26 +01001242 mbedtls_mpi_uint c = 0; /* carry */
Hanno Becker5d4ceeb2022-04-11 09:46:47 +01001243 size_t excess_len = d_len - s_len;
Hanno Beckerdefe5692022-04-06 06:12:09 +01001244
Hanno Becker63eb28c2022-04-06 11:30:51 +01001245 size_t steps_x8 = s_len / 8;
1246 size_t steps_x1 = s_len & 7;
1247
1248 while( steps_x8-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001249 {
Hanno Beckereacf3b92022-04-06 11:25:22 +01001250 MULADDC_X8_INIT
1251 MULADDC_X8_CORE
1252 MULADDC_X8_STOP
Paul Bakker5121ce52009-01-03 21:22:43 +00001253 }
1254
Hanno Becker63eb28c2022-04-06 11:30:51 +01001255 while( steps_x1-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 {
Hanno Beckereacf3b92022-04-06 11:25:22 +01001257 MULADDC_X1_INIT
1258 MULADDC_X1_CORE
1259 MULADDC_X1_STOP
Paul Bakker5121ce52009-01-03 21:22:43 +00001260 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001261
Hanno Becker284d7782022-04-11 09:19:24 +01001262 while( excess_len-- )
Gilles Peskine8e464c42020-07-24 00:08:38 +02001263 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001264 *d += c; c = ( *d < c ); d++;
1265 }
Hanno Beckerdefe5692022-04-06 06:12:09 +01001266
1267 return( c );
Paul Bakker5121ce52009-01-03 21:22:43 +00001268}
1269
1270/*
1271 * Baseline multiplication: X = A * B (HAC 14.12)
1272 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001274{
Janos Follath24eed8d2019-11-22 13:21:35 +00001275 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker1772e052022-04-13 06:51:40 +01001276 size_t i, j;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 mbedtls_mpi TA, TB;
Hanno Beckerda763de2022-04-13 06:50:02 +01001278 int result_is_zero = 0;
Hanno Becker73d7d792018-12-11 10:35:51 +00001279 MPI_VALIDATE_RET( X != NULL );
1280 MPI_VALIDATE_RET( A != NULL );
1281 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 if( X == A ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); A = &TA; }
1286 if( X == B ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); B = &TB; }
Paul Bakker5121ce52009-01-03 21:22:43 +00001287
Hanno Beckerda763de2022-04-13 06:50:02 +01001288 for( i = A->n; i > 0; i-- )
1289 if( A->p[i - 1] != 0 )
1290 break;
1291 if( i == 0 )
1292 result_is_zero = 1;
1293
1294 for( j = B->n; j > 0; j-- )
1295 if( B->p[j - 1] != 0 )
1296 break;
1297 if( j == 0 )
1298 result_is_zero = 1;
1299
1300 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + j ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001302
Hanno Becker1772e052022-04-13 06:51:40 +01001303 for( size_t k = 0; k < j; k++ )
Hanno Beckerfee261a2022-04-06 06:20:22 +01001304 {
1305 /* We know that there cannot be any carry-out since we're
1306 * iterating from bottom to top. */
Hanno Beckerda763de2022-04-13 06:50:02 +01001307 (void) mbedtls_mpi_core_mla( X->p + k, i + 1,
1308 A->p, i,
Hanno Beckeraef9cc42022-04-11 06:36:29 +01001309 B->p[k] );
Hanno Beckerfee261a2022-04-06 06:20:22 +01001310 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001311
Hanno Beckerda763de2022-04-13 06:50:02 +01001312 /* If the result is 0, we don't shortcut the operation, which reduces
1313 * but does not eliminate side channels leaking the zero-ness. We do
1314 * need to take care to set the sign bit properly since the library does
1315 * not fully support an MPI object with a value of 0 and s == -1. */
1316 if( result_is_zero )
1317 X->s = 1;
1318 else
1319 X->s = A->s * B->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001320
1321cleanup:
1322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001324
1325 return( ret );
1326}
1327
1328/*
1329 * Baseline multiplication: X = A * b
1330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001332{
Hanno Becker73d7d792018-12-11 10:35:51 +00001333 MPI_VALIDATE_RET( X != NULL );
1334 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001335
Hanno Becker35771312022-04-14 11:52:11 +01001336 size_t n = A->n;
1337 while( n > 0 && A->p[n - 1] == 0 )
1338 --n;
1339
Hanno Becker74a11a32022-04-06 06:27:00 +01001340 /* The general method below doesn't work if b==0. */
Hanno Becker35771312022-04-14 11:52:11 +01001341 if( b == 0 || n == 0 )
Paul Elliott986b55a2021-04-20 21:46:29 +01001342 return( mbedtls_mpi_lset( X, 0 ) );
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001343
Hanno Beckeraef9cc42022-04-11 06:36:29 +01001344 /* Calculate A*b as A + A*(b-1) to take advantage of mbedtls_mpi_core_mla */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001345 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001346 /* In general, A * b requires 1 limb more than b. If
1347 * A->p[n - 1] * b / b == A->p[n - 1], then A * b fits in the same
1348 * number of limbs as A and the call to grow() is not required since
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001349 * copy() will take care of the growth if needed. However, experimentally,
1350 * making the call to grow() unconditional causes slightly fewer
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001351 * calls to calloc() in ECP code, presumably because it reuses the
1352 * same mpi for a while and this way the mpi is more likely to directly
Hanno Becker9137b9c2022-04-12 10:51:54 +01001353 * grow to its final size.
1354 *
1355 * Note that calculating A*b as 0 + A*b doesn't work as-is because
1356 * A,X can be the same. */
Hanno Becker35771312022-04-14 11:52:11 +01001357 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n + 1 ) );
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001358 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
Hanno Becker35771312022-04-14 11:52:11 +01001359 mbedtls_mpi_core_mla( X->p, X->n, A->p, n, b - 1 );
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001360
1361cleanup:
1362 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001363}
1364
1365/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001366 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1367 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001368 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001369static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1,
1370 mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r )
Simon Butcher15b15d12015-11-26 19:35:03 +00001371{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001372#if defined(MBEDTLS_HAVE_UDBL)
1373 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001374#else
Simon Butcher9803d072016-01-03 00:24:34 +00001375 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
1376 const mbedtls_mpi_uint uint_halfword_mask = ( (mbedtls_mpi_uint) 1 << biH ) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001377 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1378 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001379 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001380#endif
1381
Simon Butcher15b15d12015-11-26 19:35:03 +00001382 /*
1383 * Check for overflow
1384 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001385 if( 0 == d || u1 >= d )
Simon Butcher15b15d12015-11-26 19:35:03 +00001386 {
Simon Butcherf5ba0452015-12-27 23:01:55 +00001387 if (r != NULL) *r = ~0;
Simon Butcher15b15d12015-11-26 19:35:03 +00001388
Simon Butcherf5ba0452015-12-27 23:01:55 +00001389 return ( ~0 );
Simon Butcher15b15d12015-11-26 19:35:03 +00001390 }
1391
1392#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001393 dividend = (mbedtls_t_udbl) u1 << biL;
1394 dividend |= (mbedtls_t_udbl) u0;
1395 quotient = dividend / d;
1396 if( quotient > ( (mbedtls_t_udbl) 1 << biL ) - 1 )
1397 quotient = ( (mbedtls_t_udbl) 1 << biL ) - 1;
1398
1399 if( r != NULL )
Simon Butcher9803d072016-01-03 00:24:34 +00001400 *r = (mbedtls_mpi_uint)( dividend - (quotient * d ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001401
1402 return (mbedtls_mpi_uint) quotient;
1403#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001404
1405 /*
1406 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1407 * Vol. 2 - Seminumerical Algorithms, Knuth
1408 */
1409
1410 /*
1411 * Normalize the divisor, d, and dividend, u0, u1
1412 */
Janos Follath4670f882022-07-21 18:25:42 +01001413 s = mbedtls_mpi_core_clz( d );
Simon Butcher15b15d12015-11-26 19:35:03 +00001414 d = d << s;
1415
1416 u1 = u1 << s;
Simon Butcher9803d072016-01-03 00:24:34 +00001417 u1 |= ( u0 >> ( biL - s ) ) & ( -(mbedtls_mpi_sint)s >> ( biL - 1 ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001418 u0 = u0 << s;
1419
1420 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001421 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001422
1423 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001424 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001425
1426 /*
1427 * Find the first quotient and remainder
1428 */
1429 q1 = u1 / d1;
1430 r0 = u1 - d1 * q1;
1431
1432 while( q1 >= radix || ( q1 * d0 > radix * r0 + u0_msw ) )
1433 {
1434 q1 -= 1;
1435 r0 += d1;
1436
1437 if ( r0 >= radix ) break;
1438 }
1439
Simon Butcherf5ba0452015-12-27 23:01:55 +00001440 rAX = ( u1 * radix ) + ( u0_msw - q1 * d );
Simon Butcher15b15d12015-11-26 19:35:03 +00001441 q0 = rAX / d1;
1442 r0 = rAX - q0 * d1;
1443
1444 while( q0 >= radix || ( q0 * d0 > radix * r0 + u0_lsw ) )
1445 {
1446 q0 -= 1;
1447 r0 += d1;
1448
1449 if ( r0 >= radix ) break;
1450 }
1451
1452 if (r != NULL)
Simon Butcherf5ba0452015-12-27 23:01:55 +00001453 *r = ( rAX * radix + u0_lsw - q0 * d ) >> s;
Simon Butcher15b15d12015-11-26 19:35:03 +00001454
1455 quotient = q1 * radix + q0;
1456
1457 return quotient;
1458#endif
1459}
1460
1461/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001463 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001464int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1465 const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001466{
Janos Follath24eed8d2019-11-22 13:21:35 +00001467 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001468 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 mbedtls_mpi X, Y, Z, T1, T2;
Alexander Kd19a1932019-11-01 18:20:42 +03001470 mbedtls_mpi_uint TP2[3];
Hanno Becker73d7d792018-12-11 10:35:51 +00001471 MPI_VALIDATE_RET( A != NULL );
1472 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 if( mbedtls_mpi_cmp_int( B, 0 ) == 0 )
1475 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Alexander K35d6d462019-10-31 14:46:45 +03001478 mbedtls_mpi_init( &T1 );
Alexander Kd19a1932019-11-01 18:20:42 +03001479 /*
1480 * Avoid dynamic memory allocations for constant-size T2.
1481 *
1482 * T2 is used for comparison only and the 3 limbs are assigned explicitly,
1483 * so nobody increase the size of the MPI and we're safe to use an on-stack
1484 * buffer.
1485 */
Alexander K35d6d462019-10-31 14:46:45 +03001486 T2.s = 1;
Alexander Kd19a1932019-11-01 18:20:42 +03001487 T2.n = sizeof( TP2 ) / sizeof( *TP2 );
1488 T2.p = TP2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 if( Q != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_lset( Q, 0 ) );
1493 if( R != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001494 return( 0 );
1495 }
1496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &X, A ) );
1498 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001499 X.s = Y.s = 1;
1500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &Z, A->n + 2 ) );
1502 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Z, 0 ) );
Gilles Peskine2536aa72020-07-24 00:12:59 +02001503 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, A->n + 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001504
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001505 k = mbedtls_mpi_bitlen( &Y ) % biL;
Paul Bakkerf9688572011-05-05 10:00:45 +00001506 if( k < biL - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001507 {
1508 k = biL - 1 - k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &X, k ) );
1510 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, k ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 }
1512 else k = 0;
1513
1514 n = X.n - 1;
1515 t = Y.n - 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 while( mbedtls_mpi_cmp_mpi( &X, &Y ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001519 {
1520 Z.p[n - t]++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001522 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001524
1525 for( i = n; i > t ; i-- )
1526 {
1527 if( X.p[i] >= Y.p[t] )
1528 Z.p[i - t - 1] = ~0;
1529 else
1530 {
Simon Butcher15b15d12015-11-26 19:35:03 +00001531 Z.p[i - t - 1] = mbedtls_int_div_int( X.p[i], X.p[i - 1],
1532 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001533 }
1534
Alexander K35d6d462019-10-31 14:46:45 +03001535 T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2];
1536 T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1];
1537 T2.p[2] = X.p[i];
1538
Paul Bakker5121ce52009-01-03 21:22:43 +00001539 Z.p[i - t - 1]++;
1540 do
1541 {
1542 Z.p[i - t - 1]--;
1543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T1, 0 ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001545 T1.p[0] = ( t < 1 ) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001546 T1.p[1] = Y.p[t];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001548 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 while( mbedtls_mpi_cmp_mpi( &T1, &T2 ) > 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
1552 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1553 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 if( mbedtls_mpi_cmp_int( &X, 0 ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &Y ) );
1558 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1559 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001560 Z.p[i - t - 1]--;
1561 }
1562 }
1563
1564 if( Q != NULL )
1565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( Q, &Z ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001567 Q->s = A->s * B->s;
1568 }
1569
1570 if( R != NULL )
1571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &X, k ) );
Paul Bakkerf02c5642012-11-13 10:25:21 +00001573 X.s = A->s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, &X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 if( mbedtls_mpi_cmp_int( R, 0 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001577 R->s = 1;
1578 }
1579
1580cleanup:
1581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Alexander K35d6d462019-10-31 14:46:45 +03001583 mbedtls_mpi_free( &T1 );
Alexander Kd19a1932019-11-01 18:20:42 +03001584 mbedtls_platform_zeroize( TP2, sizeof( TP2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001585
1586 return( ret );
1587}
1588
1589/*
1590 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00001591 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001592int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R,
1593 const mbedtls_mpi *A,
1594 mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001595{
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001596 mbedtls_mpi B;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001598 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001599
1600 p[0] = ( b < 0 ) ? -b : b;
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001601 B.s = ( b < 0 ) ? -1 : 1;
1602 B.n = 1;
1603 B.p = p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001604
Yuto Takano36c8ddc2021-07-05 09:10:52 +01001605 return( mbedtls_mpi_div_mpi( Q, R, A, &B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001606}
1607
1608/*
1609 * Modulo: R = A mod B
1610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001612{
Janos Follath24eed8d2019-11-22 13:21:35 +00001613 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker73d7d792018-12-11 10:35:51 +00001614 MPI_VALIDATE_RET( R != NULL );
1615 MPI_VALIDATE_RET( A != NULL );
1616 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 if( mbedtls_mpi_cmp_int( B, 0 ) < 0 )
1619 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( NULL, R, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 while( mbedtls_mpi_cmp_int( R, 0 ) < 0 )
1624 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 while( mbedtls_mpi_cmp_mpi( R, B ) >= 0 )
1627 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001628
1629cleanup:
1630
1631 return( ret );
1632}
1633
1634/*
1635 * Modulo: r = A mod b
1636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001638{
Paul Bakker23986e52011-04-24 08:57:21 +00001639 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 mbedtls_mpi_uint x, y, z;
Hanno Becker73d7d792018-12-11 10:35:51 +00001641 MPI_VALIDATE_RET( r != NULL );
1642 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001643
1644 if( b == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001646
1647 if( b < 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001649
1650 /*
1651 * handle trivial cases
1652 */
Gilles Peskineae25bb02022-06-09 19:32:46 +02001653 if( b == 1 || A->n == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001654 {
1655 *r = 0;
1656 return( 0 );
1657 }
1658
1659 if( b == 2 )
1660 {
1661 *r = A->p[0] & 1;
1662 return( 0 );
1663 }
1664
1665 /*
1666 * general case
1667 */
Paul Bakker23986e52011-04-24 08:57:21 +00001668 for( i = A->n, y = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001669 {
Paul Bakker23986e52011-04-24 08:57:21 +00001670 x = A->p[i - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001671 y = ( y << biH ) | ( x >> biH );
1672 z = y / b;
1673 y -= z * b;
1674
1675 x <<= biH;
1676 y = ( y << biH ) | ( x >> biH );
1677 z = y / b;
1678 y -= z * b;
1679 }
1680
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001681 /*
1682 * If A is negative, then the current y represents a negative value.
1683 * Flipping it to the positive side.
1684 */
1685 if( A->s < 0 && y != 0 )
1686 y = b - y;
1687
Paul Bakker5121ce52009-01-03 21:22:43 +00001688 *r = y;
1689
1690 return( 0 );
1691}
1692
1693/*
1694 * Fast Montgomery initialization (thanks to Tom St Denis)
1695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696static void mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00001697{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 mbedtls_mpi_uint x, m0 = N->p[0];
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01001699 unsigned int i;
Paul Bakker5121ce52009-01-03 21:22:43 +00001700
1701 x = m0;
1702 x += ( ( m0 + 2 ) & 4 ) << 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001703
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01001704 for( i = biL; i >= 8; i /= 2 )
1705 x *= ( 2 - ( m0 * x ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001706
1707 *mm = ~x + 1;
1708}
1709
Gilles Peskine2a82f722020-06-04 15:00:49 +02001710/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
1711 *
1712 * \param[in,out] A One of the numbers to multiply.
Gilles Peskine221626f2020-06-08 22:37:50 +02001713 * It must have at least as many limbs as N
1714 * (A->n >= N->n), and any limbs beyond n are ignored.
Gilles Peskine2a82f722020-06-04 15:00:49 +02001715 * On successful completion, A contains the result of
1716 * the multiplication A * B * R^-1 mod N where
1717 * R = (2^ciL)^n.
1718 * \param[in] B One of the numbers to multiply.
1719 * It must be nonzero and must not have more limbs than N
1720 * (B->n <= N->n).
1721 * \param[in] N The modulo. N must be odd.
1722 * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
1723 * This is -N^-1 mod 2^ciL.
1724 * \param[in,out] T A bignum for temporary storage.
Hanno Beckere1417022022-04-06 06:45:45 +01001725 * It must be at least twice the limb size of N plus 1
1726 * (T->n >= 2 * N->n + 1).
Gilles Peskine2a82f722020-06-04 15:00:49 +02001727 * Its initial content is unused and
1728 * its final content is indeterminate.
1729 * Note that unlike the usual convention in the library
1730 * for `const mbedtls_mpi*`, the content of T can change.
Paul Bakker5121ce52009-01-03 21:22:43 +00001731 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02001732static 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 +02001733 const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00001734{
Hanno Becker0235f752022-04-12 10:54:46 +01001735 size_t n, m;
1736 mbedtls_mpi_uint *d;
Paul Bakker5121ce52009-01-03 21:22:43 +00001737
1738 memset( T->p, 0, T->n * ciL );
1739
1740 d = T->p;
1741 n = N->n;
1742 m = ( B->n < n ) ? B->n : n;
1743
Hanno Becker0235f752022-04-12 10:54:46 +01001744 for( size_t i = 0; i < n; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001745 {
Hanno Becker0235f752022-04-12 10:54:46 +01001746 mbedtls_mpi_uint u0, u1;
1747
Paul Bakker5121ce52009-01-03 21:22:43 +00001748 /*
1749 * T = (T + u0*B + u1*N) / 2^biL
1750 */
1751 u0 = A->p[i];
1752 u1 = ( d[0] + u0 * B->p[0] ) * mm;
1753
Hanno Beckeraef9cc42022-04-11 06:36:29 +01001754 (void) mbedtls_mpi_core_mla( d, n + 2,
1755 B->p, m,
1756 u0 );
1757 (void) mbedtls_mpi_core_mla( d, n + 2,
1758 N->p, n,
1759 u1 );
Hanno Beckere1417022022-04-06 06:45:45 +01001760 d++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001761 }
1762
Gilles Peskine221626f2020-06-08 22:37:50 +02001763 /* At this point, d is either the desired result or the desired result
1764 * plus N. We now potentially subtract N, avoiding leaking whether the
1765 * subtraction is performed through side channels. */
Paul Bakker5121ce52009-01-03 21:22:43 +00001766
Gilles Peskine221626f2020-06-08 22:37:50 +02001767 /* Copy the n least significant limbs of d to A, so that
1768 * A = d if d < N (recall that N has n limbs). */
1769 memcpy( A->p, d, n * ciL );
Gilles Peskine09ec10a2020-06-09 10:39:38 +02001770 /* If d >= N then we want to set A to d - N. To prevent timing attacks,
Gilles Peskine221626f2020-06-08 22:37:50 +02001771 * do the calculation without using conditional tests. */
1772 /* Set d to d0 + (2^biL)^n - N where d0 is the current value of d. */
Gilles Peskine132c0972020-06-04 21:05:24 +02001773 d[n] += 1;
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001774 d[n] -= mpi_sub_hlp( n, d, d, N->p );
Gilles Peskine221626f2020-06-08 22:37:50 +02001775 /* If d0 < N then d < (2^biL)^n
1776 * so d[n] == 0 and we want to keep A as it is.
1777 * If d0 >= N then d >= (2^biL)^n, and d <= (2^biL)^n + N < 2 * (2^biL)^n
1778 * so d[n] == 1 and we want to set A to the result of the subtraction
1779 * which is d - (2^biL)^n, i.e. the n least significant limbs of d.
1780 * This exactly corresponds to a conditional assignment. */
Gabor Mezei90437e32021-10-20 11:59:27 +02001781 mbedtls_ct_mpi_uint_cond_assign( n, A->p, d, (unsigned char) d[n] );
Paul Bakker5121ce52009-01-03 21:22:43 +00001782}
1783
1784/*
1785 * Montgomery reduction: A = A * R^-1 mod N
Gilles Peskine2a82f722020-06-04 15:00:49 +02001786 *
1787 * See mpi_montmul() regarding constraints and guarantees on the parameters.
Paul Bakker5121ce52009-01-03 21:22:43 +00001788 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02001789static void mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N,
1790 mbedtls_mpi_uint mm, const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00001791{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 mbedtls_mpi_uint z = 1;
1793 mbedtls_mpi U;
Paul Bakker5121ce52009-01-03 21:22:43 +00001794
Paul Bakker8ddb6452013-02-27 14:56:33 +01001795 U.n = U.s = (int) z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001796 U.p = &z;
1797
Gilles Peskine4e91d472020-06-04 20:55:15 +02001798 mpi_montmul( A, &U, N, mm, T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001799}
1800
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001801/**
1802 * Select an MPI from a table without leaking the index.
1803 *
1804 * This is functionally equivalent to mbedtls_mpi_copy(R, T[idx]) except it
1805 * reads the entire table in order to avoid leaking the value of idx to an
1806 * attacker able to observe memory access patterns.
1807 *
1808 * \param[out] R Where to write the selected MPI.
1809 * \param[in] T The table to read from.
1810 * \param[in] T_size The number of elements in the table.
1811 * \param[in] idx The index of the element to select;
1812 * this must satisfy 0 <= idx < T_size.
1813 *
1814 * \return \c 0 on success, or a negative error code.
1815 */
1816static int mpi_select( mbedtls_mpi *R, const mbedtls_mpi *T, size_t T_size, size_t idx )
1817{
1818 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1819
1820 for( size_t i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard92413ef2021-06-03 10:42:46 +02001821 {
1822 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( R, &T[i],
Gabor Mezei90437e32021-10-20 11:59:27 +02001823 (unsigned char) mbedtls_ct_size_bool_eq( i, idx ) ) );
Manuel Pégourié-Gonnard92413ef2021-06-03 10:42:46 +02001824 }
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001825
1826cleanup:
1827 return( ret );
1828}
1829
Paul Bakker5121ce52009-01-03 21:22:43 +00001830/*
1831 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
1832 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001833int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
1834 const mbedtls_mpi *E, const mbedtls_mpi *N,
Yuto Takano538a0cb2021-07-14 10:20:09 +01001835 mbedtls_mpi *prec_RR )
Paul Bakker5121ce52009-01-03 21:22:43 +00001836{
Janos Follath24eed8d2019-11-22 13:21:35 +00001837 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001838 size_t wbits, wsize, one = 1;
1839 size_t i, j, nblimbs;
1840 size_t bufsize, nbits;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 mbedtls_mpi_uint ei, mm, state;
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001842 mbedtls_mpi RR, T, W[ 1 << MBEDTLS_MPI_WINDOW_SIZE ], WW, Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00001843 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001844
Hanno Becker73d7d792018-12-11 10:35:51 +00001845 MPI_VALIDATE_RET( X != NULL );
1846 MPI_VALIDATE_RET( A != NULL );
1847 MPI_VALIDATE_RET( E != NULL );
1848 MPI_VALIDATE_RET( N != NULL );
1849
Hanno Becker8d1dd1b2017-09-28 11:02:24 +01001850 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 || ( N->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 if( mbedtls_mpi_cmp_int( E, 0 ) < 0 )
1854 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakkerf6198c12012-05-16 08:02:29 +00001855
Chris Jones9246d042020-11-25 15:12:39 +00001856 if( mbedtls_mpi_bitlen( E ) > MBEDTLS_MPI_MAX_BITS ||
1857 mbedtls_mpi_bitlen( N ) > MBEDTLS_MPI_MAX_BITS )
1858 return ( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
1859
Paul Bakkerf6198c12012-05-16 08:02:29 +00001860 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001861 * Init temps and window size
1862 */
1863 mpi_montg_init( &mm, N );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &T );
1865 mbedtls_mpi_init( &Apos );
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01001866 mbedtls_mpi_init( &WW );
Paul Bakker5121ce52009-01-03 21:22:43 +00001867 memset( W, 0, sizeof( W ) );
1868
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001869 i = mbedtls_mpi_bitlen( E );
Paul Bakker5121ce52009-01-03 21:22:43 +00001870
1871 wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
1872 ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1;
1873
Peter Kolbuse6bcad32018-12-11 14:01:44 -06001874#if( MBEDTLS_MPI_WINDOW_SIZE < 6 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 if( wsize > MBEDTLS_MPI_WINDOW_SIZE )
1876 wsize = MBEDTLS_MPI_WINDOW_SIZE;
Peter Kolbuse6bcad32018-12-11 14:01:44 -06001877#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00001878
Paul Bakker5121ce52009-01-03 21:22:43 +00001879 j = N->n + 1;
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001880 /* All W[i] and X must have at least N->n limbs for the mpi_montmul()
1881 * and mpi_montred() calls later. Here we ensure that W[1] and X are
1882 * large enough, and later we'll grow other W[i] to the same length.
1883 * They must not be shrunk midway through this function!
1884 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
1886 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], j ) );
1887 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T, j * 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001888
1889 /*
Paul Bakker50546922012-05-19 08:40:49 +00001890 * Compensate for negative A (and correct at the end)
1891 */
1892 neg = ( A->s == -1 );
Paul Bakker50546922012-05-19 08:40:49 +00001893 if( neg )
1894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Apos, A ) );
Paul Bakker50546922012-05-19 08:40:49 +00001896 Apos.s = 1;
1897 A = &Apos;
1898 }
1899
1900 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001901 * If 1st call, pre-compute R^2 mod N
1902 */
Yuto Takano538a0cb2021-07-14 10:20:09 +01001903 if( prec_RR == NULL || prec_RR->p == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &RR, 1 ) );
1906 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &RR, N->n * 2 * biL ) );
1907 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &RR, &RR, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001908
Yuto Takano538a0cb2021-07-14 10:20:09 +01001909 if( prec_RR != NULL )
1910 memcpy( prec_RR, &RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001911 }
1912 else
Yuto Takano538a0cb2021-07-14 10:20:09 +01001913 memcpy( &RR, prec_RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001914
1915 /*
1916 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
1917 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918 if( mbedtls_mpi_cmp_mpi( A, N ) >= 0 )
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &W[1], A, N ) );
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001921 /* This should be a no-op because W[1] is already that large before
1922 * mbedtls_mpi_mod_mpi(), but it's necessary to avoid an overflow
1923 * in mpi_montmul() below, so let's make sure. */
Gilles Peskine2aa3f162021-06-15 21:22:48 +02001924 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], N->n + 1 ) );
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001925 }
Paul Bakkerc2024f42014-01-23 20:38:35 +01001926 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[1], A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001928
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02001929 /* Note that this is safe because W[1] always has at least N->n limbs
1930 * (it grew above and was preserved by mbedtls_mpi_copy()). */
Gilles Peskine4e91d472020-06-04 20:55:15 +02001931 mpi_montmul( &W[1], &RR, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001932
1933 /*
1934 * X = R^2 * R^-1 mod N = R mod N
1935 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &RR ) );
Gilles Peskine4e91d472020-06-04 20:55:15 +02001937 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001938
1939 if( wsize > 1 )
1940 {
1941 /*
1942 * W[1 << (wsize - 1)] = W[1] ^ (wsize - 1)
1943 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001944 j = one << ( wsize - 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[j], N->n + 1 ) );
1947 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[j], &W[1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001948
1949 for( i = 0; i < wsize - 1; i++ )
Gilles Peskine4e91d472020-06-04 20:55:15 +02001950 mpi_montmul( &W[j], &W[j], N, mm, &T );
Paul Bakker0d7702c2013-10-29 16:18:35 +01001951
Paul Bakker5121ce52009-01-03 21:22:43 +00001952 /*
1953 * W[i] = W[i - 1] * W[1]
1954 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001955 for( i = j + 1; i < ( one << wsize ); i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[i], N->n + 1 ) );
1958 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[i], &W[i - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001959
Gilles Peskine4e91d472020-06-04 20:55:15 +02001960 mpi_montmul( &W[i], &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001961 }
1962 }
1963
1964 nblimbs = E->n;
1965 bufsize = 0;
1966 nbits = 0;
1967 wbits = 0;
1968 state = 0;
1969
1970 while( 1 )
1971 {
1972 if( bufsize == 0 )
1973 {
Paul Bakker0d7702c2013-10-29 16:18:35 +01001974 if( nblimbs == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001975 break;
1976
Paul Bakker0d7702c2013-10-29 16:18:35 +01001977 nblimbs--;
1978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 bufsize = sizeof( mbedtls_mpi_uint ) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00001980 }
1981
1982 bufsize--;
1983
1984 ei = (E->p[nblimbs] >> bufsize) & 1;
1985
1986 /*
1987 * skip leading 0s
1988 */
1989 if( ei == 0 && state == 0 )
1990 continue;
1991
1992 if( ei == 0 && state == 1 )
1993 {
1994 /*
1995 * out of window, square X
1996 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02001997 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00001998 continue;
1999 }
2000
2001 /*
2002 * add ei to current window
2003 */
2004 state = 2;
2005
2006 nbits++;
Paul Bakker66d5d072014-06-17 16:39:18 +02002007 wbits |= ( ei << ( wsize - nbits ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002008
2009 if( nbits == wsize )
2010 {
2011 /*
2012 * X = X^wsize R^-1 mod N
2013 */
2014 for( i = 0; i < wsize; i++ )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002015 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002016
2017 /*
2018 * X = X * W[wbits] R^-1 mod N
2019 */
Manuel Pégourié-Gonnarde22176e2021-06-10 09:34:00 +02002020 MBEDTLS_MPI_CHK( mpi_select( &WW, W, (size_t) 1 << wsize, wbits ) );
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01002021 mpi_montmul( X, &WW, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002022
2023 state--;
2024 nbits = 0;
2025 wbits = 0;
2026 }
2027 }
2028
2029 /*
2030 * process the remaining bits
2031 */
2032 for( i = 0; i < nbits; i++ )
2033 {
Gilles Peskine4e91d472020-06-04 20:55:15 +02002034 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002035
2036 wbits <<= 1;
2037
Paul Bakker66d5d072014-06-17 16:39:18 +02002038 if( ( wbits & ( one << wsize ) ) != 0 )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002039 mpi_montmul( X, &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002040 }
2041
2042 /*
2043 * X = A^E * R * R^-1 mod N = A^E mod N
2044 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002045 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002046
Hanno Beckera4af1c42017-04-18 09:07:45 +01002047 if( neg && E->n != 0 && ( E->p[0] & 1 ) != 0 )
Paul Bakkerf6198c12012-05-16 08:02:29 +00002048 {
2049 X->s = -1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, N, X ) );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002051 }
2052
Paul Bakker5121ce52009-01-03 21:22:43 +00002053cleanup:
2054
Paul Bakker66d5d072014-06-17 16:39:18 +02002055 for( i = ( one << ( wsize - 1 ) ); i < ( one << wsize ); i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 mbedtls_mpi_free( &W[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058 mbedtls_mpi_free( &W[1] ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &Apos );
Manuel Pégourié-Gonnard1297ef32021-03-09 11:22:20 +01002059 mbedtls_mpi_free( &WW );
Paul Bakker6c591fa2011-05-05 11:49:20 +00002060
Yuto Takano538a0cb2021-07-14 10:20:09 +01002061 if( prec_RR == NULL || prec_RR->p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002063
2064 return( ret );
2065}
2066
Paul Bakker5121ce52009-01-03 21:22:43 +00002067/*
2068 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
2069 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00002071{
Janos Follath24eed8d2019-11-22 13:21:35 +00002072 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002073 size_t lz, lzt;
Alexander Ke8ad49f2019-08-16 16:16:07 +03002074 mbedtls_mpi TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00002075
Hanno Becker73d7d792018-12-11 10:35:51 +00002076 MPI_VALIDATE_RET( G != NULL );
2077 MPI_VALIDATE_RET( A != NULL );
2078 MPI_VALIDATE_RET( B != NULL );
2079
Alexander Ke8ad49f2019-08-16 16:16:07 +03002080 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) );
2083 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 lz = mbedtls_mpi_lsb( &TA );
2086 lzt = mbedtls_mpi_lsb( &TB );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002087
Gilles Peskine27253bc2021-06-09 13:26:43 +02002088 /* The loop below gives the correct result when A==0 but not when B==0.
2089 * So have a special case for B==0. Leverage the fact that we just
2090 * calculated the lsb and lsb(B)==0 iff B is odd or 0 to make the test
2091 * slightly more efficient than cmp_int(). */
2092 if( lzt == 0 && mbedtls_mpi_get_bit( &TB, 0 ) == 0 )
2093 {
2094 ret = mbedtls_mpi_copy( G, A );
2095 goto cleanup;
2096 }
2097
Paul Bakker66d5d072014-06-17 16:39:18 +02002098 if( lzt < lz )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002099 lz = lzt;
2100
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 TA.s = TB.s = 1;
2102
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002103 /* We mostly follow the procedure described in HAC 14.54, but with some
2104 * minor differences:
2105 * - Sequences of multiplications or divisions by 2 are grouped into a
2106 * single shift operation.
Gilles Peskineb09c7ee2021-06-21 18:58:39 +02002107 * - The procedure in HAC assumes that 0 < TB <= TA.
2108 * - The condition TB <= TA is not actually necessary for correctness.
2109 * TA and TB have symmetric roles except for the loop termination
2110 * condition, and the shifts at the beginning of the loop body
2111 * remove any significance from the ordering of TA vs TB before
2112 * the shifts.
2113 * - If TA = 0, the loop goes through 0 iterations and the result is
2114 * correctly TB.
2115 * - The case TB = 0 was short-circuited above.
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002116 *
2117 * For the correctness proof below, decompose the original values of
2118 * A and B as
2119 * A = sa * 2^a * A' with A'=0 or A' odd, and sa = +-1
2120 * B = sb * 2^b * B' with B'=0 or B' odd, and sb = +-1
2121 * Then gcd(A, B) = 2^{min(a,b)} * gcd(A',B'),
2122 * and gcd(A',B') is odd or 0.
2123 *
2124 * At the beginning, we have TA = |A| and TB = |B| so gcd(A,B) = gcd(TA,TB).
2125 * The code maintains the following invariant:
2126 * gcd(A,B) = 2^k * gcd(TA,TB) for some k (I)
Gilles Peskine4df3f1f2021-06-15 22:09:39 +02002127 */
2128
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002129 /* Proof that the loop terminates:
2130 * At each iteration, either the right-shift by 1 is made on a nonzero
2131 * value and the nonnegative integer bitlen(TA) + bitlen(TB) decreases
2132 * by at least 1, or the right-shift by 1 is made on zero and then
2133 * TA becomes 0 which ends the loop (TB cannot be 0 if it is right-shifted
2134 * since in that case TB is calculated from TB-TA with the condition TB>TA).
2135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 while( mbedtls_mpi_cmp_int( &TA, 0 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002137 {
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002138 /* Divisions by 2 preserve the invariant (I). */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, mbedtls_mpi_lsb( &TA ) ) );
2140 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, mbedtls_mpi_lsb( &TB ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002141
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002142 /* Set either TA or TB to |TA-TB|/2. Since TA and TB are both odd,
2143 * TA-TB is even so the division by 2 has an integer result.
2144 * Invariant (I) is preserved since any odd divisor of both TA and TB
2145 * also divides |TA-TB|/2, and any odd divisor of both TA and |TA-TB|/2
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002146 * also divides TB, and any odd divisor of both TB and |TA-TB|/2 also
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002147 * divides TA.
2148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 if( mbedtls_mpi_cmp_mpi( &TA, &TB ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TA, &TA, &TB ) );
2152 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002153 }
2154 else
2155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TB, &TB, &TA ) );
2157 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002158 }
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002159 /* Note that one of TA or TB is still odd. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002160 }
2161
Gilles Peskine2a63c5b2021-06-16 13:42:04 +02002162 /* By invariant (I), gcd(A,B) = 2^k * gcd(TA,TB) for some k.
2163 * At the loop exit, TA = 0, so gcd(TA,TB) = TB.
2164 * - If there was at least one loop iteration, then one of TA or TB is odd,
2165 * and TA = 0, so TB is odd and gcd(TA,TB) = gcd(A',B'). In this case,
2166 * lz = min(a,b) so gcd(A,B) = 2^lz * TB.
2167 * - If there was no loop iteration, then A was 0, and gcd(A,B) = B.
Gilles Peskine4d3fd362021-06-21 11:40:38 +02002168 * 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 +02002169 */
2170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &TB, lz ) );
2172 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( G, &TB ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002173
2174cleanup:
2175
Alexander Ke8ad49f2019-08-16 16:16:07 +03002176 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002177
2178 return( ret );
2179}
2180
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002181/* Fill X with n_bytes random bytes.
2182 * X must already have room for those bytes.
Gilles Peskineafb2bd22021-06-03 11:51:09 +02002183 * The ordering of the bytes returned from the RNG is suitable for
2184 * deterministic ECDSA (see RFC 6979 §3.3 and mbedtls_mpi_random()).
Gilles Peskineebe9b6a2021-04-13 21:55:35 +02002185 * The size and sign of X are unchanged.
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002186 * n_bytes must not be 0.
2187 */
2188static int mpi_fill_random_internal(
2189 mbedtls_mpi *X, size_t n_bytes,
2190 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
2191{
2192 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2193 const size_t limbs = CHARS_TO_LIMBS( n_bytes );
2194 const size_t overhead = ( limbs * ciL ) - n_bytes;
2195
2196 if( X->n < limbs )
2197 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002198
Gilles Peskineebe9b6a2021-04-13 21:55:35 +02002199 memset( X->p, 0, overhead );
2200 memset( (unsigned char *) X->p + limbs * ciL, 0, ( X->n - limbs ) * ciL );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002201 MBEDTLS_MPI_CHK( f_rng( p_rng, (unsigned char *) X->p + overhead, n_bytes ) );
Janos Follath4670f882022-07-21 18:25:42 +01002202 mbedtls_mpi_core_bigendian_to_host( X->p, limbs );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002203
2204cleanup:
2205 return( ret );
2206}
2207
Paul Bakker33dc46b2014-04-30 16:11:39 +02002208/*
2209 * Fill X with size bytes of random.
2210 *
2211 * Use a temporary bytes representation to make sure the result is the same
Paul Bakkerc37b0ac2014-05-01 14:19:23 +02002212 * regardless of the platform endianness (useful when f_rng is actually
Paul Bakker33dc46b2014-04-30 16:11:39 +02002213 * deterministic, eg for tests).
2214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002216 int (*f_rng)(void *, unsigned char *, size_t),
2217 void *p_rng )
Paul Bakker287781a2011-03-26 13:18:49 +00002218{
Janos Follath24eed8d2019-11-22 13:21:35 +00002219 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6dab6202019-01-02 16:42:29 +00002220 size_t const limbs = CHARS_TO_LIMBS( size );
Hanno Beckerda1655a2017-10-18 14:21:44 +01002221
Hanno Becker8ce11a32018-12-19 16:18:52 +00002222 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002223 MPI_VALIDATE_RET( f_rng != NULL );
Paul Bakker33dc46b2014-04-30 16:11:39 +02002224
Hanno Beckerda1655a2017-10-18 14:21:44 +01002225 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskineed32b572021-06-02 22:17:52 +02002226 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002227 if( size == 0 )
2228 return( 0 );
Paul Bakker287781a2011-03-26 13:18:49 +00002229
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002230 ret = mpi_fill_random_internal( X, size, f_rng, p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +00002231
2232cleanup:
2233 return( ret );
2234}
2235
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002236int mbedtls_mpi_random( mbedtls_mpi *X,
2237 mbedtls_mpi_sint min,
2238 const mbedtls_mpi *N,
2239 int (*f_rng)(void *, unsigned char *, size_t),
2240 void *p_rng )
2241{
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002242 int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskinee5381682021-04-13 21:23:25 +02002243 int count;
Gilles Peskine5b0589e2021-04-13 21:09:10 +02002244 unsigned lt_lower = 1, lt_upper = 0;
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002245 size_t n_bits = mbedtls_mpi_bitlen( N );
2246 size_t n_bytes = ( n_bits + 7 ) / 8;
Gilles Peskine5b0589e2021-04-13 21:09:10 +02002247 mbedtls_mpi lower_bound;
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002248
Gilles Peskine1e918f42021-03-29 22:14:51 +02002249 if( min < 0 )
2250 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2251 if( mbedtls_mpi_cmp_int( N, min ) <= 0 )
2252 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2253
Gilles Peskinee5381682021-04-13 21:23:25 +02002254 /*
2255 * When min == 0, each try has at worst a probability 1/2 of failing
2256 * (the msb has a probability 1/2 of being 0, and then the result will
2257 * be < N), so after 30 tries failure probability is a most 2**(-30).
2258 *
2259 * When N is just below a power of 2, as is the case when generating
Gilles Peskinee842e582021-04-15 11:45:19 +02002260 * a random scalar on most elliptic curves, 1 try is enough with
Gilles Peskinee5381682021-04-13 21:23:25 +02002261 * overwhelming probability. When N is just above a power of 2,
Gilles Peskinee842e582021-04-15 11:45:19 +02002262 * as when generating a random scalar on secp224k1, each try has
Gilles Peskinee5381682021-04-13 21:23:25 +02002263 * a probability of failing that is almost 1/2.
2264 *
2265 * The probabilities are almost the same if min is nonzero but negligible
2266 * compared to N. This is always the case when N is crypto-sized, but
2267 * it's convenient to support small N for testing purposes. When N
2268 * is small, use a higher repeat count, otherwise the probability of
2269 * failure is macroscopic.
2270 */
Gilles Peskine87823d72021-06-02 21:18:59 +02002271 count = ( n_bytes > 4 ? 30 : 250 );
Gilles Peskinee5381682021-04-13 21:23:25 +02002272
Gilles Peskine5b0589e2021-04-13 21:09:10 +02002273 mbedtls_mpi_init( &lower_bound );
2274
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002275 /* Ensure that target MPI has exactly the same number of limbs
2276 * as the upper bound, even if the upper bound has leading zeros.
2277 * This is necessary for the mbedtls_mpi_lt_mpi_ct() check. */
Gilles Peskineed32b572021-06-02 22:17:52 +02002278 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, N->n ) );
Gilles Peskine5b0589e2021-04-13 21:09:10 +02002279 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &lower_bound, N->n ) );
2280 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &lower_bound, min ) );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002281
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002282 /*
2283 * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA)
2284 * when f_rng is a suitably parametrized instance of HMAC_DRBG:
2285 * - use the same byte ordering;
2286 * - keep the leftmost n_bits bits of the generated octet string;
2287 * - try until result is in the desired range.
2288 * This also avoids any bias, which is especially important for ECDSA.
2289 */
2290 do
2291 {
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002292 MBEDTLS_MPI_CHK( mpi_fill_random_internal( X, n_bytes, f_rng, p_rng ) );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002293 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, 8 * n_bytes - n_bits ) );
2294
Gilles Peskinee5381682021-04-13 21:23:25 +02002295 if( --count == 0 )
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002296 {
2297 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2298 goto cleanup;
2299 }
2300
Gilles Peskine5b0589e2021-04-13 21:09:10 +02002301 MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( X, &lower_bound, &lt_lower ) );
2302 MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( X, N, &lt_upper ) );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002303 }
Gilles Peskine5b0589e2021-04-13 21:09:10 +02002304 while( lt_lower != 0 || lt_upper == 0 );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002305
2306cleanup:
Gilles Peskine5b0589e2021-04-13 21:09:10 +02002307 mbedtls_mpi_free( &lower_bound );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002308 return( ret );
2309}
2310
Paul Bakker5121ce52009-01-03 21:22:43 +00002311/*
2312 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002315{
Janos Follath24eed8d2019-11-22 13:21:35 +00002316 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Hanno Becker73d7d792018-12-11 10:35:51 +00002318 MPI_VALIDATE_RET( X != NULL );
2319 MPI_VALIDATE_RET( A != NULL );
2320 MPI_VALIDATE_RET( N != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002321
Hanno Becker4bcb4912017-04-18 15:49:39 +01002322 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TU ); mbedtls_mpi_init( &U1 ); mbedtls_mpi_init( &U2 );
2326 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &TB ); mbedtls_mpi_init( &TV );
2327 mbedtls_mpi_init( &V1 ); mbedtls_mpi_init( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, A, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002334 goto cleanup;
2335 }
2336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &TA, A, N ) );
2338 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TU, &TA ) );
2339 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, N ) );
2340 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TV, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U1, 1 ) );
2343 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U2, 0 ) );
2344 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V1, 0 ) );
2345 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002346
2347 do
2348 {
2349 while( ( TU.p[0] & 1 ) == 0 )
2350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TU, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002352
2353 if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
2354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &U1, &U1, &TB ) );
2356 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002357 }
2358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U1, 1 ) );
2360 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002361 }
2362
2363 while( ( TV.p[0] & 1 ) == 0 )
2364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TV, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002366
2367 if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
2368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, &TB ) );
2370 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002371 }
2372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V1, 1 ) );
2374 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002375 }
2376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 if( mbedtls_mpi_cmp_mpi( &TU, &TV ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TU, &TU, &TV ) );
2380 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U1, &U1, &V1 ) );
2381 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &V2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002382 }
2383 else
2384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TV, &TV, &TU ) );
2386 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, &U1 ) );
2387 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &U2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002388 }
2389 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 while( mbedtls_mpi_cmp_int( &TU, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 while( mbedtls_mpi_cmp_int( &V1, 0 ) < 0 )
2393 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 while( mbedtls_mpi_cmp_mpi( &V1, N ) >= 0 )
2396 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &V1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002399
2400cleanup:
2401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TU ); mbedtls_mpi_free( &U1 ); mbedtls_mpi_free( &U2 );
2403 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TV );
2404 mbedtls_mpi_free( &V1 ); mbedtls_mpi_free( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002405
2406 return( ret );
2407}
2408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002410
Paul Bakker5121ce52009-01-03 21:22:43 +00002411static const int small_prime[] =
2412{
2413 3, 5, 7, 11, 13, 17, 19, 23,
2414 29, 31, 37, 41, 43, 47, 53, 59,
2415 61, 67, 71, 73, 79, 83, 89, 97,
2416 101, 103, 107, 109, 113, 127, 131, 137,
2417 139, 149, 151, 157, 163, 167, 173, 179,
2418 181, 191, 193, 197, 199, 211, 223, 227,
2419 229, 233, 239, 241, 251, 257, 263, 269,
2420 271, 277, 281, 283, 293, 307, 311, 313,
2421 317, 331, 337, 347, 349, 353, 359, 367,
2422 373, 379, 383, 389, 397, 401, 409, 419,
2423 421, 431, 433, 439, 443, 449, 457, 461,
2424 463, 467, 479, 487, 491, 499, 503, 509,
2425 521, 523, 541, 547, 557, 563, 569, 571,
2426 577, 587, 593, 599, 601, 607, 613, 617,
2427 619, 631, 641, 643, 647, 653, 659, 661,
2428 673, 677, 683, 691, 701, 709, 719, 727,
2429 733, 739, 743, 751, 757, 761, 769, 773,
2430 787, 797, 809, 811, 821, 823, 827, 829,
2431 839, 853, 857, 859, 863, 877, 881, 883,
2432 887, 907, 911, 919, 929, 937, 941, 947,
2433 953, 967, 971, 977, 983, 991, 997, -103
2434};
2435
2436/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002437 * Small divisors test (X must be positive)
2438 *
2439 * Return values:
2440 * 0: no small factor (possible prime, more tests needed)
2441 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002443 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002444 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445static int mpi_check_small_factors( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +00002446{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002447 int ret = 0;
2448 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449 mbedtls_mpi_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +00002450
Paul Bakker5121ce52009-01-03 21:22:43 +00002451 if( ( X->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002453
2454 for( i = 0; small_prime[i] > 0; i++ )
2455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456 if( mbedtls_mpi_cmp_int( X, small_prime[i] ) <= 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002457 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, small_prime[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002460
2461 if( r == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002463 }
2464
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002465cleanup:
2466 return( ret );
2467}
2468
2469/*
2470 * Miller-Rabin pseudo-primality test (HAC 4.24)
2471 */
Janos Follathda31fa12018-09-03 14:45:23 +01002472static int mpi_miller_rabin( const mbedtls_mpi *X, size_t rounds,
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002473 int (*f_rng)(void *, unsigned char *, size_t),
2474 void *p_rng )
2475{
Pascal Junodb99183d2015-03-11 16:49:45 +01002476 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002477 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002479
Hanno Becker8ce11a32018-12-19 16:18:52 +00002480 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002481 MPI_VALIDATE_RET( f_rng != NULL );
2482
2483 mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R );
2484 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 mbedtls_mpi_init( &RR );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002486
Paul Bakker5121ce52009-01-03 21:22:43 +00002487 /*
2488 * W = |X| - 1
2489 * R = W >> lsb( W )
2490 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &W, X, 1 ) );
2492 s = mbedtls_mpi_lsb( &W );
2493 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R, &W ) );
2494 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &R, s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002495
Janos Follathda31fa12018-09-03 14:45:23 +01002496 for( i = 0; i < rounds; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002497 {
2498 /*
2499 * pick a random A, 1 < A < |X| - 1
2500 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002501 count = 0;
2502 do {
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002503 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
Pascal Junodb99183d2015-03-11 16:49:45 +01002504
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002505 j = mbedtls_mpi_bitlen( &A );
2506 k = mbedtls_mpi_bitlen( &W );
Pascal Junodb99183d2015-03-11 16:49:45 +01002507 if (j > k) {
Darryl Greene3f95ed2018-10-02 13:21:35 +01002508 A.p[A.n - 1] &= ( (mbedtls_mpi_uint) 1 << ( k - ( A.n - 1 ) * biL - 1 ) ) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002509 }
2510
2511 if (count++ > 30) {
Jens Wiklanderf08aa3e2019-01-17 13:30:57 +01002512 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2513 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002514 }
2515
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002516 } while ( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 ||
2517 mbedtls_mpi_cmp_int( &A, 1 ) <= 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002518
2519 /*
2520 * A = A^R mod |X|
2521 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &A, &A, &R, X, &RR ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 if( mbedtls_mpi_cmp_mpi( &A, &W ) == 0 ||
2525 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002526 continue;
2527
2528 j = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529 while( j < s && mbedtls_mpi_cmp_mpi( &A, &W ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002530 {
2531 /*
2532 * A = A * A mod |X|
2533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &A, &A ) );
2535 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &A, &T, X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 if( mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002538 break;
2539
2540 j++;
2541 }
2542
2543 /*
2544 * not prime if A != |X| - 1 or A == 1
2545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546 if( mbedtls_mpi_cmp_mpi( &A, &W ) != 0 ||
2547 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 break;
2551 }
2552 }
2553
2554cleanup:
Hanno Becker73d7d792018-12-11 10:35:51 +00002555 mbedtls_mpi_free( &W ); mbedtls_mpi_free( &R );
2556 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002558
2559 return( ret );
2560}
2561
2562/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002563 * Pseudo-primality test: small factors, then Miller-Rabin
2564 */
Janos Follatha0b67c22018-09-18 14:48:23 +01002565int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds,
2566 int (*f_rng)(void *, unsigned char *, size_t),
2567 void *p_rng )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002568{
Janos Follath24eed8d2019-11-22 13:21:35 +00002569 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 mbedtls_mpi XX;
Hanno Becker8ce11a32018-12-19 16:18:52 +00002571 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002572 MPI_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002573
2574 XX.s = 1;
2575 XX.n = X->n;
2576 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578 if( mbedtls_mpi_cmp_int( &XX, 0 ) == 0 ||
2579 mbedtls_mpi_cmp_int( &XX, 1 ) == 0 )
2580 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 if( mbedtls_mpi_cmp_int( &XX, 2 ) == 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002583 return( 0 );
2584
2585 if( ( ret = mpi_check_small_factors( &XX ) ) != 0 )
2586 {
2587 if( ret == 1 )
2588 return( 0 );
2589
2590 return( ret );
2591 }
2592
Janos Follathda31fa12018-09-03 14:45:23 +01002593 return( mpi_miller_rabin( &XX, rounds, f_rng, p_rng ) );
Janos Follathf301d232018-08-14 13:34:01 +01002594}
2595
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002596/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002598 *
Janos Follathf301d232018-08-14 13:34:01 +01002599 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2600 * be either 1024 bits or 1536 bits long, and flags must contain
2601 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002602 */
Janos Follath7c025a92018-08-14 11:08:41 +01002603int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002604 int (*f_rng)(void *, unsigned char *, size_t),
2605 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002606{
Jethro Beekman66689272018-02-14 19:24:10 -08002607#ifdef MBEDTLS_HAVE_INT64
2608// ceil(2^63.5)
2609#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2610#else
2611// ceil(2^31.5)
2612#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2613#endif
2614 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002615 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002616 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 mbedtls_mpi_uint r;
2618 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002619
Hanno Becker8ce11a32018-12-19 16:18:52 +00002620 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002621 MPI_VALIDATE_RET( f_rng != NULL );
2622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 if( nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS )
2624 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626 mbedtls_mpi_init( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002627
2628 n = BITS_TO_LIMBS( nbits );
2629
Janos Follathda31fa12018-09-03 14:45:23 +01002630 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR ) == 0 )
2631 {
2632 /*
2633 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2634 */
2635 rounds = ( ( nbits >= 1300 ) ? 2 : ( nbits >= 850 ) ? 3 :
2636 ( nbits >= 650 ) ? 4 : ( nbits >= 350 ) ? 8 :
2637 ( nbits >= 250 ) ? 12 : ( nbits >= 150 ) ? 18 : 27 );
2638 }
2639 else
2640 {
2641 /*
2642 * 2^-100 error probability, number of rounds computed based on HAC,
2643 * fact 4.48
2644 */
2645 rounds = ( ( nbits >= 1450 ) ? 4 : ( nbits >= 1150 ) ? 5 :
2646 ( nbits >= 1000 ) ? 6 : ( nbits >= 850 ) ? 7 :
2647 ( nbits >= 750 ) ? 8 : ( nbits >= 500 ) ? 13 :
2648 ( nbits >= 250 ) ? 28 : ( nbits >= 150 ) ? 40 : 51 );
2649 }
2650
Jethro Beekman66689272018-02-14 19:24:10 -08002651 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002652 {
Jethro Beekman66689272018-02-14 19:24:10 -08002653 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
2654 /* make sure generated number is at least (nbits-1)+0.5 bits (FIPS 186-4 §B.3.3 steps 4.4, 5.5) */
2655 if( X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2 ) continue;
2656
2657 k = n * biL;
2658 if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits ) );
2659 X->p[0] |= 1;
2660
Janos Follath7c025a92018-08-14 11:08:41 +01002661 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002662 {
Janos Follatha0b67c22018-09-18 14:48:23 +01002663 ret = mbedtls_mpi_is_prime_ext( X, rounds, f_rng, p_rng );
Jethro Beekman66689272018-02-14 19:24:10 -08002664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002666 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002667 }
Jethro Beekman66689272018-02-14 19:24:10 -08002668 else
Paul Bakker5121ce52009-01-03 21:22:43 +00002669 {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002670 /*
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01002671 * A necessary condition for Y and X = 2Y + 1 to be prime
Jethro Beekman66689272018-02-14 19:24:10 -08002672 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2673 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002674 */
Jethro Beekman66689272018-02-14 19:24:10 -08002675
2676 X->p[0] |= 2;
2677
2678 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, 3 ) );
2679 if( r == 0 )
2680 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 8 ) );
2681 else if( r == 1 )
2682 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 4 ) );
2683
2684 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
2685 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, X ) );
2686 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, 1 ) );
2687
2688 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 {
Jethro Beekman66689272018-02-14 19:24:10 -08002690 /*
2691 * First, check small factors for X and Y
2692 * before doing Miller-Rabin on any of them
2693 */
2694 if( ( ret = mpi_check_small_factors( X ) ) == 0 &&
2695 ( ret = mpi_check_small_factors( &Y ) ) == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002696 ( ret = mpi_miller_rabin( X, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002697 == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002698 ( ret = mpi_miller_rabin( &Y, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002699 == 0 )
Jethro Beekman66689272018-02-14 19:24:10 -08002700 goto cleanup;
2701
2702 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
2703 goto cleanup;
2704
2705 /*
2706 * Next candidates. We want to preserve Y = (X-1) / 2 and
2707 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2708 * so up Y by 6 and X by 12.
2709 */
2710 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 12 ) );
2711 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &Y, &Y, 6 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002713 }
2714 }
2715
2716cleanup:
2717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 mbedtls_mpi_free( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002719
2720 return( ret );
2721}
2722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002726
Paul Bakker23986e52011-04-24 08:57:21 +00002727#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002728
2729static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2730{
2731 { 693, 609, 21 },
2732 { 1764, 868, 28 },
2733 { 768454923, 542167814, 1 }
2734};
2735
Paul Bakker5121ce52009-01-03 21:22:43 +00002736/*
2737 * Checkup routine
2738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739int mbedtls_mpi_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002740{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002741 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00002743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002744 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); mbedtls_mpi_init( &X );
2745 mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &U ); mbedtls_mpi_init( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00002746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &A, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002748 "EFE021C2645FD1DC586E69184AF4A31E" \
2749 "D5F53E93B5F123FA41680867BA110131" \
2750 "944FE7952E2517337780CB0DB80E61AA" \
2751 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6" ) );
2752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002754 "B2E7EFD37075B9F03FF989C7C5051C20" \
2755 "34D2A323810251127E7BF8625A4F49A5" \
2756 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
2757 "5B5C25763222FEFCCFC38B832366C29E" ) );
2758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &N, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002760 "0066A198186C18C10B2F5ED9B522752A" \
2761 "9830B69916E535C8F047518A889A43A5" \
2762 "94B6BED27A168D31D4A52F88925AA8F5" ) );
2763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002767 "602AB7ECA597A3D6B56FF9829A5E8B85" \
2768 "9E857EA95A03512E2BAE7391688D264A" \
2769 "A5663B0341DB9CCFD2C4C5F421FEC814" \
2770 "8001B72E848A38CAE1C65F78E56ABDEF" \
2771 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
2772 "ECF677152EF804370C1A305CAF3B5BF1" \
2773 "30879B56C61DE584A0F53A2447A51E" ) );
2774
2775 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776 mbedtls_printf( " MPI test #1 (mul_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002779 {
2780 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002782
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002783 ret = 1;
2784 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002785 }
2786
2787 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &X, &Y, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002793 "256567336059E52CAE22925474705F39A94" ) );
2794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &V, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002796 "6613F26162223DF488E9CD48CC132C7A" \
2797 "0AC93C701B001B092E4E5B9F73BCD27B" \
2798 "9EE50D0657C77F374E903CDFA4C642" ) );
2799
2800 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801 mbedtls_printf( " MPI test #2 (div_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ||
2804 mbedtls_mpi_cmp_mpi( &Y, &V ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002805 {
2806 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002808
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002809 ret = 1;
2810 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002811 }
2812
2813 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &X, &A, &E, &N, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002819 "36E139AEA55215609D2816998ED020BB" \
2820 "BD96C37890F65171D948E9BC7CBAA4D9" \
2821 "325D24D6A3C12710F10A09FA08AB87" ) );
2822
2823 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824 mbedtls_printf( " MPI test #3 (exp_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002827 {
2828 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002830
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002831 ret = 1;
2832 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002833 }
2834
2835 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002841 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
2842 "C3DBA76456363A10869622EAC2DD84EC" \
2843 "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) );
2844
2845 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846 mbedtls_printf( " MPI test #4 (inv_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002849 {
2850 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002852
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002853 ret = 1;
2854 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002855 }
2856
2857 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002859
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002860 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861 mbedtls_printf( " MPI test #5 (simple gcd): " );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002862
Paul Bakker66d5d072014-06-17 16:39:18 +02002863 for( i = 0; i < GCD_PAIR_COUNT; i++ )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &X, gcd_pairs[i][0] ) );
2866 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Y, gcd_pairs[i][1] ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &A, &X, &Y ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 if( mbedtls_mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 )
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002871 {
2872 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873 mbedtls_printf( "failed at %d\n", i );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002874
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002875 ret = 1;
2876 goto cleanup;
2877 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002878 }
2879
2880 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881 mbedtls_printf( "passed\n" );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002882
Paul Bakker5121ce52009-01-03 21:22:43 +00002883cleanup:
2884
2885 if( ret != 0 && verbose != 0 )
Kenneth Soerensen518d4352020-04-01 17:22:45 +02002886 mbedtls_printf( "Unexpected error, return code = %08X\n", (unsigned int) ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &X );
2889 mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &U ); mbedtls_mpi_free( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00002890
2891 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002893
2894 return( ret );
2895}
2896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899#endif /* MBEDTLS_BIGNUM_C */