blob: 08ac4d6e94d44a6d7b199346b048d70c482872d4 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Multi-precision integer library
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
Simon Butcher15b15d12015-11-26 19:35:03 +000019
Paul Bakker5121ce52009-01-03 21:22:43 +000020/*
Simon Butcher15b15d12015-11-26 19:35:03 +000021 * The following sources were referenced in the design of this Multi-precision
22 * Integer library:
Paul Bakker5121ce52009-01-03 21:22:43 +000023 *
Simon Butcher15b15d12015-11-26 19:35:03 +000024 * [1] Handbook of Applied Cryptography - 1997
25 * Menezes, van Oorschot and Vanstone
26 *
27 * [2] Multi-Precision Math
28 * Tom St Denis
29 * https://github.com/libtom/libtommath/blob/develop/tommath.pdf
30 *
31 * [3] GNU Multi-Precision Arithmetic Library
32 * https://gmplib.org/manual/index.html
33 *
Simon Butcherf5ba0452015-12-27 23:01:55 +000034 */
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Gilles Peskinedb09ef62020-06-03 01:43:33 +020036#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/bignum.h"
41#include "mbedtls/bn_mul.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050042#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000043#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Rich Evans00ab4702015-02-06 13:43:58 +000045#include <string.h>
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020049#else
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <stdio.h>
51#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#define mbedtls_printf printf
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020053#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020055#endif
56
Hanno Becker73d7d792018-12-11 10:35:51 +000057#define MPI_VALIDATE_RET( cond ) \
58 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA )
59#define MPI_VALIDATE( cond ) \
60 MBEDTLS_INTERNAL_VALIDATE( cond )
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
Paul Bakker5121ce52009-01-03 21:22:43 +000063#define biL (ciL << 3) /* bits in limb */
64#define biH (ciL << 2) /* half limb size */
65
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +010066#define MPI_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
67
Paul Bakker5121ce52009-01-03 21:22:43 +000068/*
69 * Convert between bits/chars and number of limbs
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +020070 * Divide first in order to avoid potential overflows
Paul Bakker5121ce52009-01-03 21:22:43 +000071 */
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +020072#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
73#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050075/* Implementation that should never be optimized out by the compiler */
Andres Amaya Garcia6698d2f2018-04-24 08:39:07 -050076static void mbedtls_mpi_zeroize( mbedtls_mpi_uint *v, size_t n )
77{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050078 mbedtls_platform_zeroize( v, ciL * n );
79}
80
Paul Bakker5121ce52009-01-03 21:22:43 +000081/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000082 * Initialize one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084void mbedtls_mpi_init( mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +000085{
Hanno Becker73d7d792018-12-11 10:35:51 +000086 MPI_VALIDATE( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +000087
Paul Bakker6c591fa2011-05-05 11:49:20 +000088 X->s = 1;
89 X->n = 0;
90 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000091}
92
93/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000094 * Unallocate one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096void mbedtls_mpi_free( mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +000097{
Paul Bakker6c591fa2011-05-05 11:49:20 +000098 if( X == NULL )
99 return;
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Paul Bakker6c591fa2011-05-05 11:49:20 +0000101 if( X->p != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 {
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200103 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 }
106
Paul Bakker6c591fa2011-05-05 11:49:20 +0000107 X->s = 1;
108 X->n = 0;
109 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000110}
111
112/*
113 * Enlarge to the specified number of limbs
114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000116{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_mpi_uint *p;
Hanno Becker73d7d792018-12-11 10:35:51 +0000118 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200121 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Paul Bakkerf9688572011-05-05 10:00:45 +0000122
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 if( X->n < nblimbs )
124 {
Simon Butcher29176892016-05-20 00:19:09 +0100125 if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( nblimbs, ciL ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200126 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 if( X->p != NULL )
129 {
130 memcpy( p, X->p, X->n * ciL );
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200131 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 }
134
135 X->n = nblimbs;
136 X->p = p;
137 }
138
139 return( 0 );
140}
141
142/*
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100143 * Resize down as much as possible,
144 * while keeping at least the specified number of limbs
145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100147{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_mpi_uint *p;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100149 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000150 MPI_VALIDATE_RET( X != NULL );
151
152 if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
153 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100154
Gilles Peskinee2f563e2020-01-20 21:17:43 +0100155 /* Actually resize up if there are currently fewer than nblimbs limbs. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100156 if( X->n <= nblimbs )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 return( mbedtls_mpi_grow( X, nblimbs ) );
Gilles Peskine322752b2020-01-21 13:59:51 +0100158 /* After this point, then X->n > nblimbs and in particular X->n > 0. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100159
160 for( i = X->n - 1; i > 0; i-- )
161 if( X->p[i] != 0 )
162 break;
163 i++;
164
165 if( i < nblimbs )
166 i = nblimbs;
167
Simon Butcher29176892016-05-20 00:19:09 +0100168 if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( i, ciL ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200169 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100170
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100171 if( X->p != NULL )
172 {
173 memcpy( p, X->p, i * ciL );
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200174 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_free( X->p );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100176 }
177
178 X->n = i;
179 X->p = p;
180
181 return( 0 );
182}
183
Gilles Peskine3130ce22021-06-02 22:17:52 +0200184/* Resize X to have exactly n limbs and set it to 0. */
185static int mbedtls_mpi_resize_clear( mbedtls_mpi *X, size_t limbs )
186{
187 if( limbs == 0 )
188 {
189 mbedtls_mpi_free( X );
190 return( 0 );
191 }
192 else if( X->n == limbs )
193 {
194 memset( X->p, 0, limbs * ciL );
195 X->s = 1;
196 return( 0 );
197 }
198 else
199 {
200 mbedtls_mpi_free( X );
201 return( mbedtls_mpi_grow( X, limbs ) );
202 }
203}
204
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100205/*
Gilles Peskinef643e8e2021-06-08 23:17:42 +0200206 * Copy the contents of Y into X.
207 *
208 * This function is not constant-time. Leading zeros in Y may be removed.
209 *
210 * Ensure that X does not shrink. This is not guaranteed by the public API,
211 * but some code in the bignum module relies on this property, for example
212 * in mbedtls_mpi_exp_mod().
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000215{
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100216 int ret = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000217 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000218 MPI_VALIDATE_RET( X != NULL );
219 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
221 if( X == Y )
222 return( 0 );
223
Gilles Peskinedb420622020-01-20 21:12:50 +0100224 if( Y->n == 0 )
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200225 {
Gilles Peskinef643e8e2021-06-08 23:17:42 +0200226 if( X->n != 0 )
227 {
228 X->s = 1;
229 memset( X->p, 0, X->n * ciL );
230 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200231 return( 0 );
232 }
233
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 for( i = Y->n - 1; i > 0; i-- )
235 if( Y->p[i] != 0 )
236 break;
237 i++;
238
239 X->s = Y->s;
240
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100241 if( X->n < i )
242 {
243 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i ) );
244 }
245 else
246 {
247 memset( X->p + i, 0, ( X->n - i ) * ciL );
248 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000249
Paul Bakker5121ce52009-01-03 21:22:43 +0000250 memcpy( X->p, Y->p, i * ciL );
251
252cleanup:
253
254 return( ret );
255}
256
257/*
258 * Swap the contents of X and Y
259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000261{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000263 MPI_VALIDATE( X != NULL );
264 MPI_VALIDATE( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 memcpy( &T, X, sizeof( mbedtls_mpi ) );
267 memcpy( X, Y, sizeof( mbedtls_mpi ) );
268 memcpy( Y, &T, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000269}
270
271/*
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200272 * Conditionally assign dest = src, without leaking information
273 * about whether the assignment was made or not.
274 * dest and src must be arrays of limbs of size n.
275 * assign must be 0 or 1.
276 */
277static void mpi_safe_cond_assign( size_t n,
278 mbedtls_mpi_uint *dest,
279 const mbedtls_mpi_uint *src,
280 unsigned char assign )
281{
282 size_t i;
283 for( i = 0; i < n; i++ )
284 dest[i] = dest[i] * ( 1 - assign ) + src[i] * assign;
285}
286
287/*
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100288 * Conditionally assign X = Y, without leaking information
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +0100289 * about whether the assignment was made or not.
290 * (Leaking information about the respective sizes of X and Y is ok however.)
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100293{
294 int ret = 0;
295 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000296 MPI_VALIDATE_RET( X != NULL );
297 MPI_VALIDATE_RET( Y != NULL );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100298
Pascal Junodb99183d2015-03-11 16:49:45 +0100299 /* make sure assign is 0 or 1 in a time-constant manner */
300 assign = (assign | (unsigned char)-assign) >> 7;
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100303
Paul Bakker66d5d072014-06-17 16:39:18 +0200304 X->s = X->s * ( 1 - assign ) + Y->s * assign;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100305
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200306 mpi_safe_cond_assign( Y->n, X->p, Y->p, assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100307
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200308 for( i = Y->n; i < X->n; i++ )
Paul Bakker66d5d072014-06-17 16:39:18 +0200309 X->p[i] *= ( 1 - assign );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100310
311cleanup:
312 return( ret );
313}
314
315/*
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100316 * Conditionally swap X and Y, without leaking information
317 * about whether the swap was made or not.
318 * Here it is not ok to simply swap the pointers, which whould lead to
319 * different memory access patterns when X and Y are used afterwards.
320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100322{
323 int ret, s;
324 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 mbedtls_mpi_uint tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +0000326 MPI_VALIDATE_RET( X != NULL );
327 MPI_VALIDATE_RET( Y != NULL );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100328
329 if( X == Y )
330 return( 0 );
331
Pascal Junodb99183d2015-03-11 16:49:45 +0100332 /* make sure swap is 0 or 1 in a time-constant manner */
333 swap = (swap | (unsigned char)-swap) >> 7;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
336 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( Y, X->n ) );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100337
338 s = X->s;
Paul Bakker66d5d072014-06-17 16:39:18 +0200339 X->s = X->s * ( 1 - swap ) + Y->s * swap;
340 Y->s = Y->s * ( 1 - swap ) + s * swap;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100341
342
343 for( i = 0; i < X->n; i++ )
344 {
345 tmp = X->p[i];
Paul Bakker66d5d072014-06-17 16:39:18 +0200346 X->p[i] = X->p[i] * ( 1 - swap ) + Y->p[i] * swap;
347 Y->p[i] = Y->p[i] * ( 1 - swap ) + tmp * swap;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100348 }
349
350cleanup:
351 return( ret );
352}
353
354/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000355 * Set value from integer
356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +0000358{
Janos Follath24eed8d2019-11-22 13:21:35 +0000359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker73d7d792018-12-11 10:35:51 +0000360 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 memset( X->p, 0, X->n * ciL );
364
365 X->p[0] = ( z < 0 ) ? -z : z;
366 X->s = ( z < 0 ) ? -1 : 1;
367
368cleanup:
369
370 return( ret );
371}
372
373/*
Paul Bakker2f5947e2011-05-18 15:47:11 +0000374 * Get a specific bit
375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000377{
Hanno Becker73d7d792018-12-11 10:35:51 +0000378 MPI_VALIDATE_RET( X != NULL );
379
Paul Bakker2f5947e2011-05-18 15:47:11 +0000380 if( X->n * biL <= pos )
381 return( 0 );
382
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200383 return( ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000384}
385
Gilles Peskine11cdb052018-11-20 16:47:47 +0100386/* Get a specific byte, without range checks. */
387#define GET_BYTE( X, i ) \
388 ( ( ( X )->p[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff )
389
Paul Bakker2f5947e2011-05-18 15:47:11 +0000390/*
391 * Set a bit to a specific value of 0 or 1
392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000394{
395 int ret = 0;
396 size_t off = pos / biL;
397 size_t idx = pos % biL;
Hanno Becker73d7d792018-12-11 10:35:51 +0000398 MPI_VALIDATE_RET( X != NULL );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000399
400 if( val != 0 && val != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker9af723c2014-05-01 13:03:14 +0200402
Paul Bakker2f5947e2011-05-18 15:47:11 +0000403 if( X->n * biL <= pos )
404 {
405 if( val == 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200406 return( 0 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, off + 1 ) );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000409 }
410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 X->p[off] &= ~( (mbedtls_mpi_uint) 0x01 << idx );
412 X->p[off] |= (mbedtls_mpi_uint) val << idx;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000413
414cleanup:
Paul Bakker9af723c2014-05-01 13:03:14 +0200415
Paul Bakker2f5947e2011-05-18 15:47:11 +0000416 return( ret );
417}
418
419/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200420 * Return the number of less significant zero-bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422size_t mbedtls_mpi_lsb( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000423{
Paul Bakker23986e52011-04-24 08:57:21 +0000424 size_t i, j, count = 0;
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000425 MBEDTLS_INTERNAL_VALIDATE_RET( X != NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
427 for( i = 0; i < X->n; i++ )
Paul Bakkerf9688572011-05-05 10:00:45 +0000428 for( j = 0; j < biL; j++, count++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 if( ( ( X->p[i] >> j ) & 1 ) != 0 )
430 return( count );
431
432 return( 0 );
433}
434
435/*
Simon Butcher15b15d12015-11-26 19:35:03 +0000436 * Count leading zero bits in a given integer
437 */
438static size_t mbedtls_clz( const mbedtls_mpi_uint x )
439{
440 size_t j;
Manuel Pégourié-Gonnarde3e8edf2015-12-01 09:31:52 +0100441 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
Simon Butcher15b15d12015-11-26 19:35:03 +0000442
443 for( j = 0; j < biL; j++ )
444 {
445 if( x & mask ) break;
446
447 mask >>= 1;
448 }
449
450 return j;
451}
452
453/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200454 * Return the number of bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200456size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000457{
Paul Bakker23986e52011-04-24 08:57:21 +0000458 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +0000459
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200460 if( X->n == 0 )
461 return( 0 );
462
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 for( i = X->n - 1; i > 0; i-- )
464 if( X->p[i] != 0 )
465 break;
466
Simon Butcher15b15d12015-11-26 19:35:03 +0000467 j = biL - mbedtls_clz( X->p[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
Paul Bakker23986e52011-04-24 08:57:21 +0000469 return( ( i * biL ) + j );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470}
471
472/*
473 * Return the total size in bytes
474 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475size_t mbedtls_mpi_size( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000476{
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200477 return( ( mbedtls_mpi_bitlen( X ) + 7 ) >> 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000478}
479
480/*
481 * Convert an ASCII character to digit value
482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483static int mpi_get_digit( mbedtls_mpi_uint *d, int radix, char c )
Paul Bakker5121ce52009-01-03 21:22:43 +0000484{
485 *d = 255;
486
487 if( c >= 0x30 && c <= 0x39 ) *d = c - 0x30;
488 if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37;
489 if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57;
490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 if( *d >= (mbedtls_mpi_uint) radix )
492 return( MBEDTLS_ERR_MPI_INVALID_CHARACTER );
Paul Bakker5121ce52009-01-03 21:22:43 +0000493
494 return( 0 );
495}
496
497/*
498 * Import from an ASCII string
499 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000501{
Janos Follath24eed8d2019-11-22 13:21:35 +0000502 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000503 size_t i, j, slen, n;
Gilles Peskine80f56732021-04-03 18:26:13 +0200504 int sign = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_mpi_uint d;
506 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000507 MPI_VALIDATE_RET( X != NULL );
508 MPI_VALIDATE_RET( s != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
510 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000511 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000514
Gilles Peskined4876132021-06-08 18:32:34 +0200515 if( s[0] == 0 )
516 {
517 mbedtls_mpi_free( X );
518 return( 0 );
519 }
520
Gilles Peskine80f56732021-04-03 18:26:13 +0200521 if( s[0] == '-' )
522 {
523 ++s;
524 sign = -1;
525 }
526
Paul Bakkerff60ee62010-03-16 21:09:09 +0000527 slen = strlen( s );
528
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 if( radix == 16 )
530 {
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +0100531 if( slen > MPI_SIZE_T_MAX >> 2 )
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +0200532 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
533
Paul Bakkerff60ee62010-03-16 21:09:09 +0000534 n = BITS_TO_LIMBS( slen << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n ) );
537 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000538
Paul Bakker23986e52011-04-24 08:57:21 +0000539 for( i = slen, j = 0; i > 0; i--, j++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200542 X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000543 }
544 }
545 else
546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
Paul Bakkerff60ee62010-03-16 21:09:09 +0000549 for( i = 0; i < slen; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
552 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T, X, radix ) );
Gilles Peskine80f56732021-04-03 18:26:13 +0200553 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, &T, d ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000554 }
555 }
556
Gilles Peskine80f56732021-04-03 18:26:13 +0200557 if( sign < 0 && mbedtls_mpi_bitlen( X ) != 0 )
558 X->s = -1;
559
Paul Bakker5121ce52009-01-03 21:22:43 +0000560cleanup:
561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
564 return( ret );
565}
566
567/*
Ron Eldora16fa292018-11-20 14:07:01 +0200568 * Helper to write the digits high-order first.
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 */
Ron Eldora16fa292018-11-20 14:07:01 +0200570static int mpi_write_hlp( mbedtls_mpi *X, int radix,
571 char **p, const size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000572{
Janos Follath24eed8d2019-11-22 13:21:35 +0000573 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_mpi_uint r;
Ron Eldora16fa292018-11-20 14:07:01 +0200575 size_t length = 0;
576 char *p_end = *p + buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
Ron Eldora16fa292018-11-20 14:07:01 +0200578 do
579 {
580 if( length >= buflen )
581 {
582 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
583 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
Ron Eldora16fa292018-11-20 14:07:01 +0200585 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) );
586 MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) );
587 /*
588 * Write the residue in the current position, as an ASCII character.
589 */
590 if( r < 0xA )
591 *(--p_end) = (char)( '0' + r );
592 else
593 *(--p_end) = (char)( 'A' + ( r - 0xA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
Ron Eldora16fa292018-11-20 14:07:01 +0200595 length++;
596 } while( mbedtls_mpi_cmp_int( X, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000597
Ron Eldora16fa292018-11-20 14:07:01 +0200598 memmove( *p, p_end, length );
599 *p += length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
601cleanup:
602
603 return( ret );
604}
605
606/*
607 * Export into an ASCII string
608 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100609int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
610 char *buf, size_t buflen, size_t *olen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000611{
Paul Bakker23986e52011-04-24 08:57:21 +0000612 int ret = 0;
613 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000616 MPI_VALIDATE_RET( X != NULL );
617 MPI_VALIDATE_RET( olen != NULL );
618 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
620 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000621 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000622
Hanno Becker23cfea02019-02-04 09:45:07 +0000623 n = mbedtls_mpi_bitlen( X ); /* Number of bits necessary to present `n`. */
624 if( radix >= 4 ) n >>= 1; /* Number of 4-adic digits necessary to present
625 * `n`. If radix > 4, this might be a strict
626 * overapproximation of the number of
627 * radix-adic digits needed to present `n`. */
628 if( radix >= 16 ) n >>= 1; /* Number of hexadecimal digits necessary to
629 * present `n`. */
630
Janos Follath80470622019-03-06 13:43:02 +0000631 n += 1; /* Terminating null byte */
Hanno Becker23cfea02019-02-04 09:45:07 +0000632 n += 1; /* Compensate for the divisions above, which round down `n`
633 * in case it's not even. */
634 n += 1; /* Potential '-'-sign. */
635 n += ( n & 1 ); /* Make n even to have enough space for hexadecimal writing,
636 * which always uses an even number of hex-digits. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000637
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100638 if( buflen < n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100640 *olen = n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 }
643
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100644 p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
647 if( X->s == -1 )
Hanno Beckerc983c812019-02-01 16:41:30 +0000648 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000649 *p++ = '-';
Hanno Beckerc983c812019-02-01 16:41:30 +0000650 buflen--;
651 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
653 if( radix == 16 )
654 {
Paul Bakker23986e52011-04-24 08:57:21 +0000655 int c;
656 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000657
Paul Bakker23986e52011-04-24 08:57:21 +0000658 for( i = X->n, k = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000659 {
Paul Bakker23986e52011-04-24 08:57:21 +0000660 for( j = ciL; j > 0; j-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 {
Paul Bakker23986e52011-04-24 08:57:21 +0000662 c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
Paul Bakker6c343d72014-07-10 14:36:19 +0200664 if( c == 0 && k == 0 && ( i + j ) != 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 continue;
666
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000667 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000668 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000669 k = 1;
670 }
671 }
672 }
673 else
674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T, X ) );
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000676
677 if( T.s == -1 )
678 T.s = 1;
679
Ron Eldora16fa292018-11-20 14:07:01 +0200680 MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p, buflen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 }
682
683 *p++ = '\0';
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100684 *olen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000685
686cleanup:
687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000689
690 return( ret );
691}
692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000694/*
695 * Read X from an opened file
696 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin )
Paul Bakker5121ce52009-01-03 21:22:43 +0000698{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_mpi_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000700 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000701 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000702 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000703 * Buffer should have space for (short) label and decimal formatted MPI,
704 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Paul Bakker5121ce52009-01-03 21:22:43 +0000707
Hanno Becker73d7d792018-12-11 10:35:51 +0000708 MPI_VALIDATE_RET( X != NULL );
709 MPI_VALIDATE_RET( fin != NULL );
710
711 if( radix < 2 || radix > 16 )
712 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
713
Paul Bakker5121ce52009-01-03 21:22:43 +0000714 memset( s, 0, sizeof( s ) );
715 if( fgets( s, sizeof( s ) - 1, fin ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000717
718 slen = strlen( s );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000719 if( slen == sizeof( s ) - 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000721
Hanno Beckerb2034b72017-04-26 11:46:46 +0100722 if( slen > 0 && s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
723 if( slen > 0 && s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
Paul Bakker5121ce52009-01-03 21:22:43 +0000724
725 p = s + slen;
Hanno Beckerb2034b72017-04-26 11:46:46 +0100726 while( p-- > s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000727 if( mpi_get_digit( &d, radix, *p ) != 0 )
728 break;
729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 return( mbedtls_mpi_read_string( X, radix, p + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000731}
732
733/*
734 * Write X into an opened file (or stdout if fout == NULL)
735 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout )
Paul Bakker5121ce52009-01-03 21:22:43 +0000737{
Janos Follath24eed8d2019-11-22 13:21:35 +0000738 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000739 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000740 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000741 * Buffer should have space for (short) label and decimal formatted MPI,
742 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000743 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Hanno Becker73d7d792018-12-11 10:35:51 +0000745 MPI_VALIDATE_RET( X != NULL );
746
747 if( radix < 2 || radix > 16 )
748 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100750 memset( s, 0, sizeof( s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100752 MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, sizeof( s ) - 2, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000753
754 if( p == NULL ) p = "";
755
756 plen = strlen( p );
757 slen = strlen( s );
758 s[slen++] = '\r';
759 s[slen++] = '\n';
760
761 if( fout != NULL )
762 {
763 if( fwrite( p, 1, plen, fout ) != plen ||
764 fwrite( s, 1, slen, fout ) != slen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000766 }
767 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_printf( "%s%s", p, s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
770cleanup:
771
772 return( ret );
773}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000775
Hanno Beckerda1655a2017-10-18 14:21:44 +0100776
777/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
778 * into the storage form used by mbedtls_mpi. */
Hanno Beckerf8720072018-11-08 11:53:49 +0000779
780static mbedtls_mpi_uint mpi_uint_bigendian_to_host_c( mbedtls_mpi_uint x )
781{
782 uint8_t i;
Hanno Becker031d6332019-05-01 17:09:11 +0100783 unsigned char *x_ptr;
Hanno Beckerf8720072018-11-08 11:53:49 +0000784 mbedtls_mpi_uint tmp = 0;
Hanno Becker031d6332019-05-01 17:09:11 +0100785
786 for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
787 {
788 tmp <<= CHAR_BIT;
789 tmp |= (mbedtls_mpi_uint) *x_ptr;
790 }
791
Hanno Beckerf8720072018-11-08 11:53:49 +0000792 return( tmp );
793}
794
795static mbedtls_mpi_uint mpi_uint_bigendian_to_host( mbedtls_mpi_uint x )
796{
797#if defined(__BYTE_ORDER__)
798
799/* Nothing to do on bigendian systems. */
800#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
801 return( x );
802#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
803
804#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
805
806/* For GCC and Clang, have builtins for byte swapping. */
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000807#if defined(__GNUC__) && defined(__GNUC_PREREQ)
808#if __GNUC_PREREQ(4,3)
Hanno Beckerf8720072018-11-08 11:53:49 +0000809#define have_bswap
810#endif
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000811#endif
812
813#if defined(__clang__) && defined(__has_builtin)
814#if __has_builtin(__builtin_bswap32) && \
815 __has_builtin(__builtin_bswap64)
816#define have_bswap
817#endif
818#endif
819
Hanno Beckerf8720072018-11-08 11:53:49 +0000820#if defined(have_bswap)
821 /* The compiler is hopefully able to statically evaluate this! */
822 switch( sizeof(mbedtls_mpi_uint) )
823 {
824 case 4:
825 return( __builtin_bswap32(x) );
826 case 8:
827 return( __builtin_bswap64(x) );
828 }
829#endif
830#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
831#endif /* __BYTE_ORDER__ */
832
833 /* Fall back to C-based reordering if we don't know the byte order
834 * or we couldn't use a compiler-specific builtin. */
835 return( mpi_uint_bigendian_to_host_c( x ) );
836}
837
Hanno Becker2be8a552018-10-25 12:40:09 +0100838static void mpi_bigendian_to_host( mbedtls_mpi_uint * const p, size_t limbs )
Hanno Beckerda1655a2017-10-18 14:21:44 +0100839{
Hanno Beckerda1655a2017-10-18 14:21:44 +0100840 mbedtls_mpi_uint *cur_limb_left;
841 mbedtls_mpi_uint *cur_limb_right;
Hanno Becker2be8a552018-10-25 12:40:09 +0100842 if( limbs == 0 )
843 return;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100844
845 /*
846 * Traverse limbs and
847 * - adapt byte-order in each limb
848 * - swap the limbs themselves.
849 * For that, simultaneously traverse the limbs from left to right
850 * and from right to left, as long as the left index is not bigger
851 * than the right index (it's not a problem if limbs is odd and the
852 * indices coincide in the last iteration).
853 */
Hanno Beckerda1655a2017-10-18 14:21:44 +0100854 for( cur_limb_left = p, cur_limb_right = p + ( limbs - 1 );
855 cur_limb_left <= cur_limb_right;
856 cur_limb_left++, cur_limb_right-- )
857 {
Hanno Beckerf8720072018-11-08 11:53:49 +0000858 mbedtls_mpi_uint tmp;
859 /* Note that if cur_limb_left == cur_limb_right,
860 * this code effectively swaps the bytes only once. */
861 tmp = mpi_uint_bigendian_to_host( *cur_limb_left );
862 *cur_limb_left = mpi_uint_bigendian_to_host( *cur_limb_right );
863 *cur_limb_right = tmp;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100864 }
Hanno Beckerda1655a2017-10-18 14:21:44 +0100865}
866
Paul Bakker5121ce52009-01-03 21:22:43 +0000867/*
Janos Follatha778a942019-02-13 10:28:28 +0000868 * Import X from unsigned binary data, little endian
869 */
870int mbedtls_mpi_read_binary_le( mbedtls_mpi *X,
871 const unsigned char *buf, size_t buflen )
872{
Janos Follath24eed8d2019-11-22 13:21:35 +0000873 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follatha778a942019-02-13 10:28:28 +0000874 size_t i;
875 size_t const limbs = CHARS_TO_LIMBS( buflen );
876
877 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine3130ce22021-06-02 22:17:52 +0200878 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Janos Follatha778a942019-02-13 10:28:28 +0000879
880 for( i = 0; i < buflen; i++ )
881 X->p[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
882
883cleanup:
884
Janos Follath171a7ef2019-02-15 16:17:45 +0000885 /*
886 * This function is also used to import keys. However, wiping the buffers
887 * upon failure is not necessary because failure only can happen before any
888 * input is copied.
889 */
Janos Follatha778a942019-02-13 10:28:28 +0000890 return( ret );
891}
892
893/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000894 * Import X from unsigned binary data, big endian
895 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000897{
Janos Follath24eed8d2019-11-22 13:21:35 +0000898 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100899 size_t const limbs = CHARS_TO_LIMBS( buflen );
900 size_t const overhead = ( limbs * ciL ) - buflen;
901 unsigned char *Xp;
Paul Bakker5121ce52009-01-03 21:22:43 +0000902
Hanno Becker8ce11a32018-12-19 16:18:52 +0000903 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +0000904 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
905
Hanno Becker073c1992017-10-17 15:17:27 +0100906 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine3130ce22021-06-02 22:17:52 +0200907 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
Gilles Peskine3130ce22021-06-02 22:17:52 +0200909 /* Avoid calling `memcpy` with NULL source or destination argument,
Hanno Becker0e810b92019-01-03 17:13:11 +0000910 * even if buflen is 0. */
Gilles Peskine3130ce22021-06-02 22:17:52 +0200911 if( buflen != 0 )
Hanno Becker0e810b92019-01-03 17:13:11 +0000912 {
913 Xp = (unsigned char*) X->p;
914 memcpy( Xp + overhead, buf, buflen );
Hanno Beckerda1655a2017-10-18 14:21:44 +0100915
Hanno Becker0e810b92019-01-03 17:13:11 +0000916 mpi_bigendian_to_host( X->p, limbs );
917 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000918
919cleanup:
920
Janos Follath171a7ef2019-02-15 16:17:45 +0000921 /*
922 * This function is also used to import keys. However, wiping the buffers
923 * upon failure is not necessary because failure only can happen before any
924 * input is copied.
925 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000926 return( ret );
927}
928
929/*
Janos Follathe344d0f2019-02-19 16:17:40 +0000930 * Export X into unsigned binary data, little endian
931 */
932int mbedtls_mpi_write_binary_le( const mbedtls_mpi *X,
933 unsigned char *buf, size_t buflen )
934{
935 size_t stored_bytes = X->n * ciL;
936 size_t bytes_to_copy;
937 size_t i;
938
939 if( stored_bytes < buflen )
940 {
941 bytes_to_copy = stored_bytes;
942 }
943 else
944 {
945 bytes_to_copy = buflen;
946
947 /* The output buffer is smaller than the allocated size of X.
948 * However X may fit if its leading bytes are zero. */
949 for( i = bytes_to_copy; i < stored_bytes; i++ )
950 {
951 if( GET_BYTE( X, i ) != 0 )
952 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
953 }
954 }
955
956 for( i = 0; i < bytes_to_copy; i++ )
957 buf[i] = GET_BYTE( X, i );
958
959 if( stored_bytes < buflen )
960 {
961 /* Write trailing 0 bytes */
962 memset( buf + stored_bytes, 0, buflen - stored_bytes );
963 }
964
965 return( 0 );
966}
967
968/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000969 * Export X into unsigned binary data, big endian
970 */
Gilles Peskine11cdb052018-11-20 16:47:47 +0100971int mbedtls_mpi_write_binary( const mbedtls_mpi *X,
972 unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000973{
Hanno Becker73d7d792018-12-11 10:35:51 +0000974 size_t stored_bytes;
Gilles Peskine11cdb052018-11-20 16:47:47 +0100975 size_t bytes_to_copy;
976 unsigned char *p;
977 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000978
Hanno Becker73d7d792018-12-11 10:35:51 +0000979 MPI_VALIDATE_RET( X != NULL );
980 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
981
982 stored_bytes = X->n * ciL;
983
Gilles Peskine11cdb052018-11-20 16:47:47 +0100984 if( stored_bytes < buflen )
985 {
986 /* There is enough space in the output buffer. Write initial
987 * null bytes and record the position at which to start
988 * writing the significant bytes. In this case, the execution
989 * trace of this function does not depend on the value of the
990 * number. */
991 bytes_to_copy = stored_bytes;
992 p = buf + buflen - stored_bytes;
993 memset( buf, 0, buflen - stored_bytes );
994 }
995 else
996 {
997 /* The output buffer is smaller than the allocated size of X.
998 * However X may fit if its leading bytes are zero. */
999 bytes_to_copy = buflen;
1000 p = buf;
1001 for( i = bytes_to_copy; i < stored_bytes; i++ )
1002 {
1003 if( GET_BYTE( X, i ) != 0 )
1004 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
1005 }
1006 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001007
Gilles Peskine11cdb052018-11-20 16:47:47 +01001008 for( i = 0; i < bytes_to_copy; i++ )
1009 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
Paul Bakker5121ce52009-01-03 21:22:43 +00001010
1011 return( 0 );
1012}
1013
1014/*
1015 * Left-shift: X <<= count
1016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +00001018{
Janos Follath24eed8d2019-11-22 13:21:35 +00001019 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001020 size_t i, v0, t1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +00001022 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001023
1024 v0 = count / (biL );
1025 t1 = count & (biL - 1);
1026
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001027 i = mbedtls_mpi_bitlen( X ) + count;
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
Paul Bakkerf9688572011-05-05 10:00:45 +00001029 if( X->n * biL < i )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001031
1032 ret = 0;
1033
1034 /*
1035 * shift by count / limb_size
1036 */
1037 if( v0 > 0 )
1038 {
Paul Bakker23986e52011-04-24 08:57:21 +00001039 for( i = X->n; i > v0; i-- )
1040 X->p[i - 1] = X->p[i - v0 - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Paul Bakker23986e52011-04-24 08:57:21 +00001042 for( ; i > 0; i-- )
1043 X->p[i - 1] = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001044 }
1045
1046 /*
1047 * shift by count % limb_size
1048 */
1049 if( t1 > 0 )
1050 {
1051 for( i = v0; i < X->n; i++ )
1052 {
1053 r1 = X->p[i] >> (biL - t1);
1054 X->p[i] <<= t1;
1055 X->p[i] |= r0;
1056 r0 = r1;
1057 }
1058 }
1059
1060cleanup:
1061
1062 return( ret );
1063}
1064
1065/*
1066 * Right-shift: X >>= count
1067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +00001069{
Paul Bakker23986e52011-04-24 08:57:21 +00001070 size_t i, v0, v1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +00001072 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001073
1074 v0 = count / biL;
1075 v1 = count & (biL - 1);
1076
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001077 if( v0 > X->n || ( v0 == X->n && v1 > 0 ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 return mbedtls_mpi_lset( X, 0 );
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001079
Paul Bakker5121ce52009-01-03 21:22:43 +00001080 /*
1081 * shift by count / limb_size
1082 */
1083 if( v0 > 0 )
1084 {
1085 for( i = 0; i < X->n - v0; i++ )
1086 X->p[i] = X->p[i + v0];
1087
1088 for( ; i < X->n; i++ )
1089 X->p[i] = 0;
1090 }
1091
1092 /*
1093 * shift by count % limb_size
1094 */
1095 if( v1 > 0 )
1096 {
Paul Bakker23986e52011-04-24 08:57:21 +00001097 for( i = X->n; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001098 {
Paul Bakker23986e52011-04-24 08:57:21 +00001099 r1 = X->p[i - 1] << (biL - v1);
1100 X->p[i - 1] >>= v1;
1101 X->p[i - 1] |= r0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001102 r0 = r1;
1103 }
1104 }
1105
1106 return( 0 );
1107}
1108
1109/*
1110 * Compare unsigned values
1111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001113{
Paul Bakker23986e52011-04-24 08:57:21 +00001114 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001115 MPI_VALIDATE_RET( X != NULL );
1116 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001117
Paul Bakker23986e52011-04-24 08:57:21 +00001118 for( i = X->n; i > 0; i-- )
1119 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001120 break;
1121
Paul Bakker23986e52011-04-24 08:57:21 +00001122 for( j = Y->n; j > 0; j-- )
1123 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001124 break;
1125
Paul Bakker23986e52011-04-24 08:57:21 +00001126 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001127 return( 0 );
1128
1129 if( i > j ) return( 1 );
1130 if( j > i ) return( -1 );
1131
Paul Bakker23986e52011-04-24 08:57:21 +00001132 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001133 {
Paul Bakker23986e52011-04-24 08:57:21 +00001134 if( X->p[i - 1] > Y->p[i - 1] ) return( 1 );
1135 if( X->p[i - 1] < Y->p[i - 1] ) return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001136 }
1137
1138 return( 0 );
1139}
1140
1141/*
1142 * Compare signed values
1143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001145{
Paul Bakker23986e52011-04-24 08:57:21 +00001146 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001147 MPI_VALIDATE_RET( X != NULL );
1148 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001149
Paul Bakker23986e52011-04-24 08:57:21 +00001150 for( i = X->n; i > 0; i-- )
1151 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001152 break;
1153
Paul Bakker23986e52011-04-24 08:57:21 +00001154 for( j = Y->n; j > 0; j-- )
1155 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001156 break;
1157
Paul Bakker23986e52011-04-24 08:57:21 +00001158 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001159 return( 0 );
1160
1161 if( i > j ) return( X->s );
Paul Bakker0c8f73b2012-03-22 14:08:57 +00001162 if( j > i ) return( -Y->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001163
1164 if( X->s > 0 && Y->s < 0 ) return( 1 );
1165 if( Y->s > 0 && X->s < 0 ) return( -1 );
1166
Paul Bakker23986e52011-04-24 08:57:21 +00001167 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001168 {
Paul Bakker23986e52011-04-24 08:57:21 +00001169 if( X->p[i - 1] > Y->p[i - 1] ) return( X->s );
1170 if( X->p[i - 1] < Y->p[i - 1] ) return( -X->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001171 }
1172
1173 return( 0 );
1174}
1175
Janos Follath3f6f0e42019-10-14 09:09:32 +01001176/** Decide if an integer is less than the other, without branches.
1177 *
1178 * \param x First integer.
1179 * \param y Second integer.
1180 *
1181 * \return 1 if \p x is less than \p y, 0 otherwise
1182 */
Janos Follath0e5532d2019-10-11 14:21:53 +01001183static unsigned ct_lt_mpi_uint( const mbedtls_mpi_uint x,
1184 const mbedtls_mpi_uint y )
Janos Follathee6abce2019-09-05 14:47:19 +01001185{
1186 mbedtls_mpi_uint ret;
1187 mbedtls_mpi_uint cond;
1188
1189 /*
1190 * Check if the most significant bits (MSB) of the operands are different.
1191 */
1192 cond = ( x ^ y );
1193 /*
1194 * If the MSB are the same then the difference x-y will be negative (and
1195 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
1196 */
1197 ret = ( x - y ) & ~cond;
1198 /*
1199 * If the MSB are different, then the operand with the MSB of 1 is the
1200 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
1201 * the MSB of y is 0.)
1202 */
1203 ret |= y & cond;
1204
1205
Janos Follatha0f732b2019-10-14 08:59:14 +01001206 ret = ret >> ( biL - 1 );
Janos Follathee6abce2019-09-05 14:47:19 +01001207
Janos Follath67ce6472019-10-29 15:08:46 +00001208 return (unsigned) ret;
Janos Follathee6abce2019-09-05 14:47:19 +01001209}
1210
1211/*
1212 * Compare signed values in constant time
1213 */
Janos Follath0e5532d2019-10-11 14:21:53 +01001214int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y,
1215 unsigned *ret )
Janos Follathee6abce2019-09-05 14:47:19 +01001216{
1217 size_t i;
Janos Follathbb5147f2019-10-28 12:23:18 +00001218 /* The value of any of these variables is either 0 or 1 at all times. */
Janos Follath5e614ce2019-10-28 12:31:34 +00001219 unsigned cond, done, X_is_negative, Y_is_negative;
Janos Follathee6abce2019-09-05 14:47:19 +01001220
1221 MPI_VALIDATE_RET( X != NULL );
1222 MPI_VALIDATE_RET( Y != NULL );
1223 MPI_VALIDATE_RET( ret != NULL );
1224
1225 if( X->n != Y->n )
1226 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1227
1228 /*
Janos Follath73ba9ec2019-10-28 12:12:15 +00001229 * Set sign_N to 1 if N >= 0, 0 if N < 0.
1230 * We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0.
Janos Follathee6abce2019-09-05 14:47:19 +01001231 */
Janos Follath5e614ce2019-10-28 12:31:34 +00001232 X_is_negative = ( X->s & 2 ) >> 1;
1233 Y_is_negative = ( Y->s & 2 ) >> 1;
Janos Follath0e5532d2019-10-11 14:21:53 +01001234
1235 /*
1236 * If the signs are different, then the positive operand is the bigger.
Janos Follath5e614ce2019-10-28 12:31:34 +00001237 * That is if X is negative (X_is_negative == 1), then X < Y is true and it
1238 * is false if X is positive (X_is_negative == 0).
Janos Follath0e5532d2019-10-11 14:21:53 +01001239 */
Janos Follath5e614ce2019-10-28 12:31:34 +00001240 cond = ( X_is_negative ^ Y_is_negative );
1241 *ret = cond & X_is_negative;
Janos Follath0e5532d2019-10-11 14:21:53 +01001242
1243 /*
Janos Follathbb5147f2019-10-28 12:23:18 +00001244 * This is a constant-time function. We might have the result, but we still
Janos Follath0e5532d2019-10-11 14:21:53 +01001245 * need to go through the loop. Record if we have the result already.
1246 */
Janos Follathee6abce2019-09-05 14:47:19 +01001247 done = cond;
1248
1249 for( i = X->n; i > 0; i-- )
1250 {
1251 /*
Janos Follath30702422019-11-05 12:24:52 +00001252 * If Y->p[i - 1] < X->p[i - 1] then X < Y is true if and only if both
1253 * X and Y are negative.
Janos Follath0e5532d2019-10-11 14:21:53 +01001254 *
1255 * Again even if we can make a decision, we just mark the result and
1256 * the fact that we are done and continue looping.
Janos Follathee6abce2019-09-05 14:47:19 +01001257 */
Janos Follath30702422019-11-05 12:24:52 +00001258 cond = ct_lt_mpi_uint( Y->p[i - 1], X->p[i - 1] );
1259 *ret |= cond & ( 1 - done ) & X_is_negative;
Janos Follathc50e6d52019-10-28 12:37:21 +00001260 done |= cond;
Janos Follathee6abce2019-09-05 14:47:19 +01001261
1262 /*
Janos Follath30702422019-11-05 12:24:52 +00001263 * If X->p[i - 1] < Y->p[i - 1] then X < Y is true if and only if both
1264 * X and Y are positive.
Janos Follath0e5532d2019-10-11 14:21:53 +01001265 *
1266 * Again even if we can make a decision, we just mark the result and
1267 * the fact that we are done and continue looping.
Janos Follathee6abce2019-09-05 14:47:19 +01001268 */
Janos Follath30702422019-11-05 12:24:52 +00001269 cond = ct_lt_mpi_uint( X->p[i - 1], Y->p[i - 1] );
1270 *ret |= cond & ( 1 - done ) & ( 1 - X_is_negative );
Janos Follathc50e6d52019-10-28 12:37:21 +00001271 done |= cond;
Janos Follathee6abce2019-09-05 14:47:19 +01001272 }
1273
1274 return( 0 );
1275}
1276
Paul Bakker5121ce52009-01-03 21:22:43 +00001277/*
1278 * Compare signed values
1279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +00001281{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 mbedtls_mpi Y;
1283 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001284 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001285
1286 *p = ( z < 0 ) ? -z : z;
1287 Y.s = ( z < 0 ) ? -1 : 1;
1288 Y.n = 1;
1289 Y.p = p;
1290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 return( mbedtls_mpi_cmp_mpi( X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001292}
1293
1294/*
1295 * Unsigned addition: X = |A| + |B| (HAC 14.7)
1296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001298{
Janos Follath24eed8d2019-11-22 13:21:35 +00001299 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001300 size_t i, j;
Janos Follath6c922682015-10-30 17:43:11 +01001301 mbedtls_mpi_uint *o, *p, c, tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +00001302 MPI_VALIDATE_RET( X != NULL );
1303 MPI_VALIDATE_RET( A != NULL );
1304 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001305
1306 if( X == B )
1307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 const mbedtls_mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 }
1310
1311 if( X != A )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
Paul Bakker9af723c2014-05-01 13:03:14 +02001313
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001314 /*
1315 * X should always be positive as a result of unsigned additions.
1316 */
1317 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001318
Paul Bakker23986e52011-04-24 08:57:21 +00001319 for( j = B->n; j > 0; j-- )
1320 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 break;
1322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001324
1325 o = B->p; p = X->p; c = 0;
1326
Janos Follath6c922682015-10-30 17:43:11 +01001327 /*
1328 * tmp is used because it might happen that p == o
1329 */
Paul Bakker23986e52011-04-24 08:57:21 +00001330 for( i = 0; i < j; i++, o++, p++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 {
Janos Follath6c922682015-10-30 17:43:11 +01001332 tmp= *o;
Paul Bakker5121ce52009-01-03 21:22:43 +00001333 *p += c; c = ( *p < c );
Janos Follath6c922682015-10-30 17:43:11 +01001334 *p += tmp; c += ( *p < tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 }
1336
1337 while( c != 0 )
1338 {
1339 if( i >= X->n )
1340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 p = X->p + i;
1343 }
1344
Paul Bakker2d319fd2012-09-16 21:34:26 +00001345 *p += c; c = ( *p < c ); i++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 }
1347
1348cleanup:
1349
1350 return( ret );
1351}
1352
Gilles Peskine09ec10a2020-06-09 10:39:38 +02001353/**
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001354 * Helper for mbedtls_mpi subtraction.
1355 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001356 * Calculate l - r where l and r have the same size.
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001357 * This function operates modulo (2^ciL)^n and returns the carry
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001358 * (1 if there was a wraparound, i.e. if `l < r`, and 0 otherwise).
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001359 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001360 * d may be aliased to l or r.
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001361 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001362 * \param n Number of limbs of \p d, \p l and \p r.
1363 * \param[out] d The result of the subtraction.
1364 * \param[in] l The left operand.
1365 * \param[in] r The right operand.
1366 *
1367 * \return 1 if `l < r`.
1368 * 0 if `l >= r`.
Paul Bakker5121ce52009-01-03 21:22:43 +00001369 */
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001370static mbedtls_mpi_uint mpi_sub_hlp( size_t n,
1371 mbedtls_mpi_uint *d,
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001372 const mbedtls_mpi_uint *l,
1373 const mbedtls_mpi_uint *r )
Paul Bakker5121ce52009-01-03 21:22:43 +00001374{
Paul Bakker23986e52011-04-24 08:57:21 +00001375 size_t i;
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001376 mbedtls_mpi_uint c = 0, t, z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001377
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001378 for( i = 0; i < n; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 {
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001380 z = ( l[i] < c ); t = l[i] - c;
1381 c = ( t < r[i] ) + z; d[i] = t - r[i];
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 }
1383
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001384 return( c );
Paul Bakker5121ce52009-01-03 21:22:43 +00001385}
1386
1387/*
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001388 * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
Paul Bakker5121ce52009-01-03 21:22:43 +00001389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001391{
Janos Follath24eed8d2019-11-22 13:21:35 +00001392 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001393 size_t n;
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001394 mbedtls_mpi_uint carry;
Hanno Becker73d7d792018-12-11 10:35:51 +00001395 MPI_VALIDATE_RET( X != NULL );
1396 MPI_VALIDATE_RET( A != NULL );
1397 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
Paul Bakker23986e52011-04-24 08:57:21 +00001399 for( n = B->n; n > 0; n-- )
1400 if( B->p[n - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 break;
Gilles Peskinec8a91772021-01-27 22:30:43 +01001402 if( n > A->n )
1403 {
1404 /* B >= (2^ciL)^n > A */
1405 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1406 goto cleanup;
1407 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001408
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001409 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, A->n ) );
1410
1411 /* Set the high limbs of X to match A. Don't touch the lower limbs
1412 * because X might be aliased to B, and we must not overwrite the
1413 * significant digits of B. */
1414 if( A->n > n )
1415 memcpy( X->p + n, A->p + n, ( A->n - n ) * ciL );
1416 if( X->n > A->n )
1417 memset( X->p + A->n, 0, ( X->n - A->n ) * ciL );
1418
1419 carry = mpi_sub_hlp( n, X->p, A->p, B->p );
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001420 if( carry != 0 )
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001421 {
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001422 /* Propagate the carry to the first nonzero limb of X. */
1423 for( ; n < X->n && X->p[n] == 0; n++ )
1424 --X->p[n];
1425 /* If we ran out of space for the carry, it means that the result
1426 * is negative. */
1427 if( n == X->n )
Gilles Peskine89b41302020-07-23 01:16:46 +02001428 {
1429 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1430 goto cleanup;
1431 }
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001432 --X->p[n];
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001433 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001434
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001435 /* X should always be positive as a result of unsigned subtractions. */
1436 X->s = 1;
1437
Paul Bakker5121ce52009-01-03 21:22:43 +00001438cleanup:
Paul Bakker5121ce52009-01-03 21:22:43 +00001439 return( ret );
1440}
1441
1442/*
1443 * Signed addition: X = A + B
1444 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001446{
Hanno Becker73d7d792018-12-11 10:35:51 +00001447 int ret, s;
1448 MPI_VALIDATE_RET( X != NULL );
1449 MPI_VALIDATE_RET( A != NULL );
1450 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001451
Hanno Becker73d7d792018-12-11 10:35:51 +00001452 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001453 if( A->s * B->s < 0 )
1454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001458 X->s = s;
1459 }
1460 else
1461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001463 X->s = -s;
1464 }
1465 }
1466 else
1467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001469 X->s = s;
1470 }
1471
1472cleanup:
1473
1474 return( ret );
1475}
1476
1477/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001478 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001481{
Hanno Becker73d7d792018-12-11 10:35:51 +00001482 int ret, s;
1483 MPI_VALIDATE_RET( X != NULL );
1484 MPI_VALIDATE_RET( A != NULL );
1485 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001486
Hanno Becker73d7d792018-12-11 10:35:51 +00001487 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001488 if( A->s * B->s > 0 )
1489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001493 X->s = s;
1494 }
1495 else
1496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001498 X->s = -s;
1499 }
1500 }
1501 else
1502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001504 X->s = s;
1505 }
1506
1507cleanup:
1508
1509 return( ret );
1510}
1511
1512/*
1513 * Signed addition: X = A + b
1514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001516{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517 mbedtls_mpi _B;
1518 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001519 MPI_VALIDATE_RET( X != NULL );
1520 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001521
1522 p[0] = ( b < 0 ) ? -b : b;
1523 _B.s = ( b < 0 ) ? -1 : 1;
1524 _B.n = 1;
1525 _B.p = p;
1526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 return( mbedtls_mpi_add_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001528}
1529
1530/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001531 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001532 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001534{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 mbedtls_mpi _B;
1536 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001537 MPI_VALIDATE_RET( X != NULL );
1538 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001539
1540 p[0] = ( b < 0 ) ? -b : b;
1541 _B.s = ( b < 0 ) ? -1 : 1;
1542 _B.n = 1;
1543 _B.p = p;
1544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545 return( mbedtls_mpi_sub_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001546}
1547
Gilles Peskinea5d8d892020-07-23 21:27:15 +02001548/** Helper for mbedtls_mpi multiplication.
1549 *
1550 * Add \p b * \p s to \p d.
1551 *
1552 * \param i The number of limbs of \p s.
1553 * \param[in] s A bignum to multiply, of size \p i.
1554 * It may overlap with \p d, but only if
1555 * \p d <= \p s.
1556 * Its leading limb must not be \c 0.
1557 * \param[in,out] d The bignum to add to.
1558 * It must be sufficiently large to store the
1559 * result of the multiplication. This means
1560 * \p i + 1 limbs if \p d[\p i - 1] started as 0 and \p b
1561 * is not known a priori.
1562 * \param b A scalar to multiply.
Paul Bakkerfc4f46f2013-06-24 19:23:56 +02001563 */
1564static
1565#if defined(__APPLE__) && defined(__arm__)
1566/*
1567 * Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
1568 * appears to need this to prevent bad ARM code generation at -O3.
1569 */
1570__attribute__ ((noinline))
1571#endif
Gilles Peskinea5d8d892020-07-23 21:27:15 +02001572void mpi_mul_hlp( size_t i,
1573 const mbedtls_mpi_uint *s,
1574 mbedtls_mpi_uint *d,
1575 mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001576{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 mbedtls_mpi_uint c = 0, t = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001578
1579#if defined(MULADDC_HUIT)
1580 for( ; i >= 8; i -= 8 )
1581 {
1582 MULADDC_INIT
1583 MULADDC_HUIT
1584 MULADDC_STOP
1585 }
1586
1587 for( ; i > 0; i-- )
1588 {
1589 MULADDC_INIT
1590 MULADDC_CORE
1591 MULADDC_STOP
1592 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001593#else /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001594 for( ; i >= 16; i -= 16 )
1595 {
1596 MULADDC_INIT
1597 MULADDC_CORE MULADDC_CORE
1598 MULADDC_CORE MULADDC_CORE
1599 MULADDC_CORE MULADDC_CORE
1600 MULADDC_CORE MULADDC_CORE
1601
1602 MULADDC_CORE MULADDC_CORE
1603 MULADDC_CORE MULADDC_CORE
1604 MULADDC_CORE MULADDC_CORE
1605 MULADDC_CORE MULADDC_CORE
1606 MULADDC_STOP
1607 }
1608
1609 for( ; i >= 8; i -= 8 )
1610 {
1611 MULADDC_INIT
1612 MULADDC_CORE MULADDC_CORE
1613 MULADDC_CORE MULADDC_CORE
1614
1615 MULADDC_CORE MULADDC_CORE
1616 MULADDC_CORE MULADDC_CORE
1617 MULADDC_STOP
1618 }
1619
1620 for( ; i > 0; i-- )
1621 {
1622 MULADDC_INIT
1623 MULADDC_CORE
1624 MULADDC_STOP
1625 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001626#endif /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001627
1628 t++;
1629
Gilles Peskine8e464c42020-07-24 00:08:38 +02001630 while( c != 0 )
1631 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001632 *d += c; c = ( *d < c ); d++;
1633 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001634}
1635
1636/*
1637 * Baseline multiplication: X = A * B (HAC 14.12)
1638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001640{
Janos Follath24eed8d2019-11-22 13:21:35 +00001641 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001642 size_t i, j;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 mbedtls_mpi TA, TB;
Gilles Peskined65b5002021-06-15 21:44:32 +02001644 int result_is_zero = 0;
Hanno Becker73d7d792018-12-11 10:35:51 +00001645 MPI_VALIDATE_RET( X != NULL );
1646 MPI_VALIDATE_RET( A != NULL );
1647 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 if( X == A ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); A = &TA; }
1652 if( X == B ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); B = &TB; }
Paul Bakker5121ce52009-01-03 21:22:43 +00001653
Paul Bakker23986e52011-04-24 08:57:21 +00001654 for( i = A->n; i > 0; i-- )
1655 if( A->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001656 break;
Gilles Peskined65b5002021-06-15 21:44:32 +02001657 if( i == 0 && ( A->n == 0 || A->p[0] == 0 ) )
1658 result_is_zero = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001659
Paul Bakker23986e52011-04-24 08:57:21 +00001660 for( j = B->n; j > 0; j-- )
1661 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001662 break;
Gilles Peskined65b5002021-06-15 21:44:32 +02001663 if( j == 0 && ( B->n == 0 || B->p[0] == 0 ) )
1664 result_is_zero = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + j ) );
1667 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001668
Alexey Skalozub8e75e682016-01-13 21:59:27 +02001669 for( ; j > 0; j-- )
1670 mpi_mul_hlp( i, A->p, X->p + j - 1, B->p[j - 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00001671
Gilles Peskine70a7dcd2021-06-10 15:51:54 +02001672 /* If the result is 0, we don't shortcut the operation, which reduces
1673 * but does not eliminate side channels leaking the zero-ness. We do
1674 * need to take care to set the sign bit properly since the library does
1675 * not fully support an MPI object with a value of 0 and s == -1. */
Gilles Peskined65b5002021-06-15 21:44:32 +02001676 if( result_is_zero )
Gilles Peskine70a7dcd2021-06-10 15:51:54 +02001677 X->s = 1;
Gilles Peskine70a7dcd2021-06-10 15:51:54 +02001678 else
1679 X->s = A->s * B->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001680
1681cleanup:
1682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001684
1685 return( ret );
1686}
1687
1688/*
1689 * Baseline multiplication: X = A * b
1690 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001692{
Hanno Becker73d7d792018-12-11 10:35:51 +00001693 MPI_VALIDATE_RET( X != NULL );
1694 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001695
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001696 /* mpi_mul_hlp can't deal with a leading 0. */
1697 size_t n = A->n;
1698 while( n > 0 && A->p[n - 1] == 0 )
1699 --n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001700
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001701 /* The general method below doesn't work if n==0 or b==0. By chance
1702 * calculating the result is trivial in those cases. */
1703 if( b == 0 || n == 0 )
1704 {
Paul Elliott986b55a2021-04-20 21:46:29 +01001705 return( mbedtls_mpi_lset( X, 0 ) );
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001706 }
1707
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001708 /* Calculate A*b as A + A*(b-1) to take advantage of mpi_mul_hlp */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001709 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001710 /* In general, A * b requires 1 limb more than b. If
1711 * A->p[n - 1] * b / b == A->p[n - 1], then A * b fits in the same
1712 * number of limbs as A and the call to grow() is not required since
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001713 * copy() will take care of the growth if needed. However, experimentally,
1714 * making the call to grow() unconditional causes slightly fewer
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001715 * calls to calloc() in ECP code, presumably because it reuses the
1716 * same mpi for a while and this way the mpi is more likely to directly
1717 * grow to its final size. */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001718 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n + 1 ) );
1719 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
1720 mpi_mul_hlp( n, A->p, X->p, b - 1 );
1721
1722cleanup:
1723 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001724}
1725
1726/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001727 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1728 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001729 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001730static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1,
1731 mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r )
Simon Butcher15b15d12015-11-26 19:35:03 +00001732{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001733#if defined(MBEDTLS_HAVE_UDBL)
1734 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001735#else
Simon Butcher9803d072016-01-03 00:24:34 +00001736 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
1737 const mbedtls_mpi_uint uint_halfword_mask = ( (mbedtls_mpi_uint) 1 << biH ) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001738 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1739 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001740 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001741#endif
1742
Simon Butcher15b15d12015-11-26 19:35:03 +00001743 /*
1744 * Check for overflow
1745 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001746 if( 0 == d || u1 >= d )
Simon Butcher15b15d12015-11-26 19:35:03 +00001747 {
Simon Butcherf5ba0452015-12-27 23:01:55 +00001748 if (r != NULL) *r = ~0;
Simon Butcher15b15d12015-11-26 19:35:03 +00001749
Simon Butcherf5ba0452015-12-27 23:01:55 +00001750 return ( ~0 );
Simon Butcher15b15d12015-11-26 19:35:03 +00001751 }
1752
1753#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001754 dividend = (mbedtls_t_udbl) u1 << biL;
1755 dividend |= (mbedtls_t_udbl) u0;
1756 quotient = dividend / d;
1757 if( quotient > ( (mbedtls_t_udbl) 1 << biL ) - 1 )
1758 quotient = ( (mbedtls_t_udbl) 1 << biL ) - 1;
1759
1760 if( r != NULL )
Simon Butcher9803d072016-01-03 00:24:34 +00001761 *r = (mbedtls_mpi_uint)( dividend - (quotient * d ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001762
1763 return (mbedtls_mpi_uint) quotient;
1764#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001765
1766 /*
1767 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1768 * Vol. 2 - Seminumerical Algorithms, Knuth
1769 */
1770
1771 /*
1772 * Normalize the divisor, d, and dividend, u0, u1
1773 */
1774 s = mbedtls_clz( d );
1775 d = d << s;
1776
1777 u1 = u1 << s;
Simon Butcher9803d072016-01-03 00:24:34 +00001778 u1 |= ( u0 >> ( biL - s ) ) & ( -(mbedtls_mpi_sint)s >> ( biL - 1 ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001779 u0 = u0 << s;
1780
1781 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001782 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001783
1784 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001785 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001786
1787 /*
1788 * Find the first quotient and remainder
1789 */
1790 q1 = u1 / d1;
1791 r0 = u1 - d1 * q1;
1792
1793 while( q1 >= radix || ( q1 * d0 > radix * r0 + u0_msw ) )
1794 {
1795 q1 -= 1;
1796 r0 += d1;
1797
1798 if ( r0 >= radix ) break;
1799 }
1800
Simon Butcherf5ba0452015-12-27 23:01:55 +00001801 rAX = ( u1 * radix ) + ( u0_msw - q1 * d );
Simon Butcher15b15d12015-11-26 19:35:03 +00001802 q0 = rAX / d1;
1803 r0 = rAX - q0 * d1;
1804
1805 while( q0 >= radix || ( q0 * d0 > radix * r0 + u0_lsw ) )
1806 {
1807 q0 -= 1;
1808 r0 += d1;
1809
1810 if ( r0 >= radix ) break;
1811 }
1812
1813 if (r != NULL)
Simon Butcherf5ba0452015-12-27 23:01:55 +00001814 *r = ( rAX * radix + u0_lsw - q0 * d ) >> s;
Simon Butcher15b15d12015-11-26 19:35:03 +00001815
1816 quotient = q1 * radix + q0;
1817
1818 return quotient;
1819#endif
1820}
1821
1822/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001824 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001825int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1826 const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001827{
Janos Follath24eed8d2019-11-22 13:21:35 +00001828 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001829 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 mbedtls_mpi X, Y, Z, T1, T2;
Alexander Kd19a1932019-11-01 18:20:42 +03001831 mbedtls_mpi_uint TP2[3];
Hanno Becker73d7d792018-12-11 10:35:51 +00001832 MPI_VALIDATE_RET( A != NULL );
1833 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835 if( mbedtls_mpi_cmp_int( B, 0 ) == 0 )
1836 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Alexander K35d6d462019-10-31 14:46:45 +03001839 mbedtls_mpi_init( &T1 );
Alexander Kd19a1932019-11-01 18:20:42 +03001840 /*
1841 * Avoid dynamic memory allocations for constant-size T2.
1842 *
1843 * T2 is used for comparison only and the 3 limbs are assigned explicitly,
1844 * so nobody increase the size of the MPI and we're safe to use an on-stack
1845 * buffer.
1846 */
Alexander K35d6d462019-10-31 14:46:45 +03001847 T2.s = 1;
Alexander Kd19a1932019-11-01 18:20:42 +03001848 T2.n = sizeof( TP2 ) / sizeof( *TP2 );
1849 T2.p = TP2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851 if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 if( Q != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_lset( Q, 0 ) );
1854 if( R != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001855 return( 0 );
1856 }
1857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &X, A ) );
1859 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001860 X.s = Y.s = 1;
1861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &Z, A->n + 2 ) );
1863 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Z, 0 ) );
Gilles Peskine2536aa72020-07-24 00:12:59 +02001864 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, A->n + 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001865
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001866 k = mbedtls_mpi_bitlen( &Y ) % biL;
Paul Bakkerf9688572011-05-05 10:00:45 +00001867 if( k < biL - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001868 {
1869 k = biL - 1 - k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &X, k ) );
1871 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, k ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001872 }
1873 else k = 0;
1874
1875 n = X.n - 1;
1876 t = Y.n - 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 while( mbedtls_mpi_cmp_mpi( &X, &Y ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001880 {
1881 Z.p[n - t]++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001883 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001885
1886 for( i = n; i > t ; i-- )
1887 {
1888 if( X.p[i] >= Y.p[t] )
1889 Z.p[i - t - 1] = ~0;
1890 else
1891 {
Simon Butcher15b15d12015-11-26 19:35:03 +00001892 Z.p[i - t - 1] = mbedtls_int_div_int( X.p[i], X.p[i - 1],
1893 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001894 }
1895
Alexander K35d6d462019-10-31 14:46:45 +03001896 T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2];
1897 T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1];
1898 T2.p[2] = X.p[i];
1899
Paul Bakker5121ce52009-01-03 21:22:43 +00001900 Z.p[i - t - 1]++;
1901 do
1902 {
1903 Z.p[i - t - 1]--;
1904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T1, 0 ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001906 T1.p[0] = ( t < 1 ) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001907 T1.p[1] = Y.p[t];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001909 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910 while( mbedtls_mpi_cmp_mpi( &T1, &T2 ) > 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
1913 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1914 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 if( mbedtls_mpi_cmp_int( &X, 0 ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &Y ) );
1919 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1920 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001921 Z.p[i - t - 1]--;
1922 }
1923 }
1924
1925 if( Q != NULL )
1926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( Q, &Z ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001928 Q->s = A->s * B->s;
1929 }
1930
1931 if( R != NULL )
1932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &X, k ) );
Paul Bakkerf02c5642012-11-13 10:25:21 +00001934 X.s = A->s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, &X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 if( mbedtls_mpi_cmp_int( R, 0 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001938 R->s = 1;
1939 }
1940
1941cleanup:
1942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Alexander K35d6d462019-10-31 14:46:45 +03001944 mbedtls_mpi_free( &T1 );
Alexander Kd19a1932019-11-01 18:20:42 +03001945 mbedtls_platform_zeroize( TP2, sizeof( TP2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001946
1947 return( ret );
1948}
1949
1950/*
1951 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00001952 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001953int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R,
1954 const mbedtls_mpi *A,
1955 mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001956{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 mbedtls_mpi _B;
1958 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001959 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001960
1961 p[0] = ( b < 0 ) ? -b : b;
1962 _B.s = ( b < 0 ) ? -1 : 1;
1963 _B.n = 1;
1964 _B.p = p;
1965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 return( mbedtls_mpi_div_mpi( Q, R, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001967}
1968
1969/*
1970 * Modulo: R = A mod B
1971 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001973{
Janos Follath24eed8d2019-11-22 13:21:35 +00001974 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker73d7d792018-12-11 10:35:51 +00001975 MPI_VALIDATE_RET( R != NULL );
1976 MPI_VALIDATE_RET( A != NULL );
1977 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 if( mbedtls_mpi_cmp_int( B, 0 ) < 0 )
1980 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( NULL, R, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 while( mbedtls_mpi_cmp_int( R, 0 ) < 0 )
1985 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 while( mbedtls_mpi_cmp_mpi( R, B ) >= 0 )
1988 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001989
1990cleanup:
1991
1992 return( ret );
1993}
1994
1995/*
1996 * Modulo: r = A mod b
1997 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001999{
Paul Bakker23986e52011-04-24 08:57:21 +00002000 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001 mbedtls_mpi_uint x, y, z;
Hanno Becker73d7d792018-12-11 10:35:51 +00002002 MPI_VALIDATE_RET( r != NULL );
2003 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002004
2005 if( b == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00002007
2008 if( b < 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002010
2011 /*
2012 * handle trivial cases
2013 */
2014 if( b == 1 )
2015 {
2016 *r = 0;
2017 return( 0 );
2018 }
2019
2020 if( b == 2 )
2021 {
2022 *r = A->p[0] & 1;
2023 return( 0 );
2024 }
2025
2026 /*
2027 * general case
2028 */
Paul Bakker23986e52011-04-24 08:57:21 +00002029 for( i = A->n, y = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00002030 {
Paul Bakker23986e52011-04-24 08:57:21 +00002031 x = A->p[i - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00002032 y = ( y << biH ) | ( x >> biH );
2033 z = y / b;
2034 y -= z * b;
2035
2036 x <<= biH;
2037 y = ( y << biH ) | ( x >> biH );
2038 z = y / b;
2039 y -= z * b;
2040 }
2041
Paul Bakkerce40a6d2009-06-23 19:46:08 +00002042 /*
2043 * If A is negative, then the current y represents a negative value.
2044 * Flipping it to the positive side.
2045 */
2046 if( A->s < 0 && y != 0 )
2047 y = b - y;
2048
Paul Bakker5121ce52009-01-03 21:22:43 +00002049 *r = y;
2050
2051 return( 0 );
2052}
2053
2054/*
2055 * Fast Montgomery initialization (thanks to Tom St Denis)
2056 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057static void mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002058{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059 mbedtls_mpi_uint x, m0 = N->p[0];
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01002060 unsigned int i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002061
2062 x = m0;
2063 x += ( ( m0 + 2 ) & 4 ) << 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002064
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01002065 for( i = biL; i >= 8; i /= 2 )
2066 x *= ( 2 - ( m0 * x ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002067
2068 *mm = ~x + 1;
2069}
2070
Gilles Peskine2a82f722020-06-04 15:00:49 +02002071/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
2072 *
2073 * \param[in,out] A One of the numbers to multiply.
Gilles Peskine221626f2020-06-08 22:37:50 +02002074 * It must have at least as many limbs as N
2075 * (A->n >= N->n), and any limbs beyond n are ignored.
Gilles Peskine2a82f722020-06-04 15:00:49 +02002076 * On successful completion, A contains the result of
2077 * the multiplication A * B * R^-1 mod N where
2078 * R = (2^ciL)^n.
2079 * \param[in] B One of the numbers to multiply.
2080 * It must be nonzero and must not have more limbs than N
2081 * (B->n <= N->n).
2082 * \param[in] N The modulo. N must be odd.
2083 * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
2084 * This is -N^-1 mod 2^ciL.
2085 * \param[in,out] T A bignum for temporary storage.
2086 * It must be at least twice the limb size of N plus 2
2087 * (T->n >= 2 * (N->n + 1)).
2088 * Its initial content is unused and
2089 * its final content is indeterminate.
2090 * Note that unlike the usual convention in the library
2091 * for `const mbedtls_mpi*`, the content of T can change.
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002093static 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 +02002094 const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00002095{
Paul Bakker23986e52011-04-24 08:57:21 +00002096 size_t i, n, m;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 mbedtls_mpi_uint u0, u1, *d;
Paul Bakker5121ce52009-01-03 21:22:43 +00002098
2099 memset( T->p, 0, T->n * ciL );
2100
2101 d = T->p;
2102 n = N->n;
2103 m = ( B->n < n ) ? B->n : n;
2104
2105 for( i = 0; i < n; i++ )
2106 {
2107 /*
2108 * T = (T + u0*B + u1*N) / 2^biL
2109 */
2110 u0 = A->p[i];
2111 u1 = ( d[0] + u0 * B->p[0] ) * mm;
2112
2113 mpi_mul_hlp( m, B->p, d, u0 );
2114 mpi_mul_hlp( n, N->p, d, u1 );
2115
2116 *d++ = u0; d[n + 1] = 0;
2117 }
2118
Gilles Peskine221626f2020-06-08 22:37:50 +02002119 /* At this point, d is either the desired result or the desired result
2120 * plus N. We now potentially subtract N, avoiding leaking whether the
2121 * subtraction is performed through side channels. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002122
Gilles Peskine221626f2020-06-08 22:37:50 +02002123 /* Copy the n least significant limbs of d to A, so that
2124 * A = d if d < N (recall that N has n limbs). */
2125 memcpy( A->p, d, n * ciL );
Gilles Peskine09ec10a2020-06-09 10:39:38 +02002126 /* If d >= N then we want to set A to d - N. To prevent timing attacks,
Gilles Peskine221626f2020-06-08 22:37:50 +02002127 * do the calculation without using conditional tests. */
2128 /* Set d to d0 + (2^biL)^n - N where d0 is the current value of d. */
Gilles Peskine132c0972020-06-04 21:05:24 +02002129 d[n] += 1;
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02002130 d[n] -= mpi_sub_hlp( n, d, d, N->p );
Gilles Peskine221626f2020-06-08 22:37:50 +02002131 /* If d0 < N then d < (2^biL)^n
2132 * so d[n] == 0 and we want to keep A as it is.
2133 * If d0 >= N then d >= (2^biL)^n, and d <= (2^biL)^n + N < 2 * (2^biL)^n
2134 * so d[n] == 1 and we want to set A to the result of the subtraction
2135 * which is d - (2^biL)^n, i.e. the n least significant limbs of d.
2136 * This exactly corresponds to a conditional assignment. */
2137 mpi_safe_cond_assign( n, A->p, d, (unsigned char) d[n] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002138}
2139
2140/*
2141 * Montgomery reduction: A = A * R^-1 mod N
Gilles Peskine2a82f722020-06-04 15:00:49 +02002142 *
2143 * See mpi_montmul() regarding constraints and guarantees on the parameters.
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002145static void mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N,
2146 mbedtls_mpi_uint mm, const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00002147{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 mbedtls_mpi_uint z = 1;
2149 mbedtls_mpi U;
Paul Bakker5121ce52009-01-03 21:22:43 +00002150
Paul Bakker8ddb6452013-02-27 14:56:33 +01002151 U.n = U.s = (int) z;
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 U.p = &z;
2153
Gilles Peskine4e91d472020-06-04 20:55:15 +02002154 mpi_montmul( A, &U, N, mm, T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002155}
2156
2157/*
2158 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
2159 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002160int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
2161 const mbedtls_mpi *E, const mbedtls_mpi *N,
2162 mbedtls_mpi *_RR )
Paul Bakker5121ce52009-01-03 21:22:43 +00002163{
Janos Follath24eed8d2019-11-22 13:21:35 +00002164 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002165 size_t wbits, wsize, one = 1;
2166 size_t i, j, nblimbs;
2167 size_t bufsize, nbits;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 mbedtls_mpi_uint ei, mm, state;
Daniel Otte388f9b22020-08-21 12:34:29 +02002169 mbedtls_mpi RR, T, W[ 1 << MBEDTLS_MPI_WINDOW_SIZE ], Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00002170 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002171
Hanno Becker73d7d792018-12-11 10:35:51 +00002172 MPI_VALIDATE_RET( X != NULL );
2173 MPI_VALIDATE_RET( A != NULL );
2174 MPI_VALIDATE_RET( E != NULL );
2175 MPI_VALIDATE_RET( N != NULL );
2176
Hanno Becker8d1dd1b2017-09-28 11:02:24 +01002177 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 || ( N->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 if( mbedtls_mpi_cmp_int( E, 0 ) < 0 )
2181 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002182
Chris Jones9246d042020-11-25 15:12:39 +00002183 if( mbedtls_mpi_bitlen( E ) > MBEDTLS_MPI_MAX_BITS ||
2184 mbedtls_mpi_bitlen( N ) > MBEDTLS_MPI_MAX_BITS )
2185 return ( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2186
Paul Bakkerf6198c12012-05-16 08:02:29 +00002187 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002188 * Init temps and window size
2189 */
2190 mpi_montg_init( &mm, N );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &T );
2192 mbedtls_mpi_init( &Apos );
Paul Bakker5121ce52009-01-03 21:22:43 +00002193 memset( W, 0, sizeof( W ) );
2194
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002195 i = mbedtls_mpi_bitlen( E );
Paul Bakker5121ce52009-01-03 21:22:43 +00002196
2197 wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
2198 ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1;
2199
Peter Kolbuse6bcad32018-12-11 14:01:44 -06002200#if( MBEDTLS_MPI_WINDOW_SIZE < 6 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 if( wsize > MBEDTLS_MPI_WINDOW_SIZE )
2202 wsize = MBEDTLS_MPI_WINDOW_SIZE;
Peter Kolbuse6bcad32018-12-11 14:01:44 -06002203#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00002204
Paul Bakker5121ce52009-01-03 21:22:43 +00002205 j = N->n + 1;
Gilles Peskinef643e8e2021-06-08 23:17:42 +02002206 /* All W[i] and X must have at least N->n limbs for the mpi_montmul()
2207 * and mpi_montred() calls later. Here we ensure that W[1] and X are
2208 * large enough, and later we'll grow other W[i] to the same length.
2209 * They must not be shrunk midway through this function!
2210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
2212 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], j ) );
2213 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T, j * 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002214
2215 /*
Paul Bakker50546922012-05-19 08:40:49 +00002216 * Compensate for negative A (and correct at the end)
2217 */
2218 neg = ( A->s == -1 );
Paul Bakker50546922012-05-19 08:40:49 +00002219 if( neg )
2220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Apos, A ) );
Paul Bakker50546922012-05-19 08:40:49 +00002222 Apos.s = 1;
2223 A = &Apos;
2224 }
2225
2226 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002227 * If 1st call, pre-compute R^2 mod N
2228 */
2229 if( _RR == NULL || _RR->p == NULL )
2230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &RR, 1 ) );
2232 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &RR, N->n * 2 * biL ) );
2233 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &RR, &RR, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002234
2235 if( _RR != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 memcpy( _RR, &RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002237 }
2238 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 memcpy( &RR, _RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002240
2241 /*
2242 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
2243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 if( mbedtls_mpi_cmp_mpi( A, N ) >= 0 )
Gilles Peskinef643e8e2021-06-08 23:17:42 +02002245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &W[1], A, N ) );
Gilles Peskinef643e8e2021-06-08 23:17:42 +02002247 /* This should be a no-op because W[1] is already that large before
2248 * mbedtls_mpi_mod_mpi(), but it's necessary to avoid an overflow
2249 * in mpi_montmul() below, so let's make sure. */
Gilles Peskine0759cad2021-06-15 21:22:48 +02002250 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], N->n + 1 ) );
Gilles Peskinef643e8e2021-06-08 23:17:42 +02002251 }
Paul Bakkerc2024f42014-01-23 20:38:35 +01002252 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[1], A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002254
Gilles Peskinef643e8e2021-06-08 23:17:42 +02002255 /* Note that this is safe because W[1] always has at least N->n limbs
2256 * (it grew above and was preserved by mbedtls_mpi_copy()). */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002257 mpi_montmul( &W[1], &RR, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002258
2259 /*
2260 * X = R^2 * R^-1 mod N = R mod N
2261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &RR ) );
Gilles Peskine4e91d472020-06-04 20:55:15 +02002263 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002264
2265 if( wsize > 1 )
2266 {
2267 /*
2268 * W[1 << (wsize - 1)] = W[1] ^ (wsize - 1)
2269 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002270 j = one << ( wsize - 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[j], N->n + 1 ) );
2273 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[j], &W[1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002274
2275 for( i = 0; i < wsize - 1; i++ )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002276 mpi_montmul( &W[j], &W[j], N, mm, &T );
Paul Bakker0d7702c2013-10-29 16:18:35 +01002277
Paul Bakker5121ce52009-01-03 21:22:43 +00002278 /*
2279 * W[i] = W[i - 1] * W[1]
2280 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002281 for( i = j + 1; i < ( one << wsize ); i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[i], N->n + 1 ) );
2284 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[i], &W[i - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002285
Gilles Peskine4e91d472020-06-04 20:55:15 +02002286 mpi_montmul( &W[i], &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 }
2288 }
2289
2290 nblimbs = E->n;
2291 bufsize = 0;
2292 nbits = 0;
2293 wbits = 0;
2294 state = 0;
2295
2296 while( 1 )
2297 {
2298 if( bufsize == 0 )
2299 {
Paul Bakker0d7702c2013-10-29 16:18:35 +01002300 if( nblimbs == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002301 break;
2302
Paul Bakker0d7702c2013-10-29 16:18:35 +01002303 nblimbs--;
2304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 bufsize = sizeof( mbedtls_mpi_uint ) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00002306 }
2307
2308 bufsize--;
2309
2310 ei = (E->p[nblimbs] >> bufsize) & 1;
2311
2312 /*
2313 * skip leading 0s
2314 */
2315 if( ei == 0 && state == 0 )
2316 continue;
2317
2318 if( ei == 0 && state == 1 )
2319 {
2320 /*
2321 * out of window, square X
2322 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002323 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002324 continue;
2325 }
2326
2327 /*
2328 * add ei to current window
2329 */
2330 state = 2;
2331
2332 nbits++;
Paul Bakker66d5d072014-06-17 16:39:18 +02002333 wbits |= ( ei << ( wsize - nbits ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002334
2335 if( nbits == wsize )
2336 {
2337 /*
2338 * X = X^wsize R^-1 mod N
2339 */
2340 for( i = 0; i < wsize; i++ )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002341 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002342
2343 /*
2344 * X = X * W[wbits] R^-1 mod N
2345 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002346 mpi_montmul( X, &W[wbits], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002347
2348 state--;
2349 nbits = 0;
2350 wbits = 0;
2351 }
2352 }
2353
2354 /*
2355 * process the remaining bits
2356 */
2357 for( i = 0; i < nbits; i++ )
2358 {
Gilles Peskine4e91d472020-06-04 20:55:15 +02002359 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002360
2361 wbits <<= 1;
2362
Paul Bakker66d5d072014-06-17 16:39:18 +02002363 if( ( wbits & ( one << wsize ) ) != 0 )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002364 mpi_montmul( X, &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002365 }
2366
2367 /*
2368 * X = A^E * R * R^-1 mod N = A^E mod N
2369 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002370 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002371
Hanno Beckera4af1c42017-04-18 09:07:45 +01002372 if( neg && E->n != 0 && ( E->p[0] & 1 ) != 0 )
Paul Bakkerf6198c12012-05-16 08:02:29 +00002373 {
2374 X->s = -1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, N, X ) );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002376 }
2377
Paul Bakker5121ce52009-01-03 21:22:43 +00002378cleanup:
2379
Paul Bakker66d5d072014-06-17 16:39:18 +02002380 for( i = ( one << ( wsize - 1 ) ); i < ( one << wsize ); i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381 mbedtls_mpi_free( &W[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 mbedtls_mpi_free( &W[1] ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &Apos );
Paul Bakker6c591fa2011-05-05 11:49:20 +00002384
Paul Bakker75a28602014-03-31 12:08:17 +02002385 if( _RR == NULL || _RR->p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002387
2388 return( ret );
2389}
2390
Paul Bakker5121ce52009-01-03 21:22:43 +00002391/*
2392 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
2393 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00002395{
Janos Follath24eed8d2019-11-22 13:21:35 +00002396 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002397 size_t lz, lzt;
Alexander Ke8ad49f2019-08-16 16:16:07 +03002398 mbedtls_mpi TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00002399
Hanno Becker73d7d792018-12-11 10:35:51 +00002400 MPI_VALIDATE_RET( G != NULL );
2401 MPI_VALIDATE_RET( A != NULL );
2402 MPI_VALIDATE_RET( B != NULL );
2403
Alexander Ke8ad49f2019-08-16 16:16:07 +03002404 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) );
2407 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409 lz = mbedtls_mpi_lsb( &TA );
2410 lzt = mbedtls_mpi_lsb( &TB );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002411
Gilles Peskineb5e56ec2021-06-09 13:26:43 +02002412 /* The loop below gives the correct result when A==0 but not when B==0.
2413 * So have a special case for B==0. Leverage the fact that we just
2414 * calculated the lsb and lsb(B)==0 iff B is odd or 0 to make the test
2415 * slightly more efficient than cmp_int(). */
2416 if( lzt == 0 && mbedtls_mpi_get_bit( &TB, 0 ) == 0 )
2417 {
2418 ret = mbedtls_mpi_copy( G, A );
2419 goto cleanup;
2420 }
2421
Paul Bakker66d5d072014-06-17 16:39:18 +02002422 if( lzt < lz )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002423 lz = lzt;
2424
Paul Bakker5121ce52009-01-03 21:22:43 +00002425 TA.s = TB.s = 1;
2426
Gilles Peskineea9aa142021-06-16 13:42:04 +02002427 /* We mostly follow the procedure described in HAC 14.54, but with some
2428 * minor differences:
2429 * - Sequences of multiplications or divisions by 2 are grouped into a
2430 * single shift operation.
2431 * - The procedure in HAC assumes that 0 < A <= B.
2432 * - The condition A <= B is not actually necessary for correctness;
2433 * the first round through the loop results in TA < TB.
2434 * - If A = 0, the loop goes through 0 iterations and the result is
2435 * correctly B.
2436 * - The case B=0 was short-circuited above.
2437 *
2438 * For the correctness proof below, decompose the original values of
2439 * A and B as
2440 * A = sa * 2^a * A' with A'=0 or A' odd, and sa = +-1
2441 * B = sb * 2^b * B' with B'=0 or B' odd, and sb = +-1
2442 * Then gcd(A, B) = 2^{min(a,b)} * gcd(A',B'),
2443 * and gcd(A',B') is odd or 0.
2444 *
2445 * At the beginning, we have TA = |A| and TB = |B| so gcd(A,B) = gcd(TA,TB).
2446 * The code maintains the following invariant:
2447 * gcd(A,B) = 2^k * gcd(TA,TB) for some k (I)
Gilles Peskine6537bdb2021-06-15 22:09:39 +02002448 */
2449
Gilles Peskineea9aa142021-06-16 13:42:04 +02002450 /* Proof that the loop terminates:
2451 * At each iteration, either the right-shift by 1 is made on a nonzero
2452 * value and the nonnegative integer bitlen(TA) + bitlen(TB) decreases
2453 * by at least 1, or the right-shift by 1 is made on zero and then
2454 * TA becomes 0 which ends the loop (TB cannot be 0 if it is right-shifted
2455 * since in that case TB is calculated from TB-TA with the condition TB>TA).
2456 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457 while( mbedtls_mpi_cmp_int( &TA, 0 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 {
Gilles Peskineea9aa142021-06-16 13:42:04 +02002459 /* Divisions by 2 preserve the invariant (I). */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, mbedtls_mpi_lsb( &TA ) ) );
2461 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, mbedtls_mpi_lsb( &TB ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002462
Gilles Peskineea9aa142021-06-16 13:42:04 +02002463 /* Set either TA or TB to |TA-TB|/2. Since TA and TB are both odd,
2464 * TA-TB is even so the division by 2 has an integer result.
2465 * Invariant (I) is preserved since any odd divisor of both TA and TB
2466 * also divides |TA-TB|/2, and any odd divisor of both TA and |TA-TB|/2
2467 * also divides TB, and any odd divisior of both TB and |TA-TB|/2 also
2468 * divides TA.
2469 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470 if( mbedtls_mpi_cmp_mpi( &TA, &TB ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TA, &TA, &TB ) );
2473 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002474 }
2475 else
2476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TB, &TB, &TA ) );
2478 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002479 }
Gilles Peskineea9aa142021-06-16 13:42:04 +02002480 /* Note that one of TA or TB is still odd. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 }
2482
Gilles Peskineea9aa142021-06-16 13:42:04 +02002483 /* By invariant (I), gcd(A,B) = 2^k * gcd(TA,TB) for some k.
2484 * At the loop exit, TA = 0, so gcd(TA,TB) = TB.
2485 * - If there was at least one loop iteration, then one of TA or TB is odd,
2486 * and TA = 0, so TB is odd and gcd(TA,TB) = gcd(A',B'). In this case,
2487 * lz = min(a,b) so gcd(A,B) = 2^lz * TB.
2488 * - If there was no loop iteration, then A was 0, and gcd(A,B) = B.
2489 * In this case, B = 2^lz * TB so gcd(A,B) = 2^lz * TB as well.
2490 */
2491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &TB, lz ) );
2493 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( G, &TB ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002494
2495cleanup:
2496
Alexander Ke8ad49f2019-08-16 16:16:07 +03002497 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002498
2499 return( ret );
2500}
2501
Gilles Peskine8f454702021-04-01 15:57:18 +02002502/* Fill X with n_bytes random bytes.
2503 * X must already have room for those bytes.
Gilles Peskine23422e42021-06-03 11:51:09 +02002504 * The ordering of the bytes returned from the RNG is suitable for
2505 * deterministic ECDSA (see RFC 6979 §3.3 and mbedtls_mpi_random()).
Gilles Peskinea16001e2021-04-13 21:55:35 +02002506 * The size and sign of X are unchanged.
Gilles Peskine8f454702021-04-01 15:57:18 +02002507 * n_bytes must not be 0.
2508 */
2509static int mpi_fill_random_internal(
2510 mbedtls_mpi *X, size_t n_bytes,
2511 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
2512{
2513 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2514 const size_t limbs = CHARS_TO_LIMBS( n_bytes );
2515 const size_t overhead = ( limbs * ciL ) - n_bytes;
2516
2517 if( X->n < limbs )
2518 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Gilles Peskine8f454702021-04-01 15:57:18 +02002519
Gilles Peskinea16001e2021-04-13 21:55:35 +02002520 memset( X->p, 0, overhead );
2521 memset( (unsigned char *) X->p + limbs * ciL, 0, ( X->n - limbs ) * ciL );
Gilles Peskine8f454702021-04-01 15:57:18 +02002522 MBEDTLS_MPI_CHK( f_rng( p_rng, (unsigned char *) X->p + overhead, n_bytes ) );
2523 mpi_bigendian_to_host( X->p, limbs );
2524
2525cleanup:
2526 return( ret );
2527}
2528
Paul Bakker33dc46b2014-04-30 16:11:39 +02002529/*
2530 * Fill X with size bytes of random.
2531 *
2532 * Use a temporary bytes representation to make sure the result is the same
Paul Bakkerc37b0ac2014-05-01 14:19:23 +02002533 * regardless of the platform endianness (useful when f_rng is actually
Paul Bakker33dc46b2014-04-30 16:11:39 +02002534 * deterministic, eg for tests).
2535 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002537 int (*f_rng)(void *, unsigned char *, size_t),
2538 void *p_rng )
Paul Bakker287781a2011-03-26 13:18:49 +00002539{
Janos Follath24eed8d2019-11-22 13:21:35 +00002540 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6dab6202019-01-02 16:42:29 +00002541 size_t const limbs = CHARS_TO_LIMBS( size );
Hanno Beckerda1655a2017-10-18 14:21:44 +01002542
Hanno Becker8ce11a32018-12-19 16:18:52 +00002543 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002544 MPI_VALIDATE_RET( f_rng != NULL );
Paul Bakker33dc46b2014-04-30 16:11:39 +02002545
Hanno Beckerda1655a2017-10-18 14:21:44 +01002546 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskine3130ce22021-06-02 22:17:52 +02002547 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Gilles Peskine8f454702021-04-01 15:57:18 +02002548 if( size == 0 )
2549 return( 0 );
Paul Bakker287781a2011-03-26 13:18:49 +00002550
Gilles Peskine8f454702021-04-01 15:57:18 +02002551 ret = mpi_fill_random_internal( X, size, f_rng, p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +00002552
2553cleanup:
2554 return( ret );
2555}
2556
Gilles Peskine4699fa42021-03-29 22:02:55 +02002557int mbedtls_mpi_random( mbedtls_mpi *X,
2558 mbedtls_mpi_sint min,
2559 const mbedtls_mpi *N,
2560 int (*f_rng)(void *, unsigned char *, size_t),
2561 void *p_rng )
2562{
Gilles Peskine4699fa42021-03-29 22:02:55 +02002563 int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskinee39ee8e2021-04-13 21:23:25 +02002564 int count;
Gilles Peskine4699fa42021-03-29 22:02:55 +02002565 unsigned cmp = 0;
2566 size_t n_bits = mbedtls_mpi_bitlen( N );
2567 size_t n_bytes = ( n_bits + 7 ) / 8;
2568
Gilles Peskine9312ba52021-03-29 22:14:51 +02002569 if( min < 0 )
2570 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2571 if( mbedtls_mpi_cmp_int( N, min ) <= 0 )
2572 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2573
Gilles Peskinee39ee8e2021-04-13 21:23:25 +02002574 /*
2575 * When min == 0, each try has at worst a probability 1/2 of failing
2576 * (the msb has a probability 1/2 of being 0, and then the result will
2577 * be < N), so after 30 tries failure probability is a most 2**(-30).
2578 *
2579 * When N is just below a power of 2, as is the case when generating
Gilles Peskine3f613632021-04-15 11:45:19 +02002580 * a random scalar on most elliptic curves, 1 try is enough with
Gilles Peskinee39ee8e2021-04-13 21:23:25 +02002581 * overwhelming probability. When N is just above a power of 2,
Gilles Peskine3f613632021-04-15 11:45:19 +02002582 * as when generating a random scalar on secp224k1, each try has
Gilles Peskinee39ee8e2021-04-13 21:23:25 +02002583 * a probability of failing that is almost 1/2.
2584 *
2585 * The probabilities are almost the same if min is nonzero but negligible
2586 * compared to N. This is always the case when N is crypto-sized, but
2587 * it's convenient to support small N for testing purposes. When N
2588 * is small, use a higher repeat count, otherwise the probability of
2589 * failure is macroscopic.
2590 */
Gilles Peskine11779072021-06-02 21:18:59 +02002591 count = ( n_bytes > 4 ? 30 : 250 );
Gilles Peskinee39ee8e2021-04-13 21:23:25 +02002592
Gilles Peskine8f454702021-04-01 15:57:18 +02002593 /* Ensure that target MPI has exactly the same number of limbs
2594 * as the upper bound, even if the upper bound has leading zeros.
2595 * This is necessary for the mbedtls_mpi_lt_mpi_ct() check. */
Gilles Peskine3130ce22021-06-02 22:17:52 +02002596 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, N->n ) );
Gilles Peskine8f454702021-04-01 15:57:18 +02002597
Gilles Peskine4699fa42021-03-29 22:02:55 +02002598 /*
2599 * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA)
2600 * when f_rng is a suitably parametrized instance of HMAC_DRBG:
2601 * - use the same byte ordering;
2602 * - keep the leftmost n_bits bits of the generated octet string;
2603 * - try until result is in the desired range.
2604 * This also avoids any bias, which is especially important for ECDSA.
2605 */
2606 do
2607 {
Gilles Peskine8f454702021-04-01 15:57:18 +02002608 MBEDTLS_MPI_CHK( mpi_fill_random_internal( X, n_bytes, f_rng, p_rng ) );
Gilles Peskine4699fa42021-03-29 22:02:55 +02002609 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, 8 * n_bytes - n_bits ) );
2610
Gilles Peskinee39ee8e2021-04-13 21:23:25 +02002611 if( --count == 0 )
Gilles Peskine4699fa42021-03-29 22:02:55 +02002612 {
2613 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2614 goto cleanup;
2615 }
2616
Gilles Peskinec0b68bf2021-06-03 11:38:26 +02002617 MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( X, N, &cmp ) );
Gilles Peskine4699fa42021-03-29 22:02:55 +02002618 }
2619 while( mbedtls_mpi_cmp_int( X, min ) < 0 || cmp != 1 );
2620
2621cleanup:
2622 return( ret );
2623}
2624
Paul Bakker5121ce52009-01-03 21:22:43 +00002625/*
2626 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2627 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002629{
Janos Follath24eed8d2019-11-22 13:21:35 +00002630 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Hanno Becker73d7d792018-12-11 10:35:51 +00002632 MPI_VALIDATE_RET( X != NULL );
2633 MPI_VALIDATE_RET( A != NULL );
2634 MPI_VALIDATE_RET( N != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002635
Hanno Becker4bcb4912017-04-18 15:49:39 +01002636 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TU ); mbedtls_mpi_init( &U1 ); mbedtls_mpi_init( &U2 );
2640 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &TB ); mbedtls_mpi_init( &TV );
2641 mbedtls_mpi_init( &V1 ); mbedtls_mpi_init( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, A, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002648 goto cleanup;
2649 }
2650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &TA, A, N ) );
2652 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TU, &TA ) );
2653 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, N ) );
2654 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TV, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U1, 1 ) );
2657 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U2, 0 ) );
2658 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V1, 0 ) );
2659 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
2661 do
2662 {
2663 while( ( TU.p[0] & 1 ) == 0 )
2664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TU, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002666
2667 if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
2668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &U1, &U1, &TB ) );
2670 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002671 }
2672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U1, 1 ) );
2674 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002675 }
2676
2677 while( ( TV.p[0] & 1 ) == 0 )
2678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TV, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
2681 if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
2682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, &TB ) );
2684 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002685 }
2686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V1, 1 ) );
2688 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 }
2690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691 if( mbedtls_mpi_cmp_mpi( &TU, &TV ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TU, &TU, &TV ) );
2694 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U1, &U1, &V1 ) );
2695 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &V2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 }
2697 else
2698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TV, &TV, &TU ) );
2700 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, &U1 ) );
2701 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &U2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 }
2703 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704 while( mbedtls_mpi_cmp_int( &TU, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 while( mbedtls_mpi_cmp_int( &V1, 0 ) < 0 )
2707 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 while( mbedtls_mpi_cmp_mpi( &V1, N ) >= 0 )
2710 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &V1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002713
2714cleanup:
2715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TU ); mbedtls_mpi_free( &U1 ); mbedtls_mpi_free( &U2 );
2717 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TV );
2718 mbedtls_mpi_free( &V1 ); mbedtls_mpi_free( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002719
2720 return( ret );
2721}
2722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002724
Paul Bakker5121ce52009-01-03 21:22:43 +00002725static const int small_prime[] =
2726{
2727 3, 5, 7, 11, 13, 17, 19, 23,
2728 29, 31, 37, 41, 43, 47, 53, 59,
2729 61, 67, 71, 73, 79, 83, 89, 97,
2730 101, 103, 107, 109, 113, 127, 131, 137,
2731 139, 149, 151, 157, 163, 167, 173, 179,
2732 181, 191, 193, 197, 199, 211, 223, 227,
2733 229, 233, 239, 241, 251, 257, 263, 269,
2734 271, 277, 281, 283, 293, 307, 311, 313,
2735 317, 331, 337, 347, 349, 353, 359, 367,
2736 373, 379, 383, 389, 397, 401, 409, 419,
2737 421, 431, 433, 439, 443, 449, 457, 461,
2738 463, 467, 479, 487, 491, 499, 503, 509,
2739 521, 523, 541, 547, 557, 563, 569, 571,
2740 577, 587, 593, 599, 601, 607, 613, 617,
2741 619, 631, 641, 643, 647, 653, 659, 661,
2742 673, 677, 683, 691, 701, 709, 719, 727,
2743 733, 739, 743, 751, 757, 761, 769, 773,
2744 787, 797, 809, 811, 821, 823, 827, 829,
2745 839, 853, 857, 859, 863, 877, 881, 883,
2746 887, 907, 911, 919, 929, 937, 941, 947,
2747 953, 967, 971, 977, 983, 991, 997, -103
2748};
2749
2750/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002751 * Small divisors test (X must be positive)
2752 *
2753 * Return values:
2754 * 0: no small factor (possible prime, more tests needed)
2755 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002757 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759static int mpi_check_small_factors( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +00002760{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002761 int ret = 0;
2762 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763 mbedtls_mpi_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +00002764
Paul Bakker5121ce52009-01-03 21:22:43 +00002765 if( ( X->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002767
2768 for( i = 0; small_prime[i] > 0; i++ )
2769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 if( mbedtls_mpi_cmp_int( X, small_prime[i] ) <= 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002771 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, small_prime[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002774
2775 if( r == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002777 }
2778
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002779cleanup:
2780 return( ret );
2781}
2782
2783/*
2784 * Miller-Rabin pseudo-primality test (HAC 4.24)
2785 */
Janos Follathda31fa12018-09-03 14:45:23 +01002786static int mpi_miller_rabin( const mbedtls_mpi *X, size_t rounds,
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002787 int (*f_rng)(void *, unsigned char *, size_t),
2788 void *p_rng )
2789{
Pascal Junodb99183d2015-03-11 16:49:45 +01002790 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002791 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002793
Hanno Becker8ce11a32018-12-19 16:18:52 +00002794 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002795 MPI_VALIDATE_RET( f_rng != NULL );
2796
2797 mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R );
2798 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799 mbedtls_mpi_init( &RR );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002800
Paul Bakker5121ce52009-01-03 21:22:43 +00002801 /*
2802 * W = |X| - 1
2803 * R = W >> lsb( W )
2804 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &W, X, 1 ) );
2806 s = mbedtls_mpi_lsb( &W );
2807 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R, &W ) );
2808 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &R, s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002809
Janos Follathda31fa12018-09-03 14:45:23 +01002810 for( i = 0; i < rounds; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002811 {
2812 /*
2813 * pick a random A, 1 < A < |X| - 1
2814 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002815 count = 0;
2816 do {
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002817 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
Pascal Junodb99183d2015-03-11 16:49:45 +01002818
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002819 j = mbedtls_mpi_bitlen( &A );
2820 k = mbedtls_mpi_bitlen( &W );
Pascal Junodb99183d2015-03-11 16:49:45 +01002821 if (j > k) {
Darryl Greene3f95ed2018-10-02 13:21:35 +01002822 A.p[A.n - 1] &= ( (mbedtls_mpi_uint) 1 << ( k - ( A.n - 1 ) * biL - 1 ) ) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002823 }
2824
2825 if (count++ > 30) {
Jens Wiklanderf08aa3e2019-01-17 13:30:57 +01002826 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2827 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002828 }
2829
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002830 } while ( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 ||
2831 mbedtls_mpi_cmp_int( &A, 1 ) <= 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002832
2833 /*
2834 * A = A^R mod |X|
2835 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &A, &A, &R, X, &RR ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838 if( mbedtls_mpi_cmp_mpi( &A, &W ) == 0 ||
2839 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002840 continue;
2841
2842 j = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843 while( j < s && mbedtls_mpi_cmp_mpi( &A, &W ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002844 {
2845 /*
2846 * A = A * A mod |X|
2847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &A, &A ) );
2849 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &A, &T, X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851 if( mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002852 break;
2853
2854 j++;
2855 }
2856
2857 /*
2858 * not prime if A != |X| - 1 or A == 1
2859 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860 if( mbedtls_mpi_cmp_mpi( &A, &W ) != 0 ||
2861 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002864 break;
2865 }
2866 }
2867
2868cleanup:
Hanno Becker73d7d792018-12-11 10:35:51 +00002869 mbedtls_mpi_free( &W ); mbedtls_mpi_free( &R );
2870 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002872
2873 return( ret );
2874}
2875
2876/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002877 * Pseudo-primality test: small factors, then Miller-Rabin
2878 */
Janos Follatha0b67c22018-09-18 14:48:23 +01002879int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds,
2880 int (*f_rng)(void *, unsigned char *, size_t),
2881 void *p_rng )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002882{
Janos Follath24eed8d2019-11-22 13:21:35 +00002883 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002884 mbedtls_mpi XX;
Hanno Becker8ce11a32018-12-19 16:18:52 +00002885 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002886 MPI_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002887
2888 XX.s = 1;
2889 XX.n = X->n;
2890 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892 if( mbedtls_mpi_cmp_int( &XX, 0 ) == 0 ||
2893 mbedtls_mpi_cmp_int( &XX, 1 ) == 0 )
2894 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896 if( mbedtls_mpi_cmp_int( &XX, 2 ) == 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002897 return( 0 );
2898
2899 if( ( ret = mpi_check_small_factors( &XX ) ) != 0 )
2900 {
2901 if( ret == 1 )
2902 return( 0 );
2903
2904 return( ret );
2905 }
2906
Janos Follathda31fa12018-09-03 14:45:23 +01002907 return( mpi_miller_rabin( &XX, rounds, f_rng, p_rng ) );
Janos Follathf301d232018-08-14 13:34:01 +01002908}
2909
Janos Follatha0b67c22018-09-18 14:48:23 +01002910#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Janos Follathf301d232018-08-14 13:34:01 +01002911/*
2912 * Pseudo-primality test, error probability 2^-80
2913 */
2914int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
2915 int (*f_rng)(void *, unsigned char *, size_t),
2916 void *p_rng )
2917{
Hanno Becker8ce11a32018-12-19 16:18:52 +00002918 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002919 MPI_VALIDATE_RET( f_rng != NULL );
2920
Janos Follatha0b67c22018-09-18 14:48:23 +01002921 /*
2922 * In the past our key generation aimed for an error rate of at most
2923 * 2^-80. Since this function is deprecated, aim for the same certainty
2924 * here as well.
2925 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002926 return( mbedtls_mpi_is_prime_ext( X, 40, f_rng, p_rng ) );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002927}
Janos Follatha0b67c22018-09-18 14:48:23 +01002928#endif
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002929
2930/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002931 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002932 *
Janos Follathf301d232018-08-14 13:34:01 +01002933 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2934 * be either 1024 bits or 1536 bits long, and flags must contain
2935 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002936 */
Janos Follath7c025a92018-08-14 11:08:41 +01002937int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002938 int (*f_rng)(void *, unsigned char *, size_t),
2939 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002940{
Jethro Beekman66689272018-02-14 19:24:10 -08002941#ifdef MBEDTLS_HAVE_INT64
2942// ceil(2^63.5)
2943#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2944#else
2945// ceil(2^31.5)
2946#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2947#endif
2948 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002949 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002950 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 mbedtls_mpi_uint r;
2952 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002953
Hanno Becker8ce11a32018-12-19 16:18:52 +00002954 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002955 MPI_VALIDATE_RET( f_rng != NULL );
2956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957 if( nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS )
2958 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960 mbedtls_mpi_init( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002961
2962 n = BITS_TO_LIMBS( nbits );
2963
Janos Follathda31fa12018-09-03 14:45:23 +01002964 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR ) == 0 )
2965 {
2966 /*
2967 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2968 */
2969 rounds = ( ( nbits >= 1300 ) ? 2 : ( nbits >= 850 ) ? 3 :
2970 ( nbits >= 650 ) ? 4 : ( nbits >= 350 ) ? 8 :
2971 ( nbits >= 250 ) ? 12 : ( nbits >= 150 ) ? 18 : 27 );
2972 }
2973 else
2974 {
2975 /*
2976 * 2^-100 error probability, number of rounds computed based on HAC,
2977 * fact 4.48
2978 */
2979 rounds = ( ( nbits >= 1450 ) ? 4 : ( nbits >= 1150 ) ? 5 :
2980 ( nbits >= 1000 ) ? 6 : ( nbits >= 850 ) ? 7 :
2981 ( nbits >= 750 ) ? 8 : ( nbits >= 500 ) ? 13 :
2982 ( nbits >= 250 ) ? 28 : ( nbits >= 150 ) ? 40 : 51 );
2983 }
2984
Jethro Beekman66689272018-02-14 19:24:10 -08002985 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002986 {
Jethro Beekman66689272018-02-14 19:24:10 -08002987 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
2988 /* make sure generated number is at least (nbits-1)+0.5 bits (FIPS 186-4 §B.3.3 steps 4.4, 5.5) */
2989 if( X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2 ) continue;
2990
2991 k = n * biL;
2992 if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits ) );
2993 X->p[0] |= 1;
2994
Janos Follath7c025a92018-08-14 11:08:41 +01002995 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002996 {
Janos Follatha0b67c22018-09-18 14:48:23 +01002997 ret = mbedtls_mpi_is_prime_ext( X, rounds, f_rng, p_rng );
Jethro Beekman66689272018-02-14 19:24:10 -08002998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003000 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003001 }
Jethro Beekman66689272018-02-14 19:24:10 -08003002 else
Paul Bakker5121ce52009-01-03 21:22:43 +00003003 {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01003004 /*
Jethro Beekman66689272018-02-14 19:24:10 -08003005 * An necessary condition for Y and X = 2Y + 1 to be prime
3006 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
3007 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01003008 */
Jethro Beekman66689272018-02-14 19:24:10 -08003009
3010 X->p[0] |= 2;
3011
3012 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, 3 ) );
3013 if( r == 0 )
3014 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 8 ) );
3015 else if( r == 1 )
3016 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 4 ) );
3017
3018 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
3019 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, X ) );
3020 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, 1 ) );
3021
3022 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003023 {
Jethro Beekman66689272018-02-14 19:24:10 -08003024 /*
3025 * First, check small factors for X and Y
3026 * before doing Miller-Rabin on any of them
3027 */
3028 if( ( ret = mpi_check_small_factors( X ) ) == 0 &&
3029 ( ret = mpi_check_small_factors( &Y ) ) == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01003030 ( ret = mpi_miller_rabin( X, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01003031 == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01003032 ( ret = mpi_miller_rabin( &Y, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01003033 == 0 )
Jethro Beekman66689272018-02-14 19:24:10 -08003034 goto cleanup;
3035
3036 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
3037 goto cleanup;
3038
3039 /*
3040 * Next candidates. We want to preserve Y = (X-1) / 2 and
3041 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
3042 * so up Y by 6 and X by 12.
3043 */
3044 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 12 ) );
3045 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &Y, &Y, 6 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003046 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 }
3048 }
3049
3050cleanup:
3051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052 mbedtls_mpi_free( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00003053
3054 return( ret );
3055}
3056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00003058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003060
Paul Bakker23986e52011-04-24 08:57:21 +00003061#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003062
3063static const int gcd_pairs[GCD_PAIR_COUNT][3] =
3064{
3065 { 693, 609, 21 },
3066 { 1764, 868, 28 },
3067 { 768454923, 542167814, 1 }
3068};
3069
Paul Bakker5121ce52009-01-03 21:22:43 +00003070/*
3071 * Checkup routine
3072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003073int mbedtls_mpi_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00003074{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003075 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00003077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); mbedtls_mpi_init( &X );
3079 mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &U ); mbedtls_mpi_init( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00003080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &A, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003082 "EFE021C2645FD1DC586E69184AF4A31E" \
3083 "D5F53E93B5F123FA41680867BA110131" \
3084 "944FE7952E2517337780CB0DB80E61AA" \
3085 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6" ) );
3086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003088 "B2E7EFD37075B9F03FF989C7C5051C20" \
3089 "34D2A323810251127E7BF8625A4F49A5" \
3090 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
3091 "5B5C25763222FEFCCFC38B832366C29E" ) );
3092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &N, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003094 "0066A198186C18C10B2F5ED9B522752A" \
3095 "9830B69916E535C8F047518A889A43A5" \
3096 "94B6BED27A168D31D4A52F88925AA8F5" ) );
3097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003100 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003101 "602AB7ECA597A3D6B56FF9829A5E8B85" \
3102 "9E857EA95A03512E2BAE7391688D264A" \
3103 "A5663B0341DB9CCFD2C4C5F421FEC814" \
3104 "8001B72E848A38CAE1C65F78E56ABDEF" \
3105 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
3106 "ECF677152EF804370C1A305CAF3B5BF1" \
3107 "30879B56C61DE584A0F53A2447A51E" ) );
3108
3109 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110 mbedtls_printf( " MPI test #1 (mul_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003113 {
3114 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003115 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003116
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003117 ret = 1;
3118 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003119 }
3120
3121 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &X, &Y, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003127 "256567336059E52CAE22925474705F39A94" ) );
3128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &V, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003130 "6613F26162223DF488E9CD48CC132C7A" \
3131 "0AC93C701B001B092E4E5B9F73BCD27B" \
3132 "9EE50D0657C77F374E903CDFA4C642" ) );
3133
3134 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135 mbedtls_printf( " MPI test #2 (div_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ||
3138 mbedtls_mpi_cmp_mpi( &Y, &V ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003139 {
3140 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003142
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003143 ret = 1;
3144 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003145 }
3146
3147 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &X, &A, &E, &N, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003153 "36E139AEA55215609D2816998ED020BB" \
3154 "BD96C37890F65171D948E9BC7CBAA4D9" \
3155 "325D24D6A3C12710F10A09FA08AB87" ) );
3156
3157 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 mbedtls_printf( " MPI test #3 (exp_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003161 {
3162 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003164
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003165 ret = 1;
3166 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003167 }
3168
3169 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003175 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
3176 "C3DBA76456363A10869622EAC2DD84EC" \
3177 "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) );
3178
3179 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180 mbedtls_printf( " MPI test #4 (inv_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003183 {
3184 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003186
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003187 ret = 1;
3188 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003189 }
3190
3191 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003193
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003194 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195 mbedtls_printf( " MPI test #5 (simple gcd): " );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003196
Paul Bakker66d5d072014-06-17 16:39:18 +02003197 for( i = 0; i < GCD_PAIR_COUNT; i++ )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &X, gcd_pairs[i][0] ) );
3200 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Y, gcd_pairs[i][1] ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &A, &X, &Y ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 if( mbedtls_mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 )
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003205 {
3206 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207 mbedtls_printf( "failed at %d\n", i );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003208
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003209 ret = 1;
3210 goto cleanup;
3211 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003212 }
3213
3214 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215 mbedtls_printf( "passed\n" );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003216
Paul Bakker5121ce52009-01-03 21:22:43 +00003217cleanup:
3218
3219 if( ret != 0 && verbose != 0 )
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003220 mbedtls_printf( "Unexpected error, return code = %08X\n", (unsigned int) ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &X );
3223 mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &U ); mbedtls_mpi_free( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00003224
3225 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003227
3228 return( ret );
3229}
3230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233#endif /* MBEDTLS_BIGNUM_C */