blob: aefc51945680697d4079018f6c7a452f3e3094f9 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Multi-precision integer library
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
Simon Butcher15b15d12015-11-26 19:35:03 +000046
Paul Bakker5121ce52009-01-03 21:22:43 +000047/*
Simon Butcher15b15d12015-11-26 19:35:03 +000048 * The following sources were referenced in the design of this Multi-precision
49 * Integer library:
Paul Bakker5121ce52009-01-03 21:22:43 +000050 *
Simon Butcher15b15d12015-11-26 19:35:03 +000051 * [1] Handbook of Applied Cryptography - 1997
52 * Menezes, van Oorschot and Vanstone
53 *
54 * [2] Multi-Precision Math
55 * Tom St Denis
56 * https://github.com/libtom/libtommath/blob/develop/tommath.pdf
57 *
58 * [3] GNU Multi-Precision Arithmetic Library
59 * https://gmplib.org/manual/index.html
60 *
Simon Butcherf5ba0452015-12-27 23:01:55 +000061 */
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020065#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020067#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000071#include "mbedtls/bignum.h"
72#include "mbedtls/bn_mul.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050073#include "mbedtls/platform_util.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Rich Evans00ab4702015-02-06 13:43:58 +000075#include <string.h>
76
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000078#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020079#else
Rich Evans00ab4702015-02-06 13:43:58 +000080#include <stdio.h>
81#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#define mbedtls_printf printf
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020083#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020085#endif
86
Hanno Becker73d7d792018-12-11 10:35:51 +000087#define MPI_VALIDATE_RET( cond ) \
88 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA )
89#define MPI_VALIDATE( cond ) \
90 MBEDTLS_INTERNAL_VALIDATE( cond )
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
Paul Bakker5121ce52009-01-03 21:22:43 +000093#define biL (ciL << 3) /* bits in limb */
94#define biH (ciL << 2) /* half limb size */
95
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +010096#define MPI_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
97
Paul Bakker5121ce52009-01-03 21:22:43 +000098/*
99 * Convert between bits/chars and number of limbs
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +0200100 * Divide first in order to avoid potential overflows
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 */
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +0200102#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
103#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500105/* Implementation that should never be optimized out by the compiler */
Andres Amaya Garcia6698d2f2018-04-24 08:39:07 -0500106static void mbedtls_mpi_zeroize( mbedtls_mpi_uint *v, size_t n )
107{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500108 mbedtls_platform_zeroize( v, ciL * n );
109}
110
Paul Bakker5121ce52009-01-03 21:22:43 +0000111/*
Paul Bakker6c591fa2011-05-05 11:49:20 +0000112 * Initialize one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114void mbedtls_mpi_init( mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000115{
Hanno Becker73d7d792018-12-11 10:35:51 +0000116 MPI_VALIDATE( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
Paul Bakker6c591fa2011-05-05 11:49:20 +0000118 X->s = 1;
119 X->n = 0;
120 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000121}
122
123/*
Paul Bakker6c591fa2011-05-05 11:49:20 +0000124 * Unallocate one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126void mbedtls_mpi_free( mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000127{
Paul Bakker6c591fa2011-05-05 11:49:20 +0000128 if( X == NULL )
129 return;
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
Paul Bakker6c591fa2011-05-05 11:49:20 +0000131 if( X->p != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 {
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200133 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 }
136
Paul Bakker6c591fa2011-05-05 11:49:20 +0000137 X->s = 1;
138 X->n = 0;
139 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000140}
141
142/*
143 * Enlarge to the specified number of limbs
144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000146{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_mpi_uint *p;
Hanno Becker73d7d792018-12-11 10:35:51 +0000148 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200151 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Paul Bakkerf9688572011-05-05 10:00:45 +0000152
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 if( X->n < nblimbs )
154 {
Simon Butcher29176892016-05-20 00:19:09 +0100155 if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( nblimbs, ciL ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200156 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 if( X->p != NULL )
159 {
160 memcpy( p, X->p, X->n * ciL );
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200161 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 }
164
165 X->n = nblimbs;
166 X->p = p;
167 }
168
169 return( 0 );
170}
171
172/*
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100173 * Resize down as much as possible,
174 * while keeping at least the specified number of limbs
175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100177{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_mpi_uint *p;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100179 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000180 MPI_VALIDATE_RET( X != NULL );
181
182 if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
183 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100184
Gilles Peskine27c15c72020-01-20 21:17:43 +0100185 /* Actually resize up if there are currently fewer than nblimbs limbs. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100186 if( X->n <= nblimbs )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 return( mbedtls_mpi_grow( X, nblimbs ) );
Gilles Peskine56427c22020-01-21 13:59:51 +0100188 /* After this point, then X->n > nblimbs and in particular X->n > 0. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100189
190 for( i = X->n - 1; i > 0; i-- )
191 if( X->p[i] != 0 )
192 break;
193 i++;
194
195 if( i < nblimbs )
196 i = nblimbs;
197
Simon Butcher29176892016-05-20 00:19:09 +0100198 if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( i, ciL ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200199 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100200
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100201 if( X->p != NULL )
202 {
203 memcpy( p, X->p, i * ciL );
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200204 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_free( X->p );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100206 }
207
208 X->n = i;
209 X->p = p;
210
211 return( 0 );
212}
213
214/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 * Copy the contents of Y into X
216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000218{
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100219 int ret = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000220 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000221 MPI_VALIDATE_RET( X != NULL );
222 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
224 if( X == Y )
225 return( 0 );
226
Gilles Peskine3e9f5222020-01-20 21:12:50 +0100227 if( Y->n == 0 )
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_mpi_free( X );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200230 return( 0 );
231 }
232
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 for( i = Y->n - 1; i > 0; i-- )
234 if( Y->p[i] != 0 )
235 break;
236 i++;
237
238 X->s = Y->s;
239
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100240 if( X->n < i )
241 {
242 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i ) );
243 }
244 else
245 {
246 memset( X->p + i, 0, ( X->n - i ) * ciL );
247 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000248
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 memcpy( X->p, Y->p, i * ciL );
250
251cleanup:
252
253 return( ret );
254}
255
256/*
257 * Swap the contents of X and Y
258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000260{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000262 MPI_VALIDATE( X != NULL );
263 MPI_VALIDATE( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 memcpy( &T, X, sizeof( mbedtls_mpi ) );
266 memcpy( X, Y, sizeof( mbedtls_mpi ) );
267 memcpy( Y, &T, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000268}
269
270/*
Gilles Peskinec81c5882020-06-04 19:14:58 +0200271 * Conditionally assign dest = src, without leaking information
272 * about whether the assignment was made or not.
273 * dest and src must be arrays of limbs of size n.
274 * assign must be 0 or 1.
275 */
276static void mpi_safe_cond_assign( size_t n,
277 mbedtls_mpi_uint *dest,
278 const mbedtls_mpi_uint *src,
279 unsigned char assign )
280{
281 size_t i;
282 for( i = 0; i < n; i++ )
283 dest[i] = dest[i] * ( 1 - assign ) + src[i] * assign;
284}
285
286/*
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100287 * Conditionally assign X = Y, without leaking information
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +0100288 * about whether the assignment was made or not.
289 * (Leaking information about the respective sizes of X and Y is ok however.)
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100292{
293 int ret = 0;
294 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000295 MPI_VALIDATE_RET( X != NULL );
296 MPI_VALIDATE_RET( Y != NULL );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100297
Pascal Junodb99183d2015-03-11 16:49:45 +0100298 /* make sure assign is 0 or 1 in a time-constant manner */
299 assign = (assign | (unsigned char)-assign) >> 7;
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100302
Paul Bakker66d5d072014-06-17 16:39:18 +0200303 X->s = X->s * ( 1 - assign ) + Y->s * assign;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100304
Gilles Peskinec81c5882020-06-04 19:14:58 +0200305 mpi_safe_cond_assign( Y->n, X->p, Y->p, assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100306
Gilles Peskinec81c5882020-06-04 19:14:58 +0200307 for( i = Y->n; i < X->n; i++ )
Paul Bakker66d5d072014-06-17 16:39:18 +0200308 X->p[i] *= ( 1 - assign );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100309
310cleanup:
311 return( ret );
312}
313
314/*
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100315 * Conditionally swap X and Y, without leaking information
316 * about whether the swap was made or not.
317 * Here it is not ok to simply swap the pointers, which whould lead to
318 * different memory access patterns when X and Y are used afterwards.
319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100321{
322 int ret, s;
323 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 mbedtls_mpi_uint tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +0000325 MPI_VALIDATE_RET( X != NULL );
326 MPI_VALIDATE_RET( Y != NULL );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100327
328 if( X == Y )
329 return( 0 );
330
Pascal Junodb99183d2015-03-11 16:49:45 +0100331 /* make sure swap is 0 or 1 in a time-constant manner */
332 swap = (swap | (unsigned char)-swap) >> 7;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
335 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( Y, X->n ) );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100336
337 s = X->s;
Paul Bakker66d5d072014-06-17 16:39:18 +0200338 X->s = X->s * ( 1 - swap ) + Y->s * swap;
339 Y->s = Y->s * ( 1 - swap ) + s * swap;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100340
341
342 for( i = 0; i < X->n; i++ )
343 {
344 tmp = X->p[i];
Paul Bakker66d5d072014-06-17 16:39:18 +0200345 X->p[i] = X->p[i] * ( 1 - swap ) + Y->p[i] * swap;
346 Y->p[i] = Y->p[i] * ( 1 - swap ) + tmp * swap;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100347 }
348
349cleanup:
350 return( ret );
351}
352
353/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000354 * Set value from integer
355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +0000357{
358 int ret;
Hanno Becker73d7d792018-12-11 10:35:51 +0000359 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 memset( X->p, 0, X->n * ciL );
363
364 X->p[0] = ( z < 0 ) ? -z : z;
365 X->s = ( z < 0 ) ? -1 : 1;
366
367cleanup:
368
369 return( ret );
370}
371
372/*
Paul Bakker2f5947e2011-05-18 15:47:11 +0000373 * Get a specific bit
374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000376{
Hanno Becker73d7d792018-12-11 10:35:51 +0000377 MPI_VALIDATE_RET( X != NULL );
378
Paul Bakker2f5947e2011-05-18 15:47:11 +0000379 if( X->n * biL <= pos )
380 return( 0 );
381
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200382 return( ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000383}
384
Gilles Peskine11cdb052018-11-20 16:47:47 +0100385/* Get a specific byte, without range checks. */
386#define GET_BYTE( X, i ) \
387 ( ( ( X )->p[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff )
388
Paul Bakker2f5947e2011-05-18 15:47:11 +0000389/*
390 * Set a bit to a specific value of 0 or 1
391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000393{
394 int ret = 0;
395 size_t off = pos / biL;
396 size_t idx = pos % biL;
Hanno Becker73d7d792018-12-11 10:35:51 +0000397 MPI_VALIDATE_RET( X != NULL );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000398
399 if( val != 0 && val != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker9af723c2014-05-01 13:03:14 +0200401
Paul Bakker2f5947e2011-05-18 15:47:11 +0000402 if( X->n * biL <= pos )
403 {
404 if( val == 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200405 return( 0 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, off + 1 ) );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000408 }
409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 X->p[off] &= ~( (mbedtls_mpi_uint) 0x01 << idx );
411 X->p[off] |= (mbedtls_mpi_uint) val << idx;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000412
413cleanup:
Paul Bakker9af723c2014-05-01 13:03:14 +0200414
Paul Bakker2f5947e2011-05-18 15:47:11 +0000415 return( ret );
416}
417
418/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200419 * Return the number of less significant zero-bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421size_t mbedtls_mpi_lsb( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000422{
Paul Bakker23986e52011-04-24 08:57:21 +0000423 size_t i, j, count = 0;
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000424 MBEDTLS_INTERNAL_VALIDATE_RET( X != NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
426 for( i = 0; i < X->n; i++ )
Paul Bakkerf9688572011-05-05 10:00:45 +0000427 for( j = 0; j < biL; j++, count++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 if( ( ( X->p[i] >> j ) & 1 ) != 0 )
429 return( count );
430
431 return( 0 );
432}
433
434/*
Simon Butcher15b15d12015-11-26 19:35:03 +0000435 * Count leading zero bits in a given integer
436 */
437static size_t mbedtls_clz( const mbedtls_mpi_uint x )
438{
439 size_t j;
Manuel Pégourié-Gonnarde3e8edf2015-12-01 09:31:52 +0100440 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
Simon Butcher15b15d12015-11-26 19:35:03 +0000441
442 for( j = 0; j < biL; j++ )
443 {
444 if( x & mask ) break;
445
446 mask >>= 1;
447 }
448
449 return j;
450}
451
452/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200453 * Return the number of bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200455size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000456{
Paul Bakker23986e52011-04-24 08:57:21 +0000457 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200459 if( X->n == 0 )
460 return( 0 );
461
Paul Bakker5121ce52009-01-03 21:22:43 +0000462 for( i = X->n - 1; i > 0; i-- )
463 if( X->p[i] != 0 )
464 break;
465
Simon Butcher15b15d12015-11-26 19:35:03 +0000466 j = biL - mbedtls_clz( X->p[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
Paul Bakker23986e52011-04-24 08:57:21 +0000468 return( ( i * biL ) + j );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469}
470
471/*
472 * Return the total size in bytes
473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474size_t mbedtls_mpi_size( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000475{
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200476 return( ( mbedtls_mpi_bitlen( X ) + 7 ) >> 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000477}
478
479/*
480 * Convert an ASCII character to digit value
481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482static int mpi_get_digit( mbedtls_mpi_uint *d, int radix, char c )
Paul Bakker5121ce52009-01-03 21:22:43 +0000483{
484 *d = 255;
485
486 if( c >= 0x30 && c <= 0x39 ) *d = c - 0x30;
487 if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37;
488 if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57;
489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 if( *d >= (mbedtls_mpi_uint) radix )
491 return( MBEDTLS_ERR_MPI_INVALID_CHARACTER );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492
493 return( 0 );
494}
495
496/*
497 * Import from an ASCII string
498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000500{
Paul Bakker23986e52011-04-24 08:57:21 +0000501 int ret;
502 size_t i, j, slen, n;
Gilles Peskine984fd072021-04-03 18:26:13 +0200503 int sign = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_mpi_uint d;
505 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000506 MPI_VALIDATE_RET( X != NULL );
507 MPI_VALIDATE_RET( s != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
509 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000510 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000513
Gilles Peskine984fd072021-04-03 18:26:13 +0200514 if( s[0] == '-' )
515 {
516 ++s;
517 sign = -1;
518 }
519
Paul Bakkerff60ee62010-03-16 21:09:09 +0000520 slen = strlen( s );
521
Paul Bakker5121ce52009-01-03 21:22:43 +0000522 if( radix == 16 )
523 {
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +0100524 if( slen > MPI_SIZE_T_MAX >> 2 )
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +0200525 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
526
Paul Bakkerff60ee62010-03-16 21:09:09 +0000527 n = BITS_TO_LIMBS( slen << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n ) );
530 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
Paul Bakker23986e52011-04-24 08:57:21 +0000532 for( i = slen, j = 0; i > 0; i--, j++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200535 X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 }
537 }
538 else
539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
Paul Bakkerff60ee62010-03-16 21:09:09 +0000542 for( i = 0; i < slen; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
545 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T, X, radix ) );
Gilles Peskine984fd072021-04-03 18:26:13 +0200546 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, &T, d ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 }
548 }
549
Gilles Peskine984fd072021-04-03 18:26:13 +0200550 if( sign < 0 && mbedtls_mpi_bitlen( X ) != 0 )
551 X->s = -1;
552
Paul Bakker5121ce52009-01-03 21:22:43 +0000553cleanup:
554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
557 return( ret );
558}
559
560/*
Ron Eldora16fa292018-11-20 14:07:01 +0200561 * Helper to write the digits high-order first.
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 */
Ron Eldora16fa292018-11-20 14:07:01 +0200563static int mpi_write_hlp( mbedtls_mpi *X, int radix,
564 char **p, const size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000565{
566 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_mpi_uint r;
Ron Eldora16fa292018-11-20 14:07:01 +0200568 size_t length = 0;
569 char *p_end = *p + buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
Ron Eldora16fa292018-11-20 14:07:01 +0200571 do
572 {
573 if( length >= buflen )
574 {
575 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
576 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
Ron Eldora16fa292018-11-20 14:07:01 +0200578 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) );
579 MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) );
580 /*
581 * Write the residue in the current position, as an ASCII character.
582 */
583 if( r < 0xA )
584 *(--p_end) = (char)( '0' + r );
585 else
586 *(--p_end) = (char)( 'A' + ( r - 0xA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000587
Ron Eldora16fa292018-11-20 14:07:01 +0200588 length++;
589 } while( mbedtls_mpi_cmp_int( X, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
Ron Eldora16fa292018-11-20 14:07:01 +0200591 memmove( *p, p_end, length );
592 *p += length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000593
594cleanup:
595
596 return( ret );
597}
598
599/*
600 * Export into an ASCII string
601 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100602int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
603 char *buf, size_t buflen, size_t *olen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000604{
Paul Bakker23986e52011-04-24 08:57:21 +0000605 int ret = 0;
606 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000607 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000609 MPI_VALIDATE_RET( X != NULL );
610 MPI_VALIDATE_RET( olen != NULL );
611 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000612
613 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000614 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000615
Hanno Beckerc1fa6cd2019-02-04 09:45:07 +0000616 n = mbedtls_mpi_bitlen( X ); /* Number of bits necessary to present `n`. */
617 if( radix >= 4 ) n >>= 1; /* Number of 4-adic digits necessary to present
618 * `n`. If radix > 4, this might be a strict
619 * overapproximation of the number of
620 * radix-adic digits needed to present `n`. */
621 if( radix >= 16 ) n >>= 1; /* Number of hexadecimal digits necessary to
622 * present `n`. */
623
Janos Follath870ed002019-03-06 13:43:02 +0000624 n += 1; /* Terminating null byte */
Hanno Beckerc1fa6cd2019-02-04 09:45:07 +0000625 n += 1; /* Compensate for the divisions above, which round down `n`
626 * in case it's not even. */
627 n += 1; /* Potential '-'-sign. */
628 n += ( n & 1 ); /* Make n even to have enough space for hexadecimal writing,
629 * which always uses an even number of hex-digits. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000630
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100631 if( buflen < n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000632 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100633 *olen = n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 }
636
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100637 p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000639
640 if( X->s == -1 )
Hanno Beckeraf97cae2019-02-01 16:41:30 +0000641 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 *p++ = '-';
Hanno Beckeraf97cae2019-02-01 16:41:30 +0000643 buflen--;
644 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000645
646 if( radix == 16 )
647 {
Paul Bakker23986e52011-04-24 08:57:21 +0000648 int c;
649 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000650
Paul Bakker23986e52011-04-24 08:57:21 +0000651 for( i = X->n, k = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000652 {
Paul Bakker23986e52011-04-24 08:57:21 +0000653 for( j = ciL; j > 0; j-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000654 {
Paul Bakker23986e52011-04-24 08:57:21 +0000655 c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000656
Paul Bakker6c343d72014-07-10 14:36:19 +0200657 if( c == 0 && k == 0 && ( i + j ) != 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000658 continue;
659
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000660 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000661 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000662 k = 1;
663 }
664 }
665 }
666 else
667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T, X ) );
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000669
670 if( T.s == -1 )
671 T.s = 1;
672
Ron Eldora16fa292018-11-20 14:07:01 +0200673 MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p, buflen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000674 }
675
676 *p++ = '\0';
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100677 *olen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000678
679cleanup:
680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000682
683 return( ret );
684}
685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000687/*
688 * Read X from an opened file
689 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin )
Paul Bakker5121ce52009-01-03 21:22:43 +0000691{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 mbedtls_mpi_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000693 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000694 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000695 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000696 * Buffer should have space for (short) label and decimal formatted MPI,
697 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000698 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Paul Bakker5121ce52009-01-03 21:22:43 +0000700
Hanno Becker73d7d792018-12-11 10:35:51 +0000701 MPI_VALIDATE_RET( X != NULL );
702 MPI_VALIDATE_RET( fin != NULL );
703
704 if( radix < 2 || radix > 16 )
705 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
706
Paul Bakker5121ce52009-01-03 21:22:43 +0000707 memset( s, 0, sizeof( s ) );
708 if( fgets( s, sizeof( s ) - 1, fin ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
711 slen = strlen( s );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000712 if( slen == sizeof( s ) - 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000714
Hanno Beckerb2034b72017-04-26 11:46:46 +0100715 if( slen > 0 && s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
716 if( slen > 0 && s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
Paul Bakker5121ce52009-01-03 21:22:43 +0000717
718 p = s + slen;
Hanno Beckerb2034b72017-04-26 11:46:46 +0100719 while( p-- > s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000720 if( mpi_get_digit( &d, radix, *p ) != 0 )
721 break;
722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 return( mbedtls_mpi_read_string( X, radix, p + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000724}
725
726/*
727 * Write X into an opened file (or stdout if fout == NULL)
728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout )
Paul Bakker5121ce52009-01-03 21:22:43 +0000730{
Paul Bakker23986e52011-04-24 08:57:21 +0000731 int ret;
732 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000733 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000734 * Buffer should have space for (short) label and decimal formatted MPI,
735 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Hanno Becker73d7d792018-12-11 10:35:51 +0000738 MPI_VALIDATE_RET( X != NULL );
739
740 if( radix < 2 || radix > 16 )
741 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100743 memset( s, 0, sizeof( s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000744
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100745 MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, sizeof( s ) - 2, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000746
747 if( p == NULL ) p = "";
748
749 plen = strlen( p );
750 slen = strlen( s );
751 s[slen++] = '\r';
752 s[slen++] = '\n';
753
754 if( fout != NULL )
755 {
756 if( fwrite( p, 1, plen, fout ) != plen ||
757 fwrite( s, 1, slen, fout ) != slen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000759 }
760 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_printf( "%s%s", p, s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000762
763cleanup:
764
765 return( ret );
766}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000768
Hanno Beckerda1655a2017-10-18 14:21:44 +0100769
770/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
771 * into the storage form used by mbedtls_mpi. */
Hanno Beckerf8720072018-11-08 11:53:49 +0000772
773static mbedtls_mpi_uint mpi_uint_bigendian_to_host_c( mbedtls_mpi_uint x )
774{
775 uint8_t i;
Hanno Becker92c98932019-05-01 17:09:11 +0100776 unsigned char *x_ptr;
Hanno Beckerf8720072018-11-08 11:53:49 +0000777 mbedtls_mpi_uint tmp = 0;
Hanno Becker92c98932019-05-01 17:09:11 +0100778
779 for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
780 {
781 tmp <<= CHAR_BIT;
782 tmp |= (mbedtls_mpi_uint) *x_ptr;
783 }
784
Hanno Beckerf8720072018-11-08 11:53:49 +0000785 return( tmp );
786}
787
788static mbedtls_mpi_uint mpi_uint_bigendian_to_host( mbedtls_mpi_uint x )
789{
790#if defined(__BYTE_ORDER__)
791
792/* Nothing to do on bigendian systems. */
793#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
794 return( x );
795#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
796
797#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
798
799/* For GCC and Clang, have builtins for byte swapping. */
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000800#if defined(__GNUC__) && defined(__GNUC_PREREQ)
801#if __GNUC_PREREQ(4,3)
Hanno Beckerf8720072018-11-08 11:53:49 +0000802#define have_bswap
803#endif
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000804#endif
805
806#if defined(__clang__) && defined(__has_builtin)
807#if __has_builtin(__builtin_bswap32) && \
808 __has_builtin(__builtin_bswap64)
809#define have_bswap
810#endif
811#endif
812
Hanno Beckerf8720072018-11-08 11:53:49 +0000813#if defined(have_bswap)
814 /* The compiler is hopefully able to statically evaluate this! */
815 switch( sizeof(mbedtls_mpi_uint) )
816 {
817 case 4:
818 return( __builtin_bswap32(x) );
819 case 8:
820 return( __builtin_bswap64(x) );
821 }
822#endif
823#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
824#endif /* __BYTE_ORDER__ */
825
826 /* Fall back to C-based reordering if we don't know the byte order
827 * or we couldn't use a compiler-specific builtin. */
828 return( mpi_uint_bigendian_to_host_c( x ) );
829}
830
Hanno Becker2be8a552018-10-25 12:40:09 +0100831static void mpi_bigendian_to_host( mbedtls_mpi_uint * const p, size_t limbs )
Hanno Beckerda1655a2017-10-18 14:21:44 +0100832{
Hanno Beckerda1655a2017-10-18 14:21:44 +0100833 mbedtls_mpi_uint *cur_limb_left;
834 mbedtls_mpi_uint *cur_limb_right;
Hanno Becker2be8a552018-10-25 12:40:09 +0100835 if( limbs == 0 )
836 return;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100837
838 /*
839 * Traverse limbs and
840 * - adapt byte-order in each limb
841 * - swap the limbs themselves.
842 * For that, simultaneously traverse the limbs from left to right
843 * and from right to left, as long as the left index is not bigger
844 * than the right index (it's not a problem if limbs is odd and the
845 * indices coincide in the last iteration).
846 */
Hanno Beckerda1655a2017-10-18 14:21:44 +0100847 for( cur_limb_left = p, cur_limb_right = p + ( limbs - 1 );
848 cur_limb_left <= cur_limb_right;
849 cur_limb_left++, cur_limb_right-- )
850 {
Hanno Beckerf8720072018-11-08 11:53:49 +0000851 mbedtls_mpi_uint tmp;
852 /* Note that if cur_limb_left == cur_limb_right,
853 * this code effectively swaps the bytes only once. */
854 tmp = mpi_uint_bigendian_to_host( *cur_limb_left );
855 *cur_limb_left = mpi_uint_bigendian_to_host( *cur_limb_right );
856 *cur_limb_right = tmp;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100857 }
Hanno Beckerda1655a2017-10-18 14:21:44 +0100858}
859
Paul Bakker5121ce52009-01-03 21:22:43 +0000860/*
861 * Import X from unsigned binary data, big endian
862 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000864{
Paul Bakker23986e52011-04-24 08:57:21 +0000865 int ret;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100866 size_t const limbs = CHARS_TO_LIMBS( buflen );
867 size_t const overhead = ( limbs * ciL ) - buflen;
868 unsigned char *Xp;
Paul Bakker5121ce52009-01-03 21:22:43 +0000869
Hanno Becker8ce11a32018-12-19 16:18:52 +0000870 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +0000871 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
872
Hanno Becker073c1992017-10-17 15:17:27 +0100873 /* Ensure that target MPI has exactly the necessary number of limbs */
874 if( X->n != limbs )
875 {
876 mbedtls_mpi_free( X );
877 mbedtls_mpi_init( X );
878 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, limbs ) );
879 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000881
Hanno Becker0e810b92019-01-03 17:13:11 +0000882 /* Avoid calling `memcpy` with NULL source argument,
883 * even if buflen is 0. */
884 if( buf != NULL )
885 {
886 Xp = (unsigned char*) X->p;
887 memcpy( Xp + overhead, buf, buflen );
Hanno Beckerda1655a2017-10-18 14:21:44 +0100888
Hanno Becker0e810b92019-01-03 17:13:11 +0000889 mpi_bigendian_to_host( X->p, limbs );
890 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000891
892cleanup:
893
894 return( ret );
895}
896
897/*
898 * Export X into unsigned binary data, big endian
899 */
Gilles Peskine11cdb052018-11-20 16:47:47 +0100900int mbedtls_mpi_write_binary( const mbedtls_mpi *X,
901 unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000902{
Hanno Becker73d7d792018-12-11 10:35:51 +0000903 size_t stored_bytes;
Gilles Peskine11cdb052018-11-20 16:47:47 +0100904 size_t bytes_to_copy;
905 unsigned char *p;
906 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000907
Hanno Becker73d7d792018-12-11 10:35:51 +0000908 MPI_VALIDATE_RET( X != NULL );
909 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
910
911 stored_bytes = X->n * ciL;
912
Gilles Peskine11cdb052018-11-20 16:47:47 +0100913 if( stored_bytes < buflen )
914 {
915 /* There is enough space in the output buffer. Write initial
916 * null bytes and record the position at which to start
917 * writing the significant bytes. In this case, the execution
918 * trace of this function does not depend on the value of the
919 * number. */
920 bytes_to_copy = stored_bytes;
921 p = buf + buflen - stored_bytes;
922 memset( buf, 0, buflen - stored_bytes );
923 }
924 else
925 {
926 /* The output buffer is smaller than the allocated size of X.
927 * However X may fit if its leading bytes are zero. */
928 bytes_to_copy = buflen;
929 p = buf;
930 for( i = bytes_to_copy; i < stored_bytes; i++ )
931 {
932 if( GET_BYTE( X, i ) != 0 )
933 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
934 }
935 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000936
Gilles Peskine11cdb052018-11-20 16:47:47 +0100937 for( i = 0; i < bytes_to_copy; i++ )
938 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000939
940 return( 0 );
941}
942
943/*
944 * Left-shift: X <<= count
945 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +0000947{
Paul Bakker23986e52011-04-24 08:57:21 +0000948 int ret;
949 size_t i, v0, t1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +0000951 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000952
953 v0 = count / (biL );
954 t1 = count & (biL - 1);
955
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200956 i = mbedtls_mpi_bitlen( X ) + count;
Paul Bakker5121ce52009-01-03 21:22:43 +0000957
Paul Bakkerf9688572011-05-05 10:00:45 +0000958 if( X->n * biL < i )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000960
961 ret = 0;
962
963 /*
964 * shift by count / limb_size
965 */
966 if( v0 > 0 )
967 {
Paul Bakker23986e52011-04-24 08:57:21 +0000968 for( i = X->n; i > v0; i-- )
969 X->p[i - 1] = X->p[i - v0 - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +0000970
Paul Bakker23986e52011-04-24 08:57:21 +0000971 for( ; i > 0; i-- )
972 X->p[i - 1] = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000973 }
974
975 /*
976 * shift by count % limb_size
977 */
978 if( t1 > 0 )
979 {
980 for( i = v0; i < X->n; i++ )
981 {
982 r1 = X->p[i] >> (biL - t1);
983 X->p[i] <<= t1;
984 X->p[i] |= r0;
985 r0 = r1;
986 }
987 }
988
989cleanup:
990
991 return( ret );
992}
993
994/*
995 * Right-shift: X >>= count
996 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +0000998{
Paul Bakker23986e52011-04-24 08:57:21 +0000999 size_t i, v0, v1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +00001001 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001002
1003 v0 = count / biL;
1004 v1 = count & (biL - 1);
1005
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001006 if( v0 > X->n || ( v0 == X->n && v1 > 0 ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 return mbedtls_mpi_lset( X, 0 );
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001008
Paul Bakker5121ce52009-01-03 21:22:43 +00001009 /*
1010 * shift by count / limb_size
1011 */
1012 if( v0 > 0 )
1013 {
1014 for( i = 0; i < X->n - v0; i++ )
1015 X->p[i] = X->p[i + v0];
1016
1017 for( ; i < X->n; i++ )
1018 X->p[i] = 0;
1019 }
1020
1021 /*
1022 * shift by count % limb_size
1023 */
1024 if( v1 > 0 )
1025 {
Paul Bakker23986e52011-04-24 08:57:21 +00001026 for( i = X->n; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001027 {
Paul Bakker23986e52011-04-24 08:57:21 +00001028 r1 = X->p[i - 1] << (biL - v1);
1029 X->p[i - 1] >>= v1;
1030 X->p[i - 1] |= r0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001031 r0 = r1;
1032 }
1033 }
1034
1035 return( 0 );
1036}
1037
1038/*
1039 * Compare unsigned values
1040 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001042{
Paul Bakker23986e52011-04-24 08:57:21 +00001043 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001044 MPI_VALIDATE_RET( X != NULL );
1045 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001046
Paul Bakker23986e52011-04-24 08:57:21 +00001047 for( i = X->n; i > 0; i-- )
1048 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001049 break;
1050
Paul Bakker23986e52011-04-24 08:57:21 +00001051 for( j = Y->n; j > 0; j-- )
1052 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001053 break;
1054
Paul Bakker23986e52011-04-24 08:57:21 +00001055 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 return( 0 );
1057
1058 if( i > j ) return( 1 );
1059 if( j > i ) return( -1 );
1060
Paul Bakker23986e52011-04-24 08:57:21 +00001061 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001062 {
Paul Bakker23986e52011-04-24 08:57:21 +00001063 if( X->p[i - 1] > Y->p[i - 1] ) return( 1 );
1064 if( X->p[i - 1] < Y->p[i - 1] ) return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001065 }
1066
1067 return( 0 );
1068}
1069
1070/*
1071 * Compare signed values
1072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001074{
Paul Bakker23986e52011-04-24 08:57:21 +00001075 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001076 MPI_VALIDATE_RET( X != NULL );
1077 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001078
Paul Bakker23986e52011-04-24 08:57:21 +00001079 for( i = X->n; i > 0; i-- )
1080 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 break;
1082
Paul Bakker23986e52011-04-24 08:57:21 +00001083 for( j = Y->n; j > 0; j-- )
1084 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001085 break;
1086
Paul Bakker23986e52011-04-24 08:57:21 +00001087 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 return( 0 );
1089
1090 if( i > j ) return( X->s );
Paul Bakker0c8f73b2012-03-22 14:08:57 +00001091 if( j > i ) return( -Y->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001092
1093 if( X->s > 0 && Y->s < 0 ) return( 1 );
1094 if( Y->s > 0 && X->s < 0 ) return( -1 );
1095
Paul Bakker23986e52011-04-24 08:57:21 +00001096 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001097 {
Paul Bakker23986e52011-04-24 08:57:21 +00001098 if( X->p[i - 1] > Y->p[i - 1] ) return( X->s );
1099 if( X->p[i - 1] < Y->p[i - 1] ) return( -X->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001100 }
1101
1102 return( 0 );
1103}
1104
Janos Follath45ec9902019-10-14 09:09:32 +01001105/** Decide if an integer is less than the other, without branches.
1106 *
1107 * \param x First integer.
1108 * \param y Second integer.
1109 *
1110 * \return 1 if \p x is less than \p y, 0 otherwise
1111 */
Janos Follath867a3ab2019-10-11 14:21:53 +01001112static unsigned ct_lt_mpi_uint( const mbedtls_mpi_uint x,
1113 const mbedtls_mpi_uint y )
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001114{
1115 mbedtls_mpi_uint ret;
1116 mbedtls_mpi_uint cond;
1117
1118 /*
1119 * Check if the most significant bits (MSB) of the operands are different.
1120 */
1121 cond = ( x ^ y );
1122 /*
1123 * If the MSB are the same then the difference x-y will be negative (and
1124 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
1125 */
1126 ret = ( x - y ) & ~cond;
1127 /*
1128 * If the MSB are different, then the operand with the MSB of 1 is the
1129 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
1130 * the MSB of y is 0.)
1131 */
1132 ret |= y & cond;
1133
1134
Janos Follath7a34bcf2019-10-14 08:59:14 +01001135 ret = ret >> ( biL - 1 );
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001136
Janos Follath359a01e2019-10-29 15:08:46 +00001137 return (unsigned) ret;
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001138}
1139
1140/*
1141 * Compare signed values in constant time
1142 */
Janos Follath867a3ab2019-10-11 14:21:53 +01001143int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y,
1144 unsigned *ret )
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001145{
1146 size_t i;
Janos Follathbd87a592019-10-28 12:23:18 +00001147 /* The value of any of these variables is either 0 or 1 at all times. */
Janos Follath1f21c1d2019-10-28 12:31:34 +00001148 unsigned cond, done, X_is_negative, Y_is_negative;
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001149
1150 MPI_VALIDATE_RET( X != NULL );
1151 MPI_VALIDATE_RET( Y != NULL );
1152 MPI_VALIDATE_RET( ret != NULL );
1153
1154 if( X->n != Y->n )
1155 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1156
1157 /*
Janos Follath58525182019-10-28 12:12:15 +00001158 * Set sign_N to 1 if N >= 0, 0 if N < 0.
1159 * We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0.
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001160 */
Janos Follath1f21c1d2019-10-28 12:31:34 +00001161 X_is_negative = ( X->s & 2 ) >> 1;
1162 Y_is_negative = ( Y->s & 2 ) >> 1;
Janos Follath867a3ab2019-10-11 14:21:53 +01001163
1164 /*
1165 * If the signs are different, then the positive operand is the bigger.
Janos Follath1f21c1d2019-10-28 12:31:34 +00001166 * That is if X is negative (X_is_negative == 1), then X < Y is true and it
1167 * is false if X is positive (X_is_negative == 0).
Janos Follath867a3ab2019-10-11 14:21:53 +01001168 */
Janos Follath1f21c1d2019-10-28 12:31:34 +00001169 cond = ( X_is_negative ^ Y_is_negative );
1170 *ret = cond & X_is_negative;
Janos Follath867a3ab2019-10-11 14:21:53 +01001171
1172 /*
Janos Follathbd87a592019-10-28 12:23:18 +00001173 * This is a constant-time function. We might have the result, but we still
Janos Follath867a3ab2019-10-11 14:21:53 +01001174 * need to go through the loop. Record if we have the result already.
1175 */
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001176 done = cond;
1177
1178 for( i = X->n; i > 0; i-- )
1179 {
1180 /*
Janos Follathe25f1ee2019-11-05 12:24:52 +00001181 * If Y->p[i - 1] < X->p[i - 1] then X < Y is true if and only if both
1182 * X and Y are negative.
Janos Follath867a3ab2019-10-11 14:21:53 +01001183 *
1184 * Again even if we can make a decision, we just mark the result and
1185 * the fact that we are done and continue looping.
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001186 */
Janos Follathe25f1ee2019-11-05 12:24:52 +00001187 cond = ct_lt_mpi_uint( Y->p[i - 1], X->p[i - 1] );
1188 *ret |= cond & ( 1 - done ) & X_is_negative;
Janos Follathfbe4c942019-10-28 12:37:21 +00001189 done |= cond;
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001190
1191 /*
Janos Follathe25f1ee2019-11-05 12:24:52 +00001192 * If X->p[i - 1] < Y->p[i - 1] then X < Y is true if and only if both
1193 * X and Y are positive.
Janos Follath867a3ab2019-10-11 14:21:53 +01001194 *
1195 * Again even if we can make a decision, we just mark the result and
1196 * the fact that we are done and continue looping.
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001197 */
Janos Follathe25f1ee2019-11-05 12:24:52 +00001198 cond = ct_lt_mpi_uint( X->p[i - 1], Y->p[i - 1] );
1199 *ret |= cond & ( 1 - done ) & ( 1 - X_is_negative );
Janos Follathfbe4c942019-10-28 12:37:21 +00001200 done |= cond;
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001201 }
1202
1203 return( 0 );
1204}
1205
Paul Bakker5121ce52009-01-03 21:22:43 +00001206/*
1207 * Compare signed values
1208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +00001210{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 mbedtls_mpi Y;
1212 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001213 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001214
1215 *p = ( z < 0 ) ? -z : z;
1216 Y.s = ( z < 0 ) ? -1 : 1;
1217 Y.n = 1;
1218 Y.p = p;
1219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 return( mbedtls_mpi_cmp_mpi( X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001221}
1222
1223/*
1224 * Unsigned addition: X = |A| + |B| (HAC 14.7)
1225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001227{
Paul Bakker23986e52011-04-24 08:57:21 +00001228 int ret;
1229 size_t i, j;
Janos Follath6c922682015-10-30 17:43:11 +01001230 mbedtls_mpi_uint *o, *p, c, tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +00001231 MPI_VALIDATE_RET( X != NULL );
1232 MPI_VALIDATE_RET( A != NULL );
1233 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001234
1235 if( X == B )
1236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 const mbedtls_mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001238 }
1239
1240 if( X != A )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
Paul Bakker9af723c2014-05-01 13:03:14 +02001242
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001243 /*
1244 * X should always be positive as a result of unsigned additions.
1245 */
1246 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001247
Paul Bakker23986e52011-04-24 08:57:21 +00001248 for( j = B->n; j > 0; j-- )
1249 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 break;
1251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001253
1254 o = B->p; p = X->p; c = 0;
1255
Janos Follath6c922682015-10-30 17:43:11 +01001256 /*
1257 * tmp is used because it might happen that p == o
1258 */
Paul Bakker23986e52011-04-24 08:57:21 +00001259 for( i = 0; i < j; i++, o++, p++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001260 {
Janos Follath6c922682015-10-30 17:43:11 +01001261 tmp= *o;
Paul Bakker5121ce52009-01-03 21:22:43 +00001262 *p += c; c = ( *p < c );
Janos Follath6c922682015-10-30 17:43:11 +01001263 *p += tmp; c += ( *p < tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +00001264 }
1265
1266 while( c != 0 )
1267 {
1268 if( i >= X->n )
1269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001271 p = X->p + i;
1272 }
1273
Paul Bakker2d319fd2012-09-16 21:34:26 +00001274 *p += c; c = ( *p < c ); i++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001275 }
1276
1277cleanup:
1278
1279 return( ret );
1280}
1281
Gilles Peskinede719d52020-06-09 10:39:38 +02001282/**
Gilles Peskine36acd542020-06-08 21:58:22 +02001283 * Helper for mbedtls_mpi subtraction.
1284 *
1285 * Calculate d - s where d and s have the same size.
1286 * This function operates modulo (2^ciL)^n and returns the carry
1287 * (1 if there was a wraparound, i.e. if `d < s`, and 0 otherwise).
1288 *
1289 * \param n Number of limbs of \p d and \p s.
1290 * \param[in,out] d On input, the left operand.
1291 * On output, the result of the subtraction:
Gilles Peskinede719d52020-06-09 10:39:38 +02001292 * \param[in] s The right operand.
Gilles Peskine36acd542020-06-08 21:58:22 +02001293 *
1294 * \return 1 if `d < s`.
1295 * 0 if `d >= s`.
Paul Bakker5121ce52009-01-03 21:22:43 +00001296 */
Gilles Peskine36acd542020-06-08 21:58:22 +02001297static mbedtls_mpi_uint mpi_sub_hlp( size_t n,
1298 mbedtls_mpi_uint *d,
1299 const mbedtls_mpi_uint *s )
Paul Bakker5121ce52009-01-03 21:22:43 +00001300{
Paul Bakker23986e52011-04-24 08:57:21 +00001301 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 mbedtls_mpi_uint c, z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001303
1304 for( i = c = 0; i < n; i++, s++, d++ )
1305 {
1306 z = ( *d < c ); *d -= c;
1307 c = ( *d < *s ) + z; *d -= *s;
1308 }
1309
Gilles Peskine36acd542020-06-08 21:58:22 +02001310 return( c );
Paul Bakker5121ce52009-01-03 21:22:43 +00001311}
1312
1313/*
Gilles Peskine08fd43c2020-06-08 22:50:35 +02001314 * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001317{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 mbedtls_mpi TB;
Paul Bakker23986e52011-04-24 08:57:21 +00001319 int ret;
1320 size_t n;
Gilles Peskine08fd43c2020-06-08 22:50:35 +02001321 mbedtls_mpi_uint carry;
Hanno Becker73d7d792018-12-11 10:35:51 +00001322 MPI_VALIDATE_RET( X != NULL );
1323 MPI_VALIDATE_RET( A != NULL );
1324 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001327
1328 if( X == B )
1329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 B = &TB;
1332 }
1333
1334 if( X != A )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001336
Paul Bakker1ef7a532009-06-20 10:50:55 +00001337 /*
Paul Bakker60b1d102013-10-29 10:02:51 +01001338 * X should always be positive as a result of unsigned subtractions.
Paul Bakker1ef7a532009-06-20 10:50:55 +00001339 */
1340 X->s = 1;
1341
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 ret = 0;
1343
Paul Bakker23986e52011-04-24 08:57:21 +00001344 for( n = B->n; n > 0; n-- )
1345 if( B->p[n - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 break;
Gilles Peskine6260b702021-01-27 22:30:43 +01001347 if( n > A->n )
1348 {
1349 /* B >= (2^ciL)^n > A */
1350 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1351 goto cleanup;
1352 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001353
Gilles Peskine08fd43c2020-06-08 22:50:35 +02001354 carry = mpi_sub_hlp( n, X->p, B->p );
1355 if( carry != 0 )
Gilles Peskine36acd542020-06-08 21:58:22 +02001356 {
Gilles Peskine08fd43c2020-06-08 22:50:35 +02001357 /* Propagate the carry to the first nonzero limb of X. */
1358 for( ; n < X->n && X->p[n] == 0; n++ )
1359 --X->p[n];
1360 /* If we ran out of space for the carry, it means that the result
1361 * is negative. */
1362 if( n == X->n )
Gilles Peskine84697ca2020-07-23 01:16:46 +02001363 {
1364 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1365 goto cleanup;
1366 }
Gilles Peskine08fd43c2020-06-08 22:50:35 +02001367 --X->p[n];
Gilles Peskine36acd542020-06-08 21:58:22 +02001368 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001369
1370cleanup:
1371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 mbedtls_mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001373
1374 return( ret );
1375}
1376
1377/*
1378 * Signed addition: X = A + B
1379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001381{
Hanno Becker73d7d792018-12-11 10:35:51 +00001382 int ret, s;
1383 MPI_VALIDATE_RET( X != NULL );
1384 MPI_VALIDATE_RET( A != NULL );
1385 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001386
Hanno Becker73d7d792018-12-11 10:35:51 +00001387 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001388 if( A->s * B->s < 0 )
1389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001393 X->s = s;
1394 }
1395 else
1396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001398 X->s = -s;
1399 }
1400 }
1401 else
1402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 X->s = s;
1405 }
1406
1407cleanup:
1408
1409 return( ret );
1410}
1411
1412/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001413 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001416{
Hanno Becker73d7d792018-12-11 10:35:51 +00001417 int ret, s;
1418 MPI_VALIDATE_RET( X != NULL );
1419 MPI_VALIDATE_RET( A != NULL );
1420 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421
Hanno Becker73d7d792018-12-11 10:35:51 +00001422 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001423 if( A->s * B->s > 0 )
1424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 X->s = s;
1429 }
1430 else
1431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001433 X->s = -s;
1434 }
1435 }
1436 else
1437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001439 X->s = s;
1440 }
1441
1442cleanup:
1443
1444 return( ret );
1445}
1446
1447/*
1448 * Signed addition: X = A + b
1449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001451{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 mbedtls_mpi _B;
1453 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001454 MPI_VALIDATE_RET( X != NULL );
1455 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001456
1457 p[0] = ( b < 0 ) ? -b : b;
1458 _B.s = ( b < 0 ) ? -1 : 1;
1459 _B.n = 1;
1460 _B.p = p;
1461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 return( mbedtls_mpi_add_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001463}
1464
1465/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001466 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001467 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001469{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 mbedtls_mpi _B;
1471 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001472 MPI_VALIDATE_RET( X != NULL );
1473 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001474
1475 p[0] = ( b < 0 ) ? -b : b;
1476 _B.s = ( b < 0 ) ? -1 : 1;
1477 _B.n = 1;
1478 _B.p = p;
1479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 return( mbedtls_mpi_sub_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001481}
1482
1483/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 * Helper for mbedtls_mpi multiplication
Paul Bakkerfc4f46f2013-06-24 19:23:56 +02001485 */
1486static
1487#if defined(__APPLE__) && defined(__arm__)
1488/*
1489 * Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
1490 * appears to need this to prevent bad ARM code generation at -O3.
1491 */
1492__attribute__ ((noinline))
1493#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494void mpi_mul_hlp( size_t i, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d, mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001495{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 mbedtls_mpi_uint c = 0, t = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001497
1498#if defined(MULADDC_HUIT)
1499 for( ; i >= 8; i -= 8 )
1500 {
1501 MULADDC_INIT
1502 MULADDC_HUIT
1503 MULADDC_STOP
1504 }
1505
1506 for( ; i > 0; i-- )
1507 {
1508 MULADDC_INIT
1509 MULADDC_CORE
1510 MULADDC_STOP
1511 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001512#else /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001513 for( ; i >= 16; i -= 16 )
1514 {
1515 MULADDC_INIT
1516 MULADDC_CORE MULADDC_CORE
1517 MULADDC_CORE MULADDC_CORE
1518 MULADDC_CORE MULADDC_CORE
1519 MULADDC_CORE MULADDC_CORE
1520
1521 MULADDC_CORE MULADDC_CORE
1522 MULADDC_CORE MULADDC_CORE
1523 MULADDC_CORE MULADDC_CORE
1524 MULADDC_CORE MULADDC_CORE
1525 MULADDC_STOP
1526 }
1527
1528 for( ; i >= 8; i -= 8 )
1529 {
1530 MULADDC_INIT
1531 MULADDC_CORE MULADDC_CORE
1532 MULADDC_CORE MULADDC_CORE
1533
1534 MULADDC_CORE MULADDC_CORE
1535 MULADDC_CORE MULADDC_CORE
1536 MULADDC_STOP
1537 }
1538
1539 for( ; i > 0; i-- )
1540 {
1541 MULADDC_INIT
1542 MULADDC_CORE
1543 MULADDC_STOP
1544 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001545#endif /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001546
1547 t++;
1548
1549 do {
1550 *d += c; c = ( *d < c ); d++;
1551 }
1552 while( c != 0 );
1553}
1554
1555/*
1556 * Baseline multiplication: X = A * B (HAC 14.12)
1557 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001559{
Paul Bakker23986e52011-04-24 08:57:21 +00001560 int ret;
1561 size_t i, j;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 mbedtls_mpi TA, TB;
Hanno Becker73d7d792018-12-11 10:35:51 +00001563 MPI_VALIDATE_RET( X != NULL );
1564 MPI_VALIDATE_RET( A != NULL );
1565 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 if( X == A ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); A = &TA; }
1570 if( X == B ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); B = &TB; }
Paul Bakker5121ce52009-01-03 21:22:43 +00001571
Paul Bakker23986e52011-04-24 08:57:21 +00001572 for( i = A->n; i > 0; i-- )
1573 if( A->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001574 break;
1575
Paul Bakker23986e52011-04-24 08:57:21 +00001576 for( j = B->n; j > 0; j-- )
1577 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001578 break;
1579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + j ) );
1581 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001582
Alexey Skalozub8e75e682016-01-13 21:59:27 +02001583 for( ; j > 0; j-- )
1584 mpi_mul_hlp( i, A->p, X->p + j - 1, B->p[j - 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00001585
1586 X->s = A->s * B->s;
1587
1588cleanup:
1589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001591
1592 return( ret );
1593}
1594
1595/*
1596 * Baseline multiplication: X = A * b
1597 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001599{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 mbedtls_mpi _B;
1601 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001602 MPI_VALIDATE_RET( X != NULL );
1603 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001604
1605 _B.s = 1;
1606 _B.n = 1;
1607 _B.p = p;
1608 p[0] = b;
1609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 return( mbedtls_mpi_mul_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001611}
1612
1613/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001614 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1615 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001616 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001617static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1,
1618 mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r )
Simon Butcher15b15d12015-11-26 19:35:03 +00001619{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001620#if defined(MBEDTLS_HAVE_UDBL)
1621 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001622#else
Simon Butcher9803d072016-01-03 00:24:34 +00001623 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
1624 const mbedtls_mpi_uint uint_halfword_mask = ( (mbedtls_mpi_uint) 1 << biH ) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001625 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1626 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001627 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001628#endif
1629
Simon Butcher15b15d12015-11-26 19:35:03 +00001630 /*
1631 * Check for overflow
1632 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001633 if( 0 == d || u1 >= d )
Simon Butcher15b15d12015-11-26 19:35:03 +00001634 {
Simon Butcherf5ba0452015-12-27 23:01:55 +00001635 if (r != NULL) *r = ~0;
Simon Butcher15b15d12015-11-26 19:35:03 +00001636
Simon Butcherf5ba0452015-12-27 23:01:55 +00001637 return ( ~0 );
Simon Butcher15b15d12015-11-26 19:35:03 +00001638 }
1639
1640#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001641 dividend = (mbedtls_t_udbl) u1 << biL;
1642 dividend |= (mbedtls_t_udbl) u0;
1643 quotient = dividend / d;
1644 if( quotient > ( (mbedtls_t_udbl) 1 << biL ) - 1 )
1645 quotient = ( (mbedtls_t_udbl) 1 << biL ) - 1;
1646
1647 if( r != NULL )
Simon Butcher9803d072016-01-03 00:24:34 +00001648 *r = (mbedtls_mpi_uint)( dividend - (quotient * d ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001649
1650 return (mbedtls_mpi_uint) quotient;
1651#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001652
1653 /*
1654 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1655 * Vol. 2 - Seminumerical Algorithms, Knuth
1656 */
1657
1658 /*
1659 * Normalize the divisor, d, and dividend, u0, u1
1660 */
1661 s = mbedtls_clz( d );
1662 d = d << s;
1663
1664 u1 = u1 << s;
Simon Butcher9803d072016-01-03 00:24:34 +00001665 u1 |= ( u0 >> ( biL - s ) ) & ( -(mbedtls_mpi_sint)s >> ( biL - 1 ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001666 u0 = u0 << s;
1667
1668 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001669 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001670
1671 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001672 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001673
1674 /*
1675 * Find the first quotient and remainder
1676 */
1677 q1 = u1 / d1;
1678 r0 = u1 - d1 * q1;
1679
1680 while( q1 >= radix || ( q1 * d0 > radix * r0 + u0_msw ) )
1681 {
1682 q1 -= 1;
1683 r0 += d1;
1684
1685 if ( r0 >= radix ) break;
1686 }
1687
Simon Butcherf5ba0452015-12-27 23:01:55 +00001688 rAX = ( u1 * radix ) + ( u0_msw - q1 * d );
Simon Butcher15b15d12015-11-26 19:35:03 +00001689 q0 = rAX / d1;
1690 r0 = rAX - q0 * d1;
1691
1692 while( q0 >= radix || ( q0 * d0 > radix * r0 + u0_lsw ) )
1693 {
1694 q0 -= 1;
1695 r0 += d1;
1696
1697 if ( r0 >= radix ) break;
1698 }
1699
1700 if (r != NULL)
Simon Butcherf5ba0452015-12-27 23:01:55 +00001701 *r = ( rAX * radix + u0_lsw - q0 * d ) >> s;
Simon Butcher15b15d12015-11-26 19:35:03 +00001702
1703 quotient = q1 * radix + q0;
1704
1705 return quotient;
1706#endif
1707}
1708
1709/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001711 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001712int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1713 const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001714{
Paul Bakker23986e52011-04-24 08:57:21 +00001715 int ret;
1716 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 mbedtls_mpi X, Y, Z, T1, T2;
Hanno Becker73d7d792018-12-11 10:35:51 +00001718 MPI_VALIDATE_RET( A != NULL );
1719 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 if( mbedtls_mpi_cmp_int( B, 0 ) == 0 )
1722 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
1725 mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 if( Q != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_lset( Q, 0 ) );
1730 if( R != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001731 return( 0 );
1732 }
1733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &X, A ) );
1735 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001736 X.s = Y.s = 1;
1737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &Z, A->n + 2 ) );
1739 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Z, 0 ) );
1740 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, 2 ) );
1741 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T2, 3 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001742
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001743 k = mbedtls_mpi_bitlen( &Y ) % biL;
Paul Bakkerf9688572011-05-05 10:00:45 +00001744 if( k < biL - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001745 {
1746 k = biL - 1 - k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &X, k ) );
1748 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, k ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001749 }
1750 else k = 0;
1751
1752 n = X.n - 1;
1753 t = Y.n - 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756 while( mbedtls_mpi_cmp_mpi( &X, &Y ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001757 {
1758 Z.p[n - t]++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001760 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001762
1763 for( i = n; i > t ; i-- )
1764 {
1765 if( X.p[i] >= Y.p[t] )
1766 Z.p[i - t - 1] = ~0;
1767 else
1768 {
Simon Butcher15b15d12015-11-26 19:35:03 +00001769 Z.p[i - t - 1] = mbedtls_int_div_int( X.p[i], X.p[i - 1],
1770 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001771 }
1772
1773 Z.p[i - t - 1]++;
1774 do
1775 {
1776 Z.p[i - t - 1]--;
1777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T1, 0 ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001779 T1.p[0] = ( t < 1 ) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001780 T1.p[1] = Y.p[t];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T2, 0 ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001784 T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2];
1785 T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001786 T2.p[2] = X.p[i];
1787 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788 while( mbedtls_mpi_cmp_mpi( &T1, &T2 ) > 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
1791 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1792 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 if( mbedtls_mpi_cmp_int( &X, 0 ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &Y ) );
1797 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1798 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001799 Z.p[i - t - 1]--;
1800 }
1801 }
1802
1803 if( Q != NULL )
1804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( Q, &Z ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001806 Q->s = A->s * B->s;
1807 }
1808
1809 if( R != NULL )
1810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &X, k ) );
Paul Bakkerf02c5642012-11-13 10:25:21 +00001812 X.s = A->s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, &X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815 if( mbedtls_mpi_cmp_int( R, 0 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001816 R->s = 1;
1817 }
1818
1819cleanup:
1820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
1822 mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001823
1824 return( ret );
1825}
1826
1827/*
1828 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00001829 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001830int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R,
1831 const mbedtls_mpi *A,
1832 mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001833{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 mbedtls_mpi _B;
1835 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001836 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001837
1838 p[0] = ( b < 0 ) ? -b : b;
1839 _B.s = ( b < 0 ) ? -1 : 1;
1840 _B.n = 1;
1841 _B.p = p;
1842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 return( mbedtls_mpi_div_mpi( Q, R, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001844}
1845
1846/*
1847 * Modulo: R = A mod B
1848 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001850{
1851 int ret;
Hanno Becker73d7d792018-12-11 10:35:51 +00001852 MPI_VALIDATE_RET( R != NULL );
1853 MPI_VALIDATE_RET( A != NULL );
1854 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 if( mbedtls_mpi_cmp_int( B, 0 ) < 0 )
1857 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( NULL, R, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 while( mbedtls_mpi_cmp_int( R, 0 ) < 0 )
1862 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 while( mbedtls_mpi_cmp_mpi( R, B ) >= 0 )
1865 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001866
1867cleanup:
1868
1869 return( ret );
1870}
1871
1872/*
1873 * Modulo: r = A mod b
1874 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001876{
Paul Bakker23986e52011-04-24 08:57:21 +00001877 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 mbedtls_mpi_uint x, y, z;
Hanno Becker73d7d792018-12-11 10:35:51 +00001879 MPI_VALIDATE_RET( r != NULL );
1880 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001881
1882 if( b == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001884
1885 if( b < 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001887
1888 /*
1889 * handle trivial cases
1890 */
1891 if( b == 1 )
1892 {
1893 *r = 0;
1894 return( 0 );
1895 }
1896
1897 if( b == 2 )
1898 {
1899 *r = A->p[0] & 1;
1900 return( 0 );
1901 }
1902
1903 /*
1904 * general case
1905 */
Paul Bakker23986e52011-04-24 08:57:21 +00001906 for( i = A->n, y = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001907 {
Paul Bakker23986e52011-04-24 08:57:21 +00001908 x = A->p[i - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001909 y = ( y << biH ) | ( x >> biH );
1910 z = y / b;
1911 y -= z * b;
1912
1913 x <<= biH;
1914 y = ( y << biH ) | ( x >> biH );
1915 z = y / b;
1916 y -= z * b;
1917 }
1918
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001919 /*
1920 * If A is negative, then the current y represents a negative value.
1921 * Flipping it to the positive side.
1922 */
1923 if( A->s < 0 && y != 0 )
1924 y = b - y;
1925
Paul Bakker5121ce52009-01-03 21:22:43 +00001926 *r = y;
1927
1928 return( 0 );
1929}
1930
1931/*
1932 * Fast Montgomery initialization (thanks to Tom St Denis)
1933 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934static void mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00001935{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936 mbedtls_mpi_uint x, m0 = N->p[0];
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01001937 unsigned int i;
Paul Bakker5121ce52009-01-03 21:22:43 +00001938
1939 x = m0;
1940 x += ( ( m0 + 2 ) & 4 ) << 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001941
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01001942 for( i = biL; i >= 8; i /= 2 )
1943 x *= ( 2 - ( m0 * x ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001944
1945 *mm = ~x + 1;
1946}
1947
Gilles Peskine3ce3ddf2020-06-04 15:00:49 +02001948/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
1949 *
1950 * \param[in,out] A One of the numbers to multiply.
Gilles Peskine635a3742020-06-08 22:37:50 +02001951 * It must have at least as many limbs as N
1952 * (A->n >= N->n), and any limbs beyond n are ignored.
Gilles Peskine3ce3ddf2020-06-04 15:00:49 +02001953 * On successful completion, A contains the result of
1954 * the multiplication A * B * R^-1 mod N where
1955 * R = (2^ciL)^n.
1956 * \param[in] B One of the numbers to multiply.
1957 * It must be nonzero and must not have more limbs than N
1958 * (B->n <= N->n).
1959 * \param[in] N The modulo. N must be odd.
1960 * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
1961 * This is -N^-1 mod 2^ciL.
1962 * \param[in,out] T A bignum for temporary storage.
1963 * It must be at least twice the limb size of N plus 2
1964 * (T->n >= 2 * (N->n + 1)).
1965 * Its initial content is unused and
1966 * its final content is indeterminate.
1967 * Note that unlike the usual convention in the library
1968 * for `const mbedtls_mpi*`, the content of T can change.
Paul Bakker5121ce52009-01-03 21:22:43 +00001969 */
Gilles Peskinebdcb3962020-06-04 20:55:15 +02001970static 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 +02001971 const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00001972{
Paul Bakker23986e52011-04-24 08:57:21 +00001973 size_t i, n, m;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 mbedtls_mpi_uint u0, u1, *d;
Paul Bakker5121ce52009-01-03 21:22:43 +00001975
1976 memset( T->p, 0, T->n * ciL );
1977
1978 d = T->p;
1979 n = N->n;
1980 m = ( B->n < n ) ? B->n : n;
1981
1982 for( i = 0; i < n; i++ )
1983 {
1984 /*
1985 * T = (T + u0*B + u1*N) / 2^biL
1986 */
1987 u0 = A->p[i];
1988 u1 = ( d[0] + u0 * B->p[0] ) * mm;
1989
1990 mpi_mul_hlp( m, B->p, d, u0 );
1991 mpi_mul_hlp( n, N->p, d, u1 );
1992
1993 *d++ = u0; d[n + 1] = 0;
1994 }
1995
Gilles Peskine635a3742020-06-08 22:37:50 +02001996 /* At this point, d is either the desired result or the desired result
1997 * plus N. We now potentially subtract N, avoiding leaking whether the
1998 * subtraction is performed through side channels. */
Paul Bakker5121ce52009-01-03 21:22:43 +00001999
Gilles Peskine635a3742020-06-08 22:37:50 +02002000 /* Copy the n least significant limbs of d to A, so that
2001 * A = d if d < N (recall that N has n limbs). */
2002 memcpy( A->p, d, n * ciL );
Gilles Peskinede719d52020-06-09 10:39:38 +02002003 /* If d >= N then we want to set A to d - N. To prevent timing attacks,
Gilles Peskine635a3742020-06-08 22:37:50 +02002004 * do the calculation without using conditional tests. */
2005 /* Set d to d0 + (2^biL)^n - N where d0 is the current value of d. */
Gilles Peskine8f672662020-06-04 21:05:24 +02002006 d[n] += 1;
Gilles Peskine36acd542020-06-08 21:58:22 +02002007 d[n] -= mpi_sub_hlp( n, d, N->p );
Gilles Peskine635a3742020-06-08 22:37:50 +02002008 /* If d0 < N then d < (2^biL)^n
2009 * so d[n] == 0 and we want to keep A as it is.
2010 * If d0 >= N then d >= (2^biL)^n, and d <= (2^biL)^n + N < 2 * (2^biL)^n
2011 * so d[n] == 1 and we want to set A to the result of the subtraction
2012 * which is d - (2^biL)^n, i.e. the n least significant limbs of d.
2013 * This exactly corresponds to a conditional assignment. */
2014 mpi_safe_cond_assign( n, A->p, d, (unsigned char) d[n] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002015}
2016
2017/*
2018 * Montgomery reduction: A = A * R^-1 mod N
Gilles Peskine3ce3ddf2020-06-04 15:00:49 +02002019 *
2020 * See mpi_montmul() regarding constraints and guarantees on the parameters.
Paul Bakker5121ce52009-01-03 21:22:43 +00002021 */
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002022static void mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N,
2023 mbedtls_mpi_uint mm, const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00002024{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 mbedtls_mpi_uint z = 1;
2026 mbedtls_mpi U;
Paul Bakker5121ce52009-01-03 21:22:43 +00002027
Paul Bakker8ddb6452013-02-27 14:56:33 +01002028 U.n = U.s = (int) z;
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 U.p = &z;
2030
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002031 mpi_montmul( A, &U, N, mm, T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002032}
2033
Manuel Pégourié-Gonnard432ebba2021-06-03 10:42:46 +02002034/*
2035 * Constant-flow boolean "equal" comparison:
2036 * return x == y
2037 *
2038 * This function can be used to write constant-time code by replacing branches
2039 * with bit operations - it can be used in conjunction with
2040 * mbedtls_ssl_cf_mask_from_bit().
2041 *
2042 * This function is implemented without using comparison operators, as those
2043 * might be translated to branches by some compilers on some platforms.
2044 */
2045static size_t mbedtls_mpi_cf_bool_eq( size_t x, size_t y )
2046{
2047 /* diff = 0 if x == y, non-zero otherwise */
2048 const size_t diff = x ^ y;
2049
2050 /* MSVC has a warning about unary minus on unsigned integer types,
2051 * but this is well-defined and precisely what we want to do here. */
2052#if defined(_MSC_VER)
2053#pragma warning( push )
2054#pragma warning( disable : 4146 )
2055#endif
2056
2057 /* diff_msb's most significant bit is equal to x != y */
2058 const size_t diff_msb = ( diff | -diff );
2059
2060#if defined(_MSC_VER)
2061#pragma warning( pop )
2062#endif
2063
2064 /* diff1 = (x != y) ? 1 : 0 */
2065 const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 );
2066
2067 return( 1 ^ diff1 );
2068}
2069
Manuel Pégourié-Gonnard87bd4442021-03-09 11:22:20 +01002070/**
2071 * Select an MPI from a table without leaking the index.
2072 *
2073 * This is functionally equivalent to mbedtls_mpi_copy(R, T[idx]) except it
2074 * reads the entire table in order to avoid leaking the value of idx to an
2075 * attacker able to observe memory access patterns.
2076 *
2077 * \param[out] R Where to write the selected MPI.
2078 * \param[in] T The table to read from.
2079 * \param[in] T_size The number of elements in the table.
2080 * \param[in] idx The index of the element to select;
2081 * this must satisfy 0 <= idx < T_size.
2082 *
2083 * \return \c 0 on success, or a negative error code.
2084 */
2085static int mpi_select( mbedtls_mpi *R, const mbedtls_mpi *T, size_t T_size, size_t idx )
2086{
2087 int ret;
2088
2089 for( size_t i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard432ebba2021-06-03 10:42:46 +02002090 {
2091 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( R, &T[i],
2092 mbedtls_mpi_cf_bool_eq( i, idx ) ) );
2093 }
Manuel Pégourié-Gonnard87bd4442021-03-09 11:22:20 +01002094
2095cleanup:
2096 return( ret );
2097}
2098
Paul Bakker5121ce52009-01-03 21:22:43 +00002099/*
2100 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
2101 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002102int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
2103 const mbedtls_mpi *E, const mbedtls_mpi *N,
2104 mbedtls_mpi *_RR )
Paul Bakker5121ce52009-01-03 21:22:43 +00002105{
Paul Bakker23986e52011-04-24 08:57:21 +00002106 int ret;
2107 size_t wbits, wsize, one = 1;
2108 size_t i, j, nblimbs;
2109 size_t bufsize, nbits;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 mbedtls_mpi_uint ei, mm, state;
Manuel Pégourié-Gonnard87bd4442021-03-09 11:22:20 +01002111 mbedtls_mpi RR, T, W[ 1 << MBEDTLS_MPI_WINDOW_SIZE ], WW, Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00002112 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002113
Hanno Becker73d7d792018-12-11 10:35:51 +00002114 MPI_VALIDATE_RET( X != NULL );
2115 MPI_VALIDATE_RET( A != NULL );
2116 MPI_VALIDATE_RET( E != NULL );
2117 MPI_VALIDATE_RET( N != NULL );
2118
Hanno Becker8d1dd1b2017-09-28 11:02:24 +01002119 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 || ( N->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 if( mbedtls_mpi_cmp_int( E, 0 ) < 0 )
2123 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002124
Chris Jonesad59a2a2020-11-25 15:12:39 +00002125 if( mbedtls_mpi_bitlen( E ) > MBEDTLS_MPI_MAX_BITS ||
2126 mbedtls_mpi_bitlen( N ) > MBEDTLS_MPI_MAX_BITS )
2127 return ( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2128
Paul Bakkerf6198c12012-05-16 08:02:29 +00002129 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 * Init temps and window size
2131 */
2132 mpi_montg_init( &mm, N );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &T );
2134 mbedtls_mpi_init( &Apos );
Manuel Pégourié-Gonnard87bd4442021-03-09 11:22:20 +01002135 mbedtls_mpi_init( &WW );
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 memset( W, 0, sizeof( W ) );
2137
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002138 i = mbedtls_mpi_bitlen( E );
Paul Bakker5121ce52009-01-03 21:22:43 +00002139
2140 wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
2141 ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1;
2142
Peter Kolbusb83d41d2018-12-11 14:01:44 -06002143#if( MBEDTLS_MPI_WINDOW_SIZE < 6 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 if( wsize > MBEDTLS_MPI_WINDOW_SIZE )
2145 wsize = MBEDTLS_MPI_WINDOW_SIZE;
Peter Kolbusb83d41d2018-12-11 14:01:44 -06002146#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00002147
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 j = N->n + 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
2150 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], j ) );
2151 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T, j * 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002152
2153 /*
Paul Bakker50546922012-05-19 08:40:49 +00002154 * Compensate for negative A (and correct at the end)
2155 */
2156 neg = ( A->s == -1 );
Paul Bakker50546922012-05-19 08:40:49 +00002157 if( neg )
2158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Apos, A ) );
Paul Bakker50546922012-05-19 08:40:49 +00002160 Apos.s = 1;
2161 A = &Apos;
2162 }
2163
2164 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002165 * If 1st call, pre-compute R^2 mod N
2166 */
2167 if( _RR == NULL || _RR->p == NULL )
2168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &RR, 1 ) );
2170 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &RR, N->n * 2 * biL ) );
2171 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &RR, &RR, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002172
2173 if( _RR != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 memcpy( _RR, &RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002175 }
2176 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 memcpy( &RR, _RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002178
2179 /*
2180 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
2181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 if( mbedtls_mpi_cmp_mpi( A, N ) >= 0 )
2183 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &W[1], A, N ) );
Paul Bakkerc2024f42014-01-23 20:38:35 +01002184 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[1], A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002186
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002187 mpi_montmul( &W[1], &RR, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002188
2189 /*
2190 * X = R^2 * R^-1 mod N = R mod N
2191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &RR ) );
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002193 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002194
2195 if( wsize > 1 )
2196 {
2197 /*
2198 * W[1 << (wsize - 1)] = W[1] ^ (wsize - 1)
2199 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002200 j = one << ( wsize - 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[j], N->n + 1 ) );
2203 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[j], &W[1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002204
2205 for( i = 0; i < wsize - 1; i++ )
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002206 mpi_montmul( &W[j], &W[j], N, mm, &T );
Paul Bakker0d7702c2013-10-29 16:18:35 +01002207
Paul Bakker5121ce52009-01-03 21:22:43 +00002208 /*
2209 * W[i] = W[i - 1] * W[1]
2210 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002211 for( i = j + 1; i < ( one << wsize ); i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[i], N->n + 1 ) );
2214 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[i], &W[i - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002215
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002216 mpi_montmul( &W[i], &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002217 }
2218 }
2219
2220 nblimbs = E->n;
2221 bufsize = 0;
2222 nbits = 0;
2223 wbits = 0;
2224 state = 0;
2225
2226 while( 1 )
2227 {
2228 if( bufsize == 0 )
2229 {
Paul Bakker0d7702c2013-10-29 16:18:35 +01002230 if( nblimbs == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002231 break;
2232
Paul Bakker0d7702c2013-10-29 16:18:35 +01002233 nblimbs--;
2234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 bufsize = sizeof( mbedtls_mpi_uint ) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 }
2237
2238 bufsize--;
2239
2240 ei = (E->p[nblimbs] >> bufsize) & 1;
2241
2242 /*
2243 * skip leading 0s
2244 */
2245 if( ei == 0 && state == 0 )
2246 continue;
2247
2248 if( ei == 0 && state == 1 )
2249 {
2250 /*
2251 * out of window, square X
2252 */
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002253 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002254 continue;
2255 }
2256
2257 /*
2258 * add ei to current window
2259 */
2260 state = 2;
2261
2262 nbits++;
Paul Bakker66d5d072014-06-17 16:39:18 +02002263 wbits |= ( ei << ( wsize - nbits ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002264
2265 if( nbits == wsize )
2266 {
2267 /*
2268 * X = X^wsize R^-1 mod N
2269 */
2270 for( i = 0; i < wsize; i++ )
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002271 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002272
2273 /*
2274 * X = X * W[wbits] R^-1 mod N
2275 */
Manuel Pégourié-Gonnard87bd4442021-03-09 11:22:20 +01002276 MBEDTLS_MPI_CHK( mpi_select( &WW, W, 1 << wsize, wbits ) );
2277 mpi_montmul( X, &WW, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002278
2279 state--;
2280 nbits = 0;
2281 wbits = 0;
2282 }
2283 }
2284
2285 /*
2286 * process the remaining bits
2287 */
2288 for( i = 0; i < nbits; i++ )
2289 {
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002290 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002291
2292 wbits <<= 1;
2293
Paul Bakker66d5d072014-06-17 16:39:18 +02002294 if( ( wbits & ( one << wsize ) ) != 0 )
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002295 mpi_montmul( X, &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002296 }
2297
2298 /*
2299 * X = A^E * R * R^-1 mod N = A^E mod N
2300 */
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002301 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002302
Hanno Beckera4af1c42017-04-18 09:07:45 +01002303 if( neg && E->n != 0 && ( E->p[0] & 1 ) != 0 )
Paul Bakkerf6198c12012-05-16 08:02:29 +00002304 {
2305 X->s = -1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, N, X ) );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002307 }
2308
Paul Bakker5121ce52009-01-03 21:22:43 +00002309cleanup:
2310
Paul Bakker66d5d072014-06-17 16:39:18 +02002311 for( i = ( one << ( wsize - 1 ) ); i < ( one << wsize ); i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 mbedtls_mpi_free( &W[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 mbedtls_mpi_free( &W[1] ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &Apos );
Manuel Pégourié-Gonnard87bd4442021-03-09 11:22:20 +01002315 mbedtls_mpi_free( &WW );
Paul Bakker6c591fa2011-05-05 11:49:20 +00002316
Paul Bakker75a28602014-03-31 12:08:17 +02002317 if( _RR == NULL || _RR->p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002319
2320 return( ret );
2321}
2322
Paul Bakker5121ce52009-01-03 21:22:43 +00002323/*
2324 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
2325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00002327{
Paul Bakker23986e52011-04-24 08:57:21 +00002328 int ret;
2329 size_t lz, lzt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 mbedtls_mpi TG, TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00002331
Hanno Becker73d7d792018-12-11 10:35:51 +00002332 MPI_VALIDATE_RET( G != NULL );
2333 MPI_VALIDATE_RET( A != NULL );
2334 MPI_VALIDATE_RET( B != NULL );
2335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336 mbedtls_mpi_init( &TG ); mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) );
2339 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 lz = mbedtls_mpi_lsb( &TA );
2342 lzt = mbedtls_mpi_lsb( &TB );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002343
Paul Bakker66d5d072014-06-17 16:39:18 +02002344 if( lzt < lz )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002345 lz = lzt;
2346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, lz ) );
2348 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, lz ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002349
Paul Bakker5121ce52009-01-03 21:22:43 +00002350 TA.s = TB.s = 1;
2351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352 while( mbedtls_mpi_cmp_int( &TA, 0 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, mbedtls_mpi_lsb( &TA ) ) );
2355 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, mbedtls_mpi_lsb( &TB ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 if( mbedtls_mpi_cmp_mpi( &TA, &TB ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TA, &TA, &TB ) );
2360 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002361 }
2362 else
2363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TB, &TB, &TA ) );
2365 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002366 }
2367 }
2368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &TB, lz ) );
2370 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( G, &TB ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002371
2372cleanup:
2373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374 mbedtls_mpi_free( &TG ); mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002375
2376 return( ret );
2377}
2378
Paul Bakker33dc46b2014-04-30 16:11:39 +02002379/*
2380 * Fill X with size bytes of random.
2381 *
2382 * Use a temporary bytes representation to make sure the result is the same
Paul Bakkerc37b0ac2014-05-01 14:19:23 +02002383 * regardless of the platform endianness (useful when f_rng is actually
Paul Bakker33dc46b2014-04-30 16:11:39 +02002384 * deterministic, eg for tests).
2385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002387 int (*f_rng)(void *, unsigned char *, size_t),
2388 void *p_rng )
Paul Bakker287781a2011-03-26 13:18:49 +00002389{
Paul Bakker23986e52011-04-24 08:57:21 +00002390 int ret;
Hanno Becker6dab6202019-01-02 16:42:29 +00002391 size_t const limbs = CHARS_TO_LIMBS( size );
Hanno Beckerda1655a2017-10-18 14:21:44 +01002392 size_t const overhead = ( limbs * ciL ) - size;
2393 unsigned char *Xp;
2394
Hanno Becker8ce11a32018-12-19 16:18:52 +00002395 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002396 MPI_VALIDATE_RET( f_rng != NULL );
Paul Bakker33dc46b2014-04-30 16:11:39 +02002397
Hanno Beckerda1655a2017-10-18 14:21:44 +01002398 /* Ensure that target MPI has exactly the necessary number of limbs */
2399 if( X->n != limbs )
2400 {
2401 mbedtls_mpi_free( X );
2402 mbedtls_mpi_init( X );
2403 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, limbs ) );
2404 }
2405 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker287781a2011-03-26 13:18:49 +00002406
Hanno Beckerda1655a2017-10-18 14:21:44 +01002407 Xp = (unsigned char*) X->p;
Gilles Peskine05251142020-11-25 16:15:14 +01002408 MBEDTLS_MPI_CHK( f_rng( p_rng, Xp + overhead, size ) );
Hanno Beckerda1655a2017-10-18 14:21:44 +01002409
Hanno Becker2be8a552018-10-25 12:40:09 +01002410 mpi_bigendian_to_host( X->p, limbs );
Paul Bakker287781a2011-03-26 13:18:49 +00002411
2412cleanup:
2413 return( ret );
2414}
2415
Paul Bakker5121ce52009-01-03 21:22:43 +00002416/*
2417 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002420{
2421 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Hanno Becker73d7d792018-12-11 10:35:51 +00002423 MPI_VALIDATE_RET( X != NULL );
2424 MPI_VALIDATE_RET( A != NULL );
2425 MPI_VALIDATE_RET( N != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002426
Hanno Becker4bcb4912017-04-18 15:49:39 +01002427 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TU ); mbedtls_mpi_init( &U1 ); mbedtls_mpi_init( &U2 );
2431 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &TB ); mbedtls_mpi_init( &TV );
2432 mbedtls_mpi_init( &V1 ); mbedtls_mpi_init( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, A, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002439 goto cleanup;
2440 }
2441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &TA, A, N ) );
2443 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TU, &TA ) );
2444 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, N ) );
2445 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TV, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U1, 1 ) );
2448 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U2, 0 ) );
2449 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V1, 0 ) );
2450 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002451
2452 do
2453 {
2454 while( ( TU.p[0] & 1 ) == 0 )
2455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TU, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002457
2458 if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
2459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &U1, &U1, &TB ) );
2461 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 }
2463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U1, 1 ) );
2465 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002466 }
2467
2468 while( ( TV.p[0] & 1 ) == 0 )
2469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TV, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002471
2472 if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
2473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, &TB ) );
2475 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002476 }
2477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V1, 1 ) );
2479 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002480 }
2481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482 if( mbedtls_mpi_cmp_mpi( &TU, &TV ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TU, &TU, &TV ) );
2485 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U1, &U1, &V1 ) );
2486 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &V2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002487 }
2488 else
2489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TV, &TV, &TU ) );
2491 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, &U1 ) );
2492 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &U2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 }
2494 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 while( mbedtls_mpi_cmp_int( &TU, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 while( mbedtls_mpi_cmp_int( &V1, 0 ) < 0 )
2498 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500 while( mbedtls_mpi_cmp_mpi( &V1, N ) >= 0 )
2501 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &V1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002504
2505cleanup:
2506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TU ); mbedtls_mpi_free( &U1 ); mbedtls_mpi_free( &U2 );
2508 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TV );
2509 mbedtls_mpi_free( &V1 ); mbedtls_mpi_free( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002510
2511 return( ret );
2512}
2513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002515
Paul Bakker5121ce52009-01-03 21:22:43 +00002516static const int small_prime[] =
2517{
2518 3, 5, 7, 11, 13, 17, 19, 23,
2519 29, 31, 37, 41, 43, 47, 53, 59,
2520 61, 67, 71, 73, 79, 83, 89, 97,
2521 101, 103, 107, 109, 113, 127, 131, 137,
2522 139, 149, 151, 157, 163, 167, 173, 179,
2523 181, 191, 193, 197, 199, 211, 223, 227,
2524 229, 233, 239, 241, 251, 257, 263, 269,
2525 271, 277, 281, 283, 293, 307, 311, 313,
2526 317, 331, 337, 347, 349, 353, 359, 367,
2527 373, 379, 383, 389, 397, 401, 409, 419,
2528 421, 431, 433, 439, 443, 449, 457, 461,
2529 463, 467, 479, 487, 491, 499, 503, 509,
2530 521, 523, 541, 547, 557, 563, 569, 571,
2531 577, 587, 593, 599, 601, 607, 613, 617,
2532 619, 631, 641, 643, 647, 653, 659, 661,
2533 673, 677, 683, 691, 701, 709, 719, 727,
2534 733, 739, 743, 751, 757, 761, 769, 773,
2535 787, 797, 809, 811, 821, 823, 827, 829,
2536 839, 853, 857, 859, 863, 877, 881, 883,
2537 887, 907, 911, 919, 929, 937, 941, 947,
2538 953, 967, 971, 977, 983, 991, 997, -103
2539};
2540
2541/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002542 * Small divisors test (X must be positive)
2543 *
2544 * Return values:
2545 * 0: no small factor (possible prime, more tests needed)
2546 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002548 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550static int mpi_check_small_factors( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +00002551{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002552 int ret = 0;
2553 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554 mbedtls_mpi_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +00002555
Paul Bakker5121ce52009-01-03 21:22:43 +00002556 if( ( X->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002558
2559 for( i = 0; small_prime[i] > 0; i++ )
2560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561 if( mbedtls_mpi_cmp_int( X, small_prime[i] ) <= 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002562 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, small_prime[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002565
2566 if( r == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002568 }
2569
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002570cleanup:
2571 return( ret );
2572}
2573
2574/*
2575 * Miller-Rabin pseudo-primality test (HAC 4.24)
2576 */
Janos Follathda31fa12018-09-03 14:45:23 +01002577static int mpi_miller_rabin( const mbedtls_mpi *X, size_t rounds,
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002578 int (*f_rng)(void *, unsigned char *, size_t),
2579 void *p_rng )
2580{
Pascal Junodb99183d2015-03-11 16:49:45 +01002581 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002582 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002584
Hanno Becker8ce11a32018-12-19 16:18:52 +00002585 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002586 MPI_VALIDATE_RET( f_rng != NULL );
2587
2588 mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R );
2589 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590 mbedtls_mpi_init( &RR );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002591
Paul Bakker5121ce52009-01-03 21:22:43 +00002592 /*
2593 * W = |X| - 1
2594 * R = W >> lsb( W )
2595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &W, X, 1 ) );
2597 s = mbedtls_mpi_lsb( &W );
2598 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R, &W ) );
2599 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &R, s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002600
Janos Follathda31fa12018-09-03 14:45:23 +01002601 for( i = 0; i < rounds; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002602 {
2603 /*
2604 * pick a random A, 1 < A < |X| - 1
2605 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002606 count = 0;
2607 do {
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002608 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
Pascal Junodb99183d2015-03-11 16:49:45 +01002609
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002610 j = mbedtls_mpi_bitlen( &A );
2611 k = mbedtls_mpi_bitlen( &W );
Pascal Junodb99183d2015-03-11 16:49:45 +01002612 if (j > k) {
Darryl Greene3f95ed2018-10-02 13:21:35 +01002613 A.p[A.n - 1] &= ( (mbedtls_mpi_uint) 1 << ( k - ( A.n - 1 ) * biL - 1 ) ) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002614 }
2615
2616 if (count++ > 30) {
Jens Wiklanderdfd447e2019-01-17 13:30:57 +01002617 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2618 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002619 }
2620
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002621 } while ( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 ||
2622 mbedtls_mpi_cmp_int( &A, 1 ) <= 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002623
2624 /*
2625 * A = A^R mod |X|
2626 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &A, &A, &R, X, &RR ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 if( mbedtls_mpi_cmp_mpi( &A, &W ) == 0 ||
2630 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002631 continue;
2632
2633 j = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 while( j < s && mbedtls_mpi_cmp_mpi( &A, &W ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002635 {
2636 /*
2637 * A = A * A mod |X|
2638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &A, &A ) );
2640 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &A, &T, X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642 if( mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002643 break;
2644
2645 j++;
2646 }
2647
2648 /*
2649 * not prime if A != |X| - 1 or A == 1
2650 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651 if( mbedtls_mpi_cmp_mpi( &A, &W ) != 0 ||
2652 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 break;
2656 }
2657 }
2658
2659cleanup:
Hanno Becker73d7d792018-12-11 10:35:51 +00002660 mbedtls_mpi_free( &W ); mbedtls_mpi_free( &R );
2661 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
2664 return( ret );
2665}
2666
2667/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002668 * Pseudo-primality test: small factors, then Miller-Rabin
2669 */
Janos Follatha0b67c22018-09-18 14:48:23 +01002670int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds,
2671 int (*f_rng)(void *, unsigned char *, size_t),
2672 void *p_rng )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002673{
2674 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 mbedtls_mpi XX;
Hanno Becker8ce11a32018-12-19 16:18:52 +00002676 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002677 MPI_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002678
2679 XX.s = 1;
2680 XX.n = X->n;
2681 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683 if( mbedtls_mpi_cmp_int( &XX, 0 ) == 0 ||
2684 mbedtls_mpi_cmp_int( &XX, 1 ) == 0 )
2685 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 if( mbedtls_mpi_cmp_int( &XX, 2 ) == 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002688 return( 0 );
2689
2690 if( ( ret = mpi_check_small_factors( &XX ) ) != 0 )
2691 {
2692 if( ret == 1 )
2693 return( 0 );
2694
2695 return( ret );
2696 }
2697
Janos Follathda31fa12018-09-03 14:45:23 +01002698 return( mpi_miller_rabin( &XX, rounds, f_rng, p_rng ) );
Janos Follathf301d232018-08-14 13:34:01 +01002699}
2700
Janos Follatha0b67c22018-09-18 14:48:23 +01002701#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Janos Follathf301d232018-08-14 13:34:01 +01002702/*
2703 * Pseudo-primality test, error probability 2^-80
2704 */
2705int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
2706 int (*f_rng)(void *, unsigned char *, size_t),
2707 void *p_rng )
2708{
Hanno Becker8ce11a32018-12-19 16:18:52 +00002709 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002710 MPI_VALIDATE_RET( f_rng != NULL );
2711
Janos Follatha0b67c22018-09-18 14:48:23 +01002712 /*
2713 * In the past our key generation aimed for an error rate of at most
2714 * 2^-80. Since this function is deprecated, aim for the same certainty
2715 * here as well.
2716 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002717 return( mbedtls_mpi_is_prime_ext( X, 40, f_rng, p_rng ) );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002718}
Janos Follatha0b67c22018-09-18 14:48:23 +01002719#endif
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002720
2721/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002722 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002723 *
Janos Follathf301d232018-08-14 13:34:01 +01002724 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2725 * be either 1024 bits or 1536 bits long, and flags must contain
2726 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002727 */
Janos Follath7c025a92018-08-14 11:08:41 +01002728int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002729 int (*f_rng)(void *, unsigned char *, size_t),
2730 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002731{
Jethro Beekman66689272018-02-14 19:24:10 -08002732#ifdef MBEDTLS_HAVE_INT64
2733// ceil(2^63.5)
2734#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2735#else
2736// ceil(2^31.5)
2737#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2738#endif
2739 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002740 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002741 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742 mbedtls_mpi_uint r;
2743 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002744
Hanno Becker8ce11a32018-12-19 16:18:52 +00002745 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002746 MPI_VALIDATE_RET( f_rng != NULL );
2747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 if( nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS )
2749 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 mbedtls_mpi_init( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002752
2753 n = BITS_TO_LIMBS( nbits );
2754
Janos Follathda31fa12018-09-03 14:45:23 +01002755 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR ) == 0 )
2756 {
2757 /*
2758 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2759 */
2760 rounds = ( ( nbits >= 1300 ) ? 2 : ( nbits >= 850 ) ? 3 :
2761 ( nbits >= 650 ) ? 4 : ( nbits >= 350 ) ? 8 :
2762 ( nbits >= 250 ) ? 12 : ( nbits >= 150 ) ? 18 : 27 );
2763 }
2764 else
2765 {
2766 /*
2767 * 2^-100 error probability, number of rounds computed based on HAC,
2768 * fact 4.48
2769 */
2770 rounds = ( ( nbits >= 1450 ) ? 4 : ( nbits >= 1150 ) ? 5 :
2771 ( nbits >= 1000 ) ? 6 : ( nbits >= 850 ) ? 7 :
2772 ( nbits >= 750 ) ? 8 : ( nbits >= 500 ) ? 13 :
2773 ( nbits >= 250 ) ? 28 : ( nbits >= 150 ) ? 40 : 51 );
2774 }
2775
Jethro Beekman66689272018-02-14 19:24:10 -08002776 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002777 {
Jethro Beekman66689272018-02-14 19:24:10 -08002778 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
2779 /* make sure generated number is at least (nbits-1)+0.5 bits (FIPS 186-4 §B.3.3 steps 4.4, 5.5) */
2780 if( X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2 ) continue;
2781
2782 k = n * biL;
2783 if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits ) );
2784 X->p[0] |= 1;
2785
Janos Follath7c025a92018-08-14 11:08:41 +01002786 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002787 {
Janos Follatha0b67c22018-09-18 14:48:23 +01002788 ret = mbedtls_mpi_is_prime_ext( X, rounds, f_rng, p_rng );
Jethro Beekman66689272018-02-14 19:24:10 -08002789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002791 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002792 }
Jethro Beekman66689272018-02-14 19:24:10 -08002793 else
Paul Bakker5121ce52009-01-03 21:22:43 +00002794 {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002795 /*
Jethro Beekman66689272018-02-14 19:24:10 -08002796 * An necessary condition for Y and X = 2Y + 1 to be prime
2797 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2798 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002799 */
Jethro Beekman66689272018-02-14 19:24:10 -08002800
2801 X->p[0] |= 2;
2802
2803 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, 3 ) );
2804 if( r == 0 )
2805 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 8 ) );
2806 else if( r == 1 )
2807 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 4 ) );
2808
2809 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
2810 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, X ) );
2811 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, 1 ) );
2812
2813 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002814 {
Jethro Beekman66689272018-02-14 19:24:10 -08002815 /*
2816 * First, check small factors for X and Y
2817 * before doing Miller-Rabin on any of them
2818 */
2819 if( ( ret = mpi_check_small_factors( X ) ) == 0 &&
2820 ( ret = mpi_check_small_factors( &Y ) ) == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002821 ( ret = mpi_miller_rabin( X, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002822 == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002823 ( ret = mpi_miller_rabin( &Y, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002824 == 0 )
Jethro Beekman66689272018-02-14 19:24:10 -08002825 goto cleanup;
2826
2827 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
2828 goto cleanup;
2829
2830 /*
2831 * Next candidates. We want to preserve Y = (X-1) / 2 and
2832 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2833 * so up Y by 6 and X by 12.
2834 */
2835 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 12 ) );
2836 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &Y, &Y, 6 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002837 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002838 }
2839 }
2840
2841cleanup:
2842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843 mbedtls_mpi_free( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002844
2845 return( ret );
2846}
2847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002851
Paul Bakker23986e52011-04-24 08:57:21 +00002852#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002853
2854static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2855{
2856 { 693, 609, 21 },
2857 { 1764, 868, 28 },
2858 { 768454923, 542167814, 1 }
2859};
2860
Paul Bakker5121ce52009-01-03 21:22:43 +00002861/*
2862 * Checkup routine
2863 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002864int mbedtls_mpi_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002865{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002866 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00002868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); mbedtls_mpi_init( &X );
2870 mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &U ); mbedtls_mpi_init( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00002871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002872 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &A, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002873 "EFE021C2645FD1DC586E69184AF4A31E" \
2874 "D5F53E93B5F123FA41680867BA110131" \
2875 "944FE7952E2517337780CB0DB80E61AA" \
2876 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6" ) );
2877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002879 "B2E7EFD37075B9F03FF989C7C5051C20" \
2880 "34D2A323810251127E7BF8625A4F49A5" \
2881 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
2882 "5B5C25763222FEFCCFC38B832366C29E" ) );
2883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002884 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &N, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002885 "0066A198186C18C10B2F5ED9B522752A" \
2886 "9830B69916E535C8F047518A889A43A5" \
2887 "94B6BED27A168D31D4A52F88925AA8F5" ) );
2888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002892 "602AB7ECA597A3D6B56FF9829A5E8B85" \
2893 "9E857EA95A03512E2BAE7391688D264A" \
2894 "A5663B0341DB9CCFD2C4C5F421FEC814" \
2895 "8001B72E848A38CAE1C65F78E56ABDEF" \
2896 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
2897 "ECF677152EF804370C1A305CAF3B5BF1" \
2898 "30879B56C61DE584A0F53A2447A51E" ) );
2899
2900 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 mbedtls_printf( " MPI test #1 (mul_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002904 {
2905 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002907
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002908 ret = 1;
2909 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002910 }
2911
2912 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &X, &Y, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002918 "256567336059E52CAE22925474705F39A94" ) );
2919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &V, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002921 "6613F26162223DF488E9CD48CC132C7A" \
2922 "0AC93C701B001B092E4E5B9F73BCD27B" \
2923 "9EE50D0657C77F374E903CDFA4C642" ) );
2924
2925 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 mbedtls_printf( " MPI test #2 (div_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ||
2929 mbedtls_mpi_cmp_mpi( &Y, &V ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002930 {
2931 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002933
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002934 ret = 1;
2935 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002936 }
2937
2938 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002941 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &X, &A, &E, &N, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002944 "36E139AEA55215609D2816998ED020BB" \
2945 "BD96C37890F65171D948E9BC7CBAA4D9" \
2946 "325D24D6A3C12710F10A09FA08AB87" ) );
2947
2948 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949 mbedtls_printf( " MPI test #3 (exp_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002952 {
2953 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002955
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002956 ret = 1;
2957 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002958 }
2959
2960 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002966 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
2967 "C3DBA76456363A10869622EAC2DD84EC" \
2968 "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) );
2969
2970 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971 mbedtls_printf( " MPI test #4 (inv_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002974 {
2975 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002977
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002978 ret = 1;
2979 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002980 }
2981
2982 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002984
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002985 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986 mbedtls_printf( " MPI test #5 (simple gcd): " );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002987
Paul Bakker66d5d072014-06-17 16:39:18 +02002988 for( i = 0; i < GCD_PAIR_COUNT; i++ )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &X, gcd_pairs[i][0] ) );
2991 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Y, gcd_pairs[i][1] ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &A, &X, &Y ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995 if( mbedtls_mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 )
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002996 {
2997 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998 mbedtls_printf( "failed at %d\n", i );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002999
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003000 ret = 1;
3001 goto cleanup;
3002 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003003 }
3004
3005 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006 mbedtls_printf( "passed\n" );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003007
Paul Bakker5121ce52009-01-03 21:22:43 +00003008cleanup:
3009
3010 if( ret != 0 && verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011 mbedtls_printf( "Unexpected error, return code = %08X\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &X );
3014 mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &U ); mbedtls_mpi_free( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00003015
3016 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003018
3019 return( ret );
3020}
3021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024#endif /* MBEDTLS_BIGNUM_C */