blob: 418fbf20e92a6d97a4deb7cfb3696e59e472e870 [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
Gilles Peskinef4998b02021-06-10 15:51:54 +02001661 /* If the result is 0, we don't shortcut the operation, which reduces
1662 * but does not eliminate side channels leaking the zero-ness. We do
1663 * need to take care to set the sign bit properly since the library does
1664 * not fully support an MPI object with a value of 0 and s == -1. */
1665 if( ( i == 0 && ( A->n == 0 || A->p[0] == 0 ) ) ||
1666 ( j == 0 && ( B->n == 0 || B->p[0] == 0 ) ) )
1667 {
1668 X->s = 1;
1669 }
1670 else
1671 X->s = A->s * B->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001672
1673cleanup:
1674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001676
1677 return( ret );
1678}
1679
1680/*
1681 * Baseline multiplication: X = A * b
1682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001684{
Hanno Becker73d7d792018-12-11 10:35:51 +00001685 MPI_VALIDATE_RET( X != NULL );
1686 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001687
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001688 /* mpi_mul_hlp can't deal with a leading 0. */
1689 size_t n = A->n;
1690 while( n > 0 && A->p[n - 1] == 0 )
1691 --n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001692
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001693 /* The general method below doesn't work if n==0 or b==0. By chance
1694 * calculating the result is trivial in those cases. */
1695 if( b == 0 || n == 0 )
1696 {
Paul Elliott986b55a2021-04-20 21:46:29 +01001697 return( mbedtls_mpi_lset( X, 0 ) );
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001698 }
1699
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001700 /* Calculate A*b as A + A*(b-1) to take advantage of mpi_mul_hlp */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001701 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001702 /* In general, A * b requires 1 limb more than b. If
1703 * A->p[n - 1] * b / b == A->p[n - 1], then A * b fits in the same
1704 * number of limbs as A and the call to grow() is not required since
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001705 * copy() will take care of the growth if needed. However, experimentally,
1706 * making the call to grow() unconditional causes slightly fewer
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001707 * calls to calloc() in ECP code, presumably because it reuses the
1708 * same mpi for a while and this way the mpi is more likely to directly
1709 * grow to its final size. */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001710 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n + 1 ) );
1711 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
1712 mpi_mul_hlp( n, A->p, X->p, b - 1 );
1713
1714cleanup:
1715 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001716}
1717
1718/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001719 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1720 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001721 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001722static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1,
1723 mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r )
Simon Butcher15b15d12015-11-26 19:35:03 +00001724{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001725#if defined(MBEDTLS_HAVE_UDBL)
1726 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001727#else
Simon Butcher9803d072016-01-03 00:24:34 +00001728 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
1729 const mbedtls_mpi_uint uint_halfword_mask = ( (mbedtls_mpi_uint) 1 << biH ) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001730 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1731 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001732 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001733#endif
1734
Simon Butcher15b15d12015-11-26 19:35:03 +00001735 /*
1736 * Check for overflow
1737 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001738 if( 0 == d || u1 >= d )
Simon Butcher15b15d12015-11-26 19:35:03 +00001739 {
Simon Butcherf5ba0452015-12-27 23:01:55 +00001740 if (r != NULL) *r = ~0;
Simon Butcher15b15d12015-11-26 19:35:03 +00001741
Simon Butcherf5ba0452015-12-27 23:01:55 +00001742 return ( ~0 );
Simon Butcher15b15d12015-11-26 19:35:03 +00001743 }
1744
1745#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001746 dividend = (mbedtls_t_udbl) u1 << biL;
1747 dividend |= (mbedtls_t_udbl) u0;
1748 quotient = dividend / d;
1749 if( quotient > ( (mbedtls_t_udbl) 1 << biL ) - 1 )
1750 quotient = ( (mbedtls_t_udbl) 1 << biL ) - 1;
1751
1752 if( r != NULL )
Simon Butcher9803d072016-01-03 00:24:34 +00001753 *r = (mbedtls_mpi_uint)( dividend - (quotient * d ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001754
1755 return (mbedtls_mpi_uint) quotient;
1756#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001757
1758 /*
1759 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1760 * Vol. 2 - Seminumerical Algorithms, Knuth
1761 */
1762
1763 /*
1764 * Normalize the divisor, d, and dividend, u0, u1
1765 */
1766 s = mbedtls_clz( d );
1767 d = d << s;
1768
1769 u1 = u1 << s;
Simon Butcher9803d072016-01-03 00:24:34 +00001770 u1 |= ( u0 >> ( biL - s ) ) & ( -(mbedtls_mpi_sint)s >> ( biL - 1 ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001771 u0 = u0 << s;
1772
1773 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001774 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001775
1776 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001777 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001778
1779 /*
1780 * Find the first quotient and remainder
1781 */
1782 q1 = u1 / d1;
1783 r0 = u1 - d1 * q1;
1784
1785 while( q1 >= radix || ( q1 * d0 > radix * r0 + u0_msw ) )
1786 {
1787 q1 -= 1;
1788 r0 += d1;
1789
1790 if ( r0 >= radix ) break;
1791 }
1792
Simon Butcherf5ba0452015-12-27 23:01:55 +00001793 rAX = ( u1 * radix ) + ( u0_msw - q1 * d );
Simon Butcher15b15d12015-11-26 19:35:03 +00001794 q0 = rAX / d1;
1795 r0 = rAX - q0 * d1;
1796
1797 while( q0 >= radix || ( q0 * d0 > radix * r0 + u0_lsw ) )
1798 {
1799 q0 -= 1;
1800 r0 += d1;
1801
1802 if ( r0 >= radix ) break;
1803 }
1804
1805 if (r != NULL)
Simon Butcherf5ba0452015-12-27 23:01:55 +00001806 *r = ( rAX * radix + u0_lsw - q0 * d ) >> s;
Simon Butcher15b15d12015-11-26 19:35:03 +00001807
1808 quotient = q1 * radix + q0;
1809
1810 return quotient;
1811#endif
1812}
1813
1814/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001816 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001817int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1818 const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001819{
Janos Follath24eed8d2019-11-22 13:21:35 +00001820 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001821 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 mbedtls_mpi X, Y, Z, T1, T2;
Alexander Kd19a1932019-11-01 18:20:42 +03001823 mbedtls_mpi_uint TP2[3];
Hanno Becker73d7d792018-12-11 10:35:51 +00001824 MPI_VALIDATE_RET( A != NULL );
1825 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 if( mbedtls_mpi_cmp_int( B, 0 ) == 0 )
1828 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Alexander K35d6d462019-10-31 14:46:45 +03001831 mbedtls_mpi_init( &T1 );
Alexander Kd19a1932019-11-01 18:20:42 +03001832 /*
1833 * Avoid dynamic memory allocations for constant-size T2.
1834 *
1835 * T2 is used for comparison only and the 3 limbs are assigned explicitly,
1836 * so nobody increase the size of the MPI and we're safe to use an on-stack
1837 * buffer.
1838 */
Alexander K35d6d462019-10-31 14:46:45 +03001839 T2.s = 1;
Alexander Kd19a1932019-11-01 18:20:42 +03001840 T2.n = sizeof( TP2 ) / sizeof( *TP2 );
1841 T2.p = TP2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 if( Q != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_lset( Q, 0 ) );
1846 if( R != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001847 return( 0 );
1848 }
1849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &X, A ) );
1851 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001852 X.s = Y.s = 1;
1853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &Z, A->n + 2 ) );
1855 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Z, 0 ) );
Gilles Peskine2536aa72020-07-24 00:12:59 +02001856 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, A->n + 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001857
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001858 k = mbedtls_mpi_bitlen( &Y ) % biL;
Paul Bakkerf9688572011-05-05 10:00:45 +00001859 if( k < biL - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001860 {
1861 k = biL - 1 - k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &X, k ) );
1863 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, k ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001864 }
1865 else k = 0;
1866
1867 n = X.n - 1;
1868 t = Y.n - 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 while( mbedtls_mpi_cmp_mpi( &X, &Y ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001872 {
1873 Z.p[n - t]++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001875 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001877
1878 for( i = n; i > t ; i-- )
1879 {
1880 if( X.p[i] >= Y.p[t] )
1881 Z.p[i - t - 1] = ~0;
1882 else
1883 {
Simon Butcher15b15d12015-11-26 19:35:03 +00001884 Z.p[i - t - 1] = mbedtls_int_div_int( X.p[i], X.p[i - 1],
1885 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001886 }
1887
Alexander K35d6d462019-10-31 14:46:45 +03001888 T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2];
1889 T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1];
1890 T2.p[2] = X.p[i];
1891
Paul Bakker5121ce52009-01-03 21:22:43 +00001892 Z.p[i - t - 1]++;
1893 do
1894 {
1895 Z.p[i - t - 1]--;
1896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T1, 0 ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001898 T1.p[0] = ( t < 1 ) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001899 T1.p[1] = Y.p[t];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001901 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 while( mbedtls_mpi_cmp_mpi( &T1, &T2 ) > 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
1905 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1906 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 if( mbedtls_mpi_cmp_int( &X, 0 ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &Y ) );
1911 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1912 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001913 Z.p[i - t - 1]--;
1914 }
1915 }
1916
1917 if( Q != NULL )
1918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( Q, &Z ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001920 Q->s = A->s * B->s;
1921 }
1922
1923 if( R != NULL )
1924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &X, k ) );
Paul Bakkerf02c5642012-11-13 10:25:21 +00001926 X.s = A->s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, &X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929 if( mbedtls_mpi_cmp_int( R, 0 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001930 R->s = 1;
1931 }
1932
1933cleanup:
1934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Alexander K35d6d462019-10-31 14:46:45 +03001936 mbedtls_mpi_free( &T1 );
Alexander Kd19a1932019-11-01 18:20:42 +03001937 mbedtls_platform_zeroize( TP2, sizeof( TP2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001938
1939 return( ret );
1940}
1941
1942/*
1943 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00001944 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001945int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R,
1946 const mbedtls_mpi *A,
1947 mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001948{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 mbedtls_mpi _B;
1950 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001951 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001952
1953 p[0] = ( b < 0 ) ? -b : b;
1954 _B.s = ( b < 0 ) ? -1 : 1;
1955 _B.n = 1;
1956 _B.p = p;
1957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 return( mbedtls_mpi_div_mpi( Q, R, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001959}
1960
1961/*
1962 * Modulo: R = A mod B
1963 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001965{
Janos Follath24eed8d2019-11-22 13:21:35 +00001966 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker73d7d792018-12-11 10:35:51 +00001967 MPI_VALIDATE_RET( R != NULL );
1968 MPI_VALIDATE_RET( A != NULL );
1969 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 if( mbedtls_mpi_cmp_int( B, 0 ) < 0 )
1972 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( NULL, R, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 while( mbedtls_mpi_cmp_int( R, 0 ) < 0 )
1977 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 while( mbedtls_mpi_cmp_mpi( R, B ) >= 0 )
1980 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001981
1982cleanup:
1983
1984 return( ret );
1985}
1986
1987/*
1988 * Modulo: r = A mod b
1989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001991{
Paul Bakker23986e52011-04-24 08:57:21 +00001992 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993 mbedtls_mpi_uint x, y, z;
Hanno Becker73d7d792018-12-11 10:35:51 +00001994 MPI_VALIDATE_RET( r != NULL );
1995 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001996
1997 if( b == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001999
2000 if( b < 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002002
2003 /*
2004 * handle trivial cases
2005 */
2006 if( b == 1 )
2007 {
2008 *r = 0;
2009 return( 0 );
2010 }
2011
2012 if( b == 2 )
2013 {
2014 *r = A->p[0] & 1;
2015 return( 0 );
2016 }
2017
2018 /*
2019 * general case
2020 */
Paul Bakker23986e52011-04-24 08:57:21 +00002021 for( i = A->n, y = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00002022 {
Paul Bakker23986e52011-04-24 08:57:21 +00002023 x = A->p[i - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00002024 y = ( y << biH ) | ( x >> biH );
2025 z = y / b;
2026 y -= z * b;
2027
2028 x <<= biH;
2029 y = ( y << biH ) | ( x >> biH );
2030 z = y / b;
2031 y -= z * b;
2032 }
2033
Paul Bakkerce40a6d2009-06-23 19:46:08 +00002034 /*
2035 * If A is negative, then the current y represents a negative value.
2036 * Flipping it to the positive side.
2037 */
2038 if( A->s < 0 && y != 0 )
2039 y = b - y;
2040
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 *r = y;
2042
2043 return( 0 );
2044}
2045
2046/*
2047 * Fast Montgomery initialization (thanks to Tom St Denis)
2048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049static void mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002050{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051 mbedtls_mpi_uint x, m0 = N->p[0];
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01002052 unsigned int i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002053
2054 x = m0;
2055 x += ( ( m0 + 2 ) & 4 ) << 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002056
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01002057 for( i = biL; i >= 8; i /= 2 )
2058 x *= ( 2 - ( m0 * x ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002059
2060 *mm = ~x + 1;
2061}
2062
Gilles Peskine2a82f722020-06-04 15:00:49 +02002063/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
2064 *
2065 * \param[in,out] A One of the numbers to multiply.
Gilles Peskine221626f2020-06-08 22:37:50 +02002066 * It must have at least as many limbs as N
2067 * (A->n >= N->n), and any limbs beyond n are ignored.
Gilles Peskine2a82f722020-06-04 15:00:49 +02002068 * On successful completion, A contains the result of
2069 * the multiplication A * B * R^-1 mod N where
2070 * R = (2^ciL)^n.
2071 * \param[in] B One of the numbers to multiply.
2072 * It must be nonzero and must not have more limbs than N
2073 * (B->n <= N->n).
2074 * \param[in] N The modulo. N must be odd.
2075 * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
2076 * This is -N^-1 mod 2^ciL.
2077 * \param[in,out] T A bignum for temporary storage.
2078 * It must be at least twice the limb size of N plus 2
2079 * (T->n >= 2 * (N->n + 1)).
2080 * Its initial content is unused and
2081 * its final content is indeterminate.
2082 * Note that unlike the usual convention in the library
2083 * for `const mbedtls_mpi*`, the content of T can change.
Paul Bakker5121ce52009-01-03 21:22:43 +00002084 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002085static 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 +02002086 const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00002087{
Paul Bakker23986e52011-04-24 08:57:21 +00002088 size_t i, n, m;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 mbedtls_mpi_uint u0, u1, *d;
Paul Bakker5121ce52009-01-03 21:22:43 +00002090
2091 memset( T->p, 0, T->n * ciL );
2092
2093 d = T->p;
2094 n = N->n;
2095 m = ( B->n < n ) ? B->n : n;
2096
2097 for( i = 0; i < n; i++ )
2098 {
2099 /*
2100 * T = (T + u0*B + u1*N) / 2^biL
2101 */
2102 u0 = A->p[i];
2103 u1 = ( d[0] + u0 * B->p[0] ) * mm;
2104
2105 mpi_mul_hlp( m, B->p, d, u0 );
2106 mpi_mul_hlp( n, N->p, d, u1 );
2107
2108 *d++ = u0; d[n + 1] = 0;
2109 }
2110
Gilles Peskine221626f2020-06-08 22:37:50 +02002111 /* At this point, d is either the desired result or the desired result
2112 * plus N. We now potentially subtract N, avoiding leaking whether the
2113 * subtraction is performed through side channels. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002114
Gilles Peskine221626f2020-06-08 22:37:50 +02002115 /* Copy the n least significant limbs of d to A, so that
2116 * A = d if d < N (recall that N has n limbs). */
2117 memcpy( A->p, d, n * ciL );
Gilles Peskine09ec10a2020-06-09 10:39:38 +02002118 /* If d >= N then we want to set A to d - N. To prevent timing attacks,
Gilles Peskine221626f2020-06-08 22:37:50 +02002119 * do the calculation without using conditional tests. */
2120 /* Set d to d0 + (2^biL)^n - N where d0 is the current value of d. */
Gilles Peskine132c0972020-06-04 21:05:24 +02002121 d[n] += 1;
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02002122 d[n] -= mpi_sub_hlp( n, d, d, N->p );
Gilles Peskine221626f2020-06-08 22:37:50 +02002123 /* If d0 < N then d < (2^biL)^n
2124 * so d[n] == 0 and we want to keep A as it is.
2125 * If d0 >= N then d >= (2^biL)^n, and d <= (2^biL)^n + N < 2 * (2^biL)^n
2126 * so d[n] == 1 and we want to set A to the result of the subtraction
2127 * which is d - (2^biL)^n, i.e. the n least significant limbs of d.
2128 * This exactly corresponds to a conditional assignment. */
2129 mpi_safe_cond_assign( n, A->p, d, (unsigned char) d[n] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002130}
2131
2132/*
2133 * Montgomery reduction: A = A * R^-1 mod N
Gilles Peskine2a82f722020-06-04 15:00:49 +02002134 *
2135 * See mpi_montmul() regarding constraints and guarantees on the parameters.
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002137static void mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N,
2138 mbedtls_mpi_uint mm, const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00002139{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 mbedtls_mpi_uint z = 1;
2141 mbedtls_mpi U;
Paul Bakker5121ce52009-01-03 21:22:43 +00002142
Paul Bakker8ddb6452013-02-27 14:56:33 +01002143 U.n = U.s = (int) z;
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 U.p = &z;
2145
Gilles Peskine4e91d472020-06-04 20:55:15 +02002146 mpi_montmul( A, &U, N, mm, T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002147}
2148
2149/*
2150 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
2151 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002152int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
2153 const mbedtls_mpi *E, const mbedtls_mpi *N,
2154 mbedtls_mpi *_RR )
Paul Bakker5121ce52009-01-03 21:22:43 +00002155{
Janos Follath24eed8d2019-11-22 13:21:35 +00002156 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002157 size_t wbits, wsize, one = 1;
2158 size_t i, j, nblimbs;
2159 size_t bufsize, nbits;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 mbedtls_mpi_uint ei, mm, state;
Daniel Otte388f9b22020-08-21 12:34:29 +02002161 mbedtls_mpi RR, T, W[ 1 << MBEDTLS_MPI_WINDOW_SIZE ], Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00002162 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002163
Hanno Becker73d7d792018-12-11 10:35:51 +00002164 MPI_VALIDATE_RET( X != NULL );
2165 MPI_VALIDATE_RET( A != NULL );
2166 MPI_VALIDATE_RET( E != NULL );
2167 MPI_VALIDATE_RET( N != NULL );
2168
Hanno Becker8d1dd1b2017-09-28 11:02:24 +01002169 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 || ( N->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 if( mbedtls_mpi_cmp_int( E, 0 ) < 0 )
2173 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002174
Chris Jones9246d042020-11-25 15:12:39 +00002175 if( mbedtls_mpi_bitlen( E ) > MBEDTLS_MPI_MAX_BITS ||
2176 mbedtls_mpi_bitlen( N ) > MBEDTLS_MPI_MAX_BITS )
2177 return ( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2178
Paul Bakkerf6198c12012-05-16 08:02:29 +00002179 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002180 * Init temps and window size
2181 */
2182 mpi_montg_init( &mm, N );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &T );
2184 mbedtls_mpi_init( &Apos );
Paul Bakker5121ce52009-01-03 21:22:43 +00002185 memset( W, 0, sizeof( W ) );
2186
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002187 i = mbedtls_mpi_bitlen( E );
Paul Bakker5121ce52009-01-03 21:22:43 +00002188
2189 wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
2190 ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1;
2191
Peter Kolbuse6bcad32018-12-11 14:01:44 -06002192#if( MBEDTLS_MPI_WINDOW_SIZE < 6 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 if( wsize > MBEDTLS_MPI_WINDOW_SIZE )
2194 wsize = MBEDTLS_MPI_WINDOW_SIZE;
Peter Kolbuse6bcad32018-12-11 14:01:44 -06002195#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00002196
Paul Bakker5121ce52009-01-03 21:22:43 +00002197 j = N->n + 1;
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02002198 /* All W[i] and X must have at least N->n limbs for the mpi_montmul()
2199 * and mpi_montred() calls later. Here we ensure that W[1] and X are
2200 * large enough, and later we'll grow other W[i] to the same length.
2201 * They must not be shrunk midway through this function!
2202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
2204 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], j ) );
2205 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T, j * 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002206
2207 /*
Paul Bakker50546922012-05-19 08:40:49 +00002208 * Compensate for negative A (and correct at the end)
2209 */
2210 neg = ( A->s == -1 );
Paul Bakker50546922012-05-19 08:40:49 +00002211 if( neg )
2212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Apos, A ) );
Paul Bakker50546922012-05-19 08:40:49 +00002214 Apos.s = 1;
2215 A = &Apos;
2216 }
2217
2218 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 * If 1st call, pre-compute R^2 mod N
2220 */
2221 if( _RR == NULL || _RR->p == NULL )
2222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &RR, 1 ) );
2224 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &RR, N->n * 2 * biL ) );
2225 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &RR, &RR, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002226
2227 if( _RR != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 memcpy( _RR, &RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 }
2230 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 memcpy( &RR, _RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002232
2233 /*
2234 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
2235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 if( mbedtls_mpi_cmp_mpi( A, N ) >= 0 )
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02002237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &W[1], A, N ) );
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02002239 /* This should be a no-op because W[1] is already that large before
2240 * mbedtls_mpi_mod_mpi(), but it's necessary to avoid an overflow
2241 * in mpi_montmul() below, so let's make sure. */
2242 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], N->n +1 ) );
2243 }
Paul Bakkerc2024f42014-01-23 20:38:35 +01002244 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[1], A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002246
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02002247 /* Note that this is safe because W[1] always has at least N->n limbs
2248 * (it grew above and was preserved by mbedtls_mpi_copy()). */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002249 mpi_montmul( &W[1], &RR, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002250
2251 /*
2252 * X = R^2 * R^-1 mod N = R mod N
2253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &RR ) );
Gilles Peskine4e91d472020-06-04 20:55:15 +02002255 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002256
2257 if( wsize > 1 )
2258 {
2259 /*
2260 * W[1 << (wsize - 1)] = W[1] ^ (wsize - 1)
2261 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002262 j = one << ( wsize - 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[j], N->n + 1 ) );
2265 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[j], &W[1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002266
2267 for( i = 0; i < wsize - 1; i++ )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002268 mpi_montmul( &W[j], &W[j], N, mm, &T );
Paul Bakker0d7702c2013-10-29 16:18:35 +01002269
Paul Bakker5121ce52009-01-03 21:22:43 +00002270 /*
2271 * W[i] = W[i - 1] * W[1]
2272 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002273 for( i = j + 1; i < ( one << wsize ); i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[i], N->n + 1 ) );
2276 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[i], &W[i - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002277
Gilles Peskine4e91d472020-06-04 20:55:15 +02002278 mpi_montmul( &W[i], &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002279 }
2280 }
2281
2282 nblimbs = E->n;
2283 bufsize = 0;
2284 nbits = 0;
2285 wbits = 0;
2286 state = 0;
2287
2288 while( 1 )
2289 {
2290 if( bufsize == 0 )
2291 {
Paul Bakker0d7702c2013-10-29 16:18:35 +01002292 if( nblimbs == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002293 break;
2294
Paul Bakker0d7702c2013-10-29 16:18:35 +01002295 nblimbs--;
2296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297 bufsize = sizeof( mbedtls_mpi_uint ) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00002298 }
2299
2300 bufsize--;
2301
2302 ei = (E->p[nblimbs] >> bufsize) & 1;
2303
2304 /*
2305 * skip leading 0s
2306 */
2307 if( ei == 0 && state == 0 )
2308 continue;
2309
2310 if( ei == 0 && state == 1 )
2311 {
2312 /*
2313 * out of window, square X
2314 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002315 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002316 continue;
2317 }
2318
2319 /*
2320 * add ei to current window
2321 */
2322 state = 2;
2323
2324 nbits++;
Paul Bakker66d5d072014-06-17 16:39:18 +02002325 wbits |= ( ei << ( wsize - nbits ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002326
2327 if( nbits == wsize )
2328 {
2329 /*
2330 * X = X^wsize R^-1 mod N
2331 */
2332 for( i = 0; i < wsize; i++ )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002333 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002334
2335 /*
2336 * X = X * W[wbits] R^-1 mod N
2337 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002338 mpi_montmul( X, &W[wbits], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002339
2340 state--;
2341 nbits = 0;
2342 wbits = 0;
2343 }
2344 }
2345
2346 /*
2347 * process the remaining bits
2348 */
2349 for( i = 0; i < nbits; i++ )
2350 {
Gilles Peskine4e91d472020-06-04 20:55:15 +02002351 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002352
2353 wbits <<= 1;
2354
Paul Bakker66d5d072014-06-17 16:39:18 +02002355 if( ( wbits & ( one << wsize ) ) != 0 )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002356 mpi_montmul( X, &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002357 }
2358
2359 /*
2360 * X = A^E * R * R^-1 mod N = A^E mod N
2361 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002362 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002363
Hanno Beckera4af1c42017-04-18 09:07:45 +01002364 if( neg && E->n != 0 && ( E->p[0] & 1 ) != 0 )
Paul Bakkerf6198c12012-05-16 08:02:29 +00002365 {
2366 X->s = -1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, N, X ) );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002368 }
2369
Paul Bakker5121ce52009-01-03 21:22:43 +00002370cleanup:
2371
Paul Bakker66d5d072014-06-17 16:39:18 +02002372 for( i = ( one << ( wsize - 1 ) ); i < ( one << wsize ); i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 mbedtls_mpi_free( &W[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 mbedtls_mpi_free( &W[1] ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &Apos );
Paul Bakker6c591fa2011-05-05 11:49:20 +00002376
Paul Bakker75a28602014-03-31 12:08:17 +02002377 if( _RR == NULL || _RR->p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002379
2380 return( ret );
2381}
2382
Paul Bakker5121ce52009-01-03 21:22:43 +00002383/*
2384 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
2385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00002387{
Janos Follath24eed8d2019-11-22 13:21:35 +00002388 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002389 size_t lz, lzt;
Alexander Ke8ad49f2019-08-16 16:16:07 +03002390 mbedtls_mpi TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00002391
Hanno Becker73d7d792018-12-11 10:35:51 +00002392 MPI_VALIDATE_RET( G != NULL );
2393 MPI_VALIDATE_RET( A != NULL );
2394 MPI_VALIDATE_RET( B != NULL );
2395
Alexander Ke8ad49f2019-08-16 16:16:07 +03002396 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) );
2399 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 lz = mbedtls_mpi_lsb( &TA );
2402 lzt = mbedtls_mpi_lsb( &TB );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002403
Gilles Peskine27253bc2021-06-09 13:26:43 +02002404 /* The loop below gives the correct result when A==0 but not when B==0.
2405 * So have a special case for B==0. Leverage the fact that we just
2406 * calculated the lsb and lsb(B)==0 iff B is odd or 0 to make the test
2407 * slightly more efficient than cmp_int(). */
2408 if( lzt == 0 && mbedtls_mpi_get_bit( &TB, 0 ) == 0 )
2409 {
2410 ret = mbedtls_mpi_copy( G, A );
2411 goto cleanup;
2412 }
2413
Paul Bakker66d5d072014-06-17 16:39:18 +02002414 if( lzt < lz )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002415 lz = lzt;
2416
Paul Bakker5121ce52009-01-03 21:22:43 +00002417 TA.s = TB.s = 1;
2418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 while( mbedtls_mpi_cmp_int( &TA, 0 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, mbedtls_mpi_lsb( &TA ) ) );
2422 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, mbedtls_mpi_lsb( &TB ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 if( mbedtls_mpi_cmp_mpi( &TA, &TB ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TA, &TA, &TB ) );
2427 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002428 }
2429 else
2430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TB, &TB, &TA ) );
2432 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 }
2434 }
2435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &TB, lz ) );
2437 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( G, &TB ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002438
2439cleanup:
2440
Alexander Ke8ad49f2019-08-16 16:16:07 +03002441 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002442
2443 return( ret );
2444}
2445
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002446/* Fill X with n_bytes random bytes.
2447 * X must already have room for those bytes.
Gilles Peskineafb2bd22021-06-03 11:51:09 +02002448 * The ordering of the bytes returned from the RNG is suitable for
2449 * deterministic ECDSA (see RFC 6979 §3.3 and mbedtls_mpi_random()).
Gilles Peskineebe9b6a2021-04-13 21:55:35 +02002450 * The size and sign of X are unchanged.
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002451 * n_bytes must not be 0.
2452 */
2453static int mpi_fill_random_internal(
2454 mbedtls_mpi *X, size_t n_bytes,
2455 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
2456{
2457 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2458 const size_t limbs = CHARS_TO_LIMBS( n_bytes );
2459 const size_t overhead = ( limbs * ciL ) - n_bytes;
2460
2461 if( X->n < limbs )
2462 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002463
Gilles Peskineebe9b6a2021-04-13 21:55:35 +02002464 memset( X->p, 0, overhead );
2465 memset( (unsigned char *) X->p + limbs * ciL, 0, ( X->n - limbs ) * ciL );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002466 MBEDTLS_MPI_CHK( f_rng( p_rng, (unsigned char *) X->p + overhead, n_bytes ) );
2467 mpi_bigendian_to_host( X->p, limbs );
2468
2469cleanup:
2470 return( ret );
2471}
2472
Paul Bakker33dc46b2014-04-30 16:11:39 +02002473/*
2474 * Fill X with size bytes of random.
2475 *
2476 * Use a temporary bytes representation to make sure the result is the same
Paul Bakkerc37b0ac2014-05-01 14:19:23 +02002477 * regardless of the platform endianness (useful when f_rng is actually
Paul Bakker33dc46b2014-04-30 16:11:39 +02002478 * deterministic, eg for tests).
2479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002481 int (*f_rng)(void *, unsigned char *, size_t),
2482 void *p_rng )
Paul Bakker287781a2011-03-26 13:18:49 +00002483{
Janos Follath24eed8d2019-11-22 13:21:35 +00002484 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6dab6202019-01-02 16:42:29 +00002485 size_t const limbs = CHARS_TO_LIMBS( size );
Hanno Beckerda1655a2017-10-18 14:21:44 +01002486
Hanno Becker8ce11a32018-12-19 16:18:52 +00002487 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002488 MPI_VALIDATE_RET( f_rng != NULL );
Paul Bakker33dc46b2014-04-30 16:11:39 +02002489
Hanno Beckerda1655a2017-10-18 14:21:44 +01002490 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskineed32b572021-06-02 22:17:52 +02002491 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002492 if( size == 0 )
2493 return( 0 );
Paul Bakker287781a2011-03-26 13:18:49 +00002494
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002495 ret = mpi_fill_random_internal( X, size, f_rng, p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +00002496
2497cleanup:
2498 return( ret );
2499}
2500
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002501int mbedtls_mpi_random( mbedtls_mpi *X,
2502 mbedtls_mpi_sint min,
2503 const mbedtls_mpi *N,
2504 int (*f_rng)(void *, unsigned char *, size_t),
2505 void *p_rng )
2506{
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002507 int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskinee5381682021-04-13 21:23:25 +02002508 int count;
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002509 unsigned cmp = 0;
2510 size_t n_bits = mbedtls_mpi_bitlen( N );
2511 size_t n_bytes = ( n_bits + 7 ) / 8;
2512
Gilles Peskine1e918f42021-03-29 22:14:51 +02002513 if( min < 0 )
2514 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2515 if( mbedtls_mpi_cmp_int( N, min ) <= 0 )
2516 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2517
Gilles Peskinee5381682021-04-13 21:23:25 +02002518 /*
2519 * When min == 0, each try has at worst a probability 1/2 of failing
2520 * (the msb has a probability 1/2 of being 0, and then the result will
2521 * be < N), so after 30 tries failure probability is a most 2**(-30).
2522 *
2523 * When N is just below a power of 2, as is the case when generating
Gilles Peskinee842e582021-04-15 11:45:19 +02002524 * a random scalar on most elliptic curves, 1 try is enough with
Gilles Peskinee5381682021-04-13 21:23:25 +02002525 * overwhelming probability. When N is just above a power of 2,
Gilles Peskinee842e582021-04-15 11:45:19 +02002526 * as when generating a random scalar on secp224k1, each try has
Gilles Peskinee5381682021-04-13 21:23:25 +02002527 * a probability of failing that is almost 1/2.
2528 *
2529 * The probabilities are almost the same if min is nonzero but negligible
2530 * compared to N. This is always the case when N is crypto-sized, but
2531 * it's convenient to support small N for testing purposes. When N
2532 * is small, use a higher repeat count, otherwise the probability of
2533 * failure is macroscopic.
2534 */
Gilles Peskine87823d72021-06-02 21:18:59 +02002535 count = ( n_bytes > 4 ? 30 : 250 );
Gilles Peskinee5381682021-04-13 21:23:25 +02002536
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002537 /* Ensure that target MPI has exactly the same number of limbs
2538 * as the upper bound, even if the upper bound has leading zeros.
2539 * This is necessary for the mbedtls_mpi_lt_mpi_ct() check. */
Gilles Peskineed32b572021-06-02 22:17:52 +02002540 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, N->n ) );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002541
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002542 /*
2543 * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA)
2544 * when f_rng is a suitably parametrized instance of HMAC_DRBG:
2545 * - use the same byte ordering;
2546 * - keep the leftmost n_bits bits of the generated octet string;
2547 * - try until result is in the desired range.
2548 * This also avoids any bias, which is especially important for ECDSA.
2549 */
2550 do
2551 {
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002552 MBEDTLS_MPI_CHK( mpi_fill_random_internal( X, n_bytes, f_rng, p_rng ) );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002553 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, 8 * n_bytes - n_bits ) );
2554
Gilles Peskinee5381682021-04-13 21:23:25 +02002555 if( --count == 0 )
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002556 {
2557 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2558 goto cleanup;
2559 }
2560
Gilles Peskine405b0912021-06-03 11:38:26 +02002561 MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( X, N, &cmp ) );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002562 }
2563 while( mbedtls_mpi_cmp_int( X, min ) < 0 || cmp != 1 );
2564
2565cleanup:
2566 return( ret );
2567}
2568
Paul Bakker5121ce52009-01-03 21:22:43 +00002569/*
2570 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002573{
Janos Follath24eed8d2019-11-22 13:21:35 +00002574 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Hanno Becker73d7d792018-12-11 10:35:51 +00002576 MPI_VALIDATE_RET( X != NULL );
2577 MPI_VALIDATE_RET( A != NULL );
2578 MPI_VALIDATE_RET( N != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002579
Hanno Becker4bcb4912017-04-18 15:49:39 +01002580 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TU ); mbedtls_mpi_init( &U1 ); mbedtls_mpi_init( &U2 );
2584 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &TB ); mbedtls_mpi_init( &TV );
2585 mbedtls_mpi_init( &V1 ); mbedtls_mpi_init( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, A, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002592 goto cleanup;
2593 }
2594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &TA, A, N ) );
2596 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TU, &TA ) );
2597 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, N ) );
2598 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TV, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U1, 1 ) );
2601 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U2, 0 ) );
2602 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V1, 0 ) );
2603 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002604
2605 do
2606 {
2607 while( ( TU.p[0] & 1 ) == 0 )
2608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TU, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002610
2611 if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
2612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &U1, &U1, &TB ) );
2614 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 }
2616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U1, 1 ) );
2618 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 }
2620
2621 while( ( TV.p[0] & 1 ) == 0 )
2622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TV, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002624
2625 if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
2626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, &TB ) );
2628 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002629 }
2630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V1, 1 ) );
2632 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002633 }
2634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 if( mbedtls_mpi_cmp_mpi( &TU, &TV ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TU, &TU, &TV ) );
2638 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U1, &U1, &V1 ) );
2639 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &V2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002640 }
2641 else
2642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TV, &TV, &TU ) );
2644 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, &U1 ) );
2645 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &U2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002646 }
2647 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648 while( mbedtls_mpi_cmp_int( &TU, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 while( mbedtls_mpi_cmp_int( &V1, 0 ) < 0 )
2651 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 while( mbedtls_mpi_cmp_mpi( &V1, N ) >= 0 )
2654 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &V1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002657
2658cleanup:
2659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TU ); mbedtls_mpi_free( &U1 ); mbedtls_mpi_free( &U2 );
2661 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TV );
2662 mbedtls_mpi_free( &V1 ); mbedtls_mpi_free( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
2664 return( ret );
2665}
2666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002668
Paul Bakker5121ce52009-01-03 21:22:43 +00002669static const int small_prime[] =
2670{
2671 3, 5, 7, 11, 13, 17, 19, 23,
2672 29, 31, 37, 41, 43, 47, 53, 59,
2673 61, 67, 71, 73, 79, 83, 89, 97,
2674 101, 103, 107, 109, 113, 127, 131, 137,
2675 139, 149, 151, 157, 163, 167, 173, 179,
2676 181, 191, 193, 197, 199, 211, 223, 227,
2677 229, 233, 239, 241, 251, 257, 263, 269,
2678 271, 277, 281, 283, 293, 307, 311, 313,
2679 317, 331, 337, 347, 349, 353, 359, 367,
2680 373, 379, 383, 389, 397, 401, 409, 419,
2681 421, 431, 433, 439, 443, 449, 457, 461,
2682 463, 467, 479, 487, 491, 499, 503, 509,
2683 521, 523, 541, 547, 557, 563, 569, 571,
2684 577, 587, 593, 599, 601, 607, 613, 617,
2685 619, 631, 641, 643, 647, 653, 659, 661,
2686 673, 677, 683, 691, 701, 709, 719, 727,
2687 733, 739, 743, 751, 757, 761, 769, 773,
2688 787, 797, 809, 811, 821, 823, 827, 829,
2689 839, 853, 857, 859, 863, 877, 881, 883,
2690 887, 907, 911, 919, 929, 937, 941, 947,
2691 953, 967, 971, 977, 983, 991, 997, -103
2692};
2693
2694/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002695 * Small divisors test (X must be positive)
2696 *
2697 * Return values:
2698 * 0: no small factor (possible prime, more tests needed)
2699 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002701 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703static int mpi_check_small_factors( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +00002704{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002705 int ret = 0;
2706 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707 mbedtls_mpi_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
Paul Bakker5121ce52009-01-03 21:22:43 +00002709 if( ( X->p[0] & 1 ) == 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 for( i = 0; small_prime[i] > 0; i++ )
2713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 if( mbedtls_mpi_cmp_int( X, small_prime[i] ) <= 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002715 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, small_prime[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002718
2719 if( r == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002721 }
2722
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002723cleanup:
2724 return( ret );
2725}
2726
2727/*
2728 * Miller-Rabin pseudo-primality test (HAC 4.24)
2729 */
Janos Follathda31fa12018-09-03 14:45:23 +01002730static int mpi_miller_rabin( const mbedtls_mpi *X, size_t rounds,
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002731 int (*f_rng)(void *, unsigned char *, size_t),
2732 void *p_rng )
2733{
Pascal Junodb99183d2015-03-11 16:49:45 +01002734 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002735 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002737
Hanno Becker8ce11a32018-12-19 16:18:52 +00002738 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002739 MPI_VALIDATE_RET( f_rng != NULL );
2740
2741 mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R );
2742 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 mbedtls_mpi_init( &RR );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002744
Paul Bakker5121ce52009-01-03 21:22:43 +00002745 /*
2746 * W = |X| - 1
2747 * R = W >> lsb( W )
2748 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &W, X, 1 ) );
2750 s = mbedtls_mpi_lsb( &W );
2751 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R, &W ) );
2752 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &R, s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002753
Janos Follathda31fa12018-09-03 14:45:23 +01002754 for( i = 0; i < rounds; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002755 {
2756 /*
2757 * pick a random A, 1 < A < |X| - 1
2758 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002759 count = 0;
2760 do {
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002761 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
Pascal Junodb99183d2015-03-11 16:49:45 +01002762
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002763 j = mbedtls_mpi_bitlen( &A );
2764 k = mbedtls_mpi_bitlen( &W );
Pascal Junodb99183d2015-03-11 16:49:45 +01002765 if (j > k) {
Darryl Greene3f95ed2018-10-02 13:21:35 +01002766 A.p[A.n - 1] &= ( (mbedtls_mpi_uint) 1 << ( k - ( A.n - 1 ) * biL - 1 ) ) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002767 }
2768
2769 if (count++ > 30) {
Jens Wiklanderf08aa3e2019-01-17 13:30:57 +01002770 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2771 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002772 }
2773
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002774 } while ( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 ||
2775 mbedtls_mpi_cmp_int( &A, 1 ) <= 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002776
2777 /*
2778 * A = A^R mod |X|
2779 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &A, &A, &R, X, &RR ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782 if( mbedtls_mpi_cmp_mpi( &A, &W ) == 0 ||
2783 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002784 continue;
2785
2786 j = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 while( j < s && mbedtls_mpi_cmp_mpi( &A, &W ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002788 {
2789 /*
2790 * A = A * A mod |X|
2791 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &A, &A ) );
2793 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &A, &T, X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795 if( mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002796 break;
2797
2798 j++;
2799 }
2800
2801 /*
2802 * not prime if A != |X| - 1 or A == 1
2803 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002804 if( mbedtls_mpi_cmp_mpi( &A, &W ) != 0 ||
2805 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002808 break;
2809 }
2810 }
2811
2812cleanup:
Hanno Becker73d7d792018-12-11 10:35:51 +00002813 mbedtls_mpi_free( &W ); mbedtls_mpi_free( &R );
2814 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002816
2817 return( ret );
2818}
2819
2820/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002821 * Pseudo-primality test: small factors, then Miller-Rabin
2822 */
Janos Follatha0b67c22018-09-18 14:48:23 +01002823int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds,
2824 int (*f_rng)(void *, unsigned char *, size_t),
2825 void *p_rng )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002826{
Janos Follath24eed8d2019-11-22 13:21:35 +00002827 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828 mbedtls_mpi XX;
Hanno Becker8ce11a32018-12-19 16:18:52 +00002829 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002830 MPI_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002831
2832 XX.s = 1;
2833 XX.n = X->n;
2834 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 if( mbedtls_mpi_cmp_int( &XX, 0 ) == 0 ||
2837 mbedtls_mpi_cmp_int( &XX, 1 ) == 0 )
2838 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 if( mbedtls_mpi_cmp_int( &XX, 2 ) == 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002841 return( 0 );
2842
2843 if( ( ret = mpi_check_small_factors( &XX ) ) != 0 )
2844 {
2845 if( ret == 1 )
2846 return( 0 );
2847
2848 return( ret );
2849 }
2850
Janos Follathda31fa12018-09-03 14:45:23 +01002851 return( mpi_miller_rabin( &XX, rounds, f_rng, p_rng ) );
Janos Follathf301d232018-08-14 13:34:01 +01002852}
2853
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002854/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002855 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002856 *
Janos Follathf301d232018-08-14 13:34:01 +01002857 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2858 * be either 1024 bits or 1536 bits long, and flags must contain
2859 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002860 */
Janos Follath7c025a92018-08-14 11:08:41 +01002861int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002862 int (*f_rng)(void *, unsigned char *, size_t),
2863 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002864{
Jethro Beekman66689272018-02-14 19:24:10 -08002865#ifdef MBEDTLS_HAVE_INT64
2866// ceil(2^63.5)
2867#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2868#else
2869// ceil(2^31.5)
2870#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2871#endif
2872 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002873 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002874 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875 mbedtls_mpi_uint r;
2876 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002877
Hanno Becker8ce11a32018-12-19 16:18:52 +00002878 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002879 MPI_VALIDATE_RET( f_rng != NULL );
2880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881 if( nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS )
2882 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002884 mbedtls_mpi_init( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002885
2886 n = BITS_TO_LIMBS( nbits );
2887
Janos Follathda31fa12018-09-03 14:45:23 +01002888 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR ) == 0 )
2889 {
2890 /*
2891 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2892 */
2893 rounds = ( ( nbits >= 1300 ) ? 2 : ( nbits >= 850 ) ? 3 :
2894 ( nbits >= 650 ) ? 4 : ( nbits >= 350 ) ? 8 :
2895 ( nbits >= 250 ) ? 12 : ( nbits >= 150 ) ? 18 : 27 );
2896 }
2897 else
2898 {
2899 /*
2900 * 2^-100 error probability, number of rounds computed based on HAC,
2901 * fact 4.48
2902 */
2903 rounds = ( ( nbits >= 1450 ) ? 4 : ( nbits >= 1150 ) ? 5 :
2904 ( nbits >= 1000 ) ? 6 : ( nbits >= 850 ) ? 7 :
2905 ( nbits >= 750 ) ? 8 : ( nbits >= 500 ) ? 13 :
2906 ( nbits >= 250 ) ? 28 : ( nbits >= 150 ) ? 40 : 51 );
2907 }
2908
Jethro Beekman66689272018-02-14 19:24:10 -08002909 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002910 {
Jethro Beekman66689272018-02-14 19:24:10 -08002911 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
2912 /* make sure generated number is at least (nbits-1)+0.5 bits (FIPS 186-4 §B.3.3 steps 4.4, 5.5) */
2913 if( X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2 ) continue;
2914
2915 k = n * biL;
2916 if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits ) );
2917 X->p[0] |= 1;
2918
Janos Follath7c025a92018-08-14 11:08:41 +01002919 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002920 {
Janos Follatha0b67c22018-09-18 14:48:23 +01002921 ret = mbedtls_mpi_is_prime_ext( X, rounds, f_rng, p_rng );
Jethro Beekman66689272018-02-14 19:24:10 -08002922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002924 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002925 }
Jethro Beekman66689272018-02-14 19:24:10 -08002926 else
Paul Bakker5121ce52009-01-03 21:22:43 +00002927 {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002928 /*
Jethro Beekman66689272018-02-14 19:24:10 -08002929 * An necessary condition for Y and X = 2Y + 1 to be prime
2930 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2931 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002932 */
Jethro Beekman66689272018-02-14 19:24:10 -08002933
2934 X->p[0] |= 2;
2935
2936 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, 3 ) );
2937 if( r == 0 )
2938 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 8 ) );
2939 else if( r == 1 )
2940 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 4 ) );
2941
2942 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
2943 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, X ) );
2944 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, 1 ) );
2945
2946 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002947 {
Jethro Beekman66689272018-02-14 19:24:10 -08002948 /*
2949 * First, check small factors for X and Y
2950 * before doing Miller-Rabin on any of them
2951 */
2952 if( ( ret = mpi_check_small_factors( X ) ) == 0 &&
2953 ( ret = mpi_check_small_factors( &Y ) ) == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002954 ( ret = mpi_miller_rabin( X, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002955 == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002956 ( ret = mpi_miller_rabin( &Y, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002957 == 0 )
Jethro Beekman66689272018-02-14 19:24:10 -08002958 goto cleanup;
2959
2960 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
2961 goto cleanup;
2962
2963 /*
2964 * Next candidates. We want to preserve Y = (X-1) / 2 and
2965 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2966 * so up Y by 6 and X by 12.
2967 */
2968 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 12 ) );
2969 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &Y, &Y, 6 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002970 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002971 }
2972 }
2973
2974cleanup:
2975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976 mbedtls_mpi_free( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002977
2978 return( ret );
2979}
2980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002984
Paul Bakker23986e52011-04-24 08:57:21 +00002985#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002986
2987static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2988{
2989 { 693, 609, 21 },
2990 { 1764, 868, 28 },
2991 { 768454923, 542167814, 1 }
2992};
2993
Paul Bakker5121ce52009-01-03 21:22:43 +00002994/*
2995 * Checkup routine
2996 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997int mbedtls_mpi_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002998{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002999 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00003001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); mbedtls_mpi_init( &X );
3003 mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &U ); mbedtls_mpi_init( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00003004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &A, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003006 "EFE021C2645FD1DC586E69184AF4A31E" \
3007 "D5F53E93B5F123FA41680867BA110131" \
3008 "944FE7952E2517337780CB0DB80E61AA" \
3009 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6" ) );
3010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003012 "B2E7EFD37075B9F03FF989C7C5051C20" \
3013 "34D2A323810251127E7BF8625A4F49A5" \
3014 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
3015 "5B5C25763222FEFCCFC38B832366C29E" ) );
3016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &N, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003018 "0066A198186C18C10B2F5ED9B522752A" \
3019 "9830B69916E535C8F047518A889A43A5" \
3020 "94B6BED27A168D31D4A52F88925AA8F5" ) );
3021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003025 "602AB7ECA597A3D6B56FF9829A5E8B85" \
3026 "9E857EA95A03512E2BAE7391688D264A" \
3027 "A5663B0341DB9CCFD2C4C5F421FEC814" \
3028 "8001B72E848A38CAE1C65F78E56ABDEF" \
3029 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
3030 "ECF677152EF804370C1A305CAF3B5BF1" \
3031 "30879B56C61DE584A0F53A2447A51E" ) );
3032
3033 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034 mbedtls_printf( " MPI test #1 (mul_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003037 {
3038 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003039 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003040
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003041 ret = 1;
3042 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003043 }
3044
3045 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &X, &Y, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003050 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003051 "256567336059E52CAE22925474705F39A94" ) );
3052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &V, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003054 "6613F26162223DF488E9CD48CC132C7A" \
3055 "0AC93C701B001B092E4E5B9F73BCD27B" \
3056 "9EE50D0657C77F374E903CDFA4C642" ) );
3057
3058 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059 mbedtls_printf( " MPI test #2 (div_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ||
3062 mbedtls_mpi_cmp_mpi( &Y, &V ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003063 {
3064 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003066
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003067 ret = 1;
3068 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003069 }
3070
3071 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003074 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &X, &A, &E, &N, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003077 "36E139AEA55215609D2816998ED020BB" \
3078 "BD96C37890F65171D948E9BC7CBAA4D9" \
3079 "325D24D6A3C12710F10A09FA08AB87" ) );
3080
3081 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082 mbedtls_printf( " MPI test #3 (exp_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003085 {
3086 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003088
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003089 ret = 1;
3090 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003091 }
3092
3093 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003099 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
3100 "C3DBA76456363A10869622EAC2DD84EC" \
3101 "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) );
3102
3103 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104 mbedtls_printf( " MPI test #4 (inv_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003107 {
3108 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003110
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003111 ret = 1;
3112 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003113 }
3114
3115 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003117
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003118 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119 mbedtls_printf( " MPI test #5 (simple gcd): " );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003120
Paul Bakker66d5d072014-06-17 16:39:18 +02003121 for( i = 0; i < GCD_PAIR_COUNT; i++ )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &X, gcd_pairs[i][0] ) );
3124 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Y, gcd_pairs[i][1] ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &A, &X, &Y ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 if( mbedtls_mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 )
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003129 {
3130 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003131 mbedtls_printf( "failed at %d\n", i );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003132
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003133 ret = 1;
3134 goto cleanup;
3135 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003136 }
3137
3138 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 mbedtls_printf( "passed\n" );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003140
Paul Bakker5121ce52009-01-03 21:22:43 +00003141cleanup:
3142
3143 if( ret != 0 && verbose != 0 )
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003144 mbedtls_printf( "Unexpected error, return code = %08X\n", (unsigned int) ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &X );
3147 mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &U ); mbedtls_mpi_free( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00003148
3149 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003151
3152 return( ret );
3153}
3154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157#endif /* MBEDTLS_BIGNUM_C */