blob: 7609fa491b76349fdf192a9842ab1b39baf2596c [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;
Manuel Pégourié-Gonnard245a8062021-05-31 11:48:45 +0200282
283 /* MSVC has a warning about unary minus on unsigned integer types,
284 * but this is well-defined and precisely what we want to do here. */
285#if defined(_MSC_VER)
286#pragma warning( push )
287#pragma warning( disable : 4146 )
288#endif
289
290 /* all-bits 1 if assign is 1, all-bits 0 if assign is 0 */
291 const mbedtls_mpi_uint mask = -assign;
292
293#if defined(_MSC_VER)
294#pragma warning( pop )
295#endif
296
Gilles Peskinec81c5882020-06-04 19:14:58 +0200297 for( i = 0; i < n; i++ )
Manuel Pégourié-Gonnard245a8062021-05-31 11:48:45 +0200298 dest[i] = ( src[i] & mask ) | ( dest[i] & ~mask );
Gilles Peskinec81c5882020-06-04 19:14:58 +0200299}
300
301/*
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100302 * Conditionally assign X = Y, without leaking information
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +0100303 * about whether the assignment was made or not.
304 * (Leaking information about the respective sizes of X and Y is ok however.)
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100307{
308 int ret = 0;
309 size_t i;
Manuel Pégourié-Gonnard245a8062021-05-31 11:48:45 +0200310 unsigned int mask;
311 mbedtls_mpi_uint limb_mask;
Hanno Becker73d7d792018-12-11 10:35:51 +0000312 MPI_VALIDATE_RET( X != NULL );
313 MPI_VALIDATE_RET( Y != NULL );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100314
Manuel Pégourié-Gonnard245a8062021-05-31 11:48:45 +0200315 /* MSVC has a warning about unary minus on unsigned integer types,
316 * but this is well-defined and precisely what we want to do here. */
317#if defined(_MSC_VER)
318#pragma warning( push )
319#pragma warning( disable : 4146 )
320#endif
321
Pascal Junodb99183d2015-03-11 16:49:45 +0100322 /* make sure assign is 0 or 1 in a time-constant manner */
323 assign = (assign | (unsigned char)-assign) >> 7;
Manuel Pégourié-Gonnard245a8062021-05-31 11:48:45 +0200324 /* all-bits 1 if assign is 1, all-bits 0 if assign is 0 */
325 mask = -assign;
326 limb_mask = -assign;
327
328#if defined(_MSC_VER)
329#pragma warning( pop )
330#endif
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100333
Manuel Pégourié-Gonnard245a8062021-05-31 11:48:45 +0200334 X->s = ( X->s & ~mask ) | ( Y->s & mask );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100335
Gilles Peskinec81c5882020-06-04 19:14:58 +0200336 mpi_safe_cond_assign( Y->n, X->p, Y->p, assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100337
Gilles Peskinec81c5882020-06-04 19:14:58 +0200338 for( i = Y->n; i < X->n; i++ )
Manuel Pégourié-Gonnard245a8062021-05-31 11:48:45 +0200339 X->p[i] &= ~limb_mask;
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100340
341cleanup:
342 return( ret );
343}
344
345/*
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100346 * Conditionally swap X and Y, without leaking information
347 * about whether the swap was made or not.
348 * Here it is not ok to simply swap the pointers, which whould lead to
349 * different memory access patterns when X and Y are used afterwards.
350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100352{
353 int ret, s;
354 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 mbedtls_mpi_uint tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +0000356 MPI_VALIDATE_RET( X != NULL );
357 MPI_VALIDATE_RET( Y != NULL );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100358
359 if( X == Y )
360 return( 0 );
361
Pascal Junodb99183d2015-03-11 16:49:45 +0100362 /* make sure swap is 0 or 1 in a time-constant manner */
363 swap = (swap | (unsigned char)-swap) >> 7;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
366 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( Y, X->n ) );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100367
368 s = X->s;
Paul Bakker66d5d072014-06-17 16:39:18 +0200369 X->s = X->s * ( 1 - swap ) + Y->s * swap;
370 Y->s = Y->s * ( 1 - swap ) + s * swap;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100371
372
373 for( i = 0; i < X->n; i++ )
374 {
375 tmp = X->p[i];
Paul Bakker66d5d072014-06-17 16:39:18 +0200376 X->p[i] = X->p[i] * ( 1 - swap ) + Y->p[i] * swap;
377 Y->p[i] = Y->p[i] * ( 1 - swap ) + tmp * swap;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100378 }
379
380cleanup:
381 return( ret );
382}
383
384/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 * Set value from integer
386 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +0000388{
389 int ret;
Hanno Becker73d7d792018-12-11 10:35:51 +0000390 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000393 memset( X->p, 0, X->n * ciL );
394
395 X->p[0] = ( z < 0 ) ? -z : z;
396 X->s = ( z < 0 ) ? -1 : 1;
397
398cleanup:
399
400 return( ret );
401}
402
403/*
Paul Bakker2f5947e2011-05-18 15:47:11 +0000404 * Get a specific bit
405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000407{
Hanno Becker73d7d792018-12-11 10:35:51 +0000408 MPI_VALIDATE_RET( X != NULL );
409
Paul Bakker2f5947e2011-05-18 15:47:11 +0000410 if( X->n * biL <= pos )
411 return( 0 );
412
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200413 return( ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000414}
415
Gilles Peskine11cdb052018-11-20 16:47:47 +0100416/* Get a specific byte, without range checks. */
417#define GET_BYTE( X, i ) \
418 ( ( ( X )->p[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff )
419
Paul Bakker2f5947e2011-05-18 15:47:11 +0000420/*
421 * Set a bit to a specific value of 0 or 1
422 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000424{
425 int ret = 0;
426 size_t off = pos / biL;
427 size_t idx = pos % biL;
Hanno Becker73d7d792018-12-11 10:35:51 +0000428 MPI_VALIDATE_RET( X != NULL );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000429
430 if( val != 0 && val != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker9af723c2014-05-01 13:03:14 +0200432
Paul Bakker2f5947e2011-05-18 15:47:11 +0000433 if( X->n * biL <= pos )
434 {
435 if( val == 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200436 return( 0 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, off + 1 ) );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000439 }
440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 X->p[off] &= ~( (mbedtls_mpi_uint) 0x01 << idx );
442 X->p[off] |= (mbedtls_mpi_uint) val << idx;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000443
444cleanup:
Paul Bakker9af723c2014-05-01 13:03:14 +0200445
Paul Bakker2f5947e2011-05-18 15:47:11 +0000446 return( ret );
447}
448
449/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200450 * Return the number of less significant zero-bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452size_t mbedtls_mpi_lsb( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000453{
Paul Bakker23986e52011-04-24 08:57:21 +0000454 size_t i, j, count = 0;
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000455 MBEDTLS_INTERNAL_VALIDATE_RET( X != NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000456
457 for( i = 0; i < X->n; i++ )
Paul Bakkerf9688572011-05-05 10:00:45 +0000458 for( j = 0; j < biL; j++, count++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 if( ( ( X->p[i] >> j ) & 1 ) != 0 )
460 return( count );
461
462 return( 0 );
463}
464
465/*
Simon Butcher15b15d12015-11-26 19:35:03 +0000466 * Count leading zero bits in a given integer
467 */
468static size_t mbedtls_clz( const mbedtls_mpi_uint x )
469{
470 size_t j;
Manuel Pégourié-Gonnarde3e8edf2015-12-01 09:31:52 +0100471 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
Simon Butcher15b15d12015-11-26 19:35:03 +0000472
473 for( j = 0; j < biL; j++ )
474 {
475 if( x & mask ) break;
476
477 mask >>= 1;
478 }
479
480 return j;
481}
482
483/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200484 * Return the number of bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000485 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200486size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000487{
Paul Bakker23986e52011-04-24 08:57:21 +0000488 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200490 if( X->n == 0 )
491 return( 0 );
492
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 for( i = X->n - 1; i > 0; i-- )
494 if( X->p[i] != 0 )
495 break;
496
Simon Butcher15b15d12015-11-26 19:35:03 +0000497 j = biL - mbedtls_clz( X->p[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000498
Paul Bakker23986e52011-04-24 08:57:21 +0000499 return( ( i * biL ) + j );
Paul Bakker5121ce52009-01-03 21:22:43 +0000500}
501
502/*
503 * Return the total size in bytes
504 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505size_t mbedtls_mpi_size( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000506{
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200507 return( ( mbedtls_mpi_bitlen( X ) + 7 ) >> 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000508}
509
510/*
511 * Convert an ASCII character to digit value
512 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513static int mpi_get_digit( mbedtls_mpi_uint *d, int radix, char c )
Paul Bakker5121ce52009-01-03 21:22:43 +0000514{
515 *d = 255;
516
517 if( c >= 0x30 && c <= 0x39 ) *d = c - 0x30;
518 if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37;
519 if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57;
520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 if( *d >= (mbedtls_mpi_uint) radix )
522 return( MBEDTLS_ERR_MPI_INVALID_CHARACTER );
Paul Bakker5121ce52009-01-03 21:22:43 +0000523
524 return( 0 );
525}
526
527/*
528 * Import from an ASCII string
529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000531{
Paul Bakker23986e52011-04-24 08:57:21 +0000532 int ret;
533 size_t i, j, slen, n;
Gilles Peskine984fd072021-04-03 18:26:13 +0200534 int sign = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_mpi_uint d;
536 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000537 MPI_VALIDATE_RET( X != NULL );
538 MPI_VALIDATE_RET( s != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
540 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000541 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000544
Gilles Peskine984fd072021-04-03 18:26:13 +0200545 if( s[0] == '-' )
546 {
547 ++s;
548 sign = -1;
549 }
550
Paul Bakkerff60ee62010-03-16 21:09:09 +0000551 slen = strlen( s );
552
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 if( radix == 16 )
554 {
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +0100555 if( slen > MPI_SIZE_T_MAX >> 2 )
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +0200556 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
557
Paul Bakkerff60ee62010-03-16 21:09:09 +0000558 n = BITS_TO_LIMBS( slen << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n ) );
561 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000562
Paul Bakker23986e52011-04-24 08:57:21 +0000563 for( i = slen, j = 0; i > 0; i--, j++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200566 X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 }
568 }
569 else
570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000572
Paul Bakkerff60ee62010-03-16 21:09:09 +0000573 for( i = 0; i < slen; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
576 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T, X, radix ) );
Gilles Peskine984fd072021-04-03 18:26:13 +0200577 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, &T, d ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 }
579 }
580
Gilles Peskine984fd072021-04-03 18:26:13 +0200581 if( sign < 0 && mbedtls_mpi_bitlen( X ) != 0 )
582 X->s = -1;
583
Paul Bakker5121ce52009-01-03 21:22:43 +0000584cleanup:
585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000587
588 return( ret );
589}
590
591/*
Ron Eldora16fa292018-11-20 14:07:01 +0200592 * Helper to write the digits high-order first.
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 */
Ron Eldora16fa292018-11-20 14:07:01 +0200594static int mpi_write_hlp( mbedtls_mpi *X, int radix,
595 char **p, const size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000596{
597 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 mbedtls_mpi_uint r;
Ron Eldora16fa292018-11-20 14:07:01 +0200599 size_t length = 0;
600 char *p_end = *p + buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
Ron Eldora16fa292018-11-20 14:07:01 +0200602 do
603 {
604 if( length >= buflen )
605 {
606 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
607 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000608
Ron Eldora16fa292018-11-20 14:07:01 +0200609 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) );
610 MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) );
611 /*
612 * Write the residue in the current position, as an ASCII character.
613 */
614 if( r < 0xA )
615 *(--p_end) = (char)( '0' + r );
616 else
617 *(--p_end) = (char)( 'A' + ( r - 0xA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000618
Ron Eldora16fa292018-11-20 14:07:01 +0200619 length++;
620 } while( mbedtls_mpi_cmp_int( X, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
Ron Eldora16fa292018-11-20 14:07:01 +0200622 memmove( *p, p_end, length );
623 *p += length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000624
625cleanup:
626
627 return( ret );
628}
629
630/*
631 * Export into an ASCII string
632 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100633int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
634 char *buf, size_t buflen, size_t *olen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000635{
Paul Bakker23986e52011-04-24 08:57:21 +0000636 int ret = 0;
637 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000638 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000640 MPI_VALIDATE_RET( X != NULL );
641 MPI_VALIDATE_RET( olen != NULL );
642 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000643
644 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000645 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
Hanno Beckerc1fa6cd2019-02-04 09:45:07 +0000647 n = mbedtls_mpi_bitlen( X ); /* Number of bits necessary to present `n`. */
648 if( radix >= 4 ) n >>= 1; /* Number of 4-adic digits necessary to present
649 * `n`. If radix > 4, this might be a strict
650 * overapproximation of the number of
651 * radix-adic digits needed to present `n`. */
652 if( radix >= 16 ) n >>= 1; /* Number of hexadecimal digits necessary to
653 * present `n`. */
654
Janos Follath870ed002019-03-06 13:43:02 +0000655 n += 1; /* Terminating null byte */
Hanno Beckerc1fa6cd2019-02-04 09:45:07 +0000656 n += 1; /* Compensate for the divisions above, which round down `n`
657 * in case it's not even. */
658 n += 1; /* Potential '-'-sign. */
659 n += ( n & 1 ); /* Make n even to have enough space for hexadecimal writing,
660 * which always uses an even number of hex-digits. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000661
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100662 if( buflen < n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100664 *olen = n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000666 }
667
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100668 p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000670
671 if( X->s == -1 )
Hanno Beckeraf97cae2019-02-01 16:41:30 +0000672 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000673 *p++ = '-';
Hanno Beckeraf97cae2019-02-01 16:41:30 +0000674 buflen--;
675 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000676
677 if( radix == 16 )
678 {
Paul Bakker23986e52011-04-24 08:57:21 +0000679 int c;
680 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000681
Paul Bakker23986e52011-04-24 08:57:21 +0000682 for( i = X->n, k = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 {
Paul Bakker23986e52011-04-24 08:57:21 +0000684 for( j = ciL; j > 0; j-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 {
Paul Bakker23986e52011-04-24 08:57:21 +0000686 c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000687
Paul Bakker6c343d72014-07-10 14:36:19 +0200688 if( c == 0 && k == 0 && ( i + j ) != 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000689 continue;
690
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000691 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000692 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000693 k = 1;
694 }
695 }
696 }
697 else
698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T, X ) );
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000700
701 if( T.s == -1 )
702 T.s = 1;
703
Ron Eldora16fa292018-11-20 14:07:01 +0200704 MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p, buflen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000705 }
706
707 *p++ = '\0';
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100708 *olen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000709
710cleanup:
711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000713
714 return( ret );
715}
716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000718/*
719 * Read X from an opened file
720 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin )
Paul Bakker5121ce52009-01-03 21:22:43 +0000722{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_mpi_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000724 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000725 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000726 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000727 * Buffer should have space for (short) label and decimal formatted MPI,
728 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000729 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Paul Bakker5121ce52009-01-03 21:22:43 +0000731
Hanno Becker73d7d792018-12-11 10:35:51 +0000732 MPI_VALIDATE_RET( X != NULL );
733 MPI_VALIDATE_RET( fin != NULL );
734
735 if( radix < 2 || radix > 16 )
736 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
737
Paul Bakker5121ce52009-01-03 21:22:43 +0000738 memset( s, 0, sizeof( s ) );
739 if( fgets( s, sizeof( s ) - 1, fin ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000741
742 slen = strlen( s );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000743 if( slen == sizeof( s ) - 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000745
Hanno Beckerb2034b72017-04-26 11:46:46 +0100746 if( slen > 0 && s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
747 if( slen > 0 && s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
Paul Bakker5121ce52009-01-03 21:22:43 +0000748
749 p = s + slen;
Hanno Beckerb2034b72017-04-26 11:46:46 +0100750 while( p-- > s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000751 if( mpi_get_digit( &d, radix, *p ) != 0 )
752 break;
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 return( mbedtls_mpi_read_string( X, radix, p + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000755}
756
757/*
758 * Write X into an opened file (or stdout if fout == NULL)
759 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout )
Paul Bakker5121ce52009-01-03 21:22:43 +0000761{
Paul Bakker23986e52011-04-24 08:57:21 +0000762 int ret;
763 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000764 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000765 * Buffer should have space for (short) label and decimal formatted MPI,
766 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Hanno Becker73d7d792018-12-11 10:35:51 +0000769 MPI_VALIDATE_RET( X != NULL );
770
771 if( radix < 2 || radix > 16 )
772 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000773
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100774 memset( s, 0, sizeof( s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000775
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100776 MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, sizeof( s ) - 2, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000777
778 if( p == NULL ) p = "";
779
780 plen = strlen( p );
781 slen = strlen( s );
782 s[slen++] = '\r';
783 s[slen++] = '\n';
784
785 if( fout != NULL )
786 {
787 if( fwrite( p, 1, plen, fout ) != plen ||
788 fwrite( s, 1, slen, fout ) != slen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000790 }
791 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 mbedtls_printf( "%s%s", p, s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000793
794cleanup:
795
796 return( ret );
797}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000799
Hanno Beckerda1655a2017-10-18 14:21:44 +0100800
801/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
802 * into the storage form used by mbedtls_mpi. */
Hanno Beckerf8720072018-11-08 11:53:49 +0000803
804static mbedtls_mpi_uint mpi_uint_bigendian_to_host_c( mbedtls_mpi_uint x )
805{
806 uint8_t i;
Hanno Becker92c98932019-05-01 17:09:11 +0100807 unsigned char *x_ptr;
Hanno Beckerf8720072018-11-08 11:53:49 +0000808 mbedtls_mpi_uint tmp = 0;
Hanno Becker92c98932019-05-01 17:09:11 +0100809
810 for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
811 {
812 tmp <<= CHAR_BIT;
813 tmp |= (mbedtls_mpi_uint) *x_ptr;
814 }
815
Hanno Beckerf8720072018-11-08 11:53:49 +0000816 return( tmp );
817}
818
819static mbedtls_mpi_uint mpi_uint_bigendian_to_host( mbedtls_mpi_uint x )
820{
821#if defined(__BYTE_ORDER__)
822
823/* Nothing to do on bigendian systems. */
824#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
825 return( x );
826#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
827
828#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
829
830/* For GCC and Clang, have builtins for byte swapping. */
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000831#if defined(__GNUC__) && defined(__GNUC_PREREQ)
832#if __GNUC_PREREQ(4,3)
Hanno Beckerf8720072018-11-08 11:53:49 +0000833#define have_bswap
834#endif
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000835#endif
836
837#if defined(__clang__) && defined(__has_builtin)
838#if __has_builtin(__builtin_bswap32) && \
839 __has_builtin(__builtin_bswap64)
840#define have_bswap
841#endif
842#endif
843
Hanno Beckerf8720072018-11-08 11:53:49 +0000844#if defined(have_bswap)
845 /* The compiler is hopefully able to statically evaluate this! */
846 switch( sizeof(mbedtls_mpi_uint) )
847 {
848 case 4:
849 return( __builtin_bswap32(x) );
850 case 8:
851 return( __builtin_bswap64(x) );
852 }
853#endif
854#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
855#endif /* __BYTE_ORDER__ */
856
857 /* Fall back to C-based reordering if we don't know the byte order
858 * or we couldn't use a compiler-specific builtin. */
859 return( mpi_uint_bigendian_to_host_c( x ) );
860}
861
Hanno Becker2be8a552018-10-25 12:40:09 +0100862static void mpi_bigendian_to_host( mbedtls_mpi_uint * const p, size_t limbs )
Hanno Beckerda1655a2017-10-18 14:21:44 +0100863{
Hanno Beckerda1655a2017-10-18 14:21:44 +0100864 mbedtls_mpi_uint *cur_limb_left;
865 mbedtls_mpi_uint *cur_limb_right;
Hanno Becker2be8a552018-10-25 12:40:09 +0100866 if( limbs == 0 )
867 return;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100868
869 /*
870 * Traverse limbs and
871 * - adapt byte-order in each limb
872 * - swap the limbs themselves.
873 * For that, simultaneously traverse the limbs from left to right
874 * and from right to left, as long as the left index is not bigger
875 * than the right index (it's not a problem if limbs is odd and the
876 * indices coincide in the last iteration).
877 */
Hanno Beckerda1655a2017-10-18 14:21:44 +0100878 for( cur_limb_left = p, cur_limb_right = p + ( limbs - 1 );
879 cur_limb_left <= cur_limb_right;
880 cur_limb_left++, cur_limb_right-- )
881 {
Hanno Beckerf8720072018-11-08 11:53:49 +0000882 mbedtls_mpi_uint tmp;
883 /* Note that if cur_limb_left == cur_limb_right,
884 * this code effectively swaps the bytes only once. */
885 tmp = mpi_uint_bigendian_to_host( *cur_limb_left );
886 *cur_limb_left = mpi_uint_bigendian_to_host( *cur_limb_right );
887 *cur_limb_right = tmp;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100888 }
Hanno Beckerda1655a2017-10-18 14:21:44 +0100889}
890
Paul Bakker5121ce52009-01-03 21:22:43 +0000891/*
892 * Import X from unsigned binary data, big endian
893 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000895{
Paul Bakker23986e52011-04-24 08:57:21 +0000896 int ret;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100897 size_t const limbs = CHARS_TO_LIMBS( buflen );
898 size_t const overhead = ( limbs * ciL ) - buflen;
899 unsigned char *Xp;
Paul Bakker5121ce52009-01-03 21:22:43 +0000900
Hanno Becker8ce11a32018-12-19 16:18:52 +0000901 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +0000902 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
903
Hanno Becker073c1992017-10-17 15:17:27 +0100904 /* Ensure that target MPI has exactly the necessary number of limbs */
905 if( X->n != limbs )
906 {
907 mbedtls_mpi_free( X );
908 mbedtls_mpi_init( X );
909 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, limbs ) );
910 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000912
Hanno Becker0e810b92019-01-03 17:13:11 +0000913 /* Avoid calling `memcpy` with NULL source argument,
914 * even if buflen is 0. */
915 if( buf != NULL )
916 {
917 Xp = (unsigned char*) X->p;
918 memcpy( Xp + overhead, buf, buflen );
Hanno Beckerda1655a2017-10-18 14:21:44 +0100919
Hanno Becker0e810b92019-01-03 17:13:11 +0000920 mpi_bigendian_to_host( X->p, limbs );
921 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000922
923cleanup:
924
925 return( ret );
926}
927
928/*
929 * Export X into unsigned binary data, big endian
930 */
Gilles Peskine11cdb052018-11-20 16:47:47 +0100931int mbedtls_mpi_write_binary( const mbedtls_mpi *X,
932 unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000933{
Hanno Becker73d7d792018-12-11 10:35:51 +0000934 size_t stored_bytes;
Gilles Peskine11cdb052018-11-20 16:47:47 +0100935 size_t bytes_to_copy;
936 unsigned char *p;
937 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000938
Hanno Becker73d7d792018-12-11 10:35:51 +0000939 MPI_VALIDATE_RET( X != NULL );
940 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
941
942 stored_bytes = X->n * ciL;
943
Gilles Peskine11cdb052018-11-20 16:47:47 +0100944 if( stored_bytes < buflen )
945 {
946 /* There is enough space in the output buffer. Write initial
947 * null bytes and record the position at which to start
948 * writing the significant bytes. In this case, the execution
949 * trace of this function does not depend on the value of the
950 * number. */
951 bytes_to_copy = stored_bytes;
952 p = buf + buflen - stored_bytes;
953 memset( buf, 0, buflen - stored_bytes );
954 }
955 else
956 {
957 /* The output buffer is smaller than the allocated size of X.
958 * However X may fit if its leading bytes are zero. */
959 bytes_to_copy = buflen;
960 p = buf;
961 for( i = bytes_to_copy; i < stored_bytes; i++ )
962 {
963 if( GET_BYTE( X, i ) != 0 )
964 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
965 }
966 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000967
Gilles Peskine11cdb052018-11-20 16:47:47 +0100968 for( i = 0; i < bytes_to_copy; i++ )
969 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000970
971 return( 0 );
972}
973
974/*
975 * Left-shift: X <<= count
976 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +0000978{
Paul Bakker23986e52011-04-24 08:57:21 +0000979 int ret;
980 size_t i, v0, t1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +0000982 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000983
984 v0 = count / (biL );
985 t1 = count & (biL - 1);
986
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200987 i = mbedtls_mpi_bitlen( X ) + count;
Paul Bakker5121ce52009-01-03 21:22:43 +0000988
Paul Bakkerf9688572011-05-05 10:00:45 +0000989 if( X->n * biL < i )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000991
992 ret = 0;
993
994 /*
995 * shift by count / limb_size
996 */
997 if( v0 > 0 )
998 {
Paul Bakker23986e52011-04-24 08:57:21 +0000999 for( i = X->n; i > v0; i-- )
1000 X->p[i - 1] = X->p[i - v0 - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
Paul Bakker23986e52011-04-24 08:57:21 +00001002 for( ; i > 0; i-- )
1003 X->p[i - 1] = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001004 }
1005
1006 /*
1007 * shift by count % limb_size
1008 */
1009 if( t1 > 0 )
1010 {
1011 for( i = v0; i < X->n; i++ )
1012 {
1013 r1 = X->p[i] >> (biL - t1);
1014 X->p[i] <<= t1;
1015 X->p[i] |= r0;
1016 r0 = r1;
1017 }
1018 }
1019
1020cleanup:
1021
1022 return( ret );
1023}
1024
1025/*
1026 * Right-shift: X >>= count
1027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +00001029{
Paul Bakker23986e52011-04-24 08:57:21 +00001030 size_t i, v0, v1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +00001032 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001033
1034 v0 = count / biL;
1035 v1 = count & (biL - 1);
1036
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001037 if( v0 > X->n || ( v0 == X->n && v1 > 0 ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 return mbedtls_mpi_lset( X, 0 );
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001039
Paul Bakker5121ce52009-01-03 21:22:43 +00001040 /*
1041 * shift by count / limb_size
1042 */
1043 if( v0 > 0 )
1044 {
1045 for( i = 0; i < X->n - v0; i++ )
1046 X->p[i] = X->p[i + v0];
1047
1048 for( ; i < X->n; i++ )
1049 X->p[i] = 0;
1050 }
1051
1052 /*
1053 * shift by count % limb_size
1054 */
1055 if( v1 > 0 )
1056 {
Paul Bakker23986e52011-04-24 08:57:21 +00001057 for( i = X->n; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001058 {
Paul Bakker23986e52011-04-24 08:57:21 +00001059 r1 = X->p[i - 1] << (biL - v1);
1060 X->p[i - 1] >>= v1;
1061 X->p[i - 1] |= r0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001062 r0 = r1;
1063 }
1064 }
1065
1066 return( 0 );
1067}
1068
1069/*
1070 * Compare unsigned values
1071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001073{
Paul Bakker23986e52011-04-24 08:57:21 +00001074 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001075 MPI_VALIDATE_RET( X != NULL );
1076 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001077
Paul Bakker23986e52011-04-24 08:57:21 +00001078 for( i = X->n; i > 0; i-- )
1079 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001080 break;
1081
Paul Bakker23986e52011-04-24 08:57:21 +00001082 for( j = Y->n; j > 0; j-- )
1083 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001084 break;
1085
Paul Bakker23986e52011-04-24 08:57:21 +00001086 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001087 return( 0 );
1088
1089 if( i > j ) return( 1 );
1090 if( j > i ) return( -1 );
1091
Paul Bakker23986e52011-04-24 08:57:21 +00001092 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001093 {
Paul Bakker23986e52011-04-24 08:57:21 +00001094 if( X->p[i - 1] > Y->p[i - 1] ) return( 1 );
1095 if( X->p[i - 1] < Y->p[i - 1] ) return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001096 }
1097
1098 return( 0 );
1099}
1100
1101/*
1102 * Compare signed values
1103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001105{
Paul Bakker23986e52011-04-24 08:57:21 +00001106 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001107 MPI_VALIDATE_RET( X != NULL );
1108 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001109
Paul Bakker23986e52011-04-24 08:57:21 +00001110 for( i = X->n; i > 0; i-- )
1111 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 break;
1113
Paul Bakker23986e52011-04-24 08:57:21 +00001114 for( j = Y->n; j > 0; j-- )
1115 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001116 break;
1117
Paul Bakker23986e52011-04-24 08:57:21 +00001118 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001119 return( 0 );
1120
1121 if( i > j ) return( X->s );
Paul Bakker0c8f73b2012-03-22 14:08:57 +00001122 if( j > i ) return( -Y->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001123
1124 if( X->s > 0 && Y->s < 0 ) return( 1 );
1125 if( Y->s > 0 && X->s < 0 ) return( -1 );
1126
Paul Bakker23986e52011-04-24 08:57:21 +00001127 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001128 {
Paul Bakker23986e52011-04-24 08:57:21 +00001129 if( X->p[i - 1] > Y->p[i - 1] ) return( X->s );
1130 if( X->p[i - 1] < Y->p[i - 1] ) return( -X->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001131 }
1132
1133 return( 0 );
1134}
1135
Janos Follath45ec9902019-10-14 09:09:32 +01001136/** Decide if an integer is less than the other, without branches.
1137 *
1138 * \param x First integer.
1139 * \param y Second integer.
1140 *
1141 * \return 1 if \p x is less than \p y, 0 otherwise
1142 */
Janos Follath867a3ab2019-10-11 14:21:53 +01001143static unsigned ct_lt_mpi_uint( const mbedtls_mpi_uint x,
1144 const mbedtls_mpi_uint y )
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001145{
1146 mbedtls_mpi_uint ret;
1147 mbedtls_mpi_uint cond;
1148
1149 /*
1150 * Check if the most significant bits (MSB) of the operands are different.
1151 */
1152 cond = ( x ^ y );
1153 /*
1154 * If the MSB are the same then the difference x-y will be negative (and
1155 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
1156 */
1157 ret = ( x - y ) & ~cond;
1158 /*
1159 * If the MSB are different, then the operand with the MSB of 1 is the
1160 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
1161 * the MSB of y is 0.)
1162 */
1163 ret |= y & cond;
1164
1165
Janos Follath7a34bcf2019-10-14 08:59:14 +01001166 ret = ret >> ( biL - 1 );
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001167
Janos Follath359a01e2019-10-29 15:08:46 +00001168 return (unsigned) ret;
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001169}
1170
1171/*
1172 * Compare signed values in constant time
1173 */
Janos Follath867a3ab2019-10-11 14:21:53 +01001174int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y,
1175 unsigned *ret )
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001176{
1177 size_t i;
Janos Follathbd87a592019-10-28 12:23:18 +00001178 /* The value of any of these variables is either 0 or 1 at all times. */
Janos Follath1f21c1d2019-10-28 12:31:34 +00001179 unsigned cond, done, X_is_negative, Y_is_negative;
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001180
1181 MPI_VALIDATE_RET( X != NULL );
1182 MPI_VALIDATE_RET( Y != NULL );
1183 MPI_VALIDATE_RET( ret != NULL );
1184
1185 if( X->n != Y->n )
1186 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1187
1188 /*
Janos Follath58525182019-10-28 12:12:15 +00001189 * Set sign_N to 1 if N >= 0, 0 if N < 0.
1190 * We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0.
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001191 */
Janos Follath1f21c1d2019-10-28 12:31:34 +00001192 X_is_negative = ( X->s & 2 ) >> 1;
1193 Y_is_negative = ( Y->s & 2 ) >> 1;
Janos Follath867a3ab2019-10-11 14:21:53 +01001194
1195 /*
1196 * If the signs are different, then the positive operand is the bigger.
Janos Follath1f21c1d2019-10-28 12:31:34 +00001197 * That is if X is negative (X_is_negative == 1), then X < Y is true and it
1198 * is false if X is positive (X_is_negative == 0).
Janos Follath867a3ab2019-10-11 14:21:53 +01001199 */
Janos Follath1f21c1d2019-10-28 12:31:34 +00001200 cond = ( X_is_negative ^ Y_is_negative );
1201 *ret = cond & X_is_negative;
Janos Follath867a3ab2019-10-11 14:21:53 +01001202
1203 /*
Janos Follathbd87a592019-10-28 12:23:18 +00001204 * This is a constant-time function. We might have the result, but we still
Janos Follath867a3ab2019-10-11 14:21:53 +01001205 * need to go through the loop. Record if we have the result already.
1206 */
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001207 done = cond;
1208
1209 for( i = X->n; i > 0; i-- )
1210 {
1211 /*
Janos Follathe25f1ee2019-11-05 12:24:52 +00001212 * If Y->p[i - 1] < X->p[i - 1] then X < Y is true if and only if both
1213 * X and Y are negative.
Janos Follath867a3ab2019-10-11 14:21:53 +01001214 *
1215 * Again even if we can make a decision, we just mark the result and
1216 * the fact that we are done and continue looping.
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001217 */
Janos Follathe25f1ee2019-11-05 12:24:52 +00001218 cond = ct_lt_mpi_uint( Y->p[i - 1], X->p[i - 1] );
1219 *ret |= cond & ( 1 - done ) & X_is_negative;
Janos Follathfbe4c942019-10-28 12:37:21 +00001220 done |= cond;
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001221
1222 /*
Janos Follathe25f1ee2019-11-05 12:24:52 +00001223 * If X->p[i - 1] < Y->p[i - 1] then X < Y is true if and only if both
1224 * X and Y are positive.
Janos Follath867a3ab2019-10-11 14:21:53 +01001225 *
1226 * Again even if we can make a decision, we just mark the result and
1227 * the fact that we are done and continue looping.
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001228 */
Janos Follathe25f1ee2019-11-05 12:24:52 +00001229 cond = ct_lt_mpi_uint( X->p[i - 1], Y->p[i - 1] );
1230 *ret |= cond & ( 1 - done ) & ( 1 - X_is_negative );
Janos Follathfbe4c942019-10-28 12:37:21 +00001231 done |= cond;
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001232 }
1233
1234 return( 0 );
1235}
1236
Paul Bakker5121ce52009-01-03 21:22:43 +00001237/*
1238 * Compare signed values
1239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +00001241{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 mbedtls_mpi Y;
1243 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001244 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001245
1246 *p = ( z < 0 ) ? -z : z;
1247 Y.s = ( z < 0 ) ? -1 : 1;
1248 Y.n = 1;
1249 Y.p = p;
1250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 return( mbedtls_mpi_cmp_mpi( X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001252}
1253
1254/*
1255 * Unsigned addition: X = |A| + |B| (HAC 14.7)
1256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001258{
Paul Bakker23986e52011-04-24 08:57:21 +00001259 int ret;
1260 size_t i, j;
Janos Follath6c922682015-10-30 17:43:11 +01001261 mbedtls_mpi_uint *o, *p, c, tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +00001262 MPI_VALIDATE_RET( X != NULL );
1263 MPI_VALIDATE_RET( A != NULL );
1264 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001265
1266 if( X == B )
1267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 const mbedtls_mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001269 }
1270
1271 if( X != A )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
Paul Bakker9af723c2014-05-01 13:03:14 +02001273
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001274 /*
1275 * X should always be positive as a result of unsigned additions.
1276 */
1277 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001278
Paul Bakker23986e52011-04-24 08:57:21 +00001279 for( j = B->n; j > 0; j-- )
1280 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001281 break;
1282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001284
1285 o = B->p; p = X->p; c = 0;
1286
Janos Follath6c922682015-10-30 17:43:11 +01001287 /*
1288 * tmp is used because it might happen that p == o
1289 */
Paul Bakker23986e52011-04-24 08:57:21 +00001290 for( i = 0; i < j; i++, o++, p++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001291 {
Janos Follath6c922682015-10-30 17:43:11 +01001292 tmp= *o;
Paul Bakker5121ce52009-01-03 21:22:43 +00001293 *p += c; c = ( *p < c );
Janos Follath6c922682015-10-30 17:43:11 +01001294 *p += tmp; c += ( *p < tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +00001295 }
1296
1297 while( c != 0 )
1298 {
1299 if( i >= X->n )
1300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001302 p = X->p + i;
1303 }
1304
Paul Bakker2d319fd2012-09-16 21:34:26 +00001305 *p += c; c = ( *p < c ); i++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001306 }
1307
1308cleanup:
1309
1310 return( ret );
1311}
1312
Gilles Peskinede719d52020-06-09 10:39:38 +02001313/**
Gilles Peskine36acd542020-06-08 21:58:22 +02001314 * Helper for mbedtls_mpi subtraction.
1315 *
1316 * Calculate d - s where d and s have the same size.
1317 * This function operates modulo (2^ciL)^n and returns the carry
1318 * (1 if there was a wraparound, i.e. if `d < s`, and 0 otherwise).
1319 *
1320 * \param n Number of limbs of \p d and \p s.
1321 * \param[in,out] d On input, the left operand.
1322 * On output, the result of the subtraction:
Gilles Peskinede719d52020-06-09 10:39:38 +02001323 * \param[in] s The right operand.
Gilles Peskine36acd542020-06-08 21:58:22 +02001324 *
1325 * \return 1 if `d < s`.
1326 * 0 if `d >= s`.
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 */
Gilles Peskine36acd542020-06-08 21:58:22 +02001328static mbedtls_mpi_uint mpi_sub_hlp( size_t n,
1329 mbedtls_mpi_uint *d,
1330 const mbedtls_mpi_uint *s )
Paul Bakker5121ce52009-01-03 21:22:43 +00001331{
Paul Bakker23986e52011-04-24 08:57:21 +00001332 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 mbedtls_mpi_uint c, z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001334
1335 for( i = c = 0; i < n; i++, s++, d++ )
1336 {
1337 z = ( *d < c ); *d -= c;
1338 c = ( *d < *s ) + z; *d -= *s;
1339 }
1340
Gilles Peskine36acd542020-06-08 21:58:22 +02001341 return( c );
Paul Bakker5121ce52009-01-03 21:22:43 +00001342}
1343
1344/*
Gilles Peskine08fd43c2020-06-08 22:50:35 +02001345 * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001348{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 mbedtls_mpi TB;
Paul Bakker23986e52011-04-24 08:57:21 +00001350 int ret;
1351 size_t n;
Gilles Peskine08fd43c2020-06-08 22:50:35 +02001352 mbedtls_mpi_uint carry;
Hanno Becker73d7d792018-12-11 10:35:51 +00001353 MPI_VALIDATE_RET( X != NULL );
1354 MPI_VALIDATE_RET( A != NULL );
1355 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001358
1359 if( X == B )
1360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001362 B = &TB;
1363 }
1364
1365 if( X != A )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001367
Paul Bakker1ef7a532009-06-20 10:50:55 +00001368 /*
Paul Bakker60b1d102013-10-29 10:02:51 +01001369 * X should always be positive as a result of unsigned subtractions.
Paul Bakker1ef7a532009-06-20 10:50:55 +00001370 */
1371 X->s = 1;
1372
Paul Bakker5121ce52009-01-03 21:22:43 +00001373 ret = 0;
1374
Paul Bakker23986e52011-04-24 08:57:21 +00001375 for( n = B->n; n > 0; n-- )
1376 if( B->p[n - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001377 break;
Gilles Peskine6260b702021-01-27 22:30:43 +01001378 if( n > A->n )
1379 {
1380 /* B >= (2^ciL)^n > A */
1381 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1382 goto cleanup;
1383 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001384
Gilles Peskine08fd43c2020-06-08 22:50:35 +02001385 carry = mpi_sub_hlp( n, X->p, B->p );
1386 if( carry != 0 )
Gilles Peskine36acd542020-06-08 21:58:22 +02001387 {
Gilles Peskine08fd43c2020-06-08 22:50:35 +02001388 /* Propagate the carry to the first nonzero limb of X. */
1389 for( ; n < X->n && X->p[n] == 0; n++ )
1390 --X->p[n];
1391 /* If we ran out of space for the carry, it means that the result
1392 * is negative. */
1393 if( n == X->n )
Gilles Peskine84697ca2020-07-23 01:16:46 +02001394 {
1395 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1396 goto cleanup;
1397 }
Gilles Peskine08fd43c2020-06-08 22:50:35 +02001398 --X->p[n];
Gilles Peskine36acd542020-06-08 21:58:22 +02001399 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001400
1401cleanup:
1402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 mbedtls_mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001404
1405 return( ret );
1406}
1407
1408/*
1409 * Signed addition: X = A + B
1410 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001412{
Hanno Becker73d7d792018-12-11 10:35:51 +00001413 int ret, s;
1414 MPI_VALIDATE_RET( X != NULL );
1415 MPI_VALIDATE_RET( A != NULL );
1416 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001417
Hanno Becker73d7d792018-12-11 10:35:51 +00001418 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001419 if( A->s * B->s < 0 )
1420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 X->s = s;
1425 }
1426 else
1427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001429 X->s = -s;
1430 }
1431 }
1432 else
1433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001435 X->s = s;
1436 }
1437
1438cleanup:
1439
1440 return( ret );
1441}
1442
1443/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001444 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001445 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001447{
Hanno Becker73d7d792018-12-11 10:35:51 +00001448 int ret, s;
1449 MPI_VALIDATE_RET( X != NULL );
1450 MPI_VALIDATE_RET( A != NULL );
1451 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001452
Hanno Becker73d7d792018-12-11 10:35:51 +00001453 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001454 if( A->s * B->s > 0 )
1455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001459 X->s = s;
1460 }
1461 else
1462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001464 X->s = -s;
1465 }
1466 }
1467 else
1468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001470 X->s = s;
1471 }
1472
1473cleanup:
1474
1475 return( ret );
1476}
1477
1478/*
1479 * Signed addition: X = A + b
1480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001482{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 mbedtls_mpi _B;
1484 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001485 MPI_VALIDATE_RET( X != NULL );
1486 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001487
1488 p[0] = ( b < 0 ) ? -b : b;
1489 _B.s = ( b < 0 ) ? -1 : 1;
1490 _B.n = 1;
1491 _B.p = p;
1492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 return( mbedtls_mpi_add_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001494}
1495
1496/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001497 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001500{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 mbedtls_mpi _B;
1502 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001503 MPI_VALIDATE_RET( X != NULL );
1504 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001505
1506 p[0] = ( b < 0 ) ? -b : b;
1507 _B.s = ( b < 0 ) ? -1 : 1;
1508 _B.n = 1;
1509 _B.p = p;
1510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511 return( mbedtls_mpi_sub_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001512}
1513
1514/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 * Helper for mbedtls_mpi multiplication
Paul Bakkerfc4f46f2013-06-24 19:23:56 +02001516 */
1517static
1518#if defined(__APPLE__) && defined(__arm__)
1519/*
1520 * Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
1521 * appears to need this to prevent bad ARM code generation at -O3.
1522 */
1523__attribute__ ((noinline))
1524#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525void 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 +00001526{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 mbedtls_mpi_uint c = 0, t = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001528
1529#if defined(MULADDC_HUIT)
1530 for( ; i >= 8; i -= 8 )
1531 {
1532 MULADDC_INIT
1533 MULADDC_HUIT
1534 MULADDC_STOP
1535 }
1536
1537 for( ; i > 0; i-- )
1538 {
1539 MULADDC_INIT
1540 MULADDC_CORE
1541 MULADDC_STOP
1542 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001543#else /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001544 for( ; i >= 16; i -= 16 )
1545 {
1546 MULADDC_INIT
1547 MULADDC_CORE MULADDC_CORE
1548 MULADDC_CORE MULADDC_CORE
1549 MULADDC_CORE MULADDC_CORE
1550 MULADDC_CORE MULADDC_CORE
1551
1552 MULADDC_CORE MULADDC_CORE
1553 MULADDC_CORE MULADDC_CORE
1554 MULADDC_CORE MULADDC_CORE
1555 MULADDC_CORE MULADDC_CORE
1556 MULADDC_STOP
1557 }
1558
1559 for( ; i >= 8; i -= 8 )
1560 {
1561 MULADDC_INIT
1562 MULADDC_CORE MULADDC_CORE
1563 MULADDC_CORE MULADDC_CORE
1564
1565 MULADDC_CORE MULADDC_CORE
1566 MULADDC_CORE MULADDC_CORE
1567 MULADDC_STOP
1568 }
1569
1570 for( ; i > 0; i-- )
1571 {
1572 MULADDC_INIT
1573 MULADDC_CORE
1574 MULADDC_STOP
1575 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001576#endif /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001577
1578 t++;
1579
1580 do {
1581 *d += c; c = ( *d < c ); d++;
1582 }
1583 while( c != 0 );
1584}
1585
1586/*
1587 * Baseline multiplication: X = A * B (HAC 14.12)
1588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001590{
Paul Bakker23986e52011-04-24 08:57:21 +00001591 int ret;
1592 size_t i, j;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 mbedtls_mpi TA, TB;
Hanno Becker73d7d792018-12-11 10:35:51 +00001594 MPI_VALIDATE_RET( X != NULL );
1595 MPI_VALIDATE_RET( A != NULL );
1596 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 if( X == A ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); A = &TA; }
1601 if( X == B ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); B = &TB; }
Paul Bakker5121ce52009-01-03 21:22:43 +00001602
Paul Bakker23986e52011-04-24 08:57:21 +00001603 for( i = A->n; i > 0; i-- )
1604 if( A->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001605 break;
1606
Paul Bakker23986e52011-04-24 08:57:21 +00001607 for( j = B->n; j > 0; j-- )
1608 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001609 break;
1610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + j ) );
1612 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001613
Alexey Skalozub8e75e682016-01-13 21:59:27 +02001614 for( ; j > 0; j-- )
1615 mpi_mul_hlp( i, A->p, X->p + j - 1, B->p[j - 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00001616
1617 X->s = A->s * B->s;
1618
1619cleanup:
1620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621 mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001622
1623 return( ret );
1624}
1625
1626/*
1627 * Baseline multiplication: X = A * b
1628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001630{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 mbedtls_mpi _B;
1632 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001633 MPI_VALIDATE_RET( X != NULL );
1634 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001635
1636 _B.s = 1;
1637 _B.n = 1;
1638 _B.p = p;
1639 p[0] = b;
1640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 return( mbedtls_mpi_mul_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001642}
1643
1644/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001645 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1646 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001647 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001648static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1,
1649 mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r )
Simon Butcher15b15d12015-11-26 19:35:03 +00001650{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001651#if defined(MBEDTLS_HAVE_UDBL)
1652 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001653#else
Simon Butcher9803d072016-01-03 00:24:34 +00001654 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
1655 const mbedtls_mpi_uint uint_halfword_mask = ( (mbedtls_mpi_uint) 1 << biH ) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001656 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1657 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001658 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001659#endif
1660
Simon Butcher15b15d12015-11-26 19:35:03 +00001661 /*
1662 * Check for overflow
1663 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001664 if( 0 == d || u1 >= d )
Simon Butcher15b15d12015-11-26 19:35:03 +00001665 {
Simon Butcherf5ba0452015-12-27 23:01:55 +00001666 if (r != NULL) *r = ~0;
Simon Butcher15b15d12015-11-26 19:35:03 +00001667
Simon Butcherf5ba0452015-12-27 23:01:55 +00001668 return ( ~0 );
Simon Butcher15b15d12015-11-26 19:35:03 +00001669 }
1670
1671#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001672 dividend = (mbedtls_t_udbl) u1 << biL;
1673 dividend |= (mbedtls_t_udbl) u0;
1674 quotient = dividend / d;
1675 if( quotient > ( (mbedtls_t_udbl) 1 << biL ) - 1 )
1676 quotient = ( (mbedtls_t_udbl) 1 << biL ) - 1;
1677
1678 if( r != NULL )
Simon Butcher9803d072016-01-03 00:24:34 +00001679 *r = (mbedtls_mpi_uint)( dividend - (quotient * d ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001680
1681 return (mbedtls_mpi_uint) quotient;
1682#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001683
1684 /*
1685 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1686 * Vol. 2 - Seminumerical Algorithms, Knuth
1687 */
1688
1689 /*
1690 * Normalize the divisor, d, and dividend, u0, u1
1691 */
1692 s = mbedtls_clz( d );
1693 d = d << s;
1694
1695 u1 = u1 << s;
Simon Butcher9803d072016-01-03 00:24:34 +00001696 u1 |= ( u0 >> ( biL - s ) ) & ( -(mbedtls_mpi_sint)s >> ( biL - 1 ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001697 u0 = u0 << s;
1698
1699 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001700 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001701
1702 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001703 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001704
1705 /*
1706 * Find the first quotient and remainder
1707 */
1708 q1 = u1 / d1;
1709 r0 = u1 - d1 * q1;
1710
1711 while( q1 >= radix || ( q1 * d0 > radix * r0 + u0_msw ) )
1712 {
1713 q1 -= 1;
1714 r0 += d1;
1715
1716 if ( r0 >= radix ) break;
1717 }
1718
Simon Butcherf5ba0452015-12-27 23:01:55 +00001719 rAX = ( u1 * radix ) + ( u0_msw - q1 * d );
Simon Butcher15b15d12015-11-26 19:35:03 +00001720 q0 = rAX / d1;
1721 r0 = rAX - q0 * d1;
1722
1723 while( q0 >= radix || ( q0 * d0 > radix * r0 + u0_lsw ) )
1724 {
1725 q0 -= 1;
1726 r0 += d1;
1727
1728 if ( r0 >= radix ) break;
1729 }
1730
1731 if (r != NULL)
Simon Butcherf5ba0452015-12-27 23:01:55 +00001732 *r = ( rAX * radix + u0_lsw - q0 * d ) >> s;
Simon Butcher15b15d12015-11-26 19:35:03 +00001733
1734 quotient = q1 * radix + q0;
1735
1736 return quotient;
1737#endif
1738}
1739
1740/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001742 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001743int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1744 const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001745{
Paul Bakker23986e52011-04-24 08:57:21 +00001746 int ret;
1747 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 mbedtls_mpi X, Y, Z, T1, T2;
Hanno Becker73d7d792018-12-11 10:35:51 +00001749 MPI_VALIDATE_RET( A != NULL );
1750 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 if( mbedtls_mpi_cmp_int( B, 0 ) == 0 )
1753 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
1756 mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 if( Q != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_lset( Q, 0 ) );
1761 if( R != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001762 return( 0 );
1763 }
1764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &X, A ) );
1766 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001767 X.s = Y.s = 1;
1768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &Z, A->n + 2 ) );
1770 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Z, 0 ) );
1771 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, 2 ) );
1772 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T2, 3 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001773
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001774 k = mbedtls_mpi_bitlen( &Y ) % biL;
Paul Bakkerf9688572011-05-05 10:00:45 +00001775 if( k < biL - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001776 {
1777 k = biL - 1 - k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &X, k ) );
1779 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, k ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001780 }
1781 else k = 0;
1782
1783 n = X.n - 1;
1784 t = Y.n - 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787 while( mbedtls_mpi_cmp_mpi( &X, &Y ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001788 {
1789 Z.p[n - t]++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001791 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001793
1794 for( i = n; i > t ; i-- )
1795 {
1796 if( X.p[i] >= Y.p[t] )
1797 Z.p[i - t - 1] = ~0;
1798 else
1799 {
Simon Butcher15b15d12015-11-26 19:35:03 +00001800 Z.p[i - t - 1] = mbedtls_int_div_int( X.p[i], X.p[i - 1],
1801 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001802 }
1803
1804 Z.p[i - t - 1]++;
1805 do
1806 {
1807 Z.p[i - t - 1]--;
1808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T1, 0 ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001810 T1.p[0] = ( t < 1 ) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001811 T1.p[1] = Y.p[t];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T2, 0 ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001815 T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2];
1816 T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001817 T2.p[2] = X.p[i];
1818 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 while( mbedtls_mpi_cmp_mpi( &T1, &T2 ) > 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
1822 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1823 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 if( mbedtls_mpi_cmp_int( &X, 0 ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &Y ) );
1828 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1829 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001830 Z.p[i - t - 1]--;
1831 }
1832 }
1833
1834 if( Q != NULL )
1835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( Q, &Z ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001837 Q->s = A->s * B->s;
1838 }
1839
1840 if( R != NULL )
1841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &X, k ) );
Paul Bakkerf02c5642012-11-13 10:25:21 +00001843 X.s = A->s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, &X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 if( mbedtls_mpi_cmp_int( R, 0 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001847 R->s = 1;
1848 }
1849
1850cleanup:
1851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
1853 mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001854
1855 return( ret );
1856}
1857
1858/*
1859 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00001860 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001861int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R,
1862 const mbedtls_mpi *A,
1863 mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001864{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 mbedtls_mpi _B;
1866 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001867 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001868
1869 p[0] = ( b < 0 ) ? -b : b;
1870 _B.s = ( b < 0 ) ? -1 : 1;
1871 _B.n = 1;
1872 _B.p = p;
1873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 return( mbedtls_mpi_div_mpi( Q, R, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001875}
1876
1877/*
1878 * Modulo: R = A mod B
1879 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001881{
1882 int ret;
Hanno Becker73d7d792018-12-11 10:35:51 +00001883 MPI_VALIDATE_RET( R != NULL );
1884 MPI_VALIDATE_RET( A != NULL );
1885 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 if( mbedtls_mpi_cmp_int( B, 0 ) < 0 )
1888 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( NULL, R, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892 while( mbedtls_mpi_cmp_int( R, 0 ) < 0 )
1893 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 while( mbedtls_mpi_cmp_mpi( R, B ) >= 0 )
1896 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001897
1898cleanup:
1899
1900 return( ret );
1901}
1902
1903/*
1904 * Modulo: r = A mod b
1905 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001907{
Paul Bakker23986e52011-04-24 08:57:21 +00001908 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 mbedtls_mpi_uint x, y, z;
Hanno Becker73d7d792018-12-11 10:35:51 +00001910 MPI_VALIDATE_RET( r != NULL );
1911 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001912
1913 if( b == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001915
1916 if( b < 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001918
1919 /*
1920 * handle trivial cases
1921 */
1922 if( b == 1 )
1923 {
1924 *r = 0;
1925 return( 0 );
1926 }
1927
1928 if( b == 2 )
1929 {
1930 *r = A->p[0] & 1;
1931 return( 0 );
1932 }
1933
1934 /*
1935 * general case
1936 */
Paul Bakker23986e52011-04-24 08:57:21 +00001937 for( i = A->n, y = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001938 {
Paul Bakker23986e52011-04-24 08:57:21 +00001939 x = A->p[i - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001940 y = ( y << biH ) | ( x >> biH );
1941 z = y / b;
1942 y -= z * b;
1943
1944 x <<= biH;
1945 y = ( y << biH ) | ( x >> biH );
1946 z = y / b;
1947 y -= z * b;
1948 }
1949
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001950 /*
1951 * If A is negative, then the current y represents a negative value.
1952 * Flipping it to the positive side.
1953 */
1954 if( A->s < 0 && y != 0 )
1955 y = b - y;
1956
Paul Bakker5121ce52009-01-03 21:22:43 +00001957 *r = y;
1958
1959 return( 0 );
1960}
1961
1962/*
1963 * Fast Montgomery initialization (thanks to Tom St Denis)
1964 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965static void mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00001966{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 mbedtls_mpi_uint x, m0 = N->p[0];
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01001968 unsigned int i;
Paul Bakker5121ce52009-01-03 21:22:43 +00001969
1970 x = m0;
1971 x += ( ( m0 + 2 ) & 4 ) << 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001972
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01001973 for( i = biL; i >= 8; i /= 2 )
1974 x *= ( 2 - ( m0 * x ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001975
1976 *mm = ~x + 1;
1977}
1978
Gilles Peskine3ce3ddf2020-06-04 15:00:49 +02001979/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
1980 *
1981 * \param[in,out] A One of the numbers to multiply.
Gilles Peskine635a3742020-06-08 22:37:50 +02001982 * It must have at least as many limbs as N
1983 * (A->n >= N->n), and any limbs beyond n are ignored.
Gilles Peskine3ce3ddf2020-06-04 15:00:49 +02001984 * On successful completion, A contains the result of
1985 * the multiplication A * B * R^-1 mod N where
1986 * R = (2^ciL)^n.
1987 * \param[in] B One of the numbers to multiply.
1988 * It must be nonzero and must not have more limbs than N
1989 * (B->n <= N->n).
1990 * \param[in] N The modulo. N must be odd.
1991 * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
1992 * This is -N^-1 mod 2^ciL.
1993 * \param[in,out] T A bignum for temporary storage.
1994 * It must be at least twice the limb size of N plus 2
1995 * (T->n >= 2 * (N->n + 1)).
1996 * Its initial content is unused and
1997 * its final content is indeterminate.
1998 * Note that unlike the usual convention in the library
1999 * for `const mbedtls_mpi*`, the content of T can change.
Paul Bakker5121ce52009-01-03 21:22:43 +00002000 */
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002001static 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 +02002002 const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00002003{
Paul Bakker23986e52011-04-24 08:57:21 +00002004 size_t i, n, m;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 mbedtls_mpi_uint u0, u1, *d;
Paul Bakker5121ce52009-01-03 21:22:43 +00002006
2007 memset( T->p, 0, T->n * ciL );
2008
2009 d = T->p;
2010 n = N->n;
2011 m = ( B->n < n ) ? B->n : n;
2012
2013 for( i = 0; i < n; i++ )
2014 {
2015 /*
2016 * T = (T + u0*B + u1*N) / 2^biL
2017 */
2018 u0 = A->p[i];
2019 u1 = ( d[0] + u0 * B->p[0] ) * mm;
2020
2021 mpi_mul_hlp( m, B->p, d, u0 );
2022 mpi_mul_hlp( n, N->p, d, u1 );
2023
2024 *d++ = u0; d[n + 1] = 0;
2025 }
2026
Gilles Peskine635a3742020-06-08 22:37:50 +02002027 /* At this point, d is either the desired result or the desired result
2028 * plus N. We now potentially subtract N, avoiding leaking whether the
2029 * subtraction is performed through side channels. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002030
Gilles Peskine635a3742020-06-08 22:37:50 +02002031 /* Copy the n least significant limbs of d to A, so that
2032 * A = d if d < N (recall that N has n limbs). */
2033 memcpy( A->p, d, n * ciL );
Gilles Peskinede719d52020-06-09 10:39:38 +02002034 /* If d >= N then we want to set A to d - N. To prevent timing attacks,
Gilles Peskine635a3742020-06-08 22:37:50 +02002035 * do the calculation without using conditional tests. */
2036 /* Set d to d0 + (2^biL)^n - N where d0 is the current value of d. */
Gilles Peskine8f672662020-06-04 21:05:24 +02002037 d[n] += 1;
Gilles Peskine36acd542020-06-08 21:58:22 +02002038 d[n] -= mpi_sub_hlp( n, d, N->p );
Gilles Peskine635a3742020-06-08 22:37:50 +02002039 /* If d0 < N then d < (2^biL)^n
2040 * so d[n] == 0 and we want to keep A as it is.
2041 * If d0 >= N then d >= (2^biL)^n, and d <= (2^biL)^n + N < 2 * (2^biL)^n
2042 * so d[n] == 1 and we want to set A to the result of the subtraction
2043 * which is d - (2^biL)^n, i.e. the n least significant limbs of d.
2044 * This exactly corresponds to a conditional assignment. */
2045 mpi_safe_cond_assign( n, A->p, d, (unsigned char) d[n] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002046}
2047
2048/*
2049 * Montgomery reduction: A = A * R^-1 mod N
Gilles Peskine3ce3ddf2020-06-04 15:00:49 +02002050 *
2051 * See mpi_montmul() regarding constraints and guarantees on the parameters.
Paul Bakker5121ce52009-01-03 21:22:43 +00002052 */
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002053static void mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N,
2054 mbedtls_mpi_uint mm, const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00002055{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 mbedtls_mpi_uint z = 1;
2057 mbedtls_mpi U;
Paul Bakker5121ce52009-01-03 21:22:43 +00002058
Paul Bakker8ddb6452013-02-27 14:56:33 +01002059 U.n = U.s = (int) z;
Paul Bakker5121ce52009-01-03 21:22:43 +00002060 U.p = &z;
2061
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002062 mpi_montmul( A, &U, N, mm, T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002063}
2064
Manuel Pégourié-Gonnard432ebba2021-06-03 10:42:46 +02002065/*
2066 * Constant-flow boolean "equal" comparison:
2067 * return x == y
2068 *
2069 * This function can be used to write constant-time code by replacing branches
2070 * with bit operations - it can be used in conjunction with
2071 * mbedtls_ssl_cf_mask_from_bit().
2072 *
2073 * This function is implemented without using comparison operators, as those
2074 * might be translated to branches by some compilers on some platforms.
2075 */
2076static size_t mbedtls_mpi_cf_bool_eq( size_t x, size_t y )
2077{
2078 /* diff = 0 if x == y, non-zero otherwise */
2079 const size_t diff = x ^ y;
2080
2081 /* MSVC has a warning about unary minus on unsigned integer types,
2082 * but this is well-defined and precisely what we want to do here. */
2083#if defined(_MSC_VER)
2084#pragma warning( push )
2085#pragma warning( disable : 4146 )
2086#endif
2087
2088 /* diff_msb's most significant bit is equal to x != y */
2089 const size_t diff_msb = ( diff | -diff );
2090
2091#if defined(_MSC_VER)
2092#pragma warning( pop )
2093#endif
2094
2095 /* diff1 = (x != y) ? 1 : 0 */
2096 const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 );
2097
2098 return( 1 ^ diff1 );
2099}
2100
Manuel Pégourié-Gonnard87bd4442021-03-09 11:22:20 +01002101/**
2102 * Select an MPI from a table without leaking the index.
2103 *
2104 * This is functionally equivalent to mbedtls_mpi_copy(R, T[idx]) except it
2105 * reads the entire table in order to avoid leaking the value of idx to an
2106 * attacker able to observe memory access patterns.
2107 *
2108 * \param[out] R Where to write the selected MPI.
2109 * \param[in] T The table to read from.
2110 * \param[in] T_size The number of elements in the table.
2111 * \param[in] idx The index of the element to select;
2112 * this must satisfy 0 <= idx < T_size.
2113 *
2114 * \return \c 0 on success, or a negative error code.
2115 */
2116static int mpi_select( mbedtls_mpi *R, const mbedtls_mpi *T, size_t T_size, size_t idx )
2117{
2118 int ret;
2119
2120 for( size_t i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard432ebba2021-06-03 10:42:46 +02002121 {
2122 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( R, &T[i],
2123 mbedtls_mpi_cf_bool_eq( i, idx ) ) );
2124 }
Manuel Pégourié-Gonnard87bd4442021-03-09 11:22:20 +01002125
2126cleanup:
2127 return( ret );
2128}
2129
Paul Bakker5121ce52009-01-03 21:22:43 +00002130/*
2131 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
2132 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002133int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
2134 const mbedtls_mpi *E, const mbedtls_mpi *N,
2135 mbedtls_mpi *_RR )
Paul Bakker5121ce52009-01-03 21:22:43 +00002136{
Paul Bakker23986e52011-04-24 08:57:21 +00002137 int ret;
2138 size_t wbits, wsize, one = 1;
2139 size_t i, j, nblimbs;
2140 size_t bufsize, nbits;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 mbedtls_mpi_uint ei, mm, state;
Manuel Pégourié-Gonnard87bd4442021-03-09 11:22:20 +01002142 mbedtls_mpi RR, T, W[ 1 << MBEDTLS_MPI_WINDOW_SIZE ], WW, Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00002143 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002144
Hanno Becker73d7d792018-12-11 10:35:51 +00002145 MPI_VALIDATE_RET( X != NULL );
2146 MPI_VALIDATE_RET( A != NULL );
2147 MPI_VALIDATE_RET( E != NULL );
2148 MPI_VALIDATE_RET( N != NULL );
2149
Hanno Becker8d1dd1b2017-09-28 11:02:24 +01002150 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 || ( N->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153 if( mbedtls_mpi_cmp_int( E, 0 ) < 0 )
2154 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002155
Chris Jonesad59a2a2020-11-25 15:12:39 +00002156 if( mbedtls_mpi_bitlen( E ) > MBEDTLS_MPI_MAX_BITS ||
2157 mbedtls_mpi_bitlen( N ) > MBEDTLS_MPI_MAX_BITS )
2158 return ( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2159
Paul Bakkerf6198c12012-05-16 08:02:29 +00002160 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002161 * Init temps and window size
2162 */
2163 mpi_montg_init( &mm, N );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &T );
2165 mbedtls_mpi_init( &Apos );
Manuel Pégourié-Gonnard87bd4442021-03-09 11:22:20 +01002166 mbedtls_mpi_init( &WW );
Paul Bakker5121ce52009-01-03 21:22:43 +00002167 memset( W, 0, sizeof( W ) );
2168
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002169 i = mbedtls_mpi_bitlen( E );
Paul Bakker5121ce52009-01-03 21:22:43 +00002170
2171 wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
2172 ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1;
2173
Peter Kolbusb83d41d2018-12-11 14:01:44 -06002174#if( MBEDTLS_MPI_WINDOW_SIZE < 6 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 if( wsize > MBEDTLS_MPI_WINDOW_SIZE )
2176 wsize = MBEDTLS_MPI_WINDOW_SIZE;
Peter Kolbusb83d41d2018-12-11 14:01:44 -06002177#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00002178
Paul Bakker5121ce52009-01-03 21:22:43 +00002179 j = N->n + 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
2181 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], j ) );
2182 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T, j * 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002183
2184 /*
Paul Bakker50546922012-05-19 08:40:49 +00002185 * Compensate for negative A (and correct at the end)
2186 */
2187 neg = ( A->s == -1 );
Paul Bakker50546922012-05-19 08:40:49 +00002188 if( neg )
2189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Apos, A ) );
Paul Bakker50546922012-05-19 08:40:49 +00002191 Apos.s = 1;
2192 A = &Apos;
2193 }
2194
2195 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002196 * If 1st call, pre-compute R^2 mod N
2197 */
2198 if( _RR == NULL || _RR->p == NULL )
2199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &RR, 1 ) );
2201 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &RR, N->n * 2 * biL ) );
2202 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &RR, &RR, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002203
2204 if( _RR != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 memcpy( _RR, &RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 }
2207 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 memcpy( &RR, _RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002209
2210 /*
2211 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
2212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 if( mbedtls_mpi_cmp_mpi( A, N ) >= 0 )
2214 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &W[1], A, N ) );
Paul Bakkerc2024f42014-01-23 20:38:35 +01002215 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[1], A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002217
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002218 mpi_montmul( &W[1], &RR, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002219
2220 /*
2221 * X = R^2 * R^-1 mod N = R mod N
2222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &RR ) );
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002224 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002225
2226 if( wsize > 1 )
2227 {
2228 /*
2229 * W[1 << (wsize - 1)] = W[1] ^ (wsize - 1)
2230 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002231 j = one << ( wsize - 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[j], N->n + 1 ) );
2234 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[j], &W[1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002235
2236 for( i = 0; i < wsize - 1; i++ )
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002237 mpi_montmul( &W[j], &W[j], N, mm, &T );
Paul Bakker0d7702c2013-10-29 16:18:35 +01002238
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 /*
2240 * W[i] = W[i - 1] * W[1]
2241 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002242 for( i = j + 1; i < ( one << wsize ); i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[i], N->n + 1 ) );
2245 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[i], &W[i - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002246
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002247 mpi_montmul( &W[i], &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 }
2249 }
2250
2251 nblimbs = E->n;
2252 bufsize = 0;
2253 nbits = 0;
2254 wbits = 0;
2255 state = 0;
2256
2257 while( 1 )
2258 {
2259 if( bufsize == 0 )
2260 {
Paul Bakker0d7702c2013-10-29 16:18:35 +01002261 if( nblimbs == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 break;
2263
Paul Bakker0d7702c2013-10-29 16:18:35 +01002264 nblimbs--;
2265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 bufsize = sizeof( mbedtls_mpi_uint ) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00002267 }
2268
2269 bufsize--;
2270
2271 ei = (E->p[nblimbs] >> bufsize) & 1;
2272
2273 /*
2274 * skip leading 0s
2275 */
2276 if( ei == 0 && state == 0 )
2277 continue;
2278
2279 if( ei == 0 && state == 1 )
2280 {
2281 /*
2282 * out of window, square X
2283 */
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002284 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002285 continue;
2286 }
2287
2288 /*
2289 * add ei to current window
2290 */
2291 state = 2;
2292
2293 nbits++;
Paul Bakker66d5d072014-06-17 16:39:18 +02002294 wbits |= ( ei << ( wsize - nbits ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002295
2296 if( nbits == wsize )
2297 {
2298 /*
2299 * X = X^wsize R^-1 mod N
2300 */
2301 for( i = 0; i < wsize; i++ )
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002302 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002303
2304 /*
2305 * X = X * W[wbits] R^-1 mod N
2306 */
Manuel Pégourié-Gonnard87bd4442021-03-09 11:22:20 +01002307 MBEDTLS_MPI_CHK( mpi_select( &WW, W, 1 << wsize, wbits ) );
2308 mpi_montmul( X, &WW, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002309
2310 state--;
2311 nbits = 0;
2312 wbits = 0;
2313 }
2314 }
2315
2316 /*
2317 * process the remaining bits
2318 */
2319 for( i = 0; i < nbits; i++ )
2320 {
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002321 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002322
2323 wbits <<= 1;
2324
Paul Bakker66d5d072014-06-17 16:39:18 +02002325 if( ( wbits & ( one << wsize ) ) != 0 )
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002326 mpi_montmul( X, &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002327 }
2328
2329 /*
2330 * X = A^E * R * R^-1 mod N = A^E mod N
2331 */
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002332 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002333
Hanno Beckera4af1c42017-04-18 09:07:45 +01002334 if( neg && E->n != 0 && ( E->p[0] & 1 ) != 0 )
Paul Bakkerf6198c12012-05-16 08:02:29 +00002335 {
2336 X->s = -1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, N, X ) );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002338 }
2339
Paul Bakker5121ce52009-01-03 21:22:43 +00002340cleanup:
2341
Paul Bakker66d5d072014-06-17 16:39:18 +02002342 for( i = ( one << ( wsize - 1 ) ); i < ( one << wsize ); i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 mbedtls_mpi_free( &W[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345 mbedtls_mpi_free( &W[1] ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &Apos );
Manuel Pégourié-Gonnard87bd4442021-03-09 11:22:20 +01002346 mbedtls_mpi_free( &WW );
Paul Bakker6c591fa2011-05-05 11:49:20 +00002347
Paul Bakker75a28602014-03-31 12:08:17 +02002348 if( _RR == NULL || _RR->p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002350
2351 return( ret );
2352}
2353
Paul Bakker5121ce52009-01-03 21:22:43 +00002354/*
2355 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
2356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00002358{
Paul Bakker23986e52011-04-24 08:57:21 +00002359 int ret;
2360 size_t lz, lzt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 mbedtls_mpi TG, TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00002362
Hanno Becker73d7d792018-12-11 10:35:51 +00002363 MPI_VALIDATE_RET( G != NULL );
2364 MPI_VALIDATE_RET( A != NULL );
2365 MPI_VALIDATE_RET( B != NULL );
2366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 mbedtls_mpi_init( &TG ); mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) );
2370 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372 lz = mbedtls_mpi_lsb( &TA );
2373 lzt = mbedtls_mpi_lsb( &TB );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002374
Paul Bakker66d5d072014-06-17 16:39:18 +02002375 if( lzt < lz )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002376 lz = lzt;
2377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, lz ) );
2379 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, lz ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002380
Paul Bakker5121ce52009-01-03 21:22:43 +00002381 TA.s = TB.s = 1;
2382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 while( mbedtls_mpi_cmp_int( &TA, 0 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, mbedtls_mpi_lsb( &TA ) ) );
2386 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, mbedtls_mpi_lsb( &TB ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 if( mbedtls_mpi_cmp_mpi( &TA, &TB ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TA, &TA, &TB ) );
2391 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002392 }
2393 else
2394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TB, &TB, &TA ) );
2396 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 }
2398 }
2399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &TB, lz ) );
2401 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( G, &TB ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002402
2403cleanup:
2404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 mbedtls_mpi_free( &TG ); mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002406
2407 return( ret );
2408}
2409
Paul Bakker33dc46b2014-04-30 16:11:39 +02002410/*
2411 * Fill X with size bytes of random.
2412 *
2413 * Use a temporary bytes representation to make sure the result is the same
Paul Bakkerc37b0ac2014-05-01 14:19:23 +02002414 * regardless of the platform endianness (useful when f_rng is actually
Paul Bakker33dc46b2014-04-30 16:11:39 +02002415 * deterministic, eg for tests).
2416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002418 int (*f_rng)(void *, unsigned char *, size_t),
2419 void *p_rng )
Paul Bakker287781a2011-03-26 13:18:49 +00002420{
Paul Bakker23986e52011-04-24 08:57:21 +00002421 int ret;
Hanno Becker6dab6202019-01-02 16:42:29 +00002422 size_t const limbs = CHARS_TO_LIMBS( size );
Hanno Beckerda1655a2017-10-18 14:21:44 +01002423 size_t const overhead = ( limbs * ciL ) - size;
2424 unsigned char *Xp;
2425
Hanno Becker8ce11a32018-12-19 16:18:52 +00002426 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002427 MPI_VALIDATE_RET( f_rng != NULL );
Paul Bakker33dc46b2014-04-30 16:11:39 +02002428
Hanno Beckerda1655a2017-10-18 14:21:44 +01002429 /* Ensure that target MPI has exactly the necessary number of limbs */
2430 if( X->n != limbs )
2431 {
2432 mbedtls_mpi_free( X );
2433 mbedtls_mpi_init( X );
2434 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, limbs ) );
2435 }
2436 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker287781a2011-03-26 13:18:49 +00002437
Hanno Beckerda1655a2017-10-18 14:21:44 +01002438 Xp = (unsigned char*) X->p;
Gilles Peskine05251142020-11-25 16:15:14 +01002439 MBEDTLS_MPI_CHK( f_rng( p_rng, Xp + overhead, size ) );
Hanno Beckerda1655a2017-10-18 14:21:44 +01002440
Hanno Becker2be8a552018-10-25 12:40:09 +01002441 mpi_bigendian_to_host( X->p, limbs );
Paul Bakker287781a2011-03-26 13:18:49 +00002442
2443cleanup:
2444 return( ret );
2445}
2446
Paul Bakker5121ce52009-01-03 21:22:43 +00002447/*
2448 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002451{
2452 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Hanno Becker73d7d792018-12-11 10:35:51 +00002454 MPI_VALIDATE_RET( X != NULL );
2455 MPI_VALIDATE_RET( A != NULL );
2456 MPI_VALIDATE_RET( N != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002457
Hanno Becker4bcb4912017-04-18 15:49:39 +01002458 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TU ); mbedtls_mpi_init( &U1 ); mbedtls_mpi_init( &U2 );
2462 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &TB ); mbedtls_mpi_init( &TV );
2463 mbedtls_mpi_init( &V1 ); mbedtls_mpi_init( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, A, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 goto cleanup;
2471 }
2472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &TA, A, N ) );
2474 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TU, &TA ) );
2475 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, N ) );
2476 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TV, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U1, 1 ) );
2479 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U2, 0 ) );
2480 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V1, 0 ) );
2481 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002482
2483 do
2484 {
2485 while( ( TU.p[0] & 1 ) == 0 )
2486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TU, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002488
2489 if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
2490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &U1, &U1, &TB ) );
2492 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 }
2494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U1, 1 ) );
2496 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002497 }
2498
2499 while( ( TV.p[0] & 1 ) == 0 )
2500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TV, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002502
2503 if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
2504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, &TB ) );
2506 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002507 }
2508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V1, 1 ) );
2510 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002511 }
2512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 if( mbedtls_mpi_cmp_mpi( &TU, &TV ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TU, &TU, &TV ) );
2516 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U1, &U1, &V1 ) );
2517 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &V2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002518 }
2519 else
2520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TV, &TV, &TU ) );
2522 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, &U1 ) );
2523 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &U2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002524 }
2525 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526 while( mbedtls_mpi_cmp_int( &TU, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 while( mbedtls_mpi_cmp_int( &V1, 0 ) < 0 )
2529 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 while( mbedtls_mpi_cmp_mpi( &V1, N ) >= 0 )
2532 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &V1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002535
2536cleanup:
2537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TU ); mbedtls_mpi_free( &U1 ); mbedtls_mpi_free( &U2 );
2539 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TV );
2540 mbedtls_mpi_free( &V1 ); mbedtls_mpi_free( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002541
2542 return( ret );
2543}
2544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002546
Paul Bakker5121ce52009-01-03 21:22:43 +00002547static const int small_prime[] =
2548{
2549 3, 5, 7, 11, 13, 17, 19, 23,
2550 29, 31, 37, 41, 43, 47, 53, 59,
2551 61, 67, 71, 73, 79, 83, 89, 97,
2552 101, 103, 107, 109, 113, 127, 131, 137,
2553 139, 149, 151, 157, 163, 167, 173, 179,
2554 181, 191, 193, 197, 199, 211, 223, 227,
2555 229, 233, 239, 241, 251, 257, 263, 269,
2556 271, 277, 281, 283, 293, 307, 311, 313,
2557 317, 331, 337, 347, 349, 353, 359, 367,
2558 373, 379, 383, 389, 397, 401, 409, 419,
2559 421, 431, 433, 439, 443, 449, 457, 461,
2560 463, 467, 479, 487, 491, 499, 503, 509,
2561 521, 523, 541, 547, 557, 563, 569, 571,
2562 577, 587, 593, 599, 601, 607, 613, 617,
2563 619, 631, 641, 643, 647, 653, 659, 661,
2564 673, 677, 683, 691, 701, 709, 719, 727,
2565 733, 739, 743, 751, 757, 761, 769, 773,
2566 787, 797, 809, 811, 821, 823, 827, 829,
2567 839, 853, 857, 859, 863, 877, 881, 883,
2568 887, 907, 911, 919, 929, 937, 941, 947,
2569 953, 967, 971, 977, 983, 991, 997, -103
2570};
2571
2572/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002573 * Small divisors test (X must be positive)
2574 *
2575 * Return values:
2576 * 0: no small factor (possible prime, more tests needed)
2577 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002579 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581static int mpi_check_small_factors( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +00002582{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002583 int ret = 0;
2584 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585 mbedtls_mpi_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 if( ( X->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002589
2590 for( i = 0; small_prime[i] > 0; i++ )
2591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592 if( mbedtls_mpi_cmp_int( X, small_prime[i] ) <= 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002593 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, small_prime[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002596
2597 if( r == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 }
2600
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002601cleanup:
2602 return( ret );
2603}
2604
2605/*
2606 * Miller-Rabin pseudo-primality test (HAC 4.24)
2607 */
Janos Follathda31fa12018-09-03 14:45:23 +01002608static int mpi_miller_rabin( const mbedtls_mpi *X, size_t rounds,
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002609 int (*f_rng)(void *, unsigned char *, size_t),
2610 void *p_rng )
2611{
Pascal Junodb99183d2015-03-11 16:49:45 +01002612 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002613 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002615
Hanno Becker8ce11a32018-12-19 16:18:52 +00002616 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002617 MPI_VALIDATE_RET( f_rng != NULL );
2618
2619 mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R );
2620 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 mbedtls_mpi_init( &RR );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002622
Paul Bakker5121ce52009-01-03 21:22:43 +00002623 /*
2624 * W = |X| - 1
2625 * R = W >> lsb( W )
2626 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &W, X, 1 ) );
2628 s = mbedtls_mpi_lsb( &W );
2629 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R, &W ) );
2630 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &R, s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
Janos Follathda31fa12018-09-03 14:45:23 +01002632 for( i = 0; i < rounds; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002633 {
2634 /*
2635 * pick a random A, 1 < A < |X| - 1
2636 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002637 count = 0;
2638 do {
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002639 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
Pascal Junodb99183d2015-03-11 16:49:45 +01002640
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002641 j = mbedtls_mpi_bitlen( &A );
2642 k = mbedtls_mpi_bitlen( &W );
Pascal Junodb99183d2015-03-11 16:49:45 +01002643 if (j > k) {
Darryl Greene3f95ed2018-10-02 13:21:35 +01002644 A.p[A.n - 1] &= ( (mbedtls_mpi_uint) 1 << ( k - ( A.n - 1 ) * biL - 1 ) ) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002645 }
2646
2647 if (count++ > 30) {
Jens Wiklanderdfd447e2019-01-17 13:30:57 +01002648 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2649 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002650 }
2651
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002652 } while ( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 ||
2653 mbedtls_mpi_cmp_int( &A, 1 ) <= 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
2655 /*
2656 * A = A^R mod |X|
2657 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &A, &A, &R, X, &RR ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 if( mbedtls_mpi_cmp_mpi( &A, &W ) == 0 ||
2661 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002662 continue;
2663
2664 j = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 while( j < s && mbedtls_mpi_cmp_mpi( &A, &W ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002666 {
2667 /*
2668 * A = A * A mod |X|
2669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &A, &A ) );
2671 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &A, &T, X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673 if( mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 break;
2675
2676 j++;
2677 }
2678
2679 /*
2680 * not prime if A != |X| - 1 or A == 1
2681 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 if( mbedtls_mpi_cmp_mpi( &A, &W ) != 0 ||
2683 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002686 break;
2687 }
2688 }
2689
2690cleanup:
Hanno Becker73d7d792018-12-11 10:35:51 +00002691 mbedtls_mpi_free( &W ); mbedtls_mpi_free( &R );
2692 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002694
2695 return( ret );
2696}
2697
2698/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002699 * Pseudo-primality test: small factors, then Miller-Rabin
2700 */
Janos Follatha0b67c22018-09-18 14:48:23 +01002701int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds,
2702 int (*f_rng)(void *, unsigned char *, size_t),
2703 void *p_rng )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002704{
2705 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 mbedtls_mpi XX;
Hanno Becker8ce11a32018-12-19 16:18:52 +00002707 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002708 MPI_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002709
2710 XX.s = 1;
2711 XX.n = X->n;
2712 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 if( mbedtls_mpi_cmp_int( &XX, 0 ) == 0 ||
2715 mbedtls_mpi_cmp_int( &XX, 1 ) == 0 )
2716 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 if( mbedtls_mpi_cmp_int( &XX, 2 ) == 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002719 return( 0 );
2720
2721 if( ( ret = mpi_check_small_factors( &XX ) ) != 0 )
2722 {
2723 if( ret == 1 )
2724 return( 0 );
2725
2726 return( ret );
2727 }
2728
Janos Follathda31fa12018-09-03 14:45:23 +01002729 return( mpi_miller_rabin( &XX, rounds, f_rng, p_rng ) );
Janos Follathf301d232018-08-14 13:34:01 +01002730}
2731
Janos Follatha0b67c22018-09-18 14:48:23 +01002732#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Janos Follathf301d232018-08-14 13:34:01 +01002733/*
2734 * Pseudo-primality test, error probability 2^-80
2735 */
2736int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
2737 int (*f_rng)(void *, unsigned char *, size_t),
2738 void *p_rng )
2739{
Hanno Becker8ce11a32018-12-19 16:18:52 +00002740 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002741 MPI_VALIDATE_RET( f_rng != NULL );
2742
Janos Follatha0b67c22018-09-18 14:48:23 +01002743 /*
2744 * In the past our key generation aimed for an error rate of at most
2745 * 2^-80. Since this function is deprecated, aim for the same certainty
2746 * here as well.
2747 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002748 return( mbedtls_mpi_is_prime_ext( X, 40, f_rng, p_rng ) );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002749}
Janos Follatha0b67c22018-09-18 14:48:23 +01002750#endif
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002751
2752/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002753 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002754 *
Janos Follathf301d232018-08-14 13:34:01 +01002755 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2756 * be either 1024 bits or 1536 bits long, and flags must contain
2757 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002758 */
Janos Follath7c025a92018-08-14 11:08:41 +01002759int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002760 int (*f_rng)(void *, unsigned char *, size_t),
2761 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002762{
Jethro Beekman66689272018-02-14 19:24:10 -08002763#ifdef MBEDTLS_HAVE_INT64
2764// ceil(2^63.5)
2765#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2766#else
2767// ceil(2^31.5)
2768#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2769#endif
2770 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002771 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002772 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 mbedtls_mpi_uint r;
2774 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002775
Hanno Becker8ce11a32018-12-19 16:18:52 +00002776 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002777 MPI_VALIDATE_RET( f_rng != NULL );
2778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779 if( nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS )
2780 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782 mbedtls_mpi_init( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002783
2784 n = BITS_TO_LIMBS( nbits );
2785
Janos Follathda31fa12018-09-03 14:45:23 +01002786 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR ) == 0 )
2787 {
2788 /*
2789 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2790 */
2791 rounds = ( ( nbits >= 1300 ) ? 2 : ( nbits >= 850 ) ? 3 :
2792 ( nbits >= 650 ) ? 4 : ( nbits >= 350 ) ? 8 :
2793 ( nbits >= 250 ) ? 12 : ( nbits >= 150 ) ? 18 : 27 );
2794 }
2795 else
2796 {
2797 /*
2798 * 2^-100 error probability, number of rounds computed based on HAC,
2799 * fact 4.48
2800 */
2801 rounds = ( ( nbits >= 1450 ) ? 4 : ( nbits >= 1150 ) ? 5 :
2802 ( nbits >= 1000 ) ? 6 : ( nbits >= 850 ) ? 7 :
2803 ( nbits >= 750 ) ? 8 : ( nbits >= 500 ) ? 13 :
2804 ( nbits >= 250 ) ? 28 : ( nbits >= 150 ) ? 40 : 51 );
2805 }
2806
Jethro Beekman66689272018-02-14 19:24:10 -08002807 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002808 {
Jethro Beekman66689272018-02-14 19:24:10 -08002809 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
2810 /* make sure generated number is at least (nbits-1)+0.5 bits (FIPS 186-4 §B.3.3 steps 4.4, 5.5) */
2811 if( X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2 ) continue;
2812
2813 k = n * biL;
2814 if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits ) );
2815 X->p[0] |= 1;
2816
Janos Follath7c025a92018-08-14 11:08:41 +01002817 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002818 {
Janos Follatha0b67c22018-09-18 14:48:23 +01002819 ret = mbedtls_mpi_is_prime_ext( X, rounds, f_rng, p_rng );
Jethro Beekman66689272018-02-14 19:24:10 -08002820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002822 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002823 }
Jethro Beekman66689272018-02-14 19:24:10 -08002824 else
Paul Bakker5121ce52009-01-03 21:22:43 +00002825 {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002826 /*
Jethro Beekman66689272018-02-14 19:24:10 -08002827 * An necessary condition for Y and X = 2Y + 1 to be prime
2828 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2829 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002830 */
Jethro Beekman66689272018-02-14 19:24:10 -08002831
2832 X->p[0] |= 2;
2833
2834 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, 3 ) );
2835 if( r == 0 )
2836 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 8 ) );
2837 else if( r == 1 )
2838 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 4 ) );
2839
2840 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
2841 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, X ) );
2842 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, 1 ) );
2843
2844 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002845 {
Jethro Beekman66689272018-02-14 19:24:10 -08002846 /*
2847 * First, check small factors for X and Y
2848 * before doing Miller-Rabin on any of them
2849 */
2850 if( ( ret = mpi_check_small_factors( X ) ) == 0 &&
2851 ( ret = mpi_check_small_factors( &Y ) ) == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002852 ( ret = mpi_miller_rabin( X, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002853 == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002854 ( ret = mpi_miller_rabin( &Y, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002855 == 0 )
Jethro Beekman66689272018-02-14 19:24:10 -08002856 goto cleanup;
2857
2858 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
2859 goto cleanup;
2860
2861 /*
2862 * Next candidates. We want to preserve Y = (X-1) / 2 and
2863 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2864 * so up Y by 6 and X by 12.
2865 */
2866 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 12 ) );
2867 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &Y, &Y, 6 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002868 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002869 }
2870 }
2871
2872cleanup:
2873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874 mbedtls_mpi_free( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002875
2876 return( ret );
2877}
2878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002882
Paul Bakker23986e52011-04-24 08:57:21 +00002883#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002884
2885static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2886{
2887 { 693, 609, 21 },
2888 { 1764, 868, 28 },
2889 { 768454923, 542167814, 1 }
2890};
2891
Paul Bakker5121ce52009-01-03 21:22:43 +00002892/*
2893 * Checkup routine
2894 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895int mbedtls_mpi_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002896{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002897 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00002899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); mbedtls_mpi_init( &X );
2901 mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &U ); mbedtls_mpi_init( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00002902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &A, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002904 "EFE021C2645FD1DC586E69184AF4A31E" \
2905 "D5F53E93B5F123FA41680867BA110131" \
2906 "944FE7952E2517337780CB0DB80E61AA" \
2907 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6" ) );
2908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002910 "B2E7EFD37075B9F03FF989C7C5051C20" \
2911 "34D2A323810251127E7BF8625A4F49A5" \
2912 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
2913 "5B5C25763222FEFCCFC38B832366C29E" ) );
2914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &N, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002916 "0066A198186C18C10B2F5ED9B522752A" \
2917 "9830B69916E535C8F047518A889A43A5" \
2918 "94B6BED27A168D31D4A52F88925AA8F5" ) );
2919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002923 "602AB7ECA597A3D6B56FF9829A5E8B85" \
2924 "9E857EA95A03512E2BAE7391688D264A" \
2925 "A5663B0341DB9CCFD2C4C5F421FEC814" \
2926 "8001B72E848A38CAE1C65F78E56ABDEF" \
2927 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
2928 "ECF677152EF804370C1A305CAF3B5BF1" \
2929 "30879B56C61DE584A0F53A2447A51E" ) );
2930
2931 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 mbedtls_printf( " MPI test #1 (mul_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002935 {
2936 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002938
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002939 ret = 1;
2940 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002941 }
2942
2943 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &X, &Y, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002948 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002949 "256567336059E52CAE22925474705F39A94" ) );
2950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &V, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002952 "6613F26162223DF488E9CD48CC132C7A" \
2953 "0AC93C701B001B092E4E5B9F73BCD27B" \
2954 "9EE50D0657C77F374E903CDFA4C642" ) );
2955
2956 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957 mbedtls_printf( " MPI test #2 (div_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ||
2960 mbedtls_mpi_cmp_mpi( &Y, &V ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002961 {
2962 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002964
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002965 ret = 1;
2966 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002967 }
2968
2969 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &X, &A, &E, &N, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002975 "36E139AEA55215609D2816998ED020BB" \
2976 "BD96C37890F65171D948E9BC7CBAA4D9" \
2977 "325D24D6A3C12710F10A09FA08AB87" ) );
2978
2979 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980 mbedtls_printf( " MPI test #3 (exp_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002983 {
2984 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002986
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002987 ret = 1;
2988 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002989 }
2990
2991 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002997 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
2998 "C3DBA76456363A10869622EAC2DD84EC" \
2999 "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) );
3000
3001 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002 mbedtls_printf( " MPI test #4 (inv_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003004 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003005 {
3006 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003008
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003009 ret = 1;
3010 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003011 }
3012
3013 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003015
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003016 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017 mbedtls_printf( " MPI test #5 (simple gcd): " );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003018
Paul Bakker66d5d072014-06-17 16:39:18 +02003019 for( i = 0; i < GCD_PAIR_COUNT; i++ )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &X, gcd_pairs[i][0] ) );
3022 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Y, gcd_pairs[i][1] ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &A, &X, &Y ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026 if( mbedtls_mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 )
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003027 {
3028 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003029 mbedtls_printf( "failed at %d\n", i );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003030
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003031 ret = 1;
3032 goto cleanup;
3033 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003034 }
3035
3036 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037 mbedtls_printf( "passed\n" );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003038
Paul Bakker5121ce52009-01-03 21:22:43 +00003039cleanup:
3040
3041 if( ret != 0 && verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042 mbedtls_printf( "Unexpected error, return code = %08X\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003044 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &X );
3045 mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &U ); mbedtls_mpi_free( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00003046
3047 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003049
3050 return( ret );
3051}
3052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055#endif /* MBEDTLS_BIGNUM_C */