blob: d72dcf3e3e81785a8c489c91dbbd79dbb520a709 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Multi-precision integer library
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
Simon Butcher15b15d12015-11-26 19:35:03 +000019
Paul Bakker5121ce52009-01-03 21:22:43 +000020/*
Simon Butcher15b15d12015-11-26 19:35:03 +000021 * The following sources were referenced in the design of this Multi-precision
22 * Integer library:
Paul Bakker5121ce52009-01-03 21:22:43 +000023 *
Simon Butcher15b15d12015-11-26 19:35:03 +000024 * [1] Handbook of Applied Cryptography - 1997
25 * Menezes, van Oorschot and Vanstone
26 *
27 * [2] Multi-Precision Math
28 * Tom St Denis
29 * https://github.com/libtom/libtommath/blob/develop/tommath.pdf
30 *
31 * [3] GNU Multi-Precision Arithmetic Library
32 * https://gmplib.org/manual/index.html
33 *
Simon Butcherf5ba0452015-12-27 23:01:55 +000034 */
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Gilles Peskinedb09ef62020-06-03 01:43:33 +020036#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/bignum.h"
41#include "mbedtls/bn_mul.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050042#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000043#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Rich Evans00ab4702015-02-06 13:43:58 +000045#include <string.h>
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020049#else
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <stdio.h>
51#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#define mbedtls_printf printf
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020053#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020055#endif
56
Hanno Becker73d7d792018-12-11 10:35:51 +000057#define MPI_VALIDATE_RET( cond ) \
58 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA )
59#define MPI_VALIDATE( cond ) \
60 MBEDTLS_INTERNAL_VALIDATE( cond )
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
Paul Bakker5121ce52009-01-03 21:22:43 +000063#define biL (ciL << 3) /* bits in limb */
64#define biH (ciL << 2) /* half limb size */
65
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +010066#define MPI_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
67
Paul Bakker5121ce52009-01-03 21:22:43 +000068/*
69 * Convert between bits/chars and number of limbs
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +020070 * Divide first in order to avoid potential overflows
Paul Bakker5121ce52009-01-03 21:22:43 +000071 */
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +020072#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
73#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050075/* Implementation that should never be optimized out by the compiler */
Andres Amaya Garcia6698d2f2018-04-24 08:39:07 -050076static void mbedtls_mpi_zeroize( mbedtls_mpi_uint *v, size_t n )
77{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050078 mbedtls_platform_zeroize( v, ciL * n );
79}
80
Paul Bakker5121ce52009-01-03 21:22:43 +000081/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000082 * Initialize one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084void mbedtls_mpi_init( mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +000085{
Hanno Becker73d7d792018-12-11 10:35:51 +000086 MPI_VALIDATE( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +000087
Paul Bakker6c591fa2011-05-05 11:49:20 +000088 X->s = 1;
89 X->n = 0;
90 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000091}
92
93/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000094 * Unallocate one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096void mbedtls_mpi_free( mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +000097{
Paul Bakker6c591fa2011-05-05 11:49:20 +000098 if( X == NULL )
99 return;
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Paul Bakker6c591fa2011-05-05 11:49:20 +0000101 if( X->p != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 {
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200103 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 }
106
Paul Bakker6c591fa2011-05-05 11:49:20 +0000107 X->s = 1;
108 X->n = 0;
109 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000110}
111
112/*
113 * Enlarge to the specified number of limbs
114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000116{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_mpi_uint *p;
Hanno Becker73d7d792018-12-11 10:35:51 +0000118 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200121 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Paul Bakkerf9688572011-05-05 10:00:45 +0000122
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 if( X->n < nblimbs )
124 {
Simon Butcher29176892016-05-20 00:19:09 +0100125 if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( nblimbs, ciL ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200126 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 if( X->p != NULL )
129 {
130 memcpy( p, X->p, X->n * ciL );
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200131 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 }
134
135 X->n = nblimbs;
136 X->p = p;
137 }
138
139 return( 0 );
140}
141
142/*
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100143 * Resize down as much as possible,
144 * while keeping at least the specified number of limbs
145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100147{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_mpi_uint *p;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100149 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000150 MPI_VALIDATE_RET( X != NULL );
151
152 if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
153 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100154
Gilles Peskinee2f563e2020-01-20 21:17:43 +0100155 /* Actually resize up if there are currently fewer than nblimbs limbs. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100156 if( X->n <= nblimbs )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 return( mbedtls_mpi_grow( X, nblimbs ) );
Gilles Peskine322752b2020-01-21 13:59:51 +0100158 /* After this point, then X->n > nblimbs and in particular X->n > 0. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100159
160 for( i = X->n - 1; i > 0; i-- )
161 if( X->p[i] != 0 )
162 break;
163 i++;
164
165 if( i < nblimbs )
166 i = nblimbs;
167
Simon Butcher29176892016-05-20 00:19:09 +0100168 if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( i, ciL ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200169 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100170
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100171 if( X->p != NULL )
172 {
173 memcpy( p, X->p, i * ciL );
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200174 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_free( X->p );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100176 }
177
178 X->n = i;
179 X->p = p;
180
181 return( 0 );
182}
183
Gilles Peskine3130ce22021-06-02 22:17:52 +0200184/* Resize X to have exactly n limbs and set it to 0. */
185static int mbedtls_mpi_resize_clear( mbedtls_mpi *X, size_t limbs )
186{
187 if( limbs == 0 )
188 {
189 mbedtls_mpi_free( X );
190 return( 0 );
191 }
192 else if( X->n == limbs )
193 {
194 memset( X->p, 0, limbs * ciL );
195 X->s = 1;
196 return( 0 );
197 }
198 else
199 {
200 mbedtls_mpi_free( X );
201 return( mbedtls_mpi_grow( X, limbs ) );
202 }
203}
204
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100205/*
Gilles Peskinef643e8e2021-06-08 23:17:42 +0200206 * Copy the contents of Y into X.
207 *
208 * This function is not constant-time. Leading zeros in Y may be removed.
209 *
210 * Ensure that X does not shrink. This is not guaranteed by the public API,
211 * but some code in the bignum module relies on this property, for example
212 * in mbedtls_mpi_exp_mod().
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000215{
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100216 int ret = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000217 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000218 MPI_VALIDATE_RET( X != NULL );
219 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
221 if( X == Y )
222 return( 0 );
223
Gilles Peskinedb420622020-01-20 21:12:50 +0100224 if( Y->n == 0 )
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200225 {
Gilles Peskinef643e8e2021-06-08 23:17:42 +0200226 if( X->n != 0 )
227 {
228 X->s = 1;
229 memset( X->p, 0, X->n * ciL );
230 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200231 return( 0 );
232 }
233
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 for( i = Y->n - 1; i > 0; i-- )
235 if( Y->p[i] != 0 )
236 break;
237 i++;
238
239 X->s = Y->s;
240
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100241 if( X->n < i )
242 {
243 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i ) );
244 }
245 else
246 {
247 memset( X->p + i, 0, ( X->n - i ) * ciL );
248 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000249
Paul Bakker5121ce52009-01-03 21:22:43 +0000250 memcpy( X->p, Y->p, i * ciL );
251
252cleanup:
253
254 return( ret );
255}
256
257/*
258 * Swap the contents of X and Y
259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000261{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000263 MPI_VALIDATE( X != NULL );
264 MPI_VALIDATE( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 memcpy( &T, X, sizeof( mbedtls_mpi ) );
267 memcpy( X, Y, sizeof( mbedtls_mpi ) );
268 memcpy( Y, &T, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000269}
270
271/*
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200272 * Conditionally assign dest = src, without leaking information
273 * about whether the assignment was made or not.
274 * dest and src must be arrays of limbs of size n.
275 * assign must be 0 or 1.
276 */
277static void mpi_safe_cond_assign( size_t n,
278 mbedtls_mpi_uint *dest,
279 const mbedtls_mpi_uint *src,
280 unsigned char assign )
281{
282 size_t i;
283 for( i = 0; i < n; i++ )
284 dest[i] = dest[i] * ( 1 - assign ) + src[i] * assign;
285}
286
287/*
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100288 * Conditionally assign X = Y, without leaking information
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +0100289 * about whether the assignment was made or not.
290 * (Leaking information about the respective sizes of X and Y is ok however.)
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100293{
294 int ret = 0;
295 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000296 MPI_VALIDATE_RET( X != NULL );
297 MPI_VALIDATE_RET( Y != NULL );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100298
Pascal Junodb99183d2015-03-11 16:49:45 +0100299 /* make sure assign is 0 or 1 in a time-constant manner */
300 assign = (assign | (unsigned char)-assign) >> 7;
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100303
Paul Bakker66d5d072014-06-17 16:39:18 +0200304 X->s = X->s * ( 1 - assign ) + Y->s * assign;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100305
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200306 mpi_safe_cond_assign( Y->n, X->p, Y->p, assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100307
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200308 for( i = Y->n; i < X->n; i++ )
Paul Bakker66d5d072014-06-17 16:39:18 +0200309 X->p[i] *= ( 1 - assign );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100310
311cleanup:
312 return( ret );
313}
314
315/*
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100316 * Conditionally swap X and Y, without leaking information
317 * about whether the swap was made or not.
318 * Here it is not ok to simply swap the pointers, which whould lead to
319 * different memory access patterns when X and Y are used afterwards.
320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100322{
323 int ret, s;
324 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 mbedtls_mpi_uint tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +0000326 MPI_VALIDATE_RET( X != NULL );
327 MPI_VALIDATE_RET( Y != NULL );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100328
329 if( X == Y )
330 return( 0 );
331
Pascal Junodb99183d2015-03-11 16:49:45 +0100332 /* make sure swap is 0 or 1 in a time-constant manner */
333 swap = (swap | (unsigned char)-swap) >> 7;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
336 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( Y, X->n ) );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100337
338 s = X->s;
Paul Bakker66d5d072014-06-17 16:39:18 +0200339 X->s = X->s * ( 1 - swap ) + Y->s * swap;
340 Y->s = Y->s * ( 1 - swap ) + s * swap;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100341
342
343 for( i = 0; i < X->n; i++ )
344 {
345 tmp = X->p[i];
Paul Bakker66d5d072014-06-17 16:39:18 +0200346 X->p[i] = X->p[i] * ( 1 - swap ) + Y->p[i] * swap;
347 Y->p[i] = Y->p[i] * ( 1 - swap ) + tmp * swap;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100348 }
349
350cleanup:
351 return( ret );
352}
353
354/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000355 * Set value from integer
356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +0000358{
Janos Follath24eed8d2019-11-22 13:21:35 +0000359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker73d7d792018-12-11 10:35:51 +0000360 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 memset( X->p, 0, X->n * ciL );
364
365 X->p[0] = ( z < 0 ) ? -z : z;
366 X->s = ( z < 0 ) ? -1 : 1;
367
368cleanup:
369
370 return( ret );
371}
372
373/*
Paul Bakker2f5947e2011-05-18 15:47:11 +0000374 * Get a specific bit
375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000377{
Hanno Becker73d7d792018-12-11 10:35:51 +0000378 MPI_VALIDATE_RET( X != NULL );
379
Paul Bakker2f5947e2011-05-18 15:47:11 +0000380 if( X->n * biL <= pos )
381 return( 0 );
382
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200383 return( ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000384}
385
Gilles Peskine11cdb052018-11-20 16:47:47 +0100386/* Get a specific byte, without range checks. */
387#define GET_BYTE( X, i ) \
388 ( ( ( X )->p[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff )
389
Paul Bakker2f5947e2011-05-18 15:47:11 +0000390/*
391 * Set a bit to a specific value of 0 or 1
392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000394{
395 int ret = 0;
396 size_t off = pos / biL;
397 size_t idx = pos % biL;
Hanno Becker73d7d792018-12-11 10:35:51 +0000398 MPI_VALIDATE_RET( X != NULL );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000399
400 if( val != 0 && val != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker9af723c2014-05-01 13:03:14 +0200402
Paul Bakker2f5947e2011-05-18 15:47:11 +0000403 if( X->n * biL <= pos )
404 {
405 if( val == 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200406 return( 0 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, off + 1 ) );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000409 }
410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 X->p[off] &= ~( (mbedtls_mpi_uint) 0x01 << idx );
412 X->p[off] |= (mbedtls_mpi_uint) val << idx;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000413
414cleanup:
Paul Bakker9af723c2014-05-01 13:03:14 +0200415
Paul Bakker2f5947e2011-05-18 15:47:11 +0000416 return( ret );
417}
418
419/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200420 * Return the number of less significant zero-bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422size_t mbedtls_mpi_lsb( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000423{
Paul Bakker23986e52011-04-24 08:57:21 +0000424 size_t i, j, count = 0;
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000425 MBEDTLS_INTERNAL_VALIDATE_RET( X != NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
427 for( i = 0; i < X->n; i++ )
Paul Bakkerf9688572011-05-05 10:00:45 +0000428 for( j = 0; j < biL; j++, count++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 if( ( ( X->p[i] >> j ) & 1 ) != 0 )
430 return( count );
431
432 return( 0 );
433}
434
435/*
Simon Butcher15b15d12015-11-26 19:35:03 +0000436 * Count leading zero bits in a given integer
437 */
438static size_t mbedtls_clz( const mbedtls_mpi_uint x )
439{
440 size_t j;
Manuel Pégourié-Gonnarde3e8edf2015-12-01 09:31:52 +0100441 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
Simon Butcher15b15d12015-11-26 19:35:03 +0000442
443 for( j = 0; j < biL; j++ )
444 {
445 if( x & mask ) break;
446
447 mask >>= 1;
448 }
449
450 return j;
451}
452
453/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200454 * Return the number of bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200456size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000457{
Paul Bakker23986e52011-04-24 08:57:21 +0000458 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +0000459
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200460 if( X->n == 0 )
461 return( 0 );
462
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 for( i = X->n - 1; i > 0; i-- )
464 if( X->p[i] != 0 )
465 break;
466
Simon Butcher15b15d12015-11-26 19:35:03 +0000467 j = biL - mbedtls_clz( X->p[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
Paul Bakker23986e52011-04-24 08:57:21 +0000469 return( ( i * biL ) + j );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470}
471
472/*
473 * Return the total size in bytes
474 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475size_t mbedtls_mpi_size( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000476{
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200477 return( ( mbedtls_mpi_bitlen( X ) + 7 ) >> 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000478}
479
480/*
481 * Convert an ASCII character to digit value
482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483static int mpi_get_digit( mbedtls_mpi_uint *d, int radix, char c )
Paul Bakker5121ce52009-01-03 21:22:43 +0000484{
485 *d = 255;
486
487 if( c >= 0x30 && c <= 0x39 ) *d = c - 0x30;
488 if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37;
489 if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57;
490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 if( *d >= (mbedtls_mpi_uint) radix )
492 return( MBEDTLS_ERR_MPI_INVALID_CHARACTER );
Paul Bakker5121ce52009-01-03 21:22:43 +0000493
494 return( 0 );
495}
496
497/*
498 * Import from an ASCII string
499 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000501{
Janos Follath24eed8d2019-11-22 13:21:35 +0000502 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000503 size_t i, j, slen, n;
Gilles Peskine80f56732021-04-03 18:26:13 +0200504 int sign = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_mpi_uint d;
506 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000507 MPI_VALIDATE_RET( X != NULL );
508 MPI_VALIDATE_RET( s != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
510 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000511 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000514
Gilles Peskined4876132021-06-08 18:32:34 +0200515 if( s[0] == 0 )
516 {
517 mbedtls_mpi_free( X );
518 return( 0 );
519 }
520
Gilles Peskine80f56732021-04-03 18:26:13 +0200521 if( s[0] == '-' )
522 {
523 ++s;
524 sign = -1;
525 }
526
Paul Bakkerff60ee62010-03-16 21:09:09 +0000527 slen = strlen( s );
528
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 if( radix == 16 )
530 {
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +0100531 if( slen > MPI_SIZE_T_MAX >> 2 )
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +0200532 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
533
Paul Bakkerff60ee62010-03-16 21:09:09 +0000534 n = BITS_TO_LIMBS( slen << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n ) );
537 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000538
Paul Bakker23986e52011-04-24 08:57:21 +0000539 for( i = slen, j = 0; i > 0; i--, j++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200542 X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000543 }
544 }
545 else
546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
Paul Bakkerff60ee62010-03-16 21:09:09 +0000549 for( i = 0; i < slen; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
552 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T, X, radix ) );
Gilles Peskine80f56732021-04-03 18:26:13 +0200553 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, &T, d ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000554 }
555 }
556
Gilles Peskine80f56732021-04-03 18:26:13 +0200557 if( sign < 0 && mbedtls_mpi_bitlen( X ) != 0 )
558 X->s = -1;
559
Paul Bakker5121ce52009-01-03 21:22:43 +0000560cleanup:
561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
564 return( ret );
565}
566
567/*
Ron Eldora16fa292018-11-20 14:07:01 +0200568 * Helper to write the digits high-order first.
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 */
Ron Eldora16fa292018-11-20 14:07:01 +0200570static int mpi_write_hlp( mbedtls_mpi *X, int radix,
571 char **p, const size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000572{
Janos Follath24eed8d2019-11-22 13:21:35 +0000573 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_mpi_uint r;
Ron Eldora16fa292018-11-20 14:07:01 +0200575 size_t length = 0;
576 char *p_end = *p + buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
Ron Eldora16fa292018-11-20 14:07:01 +0200578 do
579 {
580 if( length >= buflen )
581 {
582 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
583 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
Ron Eldora16fa292018-11-20 14:07:01 +0200585 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) );
586 MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) );
587 /*
588 * Write the residue in the current position, as an ASCII character.
589 */
590 if( r < 0xA )
591 *(--p_end) = (char)( '0' + r );
592 else
593 *(--p_end) = (char)( 'A' + ( r - 0xA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
Ron Eldora16fa292018-11-20 14:07:01 +0200595 length++;
596 } while( mbedtls_mpi_cmp_int( X, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000597
Ron Eldora16fa292018-11-20 14:07:01 +0200598 memmove( *p, p_end, length );
599 *p += length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
601cleanup:
602
603 return( ret );
604}
605
606/*
607 * Export into an ASCII string
608 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100609int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
610 char *buf, size_t buflen, size_t *olen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000611{
Paul Bakker23986e52011-04-24 08:57:21 +0000612 int ret = 0;
613 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000616 MPI_VALIDATE_RET( X != NULL );
617 MPI_VALIDATE_RET( olen != NULL );
618 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
620 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000621 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000622
Hanno Becker23cfea02019-02-04 09:45:07 +0000623 n = mbedtls_mpi_bitlen( X ); /* Number of bits necessary to present `n`. */
624 if( radix >= 4 ) n >>= 1; /* Number of 4-adic digits necessary to present
625 * `n`. If radix > 4, this might be a strict
626 * overapproximation of the number of
627 * radix-adic digits needed to present `n`. */
628 if( radix >= 16 ) n >>= 1; /* Number of hexadecimal digits necessary to
629 * present `n`. */
630
Janos Follath80470622019-03-06 13:43:02 +0000631 n += 1; /* Terminating null byte */
Hanno Becker23cfea02019-02-04 09:45:07 +0000632 n += 1; /* Compensate for the divisions above, which round down `n`
633 * in case it's not even. */
634 n += 1; /* Potential '-'-sign. */
635 n += ( n & 1 ); /* Make n even to have enough space for hexadecimal writing,
636 * which always uses an even number of hex-digits. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000637
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100638 if( buflen < n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100640 *olen = n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 }
643
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100644 p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
647 if( X->s == -1 )
Hanno Beckerc983c812019-02-01 16:41:30 +0000648 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000649 *p++ = '-';
Hanno Beckerc983c812019-02-01 16:41:30 +0000650 buflen--;
651 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
653 if( radix == 16 )
654 {
Paul Bakker23986e52011-04-24 08:57:21 +0000655 int c;
656 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000657
Paul Bakker23986e52011-04-24 08:57:21 +0000658 for( i = X->n, k = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000659 {
Paul Bakker23986e52011-04-24 08:57:21 +0000660 for( j = ciL; j > 0; j-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 {
Paul Bakker23986e52011-04-24 08:57:21 +0000662 c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
Paul Bakker6c343d72014-07-10 14:36:19 +0200664 if( c == 0 && k == 0 && ( i + j ) != 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 continue;
666
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000667 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000668 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000669 k = 1;
670 }
671 }
672 }
673 else
674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T, X ) );
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000676
677 if( T.s == -1 )
678 T.s = 1;
679
Ron Eldora16fa292018-11-20 14:07:01 +0200680 MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p, buflen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 }
682
683 *p++ = '\0';
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100684 *olen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000685
686cleanup:
687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000689
690 return( ret );
691}
692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000694/*
695 * Read X from an opened file
696 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin )
Paul Bakker5121ce52009-01-03 21:22:43 +0000698{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_mpi_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000700 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000701 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000702 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000703 * Buffer should have space for (short) label and decimal formatted MPI,
704 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Paul Bakker5121ce52009-01-03 21:22:43 +0000707
Hanno Becker73d7d792018-12-11 10:35:51 +0000708 MPI_VALIDATE_RET( X != NULL );
709 MPI_VALIDATE_RET( fin != NULL );
710
711 if( radix < 2 || radix > 16 )
712 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
713
Paul Bakker5121ce52009-01-03 21:22:43 +0000714 memset( s, 0, sizeof( s ) );
715 if( fgets( s, sizeof( s ) - 1, fin ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000717
718 slen = strlen( s );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000719 if( slen == sizeof( s ) - 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000721
Hanno Beckerb2034b72017-04-26 11:46:46 +0100722 if( slen > 0 && s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
723 if( slen > 0 && s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
Paul Bakker5121ce52009-01-03 21:22:43 +0000724
725 p = s + slen;
Hanno Beckerb2034b72017-04-26 11:46:46 +0100726 while( p-- > s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000727 if( mpi_get_digit( &d, radix, *p ) != 0 )
728 break;
729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 return( mbedtls_mpi_read_string( X, radix, p + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000731}
732
733/*
734 * Write X into an opened file (or stdout if fout == NULL)
735 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout )
Paul Bakker5121ce52009-01-03 21:22:43 +0000737{
Janos Follath24eed8d2019-11-22 13:21:35 +0000738 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000739 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000740 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000741 * Buffer should have space for (short) label and decimal formatted MPI,
742 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000743 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Hanno Becker73d7d792018-12-11 10:35:51 +0000745 MPI_VALIDATE_RET( X != NULL );
746
747 if( radix < 2 || radix > 16 )
748 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100750 memset( s, 0, sizeof( s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100752 MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, sizeof( s ) - 2, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000753
754 if( p == NULL ) p = "";
755
756 plen = strlen( p );
757 slen = strlen( s );
758 s[slen++] = '\r';
759 s[slen++] = '\n';
760
761 if( fout != NULL )
762 {
763 if( fwrite( p, 1, plen, fout ) != plen ||
764 fwrite( s, 1, slen, fout ) != slen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000766 }
767 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_printf( "%s%s", p, s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
770cleanup:
771
772 return( ret );
773}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000775
Hanno Beckerda1655a2017-10-18 14:21:44 +0100776
777/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
778 * into the storage form used by mbedtls_mpi. */
Hanno Beckerf8720072018-11-08 11:53:49 +0000779
780static mbedtls_mpi_uint mpi_uint_bigendian_to_host_c( mbedtls_mpi_uint x )
781{
782 uint8_t i;
Hanno Becker031d6332019-05-01 17:09:11 +0100783 unsigned char *x_ptr;
Hanno Beckerf8720072018-11-08 11:53:49 +0000784 mbedtls_mpi_uint tmp = 0;
Hanno Becker031d6332019-05-01 17:09:11 +0100785
786 for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
787 {
788 tmp <<= CHAR_BIT;
789 tmp |= (mbedtls_mpi_uint) *x_ptr;
790 }
791
Hanno Beckerf8720072018-11-08 11:53:49 +0000792 return( tmp );
793}
794
795static mbedtls_mpi_uint mpi_uint_bigendian_to_host( mbedtls_mpi_uint x )
796{
797#if defined(__BYTE_ORDER__)
798
799/* Nothing to do on bigendian systems. */
800#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
801 return( x );
802#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
803
804#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
805
806/* For GCC and Clang, have builtins for byte swapping. */
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000807#if defined(__GNUC__) && defined(__GNUC_PREREQ)
808#if __GNUC_PREREQ(4,3)
Hanno Beckerf8720072018-11-08 11:53:49 +0000809#define have_bswap
810#endif
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000811#endif
812
813#if defined(__clang__) && defined(__has_builtin)
814#if __has_builtin(__builtin_bswap32) && \
815 __has_builtin(__builtin_bswap64)
816#define have_bswap
817#endif
818#endif
819
Hanno Beckerf8720072018-11-08 11:53:49 +0000820#if defined(have_bswap)
821 /* The compiler is hopefully able to statically evaluate this! */
822 switch( sizeof(mbedtls_mpi_uint) )
823 {
824 case 4:
825 return( __builtin_bswap32(x) );
826 case 8:
827 return( __builtin_bswap64(x) );
828 }
829#endif
830#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
831#endif /* __BYTE_ORDER__ */
832
833 /* Fall back to C-based reordering if we don't know the byte order
834 * or we couldn't use a compiler-specific builtin. */
835 return( mpi_uint_bigendian_to_host_c( x ) );
836}
837
Hanno Becker2be8a552018-10-25 12:40:09 +0100838static void mpi_bigendian_to_host( mbedtls_mpi_uint * const p, size_t limbs )
Hanno Beckerda1655a2017-10-18 14:21:44 +0100839{
Hanno Beckerda1655a2017-10-18 14:21:44 +0100840 mbedtls_mpi_uint *cur_limb_left;
841 mbedtls_mpi_uint *cur_limb_right;
Hanno Becker2be8a552018-10-25 12:40:09 +0100842 if( limbs == 0 )
843 return;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100844
845 /*
846 * Traverse limbs and
847 * - adapt byte-order in each limb
848 * - swap the limbs themselves.
849 * For that, simultaneously traverse the limbs from left to right
850 * and from right to left, as long as the left index is not bigger
851 * than the right index (it's not a problem if limbs is odd and the
852 * indices coincide in the last iteration).
853 */
Hanno Beckerda1655a2017-10-18 14:21:44 +0100854 for( cur_limb_left = p, cur_limb_right = p + ( limbs - 1 );
855 cur_limb_left <= cur_limb_right;
856 cur_limb_left++, cur_limb_right-- )
857 {
Hanno Beckerf8720072018-11-08 11:53:49 +0000858 mbedtls_mpi_uint tmp;
859 /* Note that if cur_limb_left == cur_limb_right,
860 * this code effectively swaps the bytes only once. */
861 tmp = mpi_uint_bigendian_to_host( *cur_limb_left );
862 *cur_limb_left = mpi_uint_bigendian_to_host( *cur_limb_right );
863 *cur_limb_right = tmp;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100864 }
Hanno Beckerda1655a2017-10-18 14:21:44 +0100865}
866
Paul Bakker5121ce52009-01-03 21:22:43 +0000867/*
Janos Follatha778a942019-02-13 10:28:28 +0000868 * Import X from unsigned binary data, little endian
869 */
870int mbedtls_mpi_read_binary_le( mbedtls_mpi *X,
871 const unsigned char *buf, size_t buflen )
872{
Janos Follath24eed8d2019-11-22 13:21:35 +0000873 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follatha778a942019-02-13 10:28:28 +0000874 size_t i;
875 size_t const limbs = CHARS_TO_LIMBS( buflen );
876
877 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine3130ce22021-06-02 22:17:52 +0200878 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Janos Follatha778a942019-02-13 10:28:28 +0000879
880 for( i = 0; i < buflen; i++ )
881 X->p[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
882
883cleanup:
884
Janos Follath171a7ef2019-02-15 16:17:45 +0000885 /*
886 * This function is also used to import keys. However, wiping the buffers
887 * upon failure is not necessary because failure only can happen before any
888 * input is copied.
889 */
Janos Follatha778a942019-02-13 10:28:28 +0000890 return( ret );
891}
892
893/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000894 * Import X from unsigned binary data, big endian
895 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000897{
Janos Follath24eed8d2019-11-22 13:21:35 +0000898 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100899 size_t const limbs = CHARS_TO_LIMBS( buflen );
900 size_t const overhead = ( limbs * ciL ) - buflen;
901 unsigned char *Xp;
Paul Bakker5121ce52009-01-03 21:22:43 +0000902
Hanno Becker8ce11a32018-12-19 16:18:52 +0000903 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +0000904 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
905
Hanno Becker073c1992017-10-17 15:17:27 +0100906 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine3130ce22021-06-02 22:17:52 +0200907 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
Gilles Peskine3130ce22021-06-02 22:17:52 +0200909 /* Avoid calling `memcpy` with NULL source or destination argument,
Hanno Becker0e810b92019-01-03 17:13:11 +0000910 * even if buflen is 0. */
Gilles Peskine3130ce22021-06-02 22:17:52 +0200911 if( buflen != 0 )
Hanno Becker0e810b92019-01-03 17:13:11 +0000912 {
913 Xp = (unsigned char*) X->p;
914 memcpy( Xp + overhead, buf, buflen );
Hanno Beckerda1655a2017-10-18 14:21:44 +0100915
Hanno Becker0e810b92019-01-03 17:13:11 +0000916 mpi_bigendian_to_host( X->p, limbs );
917 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000918
919cleanup:
920
Janos Follath171a7ef2019-02-15 16:17:45 +0000921 /*
922 * This function is also used to import keys. However, wiping the buffers
923 * upon failure is not necessary because failure only can happen before any
924 * input is copied.
925 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000926 return( ret );
927}
928
929/*
Janos Follathe344d0f2019-02-19 16:17:40 +0000930 * Export X into unsigned binary data, little endian
931 */
932int mbedtls_mpi_write_binary_le( const mbedtls_mpi *X,
933 unsigned char *buf, size_t buflen )
934{
935 size_t stored_bytes = X->n * ciL;
936 size_t bytes_to_copy;
937 size_t i;
938
939 if( stored_bytes < buflen )
940 {
941 bytes_to_copy = stored_bytes;
942 }
943 else
944 {
945 bytes_to_copy = buflen;
946
947 /* The output buffer is smaller than the allocated size of X.
948 * However X may fit if its leading bytes are zero. */
949 for( i = bytes_to_copy; i < stored_bytes; i++ )
950 {
951 if( GET_BYTE( X, i ) != 0 )
952 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
953 }
954 }
955
956 for( i = 0; i < bytes_to_copy; i++ )
957 buf[i] = GET_BYTE( X, i );
958
959 if( stored_bytes < buflen )
960 {
961 /* Write trailing 0 bytes */
962 memset( buf + stored_bytes, 0, buflen - stored_bytes );
963 }
964
965 return( 0 );
966}
967
968/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000969 * Export X into unsigned binary data, big endian
970 */
Gilles Peskine11cdb052018-11-20 16:47:47 +0100971int mbedtls_mpi_write_binary( const mbedtls_mpi *X,
972 unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000973{
Hanno Becker73d7d792018-12-11 10:35:51 +0000974 size_t stored_bytes;
Gilles Peskine11cdb052018-11-20 16:47:47 +0100975 size_t bytes_to_copy;
976 unsigned char *p;
977 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000978
Hanno Becker73d7d792018-12-11 10:35:51 +0000979 MPI_VALIDATE_RET( X != NULL );
980 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
981
982 stored_bytes = X->n * ciL;
983
Gilles Peskine11cdb052018-11-20 16:47:47 +0100984 if( stored_bytes < buflen )
985 {
986 /* There is enough space in the output buffer. Write initial
987 * null bytes and record the position at which to start
988 * writing the significant bytes. In this case, the execution
989 * trace of this function does not depend on the value of the
990 * number. */
991 bytes_to_copy = stored_bytes;
992 p = buf + buflen - stored_bytes;
993 memset( buf, 0, buflen - stored_bytes );
994 }
995 else
996 {
997 /* The output buffer is smaller than the allocated size of X.
998 * However X may fit if its leading bytes are zero. */
999 bytes_to_copy = buflen;
1000 p = buf;
1001 for( i = bytes_to_copy; i < stored_bytes; i++ )
1002 {
1003 if( GET_BYTE( X, i ) != 0 )
1004 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
1005 }
1006 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001007
Gilles Peskine11cdb052018-11-20 16:47:47 +01001008 for( i = 0; i < bytes_to_copy; i++ )
1009 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
Paul Bakker5121ce52009-01-03 21:22:43 +00001010
1011 return( 0 );
1012}
1013
1014/*
1015 * Left-shift: X <<= count
1016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +00001018{
Janos Follath24eed8d2019-11-22 13:21:35 +00001019 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001020 size_t i, v0, t1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +00001022 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001023
1024 v0 = count / (biL );
1025 t1 = count & (biL - 1);
1026
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001027 i = mbedtls_mpi_bitlen( X ) + count;
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
Paul Bakkerf9688572011-05-05 10:00:45 +00001029 if( X->n * biL < i )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001031
1032 ret = 0;
1033
1034 /*
1035 * shift by count / limb_size
1036 */
1037 if( v0 > 0 )
1038 {
Paul Bakker23986e52011-04-24 08:57:21 +00001039 for( i = X->n; i > v0; i-- )
1040 X->p[i - 1] = X->p[i - v0 - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Paul Bakker23986e52011-04-24 08:57:21 +00001042 for( ; i > 0; i-- )
1043 X->p[i - 1] = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001044 }
1045
1046 /*
1047 * shift by count % limb_size
1048 */
1049 if( t1 > 0 )
1050 {
1051 for( i = v0; i < X->n; i++ )
1052 {
1053 r1 = X->p[i] >> (biL - t1);
1054 X->p[i] <<= t1;
1055 X->p[i] |= r0;
1056 r0 = r1;
1057 }
1058 }
1059
1060cleanup:
1061
1062 return( ret );
1063}
1064
1065/*
1066 * Right-shift: X >>= count
1067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +00001069{
Paul Bakker23986e52011-04-24 08:57:21 +00001070 size_t i, v0, v1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +00001072 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001073
1074 v0 = count / biL;
1075 v1 = count & (biL - 1);
1076
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001077 if( v0 > X->n || ( v0 == X->n && v1 > 0 ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 return mbedtls_mpi_lset( X, 0 );
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001079
Paul Bakker5121ce52009-01-03 21:22:43 +00001080 /*
1081 * shift by count / limb_size
1082 */
1083 if( v0 > 0 )
1084 {
1085 for( i = 0; i < X->n - v0; i++ )
1086 X->p[i] = X->p[i + v0];
1087
1088 for( ; i < X->n; i++ )
1089 X->p[i] = 0;
1090 }
1091
1092 /*
1093 * shift by count % limb_size
1094 */
1095 if( v1 > 0 )
1096 {
Paul Bakker23986e52011-04-24 08:57:21 +00001097 for( i = X->n; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001098 {
Paul Bakker23986e52011-04-24 08:57:21 +00001099 r1 = X->p[i - 1] << (biL - v1);
1100 X->p[i - 1] >>= v1;
1101 X->p[i - 1] |= r0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001102 r0 = r1;
1103 }
1104 }
1105
1106 return( 0 );
1107}
1108
1109/*
1110 * Compare unsigned values
1111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001113{
Paul Bakker23986e52011-04-24 08:57:21 +00001114 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001115 MPI_VALIDATE_RET( X != NULL );
1116 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001117
Paul Bakker23986e52011-04-24 08:57:21 +00001118 for( i = X->n; i > 0; i-- )
1119 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001120 break;
1121
Paul Bakker23986e52011-04-24 08:57:21 +00001122 for( j = Y->n; j > 0; j-- )
1123 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001124 break;
1125
Paul Bakker23986e52011-04-24 08:57:21 +00001126 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001127 return( 0 );
1128
1129 if( i > j ) return( 1 );
1130 if( j > i ) return( -1 );
1131
Paul Bakker23986e52011-04-24 08:57:21 +00001132 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001133 {
Paul Bakker23986e52011-04-24 08:57:21 +00001134 if( X->p[i - 1] > Y->p[i - 1] ) return( 1 );
1135 if( X->p[i - 1] < Y->p[i - 1] ) return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001136 }
1137
1138 return( 0 );
1139}
1140
1141/*
1142 * Compare signed values
1143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001145{
Paul Bakker23986e52011-04-24 08:57:21 +00001146 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001147 MPI_VALIDATE_RET( X != NULL );
1148 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001149
Paul Bakker23986e52011-04-24 08:57:21 +00001150 for( i = X->n; i > 0; i-- )
1151 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001152 break;
1153
Paul Bakker23986e52011-04-24 08:57:21 +00001154 for( j = Y->n; j > 0; j-- )
1155 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001156 break;
1157
Paul Bakker23986e52011-04-24 08:57:21 +00001158 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001159 return( 0 );
1160
1161 if( i > j ) return( X->s );
Paul Bakker0c8f73b2012-03-22 14:08:57 +00001162 if( j > i ) return( -Y->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001163
1164 if( X->s > 0 && Y->s < 0 ) return( 1 );
1165 if( Y->s > 0 && X->s < 0 ) return( -1 );
1166
Paul Bakker23986e52011-04-24 08:57:21 +00001167 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001168 {
Paul Bakker23986e52011-04-24 08:57:21 +00001169 if( X->p[i - 1] > Y->p[i - 1] ) return( X->s );
1170 if( X->p[i - 1] < Y->p[i - 1] ) return( -X->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001171 }
1172
1173 return( 0 );
1174}
1175
Janos Follath3f6f0e42019-10-14 09:09:32 +01001176/** Decide if an integer is less than the other, without branches.
1177 *
1178 * \param x First integer.
1179 * \param y Second integer.
1180 *
1181 * \return 1 if \p x is less than \p y, 0 otherwise
1182 */
Janos Follath0e5532d2019-10-11 14:21:53 +01001183static unsigned ct_lt_mpi_uint( const mbedtls_mpi_uint x,
1184 const mbedtls_mpi_uint y )
Janos Follathee6abce2019-09-05 14:47:19 +01001185{
1186 mbedtls_mpi_uint ret;
1187 mbedtls_mpi_uint cond;
1188
1189 /*
1190 * Check if the most significant bits (MSB) of the operands are different.
1191 */
1192 cond = ( x ^ y );
1193 /*
1194 * If the MSB are the same then the difference x-y will be negative (and
1195 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
1196 */
1197 ret = ( x - y ) & ~cond;
1198 /*
1199 * If the MSB are different, then the operand with the MSB of 1 is the
1200 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
1201 * the MSB of y is 0.)
1202 */
1203 ret |= y & cond;
1204
1205
Janos Follatha0f732b2019-10-14 08:59:14 +01001206 ret = ret >> ( biL - 1 );
Janos Follathee6abce2019-09-05 14:47:19 +01001207
Janos Follath67ce6472019-10-29 15:08:46 +00001208 return (unsigned) ret;
Janos Follathee6abce2019-09-05 14:47:19 +01001209}
1210
1211/*
1212 * Compare signed values in constant time
1213 */
Janos Follath0e5532d2019-10-11 14:21:53 +01001214int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y,
1215 unsigned *ret )
Janos Follathee6abce2019-09-05 14:47:19 +01001216{
1217 size_t i;
Janos Follathbb5147f2019-10-28 12:23:18 +00001218 /* The value of any of these variables is either 0 or 1 at all times. */
Janos Follath5e614ce2019-10-28 12:31:34 +00001219 unsigned cond, done, X_is_negative, Y_is_negative;
Janos Follathee6abce2019-09-05 14:47:19 +01001220
1221 MPI_VALIDATE_RET( X != NULL );
1222 MPI_VALIDATE_RET( Y != NULL );
1223 MPI_VALIDATE_RET( ret != NULL );
1224
1225 if( X->n != Y->n )
1226 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1227
1228 /*
Janos Follath73ba9ec2019-10-28 12:12:15 +00001229 * Set sign_N to 1 if N >= 0, 0 if N < 0.
1230 * We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0.
Janos Follathee6abce2019-09-05 14:47:19 +01001231 */
Janos Follath5e614ce2019-10-28 12:31:34 +00001232 X_is_negative = ( X->s & 2 ) >> 1;
1233 Y_is_negative = ( Y->s & 2 ) >> 1;
Janos Follath0e5532d2019-10-11 14:21:53 +01001234
1235 /*
1236 * If the signs are different, then the positive operand is the bigger.
Janos Follath5e614ce2019-10-28 12:31:34 +00001237 * That is if X is negative (X_is_negative == 1), then X < Y is true and it
1238 * is false if X is positive (X_is_negative == 0).
Janos Follath0e5532d2019-10-11 14:21:53 +01001239 */
Janos Follath5e614ce2019-10-28 12:31:34 +00001240 cond = ( X_is_negative ^ Y_is_negative );
1241 *ret = cond & X_is_negative;
Janos Follath0e5532d2019-10-11 14:21:53 +01001242
1243 /*
Janos Follathbb5147f2019-10-28 12:23:18 +00001244 * This is a constant-time function. We might have the result, but we still
Janos Follath0e5532d2019-10-11 14:21:53 +01001245 * need to go through the loop. Record if we have the result already.
1246 */
Janos Follathee6abce2019-09-05 14:47:19 +01001247 done = cond;
1248
1249 for( i = X->n; i > 0; i-- )
1250 {
1251 /*
Janos Follath30702422019-11-05 12:24:52 +00001252 * If Y->p[i - 1] < X->p[i - 1] then X < Y is true if and only if both
1253 * X and Y are negative.
Janos Follath0e5532d2019-10-11 14:21:53 +01001254 *
1255 * Again even if we can make a decision, we just mark the result and
1256 * the fact that we are done and continue looping.
Janos Follathee6abce2019-09-05 14:47:19 +01001257 */
Janos Follath30702422019-11-05 12:24:52 +00001258 cond = ct_lt_mpi_uint( Y->p[i - 1], X->p[i - 1] );
1259 *ret |= cond & ( 1 - done ) & X_is_negative;
Janos Follathc50e6d52019-10-28 12:37:21 +00001260 done |= cond;
Janos Follathee6abce2019-09-05 14:47:19 +01001261
1262 /*
Janos Follath30702422019-11-05 12:24:52 +00001263 * If X->p[i - 1] < Y->p[i - 1] then X < Y is true if and only if both
1264 * X and Y are positive.
Janos Follath0e5532d2019-10-11 14:21:53 +01001265 *
1266 * Again even if we can make a decision, we just mark the result and
1267 * the fact that we are done and continue looping.
Janos Follathee6abce2019-09-05 14:47:19 +01001268 */
Janos Follath30702422019-11-05 12:24:52 +00001269 cond = ct_lt_mpi_uint( X->p[i - 1], Y->p[i - 1] );
1270 *ret |= cond & ( 1 - done ) & ( 1 - X_is_negative );
Janos Follathc50e6d52019-10-28 12:37:21 +00001271 done |= cond;
Janos Follathee6abce2019-09-05 14:47:19 +01001272 }
1273
1274 return( 0 );
1275}
1276
Paul Bakker5121ce52009-01-03 21:22:43 +00001277/*
1278 * Compare signed values
1279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +00001281{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 mbedtls_mpi Y;
1283 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001284 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001285
1286 *p = ( z < 0 ) ? -z : z;
1287 Y.s = ( z < 0 ) ? -1 : 1;
1288 Y.n = 1;
1289 Y.p = p;
1290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 return( mbedtls_mpi_cmp_mpi( X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001292}
1293
1294/*
1295 * Unsigned addition: X = |A| + |B| (HAC 14.7)
1296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001298{
Janos Follath24eed8d2019-11-22 13:21:35 +00001299 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001300 size_t i, j;
Janos Follath6c922682015-10-30 17:43:11 +01001301 mbedtls_mpi_uint *o, *p, c, tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +00001302 MPI_VALIDATE_RET( X != NULL );
1303 MPI_VALIDATE_RET( A != NULL );
1304 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001305
1306 if( X == B )
1307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 const mbedtls_mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 }
1310
1311 if( X != A )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
Paul Bakker9af723c2014-05-01 13:03:14 +02001313
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001314 /*
1315 * X should always be positive as a result of unsigned additions.
1316 */
1317 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001318
Paul Bakker23986e52011-04-24 08:57:21 +00001319 for( j = B->n; j > 0; j-- )
1320 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 break;
1322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001324
1325 o = B->p; p = X->p; c = 0;
1326
Janos Follath6c922682015-10-30 17:43:11 +01001327 /*
1328 * tmp is used because it might happen that p == o
1329 */
Paul Bakker23986e52011-04-24 08:57:21 +00001330 for( i = 0; i < j; i++, o++, p++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 {
Janos Follath6c922682015-10-30 17:43:11 +01001332 tmp= *o;
Paul Bakker5121ce52009-01-03 21:22:43 +00001333 *p += c; c = ( *p < c );
Janos Follath6c922682015-10-30 17:43:11 +01001334 *p += tmp; c += ( *p < tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 }
1336
1337 while( c != 0 )
1338 {
1339 if( i >= X->n )
1340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 p = X->p + i;
1343 }
1344
Paul Bakker2d319fd2012-09-16 21:34:26 +00001345 *p += c; c = ( *p < c ); i++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 }
1347
1348cleanup:
1349
1350 return( ret );
1351}
1352
Gilles Peskine09ec10a2020-06-09 10:39:38 +02001353/**
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001354 * Helper for mbedtls_mpi subtraction.
1355 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001356 * Calculate l - r where l and r have the same size.
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001357 * This function operates modulo (2^ciL)^n and returns the carry
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001358 * (1 if there was a wraparound, i.e. if `l < r`, and 0 otherwise).
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001359 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001360 * d may be aliased to l or r.
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001361 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001362 * \param n Number of limbs of \p d, \p l and \p r.
1363 * \param[out] d The result of the subtraction.
1364 * \param[in] l The left operand.
1365 * \param[in] r The right operand.
1366 *
1367 * \return 1 if `l < r`.
1368 * 0 if `l >= r`.
Paul Bakker5121ce52009-01-03 21:22:43 +00001369 */
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001370static mbedtls_mpi_uint mpi_sub_hlp( size_t n,
1371 mbedtls_mpi_uint *d,
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001372 const mbedtls_mpi_uint *l,
1373 const mbedtls_mpi_uint *r )
Paul Bakker5121ce52009-01-03 21:22:43 +00001374{
Paul Bakker23986e52011-04-24 08:57:21 +00001375 size_t i;
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001376 mbedtls_mpi_uint c = 0, t, z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001377
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001378 for( i = 0; i < n; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 {
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001380 z = ( l[i] < c ); t = l[i] - c;
1381 c = ( t < r[i] ) + z; d[i] = t - r[i];
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 }
1383
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001384 return( c );
Paul Bakker5121ce52009-01-03 21:22:43 +00001385}
1386
1387/*
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001388 * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
Paul Bakker5121ce52009-01-03 21:22:43 +00001389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001391{
Janos Follath24eed8d2019-11-22 13:21:35 +00001392 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001393 size_t n;
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001394 mbedtls_mpi_uint carry;
Hanno Becker73d7d792018-12-11 10:35:51 +00001395 MPI_VALIDATE_RET( X != NULL );
1396 MPI_VALIDATE_RET( A != NULL );
1397 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
Paul Bakker23986e52011-04-24 08:57:21 +00001399 for( n = B->n; n > 0; n-- )
1400 if( B->p[n - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 break;
Gilles Peskinec8a91772021-01-27 22:30:43 +01001402 if( n > A->n )
1403 {
1404 /* B >= (2^ciL)^n > A */
1405 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1406 goto cleanup;
1407 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001408
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001409 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, A->n ) );
1410
1411 /* Set the high limbs of X to match A. Don't touch the lower limbs
1412 * because X might be aliased to B, and we must not overwrite the
1413 * significant digits of B. */
1414 if( A->n > n )
1415 memcpy( X->p + n, A->p + n, ( A->n - n ) * ciL );
1416 if( X->n > A->n )
1417 memset( X->p + A->n, 0, ( X->n - A->n ) * ciL );
1418
1419 carry = mpi_sub_hlp( n, X->p, A->p, B->p );
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001420 if( carry != 0 )
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001421 {
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001422 /* Propagate the carry to the first nonzero limb of X. */
1423 for( ; n < X->n && X->p[n] == 0; n++ )
1424 --X->p[n];
1425 /* If we ran out of space for the carry, it means that the result
1426 * is negative. */
1427 if( n == X->n )
Gilles Peskine89b41302020-07-23 01:16:46 +02001428 {
1429 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1430 goto cleanup;
1431 }
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001432 --X->p[n];
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001433 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001434
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001435 /* X should always be positive as a result of unsigned subtractions. */
1436 X->s = 1;
1437
Paul Bakker5121ce52009-01-03 21:22:43 +00001438cleanup:
Paul Bakker5121ce52009-01-03 21:22:43 +00001439 return( ret );
1440}
1441
1442/*
1443 * Signed addition: X = A + B
1444 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001446{
Hanno Becker73d7d792018-12-11 10:35:51 +00001447 int ret, s;
1448 MPI_VALIDATE_RET( X != NULL );
1449 MPI_VALIDATE_RET( A != NULL );
1450 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001451
Hanno Becker73d7d792018-12-11 10:35:51 +00001452 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001453 if( A->s * B->s < 0 )
1454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001458 X->s = s;
1459 }
1460 else
1461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001463 X->s = -s;
1464 }
1465 }
1466 else
1467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001469 X->s = s;
1470 }
1471
1472cleanup:
1473
1474 return( ret );
1475}
1476
1477/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001478 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001481{
Hanno Becker73d7d792018-12-11 10:35:51 +00001482 int ret, s;
1483 MPI_VALIDATE_RET( X != NULL );
1484 MPI_VALIDATE_RET( A != NULL );
1485 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001486
Hanno Becker73d7d792018-12-11 10:35:51 +00001487 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001488 if( A->s * B->s > 0 )
1489 {
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 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001493 X->s = s;
1494 }
1495 else
1496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001498 X->s = -s;
1499 }
1500 }
1501 else
1502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001504 X->s = s;
1505 }
1506
1507cleanup:
1508
1509 return( ret );
1510}
1511
1512/*
1513 * Signed addition: X = A + b
1514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001516{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517 mbedtls_mpi _B;
1518 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001519 MPI_VALIDATE_RET( X != NULL );
1520 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001521
1522 p[0] = ( b < 0 ) ? -b : b;
1523 _B.s = ( b < 0 ) ? -1 : 1;
1524 _B.n = 1;
1525 _B.p = p;
1526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 return( mbedtls_mpi_add_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001528}
1529
1530/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001531 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001532 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001534{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 mbedtls_mpi _B;
1536 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001537 MPI_VALIDATE_RET( X != NULL );
1538 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001539
1540 p[0] = ( b < 0 ) ? -b : b;
1541 _B.s = ( b < 0 ) ? -1 : 1;
1542 _B.n = 1;
1543 _B.p = p;
1544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545 return( mbedtls_mpi_sub_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001546}
1547
Gilles Peskinea5d8d892020-07-23 21:27:15 +02001548/** Helper for mbedtls_mpi multiplication.
1549 *
1550 * Add \p b * \p s to \p d.
1551 *
1552 * \param i The number of limbs of \p s.
1553 * \param[in] s A bignum to multiply, of size \p i.
1554 * It may overlap with \p d, but only if
1555 * \p d <= \p s.
1556 * Its leading limb must not be \c 0.
1557 * \param[in,out] d The bignum to add to.
1558 * It must be sufficiently large to store the
1559 * result of the multiplication. This means
1560 * \p i + 1 limbs if \p d[\p i - 1] started as 0 and \p b
1561 * is not known a priori.
1562 * \param b A scalar to multiply.
Paul Bakkerfc4f46f2013-06-24 19:23:56 +02001563 */
1564static
1565#if defined(__APPLE__) && defined(__arm__)
1566/*
1567 * Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
1568 * appears to need this to prevent bad ARM code generation at -O3.
1569 */
1570__attribute__ ((noinline))
1571#endif
Gilles Peskinea5d8d892020-07-23 21:27:15 +02001572void mpi_mul_hlp( size_t i,
1573 const mbedtls_mpi_uint *s,
1574 mbedtls_mpi_uint *d,
1575 mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001576{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 mbedtls_mpi_uint c = 0, t = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001578
1579#if defined(MULADDC_HUIT)
1580 for( ; i >= 8; i -= 8 )
1581 {
1582 MULADDC_INIT
1583 MULADDC_HUIT
1584 MULADDC_STOP
1585 }
1586
1587 for( ; i > 0; i-- )
1588 {
1589 MULADDC_INIT
1590 MULADDC_CORE
1591 MULADDC_STOP
1592 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001593#else /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001594 for( ; i >= 16; i -= 16 )
1595 {
1596 MULADDC_INIT
1597 MULADDC_CORE MULADDC_CORE
1598 MULADDC_CORE MULADDC_CORE
1599 MULADDC_CORE MULADDC_CORE
1600 MULADDC_CORE MULADDC_CORE
1601
1602 MULADDC_CORE MULADDC_CORE
1603 MULADDC_CORE MULADDC_CORE
1604 MULADDC_CORE MULADDC_CORE
1605 MULADDC_CORE MULADDC_CORE
1606 MULADDC_STOP
1607 }
1608
1609 for( ; i >= 8; i -= 8 )
1610 {
1611 MULADDC_INIT
1612 MULADDC_CORE MULADDC_CORE
1613 MULADDC_CORE MULADDC_CORE
1614
1615 MULADDC_CORE MULADDC_CORE
1616 MULADDC_CORE MULADDC_CORE
1617 MULADDC_STOP
1618 }
1619
1620 for( ; i > 0; i-- )
1621 {
1622 MULADDC_INIT
1623 MULADDC_CORE
1624 MULADDC_STOP
1625 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001626#endif /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001627
1628 t++;
1629
Gilles Peskine8e464c42020-07-24 00:08:38 +02001630 while( c != 0 )
1631 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001632 *d += c; c = ( *d < c ); d++;
1633 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001634}
1635
1636/*
1637 * Baseline multiplication: X = A * B (HAC 14.12)
1638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001640{
Janos Follath24eed8d2019-11-22 13:21:35 +00001641 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001642 size_t i, j;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 mbedtls_mpi TA, TB;
Hanno Becker73d7d792018-12-11 10:35:51 +00001644 MPI_VALIDATE_RET( X != NULL );
1645 MPI_VALIDATE_RET( A != NULL );
1646 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650 if( X == A ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); A = &TA; }
1651 if( X == B ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); B = &TB; }
Paul Bakker5121ce52009-01-03 21:22:43 +00001652
Paul Bakker23986e52011-04-24 08:57:21 +00001653 for( i = A->n; i > 0; i-- )
1654 if( A->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001655 break;
1656
Paul Bakker23986e52011-04-24 08:57:21 +00001657 for( j = B->n; j > 0; j-- )
1658 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001659 break;
1660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + j ) );
1662 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001663
Alexey Skalozub8e75e682016-01-13 21:59:27 +02001664 for( ; j > 0; j-- )
1665 mpi_mul_hlp( i, A->p, X->p + j - 1, B->p[j - 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00001666
Gilles Peskine70a7dcd2021-06-10 15:51:54 +02001667 /* If the result is 0, we don't shortcut the operation, which reduces
1668 * but does not eliminate side channels leaking the zero-ness. We do
1669 * need to take care to set the sign bit properly since the library does
1670 * not fully support an MPI object with a value of 0 and s == -1. */
1671 if( ( i == 0 && ( A->n == 0 || A->p[0] == 0 ) ) ||
1672 ( j == 0 && ( B->n == 0 || B->p[0] == 0 ) ) )
1673 {
1674 X->s = 1;
1675 }
1676 else
1677 X->s = A->s * B->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001678
1679cleanup:
1680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001682
1683 return( ret );
1684}
1685
1686/*
1687 * Baseline multiplication: X = A * b
1688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001690{
Hanno Becker73d7d792018-12-11 10:35:51 +00001691 MPI_VALIDATE_RET( X != NULL );
1692 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001693
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001694 /* mpi_mul_hlp can't deal with a leading 0. */
1695 size_t n = A->n;
1696 while( n > 0 && A->p[n - 1] == 0 )
1697 --n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001698
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001699 /* The general method below doesn't work if n==0 or b==0. By chance
1700 * calculating the result is trivial in those cases. */
1701 if( b == 0 || n == 0 )
1702 {
Paul Elliott986b55a2021-04-20 21:46:29 +01001703 return( mbedtls_mpi_lset( X, 0 ) );
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001704 }
1705
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001706 /* Calculate A*b as A + A*(b-1) to take advantage of mpi_mul_hlp */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001707 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001708 /* In general, A * b requires 1 limb more than b. If
1709 * A->p[n - 1] * b / b == A->p[n - 1], then A * b fits in the same
1710 * number of limbs as A and the call to grow() is not required since
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001711 * copy() will take care of the growth if needed. However, experimentally,
1712 * making the call to grow() unconditional causes slightly fewer
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001713 * calls to calloc() in ECP code, presumably because it reuses the
1714 * same mpi for a while and this way the mpi is more likely to directly
1715 * grow to its final size. */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001716 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n + 1 ) );
1717 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
1718 mpi_mul_hlp( n, A->p, X->p, b - 1 );
1719
1720cleanup:
1721 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001722}
1723
1724/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001725 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1726 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001727 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001728static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1,
1729 mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r )
Simon Butcher15b15d12015-11-26 19:35:03 +00001730{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001731#if defined(MBEDTLS_HAVE_UDBL)
1732 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001733#else
Simon Butcher9803d072016-01-03 00:24:34 +00001734 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
1735 const mbedtls_mpi_uint uint_halfword_mask = ( (mbedtls_mpi_uint) 1 << biH ) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001736 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1737 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001738 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001739#endif
1740
Simon Butcher15b15d12015-11-26 19:35:03 +00001741 /*
1742 * Check for overflow
1743 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001744 if( 0 == d || u1 >= d )
Simon Butcher15b15d12015-11-26 19:35:03 +00001745 {
Simon Butcherf5ba0452015-12-27 23:01:55 +00001746 if (r != NULL) *r = ~0;
Simon Butcher15b15d12015-11-26 19:35:03 +00001747
Simon Butcherf5ba0452015-12-27 23:01:55 +00001748 return ( ~0 );
Simon Butcher15b15d12015-11-26 19:35:03 +00001749 }
1750
1751#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001752 dividend = (mbedtls_t_udbl) u1 << biL;
1753 dividend |= (mbedtls_t_udbl) u0;
1754 quotient = dividend / d;
1755 if( quotient > ( (mbedtls_t_udbl) 1 << biL ) - 1 )
1756 quotient = ( (mbedtls_t_udbl) 1 << biL ) - 1;
1757
1758 if( r != NULL )
Simon Butcher9803d072016-01-03 00:24:34 +00001759 *r = (mbedtls_mpi_uint)( dividend - (quotient * d ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001760
1761 return (mbedtls_mpi_uint) quotient;
1762#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001763
1764 /*
1765 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1766 * Vol. 2 - Seminumerical Algorithms, Knuth
1767 */
1768
1769 /*
1770 * Normalize the divisor, d, and dividend, u0, u1
1771 */
1772 s = mbedtls_clz( d );
1773 d = d << s;
1774
1775 u1 = u1 << s;
Simon Butcher9803d072016-01-03 00:24:34 +00001776 u1 |= ( u0 >> ( biL - s ) ) & ( -(mbedtls_mpi_sint)s >> ( biL - 1 ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001777 u0 = u0 << s;
1778
1779 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001780 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001781
1782 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001783 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001784
1785 /*
1786 * Find the first quotient and remainder
1787 */
1788 q1 = u1 / d1;
1789 r0 = u1 - d1 * q1;
1790
1791 while( q1 >= radix || ( q1 * d0 > radix * r0 + u0_msw ) )
1792 {
1793 q1 -= 1;
1794 r0 += d1;
1795
1796 if ( r0 >= radix ) break;
1797 }
1798
Simon Butcherf5ba0452015-12-27 23:01:55 +00001799 rAX = ( u1 * radix ) + ( u0_msw - q1 * d );
Simon Butcher15b15d12015-11-26 19:35:03 +00001800 q0 = rAX / d1;
1801 r0 = rAX - q0 * d1;
1802
1803 while( q0 >= radix || ( q0 * d0 > radix * r0 + u0_lsw ) )
1804 {
1805 q0 -= 1;
1806 r0 += d1;
1807
1808 if ( r0 >= radix ) break;
1809 }
1810
1811 if (r != NULL)
Simon Butcherf5ba0452015-12-27 23:01:55 +00001812 *r = ( rAX * radix + u0_lsw - q0 * d ) >> s;
Simon Butcher15b15d12015-11-26 19:35:03 +00001813
1814 quotient = q1 * radix + q0;
1815
1816 return quotient;
1817#endif
1818}
1819
1820/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001822 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001823int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1824 const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001825{
Janos Follath24eed8d2019-11-22 13:21:35 +00001826 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001827 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 mbedtls_mpi X, Y, Z, T1, T2;
Alexander Kd19a1932019-11-01 18:20:42 +03001829 mbedtls_mpi_uint TP2[3];
Hanno Becker73d7d792018-12-11 10:35:51 +00001830 MPI_VALIDATE_RET( A != NULL );
1831 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 if( mbedtls_mpi_cmp_int( B, 0 ) == 0 )
1834 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Alexander K35d6d462019-10-31 14:46:45 +03001837 mbedtls_mpi_init( &T1 );
Alexander Kd19a1932019-11-01 18:20:42 +03001838 /*
1839 * Avoid dynamic memory allocations for constant-size T2.
1840 *
1841 * T2 is used for comparison only and the 3 limbs are assigned explicitly,
1842 * so nobody increase the size of the MPI and we're safe to use an on-stack
1843 * buffer.
1844 */
Alexander K35d6d462019-10-31 14:46:45 +03001845 T2.s = 1;
Alexander Kd19a1932019-11-01 18:20:42 +03001846 T2.n = sizeof( TP2 ) / sizeof( *TP2 );
1847 T2.p = TP2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851 if( Q != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_lset( Q, 0 ) );
1852 if( R != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001853 return( 0 );
1854 }
1855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &X, A ) );
1857 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001858 X.s = Y.s = 1;
1859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &Z, A->n + 2 ) );
1861 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Z, 0 ) );
Gilles Peskine2536aa72020-07-24 00:12:59 +02001862 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, A->n + 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001863
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001864 k = mbedtls_mpi_bitlen( &Y ) % biL;
Paul Bakkerf9688572011-05-05 10:00:45 +00001865 if( k < biL - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001866 {
1867 k = biL - 1 - k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &X, k ) );
1869 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, k ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001870 }
1871 else k = 0;
1872
1873 n = X.n - 1;
1874 t = Y.n - 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 while( mbedtls_mpi_cmp_mpi( &X, &Y ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001878 {
1879 Z.p[n - t]++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001881 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001883
1884 for( i = n; i > t ; i-- )
1885 {
1886 if( X.p[i] >= Y.p[t] )
1887 Z.p[i - t - 1] = ~0;
1888 else
1889 {
Simon Butcher15b15d12015-11-26 19:35:03 +00001890 Z.p[i - t - 1] = mbedtls_int_div_int( X.p[i], X.p[i - 1],
1891 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001892 }
1893
Alexander K35d6d462019-10-31 14:46:45 +03001894 T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2];
1895 T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1];
1896 T2.p[2] = X.p[i];
1897
Paul Bakker5121ce52009-01-03 21:22:43 +00001898 Z.p[i - t - 1]++;
1899 do
1900 {
1901 Z.p[i - t - 1]--;
1902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T1, 0 ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001904 T1.p[0] = ( t < 1 ) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001905 T1.p[1] = Y.p[t];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001907 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 while( mbedtls_mpi_cmp_mpi( &T1, &T2 ) > 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
1911 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1912 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 if( mbedtls_mpi_cmp_int( &X, 0 ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &Y ) );
1917 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1918 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001919 Z.p[i - t - 1]--;
1920 }
1921 }
1922
1923 if( Q != NULL )
1924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( Q, &Z ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001926 Q->s = A->s * B->s;
1927 }
1928
1929 if( R != NULL )
1930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &X, k ) );
Paul Bakkerf02c5642012-11-13 10:25:21 +00001932 X.s = A->s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, &X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 if( mbedtls_mpi_cmp_int( R, 0 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001936 R->s = 1;
1937 }
1938
1939cleanup:
1940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Alexander K35d6d462019-10-31 14:46:45 +03001942 mbedtls_mpi_free( &T1 );
Alexander Kd19a1932019-11-01 18:20:42 +03001943 mbedtls_platform_zeroize( TP2, sizeof( TP2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001944
1945 return( ret );
1946}
1947
1948/*
1949 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00001950 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001951int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R,
1952 const mbedtls_mpi *A,
1953 mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001954{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 mbedtls_mpi _B;
1956 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001957 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001958
1959 p[0] = ( b < 0 ) ? -b : b;
1960 _B.s = ( b < 0 ) ? -1 : 1;
1961 _B.n = 1;
1962 _B.p = p;
1963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964 return( mbedtls_mpi_div_mpi( Q, R, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001965}
1966
1967/*
1968 * Modulo: R = A mod B
1969 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001971{
Janos Follath24eed8d2019-11-22 13:21:35 +00001972 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker73d7d792018-12-11 10:35:51 +00001973 MPI_VALIDATE_RET( R != NULL );
1974 MPI_VALIDATE_RET( A != NULL );
1975 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 if( mbedtls_mpi_cmp_int( B, 0 ) < 0 )
1978 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( NULL, R, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 while( mbedtls_mpi_cmp_int( R, 0 ) < 0 )
1983 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 while( mbedtls_mpi_cmp_mpi( R, B ) >= 0 )
1986 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001987
1988cleanup:
1989
1990 return( ret );
1991}
1992
1993/*
1994 * Modulo: r = A mod b
1995 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001997{
Paul Bakker23986e52011-04-24 08:57:21 +00001998 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 mbedtls_mpi_uint x, y, z;
Hanno Becker73d7d792018-12-11 10:35:51 +00002000 MPI_VALIDATE_RET( r != NULL );
2001 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002002
2003 if( b == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00002005
2006 if( b < 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002008
2009 /*
2010 * handle trivial cases
2011 */
2012 if( b == 1 )
2013 {
2014 *r = 0;
2015 return( 0 );
2016 }
2017
2018 if( b == 2 )
2019 {
2020 *r = A->p[0] & 1;
2021 return( 0 );
2022 }
2023
2024 /*
2025 * general case
2026 */
Paul Bakker23986e52011-04-24 08:57:21 +00002027 for( i = A->n, y = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00002028 {
Paul Bakker23986e52011-04-24 08:57:21 +00002029 x = A->p[i - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00002030 y = ( y << biH ) | ( x >> biH );
2031 z = y / b;
2032 y -= z * b;
2033
2034 x <<= biH;
2035 y = ( y << biH ) | ( x >> biH );
2036 z = y / b;
2037 y -= z * b;
2038 }
2039
Paul Bakkerce40a6d2009-06-23 19:46:08 +00002040 /*
2041 * If A is negative, then the current y represents a negative value.
2042 * Flipping it to the positive side.
2043 */
2044 if( A->s < 0 && y != 0 )
2045 y = b - y;
2046
Paul Bakker5121ce52009-01-03 21:22:43 +00002047 *r = y;
2048
2049 return( 0 );
2050}
2051
2052/*
2053 * Fast Montgomery initialization (thanks to Tom St Denis)
2054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055static void mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057 mbedtls_mpi_uint x, m0 = N->p[0];
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01002058 unsigned int i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002059
2060 x = m0;
2061 x += ( ( m0 + 2 ) & 4 ) << 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002062
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01002063 for( i = biL; i >= 8; i /= 2 )
2064 x *= ( 2 - ( m0 * x ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002065
2066 *mm = ~x + 1;
2067}
2068
Gilles Peskine2a82f722020-06-04 15:00:49 +02002069/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
2070 *
2071 * \param[in,out] A One of the numbers to multiply.
Gilles Peskine221626f2020-06-08 22:37:50 +02002072 * It must have at least as many limbs as N
2073 * (A->n >= N->n), and any limbs beyond n are ignored.
Gilles Peskine2a82f722020-06-04 15:00:49 +02002074 * On successful completion, A contains the result of
2075 * the multiplication A * B * R^-1 mod N where
2076 * R = (2^ciL)^n.
2077 * \param[in] B One of the numbers to multiply.
2078 * It must be nonzero and must not have more limbs than N
2079 * (B->n <= N->n).
2080 * \param[in] N The modulo. N must be odd.
2081 * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
2082 * This is -N^-1 mod 2^ciL.
2083 * \param[in,out] T A bignum for temporary storage.
2084 * It must be at least twice the limb size of N plus 2
2085 * (T->n >= 2 * (N->n + 1)).
2086 * Its initial content is unused and
2087 * its final content is indeterminate.
2088 * Note that unlike the usual convention in the library
2089 * for `const mbedtls_mpi*`, the content of T can change.
Paul Bakker5121ce52009-01-03 21:22:43 +00002090 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002091static 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 +02002092 const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00002093{
Paul Bakker23986e52011-04-24 08:57:21 +00002094 size_t i, n, m;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 mbedtls_mpi_uint u0, u1, *d;
Paul Bakker5121ce52009-01-03 21:22:43 +00002096
2097 memset( T->p, 0, T->n * ciL );
2098
2099 d = T->p;
2100 n = N->n;
2101 m = ( B->n < n ) ? B->n : n;
2102
2103 for( i = 0; i < n; i++ )
2104 {
2105 /*
2106 * T = (T + u0*B + u1*N) / 2^biL
2107 */
2108 u0 = A->p[i];
2109 u1 = ( d[0] + u0 * B->p[0] ) * mm;
2110
2111 mpi_mul_hlp( m, B->p, d, u0 );
2112 mpi_mul_hlp( n, N->p, d, u1 );
2113
2114 *d++ = u0; d[n + 1] = 0;
2115 }
2116
Gilles Peskine221626f2020-06-08 22:37:50 +02002117 /* At this point, d is either the desired result or the desired result
2118 * plus N. We now potentially subtract N, avoiding leaking whether the
2119 * subtraction is performed through side channels. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002120
Gilles Peskine221626f2020-06-08 22:37:50 +02002121 /* Copy the n least significant limbs of d to A, so that
2122 * A = d if d < N (recall that N has n limbs). */
2123 memcpy( A->p, d, n * ciL );
Gilles Peskine09ec10a2020-06-09 10:39:38 +02002124 /* If d >= N then we want to set A to d - N. To prevent timing attacks,
Gilles Peskine221626f2020-06-08 22:37:50 +02002125 * do the calculation without using conditional tests. */
2126 /* Set d to d0 + (2^biL)^n - N where d0 is the current value of d. */
Gilles Peskine132c0972020-06-04 21:05:24 +02002127 d[n] += 1;
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02002128 d[n] -= mpi_sub_hlp( n, d, d, N->p );
Gilles Peskine221626f2020-06-08 22:37:50 +02002129 /* If d0 < N then d < (2^biL)^n
2130 * so d[n] == 0 and we want to keep A as it is.
2131 * If d0 >= N then d >= (2^biL)^n, and d <= (2^biL)^n + N < 2 * (2^biL)^n
2132 * so d[n] == 1 and we want to set A to the result of the subtraction
2133 * which is d - (2^biL)^n, i.e. the n least significant limbs of d.
2134 * This exactly corresponds to a conditional assignment. */
2135 mpi_safe_cond_assign( n, A->p, d, (unsigned char) d[n] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002136}
2137
2138/*
2139 * Montgomery reduction: A = A * R^-1 mod N
Gilles Peskine2a82f722020-06-04 15:00:49 +02002140 *
2141 * See mpi_montmul() regarding constraints and guarantees on the parameters.
Paul Bakker5121ce52009-01-03 21:22:43 +00002142 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002143static void mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N,
2144 mbedtls_mpi_uint mm, const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00002145{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 mbedtls_mpi_uint z = 1;
2147 mbedtls_mpi U;
Paul Bakker5121ce52009-01-03 21:22:43 +00002148
Paul Bakker8ddb6452013-02-27 14:56:33 +01002149 U.n = U.s = (int) z;
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 U.p = &z;
2151
Gilles Peskine4e91d472020-06-04 20:55:15 +02002152 mpi_montmul( A, &U, N, mm, T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002153}
2154
2155/*
2156 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
2157 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002158int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
2159 const mbedtls_mpi *E, const mbedtls_mpi *N,
2160 mbedtls_mpi *_RR )
Paul Bakker5121ce52009-01-03 21:22:43 +00002161{
Janos Follath24eed8d2019-11-22 13:21:35 +00002162 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002163 size_t wbits, wsize, one = 1;
2164 size_t i, j, nblimbs;
2165 size_t bufsize, nbits;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 mbedtls_mpi_uint ei, mm, state;
Daniel Otte388f9b22020-08-21 12:34:29 +02002167 mbedtls_mpi RR, T, W[ 1 << MBEDTLS_MPI_WINDOW_SIZE ], Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00002168 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002169
Hanno Becker73d7d792018-12-11 10:35:51 +00002170 MPI_VALIDATE_RET( X != NULL );
2171 MPI_VALIDATE_RET( A != NULL );
2172 MPI_VALIDATE_RET( E != NULL );
2173 MPI_VALIDATE_RET( N != NULL );
2174
Hanno Becker8d1dd1b2017-09-28 11:02:24 +01002175 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 || ( N->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 if( mbedtls_mpi_cmp_int( E, 0 ) < 0 )
2179 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002180
Chris Jones9246d042020-11-25 15:12:39 +00002181 if( mbedtls_mpi_bitlen( E ) > MBEDTLS_MPI_MAX_BITS ||
2182 mbedtls_mpi_bitlen( N ) > MBEDTLS_MPI_MAX_BITS )
2183 return ( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2184
Paul Bakkerf6198c12012-05-16 08:02:29 +00002185 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002186 * Init temps and window size
2187 */
2188 mpi_montg_init( &mm, N );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &T );
2190 mbedtls_mpi_init( &Apos );
Paul Bakker5121ce52009-01-03 21:22:43 +00002191 memset( W, 0, sizeof( W ) );
2192
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002193 i = mbedtls_mpi_bitlen( E );
Paul Bakker5121ce52009-01-03 21:22:43 +00002194
2195 wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
2196 ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1;
2197
Peter Kolbuse6bcad32018-12-11 14:01:44 -06002198#if( MBEDTLS_MPI_WINDOW_SIZE < 6 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 if( wsize > MBEDTLS_MPI_WINDOW_SIZE )
2200 wsize = MBEDTLS_MPI_WINDOW_SIZE;
Peter Kolbuse6bcad32018-12-11 14:01:44 -06002201#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00002202
Paul Bakker5121ce52009-01-03 21:22:43 +00002203 j = N->n + 1;
Gilles Peskinef643e8e2021-06-08 23:17:42 +02002204 /* All W[i] and X must have at least N->n limbs for the mpi_montmul()
2205 * and mpi_montred() calls later. Here we ensure that W[1] and X are
2206 * large enough, and later we'll grow other W[i] to the same length.
2207 * They must not be shrunk midway through this function!
2208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
2210 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], j ) );
2211 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T, j * 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002212
2213 /*
Paul Bakker50546922012-05-19 08:40:49 +00002214 * Compensate for negative A (and correct at the end)
2215 */
2216 neg = ( A->s == -1 );
Paul Bakker50546922012-05-19 08:40:49 +00002217 if( neg )
2218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Apos, A ) );
Paul Bakker50546922012-05-19 08:40:49 +00002220 Apos.s = 1;
2221 A = &Apos;
2222 }
2223
2224 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 * If 1st call, pre-compute R^2 mod N
2226 */
2227 if( _RR == NULL || _RR->p == NULL )
2228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &RR, 1 ) );
2230 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &RR, N->n * 2 * biL ) );
2231 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &RR, &RR, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002232
2233 if( _RR != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 memcpy( _RR, &RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002235 }
2236 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 memcpy( &RR, _RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002238
2239 /*
2240 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
2241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 if( mbedtls_mpi_cmp_mpi( A, N ) >= 0 )
Gilles Peskinef643e8e2021-06-08 23:17:42 +02002243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &W[1], A, N ) );
Gilles Peskinef643e8e2021-06-08 23:17:42 +02002245 /* This should be a no-op because W[1] is already that large before
2246 * mbedtls_mpi_mod_mpi(), but it's necessary to avoid an overflow
2247 * in mpi_montmul() below, so let's make sure. */
Gilles Peskine0759cad2021-06-15 21:22:48 +02002248 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], N->n + 1 ) );
Gilles Peskinef643e8e2021-06-08 23:17:42 +02002249 }
Paul Bakkerc2024f42014-01-23 20:38:35 +01002250 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[1], A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002252
Gilles Peskinef643e8e2021-06-08 23:17:42 +02002253 /* Note that this is safe because W[1] always has at least N->n limbs
2254 * (it grew above and was preserved by mbedtls_mpi_copy()). */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002255 mpi_montmul( &W[1], &RR, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002256
2257 /*
2258 * X = R^2 * R^-1 mod N = R mod N
2259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &RR ) );
Gilles Peskine4e91d472020-06-04 20:55:15 +02002261 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002262
2263 if( wsize > 1 )
2264 {
2265 /*
2266 * W[1 << (wsize - 1)] = W[1] ^ (wsize - 1)
2267 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002268 j = one << ( wsize - 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[j], N->n + 1 ) );
2271 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[j], &W[1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002272
2273 for( i = 0; i < wsize - 1; i++ )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002274 mpi_montmul( &W[j], &W[j], N, mm, &T );
Paul Bakker0d7702c2013-10-29 16:18:35 +01002275
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 /*
2277 * W[i] = W[i - 1] * W[1]
2278 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002279 for( i = j + 1; i < ( one << wsize ); i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[i], N->n + 1 ) );
2282 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[i], &W[i - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002283
Gilles Peskine4e91d472020-06-04 20:55:15 +02002284 mpi_montmul( &W[i], &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002285 }
2286 }
2287
2288 nblimbs = E->n;
2289 bufsize = 0;
2290 nbits = 0;
2291 wbits = 0;
2292 state = 0;
2293
2294 while( 1 )
2295 {
2296 if( bufsize == 0 )
2297 {
Paul Bakker0d7702c2013-10-29 16:18:35 +01002298 if( nblimbs == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002299 break;
2300
Paul Bakker0d7702c2013-10-29 16:18:35 +01002301 nblimbs--;
2302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 bufsize = sizeof( mbedtls_mpi_uint ) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00002304 }
2305
2306 bufsize--;
2307
2308 ei = (E->p[nblimbs] >> bufsize) & 1;
2309
2310 /*
2311 * skip leading 0s
2312 */
2313 if( ei == 0 && state == 0 )
2314 continue;
2315
2316 if( ei == 0 && state == 1 )
2317 {
2318 /*
2319 * out of window, square X
2320 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002321 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002322 continue;
2323 }
2324
2325 /*
2326 * add ei to current window
2327 */
2328 state = 2;
2329
2330 nbits++;
Paul Bakker66d5d072014-06-17 16:39:18 +02002331 wbits |= ( ei << ( wsize - nbits ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002332
2333 if( nbits == wsize )
2334 {
2335 /*
2336 * X = X^wsize R^-1 mod N
2337 */
2338 for( i = 0; i < wsize; i++ )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002339 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002340
2341 /*
2342 * X = X * W[wbits] R^-1 mod N
2343 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002344 mpi_montmul( X, &W[wbits], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002345
2346 state--;
2347 nbits = 0;
2348 wbits = 0;
2349 }
2350 }
2351
2352 /*
2353 * process the remaining bits
2354 */
2355 for( i = 0; i < nbits; i++ )
2356 {
Gilles Peskine4e91d472020-06-04 20:55:15 +02002357 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002358
2359 wbits <<= 1;
2360
Paul Bakker66d5d072014-06-17 16:39:18 +02002361 if( ( wbits & ( one << wsize ) ) != 0 )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002362 mpi_montmul( X, &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002363 }
2364
2365 /*
2366 * X = A^E * R * R^-1 mod N = A^E mod N
2367 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002368 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002369
Hanno Beckera4af1c42017-04-18 09:07:45 +01002370 if( neg && E->n != 0 && ( E->p[0] & 1 ) != 0 )
Paul Bakkerf6198c12012-05-16 08:02:29 +00002371 {
2372 X->s = -1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, N, X ) );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002374 }
2375
Paul Bakker5121ce52009-01-03 21:22:43 +00002376cleanup:
2377
Paul Bakker66d5d072014-06-17 16:39:18 +02002378 for( i = ( one << ( wsize - 1 ) ); i < ( one << wsize ); i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 mbedtls_mpi_free( &W[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381 mbedtls_mpi_free( &W[1] ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &Apos );
Paul Bakker6c591fa2011-05-05 11:49:20 +00002382
Paul Bakker75a28602014-03-31 12:08:17 +02002383 if( _RR == NULL || _RR->p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002385
2386 return( ret );
2387}
2388
Paul Bakker5121ce52009-01-03 21:22:43 +00002389/*
2390 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
2391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00002393{
Janos Follath24eed8d2019-11-22 13:21:35 +00002394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002395 size_t lz, lzt;
Alexander Ke8ad49f2019-08-16 16:16:07 +03002396 mbedtls_mpi TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00002397
Hanno Becker73d7d792018-12-11 10:35:51 +00002398 MPI_VALIDATE_RET( G != NULL );
2399 MPI_VALIDATE_RET( A != NULL );
2400 MPI_VALIDATE_RET( B != NULL );
2401
Alexander Ke8ad49f2019-08-16 16:16:07 +03002402 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) );
2405 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407 lz = mbedtls_mpi_lsb( &TA );
2408 lzt = mbedtls_mpi_lsb( &TB );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002409
Gilles Peskineb5e56ec2021-06-09 13:26:43 +02002410 /* The loop below gives the correct result when A==0 but not when B==0.
2411 * So have a special case for B==0. Leverage the fact that we just
2412 * calculated the lsb and lsb(B)==0 iff B is odd or 0 to make the test
2413 * slightly more efficient than cmp_int(). */
2414 if( lzt == 0 && mbedtls_mpi_get_bit( &TB, 0 ) == 0 )
2415 {
2416 ret = mbedtls_mpi_copy( G, A );
2417 goto cleanup;
2418 }
2419
Paul Bakker66d5d072014-06-17 16:39:18 +02002420 if( lzt < lz )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002421 lz = lzt;
2422
Paul Bakker5121ce52009-01-03 21:22:43 +00002423 TA.s = TB.s = 1;
2424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 while( mbedtls_mpi_cmp_int( &TA, 0 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, mbedtls_mpi_lsb( &TA ) ) );
2428 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, mbedtls_mpi_lsb( &TB ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 if( mbedtls_mpi_cmp_mpi( &TA, &TB ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TA, &TA, &TB ) );
2433 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002434 }
2435 else
2436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TB, &TB, &TA ) );
2438 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002439 }
2440 }
2441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &TB, lz ) );
2443 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( G, &TB ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002444
2445cleanup:
2446
Alexander Ke8ad49f2019-08-16 16:16:07 +03002447 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002448
2449 return( ret );
2450}
2451
Gilles Peskine8f454702021-04-01 15:57:18 +02002452/* Fill X with n_bytes random bytes.
2453 * X must already have room for those bytes.
Gilles Peskine23422e42021-06-03 11:51:09 +02002454 * The ordering of the bytes returned from the RNG is suitable for
2455 * deterministic ECDSA (see RFC 6979 §3.3 and mbedtls_mpi_random()).
Gilles Peskinea16001e2021-04-13 21:55:35 +02002456 * The size and sign of X are unchanged.
Gilles Peskine8f454702021-04-01 15:57:18 +02002457 * n_bytes must not be 0.
2458 */
2459static int mpi_fill_random_internal(
2460 mbedtls_mpi *X, size_t n_bytes,
2461 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
2462{
2463 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2464 const size_t limbs = CHARS_TO_LIMBS( n_bytes );
2465 const size_t overhead = ( limbs * ciL ) - n_bytes;
2466
2467 if( X->n < limbs )
2468 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Gilles Peskine8f454702021-04-01 15:57:18 +02002469
Gilles Peskinea16001e2021-04-13 21:55:35 +02002470 memset( X->p, 0, overhead );
2471 memset( (unsigned char *) X->p + limbs * ciL, 0, ( X->n - limbs ) * ciL );
Gilles Peskine8f454702021-04-01 15:57:18 +02002472 MBEDTLS_MPI_CHK( f_rng( p_rng, (unsigned char *) X->p + overhead, n_bytes ) );
2473 mpi_bigendian_to_host( X->p, limbs );
2474
2475cleanup:
2476 return( ret );
2477}
2478
Paul Bakker33dc46b2014-04-30 16:11:39 +02002479/*
2480 * Fill X with size bytes of random.
2481 *
2482 * Use a temporary bytes representation to make sure the result is the same
Paul Bakkerc37b0ac2014-05-01 14:19:23 +02002483 * regardless of the platform endianness (useful when f_rng is actually
Paul Bakker33dc46b2014-04-30 16:11:39 +02002484 * deterministic, eg for tests).
2485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002487 int (*f_rng)(void *, unsigned char *, size_t),
2488 void *p_rng )
Paul Bakker287781a2011-03-26 13:18:49 +00002489{
Janos Follath24eed8d2019-11-22 13:21:35 +00002490 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6dab6202019-01-02 16:42:29 +00002491 size_t const limbs = CHARS_TO_LIMBS( size );
Hanno Beckerda1655a2017-10-18 14:21:44 +01002492
Hanno Becker8ce11a32018-12-19 16:18:52 +00002493 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002494 MPI_VALIDATE_RET( f_rng != NULL );
Paul Bakker33dc46b2014-04-30 16:11:39 +02002495
Hanno Beckerda1655a2017-10-18 14:21:44 +01002496 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine3130ce22021-06-02 22:17:52 +02002497 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Gilles Peskine8f454702021-04-01 15:57:18 +02002498 if( size == 0 )
2499 return( 0 );
Paul Bakker287781a2011-03-26 13:18:49 +00002500
Gilles Peskine8f454702021-04-01 15:57:18 +02002501 ret = mpi_fill_random_internal( X, size, f_rng, p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +00002502
2503cleanup:
2504 return( ret );
2505}
2506
Gilles Peskine4699fa42021-03-29 22:02:55 +02002507int mbedtls_mpi_random( mbedtls_mpi *X,
2508 mbedtls_mpi_sint min,
2509 const mbedtls_mpi *N,
2510 int (*f_rng)(void *, unsigned char *, size_t),
2511 void *p_rng )
2512{
Gilles Peskine4699fa42021-03-29 22:02:55 +02002513 int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskinee39ee8e2021-04-13 21:23:25 +02002514 int count;
Gilles Peskine4699fa42021-03-29 22:02:55 +02002515 unsigned cmp = 0;
2516 size_t n_bits = mbedtls_mpi_bitlen( N );
2517 size_t n_bytes = ( n_bits + 7 ) / 8;
2518
Gilles Peskine9312ba52021-03-29 22:14:51 +02002519 if( min < 0 )
2520 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2521 if( mbedtls_mpi_cmp_int( N, min ) <= 0 )
2522 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2523
Gilles Peskinee39ee8e2021-04-13 21:23:25 +02002524 /*
2525 * When min == 0, each try has at worst a probability 1/2 of failing
2526 * (the msb has a probability 1/2 of being 0, and then the result will
2527 * be < N), so after 30 tries failure probability is a most 2**(-30).
2528 *
2529 * When N is just below a power of 2, as is the case when generating
Gilles Peskine3f613632021-04-15 11:45:19 +02002530 * a random scalar on most elliptic curves, 1 try is enough with
Gilles Peskinee39ee8e2021-04-13 21:23:25 +02002531 * overwhelming probability. When N is just above a power of 2,
Gilles Peskine3f613632021-04-15 11:45:19 +02002532 * as when generating a random scalar on secp224k1, each try has
Gilles Peskinee39ee8e2021-04-13 21:23:25 +02002533 * a probability of failing that is almost 1/2.
2534 *
2535 * The probabilities are almost the same if min is nonzero but negligible
2536 * compared to N. This is always the case when N is crypto-sized, but
2537 * it's convenient to support small N for testing purposes. When N
2538 * is small, use a higher repeat count, otherwise the probability of
2539 * failure is macroscopic.
2540 */
Gilles Peskine11779072021-06-02 21:18:59 +02002541 count = ( n_bytes > 4 ? 30 : 250 );
Gilles Peskinee39ee8e2021-04-13 21:23:25 +02002542
Gilles Peskine8f454702021-04-01 15:57:18 +02002543 /* Ensure that target MPI has exactly the same number of limbs
2544 * as the upper bound, even if the upper bound has leading zeros.
2545 * This is necessary for the mbedtls_mpi_lt_mpi_ct() check. */
Gilles Peskine3130ce22021-06-02 22:17:52 +02002546 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, N->n ) );
Gilles Peskine8f454702021-04-01 15:57:18 +02002547
Gilles Peskine4699fa42021-03-29 22:02:55 +02002548 /*
2549 * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA)
2550 * when f_rng is a suitably parametrized instance of HMAC_DRBG:
2551 * - use the same byte ordering;
2552 * - keep the leftmost n_bits bits of the generated octet string;
2553 * - try until result is in the desired range.
2554 * This also avoids any bias, which is especially important for ECDSA.
2555 */
2556 do
2557 {
Gilles Peskine8f454702021-04-01 15:57:18 +02002558 MBEDTLS_MPI_CHK( mpi_fill_random_internal( X, n_bytes, f_rng, p_rng ) );
Gilles Peskine4699fa42021-03-29 22:02:55 +02002559 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, 8 * n_bytes - n_bits ) );
2560
Gilles Peskinee39ee8e2021-04-13 21:23:25 +02002561 if( --count == 0 )
Gilles Peskine4699fa42021-03-29 22:02:55 +02002562 {
2563 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2564 goto cleanup;
2565 }
2566
Gilles Peskinec0b68bf2021-06-03 11:38:26 +02002567 MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( X, N, &cmp ) );
Gilles Peskine4699fa42021-03-29 22:02:55 +02002568 }
2569 while( mbedtls_mpi_cmp_int( X, min ) < 0 || cmp != 1 );
2570
2571cleanup:
2572 return( ret );
2573}
2574
Paul Bakker5121ce52009-01-03 21:22:43 +00002575/*
2576 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002579{
Janos Follath24eed8d2019-11-22 13:21:35 +00002580 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Hanno Becker73d7d792018-12-11 10:35:51 +00002582 MPI_VALIDATE_RET( X != NULL );
2583 MPI_VALIDATE_RET( A != NULL );
2584 MPI_VALIDATE_RET( N != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002585
Hanno Becker4bcb4912017-04-18 15:49:39 +01002586 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TU ); mbedtls_mpi_init( &U1 ); mbedtls_mpi_init( &U2 );
2590 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &TB ); mbedtls_mpi_init( &TV );
2591 mbedtls_mpi_init( &V1 ); mbedtls_mpi_init( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, A, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 goto cleanup;
2599 }
2600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &TA, A, N ) );
2602 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TU, &TA ) );
2603 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, N ) );
2604 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TV, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U1, 1 ) );
2607 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U2, 0 ) );
2608 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V1, 0 ) );
2609 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002610
2611 do
2612 {
2613 while( ( TU.p[0] & 1 ) == 0 )
2614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TU, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002616
2617 if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
2618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &U1, &U1, &TB ) );
2620 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002621 }
2622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U1, 1 ) );
2624 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 }
2626
2627 while( ( TV.p[0] & 1 ) == 0 )
2628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TV, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002630
2631 if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
2632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, &TB ) );
2634 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002635 }
2636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V1, 1 ) );
2638 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002639 }
2640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 if( mbedtls_mpi_cmp_mpi( &TU, &TV ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TU, &TU, &TV ) );
2644 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U1, &U1, &V1 ) );
2645 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &V2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002646 }
2647 else
2648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TV, &TV, &TU ) );
2650 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, &U1 ) );
2651 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &U2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002652 }
2653 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 while( mbedtls_mpi_cmp_int( &TU, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 while( mbedtls_mpi_cmp_int( &V1, 0 ) < 0 )
2657 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659 while( mbedtls_mpi_cmp_mpi( &V1, N ) >= 0 )
2660 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &V1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
2664cleanup:
2665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TU ); mbedtls_mpi_free( &U1 ); mbedtls_mpi_free( &U2 );
2667 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TV );
2668 mbedtls_mpi_free( &V1 ); mbedtls_mpi_free( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002669
2670 return( ret );
2671}
2672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002674
Paul Bakker5121ce52009-01-03 21:22:43 +00002675static const int small_prime[] =
2676{
2677 3, 5, 7, 11, 13, 17, 19, 23,
2678 29, 31, 37, 41, 43, 47, 53, 59,
2679 61, 67, 71, 73, 79, 83, 89, 97,
2680 101, 103, 107, 109, 113, 127, 131, 137,
2681 139, 149, 151, 157, 163, 167, 173, 179,
2682 181, 191, 193, 197, 199, 211, 223, 227,
2683 229, 233, 239, 241, 251, 257, 263, 269,
2684 271, 277, 281, 283, 293, 307, 311, 313,
2685 317, 331, 337, 347, 349, 353, 359, 367,
2686 373, 379, 383, 389, 397, 401, 409, 419,
2687 421, 431, 433, 439, 443, 449, 457, 461,
2688 463, 467, 479, 487, 491, 499, 503, 509,
2689 521, 523, 541, 547, 557, 563, 569, 571,
2690 577, 587, 593, 599, 601, 607, 613, 617,
2691 619, 631, 641, 643, 647, 653, 659, 661,
2692 673, 677, 683, 691, 701, 709, 719, 727,
2693 733, 739, 743, 751, 757, 761, 769, 773,
2694 787, 797, 809, 811, 821, 823, 827, 829,
2695 839, 853, 857, 859, 863, 877, 881, 883,
2696 887, 907, 911, 919, 929, 937, 941, 947,
2697 953, 967, 971, 977, 983, 991, 997, -103
2698};
2699
2700/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002701 * Small divisors test (X must be positive)
2702 *
2703 * Return values:
2704 * 0: no small factor (possible prime, more tests needed)
2705 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002707 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709static int mpi_check_small_factors( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +00002710{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002711 int ret = 0;
2712 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713 mbedtls_mpi_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +00002714
Paul Bakker5121ce52009-01-03 21:22:43 +00002715 if( ( X->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002717
2718 for( i = 0; small_prime[i] > 0; i++ )
2719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720 if( mbedtls_mpi_cmp_int( X, small_prime[i] ) <= 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002721 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, small_prime[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002724
2725 if( r == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002727 }
2728
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002729cleanup:
2730 return( ret );
2731}
2732
2733/*
2734 * Miller-Rabin pseudo-primality test (HAC 4.24)
2735 */
Janos Follathda31fa12018-09-03 14:45:23 +01002736static int mpi_miller_rabin( const mbedtls_mpi *X, size_t rounds,
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002737 int (*f_rng)(void *, unsigned char *, size_t),
2738 void *p_rng )
2739{
Pascal Junodb99183d2015-03-11 16:49:45 +01002740 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002741 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002743
Hanno Becker8ce11a32018-12-19 16:18:52 +00002744 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002745 MPI_VALIDATE_RET( f_rng != NULL );
2746
2747 mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R );
2748 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749 mbedtls_mpi_init( &RR );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002750
Paul Bakker5121ce52009-01-03 21:22:43 +00002751 /*
2752 * W = |X| - 1
2753 * R = W >> lsb( W )
2754 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &W, X, 1 ) );
2756 s = mbedtls_mpi_lsb( &W );
2757 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R, &W ) );
2758 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &R, s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002759
Janos Follathda31fa12018-09-03 14:45:23 +01002760 for( i = 0; i < rounds; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002761 {
2762 /*
2763 * pick a random A, 1 < A < |X| - 1
2764 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002765 count = 0;
2766 do {
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002767 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
Pascal Junodb99183d2015-03-11 16:49:45 +01002768
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002769 j = mbedtls_mpi_bitlen( &A );
2770 k = mbedtls_mpi_bitlen( &W );
Pascal Junodb99183d2015-03-11 16:49:45 +01002771 if (j > k) {
Darryl Greene3f95ed2018-10-02 13:21:35 +01002772 A.p[A.n - 1] &= ( (mbedtls_mpi_uint) 1 << ( k - ( A.n - 1 ) * biL - 1 ) ) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002773 }
2774
2775 if (count++ > 30) {
Jens Wiklanderf08aa3e2019-01-17 13:30:57 +01002776 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2777 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002778 }
2779
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002780 } while ( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 ||
2781 mbedtls_mpi_cmp_int( &A, 1 ) <= 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002782
2783 /*
2784 * A = A^R mod |X|
2785 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &A, &A, &R, X, &RR ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788 if( mbedtls_mpi_cmp_mpi( &A, &W ) == 0 ||
2789 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002790 continue;
2791
2792 j = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793 while( j < s && mbedtls_mpi_cmp_mpi( &A, &W ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002794 {
2795 /*
2796 * A = A * A mod |X|
2797 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &A, &A ) );
2799 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &A, &T, X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801 if( mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002802 break;
2803
2804 j++;
2805 }
2806
2807 /*
2808 * not prime if A != |X| - 1 or A == 1
2809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810 if( mbedtls_mpi_cmp_mpi( &A, &W ) != 0 ||
2811 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002814 break;
2815 }
2816 }
2817
2818cleanup:
Hanno Becker73d7d792018-12-11 10:35:51 +00002819 mbedtls_mpi_free( &W ); mbedtls_mpi_free( &R );
2820 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002822
2823 return( ret );
2824}
2825
2826/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002827 * Pseudo-primality test: small factors, then Miller-Rabin
2828 */
Janos Follatha0b67c22018-09-18 14:48:23 +01002829int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds,
2830 int (*f_rng)(void *, unsigned char *, size_t),
2831 void *p_rng )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002832{
Janos Follath24eed8d2019-11-22 13:21:35 +00002833 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002834 mbedtls_mpi XX;
Hanno Becker8ce11a32018-12-19 16:18:52 +00002835 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002836 MPI_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002837
2838 XX.s = 1;
2839 XX.n = X->n;
2840 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842 if( mbedtls_mpi_cmp_int( &XX, 0 ) == 0 ||
2843 mbedtls_mpi_cmp_int( &XX, 1 ) == 0 )
2844 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846 if( mbedtls_mpi_cmp_int( &XX, 2 ) == 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002847 return( 0 );
2848
2849 if( ( ret = mpi_check_small_factors( &XX ) ) != 0 )
2850 {
2851 if( ret == 1 )
2852 return( 0 );
2853
2854 return( ret );
2855 }
2856
Janos Follathda31fa12018-09-03 14:45:23 +01002857 return( mpi_miller_rabin( &XX, rounds, f_rng, p_rng ) );
Janos Follathf301d232018-08-14 13:34:01 +01002858}
2859
Janos Follatha0b67c22018-09-18 14:48:23 +01002860#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Janos Follathf301d232018-08-14 13:34:01 +01002861/*
2862 * Pseudo-primality test, error probability 2^-80
2863 */
2864int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
2865 int (*f_rng)(void *, unsigned char *, size_t),
2866 void *p_rng )
2867{
Hanno Becker8ce11a32018-12-19 16:18:52 +00002868 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002869 MPI_VALIDATE_RET( f_rng != NULL );
2870
Janos Follatha0b67c22018-09-18 14:48:23 +01002871 /*
2872 * In the past our key generation aimed for an error rate of at most
2873 * 2^-80. Since this function is deprecated, aim for the same certainty
2874 * here as well.
2875 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002876 return( mbedtls_mpi_is_prime_ext( X, 40, f_rng, p_rng ) );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002877}
Janos Follatha0b67c22018-09-18 14:48:23 +01002878#endif
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002879
2880/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002881 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002882 *
Janos Follathf301d232018-08-14 13:34:01 +01002883 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2884 * be either 1024 bits or 1536 bits long, and flags must contain
2885 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002886 */
Janos Follath7c025a92018-08-14 11:08:41 +01002887int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002888 int (*f_rng)(void *, unsigned char *, size_t),
2889 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002890{
Jethro Beekman66689272018-02-14 19:24:10 -08002891#ifdef MBEDTLS_HAVE_INT64
2892// ceil(2^63.5)
2893#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2894#else
2895// ceil(2^31.5)
2896#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2897#endif
2898 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002899 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002900 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 mbedtls_mpi_uint r;
2902 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002903
Hanno Becker8ce11a32018-12-19 16:18:52 +00002904 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002905 MPI_VALIDATE_RET( f_rng != NULL );
2906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 if( nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS )
2908 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910 mbedtls_mpi_init( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002911
2912 n = BITS_TO_LIMBS( nbits );
2913
Janos Follathda31fa12018-09-03 14:45:23 +01002914 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR ) == 0 )
2915 {
2916 /*
2917 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2918 */
2919 rounds = ( ( nbits >= 1300 ) ? 2 : ( nbits >= 850 ) ? 3 :
2920 ( nbits >= 650 ) ? 4 : ( nbits >= 350 ) ? 8 :
2921 ( nbits >= 250 ) ? 12 : ( nbits >= 150 ) ? 18 : 27 );
2922 }
2923 else
2924 {
2925 /*
2926 * 2^-100 error probability, number of rounds computed based on HAC,
2927 * fact 4.48
2928 */
2929 rounds = ( ( nbits >= 1450 ) ? 4 : ( nbits >= 1150 ) ? 5 :
2930 ( nbits >= 1000 ) ? 6 : ( nbits >= 850 ) ? 7 :
2931 ( nbits >= 750 ) ? 8 : ( nbits >= 500 ) ? 13 :
2932 ( nbits >= 250 ) ? 28 : ( nbits >= 150 ) ? 40 : 51 );
2933 }
2934
Jethro Beekman66689272018-02-14 19:24:10 -08002935 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002936 {
Jethro Beekman66689272018-02-14 19:24:10 -08002937 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
2938 /* make sure generated number is at least (nbits-1)+0.5 bits (FIPS 186-4 §B.3.3 steps 4.4, 5.5) */
2939 if( X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2 ) continue;
2940
2941 k = n * biL;
2942 if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits ) );
2943 X->p[0] |= 1;
2944
Janos Follath7c025a92018-08-14 11:08:41 +01002945 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002946 {
Janos Follatha0b67c22018-09-18 14:48:23 +01002947 ret = mbedtls_mpi_is_prime_ext( X, rounds, f_rng, p_rng );
Jethro Beekman66689272018-02-14 19:24:10 -08002948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002950 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002951 }
Jethro Beekman66689272018-02-14 19:24:10 -08002952 else
Paul Bakker5121ce52009-01-03 21:22:43 +00002953 {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002954 /*
Jethro Beekman66689272018-02-14 19:24:10 -08002955 * An necessary condition for Y and X = 2Y + 1 to be prime
2956 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2957 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002958 */
Jethro Beekman66689272018-02-14 19:24:10 -08002959
2960 X->p[0] |= 2;
2961
2962 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, 3 ) );
2963 if( r == 0 )
2964 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 8 ) );
2965 else if( r == 1 )
2966 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 4 ) );
2967
2968 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
2969 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, X ) );
2970 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, 1 ) );
2971
2972 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002973 {
Jethro Beekman66689272018-02-14 19:24:10 -08002974 /*
2975 * First, check small factors for X and Y
2976 * before doing Miller-Rabin on any of them
2977 */
2978 if( ( ret = mpi_check_small_factors( X ) ) == 0 &&
2979 ( ret = mpi_check_small_factors( &Y ) ) == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002980 ( ret = mpi_miller_rabin( X, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002981 == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002982 ( ret = mpi_miller_rabin( &Y, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002983 == 0 )
Jethro Beekman66689272018-02-14 19:24:10 -08002984 goto cleanup;
2985
2986 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
2987 goto cleanup;
2988
2989 /*
2990 * Next candidates. We want to preserve Y = (X-1) / 2 and
2991 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2992 * so up Y by 6 and X by 12.
2993 */
2994 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 12 ) );
2995 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &Y, &Y, 6 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002996 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002997 }
2998 }
2999
3000cleanup:
3001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002 mbedtls_mpi_free( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00003003
3004 return( ret );
3005}
3006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00003008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003010
Paul Bakker23986e52011-04-24 08:57:21 +00003011#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003012
3013static const int gcd_pairs[GCD_PAIR_COUNT][3] =
3014{
3015 { 693, 609, 21 },
3016 { 1764, 868, 28 },
3017 { 768454923, 542167814, 1 }
3018};
3019
Paul Bakker5121ce52009-01-03 21:22:43 +00003020/*
3021 * Checkup routine
3022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003023int mbedtls_mpi_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00003024{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003025 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00003027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003028 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); mbedtls_mpi_init( &X );
3029 mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &U ); mbedtls_mpi_init( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00003030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &A, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003032 "EFE021C2645FD1DC586E69184AF4A31E" \
3033 "D5F53E93B5F123FA41680867BA110131" \
3034 "944FE7952E2517337780CB0DB80E61AA" \
3035 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6" ) );
3036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003038 "B2E7EFD37075B9F03FF989C7C5051C20" \
3039 "34D2A323810251127E7BF8625A4F49A5" \
3040 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
3041 "5B5C25763222FEFCCFC38B832366C29E" ) );
3042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &N, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003044 "0066A198186C18C10B2F5ED9B522752A" \
3045 "9830B69916E535C8F047518A889A43A5" \
3046 "94B6BED27A168D31D4A52F88925AA8F5" ) );
3047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003050 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003051 "602AB7ECA597A3D6B56FF9829A5E8B85" \
3052 "9E857EA95A03512E2BAE7391688D264A" \
3053 "A5663B0341DB9CCFD2C4C5F421FEC814" \
3054 "8001B72E848A38CAE1C65F78E56ABDEF" \
3055 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
3056 "ECF677152EF804370C1A305CAF3B5BF1" \
3057 "30879B56C61DE584A0F53A2447A51E" ) );
3058
3059 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060 mbedtls_printf( " MPI test #1 (mul_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003063 {
3064 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003066
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003067 ret = 1;
3068 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003069 }
3070
3071 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003074 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &X, &Y, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003077 "256567336059E52CAE22925474705F39A94" ) );
3078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &V, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003080 "6613F26162223DF488E9CD48CC132C7A" \
3081 "0AC93C701B001B092E4E5B9F73BCD27B" \
3082 "9EE50D0657C77F374E903CDFA4C642" ) );
3083
3084 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003085 mbedtls_printf( " MPI test #2 (div_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ||
3088 mbedtls_mpi_cmp_mpi( &Y, &V ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003089 {
3090 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003092
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003093 ret = 1;
3094 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003095 }
3096
3097 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003100 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &X, &A, &E, &N, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003103 "36E139AEA55215609D2816998ED020BB" \
3104 "BD96C37890F65171D948E9BC7CBAA4D9" \
3105 "325D24D6A3C12710F10A09FA08AB87" ) );
3106
3107 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003108 mbedtls_printf( " MPI test #3 (exp_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003111 {
3112 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003113 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003114
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003115 ret = 1;
3116 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003117 }
3118
3119 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003125 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
3126 "C3DBA76456363A10869622EAC2DD84EC" \
3127 "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) );
3128
3129 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003130 mbedtls_printf( " MPI test #4 (inv_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003133 {
3134 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003136
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003137 ret = 1;
3138 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003139 }
3140
3141 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003143
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003144 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145 mbedtls_printf( " MPI test #5 (simple gcd): " );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003146
Paul Bakker66d5d072014-06-17 16:39:18 +02003147 for( i = 0; i < GCD_PAIR_COUNT; i++ )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &X, gcd_pairs[i][0] ) );
3150 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Y, gcd_pairs[i][1] ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &A, &X, &Y ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 if( mbedtls_mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 )
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003155 {
3156 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 mbedtls_printf( "failed at %d\n", i );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003158
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003159 ret = 1;
3160 goto cleanup;
3161 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003162 }
3163
3164 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003165 mbedtls_printf( "passed\n" );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003166
Paul Bakker5121ce52009-01-03 21:22:43 +00003167cleanup:
3168
3169 if( ret != 0 && verbose != 0 )
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003170 mbedtls_printf( "Unexpected error, return code = %08X\n", (unsigned int) ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &X );
3173 mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &U ); mbedtls_mpi_free( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00003174
3175 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003177
3178 return( ret );
3179}
3180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003181#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003183#endif /* MBEDTLS_BIGNUM_C */