blob: f133f6c13c23c5c00815e2ad34864b45eb5a0ada [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Multi-precision integer library
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
Simon Butcher15b15d12015-11-26 19:35:03 +000046
Paul Bakker5121ce52009-01-03 21:22:43 +000047/*
Simon Butcher15b15d12015-11-26 19:35:03 +000048 * The following sources were referenced in the design of this Multi-precision
49 * Integer library:
Paul Bakker5121ce52009-01-03 21:22:43 +000050 *
Simon Butcher15b15d12015-11-26 19:35:03 +000051 * [1] Handbook of Applied Cryptography - 1997
52 * Menezes, van Oorschot and Vanstone
53 *
54 * [2] Multi-Precision Math
55 * Tom St Denis
56 * https://github.com/libtom/libtommath/blob/develop/tommath.pdf
57 *
58 * [3] GNU Multi-Precision Arithmetic Library
59 * https://gmplib.org/manual/index.html
60 *
Simon Butcherf5ba0452015-12-27 23:01:55 +000061 */
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020065#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020067#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000071#include "mbedtls/bignum.h"
72#include "mbedtls/bn_mul.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050073#include "mbedtls/platform_util.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Rich Evans00ab4702015-02-06 13:43:58 +000075#include <string.h>
76
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000078#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020079#else
Rich Evans00ab4702015-02-06 13:43:58 +000080#include <stdio.h>
81#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#define mbedtls_printf printf
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020083#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020085#endif
86
Hanno Becker73d7d792018-12-11 10:35:51 +000087#define MPI_VALIDATE_RET( cond ) \
88 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA )
89#define MPI_VALIDATE( cond ) \
90 MBEDTLS_INTERNAL_VALIDATE( cond )
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
Paul Bakker5121ce52009-01-03 21:22:43 +000093#define biL (ciL << 3) /* bits in limb */
94#define biH (ciL << 2) /* half limb size */
95
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +010096#define MPI_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
97
Paul Bakker5121ce52009-01-03 21:22:43 +000098/*
99 * Convert between bits/chars and number of limbs
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +0200100 * Divide first in order to avoid potential overflows
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 */
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +0200102#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
103#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500105/* Implementation that should never be optimized out by the compiler */
Andres Amaya Garcia6698d2f2018-04-24 08:39:07 -0500106static void mbedtls_mpi_zeroize( mbedtls_mpi_uint *v, size_t n )
107{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500108 mbedtls_platform_zeroize( v, ciL * n );
109}
110
Paul Bakker5121ce52009-01-03 21:22:43 +0000111/*
Paul Bakker6c591fa2011-05-05 11:49:20 +0000112 * Initialize one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114void mbedtls_mpi_init( mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000115{
Hanno Becker73d7d792018-12-11 10:35:51 +0000116 MPI_VALIDATE( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
Paul Bakker6c591fa2011-05-05 11:49:20 +0000118 X->s = 1;
119 X->n = 0;
120 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000121}
122
123/*
Paul Bakker6c591fa2011-05-05 11:49:20 +0000124 * Unallocate one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126void mbedtls_mpi_free( mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000127{
Paul Bakker6c591fa2011-05-05 11:49:20 +0000128 if( X == NULL )
129 return;
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
Paul Bakker6c591fa2011-05-05 11:49:20 +0000131 if( X->p != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 {
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200133 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 }
136
Paul Bakker6c591fa2011-05-05 11:49:20 +0000137 X->s = 1;
138 X->n = 0;
139 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000140}
141
142/*
143 * Enlarge to the specified number of limbs
144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000146{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_mpi_uint *p;
Hanno Becker73d7d792018-12-11 10:35:51 +0000148 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200151 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Paul Bakkerf9688572011-05-05 10:00:45 +0000152
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 if( X->n < nblimbs )
154 {
Simon Butcher29176892016-05-20 00:19:09 +0100155 if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( nblimbs, ciL ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200156 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 if( X->p != NULL )
159 {
160 memcpy( p, X->p, X->n * ciL );
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200161 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 }
164
165 X->n = nblimbs;
166 X->p = p;
167 }
168
169 return( 0 );
170}
171
172/*
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100173 * Resize down as much as possible,
174 * while keeping at least the specified number of limbs
175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100177{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_mpi_uint *p;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100179 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000180 MPI_VALIDATE_RET( X != NULL );
181
182 if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
183 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100184
Gilles Peskine27c15c72020-01-20 21:17:43 +0100185 /* Actually resize up if there are currently fewer than nblimbs limbs. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100186 if( X->n <= nblimbs )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 return( mbedtls_mpi_grow( X, nblimbs ) );
Gilles Peskine56427c22020-01-21 13:59:51 +0100188 /* After this point, then X->n > nblimbs and in particular X->n > 0. */
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100189
190 for( i = X->n - 1; i > 0; i-- )
191 if( X->p[i] != 0 )
192 break;
193 i++;
194
195 if( i < nblimbs )
196 i = nblimbs;
197
Simon Butcher29176892016-05-20 00:19:09 +0100198 if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( i, ciL ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200199 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100200
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100201 if( X->p != NULL )
202 {
203 memcpy( p, X->p, i * ciL );
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200204 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_free( X->p );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100206 }
207
208 X->n = i;
209 X->p = p;
210
211 return( 0 );
212}
213
214/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 * Copy the contents of Y into X
216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000218{
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100219 int ret = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000220 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000221 MPI_VALIDATE_RET( X != NULL );
222 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
224 if( X == Y )
225 return( 0 );
226
Gilles Peskine3e9f5222020-01-20 21:12:50 +0100227 if( Y->n == 0 )
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_mpi_free( X );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200230 return( 0 );
231 }
232
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 for( i = Y->n - 1; i > 0; i-- )
234 if( Y->p[i] != 0 )
235 break;
236 i++;
237
238 X->s = Y->s;
239
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100240 if( X->n < i )
241 {
242 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i ) );
243 }
244 else
245 {
246 memset( X->p + i, 0, ( X->n - i ) * ciL );
247 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000248
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 memcpy( X->p, Y->p, i * ciL );
250
251cleanup:
252
253 return( ret );
254}
255
256/*
257 * Swap the contents of X and Y
258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000260{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000262 MPI_VALIDATE( X != NULL );
263 MPI_VALIDATE( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 memcpy( &T, X, sizeof( mbedtls_mpi ) );
266 memcpy( X, Y, sizeof( mbedtls_mpi ) );
267 memcpy( Y, &T, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000268}
269
270/*
Gilles Peskinec81c5882020-06-04 19:14:58 +0200271 * Conditionally assign dest = src, without leaking information
272 * about whether the assignment was made or not.
273 * dest and src must be arrays of limbs of size n.
274 * assign must be 0 or 1.
275 */
276static void mpi_safe_cond_assign( size_t n,
277 mbedtls_mpi_uint *dest,
278 const mbedtls_mpi_uint *src,
279 unsigned char assign )
280{
281 size_t i;
282 for( i = 0; i < n; i++ )
283 dest[i] = dest[i] * ( 1 - assign ) + src[i] * assign;
284}
285
286/*
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100287 * Conditionally assign X = Y, without leaking information
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +0100288 * about whether the assignment was made or not.
289 * (Leaking information about the respective sizes of X and Y is ok however.)
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100292{
293 int ret = 0;
294 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000295 MPI_VALIDATE_RET( X != NULL );
296 MPI_VALIDATE_RET( Y != NULL );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100297
Pascal Junodb99183d2015-03-11 16:49:45 +0100298 /* make sure assign is 0 or 1 in a time-constant manner */
299 assign = (assign | (unsigned char)-assign) >> 7;
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100302
Paul Bakker66d5d072014-06-17 16:39:18 +0200303 X->s = X->s * ( 1 - assign ) + Y->s * assign;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100304
Gilles Peskinec81c5882020-06-04 19:14:58 +0200305 mpi_safe_cond_assign( Y->n, X->p, Y->p, assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100306
Gilles Peskinec81c5882020-06-04 19:14:58 +0200307 for( i = Y->n; i < X->n; i++ )
Paul Bakker66d5d072014-06-17 16:39:18 +0200308 X->p[i] *= ( 1 - assign );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100309
310cleanup:
311 return( ret );
312}
313
314/*
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100315 * Conditionally swap X and Y, without leaking information
316 * about whether the swap was made or not.
317 * Here it is not ok to simply swap the pointers, which whould lead to
318 * different memory access patterns when X and Y are used afterwards.
319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100321{
322 int ret, s;
323 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 mbedtls_mpi_uint tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +0000325 MPI_VALIDATE_RET( X != NULL );
326 MPI_VALIDATE_RET( Y != NULL );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100327
328 if( X == Y )
329 return( 0 );
330
Pascal Junodb99183d2015-03-11 16:49:45 +0100331 /* make sure swap is 0 or 1 in a time-constant manner */
332 swap = (swap | (unsigned char)-swap) >> 7;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
335 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( Y, X->n ) );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100336
337 s = X->s;
Paul Bakker66d5d072014-06-17 16:39:18 +0200338 X->s = X->s * ( 1 - swap ) + Y->s * swap;
339 Y->s = Y->s * ( 1 - swap ) + s * swap;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100340
341
342 for( i = 0; i < X->n; i++ )
343 {
344 tmp = X->p[i];
Paul Bakker66d5d072014-06-17 16:39:18 +0200345 X->p[i] = X->p[i] * ( 1 - swap ) + Y->p[i] * swap;
346 Y->p[i] = Y->p[i] * ( 1 - swap ) + tmp * swap;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100347 }
348
349cleanup:
350 return( ret );
351}
352
353/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000354 * Set value from integer
355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +0000357{
358 int ret;
Hanno Becker73d7d792018-12-11 10:35:51 +0000359 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 memset( X->p, 0, X->n * ciL );
363
364 X->p[0] = ( z < 0 ) ? -z : z;
365 X->s = ( z < 0 ) ? -1 : 1;
366
367cleanup:
368
369 return( ret );
370}
371
372/*
Paul Bakker2f5947e2011-05-18 15:47:11 +0000373 * Get a specific bit
374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000376{
Hanno Becker73d7d792018-12-11 10:35:51 +0000377 MPI_VALIDATE_RET( X != NULL );
378
Paul Bakker2f5947e2011-05-18 15:47:11 +0000379 if( X->n * biL <= pos )
380 return( 0 );
381
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200382 return( ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000383}
384
Gilles Peskine11cdb052018-11-20 16:47:47 +0100385/* Get a specific byte, without range checks. */
386#define GET_BYTE( X, i ) \
387 ( ( ( X )->p[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff )
388
Paul Bakker2f5947e2011-05-18 15:47:11 +0000389/*
390 * Set a bit to a specific value of 0 or 1
391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000393{
394 int ret = 0;
395 size_t off = pos / biL;
396 size_t idx = pos % biL;
Hanno Becker73d7d792018-12-11 10:35:51 +0000397 MPI_VALIDATE_RET( X != NULL );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000398
399 if( val != 0 && val != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker9af723c2014-05-01 13:03:14 +0200401
Paul Bakker2f5947e2011-05-18 15:47:11 +0000402 if( X->n * biL <= pos )
403 {
404 if( val == 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200405 return( 0 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, off + 1 ) );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000408 }
409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 X->p[off] &= ~( (mbedtls_mpi_uint) 0x01 << idx );
411 X->p[off] |= (mbedtls_mpi_uint) val << idx;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000412
413cleanup:
Paul Bakker9af723c2014-05-01 13:03:14 +0200414
Paul Bakker2f5947e2011-05-18 15:47:11 +0000415 return( ret );
416}
417
418/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200419 * Return the number of less significant zero-bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421size_t mbedtls_mpi_lsb( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000422{
Paul Bakker23986e52011-04-24 08:57:21 +0000423 size_t i, j, count = 0;
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000424 MBEDTLS_INTERNAL_VALIDATE_RET( X != NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
426 for( i = 0; i < X->n; i++ )
Paul Bakkerf9688572011-05-05 10:00:45 +0000427 for( j = 0; j < biL; j++, count++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 if( ( ( X->p[i] >> j ) & 1 ) != 0 )
429 return( count );
430
431 return( 0 );
432}
433
434/*
Simon Butcher15b15d12015-11-26 19:35:03 +0000435 * Count leading zero bits in a given integer
436 */
437static size_t mbedtls_clz( const mbedtls_mpi_uint x )
438{
439 size_t j;
Manuel Pégourié-Gonnarde3e8edf2015-12-01 09:31:52 +0100440 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
Simon Butcher15b15d12015-11-26 19:35:03 +0000441
442 for( j = 0; j < biL; j++ )
443 {
444 if( x & mask ) break;
445
446 mask >>= 1;
447 }
448
449 return j;
450}
451
452/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200453 * Return the number of bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200455size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000456{
Paul Bakker23986e52011-04-24 08:57:21 +0000457 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200459 if( X->n == 0 )
460 return( 0 );
461
Paul Bakker5121ce52009-01-03 21:22:43 +0000462 for( i = X->n - 1; i > 0; i-- )
463 if( X->p[i] != 0 )
464 break;
465
Simon Butcher15b15d12015-11-26 19:35:03 +0000466 j = biL - mbedtls_clz( X->p[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
Paul Bakker23986e52011-04-24 08:57:21 +0000468 return( ( i * biL ) + j );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469}
470
471/*
472 * Return the total size in bytes
473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474size_t mbedtls_mpi_size( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000475{
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200476 return( ( mbedtls_mpi_bitlen( X ) + 7 ) >> 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000477}
478
479/*
480 * Convert an ASCII character to digit value
481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482static int mpi_get_digit( mbedtls_mpi_uint *d, int radix, char c )
Paul Bakker5121ce52009-01-03 21:22:43 +0000483{
484 *d = 255;
485
486 if( c >= 0x30 && c <= 0x39 ) *d = c - 0x30;
487 if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37;
488 if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57;
489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 if( *d >= (mbedtls_mpi_uint) radix )
491 return( MBEDTLS_ERR_MPI_INVALID_CHARACTER );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492
493 return( 0 );
494}
495
496/*
497 * Import from an ASCII string
498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000500{
Paul Bakker23986e52011-04-24 08:57:21 +0000501 int ret;
502 size_t i, j, slen, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 mbedtls_mpi_uint d;
504 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000505 MPI_VALIDATE_RET( X != NULL );
506 MPI_VALIDATE_RET( s != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
508 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000509 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
Paul Bakkerff60ee62010-03-16 21:09:09 +0000513 slen = strlen( s );
514
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 if( radix == 16 )
516 {
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +0100517 if( slen > MPI_SIZE_T_MAX >> 2 )
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +0200518 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
519
Paul Bakkerff60ee62010-03-16 21:09:09 +0000520 n = BITS_TO_LIMBS( slen << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n ) );
523 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
Paul Bakker23986e52011-04-24 08:57:21 +0000525 for( i = slen, j = 0; i > 0; i--, j++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000526 {
Paul Bakker23986e52011-04-24 08:57:21 +0000527 if( i == 1 && s[i - 1] == '-' )
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 {
529 X->s = -1;
530 break;
531 }
532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200534 X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 }
536 }
537 else
538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Paul Bakkerff60ee62010-03-16 21:09:09 +0000541 for( i = 0; i < slen; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000542 {
543 if( i == 0 && s[i] == '-' )
544 {
545 X->s = -1;
546 continue;
547 }
548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
550 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T, X, radix ) );
Paul Bakker05feca62009-06-20 08:22:43 +0000551
552 if( X->s == 1 )
553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, &T, d ) );
Paul Bakker05feca62009-06-20 08:22:43 +0000555 }
556 else
557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( X, &T, d ) );
Paul Bakker05feca62009-06-20 08:22:43 +0000559 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 }
561 }
562
563cleanup:
564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000566
567 return( ret );
568}
569
570/*
Ron Eldora16fa292018-11-20 14:07:01 +0200571 * Helper to write the digits high-order first.
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 */
Ron Eldora16fa292018-11-20 14:07:01 +0200573static int mpi_write_hlp( mbedtls_mpi *X, int radix,
574 char **p, const size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000575{
576 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_mpi_uint r;
Ron Eldora16fa292018-11-20 14:07:01 +0200578 size_t length = 0;
579 char *p_end = *p + buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
Ron Eldora16fa292018-11-20 14:07:01 +0200581 do
582 {
583 if( length >= buflen )
584 {
585 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
586 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000587
Ron Eldora16fa292018-11-20 14:07:01 +0200588 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) );
589 MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) );
590 /*
591 * Write the residue in the current position, as an ASCII character.
592 */
593 if( r < 0xA )
594 *(--p_end) = (char)( '0' + r );
595 else
596 *(--p_end) = (char)( 'A' + ( r - 0xA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000597
Ron Eldora16fa292018-11-20 14:07:01 +0200598 length++;
599 } while( mbedtls_mpi_cmp_int( X, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
Ron Eldora16fa292018-11-20 14:07:01 +0200601 memmove( *p, p_end, length );
602 *p += length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000603
604cleanup:
605
606 return( ret );
607}
608
609/*
610 * Export into an ASCII string
611 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100612int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
613 char *buf, size_t buflen, size_t *olen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000614{
Paul Bakker23986e52011-04-24 08:57:21 +0000615 int ret = 0;
616 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000619 MPI_VALIDATE_RET( X != NULL );
620 MPI_VALIDATE_RET( olen != NULL );
621 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000622
623 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000624 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
Hanno Beckerc1fa6cd2019-02-04 09:45:07 +0000626 n = mbedtls_mpi_bitlen( X ); /* Number of bits necessary to present `n`. */
627 if( radix >= 4 ) n >>= 1; /* Number of 4-adic digits necessary to present
628 * `n`. If radix > 4, this might be a strict
629 * overapproximation of the number of
630 * radix-adic digits needed to present `n`. */
631 if( radix >= 16 ) n >>= 1; /* Number of hexadecimal digits necessary to
632 * present `n`. */
633
Janos Follath870ed002019-03-06 13:43:02 +0000634 n += 1; /* Terminating null byte */
Hanno Beckerc1fa6cd2019-02-04 09:45:07 +0000635 n += 1; /* Compensate for the divisions above, which round down `n`
636 * in case it's not even. */
637 n += 1; /* Potential '-'-sign. */
638 n += ( n & 1 ); /* Make n even to have enough space for hexadecimal writing,
639 * which always uses an even number of hex-digits. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000640
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100641 if( buflen < n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100643 *olen = n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000645 }
646
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100647 p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000649
650 if( X->s == -1 )
Hanno Beckeraf97cae2019-02-01 16:41:30 +0000651 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000652 *p++ = '-';
Hanno Beckeraf97cae2019-02-01 16:41:30 +0000653 buflen--;
654 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
656 if( radix == 16 )
657 {
Paul Bakker23986e52011-04-24 08:57:21 +0000658 int c;
659 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000660
Paul Bakker23986e52011-04-24 08:57:21 +0000661 for( i = X->n, k = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000662 {
Paul Bakker23986e52011-04-24 08:57:21 +0000663 for( j = ciL; j > 0; j-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000664 {
Paul Bakker23986e52011-04-24 08:57:21 +0000665 c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000666
Paul Bakker6c343d72014-07-10 14:36:19 +0200667 if( c == 0 && k == 0 && ( i + j ) != 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 continue;
669
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000670 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000671 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 k = 1;
673 }
674 }
675 }
676 else
677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T, X ) );
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000679
680 if( T.s == -1 )
681 T.s = 1;
682
Ron Eldora16fa292018-11-20 14:07:01 +0200683 MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p, buflen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000684 }
685
686 *p++ = '\0';
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100687 *olen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
689cleanup:
690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
693 return( ret );
694}
695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000697/*
698 * Read X from an opened file
699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin )
Paul Bakker5121ce52009-01-03 21:22:43 +0000701{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_mpi_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000703 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000704 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000705 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000706 * Buffer should have space for (short) label and decimal formatted MPI,
707 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
Hanno Becker73d7d792018-12-11 10:35:51 +0000711 MPI_VALIDATE_RET( X != NULL );
712 MPI_VALIDATE_RET( fin != NULL );
713
714 if( radix < 2 || radix > 16 )
715 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
716
Paul Bakker5121ce52009-01-03 21:22:43 +0000717 memset( s, 0, sizeof( s ) );
718 if( fgets( s, sizeof( s ) - 1, fin ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000720
721 slen = strlen( s );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000722 if( slen == sizeof( s ) - 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000724
Hanno Beckerb2034b72017-04-26 11:46:46 +0100725 if( slen > 0 && s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
726 if( slen > 0 && s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
Paul Bakker5121ce52009-01-03 21:22:43 +0000727
728 p = s + slen;
Hanno Beckerb2034b72017-04-26 11:46:46 +0100729 while( p-- > s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000730 if( mpi_get_digit( &d, radix, *p ) != 0 )
731 break;
732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 return( mbedtls_mpi_read_string( X, radix, p + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000734}
735
736/*
737 * Write X into an opened file (or stdout if fout == NULL)
738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout )
Paul Bakker5121ce52009-01-03 21:22:43 +0000740{
Paul Bakker23986e52011-04-24 08:57:21 +0000741 int ret;
742 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000743 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000744 * Buffer should have space for (short) label and decimal formatted MPI,
745 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000746 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Hanno Becker73d7d792018-12-11 10:35:51 +0000748 MPI_VALIDATE_RET( X != NULL );
749
750 if( radix < 2 || radix > 16 )
751 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000752
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100753 memset( s, 0, sizeof( s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000754
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100755 MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, sizeof( s ) - 2, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000756
757 if( p == NULL ) p = "";
758
759 plen = strlen( p );
760 slen = strlen( s );
761 s[slen++] = '\r';
762 s[slen++] = '\n';
763
764 if( fout != NULL )
765 {
766 if( fwrite( p, 1, plen, fout ) != plen ||
767 fwrite( s, 1, slen, fout ) != slen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000769 }
770 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_printf( "%s%s", p, s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000772
773cleanup:
774
775 return( ret );
776}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000778
Hanno Beckerda1655a2017-10-18 14:21:44 +0100779
780/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
781 * into the storage form used by mbedtls_mpi. */
Hanno Beckerf8720072018-11-08 11:53:49 +0000782
783static mbedtls_mpi_uint mpi_uint_bigendian_to_host_c( mbedtls_mpi_uint x )
784{
785 uint8_t i;
Hanno Becker92c98932019-05-01 17:09:11 +0100786 unsigned char *x_ptr;
Hanno Beckerf8720072018-11-08 11:53:49 +0000787 mbedtls_mpi_uint tmp = 0;
Hanno Becker92c98932019-05-01 17:09:11 +0100788
789 for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
790 {
791 tmp <<= CHAR_BIT;
792 tmp |= (mbedtls_mpi_uint) *x_ptr;
793 }
794
Hanno Beckerf8720072018-11-08 11:53:49 +0000795 return( tmp );
796}
797
798static mbedtls_mpi_uint mpi_uint_bigendian_to_host( mbedtls_mpi_uint x )
799{
800#if defined(__BYTE_ORDER__)
801
802/* Nothing to do on bigendian systems. */
803#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
804 return( x );
805#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
806
807#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
808
809/* For GCC and Clang, have builtins for byte swapping. */
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000810#if defined(__GNUC__) && defined(__GNUC_PREREQ)
811#if __GNUC_PREREQ(4,3)
Hanno Beckerf8720072018-11-08 11:53:49 +0000812#define have_bswap
813#endif
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000814#endif
815
816#if defined(__clang__) && defined(__has_builtin)
817#if __has_builtin(__builtin_bswap32) && \
818 __has_builtin(__builtin_bswap64)
819#define have_bswap
820#endif
821#endif
822
Hanno Beckerf8720072018-11-08 11:53:49 +0000823#if defined(have_bswap)
824 /* The compiler is hopefully able to statically evaluate this! */
825 switch( sizeof(mbedtls_mpi_uint) )
826 {
827 case 4:
828 return( __builtin_bswap32(x) );
829 case 8:
830 return( __builtin_bswap64(x) );
831 }
832#endif
833#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
834#endif /* __BYTE_ORDER__ */
835
836 /* Fall back to C-based reordering if we don't know the byte order
837 * or we couldn't use a compiler-specific builtin. */
838 return( mpi_uint_bigendian_to_host_c( x ) );
839}
840
Hanno Becker2be8a552018-10-25 12:40:09 +0100841static void mpi_bigendian_to_host( mbedtls_mpi_uint * const p, size_t limbs )
Hanno Beckerda1655a2017-10-18 14:21:44 +0100842{
Hanno Beckerda1655a2017-10-18 14:21:44 +0100843 mbedtls_mpi_uint *cur_limb_left;
844 mbedtls_mpi_uint *cur_limb_right;
Hanno Becker2be8a552018-10-25 12:40:09 +0100845 if( limbs == 0 )
846 return;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100847
848 /*
849 * Traverse limbs and
850 * - adapt byte-order in each limb
851 * - swap the limbs themselves.
852 * For that, simultaneously traverse the limbs from left to right
853 * and from right to left, as long as the left index is not bigger
854 * than the right index (it's not a problem if limbs is odd and the
855 * indices coincide in the last iteration).
856 */
Hanno Beckerda1655a2017-10-18 14:21:44 +0100857 for( cur_limb_left = p, cur_limb_right = p + ( limbs - 1 );
858 cur_limb_left <= cur_limb_right;
859 cur_limb_left++, cur_limb_right-- )
860 {
Hanno Beckerf8720072018-11-08 11:53:49 +0000861 mbedtls_mpi_uint tmp;
862 /* Note that if cur_limb_left == cur_limb_right,
863 * this code effectively swaps the bytes only once. */
864 tmp = mpi_uint_bigendian_to_host( *cur_limb_left );
865 *cur_limb_left = mpi_uint_bigendian_to_host( *cur_limb_right );
866 *cur_limb_right = tmp;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100867 }
Hanno Beckerda1655a2017-10-18 14:21:44 +0100868}
869
Paul Bakker5121ce52009-01-03 21:22:43 +0000870/*
871 * Import X from unsigned binary data, big endian
872 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000874{
Paul Bakker23986e52011-04-24 08:57:21 +0000875 int ret;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100876 size_t const limbs = CHARS_TO_LIMBS( buflen );
877 size_t const overhead = ( limbs * ciL ) - buflen;
878 unsigned char *Xp;
Paul Bakker5121ce52009-01-03 21:22:43 +0000879
Hanno Becker8ce11a32018-12-19 16:18:52 +0000880 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +0000881 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
882
Hanno Becker073c1992017-10-17 15:17:27 +0100883 /* Ensure that target MPI has exactly the necessary number of limbs */
884 if( X->n != limbs )
885 {
886 mbedtls_mpi_free( X );
887 mbedtls_mpi_init( X );
888 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, limbs ) );
889 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000891
Hanno Becker0e810b92019-01-03 17:13:11 +0000892 /* Avoid calling `memcpy` with NULL source argument,
893 * even if buflen is 0. */
894 if( buf != NULL )
895 {
896 Xp = (unsigned char*) X->p;
897 memcpy( Xp + overhead, buf, buflen );
Hanno Beckerda1655a2017-10-18 14:21:44 +0100898
Hanno Becker0e810b92019-01-03 17:13:11 +0000899 mpi_bigendian_to_host( X->p, limbs );
900 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000901
902cleanup:
903
904 return( ret );
905}
906
907/*
908 * Export X into unsigned binary data, big endian
909 */
Gilles Peskine11cdb052018-11-20 16:47:47 +0100910int mbedtls_mpi_write_binary( const mbedtls_mpi *X,
911 unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000912{
Hanno Becker73d7d792018-12-11 10:35:51 +0000913 size_t stored_bytes;
Gilles Peskine11cdb052018-11-20 16:47:47 +0100914 size_t bytes_to_copy;
915 unsigned char *p;
916 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000917
Hanno Becker73d7d792018-12-11 10:35:51 +0000918 MPI_VALIDATE_RET( X != NULL );
919 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
920
921 stored_bytes = X->n * ciL;
922
Gilles Peskine11cdb052018-11-20 16:47:47 +0100923 if( stored_bytes < buflen )
924 {
925 /* There is enough space in the output buffer. Write initial
926 * null bytes and record the position at which to start
927 * writing the significant bytes. In this case, the execution
928 * trace of this function does not depend on the value of the
929 * number. */
930 bytes_to_copy = stored_bytes;
931 p = buf + buflen - stored_bytes;
932 memset( buf, 0, buflen - stored_bytes );
933 }
934 else
935 {
936 /* The output buffer is smaller than the allocated size of X.
937 * However X may fit if its leading bytes are zero. */
938 bytes_to_copy = buflen;
939 p = buf;
940 for( i = bytes_to_copy; i < stored_bytes; i++ )
941 {
942 if( GET_BYTE( X, i ) != 0 )
943 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
944 }
945 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000946
Gilles Peskine11cdb052018-11-20 16:47:47 +0100947 for( i = 0; i < bytes_to_copy; i++ )
948 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000949
950 return( 0 );
951}
952
953/*
954 * Left-shift: X <<= count
955 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +0000957{
Paul Bakker23986e52011-04-24 08:57:21 +0000958 int ret;
959 size_t i, v0, t1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +0000961 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000962
963 v0 = count / (biL );
964 t1 = count & (biL - 1);
965
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200966 i = mbedtls_mpi_bitlen( X ) + count;
Paul Bakker5121ce52009-01-03 21:22:43 +0000967
Paul Bakkerf9688572011-05-05 10:00:45 +0000968 if( X->n * biL < i )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000970
971 ret = 0;
972
973 /*
974 * shift by count / limb_size
975 */
976 if( v0 > 0 )
977 {
Paul Bakker23986e52011-04-24 08:57:21 +0000978 for( i = X->n; i > v0; i-- )
979 X->p[i - 1] = X->p[i - v0 - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +0000980
Paul Bakker23986e52011-04-24 08:57:21 +0000981 for( ; i > 0; i-- )
982 X->p[i - 1] = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000983 }
984
985 /*
986 * shift by count % limb_size
987 */
988 if( t1 > 0 )
989 {
990 for( i = v0; i < X->n; i++ )
991 {
992 r1 = X->p[i] >> (biL - t1);
993 X->p[i] <<= t1;
994 X->p[i] |= r0;
995 r0 = r1;
996 }
997 }
998
999cleanup:
1000
1001 return( ret );
1002}
1003
1004/*
1005 * Right-shift: X >>= count
1006 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +00001008{
Paul Bakker23986e52011-04-24 08:57:21 +00001009 size_t i, v0, v1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +00001011 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001012
1013 v0 = count / biL;
1014 v1 = count & (biL - 1);
1015
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001016 if( v0 > X->n || ( v0 == X->n && v1 > 0 ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 return mbedtls_mpi_lset( X, 0 );
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001018
Paul Bakker5121ce52009-01-03 21:22:43 +00001019 /*
1020 * shift by count / limb_size
1021 */
1022 if( v0 > 0 )
1023 {
1024 for( i = 0; i < X->n - v0; i++ )
1025 X->p[i] = X->p[i + v0];
1026
1027 for( ; i < X->n; i++ )
1028 X->p[i] = 0;
1029 }
1030
1031 /*
1032 * shift by count % limb_size
1033 */
1034 if( v1 > 0 )
1035 {
Paul Bakker23986e52011-04-24 08:57:21 +00001036 for( i = X->n; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001037 {
Paul Bakker23986e52011-04-24 08:57:21 +00001038 r1 = X->p[i - 1] << (biL - v1);
1039 X->p[i - 1] >>= v1;
1040 X->p[i - 1] |= r0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001041 r0 = r1;
1042 }
1043 }
1044
1045 return( 0 );
1046}
1047
1048/*
1049 * Compare unsigned values
1050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001052{
Paul Bakker23986e52011-04-24 08:57:21 +00001053 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001054 MPI_VALIDATE_RET( X != NULL );
1055 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001056
Paul Bakker23986e52011-04-24 08:57:21 +00001057 for( i = X->n; i > 0; i-- )
1058 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001059 break;
1060
Paul Bakker23986e52011-04-24 08:57:21 +00001061 for( j = Y->n; j > 0; j-- )
1062 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001063 break;
1064
Paul Bakker23986e52011-04-24 08:57:21 +00001065 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001066 return( 0 );
1067
1068 if( i > j ) return( 1 );
1069 if( j > i ) return( -1 );
1070
Paul Bakker23986e52011-04-24 08:57:21 +00001071 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001072 {
Paul Bakker23986e52011-04-24 08:57:21 +00001073 if( X->p[i - 1] > Y->p[i - 1] ) return( 1 );
1074 if( X->p[i - 1] < Y->p[i - 1] ) return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001075 }
1076
1077 return( 0 );
1078}
1079
1080/*
1081 * Compare signed values
1082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001084{
Paul Bakker23986e52011-04-24 08:57:21 +00001085 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001086 MPI_VALIDATE_RET( X != NULL );
1087 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001088
Paul Bakker23986e52011-04-24 08:57:21 +00001089 for( i = X->n; i > 0; i-- )
1090 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 break;
1092
Paul Bakker23986e52011-04-24 08:57:21 +00001093 for( j = Y->n; j > 0; j-- )
1094 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001095 break;
1096
Paul Bakker23986e52011-04-24 08:57:21 +00001097 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001098 return( 0 );
1099
1100 if( i > j ) return( X->s );
Paul Bakker0c8f73b2012-03-22 14:08:57 +00001101 if( j > i ) return( -Y->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001102
1103 if( X->s > 0 && Y->s < 0 ) return( 1 );
1104 if( Y->s > 0 && X->s < 0 ) return( -1 );
1105
Paul Bakker23986e52011-04-24 08:57:21 +00001106 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001107 {
Paul Bakker23986e52011-04-24 08:57:21 +00001108 if( X->p[i - 1] > Y->p[i - 1] ) return( X->s );
1109 if( X->p[i - 1] < Y->p[i - 1] ) return( -X->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 }
1111
1112 return( 0 );
1113}
1114
Janos Follath45ec9902019-10-14 09:09:32 +01001115/** Decide if an integer is less than the other, without branches.
1116 *
1117 * \param x First integer.
1118 * \param y Second integer.
1119 *
1120 * \return 1 if \p x is less than \p y, 0 otherwise
1121 */
Janos Follath867a3ab2019-10-11 14:21:53 +01001122static unsigned ct_lt_mpi_uint( const mbedtls_mpi_uint x,
1123 const mbedtls_mpi_uint y )
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001124{
1125 mbedtls_mpi_uint ret;
1126 mbedtls_mpi_uint cond;
1127
1128 /*
1129 * Check if the most significant bits (MSB) of the operands are different.
1130 */
1131 cond = ( x ^ y );
1132 /*
1133 * If the MSB are the same then the difference x-y will be negative (and
1134 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
1135 */
1136 ret = ( x - y ) & ~cond;
1137 /*
1138 * If the MSB are different, then the operand with the MSB of 1 is the
1139 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
1140 * the MSB of y is 0.)
1141 */
1142 ret |= y & cond;
1143
1144
Janos Follath7a34bcf2019-10-14 08:59:14 +01001145 ret = ret >> ( biL - 1 );
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001146
Janos Follath359a01e2019-10-29 15:08:46 +00001147 return (unsigned) ret;
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001148}
1149
1150/*
1151 * Compare signed values in constant time
1152 */
Janos Follath867a3ab2019-10-11 14:21:53 +01001153int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y,
1154 unsigned *ret )
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001155{
1156 size_t i;
Janos Follathbd87a592019-10-28 12:23:18 +00001157 /* The value of any of these variables is either 0 or 1 at all times. */
Janos Follath1f21c1d2019-10-28 12:31:34 +00001158 unsigned cond, done, X_is_negative, Y_is_negative;
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001159
1160 MPI_VALIDATE_RET( X != NULL );
1161 MPI_VALIDATE_RET( Y != NULL );
1162 MPI_VALIDATE_RET( ret != NULL );
1163
1164 if( X->n != Y->n )
1165 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1166
1167 /*
Janos Follath58525182019-10-28 12:12:15 +00001168 * Set sign_N to 1 if N >= 0, 0 if N < 0.
1169 * We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0.
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001170 */
Janos Follath1f21c1d2019-10-28 12:31:34 +00001171 X_is_negative = ( X->s & 2 ) >> 1;
1172 Y_is_negative = ( Y->s & 2 ) >> 1;
Janos Follath867a3ab2019-10-11 14:21:53 +01001173
1174 /*
1175 * If the signs are different, then the positive operand is the bigger.
Janos Follath1f21c1d2019-10-28 12:31:34 +00001176 * That is if X is negative (X_is_negative == 1), then X < Y is true and it
1177 * is false if X is positive (X_is_negative == 0).
Janos Follath867a3ab2019-10-11 14:21:53 +01001178 */
Janos Follath1f21c1d2019-10-28 12:31:34 +00001179 cond = ( X_is_negative ^ Y_is_negative );
1180 *ret = cond & X_is_negative;
Janos Follath867a3ab2019-10-11 14:21:53 +01001181
1182 /*
Janos Follathbd87a592019-10-28 12:23:18 +00001183 * This is a constant-time function. We might have the result, but we still
Janos Follath867a3ab2019-10-11 14:21:53 +01001184 * need to go through the loop. Record if we have the result already.
1185 */
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001186 done = cond;
1187
1188 for( i = X->n; i > 0; i-- )
1189 {
1190 /*
Janos Follathe25f1ee2019-11-05 12:24:52 +00001191 * If Y->p[i - 1] < X->p[i - 1] then X < Y is true if and only if both
1192 * X and Y are negative.
Janos Follath867a3ab2019-10-11 14:21:53 +01001193 *
1194 * Again even if we can make a decision, we just mark the result and
1195 * the fact that we are done and continue looping.
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001196 */
Janos Follathe25f1ee2019-11-05 12:24:52 +00001197 cond = ct_lt_mpi_uint( Y->p[i - 1], X->p[i - 1] );
1198 *ret |= cond & ( 1 - done ) & X_is_negative;
Janos Follathfbe4c942019-10-28 12:37:21 +00001199 done |= cond;
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001200
1201 /*
Janos Follathe25f1ee2019-11-05 12:24:52 +00001202 * If X->p[i - 1] < Y->p[i - 1] then X < Y is true if and only if both
1203 * X and Y are positive.
Janos Follath867a3ab2019-10-11 14:21:53 +01001204 *
1205 * Again even if we can make a decision, we just mark the result and
1206 * the fact that we are done and continue looping.
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001207 */
Janos Follathe25f1ee2019-11-05 12:24:52 +00001208 cond = ct_lt_mpi_uint( X->p[i - 1], Y->p[i - 1] );
1209 *ret |= cond & ( 1 - done ) & ( 1 - X_is_negative );
Janos Follathfbe4c942019-10-28 12:37:21 +00001210 done |= cond;
Janos Follathb9f6f9b2019-09-05 14:47:19 +01001211 }
1212
1213 return( 0 );
1214}
1215
Paul Bakker5121ce52009-01-03 21:22:43 +00001216/*
1217 * Compare signed values
1218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +00001220{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 mbedtls_mpi Y;
1222 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001223 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001224
1225 *p = ( z < 0 ) ? -z : z;
1226 Y.s = ( z < 0 ) ? -1 : 1;
1227 Y.n = 1;
1228 Y.p = p;
1229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 return( mbedtls_mpi_cmp_mpi( X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001231}
1232
1233/*
1234 * Unsigned addition: X = |A| + |B| (HAC 14.7)
1235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001237{
Paul Bakker23986e52011-04-24 08:57:21 +00001238 int ret;
1239 size_t i, j;
Janos Follath6c922682015-10-30 17:43:11 +01001240 mbedtls_mpi_uint *o, *p, c, tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +00001241 MPI_VALIDATE_RET( X != NULL );
1242 MPI_VALIDATE_RET( A != NULL );
1243 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001244
1245 if( X == B )
1246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 const mbedtls_mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001248 }
1249
1250 if( X != A )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
Paul Bakker9af723c2014-05-01 13:03:14 +02001252
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001253 /*
1254 * X should always be positive as a result of unsigned additions.
1255 */
1256 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001257
Paul Bakker23986e52011-04-24 08:57:21 +00001258 for( j = B->n; j > 0; j-- )
1259 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001260 break;
1261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001263
1264 o = B->p; p = X->p; c = 0;
1265
Janos Follath6c922682015-10-30 17:43:11 +01001266 /*
1267 * tmp is used because it might happen that p == o
1268 */
Paul Bakker23986e52011-04-24 08:57:21 +00001269 for( i = 0; i < j; i++, o++, p++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001270 {
Janos Follath6c922682015-10-30 17:43:11 +01001271 tmp= *o;
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 *p += c; c = ( *p < c );
Janos Follath6c922682015-10-30 17:43:11 +01001273 *p += tmp; c += ( *p < tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +00001274 }
1275
1276 while( c != 0 )
1277 {
1278 if( i >= X->n )
1279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001281 p = X->p + i;
1282 }
1283
Paul Bakker2d319fd2012-09-16 21:34:26 +00001284 *p += c; c = ( *p < c ); i++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001285 }
1286
1287cleanup:
1288
1289 return( ret );
1290}
1291
Gilles Peskinede719d52020-06-09 10:39:38 +02001292/**
Gilles Peskine36acd542020-06-08 21:58:22 +02001293 * Helper for mbedtls_mpi subtraction.
1294 *
1295 * Calculate d - s where d and s have the same size.
1296 * This function operates modulo (2^ciL)^n and returns the carry
1297 * (1 if there was a wraparound, i.e. if `d < s`, and 0 otherwise).
1298 *
1299 * \param n Number of limbs of \p d and \p s.
1300 * \param[in,out] d On input, the left operand.
1301 * On output, the result of the subtraction:
Gilles Peskinede719d52020-06-09 10:39:38 +02001302 * \param[in] s The right operand.
Gilles Peskine36acd542020-06-08 21:58:22 +02001303 *
1304 * \return 1 if `d < s`.
1305 * 0 if `d >= s`.
Paul Bakker5121ce52009-01-03 21:22:43 +00001306 */
Gilles Peskine36acd542020-06-08 21:58:22 +02001307static mbedtls_mpi_uint mpi_sub_hlp( size_t n,
1308 mbedtls_mpi_uint *d,
1309 const mbedtls_mpi_uint *s )
Paul Bakker5121ce52009-01-03 21:22:43 +00001310{
Paul Bakker23986e52011-04-24 08:57:21 +00001311 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 mbedtls_mpi_uint c, z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001313
1314 for( i = c = 0; i < n; i++, s++, d++ )
1315 {
1316 z = ( *d < c ); *d -= c;
1317 c = ( *d < *s ) + z; *d -= *s;
1318 }
1319
Gilles Peskine36acd542020-06-08 21:58:22 +02001320 return( c );
Paul Bakker5121ce52009-01-03 21:22:43 +00001321}
1322
1323/*
Gilles Peskine08fd43c2020-06-08 22:50:35 +02001324 * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
Paul Bakker5121ce52009-01-03 21:22:43 +00001325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001327{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 mbedtls_mpi TB;
Paul Bakker23986e52011-04-24 08:57:21 +00001329 int ret;
1330 size_t n;
Gilles Peskine08fd43c2020-06-08 22:50:35 +02001331 mbedtls_mpi_uint carry;
Hanno Becker73d7d792018-12-11 10:35:51 +00001332 MPI_VALIDATE_RET( X != NULL );
1333 MPI_VALIDATE_RET( A != NULL );
1334 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337
1338 if( X == B )
1339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001341 B = &TB;
1342 }
1343
1344 if( X != A )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001346
Paul Bakker1ef7a532009-06-20 10:50:55 +00001347 /*
Paul Bakker60b1d102013-10-29 10:02:51 +01001348 * X should always be positive as a result of unsigned subtractions.
Paul Bakker1ef7a532009-06-20 10:50:55 +00001349 */
1350 X->s = 1;
1351
Paul Bakker5121ce52009-01-03 21:22:43 +00001352 ret = 0;
1353
Paul Bakker23986e52011-04-24 08:57:21 +00001354 for( n = B->n; n > 0; n-- )
1355 if( B->p[n - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 break;
Gilles Peskine6260b702021-01-27 22:30:43 +01001357 if( n > A->n )
1358 {
1359 /* B >= (2^ciL)^n > A */
1360 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1361 goto cleanup;
1362 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001363
Gilles Peskine08fd43c2020-06-08 22:50:35 +02001364 carry = mpi_sub_hlp( n, X->p, B->p );
1365 if( carry != 0 )
Gilles Peskine36acd542020-06-08 21:58:22 +02001366 {
Gilles Peskine08fd43c2020-06-08 22:50:35 +02001367 /* Propagate the carry to the first nonzero limb of X. */
1368 for( ; n < X->n && X->p[n] == 0; n++ )
1369 --X->p[n];
1370 /* If we ran out of space for the carry, it means that the result
1371 * is negative. */
1372 if( n == X->n )
Gilles Peskine84697ca2020-07-23 01:16:46 +02001373 {
1374 ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
1375 goto cleanup;
1376 }
Gilles Peskine08fd43c2020-06-08 22:50:35 +02001377 --X->p[n];
Gilles Peskine36acd542020-06-08 21:58:22 +02001378 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001379
1380cleanup:
1381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 mbedtls_mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001383
1384 return( ret );
1385}
1386
1387/*
1388 * Signed addition: X = A + B
1389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001391{
Hanno Becker73d7d792018-12-11 10:35:51 +00001392 int ret, s;
1393 MPI_VALIDATE_RET( X != NULL );
1394 MPI_VALIDATE_RET( A != NULL );
1395 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001396
Hanno Becker73d7d792018-12-11 10:35:51 +00001397 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001398 if( A->s * B->s < 0 )
1399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001403 X->s = s;
1404 }
1405 else
1406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001408 X->s = -s;
1409 }
1410 }
1411 else
1412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001414 X->s = s;
1415 }
1416
1417cleanup:
1418
1419 return( ret );
1420}
1421
1422/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001423 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001426{
Hanno Becker73d7d792018-12-11 10:35:51 +00001427 int ret, s;
1428 MPI_VALIDATE_RET( X != NULL );
1429 MPI_VALIDATE_RET( A != NULL );
1430 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001431
Hanno Becker73d7d792018-12-11 10:35:51 +00001432 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001433 if( A->s * B->s > 0 )
1434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 X->s = s;
1439 }
1440 else
1441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001443 X->s = -s;
1444 }
1445 }
1446 else
1447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001449 X->s = s;
1450 }
1451
1452cleanup:
1453
1454 return( ret );
1455}
1456
1457/*
1458 * Signed addition: X = A + b
1459 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001461{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 mbedtls_mpi _B;
1463 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001464 MPI_VALIDATE_RET( X != NULL );
1465 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001466
1467 p[0] = ( b < 0 ) ? -b : b;
1468 _B.s = ( b < 0 ) ? -1 : 1;
1469 _B.n = 1;
1470 _B.p = p;
1471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 return( mbedtls_mpi_add_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001473}
1474
1475/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001476 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001479{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 mbedtls_mpi _B;
1481 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001482 MPI_VALIDATE_RET( X != NULL );
1483 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001484
1485 p[0] = ( b < 0 ) ? -b : b;
1486 _B.s = ( b < 0 ) ? -1 : 1;
1487 _B.n = 1;
1488 _B.p = p;
1489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 return( mbedtls_mpi_sub_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001491}
1492
1493/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 * Helper for mbedtls_mpi multiplication
Paul Bakkerfc4f46f2013-06-24 19:23:56 +02001495 */
1496static
1497#if defined(__APPLE__) && defined(__arm__)
1498/*
1499 * Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
1500 * appears to need this to prevent bad ARM code generation at -O3.
1501 */
1502__attribute__ ((noinline))
1503#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504void 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 +00001505{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 mbedtls_mpi_uint c = 0, t = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001507
1508#if defined(MULADDC_HUIT)
1509 for( ; i >= 8; i -= 8 )
1510 {
1511 MULADDC_INIT
1512 MULADDC_HUIT
1513 MULADDC_STOP
1514 }
1515
1516 for( ; i > 0; i-- )
1517 {
1518 MULADDC_INIT
1519 MULADDC_CORE
1520 MULADDC_STOP
1521 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001522#else /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001523 for( ; i >= 16; i -= 16 )
1524 {
1525 MULADDC_INIT
1526 MULADDC_CORE MULADDC_CORE
1527 MULADDC_CORE MULADDC_CORE
1528 MULADDC_CORE MULADDC_CORE
1529 MULADDC_CORE MULADDC_CORE
1530
1531 MULADDC_CORE MULADDC_CORE
1532 MULADDC_CORE MULADDC_CORE
1533 MULADDC_CORE MULADDC_CORE
1534 MULADDC_CORE MULADDC_CORE
1535 MULADDC_STOP
1536 }
1537
1538 for( ; i >= 8; i -= 8 )
1539 {
1540 MULADDC_INIT
1541 MULADDC_CORE MULADDC_CORE
1542 MULADDC_CORE MULADDC_CORE
1543
1544 MULADDC_CORE MULADDC_CORE
1545 MULADDC_CORE MULADDC_CORE
1546 MULADDC_STOP
1547 }
1548
1549 for( ; i > 0; i-- )
1550 {
1551 MULADDC_INIT
1552 MULADDC_CORE
1553 MULADDC_STOP
1554 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001555#endif /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001556
1557 t++;
1558
1559 do {
1560 *d += c; c = ( *d < c ); d++;
1561 }
1562 while( c != 0 );
1563}
1564
1565/*
1566 * Baseline multiplication: X = A * B (HAC 14.12)
1567 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001569{
Paul Bakker23986e52011-04-24 08:57:21 +00001570 int ret;
1571 size_t i, j;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 mbedtls_mpi TA, TB;
Hanno Becker73d7d792018-12-11 10:35:51 +00001573 MPI_VALIDATE_RET( X != NULL );
1574 MPI_VALIDATE_RET( A != NULL );
1575 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 if( X == A ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); A = &TA; }
1580 if( X == B ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); B = &TB; }
Paul Bakker5121ce52009-01-03 21:22:43 +00001581
Paul Bakker23986e52011-04-24 08:57:21 +00001582 for( i = A->n; i > 0; i-- )
1583 if( A->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001584 break;
1585
Paul Bakker23986e52011-04-24 08:57:21 +00001586 for( j = B->n; j > 0; j-- )
1587 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001588 break;
1589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + j ) );
1591 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001592
Alexey Skalozub8e75e682016-01-13 21:59:27 +02001593 for( ; j > 0; j-- )
1594 mpi_mul_hlp( i, A->p, X->p + j - 1, B->p[j - 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00001595
1596 X->s = A->s * B->s;
1597
1598cleanup:
1599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001601
1602 return( ret );
1603}
1604
1605/*
1606 * Baseline multiplication: X = A * b
1607 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001609{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 mbedtls_mpi _B;
1611 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001612 MPI_VALIDATE_RET( X != NULL );
1613 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001614
1615 _B.s = 1;
1616 _B.n = 1;
1617 _B.p = p;
1618 p[0] = b;
1619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 return( mbedtls_mpi_mul_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001621}
1622
1623/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001624 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1625 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001626 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001627static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1,
1628 mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r )
Simon Butcher15b15d12015-11-26 19:35:03 +00001629{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001630#if defined(MBEDTLS_HAVE_UDBL)
1631 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001632#else
Simon Butcher9803d072016-01-03 00:24:34 +00001633 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
1634 const mbedtls_mpi_uint uint_halfword_mask = ( (mbedtls_mpi_uint) 1 << biH ) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001635 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1636 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001637 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001638#endif
1639
Simon Butcher15b15d12015-11-26 19:35:03 +00001640 /*
1641 * Check for overflow
1642 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001643 if( 0 == d || u1 >= d )
Simon Butcher15b15d12015-11-26 19:35:03 +00001644 {
Simon Butcherf5ba0452015-12-27 23:01:55 +00001645 if (r != NULL) *r = ~0;
Simon Butcher15b15d12015-11-26 19:35:03 +00001646
Simon Butcherf5ba0452015-12-27 23:01:55 +00001647 return ( ~0 );
Simon Butcher15b15d12015-11-26 19:35:03 +00001648 }
1649
1650#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001651 dividend = (mbedtls_t_udbl) u1 << biL;
1652 dividend |= (mbedtls_t_udbl) u0;
1653 quotient = dividend / d;
1654 if( quotient > ( (mbedtls_t_udbl) 1 << biL ) - 1 )
1655 quotient = ( (mbedtls_t_udbl) 1 << biL ) - 1;
1656
1657 if( r != NULL )
Simon Butcher9803d072016-01-03 00:24:34 +00001658 *r = (mbedtls_mpi_uint)( dividend - (quotient * d ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001659
1660 return (mbedtls_mpi_uint) quotient;
1661#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001662
1663 /*
1664 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1665 * Vol. 2 - Seminumerical Algorithms, Knuth
1666 */
1667
1668 /*
1669 * Normalize the divisor, d, and dividend, u0, u1
1670 */
1671 s = mbedtls_clz( d );
1672 d = d << s;
1673
1674 u1 = u1 << s;
Simon Butcher9803d072016-01-03 00:24:34 +00001675 u1 |= ( u0 >> ( biL - s ) ) & ( -(mbedtls_mpi_sint)s >> ( biL - 1 ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001676 u0 = u0 << s;
1677
1678 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001679 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001680
1681 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001682 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001683
1684 /*
1685 * Find the first quotient and remainder
1686 */
1687 q1 = u1 / d1;
1688 r0 = u1 - d1 * q1;
1689
1690 while( q1 >= radix || ( q1 * d0 > radix * r0 + u0_msw ) )
1691 {
1692 q1 -= 1;
1693 r0 += d1;
1694
1695 if ( r0 >= radix ) break;
1696 }
1697
Simon Butcherf5ba0452015-12-27 23:01:55 +00001698 rAX = ( u1 * radix ) + ( u0_msw - q1 * d );
Simon Butcher15b15d12015-11-26 19:35:03 +00001699 q0 = rAX / d1;
1700 r0 = rAX - q0 * d1;
1701
1702 while( q0 >= radix || ( q0 * d0 > radix * r0 + u0_lsw ) )
1703 {
1704 q0 -= 1;
1705 r0 += d1;
1706
1707 if ( r0 >= radix ) break;
1708 }
1709
1710 if (r != NULL)
Simon Butcherf5ba0452015-12-27 23:01:55 +00001711 *r = ( rAX * radix + u0_lsw - q0 * d ) >> s;
Simon Butcher15b15d12015-11-26 19:35:03 +00001712
1713 quotient = q1 * radix + q0;
1714
1715 return quotient;
1716#endif
1717}
1718
1719/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001721 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001722int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1723 const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001724{
Paul Bakker23986e52011-04-24 08:57:21 +00001725 int ret;
1726 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 mbedtls_mpi X, Y, Z, T1, T2;
Hanno Becker73d7d792018-12-11 10:35:51 +00001728 MPI_VALIDATE_RET( A != NULL );
1729 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 if( mbedtls_mpi_cmp_int( B, 0 ) == 0 )
1732 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
1735 mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 if( Q != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_lset( Q, 0 ) );
1740 if( R != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001741 return( 0 );
1742 }
1743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &X, A ) );
1745 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001746 X.s = Y.s = 1;
1747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &Z, A->n + 2 ) );
1749 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Z, 0 ) );
1750 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, 2 ) );
1751 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T2, 3 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001752
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001753 k = mbedtls_mpi_bitlen( &Y ) % biL;
Paul Bakkerf9688572011-05-05 10:00:45 +00001754 if( k < biL - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001755 {
1756 k = biL - 1 - k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &X, k ) );
1758 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, k ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001759 }
1760 else k = 0;
1761
1762 n = X.n - 1;
1763 t = Y.n - 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 while( mbedtls_mpi_cmp_mpi( &X, &Y ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001767 {
1768 Z.p[n - t]++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001770 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001772
1773 for( i = n; i > t ; i-- )
1774 {
1775 if( X.p[i] >= Y.p[t] )
1776 Z.p[i - t - 1] = ~0;
1777 else
1778 {
Simon Butcher15b15d12015-11-26 19:35:03 +00001779 Z.p[i - t - 1] = mbedtls_int_div_int( X.p[i], X.p[i - 1],
1780 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001781 }
1782
1783 Z.p[i - t - 1]++;
1784 do
1785 {
1786 Z.p[i - t - 1]--;
1787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T1, 0 ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001789 T1.p[0] = ( t < 1 ) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001790 T1.p[1] = Y.p[t];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T2, 0 ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001794 T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2];
1795 T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001796 T2.p[2] = X.p[i];
1797 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 while( mbedtls_mpi_cmp_mpi( &T1, &T2 ) > 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
1801 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1802 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804 if( mbedtls_mpi_cmp_int( &X, 0 ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &Y ) );
1807 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1808 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001809 Z.p[i - t - 1]--;
1810 }
1811 }
1812
1813 if( Q != NULL )
1814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( Q, &Z ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001816 Q->s = A->s * B->s;
1817 }
1818
1819 if( R != NULL )
1820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &X, k ) );
Paul Bakkerf02c5642012-11-13 10:25:21 +00001822 X.s = A->s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, &X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 if( mbedtls_mpi_cmp_int( R, 0 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001826 R->s = 1;
1827 }
1828
1829cleanup:
1830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
1832 mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001833
1834 return( ret );
1835}
1836
1837/*
1838 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00001839 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001840int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R,
1841 const mbedtls_mpi *A,
1842 mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001843{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 mbedtls_mpi _B;
1845 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001846 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001847
1848 p[0] = ( b < 0 ) ? -b : b;
1849 _B.s = ( b < 0 ) ? -1 : 1;
1850 _B.n = 1;
1851 _B.p = p;
1852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 return( mbedtls_mpi_div_mpi( Q, R, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001854}
1855
1856/*
1857 * Modulo: R = A mod B
1858 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001860{
1861 int ret;
Hanno Becker73d7d792018-12-11 10:35:51 +00001862 MPI_VALIDATE_RET( R != NULL );
1863 MPI_VALIDATE_RET( A != NULL );
1864 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 if( mbedtls_mpi_cmp_int( B, 0 ) < 0 )
1867 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( NULL, R, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 while( mbedtls_mpi_cmp_int( R, 0 ) < 0 )
1872 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 while( mbedtls_mpi_cmp_mpi( R, B ) >= 0 )
1875 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001876
1877cleanup:
1878
1879 return( ret );
1880}
1881
1882/*
1883 * Modulo: r = A mod b
1884 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001886{
Paul Bakker23986e52011-04-24 08:57:21 +00001887 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 mbedtls_mpi_uint x, y, z;
Hanno Becker73d7d792018-12-11 10:35:51 +00001889 MPI_VALIDATE_RET( r != NULL );
1890 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001891
1892 if( b == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001894
1895 if( b < 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001897
1898 /*
1899 * handle trivial cases
1900 */
1901 if( b == 1 )
1902 {
1903 *r = 0;
1904 return( 0 );
1905 }
1906
1907 if( b == 2 )
1908 {
1909 *r = A->p[0] & 1;
1910 return( 0 );
1911 }
1912
1913 /*
1914 * general case
1915 */
Paul Bakker23986e52011-04-24 08:57:21 +00001916 for( i = A->n, y = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001917 {
Paul Bakker23986e52011-04-24 08:57:21 +00001918 x = A->p[i - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001919 y = ( y << biH ) | ( x >> biH );
1920 z = y / b;
1921 y -= z * b;
1922
1923 x <<= biH;
1924 y = ( y << biH ) | ( x >> biH );
1925 z = y / b;
1926 y -= z * b;
1927 }
1928
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001929 /*
1930 * If A is negative, then the current y represents a negative value.
1931 * Flipping it to the positive side.
1932 */
1933 if( A->s < 0 && y != 0 )
1934 y = b - y;
1935
Paul Bakker5121ce52009-01-03 21:22:43 +00001936 *r = y;
1937
1938 return( 0 );
1939}
1940
1941/*
1942 * Fast Montgomery initialization (thanks to Tom St Denis)
1943 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944static void mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00001945{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 mbedtls_mpi_uint x, m0 = N->p[0];
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01001947 unsigned int i;
Paul Bakker5121ce52009-01-03 21:22:43 +00001948
1949 x = m0;
1950 x += ( ( m0 + 2 ) & 4 ) << 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001951
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01001952 for( i = biL; i >= 8; i /= 2 )
1953 x *= ( 2 - ( m0 * x ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001954
1955 *mm = ~x + 1;
1956}
1957
Gilles Peskine3ce3ddf2020-06-04 15:00:49 +02001958/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
1959 *
1960 * \param[in,out] A One of the numbers to multiply.
Gilles Peskine635a3742020-06-08 22:37:50 +02001961 * It must have at least as many limbs as N
1962 * (A->n >= N->n), and any limbs beyond n are ignored.
Gilles Peskine3ce3ddf2020-06-04 15:00:49 +02001963 * On successful completion, A contains the result of
1964 * the multiplication A * B * R^-1 mod N where
1965 * R = (2^ciL)^n.
1966 * \param[in] B One of the numbers to multiply.
1967 * It must be nonzero and must not have more limbs than N
1968 * (B->n <= N->n).
1969 * \param[in] N The modulo. N must be odd.
1970 * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
1971 * This is -N^-1 mod 2^ciL.
1972 * \param[in,out] T A bignum for temporary storage.
1973 * It must be at least twice the limb size of N plus 2
1974 * (T->n >= 2 * (N->n + 1)).
1975 * Its initial content is unused and
1976 * its final content is indeterminate.
1977 * Note that unlike the usual convention in the library
1978 * for `const mbedtls_mpi*`, the content of T can change.
Paul Bakker5121ce52009-01-03 21:22:43 +00001979 */
Gilles Peskinebdcb3962020-06-04 20:55:15 +02001980static 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 +02001981 const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00001982{
Paul Bakker23986e52011-04-24 08:57:21 +00001983 size_t i, n, m;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 mbedtls_mpi_uint u0, u1, *d;
Paul Bakker5121ce52009-01-03 21:22:43 +00001985
1986 memset( T->p, 0, T->n * ciL );
1987
1988 d = T->p;
1989 n = N->n;
1990 m = ( B->n < n ) ? B->n : n;
1991
1992 for( i = 0; i < n; i++ )
1993 {
1994 /*
1995 * T = (T + u0*B + u1*N) / 2^biL
1996 */
1997 u0 = A->p[i];
1998 u1 = ( d[0] + u0 * B->p[0] ) * mm;
1999
2000 mpi_mul_hlp( m, B->p, d, u0 );
2001 mpi_mul_hlp( n, N->p, d, u1 );
2002
2003 *d++ = u0; d[n + 1] = 0;
2004 }
2005
Gilles Peskine635a3742020-06-08 22:37:50 +02002006 /* At this point, d is either the desired result or the desired result
2007 * plus N. We now potentially subtract N, avoiding leaking whether the
2008 * subtraction is performed through side channels. */
Paul Bakker5121ce52009-01-03 21:22:43 +00002009
Gilles Peskine635a3742020-06-08 22:37:50 +02002010 /* Copy the n least significant limbs of d to A, so that
2011 * A = d if d < N (recall that N has n limbs). */
2012 memcpy( A->p, d, n * ciL );
Gilles Peskinede719d52020-06-09 10:39:38 +02002013 /* If d >= N then we want to set A to d - N. To prevent timing attacks,
Gilles Peskine635a3742020-06-08 22:37:50 +02002014 * do the calculation without using conditional tests. */
2015 /* Set d to d0 + (2^biL)^n - N where d0 is the current value of d. */
Gilles Peskine8f672662020-06-04 21:05:24 +02002016 d[n] += 1;
Gilles Peskine36acd542020-06-08 21:58:22 +02002017 d[n] -= mpi_sub_hlp( n, d, N->p );
Gilles Peskine635a3742020-06-08 22:37:50 +02002018 /* If d0 < N then d < (2^biL)^n
2019 * so d[n] == 0 and we want to keep A as it is.
2020 * If d0 >= N then d >= (2^biL)^n, and d <= (2^biL)^n + N < 2 * (2^biL)^n
2021 * so d[n] == 1 and we want to set A to the result of the subtraction
2022 * which is d - (2^biL)^n, i.e. the n least significant limbs of d.
2023 * This exactly corresponds to a conditional assignment. */
2024 mpi_safe_cond_assign( n, A->p, d, (unsigned char) d[n] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002025}
2026
2027/*
2028 * Montgomery reduction: A = A * R^-1 mod N
Gilles Peskine3ce3ddf2020-06-04 15:00:49 +02002029 *
2030 * See mpi_montmul() regarding constraints and guarantees on the parameters.
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 */
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002032static void mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N,
2033 mbedtls_mpi_uint mm, const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00002034{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035 mbedtls_mpi_uint z = 1;
2036 mbedtls_mpi U;
Paul Bakker5121ce52009-01-03 21:22:43 +00002037
Paul Bakker8ddb6452013-02-27 14:56:33 +01002038 U.n = U.s = (int) z;
Paul Bakker5121ce52009-01-03 21:22:43 +00002039 U.p = &z;
2040
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002041 mpi_montmul( A, &U, N, mm, T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002042}
2043
2044/*
2045 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
2046 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002047int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
2048 const mbedtls_mpi *E, const mbedtls_mpi *N,
2049 mbedtls_mpi *_RR )
Paul Bakker5121ce52009-01-03 21:22:43 +00002050{
Paul Bakker23986e52011-04-24 08:57:21 +00002051 int ret;
2052 size_t wbits, wsize, one = 1;
2053 size_t i, j, nblimbs;
2054 size_t bufsize, nbits;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055 mbedtls_mpi_uint ei, mm, state;
Daniel Otte359d0492020-08-21 12:34:29 +02002056 mbedtls_mpi RR, T, W[ 1 << MBEDTLS_MPI_WINDOW_SIZE ], Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00002057 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002058
Hanno Becker73d7d792018-12-11 10:35:51 +00002059 MPI_VALIDATE_RET( X != NULL );
2060 MPI_VALIDATE_RET( A != NULL );
2061 MPI_VALIDATE_RET( E != NULL );
2062 MPI_VALIDATE_RET( N != NULL );
2063
Hanno Becker8d1dd1b2017-09-28 11:02:24 +01002064 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 || ( N->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002067 if( mbedtls_mpi_cmp_int( E, 0 ) < 0 )
2068 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002069
Chris Jonesad59a2a2020-11-25 15:12:39 +00002070 if( mbedtls_mpi_bitlen( E ) > MBEDTLS_MPI_MAX_BITS ||
2071 mbedtls_mpi_bitlen( N ) > MBEDTLS_MPI_MAX_BITS )
2072 return ( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
2073
Paul Bakkerf6198c12012-05-16 08:02:29 +00002074 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002075 * Init temps and window size
2076 */
2077 mpi_montg_init( &mm, N );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &T );
2079 mbedtls_mpi_init( &Apos );
Paul Bakker5121ce52009-01-03 21:22:43 +00002080 memset( W, 0, sizeof( W ) );
2081
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002082 i = mbedtls_mpi_bitlen( E );
Paul Bakker5121ce52009-01-03 21:22:43 +00002083
2084 wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
2085 ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1;
2086
Peter Kolbusb83d41d2018-12-11 14:01:44 -06002087#if( MBEDTLS_MPI_WINDOW_SIZE < 6 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088 if( wsize > MBEDTLS_MPI_WINDOW_SIZE )
2089 wsize = MBEDTLS_MPI_WINDOW_SIZE;
Peter Kolbusb83d41d2018-12-11 14:01:44 -06002090#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00002091
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 j = N->n + 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
2094 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], j ) );
2095 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T, j * 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002096
2097 /*
Paul Bakker50546922012-05-19 08:40:49 +00002098 * Compensate for negative A (and correct at the end)
2099 */
2100 neg = ( A->s == -1 );
Paul Bakker50546922012-05-19 08:40:49 +00002101 if( neg )
2102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Apos, A ) );
Paul Bakker50546922012-05-19 08:40:49 +00002104 Apos.s = 1;
2105 A = &Apos;
2106 }
2107
2108 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00002109 * If 1st call, pre-compute R^2 mod N
2110 */
2111 if( _RR == NULL || _RR->p == NULL )
2112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &RR, 1 ) );
2114 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &RR, N->n * 2 * biL ) );
2115 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &RR, &RR, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002116
2117 if( _RR != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 memcpy( _RR, &RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002119 }
2120 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 memcpy( &RR, _RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002122
2123 /*
2124 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
2125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 if( mbedtls_mpi_cmp_mpi( A, N ) >= 0 )
2127 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &W[1], A, N ) );
Paul Bakkerc2024f42014-01-23 20:38:35 +01002128 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[1], A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002130
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002131 mpi_montmul( &W[1], &RR, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002132
2133 /*
2134 * X = R^2 * R^-1 mod N = R mod N
2135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &RR ) );
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002137 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002138
2139 if( wsize > 1 )
2140 {
2141 /*
2142 * W[1 << (wsize - 1)] = W[1] ^ (wsize - 1)
2143 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002144 j = one << ( wsize - 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[j], N->n + 1 ) );
2147 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[j], &W[1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002148
2149 for( i = 0; i < wsize - 1; i++ )
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002150 mpi_montmul( &W[j], &W[j], N, mm, &T );
Paul Bakker0d7702c2013-10-29 16:18:35 +01002151
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 /*
2153 * W[i] = W[i - 1] * W[1]
2154 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002155 for( i = j + 1; i < ( one << wsize ); i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[i], N->n + 1 ) );
2158 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[i], &W[i - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002159
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002160 mpi_montmul( &W[i], &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002161 }
2162 }
2163
2164 nblimbs = E->n;
2165 bufsize = 0;
2166 nbits = 0;
2167 wbits = 0;
2168 state = 0;
2169
2170 while( 1 )
2171 {
2172 if( bufsize == 0 )
2173 {
Paul Bakker0d7702c2013-10-29 16:18:35 +01002174 if( nblimbs == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002175 break;
2176
Paul Bakker0d7702c2013-10-29 16:18:35 +01002177 nblimbs--;
2178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 bufsize = sizeof( mbedtls_mpi_uint ) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00002180 }
2181
2182 bufsize--;
2183
2184 ei = (E->p[nblimbs] >> bufsize) & 1;
2185
2186 /*
2187 * skip leading 0s
2188 */
2189 if( ei == 0 && state == 0 )
2190 continue;
2191
2192 if( ei == 0 && state == 1 )
2193 {
2194 /*
2195 * out of window, square X
2196 */
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002197 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 continue;
2199 }
2200
2201 /*
2202 * add ei to current window
2203 */
2204 state = 2;
2205
2206 nbits++;
Paul Bakker66d5d072014-06-17 16:39:18 +02002207 wbits |= ( ei << ( wsize - nbits ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002208
2209 if( nbits == wsize )
2210 {
2211 /*
2212 * X = X^wsize R^-1 mod N
2213 */
2214 for( i = 0; i < wsize; i++ )
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002215 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002216
2217 /*
2218 * X = X * W[wbits] R^-1 mod N
2219 */
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002220 mpi_montmul( X, &W[wbits], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002221
2222 state--;
2223 nbits = 0;
2224 wbits = 0;
2225 }
2226 }
2227
2228 /*
2229 * process the remaining bits
2230 */
2231 for( i = 0; i < nbits; i++ )
2232 {
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002233 mpi_montmul( X, X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002234
2235 wbits <<= 1;
2236
Paul Bakker66d5d072014-06-17 16:39:18 +02002237 if( ( wbits & ( one << wsize ) ) != 0 )
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002238 mpi_montmul( X, &W[1], N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 }
2240
2241 /*
2242 * X = A^E * R * R^-1 mod N = A^E mod N
2243 */
Gilles Peskinebdcb3962020-06-04 20:55:15 +02002244 mpi_montred( X, N, mm, &T );
Paul Bakker5121ce52009-01-03 21:22:43 +00002245
Hanno Beckera4af1c42017-04-18 09:07:45 +01002246 if( neg && E->n != 0 && ( E->p[0] & 1 ) != 0 )
Paul Bakkerf6198c12012-05-16 08:02:29 +00002247 {
2248 X->s = -1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, N, X ) );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002250 }
2251
Paul Bakker5121ce52009-01-03 21:22:43 +00002252cleanup:
2253
Paul Bakker66d5d072014-06-17 16:39:18 +02002254 for( i = ( one << ( wsize - 1 ) ); i < ( one << wsize ); i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255 mbedtls_mpi_free( &W[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 mbedtls_mpi_free( &W[1] ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &Apos );
Paul Bakker6c591fa2011-05-05 11:49:20 +00002258
Paul Bakker75a28602014-03-31 12:08:17 +02002259 if( _RR == NULL || _RR->p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
2262 return( ret );
2263}
2264
Paul Bakker5121ce52009-01-03 21:22:43 +00002265/*
2266 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
2267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00002269{
Paul Bakker23986e52011-04-24 08:57:21 +00002270 int ret;
2271 size_t lz, lzt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 mbedtls_mpi TG, TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00002273
Hanno Becker73d7d792018-12-11 10:35:51 +00002274 MPI_VALIDATE_RET( G != NULL );
2275 MPI_VALIDATE_RET( A != NULL );
2276 MPI_VALIDATE_RET( B != NULL );
2277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 mbedtls_mpi_init( &TG ); mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) );
2281 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 lz = mbedtls_mpi_lsb( &TA );
2284 lzt = mbedtls_mpi_lsb( &TB );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002285
Paul Bakker66d5d072014-06-17 16:39:18 +02002286 if( lzt < lz )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002287 lz = lzt;
2288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, lz ) );
2290 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, lz ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002291
Paul Bakker5121ce52009-01-03 21:22:43 +00002292 TA.s = TB.s = 1;
2293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 while( mbedtls_mpi_cmp_int( &TA, 0 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, mbedtls_mpi_lsb( &TA ) ) );
2297 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, mbedtls_mpi_lsb( &TB ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 if( mbedtls_mpi_cmp_mpi( &TA, &TB ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TA, &TA, &TB ) );
2302 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002303 }
2304 else
2305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TB, &TB, &TA ) );
2307 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002308 }
2309 }
2310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &TB, lz ) );
2312 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( G, &TB ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002313
2314cleanup:
2315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 mbedtls_mpi_free( &TG ); mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002317
2318 return( ret );
2319}
2320
Paul Bakker33dc46b2014-04-30 16:11:39 +02002321/*
2322 * Fill X with size bytes of random.
2323 *
2324 * Use a temporary bytes representation to make sure the result is the same
Paul Bakkerc37b0ac2014-05-01 14:19:23 +02002325 * regardless of the platform endianness (useful when f_rng is actually
Paul Bakker33dc46b2014-04-30 16:11:39 +02002326 * deterministic, eg for tests).
2327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002329 int (*f_rng)(void *, unsigned char *, size_t),
2330 void *p_rng )
Paul Bakker287781a2011-03-26 13:18:49 +00002331{
Paul Bakker23986e52011-04-24 08:57:21 +00002332 int ret;
Hanno Becker6dab6202019-01-02 16:42:29 +00002333 size_t const limbs = CHARS_TO_LIMBS( size );
Hanno Beckerda1655a2017-10-18 14:21:44 +01002334 size_t const overhead = ( limbs * ciL ) - size;
2335 unsigned char *Xp;
2336
Hanno Becker8ce11a32018-12-19 16:18:52 +00002337 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002338 MPI_VALIDATE_RET( f_rng != NULL );
Paul Bakker33dc46b2014-04-30 16:11:39 +02002339
Hanno Beckerda1655a2017-10-18 14:21:44 +01002340 /* Ensure that target MPI has exactly the necessary number of limbs */
2341 if( X->n != limbs )
2342 {
2343 mbedtls_mpi_free( X );
2344 mbedtls_mpi_init( X );
2345 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, limbs ) );
2346 }
2347 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker287781a2011-03-26 13:18:49 +00002348
Hanno Beckerda1655a2017-10-18 14:21:44 +01002349 Xp = (unsigned char*) X->p;
Gilles Peskine05251142020-11-25 16:15:14 +01002350 MBEDTLS_MPI_CHK( f_rng( p_rng, Xp + overhead, size ) );
Hanno Beckerda1655a2017-10-18 14:21:44 +01002351
Hanno Becker2be8a552018-10-25 12:40:09 +01002352 mpi_bigendian_to_host( X->p, limbs );
Paul Bakker287781a2011-03-26 13:18:49 +00002353
2354cleanup:
2355 return( ret );
2356}
2357
Paul Bakker5121ce52009-01-03 21:22:43 +00002358/*
2359 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2360 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002362{
2363 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Hanno Becker73d7d792018-12-11 10:35:51 +00002365 MPI_VALIDATE_RET( X != NULL );
2366 MPI_VALIDATE_RET( A != NULL );
2367 MPI_VALIDATE_RET( N != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002368
Hanno Becker4bcb4912017-04-18 15:49:39 +01002369 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TU ); mbedtls_mpi_init( &U1 ); mbedtls_mpi_init( &U2 );
2373 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &TB ); mbedtls_mpi_init( &TV );
2374 mbedtls_mpi_init( &V1 ); mbedtls_mpi_init( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, A, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002381 goto cleanup;
2382 }
2383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &TA, A, N ) );
2385 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TU, &TA ) );
2386 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, N ) );
2387 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TV, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U1, 1 ) );
2390 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U2, 0 ) );
2391 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V1, 0 ) );
2392 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002393
2394 do
2395 {
2396 while( ( TU.p[0] & 1 ) == 0 )
2397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TU, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002399
2400 if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
2401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &U1, &U1, &TB ) );
2403 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 }
2405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U1, 1 ) );
2407 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 }
2409
2410 while( ( TV.p[0] & 1 ) == 0 )
2411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TV, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002413
2414 if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
2415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, &TB ) );
2417 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002418 }
2419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V1, 1 ) );
2421 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002422 }
2423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 if( mbedtls_mpi_cmp_mpi( &TU, &TV ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TU, &TU, &TV ) );
2427 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U1, &U1, &V1 ) );
2428 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &V2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 }
2430 else
2431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TV, &TV, &TU ) );
2433 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, &U1 ) );
2434 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &U2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002435 }
2436 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 while( mbedtls_mpi_cmp_int( &TU, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439 while( mbedtls_mpi_cmp_int( &V1, 0 ) < 0 )
2440 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 while( mbedtls_mpi_cmp_mpi( &V1, N ) >= 0 )
2443 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &V1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002446
2447cleanup:
2448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TU ); mbedtls_mpi_free( &U1 ); mbedtls_mpi_free( &U2 );
2450 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TV );
2451 mbedtls_mpi_free( &V1 ); mbedtls_mpi_free( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002452
2453 return( ret );
2454}
2455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002457
Paul Bakker5121ce52009-01-03 21:22:43 +00002458static const int small_prime[] =
2459{
2460 3, 5, 7, 11, 13, 17, 19, 23,
2461 29, 31, 37, 41, 43, 47, 53, 59,
2462 61, 67, 71, 73, 79, 83, 89, 97,
2463 101, 103, 107, 109, 113, 127, 131, 137,
2464 139, 149, 151, 157, 163, 167, 173, 179,
2465 181, 191, 193, 197, 199, 211, 223, 227,
2466 229, 233, 239, 241, 251, 257, 263, 269,
2467 271, 277, 281, 283, 293, 307, 311, 313,
2468 317, 331, 337, 347, 349, 353, 359, 367,
2469 373, 379, 383, 389, 397, 401, 409, 419,
2470 421, 431, 433, 439, 443, 449, 457, 461,
2471 463, 467, 479, 487, 491, 499, 503, 509,
2472 521, 523, 541, 547, 557, 563, 569, 571,
2473 577, 587, 593, 599, 601, 607, 613, 617,
2474 619, 631, 641, 643, 647, 653, 659, 661,
2475 673, 677, 683, 691, 701, 709, 719, 727,
2476 733, 739, 743, 751, 757, 761, 769, 773,
2477 787, 797, 809, 811, 821, 823, 827, 829,
2478 839, 853, 857, 859, 863, 877, 881, 883,
2479 887, 907, 911, 919, 929, 937, 941, 947,
2480 953, 967, 971, 977, 983, 991, 997, -103
2481};
2482
2483/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002484 * Small divisors test (X must be positive)
2485 *
2486 * Return values:
2487 * 0: no small factor (possible prime, more tests needed)
2488 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002490 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492static int mpi_check_small_factors( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +00002493{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002494 int ret = 0;
2495 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496 mbedtls_mpi_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +00002497
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 if( ( X->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002500
2501 for( i = 0; small_prime[i] > 0; i++ )
2502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 if( mbedtls_mpi_cmp_int( X, small_prime[i] ) <= 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002504 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, small_prime[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002507
2508 if( r == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 }
2511
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002512cleanup:
2513 return( ret );
2514}
2515
2516/*
2517 * Miller-Rabin pseudo-primality test (HAC 4.24)
2518 */
Janos Follathda31fa12018-09-03 14:45:23 +01002519static int mpi_miller_rabin( const mbedtls_mpi *X, size_t rounds,
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002520 int (*f_rng)(void *, unsigned char *, size_t),
2521 void *p_rng )
2522{
Pascal Junodb99183d2015-03-11 16:49:45 +01002523 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002524 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002526
Hanno Becker8ce11a32018-12-19 16:18:52 +00002527 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002528 MPI_VALIDATE_RET( f_rng != NULL );
2529
2530 mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R );
2531 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532 mbedtls_mpi_init( &RR );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002533
Paul Bakker5121ce52009-01-03 21:22:43 +00002534 /*
2535 * W = |X| - 1
2536 * R = W >> lsb( W )
2537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &W, X, 1 ) );
2539 s = mbedtls_mpi_lsb( &W );
2540 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R, &W ) );
2541 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &R, s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
Janos Follathda31fa12018-09-03 14:45:23 +01002543 for( i = 0; i < rounds; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002544 {
2545 /*
2546 * pick a random A, 1 < A < |X| - 1
2547 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002548 count = 0;
2549 do {
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002550 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
Pascal Junodb99183d2015-03-11 16:49:45 +01002551
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002552 j = mbedtls_mpi_bitlen( &A );
2553 k = mbedtls_mpi_bitlen( &W );
Pascal Junodb99183d2015-03-11 16:49:45 +01002554 if (j > k) {
Darryl Greene3f95ed2018-10-02 13:21:35 +01002555 A.p[A.n - 1] &= ( (mbedtls_mpi_uint) 1 << ( k - ( A.n - 1 ) * biL - 1 ) ) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002556 }
2557
2558 if (count++ > 30) {
Jens Wiklanderdfd447e2019-01-17 13:30:57 +01002559 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2560 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002561 }
2562
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002563 } while ( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 ||
2564 mbedtls_mpi_cmp_int( &A, 1 ) <= 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002565
2566 /*
2567 * A = A^R mod |X|
2568 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &A, &A, &R, X, &RR ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571 if( mbedtls_mpi_cmp_mpi( &A, &W ) == 0 ||
2572 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 continue;
2574
2575 j = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 while( j < s && mbedtls_mpi_cmp_mpi( &A, &W ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002577 {
2578 /*
2579 * A = A * A mod |X|
2580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &A, &A ) );
2582 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &A, &T, X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584 if( mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002585 break;
2586
2587 j++;
2588 }
2589
2590 /*
2591 * not prime if A != |X| - 1 or A == 1
2592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593 if( mbedtls_mpi_cmp_mpi( &A, &W ) != 0 ||
2594 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 break;
2598 }
2599 }
2600
2601cleanup:
Hanno Becker73d7d792018-12-11 10:35:51 +00002602 mbedtls_mpi_free( &W ); mbedtls_mpi_free( &R );
2603 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002605
2606 return( ret );
2607}
2608
2609/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002610 * Pseudo-primality test: small factors, then Miller-Rabin
2611 */
Janos Follatha0b67c22018-09-18 14:48:23 +01002612int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds,
2613 int (*f_rng)(void *, unsigned char *, size_t),
2614 void *p_rng )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002615{
2616 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 mbedtls_mpi XX;
Hanno Becker8ce11a32018-12-19 16:18:52 +00002618 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002619 MPI_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002620
2621 XX.s = 1;
2622 XX.n = X->n;
2623 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625 if( mbedtls_mpi_cmp_int( &XX, 0 ) == 0 ||
2626 mbedtls_mpi_cmp_int( &XX, 1 ) == 0 )
2627 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 if( mbedtls_mpi_cmp_int( &XX, 2 ) == 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002630 return( 0 );
2631
2632 if( ( ret = mpi_check_small_factors( &XX ) ) != 0 )
2633 {
2634 if( ret == 1 )
2635 return( 0 );
2636
2637 return( ret );
2638 }
2639
Janos Follathda31fa12018-09-03 14:45:23 +01002640 return( mpi_miller_rabin( &XX, rounds, f_rng, p_rng ) );
Janos Follathf301d232018-08-14 13:34:01 +01002641}
2642
Janos Follatha0b67c22018-09-18 14:48:23 +01002643#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Janos Follathf301d232018-08-14 13:34:01 +01002644/*
2645 * Pseudo-primality test, error probability 2^-80
2646 */
2647int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
2648 int (*f_rng)(void *, unsigned char *, size_t),
2649 void *p_rng )
2650{
Hanno Becker8ce11a32018-12-19 16:18:52 +00002651 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002652 MPI_VALIDATE_RET( f_rng != NULL );
2653
Janos Follatha0b67c22018-09-18 14:48:23 +01002654 /*
2655 * In the past our key generation aimed for an error rate of at most
2656 * 2^-80. Since this function is deprecated, aim for the same certainty
2657 * here as well.
2658 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002659 return( mbedtls_mpi_is_prime_ext( X, 40, f_rng, p_rng ) );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002660}
Janos Follatha0b67c22018-09-18 14:48:23 +01002661#endif
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002662
2663/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002665 *
Janos Follathf301d232018-08-14 13:34:01 +01002666 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2667 * be either 1024 bits or 1536 bits long, and flags must contain
2668 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002669 */
Janos Follath7c025a92018-08-14 11:08:41 +01002670int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002671 int (*f_rng)(void *, unsigned char *, size_t),
2672 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002673{
Jethro Beekman66689272018-02-14 19:24:10 -08002674#ifdef MBEDTLS_HAVE_INT64
2675// ceil(2^63.5)
2676#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2677#else
2678// ceil(2^31.5)
2679#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2680#endif
2681 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002682 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002683 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 mbedtls_mpi_uint r;
2685 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002686
Hanno Becker8ce11a32018-12-19 16:18:52 +00002687 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002688 MPI_VALIDATE_RET( f_rng != NULL );
2689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690 if( nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS )
2691 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693 mbedtls_mpi_init( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002694
2695 n = BITS_TO_LIMBS( nbits );
2696
Janos Follathda31fa12018-09-03 14:45:23 +01002697 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR ) == 0 )
2698 {
2699 /*
2700 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2701 */
2702 rounds = ( ( nbits >= 1300 ) ? 2 : ( nbits >= 850 ) ? 3 :
2703 ( nbits >= 650 ) ? 4 : ( nbits >= 350 ) ? 8 :
2704 ( nbits >= 250 ) ? 12 : ( nbits >= 150 ) ? 18 : 27 );
2705 }
2706 else
2707 {
2708 /*
2709 * 2^-100 error probability, number of rounds computed based on HAC,
2710 * fact 4.48
2711 */
2712 rounds = ( ( nbits >= 1450 ) ? 4 : ( nbits >= 1150 ) ? 5 :
2713 ( nbits >= 1000 ) ? 6 : ( nbits >= 850 ) ? 7 :
2714 ( nbits >= 750 ) ? 8 : ( nbits >= 500 ) ? 13 :
2715 ( nbits >= 250 ) ? 28 : ( nbits >= 150 ) ? 40 : 51 );
2716 }
2717
Jethro Beekman66689272018-02-14 19:24:10 -08002718 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002719 {
Jethro Beekman66689272018-02-14 19:24:10 -08002720 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
2721 /* make sure generated number is at least (nbits-1)+0.5 bits (FIPS 186-4 §B.3.3 steps 4.4, 5.5) */
2722 if( X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2 ) continue;
2723
2724 k = n * biL;
2725 if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits ) );
2726 X->p[0] |= 1;
2727
Janos Follath7c025a92018-08-14 11:08:41 +01002728 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002729 {
Janos Follatha0b67c22018-09-18 14:48:23 +01002730 ret = mbedtls_mpi_is_prime_ext( X, rounds, f_rng, p_rng );
Jethro Beekman66689272018-02-14 19:24:10 -08002731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002734 }
Jethro Beekman66689272018-02-14 19:24:10 -08002735 else
Paul Bakker5121ce52009-01-03 21:22:43 +00002736 {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002737 /*
Jethro Beekman66689272018-02-14 19:24:10 -08002738 * An necessary condition for Y and X = 2Y + 1 to be prime
2739 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2740 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002741 */
Jethro Beekman66689272018-02-14 19:24:10 -08002742
2743 X->p[0] |= 2;
2744
2745 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, 3 ) );
2746 if( r == 0 )
2747 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 8 ) );
2748 else if( r == 1 )
2749 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 4 ) );
2750
2751 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
2752 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, X ) );
2753 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, 1 ) );
2754
2755 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002756 {
Jethro Beekman66689272018-02-14 19:24:10 -08002757 /*
2758 * First, check small factors for X and Y
2759 * before doing Miller-Rabin on any of them
2760 */
2761 if( ( ret = mpi_check_small_factors( X ) ) == 0 &&
2762 ( ret = mpi_check_small_factors( &Y ) ) == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002763 ( ret = mpi_miller_rabin( X, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002764 == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002765 ( ret = mpi_miller_rabin( &Y, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002766 == 0 )
Jethro Beekman66689272018-02-14 19:24:10 -08002767 goto cleanup;
2768
2769 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
2770 goto cleanup;
2771
2772 /*
2773 * Next candidates. We want to preserve Y = (X-1) / 2 and
2774 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2775 * so up Y by 6 and X by 12.
2776 */
2777 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 12 ) );
2778 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &Y, &Y, 6 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002779 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002780 }
2781 }
2782
2783cleanup:
2784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 mbedtls_mpi_free( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002786
2787 return( ret );
2788}
2789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002793
Paul Bakker23986e52011-04-24 08:57:21 +00002794#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002795
2796static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2797{
2798 { 693, 609, 21 },
2799 { 1764, 868, 28 },
2800 { 768454923, 542167814, 1 }
2801};
2802
Paul Bakker5121ce52009-01-03 21:22:43 +00002803/*
2804 * Checkup routine
2805 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806int mbedtls_mpi_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002807{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002808 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00002810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); mbedtls_mpi_init( &X );
2812 mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &U ); mbedtls_mpi_init( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00002813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &A, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002815 "EFE021C2645FD1DC586E69184AF4A31E" \
2816 "D5F53E93B5F123FA41680867BA110131" \
2817 "944FE7952E2517337780CB0DB80E61AA" \
2818 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6" ) );
2819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002821 "B2E7EFD37075B9F03FF989C7C5051C20" \
2822 "34D2A323810251127E7BF8625A4F49A5" \
2823 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
2824 "5B5C25763222FEFCCFC38B832366C29E" ) );
2825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &N, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002827 "0066A198186C18C10B2F5ED9B522752A" \
2828 "9830B69916E535C8F047518A889A43A5" \
2829 "94B6BED27A168D31D4A52F88925AA8F5" ) );
2830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002834 "602AB7ECA597A3D6B56FF9829A5E8B85" \
2835 "9E857EA95A03512E2BAE7391688D264A" \
2836 "A5663B0341DB9CCFD2C4C5F421FEC814" \
2837 "8001B72E848A38CAE1C65F78E56ABDEF" \
2838 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
2839 "ECF677152EF804370C1A305CAF3B5BF1" \
2840 "30879B56C61DE584A0F53A2447A51E" ) );
2841
2842 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843 mbedtls_printf( " MPI test #1 (mul_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002846 {
2847 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002849
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002850 ret = 1;
2851 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002852 }
2853
2854 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &X, &Y, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002859 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002860 "256567336059E52CAE22925474705F39A94" ) );
2861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &V, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002863 "6613F26162223DF488E9CD48CC132C7A" \
2864 "0AC93C701B001B092E4E5B9F73BCD27B" \
2865 "9EE50D0657C77F374E903CDFA4C642" ) );
2866
2867 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868 mbedtls_printf( " MPI test #2 (div_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ||
2871 mbedtls_mpi_cmp_mpi( &Y, &V ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002872 {
2873 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002875
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002876 ret = 1;
2877 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002878 }
2879
2880 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &X, &A, &E, &N, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002886 "36E139AEA55215609D2816998ED020BB" \
2887 "BD96C37890F65171D948E9BC7CBAA4D9" \
2888 "325D24D6A3C12710F10A09FA08AB87" ) );
2889
2890 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891 mbedtls_printf( " MPI test #3 (exp_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002893 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002894 {
2895 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002897
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002898 ret = 1;
2899 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002900 }
2901
2902 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002908 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
2909 "C3DBA76456363A10869622EAC2DD84EC" \
2910 "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) );
2911
2912 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913 mbedtls_printf( " MPI test #4 (inv_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002916 {
2917 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002919
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002920 ret = 1;
2921 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002922 }
2923
2924 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002926
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002927 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 mbedtls_printf( " MPI test #5 (simple gcd): " );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002929
Paul Bakker66d5d072014-06-17 16:39:18 +02002930 for( i = 0; i < GCD_PAIR_COUNT; i++ )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &X, gcd_pairs[i][0] ) );
2933 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Y, gcd_pairs[i][1] ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &A, &X, &Y ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937 if( mbedtls_mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 )
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002938 {
2939 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940 mbedtls_printf( "failed at %d\n", i );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002941
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002942 ret = 1;
2943 goto cleanup;
2944 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002945 }
2946
2947 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002948 mbedtls_printf( "passed\n" );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002949
Paul Bakker5121ce52009-01-03 21:22:43 +00002950cleanup:
2951
2952 if( ret != 0 && verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953 mbedtls_printf( "Unexpected error, return code = %08X\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &X );
2956 mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &U ); mbedtls_mpi_free( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00002957
2958 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002960
2961 return( ret );
2962}
2963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966#endif /* MBEDTLS_BIGNUM_C */