blob: 1d258db0e954f50a09e5fce0aa9681144afb8bfa [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Multi-precision integer library
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
Simon Butcher15b15d12015-11-26 19:35:03 +000021
Paul Bakker5121ce52009-01-03 21:22:43 +000022/*
Simon Butcher15b15d12015-11-26 19:35:03 +000023 * The following sources were referenced in the design of this Multi-precision
24 * Integer library:
Paul Bakker5121ce52009-01-03 21:22:43 +000025 *
Simon Butcher15b15d12015-11-26 19:35:03 +000026 * [1] Handbook of Applied Cryptography - 1997
27 * Menezes, van Oorschot and Vanstone
28 *
29 * [2] Multi-Precision Math
30 * Tom St Denis
31 * https://github.com/libtom/libtommath/blob/develop/tommath.pdf
32 *
33 * [3] GNU Multi-Precision Arithmetic Library
34 * https://gmplib.org/manual/index.html
35 *
Simon Butcherf5ba0452015-12-27 23:01:55 +000036 */
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020040#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_BIGNUM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000045
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/bignum.h"
47#include "mbedtls/bn_mul.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050048#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000049#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020055#else
Rich Evans00ab4702015-02-06 13:43:58 +000056#include <stdio.h>
57#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define mbedtls_printf printf
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020059#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020061#endif
62
Hanno Becker73d7d792018-12-11 10:35:51 +000063#define MPI_VALIDATE_RET( cond ) \
64 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA )
65#define MPI_VALIDATE( cond ) \
66 MBEDTLS_INTERNAL_VALIDATE( cond )
67
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
Paul Bakker5121ce52009-01-03 21:22:43 +000069#define biL (ciL << 3) /* bits in limb */
70#define biH (ciL << 2) /* half limb size */
71
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +010072#define MPI_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
73
Paul Bakker5121ce52009-01-03 21:22:43 +000074/*
75 * Convert between bits/chars and number of limbs
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +020076 * Divide first in order to avoid potential overflows
Paul Bakker5121ce52009-01-03 21:22:43 +000077 */
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +020078#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
79#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +000080
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050081/* Implementation that should never be optimized out by the compiler */
Andres Amaya Garcia6698d2f2018-04-24 08:39:07 -050082static void mbedtls_mpi_zeroize( mbedtls_mpi_uint *v, size_t n )
83{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050084 mbedtls_platform_zeroize( v, ciL * n );
85}
86
Paul Bakker5121ce52009-01-03 21:22:43 +000087/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000088 * Initialize one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090void mbedtls_mpi_init( mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +000091{
Hanno Becker73d7d792018-12-11 10:35:51 +000092 MPI_VALIDATE( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +000093
Paul Bakker6c591fa2011-05-05 11:49:20 +000094 X->s = 1;
95 X->n = 0;
96 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000097}
98
99/*
Paul Bakker6c591fa2011-05-05 11:49:20 +0000100 * Unallocate one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102void mbedtls_mpi_free( mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000103{
Paul Bakker6c591fa2011-05-05 11:49:20 +0000104 if( X == NULL )
105 return;
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
Paul Bakker6c591fa2011-05-05 11:49:20 +0000107 if( X->p != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 {
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200109 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 }
112
Paul Bakker6c591fa2011-05-05 11:49:20 +0000113 X->s = 1;
114 X->n = 0;
115 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +0000116}
117
118/*
119 * Enlarge to the specified number of limbs
120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000122{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 mbedtls_mpi_uint *p;
Hanno Becker73d7d792018-12-11 10:35:51 +0000124 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200127 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Paul Bakkerf9688572011-05-05 10:00:45 +0000128
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 if( X->n < nblimbs )
130 {
Simon Butcher29176892016-05-20 00:19:09 +0100131 if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( nblimbs, ciL ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200132 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 if( X->p != NULL )
135 {
136 memcpy( p, X->p, X->n * ciL );
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200137 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 }
140
141 X->n = nblimbs;
142 X->p = p;
143 }
144
145 return( 0 );
146}
147
148/*
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100149 * Resize down as much as possible,
150 * while keeping at least the specified number of limbs
151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100153{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_mpi_uint *p;
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100155 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000156 MPI_VALIDATE_RET( X != NULL );
157
158 if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
159 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100160
161 /* Actually resize up in this case */
162 if( X->n <= nblimbs )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 return( mbedtls_mpi_grow( X, nblimbs ) );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100164
165 for( i = X->n - 1; i > 0; i-- )
166 if( X->p[i] != 0 )
167 break;
168 i++;
169
170 if( i < nblimbs )
171 i = nblimbs;
172
Simon Butcher29176892016-05-20 00:19:09 +0100173 if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( i, ciL ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200174 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100175
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100176 if( X->p != NULL )
177 {
178 memcpy( p, X->p, i * ciL );
Alexey Skalozube17a8da2016-01-13 17:19:33 +0200179 mbedtls_mpi_zeroize( X->p, X->n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_free( X->p );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100181 }
182
183 X->n = i;
184 X->p = p;
185
186 return( 0 );
187}
188
189/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 * Copy the contents of Y into X
191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000193{
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100194 int ret = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000195 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000196 MPI_VALIDATE_RET( X != NULL );
197 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
199 if( X == Y )
200 return( 0 );
201
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200202 if( Y->p == NULL )
203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_mpi_free( X );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200205 return( 0 );
206 }
207
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 for( i = Y->n - 1; i > 0; i-- )
209 if( Y->p[i] != 0 )
210 break;
211 i++;
212
213 X->s = Y->s;
214
Gilles Peskine4e4be7c2018-03-21 16:29:03 +0100215 if( X->n < i )
216 {
217 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i ) );
218 }
219 else
220 {
221 memset( X->p + i, 0, ( X->n - i ) * ciL );
222 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 memcpy( X->p, Y->p, i * ciL );
225
226cleanup:
227
228 return( ret );
229}
230
231/*
232 * Swap the contents of X and Y
233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000237 MPI_VALIDATE( X != NULL );
238 MPI_VALIDATE( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 memcpy( &T, X, sizeof( mbedtls_mpi ) );
241 memcpy( X, Y, sizeof( mbedtls_mpi ) );
242 memcpy( Y, &T, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000243}
244
245/*
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100246 * Conditionally assign X = Y, without leaking information
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +0100247 * about whether the assignment was made or not.
248 * (Leaking information about the respective sizes of X and Y is ok however.)
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100251{
252 int ret = 0;
253 size_t i;
Hanno Becker73d7d792018-12-11 10:35:51 +0000254 MPI_VALIDATE_RET( X != NULL );
255 MPI_VALIDATE_RET( Y != NULL );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100256
Pascal Junodb99183d2015-03-11 16:49:45 +0100257 /* make sure assign is 0 or 1 in a time-constant manner */
258 assign = (assign | (unsigned char)-assign) >> 7;
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100261
Paul Bakker66d5d072014-06-17 16:39:18 +0200262 X->s = X->s * ( 1 - assign ) + Y->s * assign;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100263
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +0100264 for( i = 0; i < Y->n; i++ )
Paul Bakker66d5d072014-06-17 16:39:18 +0200265 X->p[i] = X->p[i] * ( 1 - assign ) + Y->p[i] * assign;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100266
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +0100267 for( ; i < X->n; i++ )
Paul Bakker66d5d072014-06-17 16:39:18 +0200268 X->p[i] *= ( 1 - assign );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100269
270cleanup:
271 return( ret );
272}
273
274/*
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100275 * Conditionally swap X and Y, without leaking information
276 * about whether the swap was made or not.
277 * Here it is not ok to simply swap the pointers, which whould lead to
278 * different memory access patterns when X and Y are used afterwards.
279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100281{
282 int ret, s;
283 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_mpi_uint tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +0000285 MPI_VALIDATE_RET( X != NULL );
286 MPI_VALIDATE_RET( Y != NULL );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100287
288 if( X == Y )
289 return( 0 );
290
Pascal Junodb99183d2015-03-11 16:49:45 +0100291 /* make sure swap is 0 or 1 in a time-constant manner */
292 swap = (swap | (unsigned char)-swap) >> 7;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
295 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( Y, X->n ) );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100296
297 s = X->s;
Paul Bakker66d5d072014-06-17 16:39:18 +0200298 X->s = X->s * ( 1 - swap ) + Y->s * swap;
299 Y->s = Y->s * ( 1 - swap ) + s * swap;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100300
301
302 for( i = 0; i < X->n; i++ )
303 {
304 tmp = X->p[i];
Paul Bakker66d5d072014-06-17 16:39:18 +0200305 X->p[i] = X->p[i] * ( 1 - swap ) + Y->p[i] * swap;
306 Y->p[i] = Y->p[i] * ( 1 - swap ) + tmp * swap;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100307 }
308
309cleanup:
310 return( ret );
311}
312
313/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 * Set value from integer
315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +0000317{
Janos Follath24eed8d2019-11-22 13:21:35 +0000318 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker73d7d792018-12-11 10:35:51 +0000319 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000322 memset( X->p, 0, X->n * ciL );
323
324 X->p[0] = ( z < 0 ) ? -z : z;
325 X->s = ( z < 0 ) ? -1 : 1;
326
327cleanup:
328
329 return( ret );
330}
331
332/*
Paul Bakker2f5947e2011-05-18 15:47:11 +0000333 * Get a specific bit
334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000336{
Hanno Becker73d7d792018-12-11 10:35:51 +0000337 MPI_VALIDATE_RET( X != NULL );
338
Paul Bakker2f5947e2011-05-18 15:47:11 +0000339 if( X->n * biL <= pos )
340 return( 0 );
341
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200342 return( ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000343}
344
Gilles Peskine11cdb052018-11-20 16:47:47 +0100345/* Get a specific byte, without range checks. */
346#define GET_BYTE( X, i ) \
347 ( ( ( X )->p[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff )
348
Paul Bakker2f5947e2011-05-18 15:47:11 +0000349/*
350 * Set a bit to a specific value of 0 or 1
351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000353{
354 int ret = 0;
355 size_t off = pos / biL;
356 size_t idx = pos % biL;
Hanno Becker73d7d792018-12-11 10:35:51 +0000357 MPI_VALIDATE_RET( X != NULL );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000358
359 if( val != 0 && val != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker9af723c2014-05-01 13:03:14 +0200361
Paul Bakker2f5947e2011-05-18 15:47:11 +0000362 if( X->n * biL <= pos )
363 {
364 if( val == 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200365 return( 0 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, off + 1 ) );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000368 }
369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 X->p[off] &= ~( (mbedtls_mpi_uint) 0x01 << idx );
371 X->p[off] |= (mbedtls_mpi_uint) val << idx;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000372
373cleanup:
Paul Bakker9af723c2014-05-01 13:03:14 +0200374
Paul Bakker2f5947e2011-05-18 15:47:11 +0000375 return( ret );
376}
377
378/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200379 * Return the number of less significant zero-bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381size_t mbedtls_mpi_lsb( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000382{
Paul Bakker23986e52011-04-24 08:57:21 +0000383 size_t i, j, count = 0;
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000384 MBEDTLS_INTERNAL_VALIDATE_RET( X != NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000385
386 for( i = 0; i < X->n; i++ )
Paul Bakkerf9688572011-05-05 10:00:45 +0000387 for( j = 0; j < biL; j++, count++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 if( ( ( X->p[i] >> j ) & 1 ) != 0 )
389 return( count );
390
391 return( 0 );
392}
393
394/*
Simon Butcher15b15d12015-11-26 19:35:03 +0000395 * Count leading zero bits in a given integer
396 */
397static size_t mbedtls_clz( const mbedtls_mpi_uint x )
398{
399 size_t j;
Manuel Pégourié-Gonnarde3e8edf2015-12-01 09:31:52 +0100400 mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
Simon Butcher15b15d12015-11-26 19:35:03 +0000401
402 for( j = 0; j < biL; j++ )
403 {
404 if( x & mask ) break;
405
406 mask >>= 1;
407 }
408
409 return j;
410}
411
412/*
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200413 * Return the number of bits
Paul Bakker5121ce52009-01-03 21:22:43 +0000414 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200415size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000416{
Paul Bakker23986e52011-04-24 08:57:21 +0000417 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +0000418
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200419 if( X->n == 0 )
420 return( 0 );
421
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 for( i = X->n - 1; i > 0; i-- )
423 if( X->p[i] != 0 )
424 break;
425
Simon Butcher15b15d12015-11-26 19:35:03 +0000426 j = biL - mbedtls_clz( X->p[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000427
Paul Bakker23986e52011-04-24 08:57:21 +0000428 return( ( i * biL ) + j );
Paul Bakker5121ce52009-01-03 21:22:43 +0000429}
430
431/*
432 * Return the total size in bytes
433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434size_t mbedtls_mpi_size( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000435{
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200436 return( ( mbedtls_mpi_bitlen( X ) + 7 ) >> 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000437}
438
439/*
440 * Convert an ASCII character to digit value
441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442static int mpi_get_digit( mbedtls_mpi_uint *d, int radix, char c )
Paul Bakker5121ce52009-01-03 21:22:43 +0000443{
444 *d = 255;
445
446 if( c >= 0x30 && c <= 0x39 ) *d = c - 0x30;
447 if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37;
448 if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57;
449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 if( *d >= (mbedtls_mpi_uint) radix )
451 return( MBEDTLS_ERR_MPI_INVALID_CHARACTER );
Paul Bakker5121ce52009-01-03 21:22:43 +0000452
453 return( 0 );
454}
455
456/*
457 * Import from an ASCII string
458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000460{
Janos Follath24eed8d2019-11-22 13:21:35 +0000461 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000462 size_t i, j, slen, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_mpi_uint d;
464 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000465 MPI_VALIDATE_RET( X != NULL );
466 MPI_VALIDATE_RET( s != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
468 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000469 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
Paul Bakkerff60ee62010-03-16 21:09:09 +0000473 slen = strlen( s );
474
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 if( radix == 16 )
476 {
Manuel Pégourié-Gonnard2d708342015-10-05 15:23:11 +0100477 if( slen > MPI_SIZE_T_MAX >> 2 )
Manuel Pégourié-Gonnard58fb4952015-09-28 13:48:04 +0200478 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
479
Paul Bakkerff60ee62010-03-16 21:09:09 +0000480 n = BITS_TO_LIMBS( slen << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n ) );
483 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
Paul Bakker23986e52011-04-24 08:57:21 +0000485 for( i = slen, j = 0; i > 0; i--, j++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000486 {
Paul Bakker23986e52011-04-24 08:57:21 +0000487 if( i == 1 && s[i - 1] == '-' )
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 {
489 X->s = -1;
490 break;
491 }
492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200494 X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 }
496 }
497 else
498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000500
Paul Bakkerff60ee62010-03-16 21:09:09 +0000501 for( i = 0; i < slen; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000502 {
503 if( i == 0 && s[i] == '-' )
504 {
505 X->s = -1;
506 continue;
507 }
508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
510 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T, X, radix ) );
Paul Bakker05feca62009-06-20 08:22:43 +0000511
512 if( X->s == 1 )
513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, &T, d ) );
Paul Bakker05feca62009-06-20 08:22:43 +0000515 }
516 else
517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( X, &T, d ) );
Paul Bakker05feca62009-06-20 08:22:43 +0000519 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000520 }
521 }
522
523cleanup:
524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527 return( ret );
528}
529
530/*
Ron Eldora16fa292018-11-20 14:07:01 +0200531 * Helper to write the digits high-order first.
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 */
Ron Eldora16fa292018-11-20 14:07:01 +0200533static int mpi_write_hlp( mbedtls_mpi *X, int radix,
534 char **p, const size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000535{
Janos Follath24eed8d2019-11-22 13:21:35 +0000536 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_mpi_uint r;
Ron Eldora16fa292018-11-20 14:07:01 +0200538 size_t length = 0;
539 char *p_end = *p + buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Ron Eldora16fa292018-11-20 14:07:01 +0200541 do
542 {
543 if( length >= buflen )
544 {
545 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
546 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
Ron Eldora16fa292018-11-20 14:07:01 +0200548 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) );
549 MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) );
550 /*
551 * Write the residue in the current position, as an ASCII character.
552 */
553 if( r < 0xA )
554 *(--p_end) = (char)( '0' + r );
555 else
556 *(--p_end) = (char)( 'A' + ( r - 0xA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
Ron Eldora16fa292018-11-20 14:07:01 +0200558 length++;
559 } while( mbedtls_mpi_cmp_int( X, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000560
Ron Eldora16fa292018-11-20 14:07:01 +0200561 memmove( *p, p_end, length );
562 *p += length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
564cleanup:
565
566 return( ret );
567}
568
569/*
570 * Export into an ASCII string
571 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100572int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
573 char *buf, size_t buflen, size_t *olen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000574{
Paul Bakker23986e52011-04-24 08:57:21 +0000575 int ret = 0;
576 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_mpi T;
Hanno Becker73d7d792018-12-11 10:35:51 +0000579 MPI_VALIDATE_RET( X != NULL );
580 MPI_VALIDATE_RET( olen != NULL );
581 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
583 if( radix < 2 || radix > 16 )
Hanno Becker54c91dd2018-12-12 13:37:06 +0000584 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
Hanno Becker23cfea02019-02-04 09:45:07 +0000586 n = mbedtls_mpi_bitlen( X ); /* Number of bits necessary to present `n`. */
587 if( radix >= 4 ) n >>= 1; /* Number of 4-adic digits necessary to present
588 * `n`. If radix > 4, this might be a strict
589 * overapproximation of the number of
590 * radix-adic digits needed to present `n`. */
591 if( radix >= 16 ) n >>= 1; /* Number of hexadecimal digits necessary to
592 * present `n`. */
593
Janos Follath80470622019-03-06 13:43:02 +0000594 n += 1; /* Terminating null byte */
Hanno Becker23cfea02019-02-04 09:45:07 +0000595 n += 1; /* Compensate for the divisions above, which round down `n`
596 * in case it's not even. */
597 n += 1; /* Potential '-'-sign. */
598 n += ( n & 1 ); /* Make n even to have enough space for hexadecimal writing,
599 * which always uses an even number of hex-digits. */
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100601 if( buflen < n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000602 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100603 *olen = n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 }
606
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100607 p = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000609
610 if( X->s == -1 )
Hanno Beckerc983c812019-02-01 16:41:30 +0000611 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000612 *p++ = '-';
Hanno Beckerc983c812019-02-01 16:41:30 +0000613 buflen--;
614 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000615
616 if( radix == 16 )
617 {
Paul Bakker23986e52011-04-24 08:57:21 +0000618 int c;
619 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
Paul Bakker23986e52011-04-24 08:57:21 +0000621 for( i = X->n, k = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000622 {
Paul Bakker23986e52011-04-24 08:57:21 +0000623 for( j = ciL; j > 0; j-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000624 {
Paul Bakker23986e52011-04-24 08:57:21 +0000625 c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000626
Paul Bakker6c343d72014-07-10 14:36:19 +0200627 if( c == 0 && k == 0 && ( i + j ) != 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000628 continue;
629
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000630 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000631 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000632 k = 1;
633 }
634 }
635 }
636 else
637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T, X ) );
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000639
640 if( T.s == -1 )
641 T.s = 1;
642
Ron Eldora16fa292018-11-20 14:07:01 +0200643 MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p, buflen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 }
645
646 *p++ = '\0';
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100647 *olen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000648
649cleanup:
650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
653 return( ret );
654}
655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000657/*
658 * Read X from an opened file
659 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin )
Paul Bakker5121ce52009-01-03 21:22:43 +0000661{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_mpi_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000663 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000664 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000665 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000666 * Buffer should have space for (short) label and decimal formatted MPI,
667 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000668 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Paul Bakker5121ce52009-01-03 21:22:43 +0000670
Hanno Becker73d7d792018-12-11 10:35:51 +0000671 MPI_VALIDATE_RET( X != NULL );
672 MPI_VALIDATE_RET( fin != NULL );
673
674 if( radix < 2 || radix > 16 )
675 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
676
Paul Bakker5121ce52009-01-03 21:22:43 +0000677 memset( s, 0, sizeof( s ) );
678 if( fgets( s, sizeof( s ) - 1, fin ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000680
681 slen = strlen( s );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000682 if( slen == sizeof( s ) - 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000684
Hanno Beckerb2034b72017-04-26 11:46:46 +0100685 if( slen > 0 && s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
686 if( slen > 0 && s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
Paul Bakker5121ce52009-01-03 21:22:43 +0000687
688 p = s + slen;
Hanno Beckerb2034b72017-04-26 11:46:46 +0100689 while( p-- > s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000690 if( mpi_get_digit( &d, radix, *p ) != 0 )
691 break;
692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 return( mbedtls_mpi_read_string( X, radix, p + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000694}
695
696/*
697 * Write X into an opened file (or stdout if fout == NULL)
698 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout )
Paul Bakker5121ce52009-01-03 21:22:43 +0000700{
Janos Follath24eed8d2019-11-22 13:21:35 +0000701 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000702 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000703 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000704 * Buffer should have space for (short) label and decimal formatted MPI,
705 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
Hanno Becker73d7d792018-12-11 10:35:51 +0000708 MPI_VALIDATE_RET( X != NULL );
709
710 if( radix < 2 || radix > 16 )
711 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000712
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100713 memset( s, 0, sizeof( s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000714
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100715 MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, sizeof( s ) - 2, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000716
717 if( p == NULL ) p = "";
718
719 plen = strlen( p );
720 slen = strlen( s );
721 s[slen++] = '\r';
722 s[slen++] = '\n';
723
724 if( fout != NULL )
725 {
726 if( fwrite( p, 1, plen, fout ) != plen ||
727 fwrite( s, 1, slen, fout ) != slen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000729 }
730 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_printf( "%s%s", p, s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000732
733cleanup:
734
735 return( ret );
736}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000738
Hanno Beckerda1655a2017-10-18 14:21:44 +0100739
740/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
741 * into the storage form used by mbedtls_mpi. */
Hanno Beckerf8720072018-11-08 11:53:49 +0000742
743static mbedtls_mpi_uint mpi_uint_bigendian_to_host_c( mbedtls_mpi_uint x )
744{
745 uint8_t i;
Hanno Becker031d6332019-05-01 17:09:11 +0100746 unsigned char *x_ptr;
Hanno Beckerf8720072018-11-08 11:53:49 +0000747 mbedtls_mpi_uint tmp = 0;
Hanno Becker031d6332019-05-01 17:09:11 +0100748
749 for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
750 {
751 tmp <<= CHAR_BIT;
752 tmp |= (mbedtls_mpi_uint) *x_ptr;
753 }
754
Hanno Beckerf8720072018-11-08 11:53:49 +0000755 return( tmp );
756}
757
758static mbedtls_mpi_uint mpi_uint_bigendian_to_host( mbedtls_mpi_uint x )
759{
760#if defined(__BYTE_ORDER__)
761
762/* Nothing to do on bigendian systems. */
763#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
764 return( x );
765#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
766
767#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
768
769/* For GCC and Clang, have builtins for byte swapping. */
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000770#if defined(__GNUC__) && defined(__GNUC_PREREQ)
771#if __GNUC_PREREQ(4,3)
Hanno Beckerf8720072018-11-08 11:53:49 +0000772#define have_bswap
773#endif
Hanno Becker9f6d16a2019-01-02 17:15:06 +0000774#endif
775
776#if defined(__clang__) && defined(__has_builtin)
777#if __has_builtin(__builtin_bswap32) && \
778 __has_builtin(__builtin_bswap64)
779#define have_bswap
780#endif
781#endif
782
Hanno Beckerf8720072018-11-08 11:53:49 +0000783#if defined(have_bswap)
784 /* The compiler is hopefully able to statically evaluate this! */
785 switch( sizeof(mbedtls_mpi_uint) )
786 {
787 case 4:
788 return( __builtin_bswap32(x) );
789 case 8:
790 return( __builtin_bswap64(x) );
791 }
792#endif
793#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
794#endif /* __BYTE_ORDER__ */
795
796 /* Fall back to C-based reordering if we don't know the byte order
797 * or we couldn't use a compiler-specific builtin. */
798 return( mpi_uint_bigendian_to_host_c( x ) );
799}
800
Hanno Becker2be8a552018-10-25 12:40:09 +0100801static void mpi_bigendian_to_host( mbedtls_mpi_uint * const p, size_t limbs )
Hanno Beckerda1655a2017-10-18 14:21:44 +0100802{
Hanno Beckerda1655a2017-10-18 14:21:44 +0100803 mbedtls_mpi_uint *cur_limb_left;
804 mbedtls_mpi_uint *cur_limb_right;
Hanno Becker2be8a552018-10-25 12:40:09 +0100805 if( limbs == 0 )
806 return;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100807
808 /*
809 * Traverse limbs and
810 * - adapt byte-order in each limb
811 * - swap the limbs themselves.
812 * For that, simultaneously traverse the limbs from left to right
813 * and from right to left, as long as the left index is not bigger
814 * than the right index (it's not a problem if limbs is odd and the
815 * indices coincide in the last iteration).
816 */
Hanno Beckerda1655a2017-10-18 14:21:44 +0100817 for( cur_limb_left = p, cur_limb_right = p + ( limbs - 1 );
818 cur_limb_left <= cur_limb_right;
819 cur_limb_left++, cur_limb_right-- )
820 {
Hanno Beckerf8720072018-11-08 11:53:49 +0000821 mbedtls_mpi_uint tmp;
822 /* Note that if cur_limb_left == cur_limb_right,
823 * this code effectively swaps the bytes only once. */
824 tmp = mpi_uint_bigendian_to_host( *cur_limb_left );
825 *cur_limb_left = mpi_uint_bigendian_to_host( *cur_limb_right );
826 *cur_limb_right = tmp;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100827 }
Hanno Beckerda1655a2017-10-18 14:21:44 +0100828}
829
Paul Bakker5121ce52009-01-03 21:22:43 +0000830/*
Janos Follatha778a942019-02-13 10:28:28 +0000831 * Import X from unsigned binary data, little endian
832 */
833int mbedtls_mpi_read_binary_le( mbedtls_mpi *X,
834 const unsigned char *buf, size_t buflen )
835{
Janos Follath24eed8d2019-11-22 13:21:35 +0000836 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follatha778a942019-02-13 10:28:28 +0000837 size_t i;
838 size_t const limbs = CHARS_TO_LIMBS( buflen );
839
840 /* Ensure that target MPI has exactly the necessary number of limbs */
841 if( X->n != limbs )
842 {
843 mbedtls_mpi_free( X );
844 mbedtls_mpi_init( X );
845 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, limbs ) );
846 }
847
848 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
849
850 for( i = 0; i < buflen; i++ )
851 X->p[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
852
853cleanup:
854
Janos Follath171a7ef2019-02-15 16:17:45 +0000855 /*
856 * This function is also used to import keys. However, wiping the buffers
857 * upon failure is not necessary because failure only can happen before any
858 * input is copied.
859 */
Janos Follatha778a942019-02-13 10:28:28 +0000860 return( ret );
861}
862
863/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000864 * Import X from unsigned binary data, big endian
865 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000867{
Janos Follath24eed8d2019-11-22 13:21:35 +0000868 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerda1655a2017-10-18 14:21:44 +0100869 size_t const limbs = CHARS_TO_LIMBS( buflen );
870 size_t const overhead = ( limbs * ciL ) - buflen;
871 unsigned char *Xp;
Paul Bakker5121ce52009-01-03 21:22:43 +0000872
Hanno Becker8ce11a32018-12-19 16:18:52 +0000873 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +0000874 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
875
Hanno Becker073c1992017-10-17 15:17:27 +0100876 /* Ensure that target MPI has exactly the necessary number of limbs */
877 if( X->n != limbs )
878 {
879 mbedtls_mpi_free( X );
880 mbedtls_mpi_init( X );
881 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, limbs ) );
882 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000884
Hanno Becker0e810b92019-01-03 17:13:11 +0000885 /* Avoid calling `memcpy` with NULL source argument,
886 * even if buflen is 0. */
887 if( buf != NULL )
888 {
889 Xp = (unsigned char*) X->p;
890 memcpy( Xp + overhead, buf, buflen );
Hanno Beckerda1655a2017-10-18 14:21:44 +0100891
Hanno Becker0e810b92019-01-03 17:13:11 +0000892 mpi_bigendian_to_host( X->p, limbs );
893 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000894
895cleanup:
896
Janos Follath171a7ef2019-02-15 16:17:45 +0000897 /*
898 * This function is also used to import keys. However, wiping the buffers
899 * upon failure is not necessary because failure only can happen before any
900 * input is copied.
901 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000902 return( ret );
903}
904
905/*
Janos Follathe344d0f2019-02-19 16:17:40 +0000906 * Export X into unsigned binary data, little endian
907 */
908int mbedtls_mpi_write_binary_le( const mbedtls_mpi *X,
909 unsigned char *buf, size_t buflen )
910{
911 size_t stored_bytes = X->n * ciL;
912 size_t bytes_to_copy;
913 size_t i;
914
915 if( stored_bytes < buflen )
916 {
917 bytes_to_copy = stored_bytes;
918 }
919 else
920 {
921 bytes_to_copy = buflen;
922
923 /* The output buffer is smaller than the allocated size of X.
924 * However X may fit if its leading bytes are zero. */
925 for( i = bytes_to_copy; i < stored_bytes; i++ )
926 {
927 if( GET_BYTE( X, i ) != 0 )
928 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
929 }
930 }
931
932 for( i = 0; i < bytes_to_copy; i++ )
933 buf[i] = GET_BYTE( X, i );
934
935 if( stored_bytes < buflen )
936 {
937 /* Write trailing 0 bytes */
938 memset( buf + stored_bytes, 0, buflen - stored_bytes );
939 }
940
941 return( 0 );
942}
943
944/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000945 * Export X into unsigned binary data, big endian
946 */
Gilles Peskine11cdb052018-11-20 16:47:47 +0100947int mbedtls_mpi_write_binary( const mbedtls_mpi *X,
948 unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000949{
Hanno Becker73d7d792018-12-11 10:35:51 +0000950 size_t stored_bytes;
Gilles Peskine11cdb052018-11-20 16:47:47 +0100951 size_t bytes_to_copy;
952 unsigned char *p;
953 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000954
Hanno Becker73d7d792018-12-11 10:35:51 +0000955 MPI_VALIDATE_RET( X != NULL );
956 MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
957
958 stored_bytes = X->n * ciL;
959
Gilles Peskine11cdb052018-11-20 16:47:47 +0100960 if( stored_bytes < buflen )
961 {
962 /* There is enough space in the output buffer. Write initial
963 * null bytes and record the position at which to start
964 * writing the significant bytes. In this case, the execution
965 * trace of this function does not depend on the value of the
966 * number. */
967 bytes_to_copy = stored_bytes;
968 p = buf + buflen - stored_bytes;
969 memset( buf, 0, buflen - stored_bytes );
970 }
971 else
972 {
973 /* The output buffer is smaller than the allocated size of X.
974 * However X may fit if its leading bytes are zero. */
975 bytes_to_copy = buflen;
976 p = buf;
977 for( i = bytes_to_copy; i < stored_bytes; i++ )
978 {
979 if( GET_BYTE( X, i ) != 0 )
980 return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
981 }
982 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000983
Gilles Peskine11cdb052018-11-20 16:47:47 +0100984 for( i = 0; i < bytes_to_copy; i++ )
985 p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000986
987 return( 0 );
988}
989
990/*
991 * Left-shift: X <<= count
992 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +0000994{
Janos Follath24eed8d2019-11-22 13:21:35 +0000995 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000996 size_t i, v0, t1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +0000998 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000999
1000 v0 = count / (biL );
1001 t1 = count & (biL - 1);
1002
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001003 i = mbedtls_mpi_bitlen( X ) + count;
Paul Bakker5121ce52009-01-03 21:22:43 +00001004
Paul Bakkerf9688572011-05-05 10:00:45 +00001005 if( X->n * biL < i )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001007
1008 ret = 0;
1009
1010 /*
1011 * shift by count / limb_size
1012 */
1013 if( v0 > 0 )
1014 {
Paul Bakker23986e52011-04-24 08:57:21 +00001015 for( i = X->n; i > v0; i-- )
1016 X->p[i - 1] = X->p[i - v0 - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
Paul Bakker23986e52011-04-24 08:57:21 +00001018 for( ; i > 0; i-- )
1019 X->p[i - 1] = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001020 }
1021
1022 /*
1023 * shift by count % limb_size
1024 */
1025 if( t1 > 0 )
1026 {
1027 for( i = v0; i < X->n; i++ )
1028 {
1029 r1 = X->p[i] >> (biL - t1);
1030 X->p[i] <<= t1;
1031 X->p[i] |= r0;
1032 r0 = r1;
1033 }
1034 }
1035
1036cleanup:
1037
1038 return( ret );
1039}
1040
1041/*
1042 * Right-shift: X >>= count
1043 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +00001045{
Paul Bakker23986e52011-04-24 08:57:21 +00001046 size_t i, v0, v1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 mbedtls_mpi_uint r0 = 0, r1;
Hanno Becker73d7d792018-12-11 10:35:51 +00001048 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001049
1050 v0 = count / biL;
1051 v1 = count & (biL - 1);
1052
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001053 if( v0 > X->n || ( v0 == X->n && v1 > 0 ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 return mbedtls_mpi_lset( X, 0 );
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +01001055
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 /*
1057 * shift by count / limb_size
1058 */
1059 if( v0 > 0 )
1060 {
1061 for( i = 0; i < X->n - v0; i++ )
1062 X->p[i] = X->p[i + v0];
1063
1064 for( ; i < X->n; i++ )
1065 X->p[i] = 0;
1066 }
1067
1068 /*
1069 * shift by count % limb_size
1070 */
1071 if( v1 > 0 )
1072 {
Paul Bakker23986e52011-04-24 08:57:21 +00001073 for( i = X->n; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001074 {
Paul Bakker23986e52011-04-24 08:57:21 +00001075 r1 = X->p[i - 1] << (biL - v1);
1076 X->p[i - 1] >>= v1;
1077 X->p[i - 1] |= r0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001078 r0 = r1;
1079 }
1080 }
1081
1082 return( 0 );
1083}
1084
1085/*
1086 * Compare unsigned values
1087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001089{
Paul Bakker23986e52011-04-24 08:57:21 +00001090 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001091 MPI_VALIDATE_RET( X != NULL );
1092 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001093
Paul Bakker23986e52011-04-24 08:57:21 +00001094 for( i = X->n; i > 0; i-- )
1095 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001096 break;
1097
Paul Bakker23986e52011-04-24 08:57:21 +00001098 for( j = Y->n; j > 0; j-- )
1099 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001100 break;
1101
Paul Bakker23986e52011-04-24 08:57:21 +00001102 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001103 return( 0 );
1104
1105 if( i > j ) return( 1 );
1106 if( j > i ) return( -1 );
1107
Paul Bakker23986e52011-04-24 08:57:21 +00001108 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001109 {
Paul Bakker23986e52011-04-24 08:57:21 +00001110 if( X->p[i - 1] > Y->p[i - 1] ) return( 1 );
1111 if( X->p[i - 1] < Y->p[i - 1] ) return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 }
1113
1114 return( 0 );
1115}
1116
1117/*
1118 * Compare signed values
1119 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +00001121{
Paul Bakker23986e52011-04-24 08:57:21 +00001122 size_t i, j;
Hanno Becker73d7d792018-12-11 10:35:51 +00001123 MPI_VALIDATE_RET( X != NULL );
1124 MPI_VALIDATE_RET( Y != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001125
Paul Bakker23986e52011-04-24 08:57:21 +00001126 for( i = X->n; i > 0; i-- )
1127 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001128 break;
1129
Paul Bakker23986e52011-04-24 08:57:21 +00001130 for( j = Y->n; j > 0; j-- )
1131 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001132 break;
1133
Paul Bakker23986e52011-04-24 08:57:21 +00001134 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001135 return( 0 );
1136
1137 if( i > j ) return( X->s );
Paul Bakker0c8f73b2012-03-22 14:08:57 +00001138 if( j > i ) return( -Y->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001139
1140 if( X->s > 0 && Y->s < 0 ) return( 1 );
1141 if( Y->s > 0 && X->s < 0 ) return( -1 );
1142
Paul Bakker23986e52011-04-24 08:57:21 +00001143 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001144 {
Paul Bakker23986e52011-04-24 08:57:21 +00001145 if( X->p[i - 1] > Y->p[i - 1] ) return( X->s );
1146 if( X->p[i - 1] < Y->p[i - 1] ) return( -X->s );
Paul Bakker5121ce52009-01-03 21:22:43 +00001147 }
1148
1149 return( 0 );
1150}
1151
1152/*
1153 * Compare signed values
1154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +00001156{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 mbedtls_mpi Y;
1158 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001159 MPI_VALIDATE_RET( X != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001160
1161 *p = ( z < 0 ) ? -z : z;
1162 Y.s = ( z < 0 ) ? -1 : 1;
1163 Y.n = 1;
1164 Y.p = p;
1165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 return( mbedtls_mpi_cmp_mpi( X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001167}
1168
1169/*
1170 * Unsigned addition: X = |A| + |B| (HAC 14.7)
1171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001173{
Janos Follath24eed8d2019-11-22 13:21:35 +00001174 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001175 size_t i, j;
Janos Follath6c922682015-10-30 17:43:11 +01001176 mbedtls_mpi_uint *o, *p, c, tmp;
Hanno Becker73d7d792018-12-11 10:35:51 +00001177 MPI_VALIDATE_RET( X != NULL );
1178 MPI_VALIDATE_RET( A != NULL );
1179 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001180
1181 if( X == B )
1182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 const mbedtls_mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 }
1185
1186 if( X != A )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
Paul Bakker9af723c2014-05-01 13:03:14 +02001188
Paul Bakkerf7ca7b92009-06-20 10:31:06 +00001189 /*
1190 * X should always be positive as a result of unsigned additions.
1191 */
1192 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001193
Paul Bakker23986e52011-04-24 08:57:21 +00001194 for( j = B->n; j > 0; j-- )
1195 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001196 break;
1197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001199
1200 o = B->p; p = X->p; c = 0;
1201
Janos Follath6c922682015-10-30 17:43:11 +01001202 /*
1203 * tmp is used because it might happen that p == o
1204 */
Paul Bakker23986e52011-04-24 08:57:21 +00001205 for( i = 0; i < j; i++, o++, p++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 {
Janos Follath6c922682015-10-30 17:43:11 +01001207 tmp= *o;
Paul Bakker5121ce52009-01-03 21:22:43 +00001208 *p += c; c = ( *p < c );
Janos Follath6c922682015-10-30 17:43:11 +01001209 *p += tmp; c += ( *p < tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +00001210 }
1211
1212 while( c != 0 )
1213 {
1214 if( i >= X->n )
1215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001217 p = X->p + i;
1218 }
1219
Paul Bakker2d319fd2012-09-16 21:34:26 +00001220 *p += c; c = ( *p < c ); i++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001221 }
1222
1223cleanup:
1224
1225 return( ret );
1226}
1227
1228/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 * Helper for mbedtls_mpi subtraction
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231static void mpi_sub_hlp( size_t n, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d )
Paul Bakker5121ce52009-01-03 21:22:43 +00001232{
Paul Bakker23986e52011-04-24 08:57:21 +00001233 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 mbedtls_mpi_uint c, z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001235
1236 for( i = c = 0; i < n; i++, s++, d++ )
1237 {
1238 z = ( *d < c ); *d -= c;
1239 c = ( *d < *s ) + z; *d -= *s;
1240 }
1241
1242 while( c != 0 )
1243 {
1244 z = ( *d < c ); *d -= c;
Alexey Skalozub8e75e682016-01-13 21:59:27 +02001245 c = z; d++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 }
1247}
1248
1249/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001250 * Unsigned subtraction: X = |A| - |B| (HAC 14.9)
Paul Bakker5121ce52009-01-03 21:22:43 +00001251 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001253{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 mbedtls_mpi TB;
Janos Follath24eed8d2019-11-22 13:21:35 +00001255 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001256 size_t n;
Hanno Becker73d7d792018-12-11 10:35:51 +00001257 MPI_VALIDATE_RET( X != NULL );
1258 MPI_VALIDATE_RET( A != NULL );
1259 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
1262 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001265
1266 if( X == B )
1267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001269 B = &TB;
1270 }
1271
1272 if( X != A )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001274
Paul Bakker1ef7a532009-06-20 10:50:55 +00001275 /*
Paul Bakker60b1d102013-10-29 10:02:51 +01001276 * X should always be positive as a result of unsigned subtractions.
Paul Bakker1ef7a532009-06-20 10:50:55 +00001277 */
1278 X->s = 1;
1279
Paul Bakker5121ce52009-01-03 21:22:43 +00001280 ret = 0;
1281
Paul Bakker23986e52011-04-24 08:57:21 +00001282 for( n = B->n; n > 0; n-- )
1283 if( B->p[n - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 break;
1285
Paul Bakker23986e52011-04-24 08:57:21 +00001286 mpi_sub_hlp( n, B->p, X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +00001287
1288cleanup:
1289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 mbedtls_mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001291
1292 return( ret );
1293}
1294
1295/*
1296 * Signed addition: X = A + B
1297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001299{
Hanno Becker73d7d792018-12-11 10:35:51 +00001300 int ret, s;
1301 MPI_VALIDATE_RET( X != NULL );
1302 MPI_VALIDATE_RET( A != NULL );
1303 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001304
Hanno Becker73d7d792018-12-11 10:35:51 +00001305 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001306 if( A->s * B->s < 0 )
1307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 X->s = s;
1312 }
1313 else
1314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 X->s = -s;
1317 }
1318 }
1319 else
1320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 X->s = s;
1323 }
1324
1325cleanup:
1326
1327 return( ret );
1328}
1329
1330/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001331 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001334{
Hanno Becker73d7d792018-12-11 10:35:51 +00001335 int ret, s;
1336 MPI_VALIDATE_RET( X != NULL );
1337 MPI_VALIDATE_RET( A != NULL );
1338 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001339
Hanno Becker73d7d792018-12-11 10:35:51 +00001340 s = A->s;
Paul Bakker5121ce52009-01-03 21:22:43 +00001341 if( A->s * B->s > 0 )
1342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 X->s = s;
1347 }
1348 else
1349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001351 X->s = -s;
1352 }
1353 }
1354 else
1355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 X->s = s;
1358 }
1359
1360cleanup:
1361
1362 return( ret );
1363}
1364
1365/*
1366 * Signed addition: X = A + b
1367 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001369{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 mbedtls_mpi _B;
1371 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001372 MPI_VALIDATE_RET( X != NULL );
1373 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001374
1375 p[0] = ( b < 0 ) ? -b : b;
1376 _B.s = ( b < 0 ) ? -1 : 1;
1377 _B.n = 1;
1378 _B.p = p;
1379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 return( mbedtls_mpi_add_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001381}
1382
1383/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001384 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001387{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 mbedtls_mpi _B;
1389 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001390 MPI_VALIDATE_RET( X != NULL );
1391 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001392
1393 p[0] = ( b < 0 ) ? -b : b;
1394 _B.s = ( b < 0 ) ? -1 : 1;
1395 _B.n = 1;
1396 _B.p = p;
1397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 return( mbedtls_mpi_sub_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001399}
1400
1401/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 * Helper for mbedtls_mpi multiplication
Paul Bakkerfc4f46f2013-06-24 19:23:56 +02001403 */
1404static
1405#if defined(__APPLE__) && defined(__arm__)
1406/*
1407 * Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
1408 * appears to need this to prevent bad ARM code generation at -O3.
1409 */
1410__attribute__ ((noinline))
1411#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412void 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 +00001413{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 mbedtls_mpi_uint c = 0, t = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001415
1416#if defined(MULADDC_HUIT)
1417 for( ; i >= 8; i -= 8 )
1418 {
1419 MULADDC_INIT
1420 MULADDC_HUIT
1421 MULADDC_STOP
1422 }
1423
1424 for( ; i > 0; i-- )
1425 {
1426 MULADDC_INIT
1427 MULADDC_CORE
1428 MULADDC_STOP
1429 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001430#else /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001431 for( ; i >= 16; i -= 16 )
1432 {
1433 MULADDC_INIT
1434 MULADDC_CORE MULADDC_CORE
1435 MULADDC_CORE MULADDC_CORE
1436 MULADDC_CORE MULADDC_CORE
1437 MULADDC_CORE MULADDC_CORE
1438
1439 MULADDC_CORE MULADDC_CORE
1440 MULADDC_CORE MULADDC_CORE
1441 MULADDC_CORE MULADDC_CORE
1442 MULADDC_CORE MULADDC_CORE
1443 MULADDC_STOP
1444 }
1445
1446 for( ; i >= 8; i -= 8 )
1447 {
1448 MULADDC_INIT
1449 MULADDC_CORE MULADDC_CORE
1450 MULADDC_CORE MULADDC_CORE
1451
1452 MULADDC_CORE MULADDC_CORE
1453 MULADDC_CORE MULADDC_CORE
1454 MULADDC_STOP
1455 }
1456
1457 for( ; i > 0; i-- )
1458 {
1459 MULADDC_INIT
1460 MULADDC_CORE
1461 MULADDC_STOP
1462 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001463#endif /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001464
1465 t++;
1466
1467 do {
1468 *d += c; c = ( *d < c ); d++;
1469 }
1470 while( c != 0 );
1471}
1472
1473/*
1474 * Baseline multiplication: X = A * B (HAC 14.12)
1475 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001477{
Janos Follath24eed8d2019-11-22 13:21:35 +00001478 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001479 size_t i, j;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 mbedtls_mpi TA, TB;
Hanno Becker73d7d792018-12-11 10:35:51 +00001481 MPI_VALIDATE_RET( X != NULL );
1482 MPI_VALIDATE_RET( A != NULL );
1483 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 if( X == A ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); A = &TA; }
1488 if( X == B ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); B = &TB; }
Paul Bakker5121ce52009-01-03 21:22:43 +00001489
Paul Bakker23986e52011-04-24 08:57:21 +00001490 for( i = A->n; i > 0; i-- )
1491 if( A->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 break;
1493
Paul Bakker23986e52011-04-24 08:57:21 +00001494 for( j = B->n; j > 0; j-- )
1495 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001496 break;
1497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + j ) );
1499 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001500
Alexey Skalozub8e75e682016-01-13 21:59:27 +02001501 for( ; j > 0; j-- )
1502 mpi_mul_hlp( i, A->p, X->p + j - 1, B->p[j - 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00001503
1504 X->s = A->s * B->s;
1505
1506cleanup:
1507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001509
1510 return( ret );
1511}
1512
1513/*
1514 * Baseline multiplication: X = A * b
1515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 mbedtls_mpi _B;
1519 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001520 MPI_VALIDATE_RET( X != NULL );
1521 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001522
1523 _B.s = 1;
1524 _B.n = 1;
1525 _B.p = p;
1526 p[0] = b;
1527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 return( mbedtls_mpi_mul_mpi( X, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001529}
1530
1531/*
Simon Butcherf5ba0452015-12-27 23:01:55 +00001532 * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
1533 * mbedtls_mpi_uint divisor, d
Simon Butcher15b15d12015-11-26 19:35:03 +00001534 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001535static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1,
1536 mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r )
Simon Butcher15b15d12015-11-26 19:35:03 +00001537{
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001538#if defined(MBEDTLS_HAVE_UDBL)
1539 mbedtls_t_udbl dividend, quotient;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001540#else
Simon Butcher9803d072016-01-03 00:24:34 +00001541 const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
1542 const mbedtls_mpi_uint uint_halfword_mask = ( (mbedtls_mpi_uint) 1 << biH ) - 1;
Simon Butcherf5ba0452015-12-27 23:01:55 +00001543 mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
1544 mbedtls_mpi_uint u0_msw, u0_lsw;
Simon Butcher9803d072016-01-03 00:24:34 +00001545 size_t s;
Manuel Pégourié-Gonnard16308882015-12-01 10:27:00 +01001546#endif
1547
Simon Butcher15b15d12015-11-26 19:35:03 +00001548 /*
1549 * Check for overflow
1550 */
Simon Butcherf5ba0452015-12-27 23:01:55 +00001551 if( 0 == d || u1 >= d )
Simon Butcher15b15d12015-11-26 19:35:03 +00001552 {
Simon Butcherf5ba0452015-12-27 23:01:55 +00001553 if (r != NULL) *r = ~0;
Simon Butcher15b15d12015-11-26 19:35:03 +00001554
Simon Butcherf5ba0452015-12-27 23:01:55 +00001555 return ( ~0 );
Simon Butcher15b15d12015-11-26 19:35:03 +00001556 }
1557
1558#if defined(MBEDTLS_HAVE_UDBL)
Simon Butcher15b15d12015-11-26 19:35:03 +00001559 dividend = (mbedtls_t_udbl) u1 << biL;
1560 dividend |= (mbedtls_t_udbl) u0;
1561 quotient = dividend / d;
1562 if( quotient > ( (mbedtls_t_udbl) 1 << biL ) - 1 )
1563 quotient = ( (mbedtls_t_udbl) 1 << biL ) - 1;
1564
1565 if( r != NULL )
Simon Butcher9803d072016-01-03 00:24:34 +00001566 *r = (mbedtls_mpi_uint)( dividend - (quotient * d ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001567
1568 return (mbedtls_mpi_uint) quotient;
1569#else
Simon Butcher15b15d12015-11-26 19:35:03 +00001570
1571 /*
1572 * Algorithm D, Section 4.3.1 - The Art of Computer Programming
1573 * Vol. 2 - Seminumerical Algorithms, Knuth
1574 */
1575
1576 /*
1577 * Normalize the divisor, d, and dividend, u0, u1
1578 */
1579 s = mbedtls_clz( d );
1580 d = d << s;
1581
1582 u1 = u1 << s;
Simon Butcher9803d072016-01-03 00:24:34 +00001583 u1 |= ( u0 >> ( biL - s ) ) & ( -(mbedtls_mpi_sint)s >> ( biL - 1 ) );
Simon Butcher15b15d12015-11-26 19:35:03 +00001584 u0 = u0 << s;
1585
1586 d1 = d >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001587 d0 = d & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001588
1589 u0_msw = u0 >> biH;
Simon Butcher9803d072016-01-03 00:24:34 +00001590 u0_lsw = u0 & uint_halfword_mask;
Simon Butcher15b15d12015-11-26 19:35:03 +00001591
1592 /*
1593 * Find the first quotient and remainder
1594 */
1595 q1 = u1 / d1;
1596 r0 = u1 - d1 * q1;
1597
1598 while( q1 >= radix || ( q1 * d0 > radix * r0 + u0_msw ) )
1599 {
1600 q1 -= 1;
1601 r0 += d1;
1602
1603 if ( r0 >= radix ) break;
1604 }
1605
Simon Butcherf5ba0452015-12-27 23:01:55 +00001606 rAX = ( u1 * radix ) + ( u0_msw - q1 * d );
Simon Butcher15b15d12015-11-26 19:35:03 +00001607 q0 = rAX / d1;
1608 r0 = rAX - q0 * d1;
1609
1610 while( q0 >= radix || ( q0 * d0 > radix * r0 + u0_lsw ) )
1611 {
1612 q0 -= 1;
1613 r0 += d1;
1614
1615 if ( r0 >= radix ) break;
1616 }
1617
1618 if (r != NULL)
Simon Butcherf5ba0452015-12-27 23:01:55 +00001619 *r = ( rAX * radix + u0_lsw - q0 * d ) >> s;
Simon Butcher15b15d12015-11-26 19:35:03 +00001620
1621 quotient = q1 * radix + q0;
1622
1623 return quotient;
1624#endif
1625}
1626
1627/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
Paul Bakker5121ce52009-01-03 21:22:43 +00001629 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001630int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
1631 const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001632{
Janos Follath24eed8d2019-11-22 13:21:35 +00001633 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001634 size_t i, n, t, k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635 mbedtls_mpi X, Y, Z, T1, T2;
Alexander Kd19a1932019-11-01 18:20:42 +03001636 mbedtls_mpi_uint TP2[3];
Hanno Becker73d7d792018-12-11 10:35:51 +00001637 MPI_VALIDATE_RET( A != NULL );
1638 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 if( mbedtls_mpi_cmp_int( B, 0 ) == 0 )
1641 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Alexander K35d6d462019-10-31 14:46:45 +03001644 mbedtls_mpi_init( &T1 );
Alexander Kd19a1932019-11-01 18:20:42 +03001645 /*
1646 * Avoid dynamic memory allocations for constant-size T2.
1647 *
1648 * T2 is used for comparison only and the 3 limbs are assigned explicitly,
1649 * so nobody increase the size of the MPI and we're safe to use an on-stack
1650 * buffer.
1651 */
Alexander K35d6d462019-10-31 14:46:45 +03001652 T2.s = 1;
Alexander Kd19a1932019-11-01 18:20:42 +03001653 T2.n = sizeof( TP2 ) / sizeof( *TP2 );
1654 T2.p = TP2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656 if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 if( Q != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_lset( Q, 0 ) );
1659 if( R != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001660 return( 0 );
1661 }
1662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &X, A ) );
1664 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001665 X.s = Y.s = 1;
1666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &Z, A->n + 2 ) );
1668 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Z, 0 ) );
1669 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001670
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001671 k = mbedtls_mpi_bitlen( &Y ) % biL;
Paul Bakkerf9688572011-05-05 10:00:45 +00001672 if( k < biL - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001673 {
1674 k = biL - 1 - k;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &X, k ) );
1676 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, k ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001677 }
1678 else k = 0;
1679
1680 n = X.n - 1;
1681 t = Y.n - 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 while( mbedtls_mpi_cmp_mpi( &X, &Y ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001685 {
1686 Z.p[n - t]++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001688 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001690
1691 for( i = n; i > t ; i-- )
1692 {
1693 if( X.p[i] >= Y.p[t] )
1694 Z.p[i - t - 1] = ~0;
1695 else
1696 {
Simon Butcher15b15d12015-11-26 19:35:03 +00001697 Z.p[i - t - 1] = mbedtls_int_div_int( X.p[i], X.p[i - 1],
1698 Y.p[t], NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +00001699 }
1700
Alexander K35d6d462019-10-31 14:46:45 +03001701 T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2];
1702 T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1];
1703 T2.p[2] = X.p[i];
1704
Paul Bakker5121ce52009-01-03 21:22:43 +00001705 Z.p[i - t - 1]++;
1706 do
1707 {
1708 Z.p[i - t - 1]--;
1709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T1, 0 ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001711 T1.p[0] = ( t < 1 ) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001712 T1.p[1] = Y.p[t];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001714 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 while( mbedtls_mpi_cmp_mpi( &T1, &T2 ) > 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
1718 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1719 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 if( mbedtls_mpi_cmp_int( &X, 0 ) < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &Y ) );
1724 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
1725 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X, &X, &T1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001726 Z.p[i - t - 1]--;
1727 }
1728 }
1729
1730 if( Q != NULL )
1731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( Q, &Z ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001733 Q->s = A->s * B->s;
1734 }
1735
1736 if( R != NULL )
1737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &X, k ) );
Paul Bakkerf02c5642012-11-13 10:25:21 +00001739 X.s = A->s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, &X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742 if( mbedtls_mpi_cmp_int( R, 0 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001743 R->s = 1;
1744 }
1745
1746cleanup:
1747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Alexander K35d6d462019-10-31 14:46:45 +03001749 mbedtls_mpi_free( &T1 );
Alexander Kd19a1932019-11-01 18:20:42 +03001750 mbedtls_platform_zeroize( TP2, sizeof( TP2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001751
1752 return( ret );
1753}
1754
1755/*
1756 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00001757 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001758int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R,
1759 const mbedtls_mpi *A,
1760 mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001761{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 mbedtls_mpi _B;
1763 mbedtls_mpi_uint p[1];
Hanno Becker73d7d792018-12-11 10:35:51 +00001764 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
1766 p[0] = ( b < 0 ) ? -b : b;
1767 _B.s = ( b < 0 ) ? -1 : 1;
1768 _B.n = 1;
1769 _B.p = p;
1770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 return( mbedtls_mpi_div_mpi( Q, R, A, &_B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001772}
1773
1774/*
1775 * Modulo: R = A mod B
1776 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001778{
Janos Follath24eed8d2019-11-22 13:21:35 +00001779 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker73d7d792018-12-11 10:35:51 +00001780 MPI_VALIDATE_RET( R != NULL );
1781 MPI_VALIDATE_RET( A != NULL );
1782 MPI_VALIDATE_RET( B != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 if( mbedtls_mpi_cmp_int( B, 0 ) < 0 )
1785 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( NULL, R, A, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 while( mbedtls_mpi_cmp_int( R, 0 ) < 0 )
1790 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 while( mbedtls_mpi_cmp_mpi( R, B ) >= 0 )
1793 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( R, R, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001794
1795cleanup:
1796
1797 return( ret );
1798}
1799
1800/*
1801 * Modulo: r = A mod b
1802 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001804{
Paul Bakker23986e52011-04-24 08:57:21 +00001805 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 mbedtls_mpi_uint x, y, z;
Hanno Becker73d7d792018-12-11 10:35:51 +00001807 MPI_VALIDATE_RET( r != NULL );
1808 MPI_VALIDATE_RET( A != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001809
1810 if( b == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001812
1813 if( b < 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001815
1816 /*
1817 * handle trivial cases
1818 */
1819 if( b == 1 )
1820 {
1821 *r = 0;
1822 return( 0 );
1823 }
1824
1825 if( b == 2 )
1826 {
1827 *r = A->p[0] & 1;
1828 return( 0 );
1829 }
1830
1831 /*
1832 * general case
1833 */
Paul Bakker23986e52011-04-24 08:57:21 +00001834 for( i = A->n, y = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001835 {
Paul Bakker23986e52011-04-24 08:57:21 +00001836 x = A->p[i - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001837 y = ( y << biH ) | ( x >> biH );
1838 z = y / b;
1839 y -= z * b;
1840
1841 x <<= biH;
1842 y = ( y << biH ) | ( x >> biH );
1843 z = y / b;
1844 y -= z * b;
1845 }
1846
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001847 /*
1848 * If A is negative, then the current y represents a negative value.
1849 * Flipping it to the positive side.
1850 */
1851 if( A->s < 0 && y != 0 )
1852 y = b - y;
1853
Paul Bakker5121ce52009-01-03 21:22:43 +00001854 *r = y;
1855
1856 return( 0 );
1857}
1858
1859/*
1860 * Fast Montgomery initialization (thanks to Tom St Denis)
1861 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862static void mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00001863{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 mbedtls_mpi_uint x, m0 = N->p[0];
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01001865 unsigned int i;
Paul Bakker5121ce52009-01-03 21:22:43 +00001866
1867 x = m0;
1868 x += ( ( m0 + 2 ) & 4 ) << 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001869
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01001870 for( i = biL; i >= 8; i /= 2 )
1871 x *= ( 2 - ( m0 * x ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001872
1873 *mm = ~x + 1;
1874}
1875
1876/*
1877 * Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
1878 */
Nicholas Wilson91c68a52016-04-13 11:44:29 +01001879static int 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 +02001880 const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00001881{
Paul Bakker23986e52011-04-24 08:57:21 +00001882 size_t i, n, m;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 mbedtls_mpi_uint u0, u1, *d;
Paul Bakker5121ce52009-01-03 21:22:43 +00001884
Nicholas Wilson91c68a52016-04-13 11:44:29 +01001885 if( T->n < N->n + 1 || T->p == NULL )
1886 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
1887
Paul Bakker5121ce52009-01-03 21:22:43 +00001888 memset( T->p, 0, T->n * ciL );
1889
1890 d = T->p;
1891 n = N->n;
1892 m = ( B->n < n ) ? B->n : n;
1893
1894 for( i = 0; i < n; i++ )
1895 {
1896 /*
1897 * T = (T + u0*B + u1*N) / 2^biL
1898 */
1899 u0 = A->p[i];
1900 u1 = ( d[0] + u0 * B->p[0] ) * mm;
1901
1902 mpi_mul_hlp( m, B->p, d, u0 );
1903 mpi_mul_hlp( n, N->p, d, u1 );
1904
1905 *d++ = u0; d[n + 1] = 0;
1906 }
1907
Paul Bakker66d5d072014-06-17 16:39:18 +02001908 memcpy( A->p, d, ( n + 1 ) * ciL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910 if( mbedtls_mpi_cmp_abs( A, N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001911 mpi_sub_hlp( n, N->p, A->p );
1912 else
1913 /* prevent timing attacks */
1914 mpi_sub_hlp( n, A->p, T->p );
Nicholas Wilson91c68a52016-04-13 11:44:29 +01001915
1916 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001917}
1918
1919/*
1920 * Montgomery reduction: A = A * R^-1 mod N
1921 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001922static int mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N,
1923 mbedtls_mpi_uint mm, const mbedtls_mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00001924{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 mbedtls_mpi_uint z = 1;
1926 mbedtls_mpi U;
Paul Bakker5121ce52009-01-03 21:22:43 +00001927
Paul Bakker8ddb6452013-02-27 14:56:33 +01001928 U.n = U.s = (int) z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 U.p = &z;
1930
Nicholas Wilson91c68a52016-04-13 11:44:29 +01001931 return( mpi_montmul( A, &U, N, mm, T ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001932}
1933
1934/*
1935 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
1936 */
Hanno Becker73d7d792018-12-11 10:35:51 +00001937int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
1938 const mbedtls_mpi *E, const mbedtls_mpi *N,
1939 mbedtls_mpi *_RR )
Paul Bakker5121ce52009-01-03 21:22:43 +00001940{
Janos Follath24eed8d2019-11-22 13:21:35 +00001941 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001942 size_t wbits, wsize, one = 1;
1943 size_t i, j, nblimbs;
1944 size_t bufsize, nbits;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945 mbedtls_mpi_uint ei, mm, state;
1946 mbedtls_mpi RR, T, W[ 2 << MBEDTLS_MPI_WINDOW_SIZE ], Apos;
Paul Bakkerf6198c12012-05-16 08:02:29 +00001947 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001948
Hanno Becker73d7d792018-12-11 10:35:51 +00001949 MPI_VALIDATE_RET( X != NULL );
1950 MPI_VALIDATE_RET( A != NULL );
1951 MPI_VALIDATE_RET( E != NULL );
1952 MPI_VALIDATE_RET( N != NULL );
1953
Hanno Becker8d1dd1b2017-09-28 11:02:24 +01001954 if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 || ( N->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 if( mbedtls_mpi_cmp_int( E, 0 ) < 0 )
1958 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakkerf6198c12012-05-16 08:02:29 +00001959
1960 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001961 * Init temps and window size
1962 */
1963 mpi_montg_init( &mm, N );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &T );
1965 mbedtls_mpi_init( &Apos );
Paul Bakker5121ce52009-01-03 21:22:43 +00001966 memset( W, 0, sizeof( W ) );
1967
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001968 i = mbedtls_mpi_bitlen( E );
Paul Bakker5121ce52009-01-03 21:22:43 +00001969
1970 wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
1971 ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1;
1972
Peter Kolbuse6bcad32018-12-11 14:01:44 -06001973#if( MBEDTLS_MPI_WINDOW_SIZE < 6 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 if( wsize > MBEDTLS_MPI_WINDOW_SIZE )
1975 wsize = MBEDTLS_MPI_WINDOW_SIZE;
Peter Kolbuse6bcad32018-12-11 14:01:44 -06001976#endif
Paul Bakkerb6d5f082011-11-25 11:52:11 +00001977
Paul Bakker5121ce52009-01-03 21:22:43 +00001978 j = N->n + 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
1980 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], j ) );
1981 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T, j * 2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001982
1983 /*
Paul Bakker50546922012-05-19 08:40:49 +00001984 * Compensate for negative A (and correct at the end)
1985 */
1986 neg = ( A->s == -1 );
Paul Bakker50546922012-05-19 08:40:49 +00001987 if( neg )
1988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Apos, A ) );
Paul Bakker50546922012-05-19 08:40:49 +00001990 Apos.s = 1;
1991 A = &Apos;
1992 }
1993
1994 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001995 * If 1st call, pre-compute R^2 mod N
1996 */
1997 if( _RR == NULL || _RR->p == NULL )
1998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &RR, 1 ) );
2000 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &RR, N->n * 2 * biL ) );
2001 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &RR, &RR, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002002
2003 if( _RR != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 memcpy( _RR, &RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002005 }
2006 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 memcpy( &RR, _RR, sizeof( mbedtls_mpi ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002008
2009 /*
2010 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
2011 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 if( mbedtls_mpi_cmp_mpi( A, N ) >= 0 )
2013 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &W[1], A, N ) );
Paul Bakkerc2024f42014-01-23 20:38:35 +01002014 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[1], A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002016
Nicholas Wilson91c68a52016-04-13 11:44:29 +01002017 MBEDTLS_MPI_CHK( mpi_montmul( &W[1], &RR, N, mm, &T ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002018
2019 /*
2020 * X = R^2 * R^-1 mod N = R mod N
2021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &RR ) );
Nicholas Wilson91c68a52016-04-13 11:44:29 +01002023 MBEDTLS_MPI_CHK( mpi_montred( X, N, mm, &T ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002024
2025 if( wsize > 1 )
2026 {
2027 /*
2028 * W[1 << (wsize - 1)] = W[1] ^ (wsize - 1)
2029 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002030 j = one << ( wsize - 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[j], N->n + 1 ) );
2033 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[j], &W[1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002034
2035 for( i = 0; i < wsize - 1; i++ )
Nicholas Wilson91c68a52016-04-13 11:44:29 +01002036 MBEDTLS_MPI_CHK( mpi_montmul( &W[j], &W[j], N, mm, &T ) );
Paul Bakker0d7702c2013-10-29 16:18:35 +01002037
Paul Bakker5121ce52009-01-03 21:22:43 +00002038 /*
2039 * W[i] = W[i - 1] * W[1]
2040 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002041 for( i = j + 1; i < ( one << wsize ); i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[i], N->n + 1 ) );
2044 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[i], &W[i - 1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002045
Nicholas Wilson91c68a52016-04-13 11:44:29 +01002046 MBEDTLS_MPI_CHK( mpi_montmul( &W[i], &W[1], N, mm, &T ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002047 }
2048 }
2049
2050 nblimbs = E->n;
2051 bufsize = 0;
2052 nbits = 0;
2053 wbits = 0;
2054 state = 0;
2055
2056 while( 1 )
2057 {
2058 if( bufsize == 0 )
2059 {
Paul Bakker0d7702c2013-10-29 16:18:35 +01002060 if( nblimbs == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002061 break;
2062
Paul Bakker0d7702c2013-10-29 16:18:35 +01002063 nblimbs--;
2064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065 bufsize = sizeof( mbedtls_mpi_uint ) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00002066 }
2067
2068 bufsize--;
2069
2070 ei = (E->p[nblimbs] >> bufsize) & 1;
2071
2072 /*
2073 * skip leading 0s
2074 */
2075 if( ei == 0 && state == 0 )
2076 continue;
2077
2078 if( ei == 0 && state == 1 )
2079 {
2080 /*
2081 * out of window, square X
2082 */
Nicholas Wilson91c68a52016-04-13 11:44:29 +01002083 MBEDTLS_MPI_CHK( mpi_montmul( X, X, N, mm, &T ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002084 continue;
2085 }
2086
2087 /*
2088 * add ei to current window
2089 */
2090 state = 2;
2091
2092 nbits++;
Paul Bakker66d5d072014-06-17 16:39:18 +02002093 wbits |= ( ei << ( wsize - nbits ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002094
2095 if( nbits == wsize )
2096 {
2097 /*
2098 * X = X^wsize R^-1 mod N
2099 */
2100 for( i = 0; i < wsize; i++ )
Nicholas Wilson91c68a52016-04-13 11:44:29 +01002101 MBEDTLS_MPI_CHK( mpi_montmul( X, X, N, mm, &T ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002102
2103 /*
2104 * X = X * W[wbits] R^-1 mod N
2105 */
Nicholas Wilson91c68a52016-04-13 11:44:29 +01002106 MBEDTLS_MPI_CHK( mpi_montmul( X, &W[wbits], N, mm, &T ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002107
2108 state--;
2109 nbits = 0;
2110 wbits = 0;
2111 }
2112 }
2113
2114 /*
2115 * process the remaining bits
2116 */
2117 for( i = 0; i < nbits; i++ )
2118 {
Nicholas Wilson91c68a52016-04-13 11:44:29 +01002119 MBEDTLS_MPI_CHK( mpi_montmul( X, X, N, mm, &T ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002120
2121 wbits <<= 1;
2122
Paul Bakker66d5d072014-06-17 16:39:18 +02002123 if( ( wbits & ( one << wsize ) ) != 0 )
Nicholas Wilson91c68a52016-04-13 11:44:29 +01002124 MBEDTLS_MPI_CHK( mpi_montmul( X, &W[1], N, mm, &T ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 }
2126
2127 /*
2128 * X = A^E * R * R^-1 mod N = A^E mod N
2129 */
Nicholas Wilson91c68a52016-04-13 11:44:29 +01002130 MBEDTLS_MPI_CHK( mpi_montred( X, N, mm, &T ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002131
Hanno Beckera4af1c42017-04-18 09:07:45 +01002132 if( neg && E->n != 0 && ( E->p[0] & 1 ) != 0 )
Paul Bakkerf6198c12012-05-16 08:02:29 +00002133 {
2134 X->s = -1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, N, X ) );
Paul Bakkerf6198c12012-05-16 08:02:29 +00002136 }
2137
Paul Bakker5121ce52009-01-03 21:22:43 +00002138cleanup:
2139
Paul Bakker66d5d072014-06-17 16:39:18 +02002140 for( i = ( one << ( wsize - 1 ) ); i < ( one << wsize ); i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 mbedtls_mpi_free( &W[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +00002142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143 mbedtls_mpi_free( &W[1] ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &Apos );
Paul Bakker6c591fa2011-05-05 11:49:20 +00002144
Paul Bakker75a28602014-03-31 12:08:17 +02002145 if( _RR == NULL || _RR->p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002147
2148 return( ret );
2149}
2150
Paul Bakker5121ce52009-01-03 21:22:43 +00002151/*
2152 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
2153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00002155{
Janos Follath24eed8d2019-11-22 13:21:35 +00002156 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002157 size_t lz, lzt;
Alexander Ke8ad49f2019-08-16 16:16:07 +03002158 mbedtls_mpi TA, TB;
Paul Bakker5121ce52009-01-03 21:22:43 +00002159
Hanno Becker73d7d792018-12-11 10:35:51 +00002160 MPI_VALIDATE_RET( G != NULL );
2161 MPI_VALIDATE_RET( A != NULL );
2162 MPI_VALIDATE_RET( B != NULL );
2163
Alexander Ke8ad49f2019-08-16 16:16:07 +03002164 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) );
2167 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 lz = mbedtls_mpi_lsb( &TA );
2170 lzt = mbedtls_mpi_lsb( &TB );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002171
Paul Bakker66d5d072014-06-17 16:39:18 +02002172 if( lzt < lz )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002173 lz = lzt;
2174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, lz ) );
2176 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, lz ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002177
Paul Bakker5121ce52009-01-03 21:22:43 +00002178 TA.s = TB.s = 1;
2179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 while( mbedtls_mpi_cmp_int( &TA, 0 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, mbedtls_mpi_lsb( &TA ) ) );
2183 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, mbedtls_mpi_lsb( &TB ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185 if( mbedtls_mpi_cmp_mpi( &TA, &TB ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TA, &TA, &TB ) );
2188 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002189 }
2190 else
2191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TB, &TB, &TA ) );
2193 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002194 }
2195 }
2196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &TB, lz ) );
2198 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( G, &TB ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002199
2200cleanup:
2201
Alexander Ke8ad49f2019-08-16 16:16:07 +03002202 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00002203
2204 return( ret );
2205}
2206
Paul Bakker33dc46b2014-04-30 16:11:39 +02002207/*
2208 * Fill X with size bytes of random.
2209 *
2210 * Use a temporary bytes representation to make sure the result is the same
Paul Bakkerc37b0ac2014-05-01 14:19:23 +02002211 * regardless of the platform endianness (useful when f_rng is actually
Paul Bakker33dc46b2014-04-30 16:11:39 +02002212 * deterministic, eg for tests).
2213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002215 int (*f_rng)(void *, unsigned char *, size_t),
2216 void *p_rng )
Paul Bakker287781a2011-03-26 13:18:49 +00002217{
Janos Follath24eed8d2019-11-22 13:21:35 +00002218 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6dab6202019-01-02 16:42:29 +00002219 size_t const limbs = CHARS_TO_LIMBS( size );
Hanno Beckerda1655a2017-10-18 14:21:44 +01002220 size_t const overhead = ( limbs * ciL ) - size;
2221 unsigned char *Xp;
2222
Hanno Becker8ce11a32018-12-19 16:18:52 +00002223 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002224 MPI_VALIDATE_RET( f_rng != NULL );
Paul Bakker33dc46b2014-04-30 16:11:39 +02002225
Hanno Beckerda1655a2017-10-18 14:21:44 +01002226 /* Ensure that target MPI has exactly the necessary number of limbs */
2227 if( X->n != limbs )
2228 {
2229 mbedtls_mpi_free( X );
2230 mbedtls_mpi_init( X );
2231 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, limbs ) );
2232 }
2233 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
Paul Bakker287781a2011-03-26 13:18:49 +00002234
Hanno Beckerda1655a2017-10-18 14:21:44 +01002235 Xp = (unsigned char*) X->p;
2236 f_rng( p_rng, Xp + overhead, size );
2237
Hanno Becker2be8a552018-10-25 12:40:09 +01002238 mpi_bigendian_to_host( X->p, limbs );
Paul Bakker287781a2011-03-26 13:18:49 +00002239
2240cleanup:
2241 return( ret );
2242}
2243
Paul Bakker5121ce52009-01-03 21:22:43 +00002244/*
2245 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
2246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00002248{
Janos Follath24eed8d2019-11-22 13:21:35 +00002249 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
Hanno Becker73d7d792018-12-11 10:35:51 +00002251 MPI_VALIDATE_RET( X != NULL );
2252 MPI_VALIDATE_RET( A != NULL );
2253 MPI_VALIDATE_RET( N != NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00002254
Hanno Becker4bcb4912017-04-18 15:49:39 +01002255 if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TU ); mbedtls_mpi_init( &U1 ); mbedtls_mpi_init( &U2 );
2259 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &TB ); mbedtls_mpi_init( &TV );
2260 mbedtls_mpi_init( &V1 ); mbedtls_mpi_init( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, A, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264 if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002267 goto cleanup;
2268 }
2269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &TA, A, N ) );
2271 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TU, &TA ) );
2272 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, N ) );
2273 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TV, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U1, 1 ) );
2276 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U2, 0 ) );
2277 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V1, 0 ) );
2278 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002279
2280 do
2281 {
2282 while( ( TU.p[0] & 1 ) == 0 )
2283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TU, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002285
2286 if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
2287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &U1, &U1, &TB ) );
2289 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002290 }
2291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U1, 1 ) );
2293 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002294 }
2295
2296 while( ( TV.p[0] & 1 ) == 0 )
2297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TV, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002299
2300 if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
2301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, &TB ) );
2303 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &TA ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002304 }
2305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V1, 1 ) );
2307 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V2, 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002308 }
2309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 if( mbedtls_mpi_cmp_mpi( &TU, &TV ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TU, &TU, &TV ) );
2313 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U1, &U1, &V1 ) );
2314 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &V2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002315 }
2316 else
2317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TV, &TV, &TU ) );
2319 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, &U1 ) );
2320 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &U2 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002321 }
2322 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 while( mbedtls_mpi_cmp_int( &TU, 0 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 while( mbedtls_mpi_cmp_int( &V1, 0 ) < 0 )
2326 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 while( mbedtls_mpi_cmp_mpi( &V1, N ) >= 0 )
2329 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &V1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002332
2333cleanup:
2334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TU ); mbedtls_mpi_free( &U1 ); mbedtls_mpi_free( &U2 );
2336 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TV );
2337 mbedtls_mpi_free( &V1 ); mbedtls_mpi_free( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002338
2339 return( ret );
2340}
2341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342#if defined(MBEDTLS_GENPRIME)
Paul Bakkerd9374b02012-11-02 11:02:58 +00002343
Paul Bakker5121ce52009-01-03 21:22:43 +00002344static const int small_prime[] =
2345{
2346 3, 5, 7, 11, 13, 17, 19, 23,
2347 29, 31, 37, 41, 43, 47, 53, 59,
2348 61, 67, 71, 73, 79, 83, 89, 97,
2349 101, 103, 107, 109, 113, 127, 131, 137,
2350 139, 149, 151, 157, 163, 167, 173, 179,
2351 181, 191, 193, 197, 199, 211, 223, 227,
2352 229, 233, 239, 241, 251, 257, 263, 269,
2353 271, 277, 281, 283, 293, 307, 311, 313,
2354 317, 331, 337, 347, 349, 353, 359, 367,
2355 373, 379, 383, 389, 397, 401, 409, 419,
2356 421, 431, 433, 439, 443, 449, 457, 461,
2357 463, 467, 479, 487, 491, 499, 503, 509,
2358 521, 523, 541, 547, 557, 563, 569, 571,
2359 577, 587, 593, 599, 601, 607, 613, 617,
2360 619, 631, 641, 643, 647, 653, 659, 661,
2361 673, 677, 683, 691, 701, 709, 719, 727,
2362 733, 739, 743, 751, 757, 761, 769, 773,
2363 787, 797, 809, 811, 821, 823, 827, 829,
2364 839, 853, 857, 859, 863, 877, 881, 883,
2365 887, 907, 911, 919, 929, 937, 941, 947,
2366 953, 967, 971, 977, 983, 991, 997, -103
2367};
2368
2369/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002370 * Small divisors test (X must be positive)
2371 *
2372 * Return values:
2373 * 0: no small factor (possible prime, more tests needed)
2374 * 1: certain prime
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002376 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00002377 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378static int mpi_check_small_factors( const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +00002379{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002380 int ret = 0;
2381 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 mbedtls_mpi_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +00002383
Paul Bakker5121ce52009-01-03 21:22:43 +00002384 if( ( X->p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002386
2387 for( i = 0; small_prime[i] > 0; i++ )
2388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389 if( mbedtls_mpi_cmp_int( X, small_prime[i] ) <= 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002390 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, small_prime[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002393
2394 if( r == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 }
2397
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002398cleanup:
2399 return( ret );
2400}
2401
2402/*
2403 * Miller-Rabin pseudo-primality test (HAC 4.24)
2404 */
Janos Follathda31fa12018-09-03 14:45:23 +01002405static int mpi_miller_rabin( const mbedtls_mpi *X, size_t rounds,
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002406 int (*f_rng)(void *, unsigned char *, size_t),
2407 void *p_rng )
2408{
Pascal Junodb99183d2015-03-11 16:49:45 +01002409 int ret, count;
Janos Follathda31fa12018-09-03 14:45:23 +01002410 size_t i, j, k, s;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 mbedtls_mpi W, R, T, A, RR;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002412
Hanno Becker8ce11a32018-12-19 16:18:52 +00002413 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002414 MPI_VALIDATE_RET( f_rng != NULL );
2415
2416 mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R );
2417 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 mbedtls_mpi_init( &RR );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002419
Paul Bakker5121ce52009-01-03 21:22:43 +00002420 /*
2421 * W = |X| - 1
2422 * R = W >> lsb( W )
2423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &W, X, 1 ) );
2425 s = mbedtls_mpi_lsb( &W );
2426 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R, &W ) );
2427 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &R, s ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002428
Janos Follathda31fa12018-09-03 14:45:23 +01002429 for( i = 0; i < rounds; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00002430 {
2431 /*
2432 * pick a random A, 1 < A < |X| - 1
2433 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002434 count = 0;
2435 do {
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002436 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
Pascal Junodb99183d2015-03-11 16:49:45 +01002437
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002438 j = mbedtls_mpi_bitlen( &A );
2439 k = mbedtls_mpi_bitlen( &W );
Pascal Junodb99183d2015-03-11 16:49:45 +01002440 if (j > k) {
Darryl Greene3f95ed2018-10-02 13:21:35 +01002441 A.p[A.n - 1] &= ( (mbedtls_mpi_uint) 1 << ( k - ( A.n - 1 ) * biL - 1 ) ) - 1;
Pascal Junodb99183d2015-03-11 16:49:45 +01002442 }
2443
2444 if (count++ > 30) {
Jens Wiklanderf08aa3e2019-01-17 13:30:57 +01002445 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
2446 goto cleanup;
Pascal Junodb99183d2015-03-11 16:49:45 +01002447 }
2448
Manuel Pégourié-Gonnard53c76c02015-04-17 20:15:36 +02002449 } while ( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 ||
2450 mbedtls_mpi_cmp_int( &A, 1 ) <= 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002451
2452 /*
2453 * A = A^R mod |X|
2454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &A, &A, &R, X, &RR ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457 if( mbedtls_mpi_cmp_mpi( &A, &W ) == 0 ||
2458 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002459 continue;
2460
2461 j = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 while( j < s && mbedtls_mpi_cmp_mpi( &A, &W ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002463 {
2464 /*
2465 * A = A * A mod |X|
2466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &A, &A ) );
2468 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &A, &T, X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470 if( mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002471 break;
2472
2473 j++;
2474 }
2475
2476 /*
2477 * not prime if A != |X| - 1 or A == 1
2478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 if( mbedtls_mpi_cmp_mpi( &A, &W ) != 0 ||
2480 mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482 ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002483 break;
2484 }
2485 }
2486
2487cleanup:
Hanno Becker73d7d792018-12-11 10:35:51 +00002488 mbedtls_mpi_free( &W ); mbedtls_mpi_free( &R );
2489 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 mbedtls_mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002491
2492 return( ret );
2493}
2494
2495/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002496 * Pseudo-primality test: small factors, then Miller-Rabin
2497 */
Janos Follatha0b67c22018-09-18 14:48:23 +01002498int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds,
2499 int (*f_rng)(void *, unsigned char *, size_t),
2500 void *p_rng )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002501{
Janos Follath24eed8d2019-11-22 13:21:35 +00002502 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 mbedtls_mpi XX;
Hanno Becker8ce11a32018-12-19 16:18:52 +00002504 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002505 MPI_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002506
2507 XX.s = 1;
2508 XX.n = X->n;
2509 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 if( mbedtls_mpi_cmp_int( &XX, 0 ) == 0 ||
2512 mbedtls_mpi_cmp_int( &XX, 1 ) == 0 )
2513 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 if( mbedtls_mpi_cmp_int( &XX, 2 ) == 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002516 return( 0 );
2517
2518 if( ( ret = mpi_check_small_factors( &XX ) ) != 0 )
2519 {
2520 if( ret == 1 )
2521 return( 0 );
2522
2523 return( ret );
2524 }
2525
Janos Follathda31fa12018-09-03 14:45:23 +01002526 return( mpi_miller_rabin( &XX, rounds, f_rng, p_rng ) );
Janos Follathf301d232018-08-14 13:34:01 +01002527}
2528
Janos Follatha0b67c22018-09-18 14:48:23 +01002529#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Janos Follathf301d232018-08-14 13:34:01 +01002530/*
2531 * Pseudo-primality test, error probability 2^-80
2532 */
2533int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
2534 int (*f_rng)(void *, unsigned char *, size_t),
2535 void *p_rng )
2536{
Hanno Becker8ce11a32018-12-19 16:18:52 +00002537 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002538 MPI_VALIDATE_RET( f_rng != NULL );
2539
Janos Follatha0b67c22018-09-18 14:48:23 +01002540 /*
2541 * In the past our key generation aimed for an error rate of at most
2542 * 2^-80. Since this function is deprecated, aim for the same certainty
2543 * here as well.
2544 */
Hanno Becker73d7d792018-12-11 10:35:51 +00002545 return( mbedtls_mpi_is_prime_ext( X, 40, f_rng, p_rng ) );
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002546}
Janos Follatha0b67c22018-09-18 14:48:23 +01002547#endif
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002548
2549/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 * Prime number generation
Jethro Beekman66689272018-02-14 19:24:10 -08002551 *
Janos Follathf301d232018-08-14 13:34:01 +01002552 * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
2553 * be either 1024 bits or 1536 bits long, and flags must contain
2554 * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
Paul Bakker5121ce52009-01-03 21:22:43 +00002555 */
Janos Follath7c025a92018-08-14 11:08:41 +01002556int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002557 int (*f_rng)(void *, unsigned char *, size_t),
2558 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002559{
Jethro Beekman66689272018-02-14 19:24:10 -08002560#ifdef MBEDTLS_HAVE_INT64
2561// ceil(2^63.5)
2562#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
2563#else
2564// ceil(2^31.5)
2565#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
2566#endif
2567 int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002568 size_t k, n;
Janos Follathda31fa12018-09-03 14:45:23 +01002569 int rounds;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 mbedtls_mpi_uint r;
2571 mbedtls_mpi Y;
Paul Bakker5121ce52009-01-03 21:22:43 +00002572
Hanno Becker8ce11a32018-12-19 16:18:52 +00002573 MPI_VALIDATE_RET( X != NULL );
Hanno Becker73d7d792018-12-11 10:35:51 +00002574 MPI_VALIDATE_RET( f_rng != NULL );
2575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 if( nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS )
2577 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579 mbedtls_mpi_init( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002580
2581 n = BITS_TO_LIMBS( nbits );
2582
Janos Follathda31fa12018-09-03 14:45:23 +01002583 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR ) == 0 )
2584 {
2585 /*
2586 * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
2587 */
2588 rounds = ( ( nbits >= 1300 ) ? 2 : ( nbits >= 850 ) ? 3 :
2589 ( nbits >= 650 ) ? 4 : ( nbits >= 350 ) ? 8 :
2590 ( nbits >= 250 ) ? 12 : ( nbits >= 150 ) ? 18 : 27 );
2591 }
2592 else
2593 {
2594 /*
2595 * 2^-100 error probability, number of rounds computed based on HAC,
2596 * fact 4.48
2597 */
2598 rounds = ( ( nbits >= 1450 ) ? 4 : ( nbits >= 1150 ) ? 5 :
2599 ( nbits >= 1000 ) ? 6 : ( nbits >= 850 ) ? 7 :
2600 ( nbits >= 750 ) ? 8 : ( nbits >= 500 ) ? 13 :
2601 ( nbits >= 250 ) ? 28 : ( nbits >= 150 ) ? 40 : 51 );
2602 }
2603
Jethro Beekman66689272018-02-14 19:24:10 -08002604 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002605 {
Jethro Beekman66689272018-02-14 19:24:10 -08002606 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
2607 /* make sure generated number is at least (nbits-1)+0.5 bits (FIPS 186-4 §B.3.3 steps 4.4, 5.5) */
2608 if( X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2 ) continue;
2609
2610 k = n * biL;
2611 if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits ) );
2612 X->p[0] |= 1;
2613
Janos Follath7c025a92018-08-14 11:08:41 +01002614 if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 {
Janos Follatha0b67c22018-09-18 14:48:23 +01002616 ret = mbedtls_mpi_is_prime_ext( X, rounds, f_rng, p_rng );
Jethro Beekman66689272018-02-14 19:24:10 -08002617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002618 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002620 }
Jethro Beekman66689272018-02-14 19:24:10 -08002621 else
Paul Bakker5121ce52009-01-03 21:22:43 +00002622 {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002623 /*
Jethro Beekman66689272018-02-14 19:24:10 -08002624 * An necessary condition for Y and X = 2Y + 1 to be prime
2625 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2626 * Make sure it is satisfied, while keeping X = 3 mod 4
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002627 */
Jethro Beekman66689272018-02-14 19:24:10 -08002628
2629 X->p[0] |= 2;
2630
2631 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, 3 ) );
2632 if( r == 0 )
2633 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 8 ) );
2634 else if( r == 1 )
2635 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 4 ) );
2636
2637 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
2638 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, X ) );
2639 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, 1 ) );
2640
2641 while( 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002642 {
Jethro Beekman66689272018-02-14 19:24:10 -08002643 /*
2644 * First, check small factors for X and Y
2645 * before doing Miller-Rabin on any of them
2646 */
2647 if( ( ret = mpi_check_small_factors( X ) ) == 0 &&
2648 ( ret = mpi_check_small_factors( &Y ) ) == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002649 ( ret = mpi_miller_rabin( X, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002650 == 0 &&
Janos Follathda31fa12018-09-03 14:45:23 +01002651 ( ret = mpi_miller_rabin( &Y, rounds, f_rng, p_rng ) )
Janos Follathf301d232018-08-14 13:34:01 +01002652 == 0 )
Jethro Beekman66689272018-02-14 19:24:10 -08002653 goto cleanup;
2654
2655 if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
2656 goto cleanup;
2657
2658 /*
2659 * Next candidates. We want to preserve Y = (X-1) / 2 and
2660 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2661 * so up Y by 6 and X by 12.
2662 */
2663 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 12 ) );
2664 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &Y, &Y, 6 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002665 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002666 }
2667 }
2668
2669cleanup:
2670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 mbedtls_mpi_free( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002672
2673 return( ret );
2674}
2675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002679
Paul Bakker23986e52011-04-24 08:57:21 +00002680#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002681
2682static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2683{
2684 { 693, 609, 21 },
2685 { 1764, 868, 28 },
2686 { 768454923, 542167814, 1 }
2687};
2688
Paul Bakker5121ce52009-01-03 21:22:43 +00002689/*
2690 * Checkup routine
2691 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692int mbedtls_mpi_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00002693{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002694 int ret, i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 mbedtls_mpi A, E, N, X, Y, U, V;
Paul Bakker5121ce52009-01-03 21:22:43 +00002696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); mbedtls_mpi_init( &X );
2698 mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &U ); mbedtls_mpi_init( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &A, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002701 "EFE021C2645FD1DC586E69184AF4A31E" \
2702 "D5F53E93B5F123FA41680867BA110131" \
2703 "944FE7952E2517337780CB0DB80E61AA" \
2704 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6" ) );
2705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 "B2E7EFD37075B9F03FF989C7C5051C20" \
2708 "34D2A323810251127E7BF8625A4F49A5" \
2709 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
2710 "5B5C25763222FEFCCFC38B832366C29E" ) );
2711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &N, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002713 "0066A198186C18C10B2F5ED9B522752A" \
2714 "9830B69916E535C8F047518A889A43A5" \
2715 "94B6BED27A168D31D4A52F88925AA8F5" ) );
2716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 "602AB7ECA597A3D6B56FF9829A5E8B85" \
2721 "9E857EA95A03512E2BAE7391688D264A" \
2722 "A5663B0341DB9CCFD2C4C5F421FEC814" \
2723 "8001B72E848A38CAE1C65F78E56ABDEF" \
2724 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
2725 "ECF677152EF804370C1A305CAF3B5BF1" \
2726 "30879B56C61DE584A0F53A2447A51E" ) );
2727
2728 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729 mbedtls_printf( " MPI test #1 (mul_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002732 {
2733 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002735
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002736 ret = 1;
2737 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002738 }
2739
2740 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &X, &Y, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002746 "256567336059E52CAE22925474705F39A94" ) );
2747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &V, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002749 "6613F26162223DF488E9CD48CC132C7A" \
2750 "0AC93C701B001B092E4E5B9F73BCD27B" \
2751 "9EE50D0657C77F374E903CDFA4C642" ) );
2752
2753 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 mbedtls_printf( " MPI test #2 (div_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ||
2757 mbedtls_mpi_cmp_mpi( &Y, &V ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002758 {
2759 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002761
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002762 ret = 1;
2763 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002764 }
2765
2766 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002769 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &X, &A, &E, &N, NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002772 "36E139AEA55215609D2816998ED020BB" \
2773 "BD96C37890F65171D948E9BC7CBAA4D9" \
2774 "325D24D6A3C12710F10A09FA08AB87" ) );
2775
2776 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 mbedtls_printf( " MPI test #3 (exp_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002780 {
2781 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002783
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002784 ret = 1;
2785 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002786 }
2787
2788 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &X, &A, &N ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
Paul Bakker5121ce52009-01-03 21:22:43 +00002794 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
2795 "C3DBA76456363A10869622EAC2DD84EC" \
2796 "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) );
2797
2798 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799 mbedtls_printf( " MPI test #4 (inv_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801 if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002802 {
2803 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002804 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002805
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002806 ret = 1;
2807 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002808 }
2809
2810 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002812
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002813 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814 mbedtls_printf( " MPI test #5 (simple gcd): " );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002815
Paul Bakker66d5d072014-06-17 16:39:18 +02002816 for( i = 0; i < GCD_PAIR_COUNT; i++ )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &X, gcd_pairs[i][0] ) );
2819 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Y, gcd_pairs[i][1] ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &A, &X, &Y ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 if( mbedtls_mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 )
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002824 {
2825 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826 mbedtls_printf( "failed at %d\n", i );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002827
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002828 ret = 1;
2829 goto cleanup;
2830 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002831 }
2832
2833 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002834 mbedtls_printf( "passed\n" );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002835
Paul Bakker5121ce52009-01-03 21:22:43 +00002836cleanup:
2837
2838 if( ret != 0 && verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839 mbedtls_printf( "Unexpected error, return code = %08X\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002841 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &X );
2842 mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &U ); mbedtls_mpi_free( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00002843
2844 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002846
2847 return( ret );
2848}
2849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852#endif /* MBEDTLS_BIGNUM_C */