blob: acdccde11adf92062d8805f65137b15195805f12 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Multi-precision integer library
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
Simon Butcher15b15d12015-11-26 19:35:03 +000019
Paul Bakker5121ce52009-01-03 21:22:43 +000020/*
Simon Butcher15b15d12015-11-26 19:35:03 +000021 * The following sources were referenced in the design of this Multi-precision
22 * Integer library:
Paul Bakker5121ce52009-01-03 21:22:43 +000023 *
Simon Butcher15b15d12015-11-26 19:35:03 +000024 * [1] Handbook of Applied Cryptography - 1997
25 * Menezes, van Oorschot and Vanstone
26 *
27 * [2] Multi-Precision Math
28 * Tom St Denis
29 * https://github.com/libtom/libtommath/blob/develop/tommath.pdf
30 *
31 * [3] GNU Multi-Precision Arithmetic Library
32 * https://gmplib.org/manual/index.html
33 *
Simon Butcherf5ba0452015-12-27 23:01:55 +000034 */
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Gilles Peskinedb09ef62020-06-03 01:43:33 +020036#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/bignum.h"
Chris Jones4c5819c2021-03-03 17:45:34 +000041#include "bn_mul.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050042#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000043#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Rich Evans00ab4702015-02-06 13:43:58 +000045#include <string.h>
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020049#else
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <stdio.h>
51#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#define mbedtls_printf printf
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020053#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020055#endif
56
Hanno Becker73d7d792018-12-11 10:35:51 +000057#define MPI_VALIDATE_RET( cond ) \
58 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA )
59#define MPI_VALIDATE( cond ) \
60 MBEDTLS_INTERNAL_VALIDATE( cond )
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
Paul Bakker5121ce52009-01-03 21:22:43 +000063#define biL (ciL << 3) /* bits in limb */
64#define biH (ciL << 2) /* half limb size */
65
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +010066#define MPI_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
67
Paul Bakker5121ce52009-01-03 21:22:43 +000068/*
69 * Convert between bits/chars and number of limbs
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +020070 * Divide first in order to avoid potential overflows
Paul Bakker5121ce52009-01-03 21:22:43 +000071 */
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +020072#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
73#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050075/* Implementation that should never be optimized out by the compiler */
Andres Amaya Garcia6698d2f2018-04-24 08:39:07 -050076static void mbedtls_mpi_zeroize( mbedtls_mpi_uint *v, size_t n )
77{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050078 mbedtls_platform_zeroize( v, ciL * n );
79}
80
Paul Bakker5121ce52009-01-03 21:22:43 +000081/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000082 * Initialize one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084void mbedtls_mpi_init( mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +000085{
Hanno Becker73d7d792018-12-11 10:35:51 +000086 MPI_VALIDATE( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +000087
Paul Bakker6c591fa2011-05-05 11:49:20 +000088 X->s = 1;
89 X->n = 0;
90 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000091}
92
93/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000094 * Unallocate one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096void mbedtls_mpi_free( mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +000097{
Paul Bakker6c591fa2011-05-05 11:49:20 +000098 if( X == NULL )
99 return;
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Paul Bakker6c591fa2011-05-05 11:49:20 +0000101 if( X->p != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 {
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200103 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 }
106
Paul Bakker6c591fa2011-05-05 11:49:20 +0000107 X->s = 1;
108 X->n = 0;
109 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000110}
111
112/*
113 * Enlarge to the specified number of limbs
114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000116{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_mpi_uint *p;
Hanno Becker73d7d792018-12-11 10:35:51 +0000118 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200121 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Paul Bakkerf9688572011-05-05 10:00:45 +0000122
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 if( X->n < nblimbs )
124 {
Simon Butcher29176892016-05-20 00:19:09 +0100125 if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( nblimbs, ciL ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200126 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 if( X->p != NULL )
129 {
130 memcpy( p, X->p, X->n * ciL );
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200131 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 }
134
135 X->n = nblimbs;
136 X->p = p;
137 }
138
139 return( 0 );
140}
141
142/*
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100143 * Resize down as much as possible,
144 * while keeping at least the specified number of limbs
145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100147{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_mpi_uint *p;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100149 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000150 MPI_VALIDATE_RET( X != NULL );
151
152 if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
153 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100154
Gilles Peskinee2f563e2020-01-20 21:17:43 +0100155 /* Actually resize up if there are currently fewer than nblimbs limbs. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100156 if( X->n <= nblimbs )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 return( mbedtls_mpi_grow( X, nblimbs ) );
Gilles Peskine322752b2020-01-21 13:59:51 +0100158 /* After this point, then X->n > nblimbs and in particular X->n > 0. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100159
160 for( i = X->n - 1; i > 0; i-- )
161 if( X->p[i] != 0 )
162 break;
163 i++;
164
165 if( i < nblimbs )
166 i = nblimbs;
167
Simon Butcher29176892016-05-20 00:19:09 +0100168 if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( i, ciL ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200169 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100170
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100171 if( X->p != NULL )
172 {
173 memcpy( p, X->p, i * ciL );
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200174 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_free( X->p );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100176 }
177
178 X->n = i;
179 X->p = p;
180
181 return( 0 );
182}
183
Gilles Peskineed32b572021-06-02 22:17:52 +0200184/* Resize X to have exactly n limbs and set it to 0. */
185static int mbedtls_mpi_resize_clear( mbedtls_mpi *X, size_t limbs )
186{
187 if( limbs == 0 )
188 {
189 mbedtls_mpi_free( X );
190 return( 0 );
191 }
192 else if( X->n == limbs )
193 {
194 memset( X->p, 0, limbs * ciL );
195 X->s = 1;
196 return( 0 );
197 }
198 else
199 {
200 mbedtls_mpi_free( X );
201 return( mbedtls_mpi_grow( X, limbs ) );
202 }
203}
204
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100205/*
Gilles Peskine3da1a8f2021-06-08 23:17:42 +0200206 * Copy the contents of Y into X.
207 *
208 * This function is not constant-time. Leading zeros in Y may be removed.
209 *
210 * Ensure that X does not shrink. This is not guaranteed by the public API,
211 * but some code in the bignum module relies on this property, for example
212 * in mbedtls_mpi_exp_mod().
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000215{
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100216 int ret = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000217 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000218 MPI_VALIDATE_RET( X != NULL );
219 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
221 if( X == Y )
222 return( 0 );
223
Gilles Peskinedb420622020-01-20 21:12:50 +0100224 if( Y->n == 0 )
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200225 {
Gilles Peskine3da1a8f2021-06-08 23:17:42 +0200226 if( X->n != 0 )
227 {
228 X->s = 1;
229 memset( X->p, 0, X->n * ciL );
230 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200231 return( 0 );
232 }
233
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 for( i = Y->n - 1; i > 0; i-- )
235 if( Y->p[i] != 0 )
236 break;
237 i++;
238
239 X->s = Y->s;
240
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100241 if( X->n < i )
242 {
243 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i ) );
244 }
245 else
246 {
247 memset( X->p + i, 0, ( X->n - i ) * ciL );
248 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000249
Paul Bakker5121ce52009-01-03 21:22:43 +0000250 memcpy( X->p, Y->p, i * ciL );
251
252cleanup:
253
254 return( ret );
255}
256
257/*
258 * Swap the contents of X and Y
259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000261{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000263 MPI_VALIDATE( X != NULL );
264 MPI_VALIDATE( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 memcpy( &T, X, sizeof( mbedtls_mpi ) );
267 memcpy( X, Y, sizeof( mbedtls_mpi ) );
268 memcpy( Y, &T, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000269}
270
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 Peskine80f56732021-04-03 18:26:13 +0200515 if( s[0] == '-' )
516 {
517 ++s;
518 sign = -1;
519 }
520
Paul Bakkerff60ee62010-03-16 21:09:09 +0000521 slen = strlen( s );
522
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 if( radix == 16 )
524 {
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +0100525 if( slen > MPI_SIZE_T_MAX >> 2 )
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +0200526 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
527
Paul Bakkerff60ee62010-03-16 21:09:09 +0000528 n = BITS_TO_LIMBS( slen << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n ) );
531 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000532
Paul Bakker23986e52011-04-24 08:57:21 +0000533 for( i = slen, j = 0; i > 0; i--, j++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200536 X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 }
538 }
539 else
540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000542
Paul Bakkerff60ee62010-03-16 21:09:09 +0000543 for( i = 0; i < slen; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
546 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T, X, radix ) );
Gilles Peskine80f56732021-04-03 18:26:13 +0200547 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, &T, d ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000548 }
549 }
550
Gilles Peskine80f56732021-04-03 18:26:13 +0200551 if( sign < 0 && mbedtls_mpi_bitlen( X ) != 0 )
552 X->s = -1;
553
Paul Bakker5121ce52009-01-03 21:22:43 +0000554cleanup:
555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
558 return( ret );
559}
560
561/*
Ron Eldora16fa292018-11-20 14:07:01 +0200562 * Helper to write the digits high-order first.
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 */
Ron Eldora16fa292018-11-20 14:07:01 +0200564static int mpi_write_hlp( mbedtls_mpi *X, int radix,
565 char **p, const size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000566{
Janos Follath24eed8d2019-11-22 13:21:35 +0000567 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_mpi_uint r;
Ron Eldora16fa292018-11-20 14:07:01 +0200569 size_t length = 0;
570 char *p_end = *p + buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
Ron Eldora16fa292018-11-20 14:07:01 +0200572 do
573 {
574 if( length >= buflen )
575 {
576 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
577 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Ron Eldora16fa292018-11-20 14:07:01 +0200579 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) );
580 MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) );
581 /*
582 * Write the residue in the current position, as an ASCII character.
583 */
584 if( r < 0xA )
585 *(--p_end) = (char)( '0' + r );
586 else
587 *(--p_end) = (char)( 'A' + ( r - 0xA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000588
Ron Eldora16fa292018-11-20 14:07:01 +0200589 length++;
590 } while( mbedtls_mpi_cmp_int( X, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
Ron Eldora16fa292018-11-20 14:07:01 +0200592 memmove( *p, p_end, length );
593 *p += length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
595cleanup:
596
597 return( ret );
598}
599
600/*
601 * Export into an ASCII string
602 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100603int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
604 char *buf, size_t buflen, size_t *olen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000605{
Paul Bakker23986e52011-04-24 08:57:21 +0000606 int ret = 0;
607 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000608 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000610 MPI_VALIDATE_RET( X != NULL );
611 MPI_VALIDATE_RET( olen != NULL );
612 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000613
614 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000615 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000616
Hanno Becker23cfea02019-02-04 09:45:07 +0000617 n = mbedtls_mpi_bitlen( X ); /* Number of bits necessary to present `n`. */
618 if( radix >= 4 ) n >>= 1; /* Number of 4-adic digits necessary to present
619 * `n`. If radix > 4, this might be a strict
620 * overapproximation of the number of
621 * radix-adic digits needed to present `n`. */
622 if( radix >= 16 ) n >>= 1; /* Number of hexadecimal digits necessary to
623 * present `n`. */
624
Janos Follath80470622019-03-06 13:43:02 +0000625 n += 1; /* Terminating null byte */
Hanno Becker23cfea02019-02-04 09:45:07 +0000626 n += 1; /* Compensate for the divisions above, which round down `n`
627 * in case it's not even. */
628 n += 1; /* Potential '-'-sign. */
629 n += ( n & 1 ); /* Make n even to have enough space for hexadecimal writing,
630 * which always uses an even number of hex-digits. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000631
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100632 if( buflen < n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000633 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100634 *olen = n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000636 }
637
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100638 p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000640
641 if( X->s == -1 )
Hanno Beckerc983c812019-02-01 16:41:30 +0000642 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000643 *p++ = '-';
Hanno Beckerc983c812019-02-01 16:41:30 +0000644 buflen--;
645 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
647 if( radix == 16 )
648 {
Paul Bakker23986e52011-04-24 08:57:21 +0000649 int c;
650 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000651
Paul Bakker23986e52011-04-24 08:57:21 +0000652 for( i = X->n, k = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000653 {
Paul Bakker23986e52011-04-24 08:57:21 +0000654 for( j = ciL; j > 0; j-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 {
Paul Bakker23986e52011-04-24 08:57:21 +0000656 c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000657
Paul Bakker6c343d72014-07-10 14:36:19 +0200658 if( c == 0 && k == 0 && ( i + j ) != 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000659 continue;
660
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000661 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000662 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 k = 1;
664 }
665 }
666 }
667 else
668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T, X ) );
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000670
671 if( T.s == -1 )
672 T.s = 1;
673
Ron Eldora16fa292018-11-20 14:07:01 +0200674 MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p, buflen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000675 }
676
677 *p++ = '\0';
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100678 *olen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000679
680cleanup:
681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
684 return( ret );
685}
686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000688/*
689 * Read X from an opened file
690 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin )
Paul Bakker5121ce52009-01-03 21:22:43 +0000692{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_mpi_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000694 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000695 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000696 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000697 * Buffer should have space for (short) label and decimal formatted MPI,
698 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Paul Bakker5121ce52009-01-03 21:22:43 +0000701
Hanno Becker73d7d792018-12-11 10:35:51 +0000702 MPI_VALIDATE_RET( X != NULL );
703 MPI_VALIDATE_RET( fin != NULL );
704
705 if( radix < 2 || radix > 16 )
706 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
707
Paul Bakker5121ce52009-01-03 21:22:43 +0000708 memset( s, 0, sizeof( s ) );
709 if( fgets( s, sizeof( s ) - 1, fin ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000711
712 slen = strlen( s );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000713 if( slen == sizeof( s ) - 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000715
Hanno Beckerb2034b72017-04-26 11:46:46 +0100716 if( slen > 0 && s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
717 if( slen > 0 && s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
Paul Bakker5121ce52009-01-03 21:22:43 +0000718
719 p = s + slen;
Hanno Beckerb2034b72017-04-26 11:46:46 +0100720 while( p-- > s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000721 if( mpi_get_digit( &d, radix, *p ) != 0 )
722 break;
723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 return( mbedtls_mpi_read_string( X, radix, p + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000725}
726
727/*
728 * Write X into an opened file (or stdout if fout == NULL)
729 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout )
Paul Bakker5121ce52009-01-03 21:22:43 +0000731{
Janos Follath24eed8d2019-11-22 13:21:35 +0000732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000733 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000734 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000735 * Buffer should have space for (short) label and decimal formatted MPI,
736 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Hanno Becker73d7d792018-12-11 10:35:51 +0000739 MPI_VALIDATE_RET( X != NULL );
740
741 if( radix < 2 || radix > 16 )
742 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000743
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100744 memset( s, 0, sizeof( s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100746 MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, sizeof( s ) - 2, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000747
748 if( p == NULL ) p = "";
749
750 plen = strlen( p );
751 slen = strlen( s );
752 s[slen++] = '\r';
753 s[slen++] = '\n';
754
755 if( fout != NULL )
756 {
757 if( fwrite( p, 1, plen, fout ) != plen ||
758 fwrite( s, 1, slen, fout ) != slen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000760 }
761 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_printf( "%s%s", p, s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000763
764cleanup:
765
766 return( ret );
767}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
Hanno Beckerda1655a2017-10-18 14:21:44 +0100770
771/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
772 * into the storage form used by mbedtls_mpi. */
Hanno Beckerf8720072018-11-08 11:53:49 +0000773
774static mbedtls_mpi_uint mpi_uint_bigendian_to_host_c( mbedtls_mpi_uint x )
775{
776 uint8_t i;
Hanno Becker031d6332019-05-01 17:09:11 +0100777 unsigned char *x_ptr;
Hanno Beckerf8720072018-11-08 11:53:49 +0000778 mbedtls_mpi_uint tmp = 0;
Hanno Becker031d6332019-05-01 17:09:11 +0100779
780 for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
781 {
782 tmp <<= CHAR_BIT;
783 tmp |= (mbedtls_mpi_uint) *x_ptr;
784 }
785
Hanno Beckerf8720072018-11-08 11:53:49 +0000786 return( tmp );
787}
788
789static mbedtls_mpi_uint mpi_uint_bigendian_to_host( mbedtls_mpi_uint x )
790{
791#if defined(__BYTE_ORDER__)
792
793/* Nothing to do on bigendian systems. */
794#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
795 return( x );
796#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
797
798#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
799
800/* For GCC and Clang, have builtins for byte swapping. */
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000801#if defined(__GNUC__) && defined(__GNUC_PREREQ)
802#if __GNUC_PREREQ(4,3)
Hanno Beckerf8720072018-11-08 11:53:49 +0000803#define have_bswap
804#endif
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000805#endif
806
807#if defined(__clang__) && defined(__has_builtin)
808#if __has_builtin(__builtin_bswap32) && \
809 __has_builtin(__builtin_bswap64)
810#define have_bswap
811#endif
812#endif
813
Hanno Beckerf8720072018-11-08 11:53:49 +0000814#if defined(have_bswap)
815 /* The compiler is hopefully able to statically evaluate this! */
816 switch( sizeof(mbedtls_mpi_uint) )
817 {
818 case 4:
819 return( __builtin_bswap32(x) );
820 case 8:
821 return( __builtin_bswap64(x) );
822 }
823#endif
824#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
825#endif /* __BYTE_ORDER__ */
826
827 /* Fall back to C-based reordering if we don't know the byte order
828 * or we couldn't use a compiler-specific builtin. */
829 return( mpi_uint_bigendian_to_host_c( x ) );
830}
831
Hanno Becker2be8a552018-10-25 12:40:09 +0100832static void mpi_bigendian_to_host( mbedtls_mpi_uint * const p, size_t limbs )
Hanno Beckerda1655a2017-10-18 14:21:44 +0100833{
Hanno Beckerda1655a2017-10-18 14:21:44 +0100834 mbedtls_mpi_uint *cur_limb_left;
835 mbedtls_mpi_uint *cur_limb_right;
Hanno Becker2be8a552018-10-25 12:40:09 +0100836 if( limbs == 0 )
837 return;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100838
839 /*
840 * Traverse limbs and
841 * - adapt byte-order in each limb
842 * - swap the limbs themselves.
843 * For that, simultaneously traverse the limbs from left to right
844 * and from right to left, as long as the left index is not bigger
845 * than the right index (it's not a problem if limbs is odd and the
846 * indices coincide in the last iteration).
847 */
Hanno Beckerda1655a2017-10-18 14:21:44 +0100848 for( cur_limb_left = p, cur_limb_right = p + ( limbs - 1 );
849 cur_limb_left <= cur_limb_right;
850 cur_limb_left++, cur_limb_right-- )
851 {
Hanno Beckerf8720072018-11-08 11:53:49 +0000852 mbedtls_mpi_uint tmp;
853 /* Note that if cur_limb_left == cur_limb_right,
854 * this code effectively swaps the bytes only once. */
855 tmp = mpi_uint_bigendian_to_host( *cur_limb_left );
856 *cur_limb_left = mpi_uint_bigendian_to_host( *cur_limb_right );
857 *cur_limb_right = tmp;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100858 }
Hanno Beckerda1655a2017-10-18 14:21:44 +0100859}
860
Paul Bakker5121ce52009-01-03 21:22:43 +0000861/*
Janos Follatha778a942019-02-13 10:28:28 +0000862 * Import X from unsigned binary data, little endian
863 */
864int mbedtls_mpi_read_binary_le( mbedtls_mpi *X,
865 const unsigned char *buf, size_t buflen )
866{
Janos Follath24eed8d2019-11-22 13:21:35 +0000867 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follatha778a942019-02-13 10:28:28 +0000868 size_t i;
869 size_t const limbs = CHARS_TO_LIMBS( buflen );
870
871 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskineed32b572021-06-02 22:17:52 +0200872 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Janos Follatha778a942019-02-13 10:28:28 +0000873
874 for( i = 0; i < buflen; i++ )
875 X->p[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
876
877cleanup:
878
Janos Follath171a7ef2019-02-15 16:17:45 +0000879 /*
880 * This function is also used to import keys. However, wiping the buffers
881 * upon failure is not necessary because failure only can happen before any
882 * input is copied.
883 */
Janos Follatha778a942019-02-13 10:28:28 +0000884 return( ret );
885}
886
887/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000888 * Import X from unsigned binary data, big endian
889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000891{
Janos Follath24eed8d2019-11-22 13:21:35 +0000892 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100893 size_t const limbs = CHARS_TO_LIMBS( buflen );
894 size_t const overhead = ( limbs * ciL ) - buflen;
895 unsigned char *Xp;
Paul Bakker5121ce52009-01-03 21:22:43 +0000896
Hanno Becker8ce11a32018-12-19 16:18:52 +0000897 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +0000898 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
899
Hanno Becker073c1992017-10-17 15:17:27 +0100900 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskineed32b572021-06-02 22:17:52 +0200901 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000902
Gilles Peskineed32b572021-06-02 22:17:52 +0200903 /* Avoid calling `memcpy` with NULL source or destination argument,
Hanno Becker0e810b92019-01-03 17:13:11 +0000904 * even if buflen is 0. */
Gilles Peskineed32b572021-06-02 22:17:52 +0200905 if( buflen != 0 )
Hanno Becker0e810b92019-01-03 17:13:11 +0000906 {
907 Xp = (unsigned char*) X->p;
908 memcpy( Xp + overhead, buf, buflen );
Hanno Beckerda1655a2017-10-18 14:21:44 +0100909
Hanno Becker0e810b92019-01-03 17:13:11 +0000910 mpi_bigendian_to_host( X->p, limbs );
911 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000912
913cleanup:
914
Janos Follath171a7ef2019-02-15 16:17:45 +0000915 /*
916 * This function is also used to import keys. However, wiping the buffers
917 * upon failure is not necessary because failure only can happen before any
918 * input is copied.
919 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000920 return( ret );
921}
922
923/*
Janos Follathe344d0f2019-02-19 16:17:40 +0000924 * Export X into unsigned binary data, little endian
925 */
926int mbedtls_mpi_write_binary_le( const mbedtls_mpi *X,
927 unsigned char *buf, size_t buflen )
928{
929 size_t stored_bytes = X->n * ciL;
930 size_t bytes_to_copy;
931 size_t i;
932
933 if( stored_bytes < buflen )
934 {
935 bytes_to_copy = stored_bytes;
936 }
937 else
938 {
939 bytes_to_copy = buflen;
940
941 /* The output buffer is smaller than the allocated size of X.
942 * However X may fit if its leading bytes are zero. */
943 for( i = bytes_to_copy; i < stored_bytes; i++ )
944 {
945 if( GET_BYTE( X, i ) != 0 )
946 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
947 }
948 }
949
950 for( i = 0; i < bytes_to_copy; i++ )
951 buf[i] = GET_BYTE( X, i );
952
953 if( stored_bytes < buflen )
954 {
955 /* Write trailing 0 bytes */
956 memset( buf + stored_bytes, 0, buflen - stored_bytes );
957 }
958
959 return( 0 );
960}
961
962/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000963 * Export X into unsigned binary data, big endian
964 */
Gilles Peskine11cdb052018-11-20 16:47:47 +0100965int mbedtls_mpi_write_binary( const mbedtls_mpi *X,
966 unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000967{
Hanno Becker73d7d792018-12-11 10:35:51 +0000968 size_t stored_bytes;
Gilles Peskine11cdb052018-11-20 16:47:47 +0100969 size_t bytes_to_copy;
970 unsigned char *p;
971 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000972
Hanno Becker73d7d792018-12-11 10:35:51 +0000973 MPI_VALIDATE_RET( X != NULL );
974 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
975
976 stored_bytes = X->n * ciL;
977
Gilles Peskine11cdb052018-11-20 16:47:47 +0100978 if( stored_bytes < buflen )
979 {
980 /* There is enough space in the output buffer. Write initial
981 * null bytes and record the position at which to start
982 * writing the significant bytes. In this case, the execution
983 * trace of this function does not depend on the value of the
984 * number. */
985 bytes_to_copy = stored_bytes;
986 p = buf + buflen - stored_bytes;
987 memset( buf, 0, buflen - stored_bytes );
988 }
989 else
990 {
991 /* The output buffer is smaller than the allocated size of X.
992 * However X may fit if its leading bytes are zero. */
993 bytes_to_copy = buflen;
994 p = buf;
995 for( i = bytes_to_copy; i < stored_bytes; i++ )
996 {
997 if( GET_BYTE( X, i ) != 0 )
998 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
999 }
1000 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
Gilles Peskine11cdb052018-11-20 16:47:47 +01001002 for( i = 0; i < bytes_to_copy; i++ )
1003 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
Paul Bakker5121ce52009-01-03 21:22:43 +00001004
1005 return( 0 );
1006}
1007
1008/*
1009 * Left-shift: X <<= count
1010 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +00001012{
Janos Follath24eed8d2019-11-22 13:21:35 +00001013 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001014 size_t i, v0, t1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +00001016 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
1018 v0 = count / (biL );
1019 t1 = count & (biL - 1);
1020
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001021 i = mbedtls_mpi_bitlen( X ) + count;
Paul Bakker5121ce52009-01-03 21:22:43 +00001022
Paul Bakkerf9688572011-05-05 10:00:45 +00001023 if( X->n * biL < i )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001025
1026 ret = 0;
1027
1028 /*
1029 * shift by count / limb_size
1030 */
1031 if( v0 > 0 )
1032 {
Paul Bakker23986e52011-04-24 08:57:21 +00001033 for( i = X->n; i > v0; i-- )
1034 X->p[i - 1] = X->p[i - v0 - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001035
Paul Bakker23986e52011-04-24 08:57:21 +00001036 for( ; i > 0; i-- )
1037 X->p[i - 1] = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001038 }
1039
1040 /*
1041 * shift by count % limb_size
1042 */
1043 if( t1 > 0 )
1044 {
1045 for( i = v0; i < X->n; i++ )
1046 {
1047 r1 = X->p[i] >> (biL - t1);
1048 X->p[i] <<= t1;
1049 X->p[i] |= r0;
1050 r0 = r1;
1051 }
1052 }
1053
1054cleanup:
1055
1056 return( ret );
1057}
1058
1059/*
1060 * Right-shift: X >>= count
1061 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +00001063{
Paul Bakker23986e52011-04-24 08:57:21 +00001064 size_t i, v0, v1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +00001066 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001067
1068 v0 = count / biL;
1069 v1 = count & (biL - 1);
1070
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001071 if( v0 > X->n || ( v0 == X->n && v1 > 0 ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 return mbedtls_mpi_lset( X, 0 );
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001073
Paul Bakker5121ce52009-01-03 21:22:43 +00001074 /*
1075 * shift by count / limb_size
1076 */
1077 if( v0 > 0 )
1078 {
1079 for( i = 0; i < X->n - v0; i++ )
1080 X->p[i] = X->p[i + v0];
1081
1082 for( ; i < X->n; i++ )
1083 X->p[i] = 0;
1084 }
1085
1086 /*
1087 * shift by count % limb_size
1088 */
1089 if( v1 > 0 )
1090 {
Paul Bakker23986e52011-04-24 08:57:21 +00001091 for( i = X->n; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001092 {
Paul Bakker23986e52011-04-24 08:57:21 +00001093 r1 = X->p[i - 1] << (biL - v1);
1094 X->p[i - 1] >>= v1;
1095 X->p[i - 1] |= r0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001096 r0 = r1;
1097 }
1098 }
1099
1100 return( 0 );
1101}
1102
1103/*
1104 * Compare unsigned values
1105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001107{
Paul Bakker23986e52011-04-24 08:57:21 +00001108 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001109 MPI_VALIDATE_RET( X != NULL );
1110 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001111
Paul Bakker23986e52011-04-24 08:57:21 +00001112 for( i = X->n; i > 0; i-- )
1113 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001114 break;
1115
Paul Bakker23986e52011-04-24 08:57:21 +00001116 for( j = Y->n; j > 0; j-- )
1117 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001118 break;
1119
Paul Bakker23986e52011-04-24 08:57:21 +00001120 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001121 return( 0 );
1122
1123 if( i > j ) return( 1 );
1124 if( j > i ) return( -1 );
1125
Paul Bakker23986e52011-04-24 08:57:21 +00001126 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001127 {
Paul Bakker23986e52011-04-24 08:57:21 +00001128 if( X->p[i - 1] > Y->p[i - 1] ) return( 1 );
1129 if( X->p[i - 1] < Y->p[i - 1] ) return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 }
1131
1132 return( 0 );
1133}
1134
1135/*
1136 * Compare signed values
1137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001139{
Paul Bakker23986e52011-04-24 08:57:21 +00001140 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001141 MPI_VALIDATE_RET( X != NULL );
1142 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001143
Paul Bakker23986e52011-04-24 08:57:21 +00001144 for( i = X->n; i > 0; i-- )
1145 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001146 break;
1147
Paul Bakker23986e52011-04-24 08:57:21 +00001148 for( j = Y->n; j > 0; j-- )
1149 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001150 break;
1151
Paul Bakker23986e52011-04-24 08:57:21 +00001152 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001153 return( 0 );
1154
1155 if( i > j ) return( X->s );
Paul Bakker0c8f73b2012-03-22 14:08:57 +00001156 if( j > i ) return( -Y->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001157
1158 if( X->s > 0 && Y->s < 0 ) return( 1 );
1159 if( Y->s > 0 && X->s < 0 ) return( -1 );
1160
Paul Bakker23986e52011-04-24 08:57:21 +00001161 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001162 {
Paul Bakker23986e52011-04-24 08:57:21 +00001163 if( X->p[i - 1] > Y->p[i - 1] ) return( X->s );
1164 if( X->p[i - 1] < Y->p[i - 1] ) return( -X->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001165 }
1166
1167 return( 0 );
1168}
1169
Janos Follath3f6f0e42019-10-14 09:09:32 +01001170/** Decide if an integer is less than the other, without branches.
1171 *
1172 * \param x First integer.
1173 * \param y Second integer.
1174 *
1175 * \return 1 if \p x is less than \p y, 0 otherwise
1176 */
Janos Follath0e5532d2019-10-11 14:21:53 +01001177static unsigned ct_lt_mpi_uint( const mbedtls_mpi_uint x,
1178 const mbedtls_mpi_uint y )
Janos Follathee6abce2019-09-05 14:47:19 +01001179{
1180 mbedtls_mpi_uint ret;
1181 mbedtls_mpi_uint cond;
1182
1183 /*
1184 * Check if the most significant bits (MSB) of the operands are different.
1185 */
1186 cond = ( x ^ y );
1187 /*
1188 * If the MSB are the same then the difference x-y will be negative (and
1189 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
1190 */
1191 ret = ( x - y ) & ~cond;
1192 /*
1193 * If the MSB are different, then the operand with the MSB of 1 is the
1194 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
1195 * the MSB of y is 0.)
1196 */
1197 ret |= y & cond;
1198
1199
Janos Follatha0f732b2019-10-14 08:59:14 +01001200 ret = ret >> ( biL - 1 );
Janos Follathee6abce2019-09-05 14:47:19 +01001201
Janos Follath67ce6472019-10-29 15:08:46 +00001202 return (unsigned) ret;
Janos Follathee6abce2019-09-05 14:47:19 +01001203}
1204
1205/*
1206 * Compare signed values in constant time
1207 */
Janos Follath0e5532d2019-10-11 14:21:53 +01001208int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y,
1209 unsigned *ret )
Janos Follathee6abce2019-09-05 14:47:19 +01001210{
1211 size_t i;
Janos Follathbb5147f2019-10-28 12:23:18 +00001212 /* The value of any of these variables is either 0 or 1 at all times. */
Janos Follath5e614ce2019-10-28 12:31:34 +00001213 unsigned cond, done, X_is_negative, Y_is_negative;
Janos Follathee6abce2019-09-05 14:47:19 +01001214
1215 MPI_VALIDATE_RET( X != NULL );
1216 MPI_VALIDATE_RET( Y != NULL );
1217 MPI_VALIDATE_RET( ret != NULL );
1218
1219 if( X->n != Y->n )
1220 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1221
1222 /*
Janos Follath73ba9ec2019-10-28 12:12:15 +00001223 * Set sign_N to 1 if N >= 0, 0 if N < 0.
1224 * We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0.
Janos Follathee6abce2019-09-05 14:47:19 +01001225 */
Janos Follath5e614ce2019-10-28 12:31:34 +00001226 X_is_negative = ( X->s & 2 ) >> 1;
1227 Y_is_negative = ( Y->s & 2 ) >> 1;
Janos Follath0e5532d2019-10-11 14:21:53 +01001228
1229 /*
1230 * If the signs are different, then the positive operand is the bigger.
Janos Follath5e614ce2019-10-28 12:31:34 +00001231 * That is if X is negative (X_is_negative == 1), then X < Y is true and it
1232 * is false if X is positive (X_is_negative == 0).
Janos Follath0e5532d2019-10-11 14:21:53 +01001233 */
Janos Follath5e614ce2019-10-28 12:31:34 +00001234 cond = ( X_is_negative ^ Y_is_negative );
1235 *ret = cond & X_is_negative;
Janos Follath0e5532d2019-10-11 14:21:53 +01001236
1237 /*
Janos Follathbb5147f2019-10-28 12:23:18 +00001238 * This is a constant-time function. We might have the result, but we still
Janos Follath0e5532d2019-10-11 14:21:53 +01001239 * need to go through the loop. Record if we have the result already.
1240 */
Janos Follathee6abce2019-09-05 14:47:19 +01001241 done = cond;
1242
1243 for( i = X->n; i > 0; i-- )
1244 {
1245 /*
Janos Follath30702422019-11-05 12:24:52 +00001246 * If Y->p[i - 1] < X->p[i - 1] then X < Y is true if and only if both
1247 * X and Y are negative.
Janos Follath0e5532d2019-10-11 14:21:53 +01001248 *
1249 * Again even if we can make a decision, we just mark the result and
1250 * the fact that we are done and continue looping.
Janos Follathee6abce2019-09-05 14:47:19 +01001251 */
Janos Follath30702422019-11-05 12:24:52 +00001252 cond = ct_lt_mpi_uint( Y->p[i - 1], X->p[i - 1] );
1253 *ret |= cond & ( 1 - done ) & X_is_negative;
Janos Follathc50e6d52019-10-28 12:37:21 +00001254 done |= cond;
Janos Follathee6abce2019-09-05 14:47:19 +01001255
1256 /*
Janos Follath30702422019-11-05 12:24:52 +00001257 * If X->p[i - 1] < Y->p[i - 1] then X < Y is true if and only if both
1258 * X and Y are positive.
Janos Follath0e5532d2019-10-11 14:21:53 +01001259 *
1260 * Again even if we can make a decision, we just mark the result and
1261 * the fact that we are done and continue looping.
Janos Follathee6abce2019-09-05 14:47:19 +01001262 */
Janos Follath30702422019-11-05 12:24:52 +00001263 cond = ct_lt_mpi_uint( X->p[i - 1], Y->p[i - 1] );
1264 *ret |= cond & ( 1 - done ) & ( 1 - X_is_negative );
Janos Follathc50e6d52019-10-28 12:37:21 +00001265 done |= cond;
Janos Follathee6abce2019-09-05 14:47:19 +01001266 }
1267
1268 return( 0 );
1269}
1270
Paul Bakker5121ce52009-01-03 21:22:43 +00001271/*
1272 * Compare signed values
1273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +00001275{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 mbedtls_mpi Y;
1277 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001278 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001279
1280 *p = ( z < 0 ) ? -z : z;
1281 Y.s = ( z < 0 ) ? -1 : 1;
1282 Y.n = 1;
1283 Y.p = p;
1284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 return( mbedtls_mpi_cmp_mpi( X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001286}
1287
1288/*
1289 * Unsigned addition: X = |A| + |B| (HAC 14.7)
1290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001292{
Janos Follath24eed8d2019-11-22 13:21:35 +00001293 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001294 size_t i, j;
Janos Follath6c922682015-10-30 17:43:11 +01001295 mbedtls_mpi_uint *o, *p, c, tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +00001296 MPI_VALIDATE_RET( X != NULL );
1297 MPI_VALIDATE_RET( A != NULL );
1298 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001299
1300 if( X == B )
1301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 const mbedtls_mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001303 }
1304
1305 if( X != A )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
Paul Bakker9af723c2014-05-01 13:03:14 +02001307
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001308 /*
1309 * X should always be positive as a result of unsigned additions.
1310 */
1311 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001312
Paul Bakker23986e52011-04-24 08:57:21 +00001313 for( j = B->n; j > 0; j-- )
1314 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 break;
1316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001318
1319 o = B->p; p = X->p; c = 0;
1320
Janos Follath6c922682015-10-30 17:43:11 +01001321 /*
1322 * tmp is used because it might happen that p == o
1323 */
Paul Bakker23986e52011-04-24 08:57:21 +00001324 for( i = 0; i < j; i++, o++, p++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001325 {
Janos Follath6c922682015-10-30 17:43:11 +01001326 tmp= *o;
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 *p += c; c = ( *p < c );
Janos Follath6c922682015-10-30 17:43:11 +01001328 *p += tmp; c += ( *p < tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +00001329 }
1330
1331 while( c != 0 )
1332 {
1333 if( i >= X->n )
1334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001336 p = X->p + i;
1337 }
1338
Paul Bakker2d319fd2012-09-16 21:34:26 +00001339 *p += c; c = ( *p < c ); i++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 }
1341
1342cleanup:
1343
1344 return( ret );
1345}
1346
Gilles Peskine09ec10a2020-06-09 10:39:38 +02001347/**
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001348 * Helper for mbedtls_mpi subtraction.
1349 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001350 * Calculate l - r where l and r have the same size.
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001351 * This function operates modulo (2^ciL)^n and returns the carry
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001352 * (1 if there was a wraparound, i.e. if `l < r`, and 0 otherwise).
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001353 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001354 * d may be aliased to l or r.
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001355 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001356 * \param n Number of limbs of \p d, \p l and \p r.
1357 * \param[out] d The result of the subtraction.
1358 * \param[in] l The left operand.
1359 * \param[in] r The right operand.
1360 *
1361 * \return 1 if `l < r`.
1362 * 0 if `l >= r`.
Paul Bakker5121ce52009-01-03 21:22:43 +00001363 */
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001364static mbedtls_mpi_uint mpi_sub_hlp( size_t n,
1365 mbedtls_mpi_uint *d,
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001366 const mbedtls_mpi_uint *l,
1367 const mbedtls_mpi_uint *r )
Paul Bakker5121ce52009-01-03 21:22:43 +00001368{
Paul Bakker23986e52011-04-24 08:57:21 +00001369 size_t i;
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001370 mbedtls_mpi_uint c = 0, t, z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001371
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001372 for( i = 0; i < n; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001373 {
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001374 z = ( l[i] < c ); t = l[i] - c;
1375 c = ( t < r[i] ) + z; d[i] = t - r[i];
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 }
1377
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001378 return( c );
Paul Bakker5121ce52009-01-03 21:22:43 +00001379}
1380
1381/*
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001382 * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
Paul Bakker5121ce52009-01-03 21:22:43 +00001383 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001385{
Janos Follath24eed8d2019-11-22 13:21:35 +00001386 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001387 size_t n;
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001388 mbedtls_mpi_uint carry;
Hanno Becker73d7d792018-12-11 10:35:51 +00001389 MPI_VALIDATE_RET( X != NULL );
1390 MPI_VALIDATE_RET( A != NULL );
1391 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001392
Paul Bakker23986e52011-04-24 08:57:21 +00001393 for( n = B->n; n > 0; n-- )
1394 if( B->p[n - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001395 break;
Gilles Peskinec8a91772021-01-27 22:30:43 +01001396 if( n > A->n )
1397 {
1398 /* B >= (2^ciL)^n > A */
1399 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1400 goto cleanup;
1401 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001402
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001403 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, A->n ) );
1404
1405 /* Set the high limbs of X to match A. Don't touch the lower limbs
1406 * because X might be aliased to B, and we must not overwrite the
1407 * significant digits of B. */
1408 if( A->n > n )
1409 memcpy( X->p + n, A->p + n, ( A->n - n ) * ciL );
1410 if( X->n > A->n )
1411 memset( X->p + A->n, 0, ( X->n - A->n ) * ciL );
1412
1413 carry = mpi_sub_hlp( n, X->p, A->p, B->p );
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001414 if( carry != 0 )
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001415 {
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001416 /* Propagate the carry to the first nonzero limb of X. */
1417 for( ; n < X->n && X->p[n] == 0; n++ )
1418 --X->p[n];
1419 /* If we ran out of space for the carry, it means that the result
1420 * is negative. */
1421 if( n == X->n )
Gilles Peskine89b41302020-07-23 01:16:46 +02001422 {
1423 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1424 goto cleanup;
1425 }
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001426 --X->p[n];
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001427 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001428
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001429 /* X should always be positive as a result of unsigned subtractions. */
1430 X->s = 1;
1431
Paul Bakker5121ce52009-01-03 21:22:43 +00001432cleanup:
Paul Bakker5121ce52009-01-03 21:22:43 +00001433 return( ret );
1434}
1435
1436/*
1437 * Signed addition: X = A + B
1438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001440{
Hanno Becker73d7d792018-12-11 10:35:51 +00001441 int ret, s;
1442 MPI_VALIDATE_RET( X != NULL );
1443 MPI_VALIDATE_RET( A != NULL );
1444 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001445
Hanno Becker73d7d792018-12-11 10:35:51 +00001446 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 if( A->s * B->s < 0 )
1448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001452 X->s = s;
1453 }
1454 else
1455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001457 X->s = -s;
1458 }
1459 }
1460 else
1461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001463 X->s = s;
1464 }
1465
1466cleanup:
1467
1468 return( ret );
1469}
1470
1471/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001472 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001475{
Hanno Becker73d7d792018-12-11 10:35:51 +00001476 int ret, s;
1477 MPI_VALIDATE_RET( X != NULL );
1478 MPI_VALIDATE_RET( A != NULL );
1479 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001480
Hanno Becker73d7d792018-12-11 10:35:51 +00001481 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001482 if( A->s * B->s > 0 )
1483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001487 X->s = s;
1488 }
1489 else
1490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 X->s = -s;
1493 }
1494 }
1495 else
1496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001498 X->s = s;
1499 }
1500
1501cleanup:
1502
1503 return( ret );
1504}
1505
1506/*
1507 * Signed addition: X = A + b
1508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001510{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511 mbedtls_mpi _B;
1512 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001513 MPI_VALIDATE_RET( X != NULL );
1514 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001515
1516 p[0] = ( b < 0 ) ? -b : b;
1517 _B.s = ( b < 0 ) ? -1 : 1;
1518 _B.n = 1;
1519 _B.p = p;
1520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 return( mbedtls_mpi_add_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001522}
1523
1524/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001525 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001528{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 mbedtls_mpi _B;
1530 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001531 MPI_VALIDATE_RET( X != NULL );
1532 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001533
1534 p[0] = ( b < 0 ) ? -b : b;
1535 _B.s = ( b < 0 ) ? -1 : 1;
1536 _B.n = 1;
1537 _B.p = p;
1538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 return( mbedtls_mpi_sub_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001540}
1541
Gilles Peskinea5d8d892020-07-23 21:27:15 +02001542/** Helper for mbedtls_mpi multiplication.
1543 *
1544 * Add \p b * \p s to \p d.
1545 *
1546 * \param i The number of limbs of \p s.
1547 * \param[in] s A bignum to multiply, of size \p i.
1548 * It may overlap with \p d, but only if
1549 * \p d <= \p s.
1550 * Its leading limb must not be \c 0.
1551 * \param[in,out] d The bignum to add to.
1552 * It must be sufficiently large to store the
1553 * result of the multiplication. This means
1554 * \p i + 1 limbs if \p d[\p i - 1] started as 0 and \p b
1555 * is not known a priori.
1556 * \param b A scalar to multiply.
Paul Bakkerfc4f46f2013-06-24 19:23:56 +02001557 */
1558static
1559#if defined(__APPLE__) && defined(__arm__)
1560/*
1561 * Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
1562 * appears to need this to prevent bad ARM code generation at -O3.
1563 */
1564__attribute__ ((noinline))
1565#endif
Gilles Peskinea5d8d892020-07-23 21:27:15 +02001566void mpi_mul_hlp( size_t i,
1567 const mbedtls_mpi_uint *s,
1568 mbedtls_mpi_uint *d,
1569 mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001570{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 mbedtls_mpi_uint c = 0, t = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001572
1573#if defined(MULADDC_HUIT)
1574 for( ; i >= 8; i -= 8 )
1575 {
1576 MULADDC_INIT
1577 MULADDC_HUIT
1578 MULADDC_STOP
1579 }
1580
1581 for( ; i > 0; i-- )
1582 {
1583 MULADDC_INIT
1584 MULADDC_CORE
1585 MULADDC_STOP
1586 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001587#else /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001588 for( ; i >= 16; i -= 16 )
1589 {
1590 MULADDC_INIT
1591 MULADDC_CORE MULADDC_CORE
1592 MULADDC_CORE MULADDC_CORE
1593 MULADDC_CORE MULADDC_CORE
1594 MULADDC_CORE MULADDC_CORE
1595
1596 MULADDC_CORE MULADDC_CORE
1597 MULADDC_CORE MULADDC_CORE
1598 MULADDC_CORE MULADDC_CORE
1599 MULADDC_CORE MULADDC_CORE
1600 MULADDC_STOP
1601 }
1602
1603 for( ; i >= 8; i -= 8 )
1604 {
1605 MULADDC_INIT
1606 MULADDC_CORE MULADDC_CORE
1607 MULADDC_CORE MULADDC_CORE
1608
1609 MULADDC_CORE MULADDC_CORE
1610 MULADDC_CORE MULADDC_CORE
1611 MULADDC_STOP
1612 }
1613
1614 for( ; i > 0; i-- )
1615 {
1616 MULADDC_INIT
1617 MULADDC_CORE
1618 MULADDC_STOP
1619 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001620#endif /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001621
1622 t++;
1623
Gilles Peskine8e464c42020-07-24 00:08:38 +02001624 while( c != 0 )
1625 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001626 *d += c; c = ( *d < c ); d++;
1627 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001628}
1629
1630/*
1631 * Baseline multiplication: X = A * B (HAC 14.12)
1632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001634{
Janos Follath24eed8d2019-11-22 13:21:35 +00001635 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001636 size_t i, j;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 mbedtls_mpi TA, TB;
Hanno Becker73d7d792018-12-11 10:35:51 +00001638 MPI_VALIDATE_RET( X != NULL );
1639 MPI_VALIDATE_RET( A != NULL );
1640 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644 if( X == A ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); A = &TA; }
1645 if( X == B ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); B = &TB; }
Paul Bakker5121ce52009-01-03 21:22:43 +00001646
Paul Bakker23986e52011-04-24 08:57:21 +00001647 for( i = A->n; i > 0; i-- )
1648 if( A->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001649 break;
1650
Paul Bakker23986e52011-04-24 08:57:21 +00001651 for( j = B->n; j > 0; j-- )
1652 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001653 break;
1654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + j ) );
1656 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
Alexey Skalozub8e75e682016-01-13 21:59:27 +02001658 for( ; j > 0; j-- )
1659 mpi_mul_hlp( i, A->p, X->p + j - 1, B->p[j - 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00001660
1661 X->s = A->s * B->s;
1662
1663cleanup:
1664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001666
1667 return( ret );
1668}
1669
1670/*
1671 * Baseline multiplication: X = A * b
1672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001674{
Hanno Becker73d7d792018-12-11 10:35:51 +00001675 MPI_VALIDATE_RET( X != NULL );
1676 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001678 /* mpi_mul_hlp can't deal with a leading 0. */
1679 size_t n = A->n;
1680 while( n > 0 && A->p[n - 1] == 0 )
1681 --n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001682
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001683 /* The general method below doesn't work if n==0 or b==0. By chance
1684 * calculating the result is trivial in those cases. */
1685 if( b == 0 || n == 0 )
1686 {
Paul Elliott986b55a2021-04-20 21:46:29 +01001687 return( mbedtls_mpi_lset( X, 0 ) );
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001688 }
1689
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001690 /* Calculate A*b as A + A*(b-1) to take advantage of mpi_mul_hlp */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001691 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001692 /* In general, A * b requires 1 limb more than b. If
1693 * A->p[n - 1] * b / b == A->p[n - 1], then A * b fits in the same
1694 * number of limbs as A and the call to grow() is not required since
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001695 * copy() will take care of the growth if needed. However, experimentally,
1696 * making the call to grow() unconditional causes slightly fewer
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001697 * calls to calloc() in ECP code, presumably because it reuses the
1698 * same mpi for a while and this way the mpi is more likely to directly
1699 * grow to its final size. */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001700 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n + 1 ) );
1701 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
1702 mpi_mul_hlp( n, A->p, X->p, b - 1 );
1703
1704cleanup:
1705 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001706}
1707
1708/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001709 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1710 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001711 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001712static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1,
1713 mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r )
Simon Butcher15b15d12015-11-26 19:35:03 +00001714{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001715#if defined(MBEDTLS_HAVE_UDBL)
1716 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001717#else
Simon Butcher9803d072016-01-03 00:24:34 +00001718 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
1719 const mbedtls_mpi_uint uint_halfword_mask = ( (mbedtls_mpi_uint) 1 << biH ) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001720 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1721 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001722 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001723#endif
1724
Simon Butcher15b15d12015-11-26 19:35:03 +00001725 /*
1726 * Check for overflow
1727 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001728 if( 0 == d || u1 >= d )
Simon Butcher15b15d12015-11-26 19:35:03 +00001729 {
Simon Butcherf5ba0452015-12-27 23:01:55 +00001730 if (r != NULL) *r = ~0;
Simon Butcher15b15d12015-11-26 19:35:03 +00001731
Simon Butcherf5ba0452015-12-27 23:01:55 +00001732 return ( ~0 );
Simon Butcher15b15d12015-11-26 19:35:03 +00001733 }
1734
1735#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001736 dividend = (mbedtls_t_udbl) u1 << biL;
1737 dividend |= (mbedtls_t_udbl) u0;
1738 quotient = dividend / d;
1739 if( quotient > ( (mbedtls_t_udbl) 1 << biL ) - 1 )
1740 quotient = ( (mbedtls_t_udbl) 1 << biL ) - 1;
1741
1742 if( r != NULL )
Simon Butcher9803d072016-01-03 00:24:34 +00001743 *r = (mbedtls_mpi_uint)( dividend - (quotient * d ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001744
1745 return (mbedtls_mpi_uint) quotient;
1746#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001747
1748 /*
1749 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1750 * Vol. 2 - Seminumerical Algorithms, Knuth
1751 */
1752
1753 /*
1754 * Normalize the divisor, d, and dividend, u0, u1
1755 */
1756 s = mbedtls_clz( d );
1757 d = d << s;
1758
1759 u1 = u1 << s;
Simon Butcher9803d072016-01-03 00:24:34 +00001760 u1 |= ( u0 >> ( biL - s ) ) & ( -(mbedtls_mpi_sint)s >> ( biL - 1 ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001761 u0 = u0 << s;
1762
1763 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001764 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001765
1766 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001767 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001768
1769 /*
1770 * Find the first quotient and remainder
1771 */
1772 q1 = u1 / d1;
1773 r0 = u1 - d1 * q1;
1774
1775 while( q1 >= radix || ( q1 * d0 > radix * r0 + u0_msw ) )
1776 {
1777 q1 -= 1;
1778 r0 += d1;
1779
1780 if ( r0 >= radix ) break;
1781 }
1782
Simon Butcherf5ba0452015-12-27 23:01:55 +00001783 rAX = ( u1 * radix ) + ( u0_msw - q1 * d );
Simon Butcher15b15d12015-11-26 19:35:03 +00001784 q0 = rAX / d1;
1785 r0 = rAX - q0 * d1;
1786
1787 while( q0 >= radix || ( q0 * d0 > radix * r0 + u0_lsw ) )
1788 {
1789 q0 -= 1;
1790 r0 += d1;
1791
1792 if ( r0 >= radix ) break;
1793 }
1794
1795 if (r != NULL)
Simon Butcherf5ba0452015-12-27 23:01:55 +00001796 *r = ( rAX * radix + u0_lsw - q0 * d ) >> s;
Simon Butcher15b15d12015-11-26 19:35:03 +00001797
1798 quotient = q1 * radix + q0;
1799
1800 return quotient;
1801#endif
1802}
1803
1804/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001806 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001807int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1808 const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001809{
Janos Follath24eed8d2019-11-22 13:21:35 +00001810 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001811 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812 mbedtls_mpi X, Y, Z, T1, T2;
Alexander Kd19a1932019-11-01 18:20:42 +03001813 mbedtls_mpi_uint TP2[3];
Hanno Becker73d7d792018-12-11 10:35:51 +00001814 MPI_VALIDATE_RET( A != NULL );
1815 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 if( mbedtls_mpi_cmp_int( B, 0 ) == 0 )
1818 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Alexander K35d6d462019-10-31 14:46:45 +03001821 mbedtls_mpi_init( &T1 );
Alexander Kd19a1932019-11-01 18:20:42 +03001822 /*
1823 * Avoid dynamic memory allocations for constant-size T2.
1824 *
1825 * T2 is used for comparison only and the 3 limbs are assigned explicitly,
1826 * so nobody increase the size of the MPI and we're safe to use an on-stack
1827 * buffer.
1828 */
Alexander K35d6d462019-10-31 14:46:45 +03001829 T2.s = 1;
Alexander Kd19a1932019-11-01 18:20:42 +03001830 T2.n = sizeof( TP2 ) / sizeof( *TP2 );
1831 T2.p = TP2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835 if( Q != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_lset( Q, 0 ) );
1836 if( R != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001837 return( 0 );
1838 }
1839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &X, A ) );
1841 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001842 X.s = Y.s = 1;
1843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &Z, A->n + 2 ) );
1845 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Z, 0 ) );
Gilles Peskine2536aa72020-07-24 00:12:59 +02001846 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, A->n + 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001847
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001848 k = mbedtls_mpi_bitlen( &Y ) % biL;
Paul Bakkerf9688572011-05-05 10:00:45 +00001849 if( k < biL - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001850 {
1851 k = biL - 1 - k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &X, k ) );
1853 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, k ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001854 }
1855 else k = 0;
1856
1857 n = X.n - 1;
1858 t = Y.n - 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 while( mbedtls_mpi_cmp_mpi( &X, &Y ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001862 {
1863 Z.p[n - t]++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001865 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001867
1868 for( i = n; i > t ; i-- )
1869 {
1870 if( X.p[i] >= Y.p[t] )
1871 Z.p[i - t - 1] = ~0;
1872 else
1873 {
Simon Butcher15b15d12015-11-26 19:35:03 +00001874 Z.p[i - t - 1] = mbedtls_int_div_int( X.p[i], X.p[i - 1],
1875 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001876 }
1877
Alexander K35d6d462019-10-31 14:46:45 +03001878 T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2];
1879 T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1];
1880 T2.p[2] = X.p[i];
1881
Paul Bakker5121ce52009-01-03 21:22:43 +00001882 Z.p[i - t - 1]++;
1883 do
1884 {
1885 Z.p[i - t - 1]--;
1886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T1, 0 ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001888 T1.p[0] = ( t < 1 ) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001889 T1.p[1] = Y.p[t];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001891 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892 while( mbedtls_mpi_cmp_mpi( &T1, &T2 ) > 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
1895 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1896 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 if( mbedtls_mpi_cmp_int( &X, 0 ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &Y ) );
1901 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1902 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001903 Z.p[i - t - 1]--;
1904 }
1905 }
1906
1907 if( Q != NULL )
1908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( Q, &Z ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 Q->s = A->s * B->s;
1911 }
1912
1913 if( R != NULL )
1914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &X, k ) );
Paul Bakkerf02c5642012-11-13 10:25:21 +00001916 X.s = A->s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, &X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 if( mbedtls_mpi_cmp_int( R, 0 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001920 R->s = 1;
1921 }
1922
1923cleanup:
1924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Alexander K35d6d462019-10-31 14:46:45 +03001926 mbedtls_mpi_free( &T1 );
Alexander Kd19a1932019-11-01 18:20:42 +03001927 mbedtls_platform_zeroize( TP2, sizeof( TP2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001928
1929 return( ret );
1930}
1931
1932/*
1933 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00001934 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001935int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R,
1936 const mbedtls_mpi *A,
1937 mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001938{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939 mbedtls_mpi _B;
1940 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001941 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001942
1943 p[0] = ( b < 0 ) ? -b : b;
1944 _B.s = ( b < 0 ) ? -1 : 1;
1945 _B.n = 1;
1946 _B.p = p;
1947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948 return( mbedtls_mpi_div_mpi( Q, R, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001949}
1950
1951/*
1952 * Modulo: R = A mod B
1953 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001955{
Janos Follath24eed8d2019-11-22 13:21:35 +00001956 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker73d7d792018-12-11 10:35:51 +00001957 MPI_VALIDATE_RET( R != NULL );
1958 MPI_VALIDATE_RET( A != NULL );
1959 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 if( mbedtls_mpi_cmp_int( B, 0 ) < 0 )
1962 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( NULL, R, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 while( mbedtls_mpi_cmp_int( R, 0 ) < 0 )
1967 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 while( mbedtls_mpi_cmp_mpi( R, B ) >= 0 )
1970 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001971
1972cleanup:
1973
1974 return( ret );
1975}
1976
1977/*
1978 * Modulo: r = A mod b
1979 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001981{
Paul Bakker23986e52011-04-24 08:57:21 +00001982 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 mbedtls_mpi_uint x, y, z;
Hanno Becker73d7d792018-12-11 10:35:51 +00001984 MPI_VALIDATE_RET( r != NULL );
1985 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001986
1987 if( b == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001989
1990 if( b < 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001992
1993 /*
1994 * handle trivial cases
1995 */
1996 if( b == 1 )
1997 {
1998 *r = 0;
1999 return( 0 );
2000 }
2001
2002 if( b == 2 )
2003 {
2004 *r = A->p[0] & 1;
2005 return( 0 );
2006 }
2007
2008 /*
2009 * general case
2010 */
Paul Bakker23986e52011-04-24 08:57:21 +00002011 for( i = A->n, y = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00002012 {
Paul Bakker23986e52011-04-24 08:57:21 +00002013 x = A->p[i - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00002014 y = ( y << biH ) | ( x >> biH );
2015 z = y / b;
2016 y -= z * b;
2017
2018 x <<= biH;
2019 y = ( y << biH ) | ( x >> biH );
2020 z = y / b;
2021 y -= z * b;
2022 }
2023
Paul Bakkerce40a6d2009-06-23 19:46:08 +00002024 /*
2025 * If A is negative, then the current y represents a negative value.
2026 * Flipping it to the positive side.
2027 */
2028 if( A->s < 0 && y != 0 )
2029 y = b - y;
2030
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 *r = y;
2032
2033 return( 0 );
2034}
2035
2036/*
2037 * Fast Montgomery initialization (thanks to Tom St Denis)
2038 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039static void mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002040{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 mbedtls_mpi_uint x, m0 = N->p[0];
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01002042 unsigned int i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002043
2044 x = m0;
2045 x += ( ( m0 + 2 ) & 4 ) << 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002046
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01002047 for( i = biL; i >= 8; i /= 2 )
2048 x *= ( 2 - ( m0 * x ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002049
2050 *mm = ~x + 1;
2051}
2052
Gilles Peskine2a82f722020-06-04 15:00:49 +02002053/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
2054 *
2055 * \param[in,out] A One of the numbers to multiply.
Gilles Peskine221626f2020-06-08 22:37:50 +02002056 * It must have at least as many limbs as N
2057 * (A->n >= N->n), and any limbs beyond n are ignored.
Gilles Peskine2a82f722020-06-04 15:00:49 +02002058 * On successful completion, A contains the result of
2059 * the multiplication A * B * R^-1 mod N where
2060 * R = (2^ciL)^n.
2061 * \param[in] B One of the numbers to multiply.
2062 * It must be nonzero and must not have more limbs than N
2063 * (B->n <= N->n).
2064 * \param[in] N The modulo. N must be odd.
2065 * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
2066 * This is -N^-1 mod 2^ciL.
2067 * \param[in,out] T A bignum for temporary storage.
2068 * It must be at least twice the limb size of N plus 2
2069 * (T->n >= 2 * (N->n + 1)).
2070 * Its initial content is unused and
2071 * its final content is indeterminate.
2072 * Note that unlike the usual convention in the library
2073 * for `const mbedtls_mpi*`, the content of T can change.
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002075static 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 +02002076 const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00002077{
Paul Bakker23986e52011-04-24 08:57:21 +00002078 size_t i, n, m;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079 mbedtls_mpi_uint u0, u1, *d;
Paul Bakker5121ce52009-01-03 21:22:43 +00002080
2081 memset( T->p, 0, T->n * ciL );
2082
2083 d = T->p;
2084 n = N->n;
2085 m = ( B->n < n ) ? B->n : n;
2086
2087 for( i = 0; i < n; i++ )
2088 {
2089 /*
2090 * T = (T + u0*B + u1*N) / 2^biL
2091 */
2092 u0 = A->p[i];
2093 u1 = ( d[0] + u0 * B->p[0] ) * mm;
2094
2095 mpi_mul_hlp( m, B->p, d, u0 );
2096 mpi_mul_hlp( n, N->p, d, u1 );
2097
2098 *d++ = u0; d[n + 1] = 0;
2099 }
2100
Gilles Peskine221626f2020-06-08 22:37:50 +02002101 /* At this point, d is either the desired result or the desired result
2102 * plus N. We now potentially subtract N, avoiding leaking whether the
2103 * subtraction is performed through side channels. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002104
Gilles Peskine221626f2020-06-08 22:37:50 +02002105 /* Copy the n least significant limbs of d to A, so that
2106 * A = d if d < N (recall that N has n limbs). */
2107 memcpy( A->p, d, n * ciL );
Gilles Peskine09ec10a2020-06-09 10:39:38 +02002108 /* If d >= N then we want to set A to d - N. To prevent timing attacks,
Gilles Peskine221626f2020-06-08 22:37:50 +02002109 * do the calculation without using conditional tests. */
2110 /* Set d to d0 + (2^biL)^n - N where d0 is the current value of d. */
Gilles Peskine132c0972020-06-04 21:05:24 +02002111 d[n] += 1;
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02002112 d[n] -= mpi_sub_hlp( n, d, d, N->p );
Gilles Peskine221626f2020-06-08 22:37:50 +02002113 /* If d0 < N then d < (2^biL)^n
2114 * so d[n] == 0 and we want to keep A as it is.
2115 * If d0 >= N then d >= (2^biL)^n, and d <= (2^biL)^n + N < 2 * (2^biL)^n
2116 * so d[n] == 1 and we want to set A to the result of the subtraction
2117 * which is d - (2^biL)^n, i.e. the n least significant limbs of d.
2118 * This exactly corresponds to a conditional assignment. */
2119 mpi_safe_cond_assign( n, A->p, d, (unsigned char) d[n] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002120}
2121
2122/*
2123 * Montgomery reduction: A = A * R^-1 mod N
Gilles Peskine2a82f722020-06-04 15:00:49 +02002124 *
2125 * See mpi_montmul() regarding constraints and guarantees on the parameters.
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002127static void mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N,
2128 mbedtls_mpi_uint mm, const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00002129{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 mbedtls_mpi_uint z = 1;
2131 mbedtls_mpi U;
Paul Bakker5121ce52009-01-03 21:22:43 +00002132
Paul Bakker8ddb6452013-02-27 14:56:33 +01002133 U.n = U.s = (int) z;
Paul Bakker5121ce52009-01-03 21:22:43 +00002134 U.p = &z;
2135
Gilles Peskine4e91d472020-06-04 20:55:15 +02002136 mpi_montmul( A, &U, N, mm, T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002137}
2138
2139/*
2140 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
2141 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002142int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
2143 const mbedtls_mpi *E, const mbedtls_mpi *N,
2144 mbedtls_mpi *_RR )
Paul Bakker5121ce52009-01-03 21:22:43 +00002145{
Janos Follath24eed8d2019-11-22 13:21:35 +00002146 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002147 size_t wbits, wsize, one = 1;
2148 size_t i, j, nblimbs;
2149 size_t bufsize, nbits;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 mbedtls_mpi_uint ei, mm, state;
Daniel Otte388f9b22020-08-21 12:34:29 +02002151 mbedtls_mpi RR, T, W[ 1 << MBEDTLS_MPI_WINDOW_SIZE ], Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00002152 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002153
Hanno Becker73d7d792018-12-11 10:35:51 +00002154 MPI_VALIDATE_RET( X != NULL );
2155 MPI_VALIDATE_RET( A != NULL );
2156 MPI_VALIDATE_RET( E != NULL );
2157 MPI_VALIDATE_RET( N != NULL );
2158
Hanno Becker8d1dd1b2017-09-28 11:02:24 +01002159 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 || ( N->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 if( mbedtls_mpi_cmp_int( E, 0 ) < 0 )
2163 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002164
Chris Jones9246d042020-11-25 15:12:39 +00002165 if( mbedtls_mpi_bitlen( E ) > MBEDTLS_MPI_MAX_BITS ||
2166 mbedtls_mpi_bitlen( N ) > MBEDTLS_MPI_MAX_BITS )
2167 return ( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2168
Paul Bakkerf6198c12012-05-16 08:02:29 +00002169 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 * Init temps and window size
2171 */
2172 mpi_montg_init( &mm, N );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &T );
2174 mbedtls_mpi_init( &Apos );
Paul Bakker5121ce52009-01-03 21:22:43 +00002175 memset( W, 0, sizeof( W ) );
2176
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002177 i = mbedtls_mpi_bitlen( E );
Paul Bakker5121ce52009-01-03 21:22:43 +00002178
2179 wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
2180 ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1;
2181
Peter Kolbuse6bcad32018-12-11 14:01:44 -06002182#if( MBEDTLS_MPI_WINDOW_SIZE < 6 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 if( wsize > MBEDTLS_MPI_WINDOW_SIZE )
2184 wsize = MBEDTLS_MPI_WINDOW_SIZE;
Peter Kolbuse6bcad32018-12-11 14:01:44 -06002185#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00002186
Paul Bakker5121ce52009-01-03 21:22:43 +00002187 j = N->n + 1;
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02002188 /* All W[i] and X must have at least N->n limbs for the mpi_montmul()
2189 * and mpi_montred() calls later. Here we ensure that W[1] and X are
2190 * large enough, and later we'll grow other W[i] to the same length.
2191 * They must not be shrunk midway through this function!
2192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
2194 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], j ) );
2195 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T, j * 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002196
2197 /*
Paul Bakker50546922012-05-19 08:40:49 +00002198 * Compensate for negative A (and correct at the end)
2199 */
2200 neg = ( A->s == -1 );
Paul Bakker50546922012-05-19 08:40:49 +00002201 if( neg )
2202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Apos, A ) );
Paul Bakker50546922012-05-19 08:40:49 +00002204 Apos.s = 1;
2205 A = &Apos;
2206 }
2207
2208 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 * If 1st call, pre-compute R^2 mod N
2210 */
2211 if( _RR == NULL || _RR->p == NULL )
2212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &RR, 1 ) );
2214 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &RR, N->n * 2 * biL ) );
2215 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &RR, &RR, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002216
2217 if( _RR != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218 memcpy( _RR, &RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 }
2220 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 memcpy( &RR, _RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002222
2223 /*
2224 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
2225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 if( mbedtls_mpi_cmp_mpi( A, N ) >= 0 )
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02002227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &W[1], A, N ) );
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02002229 /* This should be a no-op because W[1] is already that large before
2230 * mbedtls_mpi_mod_mpi(), but it's necessary to avoid an overflow
2231 * in mpi_montmul() below, so let's make sure. */
2232 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], N->n +1 ) );
2233 }
Paul Bakkerc2024f42014-01-23 20:38:35 +01002234 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[1], A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002236
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02002237 /* Note that this is safe because W[1] always has at least N->n limbs
2238 * (it grew above and was preserved by mbedtls_mpi_copy()). */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002239 mpi_montmul( &W[1], &RR, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002240
2241 /*
2242 * X = R^2 * R^-1 mod N = R mod N
2243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &RR ) );
Gilles Peskine4e91d472020-06-04 20:55:15 +02002245 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002246
2247 if( wsize > 1 )
2248 {
2249 /*
2250 * W[1 << (wsize - 1)] = W[1] ^ (wsize - 1)
2251 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002252 j = one << ( wsize - 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[j], N->n + 1 ) );
2255 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[j], &W[1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002256
2257 for( i = 0; i < wsize - 1; i++ )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002258 mpi_montmul( &W[j], &W[j], N, mm, &T );
Paul Bakker0d7702c2013-10-29 16:18:35 +01002259
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 /*
2261 * W[i] = W[i - 1] * W[1]
2262 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002263 for( i = j + 1; i < ( one << wsize ); i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[i], N->n + 1 ) );
2266 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[i], &W[i - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002267
Gilles Peskine4e91d472020-06-04 20:55:15 +02002268 mpi_montmul( &W[i], &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002269 }
2270 }
2271
2272 nblimbs = E->n;
2273 bufsize = 0;
2274 nbits = 0;
2275 wbits = 0;
2276 state = 0;
2277
2278 while( 1 )
2279 {
2280 if( bufsize == 0 )
2281 {
Paul Bakker0d7702c2013-10-29 16:18:35 +01002282 if( nblimbs == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002283 break;
2284
Paul Bakker0d7702c2013-10-29 16:18:35 +01002285 nblimbs--;
2286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 bufsize = sizeof( mbedtls_mpi_uint ) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00002288 }
2289
2290 bufsize--;
2291
2292 ei = (E->p[nblimbs] >> bufsize) & 1;
2293
2294 /*
2295 * skip leading 0s
2296 */
2297 if( ei == 0 && state == 0 )
2298 continue;
2299
2300 if( ei == 0 && state == 1 )
2301 {
2302 /*
2303 * out of window, square X
2304 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002305 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002306 continue;
2307 }
2308
2309 /*
2310 * add ei to current window
2311 */
2312 state = 2;
2313
2314 nbits++;
Paul Bakker66d5d072014-06-17 16:39:18 +02002315 wbits |= ( ei << ( wsize - nbits ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002316
2317 if( nbits == wsize )
2318 {
2319 /*
2320 * X = X^wsize R^-1 mod N
2321 */
2322 for( i = 0; i < wsize; i++ )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002323 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002324
2325 /*
2326 * X = X * W[wbits] R^-1 mod N
2327 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002328 mpi_montmul( X, &W[wbits], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002329
2330 state--;
2331 nbits = 0;
2332 wbits = 0;
2333 }
2334 }
2335
2336 /*
2337 * process the remaining bits
2338 */
2339 for( i = 0; i < nbits; i++ )
2340 {
Gilles Peskine4e91d472020-06-04 20:55:15 +02002341 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002342
2343 wbits <<= 1;
2344
Paul Bakker66d5d072014-06-17 16:39:18 +02002345 if( ( wbits & ( one << wsize ) ) != 0 )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002346 mpi_montmul( X, &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002347 }
2348
2349 /*
2350 * X = A^E * R * R^-1 mod N = A^E mod N
2351 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002352 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002353
Hanno Beckera4af1c42017-04-18 09:07:45 +01002354 if( neg && E->n != 0 && ( E->p[0] & 1 ) != 0 )
Paul Bakkerf6198c12012-05-16 08:02:29 +00002355 {
2356 X->s = -1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, N, X ) );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002358 }
2359
Paul Bakker5121ce52009-01-03 21:22:43 +00002360cleanup:
2361
Paul Bakker66d5d072014-06-17 16:39:18 +02002362 for( i = ( one << ( wsize - 1 ) ); i < ( one << wsize ); i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 mbedtls_mpi_free( &W[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 mbedtls_mpi_free( &W[1] ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &Apos );
Paul Bakker6c591fa2011-05-05 11:49:20 +00002366
Paul Bakker75a28602014-03-31 12:08:17 +02002367 if( _RR == NULL || _RR->p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002369
2370 return( ret );
2371}
2372
Paul Bakker5121ce52009-01-03 21:22:43 +00002373/*
2374 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
2375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00002377{
Janos Follath24eed8d2019-11-22 13:21:35 +00002378 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002379 size_t lz, lzt;
Alexander Ke8ad49f2019-08-16 16:16:07 +03002380 mbedtls_mpi TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00002381
Hanno Becker73d7d792018-12-11 10:35:51 +00002382 MPI_VALIDATE_RET( G != NULL );
2383 MPI_VALIDATE_RET( A != NULL );
2384 MPI_VALIDATE_RET( B != NULL );
2385
Alexander Ke8ad49f2019-08-16 16:16:07 +03002386 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) );
2389 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391 lz = mbedtls_mpi_lsb( &TA );
2392 lzt = mbedtls_mpi_lsb( &TB );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002393
Gilles Peskine27253bc2021-06-09 13:26:43 +02002394 /* The loop below gives the correct result when A==0 but not when B==0.
2395 * So have a special case for B==0. Leverage the fact that we just
2396 * calculated the lsb and lsb(B)==0 iff B is odd or 0 to make the test
2397 * slightly more efficient than cmp_int(). */
2398 if( lzt == 0 && mbedtls_mpi_get_bit( &TB, 0 ) == 0 )
2399 {
2400 ret = mbedtls_mpi_copy( G, A );
2401 goto cleanup;
2402 }
2403
Paul Bakker66d5d072014-06-17 16:39:18 +02002404 if( lzt < lz )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002405 lz = lzt;
2406
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 TA.s = TB.s = 1;
2408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409 while( mbedtls_mpi_cmp_int( &TA, 0 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, mbedtls_mpi_lsb( &TA ) ) );
2412 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, mbedtls_mpi_lsb( &TB ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414 if( mbedtls_mpi_cmp_mpi( &TA, &TB ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TA, &TA, &TB ) );
2417 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002418 }
2419 else
2420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TB, &TB, &TA ) );
2422 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002423 }
2424 }
2425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &TB, lz ) );
2427 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( G, &TB ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002428
2429cleanup:
2430
Alexander Ke8ad49f2019-08-16 16:16:07 +03002431 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002432
2433 return( ret );
2434}
2435
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002436/* Fill X with n_bytes random bytes.
2437 * X must already have room for those bytes.
Gilles Peskineafb2bd22021-06-03 11:51:09 +02002438 * The ordering of the bytes returned from the RNG is suitable for
2439 * deterministic ECDSA (see RFC 6979 §3.3 and mbedtls_mpi_random()).
Gilles Peskineebe9b6a2021-04-13 21:55:35 +02002440 * The size and sign of X are unchanged.
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002441 * n_bytes must not be 0.
2442 */
2443static int mpi_fill_random_internal(
2444 mbedtls_mpi *X, size_t n_bytes,
2445 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
2446{
2447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2448 const size_t limbs = CHARS_TO_LIMBS( n_bytes );
2449 const size_t overhead = ( limbs * ciL ) - n_bytes;
2450
2451 if( X->n < limbs )
2452 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002453
Gilles Peskineebe9b6a2021-04-13 21:55:35 +02002454 memset( X->p, 0, overhead );
2455 memset( (unsigned char *) X->p + limbs * ciL, 0, ( X->n - limbs ) * ciL );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002456 MBEDTLS_MPI_CHK( f_rng( p_rng, (unsigned char *) X->p + overhead, n_bytes ) );
2457 mpi_bigendian_to_host( X->p, limbs );
2458
2459cleanup:
2460 return( ret );
2461}
2462
Paul Bakker33dc46b2014-04-30 16:11:39 +02002463/*
2464 * Fill X with size bytes of random.
2465 *
2466 * Use a temporary bytes representation to make sure the result is the same
Paul Bakkerc37b0ac2014-05-01 14:19:23 +02002467 * regardless of the platform endianness (useful when f_rng is actually
Paul Bakker33dc46b2014-04-30 16:11:39 +02002468 * deterministic, eg for tests).
2469 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002471 int (*f_rng)(void *, unsigned char *, size_t),
2472 void *p_rng )
Paul Bakker287781a2011-03-26 13:18:49 +00002473{
Janos Follath24eed8d2019-11-22 13:21:35 +00002474 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6dab6202019-01-02 16:42:29 +00002475 size_t const limbs = CHARS_TO_LIMBS( size );
Hanno Beckerda1655a2017-10-18 14:21:44 +01002476
Hanno Becker8ce11a32018-12-19 16:18:52 +00002477 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002478 MPI_VALIDATE_RET( f_rng != NULL );
Paul Bakker33dc46b2014-04-30 16:11:39 +02002479
Hanno Beckerda1655a2017-10-18 14:21:44 +01002480 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskineed32b572021-06-02 22:17:52 +02002481 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002482 if( size == 0 )
2483 return( 0 );
Paul Bakker287781a2011-03-26 13:18:49 +00002484
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002485 ret = mpi_fill_random_internal( X, size, f_rng, p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +00002486
2487cleanup:
2488 return( ret );
2489}
2490
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002491int mbedtls_mpi_random( mbedtls_mpi *X,
2492 mbedtls_mpi_sint min,
2493 const mbedtls_mpi *N,
2494 int (*f_rng)(void *, unsigned char *, size_t),
2495 void *p_rng )
2496{
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002497 int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskinee5381682021-04-13 21:23:25 +02002498 int count;
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002499 unsigned cmp = 0;
2500 size_t n_bits = mbedtls_mpi_bitlen( N );
2501 size_t n_bytes = ( n_bits + 7 ) / 8;
2502
Gilles Peskine1e918f42021-03-29 22:14:51 +02002503 if( min < 0 )
2504 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2505 if( mbedtls_mpi_cmp_int( N, min ) <= 0 )
2506 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2507
Gilles Peskinee5381682021-04-13 21:23:25 +02002508 /*
2509 * When min == 0, each try has at worst a probability 1/2 of failing
2510 * (the msb has a probability 1/2 of being 0, and then the result will
2511 * be < N), so after 30 tries failure probability is a most 2**(-30).
2512 *
2513 * When N is just below a power of 2, as is the case when generating
Gilles Peskinee842e582021-04-15 11:45:19 +02002514 * a random scalar on most elliptic curves, 1 try is enough with
Gilles Peskinee5381682021-04-13 21:23:25 +02002515 * overwhelming probability. When N is just above a power of 2,
Gilles Peskinee842e582021-04-15 11:45:19 +02002516 * as when generating a random scalar on secp224k1, each try has
Gilles Peskinee5381682021-04-13 21:23:25 +02002517 * a probability of failing that is almost 1/2.
2518 *
2519 * The probabilities are almost the same if min is nonzero but negligible
2520 * compared to N. This is always the case when N is crypto-sized, but
2521 * it's convenient to support small N for testing purposes. When N
2522 * is small, use a higher repeat count, otherwise the probability of
2523 * failure is macroscopic.
2524 */
Gilles Peskine87823d72021-06-02 21:18:59 +02002525 count = ( n_bytes > 4 ? 30 : 250 );
Gilles Peskinee5381682021-04-13 21:23:25 +02002526
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002527 /* Ensure that target MPI has exactly the same number of limbs
2528 * as the upper bound, even if the upper bound has leading zeros.
2529 * This is necessary for the mbedtls_mpi_lt_mpi_ct() check. */
Gilles Peskineed32b572021-06-02 22:17:52 +02002530 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, N->n ) );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002531
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002532 /*
2533 * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA)
2534 * when f_rng is a suitably parametrized instance of HMAC_DRBG:
2535 * - use the same byte ordering;
2536 * - keep the leftmost n_bits bits of the generated octet string;
2537 * - try until result is in the desired range.
2538 * This also avoids any bias, which is especially important for ECDSA.
2539 */
2540 do
2541 {
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002542 MBEDTLS_MPI_CHK( mpi_fill_random_internal( X, n_bytes, f_rng, p_rng ) );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002543 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, 8 * n_bytes - n_bits ) );
2544
Gilles Peskinee5381682021-04-13 21:23:25 +02002545 if( --count == 0 )
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002546 {
2547 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2548 goto cleanup;
2549 }
2550
Gilles Peskine405b0912021-06-03 11:38:26 +02002551 MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( X, N, &cmp ) );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002552 }
2553 while( mbedtls_mpi_cmp_int( X, min ) < 0 || cmp != 1 );
2554
2555cleanup:
2556 return( ret );
2557}
2558
Paul Bakker5121ce52009-01-03 21:22:43 +00002559/*
2560 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002563{
Janos Follath24eed8d2019-11-22 13:21:35 +00002564 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Hanno Becker73d7d792018-12-11 10:35:51 +00002566 MPI_VALIDATE_RET( X != NULL );
2567 MPI_VALIDATE_RET( A != NULL );
2568 MPI_VALIDATE_RET( N != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002569
Hanno Becker4bcb4912017-04-18 15:49:39 +01002570 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TU ); mbedtls_mpi_init( &U1 ); mbedtls_mpi_init( &U2 );
2574 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &TB ); mbedtls_mpi_init( &TV );
2575 mbedtls_mpi_init( &V1 ); mbedtls_mpi_init( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, A, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002582 goto cleanup;
2583 }
2584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &TA, A, N ) );
2586 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TU, &TA ) );
2587 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, N ) );
2588 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TV, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U1, 1 ) );
2591 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U2, 0 ) );
2592 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V1, 0 ) );
2593 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002594
2595 do
2596 {
2597 while( ( TU.p[0] & 1 ) == 0 )
2598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TU, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002600
2601 if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
2602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &U1, &U1, &TB ) );
2604 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002605 }
2606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U1, 1 ) );
2608 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 }
2610
2611 while( ( TV.p[0] & 1 ) == 0 )
2612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TV, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002614
2615 if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
2616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, &TB ) );
2618 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 }
2620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V1, 1 ) );
2622 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002623 }
2624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625 if( mbedtls_mpi_cmp_mpi( &TU, &TV ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TU, &TU, &TV ) );
2628 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U1, &U1, &V1 ) );
2629 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &V2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002630 }
2631 else
2632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TV, &TV, &TU ) );
2634 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, &U1 ) );
2635 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &U2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002636 }
2637 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 while( mbedtls_mpi_cmp_int( &TU, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 while( mbedtls_mpi_cmp_int( &V1, 0 ) < 0 )
2641 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 while( mbedtls_mpi_cmp_mpi( &V1, N ) >= 0 )
2644 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &V1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002647
2648cleanup:
2649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TU ); mbedtls_mpi_free( &U1 ); mbedtls_mpi_free( &U2 );
2651 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TV );
2652 mbedtls_mpi_free( &V1 ); mbedtls_mpi_free( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002653
2654 return( ret );
2655}
2656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002658
Paul Bakker5121ce52009-01-03 21:22:43 +00002659static const int small_prime[] =
2660{
2661 3, 5, 7, 11, 13, 17, 19, 23,
2662 29, 31, 37, 41, 43, 47, 53, 59,
2663 61, 67, 71, 73, 79, 83, 89, 97,
2664 101, 103, 107, 109, 113, 127, 131, 137,
2665 139, 149, 151, 157, 163, 167, 173, 179,
2666 181, 191, 193, 197, 199, 211, 223, 227,
2667 229, 233, 239, 241, 251, 257, 263, 269,
2668 271, 277, 281, 283, 293, 307, 311, 313,
2669 317, 331, 337, 347, 349, 353, 359, 367,
2670 373, 379, 383, 389, 397, 401, 409, 419,
2671 421, 431, 433, 439, 443, 449, 457, 461,
2672 463, 467, 479, 487, 491, 499, 503, 509,
2673 521, 523, 541, 547, 557, 563, 569, 571,
2674 577, 587, 593, 599, 601, 607, 613, 617,
2675 619, 631, 641, 643, 647, 653, 659, 661,
2676 673, 677, 683, 691, 701, 709, 719, 727,
2677 733, 739, 743, 751, 757, 761, 769, 773,
2678 787, 797, 809, 811, 821, 823, 827, 829,
2679 839, 853, 857, 859, 863, 877, 881, 883,
2680 887, 907, 911, 919, 929, 937, 941, 947,
2681 953, 967, 971, 977, 983, 991, 997, -103
2682};
2683
2684/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002685 * Small divisors test (X must be positive)
2686 *
2687 * Return values:
2688 * 0: no small factor (possible prime, more tests needed)
2689 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002691 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693static int mpi_check_small_factors( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +00002694{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002695 int ret = 0;
2696 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 mbedtls_mpi_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +00002698
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 if( ( X->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002701
2702 for( i = 0; small_prime[i] > 0; i++ )
2703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704 if( mbedtls_mpi_cmp_int( X, small_prime[i] ) <= 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002705 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, small_prime[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
2709 if( r == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002711 }
2712
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002713cleanup:
2714 return( ret );
2715}
2716
2717/*
2718 * Miller-Rabin pseudo-primality test (HAC 4.24)
2719 */
Janos Follathda31fa12018-09-03 14:45:23 +01002720static int mpi_miller_rabin( const mbedtls_mpi *X, size_t rounds,
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002721 int (*f_rng)(void *, unsigned char *, size_t),
2722 void *p_rng )
2723{
Pascal Junodb99183d2015-03-11 16:49:45 +01002724 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002725 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002727
Hanno Becker8ce11a32018-12-19 16:18:52 +00002728 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002729 MPI_VALIDATE_RET( f_rng != NULL );
2730
2731 mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R );
2732 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 mbedtls_mpi_init( &RR );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002734
Paul Bakker5121ce52009-01-03 21:22:43 +00002735 /*
2736 * W = |X| - 1
2737 * R = W >> lsb( W )
2738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &W, X, 1 ) );
2740 s = mbedtls_mpi_lsb( &W );
2741 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R, &W ) );
2742 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &R, s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002743
Janos Follathda31fa12018-09-03 14:45:23 +01002744 for( i = 0; i < rounds; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002745 {
2746 /*
2747 * pick a random A, 1 < A < |X| - 1
2748 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002749 count = 0;
2750 do {
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002751 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
Pascal Junodb99183d2015-03-11 16:49:45 +01002752
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002753 j = mbedtls_mpi_bitlen( &A );
2754 k = mbedtls_mpi_bitlen( &W );
Pascal Junodb99183d2015-03-11 16:49:45 +01002755 if (j > k) {
Darryl Greene3f95ed2018-10-02 13:21:35 +01002756 A.p[A.n - 1] &= ( (mbedtls_mpi_uint) 1 << ( k - ( A.n - 1 ) * biL - 1 ) ) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002757 }
2758
2759 if (count++ > 30) {
Jens Wiklanderf08aa3e2019-01-17 13:30:57 +01002760 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2761 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002762 }
2763
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002764 } while ( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 ||
2765 mbedtls_mpi_cmp_int( &A, 1 ) <= 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002766
2767 /*
2768 * A = A^R mod |X|
2769 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &A, &A, &R, X, &RR ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 if( mbedtls_mpi_cmp_mpi( &A, &W ) == 0 ||
2773 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002774 continue;
2775
2776 j = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 while( j < s && mbedtls_mpi_cmp_mpi( &A, &W ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 {
2779 /*
2780 * A = A * A mod |X|
2781 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &A, &A ) );
2783 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &A, &T, X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 if( mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002786 break;
2787
2788 j++;
2789 }
2790
2791 /*
2792 * not prime if A != |X| - 1 or A == 1
2793 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794 if( mbedtls_mpi_cmp_mpi( &A, &W ) != 0 ||
2795 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002798 break;
2799 }
2800 }
2801
2802cleanup:
Hanno Becker73d7d792018-12-11 10:35:51 +00002803 mbedtls_mpi_free( &W ); mbedtls_mpi_free( &R );
2804 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002806
2807 return( ret );
2808}
2809
2810/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002811 * Pseudo-primality test: small factors, then Miller-Rabin
2812 */
Janos Follatha0b67c22018-09-18 14:48:23 +01002813int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds,
2814 int (*f_rng)(void *, unsigned char *, size_t),
2815 void *p_rng )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002816{
Janos Follath24eed8d2019-11-22 13:21:35 +00002817 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818 mbedtls_mpi XX;
Hanno Becker8ce11a32018-12-19 16:18:52 +00002819 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002820 MPI_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002821
2822 XX.s = 1;
2823 XX.n = X->n;
2824 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826 if( mbedtls_mpi_cmp_int( &XX, 0 ) == 0 ||
2827 mbedtls_mpi_cmp_int( &XX, 1 ) == 0 )
2828 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830 if( mbedtls_mpi_cmp_int( &XX, 2 ) == 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002831 return( 0 );
2832
2833 if( ( ret = mpi_check_small_factors( &XX ) ) != 0 )
2834 {
2835 if( ret == 1 )
2836 return( 0 );
2837
2838 return( ret );
2839 }
2840
Janos Follathda31fa12018-09-03 14:45:23 +01002841 return( mpi_miller_rabin( &XX, rounds, f_rng, p_rng ) );
Janos Follathf301d232018-08-14 13:34:01 +01002842}
2843
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002844/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002845 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002846 *
Janos Follathf301d232018-08-14 13:34:01 +01002847 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2848 * be either 1024 bits or 1536 bits long, and flags must contain
2849 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002850 */
Janos Follath7c025a92018-08-14 11:08:41 +01002851int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002852 int (*f_rng)(void *, unsigned char *, size_t),
2853 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002854{
Jethro Beekman66689272018-02-14 19:24:10 -08002855#ifdef MBEDTLS_HAVE_INT64
2856// ceil(2^63.5)
2857#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2858#else
2859// ceil(2^31.5)
2860#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2861#endif
2862 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002863 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002864 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865 mbedtls_mpi_uint r;
2866 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002867
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871 if( nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS )
2872 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874 mbedtls_mpi_init( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002875
2876 n = BITS_TO_LIMBS( nbits );
2877
Janos Follathda31fa12018-09-03 14:45:23 +01002878 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR ) == 0 )
2879 {
2880 /*
2881 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2882 */
2883 rounds = ( ( nbits >= 1300 ) ? 2 : ( nbits >= 850 ) ? 3 :
2884 ( nbits >= 650 ) ? 4 : ( nbits >= 350 ) ? 8 :
2885 ( nbits >= 250 ) ? 12 : ( nbits >= 150 ) ? 18 : 27 );
2886 }
2887 else
2888 {
2889 /*
2890 * 2^-100 error probability, number of rounds computed based on HAC,
2891 * fact 4.48
2892 */
2893 rounds = ( ( nbits >= 1450 ) ? 4 : ( nbits >= 1150 ) ? 5 :
2894 ( nbits >= 1000 ) ? 6 : ( nbits >= 850 ) ? 7 :
2895 ( nbits >= 750 ) ? 8 : ( nbits >= 500 ) ? 13 :
2896 ( nbits >= 250 ) ? 28 : ( nbits >= 150 ) ? 40 : 51 );
2897 }
2898
Jethro Beekman66689272018-02-14 19:24:10 -08002899 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002900 {
Jethro Beekman66689272018-02-14 19:24:10 -08002901 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
2902 /* make sure generated number is at least (nbits-1)+0.5 bits (FIPS 186-4 §B.3.3 steps 4.4, 5.5) */
2903 if( X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2 ) continue;
2904
2905 k = n * biL;
2906 if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits ) );
2907 X->p[0] |= 1;
2908
Janos Follath7c025a92018-08-14 11:08:41 +01002909 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002910 {
Janos Follatha0b67c22018-09-18 14:48:23 +01002911 ret = mbedtls_mpi_is_prime_ext( X, rounds, f_rng, p_rng );
Jethro Beekman66689272018-02-14 19:24:10 -08002912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002914 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002915 }
Jethro Beekman66689272018-02-14 19:24:10 -08002916 else
Paul Bakker5121ce52009-01-03 21:22:43 +00002917 {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002918 /*
Jethro Beekman66689272018-02-14 19:24:10 -08002919 * An necessary condition for Y and X = 2Y + 1 to be prime
2920 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2921 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002922 */
Jethro Beekman66689272018-02-14 19:24:10 -08002923
2924 X->p[0] |= 2;
2925
2926 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, 3 ) );
2927 if( r == 0 )
2928 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 8 ) );
2929 else if( r == 1 )
2930 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 4 ) );
2931
2932 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
2933 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, X ) );
2934 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, 1 ) );
2935
2936 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002937 {
Jethro Beekman66689272018-02-14 19:24:10 -08002938 /*
2939 * First, check small factors for X and Y
2940 * before doing Miller-Rabin on any of them
2941 */
2942 if( ( ret = mpi_check_small_factors( X ) ) == 0 &&
2943 ( ret = mpi_check_small_factors( &Y ) ) == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002944 ( ret = mpi_miller_rabin( X, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002945 == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002946 ( ret = mpi_miller_rabin( &Y, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002947 == 0 )
Jethro Beekman66689272018-02-14 19:24:10 -08002948 goto cleanup;
2949
2950 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
2951 goto cleanup;
2952
2953 /*
2954 * Next candidates. We want to preserve Y = (X-1) / 2 and
2955 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2956 * so up Y by 6 and X by 12.
2957 */
2958 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 12 ) );
2959 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &Y, &Y, 6 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002960 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002961 }
2962 }
2963
2964cleanup:
2965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966 mbedtls_mpi_free( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002967
2968 return( ret );
2969}
2970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002974
Paul Bakker23986e52011-04-24 08:57:21 +00002975#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002976
2977static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2978{
2979 { 693, 609, 21 },
2980 { 1764, 868, 28 },
2981 { 768454923, 542167814, 1 }
2982};
2983
Paul Bakker5121ce52009-01-03 21:22:43 +00002984/*
2985 * Checkup routine
2986 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987int mbedtls_mpi_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002988{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002989 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00002991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); mbedtls_mpi_init( &X );
2993 mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &U ); mbedtls_mpi_init( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00002994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &A, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002996 "EFE021C2645FD1DC586E69184AF4A31E" \
2997 "D5F53E93B5F123FA41680867BA110131" \
2998 "944FE7952E2517337780CB0DB80E61AA" \
2999 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6" ) );
3000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003002 "B2E7EFD37075B9F03FF989C7C5051C20" \
3003 "34D2A323810251127E7BF8625A4F49A5" \
3004 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
3005 "5B5C25763222FEFCCFC38B832366C29E" ) );
3006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &N, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003008 "0066A198186C18C10B2F5ED9B522752A" \
3009 "9830B69916E535C8F047518A889A43A5" \
3010 "94B6BED27A168D31D4A52F88925AA8F5" ) );
3011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003015 "602AB7ECA597A3D6B56FF9829A5E8B85" \
3016 "9E857EA95A03512E2BAE7391688D264A" \
3017 "A5663B0341DB9CCFD2C4C5F421FEC814" \
3018 "8001B72E848A38CAE1C65F78E56ABDEF" \
3019 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
3020 "ECF677152EF804370C1A305CAF3B5BF1" \
3021 "30879B56C61DE584A0F53A2447A51E" ) );
3022
3023 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024 mbedtls_printf( " MPI test #1 (mul_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003027 {
3028 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003029 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003030
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003031 ret = 1;
3032 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003033 }
3034
3035 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003038 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &X, &Y, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003041 "256567336059E52CAE22925474705F39A94" ) );
3042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &V, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003044 "6613F26162223DF488E9CD48CC132C7A" \
3045 "0AC93C701B001B092E4E5B9F73BCD27B" \
3046 "9EE50D0657C77F374E903CDFA4C642" ) );
3047
3048 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049 mbedtls_printf( " MPI test #2 (div_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ||
3052 mbedtls_mpi_cmp_mpi( &Y, &V ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003053 {
3054 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003056
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003057 ret = 1;
3058 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003059 }
3060
3061 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &X, &A, &E, &N, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003067 "36E139AEA55215609D2816998ED020BB" \
3068 "BD96C37890F65171D948E9BC7CBAA4D9" \
3069 "325D24D6A3C12710F10A09FA08AB87" ) );
3070
3071 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072 mbedtls_printf( " MPI test #3 (exp_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003074 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003075 {
3076 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003078
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003079 ret = 1;
3080 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003081 }
3082
3083 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003089 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
3090 "C3DBA76456363A10869622EAC2DD84EC" \
3091 "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) );
3092
3093 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094 mbedtls_printf( " MPI test #4 (inv_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003097 {
3098 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003100
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003101 ret = 1;
3102 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003103 }
3104
3105 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003107
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003108 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109 mbedtls_printf( " MPI test #5 (simple gcd): " );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003110
Paul Bakker66d5d072014-06-17 16:39:18 +02003111 for( i = 0; i < GCD_PAIR_COUNT; i++ )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003113 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &X, gcd_pairs[i][0] ) );
3114 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Y, gcd_pairs[i][1] ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &A, &X, &Y ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118 if( mbedtls_mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 )
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003119 {
3120 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003121 mbedtls_printf( "failed at %d\n", i );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003122
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003123 ret = 1;
3124 goto cleanup;
3125 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003126 }
3127
3128 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129 mbedtls_printf( "passed\n" );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003130
Paul Bakker5121ce52009-01-03 21:22:43 +00003131cleanup:
3132
3133 if( ret != 0 && verbose != 0 )
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003134 mbedtls_printf( "Unexpected error, return code = %08X\n", (unsigned int) ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &X );
3137 mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &U ); mbedtls_mpi_free( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00003138
3139 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003141
3142 return( ret );
3143}
3144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147#endif /* MBEDTLS_BIGNUM_C */