blob: 30226e1e5c3ffe74511952125043a2024b3c5d33 [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
Manuel Pégourié-Gonnarddc6a5f22021-06-07 09:51:00 +0200270/**
271 * Select between two sign values in constant-time.
272 *
273 * This is functionally equivalent to second ? a : b but uses only bit
274 * operations in order to avoid branches.
275 *
276 * \param[in] a The first sign; must be either +1 or -1.
277 * \param[in] b The second sign; must be either +1 or -1.
278 * \param[in] second Must be either 1 (return b) or 0 (return a).
279 *
280 * \return The selected sign value.
281 */
282static int mpi_safe_cond_select_sign( int a, int b, unsigned char second )
283{
284 /* In order to avoid questions about what we can reasonnably assume about
285 * the representations of signed integers, move everything to unsigned
286 * by taking advantage of the fact that a and b are either +1 or -1. */
287 unsigned ua = a + 1;
288 unsigned ub = b + 1;
289
Manuel Pégourié-Gonnard12f02382021-06-10 09:36:41 +0200290 /* second was 0 or 1, mask is 0 or 2 as are ua and ub */
291 const unsigned mask = second << 1;
Manuel Pégourié-Gonnarddc6a5f22021-06-07 09:51:00 +0200292
293 /* select ua or ub */
294 unsigned ur = ( ua & ~mask ) | ( ub & mask );
295
296 /* ur is now 0 or 2, convert back to -1 or +1 */
297 return( (int) ur - 1 );
298}
299
Paul Bakker5121ce52009-01-03 21:22:43 +0000300/*
Gilles Peskinec81c5882020-06-04 19:14:58 +0200301 * Conditionally assign dest = src, without leaking information
302 * about whether the assignment was made or not.
303 * dest and src must be arrays of limbs of size n.
304 * assign must be 0 or 1.
305 */
306static void mpi_safe_cond_assign( size_t n,
307 mbedtls_mpi_uint *dest,
308 const mbedtls_mpi_uint *src,
309 unsigned char assign )
310{
311 size_t i;
Manuel Pégourié-Gonnard245a8062021-05-31 11:48:45 +0200312
313 /* MSVC has a warning about unary minus on unsigned integer types,
314 * but this is well-defined and precisely what we want to do here. */
315#if defined(_MSC_VER)
316#pragma warning( push )
317#pragma warning( disable : 4146 )
318#endif
319
320 /* all-bits 1 if assign is 1, all-bits 0 if assign is 0 */
321 const mbedtls_mpi_uint mask = -assign;
322
323#if defined(_MSC_VER)
324#pragma warning( pop )
325#endif
326
Gilles Peskinec81c5882020-06-04 19:14:58 +0200327 for( i = 0; i < n; i++ )
Manuel Pégourié-Gonnard245a8062021-05-31 11:48:45 +0200328 dest[i] = ( src[i] & mask ) | ( dest[i] & ~mask );
Gilles Peskinec81c5882020-06-04 19:14:58 +0200329}
330
331/*
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100332 * Conditionally assign X = Y, without leaking information
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +0100333 * about whether the assignment was made or not.
334 * (Leaking information about the respective sizes of X and Y is ok however.)
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100337{
338 int ret = 0;
339 size_t i;
Manuel Pégourié-Gonnard245a8062021-05-31 11:48:45 +0200340 mbedtls_mpi_uint limb_mask;
Hanno Becker73d7d792018-12-11 10:35:51 +0000341 MPI_VALIDATE_RET( X != NULL );
342 MPI_VALIDATE_RET( Y != NULL );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100343
Manuel Pégourié-Gonnard245a8062021-05-31 11:48:45 +0200344 /* MSVC has a warning about unary minus on unsigned integer types,
345 * but this is well-defined and precisely what we want to do here. */
346#if defined(_MSC_VER)
347#pragma warning( push )
348#pragma warning( disable : 4146 )
349#endif
350
Pascal Junodb99183d2015-03-11 16:49:45 +0100351 /* make sure assign is 0 or 1 in a time-constant manner */
352 assign = (assign | (unsigned char)-assign) >> 7;
Manuel Pégourié-Gonnard245a8062021-05-31 11:48:45 +0200353 /* all-bits 1 if assign is 1, all-bits 0 if assign is 0 */
Manuel Pégourié-Gonnard245a8062021-05-31 11:48:45 +0200354 limb_mask = -assign;
355
356#if defined(_MSC_VER)
357#pragma warning( pop )
358#endif
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100361
Manuel Pégourié-Gonnarddc6a5f22021-06-07 09:51:00 +0200362 X->s = mpi_safe_cond_select_sign( X->s, Y->s, assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100363
Gilles Peskinec81c5882020-06-04 19:14:58 +0200364 mpi_safe_cond_assign( Y->n, X->p, Y->p, assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100365
Gilles Peskinec81c5882020-06-04 19:14:58 +0200366 for( i = Y->n; i < X->n; i++ )
Manuel Pégourié-Gonnard245a8062021-05-31 11:48:45 +0200367 X->p[i] &= ~limb_mask;
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100368
369cleanup:
370 return( ret );
371}
372
373/*
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100374 * Conditionally swap X and Y, without leaking information
375 * about whether the swap was made or not.
376 * Here it is not ok to simply swap the pointers, which whould lead to
377 * different memory access patterns when X and Y are used afterwards.
378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100380{
381 int ret, s;
382 size_t i;
Manuel Pégourié-Gonnarda1283cc2021-06-03 10:54:01 +0200383 mbedtls_mpi_uint limb_mask;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 mbedtls_mpi_uint tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +0000385 MPI_VALIDATE_RET( X != NULL );
386 MPI_VALIDATE_RET( Y != NULL );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100387
388 if( X == Y )
389 return( 0 );
390
Manuel Pégourié-Gonnarda1283cc2021-06-03 10:54:01 +0200391 /* MSVC has a warning about unary minus on unsigned integer types,
392 * but this is well-defined and precisely what we want to do here. */
393#if defined(_MSC_VER)
394#pragma warning( push )
395#pragma warning( disable : 4146 )
396#endif
397
Pascal Junodb99183d2015-03-11 16:49:45 +0100398 /* make sure swap is 0 or 1 in a time-constant manner */
399 swap = (swap | (unsigned char)-swap) >> 7;
Manuel Pégourié-Gonnarda1283cc2021-06-03 10:54:01 +0200400 /* all-bits 1 if swap is 1, all-bits 0 if swap is 0 */
Manuel Pégourié-Gonnarda1283cc2021-06-03 10:54:01 +0200401 limb_mask = -swap;
402
403#if defined(_MSC_VER)
404#pragma warning( pop )
405#endif
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
408 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( Y, X->n ) );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100409
410 s = X->s;
Manuel Pégourié-Gonnarddc6a5f22021-06-07 09:51:00 +0200411 X->s = mpi_safe_cond_select_sign( X->s, Y->s, swap );
412 Y->s = mpi_safe_cond_select_sign( Y->s, s, swap );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100413
414
415 for( i = 0; i < X->n; i++ )
416 {
417 tmp = X->p[i];
Manuel Pégourié-Gonnarda1283cc2021-06-03 10:54:01 +0200418 X->p[i] = ( X->p[i] & ~limb_mask ) | ( Y->p[i] & limb_mask );
419 Y->p[i] = ( Y->p[i] & ~limb_mask ) | ( tmp & limb_mask );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100420 }
421
422cleanup:
423 return( ret );
424}
425
426/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 * Set value from integer
428 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +0000430{
431 int ret;
Hanno Becker73d7d792018-12-11 10:35:51 +0000432 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435 memset( X->p, 0, X->n * ciL );
436
437 X->p[0] = ( z < 0 ) ? -z : z;
438 X->s = ( z < 0 ) ? -1 : 1;
439
440cleanup:
441
442 return( ret );
443}
444
445/*
Paul Bakker2f5947e2011-05-18 15:47:11 +0000446 * Get a specific bit
447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000449{
Hanno Becker73d7d792018-12-11 10:35:51 +0000450 MPI_VALIDATE_RET( X != NULL );
451
Paul Bakker2f5947e2011-05-18 15:47:11 +0000452 if( X->n * biL <= pos )
453 return( 0 );
454
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200455 return( ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000456}
457
Gilles Peskine11cdb052018-11-20 16:47:47 +0100458/* Get a specific byte, without range checks. */
459#define GET_BYTE( X, i ) \
460 ( ( ( X )->p[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff )
461
Paul Bakker2f5947e2011-05-18 15:47:11 +0000462/*
463 * Set a bit to a specific value of 0 or 1
464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000466{
467 int ret = 0;
468 size_t off = pos / biL;
469 size_t idx = pos % biL;
Hanno Becker73d7d792018-12-11 10:35:51 +0000470 MPI_VALIDATE_RET( X != NULL );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000471
472 if( val != 0 && val != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker9af723c2014-05-01 13:03:14 +0200474
Paul Bakker2f5947e2011-05-18 15:47:11 +0000475 if( X->n * biL <= pos )
476 {
477 if( val == 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200478 return( 0 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, off + 1 ) );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000481 }
482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 X->p[off] &= ~( (mbedtls_mpi_uint) 0x01 << idx );
484 X->p[off] |= (mbedtls_mpi_uint) val << idx;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000485
486cleanup:
Paul Bakker9af723c2014-05-01 13:03:14 +0200487
Paul Bakker2f5947e2011-05-18 15:47:11 +0000488 return( ret );
489}
490
491/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200492 * Return the number of less significant zero-bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494size_t mbedtls_mpi_lsb( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000495{
Paul Bakker23986e52011-04-24 08:57:21 +0000496 size_t i, j, count = 0;
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000497 MBEDTLS_INTERNAL_VALIDATE_RET( X != NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000498
499 for( i = 0; i < X->n; i++ )
Paul Bakkerf9688572011-05-05 10:00:45 +0000500 for( j = 0; j < biL; j++, count++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 if( ( ( X->p[i] >> j ) & 1 ) != 0 )
502 return( count );
503
504 return( 0 );
505}
506
507/*
Simon Butcher15b15d12015-11-26 19:35:03 +0000508 * Count leading zero bits in a given integer
509 */
510static size_t mbedtls_clz( const mbedtls_mpi_uint x )
511{
512 size_t j;
Manuel Pégourié-Gonnarde3e8edf2015-12-01 09:31:52 +0100513 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
Simon Butcher15b15d12015-11-26 19:35:03 +0000514
515 for( j = 0; j < biL; j++ )
516 {
517 if( x & mask ) break;
518
519 mask >>= 1;
520 }
521
522 return j;
523}
524
525/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200526 * Return the number of bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000527 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200528size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000529{
Paul Bakker23986e52011-04-24 08:57:21 +0000530 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200532 if( X->n == 0 )
533 return( 0 );
534
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 for( i = X->n - 1; i > 0; i-- )
536 if( X->p[i] != 0 )
537 break;
538
Simon Butcher15b15d12015-11-26 19:35:03 +0000539 j = biL - mbedtls_clz( X->p[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Paul Bakker23986e52011-04-24 08:57:21 +0000541 return( ( i * biL ) + j );
Paul Bakker5121ce52009-01-03 21:22:43 +0000542}
543
544/*
545 * Return the total size in bytes
546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547size_t mbedtls_mpi_size( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000548{
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200549 return( ( mbedtls_mpi_bitlen( X ) + 7 ) >> 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000550}
551
552/*
553 * Convert an ASCII character to digit value
554 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555static int mpi_get_digit( mbedtls_mpi_uint *d, int radix, char c )
Paul Bakker5121ce52009-01-03 21:22:43 +0000556{
557 *d = 255;
558
559 if( c >= 0x30 && c <= 0x39 ) *d = c - 0x30;
560 if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37;
561 if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57;
562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 if( *d >= (mbedtls_mpi_uint) radix )
564 return( MBEDTLS_ERR_MPI_INVALID_CHARACTER );
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
566 return( 0 );
567}
568
569/*
570 * Import from an ASCII string
571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000573{
Paul Bakker23986e52011-04-24 08:57:21 +0000574 int ret;
575 size_t i, j, slen, n;
Gilles Peskine984fd072021-04-03 18:26:13 +0200576 int sign = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_mpi_uint d;
578 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000579 MPI_VALIDATE_RET( X != NULL );
580 MPI_VALIDATE_RET( s != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000581
582 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000583 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
Gilles Peskine984fd072021-04-03 18:26:13 +0200587 if( s[0] == '-' )
588 {
589 ++s;
590 sign = -1;
591 }
592
Paul Bakkerff60ee62010-03-16 21:09:09 +0000593 slen = strlen( s );
594
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 if( radix == 16 )
596 {
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +0100597 if( slen > MPI_SIZE_T_MAX >> 2 )
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +0200598 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
599
Paul Bakkerff60ee62010-03-16 21:09:09 +0000600 n = BITS_TO_LIMBS( slen << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n ) );
603 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000604
Paul Bakker23986e52011-04-24 08:57:21 +0000605 for( i = slen, j = 0; i > 0; i--, j++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200608 X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000609 }
610 }
611 else
612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000614
Paul Bakkerff60ee62010-03-16 21:09:09 +0000615 for( i = 0; i < slen; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
618 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T, X, radix ) );
Gilles Peskine984fd072021-04-03 18:26:13 +0200619 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, &T, d ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 }
621 }
622
Gilles Peskine984fd072021-04-03 18:26:13 +0200623 if( sign < 0 && mbedtls_mpi_bitlen( X ) != 0 )
624 X->s = -1;
625
Paul Bakker5121ce52009-01-03 21:22:43 +0000626cleanup:
627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
630 return( ret );
631}
632
633/*
Ron Eldora16fa292018-11-20 14:07:01 +0200634 * Helper to write the digits high-order first.
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 */
Ron Eldora16fa292018-11-20 14:07:01 +0200636static int mpi_write_hlp( mbedtls_mpi *X, int radix,
637 char **p, const size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000638{
639 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 mbedtls_mpi_uint r;
Ron Eldora16fa292018-11-20 14:07:01 +0200641 size_t length = 0;
642 char *p_end = *p + buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000643
Ron Eldora16fa292018-11-20 14:07:01 +0200644 do
645 {
646 if( length >= buflen )
647 {
648 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
649 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000650
Ron Eldora16fa292018-11-20 14:07:01 +0200651 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) );
652 MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) );
653 /*
654 * Write the residue in the current position, as an ASCII character.
655 */
656 if( r < 0xA )
657 *(--p_end) = (char)( '0' + r );
658 else
659 *(--p_end) = (char)( 'A' + ( r - 0xA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000660
Ron Eldora16fa292018-11-20 14:07:01 +0200661 length++;
662 } while( mbedtls_mpi_cmp_int( X, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
Ron Eldora16fa292018-11-20 14:07:01 +0200664 memmove( *p, p_end, length );
665 *p += length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000666
667cleanup:
668
669 return( ret );
670}
671
672/*
673 * Export into an ASCII string
674 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100675int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
676 char *buf, size_t buflen, size_t *olen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000677{
Paul Bakker23986e52011-04-24 08:57:21 +0000678 int ret = 0;
679 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000680 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000682 MPI_VALIDATE_RET( X != NULL );
683 MPI_VALIDATE_RET( olen != NULL );
684 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000685
686 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000687 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
Hanno Beckerc1fa6cd2019-02-04 09:45:07 +0000689 n = mbedtls_mpi_bitlen( X ); /* Number of bits necessary to present `n`. */
690 if( radix >= 4 ) n >>= 1; /* Number of 4-adic digits necessary to present
691 * `n`. If radix > 4, this might be a strict
692 * overapproximation of the number of
693 * radix-adic digits needed to present `n`. */
694 if( radix >= 16 ) n >>= 1; /* Number of hexadecimal digits necessary to
695 * present `n`. */
696
Janos Follath870ed002019-03-06 13:43:02 +0000697 n += 1; /* Terminating null byte */
Hanno Beckerc1fa6cd2019-02-04 09:45:07 +0000698 n += 1; /* Compensate for the divisions above, which round down `n`
699 * in case it's not even. */
700 n += 1; /* Potential '-'-sign. */
701 n += ( n & 1 ); /* Make n even to have enough space for hexadecimal writing,
702 * which always uses an even number of hex-digits. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100704 if( buflen < n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000705 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100706 *olen = n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000708 }
709
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100710 p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000712
713 if( X->s == -1 )
Hanno Beckeraf97cae2019-02-01 16:41:30 +0000714 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000715 *p++ = '-';
Hanno Beckeraf97cae2019-02-01 16:41:30 +0000716 buflen--;
717 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000718
719 if( radix == 16 )
720 {
Paul Bakker23986e52011-04-24 08:57:21 +0000721 int c;
722 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000723
Paul Bakker23986e52011-04-24 08:57:21 +0000724 for( i = X->n, k = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000725 {
Paul Bakker23986e52011-04-24 08:57:21 +0000726 for( j = ciL; j > 0; j-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000727 {
Paul Bakker23986e52011-04-24 08:57:21 +0000728 c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000729
Paul Bakker6c343d72014-07-10 14:36:19 +0200730 if( c == 0 && k == 0 && ( i + j ) != 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000731 continue;
732
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000733 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000734 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000735 k = 1;
736 }
737 }
738 }
739 else
740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T, X ) );
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000742
743 if( T.s == -1 )
744 T.s = 1;
745
Ron Eldora16fa292018-11-20 14:07:01 +0200746 MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p, buflen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000747 }
748
749 *p++ = '\0';
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100750 *olen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
752cleanup:
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
756 return( ret );
757}
758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000760/*
761 * Read X from an opened file
762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin )
Paul Bakker5121ce52009-01-03 21:22:43 +0000764{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 mbedtls_mpi_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000766 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000767 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000768 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000769 * Buffer should have space for (short) label and decimal formatted MPI,
770 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000771 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Paul Bakker5121ce52009-01-03 21:22:43 +0000773
Hanno Becker73d7d792018-12-11 10:35:51 +0000774 MPI_VALIDATE_RET( X != NULL );
775 MPI_VALIDATE_RET( fin != NULL );
776
777 if( radix < 2 || radix > 16 )
778 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
779
Paul Bakker5121ce52009-01-03 21:22:43 +0000780 memset( s, 0, sizeof( s ) );
781 if( fgets( s, sizeof( s ) - 1, fin ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000783
784 slen = strlen( s );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000785 if( slen == sizeof( s ) - 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000787
Hanno Beckerb2034b72017-04-26 11:46:46 +0100788 if( slen > 0 && s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
789 if( slen > 0 && s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
Paul Bakker5121ce52009-01-03 21:22:43 +0000790
791 p = s + slen;
Hanno Beckerb2034b72017-04-26 11:46:46 +0100792 while( p-- > s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000793 if( mpi_get_digit( &d, radix, *p ) != 0 )
794 break;
795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 return( mbedtls_mpi_read_string( X, radix, p + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000797}
798
799/*
800 * Write X into an opened file (or stdout if fout == NULL)
801 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout )
Paul Bakker5121ce52009-01-03 21:22:43 +0000803{
Paul Bakker23986e52011-04-24 08:57:21 +0000804 int ret;
805 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000806 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000807 * Buffer should have space for (short) label and decimal formatted MPI,
808 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Hanno Becker73d7d792018-12-11 10:35:51 +0000811 MPI_VALIDATE_RET( X != NULL );
812
813 if( radix < 2 || radix > 16 )
814 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000815
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100816 memset( s, 0, sizeof( s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000817
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100818 MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, sizeof( s ) - 2, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000819
820 if( p == NULL ) p = "";
821
822 plen = strlen( p );
823 slen = strlen( s );
824 s[slen++] = '\r';
825 s[slen++] = '\n';
826
827 if( fout != NULL )
828 {
829 if( fwrite( p, 1, plen, fout ) != plen ||
830 fwrite( s, 1, slen, fout ) != slen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000832 }
833 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 mbedtls_printf( "%s%s", p, s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000835
836cleanup:
837
838 return( ret );
839}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000841
Hanno Beckerda1655a2017-10-18 14:21:44 +0100842
843/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
844 * into the storage form used by mbedtls_mpi. */
Hanno Beckerf8720072018-11-08 11:53:49 +0000845
846static mbedtls_mpi_uint mpi_uint_bigendian_to_host_c( mbedtls_mpi_uint x )
847{
848 uint8_t i;
Hanno Becker92c98932019-05-01 17:09:11 +0100849 unsigned char *x_ptr;
Hanno Beckerf8720072018-11-08 11:53:49 +0000850 mbedtls_mpi_uint tmp = 0;
Hanno Becker92c98932019-05-01 17:09:11 +0100851
852 for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
853 {
854 tmp <<= CHAR_BIT;
855 tmp |= (mbedtls_mpi_uint) *x_ptr;
856 }
857
Hanno Beckerf8720072018-11-08 11:53:49 +0000858 return( tmp );
859}
860
861static mbedtls_mpi_uint mpi_uint_bigendian_to_host( mbedtls_mpi_uint x )
862{
863#if defined(__BYTE_ORDER__)
864
865/* Nothing to do on bigendian systems. */
866#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
867 return( x );
868#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
869
870#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
871
872/* For GCC and Clang, have builtins for byte swapping. */
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000873#if defined(__GNUC__) && defined(__GNUC_PREREQ)
874#if __GNUC_PREREQ(4,3)
Hanno Beckerf8720072018-11-08 11:53:49 +0000875#define have_bswap
876#endif
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000877#endif
878
879#if defined(__clang__) && defined(__has_builtin)
880#if __has_builtin(__builtin_bswap32) && \
881 __has_builtin(__builtin_bswap64)
882#define have_bswap
883#endif
884#endif
885
Hanno Beckerf8720072018-11-08 11:53:49 +0000886#if defined(have_bswap)
887 /* The compiler is hopefully able to statically evaluate this! */
888 switch( sizeof(mbedtls_mpi_uint) )
889 {
890 case 4:
891 return( __builtin_bswap32(x) );
892 case 8:
893 return( __builtin_bswap64(x) );
894 }
895#endif
896#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
897#endif /* __BYTE_ORDER__ */
898
899 /* Fall back to C-based reordering if we don't know the byte order
900 * or we couldn't use a compiler-specific builtin. */
901 return( mpi_uint_bigendian_to_host_c( x ) );
902}
903
Hanno Becker2be8a552018-10-25 12:40:09 +0100904static void mpi_bigendian_to_host( mbedtls_mpi_uint * const p, size_t limbs )
Hanno Beckerda1655a2017-10-18 14:21:44 +0100905{
Hanno Beckerda1655a2017-10-18 14:21:44 +0100906 mbedtls_mpi_uint *cur_limb_left;
907 mbedtls_mpi_uint *cur_limb_right;
Hanno Becker2be8a552018-10-25 12:40:09 +0100908 if( limbs == 0 )
909 return;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100910
911 /*
912 * Traverse limbs and
913 * - adapt byte-order in each limb
914 * - swap the limbs themselves.
915 * For that, simultaneously traverse the limbs from left to right
916 * and from right to left, as long as the left index is not bigger
917 * than the right index (it's not a problem if limbs is odd and the
918 * indices coincide in the last iteration).
919 */
Hanno Beckerda1655a2017-10-18 14:21:44 +0100920 for( cur_limb_left = p, cur_limb_right = p + ( limbs - 1 );
921 cur_limb_left <= cur_limb_right;
922 cur_limb_left++, cur_limb_right-- )
923 {
Hanno Beckerf8720072018-11-08 11:53:49 +0000924 mbedtls_mpi_uint tmp;
925 /* Note that if cur_limb_left == cur_limb_right,
926 * this code effectively swaps the bytes only once. */
927 tmp = mpi_uint_bigendian_to_host( *cur_limb_left );
928 *cur_limb_left = mpi_uint_bigendian_to_host( *cur_limb_right );
929 *cur_limb_right = tmp;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100930 }
Hanno Beckerda1655a2017-10-18 14:21:44 +0100931}
932
Paul Bakker5121ce52009-01-03 21:22:43 +0000933/*
934 * Import X from unsigned binary data, big endian
935 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000937{
Paul Bakker23986e52011-04-24 08:57:21 +0000938 int ret;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100939 size_t const limbs = CHARS_TO_LIMBS( buflen );
940 size_t const overhead = ( limbs * ciL ) - buflen;
941 unsigned char *Xp;
Paul Bakker5121ce52009-01-03 21:22:43 +0000942
Hanno Becker8ce11a32018-12-19 16:18:52 +0000943 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +0000944 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
945
Hanno Becker073c1992017-10-17 15:17:27 +0100946 /* Ensure that target MPI has exactly the necessary number of limbs */
947 if( X->n != limbs )
948 {
949 mbedtls_mpi_free( X );
950 mbedtls_mpi_init( X );
951 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, limbs ) );
952 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000954
Hanno Becker0e810b92019-01-03 17:13:11 +0000955 /* Avoid calling `memcpy` with NULL source argument,
956 * even if buflen is 0. */
957 if( buf != NULL )
958 {
959 Xp = (unsigned char*) X->p;
960 memcpy( Xp + overhead, buf, buflen );
Hanno Beckerda1655a2017-10-18 14:21:44 +0100961
Hanno Becker0e810b92019-01-03 17:13:11 +0000962 mpi_bigendian_to_host( X->p, limbs );
963 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000964
965cleanup:
966
967 return( ret );
968}
969
970/*
971 * Export X into unsigned binary data, big endian
972 */
Gilles Peskine11cdb052018-11-20 16:47:47 +0100973int mbedtls_mpi_write_binary( const mbedtls_mpi *X,
974 unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000975{
Hanno Becker73d7d792018-12-11 10:35:51 +0000976 size_t stored_bytes;
Gilles Peskine11cdb052018-11-20 16:47:47 +0100977 size_t bytes_to_copy;
978 unsigned char *p;
979 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000980
Hanno Becker73d7d792018-12-11 10:35:51 +0000981 MPI_VALIDATE_RET( X != NULL );
982 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
983
984 stored_bytes = X->n * ciL;
985
Gilles Peskine11cdb052018-11-20 16:47:47 +0100986 if( stored_bytes < buflen )
987 {
988 /* There is enough space in the output buffer. Write initial
989 * null bytes and record the position at which to start
990 * writing the significant bytes. In this case, the execution
991 * trace of this function does not depend on the value of the
992 * number. */
993 bytes_to_copy = stored_bytes;
994 p = buf + buflen - stored_bytes;
995 memset( buf, 0, buflen - stored_bytes );
996 }
997 else
998 {
999 /* The output buffer is smaller than the allocated size of X.
1000 * However X may fit if its leading bytes are zero. */
1001 bytes_to_copy = buflen;
1002 p = buf;
1003 for( i = bytes_to_copy; i < stored_bytes; i++ )
1004 {
1005 if( GET_BYTE( X, i ) != 0 )
1006 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
1007 }
1008 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001009
Gilles Peskine11cdb052018-11-20 16:47:47 +01001010 for( i = 0; i < bytes_to_copy; i++ )
1011 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
Paul Bakker5121ce52009-01-03 21:22:43 +00001012
1013 return( 0 );
1014}
1015
1016/*
1017 * Left-shift: X <<= count
1018 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +00001020{
Paul Bakker23986e52011-04-24 08:57:21 +00001021 int ret;
1022 size_t i, v0, t1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +00001024 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001025
1026 v0 = count / (biL );
1027 t1 = count & (biL - 1);
1028
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001029 i = mbedtls_mpi_bitlen( X ) + count;
Paul Bakker5121ce52009-01-03 21:22:43 +00001030
Paul Bakkerf9688572011-05-05 10:00:45 +00001031 if( X->n * biL < i )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001033
1034 ret = 0;
1035
1036 /*
1037 * shift by count / limb_size
1038 */
1039 if( v0 > 0 )
1040 {
Paul Bakker23986e52011-04-24 08:57:21 +00001041 for( i = X->n; i > v0; i-- )
1042 X->p[i - 1] = X->p[i - v0 - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001043
Paul Bakker23986e52011-04-24 08:57:21 +00001044 for( ; i > 0; i-- )
1045 X->p[i - 1] = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001046 }
1047
1048 /*
1049 * shift by count % limb_size
1050 */
1051 if( t1 > 0 )
1052 {
1053 for( i = v0; i < X->n; i++ )
1054 {
1055 r1 = X->p[i] >> (biL - t1);
1056 X->p[i] <<= t1;
1057 X->p[i] |= r0;
1058 r0 = r1;
1059 }
1060 }
1061
1062cleanup:
1063
1064 return( ret );
1065}
1066
1067/*
1068 * Right-shift: X >>= count
1069 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +00001071{
Paul Bakker23986e52011-04-24 08:57:21 +00001072 size_t i, v0, v1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +00001074 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001075
1076 v0 = count / biL;
1077 v1 = count & (biL - 1);
1078
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001079 if( v0 > X->n || ( v0 == X->n && v1 > 0 ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 return mbedtls_mpi_lset( X, 0 );
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001081
Paul Bakker5121ce52009-01-03 21:22:43 +00001082 /*
1083 * shift by count / limb_size
1084 */
1085 if( v0 > 0 )
1086 {
1087 for( i = 0; i < X->n - v0; i++ )
1088 X->p[i] = X->p[i + v0];
1089
1090 for( ; i < X->n; i++ )
1091 X->p[i] = 0;
1092 }
1093
1094 /*
1095 * shift by count % limb_size
1096 */
1097 if( v1 > 0 )
1098 {
Paul Bakker23986e52011-04-24 08:57:21 +00001099 for( i = X->n; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001100 {
Paul Bakker23986e52011-04-24 08:57:21 +00001101 r1 = X->p[i - 1] << (biL - v1);
1102 X->p[i - 1] >>= v1;
1103 X->p[i - 1] |= r0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 r0 = r1;
1105 }
1106 }
1107
1108 return( 0 );
1109}
1110
1111/*
1112 * Compare unsigned values
1113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001115{
Paul Bakker23986e52011-04-24 08:57:21 +00001116 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001117 MPI_VALIDATE_RET( X != NULL );
1118 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001119
Paul Bakker23986e52011-04-24 08:57:21 +00001120 for( i = X->n; i > 0; i-- )
1121 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001122 break;
1123
Paul Bakker23986e52011-04-24 08:57:21 +00001124 for( j = Y->n; j > 0; j-- )
1125 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001126 break;
1127
Paul Bakker23986e52011-04-24 08:57:21 +00001128 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001129 return( 0 );
1130
1131 if( i > j ) return( 1 );
1132 if( j > i ) return( -1 );
1133
Paul Bakker23986e52011-04-24 08:57:21 +00001134 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001135 {
Paul Bakker23986e52011-04-24 08:57:21 +00001136 if( X->p[i - 1] > Y->p[i - 1] ) return( 1 );
1137 if( X->p[i - 1] < Y->p[i - 1] ) return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001138 }
1139
1140 return( 0 );
1141}
1142
1143/*
1144 * Compare signed values
1145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001147{
Paul Bakker23986e52011-04-24 08:57:21 +00001148 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001149 MPI_VALIDATE_RET( X != NULL );
1150 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001151
Paul Bakker23986e52011-04-24 08:57:21 +00001152 for( i = X->n; i > 0; i-- )
1153 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 break;
1155
Paul Bakker23986e52011-04-24 08:57:21 +00001156 for( j = Y->n; j > 0; j-- )
1157 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001158 break;
1159
Paul Bakker23986e52011-04-24 08:57:21 +00001160 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001161 return( 0 );
1162
1163 if( i > j ) return( X->s );
Paul Bakker0c8f73b2012-03-22 14:08:57 +00001164 if( j > i ) return( -Y->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001165
1166 if( X->s > 0 && Y->s < 0 ) return( 1 );
1167 if( Y->s > 0 && X->s < 0 ) return( -1 );
1168
Paul Bakker23986e52011-04-24 08:57:21 +00001169 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 {
Paul Bakker23986e52011-04-24 08:57:21 +00001171 if( X->p[i - 1] > Y->p[i - 1] ) return( X->s );
1172 if( X->p[i - 1] < Y->p[i - 1] ) return( -X->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001173 }
1174
1175 return( 0 );
1176}
1177
Janos Follath45ec9902019-10-14 09:09:32 +01001178/** Decide if an integer is less than the other, without branches.
1179 *
1180 * \param x First integer.
1181 * \param y Second integer.
1182 *
1183 * \return 1 if \p x is less than \p y, 0 otherwise
1184 */
Janos Follath867a3ab2019-10-11 14:21:53 +01001185static unsigned ct_lt_mpi_uint( const mbedtls_mpi_uint x,
1186 const mbedtls_mpi_uint y )
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001187{
1188 mbedtls_mpi_uint ret;
1189 mbedtls_mpi_uint cond;
1190
1191 /*
1192 * Check if the most significant bits (MSB) of the operands are different.
1193 */
1194 cond = ( x ^ y );
1195 /*
1196 * If the MSB are the same then the difference x-y will be negative (and
1197 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
1198 */
1199 ret = ( x - y ) & ~cond;
1200 /*
1201 * If the MSB are different, then the operand with the MSB of 1 is the
1202 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
1203 * the MSB of y is 0.)
1204 */
1205 ret |= y & cond;
1206
1207
Janos Follath7a34bcf2019-10-14 08:59:14 +01001208 ret = ret >> ( biL - 1 );
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001209
Janos Follath359a01e2019-10-29 15:08:46 +00001210 return (unsigned) ret;
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001211}
1212
1213/*
1214 * Compare signed values in constant time
1215 */
Janos Follath867a3ab2019-10-11 14:21:53 +01001216int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y,
1217 unsigned *ret )
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001218{
1219 size_t i;
Janos Follathbd87a592019-10-28 12:23:18 +00001220 /* The value of any of these variables is either 0 or 1 at all times. */
Janos Follath1f21c1d2019-10-28 12:31:34 +00001221 unsigned cond, done, X_is_negative, Y_is_negative;
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001222
1223 MPI_VALIDATE_RET( X != NULL );
1224 MPI_VALIDATE_RET( Y != NULL );
1225 MPI_VALIDATE_RET( ret != NULL );
1226
1227 if( X->n != Y->n )
1228 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1229
1230 /*
Janos Follath58525182019-10-28 12:12:15 +00001231 * Set sign_N to 1 if N >= 0, 0 if N < 0.
1232 * We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0.
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001233 */
Janos Follath1f21c1d2019-10-28 12:31:34 +00001234 X_is_negative = ( X->s & 2 ) >> 1;
1235 Y_is_negative = ( Y->s & 2 ) >> 1;
Janos Follath867a3ab2019-10-11 14:21:53 +01001236
1237 /*
1238 * If the signs are different, then the positive operand is the bigger.
Janos Follath1f21c1d2019-10-28 12:31:34 +00001239 * That is if X is negative (X_is_negative == 1), then X < Y is true and it
1240 * is false if X is positive (X_is_negative == 0).
Janos Follath867a3ab2019-10-11 14:21:53 +01001241 */
Janos Follath1f21c1d2019-10-28 12:31:34 +00001242 cond = ( X_is_negative ^ Y_is_negative );
1243 *ret = cond & X_is_negative;
Janos Follath867a3ab2019-10-11 14:21:53 +01001244
1245 /*
Janos Follathbd87a592019-10-28 12:23:18 +00001246 * This is a constant-time function. We might have the result, but we still
Janos Follath867a3ab2019-10-11 14:21:53 +01001247 * need to go through the loop. Record if we have the result already.
1248 */
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001249 done = cond;
1250
1251 for( i = X->n; i > 0; i-- )
1252 {
1253 /*
Janos Follathe25f1ee2019-11-05 12:24:52 +00001254 * If Y->p[i - 1] < X->p[i - 1] then X < Y is true if and only if both
1255 * X and Y are negative.
Janos Follath867a3ab2019-10-11 14:21:53 +01001256 *
1257 * Again even if we can make a decision, we just mark the result and
1258 * the fact that we are done and continue looping.
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001259 */
Janos Follathe25f1ee2019-11-05 12:24:52 +00001260 cond = ct_lt_mpi_uint( Y->p[i - 1], X->p[i - 1] );
1261 *ret |= cond & ( 1 - done ) & X_is_negative;
Janos Follathfbe4c942019-10-28 12:37:21 +00001262 done |= cond;
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001263
1264 /*
Janos Follathe25f1ee2019-11-05 12:24:52 +00001265 * If X->p[i - 1] < Y->p[i - 1] then X < Y is true if and only if both
1266 * X and Y are positive.
Janos Follath867a3ab2019-10-11 14:21:53 +01001267 *
1268 * Again even if we can make a decision, we just mark the result and
1269 * the fact that we are done and continue looping.
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001270 */
Janos Follathe25f1ee2019-11-05 12:24:52 +00001271 cond = ct_lt_mpi_uint( X->p[i - 1], Y->p[i - 1] );
1272 *ret |= cond & ( 1 - done ) & ( 1 - X_is_negative );
Janos Follathfbe4c942019-10-28 12:37:21 +00001273 done |= cond;
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001274 }
1275
1276 return( 0 );
1277}
1278
Paul Bakker5121ce52009-01-03 21:22:43 +00001279/*
1280 * Compare signed values
1281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +00001283{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 mbedtls_mpi Y;
1285 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001286 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001287
1288 *p = ( z < 0 ) ? -z : z;
1289 Y.s = ( z < 0 ) ? -1 : 1;
1290 Y.n = 1;
1291 Y.p = p;
1292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 return( mbedtls_mpi_cmp_mpi( X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001294}
1295
1296/*
1297 * Unsigned addition: X = |A| + |B| (HAC 14.7)
1298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001300{
Paul Bakker23986e52011-04-24 08:57:21 +00001301 int ret;
1302 size_t i, j;
Janos Follath6c922682015-10-30 17:43:11 +01001303 mbedtls_mpi_uint *o, *p, c, tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +00001304 MPI_VALIDATE_RET( X != NULL );
1305 MPI_VALIDATE_RET( A != NULL );
1306 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001307
1308 if( X == B )
1309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 const mbedtls_mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 }
1312
1313 if( X != A )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
Paul Bakker9af723c2014-05-01 13:03:14 +02001315
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001316 /*
1317 * X should always be positive as a result of unsigned additions.
1318 */
1319 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001320
Paul Bakker23986e52011-04-24 08:57:21 +00001321 for( j = B->n; j > 0; j-- )
1322 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 break;
1324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001326
1327 o = B->p; p = X->p; c = 0;
1328
Janos Follath6c922682015-10-30 17:43:11 +01001329 /*
1330 * tmp is used because it might happen that p == o
1331 */
Paul Bakker23986e52011-04-24 08:57:21 +00001332 for( i = 0; i < j; i++, o++, p++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001333 {
Janos Follath6c922682015-10-30 17:43:11 +01001334 tmp= *o;
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 *p += c; c = ( *p < c );
Janos Follath6c922682015-10-30 17:43:11 +01001336 *p += tmp; c += ( *p < tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 }
1338
1339 while( c != 0 )
1340 {
1341 if( i >= X->n )
1342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 p = X->p + i;
1345 }
1346
Paul Bakker2d319fd2012-09-16 21:34:26 +00001347 *p += c; c = ( *p < c ); i++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001348 }
1349
1350cleanup:
1351
1352 return( ret );
1353}
1354
Gilles Peskinede719d52020-06-09 10:39:38 +02001355/**
Gilles Peskine36acd542020-06-08 21:58:22 +02001356 * Helper for mbedtls_mpi subtraction.
1357 *
1358 * Calculate d - s where d and s have the same size.
1359 * This function operates modulo (2^ciL)^n and returns the carry
1360 * (1 if there was a wraparound, i.e. if `d < s`, and 0 otherwise).
1361 *
1362 * \param n Number of limbs of \p d and \p s.
1363 * \param[in,out] d On input, the left operand.
1364 * On output, the result of the subtraction:
Gilles Peskinede719d52020-06-09 10:39:38 +02001365 * \param[in] s The right operand.
Gilles Peskine36acd542020-06-08 21:58:22 +02001366 *
1367 * \return 1 if `d < s`.
1368 * 0 if `d >= s`.
Paul Bakker5121ce52009-01-03 21:22:43 +00001369 */
Gilles Peskine36acd542020-06-08 21:58:22 +02001370static mbedtls_mpi_uint mpi_sub_hlp( size_t n,
1371 mbedtls_mpi_uint *d,
1372 const mbedtls_mpi_uint *s )
Paul Bakker5121ce52009-01-03 21:22:43 +00001373{
Paul Bakker23986e52011-04-24 08:57:21 +00001374 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 mbedtls_mpi_uint c, z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001376
1377 for( i = c = 0; i < n; i++, s++, d++ )
1378 {
1379 z = ( *d < c ); *d -= c;
1380 c = ( *d < *s ) + z; *d -= *s;
1381 }
1382
Gilles Peskine36acd542020-06-08 21:58:22 +02001383 return( c );
Paul Bakker5121ce52009-01-03 21:22:43 +00001384}
1385
1386/*
Gilles Peskine08fd43c2020-06-08 22:50:35 +02001387 * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
Paul Bakker5121ce52009-01-03 21:22:43 +00001388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001390{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391 mbedtls_mpi TB;
Paul Bakker23986e52011-04-24 08:57:21 +00001392 int ret;
1393 size_t n;
Gilles Peskine08fd43c2020-06-08 22:50:35 +02001394 mbedtls_mpi_uint carry;
Hanno Becker73d7d792018-12-11 10:35:51 +00001395 MPI_VALIDATE_RET( X != NULL );
1396 MPI_VALIDATE_RET( A != NULL );
1397 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001400
1401 if( X == B )
1402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 B = &TB;
1405 }
1406
1407 if( X != A )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001409
Paul Bakker1ef7a532009-06-20 10:50:55 +00001410 /*
Paul Bakker60b1d102013-10-29 10:02:51 +01001411 * X should always be positive as a result of unsigned subtractions.
Paul Bakker1ef7a532009-06-20 10:50:55 +00001412 */
1413 X->s = 1;
1414
Paul Bakker5121ce52009-01-03 21:22:43 +00001415 ret = 0;
1416
Paul Bakker23986e52011-04-24 08:57:21 +00001417 for( n = B->n; n > 0; n-- )
1418 if( B->p[n - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001419 break;
Gilles Peskine6260b702021-01-27 22:30:43 +01001420 if( n > A->n )
1421 {
1422 /* B >= (2^ciL)^n > A */
1423 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1424 goto cleanup;
1425 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001426
Gilles Peskine08fd43c2020-06-08 22:50:35 +02001427 carry = mpi_sub_hlp( n, X->p, B->p );
1428 if( carry != 0 )
Gilles Peskine36acd542020-06-08 21:58:22 +02001429 {
Gilles Peskine08fd43c2020-06-08 22:50:35 +02001430 /* Propagate the carry to the first nonzero limb of X. */
1431 for( ; n < X->n && X->p[n] == 0; n++ )
1432 --X->p[n];
1433 /* If we ran out of space for the carry, it means that the result
1434 * is negative. */
1435 if( n == X->n )
Gilles Peskine84697ca2020-07-23 01:16:46 +02001436 {
1437 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1438 goto cleanup;
1439 }
Gilles Peskine08fd43c2020-06-08 22:50:35 +02001440 --X->p[n];
Gilles Peskine36acd542020-06-08 21:58:22 +02001441 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001442
1443cleanup:
1444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 mbedtls_mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001446
1447 return( ret );
1448}
1449
1450/*
1451 * Signed addition: X = A + B
1452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001454{
Hanno Becker73d7d792018-12-11 10:35:51 +00001455 int ret, s;
1456 MPI_VALIDATE_RET( X != NULL );
1457 MPI_VALIDATE_RET( A != NULL );
1458 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001459
Hanno Becker73d7d792018-12-11 10:35:51 +00001460 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001461 if( A->s * B->s < 0 )
1462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001466 X->s = s;
1467 }
1468 else
1469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001471 X->s = -s;
1472 }
1473 }
1474 else
1475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001477 X->s = s;
1478 }
1479
1480cleanup:
1481
1482 return( ret );
1483}
1484
1485/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001486 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001487 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001489{
Hanno Becker73d7d792018-12-11 10:35:51 +00001490 int ret, s;
1491 MPI_VALIDATE_RET( X != NULL );
1492 MPI_VALIDATE_RET( A != NULL );
1493 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001494
Hanno Becker73d7d792018-12-11 10:35:51 +00001495 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001496 if( A->s * B->s > 0 )
1497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001501 X->s = s;
1502 }
1503 else
1504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001506 X->s = -s;
1507 }
1508 }
1509 else
1510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001512 X->s = s;
1513 }
1514
1515cleanup:
1516
1517 return( ret );
1518}
1519
1520/*
1521 * Signed addition: X = A + b
1522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001524{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 mbedtls_mpi _B;
1526 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001527 MPI_VALIDATE_RET( X != NULL );
1528 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001529
1530 p[0] = ( b < 0 ) ? -b : b;
1531 _B.s = ( b < 0 ) ? -1 : 1;
1532 _B.n = 1;
1533 _B.p = p;
1534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 return( mbedtls_mpi_add_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001536}
1537
1538/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001539 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001540 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001542{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543 mbedtls_mpi _B;
1544 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001545 MPI_VALIDATE_RET( X != NULL );
1546 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001547
1548 p[0] = ( b < 0 ) ? -b : b;
1549 _B.s = ( b < 0 ) ? -1 : 1;
1550 _B.n = 1;
1551 _B.p = p;
1552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 return( mbedtls_mpi_sub_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001554}
1555
1556/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 * Helper for mbedtls_mpi multiplication
Paul Bakkerfc4f46f2013-06-24 19:23:56 +02001558 */
1559static
1560#if defined(__APPLE__) && defined(__arm__)
1561/*
1562 * Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
1563 * appears to need this to prevent bad ARM code generation at -O3.
1564 */
1565__attribute__ ((noinline))
1566#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567void 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 +00001568{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 mbedtls_mpi_uint c = 0, t = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001570
1571#if defined(MULADDC_HUIT)
1572 for( ; i >= 8; i -= 8 )
1573 {
1574 MULADDC_INIT
1575 MULADDC_HUIT
1576 MULADDC_STOP
1577 }
1578
1579 for( ; i > 0; i-- )
1580 {
1581 MULADDC_INIT
1582 MULADDC_CORE
1583 MULADDC_STOP
1584 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001585#else /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001586 for( ; i >= 16; i -= 16 )
1587 {
1588 MULADDC_INIT
1589 MULADDC_CORE MULADDC_CORE
1590 MULADDC_CORE MULADDC_CORE
1591 MULADDC_CORE MULADDC_CORE
1592 MULADDC_CORE MULADDC_CORE
1593
1594 MULADDC_CORE MULADDC_CORE
1595 MULADDC_CORE MULADDC_CORE
1596 MULADDC_CORE MULADDC_CORE
1597 MULADDC_CORE MULADDC_CORE
1598 MULADDC_STOP
1599 }
1600
1601 for( ; i >= 8; i -= 8 )
1602 {
1603 MULADDC_INIT
1604 MULADDC_CORE MULADDC_CORE
1605 MULADDC_CORE MULADDC_CORE
1606
1607 MULADDC_CORE MULADDC_CORE
1608 MULADDC_CORE MULADDC_CORE
1609 MULADDC_STOP
1610 }
1611
1612 for( ; i > 0; i-- )
1613 {
1614 MULADDC_INIT
1615 MULADDC_CORE
1616 MULADDC_STOP
1617 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001618#endif /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001619
1620 t++;
1621
1622 do {
1623 *d += c; c = ( *d < c ); d++;
1624 }
1625 while( c != 0 );
1626}
1627
1628/*
1629 * Baseline multiplication: X = A * B (HAC 14.12)
1630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001632{
Paul Bakker23986e52011-04-24 08:57:21 +00001633 int ret;
1634 size_t i, j;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635 mbedtls_mpi TA, TB;
Hanno Becker73d7d792018-12-11 10:35:51 +00001636 MPI_VALIDATE_RET( X != NULL );
1637 MPI_VALIDATE_RET( A != NULL );
1638 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 if( X == A ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); A = &TA; }
1643 if( X == B ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); B = &TB; }
Paul Bakker5121ce52009-01-03 21:22:43 +00001644
Paul Bakker23986e52011-04-24 08:57:21 +00001645 for( i = A->n; i > 0; i-- )
1646 if( A->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001647 break;
1648
Paul Bakker23986e52011-04-24 08:57:21 +00001649 for( j = B->n; j > 0; j-- )
1650 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001651 break;
1652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + j ) );
1654 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001655
Alexey Skalozub8e75e682016-01-13 21:59:27 +02001656 for( ; j > 0; j-- )
1657 mpi_mul_hlp( i, A->p, X->p + j - 1, B->p[j - 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
1659 X->s = A->s * B->s;
1660
1661cleanup:
1662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663 mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001664
1665 return( ret );
1666}
1667
1668/*
1669 * Baseline multiplication: X = A * b
1670 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001672{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 mbedtls_mpi _B;
1674 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001675 MPI_VALIDATE_RET( X != NULL );
1676 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
1678 _B.s = 1;
1679 _B.n = 1;
1680 _B.p = p;
1681 p[0] = b;
1682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 return( mbedtls_mpi_mul_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001684}
1685
1686/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001687 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1688 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001689 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001690static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1,
1691 mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r )
Simon Butcher15b15d12015-11-26 19:35:03 +00001692{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001693#if defined(MBEDTLS_HAVE_UDBL)
1694 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001695#else
Simon Butcher9803d072016-01-03 00:24:34 +00001696 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
1697 const mbedtls_mpi_uint uint_halfword_mask = ( (mbedtls_mpi_uint) 1 << biH ) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001698 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1699 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001700 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001701#endif
1702
Simon Butcher15b15d12015-11-26 19:35:03 +00001703 /*
1704 * Check for overflow
1705 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001706 if( 0 == d || u1 >= d )
Simon Butcher15b15d12015-11-26 19:35:03 +00001707 {
Simon Butcherf5ba0452015-12-27 23:01:55 +00001708 if (r != NULL) *r = ~0;
Simon Butcher15b15d12015-11-26 19:35:03 +00001709
Simon Butcherf5ba0452015-12-27 23:01:55 +00001710 return ( ~0 );
Simon Butcher15b15d12015-11-26 19:35:03 +00001711 }
1712
1713#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001714 dividend = (mbedtls_t_udbl) u1 << biL;
1715 dividend |= (mbedtls_t_udbl) u0;
1716 quotient = dividend / d;
1717 if( quotient > ( (mbedtls_t_udbl) 1 << biL ) - 1 )
1718 quotient = ( (mbedtls_t_udbl) 1 << biL ) - 1;
1719
1720 if( r != NULL )
Simon Butcher9803d072016-01-03 00:24:34 +00001721 *r = (mbedtls_mpi_uint)( dividend - (quotient * d ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001722
1723 return (mbedtls_mpi_uint) quotient;
1724#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001725
1726 /*
1727 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1728 * Vol. 2 - Seminumerical Algorithms, Knuth
1729 */
1730
1731 /*
1732 * Normalize the divisor, d, and dividend, u0, u1
1733 */
1734 s = mbedtls_clz( d );
1735 d = d << s;
1736
1737 u1 = u1 << s;
Simon Butcher9803d072016-01-03 00:24:34 +00001738 u1 |= ( u0 >> ( biL - s ) ) & ( -(mbedtls_mpi_sint)s >> ( biL - 1 ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001739 u0 = u0 << s;
1740
1741 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001742 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001743
1744 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001745 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001746
1747 /*
1748 * Find the first quotient and remainder
1749 */
1750 q1 = u1 / d1;
1751 r0 = u1 - d1 * q1;
1752
1753 while( q1 >= radix || ( q1 * d0 > radix * r0 + u0_msw ) )
1754 {
1755 q1 -= 1;
1756 r0 += d1;
1757
1758 if ( r0 >= radix ) break;
1759 }
1760
Simon Butcherf5ba0452015-12-27 23:01:55 +00001761 rAX = ( u1 * radix ) + ( u0_msw - q1 * d );
Simon Butcher15b15d12015-11-26 19:35:03 +00001762 q0 = rAX / d1;
1763 r0 = rAX - q0 * d1;
1764
1765 while( q0 >= radix || ( q0 * d0 > radix * r0 + u0_lsw ) )
1766 {
1767 q0 -= 1;
1768 r0 += d1;
1769
1770 if ( r0 >= radix ) break;
1771 }
1772
1773 if (r != NULL)
Simon Butcherf5ba0452015-12-27 23:01:55 +00001774 *r = ( rAX * radix + u0_lsw - q0 * d ) >> s;
Simon Butcher15b15d12015-11-26 19:35:03 +00001775
1776 quotient = q1 * radix + q0;
1777
1778 return quotient;
1779#endif
1780}
1781
1782/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001784 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001785int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1786 const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001787{
Paul Bakker23986e52011-04-24 08:57:21 +00001788 int ret;
1789 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 mbedtls_mpi X, Y, Z, T1, T2;
Hanno Becker73d7d792018-12-11 10:35:51 +00001791 MPI_VALIDATE_RET( A != NULL );
1792 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 if( mbedtls_mpi_cmp_int( B, 0 ) == 0 )
1795 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
1798 mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 if( Q != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_lset( Q, 0 ) );
1803 if( R != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001804 return( 0 );
1805 }
1806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &X, A ) );
1808 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001809 X.s = Y.s = 1;
1810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &Z, A->n + 2 ) );
1812 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Z, 0 ) );
1813 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, 2 ) );
1814 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T2, 3 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001815
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001816 k = mbedtls_mpi_bitlen( &Y ) % biL;
Paul Bakkerf9688572011-05-05 10:00:45 +00001817 if( k < biL - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001818 {
1819 k = biL - 1 - k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &X, k ) );
1821 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, k ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001822 }
1823 else k = 0;
1824
1825 n = X.n - 1;
1826 t = Y.n - 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 while( mbedtls_mpi_cmp_mpi( &X, &Y ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001830 {
1831 Z.p[n - t]++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001833 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001835
1836 for( i = n; i > t ; i-- )
1837 {
1838 if( X.p[i] >= Y.p[t] )
1839 Z.p[i - t - 1] = ~0;
1840 else
1841 {
Simon Butcher15b15d12015-11-26 19:35:03 +00001842 Z.p[i - t - 1] = mbedtls_int_div_int( X.p[i], X.p[i - 1],
1843 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001844 }
1845
1846 Z.p[i - t - 1]++;
1847 do
1848 {
1849 Z.p[i - t - 1]--;
1850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T1, 0 ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001852 T1.p[0] = ( t < 1 ) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001853 T1.p[1] = Y.p[t];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T2, 0 ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001857 T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2];
1858 T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001859 T2.p[2] = X.p[i];
1860 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 while( mbedtls_mpi_cmp_mpi( &T1, &T2 ) > 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
1864 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1865 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867 if( mbedtls_mpi_cmp_int( &X, 0 ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &Y ) );
1870 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1871 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001872 Z.p[i - t - 1]--;
1873 }
1874 }
1875
1876 if( Q != NULL )
1877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( Q, &Z ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001879 Q->s = A->s * B->s;
1880 }
1881
1882 if( R != NULL )
1883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &X, k ) );
Paul Bakkerf02c5642012-11-13 10:25:21 +00001885 X.s = A->s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, &X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 if( mbedtls_mpi_cmp_int( R, 0 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001889 R->s = 1;
1890 }
1891
1892cleanup:
1893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
1895 mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001896
1897 return( ret );
1898}
1899
1900/*
1901 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00001902 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001903int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R,
1904 const mbedtls_mpi *A,
1905 mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001906{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 mbedtls_mpi _B;
1908 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001909 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001910
1911 p[0] = ( b < 0 ) ? -b : b;
1912 _B.s = ( b < 0 ) ? -1 : 1;
1913 _B.n = 1;
1914 _B.p = p;
1915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 return( mbedtls_mpi_div_mpi( Q, R, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001917}
1918
1919/*
1920 * Modulo: R = A mod B
1921 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001923{
1924 int ret;
Hanno Becker73d7d792018-12-11 10:35:51 +00001925 MPI_VALIDATE_RET( R != NULL );
1926 MPI_VALIDATE_RET( A != NULL );
1927 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929 if( mbedtls_mpi_cmp_int( B, 0 ) < 0 )
1930 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( NULL, R, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 while( mbedtls_mpi_cmp_int( R, 0 ) < 0 )
1935 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 while( mbedtls_mpi_cmp_mpi( R, B ) >= 0 )
1938 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001939
1940cleanup:
1941
1942 return( ret );
1943}
1944
1945/*
1946 * Modulo: r = A mod b
1947 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001949{
Paul Bakker23986e52011-04-24 08:57:21 +00001950 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 mbedtls_mpi_uint x, y, z;
Hanno Becker73d7d792018-12-11 10:35:51 +00001952 MPI_VALIDATE_RET( r != NULL );
1953 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001954
1955 if( b == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001957
1958 if( b < 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001960
1961 /*
1962 * handle trivial cases
1963 */
1964 if( b == 1 )
1965 {
1966 *r = 0;
1967 return( 0 );
1968 }
1969
1970 if( b == 2 )
1971 {
1972 *r = A->p[0] & 1;
1973 return( 0 );
1974 }
1975
1976 /*
1977 * general case
1978 */
Paul Bakker23986e52011-04-24 08:57:21 +00001979 for( i = A->n, y = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001980 {
Paul Bakker23986e52011-04-24 08:57:21 +00001981 x = A->p[i - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001982 y = ( y << biH ) | ( x >> biH );
1983 z = y / b;
1984 y -= z * b;
1985
1986 x <<= biH;
1987 y = ( y << biH ) | ( x >> biH );
1988 z = y / b;
1989 y -= z * b;
1990 }
1991
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001992 /*
1993 * If A is negative, then the current y represents a negative value.
1994 * Flipping it to the positive side.
1995 */
1996 if( A->s < 0 && y != 0 )
1997 y = b - y;
1998
Paul Bakker5121ce52009-01-03 21:22:43 +00001999 *r = y;
2000
2001 return( 0 );
2002}
2003
2004/*
2005 * Fast Montgomery initialization (thanks to Tom St Denis)
2006 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007static void mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002008{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 mbedtls_mpi_uint x, m0 = N->p[0];
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01002010 unsigned int i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002011
2012 x = m0;
2013 x += ( ( m0 + 2 ) & 4 ) << 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002014
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01002015 for( i = biL; i >= 8; i /= 2 )
2016 x *= ( 2 - ( m0 * x ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002017
2018 *mm = ~x + 1;
2019}
2020
Gilles Peskine3ce3ddf2020-06-04 15:00:49 +02002021/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
2022 *
2023 * \param[in,out] A One of the numbers to multiply.
Gilles Peskine635a3742020-06-08 22:37:50 +02002024 * It must have at least as many limbs as N
2025 * (A->n >= N->n), and any limbs beyond n are ignored.
Gilles Peskine3ce3ddf2020-06-04 15:00:49 +02002026 * On successful completion, A contains the result of
2027 * the multiplication A * B * R^-1 mod N where
2028 * R = (2^ciL)^n.
2029 * \param[in] B One of the numbers to multiply.
2030 * It must be nonzero and must not have more limbs than N
2031 * (B->n <= N->n).
2032 * \param[in] N The modulo. N must be odd.
2033 * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
2034 * This is -N^-1 mod 2^ciL.
2035 * \param[in,out] T A bignum for temporary storage.
2036 * It must be at least twice the limb size of N plus 2
2037 * (T->n >= 2 * (N->n + 1)).
2038 * Its initial content is unused and
2039 * its final content is indeterminate.
2040 * Note that unlike the usual convention in the library
2041 * for `const mbedtls_mpi*`, the content of T can change.
Paul Bakker5121ce52009-01-03 21:22:43 +00002042 */
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002043static 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 +02002044 const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00002045{
Paul Bakker23986e52011-04-24 08:57:21 +00002046 size_t i, n, m;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 mbedtls_mpi_uint u0, u1, *d;
Paul Bakker5121ce52009-01-03 21:22:43 +00002048
2049 memset( T->p, 0, T->n * ciL );
2050
2051 d = T->p;
2052 n = N->n;
2053 m = ( B->n < n ) ? B->n : n;
2054
2055 for( i = 0; i < n; i++ )
2056 {
2057 /*
2058 * T = (T + u0*B + u1*N) / 2^biL
2059 */
2060 u0 = A->p[i];
2061 u1 = ( d[0] + u0 * B->p[0] ) * mm;
2062
2063 mpi_mul_hlp( m, B->p, d, u0 );
2064 mpi_mul_hlp( n, N->p, d, u1 );
2065
2066 *d++ = u0; d[n + 1] = 0;
2067 }
2068
Gilles Peskine635a3742020-06-08 22:37:50 +02002069 /* At this point, d is either the desired result or the desired result
2070 * plus N. We now potentially subtract N, avoiding leaking whether the
2071 * subtraction is performed through side channels. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002072
Gilles Peskine635a3742020-06-08 22:37:50 +02002073 /* Copy the n least significant limbs of d to A, so that
2074 * A = d if d < N (recall that N has n limbs). */
2075 memcpy( A->p, d, n * ciL );
Gilles Peskinede719d52020-06-09 10:39:38 +02002076 /* If d >= N then we want to set A to d - N. To prevent timing attacks,
Gilles Peskine635a3742020-06-08 22:37:50 +02002077 * do the calculation without using conditional tests. */
2078 /* Set d to d0 + (2^biL)^n - N where d0 is the current value of d. */
Gilles Peskine8f672662020-06-04 21:05:24 +02002079 d[n] += 1;
Gilles Peskine36acd542020-06-08 21:58:22 +02002080 d[n] -= mpi_sub_hlp( n, d, N->p );
Gilles Peskine635a3742020-06-08 22:37:50 +02002081 /* If d0 < N then d < (2^biL)^n
2082 * so d[n] == 0 and we want to keep A as it is.
2083 * If d0 >= N then d >= (2^biL)^n, and d <= (2^biL)^n + N < 2 * (2^biL)^n
2084 * so d[n] == 1 and we want to set A to the result of the subtraction
2085 * which is d - (2^biL)^n, i.e. the n least significant limbs of d.
2086 * This exactly corresponds to a conditional assignment. */
2087 mpi_safe_cond_assign( n, A->p, d, (unsigned char) d[n] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002088}
2089
2090/*
2091 * Montgomery reduction: A = A * R^-1 mod N
Gilles Peskine3ce3ddf2020-06-04 15:00:49 +02002092 *
2093 * See mpi_montmul() regarding constraints and guarantees on the parameters.
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 */
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002095static void mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N,
2096 mbedtls_mpi_uint mm, const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00002097{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 mbedtls_mpi_uint z = 1;
2099 mbedtls_mpi U;
Paul Bakker5121ce52009-01-03 21:22:43 +00002100
Paul Bakker8ddb6452013-02-27 14:56:33 +01002101 U.n = U.s = (int) z;
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 U.p = &z;
2103
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002104 mpi_montmul( A, &U, N, mm, T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002105}
2106
Manuel Pégourié-Gonnard432ebba2021-06-03 10:42:46 +02002107/*
2108 * Constant-flow boolean "equal" comparison:
2109 * return x == y
2110 *
2111 * This function can be used to write constant-time code by replacing branches
2112 * with bit operations - it can be used in conjunction with
2113 * mbedtls_ssl_cf_mask_from_bit().
2114 *
2115 * This function is implemented without using comparison operators, as those
2116 * might be translated to branches by some compilers on some platforms.
2117 */
2118static size_t mbedtls_mpi_cf_bool_eq( size_t x, size_t y )
2119{
2120 /* diff = 0 if x == y, non-zero otherwise */
2121 const size_t diff = x ^ y;
2122
2123 /* MSVC has a warning about unary minus on unsigned integer types,
2124 * but this is well-defined and precisely what we want to do here. */
2125#if defined(_MSC_VER)
2126#pragma warning( push )
2127#pragma warning( disable : 4146 )
2128#endif
2129
2130 /* diff_msb's most significant bit is equal to x != y */
2131 const size_t diff_msb = ( diff | -diff );
2132
2133#if defined(_MSC_VER)
2134#pragma warning( pop )
2135#endif
2136
2137 /* diff1 = (x != y) ? 1 : 0 */
2138 const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 );
2139
2140 return( 1 ^ diff1 );
2141}
2142
Manuel Pégourié-Gonnard87bd4442021-03-09 11:22:20 +01002143/**
2144 * Select an MPI from a table without leaking the index.
2145 *
2146 * This is functionally equivalent to mbedtls_mpi_copy(R, T[idx]) except it
2147 * reads the entire table in order to avoid leaking the value of idx to an
2148 * attacker able to observe memory access patterns.
2149 *
2150 * \param[out] R Where to write the selected MPI.
2151 * \param[in] T The table to read from.
2152 * \param[in] T_size The number of elements in the table.
2153 * \param[in] idx The index of the element to select;
2154 * this must satisfy 0 <= idx < T_size.
2155 *
2156 * \return \c 0 on success, or a negative error code.
2157 */
2158static int mpi_select( mbedtls_mpi *R, const mbedtls_mpi *T, size_t T_size, size_t idx )
2159{
Manuel Pégourié-Gonnardde2ab2a2021-06-15 12:37:23 +02002160 int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard87bd4442021-03-09 11:22:20 +01002161
2162 for( size_t i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard432ebba2021-06-03 10:42:46 +02002163 {
2164 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( R, &T[i],
Manuel Pégourié-Gonnard4fc96df2021-06-10 09:34:00 +02002165 (unsigned char) mbedtls_mpi_cf_bool_eq( i, idx ) ) );
Manuel Pégourié-Gonnard432ebba2021-06-03 10:42:46 +02002166 }
Manuel Pégourié-Gonnard87bd4442021-03-09 11:22:20 +01002167
2168cleanup:
2169 return( ret );
2170}
2171
Paul Bakker5121ce52009-01-03 21:22:43 +00002172/*
2173 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
2174 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002175int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
2176 const mbedtls_mpi *E, const mbedtls_mpi *N,
2177 mbedtls_mpi *_RR )
Paul Bakker5121ce52009-01-03 21:22:43 +00002178{
Paul Bakker23986e52011-04-24 08:57:21 +00002179 int ret;
2180 size_t wbits, wsize, one = 1;
2181 size_t i, j, nblimbs;
2182 size_t bufsize, nbits;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 mbedtls_mpi_uint ei, mm, state;
Manuel Pégourié-Gonnard87bd4442021-03-09 11:22:20 +01002184 mbedtls_mpi RR, T, W[ 1 << MBEDTLS_MPI_WINDOW_SIZE ], WW, Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00002185 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002186
Hanno Becker73d7d792018-12-11 10:35:51 +00002187 MPI_VALIDATE_RET( X != NULL );
2188 MPI_VALIDATE_RET( A != NULL );
2189 MPI_VALIDATE_RET( E != NULL );
2190 MPI_VALIDATE_RET( N != NULL );
2191
Hanno Becker8d1dd1b2017-09-28 11:02:24 +01002192 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 || ( N->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195 if( mbedtls_mpi_cmp_int( E, 0 ) < 0 )
2196 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002197
Chris Jonesad59a2a2020-11-25 15:12:39 +00002198 if( mbedtls_mpi_bitlen( E ) > MBEDTLS_MPI_MAX_BITS ||
2199 mbedtls_mpi_bitlen( N ) > MBEDTLS_MPI_MAX_BITS )
2200 return ( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2201
Paul Bakkerf6198c12012-05-16 08:02:29 +00002202 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002203 * Init temps and window size
2204 */
2205 mpi_montg_init( &mm, N );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &T );
2207 mbedtls_mpi_init( &Apos );
Manuel Pégourié-Gonnard87bd4442021-03-09 11:22:20 +01002208 mbedtls_mpi_init( &WW );
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 memset( W, 0, sizeof( W ) );
2210
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002211 i = mbedtls_mpi_bitlen( E );
Paul Bakker5121ce52009-01-03 21:22:43 +00002212
2213 wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
2214 ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1;
2215
Peter Kolbusb83d41d2018-12-11 14:01:44 -06002216#if( MBEDTLS_MPI_WINDOW_SIZE < 6 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 if( wsize > MBEDTLS_MPI_WINDOW_SIZE )
2218 wsize = MBEDTLS_MPI_WINDOW_SIZE;
Peter Kolbusb83d41d2018-12-11 14:01:44 -06002219#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00002220
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 j = N->n + 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
2223 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], j ) );
2224 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T, j * 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002225
2226 /*
Paul Bakker50546922012-05-19 08:40:49 +00002227 * Compensate for negative A (and correct at the end)
2228 */
2229 neg = ( A->s == -1 );
Paul Bakker50546922012-05-19 08:40:49 +00002230 if( neg )
2231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Apos, A ) );
Paul Bakker50546922012-05-19 08:40:49 +00002233 Apos.s = 1;
2234 A = &Apos;
2235 }
2236
2237 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 * If 1st call, pre-compute R^2 mod N
2239 */
2240 if( _RR == NULL || _RR->p == NULL )
2241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &RR, 1 ) );
2243 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &RR, N->n * 2 * biL ) );
2244 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &RR, &RR, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002245
2246 if( _RR != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 memcpy( _RR, &RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 }
2249 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 memcpy( &RR, _RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002251
2252 /*
2253 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
2254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255 if( mbedtls_mpi_cmp_mpi( A, N ) >= 0 )
2256 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &W[1], A, N ) );
Paul Bakkerc2024f42014-01-23 20:38:35 +01002257 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[1], A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002259
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002260 mpi_montmul( &W[1], &RR, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
2262 /*
2263 * X = R^2 * R^-1 mod N = R mod N
2264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &RR ) );
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002266 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002267
2268 if( wsize > 1 )
2269 {
2270 /*
2271 * W[1 << (wsize - 1)] = W[1] ^ (wsize - 1)
2272 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002273 j = one << ( wsize - 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[j], N->n + 1 ) );
2276 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[j], &W[1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002277
2278 for( i = 0; i < wsize - 1; i++ )
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002279 mpi_montmul( &W[j], &W[j], N, mm, &T );
Paul Bakker0d7702c2013-10-29 16:18:35 +01002280
Paul Bakker5121ce52009-01-03 21:22:43 +00002281 /*
2282 * W[i] = W[i - 1] * W[1]
2283 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002284 for( i = j + 1; i < ( one << wsize ); i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[i], N->n + 1 ) );
2287 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[i], &W[i - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002288
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002289 mpi_montmul( &W[i], &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002290 }
2291 }
2292
2293 nblimbs = E->n;
2294 bufsize = 0;
2295 nbits = 0;
2296 wbits = 0;
2297 state = 0;
2298
2299 while( 1 )
2300 {
2301 if( bufsize == 0 )
2302 {
Paul Bakker0d7702c2013-10-29 16:18:35 +01002303 if( nblimbs == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002304 break;
2305
Paul Bakker0d7702c2013-10-29 16:18:35 +01002306 nblimbs--;
2307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 bufsize = sizeof( mbedtls_mpi_uint ) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00002309 }
2310
2311 bufsize--;
2312
2313 ei = (E->p[nblimbs] >> bufsize) & 1;
2314
2315 /*
2316 * skip leading 0s
2317 */
2318 if( ei == 0 && state == 0 )
2319 continue;
2320
2321 if( ei == 0 && state == 1 )
2322 {
2323 /*
2324 * out of window, square X
2325 */
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002326 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002327 continue;
2328 }
2329
2330 /*
2331 * add ei to current window
2332 */
2333 state = 2;
2334
2335 nbits++;
Paul Bakker66d5d072014-06-17 16:39:18 +02002336 wbits |= ( ei << ( wsize - nbits ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002337
2338 if( nbits == wsize )
2339 {
2340 /*
2341 * X = X^wsize R^-1 mod N
2342 */
2343 for( i = 0; i < wsize; i++ )
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002344 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002345
2346 /*
2347 * X = X * W[wbits] R^-1 mod N
2348 */
Manuel Pégourié-Gonnard4fc96df2021-06-10 09:34:00 +02002349 MBEDTLS_MPI_CHK( mpi_select( &WW, W, (size_t) 1 << wsize, wbits ) );
Manuel Pégourié-Gonnard87bd4442021-03-09 11:22:20 +01002350 mpi_montmul( X, &WW, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002351
2352 state--;
2353 nbits = 0;
2354 wbits = 0;
2355 }
2356 }
2357
2358 /*
2359 * process the remaining bits
2360 */
2361 for( i = 0; i < nbits; i++ )
2362 {
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002363 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002364
2365 wbits <<= 1;
2366
Paul Bakker66d5d072014-06-17 16:39:18 +02002367 if( ( wbits & ( one << wsize ) ) != 0 )
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002368 mpi_montmul( X, &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002369 }
2370
2371 /*
2372 * X = A^E * R * R^-1 mod N = A^E mod N
2373 */
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002374 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002375
Hanno Beckera4af1c42017-04-18 09:07:45 +01002376 if( neg && E->n != 0 && ( E->p[0] & 1 ) != 0 )
Paul Bakkerf6198c12012-05-16 08:02:29 +00002377 {
2378 X->s = -1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, N, X ) );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002380 }
2381
Paul Bakker5121ce52009-01-03 21:22:43 +00002382cleanup:
2383
Paul Bakker66d5d072014-06-17 16:39:18 +02002384 for( i = ( one << ( wsize - 1 ) ); i < ( one << wsize ); i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 mbedtls_mpi_free( &W[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 mbedtls_mpi_free( &W[1] ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &Apos );
Manuel Pégourié-Gonnard87bd4442021-03-09 11:22:20 +01002388 mbedtls_mpi_free( &WW );
Paul Bakker6c591fa2011-05-05 11:49:20 +00002389
Paul Bakker75a28602014-03-31 12:08:17 +02002390 if( _RR == NULL || _RR->p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002392
2393 return( ret );
2394}
2395
Paul Bakker5121ce52009-01-03 21:22:43 +00002396/*
2397 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
2398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00002400{
Paul Bakker23986e52011-04-24 08:57:21 +00002401 int ret;
2402 size_t lz, lzt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 mbedtls_mpi TG, TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00002404
Hanno Becker73d7d792018-12-11 10:35:51 +00002405 MPI_VALIDATE_RET( G != NULL );
2406 MPI_VALIDATE_RET( A != NULL );
2407 MPI_VALIDATE_RET( B != NULL );
2408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409 mbedtls_mpi_init( &TG ); mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) );
2412 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414 lz = mbedtls_mpi_lsb( &TA );
2415 lzt = mbedtls_mpi_lsb( &TB );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002416
Paul Bakker66d5d072014-06-17 16:39:18 +02002417 if( lzt < lz )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002418 lz = lzt;
2419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, lz ) );
2421 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, lz ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002422
Paul Bakker5121ce52009-01-03 21:22:43 +00002423 TA.s = TB.s = 1;
2424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 while( mbedtls_mpi_cmp_int( &TA, 0 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, mbedtls_mpi_lsb( &TA ) ) );
2428 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, mbedtls_mpi_lsb( &TB ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 if( mbedtls_mpi_cmp_mpi( &TA, &TB ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TA, &TA, &TB ) );
2433 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002434 }
2435 else
2436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TB, &TB, &TA ) );
2438 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002439 }
2440 }
2441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &TB, lz ) );
2443 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( G, &TB ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002444
2445cleanup:
2446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 mbedtls_mpi_free( &TG ); mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002448
2449 return( ret );
2450}
2451
Paul Bakker33dc46b2014-04-30 16:11:39 +02002452/*
2453 * Fill X with size bytes of random.
2454 *
2455 * Use a temporary bytes representation to make sure the result is the same
Paul Bakkerc37b0ac2014-05-01 14:19:23 +02002456 * regardless of the platform endianness (useful when f_rng is actually
Paul Bakker33dc46b2014-04-30 16:11:39 +02002457 * deterministic, eg for tests).
2458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002460 int (*f_rng)(void *, unsigned char *, size_t),
2461 void *p_rng )
Paul Bakker287781a2011-03-26 13:18:49 +00002462{
Paul Bakker23986e52011-04-24 08:57:21 +00002463 int ret;
Hanno Becker6dab6202019-01-02 16:42:29 +00002464 size_t const limbs = CHARS_TO_LIMBS( size );
Hanno Beckerda1655a2017-10-18 14:21:44 +01002465 size_t const overhead = ( limbs * ciL ) - size;
2466 unsigned char *Xp;
2467
Hanno Becker8ce11a32018-12-19 16:18:52 +00002468 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002469 MPI_VALIDATE_RET( f_rng != NULL );
Paul Bakker33dc46b2014-04-30 16:11:39 +02002470
Hanno Beckerda1655a2017-10-18 14:21:44 +01002471 /* Ensure that target MPI has exactly the necessary number of limbs */
2472 if( X->n != limbs )
2473 {
2474 mbedtls_mpi_free( X );
2475 mbedtls_mpi_init( X );
2476 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, limbs ) );
2477 }
2478 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker287781a2011-03-26 13:18:49 +00002479
Hanno Beckerda1655a2017-10-18 14:21:44 +01002480 Xp = (unsigned char*) X->p;
Gilles Peskine05251142020-11-25 16:15:14 +01002481 MBEDTLS_MPI_CHK( f_rng( p_rng, Xp + overhead, size ) );
Hanno Beckerda1655a2017-10-18 14:21:44 +01002482
Hanno Becker2be8a552018-10-25 12:40:09 +01002483 mpi_bigendian_to_host( X->p, limbs );
Paul Bakker287781a2011-03-26 13:18:49 +00002484
2485cleanup:
2486 return( ret );
2487}
2488
Paul Bakker5121ce52009-01-03 21:22:43 +00002489/*
2490 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002493{
2494 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Hanno Becker73d7d792018-12-11 10:35:51 +00002496 MPI_VALIDATE_RET( X != NULL );
2497 MPI_VALIDATE_RET( A != NULL );
2498 MPI_VALIDATE_RET( N != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
Hanno Becker4bcb4912017-04-18 15:49:39 +01002500 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TU ); mbedtls_mpi_init( &U1 ); mbedtls_mpi_init( &U2 );
2504 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &TB ); mbedtls_mpi_init( &TV );
2505 mbedtls_mpi_init( &V1 ); mbedtls_mpi_init( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, A, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002512 goto cleanup;
2513 }
2514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &TA, A, N ) );
2516 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TU, &TA ) );
2517 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, N ) );
2518 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TV, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U1, 1 ) );
2521 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U2, 0 ) );
2522 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V1, 0 ) );
2523 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002524
2525 do
2526 {
2527 while( ( TU.p[0] & 1 ) == 0 )
2528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TU, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002530
2531 if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
2532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &U1, &U1, &TB ) );
2534 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002535 }
2536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U1, 1 ) );
2538 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002539 }
2540
2541 while( ( TV.p[0] & 1 ) == 0 )
2542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TV, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002544
2545 if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
2546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, &TB ) );
2548 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002549 }
2550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V1, 1 ) );
2552 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002553 }
2554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555 if( mbedtls_mpi_cmp_mpi( &TU, &TV ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TU, &TU, &TV ) );
2558 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U1, &U1, &V1 ) );
2559 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &V2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002560 }
2561 else
2562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TV, &TV, &TU ) );
2564 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, &U1 ) );
2565 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &U2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 }
2567 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568 while( mbedtls_mpi_cmp_int( &TU, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 while( mbedtls_mpi_cmp_int( &V1, 0 ) < 0 )
2571 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573 while( mbedtls_mpi_cmp_mpi( &V1, N ) >= 0 )
2574 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &V1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002577
2578cleanup:
2579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TU ); mbedtls_mpi_free( &U1 ); mbedtls_mpi_free( &U2 );
2581 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TV );
2582 mbedtls_mpi_free( &V1 ); mbedtls_mpi_free( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002583
2584 return( ret );
2585}
2586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002588
Paul Bakker5121ce52009-01-03 21:22:43 +00002589static const int small_prime[] =
2590{
2591 3, 5, 7, 11, 13, 17, 19, 23,
2592 29, 31, 37, 41, 43, 47, 53, 59,
2593 61, 67, 71, 73, 79, 83, 89, 97,
2594 101, 103, 107, 109, 113, 127, 131, 137,
2595 139, 149, 151, 157, 163, 167, 173, 179,
2596 181, 191, 193, 197, 199, 211, 223, 227,
2597 229, 233, 239, 241, 251, 257, 263, 269,
2598 271, 277, 281, 283, 293, 307, 311, 313,
2599 317, 331, 337, 347, 349, 353, 359, 367,
2600 373, 379, 383, 389, 397, 401, 409, 419,
2601 421, 431, 433, 439, 443, 449, 457, 461,
2602 463, 467, 479, 487, 491, 499, 503, 509,
2603 521, 523, 541, 547, 557, 563, 569, 571,
2604 577, 587, 593, 599, 601, 607, 613, 617,
2605 619, 631, 641, 643, 647, 653, 659, 661,
2606 673, 677, 683, 691, 701, 709, 719, 727,
2607 733, 739, 743, 751, 757, 761, 769, 773,
2608 787, 797, 809, 811, 821, 823, 827, 829,
2609 839, 853, 857, 859, 863, 877, 881, 883,
2610 887, 907, 911, 919, 929, 937, 941, 947,
2611 953, 967, 971, 977, 983, 991, 997, -103
2612};
2613
2614/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002615 * Small divisors test (X must be positive)
2616 *
2617 * Return values:
2618 * 0: no small factor (possible prime, more tests needed)
2619 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002621 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623static int mpi_check_small_factors( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +00002624{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002625 int ret = 0;
2626 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 mbedtls_mpi_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +00002628
Paul Bakker5121ce52009-01-03 21:22:43 +00002629 if( ( X->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
2632 for( i = 0; small_prime[i] > 0; i++ )
2633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 if( mbedtls_mpi_cmp_int( X, small_prime[i] ) <= 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002635 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, small_prime[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
2639 if( r == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002641 }
2642
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002643cleanup:
2644 return( ret );
2645}
2646
2647/*
2648 * Miller-Rabin pseudo-primality test (HAC 4.24)
2649 */
Janos Follathda31fa12018-09-03 14:45:23 +01002650static int mpi_miller_rabin( const mbedtls_mpi *X, size_t rounds,
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002651 int (*f_rng)(void *, unsigned char *, size_t),
2652 void *p_rng )
2653{
Pascal Junodb99183d2015-03-11 16:49:45 +01002654 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002655 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002657
Hanno Becker8ce11a32018-12-19 16:18:52 +00002658 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002659 MPI_VALIDATE_RET( f_rng != NULL );
2660
2661 mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R );
2662 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002663 mbedtls_mpi_init( &RR );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002664
Paul Bakker5121ce52009-01-03 21:22:43 +00002665 /*
2666 * W = |X| - 1
2667 * R = W >> lsb( W )
2668 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &W, X, 1 ) );
2670 s = mbedtls_mpi_lsb( &W );
2671 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R, &W ) );
2672 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &R, s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002673
Janos Follathda31fa12018-09-03 14:45:23 +01002674 for( i = 0; i < rounds; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002675 {
2676 /*
2677 * pick a random A, 1 < A < |X| - 1
2678 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002679 count = 0;
2680 do {
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002681 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
Pascal Junodb99183d2015-03-11 16:49:45 +01002682
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002683 j = mbedtls_mpi_bitlen( &A );
2684 k = mbedtls_mpi_bitlen( &W );
Pascal Junodb99183d2015-03-11 16:49:45 +01002685 if (j > k) {
Darryl Greene3f95ed2018-10-02 13:21:35 +01002686 A.p[A.n - 1] &= ( (mbedtls_mpi_uint) 1 << ( k - ( A.n - 1 ) * biL - 1 ) ) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002687 }
2688
2689 if (count++ > 30) {
Jens Wiklanderdfd447e2019-01-17 13:30:57 +01002690 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2691 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002692 }
2693
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002694 } while ( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 ||
2695 mbedtls_mpi_cmp_int( &A, 1 ) <= 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002696
2697 /*
2698 * A = A^R mod |X|
2699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &A, &A, &R, X, &RR ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702 if( mbedtls_mpi_cmp_mpi( &A, &W ) == 0 ||
2703 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 continue;
2705
2706 j = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707 while( j < s && mbedtls_mpi_cmp_mpi( &A, &W ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 {
2709 /*
2710 * A = A * A mod |X|
2711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &A, &A ) );
2713 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &A, &T, X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 if( mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002716 break;
2717
2718 j++;
2719 }
2720
2721 /*
2722 * not prime if A != |X| - 1 or A == 1
2723 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724 if( mbedtls_mpi_cmp_mpi( &A, &W ) != 0 ||
2725 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002728 break;
2729 }
2730 }
2731
2732cleanup:
Hanno Becker73d7d792018-12-11 10:35:51 +00002733 mbedtls_mpi_free( &W ); mbedtls_mpi_free( &R );
2734 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002736
2737 return( ret );
2738}
2739
2740/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002741 * Pseudo-primality test: small factors, then Miller-Rabin
2742 */
Janos Follatha0b67c22018-09-18 14:48:23 +01002743int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds,
2744 int (*f_rng)(void *, unsigned char *, size_t),
2745 void *p_rng )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002746{
2747 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 mbedtls_mpi XX;
Hanno Becker8ce11a32018-12-19 16:18:52 +00002749 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002750 MPI_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002751
2752 XX.s = 1;
2753 XX.n = X->n;
2754 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 if( mbedtls_mpi_cmp_int( &XX, 0 ) == 0 ||
2757 mbedtls_mpi_cmp_int( &XX, 1 ) == 0 )
2758 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760 if( mbedtls_mpi_cmp_int( &XX, 2 ) == 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002761 return( 0 );
2762
2763 if( ( ret = mpi_check_small_factors( &XX ) ) != 0 )
2764 {
2765 if( ret == 1 )
2766 return( 0 );
2767
2768 return( ret );
2769 }
2770
Janos Follathda31fa12018-09-03 14:45:23 +01002771 return( mpi_miller_rabin( &XX, rounds, f_rng, p_rng ) );
Janos Follathf301d232018-08-14 13:34:01 +01002772}
2773
Janos Follatha0b67c22018-09-18 14:48:23 +01002774#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Janos Follathf301d232018-08-14 13:34:01 +01002775/*
2776 * Pseudo-primality test, error probability 2^-80
2777 */
2778int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
2779 int (*f_rng)(void *, unsigned char *, size_t),
2780 void *p_rng )
2781{
Hanno Becker8ce11a32018-12-19 16:18:52 +00002782 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002783 MPI_VALIDATE_RET( f_rng != NULL );
2784
Janos Follatha0b67c22018-09-18 14:48:23 +01002785 /*
2786 * In the past our key generation aimed for an error rate of at most
2787 * 2^-80. Since this function is deprecated, aim for the same certainty
2788 * here as well.
2789 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002790 return( mbedtls_mpi_is_prime_ext( X, 40, f_rng, p_rng ) );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002791}
Janos Follatha0b67c22018-09-18 14:48:23 +01002792#endif
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002793
2794/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002795 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002796 *
Janos Follathf301d232018-08-14 13:34:01 +01002797 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2798 * be either 1024 bits or 1536 bits long, and flags must contain
2799 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002800 */
Janos Follath7c025a92018-08-14 11:08:41 +01002801int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002802 int (*f_rng)(void *, unsigned char *, size_t),
2803 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002804{
Jethro Beekman66689272018-02-14 19:24:10 -08002805#ifdef MBEDTLS_HAVE_INT64
2806// ceil(2^63.5)
2807#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2808#else
2809// ceil(2^31.5)
2810#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2811#endif
2812 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002813 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002814 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815 mbedtls_mpi_uint r;
2816 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002817
Hanno Becker8ce11a32018-12-19 16:18:52 +00002818 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002819 MPI_VALIDATE_RET( f_rng != NULL );
2820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821 if( nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS )
2822 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824 mbedtls_mpi_init( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002825
2826 n = BITS_TO_LIMBS( nbits );
2827
Janos Follathda31fa12018-09-03 14:45:23 +01002828 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR ) == 0 )
2829 {
2830 /*
2831 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2832 */
2833 rounds = ( ( nbits >= 1300 ) ? 2 : ( nbits >= 850 ) ? 3 :
2834 ( nbits >= 650 ) ? 4 : ( nbits >= 350 ) ? 8 :
2835 ( nbits >= 250 ) ? 12 : ( nbits >= 150 ) ? 18 : 27 );
2836 }
2837 else
2838 {
2839 /*
2840 * 2^-100 error probability, number of rounds computed based on HAC,
2841 * fact 4.48
2842 */
2843 rounds = ( ( nbits >= 1450 ) ? 4 : ( nbits >= 1150 ) ? 5 :
2844 ( nbits >= 1000 ) ? 6 : ( nbits >= 850 ) ? 7 :
2845 ( nbits >= 750 ) ? 8 : ( nbits >= 500 ) ? 13 :
2846 ( nbits >= 250 ) ? 28 : ( nbits >= 150 ) ? 40 : 51 );
2847 }
2848
Jethro Beekman66689272018-02-14 19:24:10 -08002849 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002850 {
Jethro Beekman66689272018-02-14 19:24:10 -08002851 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
2852 /* make sure generated number is at least (nbits-1)+0.5 bits (FIPS 186-4 §B.3.3 steps 4.4, 5.5) */
2853 if( X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2 ) continue;
2854
2855 k = n * biL;
2856 if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits ) );
2857 X->p[0] |= 1;
2858
Janos Follath7c025a92018-08-14 11:08:41 +01002859 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002860 {
Janos Follatha0b67c22018-09-18 14:48:23 +01002861 ret = mbedtls_mpi_is_prime_ext( X, rounds, f_rng, p_rng );
Jethro Beekman66689272018-02-14 19:24:10 -08002862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002864 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002865 }
Jethro Beekman66689272018-02-14 19:24:10 -08002866 else
Paul Bakker5121ce52009-01-03 21:22:43 +00002867 {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002868 /*
Jethro Beekman66689272018-02-14 19:24:10 -08002869 * An necessary condition for Y and X = 2Y + 1 to be prime
2870 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2871 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002872 */
Jethro Beekman66689272018-02-14 19:24:10 -08002873
2874 X->p[0] |= 2;
2875
2876 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, 3 ) );
2877 if( r == 0 )
2878 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 8 ) );
2879 else if( r == 1 )
2880 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 4 ) );
2881
2882 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
2883 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, X ) );
2884 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, 1 ) );
2885
2886 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002887 {
Jethro Beekman66689272018-02-14 19:24:10 -08002888 /*
2889 * First, check small factors for X and Y
2890 * before doing Miller-Rabin on any of them
2891 */
2892 if( ( ret = mpi_check_small_factors( X ) ) == 0 &&
2893 ( ret = mpi_check_small_factors( &Y ) ) == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002894 ( ret = mpi_miller_rabin( X, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002895 == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002896 ( ret = mpi_miller_rabin( &Y, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002897 == 0 )
Jethro Beekman66689272018-02-14 19:24:10 -08002898 goto cleanup;
2899
2900 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
2901 goto cleanup;
2902
2903 /*
2904 * Next candidates. We want to preserve Y = (X-1) / 2 and
2905 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2906 * so up Y by 6 and X by 12.
2907 */
2908 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 12 ) );
2909 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &Y, &Y, 6 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002910 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002911 }
2912 }
2913
2914cleanup:
2915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916 mbedtls_mpi_free( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002917
2918 return( ret );
2919}
2920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002924
Paul Bakker23986e52011-04-24 08:57:21 +00002925#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002926
2927static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2928{
2929 { 693, 609, 21 },
2930 { 1764, 868, 28 },
2931 { 768454923, 542167814, 1 }
2932};
2933
Paul Bakker5121ce52009-01-03 21:22:43 +00002934/*
2935 * Checkup routine
2936 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937int mbedtls_mpi_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002938{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002939 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00002941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); mbedtls_mpi_init( &X );
2943 mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &U ); mbedtls_mpi_init( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00002944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &A, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002946 "EFE021C2645FD1DC586E69184AF4A31E" \
2947 "D5F53E93B5F123FA41680867BA110131" \
2948 "944FE7952E2517337780CB0DB80E61AA" \
2949 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6" ) );
2950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002952 "B2E7EFD37075B9F03FF989C7C5051C20" \
2953 "34D2A323810251127E7BF8625A4F49A5" \
2954 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
2955 "5B5C25763222FEFCCFC38B832366C29E" ) );
2956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &N, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002958 "0066A198186C18C10B2F5ED9B522752A" \
2959 "9830B69916E535C8F047518A889A43A5" \
2960 "94B6BED27A168D31D4A52F88925AA8F5" ) );
2961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002965 "602AB7ECA597A3D6B56FF9829A5E8B85" \
2966 "9E857EA95A03512E2BAE7391688D264A" \
2967 "A5663B0341DB9CCFD2C4C5F421FEC814" \
2968 "8001B72E848A38CAE1C65F78E56ABDEF" \
2969 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
2970 "ECF677152EF804370C1A305CAF3B5BF1" \
2971 "30879B56C61DE584A0F53A2447A51E" ) );
2972
2973 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974 mbedtls_printf( " MPI test #1 (mul_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002977 {
2978 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002980
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002981 ret = 1;
2982 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002983 }
2984
2985 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &X, &Y, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002991 "256567336059E52CAE22925474705F39A94" ) );
2992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &V, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002994 "6613F26162223DF488E9CD48CC132C7A" \
2995 "0AC93C701B001B092E4E5B9F73BCD27B" \
2996 "9EE50D0657C77F374E903CDFA4C642" ) );
2997
2998 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999 mbedtls_printf( " MPI test #2 (div_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ||
3002 mbedtls_mpi_cmp_mpi( &Y, &V ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003003 {
3004 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003006
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003007 ret = 1;
3008 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003009 }
3010
3011 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &X, &A, &E, &N, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003017 "36E139AEA55215609D2816998ED020BB" \
3018 "BD96C37890F65171D948E9BC7CBAA4D9" \
3019 "325D24D6A3C12710F10A09FA08AB87" ) );
3020
3021 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022 mbedtls_printf( " MPI test #3 (exp_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003025 {
3026 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003027 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003028
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003029 ret = 1;
3030 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003031 }
3032
3033 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003038 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00003039 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
3040 "C3DBA76456363A10869622EAC2DD84EC" \
3041 "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) );
3042
3043 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003044 mbedtls_printf( " MPI test #4 (inv_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00003045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 {
3048 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003050
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003051 ret = 1;
3052 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00003053 }
3054
3055 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003057
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003058 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059 mbedtls_printf( " MPI test #5 (simple gcd): " );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003060
Paul Bakker66d5d072014-06-17 16:39:18 +02003061 for( i = 0; i < GCD_PAIR_COUNT; i++ )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &X, gcd_pairs[i][0] ) );
3064 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Y, gcd_pairs[i][1] ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &A, &X, &Y ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003068 if( mbedtls_mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 )
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003069 {
3070 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071 mbedtls_printf( "failed at %d\n", i );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003072
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01003073 ret = 1;
3074 goto cleanup;
3075 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003076 }
3077
3078 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079 mbedtls_printf( "passed\n" );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00003080
Paul Bakker5121ce52009-01-03 21:22:43 +00003081cleanup:
3082
3083 if( ret != 0 && verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084 mbedtls_printf( "Unexpected error, return code = %08X\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &X );
3087 mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &U ); mbedtls_mpi_free( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00003088
3089 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00003091
3092 return( ret );
3093}
3094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00003096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097#endif /* MBEDTLS_BIGNUM_C */