blob: 12c72af3a49d40b661a6da1cac37d281c5dbcc53 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Multi-precision integer library
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * This MPI implementation is based on:
24 *
25 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
26 * http://www.stillhq.com/extracted/gnupg-api/mpi/
27 * http://math.libtomcrypt.com/files/tommath.pdf
28 */
29
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
33#include POLARSSL_CONFIG_FILE
34#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Paul Bakker40e46942009-01-03 21:51:57 +000036#if defined(POLARSSL_BIGNUM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Paul Bakker40e46942009-01-03 21:51:57 +000038#include "polarssl/bignum.h"
39#include "polarssl/bn_mul.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42
Paul Bakker7dc4c442014-02-01 22:50:26 +010043#if defined(POLARSSL_PLATFORM_C)
44#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020045#else
Rich Evans00ab4702015-02-06 13:43:58 +000046#include <stdio.h>
47#include <stdlib.h>
Paul Bakker7dc4c442014-02-01 22:50:26 +010048#define polarssl_printf printf
Paul Bakker6e339b52013-07-03 13:37:05 +020049#define polarssl_malloc malloc
50#define polarssl_free free
51#endif
52
Paul Bakker34617722014-06-13 17:20:13 +020053/* Implementation that should never be optimized out by the compiler */
54static void polarssl_zeroize( void *v, size_t n ) {
55 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
56}
57
Paul Bakkerf9688572011-05-05 10:00:45 +000058#define ciL (sizeof(t_uint)) /* chars in limb */
Paul Bakker5121ce52009-01-03 21:22:43 +000059#define biL (ciL << 3) /* bits in limb */
60#define biH (ciL << 2) /* half limb size */
61
62/*
63 * Convert between bits/chars and number of limbs
64 */
65#define BITS_TO_LIMBS(i) (((i) + biL - 1) / biL)
66#define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
67
68/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000069 * Initialize one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000070 */
Paul Bakker6c591fa2011-05-05 11:49:20 +000071void mpi_init( mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +000072{
Paul Bakker6c591fa2011-05-05 11:49:20 +000073 if( X == NULL )
74 return;
Paul Bakker5121ce52009-01-03 21:22:43 +000075
Paul Bakker6c591fa2011-05-05 11:49:20 +000076 X->s = 1;
77 X->n = 0;
78 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000079}
80
81/*
Paul Bakker6c591fa2011-05-05 11:49:20 +000082 * Unallocate one MPI
Paul Bakker5121ce52009-01-03 21:22:43 +000083 */
Paul Bakker6c591fa2011-05-05 11:49:20 +000084void mpi_free( mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +000085{
Paul Bakker6c591fa2011-05-05 11:49:20 +000086 if( X == NULL )
87 return;
Paul Bakker5121ce52009-01-03 21:22:43 +000088
Paul Bakker6c591fa2011-05-05 11:49:20 +000089 if( X->p != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000090 {
Paul Bakker34617722014-06-13 17:20:13 +020091 polarssl_zeroize( X->p, X->n * ciL );
Paul Bakker6e339b52013-07-03 13:37:05 +020092 polarssl_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +000093 }
94
Paul Bakker6c591fa2011-05-05 11:49:20 +000095 X->s = 1;
96 X->n = 0;
97 X->p = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000098}
99
100/*
101 * Enlarge to the specified number of limbs
102 */
Paul Bakker23986e52011-04-24 08:57:21 +0000103int mpi_grow( mpi *X, size_t nblimbs )
Paul Bakker5121ce52009-01-03 21:22:43 +0000104{
Paul Bakkera755ca12011-04-24 09:11:17 +0000105 t_uint *p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
Paul Bakkerf9688572011-05-05 10:00:45 +0000107 if( nblimbs > POLARSSL_MPI_MAX_LIMBS )
Paul Bakker69e095c2011-12-10 21:55:01 +0000108 return( POLARSSL_ERR_MPI_MALLOC_FAILED );
Paul Bakkerf9688572011-05-05 10:00:45 +0000109
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 if( X->n < nblimbs )
111 {
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500112 if( ( p = polarssl_malloc( nblimbs * ciL ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000113 return( POLARSSL_ERR_MPI_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
115 memset( p, 0, nblimbs * ciL );
116
117 if( X->p != NULL )
118 {
119 memcpy( p, X->p, X->n * ciL );
Paul Bakker34617722014-06-13 17:20:13 +0200120 polarssl_zeroize( X->p, X->n * ciL );
Paul Bakker6e339b52013-07-03 13:37:05 +0200121 polarssl_free( X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 }
123
124 X->n = nblimbs;
125 X->p = p;
126 }
127
128 return( 0 );
129}
130
131/*
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100132 * Resize down as much as possible,
133 * while keeping at least the specified number of limbs
134 */
135int mpi_shrink( mpi *X, size_t nblimbs )
136{
137 t_uint *p;
138 size_t i;
139
140 /* Actually resize up in this case */
141 if( X->n <= nblimbs )
142 return( mpi_grow( X, nblimbs ) );
143
144 for( i = X->n - 1; i > 0; i-- )
145 if( X->p[i] != 0 )
146 break;
147 i++;
148
149 if( i < nblimbs )
150 i = nblimbs;
151
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500152 if( ( p = polarssl_malloc( i * ciL ) ) == NULL )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100153 return( POLARSSL_ERR_MPI_MALLOC_FAILED );
154
155 memset( p, 0, i * ciL );
156
157 if( X->p != NULL )
158 {
159 memcpy( p, X->p, i * ciL );
Paul Bakker34617722014-06-13 17:20:13 +0200160 polarssl_zeroize( X->p, X->n * ciL );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100161 polarssl_free( X->p );
162 }
163
164 X->n = i;
165 X->p = p;
166
167 return( 0 );
168}
169
170/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000171 * Copy the contents of Y into X
172 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000173int mpi_copy( mpi *X, const mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000174{
Paul Bakker23986e52011-04-24 08:57:21 +0000175 int ret;
176 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000177
178 if( X == Y )
179 return( 0 );
180
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200181 if( Y->p == NULL )
182 {
183 mpi_free( X );
184 return( 0 );
185 }
186
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 for( i = Y->n - 1; i > 0; i-- )
188 if( Y->p[i] != 0 )
189 break;
190 i++;
191
192 X->s = Y->s;
193
194 MPI_CHK( mpi_grow( X, i ) );
195
196 memset( X->p, 0, X->n * ciL );
197 memcpy( X->p, Y->p, i * ciL );
198
199cleanup:
200
201 return( ret );
202}
203
204/*
205 * Swap the contents of X and Y
206 */
207void mpi_swap( mpi *X, mpi *Y )
208{
209 mpi T;
210
211 memcpy( &T, X, sizeof( mpi ) );
212 memcpy( X, Y, sizeof( mpi ) );
213 memcpy( Y, &T, sizeof( mpi ) );
214}
215
216/*
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100217 * Conditionally assign X = Y, without leaking information
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +0100218 * about whether the assignment was made or not.
219 * (Leaking information about the respective sizes of X and Y is ok however.)
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100220 */
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +0100221int mpi_safe_cond_assign( mpi *X, const mpi *Y, unsigned char assign )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100222{
223 int ret = 0;
224 size_t i;
225
Pascal Junodb99183d2015-03-11 16:49:45 +0100226 /* make sure assign is 0 or 1 in a time-constant manner */
227 assign = (assign | (unsigned char)-assign) >> 7;
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100228
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100229 MPI_CHK( mpi_grow( X, Y->n ) );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100230
Paul Bakker66d5d072014-06-17 16:39:18 +0200231 X->s = X->s * ( 1 - assign ) + Y->s * assign;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100232
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +0100233 for( i = 0; i < Y->n; i++ )
Paul Bakker66d5d072014-06-17 16:39:18 +0200234 X->p[i] = X->p[i] * ( 1 - assign ) + Y->p[i] * assign;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100235
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +0100236 for( ; i < X->n; i++ )
Paul Bakker66d5d072014-06-17 16:39:18 +0200237 X->p[i] *= ( 1 - assign );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100238
239cleanup:
240 return( ret );
241}
242
243/*
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100244 * Conditionally swap X and Y, without leaking information
245 * about whether the swap was made or not.
246 * Here it is not ok to simply swap the pointers, which whould lead to
247 * different memory access patterns when X and Y are used afterwards.
248 */
249int mpi_safe_cond_swap( mpi *X, mpi *Y, unsigned char swap )
250{
251 int ret, s;
252 size_t i;
253 t_uint tmp;
254
255 if( X == Y )
256 return( 0 );
257
Pascal Junodb99183d2015-03-11 16:49:45 +0100258 /* make sure swap is 0 or 1 in a time-constant manner */
259 swap = (swap | (unsigned char)-swap) >> 7;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100260
261 MPI_CHK( mpi_grow( X, Y->n ) );
262 MPI_CHK( mpi_grow( Y, X->n ) );
263
264 s = X->s;
Paul Bakker66d5d072014-06-17 16:39:18 +0200265 X->s = X->s * ( 1 - swap ) + Y->s * swap;
266 Y->s = Y->s * ( 1 - swap ) + s * swap;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100267
268
269 for( i = 0; i < X->n; i++ )
270 {
271 tmp = X->p[i];
Paul Bakker66d5d072014-06-17 16:39:18 +0200272 X->p[i] = X->p[i] * ( 1 - swap ) + Y->p[i] * swap;
273 Y->p[i] = Y->p[i] * ( 1 - swap ) + tmp * swap;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100274 }
275
276cleanup:
277 return( ret );
278}
279
280/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000281 * Set value from integer
282 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000283int mpi_lset( mpi *X, t_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +0000284{
285 int ret;
286
287 MPI_CHK( mpi_grow( X, 1 ) );
288 memset( X->p, 0, X->n * ciL );
289
290 X->p[0] = ( z < 0 ) ? -z : z;
291 X->s = ( z < 0 ) ? -1 : 1;
292
293cleanup:
294
295 return( ret );
296}
297
298/*
Paul Bakker2f5947e2011-05-18 15:47:11 +0000299 * Get a specific bit
300 */
Paul Bakker6b906e52012-05-08 12:01:43 +0000301int mpi_get_bit( const mpi *X, size_t pos )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000302{
303 if( X->n * biL <= pos )
304 return( 0 );
305
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200306 return( ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000307}
308
309/*
310 * Set a bit to a specific value of 0 or 1
311 */
312int mpi_set_bit( mpi *X, size_t pos, unsigned char val )
313{
314 int ret = 0;
315 size_t off = pos / biL;
316 size_t idx = pos % biL;
317
318 if( val != 0 && val != 1 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200319 return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker9af723c2014-05-01 13:03:14 +0200320
Paul Bakker2f5947e2011-05-18 15:47:11 +0000321 if( X->n * biL <= pos )
322 {
323 if( val == 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200324 return( 0 );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000325
326 MPI_CHK( mpi_grow( X, off + 1 ) );
327 }
328
Manuel Pégourié-Gonnard9a4a5ac2013-12-04 18:05:29 +0100329 X->p[off] &= ~( (t_uint) 0x01 << idx );
330 X->p[off] |= (t_uint) val << idx;
Paul Bakker2f5947e2011-05-18 15:47:11 +0000331
332cleanup:
Paul Bakker9af723c2014-05-01 13:03:14 +0200333
Paul Bakker2f5947e2011-05-18 15:47:11 +0000334 return( ret );
335}
336
337/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000338 * Return the number of least significant bits
339 */
Paul Bakker23986e52011-04-24 08:57:21 +0000340size_t mpi_lsb( const mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000341{
Paul Bakker23986e52011-04-24 08:57:21 +0000342 size_t i, j, count = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000343
344 for( i = 0; i < X->n; i++ )
Paul Bakkerf9688572011-05-05 10:00:45 +0000345 for( j = 0; j < biL; j++, count++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000346 if( ( ( X->p[i] >> j ) & 1 ) != 0 )
347 return( count );
348
349 return( 0 );
350}
351
352/*
353 * Return the number of most significant bits
354 */
Paul Bakker23986e52011-04-24 08:57:21 +0000355size_t mpi_msb( const mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000356{
Paul Bakker23986e52011-04-24 08:57:21 +0000357 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +0000358
359 for( i = X->n - 1; i > 0; i-- )
360 if( X->p[i] != 0 )
361 break;
362
Paul Bakker23986e52011-04-24 08:57:21 +0000363 for( j = biL; j > 0; j-- )
364 if( ( ( X->p[i] >> ( j - 1 ) ) & 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000365 break;
366
Paul Bakker23986e52011-04-24 08:57:21 +0000367 return( ( i * biL ) + j );
Paul Bakker5121ce52009-01-03 21:22:43 +0000368}
369
370/*
371 * Return the total size in bytes
372 */
Paul Bakker23986e52011-04-24 08:57:21 +0000373size_t mpi_size( const mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000374{
375 return( ( mpi_msb( X ) + 7 ) >> 3 );
376}
377
378/*
379 * Convert an ASCII character to digit value
380 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000381static int mpi_get_digit( t_uint *d, int radix, char c )
Paul Bakker5121ce52009-01-03 21:22:43 +0000382{
383 *d = 255;
384
385 if( c >= 0x30 && c <= 0x39 ) *d = c - 0x30;
386 if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37;
387 if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57;
388
Paul Bakkera755ca12011-04-24 09:11:17 +0000389 if( *d >= (t_uint) radix )
Paul Bakker40e46942009-01-03 21:51:57 +0000390 return( POLARSSL_ERR_MPI_INVALID_CHARACTER );
Paul Bakker5121ce52009-01-03 21:22:43 +0000391
392 return( 0 );
393}
394
395/*
396 * Import from an ASCII string
397 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000398int mpi_read_string( mpi *X, int radix, const char *s )
Paul Bakker5121ce52009-01-03 21:22:43 +0000399{
Paul Bakker23986e52011-04-24 08:57:21 +0000400 int ret;
401 size_t i, j, slen, n;
Paul Bakkera755ca12011-04-24 09:11:17 +0000402 t_uint d;
Paul Bakker5121ce52009-01-03 21:22:43 +0000403 mpi T;
404
405 if( radix < 2 || radix > 16 )
Paul Bakker40e46942009-01-03 21:51:57 +0000406 return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000407
Paul Bakker6c591fa2011-05-05 11:49:20 +0000408 mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000409
Paul Bakkerff60ee62010-03-16 21:09:09 +0000410 slen = strlen( s );
411
Paul Bakker5121ce52009-01-03 21:22:43 +0000412 if( radix == 16 )
413 {
Paul Bakkerff60ee62010-03-16 21:09:09 +0000414 n = BITS_TO_LIMBS( slen << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000415
416 MPI_CHK( mpi_grow( X, n ) );
417 MPI_CHK( mpi_lset( X, 0 ) );
418
Paul Bakker23986e52011-04-24 08:57:21 +0000419 for( i = slen, j = 0; i > 0; i--, j++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 {
Paul Bakker23986e52011-04-24 08:57:21 +0000421 if( i == 1 && s[i - 1] == '-' )
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 {
423 X->s = -1;
424 break;
425 }
426
Paul Bakker23986e52011-04-24 08:57:21 +0000427 MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200428 X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 }
430 }
431 else
432 {
433 MPI_CHK( mpi_lset( X, 0 ) );
434
Paul Bakkerff60ee62010-03-16 21:09:09 +0000435 for( i = 0; i < slen; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000436 {
437 if( i == 0 && s[i] == '-' )
438 {
439 X->s = -1;
440 continue;
441 }
442
443 MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
444 MPI_CHK( mpi_mul_int( &T, X, radix ) );
Paul Bakker05feca62009-06-20 08:22:43 +0000445
446 if( X->s == 1 )
447 {
448 MPI_CHK( mpi_add_int( X, &T, d ) );
449 }
450 else
451 {
452 MPI_CHK( mpi_sub_int( X, &T, d ) );
453 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 }
455 }
456
457cleanup:
458
Paul Bakker6c591fa2011-05-05 11:49:20 +0000459 mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461 return( ret );
462}
463
464/*
465 * Helper to write the digits high-order first
466 */
467static int mpi_write_hlp( mpi *X, int radix, char **p )
468{
469 int ret;
Paul Bakkera755ca12011-04-24 09:11:17 +0000470 t_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +0000471
472 if( radix < 2 || radix > 16 )
Paul Bakker40e46942009-01-03 21:51:57 +0000473 return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000474
475 MPI_CHK( mpi_mod_int( &r, X, radix ) );
476 MPI_CHK( mpi_div_int( X, NULL, X, radix ) );
477
478 if( mpi_cmp_int( X, 0 ) != 0 )
479 MPI_CHK( mpi_write_hlp( X, radix, p ) );
480
481 if( r < 10 )
482 *(*p)++ = (char)( r + 0x30 );
483 else
484 *(*p)++ = (char)( r + 0x37 );
485
486cleanup:
487
488 return( ret );
489}
490
491/*
492 * Export into an ASCII string
493 */
Paul Bakker23986e52011-04-24 08:57:21 +0000494int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000495{
Paul Bakker23986e52011-04-24 08:57:21 +0000496 int ret = 0;
497 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000498 char *p;
499 mpi T;
500
501 if( radix < 2 || radix > 16 )
Paul Bakker40e46942009-01-03 21:51:57 +0000502 return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000503
504 n = mpi_msb( X );
505 if( radix >= 4 ) n >>= 1;
506 if( radix >= 16 ) n >>= 1;
507 n += 3;
508
509 if( *slen < n )
510 {
511 *slen = n;
Paul Bakker40e46942009-01-03 21:51:57 +0000512 return( POLARSSL_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 }
514
515 p = s;
Paul Bakker6c591fa2011-05-05 11:49:20 +0000516 mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000517
518 if( X->s == -1 )
519 *p++ = '-';
520
521 if( radix == 16 )
522 {
Paul Bakker23986e52011-04-24 08:57:21 +0000523 int c;
524 size_t i, j, k;
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
Paul Bakker23986e52011-04-24 08:57:21 +0000526 for( i = X->n, k = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000527 {
Paul Bakker23986e52011-04-24 08:57:21 +0000528 for( j = ciL; j > 0; j-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 {
Paul Bakker23986e52011-04-24 08:57:21 +0000530 c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF;
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
Paul Bakker6c343d72014-07-10 14:36:19 +0200532 if( c == 0 && k == 0 && ( i + j ) != 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 continue;
534
Paul Bakker98fe5ea2012-10-24 11:17:48 +0000535 *(p++) = "0123456789ABCDEF" [c / 16];
Paul Bakkerd2c167e2012-10-30 07:49:19 +0000536 *(p++) = "0123456789ABCDEF" [c % 16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 k = 1;
538 }
539 }
540 }
541 else
542 {
543 MPI_CHK( mpi_copy( &T, X ) );
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000544
545 if( T.s == -1 )
546 T.s = 1;
547
Paul Bakker5121ce52009-01-03 21:22:43 +0000548 MPI_CHK( mpi_write_hlp( &T, radix, &p ) );
549 }
550
551 *p++ = '\0';
552 *slen = p - s;
553
554cleanup:
555
Paul Bakker6c591fa2011-05-05 11:49:20 +0000556 mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
558 return( ret );
559}
560
Paul Bakker335db3f2011-04-25 15:28:35 +0000561#if defined(POLARSSL_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000562/*
563 * Read X from an opened file
564 */
565int mpi_read_file( mpi *X, int radix, FILE *fin )
566{
Paul Bakkera755ca12011-04-24 09:11:17 +0000567 t_uint d;
Paul Bakker23986e52011-04-24 08:57:21 +0000568 size_t slen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 char *p;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000570 /*
Paul Bakkercb37aa52011-11-30 16:00:20 +0000571 * Buffer should have space for (short) label and decimal formatted MPI,
572 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000573 */
Paul Bakker5531c6d2012-09-26 19:20:46 +0000574 char s[ POLARSSL_MPI_RW_BUFFER_SIZE ];
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
576 memset( s, 0, sizeof( s ) );
577 if( fgets( s, sizeof( s ) - 1, fin ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000578 return( POLARSSL_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000579
580 slen = strlen( s );
Paul Bakkercb37aa52011-11-30 16:00:20 +0000581 if( slen == sizeof( s ) - 2 )
582 return( POLARSSL_ERR_MPI_BUFFER_TOO_SMALL );
583
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 if( s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
585 if( s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
586
587 p = s + slen;
588 while( --p >= s )
589 if( mpi_get_digit( &d, radix, *p ) != 0 )
590 break;
591
592 return( mpi_read_string( X, radix, p + 1 ) );
593}
594
595/*
596 * Write X into an opened file (or stdout if fout == NULL)
597 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000598int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout )
Paul Bakker5121ce52009-01-03 21:22:43 +0000599{
Paul Bakker23986e52011-04-24 08:57:21 +0000600 int ret;
601 size_t n, slen, plen;
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000602 /*
Paul Bakker5531c6d2012-09-26 19:20:46 +0000603 * Buffer should have space for (short) label and decimal formatted MPI,
604 * newline characters and '\0'
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000605 */
Paul Bakker5531c6d2012-09-26 19:20:46 +0000606 char s[ POLARSSL_MPI_RW_BUFFER_SIZE ];
Paul Bakker5121ce52009-01-03 21:22:43 +0000607
608 n = sizeof( s );
609 memset( s, 0, n );
610 n -= 2;
611
Paul Bakker23986e52011-04-24 08:57:21 +0000612 MPI_CHK( mpi_write_string( X, radix, s, (size_t *) &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000613
614 if( p == NULL ) p = "";
615
616 plen = strlen( p );
617 slen = strlen( s );
618 s[slen++] = '\r';
619 s[slen++] = '\n';
620
621 if( fout != NULL )
622 {
623 if( fwrite( p, 1, plen, fout ) != plen ||
624 fwrite( s, 1, slen, fout ) != slen )
Paul Bakker40e46942009-01-03 21:51:57 +0000625 return( POLARSSL_ERR_MPI_FILE_IO_ERROR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000626 }
627 else
Paul Bakker7dc4c442014-02-01 22:50:26 +0100628 polarssl_printf( "%s%s", p, s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
630cleanup:
631
632 return( ret );
633}
Paul Bakker335db3f2011-04-25 15:28:35 +0000634#endif /* POLARSSL_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
636/*
637 * Import X from unsigned binary data, big endian
638 */
Paul Bakker23986e52011-04-24 08:57:21 +0000639int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000640{
Paul Bakker23986e52011-04-24 08:57:21 +0000641 int ret;
642 size_t i, j, n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000643
644 for( n = 0; n < buflen; n++ )
645 if( buf[n] != 0 )
646 break;
647
648 MPI_CHK( mpi_grow( X, CHARS_TO_LIMBS( buflen - n ) ) );
649 MPI_CHK( mpi_lset( X, 0 ) );
650
Paul Bakker23986e52011-04-24 08:57:21 +0000651 for( i = buflen, j = 0; i > n; i--, j++ )
Paul Bakkera755ca12011-04-24 09:11:17 +0000652 X->p[j / ciL] |= ((t_uint) buf[i - 1]) << ((j % ciL) << 3);
Paul Bakker5121ce52009-01-03 21:22:43 +0000653
654cleanup:
655
656 return( ret );
657}
658
659/*
660 * Export X into unsigned binary data, big endian
661 */
Paul Bakker23986e52011-04-24 08:57:21 +0000662int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000663{
Paul Bakker23986e52011-04-24 08:57:21 +0000664 size_t i, j, n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000665
666 n = mpi_size( X );
667
668 if( buflen < n )
Paul Bakker40e46942009-01-03 21:51:57 +0000669 return( POLARSSL_ERR_MPI_BUFFER_TOO_SMALL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000670
671 memset( buf, 0, buflen );
672
673 for( i = buflen - 1, j = 0; n > 0; i--, j++, n-- )
674 buf[i] = (unsigned char)( X->p[j / ciL] >> ((j % ciL) << 3) );
675
676 return( 0 );
677}
678
679/*
680 * Left-shift: X <<= count
681 */
Paul Bakker23986e52011-04-24 08:57:21 +0000682int mpi_shift_l( mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +0000683{
Paul Bakker23986e52011-04-24 08:57:21 +0000684 int ret;
685 size_t i, v0, t1;
Paul Bakkera755ca12011-04-24 09:11:17 +0000686 t_uint r0 = 0, r1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000687
688 v0 = count / (biL );
689 t1 = count & (biL - 1);
690
691 i = mpi_msb( X ) + count;
692
Paul Bakkerf9688572011-05-05 10:00:45 +0000693 if( X->n * biL < i )
Paul Bakker5121ce52009-01-03 21:22:43 +0000694 MPI_CHK( mpi_grow( X, BITS_TO_LIMBS( i ) ) );
695
696 ret = 0;
697
698 /*
699 * shift by count / limb_size
700 */
701 if( v0 > 0 )
702 {
Paul Bakker23986e52011-04-24 08:57:21 +0000703 for( i = X->n; i > v0; i-- )
704 X->p[i - 1] = X->p[i - v0 - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +0000705
Paul Bakker23986e52011-04-24 08:57:21 +0000706 for( ; i > 0; i-- )
707 X->p[i - 1] = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000708 }
709
710 /*
711 * shift by count % limb_size
712 */
713 if( t1 > 0 )
714 {
715 for( i = v0; i < X->n; i++ )
716 {
717 r1 = X->p[i] >> (biL - t1);
718 X->p[i] <<= t1;
719 X->p[i] |= r0;
720 r0 = r1;
721 }
722 }
723
724cleanup:
725
726 return( ret );
727}
728
729/*
730 * Right-shift: X >>= count
731 */
Paul Bakker23986e52011-04-24 08:57:21 +0000732int mpi_shift_r( mpi *X, size_t count )
Paul Bakker5121ce52009-01-03 21:22:43 +0000733{
Paul Bakker23986e52011-04-24 08:57:21 +0000734 size_t i, v0, v1;
Paul Bakkera755ca12011-04-24 09:11:17 +0000735 t_uint r0 = 0, r1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000736
737 v0 = count / biL;
738 v1 = count & (biL - 1);
739
Manuel Pégourié-Gonnarde44ec102012-11-17 12:42:51 +0100740 if( v0 > X->n || ( v0 == X->n && v1 > 0 ) )
741 return mpi_lset( X, 0 );
742
Paul Bakker5121ce52009-01-03 21:22:43 +0000743 /*
744 * shift by count / limb_size
745 */
746 if( v0 > 0 )
747 {
748 for( i = 0; i < X->n - v0; i++ )
749 X->p[i] = X->p[i + v0];
750
751 for( ; i < X->n; i++ )
752 X->p[i] = 0;
753 }
754
755 /*
756 * shift by count % limb_size
757 */
758 if( v1 > 0 )
759 {
Paul Bakker23986e52011-04-24 08:57:21 +0000760 for( i = X->n; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000761 {
Paul Bakker23986e52011-04-24 08:57:21 +0000762 r1 = X->p[i - 1] << (biL - v1);
763 X->p[i - 1] >>= v1;
764 X->p[i - 1] |= r0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000765 r0 = r1;
766 }
767 }
768
769 return( 0 );
770}
771
772/*
773 * Compare unsigned values
774 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000775int mpi_cmp_abs( const mpi *X, const mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000776{
Paul Bakker23986e52011-04-24 08:57:21 +0000777 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +0000778
Paul Bakker23986e52011-04-24 08:57:21 +0000779 for( i = X->n; i > 0; i-- )
780 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000781 break;
782
Paul Bakker23986e52011-04-24 08:57:21 +0000783 for( j = Y->n; j > 0; j-- )
784 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000785 break;
786
Paul Bakker23986e52011-04-24 08:57:21 +0000787 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000788 return( 0 );
789
790 if( i > j ) return( 1 );
791 if( j > i ) return( -1 );
792
Paul Bakker23986e52011-04-24 08:57:21 +0000793 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000794 {
Paul Bakker23986e52011-04-24 08:57:21 +0000795 if( X->p[i - 1] > Y->p[i - 1] ) return( 1 );
796 if( X->p[i - 1] < Y->p[i - 1] ) return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000797 }
798
799 return( 0 );
800}
801
802/*
803 * Compare signed values
804 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000805int mpi_cmp_mpi( const mpi *X, const mpi *Y )
Paul Bakker5121ce52009-01-03 21:22:43 +0000806{
Paul Bakker23986e52011-04-24 08:57:21 +0000807 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +0000808
Paul Bakker23986e52011-04-24 08:57:21 +0000809 for( i = X->n; i > 0; i-- )
810 if( X->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000811 break;
812
Paul Bakker23986e52011-04-24 08:57:21 +0000813 for( j = Y->n; j > 0; j-- )
814 if( Y->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000815 break;
816
Paul Bakker23986e52011-04-24 08:57:21 +0000817 if( i == 0 && j == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000818 return( 0 );
819
820 if( i > j ) return( X->s );
Paul Bakker0c8f73b2012-03-22 14:08:57 +0000821 if( j > i ) return( -Y->s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000822
823 if( X->s > 0 && Y->s < 0 ) return( 1 );
824 if( Y->s > 0 && X->s < 0 ) return( -1 );
825
Paul Bakker23986e52011-04-24 08:57:21 +0000826 for( ; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000827 {
Paul Bakker23986e52011-04-24 08:57:21 +0000828 if( X->p[i - 1] > Y->p[i - 1] ) return( X->s );
829 if( X->p[i - 1] < Y->p[i - 1] ) return( -X->s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000830 }
831
832 return( 0 );
833}
834
835/*
836 * Compare signed values
837 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000838int mpi_cmp_int( const mpi *X, t_sint z )
Paul Bakker5121ce52009-01-03 21:22:43 +0000839{
840 mpi Y;
Paul Bakkera755ca12011-04-24 09:11:17 +0000841 t_uint p[1];
Paul Bakker5121ce52009-01-03 21:22:43 +0000842
843 *p = ( z < 0 ) ? -z : z;
844 Y.s = ( z < 0 ) ? -1 : 1;
845 Y.n = 1;
846 Y.p = p;
847
848 return( mpi_cmp_mpi( X, &Y ) );
849}
850
851/*
852 * Unsigned addition: X = |A| + |B| (HAC 14.7)
853 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000854int mpi_add_abs( mpi *X, const mpi *A, const mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +0000855{
Paul Bakker23986e52011-04-24 08:57:21 +0000856 int ret;
857 size_t i, j;
Paul Bakkera755ca12011-04-24 09:11:17 +0000858 t_uint *o, *p, c;
Paul Bakker5121ce52009-01-03 21:22:43 +0000859
860 if( X == B )
861 {
Paul Bakkerff60ee62010-03-16 21:09:09 +0000862 const mpi *T = A; A = X; B = T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000863 }
864
865 if( X != A )
866 MPI_CHK( mpi_copy( X, A ) );
Paul Bakker9af723c2014-05-01 13:03:14 +0200867
Paul Bakkerf7ca7b92009-06-20 10:31:06 +0000868 /*
869 * X should always be positive as a result of unsigned additions.
870 */
871 X->s = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000872
Paul Bakker23986e52011-04-24 08:57:21 +0000873 for( j = B->n; j > 0; j-- )
874 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000875 break;
876
Paul Bakker23986e52011-04-24 08:57:21 +0000877 MPI_CHK( mpi_grow( X, j ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000878
879 o = B->p; p = X->p; c = 0;
880
Paul Bakker23986e52011-04-24 08:57:21 +0000881 for( i = 0; i < j; i++, o++, p++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000882 {
883 *p += c; c = ( *p < c );
884 *p += *o; c += ( *p < *o );
885 }
886
887 while( c != 0 )
888 {
889 if( i >= X->n )
890 {
891 MPI_CHK( mpi_grow( X, i + 1 ) );
892 p = X->p + i;
893 }
894
Paul Bakker2d319fd2012-09-16 21:34:26 +0000895 *p += c; c = ( *p < c ); i++; p++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000896 }
897
898cleanup:
899
900 return( ret );
901}
902
903/*
Paul Bakker60b1d102013-10-29 10:02:51 +0100904 * Helper for mpi subtraction
Paul Bakker5121ce52009-01-03 21:22:43 +0000905 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000906static void mpi_sub_hlp( size_t n, t_uint *s, t_uint *d )
Paul Bakker5121ce52009-01-03 21:22:43 +0000907{
Paul Bakker23986e52011-04-24 08:57:21 +0000908 size_t i;
Paul Bakkera755ca12011-04-24 09:11:17 +0000909 t_uint c, z;
Paul Bakker5121ce52009-01-03 21:22:43 +0000910
911 for( i = c = 0; i < n; i++, s++, d++ )
912 {
913 z = ( *d < c ); *d -= c;
914 c = ( *d < *s ) + z; *d -= *s;
915 }
916
917 while( c != 0 )
918 {
919 z = ( *d < c ); *d -= c;
920 c = z; i++; d++;
921 }
922}
923
924/*
Paul Bakker60b1d102013-10-29 10:02:51 +0100925 * Unsigned subtraction: X = |A| - |B| (HAC 14.9)
Paul Bakker5121ce52009-01-03 21:22:43 +0000926 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000927int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +0000928{
929 mpi TB;
Paul Bakker23986e52011-04-24 08:57:21 +0000930 int ret;
931 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000932
933 if( mpi_cmp_abs( A, B ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000934 return( POLARSSL_ERR_MPI_NEGATIVE_VALUE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000935
Paul Bakker6c591fa2011-05-05 11:49:20 +0000936 mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +0000937
938 if( X == B )
939 {
940 MPI_CHK( mpi_copy( &TB, B ) );
941 B = &TB;
942 }
943
944 if( X != A )
945 MPI_CHK( mpi_copy( X, A ) );
946
Paul Bakker1ef7a532009-06-20 10:50:55 +0000947 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100948 * X should always be positive as a result of unsigned subtractions.
Paul Bakker1ef7a532009-06-20 10:50:55 +0000949 */
950 X->s = 1;
951
Paul Bakker5121ce52009-01-03 21:22:43 +0000952 ret = 0;
953
Paul Bakker23986e52011-04-24 08:57:21 +0000954 for( n = B->n; n > 0; n-- )
955 if( B->p[n - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000956 break;
957
Paul Bakker23986e52011-04-24 08:57:21 +0000958 mpi_sub_hlp( n, B->p, X->p );
Paul Bakker5121ce52009-01-03 21:22:43 +0000959
960cleanup:
961
Paul Bakker6c591fa2011-05-05 11:49:20 +0000962 mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +0000963
964 return( ret );
965}
966
967/*
968 * Signed addition: X = A + B
969 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000970int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +0000971{
972 int ret, s = A->s;
973
974 if( A->s * B->s < 0 )
975 {
976 if( mpi_cmp_abs( A, B ) >= 0 )
977 {
978 MPI_CHK( mpi_sub_abs( X, A, B ) );
979 X->s = s;
980 }
981 else
982 {
983 MPI_CHK( mpi_sub_abs( X, B, A ) );
984 X->s = -s;
985 }
986 }
987 else
988 {
989 MPI_CHK( mpi_add_abs( X, A, B ) );
990 X->s = s;
991 }
992
993cleanup:
994
995 return( ret );
996}
997
998/*
Paul Bakker60b1d102013-10-29 10:02:51 +0100999 * Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +00001000 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001001int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001002{
1003 int ret, s = A->s;
1004
1005 if( A->s * B->s > 0 )
1006 {
1007 if( mpi_cmp_abs( A, B ) >= 0 )
1008 {
1009 MPI_CHK( mpi_sub_abs( X, A, B ) );
1010 X->s = s;
1011 }
1012 else
1013 {
1014 MPI_CHK( mpi_sub_abs( X, B, A ) );
1015 X->s = -s;
1016 }
1017 }
1018 else
1019 {
1020 MPI_CHK( mpi_add_abs( X, A, B ) );
1021 X->s = s;
1022 }
1023
1024cleanup:
1025
1026 return( ret );
1027}
1028
1029/*
1030 * Signed addition: X = A + b
1031 */
Paul Bakkera755ca12011-04-24 09:11:17 +00001032int mpi_add_int( mpi *X, const mpi *A, t_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001033{
1034 mpi _B;
Paul Bakkera755ca12011-04-24 09:11:17 +00001035 t_uint p[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001036
1037 p[0] = ( b < 0 ) ? -b : b;
1038 _B.s = ( b < 0 ) ? -1 : 1;
1039 _B.n = 1;
1040 _B.p = p;
1041
1042 return( mpi_add_mpi( X, A, &_B ) );
1043}
1044
1045/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001046 * Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +00001047 */
Paul Bakkera755ca12011-04-24 09:11:17 +00001048int mpi_sub_int( mpi *X, const mpi *A, t_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001049{
1050 mpi _B;
Paul Bakkera755ca12011-04-24 09:11:17 +00001051 t_uint p[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001052
1053 p[0] = ( b < 0 ) ? -b : b;
1054 _B.s = ( b < 0 ) ? -1 : 1;
1055 _B.n = 1;
1056 _B.p = p;
1057
1058 return( mpi_sub_mpi( X, A, &_B ) );
1059}
1060
1061/*
1062 * Helper for mpi multiplication
Paul Bakkerfc4f46f2013-06-24 19:23:56 +02001063 */
1064static
1065#if defined(__APPLE__) && defined(__arm__)
1066/*
1067 * Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
1068 * appears to need this to prevent bad ARM code generation at -O3.
1069 */
1070__attribute__ ((noinline))
1071#endif
1072void mpi_mul_hlp( size_t i, t_uint *s, t_uint *d, t_uint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001073{
Paul Bakkera755ca12011-04-24 09:11:17 +00001074 t_uint c = 0, t = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001075
1076#if defined(MULADDC_HUIT)
1077 for( ; i >= 8; i -= 8 )
1078 {
1079 MULADDC_INIT
1080 MULADDC_HUIT
1081 MULADDC_STOP
1082 }
1083
1084 for( ; i > 0; i-- )
1085 {
1086 MULADDC_INIT
1087 MULADDC_CORE
1088 MULADDC_STOP
1089 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001090#else /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 for( ; i >= 16; i -= 16 )
1092 {
1093 MULADDC_INIT
1094 MULADDC_CORE MULADDC_CORE
1095 MULADDC_CORE MULADDC_CORE
1096 MULADDC_CORE MULADDC_CORE
1097 MULADDC_CORE MULADDC_CORE
1098
1099 MULADDC_CORE MULADDC_CORE
1100 MULADDC_CORE MULADDC_CORE
1101 MULADDC_CORE MULADDC_CORE
1102 MULADDC_CORE MULADDC_CORE
1103 MULADDC_STOP
1104 }
1105
1106 for( ; i >= 8; i -= 8 )
1107 {
1108 MULADDC_INIT
1109 MULADDC_CORE MULADDC_CORE
1110 MULADDC_CORE MULADDC_CORE
1111
1112 MULADDC_CORE MULADDC_CORE
1113 MULADDC_CORE MULADDC_CORE
1114 MULADDC_STOP
1115 }
1116
1117 for( ; i > 0; i-- )
1118 {
1119 MULADDC_INIT
1120 MULADDC_CORE
1121 MULADDC_STOP
1122 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001123#endif /* MULADDC_HUIT */
Paul Bakker5121ce52009-01-03 21:22:43 +00001124
1125 t++;
1126
1127 do {
1128 *d += c; c = ( *d < c ); d++;
1129 }
1130 while( c != 0 );
1131}
1132
1133/*
1134 * Baseline multiplication: X = A * B (HAC 14.12)
1135 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001136int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001137{
Paul Bakker23986e52011-04-24 08:57:21 +00001138 int ret;
1139 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00001140 mpi TA, TB;
1141
Paul Bakker6c591fa2011-05-05 11:49:20 +00001142 mpi_init( &TA ); mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001143
1144 if( X == A ) { MPI_CHK( mpi_copy( &TA, A ) ); A = &TA; }
1145 if( X == B ) { MPI_CHK( mpi_copy( &TB, B ) ); B = &TB; }
1146
Paul Bakker23986e52011-04-24 08:57:21 +00001147 for( i = A->n; i > 0; i-- )
1148 if( A->p[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001149 break;
1150
Paul Bakker23986e52011-04-24 08:57:21 +00001151 for( j = B->n; j > 0; j-- )
1152 if( B->p[j - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001153 break;
1154
Paul Bakker23986e52011-04-24 08:57:21 +00001155 MPI_CHK( mpi_grow( X, i + j ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001156 MPI_CHK( mpi_lset( X, 0 ) );
1157
Paul Bakker23986e52011-04-24 08:57:21 +00001158 for( i++; j > 0; j-- )
1159 mpi_mul_hlp( i - 1, A->p, X->p + j - 1, B->p[j - 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00001160
1161 X->s = A->s * B->s;
1162
1163cleanup:
1164
Paul Bakker6c591fa2011-05-05 11:49:20 +00001165 mpi_free( &TB ); mpi_free( &TA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001166
1167 return( ret );
1168}
1169
1170/*
1171 * Baseline multiplication: X = A * b
1172 */
Paul Bakkera755ca12011-04-24 09:11:17 +00001173int mpi_mul_int( mpi *X, const mpi *A, t_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001174{
1175 mpi _B;
Paul Bakkera755ca12011-04-24 09:11:17 +00001176 t_uint p[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001177
1178 _B.s = 1;
1179 _B.n = 1;
1180 _B.p = p;
1181 p[0] = b;
1182
1183 return( mpi_mul_mpi( X, A, &_B ) );
1184}
1185
1186/*
1187 * Division by mpi: A = Q * B + R (HAC 14.20)
1188 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001189int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001190{
Paul Bakker23986e52011-04-24 08:57:21 +00001191 int ret;
1192 size_t i, n, t, k;
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 mpi X, Y, Z, T1, T2;
1194
1195 if( mpi_cmp_int( B, 0 ) == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +00001196 return( POLARSSL_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001197
Paul Bakker6c591fa2011-05-05 11:49:20 +00001198 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
1199 mpi_init( &T1 ); mpi_init( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001200
1201 if( mpi_cmp_abs( A, B ) < 0 )
1202 {
1203 if( Q != NULL ) MPI_CHK( mpi_lset( Q, 0 ) );
1204 if( R != NULL ) MPI_CHK( mpi_copy( R, A ) );
1205 return( 0 );
1206 }
1207
1208 MPI_CHK( mpi_copy( &X, A ) );
1209 MPI_CHK( mpi_copy( &Y, B ) );
1210 X.s = Y.s = 1;
1211
1212 MPI_CHK( mpi_grow( &Z, A->n + 2 ) );
1213 MPI_CHK( mpi_lset( &Z, 0 ) );
1214 MPI_CHK( mpi_grow( &T1, 2 ) );
1215 MPI_CHK( mpi_grow( &T2, 3 ) );
1216
1217 k = mpi_msb( &Y ) % biL;
Paul Bakkerf9688572011-05-05 10:00:45 +00001218 if( k < biL - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001219 {
1220 k = biL - 1 - k;
1221 MPI_CHK( mpi_shift_l( &X, k ) );
1222 MPI_CHK( mpi_shift_l( &Y, k ) );
1223 }
1224 else k = 0;
1225
1226 n = X.n - 1;
1227 t = Y.n - 1;
Paul Bakker66d5d072014-06-17 16:39:18 +02001228 MPI_CHK( mpi_shift_l( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001229
1230 while( mpi_cmp_mpi( &X, &Y ) >= 0 )
1231 {
1232 Z.p[n - t]++;
Paul Bakker6ea1a952013-12-31 11:16:03 +01001233 MPI_CHK( mpi_sub_mpi( &X, &X, &Y ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001234 }
Paul Bakker66d5d072014-06-17 16:39:18 +02001235 MPI_CHK( mpi_shift_r( &Y, biL * ( n - t ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001236
1237 for( i = n; i > t ; i-- )
1238 {
1239 if( X.p[i] >= Y.p[t] )
1240 Z.p[i - t - 1] = ~0;
1241 else
1242 {
Manuel Pégourié-Gonnardd72704b2015-02-12 09:38:54 +00001243#if defined(POLARSSL_HAVE_UDBL)
Paul Bakkeradb7ce12011-08-23 14:55:55 +00001244 t_udbl r;
Paul Bakker5121ce52009-01-03 21:22:43 +00001245
Paul Bakkeradb7ce12011-08-23 14:55:55 +00001246 r = (t_udbl) X.p[i] << biL;
1247 r |= (t_udbl) X.p[i - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001248 r /= Y.p[t];
Paul Bakker66d5d072014-06-17 16:39:18 +02001249 if( r > ( (t_udbl) 1 << biL ) - 1 )
1250 r = ( (t_udbl) 1 << biL ) - 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001251
Paul Bakkera755ca12011-04-24 09:11:17 +00001252 Z.p[i - t - 1] = (t_uint) r;
Paul Bakker5121ce52009-01-03 21:22:43 +00001253#else
1254 /*
1255 * __udiv_qrnnd_c, from gmp/longlong.h
1256 */
Paul Bakkera755ca12011-04-24 09:11:17 +00001257 t_uint q0, q1, r0, r1;
1258 t_uint d0, d1, d, m;
Paul Bakker5121ce52009-01-03 21:22:43 +00001259
1260 d = Y.p[t];
1261 d0 = ( d << biH ) >> biH;
1262 d1 = ( d >> biH );
1263
1264 q1 = X.p[i] / d1;
1265 r1 = X.p[i] - d1 * q1;
1266 r1 <<= biH;
1267 r1 |= ( X.p[i - 1] >> biH );
1268
1269 m = q1 * d0;
1270 if( r1 < m )
1271 {
1272 q1--, r1 += d;
1273 while( r1 >= d && r1 < m )
1274 q1--, r1 += d;
1275 }
1276 r1 -= m;
1277
1278 q0 = r1 / d1;
1279 r0 = r1 - d1 * q0;
1280 r0 <<= biH;
1281 r0 |= ( X.p[i - 1] << biH ) >> biH;
1282
1283 m = q0 * d0;
1284 if( r0 < m )
1285 {
1286 q0--, r0 += d;
1287 while( r0 >= d && r0 < m )
1288 q0--, r0 += d;
1289 }
1290 r0 -= m;
1291
1292 Z.p[i - t - 1] = ( q1 << biH ) | q0;
Paul Bakkerdb20c102014-06-17 14:34:44 +02001293#endif /* POLARSSL_HAVE_UDBL && !64-bit Apple with Clang 5.0 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001294 }
1295
1296 Z.p[i - t - 1]++;
1297 do
1298 {
1299 Z.p[i - t - 1]--;
1300
1301 MPI_CHK( mpi_lset( &T1, 0 ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001302 T1.p[0] = ( t < 1 ) ? 0 : Y.p[t - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001303 T1.p[1] = Y.p[t];
1304 MPI_CHK( mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
1305
1306 MPI_CHK( mpi_lset( &T2, 0 ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001307 T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2];
1308 T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 T2.p[2] = X.p[i];
1310 }
1311 while( mpi_cmp_mpi( &T1, &T2 ) > 0 );
1312
1313 MPI_CHK( mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001314 MPI_CHK( mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) );
1316
1317 if( mpi_cmp_int( &X, 0 ) < 0 )
1318 {
1319 MPI_CHK( mpi_copy( &T1, &Y ) );
Paul Bakker66d5d072014-06-17 16:39:18 +02001320 MPI_CHK( mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 MPI_CHK( mpi_add_mpi( &X, &X, &T1 ) );
1322 Z.p[i - t - 1]--;
1323 }
1324 }
1325
1326 if( Q != NULL )
1327 {
Paul Bakker6ea1a952013-12-31 11:16:03 +01001328 MPI_CHK( mpi_copy( Q, &Z ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001329 Q->s = A->s * B->s;
1330 }
1331
1332 if( R != NULL )
1333 {
Paul Bakker6ea1a952013-12-31 11:16:03 +01001334 MPI_CHK( mpi_shift_r( &X, k ) );
Paul Bakkerf02c5642012-11-13 10:25:21 +00001335 X.s = A->s;
Paul Bakker6ea1a952013-12-31 11:16:03 +01001336 MPI_CHK( mpi_copy( R, &X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337
Paul Bakker5121ce52009-01-03 21:22:43 +00001338 if( mpi_cmp_int( R, 0 ) == 0 )
1339 R->s = 1;
1340 }
1341
1342cleanup:
1343
Paul Bakker6c591fa2011-05-05 11:49:20 +00001344 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
1345 mpi_free( &T1 ); mpi_free( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001346
1347 return( ret );
1348}
1349
1350/*
1351 * Division by int: A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +00001352 */
Paul Bakkera755ca12011-04-24 09:11:17 +00001353int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001354{
1355 mpi _B;
Paul Bakkera755ca12011-04-24 09:11:17 +00001356 t_uint p[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001357
1358 p[0] = ( b < 0 ) ? -b : b;
1359 _B.s = ( b < 0 ) ? -1 : 1;
1360 _B.n = 1;
1361 _B.p = p;
1362
1363 return( mpi_div_mpi( Q, R, A, &_B ) );
1364}
1365
1366/*
1367 * Modulo: R = A mod B
1368 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001369int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001370{
1371 int ret;
1372
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001373 if( mpi_cmp_int( B, 0 ) < 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001374 return( POLARSSL_ERR_MPI_NEGATIVE_VALUE );
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001375
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 MPI_CHK( mpi_div_mpi( NULL, R, A, B ) );
1377
1378 while( mpi_cmp_int( R, 0 ) < 0 )
1379 MPI_CHK( mpi_add_mpi( R, R, B ) );
1380
1381 while( mpi_cmp_mpi( R, B ) >= 0 )
1382 MPI_CHK( mpi_sub_mpi( R, R, B ) );
1383
1384cleanup:
1385
1386 return( ret );
1387}
1388
1389/*
1390 * Modulo: r = A mod b
1391 */
Paul Bakkera755ca12011-04-24 09:11:17 +00001392int mpi_mod_int( t_uint *r, const mpi *A, t_sint b )
Paul Bakker5121ce52009-01-03 21:22:43 +00001393{
Paul Bakker23986e52011-04-24 08:57:21 +00001394 size_t i;
Paul Bakkera755ca12011-04-24 09:11:17 +00001395 t_uint x, y, z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001396
1397 if( b == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +00001398 return( POLARSSL_ERR_MPI_DIVISION_BY_ZERO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001399
1400 if( b < 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001401 return( POLARSSL_ERR_MPI_NEGATIVE_VALUE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001402
1403 /*
1404 * handle trivial cases
1405 */
1406 if( b == 1 )
1407 {
1408 *r = 0;
1409 return( 0 );
1410 }
1411
1412 if( b == 2 )
1413 {
1414 *r = A->p[0] & 1;
1415 return( 0 );
1416 }
1417
1418 /*
1419 * general case
1420 */
Paul Bakker23986e52011-04-24 08:57:21 +00001421 for( i = A->n, y = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 {
Paul Bakker23986e52011-04-24 08:57:21 +00001423 x = A->p[i - 1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 y = ( y << biH ) | ( x >> biH );
1425 z = y / b;
1426 y -= z * b;
1427
1428 x <<= biH;
1429 y = ( y << biH ) | ( x >> biH );
1430 z = y / b;
1431 y -= z * b;
1432 }
1433
Paul Bakkerce40a6d2009-06-23 19:46:08 +00001434 /*
1435 * If A is negative, then the current y represents a negative value.
1436 * Flipping it to the positive side.
1437 */
1438 if( A->s < 0 && y != 0 )
1439 y = b - y;
1440
Paul Bakker5121ce52009-01-03 21:22:43 +00001441 *r = y;
1442
1443 return( 0 );
1444}
1445
1446/*
1447 * Fast Montgomery initialization (thanks to Tom St Denis)
1448 */
Paul Bakkera755ca12011-04-24 09:11:17 +00001449static void mpi_montg_init( t_uint *mm, const mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00001450{
Paul Bakkera755ca12011-04-24 09:11:17 +00001451 t_uint x, m0 = N->p[0];
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01001452 unsigned int i;
Paul Bakker5121ce52009-01-03 21:22:43 +00001453
1454 x = m0;
1455 x += ( ( m0 + 2 ) & 4 ) << 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001456
Manuel Pégourié-Gonnardfdf3f0e2014-03-11 13:47:05 +01001457 for( i = biL; i >= 8; i /= 2 )
1458 x *= ( 2 - ( m0 * x ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001459
1460 *mm = ~x + 1;
1461}
1462
1463/*
1464 * Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
1465 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001466static void mpi_montmul( mpi *A, const mpi *B, const mpi *N, t_uint mm,
1467 const mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00001468{
Paul Bakker23986e52011-04-24 08:57:21 +00001469 size_t i, n, m;
Paul Bakkera755ca12011-04-24 09:11:17 +00001470 t_uint u0, u1, *d;
Paul Bakker5121ce52009-01-03 21:22:43 +00001471
1472 memset( T->p, 0, T->n * ciL );
1473
1474 d = T->p;
1475 n = N->n;
1476 m = ( B->n < n ) ? B->n : n;
1477
1478 for( i = 0; i < n; i++ )
1479 {
1480 /*
1481 * T = (T + u0*B + u1*N) / 2^biL
1482 */
1483 u0 = A->p[i];
1484 u1 = ( d[0] + u0 * B->p[0] ) * mm;
1485
1486 mpi_mul_hlp( m, B->p, d, u0 );
1487 mpi_mul_hlp( n, N->p, d, u1 );
1488
1489 *d++ = u0; d[n + 1] = 0;
1490 }
1491
Paul Bakker66d5d072014-06-17 16:39:18 +02001492 memcpy( A->p, d, ( n + 1 ) * ciL );
Paul Bakker5121ce52009-01-03 21:22:43 +00001493
1494 if( mpi_cmp_abs( A, N ) >= 0 )
1495 mpi_sub_hlp( n, N->p, A->p );
1496 else
1497 /* prevent timing attacks */
1498 mpi_sub_hlp( n, A->p, T->p );
1499}
1500
1501/*
1502 * Montgomery reduction: A = A * R^-1 mod N
1503 */
Paul Bakkera755ca12011-04-24 09:11:17 +00001504static void mpi_montred( mpi *A, const mpi *N, t_uint mm, const mpi *T )
Paul Bakker5121ce52009-01-03 21:22:43 +00001505{
Paul Bakkera755ca12011-04-24 09:11:17 +00001506 t_uint z = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001507 mpi U;
1508
Paul Bakker8ddb6452013-02-27 14:56:33 +01001509 U.n = U.s = (int) z;
Paul Bakker5121ce52009-01-03 21:22:43 +00001510 U.p = &z;
1511
1512 mpi_montmul( A, &U, N, mm, T );
1513}
1514
1515/*
1516 * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
1517 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001518int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR )
Paul Bakker5121ce52009-01-03 21:22:43 +00001519{
Paul Bakker23986e52011-04-24 08:57:21 +00001520 int ret;
1521 size_t wbits, wsize, one = 1;
1522 size_t i, j, nblimbs;
1523 size_t bufsize, nbits;
Paul Bakkera755ca12011-04-24 09:11:17 +00001524 t_uint ei, mm, state;
Paul Bakkerf6198c12012-05-16 08:02:29 +00001525 mpi RR, T, W[ 2 << POLARSSL_MPI_WINDOW_SIZE ], Apos;
1526 int neg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001527
1528 if( mpi_cmp_int( N, 0 ) < 0 || ( N->p[0] & 1 ) == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +00001529 return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001530
Paul Bakkerf6198c12012-05-16 08:02:29 +00001531 if( mpi_cmp_int( E, 0 ) < 0 )
1532 return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
1533
1534 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001535 * Init temps and window size
1536 */
1537 mpi_montg_init( &mm, N );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001538 mpi_init( &RR ); mpi_init( &T );
Manuel Pégourié-Gonnardfd6a1912014-01-18 19:05:23 +01001539 mpi_init( &Apos );
Paul Bakker5121ce52009-01-03 21:22:43 +00001540 memset( W, 0, sizeof( W ) );
1541
1542 i = mpi_msb( E );
1543
1544 wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
1545 ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1;
1546
Paul Bakkerb6d5f082011-11-25 11:52:11 +00001547 if( wsize > POLARSSL_MPI_WINDOW_SIZE )
1548 wsize = POLARSSL_MPI_WINDOW_SIZE;
1549
Paul Bakker5121ce52009-01-03 21:22:43 +00001550 j = N->n + 1;
1551 MPI_CHK( mpi_grow( X, j ) );
1552 MPI_CHK( mpi_grow( &W[1], j ) );
1553 MPI_CHK( mpi_grow( &T, j * 2 ) );
1554
1555 /*
Paul Bakker50546922012-05-19 08:40:49 +00001556 * Compensate for negative A (and correct at the end)
1557 */
1558 neg = ( A->s == -1 );
Paul Bakker50546922012-05-19 08:40:49 +00001559 if( neg )
1560 {
1561 MPI_CHK( mpi_copy( &Apos, A ) );
1562 Apos.s = 1;
1563 A = &Apos;
1564 }
1565
1566 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001567 * If 1st call, pre-compute R^2 mod N
1568 */
1569 if( _RR == NULL || _RR->p == NULL )
1570 {
1571 MPI_CHK( mpi_lset( &RR, 1 ) );
1572 MPI_CHK( mpi_shift_l( &RR, N->n * 2 * biL ) );
1573 MPI_CHK( mpi_mod_mpi( &RR, &RR, N ) );
1574
1575 if( _RR != NULL )
1576 memcpy( _RR, &RR, sizeof( mpi ) );
1577 }
1578 else
1579 memcpy( &RR, _RR, sizeof( mpi ) );
1580
1581 /*
1582 * W[1] = A * R^2 * R^-1 mod N = A * R mod N
1583 */
1584 if( mpi_cmp_mpi( A, N ) >= 0 )
Paul Bakkerc2024f42014-01-23 20:38:35 +01001585 MPI_CHK( mpi_mod_mpi( &W[1], A, N ) );
1586 else
1587 MPI_CHK( mpi_copy( &W[1], A ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001588
1589 mpi_montmul( &W[1], &RR, N, mm, &T );
1590
1591 /*
1592 * X = R^2 * R^-1 mod N = R mod N
1593 */
1594 MPI_CHK( mpi_copy( X, &RR ) );
1595 mpi_montred( X, N, mm, &T );
1596
1597 if( wsize > 1 )
1598 {
1599 /*
1600 * W[1 << (wsize - 1)] = W[1] ^ (wsize - 1)
1601 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001602 j = one << ( wsize - 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001603
1604 MPI_CHK( mpi_grow( &W[j], N->n + 1 ) );
1605 MPI_CHK( mpi_copy( &W[j], &W[1] ) );
1606
1607 for( i = 0; i < wsize - 1; i++ )
1608 mpi_montmul( &W[j], &W[j], N, mm, &T );
Paul Bakker0d7702c2013-10-29 16:18:35 +01001609
Paul Bakker5121ce52009-01-03 21:22:43 +00001610 /*
1611 * W[i] = W[i - 1] * W[1]
1612 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001613 for( i = j + 1; i < ( one << wsize ); i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001614 {
1615 MPI_CHK( mpi_grow( &W[i], N->n + 1 ) );
1616 MPI_CHK( mpi_copy( &W[i], &W[i - 1] ) );
1617
1618 mpi_montmul( &W[i], &W[1], N, mm, &T );
1619 }
1620 }
1621
1622 nblimbs = E->n;
1623 bufsize = 0;
1624 nbits = 0;
1625 wbits = 0;
1626 state = 0;
1627
1628 while( 1 )
1629 {
1630 if( bufsize == 0 )
1631 {
Paul Bakker0d7702c2013-10-29 16:18:35 +01001632 if( nblimbs == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001633 break;
1634
Paul Bakker0d7702c2013-10-29 16:18:35 +01001635 nblimbs--;
1636
Paul Bakkera755ca12011-04-24 09:11:17 +00001637 bufsize = sizeof( t_uint ) << 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00001638 }
1639
1640 bufsize--;
1641
1642 ei = (E->p[nblimbs] >> bufsize) & 1;
1643
1644 /*
1645 * skip leading 0s
1646 */
1647 if( ei == 0 && state == 0 )
1648 continue;
1649
1650 if( ei == 0 && state == 1 )
1651 {
1652 /*
1653 * out of window, square X
1654 */
1655 mpi_montmul( X, X, N, mm, &T );
1656 continue;
1657 }
1658
1659 /*
1660 * add ei to current window
1661 */
1662 state = 2;
1663
1664 nbits++;
Paul Bakker66d5d072014-06-17 16:39:18 +02001665 wbits |= ( ei << ( wsize - nbits ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001666
1667 if( nbits == wsize )
1668 {
1669 /*
1670 * X = X^wsize R^-1 mod N
1671 */
1672 for( i = 0; i < wsize; i++ )
1673 mpi_montmul( X, X, N, mm, &T );
1674
1675 /*
1676 * X = X * W[wbits] R^-1 mod N
1677 */
1678 mpi_montmul( X, &W[wbits], N, mm, &T );
1679
1680 state--;
1681 nbits = 0;
1682 wbits = 0;
1683 }
1684 }
1685
1686 /*
1687 * process the remaining bits
1688 */
1689 for( i = 0; i < nbits; i++ )
1690 {
1691 mpi_montmul( X, X, N, mm, &T );
1692
1693 wbits <<= 1;
1694
Paul Bakker66d5d072014-06-17 16:39:18 +02001695 if( ( wbits & ( one << wsize ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001696 mpi_montmul( X, &W[1], N, mm, &T );
1697 }
1698
1699 /*
1700 * X = A^E * R * R^-1 mod N = A^E mod N
1701 */
1702 mpi_montred( X, N, mm, &T );
1703
Paul Bakkerf6198c12012-05-16 08:02:29 +00001704 if( neg )
1705 {
1706 X->s = -1;
Paul Bakkerc2024f42014-01-23 20:38:35 +01001707 MPI_CHK( mpi_add_mpi( X, N, X ) );
Paul Bakkerf6198c12012-05-16 08:02:29 +00001708 }
1709
Paul Bakker5121ce52009-01-03 21:22:43 +00001710cleanup:
1711
Paul Bakker66d5d072014-06-17 16:39:18 +02001712 for( i = ( one << ( wsize - 1 ) ); i < ( one << wsize ); i++ )
Paul Bakker6c591fa2011-05-05 11:49:20 +00001713 mpi_free( &W[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +00001714
Paul Bakkerf6198c12012-05-16 08:02:29 +00001715 mpi_free( &W[1] ); mpi_free( &T ); mpi_free( &Apos );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001716
Paul Bakker75a28602014-03-31 12:08:17 +02001717 if( _RR == NULL || _RR->p == NULL )
Paul Bakker6c591fa2011-05-05 11:49:20 +00001718 mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00001719
1720 return( ret );
1721}
1722
Paul Bakker5121ce52009-01-03 21:22:43 +00001723/*
1724 * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
1725 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001726int mpi_gcd( mpi *G, const mpi *A, const mpi *B )
Paul Bakker5121ce52009-01-03 21:22:43 +00001727{
Paul Bakker23986e52011-04-24 08:57:21 +00001728 int ret;
1729 size_t lz, lzt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001730 mpi TG, TA, TB;
1731
Paul Bakker6c591fa2011-05-05 11:49:20 +00001732 mpi_init( &TG ); mpi_init( &TA ); mpi_init( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001733
Paul Bakker5121ce52009-01-03 21:22:43 +00001734 MPI_CHK( mpi_copy( &TA, A ) );
1735 MPI_CHK( mpi_copy( &TB, B ) );
1736
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00001737 lz = mpi_lsb( &TA );
1738 lzt = mpi_lsb( &TB );
1739
Paul Bakker66d5d072014-06-17 16:39:18 +02001740 if( lzt < lz )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00001741 lz = lzt;
1742
1743 MPI_CHK( mpi_shift_r( &TA, lz ) );
1744 MPI_CHK( mpi_shift_r( &TB, lz ) );
1745
Paul Bakker5121ce52009-01-03 21:22:43 +00001746 TA.s = TB.s = 1;
1747
1748 while( mpi_cmp_int( &TA, 0 ) != 0 )
1749 {
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00001750 MPI_CHK( mpi_shift_r( &TA, mpi_lsb( &TA ) ) );
1751 MPI_CHK( mpi_shift_r( &TB, mpi_lsb( &TB ) ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001752
1753 if( mpi_cmp_mpi( &TA, &TB ) >= 0 )
1754 {
1755 MPI_CHK( mpi_sub_abs( &TA, &TA, &TB ) );
1756 MPI_CHK( mpi_shift_r( &TA, 1 ) );
1757 }
1758 else
1759 {
1760 MPI_CHK( mpi_sub_abs( &TB, &TB, &TA ) );
1761 MPI_CHK( mpi_shift_r( &TB, 1 ) );
1762 }
1763 }
1764
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00001765 MPI_CHK( mpi_shift_l( &TB, lz ) );
1766 MPI_CHK( mpi_copy( G, &TB ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001767
1768cleanup:
1769
Paul Bakker6c591fa2011-05-05 11:49:20 +00001770 mpi_free( &TG ); mpi_free( &TA ); mpi_free( &TB );
Paul Bakker5121ce52009-01-03 21:22:43 +00001771
1772 return( ret );
1773}
1774
Paul Bakker33dc46b2014-04-30 16:11:39 +02001775/*
1776 * Fill X with size bytes of random.
1777 *
1778 * Use a temporary bytes representation to make sure the result is the same
Paul Bakkerc37b0ac2014-05-01 14:19:23 +02001779 * regardless of the platform endianness (useful when f_rng is actually
Paul Bakker33dc46b2014-04-30 16:11:39 +02001780 * deterministic, eg for tests).
1781 */
Paul Bakkera3d195c2011-11-27 21:07:34 +00001782int mpi_fill_random( mpi *X, size_t size,
1783 int (*f_rng)(void *, unsigned char *, size_t),
1784 void *p_rng )
Paul Bakker287781a2011-03-26 13:18:49 +00001785{
Paul Bakker23986e52011-04-24 08:57:21 +00001786 int ret;
Paul Bakker33dc46b2014-04-30 16:11:39 +02001787 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
1788
1789 if( size > POLARSSL_MPI_MAX_SIZE )
1790 return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker287781a2011-03-26 13:18:49 +00001791
Paul Bakker33dc46b2014-04-30 16:11:39 +02001792 MPI_CHK( f_rng( p_rng, buf, size ) );
1793 MPI_CHK( mpi_read_binary( X, buf, size ) );
Paul Bakker287781a2011-03-26 13:18:49 +00001794
1795cleanup:
1796 return( ret );
1797}
1798
Paul Bakker5121ce52009-01-03 21:22:43 +00001799/*
1800 * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
1801 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001802int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N )
Paul Bakker5121ce52009-01-03 21:22:43 +00001803{
1804 int ret;
1805 mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
1806
1807 if( mpi_cmp_int( N, 0 ) <= 0 )
Paul Bakker40e46942009-01-03 21:51:57 +00001808 return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001809
Paul Bakker6c591fa2011-05-05 11:49:20 +00001810 mpi_init( &TA ); mpi_init( &TU ); mpi_init( &U1 ); mpi_init( &U2 );
1811 mpi_init( &G ); mpi_init( &TB ); mpi_init( &TV );
1812 mpi_init( &V1 ); mpi_init( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001813
1814 MPI_CHK( mpi_gcd( &G, A, N ) );
1815
1816 if( mpi_cmp_int( &G, 1 ) != 0 )
1817 {
Paul Bakker40e46942009-01-03 21:51:57 +00001818 ret = POLARSSL_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00001819 goto cleanup;
1820 }
1821
1822 MPI_CHK( mpi_mod_mpi( &TA, A, N ) );
1823 MPI_CHK( mpi_copy( &TU, &TA ) );
1824 MPI_CHK( mpi_copy( &TB, N ) );
1825 MPI_CHK( mpi_copy( &TV, N ) );
1826
1827 MPI_CHK( mpi_lset( &U1, 1 ) );
1828 MPI_CHK( mpi_lset( &U2, 0 ) );
1829 MPI_CHK( mpi_lset( &V1, 0 ) );
1830 MPI_CHK( mpi_lset( &V2, 1 ) );
1831
1832 do
1833 {
1834 while( ( TU.p[0] & 1 ) == 0 )
1835 {
1836 MPI_CHK( mpi_shift_r( &TU, 1 ) );
1837
1838 if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
1839 {
1840 MPI_CHK( mpi_add_mpi( &U1, &U1, &TB ) );
1841 MPI_CHK( mpi_sub_mpi( &U2, &U2, &TA ) );
1842 }
1843
1844 MPI_CHK( mpi_shift_r( &U1, 1 ) );
1845 MPI_CHK( mpi_shift_r( &U2, 1 ) );
1846 }
1847
1848 while( ( TV.p[0] & 1 ) == 0 )
1849 {
1850 MPI_CHK( mpi_shift_r( &TV, 1 ) );
1851
1852 if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
1853 {
1854 MPI_CHK( mpi_add_mpi( &V1, &V1, &TB ) );
1855 MPI_CHK( mpi_sub_mpi( &V2, &V2, &TA ) );
1856 }
1857
1858 MPI_CHK( mpi_shift_r( &V1, 1 ) );
1859 MPI_CHK( mpi_shift_r( &V2, 1 ) );
1860 }
1861
1862 if( mpi_cmp_mpi( &TU, &TV ) >= 0 )
1863 {
1864 MPI_CHK( mpi_sub_mpi( &TU, &TU, &TV ) );
1865 MPI_CHK( mpi_sub_mpi( &U1, &U1, &V1 ) );
1866 MPI_CHK( mpi_sub_mpi( &U2, &U2, &V2 ) );
1867 }
1868 else
1869 {
1870 MPI_CHK( mpi_sub_mpi( &TV, &TV, &TU ) );
1871 MPI_CHK( mpi_sub_mpi( &V1, &V1, &U1 ) );
1872 MPI_CHK( mpi_sub_mpi( &V2, &V2, &U2 ) );
1873 }
1874 }
1875 while( mpi_cmp_int( &TU, 0 ) != 0 );
1876
1877 while( mpi_cmp_int( &V1, 0 ) < 0 )
1878 MPI_CHK( mpi_add_mpi( &V1, &V1, N ) );
1879
1880 while( mpi_cmp_mpi( &V1, N ) >= 0 )
1881 MPI_CHK( mpi_sub_mpi( &V1, &V1, N ) );
1882
1883 MPI_CHK( mpi_copy( X, &V1 ) );
1884
1885cleanup:
1886
Paul Bakker6c591fa2011-05-05 11:49:20 +00001887 mpi_free( &TA ); mpi_free( &TU ); mpi_free( &U1 ); mpi_free( &U2 );
1888 mpi_free( &G ); mpi_free( &TB ); mpi_free( &TV );
1889 mpi_free( &V1 ); mpi_free( &V2 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001890
1891 return( ret );
1892}
1893
Paul Bakkerd9374b02012-11-02 11:02:58 +00001894#if defined(POLARSSL_GENPRIME)
1895
Paul Bakker5121ce52009-01-03 21:22:43 +00001896static const int small_prime[] =
1897{
1898 3, 5, 7, 11, 13, 17, 19, 23,
1899 29, 31, 37, 41, 43, 47, 53, 59,
1900 61, 67, 71, 73, 79, 83, 89, 97,
1901 101, 103, 107, 109, 113, 127, 131, 137,
1902 139, 149, 151, 157, 163, 167, 173, 179,
1903 181, 191, 193, 197, 199, 211, 223, 227,
1904 229, 233, 239, 241, 251, 257, 263, 269,
1905 271, 277, 281, 283, 293, 307, 311, 313,
1906 317, 331, 337, 347, 349, 353, 359, 367,
1907 373, 379, 383, 389, 397, 401, 409, 419,
1908 421, 431, 433, 439, 443, 449, 457, 461,
1909 463, 467, 479, 487, 491, 499, 503, 509,
1910 521, 523, 541, 547, 557, 563, 569, 571,
1911 577, 587, 593, 599, 601, 607, 613, 617,
1912 619, 631, 641, 643, 647, 653, 659, 661,
1913 673, 677, 683, 691, 701, 709, 719, 727,
1914 733, 739, 743, 751, 757, 761, 769, 773,
1915 787, 797, 809, 811, 821, 823, 827, 829,
1916 839, 853, 857, 859, 863, 877, 881, 883,
1917 887, 907, 911, 919, 929, 937, 941, 947,
1918 953, 967, 971, 977, 983, 991, 997, -103
1919};
1920
1921/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01001922 * Small divisors test (X must be positive)
1923 *
1924 * Return values:
1925 * 0: no small factor (possible prime, more tests needed)
1926 * 1: certain prime
1927 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
1928 * other negative: error
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 */
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01001930static int mpi_check_small_factors( const mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +00001931{
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01001932 int ret = 0;
1933 size_t i;
1934 t_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +00001935
Paul Bakker5121ce52009-01-03 21:22:43 +00001936 if( ( X->p[0] & 1 ) == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +00001937 return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001938
1939 for( i = 0; small_prime[i] > 0; i++ )
1940 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001941 if( mpi_cmp_int( X, small_prime[i] ) <= 0 )
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01001942 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001943
1944 MPI_CHK( mpi_mod_int( &r, X, small_prime[i] ) );
1945
1946 if( r == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +00001947 return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001948 }
1949
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01001950cleanup:
1951 return( ret );
1952}
1953
1954/*
1955 * Miller-Rabin pseudo-primality test (HAC 4.24)
1956 */
1957static int mpi_miller_rabin( const mpi *X,
1958 int (*f_rng)(void *, unsigned char *, size_t),
1959 void *p_rng )
1960{
Pascal Junodb99183d2015-03-11 16:49:45 +01001961 int ret, count;
1962 size_t i, j, k, n, s;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01001963 mpi W, R, T, A, RR;
1964
1965 mpi_init( &W ); mpi_init( &R ); mpi_init( &T ); mpi_init( &A );
1966 mpi_init( &RR );
1967
Paul Bakker5121ce52009-01-03 21:22:43 +00001968 /*
1969 * W = |X| - 1
1970 * R = W >> lsb( W )
1971 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 MPI_CHK( mpi_sub_int( &W, X, 1 ) );
Paul Bakker41d13f42010-03-16 21:26:36 +00001973 s = mpi_lsb( &W );
Paul Bakker5121ce52009-01-03 21:22:43 +00001974 MPI_CHK( mpi_copy( &R, &W ) );
1975 MPI_CHK( mpi_shift_r( &R, s ) );
1976
1977 i = mpi_msb( X );
1978 /*
1979 * HAC, table 4.4
1980 */
1981 n = ( ( i >= 1300 ) ? 2 : ( i >= 850 ) ? 3 :
1982 ( i >= 650 ) ? 4 : ( i >= 350 ) ? 8 :
1983 ( i >= 250 ) ? 12 : ( i >= 150 ) ? 18 : 27 );
1984
1985 for( i = 0; i < n; i++ )
1986 {
1987 /*
1988 * pick a random A, 1 < A < |X| - 1
1989 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001990
Pascal Junodb99183d2015-03-11 16:49:45 +01001991 count = 0;
1992 do {
1993 MPI_CHK( mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
1994
1995 j = mpi_msb( &A );
1996 k = mpi_msb( &W );
1997 if (j > k) {
1998 MPI_CHK( mpi_shift_r( &A, j - k ) );
1999 }
2000
2001 if (count++ > 30) {
2002 return POLARSSL_ERR_MPI_NOT_ACCEPTABLE;
2003 }
2004
2005 } while ( (mpi_cmp_mpi( &A, &W ) >= 0) ||
2006 (mpi_cmp_int( &A, 1 ) <= 0) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002007
2008 /*
2009 * A = A^R mod |X|
2010 */
2011 MPI_CHK( mpi_exp_mod( &A, &A, &R, X, &RR ) );
2012
2013 if( mpi_cmp_mpi( &A, &W ) == 0 ||
2014 mpi_cmp_int( &A, 1 ) == 0 )
2015 continue;
2016
2017 j = 1;
2018 while( j < s && mpi_cmp_mpi( &A, &W ) != 0 )
2019 {
2020 /*
2021 * A = A * A mod |X|
2022 */
2023 MPI_CHK( mpi_mul_mpi( &T, &A, &A ) );
2024 MPI_CHK( mpi_mod_mpi( &A, &T, X ) );
2025
2026 if( mpi_cmp_int( &A, 1 ) == 0 )
2027 break;
2028
2029 j++;
2030 }
2031
2032 /*
2033 * not prime if A != |X| - 1 or A == 1
2034 */
2035 if( mpi_cmp_mpi( &A, &W ) != 0 ||
2036 mpi_cmp_int( &A, 1 ) == 0 )
2037 {
Paul Bakker40e46942009-01-03 21:51:57 +00002038 ret = POLARSSL_ERR_MPI_NOT_ACCEPTABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002039 break;
2040 }
2041 }
2042
2043cleanup:
Paul Bakker6c591fa2011-05-05 11:49:20 +00002044 mpi_free( &W ); mpi_free( &R ); mpi_free( &T ); mpi_free( &A );
2045 mpi_free( &RR );
Paul Bakker5121ce52009-01-03 21:22:43 +00002046
2047 return( ret );
2048}
2049
2050/*
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002051 * Pseudo-primality test: small factors, then Miller-Rabin
2052 */
Paul Bakker45f457d2013-11-25 14:26:52 +01002053int mpi_is_prime( mpi *X,
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002054 int (*f_rng)(void *, unsigned char *, size_t),
2055 void *p_rng )
2056{
2057 int ret;
Manuel Pégourié-Gonnard7f4ed672014-10-14 20:56:02 +02002058 mpi XX;
2059
2060 XX.s = 1;
2061 XX.n = X->n;
2062 XX.p = X->p;
Manuel Pégourié-Gonnard378fb4b2013-11-22 18:39:18 +01002063
2064 if( mpi_cmp_int( &XX, 0 ) == 0 ||
2065 mpi_cmp_int( &XX, 1 ) == 0 )
2066 return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
2067
2068 if( mpi_cmp_int( &XX, 2 ) == 0 )
2069 return( 0 );
2070
2071 if( ( ret = mpi_check_small_factors( &XX ) ) != 0 )
2072 {
2073 if( ret == 1 )
2074 return( 0 );
2075
2076 return( ret );
2077 }
2078
2079 return( mpi_miller_rabin( &XX, f_rng, p_rng ) );
2080}
2081
2082/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 * Prime number generation
2084 */
Paul Bakker23986e52011-04-24 08:57:21 +00002085int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +00002086 int (*f_rng)(void *, unsigned char *, size_t),
2087 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002088{
Paul Bakker23986e52011-04-24 08:57:21 +00002089 int ret;
2090 size_t k, n;
Manuel Pégourié-Gonnard0160eac2013-11-22 17:54:59 +01002091 t_uint r;
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 mpi Y;
2093
Paul Bakkerfe3256e2011-11-25 12:11:43 +00002094 if( nbits < 3 || nbits > POLARSSL_MPI_MAX_BITS )
Paul Bakker40e46942009-01-03 21:51:57 +00002095 return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002096
Paul Bakker6c591fa2011-05-05 11:49:20 +00002097 mpi_init( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002098
2099 n = BITS_TO_LIMBS( nbits );
2100
Paul Bakker901c6562012-04-20 13:25:38 +00002101 MPI_CHK( mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002102
2103 k = mpi_msb( X );
Pascal Junodb99183d2015-03-11 16:49:45 +01002104 if( k > nbits ) MPI_CHK( mpi_shift_r( X, k - nbits + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002105
Pascal Junodb99183d2015-03-11 16:49:45 +01002106 mpi_set_bit( X, nbits-1, 1 );
2107
2108 X->p[0] |= 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002109
2110 if( dh_flag == 0 )
2111 {
2112 while( ( ret = mpi_is_prime( X, f_rng, p_rng ) ) != 0 )
2113 {
Paul Bakker40e46942009-01-03 21:51:57 +00002114 if( ret != POLARSSL_ERR_MPI_NOT_ACCEPTABLE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 goto cleanup;
2116
2117 MPI_CHK( mpi_add_int( X, X, 2 ) );
2118 }
2119 }
2120 else
2121 {
Manuel Pégourié-Gonnard0160eac2013-11-22 17:54:59 +01002122 /*
2123 * An necessary condition for Y and X = 2Y + 1 to be prime
2124 * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
2125 * Make sure it is satisfied, while keeping X = 3 mod 4
2126 */
Pascal Junodb99183d2015-03-11 16:49:45 +01002127
2128 X->p[0] |= 2;
2129
Manuel Pégourié-Gonnard0160eac2013-11-22 17:54:59 +01002130 MPI_CHK( mpi_mod_int( &r, X, 3 ) );
2131 if( r == 0 )
2132 MPI_CHK( mpi_add_int( X, X, 8 ) );
2133 else if( r == 1 )
2134 MPI_CHK( mpi_add_int( X, X, 4 ) );
2135
2136 /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
2137 MPI_CHK( mpi_copy( &Y, X ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 MPI_CHK( mpi_shift_r( &Y, 1 ) );
2139
2140 while( 1 )
2141 {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002142 /*
2143 * First, check small factors for X and Y
2144 * before doing Miller-Rabin on any of them
2145 */
2146 if( ( ret = mpi_check_small_factors( X ) ) == 0 &&
2147 ( ret = mpi_check_small_factors( &Y ) ) == 0 &&
2148 ( ret = mpi_miller_rabin( X, f_rng, p_rng ) ) == 0 &&
2149 ( ret = mpi_miller_rabin( &Y, f_rng, p_rng ) ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 {
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002151 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 }
2153
Paul Bakker40e46942009-01-03 21:51:57 +00002154 if( ret != POLARSSL_ERR_MPI_NOT_ACCEPTABLE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002155 goto cleanup;
2156
Manuel Pégourié-Gonnard0160eac2013-11-22 17:54:59 +01002157 /*
Manuel Pégourié-Gonnardddf76152013-11-22 19:58:22 +01002158 * Next candidates. We want to preserve Y = (X-1) / 2 and
Manuel Pégourié-Gonnard0160eac2013-11-22 17:54:59 +01002159 * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
2160 * so up Y by 6 and X by 12.
2161 */
2162 MPI_CHK( mpi_add_int( X, X, 12 ) );
2163 MPI_CHK( mpi_add_int( &Y, &Y, 6 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002164 }
2165 }
2166
2167cleanup:
2168
Paul Bakker6c591fa2011-05-05 11:49:20 +00002169 mpi_free( &Y );
Paul Bakker5121ce52009-01-03 21:22:43 +00002170
2171 return( ret );
2172}
2173
Manuel Pégourié-Gonnarddf0142b2013-08-22 18:29:07 +02002174#endif /* POLARSSL_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002175
Paul Bakker40e46942009-01-03 21:51:57 +00002176#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00002177
Paul Bakker23986e52011-04-24 08:57:21 +00002178#define GCD_PAIR_COUNT 3
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002179
2180static const int gcd_pairs[GCD_PAIR_COUNT][3] =
2181{
2182 { 693, 609, 21 },
2183 { 1764, 868, 28 },
2184 { 768454923, 542167814, 1 }
2185};
2186
Paul Bakker5121ce52009-01-03 21:22:43 +00002187/*
2188 * Checkup routine
2189 */
2190int mpi_self_test( int verbose )
2191{
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002192 int ret, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002193 mpi A, E, N, X, Y, U, V;
2194
Paul Bakker6c591fa2011-05-05 11:49:20 +00002195 mpi_init( &A ); mpi_init( &E ); mpi_init( &N ); mpi_init( &X );
2196 mpi_init( &Y ); mpi_init( &U ); mpi_init( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00002197
2198 MPI_CHK( mpi_read_string( &A, 16,
2199 "EFE021C2645FD1DC586E69184AF4A31E" \
2200 "D5F53E93B5F123FA41680867BA110131" \
2201 "944FE7952E2517337780CB0DB80E61AA" \
2202 "E7C8DDC6C5C6AADEB34EB38A2F40D5E6" ) );
2203
2204 MPI_CHK( mpi_read_string( &E, 16,
2205 "B2E7EFD37075B9F03FF989C7C5051C20" \
2206 "34D2A323810251127E7BF8625A4F49A5" \
2207 "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
2208 "5B5C25763222FEFCCFC38B832366C29E" ) );
2209
2210 MPI_CHK( mpi_read_string( &N, 16,
2211 "0066A198186C18C10B2F5ED9B522752A" \
2212 "9830B69916E535C8F047518A889A43A5" \
2213 "94B6BED27A168D31D4A52F88925AA8F5" ) );
2214
2215 MPI_CHK( mpi_mul_mpi( &X, &A, &N ) );
2216
2217 MPI_CHK( mpi_read_string( &U, 16,
2218 "602AB7ECA597A3D6B56FF9829A5E8B85" \
2219 "9E857EA95A03512E2BAE7391688D264A" \
2220 "A5663B0341DB9CCFD2C4C5F421FEC814" \
2221 "8001B72E848A38CAE1C65F78E56ABDEF" \
2222 "E12D3C039B8A02D6BE593F0BBBDA56F1" \
2223 "ECF677152EF804370C1A305CAF3B5BF1" \
2224 "30879B56C61DE584A0F53A2447A51E" ) );
2225
2226 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002227 polarssl_printf( " MPI test #1 (mul_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002228
2229 if( mpi_cmp_mpi( &X, &U ) != 0 )
2230 {
2231 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002232 polarssl_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002233
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002234 ret = 1;
2235 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 }
2237
2238 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002239 polarssl_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002240
2241 MPI_CHK( mpi_div_mpi( &X, &Y, &A, &N ) );
2242
2243 MPI_CHK( mpi_read_string( &U, 16,
2244 "256567336059E52CAE22925474705F39A94" ) );
2245
2246 MPI_CHK( mpi_read_string( &V, 16,
2247 "6613F26162223DF488E9CD48CC132C7A" \
2248 "0AC93C701B001B092E4E5B9F73BCD27B" \
2249 "9EE50D0657C77F374E903CDFA4C642" ) );
2250
2251 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002252 polarssl_printf( " MPI test #2 (div_mpi): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002253
2254 if( mpi_cmp_mpi( &X, &U ) != 0 ||
2255 mpi_cmp_mpi( &Y, &V ) != 0 )
2256 {
2257 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002258 polarssl_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002259
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002260 ret = 1;
2261 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 }
2263
2264 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002265 polarssl_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002266
2267 MPI_CHK( mpi_exp_mod( &X, &A, &E, &N, NULL ) );
2268
2269 MPI_CHK( mpi_read_string( &U, 16,
2270 "36E139AEA55215609D2816998ED020BB" \
2271 "BD96C37890F65171D948E9BC7CBAA4D9" \
2272 "325D24D6A3C12710F10A09FA08AB87" ) );
2273
2274 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002275 polarssl_printf( " MPI test #3 (exp_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002276
2277 if( mpi_cmp_mpi( &X, &U ) != 0 )
2278 {
2279 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002280 polarssl_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002281
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002282 ret = 1;
2283 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 }
2285
2286 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002287 polarssl_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002288
2289 MPI_CHK( mpi_inv_mod( &X, &A, &N ) );
2290
2291 MPI_CHK( mpi_read_string( &U, 16,
2292 "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
2293 "C3DBA76456363A10869622EAC2DD84EC" \
2294 "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) );
2295
2296 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002297 polarssl_printf( " MPI test #4 (inv_mod): " );
Paul Bakker5121ce52009-01-03 21:22:43 +00002298
2299 if( mpi_cmp_mpi( &X, &U ) != 0 )
2300 {
2301 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002302 polarssl_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002303
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002304 ret = 1;
2305 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00002306 }
2307
2308 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002309 polarssl_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002310
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002311 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002312 polarssl_printf( " MPI test #5 (simple gcd): " );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002313
Paul Bakker66d5d072014-06-17 16:39:18 +02002314 for( i = 0; i < GCD_PAIR_COUNT; i++ )
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002315 {
2316 MPI_CHK( mpi_lset( &X, gcd_pairs[i][0] ) );
Paul Bakker23986e52011-04-24 08:57:21 +00002317 MPI_CHK( mpi_lset( &Y, gcd_pairs[i][1] ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002318
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002319 MPI_CHK( mpi_gcd( &A, &X, &Y ) );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002320
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002321 if( mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 )
2322 {
2323 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002324 polarssl_printf( "failed at %d\n", i );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002325
Manuel Pégourié-Gonnard9e987ed2014-01-20 10:03:15 +01002326 ret = 1;
2327 goto cleanup;
2328 }
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002329 }
2330
2331 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002332 polarssl_printf( "passed\n" );
Paul Bakker4e0d7ca2009-01-29 22:24:33 +00002333
Paul Bakker5121ce52009-01-03 21:22:43 +00002334cleanup:
2335
2336 if( ret != 0 && verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002337 polarssl_printf( "Unexpected error, return code = %08X\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002338
Paul Bakker6c591fa2011-05-05 11:49:20 +00002339 mpi_free( &A ); mpi_free( &E ); mpi_free( &N ); mpi_free( &X );
2340 mpi_free( &Y ); mpi_free( &U ); mpi_free( &V );
Paul Bakker5121ce52009-01-03 21:22:43 +00002341
2342 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002343 polarssl_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00002344
2345 return( ret );
2346}
2347
Paul Bakker9af723c2014-05-01 13:03:14 +02002348#endif /* POLARSSL_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00002349
Paul Bakker9af723c2014-05-01 13:03:14 +02002350#endif /* POLARSSL_BIGNUM_C */