blob: 40f1239e9332c4f39a8e3cc0b1acb4b1b6690fea [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Multi-precision integer library
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
Simon Butcher15b15d12015-11-26 19:35:03 +000019
Paul Bakker5121ce52009-01-03 21:22:43 +000020/*
Simon Butcher15b15d12015-11-26 19:35:03 +000021 * The following sources were referenced in the design of this Multi-precision
22 * Integer library:
Paul Bakker5121ce52009-01-03 21:22:43 +000023 *
Simon Butcher15b15d12015-11-26 19:35:03 +000024 * [1] Handbook of Applied Cryptography - 1997
25 * Menezes, van Oorschot and Vanstone
26 *
27 * [2] Multi-Precision Math
28 * Tom St Denis
29 * https://github.com/libtom/libtommath/blob/develop/tommath.pdf
30 *
31 * [3] GNU Multi-Precision Arithmetic Library
32 * https://gmplib.org/manual/index.html
33 *
Simon Butcherf5ba0452015-12-27 23:01:55 +000034 */
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Gilles Peskinedb09ef62020-06-03 01:43:33 +020036#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/bignum.h"
Chris Jones4c5819c2021-03-03 17:45:34 +000041#include "bn_mul.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050042#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000043#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Rich Evans00ab4702015-02-06 13:43:58 +000045#include <string.h>
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020049#else
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <stdio.h>
51#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#define mbedtls_printf printf
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020053#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020055#endif
56
Hanno Becker73d7d792018-12-11 10:35:51 +000057#define MPI_VALIDATE_RET( cond ) \
58 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA )
59#define MPI_VALIDATE( cond ) \
60 MBEDTLS_INTERNAL_VALIDATE( cond )
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
Paul Bakker5121ce52009-01-03 21:22:43 +000063#define biL (ciL << 3) /* bits in limb */
64#define biH (ciL << 2) /* half limb size */
65
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +010066#define MPI_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
67
Paul Bakker5121ce52009-01-03 21:22:43 +000068/*
69 * Convert between bits/chars and number of limbs
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +020070 * Divide first in order to avoid potential overflows
Paul Bakker5121ce52009-01-03 21:22:43 +000071 */
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +020072#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
73#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050075/* Implementation that should never be optimized out by the compiler */
Andres Amaya Garcia6698d2f2018-04-24 08:39:07 -050076static void mbedtls_mpi_zeroize( mbedtls_mpi_uint *v, size_t n )
77{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050078 mbedtls_platform_zeroize( v, ciL * n );
79}
80
Paul Bakker5121ce52009-01-03 21:22:43 +000081/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000082 * Initialize one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084void mbedtls_mpi_init( mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +000085{
Hanno Becker73d7d792018-12-11 10:35:51 +000086 MPI_VALIDATE( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +000087
Paul Bakker6c591fa2011-05-05 11:49:20 +000088 X->s = 1;
89 X->n = 0;
90 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000091}
92
93/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000094 * Unallocate one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096void mbedtls_mpi_free( mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +000097{
Paul Bakker6c591fa2011-05-05 11:49:20 +000098 if( X == NULL )
99 return;
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Paul Bakker6c591fa2011-05-05 11:49:20 +0000101 if( X->p != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 {
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200103 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 }
106
Paul Bakker6c591fa2011-05-05 11:49:20 +0000107 X->s = 1;
108 X->n = 0;
109 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000110}
111
112/*
113 * Enlarge to the specified number of limbs
114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000116{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_mpi_uint *p;
Hanno Becker73d7d792018-12-11 10:35:51 +0000118 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200121 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Paul Bakkerf9688572011-05-05 10:00:45 +0000122
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 if( X->n < nblimbs )
124 {
Simon Butcher29176892016-05-20 00:19:09 +0100125 if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( nblimbs, ciL ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200126 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 if( X->p != NULL )
129 {
130 memcpy( p, X->p, X->n * ciL );
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200131 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 }
134
135 X->n = nblimbs;
136 X->p = p;
137 }
138
139 return( 0 );
140}
141
142/*
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100143 * Resize down as much as possible,
144 * while keeping at least the specified number of limbs
145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100147{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_mpi_uint *p;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100149 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000150 MPI_VALIDATE_RET( X != NULL );
151
152 if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
153 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100154
Gilles Peskinee2f563e2020-01-20 21:17:43 +0100155 /* Actually resize up if there are currently fewer than nblimbs limbs. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100156 if( X->n <= nblimbs )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 return( mbedtls_mpi_grow( X, nblimbs ) );
Gilles Peskine322752b2020-01-21 13:59:51 +0100158 /* After this point, then X->n > nblimbs and in particular X->n > 0. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100159
160 for( i = X->n - 1; i > 0; i-- )
161 if( X->p[i] != 0 )
162 break;
163 i++;
164
165 if( i < nblimbs )
166 i = nblimbs;
167
Simon Butcher29176892016-05-20 00:19:09 +0100168 if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( i, ciL ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200169 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100170
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100171 if( X->p != NULL )
172 {
173 memcpy( p, X->p, i * ciL );
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200174 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_free( X->p );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100176 }
177
178 X->n = i;
179 X->p = p;
180
181 return( 0 );
182}
183
Gilles Peskineed32b572021-06-02 22:17:52 +0200184/* Resize X to have exactly n limbs and set it to 0. */
185static int mbedtls_mpi_resize_clear( mbedtls_mpi *X, size_t limbs )
186{
187 if( limbs == 0 )
188 {
189 mbedtls_mpi_free( X );
190 return( 0 );
191 }
192 else if( X->n == limbs )
193 {
194 memset( X->p, 0, limbs * ciL );
195 X->s = 1;
196 return( 0 );
197 }
198 else
199 {
200 mbedtls_mpi_free( X );
201 return( mbedtls_mpi_grow( X, limbs ) );
202 }
203}
204
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100205/*
Gilles Peskine3da1a8f2021-06-08 23:17:42 +0200206 * Copy the contents of Y into X.
207 *
208 * This function is not constant-time. Leading zeros in Y may be removed.
209 *
210 * Ensure that X does not shrink. This is not guaranteed by the public API,
211 * but some code in the bignum module relies on this property, for example
212 * in mbedtls_mpi_exp_mod().
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000215{
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100216 int ret = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000217 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000218 MPI_VALIDATE_RET( X != NULL );
219 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
221 if( X == Y )
222 return( 0 );
223
Gilles Peskinedb420622020-01-20 21:12:50 +0100224 if( Y->n == 0 )
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200225 {
Gilles Peskine3da1a8f2021-06-08 23:17:42 +0200226 if( X->n != 0 )
227 {
228 X->s = 1;
229 memset( X->p, 0, X->n * ciL );
230 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200231 return( 0 );
232 }
233
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 for( i = Y->n - 1; i > 0; i-- )
235 if( Y->p[i] != 0 )
236 break;
237 i++;
238
239 X->s = Y->s;
240
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100241 if( X->n < i )
242 {
243 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i ) );
244 }
245 else
246 {
247 memset( X->p + i, 0, ( X->n - i ) * ciL );
248 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000249
Paul Bakker5121ce52009-01-03 21:22:43 +0000250 memcpy( X->p, Y->p, i * ciL );
251
252cleanup:
253
254 return( ret );
255}
256
257/*
258 * Swap the contents of X and Y
259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000261{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000263 MPI_VALIDATE( X != NULL );
264 MPI_VALIDATE( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 memcpy( &T, X, sizeof( mbedtls_mpi ) );
267 memcpy( X, Y, sizeof( mbedtls_mpi ) );
268 memcpy( Y, &T, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000269}
270
271/*
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200272 * Conditionally assign dest = src, without leaking information
273 * about whether the assignment was made or not.
274 * dest and src must be arrays of limbs of size n.
275 * assign must be 0 or 1.
276 */
277static void mpi_safe_cond_assign( size_t n,
278 mbedtls_mpi_uint *dest,
279 const mbedtls_mpi_uint *src,
280 unsigned char assign )
281{
282 size_t i;
283 for( i = 0; i < n; i++ )
284 dest[i] = dest[i] * ( 1 - assign ) + src[i] * assign;
285}
286
287/*
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100288 * Conditionally assign X = Y, without leaking information
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +0100289 * about whether the assignment was made or not.
290 * (Leaking information about the respective sizes of X and Y is ok however.)
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100293{
294 int ret = 0;
295 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000296 MPI_VALIDATE_RET( X != NULL );
297 MPI_VALIDATE_RET( Y != NULL );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100298
Pascal Junodb99183d2015-03-11 16:49:45 +0100299 /* make sure assign is 0 or 1 in a time-constant manner */
300 assign = (assign | (unsigned char)-assign) >> 7;
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100303
Paul Bakker66d5d072014-06-17 16:39:18 +0200304 X->s = X->s * ( 1 - assign ) + Y->s * assign;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100305
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200306 mpi_safe_cond_assign( Y->n, X->p, Y->p, assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100307
Gilles Peskinef04d11e2020-06-04 19:14:58 +0200308 for( i = Y->n; i < X->n; i++ )
Paul Bakker66d5d072014-06-17 16:39:18 +0200309 X->p[i] *= ( 1 - assign );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100310
311cleanup:
312 return( ret );
313}
314
315/*
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100316 * Conditionally swap X and Y, without leaking information
317 * about whether the swap was made or not.
318 * Here it is not ok to simply swap the pointers, which whould lead to
319 * different memory access patterns when X and Y are used afterwards.
320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100322{
323 int ret, s;
324 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 mbedtls_mpi_uint tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +0000326 MPI_VALIDATE_RET( X != NULL );
327 MPI_VALIDATE_RET( Y != NULL );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100328
329 if( X == Y )
330 return( 0 );
331
Pascal Junodb99183d2015-03-11 16:49:45 +0100332 /* make sure swap is 0 or 1 in a time-constant manner */
333 swap = (swap | (unsigned char)-swap) >> 7;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
336 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( Y, X->n ) );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100337
338 s = X->s;
Paul Bakker66d5d072014-06-17 16:39:18 +0200339 X->s = X->s * ( 1 - swap ) + Y->s * swap;
340 Y->s = Y->s * ( 1 - swap ) + s * swap;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100341
342
343 for( i = 0; i < X->n; i++ )
344 {
345 tmp = X->p[i];
Paul Bakker66d5d072014-06-17 16:39:18 +0200346 X->p[i] = X->p[i] * ( 1 - swap ) + Y->p[i] * swap;
347 Y->p[i] = Y->p[i] * ( 1 - swap ) + tmp * swap;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100348 }
349
350cleanup:
351 return( ret );
352}
353
354/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000355 * Set value from integer
356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +0000358{
Janos Follath24eed8d2019-11-22 13:21:35 +0000359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker73d7d792018-12-11 10:35:51 +0000360 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 memset( X->p, 0, X->n * ciL );
364
365 X->p[0] = ( z < 0 ) ? -z : z;
366 X->s = ( z < 0 ) ? -1 : 1;
367
368cleanup:
369
370 return( ret );
371}
372
373/*
Paul Bakker2f5947e2011-05-18 15:47:11 +0000374 * Get a specific bit
375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000377{
Hanno Becker73d7d792018-12-11 10:35:51 +0000378 MPI_VALIDATE_RET( X != NULL );
379
Paul Bakker2f5947e2011-05-18 15:47:11 +0000380 if( X->n * biL <= pos )
381 return( 0 );
382
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200383 return( ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000384}
385
Gilles Peskine11cdb052018-11-20 16:47:47 +0100386/* Get a specific byte, without range checks. */
387#define GET_BYTE( X, i ) \
388 ( ( ( X )->p[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff )
389
Paul Bakker2f5947e2011-05-18 15:47:11 +0000390/*
391 * Set a bit to a specific value of 0 or 1
392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000394{
395 int ret = 0;
396 size_t off = pos / biL;
397 size_t idx = pos % biL;
Hanno Becker73d7d792018-12-11 10:35:51 +0000398 MPI_VALIDATE_RET( X != NULL );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000399
400 if( val != 0 && val != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker9af723c2014-05-01 13:03:14 +0200402
Paul Bakker2f5947e2011-05-18 15:47:11 +0000403 if( X->n * biL <= pos )
404 {
405 if( val == 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200406 return( 0 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, off + 1 ) );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000409 }
410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 X->p[off] &= ~( (mbedtls_mpi_uint) 0x01 << idx );
412 X->p[off] |= (mbedtls_mpi_uint) val << idx;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000413
414cleanup:
Paul Bakker9af723c2014-05-01 13:03:14 +0200415
Paul Bakker2f5947e2011-05-18 15:47:11 +0000416 return( ret );
417}
418
419/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200420 * Return the number of less significant zero-bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422size_t mbedtls_mpi_lsb( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000423{
Paul Bakker23986e52011-04-24 08:57:21 +0000424 size_t i, j, count = 0;
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000425 MBEDTLS_INTERNAL_VALIDATE_RET( X != NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
427 for( i = 0; i < X->n; i++ )
Paul Bakkerf9688572011-05-05 10:00:45 +0000428 for( j = 0; j < biL; j++, count++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 if( ( ( X->p[i] >> j ) & 1 ) != 0 )
430 return( count );
431
432 return( 0 );
433}
434
435/*
Simon Butcher15b15d12015-11-26 19:35:03 +0000436 * Count leading zero bits in a given integer
437 */
438static size_t mbedtls_clz( const mbedtls_mpi_uint x )
439{
440 size_t j;
Manuel Pégourié-Gonnarde3e8edf2015-12-01 09:31:52 +0100441 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
Simon Butcher15b15d12015-11-26 19:35:03 +0000442
443 for( j = 0; j < biL; j++ )
444 {
445 if( x & mask ) break;
446
447 mask >>= 1;
448 }
449
450 return j;
451}
452
453/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200454 * Return the number of bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200456size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000457{
Paul Bakker23986e52011-04-24 08:57:21 +0000458 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +0000459
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200460 if( X->n == 0 )
461 return( 0 );
462
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 for( i = X->n - 1; i > 0; i-- )
464 if( X->p[i] != 0 )
465 break;
466
Simon Butcher15b15d12015-11-26 19:35:03 +0000467 j = biL - mbedtls_clz( X->p[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
Paul Bakker23986e52011-04-24 08:57:21 +0000469 return( ( i * biL ) + j );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470}
471
472/*
473 * Return the total size in bytes
474 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475size_t mbedtls_mpi_size( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000476{
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200477 return( ( mbedtls_mpi_bitlen( X ) + 7 ) >> 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000478}
479
480/*
481 * Convert an ASCII character to digit value
482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483static int mpi_get_digit( mbedtls_mpi_uint *d, int radix, char c )
Paul Bakker5121ce52009-01-03 21:22:43 +0000484{
485 *d = 255;
486
487 if( c >= 0x30 && c <= 0x39 ) *d = c - 0x30;
488 if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37;
489 if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57;
490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 if( *d >= (mbedtls_mpi_uint) radix )
492 return( MBEDTLS_ERR_MPI_INVALID_CHARACTER );
Paul Bakker5121ce52009-01-03 21:22:43 +0000493
494 return( 0 );
495}
496
497/*
498 * Import from an ASCII string
499 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000501{
Janos Follath24eed8d2019-11-22 13:21:35 +0000502 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000503 size_t i, j, slen, n;
Gilles Peskine80f56732021-04-03 18:26:13 +0200504 int sign = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_mpi_uint d;
506 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000507 MPI_VALIDATE_RET( X != NULL );
508 MPI_VALIDATE_RET( s != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
510 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000511 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000514
Gilles Peskine80f56732021-04-03 18:26:13 +0200515 if( s[0] == '-' )
516 {
517 ++s;
518 sign = -1;
519 }
520
Paul Bakkerff60ee62010-03-16 21:09:09 +0000521 slen = strlen( s );
522
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 if( radix == 16 )
524 {
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +0100525 if( slen > MPI_SIZE_T_MAX >> 2 )
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +0200526 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
527
Paul Bakkerff60ee62010-03-16 21:09:09 +0000528 n = BITS_TO_LIMBS( slen << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n ) );
531 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000532
Paul Bakker23986e52011-04-24 08:57:21 +0000533 for( i = slen, j = 0; i > 0; i--, j++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200536 X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 }
538 }
539 else
540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000542
Paul Bakkerff60ee62010-03-16 21:09:09 +0000543 for( i = 0; i < slen; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
546 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T, X, radix ) );
Gilles Peskine80f56732021-04-03 18:26:13 +0200547 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, &T, d ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000548 }
549 }
550
Gilles Peskine80f56732021-04-03 18:26:13 +0200551 if( sign < 0 && mbedtls_mpi_bitlen( X ) != 0 )
552 X->s = -1;
553
Paul Bakker5121ce52009-01-03 21:22:43 +0000554cleanup:
555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
558 return( ret );
559}
560
561/*
Ron Eldora16fa292018-11-20 14:07:01 +0200562 * Helper to write the digits high-order first.
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 */
Ron Eldora16fa292018-11-20 14:07:01 +0200564static int mpi_write_hlp( mbedtls_mpi *X, int radix,
565 char **p, const size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000566{
Janos Follath24eed8d2019-11-22 13:21:35 +0000567 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_mpi_uint r;
Ron Eldora16fa292018-11-20 14:07:01 +0200569 size_t length = 0;
570 char *p_end = *p + buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
Ron Eldora16fa292018-11-20 14:07:01 +0200572 do
573 {
574 if( length >= buflen )
575 {
576 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
577 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Ron Eldora16fa292018-11-20 14:07:01 +0200579 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) );
580 MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) );
581 /*
582 * Write the residue in the current position, as an ASCII character.
583 */
584 if( r < 0xA )
585 *(--p_end) = (char)( '0' + r );
586 else
587 *(--p_end) = (char)( 'A' + ( r - 0xA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000588
Ron Eldora16fa292018-11-20 14:07:01 +0200589 length++;
590 } while( mbedtls_mpi_cmp_int( X, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
Ron Eldora16fa292018-11-20 14:07:01 +0200592 memmove( *p, p_end, length );
593 *p += length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
595cleanup:
596
597 return( ret );
598}
599
600/*
601 * Export into an ASCII string
602 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100603int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
604 char *buf, size_t buflen, size_t *olen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000605{
Paul Bakker23986e52011-04-24 08:57:21 +0000606 int ret = 0;
607 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000608 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000610 MPI_VALIDATE_RET( X != NULL );
611 MPI_VALIDATE_RET( olen != NULL );
612 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000613
614 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000615 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000616
Hanno Becker23cfea02019-02-04 09:45:07 +0000617 n = mbedtls_mpi_bitlen( X ); /* Number of bits necessary to present `n`. */
618 if( radix >= 4 ) n >>= 1; /* Number of 4-adic digits necessary to present
619 * `n`. If radix > 4, this might be a strict
620 * overapproximation of the number of
621 * radix-adic digits needed to present `n`. */
622 if( radix >= 16 ) n >>= 1; /* Number of hexadecimal digits necessary to
623 * present `n`. */
624
Janos Follath80470622019-03-06 13:43:02 +0000625 n += 1; /* Terminating null byte */
Hanno Becker23cfea02019-02-04 09:45:07 +0000626 n += 1; /* Compensate for the divisions above, which round down `n`
627 * in case it's not even. */
628 n += 1; /* Potential '-'-sign. */
629 n += ( n & 1 ); /* Make n even to have enough space for hexadecimal writing,
630 * which always uses an even number of hex-digits. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000631
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100632 if( buflen < n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000633 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100634 *olen = n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000636 }
637
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100638 p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000640
641 if( X->s == -1 )
Hanno Beckerc983c812019-02-01 16:41:30 +0000642 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000643 *p++ = '-';
Hanno Beckerc983c812019-02-01 16:41:30 +0000644 buflen--;
645 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
647 if( radix == 16 )
648 {
Paul Bakker23986e52011-04-24 08:57:21 +0000649 int c;
650 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000651
Paul Bakker23986e52011-04-24 08:57:21 +0000652 for( i = X->n, k = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000653 {
Paul Bakker23986e52011-04-24 08:57:21 +0000654 for( j = ciL; j > 0; j-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 {
Paul Bakker23986e52011-04-24 08:57:21 +0000656 c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000657
Paul Bakker6c343d72014-07-10 14:36:19 +0200658 if( c == 0 && k == 0 && ( i + j ) != 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000659 continue;
660
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000661 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000662 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 k = 1;
664 }
665 }
666 }
667 else
668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T, X ) );
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000670
671 if( T.s == -1 )
672 T.s = 1;
673
Ron Eldora16fa292018-11-20 14:07:01 +0200674 MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p, buflen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000675 }
676
677 *p++ = '\0';
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100678 *olen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000679
680cleanup:
681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
684 return( ret );
685}
686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000688/*
689 * Read X from an opened file
690 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin )
Paul Bakker5121ce52009-01-03 21:22:43 +0000692{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_mpi_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000694 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000695 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000696 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000697 * Buffer should have space for (short) label and decimal formatted MPI,
698 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Paul Bakker5121ce52009-01-03 21:22:43 +0000701
Hanno Becker73d7d792018-12-11 10:35:51 +0000702 MPI_VALIDATE_RET( X != NULL );
703 MPI_VALIDATE_RET( fin != NULL );
704
705 if( radix < 2 || radix > 16 )
706 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
707
Paul Bakker5121ce52009-01-03 21:22:43 +0000708 memset( s, 0, sizeof( s ) );
709 if( fgets( s, sizeof( s ) - 1, fin ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000711
712 slen = strlen( s );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000713 if( slen == sizeof( s ) - 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000715
Hanno Beckerb2034b72017-04-26 11:46:46 +0100716 if( slen > 0 && s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
717 if( slen > 0 && s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
Paul Bakker5121ce52009-01-03 21:22:43 +0000718
719 p = s + slen;
Hanno Beckerb2034b72017-04-26 11:46:46 +0100720 while( p-- > s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000721 if( mpi_get_digit( &d, radix, *p ) != 0 )
722 break;
723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 return( mbedtls_mpi_read_string( X, radix, p + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000725}
726
727/*
728 * Write X into an opened file (or stdout if fout == NULL)
729 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout )
Paul Bakker5121ce52009-01-03 21:22:43 +0000731{
Janos Follath24eed8d2019-11-22 13:21:35 +0000732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000733 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000734 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000735 * Buffer should have space for (short) label and decimal formatted MPI,
736 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Hanno Becker73d7d792018-12-11 10:35:51 +0000739 MPI_VALIDATE_RET( X != NULL );
740
741 if( radix < 2 || radix > 16 )
742 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000743
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100744 memset( s, 0, sizeof( s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100746 MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, sizeof( s ) - 2, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000747
748 if( p == NULL ) p = "";
749
750 plen = strlen( p );
751 slen = strlen( s );
752 s[slen++] = '\r';
753 s[slen++] = '\n';
754
755 if( fout != NULL )
756 {
757 if( fwrite( p, 1, plen, fout ) != plen ||
758 fwrite( s, 1, slen, fout ) != slen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000760 }
761 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_printf( "%s%s", p, s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000763
764cleanup:
765
766 return( ret );
767}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
Hanno Beckerda1655a2017-10-18 14:21:44 +0100770
771/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
772 * into the storage form used by mbedtls_mpi. */
Hanno Beckerf8720072018-11-08 11:53:49 +0000773
774static mbedtls_mpi_uint mpi_uint_bigendian_to_host_c( mbedtls_mpi_uint x )
775{
776 uint8_t i;
Hanno Becker031d6332019-05-01 17:09:11 +0100777 unsigned char *x_ptr;
Hanno Beckerf8720072018-11-08 11:53:49 +0000778 mbedtls_mpi_uint tmp = 0;
Hanno Becker031d6332019-05-01 17:09:11 +0100779
780 for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
781 {
782 tmp <<= CHAR_BIT;
783 tmp |= (mbedtls_mpi_uint) *x_ptr;
784 }
785
Hanno Beckerf8720072018-11-08 11:53:49 +0000786 return( tmp );
787}
788
789static mbedtls_mpi_uint mpi_uint_bigendian_to_host( mbedtls_mpi_uint x )
790{
791#if defined(__BYTE_ORDER__)
792
793/* Nothing to do on bigendian systems. */
794#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
795 return( x );
796#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
797
798#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
799
800/* For GCC and Clang, have builtins for byte swapping. */
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000801#if defined(__GNUC__) && defined(__GNUC_PREREQ)
802#if __GNUC_PREREQ(4,3)
Hanno Beckerf8720072018-11-08 11:53:49 +0000803#define have_bswap
804#endif
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000805#endif
806
807#if defined(__clang__) && defined(__has_builtin)
808#if __has_builtin(__builtin_bswap32) && \
809 __has_builtin(__builtin_bswap64)
810#define have_bswap
811#endif
812#endif
813
Hanno Beckerf8720072018-11-08 11:53:49 +0000814#if defined(have_bswap)
815 /* The compiler is hopefully able to statically evaluate this! */
816 switch( sizeof(mbedtls_mpi_uint) )
817 {
818 case 4:
819 return( __builtin_bswap32(x) );
820 case 8:
821 return( __builtin_bswap64(x) );
822 }
823#endif
824#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
825#endif /* __BYTE_ORDER__ */
826
827 /* Fall back to C-based reordering if we don't know the byte order
828 * or we couldn't use a compiler-specific builtin. */
829 return( mpi_uint_bigendian_to_host_c( x ) );
830}
831
Hanno Becker2be8a552018-10-25 12:40:09 +0100832static void mpi_bigendian_to_host( mbedtls_mpi_uint * const p, size_t limbs )
Hanno Beckerda1655a2017-10-18 14:21:44 +0100833{
Hanno Beckerda1655a2017-10-18 14:21:44 +0100834 mbedtls_mpi_uint *cur_limb_left;
835 mbedtls_mpi_uint *cur_limb_right;
Hanno Becker2be8a552018-10-25 12:40:09 +0100836 if( limbs == 0 )
837 return;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100838
839 /*
840 * Traverse limbs and
841 * - adapt byte-order in each limb
842 * - swap the limbs themselves.
843 * For that, simultaneously traverse the limbs from left to right
844 * and from right to left, as long as the left index is not bigger
845 * than the right index (it's not a problem if limbs is odd and the
846 * indices coincide in the last iteration).
847 */
Hanno Beckerda1655a2017-10-18 14:21:44 +0100848 for( cur_limb_left = p, cur_limb_right = p + ( limbs - 1 );
849 cur_limb_left <= cur_limb_right;
850 cur_limb_left++, cur_limb_right-- )
851 {
Hanno Beckerf8720072018-11-08 11:53:49 +0000852 mbedtls_mpi_uint tmp;
853 /* Note that if cur_limb_left == cur_limb_right,
854 * this code effectively swaps the bytes only once. */
855 tmp = mpi_uint_bigendian_to_host( *cur_limb_left );
856 *cur_limb_left = mpi_uint_bigendian_to_host( *cur_limb_right );
857 *cur_limb_right = tmp;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100858 }
Hanno Beckerda1655a2017-10-18 14:21:44 +0100859}
860
Paul Bakker5121ce52009-01-03 21:22:43 +0000861/*
Janos Follatha778a942019-02-13 10:28:28 +0000862 * Import X from unsigned binary data, little endian
863 */
864int mbedtls_mpi_read_binary_le( mbedtls_mpi *X,
865 const unsigned char *buf, size_t buflen )
866{
Janos Follath24eed8d2019-11-22 13:21:35 +0000867 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follatha778a942019-02-13 10:28:28 +0000868 size_t i;
869 size_t const limbs = CHARS_TO_LIMBS( buflen );
870
871 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskineed32b572021-06-02 22:17:52 +0200872 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Janos Follatha778a942019-02-13 10:28:28 +0000873
874 for( i = 0; i < buflen; i++ )
875 X->p[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
876
877cleanup:
878
Janos Follath171a7ef2019-02-15 16:17:45 +0000879 /*
880 * This function is also used to import keys. However, wiping the buffers
881 * upon failure is not necessary because failure only can happen before any
882 * input is copied.
883 */
Janos Follatha778a942019-02-13 10:28:28 +0000884 return( ret );
885}
886
887/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000888 * Import X from unsigned binary data, big endian
889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000891{
Janos Follath24eed8d2019-11-22 13:21:35 +0000892 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100893 size_t const limbs = CHARS_TO_LIMBS( buflen );
894 size_t const overhead = ( limbs * ciL ) - buflen;
895 unsigned char *Xp;
Paul Bakker5121ce52009-01-03 21:22:43 +0000896
Hanno Becker8ce11a32018-12-19 16:18:52 +0000897 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +0000898 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
899
Hanno Becker073c1992017-10-17 15:17:27 +0100900 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskineed32b572021-06-02 22:17:52 +0200901 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000902
Gilles Peskineed32b572021-06-02 22:17:52 +0200903 /* Avoid calling `memcpy` with NULL source or destination argument,
Hanno Becker0e810b92019-01-03 17:13:11 +0000904 * even if buflen is 0. */
Gilles Peskineed32b572021-06-02 22:17:52 +0200905 if( buflen != 0 )
Hanno Becker0e810b92019-01-03 17:13:11 +0000906 {
907 Xp = (unsigned char*) X->p;
908 memcpy( Xp + overhead, buf, buflen );
Hanno Beckerda1655a2017-10-18 14:21:44 +0100909
Hanno Becker0e810b92019-01-03 17:13:11 +0000910 mpi_bigendian_to_host( X->p, limbs );
911 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000912
913cleanup:
914
Janos Follath171a7ef2019-02-15 16:17:45 +0000915 /*
916 * This function is also used to import keys. However, wiping the buffers
917 * upon failure is not necessary because failure only can happen before any
918 * input is copied.
919 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000920 return( ret );
921}
922
923/*
Janos Follathe344d0f2019-02-19 16:17:40 +0000924 * Export X into unsigned binary data, little endian
925 */
926int mbedtls_mpi_write_binary_le( const mbedtls_mpi *X,
927 unsigned char *buf, size_t buflen )
928{
929 size_t stored_bytes = X->n * ciL;
930 size_t bytes_to_copy;
931 size_t i;
932
933 if( stored_bytes < buflen )
934 {
935 bytes_to_copy = stored_bytes;
936 }
937 else
938 {
939 bytes_to_copy = buflen;
940
941 /* The output buffer is smaller than the allocated size of X.
942 * However X may fit if its leading bytes are zero. */
943 for( i = bytes_to_copy; i < stored_bytes; i++ )
944 {
945 if( GET_BYTE( X, i ) != 0 )
946 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
947 }
948 }
949
950 for( i = 0; i < bytes_to_copy; i++ )
951 buf[i] = GET_BYTE( X, i );
952
953 if( stored_bytes < buflen )
954 {
955 /* Write trailing 0 bytes */
956 memset( buf + stored_bytes, 0, buflen - stored_bytes );
957 }
958
959 return( 0 );
960}
961
962/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000963 * Export X into unsigned binary data, big endian
964 */
Gilles Peskine11cdb052018-11-20 16:47:47 +0100965int mbedtls_mpi_write_binary( const mbedtls_mpi *X,
966 unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000967{
Hanno Becker73d7d792018-12-11 10:35:51 +0000968 size_t stored_bytes;
Gilles Peskine11cdb052018-11-20 16:47:47 +0100969 size_t bytes_to_copy;
970 unsigned char *p;
971 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000972
Hanno Becker73d7d792018-12-11 10:35:51 +0000973 MPI_VALIDATE_RET( X != NULL );
974 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
975
976 stored_bytes = X->n * ciL;
977
Gilles Peskine11cdb052018-11-20 16:47:47 +0100978 if( stored_bytes < buflen )
979 {
980 /* There is enough space in the output buffer. Write initial
981 * null bytes and record the position at which to start
982 * writing the significant bytes. In this case, the execution
983 * trace of this function does not depend on the value of the
984 * number. */
985 bytes_to_copy = stored_bytes;
986 p = buf + buflen - stored_bytes;
987 memset( buf, 0, buflen - stored_bytes );
988 }
989 else
990 {
991 /* The output buffer is smaller than the allocated size of X.
992 * However X may fit if its leading bytes are zero. */
993 bytes_to_copy = buflen;
994 p = buf;
995 for( i = bytes_to_copy; i < stored_bytes; i++ )
996 {
997 if( GET_BYTE( X, i ) != 0 )
998 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
999 }
1000 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
Gilles Peskine11cdb052018-11-20 16:47:47 +01001002 for( i = 0; i < bytes_to_copy; i++ )
1003 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
Paul Bakker5121ce52009-01-03 21:22:43 +00001004
1005 return( 0 );
1006}
1007
1008/*
1009 * Left-shift: X <<= count
1010 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +00001012{
Janos Follath24eed8d2019-11-22 13:21:35 +00001013 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001014 size_t i, v0, t1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +00001016 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
1018 v0 = count / (biL );
1019 t1 = count & (biL - 1);
1020
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001021 i = mbedtls_mpi_bitlen( X ) + count;
Paul Bakker5121ce52009-01-03 21:22:43 +00001022
Paul Bakkerf9688572011-05-05 10:00:45 +00001023 if( X->n * biL < i )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001025
1026 ret = 0;
1027
1028 /*
1029 * shift by count / limb_size
1030 */
1031 if( v0 > 0 )
1032 {
Paul Bakker23986e52011-04-24 08:57:21 +00001033 for( i = X->n; i > v0; i-- )
1034 X->p[i - 1] = X->p[i - v0 - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001035
Paul Bakker23986e52011-04-24 08:57:21 +00001036 for( ; i > 0; i-- )
1037 X->p[i - 1] = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001038 }
1039
1040 /*
1041 * shift by count % limb_size
1042 */
1043 if( t1 > 0 )
1044 {
1045 for( i = v0; i < X->n; i++ )
1046 {
1047 r1 = X->p[i] >> (biL - t1);
1048 X->p[i] <<= t1;
1049 X->p[i] |= r0;
1050 r0 = r1;
1051 }
1052 }
1053
1054cleanup:
1055
1056 return( ret );
1057}
1058
1059/*
1060 * Right-shift: X >>= count
1061 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +00001063{
Paul Bakker23986e52011-04-24 08:57:21 +00001064 size_t i, v0, v1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +00001066 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001067
1068 v0 = count / biL;
1069 v1 = count & (biL - 1);
1070
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001071 if( v0 > X->n || ( v0 == X->n && v1 > 0 ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 return mbedtls_mpi_lset( X, 0 );
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001073
Paul Bakker5121ce52009-01-03 21:22:43 +00001074 /*
1075 * shift by count / limb_size
1076 */
1077 if( v0 > 0 )
1078 {
1079 for( i = 0; i < X->n - v0; i++ )
1080 X->p[i] = X->p[i + v0];
1081
1082 for( ; i < X->n; i++ )
1083 X->p[i] = 0;
1084 }
1085
1086 /*
1087 * shift by count % limb_size
1088 */
1089 if( v1 > 0 )
1090 {
Paul Bakker23986e52011-04-24 08:57:21 +00001091 for( i = X->n; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001092 {
Paul Bakker23986e52011-04-24 08:57:21 +00001093 r1 = X->p[i - 1] << (biL - v1);
1094 X->p[i - 1] >>= v1;
1095 X->p[i - 1] |= r0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001096 r0 = r1;
1097 }
1098 }
1099
1100 return( 0 );
1101}
1102
1103/*
1104 * Compare unsigned values
1105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001107{
Paul Bakker23986e52011-04-24 08:57:21 +00001108 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001109 MPI_VALIDATE_RET( X != NULL );
1110 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001111
Paul Bakker23986e52011-04-24 08:57:21 +00001112 for( i = X->n; i > 0; i-- )
1113 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001114 break;
1115
Paul Bakker23986e52011-04-24 08:57:21 +00001116 for( j = Y->n; j > 0; j-- )
1117 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001118 break;
1119
Paul Bakker23986e52011-04-24 08:57:21 +00001120 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001121 return( 0 );
1122
1123 if( i > j ) return( 1 );
1124 if( j > i ) return( -1 );
1125
Paul Bakker23986e52011-04-24 08:57:21 +00001126 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001127 {
Paul Bakker23986e52011-04-24 08:57:21 +00001128 if( X->p[i - 1] > Y->p[i - 1] ) return( 1 );
1129 if( X->p[i - 1] < Y->p[i - 1] ) return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 }
1131
1132 return( 0 );
1133}
1134
1135/*
1136 * Compare signed values
1137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001139{
Paul Bakker23986e52011-04-24 08:57:21 +00001140 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001141 MPI_VALIDATE_RET( X != NULL );
1142 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001143
Paul Bakker23986e52011-04-24 08:57:21 +00001144 for( i = X->n; i > 0; i-- )
1145 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001146 break;
1147
Paul Bakker23986e52011-04-24 08:57:21 +00001148 for( j = Y->n; j > 0; j-- )
1149 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001150 break;
1151
Paul Bakker23986e52011-04-24 08:57:21 +00001152 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001153 return( 0 );
1154
1155 if( i > j ) return( X->s );
Paul Bakker0c8f73b2012-03-22 14:08:57 +00001156 if( j > i ) return( -Y->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001157
1158 if( X->s > 0 && Y->s < 0 ) return( 1 );
1159 if( Y->s > 0 && X->s < 0 ) return( -1 );
1160
Paul Bakker23986e52011-04-24 08:57:21 +00001161 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001162 {
Paul Bakker23986e52011-04-24 08:57:21 +00001163 if( X->p[i - 1] > Y->p[i - 1] ) return( X->s );
1164 if( X->p[i - 1] < Y->p[i - 1] ) return( -X->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001165 }
1166
1167 return( 0 );
1168}
1169
Janos Follath3f6f0e42019-10-14 09:09:32 +01001170/** Decide if an integer is less than the other, without branches.
1171 *
1172 * \param x First integer.
1173 * \param y Second integer.
1174 *
1175 * \return 1 if \p x is less than \p y, 0 otherwise
1176 */
Janos Follath0e5532d2019-10-11 14:21:53 +01001177static unsigned ct_lt_mpi_uint( const mbedtls_mpi_uint x,
1178 const mbedtls_mpi_uint y )
Janos Follathee6abce2019-09-05 14:47:19 +01001179{
1180 mbedtls_mpi_uint ret;
1181 mbedtls_mpi_uint cond;
1182
1183 /*
1184 * Check if the most significant bits (MSB) of the operands are different.
1185 */
1186 cond = ( x ^ y );
1187 /*
1188 * If the MSB are the same then the difference x-y will be negative (and
1189 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
1190 */
1191 ret = ( x - y ) & ~cond;
1192 /*
1193 * If the MSB are different, then the operand with the MSB of 1 is the
1194 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
1195 * the MSB of y is 0.)
1196 */
1197 ret |= y & cond;
1198
1199
Janos Follatha0f732b2019-10-14 08:59:14 +01001200 ret = ret >> ( biL - 1 );
Janos Follathee6abce2019-09-05 14:47:19 +01001201
Janos Follath67ce6472019-10-29 15:08:46 +00001202 return (unsigned) ret;
Janos Follathee6abce2019-09-05 14:47:19 +01001203}
1204
1205/*
1206 * Compare signed values in constant time
1207 */
Janos Follath0e5532d2019-10-11 14:21:53 +01001208int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y,
1209 unsigned *ret )
Janos Follathee6abce2019-09-05 14:47:19 +01001210{
1211 size_t i;
Janos Follathbb5147f2019-10-28 12:23:18 +00001212 /* The value of any of these variables is either 0 or 1 at all times. */
Janos Follath5e614ce2019-10-28 12:31:34 +00001213 unsigned cond, done, X_is_negative, Y_is_negative;
Janos Follathee6abce2019-09-05 14:47:19 +01001214
1215 MPI_VALIDATE_RET( X != NULL );
1216 MPI_VALIDATE_RET( Y != NULL );
1217 MPI_VALIDATE_RET( ret != NULL );
1218
1219 if( X->n != Y->n )
1220 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1221
1222 /*
Janos Follath73ba9ec2019-10-28 12:12:15 +00001223 * Set sign_N to 1 if N >= 0, 0 if N < 0.
1224 * We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0.
Janos Follathee6abce2019-09-05 14:47:19 +01001225 */
Janos Follath5e614ce2019-10-28 12:31:34 +00001226 X_is_negative = ( X->s & 2 ) >> 1;
1227 Y_is_negative = ( Y->s & 2 ) >> 1;
Janos Follath0e5532d2019-10-11 14:21:53 +01001228
1229 /*
1230 * If the signs are different, then the positive operand is the bigger.
Janos Follath5e614ce2019-10-28 12:31:34 +00001231 * That is if X is negative (X_is_negative == 1), then X < Y is true and it
1232 * is false if X is positive (X_is_negative == 0).
Janos Follath0e5532d2019-10-11 14:21:53 +01001233 */
Janos Follath5e614ce2019-10-28 12:31:34 +00001234 cond = ( X_is_negative ^ Y_is_negative );
1235 *ret = cond & X_is_negative;
Janos Follath0e5532d2019-10-11 14:21:53 +01001236
1237 /*
Janos Follathbb5147f2019-10-28 12:23:18 +00001238 * This is a constant-time function. We might have the result, but we still
Janos Follath0e5532d2019-10-11 14:21:53 +01001239 * need to go through the loop. Record if we have the result already.
1240 */
Janos Follathee6abce2019-09-05 14:47:19 +01001241 done = cond;
1242
1243 for( i = X->n; i > 0; i-- )
1244 {
1245 /*
Janos Follath30702422019-11-05 12:24:52 +00001246 * If Y->p[i - 1] < X->p[i - 1] then X < Y is true if and only if both
1247 * X and Y are negative.
Janos Follath0e5532d2019-10-11 14:21:53 +01001248 *
1249 * Again even if we can make a decision, we just mark the result and
1250 * the fact that we are done and continue looping.
Janos Follathee6abce2019-09-05 14:47:19 +01001251 */
Janos Follath30702422019-11-05 12:24:52 +00001252 cond = ct_lt_mpi_uint( Y->p[i - 1], X->p[i - 1] );
1253 *ret |= cond & ( 1 - done ) & X_is_negative;
Janos Follathc50e6d52019-10-28 12:37:21 +00001254 done |= cond;
Janos Follathee6abce2019-09-05 14:47:19 +01001255
1256 /*
Janos Follath30702422019-11-05 12:24:52 +00001257 * If X->p[i - 1] < Y->p[i - 1] then X < Y is true if and only if both
1258 * X and Y are positive.
Janos Follath0e5532d2019-10-11 14:21:53 +01001259 *
1260 * Again even if we can make a decision, we just mark the result and
1261 * the fact that we are done and continue looping.
Janos Follathee6abce2019-09-05 14:47:19 +01001262 */
Janos Follath30702422019-11-05 12:24:52 +00001263 cond = ct_lt_mpi_uint( X->p[i - 1], Y->p[i - 1] );
1264 *ret |= cond & ( 1 - done ) & ( 1 - X_is_negative );
Janos Follathc50e6d52019-10-28 12:37:21 +00001265 done |= cond;
Janos Follathee6abce2019-09-05 14:47:19 +01001266 }
1267
1268 return( 0 );
1269}
1270
Paul Bakker5121ce52009-01-03 21:22:43 +00001271/*
1272 * Compare signed values
1273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +00001275{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 mbedtls_mpi Y;
1277 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001278 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001279
1280 *p = ( z < 0 ) ? -z : z;
1281 Y.s = ( z < 0 ) ? -1 : 1;
1282 Y.n = 1;
1283 Y.p = p;
1284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 return( mbedtls_mpi_cmp_mpi( X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001286}
1287
1288/*
1289 * Unsigned addition: X = |A| + |B| (HAC 14.7)
1290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001292{
Janos Follath24eed8d2019-11-22 13:21:35 +00001293 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001294 size_t i, j;
Janos Follath6c922682015-10-30 17:43:11 +01001295 mbedtls_mpi_uint *o, *p, c, tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +00001296 MPI_VALIDATE_RET( X != NULL );
1297 MPI_VALIDATE_RET( A != NULL );
1298 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001299
1300 if( X == B )
1301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 const mbedtls_mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001303 }
1304
1305 if( X != A )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
Paul Bakker9af723c2014-05-01 13:03:14 +02001307
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001308 /*
1309 * X should always be positive as a result of unsigned additions.
1310 */
1311 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001312
Paul Bakker23986e52011-04-24 08:57:21 +00001313 for( j = B->n; j > 0; j-- )
1314 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 break;
1316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001318
1319 o = B->p; p = X->p; c = 0;
1320
Janos Follath6c922682015-10-30 17:43:11 +01001321 /*
1322 * tmp is used because it might happen that p == o
1323 */
Paul Bakker23986e52011-04-24 08:57:21 +00001324 for( i = 0; i < j; i++, o++, p++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001325 {
Janos Follath6c922682015-10-30 17:43:11 +01001326 tmp= *o;
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 *p += c; c = ( *p < c );
Janos Follath6c922682015-10-30 17:43:11 +01001328 *p += tmp; c += ( *p < tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +00001329 }
1330
1331 while( c != 0 )
1332 {
1333 if( i >= X->n )
1334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001336 p = X->p + i;
1337 }
1338
Paul Bakker2d319fd2012-09-16 21:34:26 +00001339 *p += c; c = ( *p < c ); i++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 }
1341
1342cleanup:
1343
1344 return( ret );
1345}
1346
Gilles Peskine09ec10a2020-06-09 10:39:38 +02001347/**
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001348 * Helper for mbedtls_mpi subtraction.
1349 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001350 * Calculate l - r where l and r have the same size.
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001351 * This function operates modulo (2^ciL)^n and returns the carry
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001352 * (1 if there was a wraparound, i.e. if `l < r`, and 0 otherwise).
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001353 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001354 * d may be aliased to l or r.
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001355 *
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001356 * \param n Number of limbs of \p d, \p l and \p r.
1357 * \param[out] d The result of the subtraction.
1358 * \param[in] l The left operand.
1359 * \param[in] r The right operand.
1360 *
1361 * \return 1 if `l < r`.
1362 * 0 if `l >= r`.
Paul Bakker5121ce52009-01-03 21:22:43 +00001363 */
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001364static mbedtls_mpi_uint mpi_sub_hlp( size_t n,
1365 mbedtls_mpi_uint *d,
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001366 const mbedtls_mpi_uint *l,
1367 const mbedtls_mpi_uint *r )
Paul Bakker5121ce52009-01-03 21:22:43 +00001368{
Paul Bakker23986e52011-04-24 08:57:21 +00001369 size_t i;
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001370 mbedtls_mpi_uint c = 0, t, z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001371
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001372 for( i = 0; i < n; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001373 {
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001374 z = ( l[i] < c ); t = l[i] - c;
1375 c = ( t < r[i] ) + z; d[i] = t - r[i];
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 }
1377
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001378 return( c );
Paul Bakker5121ce52009-01-03 21:22:43 +00001379}
1380
1381/*
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001382 * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
Paul Bakker5121ce52009-01-03 21:22:43 +00001383 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001385{
Janos Follath24eed8d2019-11-22 13:21:35 +00001386 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001387 size_t n;
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001388 mbedtls_mpi_uint carry;
Hanno Becker73d7d792018-12-11 10:35:51 +00001389 MPI_VALIDATE_RET( X != NULL );
1390 MPI_VALIDATE_RET( A != NULL );
1391 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001392
Paul Bakker23986e52011-04-24 08:57:21 +00001393 for( n = B->n; n > 0; n-- )
1394 if( B->p[n - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001395 break;
Gilles Peskinec8a91772021-01-27 22:30:43 +01001396 if( n > A->n )
1397 {
1398 /* B >= (2^ciL)^n > A */
1399 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1400 goto cleanup;
1401 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001402
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001403 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, A->n ) );
1404
1405 /* Set the high limbs of X to match A. Don't touch the lower limbs
1406 * because X might be aliased to B, and we must not overwrite the
1407 * significant digits of B. */
1408 if( A->n > n )
1409 memcpy( X->p + n, A->p + n, ( A->n - n ) * ciL );
1410 if( X->n > A->n )
1411 memset( X->p + A->n, 0, ( X->n - A->n ) * ciL );
1412
1413 carry = mpi_sub_hlp( n, X->p, A->p, B->p );
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001414 if( carry != 0 )
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001415 {
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001416 /* Propagate the carry to the first nonzero limb of X. */
1417 for( ; n < X->n && X->p[n] == 0; n++ )
1418 --X->p[n];
1419 /* If we ran out of space for the carry, it means that the result
1420 * is negative. */
1421 if( n == X->n )
Gilles Peskine89b41302020-07-23 01:16:46 +02001422 {
1423 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1424 goto cleanup;
1425 }
Gilles Peskine0e5faf62020-06-08 22:50:35 +02001426 --X->p[n];
Gilles Peskinec097e9e2020-06-08 21:58:22 +02001427 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001428
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02001429 /* X should always be positive as a result of unsigned subtractions. */
1430 X->s = 1;
1431
Paul Bakker5121ce52009-01-03 21:22:43 +00001432cleanup:
Paul Bakker5121ce52009-01-03 21:22:43 +00001433 return( ret );
1434}
1435
1436/*
1437 * Signed addition: X = A + B
1438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001440{
Hanno Becker73d7d792018-12-11 10:35:51 +00001441 int ret, s;
1442 MPI_VALIDATE_RET( X != NULL );
1443 MPI_VALIDATE_RET( A != NULL );
1444 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001445
Hanno Becker73d7d792018-12-11 10:35:51 +00001446 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 if( A->s * B->s < 0 )
1448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001452 X->s = s;
1453 }
1454 else
1455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001457 X->s = -s;
1458 }
1459 }
1460 else
1461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001463 X->s = s;
1464 }
1465
1466cleanup:
1467
1468 return( ret );
1469}
1470
1471/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001472 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001475{
Hanno Becker73d7d792018-12-11 10:35:51 +00001476 int ret, s;
1477 MPI_VALIDATE_RET( X != NULL );
1478 MPI_VALIDATE_RET( A != NULL );
1479 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001480
Hanno Becker73d7d792018-12-11 10:35:51 +00001481 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001482 if( A->s * B->s > 0 )
1483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001487 X->s = s;
1488 }
1489 else
1490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 X->s = -s;
1493 }
1494 }
1495 else
1496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001498 X->s = s;
1499 }
1500
1501cleanup:
1502
1503 return( ret );
1504}
1505
1506/*
1507 * Signed addition: X = A + b
1508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001510{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511 mbedtls_mpi _B;
1512 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001513 MPI_VALIDATE_RET( X != NULL );
1514 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001515
1516 p[0] = ( b < 0 ) ? -b : b;
1517 _B.s = ( b < 0 ) ? -1 : 1;
1518 _B.n = 1;
1519 _B.p = p;
1520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 return( mbedtls_mpi_add_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001522}
1523
1524/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001525 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001528{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 mbedtls_mpi _B;
1530 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001531 MPI_VALIDATE_RET( X != NULL );
1532 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001533
1534 p[0] = ( b < 0 ) ? -b : b;
1535 _B.s = ( b < 0 ) ? -1 : 1;
1536 _B.n = 1;
1537 _B.p = p;
1538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 return( mbedtls_mpi_sub_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001540}
1541
Gilles Peskinea5d8d892020-07-23 21:27:15 +02001542/** Helper for mbedtls_mpi multiplication.
1543 *
1544 * Add \p b * \p s to \p d.
1545 *
1546 * \param i The number of limbs of \p s.
1547 * \param[in] s A bignum to multiply, of size \p i.
1548 * It may overlap with \p d, but only if
1549 * \p d <= \p s.
1550 * Its leading limb must not be \c 0.
1551 * \param[in,out] d The bignum to add to.
1552 * It must be sufficiently large to store the
1553 * result of the multiplication. This means
1554 * \p i + 1 limbs if \p d[\p i - 1] started as 0 and \p b
1555 * is not known a priori.
1556 * \param b A scalar to multiply.
Paul Bakkerfc4f46f2013-06-24 19:23:56 +02001557 */
1558static
1559#if defined(__APPLE__) && defined(__arm__)
1560/*
1561 * Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
1562 * appears to need this to prevent bad ARM code generation at -O3.
1563 */
1564__attribute__ ((noinline))
1565#endif
Gilles Peskinea5d8d892020-07-23 21:27:15 +02001566void mpi_mul_hlp( size_t i,
1567 const mbedtls_mpi_uint *s,
1568 mbedtls_mpi_uint *d,
1569 mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001570{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 mbedtls_mpi_uint c = 0, t = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001572
1573#if defined(MULADDC_HUIT)
1574 for( ; i >= 8; i -= 8 )
1575 {
1576 MULADDC_INIT
1577 MULADDC_HUIT
1578 MULADDC_STOP
1579 }
1580
1581 for( ; i > 0; i-- )
1582 {
1583 MULADDC_INIT
1584 MULADDC_CORE
1585 MULADDC_STOP
1586 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001587#else /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001588 for( ; i >= 16; i -= 16 )
1589 {
1590 MULADDC_INIT
1591 MULADDC_CORE MULADDC_CORE
1592 MULADDC_CORE MULADDC_CORE
1593 MULADDC_CORE MULADDC_CORE
1594 MULADDC_CORE MULADDC_CORE
1595
1596 MULADDC_CORE MULADDC_CORE
1597 MULADDC_CORE MULADDC_CORE
1598 MULADDC_CORE MULADDC_CORE
1599 MULADDC_CORE MULADDC_CORE
1600 MULADDC_STOP
1601 }
1602
1603 for( ; i >= 8; i -= 8 )
1604 {
1605 MULADDC_INIT
1606 MULADDC_CORE MULADDC_CORE
1607 MULADDC_CORE MULADDC_CORE
1608
1609 MULADDC_CORE MULADDC_CORE
1610 MULADDC_CORE MULADDC_CORE
1611 MULADDC_STOP
1612 }
1613
1614 for( ; i > 0; i-- )
1615 {
1616 MULADDC_INIT
1617 MULADDC_CORE
1618 MULADDC_STOP
1619 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001620#endif /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001621
1622 t++;
1623
Gilles Peskine8e464c42020-07-24 00:08:38 +02001624 while( c != 0 )
1625 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001626 *d += c; c = ( *d < c ); d++;
1627 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001628}
1629
1630/*
1631 * Baseline multiplication: X = A * B (HAC 14.12)
1632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001634{
Janos Follath24eed8d2019-11-22 13:21:35 +00001635 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001636 size_t i, j;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 mbedtls_mpi TA, TB;
Hanno Becker73d7d792018-12-11 10:35:51 +00001638 MPI_VALIDATE_RET( X != NULL );
1639 MPI_VALIDATE_RET( A != NULL );
1640 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644 if( X == A ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); A = &TA; }
1645 if( X == B ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); B = &TB; }
Paul Bakker5121ce52009-01-03 21:22:43 +00001646
Paul Bakker23986e52011-04-24 08:57:21 +00001647 for( i = A->n; i > 0; i-- )
1648 if( A->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001649 break;
1650
Paul Bakker23986e52011-04-24 08:57:21 +00001651 for( j = B->n; j > 0; j-- )
1652 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001653 break;
1654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + j ) );
1656 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
Alexey Skalozub8e75e682016-01-13 21:59:27 +02001658 for( ; j > 0; j-- )
1659 mpi_mul_hlp( i, A->p, X->p + j - 1, B->p[j - 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00001660
1661 X->s = A->s * B->s;
1662
1663cleanup:
1664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001666
1667 return( ret );
1668}
1669
1670/*
1671 * Baseline multiplication: X = A * b
1672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001674{
Hanno Becker73d7d792018-12-11 10:35:51 +00001675 MPI_VALIDATE_RET( X != NULL );
1676 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001678 /* mpi_mul_hlp can't deal with a leading 0. */
1679 size_t n = A->n;
1680 while( n > 0 && A->p[n - 1] == 0 )
1681 --n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001682
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001683 /* The general method below doesn't work if n==0 or b==0. By chance
1684 * calculating the result is trivial in those cases. */
1685 if( b == 0 || n == 0 )
1686 {
Paul Elliott986b55a2021-04-20 21:46:29 +01001687 return( mbedtls_mpi_lset( X, 0 ) );
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001688 }
1689
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001690 /* Calculate A*b as A + A*(b-1) to take advantage of mpi_mul_hlp */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001691 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001692 /* In general, A * b requires 1 limb more than b. If
1693 * A->p[n - 1] * b / b == A->p[n - 1], then A * b fits in the same
1694 * number of limbs as A and the call to grow() is not required since
Gilles Peskinee1bba7c2021-03-10 23:44:10 +01001695 * copy() will take care of the growth if needed. However, experimentally,
1696 * making the call to grow() unconditional causes slightly fewer
Gilles Peskinecd0dbf32020-07-24 00:09:04 +02001697 * calls to calloc() in ECP code, presumably because it reuses the
1698 * same mpi for a while and this way the mpi is more likely to directly
1699 * grow to its final size. */
Gilles Peskine8fd95c62020-07-23 21:58:50 +02001700 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n + 1 ) );
1701 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
1702 mpi_mul_hlp( n, A->p, X->p, b - 1 );
1703
1704cleanup:
1705 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001706}
1707
1708/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001709 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1710 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001711 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001712static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1,
1713 mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r )
Simon Butcher15b15d12015-11-26 19:35:03 +00001714{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001715#if defined(MBEDTLS_HAVE_UDBL)
1716 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001717#else
Simon Butcher9803d072016-01-03 00:24:34 +00001718 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
1719 const mbedtls_mpi_uint uint_halfword_mask = ( (mbedtls_mpi_uint) 1 << biH ) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001720 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1721 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001722 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001723#endif
1724
Simon Butcher15b15d12015-11-26 19:35:03 +00001725 /*
1726 * Check for overflow
1727 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001728 if( 0 == d || u1 >= d )
Simon Butcher15b15d12015-11-26 19:35:03 +00001729 {
Simon Butcherf5ba0452015-12-27 23:01:55 +00001730 if (r != NULL) *r = ~0;
Simon Butcher15b15d12015-11-26 19:35:03 +00001731
Simon Butcherf5ba0452015-12-27 23:01:55 +00001732 return ( ~0 );
Simon Butcher15b15d12015-11-26 19:35:03 +00001733 }
1734
1735#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001736 dividend = (mbedtls_t_udbl) u1 << biL;
1737 dividend |= (mbedtls_t_udbl) u0;
1738 quotient = dividend / d;
1739 if( quotient > ( (mbedtls_t_udbl) 1 << biL ) - 1 )
1740 quotient = ( (mbedtls_t_udbl) 1 << biL ) - 1;
1741
1742 if( r != NULL )
Simon Butcher9803d072016-01-03 00:24:34 +00001743 *r = (mbedtls_mpi_uint)( dividend - (quotient * d ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001744
1745 return (mbedtls_mpi_uint) quotient;
1746#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001747
1748 /*
1749 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1750 * Vol. 2 - Seminumerical Algorithms, Knuth
1751 */
1752
1753 /*
1754 * Normalize the divisor, d, and dividend, u0, u1
1755 */
1756 s = mbedtls_clz( d );
1757 d = d << s;
1758
1759 u1 = u1 << s;
Simon Butcher9803d072016-01-03 00:24:34 +00001760 u1 |= ( u0 >> ( biL - s ) ) & ( -(mbedtls_mpi_sint)s >> ( biL - 1 ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001761 u0 = u0 << s;
1762
1763 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001764 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001765
1766 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001767 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001768
1769 /*
1770 * Find the first quotient and remainder
1771 */
1772 q1 = u1 / d1;
1773 r0 = u1 - d1 * q1;
1774
1775 while( q1 >= radix || ( q1 * d0 > radix * r0 + u0_msw ) )
1776 {
1777 q1 -= 1;
1778 r0 += d1;
1779
1780 if ( r0 >= radix ) break;
1781 }
1782
Simon Butcherf5ba0452015-12-27 23:01:55 +00001783 rAX = ( u1 * radix ) + ( u0_msw - q1 * d );
Simon Butcher15b15d12015-11-26 19:35:03 +00001784 q0 = rAX / d1;
1785 r0 = rAX - q0 * d1;
1786
1787 while( q0 >= radix || ( q0 * d0 > radix * r0 + u0_lsw ) )
1788 {
1789 q0 -= 1;
1790 r0 += d1;
1791
1792 if ( r0 >= radix ) break;
1793 }
1794
1795 if (r != NULL)
Simon Butcherf5ba0452015-12-27 23:01:55 +00001796 *r = ( rAX * radix + u0_lsw - q0 * d ) >> s;
Simon Butcher15b15d12015-11-26 19:35:03 +00001797
1798 quotient = q1 * radix + q0;
1799
1800 return quotient;
1801#endif
1802}
1803
1804/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001806 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001807int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1808 const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001809{
Janos Follath24eed8d2019-11-22 13:21:35 +00001810 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001811 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812 mbedtls_mpi X, Y, Z, T1, T2;
Alexander Kd19a1932019-11-01 18:20:42 +03001813 mbedtls_mpi_uint TP2[3];
Hanno Becker73d7d792018-12-11 10:35:51 +00001814 MPI_VALIDATE_RET( A != NULL );
1815 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 if( mbedtls_mpi_cmp_int( B, 0 ) == 0 )
1818 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Alexander K35d6d462019-10-31 14:46:45 +03001821 mbedtls_mpi_init( &T1 );
Alexander Kd19a1932019-11-01 18:20:42 +03001822 /*
1823 * Avoid dynamic memory allocations for constant-size T2.
1824 *
1825 * T2 is used for comparison only and the 3 limbs are assigned explicitly,
1826 * so nobody increase the size of the MPI and we're safe to use an on-stack
1827 * buffer.
1828 */
Alexander K35d6d462019-10-31 14:46:45 +03001829 T2.s = 1;
Alexander Kd19a1932019-11-01 18:20:42 +03001830 T2.n = sizeof( TP2 ) / sizeof( *TP2 );
1831 T2.p = TP2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835 if( Q != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_lset( Q, 0 ) );
1836 if( R != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001837 return( 0 );
1838 }
1839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &X, A ) );
1841 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001842 X.s = Y.s = 1;
1843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &Z, A->n + 2 ) );
1845 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Z, 0 ) );
Gilles Peskine2536aa72020-07-24 00:12:59 +02001846 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, A->n + 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001847
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001848 k = mbedtls_mpi_bitlen( &Y ) % biL;
Paul Bakkerf9688572011-05-05 10:00:45 +00001849 if( k < biL - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001850 {
1851 k = biL - 1 - k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &X, k ) );
1853 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, k ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001854 }
1855 else k = 0;
1856
1857 n = X.n - 1;
1858 t = Y.n - 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 while( mbedtls_mpi_cmp_mpi( &X, &Y ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001862 {
1863 Z.p[n - t]++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001865 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001867
1868 for( i = n; i > t ; i-- )
1869 {
1870 if( X.p[i] >= Y.p[t] )
1871 Z.p[i - t - 1] = ~0;
1872 else
1873 {
Simon Butcher15b15d12015-11-26 19:35:03 +00001874 Z.p[i - t - 1] = mbedtls_int_div_int( X.p[i], X.p[i - 1],
1875 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001876 }
1877
Alexander K35d6d462019-10-31 14:46:45 +03001878 T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2];
1879 T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1];
1880 T2.p[2] = X.p[i];
1881
Paul Bakker5121ce52009-01-03 21:22:43 +00001882 Z.p[i - t - 1]++;
1883 do
1884 {
1885 Z.p[i - t - 1]--;
1886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T1, 0 ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001888 T1.p[0] = ( t < 1 ) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001889 T1.p[1] = Y.p[t];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001891 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892 while( mbedtls_mpi_cmp_mpi( &T1, &T2 ) > 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
1895 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1896 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 if( mbedtls_mpi_cmp_int( &X, 0 ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &Y ) );
1901 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1902 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001903 Z.p[i - t - 1]--;
1904 }
1905 }
1906
1907 if( Q != NULL )
1908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( Q, &Z ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 Q->s = A->s * B->s;
1911 }
1912
1913 if( R != NULL )
1914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &X, k ) );
Paul Bakkerf02c5642012-11-13 10:25:21 +00001916 X.s = A->s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, &X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 if( mbedtls_mpi_cmp_int( R, 0 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001920 R->s = 1;
1921 }
1922
1923cleanup:
1924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Alexander K35d6d462019-10-31 14:46:45 +03001926 mbedtls_mpi_free( &T1 );
Alexander Kd19a1932019-11-01 18:20:42 +03001927 mbedtls_platform_zeroize( TP2, sizeof( TP2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001928
1929 return( ret );
1930}
1931
1932/*
1933 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00001934 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001935int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R,
1936 const mbedtls_mpi *A,
1937 mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001938{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939 mbedtls_mpi _B;
1940 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001941 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001942
1943 p[0] = ( b < 0 ) ? -b : b;
1944 _B.s = ( b < 0 ) ? -1 : 1;
1945 _B.n = 1;
1946 _B.p = p;
1947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948 return( mbedtls_mpi_div_mpi( Q, R, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001949}
1950
1951/*
1952 * Modulo: R = A mod B
1953 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001955{
Janos Follath24eed8d2019-11-22 13:21:35 +00001956 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker73d7d792018-12-11 10:35:51 +00001957 MPI_VALIDATE_RET( R != NULL );
1958 MPI_VALIDATE_RET( A != NULL );
1959 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 if( mbedtls_mpi_cmp_int( B, 0 ) < 0 )
1962 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( NULL, R, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 while( mbedtls_mpi_cmp_int( R, 0 ) < 0 )
1967 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 while( mbedtls_mpi_cmp_mpi( R, B ) >= 0 )
1970 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001971
1972cleanup:
1973
1974 return( ret );
1975}
1976
1977/*
1978 * Modulo: r = A mod b
1979 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001981{
Paul Bakker23986e52011-04-24 08:57:21 +00001982 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 mbedtls_mpi_uint x, y, z;
Hanno Becker73d7d792018-12-11 10:35:51 +00001984 MPI_VALIDATE_RET( r != NULL );
1985 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001986
1987 if( b == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001989
1990 if( b < 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001992
1993 /*
1994 * handle trivial cases
1995 */
1996 if( b == 1 )
1997 {
1998 *r = 0;
1999 return( 0 );
2000 }
2001
2002 if( b == 2 )
2003 {
2004 *r = A->p[0] & 1;
2005 return( 0 );
2006 }
2007
2008 /*
2009 * general case
2010 */
Paul Bakker23986e52011-04-24 08:57:21 +00002011 for( i = A->n, y = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00002012 {
Paul Bakker23986e52011-04-24 08:57:21 +00002013 x = A->p[i - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00002014 y = ( y << biH ) | ( x >> biH );
2015 z = y / b;
2016 y -= z * b;
2017
2018 x <<= biH;
2019 y = ( y << biH ) | ( x >> biH );
2020 z = y / b;
2021 y -= z * b;
2022 }
2023
Paul Bakkerce40a6d2009-06-23 19:46:08 +00002024 /*
2025 * If A is negative, then the current y represents a negative value.
2026 * Flipping it to the positive side.
2027 */
2028 if( A->s < 0 && y != 0 )
2029 y = b - y;
2030
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 *r = y;
2032
2033 return( 0 );
2034}
2035
2036/*
2037 * Fast Montgomery initialization (thanks to Tom St Denis)
2038 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039static void mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002040{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 mbedtls_mpi_uint x, m0 = N->p[0];
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01002042 unsigned int i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002043
2044 x = m0;
2045 x += ( ( m0 + 2 ) & 4 ) << 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002046
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01002047 for( i = biL; i >= 8; i /= 2 )
2048 x *= ( 2 - ( m0 * x ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002049
2050 *mm = ~x + 1;
2051}
2052
Gilles Peskine2a82f722020-06-04 15:00:49 +02002053/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
2054 *
2055 * \param[in,out] A One of the numbers to multiply.
Gilles Peskine221626f2020-06-08 22:37:50 +02002056 * It must have at least as many limbs as N
2057 * (A->n >= N->n), and any limbs beyond n are ignored.
Gilles Peskine2a82f722020-06-04 15:00:49 +02002058 * On successful completion, A contains the result of
2059 * the multiplication A * B * R^-1 mod N where
2060 * R = (2^ciL)^n.
2061 * \param[in] B One of the numbers to multiply.
2062 * It must be nonzero and must not have more limbs than N
2063 * (B->n <= N->n).
2064 * \param[in] N The modulo. N must be odd.
2065 * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
2066 * This is -N^-1 mod 2^ciL.
2067 * \param[in,out] T A bignum for temporary storage.
2068 * It must be at least twice the limb size of N plus 2
2069 * (T->n >= 2 * (N->n + 1)).
2070 * Its initial content is unused and
2071 * its final content is indeterminate.
2072 * Note that unlike the usual convention in the library
2073 * for `const mbedtls_mpi*`, the content of T can change.
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002075static void mpi_montmul( mbedtls_mpi *A, const mbedtls_mpi *B, const mbedtls_mpi *N, mbedtls_mpi_uint mm,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076 const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00002077{
Paul Bakker23986e52011-04-24 08:57:21 +00002078 size_t i, n, m;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079 mbedtls_mpi_uint u0, u1, *d;
Paul Bakker5121ce52009-01-03 21:22:43 +00002080
2081 memset( T->p, 0, T->n * ciL );
2082
2083 d = T->p;
2084 n = N->n;
2085 m = ( B->n < n ) ? B->n : n;
2086
2087 for( i = 0; i < n; i++ )
2088 {
2089 /*
2090 * T = (T + u0*B + u1*N) / 2^biL
2091 */
2092 u0 = A->p[i];
2093 u1 = ( d[0] + u0 * B->p[0] ) * mm;
2094
2095 mpi_mul_hlp( m, B->p, d, u0 );
2096 mpi_mul_hlp( n, N->p, d, u1 );
2097
2098 *d++ = u0; d[n + 1] = 0;
2099 }
2100
Gilles Peskine221626f2020-06-08 22:37:50 +02002101 /* At this point, d is either the desired result or the desired result
2102 * plus N. We now potentially subtract N, avoiding leaking whether the
2103 * subtraction is performed through side channels. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002104
Gilles Peskine221626f2020-06-08 22:37:50 +02002105 /* Copy the n least significant limbs of d to A, so that
2106 * A = d if d < N (recall that N has n limbs). */
2107 memcpy( A->p, d, n * ciL );
Gilles Peskine09ec10a2020-06-09 10:39:38 +02002108 /* If d >= N then we want to set A to d - N. To prevent timing attacks,
Gilles Peskine221626f2020-06-08 22:37:50 +02002109 * do the calculation without using conditional tests. */
2110 /* Set d to d0 + (2^biL)^n - N where d0 is the current value of d. */
Gilles Peskine132c0972020-06-04 21:05:24 +02002111 d[n] += 1;
Gilles Peskine1acf7cb2020-07-23 01:03:22 +02002112 d[n] -= mpi_sub_hlp( n, d, d, N->p );
Gilles Peskine221626f2020-06-08 22:37:50 +02002113 /* If d0 < N then d < (2^biL)^n
2114 * so d[n] == 0 and we want to keep A as it is.
2115 * If d0 >= N then d >= (2^biL)^n, and d <= (2^biL)^n + N < 2 * (2^biL)^n
2116 * so d[n] == 1 and we want to set A to the result of the subtraction
2117 * which is d - (2^biL)^n, i.e. the n least significant limbs of d.
2118 * This exactly corresponds to a conditional assignment. */
2119 mpi_safe_cond_assign( n, A->p, d, (unsigned char) d[n] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002120}
2121
2122/*
2123 * Montgomery reduction: A = A * R^-1 mod N
Gilles Peskine2a82f722020-06-04 15:00:49 +02002124 *
2125 * See mpi_montmul() regarding constraints and guarantees on the parameters.
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002127static void mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N,
2128 mbedtls_mpi_uint mm, const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00002129{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 mbedtls_mpi_uint z = 1;
2131 mbedtls_mpi U;
Paul Bakker5121ce52009-01-03 21:22:43 +00002132
Paul Bakker8ddb6452013-02-27 14:56:33 +01002133 U.n = U.s = (int) z;
Paul Bakker5121ce52009-01-03 21:22:43 +00002134 U.p = &z;
2135
Gilles Peskine4e91d472020-06-04 20:55:15 +02002136 mpi_montmul( A, &U, N, mm, T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002137}
2138
2139/*
2140 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
2141 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002142int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
2143 const mbedtls_mpi *E, const mbedtls_mpi *N,
2144 mbedtls_mpi *_RR )
Paul Bakker5121ce52009-01-03 21:22:43 +00002145{
Janos Follath24eed8d2019-11-22 13:21:35 +00002146 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002147 size_t wbits, wsize, one = 1;
2148 size_t i, j, nblimbs;
2149 size_t bufsize, nbits;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 mbedtls_mpi_uint ei, mm, state;
Daniel Otte388f9b22020-08-21 12:34:29 +02002151 mbedtls_mpi RR, T, W[ 1 << MBEDTLS_MPI_WINDOW_SIZE ], Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00002152 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002153
Hanno Becker73d7d792018-12-11 10:35:51 +00002154 MPI_VALIDATE_RET( X != NULL );
2155 MPI_VALIDATE_RET( A != NULL );
2156 MPI_VALIDATE_RET( E != NULL );
2157 MPI_VALIDATE_RET( N != NULL );
2158
Hanno Becker8d1dd1b2017-09-28 11:02:24 +01002159 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 || ( N->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 if( mbedtls_mpi_cmp_int( E, 0 ) < 0 )
2163 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002164
Chris Jones9246d042020-11-25 15:12:39 +00002165 if( mbedtls_mpi_bitlen( E ) > MBEDTLS_MPI_MAX_BITS ||
2166 mbedtls_mpi_bitlen( N ) > MBEDTLS_MPI_MAX_BITS )
2167 return ( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2168
Paul Bakkerf6198c12012-05-16 08:02:29 +00002169 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 * Init temps and window size
2171 */
2172 mpi_montg_init( &mm, N );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &T );
2174 mbedtls_mpi_init( &Apos );
Paul Bakker5121ce52009-01-03 21:22:43 +00002175 memset( W, 0, sizeof( W ) );
2176
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002177 i = mbedtls_mpi_bitlen( E );
Paul Bakker5121ce52009-01-03 21:22:43 +00002178
2179 wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
2180 ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1;
2181
Peter Kolbuse6bcad32018-12-11 14:01:44 -06002182#if( MBEDTLS_MPI_WINDOW_SIZE < 6 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 if( wsize > MBEDTLS_MPI_WINDOW_SIZE )
2184 wsize = MBEDTLS_MPI_WINDOW_SIZE;
Peter Kolbuse6bcad32018-12-11 14:01:44 -06002185#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00002186
Paul Bakker5121ce52009-01-03 21:22:43 +00002187 j = N->n + 1;
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02002188 /* All W[i] and X must have at least N->n limbs for the mpi_montmul()
2189 * and mpi_montred() calls later. Here we ensure that W[1] and X are
2190 * large enough, and later we'll grow other W[i] to the same length.
2191 * They must not be shrunk midway through this function!
2192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
2194 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], j ) );
2195 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T, j * 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002196
2197 /*
Paul Bakker50546922012-05-19 08:40:49 +00002198 * Compensate for negative A (and correct at the end)
2199 */
2200 neg = ( A->s == -1 );
Paul Bakker50546922012-05-19 08:40:49 +00002201 if( neg )
2202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Apos, A ) );
Paul Bakker50546922012-05-19 08:40:49 +00002204 Apos.s = 1;
2205 A = &Apos;
2206 }
2207
2208 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 * If 1st call, pre-compute R^2 mod N
2210 */
2211 if( _RR == NULL || _RR->p == NULL )
2212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &RR, 1 ) );
2214 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &RR, N->n * 2 * biL ) );
2215 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &RR, &RR, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002216
2217 if( _RR != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218 memcpy( _RR, &RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 }
2220 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 memcpy( &RR, _RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002222
2223 /*
2224 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
2225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 if( mbedtls_mpi_cmp_mpi( A, N ) >= 0 )
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02002227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &W[1], A, N ) );
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02002229 /* This should be a no-op because W[1] is already that large before
2230 * mbedtls_mpi_mod_mpi(), but it's necessary to avoid an overflow
2231 * in mpi_montmul() below, so let's make sure. */
2232 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], N->n +1 ) );
2233 }
Paul Bakkerc2024f42014-01-23 20:38:35 +01002234 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[1], A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002236
Gilles Peskine3da1a8f2021-06-08 23:17:42 +02002237 /* Note that this is safe because W[1] always has at least N->n limbs
2238 * (it grew above and was preserved by mbedtls_mpi_copy()). */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002239 mpi_montmul( &W[1], &RR, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002240
2241 /*
2242 * X = R^2 * R^-1 mod N = R mod N
2243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &RR ) );
Gilles Peskine4e91d472020-06-04 20:55:15 +02002245 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002246
2247 if( wsize > 1 )
2248 {
2249 /*
2250 * W[1 << (wsize - 1)] = W[1] ^ (wsize - 1)
2251 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002252 j = one << ( wsize - 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[j], N->n + 1 ) );
2255 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[j], &W[1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002256
2257 for( i = 0; i < wsize - 1; i++ )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002258 mpi_montmul( &W[j], &W[j], N, mm, &T );
Paul Bakker0d7702c2013-10-29 16:18:35 +01002259
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 /*
2261 * W[i] = W[i - 1] * W[1]
2262 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002263 for( i = j + 1; i < ( one << wsize ); i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[i], N->n + 1 ) );
2266 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[i], &W[i - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002267
Gilles Peskine4e91d472020-06-04 20:55:15 +02002268 mpi_montmul( &W[i], &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002269 }
2270 }
2271
2272 nblimbs = E->n;
2273 bufsize = 0;
2274 nbits = 0;
2275 wbits = 0;
2276 state = 0;
2277
2278 while( 1 )
2279 {
2280 if( bufsize == 0 )
2281 {
Paul Bakker0d7702c2013-10-29 16:18:35 +01002282 if( nblimbs == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002283 break;
2284
Paul Bakker0d7702c2013-10-29 16:18:35 +01002285 nblimbs--;
2286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 bufsize = sizeof( mbedtls_mpi_uint ) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00002288 }
2289
2290 bufsize--;
2291
2292 ei = (E->p[nblimbs] >> bufsize) & 1;
2293
2294 /*
2295 * skip leading 0s
2296 */
2297 if( ei == 0 && state == 0 )
2298 continue;
2299
2300 if( ei == 0 && state == 1 )
2301 {
2302 /*
2303 * out of window, square X
2304 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002305 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002306 continue;
2307 }
2308
2309 /*
2310 * add ei to current window
2311 */
2312 state = 2;
2313
2314 nbits++;
Paul Bakker66d5d072014-06-17 16:39:18 +02002315 wbits |= ( ei << ( wsize - nbits ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002316
2317 if( nbits == wsize )
2318 {
2319 /*
2320 * X = X^wsize R^-1 mod N
2321 */
2322 for( i = 0; i < wsize; i++ )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002323 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002324
2325 /*
2326 * X = X * W[wbits] R^-1 mod N
2327 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002328 mpi_montmul( X, &W[wbits], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002329
2330 state--;
2331 nbits = 0;
2332 wbits = 0;
2333 }
2334 }
2335
2336 /*
2337 * process the remaining bits
2338 */
2339 for( i = 0; i < nbits; i++ )
2340 {
Gilles Peskine4e91d472020-06-04 20:55:15 +02002341 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002342
2343 wbits <<= 1;
2344
Paul Bakker66d5d072014-06-17 16:39:18 +02002345 if( ( wbits & ( one << wsize ) ) != 0 )
Gilles Peskine4e91d472020-06-04 20:55:15 +02002346 mpi_montmul( X, &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002347 }
2348
2349 /*
2350 * X = A^E * R * R^-1 mod N = A^E mod N
2351 */
Gilles Peskine4e91d472020-06-04 20:55:15 +02002352 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002353
Hanno Beckera4af1c42017-04-18 09:07:45 +01002354 if( neg && E->n != 0 && ( E->p[0] & 1 ) != 0 )
Paul Bakkerf6198c12012-05-16 08:02:29 +00002355 {
2356 X->s = -1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, N, X ) );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002358 }
2359
Paul Bakker5121ce52009-01-03 21:22:43 +00002360cleanup:
2361
Paul Bakker66d5d072014-06-17 16:39:18 +02002362 for( i = ( one << ( wsize - 1 ) ); i < ( one << wsize ); i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 mbedtls_mpi_free( &W[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 mbedtls_mpi_free( &W[1] ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &Apos );
Paul Bakker6c591fa2011-05-05 11:49:20 +00002366
Paul Bakker75a28602014-03-31 12:08:17 +02002367 if( _RR == NULL || _RR->p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002369
2370 return( ret );
2371}
2372
Paul Bakker5121ce52009-01-03 21:22:43 +00002373/*
2374 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
2375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00002377{
Janos Follath24eed8d2019-11-22 13:21:35 +00002378 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002379 size_t lz, lzt;
Alexander Ke8ad49f2019-08-16 16:16:07 +03002380 mbedtls_mpi TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00002381
Hanno Becker73d7d792018-12-11 10:35:51 +00002382 MPI_VALIDATE_RET( G != NULL );
2383 MPI_VALIDATE_RET( A != NULL );
2384 MPI_VALIDATE_RET( B != NULL );
2385
Alexander Ke8ad49f2019-08-16 16:16:07 +03002386 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) );
2389 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391 lz = mbedtls_mpi_lsb( &TA );
2392 lzt = mbedtls_mpi_lsb( &TB );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002393
Paul Bakker66d5d072014-06-17 16:39:18 +02002394 if( lzt < lz )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002395 lz = lzt;
2396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, lz ) );
2398 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, lz ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002399
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 TA.s = TB.s = 1;
2401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402 while( mbedtls_mpi_cmp_int( &TA, 0 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, mbedtls_mpi_lsb( &TA ) ) );
2405 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, mbedtls_mpi_lsb( &TB ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407 if( mbedtls_mpi_cmp_mpi( &TA, &TB ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TA, &TA, &TB ) );
2410 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002411 }
2412 else
2413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TB, &TB, &TA ) );
2415 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002416 }
2417 }
2418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &TB, lz ) );
2420 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( G, &TB ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002421
2422cleanup:
2423
Alexander Ke8ad49f2019-08-16 16:16:07 +03002424 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002425
2426 return( ret );
2427}
2428
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002429/* Fill X with n_bytes random bytes.
2430 * X must already have room for those bytes.
Gilles Peskineafb2bd22021-06-03 11:51:09 +02002431 * The ordering of the bytes returned from the RNG is suitable for
2432 * deterministic ECDSA (see RFC 6979 §3.3 and mbedtls_mpi_random()).
Gilles Peskineebe9b6a2021-04-13 21:55:35 +02002433 * The size and sign of X are unchanged.
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002434 * n_bytes must not be 0.
2435 */
2436static int mpi_fill_random_internal(
2437 mbedtls_mpi *X, size_t n_bytes,
2438 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
2439{
2440 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2441 const size_t limbs = CHARS_TO_LIMBS( n_bytes );
2442 const size_t overhead = ( limbs * ciL ) - n_bytes;
2443
2444 if( X->n < limbs )
2445 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002446
Gilles Peskineebe9b6a2021-04-13 21:55:35 +02002447 memset( X->p, 0, overhead );
2448 memset( (unsigned char *) X->p + limbs * ciL, 0, ( X->n - limbs ) * ciL );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002449 MBEDTLS_MPI_CHK( f_rng( p_rng, (unsigned char *) X->p + overhead, n_bytes ) );
2450 mpi_bigendian_to_host( X->p, limbs );
2451
2452cleanup:
2453 return( ret );
2454}
2455
Paul Bakker33dc46b2014-04-30 16:11:39 +02002456/*
2457 * Fill X with size bytes of random.
2458 *
2459 * Use a temporary bytes representation to make sure the result is the same
Paul Bakkerc37b0ac2014-05-01 14:19:23 +02002460 * regardless of the platform endianness (useful when f_rng is actually
Paul Bakker33dc46b2014-04-30 16:11:39 +02002461 * deterministic, eg for tests).
2462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002464 int (*f_rng)(void *, unsigned char *, size_t),
2465 void *p_rng )
Paul Bakker287781a2011-03-26 13:18:49 +00002466{
Janos Follath24eed8d2019-11-22 13:21:35 +00002467 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6dab6202019-01-02 16:42:29 +00002468 size_t const limbs = CHARS_TO_LIMBS( size );
Hanno Beckerda1655a2017-10-18 14:21:44 +01002469
Hanno Becker8ce11a32018-12-19 16:18:52 +00002470 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002471 MPI_VALIDATE_RET( f_rng != NULL );
Paul Bakker33dc46b2014-04-30 16:11:39 +02002472
Hanno Beckerda1655a2017-10-18 14:21:44 +01002473 /* Ensure that target MPI has exactly the necessary number of limbs */
Gilles Peskineed32b572021-06-02 22:17:52 +02002474 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, limbs ) );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002475 if( size == 0 )
2476 return( 0 );
Paul Bakker287781a2011-03-26 13:18:49 +00002477
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002478 ret = mpi_fill_random_internal( X, size, f_rng, p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +00002479
2480cleanup:
2481 return( ret );
2482}
2483
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002484int mbedtls_mpi_random( mbedtls_mpi *X,
2485 mbedtls_mpi_sint min,
2486 const mbedtls_mpi *N,
2487 int (*f_rng)(void *, unsigned char *, size_t),
2488 void *p_rng )
2489{
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002490 int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskinee5381682021-04-13 21:23:25 +02002491 int count;
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002492 unsigned cmp = 0;
2493 size_t n_bits = mbedtls_mpi_bitlen( N );
2494 size_t n_bytes = ( n_bits + 7 ) / 8;
2495
Gilles Peskine1e918f42021-03-29 22:14:51 +02002496 if( min < 0 )
2497 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2498 if( mbedtls_mpi_cmp_int( N, min ) <= 0 )
2499 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2500
Gilles Peskinee5381682021-04-13 21:23:25 +02002501 /*
2502 * When min == 0, each try has at worst a probability 1/2 of failing
2503 * (the msb has a probability 1/2 of being 0, and then the result will
2504 * be < N), so after 30 tries failure probability is a most 2**(-30).
2505 *
2506 * When N is just below a power of 2, as is the case when generating
Gilles Peskinee842e582021-04-15 11:45:19 +02002507 * a random scalar on most elliptic curves, 1 try is enough with
Gilles Peskinee5381682021-04-13 21:23:25 +02002508 * overwhelming probability. When N is just above a power of 2,
Gilles Peskinee842e582021-04-15 11:45:19 +02002509 * as when generating a random scalar on secp224k1, each try has
Gilles Peskinee5381682021-04-13 21:23:25 +02002510 * a probability of failing that is almost 1/2.
2511 *
2512 * The probabilities are almost the same if min is nonzero but negligible
2513 * compared to N. This is always the case when N is crypto-sized, but
2514 * it's convenient to support small N for testing purposes. When N
2515 * is small, use a higher repeat count, otherwise the probability of
2516 * failure is macroscopic.
2517 */
Gilles Peskine87823d72021-06-02 21:18:59 +02002518 count = ( n_bytes > 4 ? 30 : 250 );
Gilles Peskinee5381682021-04-13 21:23:25 +02002519
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002520 /* Ensure that target MPI has exactly the same number of limbs
2521 * as the upper bound, even if the upper bound has leading zeros.
2522 * This is necessary for the mbedtls_mpi_lt_mpi_ct() check. */
Gilles Peskineed32b572021-06-02 22:17:52 +02002523 MBEDTLS_MPI_CHK( mbedtls_mpi_resize_clear( X, N->n ) );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002524
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002525 /*
2526 * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA)
2527 * when f_rng is a suitably parametrized instance of HMAC_DRBG:
2528 * - use the same byte ordering;
2529 * - keep the leftmost n_bits bits of the generated octet string;
2530 * - try until result is in the desired range.
2531 * This also avoids any bias, which is especially important for ECDSA.
2532 */
2533 do
2534 {
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02002535 MBEDTLS_MPI_CHK( mpi_fill_random_internal( X, n_bytes, f_rng, p_rng ) );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002536 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, 8 * n_bytes - n_bits ) );
2537
Gilles Peskinee5381682021-04-13 21:23:25 +02002538 if( --count == 0 )
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002539 {
2540 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2541 goto cleanup;
2542 }
2543
Gilles Peskine405b0912021-06-03 11:38:26 +02002544 MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( X, N, &cmp ) );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02002545 }
2546 while( mbedtls_mpi_cmp_int( X, min ) < 0 || cmp != 1 );
2547
2548cleanup:
2549 return( ret );
2550}
2551
Paul Bakker5121ce52009-01-03 21:22:43 +00002552/*
2553 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2554 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002556{
Janos Follath24eed8d2019-11-22 13:21:35 +00002557 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Hanno Becker73d7d792018-12-11 10:35:51 +00002559 MPI_VALIDATE_RET( X != NULL );
2560 MPI_VALIDATE_RET( A != NULL );
2561 MPI_VALIDATE_RET( N != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002562
Hanno Becker4bcb4912017-04-18 15:49:39 +01002563 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TU ); mbedtls_mpi_init( &U1 ); mbedtls_mpi_init( &U2 );
2567 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &TB ); mbedtls_mpi_init( &TV );
2568 mbedtls_mpi_init( &V1 ); mbedtls_mpi_init( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, A, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 goto cleanup;
2576 }
2577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &TA, A, N ) );
2579 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TU, &TA ) );
2580 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, N ) );
2581 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TV, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U1, 1 ) );
2584 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U2, 0 ) );
2585 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V1, 0 ) );
2586 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002587
2588 do
2589 {
2590 while( ( TU.p[0] & 1 ) == 0 )
2591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TU, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002593
2594 if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
2595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &U1, &U1, &TB ) );
2597 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 }
2599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U1, 1 ) );
2601 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002602 }
2603
2604 while( ( TV.p[0] & 1 ) == 0 )
2605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TV, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002607
2608 if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
2609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, &TB ) );
2611 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 }
2613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V1, 1 ) );
2615 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002616 }
2617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002618 if( mbedtls_mpi_cmp_mpi( &TU, &TV ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TU, &TU, &TV ) );
2621 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U1, &U1, &V1 ) );
2622 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &V2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002623 }
2624 else
2625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TV, &TV, &TU ) );
2627 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, &U1 ) );
2628 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &U2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002629 }
2630 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 while( mbedtls_mpi_cmp_int( &TU, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 while( mbedtls_mpi_cmp_int( &V1, 0 ) < 0 )
2634 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 while( mbedtls_mpi_cmp_mpi( &V1, N ) >= 0 )
2637 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &V1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002640
2641cleanup:
2642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TU ); mbedtls_mpi_free( &U1 ); mbedtls_mpi_free( &U2 );
2644 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TV );
2645 mbedtls_mpi_free( &V1 ); mbedtls_mpi_free( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002646
2647 return( ret );
2648}
2649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002651
Paul Bakker5121ce52009-01-03 21:22:43 +00002652static const int small_prime[] =
2653{
2654 3, 5, 7, 11, 13, 17, 19, 23,
2655 29, 31, 37, 41, 43, 47, 53, 59,
2656 61, 67, 71, 73, 79, 83, 89, 97,
2657 101, 103, 107, 109, 113, 127, 131, 137,
2658 139, 149, 151, 157, 163, 167, 173, 179,
2659 181, 191, 193, 197, 199, 211, 223, 227,
2660 229, 233, 239, 241, 251, 257, 263, 269,
2661 271, 277, 281, 283, 293, 307, 311, 313,
2662 317, 331, 337, 347, 349, 353, 359, 367,
2663 373, 379, 383, 389, 397, 401, 409, 419,
2664 421, 431, 433, 439, 443, 449, 457, 461,
2665 463, 467, 479, 487, 491, 499, 503, 509,
2666 521, 523, 541, 547, 557, 563, 569, 571,
2667 577, 587, 593, 599, 601, 607, 613, 617,
2668 619, 631, 641, 643, 647, 653, 659, 661,
2669 673, 677, 683, 691, 701, 709, 719, 727,
2670 733, 739, 743, 751, 757, 761, 769, 773,
2671 787, 797, 809, 811, 821, 823, 827, 829,
2672 839, 853, 857, 859, 863, 877, 881, 883,
2673 887, 907, 911, 919, 929, 937, 941, 947,
2674 953, 967, 971, 977, 983, 991, 997, -103
2675};
2676
2677/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002678 * Small divisors test (X must be positive)
2679 *
2680 * Return values:
2681 * 0: no small factor (possible prime, more tests needed)
2682 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002684 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686static int mpi_check_small_factors( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +00002687{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002688 int ret = 0;
2689 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690 mbedtls_mpi_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +00002691
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 if( ( X->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002694
2695 for( i = 0; small_prime[i] > 0; i++ )
2696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 if( mbedtls_mpi_cmp_int( X, small_prime[i] ) <= 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002698 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, small_prime[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002701
2702 if( r == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 }
2705
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002706cleanup:
2707 return( ret );
2708}
2709
2710/*
2711 * Miller-Rabin pseudo-primality test (HAC 4.24)
2712 */
Janos Follathda31fa12018-09-03 14:45:23 +01002713static int mpi_miller_rabin( const mbedtls_mpi *X, size_t rounds,
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002714 int (*f_rng)(void *, unsigned char *, size_t),
2715 void *p_rng )
2716{
Pascal Junodb99183d2015-03-11 16:49:45 +01002717 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002718 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002720
Hanno Becker8ce11a32018-12-19 16:18:52 +00002721 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002722 MPI_VALIDATE_RET( f_rng != NULL );
2723
2724 mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R );
2725 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 mbedtls_mpi_init( &RR );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002727
Paul Bakker5121ce52009-01-03 21:22:43 +00002728 /*
2729 * W = |X| - 1
2730 * R = W >> lsb( W )
2731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &W, X, 1 ) );
2733 s = mbedtls_mpi_lsb( &W );
2734 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R, &W ) );
2735 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &R, s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002736
Janos Follathda31fa12018-09-03 14:45:23 +01002737 for( i = 0; i < rounds; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002738 {
2739 /*
2740 * pick a random A, 1 < A < |X| - 1
2741 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002742 count = 0;
2743 do {
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002744 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
Pascal Junodb99183d2015-03-11 16:49:45 +01002745
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002746 j = mbedtls_mpi_bitlen( &A );
2747 k = mbedtls_mpi_bitlen( &W );
Pascal Junodb99183d2015-03-11 16:49:45 +01002748 if (j > k) {
Darryl Greene3f95ed2018-10-02 13:21:35 +01002749 A.p[A.n - 1] &= ( (mbedtls_mpi_uint) 1 << ( k - ( A.n - 1 ) * biL - 1 ) ) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002750 }
2751
2752 if (count++ > 30) {
Jens Wiklanderf08aa3e2019-01-17 13:30:57 +01002753 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2754 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002755 }
2756
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002757 } while ( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 ||
2758 mbedtls_mpi_cmp_int( &A, 1 ) <= 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002759
2760 /*
2761 * A = A^R mod |X|
2762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &A, &A, &R, X, &RR ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765 if( mbedtls_mpi_cmp_mpi( &A, &W ) == 0 ||
2766 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002767 continue;
2768
2769 j = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 while( j < s && mbedtls_mpi_cmp_mpi( &A, &W ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002771 {
2772 /*
2773 * A = A * A mod |X|
2774 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &A, &A ) );
2776 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &A, &T, X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778 if( mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002779 break;
2780
2781 j++;
2782 }
2783
2784 /*
2785 * not prime if A != |X| - 1 or A == 1
2786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 if( mbedtls_mpi_cmp_mpi( &A, &W ) != 0 ||
2788 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002791 break;
2792 }
2793 }
2794
2795cleanup:
Hanno Becker73d7d792018-12-11 10:35:51 +00002796 mbedtls_mpi_free( &W ); mbedtls_mpi_free( &R );
2797 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002799
2800 return( ret );
2801}
2802
2803/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002804 * Pseudo-primality test: small factors, then Miller-Rabin
2805 */
Janos Follatha0b67c22018-09-18 14:48:23 +01002806int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds,
2807 int (*f_rng)(void *, unsigned char *, size_t),
2808 void *p_rng )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002809{
Janos Follath24eed8d2019-11-22 13:21:35 +00002810 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 mbedtls_mpi XX;
Hanno Becker8ce11a32018-12-19 16:18:52 +00002812 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002813 MPI_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002814
2815 XX.s = 1;
2816 XX.n = X->n;
2817 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819 if( mbedtls_mpi_cmp_int( &XX, 0 ) == 0 ||
2820 mbedtls_mpi_cmp_int( &XX, 1 ) == 0 )
2821 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 if( mbedtls_mpi_cmp_int( &XX, 2 ) == 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002824 return( 0 );
2825
2826 if( ( ret = mpi_check_small_factors( &XX ) ) != 0 )
2827 {
2828 if( ret == 1 )
2829 return( 0 );
2830
2831 return( ret );
2832 }
2833
Janos Follathda31fa12018-09-03 14:45:23 +01002834 return( mpi_miller_rabin( &XX, rounds, f_rng, p_rng ) );
Janos Follathf301d232018-08-14 13:34:01 +01002835}
2836
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002837/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002838 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002839 *
Janos Follathf301d232018-08-14 13:34:01 +01002840 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2841 * be either 1024 bits or 1536 bits long, and flags must contain
2842 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002843 */
Janos Follath7c025a92018-08-14 11:08:41 +01002844int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002845 int (*f_rng)(void *, unsigned char *, size_t),
2846 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002847{
Jethro Beekman66689272018-02-14 19:24:10 -08002848#ifdef MBEDTLS_HAVE_INT64
2849// ceil(2^63.5)
2850#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2851#else
2852// ceil(2^31.5)
2853#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2854#endif
2855 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002856 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002857 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858 mbedtls_mpi_uint r;
2859 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002860
Hanno Becker8ce11a32018-12-19 16:18:52 +00002861 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002862 MPI_VALIDATE_RET( f_rng != NULL );
2863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002864 if( nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS )
2865 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867 mbedtls_mpi_init( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002868
2869 n = BITS_TO_LIMBS( nbits );
2870
Janos Follathda31fa12018-09-03 14:45:23 +01002871 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR ) == 0 )
2872 {
2873 /*
2874 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2875 */
2876 rounds = ( ( nbits >= 1300 ) ? 2 : ( nbits >= 850 ) ? 3 :
2877 ( nbits >= 650 ) ? 4 : ( nbits >= 350 ) ? 8 :
2878 ( nbits >= 250 ) ? 12 : ( nbits >= 150 ) ? 18 : 27 );
2879 }
2880 else
2881 {
2882 /*
2883 * 2^-100 error probability, number of rounds computed based on HAC,
2884 * fact 4.48
2885 */
2886 rounds = ( ( nbits >= 1450 ) ? 4 : ( nbits >= 1150 ) ? 5 :
2887 ( nbits >= 1000 ) ? 6 : ( nbits >= 850 ) ? 7 :
2888 ( nbits >= 750 ) ? 8 : ( nbits >= 500 ) ? 13 :
2889 ( nbits >= 250 ) ? 28 : ( nbits >= 150 ) ? 40 : 51 );
2890 }
2891
Jethro Beekman66689272018-02-14 19:24:10 -08002892 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002893 {
Jethro Beekman66689272018-02-14 19:24:10 -08002894 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
2895 /* make sure generated number is at least (nbits-1)+0.5 bits (FIPS 186-4 §B.3.3 steps 4.4, 5.5) */
2896 if( X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2 ) continue;
2897
2898 k = n * biL;
2899 if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits ) );
2900 X->p[0] |= 1;
2901
Janos Follath7c025a92018-08-14 11:08:41 +01002902 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002903 {
Janos Follatha0b67c22018-09-18 14:48:23 +01002904 ret = mbedtls_mpi_is_prime_ext( X, rounds, f_rng, p_rng );
Jethro Beekman66689272018-02-14 19:24:10 -08002905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002907 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002908 }
Jethro Beekman66689272018-02-14 19:24:10 -08002909 else
Paul Bakker5121ce52009-01-03 21:22:43 +00002910 {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002911 /*
Jethro Beekman66689272018-02-14 19:24:10 -08002912 * An necessary condition for Y and X = 2Y + 1 to be prime
2913 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2914 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002915 */
Jethro Beekman66689272018-02-14 19:24:10 -08002916
2917 X->p[0] |= 2;
2918
2919 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, 3 ) );
2920 if( r == 0 )
2921 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 8 ) );
2922 else if( r == 1 )
2923 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 4 ) );
2924
2925 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
2926 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, X ) );
2927 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, 1 ) );
2928
2929 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002930 {
Jethro Beekman66689272018-02-14 19:24:10 -08002931 /*
2932 * First, check small factors for X and Y
2933 * before doing Miller-Rabin on any of them
2934 */
2935 if( ( ret = mpi_check_small_factors( X ) ) == 0 &&
2936 ( ret = mpi_check_small_factors( &Y ) ) == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002937 ( ret = mpi_miller_rabin( X, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002938 == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002939 ( ret = mpi_miller_rabin( &Y, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002940 == 0 )
Jethro Beekman66689272018-02-14 19:24:10 -08002941 goto cleanup;
2942
2943 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
2944 goto cleanup;
2945
2946 /*
2947 * Next candidates. We want to preserve Y = (X-1) / 2 and
2948 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2949 * so up Y by 6 and X by 12.
2950 */
2951 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 12 ) );
2952 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &Y, &Y, 6 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002953 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002954 }
2955 }
2956
2957cleanup:
2958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959 mbedtls_mpi_free( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002960
2961 return( ret );
2962}
2963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002967
Paul Bakker23986e52011-04-24 08:57:21 +00002968#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002969
2970static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2971{
2972 { 693, 609, 21 },
2973 { 1764, 868, 28 },
2974 { 768454923, 542167814, 1 }
2975};
2976
Paul Bakker5121ce52009-01-03 21:22:43 +00002977/*
2978 * Checkup routine
2979 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980int mbedtls_mpi_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002981{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002982 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00002984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); mbedtls_mpi_init( &X );
2986 mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &U ); mbedtls_mpi_init( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00002987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &A, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002989 "EFE021C2645FD1DC586E69184AF4A31E" \
2990 "D5F53E93B5F123FA41680867BA110131" \
2991 "944FE7952E2517337780CB0DB80E61AA" \
2992 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6" ) );
2993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002995 "B2E7EFD37075B9F03FF989C7C5051C20" \
2996 "34D2A323810251127E7BF8625A4F49A5" \
2997 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
2998 "5B5C25763222FEFCCFC38B832366C29E" ) );
2999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &N, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003001 "0066A198186C18C10B2F5ED9B522752A" \
3002 "9830B69916E535C8F047518A889A43A5" \
3003 "94B6BED27A168D31D4A52F88925AA8F5" ) );
3004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003008 "602AB7ECA597A3D6B56FF9829A5E8B85" \
3009 "9E857EA95A03512E2BAE7391688D264A" \
3010 "A5663B0341DB9CCFD2C4C5F421FEC814" \
3011 "8001B72E848A38CAE1C65F78E56ABDEF" \
3012 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
3013 "ECF677152EF804370C1A305CAF3B5BF1" \
3014 "30879B56C61DE584A0F53A2447A51E" ) );
3015
3016 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017 mbedtls_printf( " MPI test #1 (mul_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003020 {
3021 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003023
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003024 ret = 1;
3025 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003026 }
3027
3028 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003029 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &X, &Y, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003033 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003034 "256567336059E52CAE22925474705F39A94" ) );
3035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &V, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003037 "6613F26162223DF488E9CD48CC132C7A" \
3038 "0AC93C701B001B092E4E5B9F73BCD27B" \
3039 "9EE50D0657C77F374E903CDFA4C642" ) );
3040
3041 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042 mbedtls_printf( " MPI test #2 (div_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003044 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ||
3045 mbedtls_mpi_cmp_mpi( &Y, &V ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003046 {
3047 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003049
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003050 ret = 1;
3051 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003052 }
3053
3054 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &X, &A, &E, &N, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003060 "36E139AEA55215609D2816998ED020BB" \
3061 "BD96C37890F65171D948E9BC7CBAA4D9" \
3062 "325D24D6A3C12710F10A09FA08AB87" ) );
3063
3064 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 mbedtls_printf( " MPI test #3 (exp_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003068 {
3069 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003070 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003071
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003072 ret = 1;
3073 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003074 }
3075
3076 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &X, &A, &N ) );
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( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003082 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
3083 "C3DBA76456363A10869622EAC2DD84EC" \
3084 "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) );
3085
3086 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087 mbedtls_printf( " MPI test #4 (inv_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003090 {
3091 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003093
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003094 ret = 1;
3095 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003096 }
3097
3098 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003100
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003101 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102 mbedtls_printf( " MPI test #5 (simple gcd): " );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003103
Paul Bakker66d5d072014-06-17 16:39:18 +02003104 for( i = 0; i < GCD_PAIR_COUNT; i++ )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &X, gcd_pairs[i][0] ) );
3107 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Y, gcd_pairs[i][1] ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &A, &X, &Y ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111 if( mbedtls_mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 )
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003112 {
3113 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114 mbedtls_printf( "failed at %d\n", i );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003115
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003116 ret = 1;
3117 goto cleanup;
3118 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003119 }
3120
3121 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122 mbedtls_printf( "passed\n" );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003123
Paul Bakker5121ce52009-01-03 21:22:43 +00003124cleanup:
3125
3126 if( ret != 0 && verbose != 0 )
Kenneth Soerensen518d4352020-04-01 17:22:45 +02003127 mbedtls_printf( "Unexpected error, return code = %08X\n", (unsigned int) ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &X );
3130 mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &U ); mbedtls_mpi_free( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00003131
3132 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003134
3135 return( ret );
3136}
3137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140#endif /* MBEDTLS_BIGNUM_C */