blob: 1e579e99e91785b32c00f0e27da9021a75f51c82 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/bignum.h"
Gilles Peskine3cb1e292020-11-25 15:37:20 +01003#include "mbedtls/entropy.h"
Janos Follathf1d617d2022-07-21 09:29:32 +01004#include "bignum_core.h"
Gabor Mezei23a1ce92022-08-02 11:54:44 +02005#include "bignum_mod.h"
6#include "bignum_mod_raw.h"
Janos Follath23bdeca2022-07-22 18:24:06 +01007#include "constant_time_internal.h"
8#include "test/constant_flow.h"
Janos Follath64eca052018-09-05 17:04:49 +01009
Chris Jonese64a46f2020-12-03 17:44:03 +000010#if MBEDTLS_MPI_MAX_BITS > 792
11#define MPI_MAX_BITS_LARGER_THAN_792
Chris Jones4592bd82020-12-03 14:24:33 +000012#endif
Gabor Mezei89e31462022-08-12 15:36:56 +020013
Gilles Peskinedffc7102021-06-10 15:34:15 +020014/* Check the validity of the sign bit in an MPI object. Reject representations
15 * that are not supported by the rest of the library and indicate a bug when
16 * constructing the value. */
17static int sign_is_valid( const mbedtls_mpi *X )
18{
19 if( X->s != 1 && X->s != -1 )
20 return( 0 ); // invalid sign bit, e.g. 0
21 if( mbedtls_mpi_bitlen( X ) == 0 && X->s != 1 )
22 return( 0 ); // negative zero
23 return( 1 );
24}
25
Janos Follath64eca052018-09-05 17:04:49 +010026typedef struct mbedtls_test_mpi_random
27{
28 data_t *data;
29 size_t pos;
30 size_t chunk_len;
31} mbedtls_test_mpi_random;
32
33/*
34 * This function is called by the Miller-Rabin primality test each time it
35 * chooses a random witness. The witnesses (or non-witnesses as provided by the
36 * test) are stored in the data member of the state structure. Each number is in
37 * the format that mbedtls_mpi_read_string understands and is chunk_len long.
38 */
39int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
40 unsigned char* buf,
41 size_t len )
42{
43 mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
44
45 if( random == NULL || random->data->x == NULL || buf == NULL )
46 return( -1 );
47
48 if( random->pos + random->chunk_len > random->data->len
49 || random->chunk_len > len )
50 {
51 return( -1 );
52 }
53
54 memset( buf, 0, len );
55
56 /* The witness is written to the end of the buffer, since the buffer is
57 * used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
58 * Writing the witness to the start of the buffer would result in the
59 * buffer being 'witness 000...000', which would be treated as
60 * witness * 2^n for some n. */
61 memcpy( buf + len - random->chunk_len, &random->data->x[random->pos],
62 random->chunk_len );
63
64 random->pos += random->chunk_len;
65
66 return( 0 );
67}
Gilles Peskine3cb1e292020-11-25 15:37:20 +010068
69/* Random generator that is told how many bytes to return. */
70static int f_rng_bytes_left( void *state, unsigned char *buf, size_t len )
71{
72 size_t *bytes_left = state;
73 size_t i;
74 for( i = 0; i < len; i++ )
75 {
76 if( *bytes_left == 0 )
77 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
78 buf[i] = *bytes_left & 0xff;
79 --( *bytes_left );
80 }
81 return( 0 );
82}
83
Gilles Peskineeedefa52021-04-13 19:50:04 +020084/* Test whether bytes represents (in big-endian base 256) a number b that
85 * is significantly above a power of 2. That is, b must not have a long run
86 * of unset bits after the most significant bit.
87 *
88 * Let n be the bit-size of b, i.e. the integer such that 2^n <= b < 2^{n+1}.
89 * This function returns 1 if, when drawing a number between 0 and b,
90 * the probability that this number is at least 2^n is not negligible.
91 * This probability is (b - 2^n) / b and this function checks that this
92 * number is above some threshold A. The threshold value is heuristic and
93 * based on the needs of mpi_random_many().
Gilles Peskine02ac93a2021-03-29 22:02:55 +020094 */
95static int is_significantly_above_a_power_of_2( data_t *bytes )
96{
97 const uint8_t *p = bytes->x;
98 size_t len = bytes->len;
99 unsigned x;
Gilles Peskineeedefa52021-04-13 19:50:04 +0200100
101 /* Skip leading null bytes */
Gilles Peskine02ac93a2021-03-29 22:02:55 +0200102 while( len > 0 && p[0] == 0 )
103 {
104 ++p;
105 --len;
106 }
Gilles Peskineeedefa52021-04-13 19:50:04 +0200107 /* 0 is not significantly above a power of 2 */
Gilles Peskine02ac93a2021-03-29 22:02:55 +0200108 if( len == 0 )
109 return( 0 );
Gilles Peskineeedefa52021-04-13 19:50:04 +0200110 /* Extract the (up to) 2 most significant bytes */
111 if( len == 1 )
Gilles Peskine02ac93a2021-03-29 22:02:55 +0200112 x = p[0];
113 else
114 x = ( p[0] << 8 ) | p[1];
115
Gilles Peskineeedefa52021-04-13 19:50:04 +0200116 /* Shift the most significant bit of x to position 8 and mask it out */
117 while( ( x & 0xfe00 ) != 0 )
118 x >>= 1;
119 x &= 0x00ff;
Gilles Peskine02ac93a2021-03-29 22:02:55 +0200120
Gilles Peskineeedefa52021-04-13 19:50:04 +0200121 /* At this point, x = floor((b - 2^n) / 2^(n-8)). b is significantly above
122 * a power of 2 iff x is significantly above 0 compared to 2^8.
123 * Testing x >= 2^4 amounts to picking A = 1/16 in the function
124 * description above. */
125 return( x >= 0x10 );
Gilles Peskine02ac93a2021-03-29 22:02:55 +0200126}
127
Paul Bakker33b43f12013-08-20 11:48:36 +0200128/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +0000129
Paul Bakker33b43f12013-08-20 11:48:36 +0200130/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200132 * END_DEPENDENCIES
133 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000134
Hanno Beckerb48e1aa2018-12-18 23:25:01 +0000135/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100136void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200137{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200138 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200139
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200140 mbedtls_mpi_init( &X );
141 mbedtls_mpi_init( &Y );
142 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200143
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200144 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
145 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200146 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200147 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200148
149exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200150 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200151}
152/* END_CASE */
153
154/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100155void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
156 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +0200157 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +0000158{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000160 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100161 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +0000162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000164
Janos Follath04dadb72019-03-06 12:29:37 +0000165 memset( str, '!', sizeof( str ) );
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200168 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000169 {
Gilles Peskinedffc7102021-06-10 15:34:15 +0200170 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100171 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200172 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000173 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200174 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Janos Follath04dadb72019-03-06 12:29:37 +0000175 TEST_ASSERT( str[len] == '!' );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000176 }
177 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000178
Paul Bakkerbd51b262014-07-10 15:26:12 +0200179exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000181}
Paul Bakker33b43f12013-08-20 11:48:36 +0200182/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000183
Paul Bakker33b43f12013-08-20 11:48:36 +0200184/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100185void mpi_read_binary( data_t * buf, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000186{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000188 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100189 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000192
Paul Bakkere896fea2009-07-06 06:40:23 +0000193
Azim Khand30ca132017-06-09 04:32:58 +0100194 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200195 TEST_ASSERT( sign_is_valid( &X ) );
Werner Lewisf65a3272022-07-07 11:38:44 +0100196 TEST_ASSERT( mbedtls_mpi_write_string( &X, 16, str, sizeof( str ), &len ) == 0 );
Werner Lewisdc47fe72022-08-01 13:55:41 +0100197 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000198
Paul Bakkerbd51b262014-07-10 15:26:12 +0200199exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000201}
Paul Bakker33b43f12013-08-20 11:48:36 +0200202/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000203
Paul Bakker33b43f12013-08-20 11:48:36 +0200204/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100205void mpi_core_io_null()
Janos Follath91dc67d2022-07-22 14:24:58 +0100206{
207 mbedtls_mpi_uint X = 0;
208 int ret;
209
210 ret = mbedtls_mpi_core_read_be( &X, 1, NULL, 0 );
Janos Follath494a6d22022-08-22 09:36:17 +0100211 TEST_EQUAL( ret, 0 );
Janos Follath91dc67d2022-07-22 14:24:58 +0100212 ret = mbedtls_mpi_core_write_be( &X, 1, NULL, 0 );
Janos Follath494a6d22022-08-22 09:36:17 +0100213 TEST_EQUAL( ret, 0 );
Janos Follath91dc67d2022-07-22 14:24:58 +0100214
215 ret = mbedtls_mpi_core_read_be( NULL, 0, NULL, 0 );
Janos Follath494a6d22022-08-22 09:36:17 +0100216 TEST_EQUAL( ret, 0 );
Janos Follath91dc67d2022-07-22 14:24:58 +0100217 ret = mbedtls_mpi_core_write_be( NULL, 0, NULL, 0 );
Janos Follath494a6d22022-08-22 09:36:17 +0100218 TEST_EQUAL( ret, 0 );
Janos Follath91dc67d2022-07-22 14:24:58 +0100219
220 ret = mbedtls_mpi_core_read_le( &X, 1, NULL, 0 );
Janos Follath494a6d22022-08-22 09:36:17 +0100221 TEST_EQUAL( ret, 0 );
Janos Follath91dc67d2022-07-22 14:24:58 +0100222 ret = mbedtls_mpi_core_write_le( &X, 1, NULL, 0 );
Janos Follath494a6d22022-08-22 09:36:17 +0100223 TEST_EQUAL( ret, 0 );
Janos Follath91dc67d2022-07-22 14:24:58 +0100224
225 ret = mbedtls_mpi_core_read_le( NULL, 0, NULL, 0 );
Janos Follath494a6d22022-08-22 09:36:17 +0100226 TEST_EQUAL( ret, 0 );
Janos Follath91dc67d2022-07-22 14:24:58 +0100227 ret = mbedtls_mpi_core_write_le( NULL, 0, NULL, 0 );
Janos Follath494a6d22022-08-22 09:36:17 +0100228 TEST_EQUAL( ret, 0 );
Janos Follath91dc67d2022-07-22 14:24:58 +0100229
230exit:
231 ;
232}
233/* END_CASE */
234
235/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100236void mpi_core_io_be( data_t *input, int nb_int, int nx_32_int, int iret,
237 int oret )
Janos Follathf1d617d2022-07-21 09:29:32 +0100238{
Janos Follathf1d617d2022-07-21 09:29:32 +0100239 if( iret != 0 )
240 TEST_ASSERT( oret == 0 );
241
Tom Cosgrove1feb5ac2022-09-15 14:22:35 +0100242 TEST_LE_S( 0, nb_int );
Janos Follath81620642022-08-15 11:13:38 +0100243 size_t nb = nb_int;
Janos Follathf1d617d2022-07-21 09:29:32 +0100244
Janos Follath81620642022-08-15 11:13:38 +0100245 unsigned char buf[1024];
Tom Cosgrove1feb5ac2022-09-15 14:22:35 +0100246 TEST_LE_U( nb, sizeof( buf ) );
Janos Follath81620642022-08-15 11:13:38 +0100247
Janos Follath1cb3b972022-08-11 10:50:04 +0100248 /* nx_32_int is the number of 32 bit limbs, if we have 64 bit limbs we need
249 * to halve the number of limbs to have the same size. */
Janos Follath6b8e0c22022-08-22 09:54:25 +0100250 size_t nx;
Tom Cosgrove1feb5ac2022-09-15 14:22:35 +0100251 TEST_LE_S( 0, nx_32_int );
Janos Follath6b8e0c22022-08-22 09:54:25 +0100252 if( sizeof( mbedtls_mpi_uint ) == 8 )
253 nx = nx_32_int / 2 + nx_32_int % 2;
254 else
255 nx = nx_32_int;
Janos Follathf1d617d2022-07-21 09:29:32 +0100256
Janos Follath81620642022-08-15 11:13:38 +0100257 mbedtls_mpi_uint X[sizeof( buf ) / sizeof( mbedtls_mpi_uint )];
Tom Cosgrove1feb5ac2022-09-15 14:22:35 +0100258 TEST_LE_U( nx, sizeof( X ) / sizeof( X[0] ) );
Janos Follath81620642022-08-15 11:13:38 +0100259
260 int ret = mbedtls_mpi_core_read_be( X, nx, input->x, input->len );
Janos Follath494a6d22022-08-22 09:36:17 +0100261 TEST_EQUAL( ret, iret );
Janos Follathf1d617d2022-07-21 09:29:32 +0100262
263 if( iret == 0 )
264 {
265 ret = mbedtls_mpi_core_write_be( X, nx, buf, nb );
Janos Follath494a6d22022-08-22 09:36:17 +0100266 TEST_EQUAL( ret, oret );
Janos Follathf1d617d2022-07-21 09:29:32 +0100267 }
268
269 if( ( iret == 0 ) && ( oret == 0 ) )
270 {
271 if( nb > input->len )
272 {
273 size_t leading_zeroes = nb - input->len;
274 TEST_ASSERT( memcmp( buf + nb - input->len, input->x, input->len ) == 0 );
275 for( size_t i = 0; i < leading_zeroes; i++ )
Janos Follath494a6d22022-08-22 09:36:17 +0100276 TEST_EQUAL( buf[i], 0 );
Janos Follathf1d617d2022-07-21 09:29:32 +0100277 }
278 else
279 {
280 size_t leading_zeroes = input->len - nb;
281 TEST_ASSERT( memcmp( input->x + input->len - nb, buf, nb ) == 0 );
282 for( size_t i = 0; i < leading_zeroes; i++ )
Janos Follath494a6d22022-08-22 09:36:17 +0100283 TEST_EQUAL( input->x[i], 0 );
Janos Follathf1d617d2022-07-21 09:29:32 +0100284 }
285 }
286
287exit:
288 ;
Janos Follathf1d617d2022-07-21 09:29:32 +0100289}
290/* END_CASE */
291
292/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100293void mpi_core_io_le( data_t *input, int nb_int, int nx_32_int, int iret,
294 int oret )
Janos Follath6ff35362022-07-21 15:27:21 +0100295{
Janos Follath6ff35362022-07-21 15:27:21 +0100296 if( iret != 0 )
297 TEST_ASSERT( oret == 0 );
298
Tom Cosgrove1feb5ac2022-09-15 14:22:35 +0100299 TEST_LE_S( 0, nb_int );
Janos Follath81620642022-08-15 11:13:38 +0100300 size_t nb = nb_int;
Janos Follath6ff35362022-07-21 15:27:21 +0100301
Janos Follath81620642022-08-15 11:13:38 +0100302 unsigned char buf[1024];
Tom Cosgrove1feb5ac2022-09-15 14:22:35 +0100303 TEST_LE_U( nb, sizeof( buf ) );
Janos Follath81620642022-08-15 11:13:38 +0100304
Janos Follath9dfb5622022-08-11 12:15:55 +0100305 /* nx_32_int is the number of 32 bit limbs, if we have 64 bit limbs we need
306 * to halve the number of limbs to have the same size. */
Janos Follath6b8e0c22022-08-22 09:54:25 +0100307 size_t nx;
Tom Cosgrove1feb5ac2022-09-15 14:22:35 +0100308 TEST_LE_S( 0, nx_32_int );
Janos Follath6b8e0c22022-08-22 09:54:25 +0100309 if( sizeof( mbedtls_mpi_uint ) == 8 )
310 nx = nx_32_int / 2 + nx_32_int % 2;
311 else
312 nx = nx_32_int;
Janos Follath6ff35362022-07-21 15:27:21 +0100313
Janos Follath81620642022-08-15 11:13:38 +0100314 mbedtls_mpi_uint X[sizeof( buf ) / sizeof( mbedtls_mpi_uint )];
Tom Cosgrove1feb5ac2022-09-15 14:22:35 +0100315 TEST_LE_U( nx, sizeof( X ) / sizeof( X[0] ) );
Janos Follath81620642022-08-15 11:13:38 +0100316
317 int ret = mbedtls_mpi_core_read_le( X, nx, input->x, input->len );
Janos Follath494a6d22022-08-22 09:36:17 +0100318 TEST_EQUAL( ret, iret );
Janos Follath6ff35362022-07-21 15:27:21 +0100319
320 if( iret == 0 )
321 {
322 ret = mbedtls_mpi_core_write_le( X, nx, buf, nb );
Janos Follath494a6d22022-08-22 09:36:17 +0100323 TEST_EQUAL( ret, oret );
Janos Follath6ff35362022-07-21 15:27:21 +0100324 }
325
326 if( ( iret == 0 ) && ( oret == 0 ) )
327 {
328 if( nb > input->len )
329 {
330 TEST_ASSERT( memcmp( buf, input->x, input->len ) == 0 );
331 for( size_t i = input->len; i < nb; i++ )
Janos Follath494a6d22022-08-22 09:36:17 +0100332 TEST_EQUAL( buf[i], 0 );
Janos Follath6ff35362022-07-21 15:27:21 +0100333 }
334 else
335 {
336 TEST_ASSERT( memcmp( input->x, buf, nb ) == 0 );
337 for( size_t i = nb; i < input->len; i++ )
Janos Follath494a6d22022-08-22 09:36:17 +0100338 TEST_EQUAL( input->x[i], 0 );
Janos Follath6ff35362022-07-21 15:27:21 +0100339 }
340 }
341
342exit:
343 ;
Janos Follath6ff35362022-07-21 15:27:21 +0100344}
345/* END_CASE */
346
347/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100348void mpi_mod_setup( int ext_rep, int int_rep, int iret )
Janos Follath16949692022-08-08 13:37:20 +0100349{
350 #define MLIMBS 8
351 mbedtls_mpi_uint mp[MLIMBS];
352 mbedtls_mpi_mod_modulus m;
353 int ret;
354
355 memset( mp, 0xFF, sizeof(mp) );
356
357 mbedtls_mpi_mod_modulus_init( &m );
358 ret = mbedtls_mpi_mod_modulus_setup( &m, mp, MLIMBS, ext_rep, int_rep );
Janos Follath494a6d22022-08-22 09:36:17 +0100359 TEST_EQUAL( ret, iret );
Janos Follath16949692022-08-08 13:37:20 +0100360
361 /* Address sanitiser should catch if we try to free mp */
362 mbedtls_mpi_mod_modulus_free( &m );
363
364 /* Make sure that the modulus doesn't have reference to mp anymore */
365 TEST_ASSERT( m.p != mp );
366
367exit:
368 /* It should be safe to call an mbedtls free several times */
369 mbedtls_mpi_mod_modulus_free( &m );
370
371 #undef MLIMBS
372}
373/* END_CASE */
374
375
376/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100377void mpi_mod_raw_io( data_t *input, int nb_int, int nx_32_int,
378 int iendian, int iret, int oret )
Gabor Mezei23a1ce92022-08-02 11:54:44 +0200379{
Gabor Mezei23a1ce92022-08-02 11:54:44 +0200380 if( iret != 0 )
381 TEST_ASSERT( oret == 0 );
382
Tom Cosgrove1feb5ac2022-09-15 14:22:35 +0100383 TEST_LE_S( 0, nb_int );
Janos Follath81620642022-08-15 11:13:38 +0100384 size_t nb = nb_int;
Gabor Mezei23a1ce92022-08-02 11:54:44 +0200385
Janos Follath81620642022-08-15 11:13:38 +0100386 unsigned char buf[1024];
Tom Cosgrove1feb5ac2022-09-15 14:22:35 +0100387 TEST_LE_U( nb, sizeof( buf ) );
Janos Follath81620642022-08-15 11:13:38 +0100388
Gabor Mezei7f081782022-08-12 18:00:33 +0200389 /* nx_32_int is the number of 32 bit limbs, if we have 64 bit limbs we need
390 * to halve the number of limbs to have the same size. */
Janos Follath6b8e0c22022-08-22 09:54:25 +0100391 size_t nx;
Tom Cosgrove1feb5ac2022-09-15 14:22:35 +0100392 TEST_LE_S( 0, nx_32_int );
Janos Follath6b8e0c22022-08-22 09:54:25 +0100393 if( sizeof( mbedtls_mpi_uint ) == 8 )
394 nx = nx_32_int / 2 + nx_32_int % 2;
395 else
396 nx = nx_32_int;
Gabor Mezei23a1ce92022-08-02 11:54:44 +0200397
Janos Follath81620642022-08-15 11:13:38 +0100398 mbedtls_mpi_uint X[sizeof( buf ) / sizeof( mbedtls_mpi_uint )];
Tom Cosgrove1feb5ac2022-09-15 14:22:35 +0100399 TEST_LE_U( nx, sizeof( X ) / sizeof( X[0] ) );
Janos Follath81620642022-08-15 11:13:38 +0100400
401 int endian;
Gabor Mezei23a1ce92022-08-02 11:54:44 +0200402 if( iendian == MBEDTLS_MPI_MOD_EXT_REP_INVALID )
403 endian = MBEDTLS_MPI_MOD_EXT_REP_LE;
404 else
405 endian = iendian;
406
Janos Follath81620642022-08-15 11:13:38 +0100407 mbedtls_mpi_mod_modulus m;
Gabor Mezei23a1ce92022-08-02 11:54:44 +0200408 mbedtls_mpi_mod_modulus_init( &m );
Janos Follath81620642022-08-15 11:13:38 +0100409 mbedtls_mpi_uint init[sizeof( X ) / sizeof( X[0] )];
410 memset( init, 0xFF, sizeof( init ) );
411 int ret = mbedtls_mpi_mod_modulus_setup( &m, init, nx, endian,
412 MBEDTLS_MPI_MOD_REP_MONTGOMERY );
Janos Follath494a6d22022-08-22 09:36:17 +0100413 TEST_EQUAL( ret, 0 );
Gabor Mezei23a1ce92022-08-02 11:54:44 +0200414
415 if( iendian == MBEDTLS_MPI_MOD_EXT_REP_INVALID && iret != 0 )
416 m.ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
417
418 ret = mbedtls_mpi_mod_raw_read( X, &m, input->x, input->len );
Janos Follath494a6d22022-08-22 09:36:17 +0100419 TEST_EQUAL( ret, iret );
Gabor Mezei23a1ce92022-08-02 11:54:44 +0200420
421 if( iret == 0 )
422 {
423 if( iendian == MBEDTLS_MPI_MOD_EXT_REP_INVALID && oret != 0 )
424 m.ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
425
426 ret = mbedtls_mpi_mod_raw_write( X, &m, buf, nb );
Janos Follath494a6d22022-08-22 09:36:17 +0100427 TEST_EQUAL( ret, oret );
Gabor Mezei23a1ce92022-08-02 11:54:44 +0200428 }
429
430 if( ( iret == 0 ) && ( oret == 0 ) )
431 {
432 if( nb > input->len )
433 {
434 if( endian == MBEDTLS_MPI_MOD_EXT_REP_BE )
435 {
436 size_t leading_zeroes = nb - input->len;
437 TEST_ASSERT( memcmp( buf + nb - input->len, input->x, input->len ) == 0 );
438 for( size_t i = 0; i < leading_zeroes; i++ )
Janos Follath494a6d22022-08-22 09:36:17 +0100439 TEST_EQUAL( buf[i], 0 );
Gabor Mezei23a1ce92022-08-02 11:54:44 +0200440 }
441 else
442 {
443 TEST_ASSERT( memcmp( buf, input->x, input->len ) == 0 );
444 for( size_t i = input->len; i < nb; i++ )
Janos Follath494a6d22022-08-22 09:36:17 +0100445 TEST_EQUAL( buf[i], 0 );
Gabor Mezei23a1ce92022-08-02 11:54:44 +0200446 }
447 }
448 else
449 {
450 if( endian == MBEDTLS_MPI_MOD_EXT_REP_BE )
451 {
452 size_t leading_zeroes = input->len - nb;
453 TEST_ASSERT( memcmp( input->x + input->len - nb, buf, nb ) == 0 );
454 for( size_t i = 0; i < leading_zeroes; i++ )
Janos Follath494a6d22022-08-22 09:36:17 +0100455 TEST_EQUAL( input->x[i], 0 );
Gabor Mezei23a1ce92022-08-02 11:54:44 +0200456 }
457 else
458 {
459 TEST_ASSERT( memcmp( input->x, buf, nb ) == 0 );
460 for( size_t i = nb; i < input->len; i++ )
Janos Follath494a6d22022-08-22 09:36:17 +0100461 TEST_EQUAL( input->x[i], 0 );
Gabor Mezei23a1ce92022-08-02 11:54:44 +0200462 }
463 }
464 }
465
466exit:
467 mbedtls_mpi_mod_modulus_free( &m );
Gabor Mezei23a1ce92022-08-02 11:54:44 +0200468}
469/* END_CASE */
470
471/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100472void mpi_read_binary_le( data_t * buf, char * input_A )
Janos Follatha778a942019-02-13 10:28:28 +0000473{
474 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000475 char str[1000];
Janos Follatha778a942019-02-13 10:28:28 +0000476 size_t len;
477
478 mbedtls_mpi_init( &X );
479
480
481 TEST_ASSERT( mbedtls_mpi_read_binary_le( &X, buf->x, buf->len ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200482 TEST_ASSERT( sign_is_valid( &X ) );
Werner Lewisf65a3272022-07-07 11:38:44 +0100483 TEST_ASSERT( mbedtls_mpi_write_string( &X, 16, str, sizeof( str ), &len ) == 0 );
Werner Lewisdc47fe72022-08-01 13:55:41 +0100484 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Janos Follatha778a942019-02-13 10:28:28 +0000485
486exit:
487 mbedtls_mpi_free( &X );
488}
489/* END_CASE */
490
491/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100492void mpi_write_binary( char * input_X, data_t * input_A,
493 int output_size, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000494{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000496 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000497 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000498
499 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000502
Werner Lewis19b4cd82022-07-07 11:02:27 +0100503 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200506 if( buflen > (size_t) output_size )
507 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200510 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000511 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000512
Ronald Cron2dbba992020-06-10 11:42:32 +0200513 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
514 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000515 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000516
Paul Bakkerbd51b262014-07-10 15:26:12 +0200517exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000519}
Paul Bakker33b43f12013-08-20 11:48:36 +0200520/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000521
Janos Follathe344d0f2019-02-19 16:17:40 +0000522/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100523void mpi_write_binary_le( char * input_X, data_t * input_A,
524 int output_size, int result )
Janos Follathe344d0f2019-02-19 16:17:40 +0000525{
526 mbedtls_mpi X;
527 unsigned char buf[1000];
528 size_t buflen;
529
530 memset( buf, 0x00, 1000 );
531
532 mbedtls_mpi_init( &X );
533
Werner Lewis19b4cd82022-07-07 11:02:27 +0100534 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000535
536 buflen = mbedtls_mpi_size( &X );
537 if( buflen > (size_t) output_size )
538 buflen = (size_t) output_size;
539
540 TEST_ASSERT( mbedtls_mpi_write_binary_le( &X, buf, buflen ) == result );
541 if( result == 0)
542 {
543
Ronald Cron2dbba992020-06-10 11:42:32 +0200544 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
545 buflen, input_A->len ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000546 }
547
548exit:
549 mbedtls_mpi_free( &X );
550}
551/* END_CASE */
552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100554void mpi_read_file( char * input_file, data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000555{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000557 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000558 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000559 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000560 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000561
562 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000565
Paul Bakker33b43f12013-08-20 11:48:36 +0200566 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200567 TEST_ASSERT( file != NULL );
Werner Lewisf65a3272022-07-07 11:38:44 +0100568 ret = mbedtls_mpi_read_file( &X, 16, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000569 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000570 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000571
Paul Bakker33b43f12013-08-20 11:48:36 +0200572 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000573 {
Gilles Peskinedffc7102021-06-10 15:34:15 +0200574 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 buflen = mbedtls_mpi_size( &X );
576 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000577
Paul Bakkere896fea2009-07-06 06:40:23 +0000578
Ronald Cron2dbba992020-06-10 11:42:32 +0200579 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
580 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000581 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000582
Paul Bakkerbd51b262014-07-10 15:26:12 +0200583exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000585}
Paul Bakker33b43f12013-08-20 11:48:36 +0200586/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100589void mpi_write_file( char * input_X, char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000590{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000592 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200593 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000596
Werner Lewis19b4cd82022-07-07 11:02:27 +0100597 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000598
Paul Bakker33b43f12013-08-20 11:48:36 +0200599 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000600 TEST_ASSERT( file_out != NULL );
Werner Lewisf65a3272022-07-07 11:38:44 +0100601 ret = mbedtls_mpi_write_file( NULL, &X, 16, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000602 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200603 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000604
Paul Bakker33b43f12013-08-20 11:48:36 +0200605 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000606 TEST_ASSERT( file_in != NULL );
Werner Lewisf65a3272022-07-07 11:38:44 +0100607 ret = mbedtls_mpi_read_file( &Y, 16, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000608 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200609 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000612
Paul Bakkerbd51b262014-07-10 15:26:12 +0200613exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000615}
Paul Bakker33b43f12013-08-20 11:48:36 +0200616/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000617
Paul Bakker33b43f12013-08-20 11:48:36 +0200618/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100619void mpi_get_bit( char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000620{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_mpi X;
622 mbedtls_mpi_init( &X );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100623 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000625
Paul Bakkerbd51b262014-07-10 15:26:12 +0200626exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000628}
Paul Bakker33b43f12013-08-20 11:48:36 +0200629/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000630
Paul Bakker33b43f12013-08-20 11:48:36 +0200631/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100632void mpi_set_bit( char * input_X, int pos, int val,
633 char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000634{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_mpi X, Y;
636 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000637
Werner Lewis19b4cd82022-07-07 11:02:27 +0100638 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
639 TEST_ASSERT( mbedtls_test_read_mpi( &Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100640 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
641
642 if( result == 0 )
643 {
Gilles Peskinedffc7102021-06-10 15:34:15 +0200644 TEST_ASSERT( sign_is_valid( &X ) );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100645 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
646 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000647
Paul Bakkerbd51b262014-07-10 15:26:12 +0200648exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000650}
Paul Bakker33b43f12013-08-20 11:48:36 +0200651/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000652
Paul Bakker33b43f12013-08-20 11:48:36 +0200653/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100654void mpi_lsb( char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000655{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_mpi X;
657 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000658
Werner Lewis19b4cd82022-07-07 11:02:27 +0100659 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000661
Paul Bakkerbd51b262014-07-10 15:26:12 +0200662exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000664}
Paul Bakker33b43f12013-08-20 11:48:36 +0200665/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000666
Paul Bakker33b43f12013-08-20 11:48:36 +0200667/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100668void mpi_bitlen( char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000669{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 mbedtls_mpi X;
671 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000672
Werner Lewis19b4cd82022-07-07 11:02:27 +0100673 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200674 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000675
Paul Bakkerbd51b262014-07-10 15:26:12 +0200676exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000678}
Paul Bakker33b43f12013-08-20 11:48:36 +0200679/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000680
Paul Bakker33b43f12013-08-20 11:48:36 +0200681/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100682void mpi_gcd( char * input_X, char * input_Y,
683 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000684{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 mbedtls_mpi A, X, Y, Z;
686 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000687
Werner Lewis19b4cd82022-07-07 11:02:27 +0100688 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
689 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
690 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200692 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000694
Paul Bakkerbd51b262014-07-10 15:26:12 +0200695exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000697}
Paul Bakker33b43f12013-08-20 11:48:36 +0200698/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000699
Paul Bakker33b43f12013-08-20 11:48:36 +0200700/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100701void mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000702{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_mpi X;
704 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
707 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000708
Paul Bakkerbd51b262014-07-10 15:26:12 +0200709exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000711}
Paul Bakker33b43f12013-08-20 11:48:36 +0200712/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000713
Paul Bakker33b43f12013-08-20 11:48:36 +0200714/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100715void mpi_cmp_mpi( char * input_X, char * input_Y,
716 int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000717{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 mbedtls_mpi X, Y;
719 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000720
Werner Lewis19b4cd82022-07-07 11:02:27 +0100721 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
722 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000724
Paul Bakkerbd51b262014-07-10 15:26:12 +0200725exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000727}
Paul Bakker33b43f12013-08-20 11:48:36 +0200728/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000729
Paul Bakker33b43f12013-08-20 11:48:36 +0200730/* BEGIN_CASE */
Janos Follathdeb80302022-08-19 13:32:17 +0100731void mpi_core_lt_ct( data_t * input_X, data_t * input_Y, int input_ret )
Janos Follath23bdeca2022-07-22 18:24:06 +0100732{
733 #define MAX_LEN 64
734 mbedtls_mpi_uint X[MAX_LEN];
735 mbedtls_mpi_uint Y[MAX_LEN];
736 unsigned exp_ret = input_ret;
737 unsigned ret;
738 size_t len = CHARS_TO_LIMBS(
739 input_X->len > input_Y->len ? input_X->len : input_Y->len );
740
Tom Cosgrove1feb5ac2022-09-15 14:22:35 +0100741 TEST_LE_U( len, MAX_LEN );
Janos Follath23bdeca2022-07-22 18:24:06 +0100742
743 TEST_ASSERT( mbedtls_mpi_core_read_be( X, len, input_X->x, input_X->len )
744 == 0 );
745 TEST_ASSERT( mbedtls_mpi_core_read_be( Y, len, input_Y->x, input_Y->len )
746 == 0 );
747
748 TEST_CF_SECRET( X, len * sizeof( mbedtls_mpi_uint ) );
749 TEST_CF_SECRET( Y, len * sizeof( mbedtls_mpi_uint ) );
750
751 ret = mbedtls_mpi_core_lt_ct( X, Y, len );
752
753 TEST_CF_PUBLIC( X, len * sizeof( mbedtls_mpi_uint ) );
754 TEST_CF_PUBLIC( Y, len * sizeof( mbedtls_mpi_uint ) );
755 TEST_CF_PUBLIC( &ret, sizeof( ret ) );
756
Janos Follath494a6d22022-08-22 09:36:17 +0100757 TEST_EQUAL( ret, exp_ret );
Janos Follath23bdeca2022-07-22 18:24:06 +0100758
759exit:
760 ;
761
762 #undef MAX_LEN
763}
764/* END_CASE */
765
766/* BEGIN_CASE */
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200767void mpi_core_cond_assign( data_t * input_X,
Gabor Mezei02e5d432022-10-03 16:45:11 +0200768 data_t * input_Y,
769 int input_len,
770 int is_fail )
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200771{
Gabor Mezeia7584882022-09-27 13:18:02 +0200772 mbedtls_mpi_uint *X = NULL;
773 mbedtls_mpi_uint *Y = NULL;
Gabor Mezeif5ca7262022-09-30 14:28:26 +0200774 size_t limbs_X = CHARS_TO_LIMBS( input_X->len );
775 size_t limbs_Y = CHARS_TO_LIMBS( input_Y->len );
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200776 size_t limbs = limbs_X;
Gabor Mezei02e5d432022-10-03 16:45:11 +0200777 size_t copy_limbs = CHARS_TO_LIMBS( input_len );
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200778 size_t len = limbs * sizeof( mbedtls_mpi_uint );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200779
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200780 TEST_ASSERT( limbs_X == limbs_Y );
Gabor Mezei02e5d432022-10-03 16:45:11 +0200781 TEST_ASSERT( copy_limbs <= limbs );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200782
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200783 ASSERT_ALLOC( X, len );
784 ASSERT_ALLOC( Y, len );
Gabor Mezeia7584882022-09-27 13:18:02 +0200785
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200786 TEST_ASSERT( mbedtls_mpi_core_read_be( X, limbs, input_X->x, input_X->len )
Gabor Mezei027d6962022-09-16 17:16:27 +0200787 == 0 );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200788
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200789 TEST_ASSERT( mbedtls_mpi_core_read_be( Y, limbs, input_Y->x, input_Y->len )
Gabor Mezei027d6962022-09-16 17:16:27 +0200790 == 0 );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200791
Gabor Mezei821d1512022-09-27 12:41:28 +0200792 /* condition is false */
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200793 TEST_CF_SECRET( X, len );
794 TEST_CF_SECRET( Y, len );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200795
Gabor Mezei02e5d432022-10-03 16:45:11 +0200796 mbedtls_mpi_core_cond_assign( X, Y, copy_limbs, 0 );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200797
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200798 TEST_CF_PUBLIC( X, len );
799 TEST_CF_PUBLIC( Y, len );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200800
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200801 TEST_ASSERT( memcmp( X, Y, len ) != 0 );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200802
Gabor Mezei821d1512022-09-27 12:41:28 +0200803 /* condition is true */
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200804 TEST_CF_SECRET( X, len );
805 TEST_CF_SECRET( Y, len );
Gabor Mezei821d1512022-09-27 12:41:28 +0200806
Gabor Mezei02e5d432022-10-03 16:45:11 +0200807 mbedtls_mpi_core_cond_assign( X, Y, copy_limbs, 1 );
Gabor Mezei821d1512022-09-27 12:41:28 +0200808
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200809 TEST_CF_PUBLIC( X, len );
810 TEST_CF_PUBLIC( Y, len );
Gabor Mezei821d1512022-09-27 12:41:28 +0200811
Gabor Mezei02e5d432022-10-03 16:45:11 +0200812 if( is_fail )
813 TEST_ASSERT( memcmp( X, Y, len ) != 0 );
814 else
815 ASSERT_COMPARE( X, len, Y, len );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200816
817exit:
Gabor Mezei8b05e3b2022-09-28 12:37:02 +0200818 mbedtls_free( X );
819 mbedtls_free( Y );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200820}
821/* END_CASE */
822
823/* BEGIN_CASE */
824void mpi_core_cond_swap( data_t * input_X,
Gabor Mezei02e5d432022-10-03 16:45:11 +0200825 data_t * input_Y,
826 int input_len,
827 int is_fail )
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200828{
Gabor Mezeia7584882022-09-27 13:18:02 +0200829 mbedtls_mpi_uint *tmp_X = NULL;
830 mbedtls_mpi_uint *tmp_Y = NULL;
831 mbedtls_mpi_uint *X = NULL;
832 mbedtls_mpi_uint *Y = NULL;
Gabor Mezeif5ca7262022-09-30 14:28:26 +0200833 size_t limbs_X = CHARS_TO_LIMBS( input_X->len );
834 size_t limbs_Y = CHARS_TO_LIMBS( input_Y->len );
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200835 size_t limbs = limbs_X;
Gabor Mezei02e5d432022-10-03 16:45:11 +0200836 size_t copy_limbs = CHARS_TO_LIMBS( input_len );
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200837 size_t len = limbs * sizeof( mbedtls_mpi_uint );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200838
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200839 TEST_ASSERT( limbs_X == limbs_Y );
Gabor Mezei02e5d432022-10-03 16:45:11 +0200840 TEST_ASSERT( copy_limbs <= limbs );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200841
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200842 ASSERT_ALLOC( tmp_X, len );
843 ASSERT_ALLOC( tmp_Y, len );
Gabor Mezeia7584882022-09-27 13:18:02 +0200844
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200845 TEST_ASSERT( mbedtls_mpi_core_read_be( tmp_X, limbs,
Gabor Mezei027d6962022-09-16 17:16:27 +0200846 input_X->x, input_X->len )
847 == 0 );
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200848 ASSERT_ALLOC( X, len );
849 memcpy( X, tmp_X, len );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200850
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200851 TEST_ASSERT( mbedtls_mpi_core_read_be( tmp_Y, limbs,
Gabor Mezei027d6962022-09-16 17:16:27 +0200852 input_Y->x, input_Y->len )
853 == 0 );
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200854 ASSERT_ALLOC( Y, len );
855 memcpy( Y, tmp_Y, len );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200856
Gabor Mezei821d1512022-09-27 12:41:28 +0200857 /* condition is false */
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200858 TEST_CF_SECRET( X, len );
859 TEST_CF_SECRET( Y, len );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200860
Gabor Mezei02e5d432022-10-03 16:45:11 +0200861 mbedtls_mpi_core_cond_swap( X, Y, copy_limbs, 0 );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200862
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200863 TEST_CF_PUBLIC( X, len );
864 TEST_CF_PUBLIC( Y, len );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200865
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200866 ASSERT_COMPARE( X, len, tmp_X, len );
867 ASSERT_COMPARE( Y, len, tmp_Y, len );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200868
Gabor Mezei821d1512022-09-27 12:41:28 +0200869 /* condition is true */
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200870 TEST_CF_SECRET( X, len );
871 TEST_CF_SECRET( Y, len );
Gabor Mezei821d1512022-09-27 12:41:28 +0200872
Gabor Mezei02e5d432022-10-03 16:45:11 +0200873 mbedtls_mpi_core_cond_swap( X, Y, copy_limbs, 1 );
Gabor Mezei821d1512022-09-27 12:41:28 +0200874
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200875 TEST_CF_PUBLIC( X, len );
876 TEST_CF_PUBLIC( Y, len );
Gabor Mezei821d1512022-09-27 12:41:28 +0200877
Gabor Mezei02e5d432022-10-03 16:45:11 +0200878 if( is_fail )
879 {
880 TEST_ASSERT( memcmp( X, tmp_X, len ) != 0 );
881 TEST_ASSERT( memcmp( X, tmp_Y, len ) != 0 );
882 TEST_ASSERT( memcmp( Y, tmp_X, len ) != 0 );
883 TEST_ASSERT( memcmp( Y, tmp_Y, len ) != 0 );
884 }
885 else
886 {
887 ASSERT_COMPARE( X, len, tmp_Y, len );
888 ASSERT_COMPARE( Y, len, tmp_X, len );
889 }
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200890
891exit:
Gabor Mezei8b05e3b2022-09-28 12:37:02 +0200892 mbedtls_free( tmp_X );
893 mbedtls_free( tmp_Y );
894 mbedtls_free( X );
895 mbedtls_free( Y );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200896}
897/* END_CASE */
898
899/* BEGIN_CASE */
900void mpi_mod_raw_cond_assign( data_t * input_X,
Gabor Mezei02e5d432022-10-03 16:45:11 +0200901 data_t * input_Y,
902 int input_len,
903 int is_fail )
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200904{
905 #define MAX_LEN 64
Gabor Mezeia7584882022-09-27 13:18:02 +0200906 mbedtls_mpi_uint *X = NULL;
907 mbedtls_mpi_uint *Y = NULL;
Gabor Mezeiec5685f2022-09-30 14:41:13 +0200908 mbedtls_mpi_uint *buff_m = NULL;
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200909 mbedtls_mpi_mod_modulus m;
Gabor Mezeif5ca7262022-09-30 14:28:26 +0200910 size_t limbs_X = CHARS_TO_LIMBS( input_X->len );
911 size_t limbs_Y = CHARS_TO_LIMBS( input_Y->len );
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200912 size_t limbs = limbs_X;
Gabor Mezei02e5d432022-10-03 16:45:11 +0200913 size_t copy_limbs = CHARS_TO_LIMBS( input_len );
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200914 size_t len = limbs * sizeof( mbedtls_mpi_uint );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200915
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200916 TEST_ASSERT( limbs_X == limbs_Y );
Gabor Mezei02e5d432022-10-03 16:45:11 +0200917 TEST_ASSERT( copy_limbs <= limbs );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200918
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200919 ASSERT_ALLOC( X, len );
920 ASSERT_ALLOC( Y, len );
Gabor Mezeia7584882022-09-27 13:18:02 +0200921
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200922 ASSERT_ALLOC( buff_m, len );
923 memset( buff_m, 0, len );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200924 mbedtls_mpi_mod_modulus_init( &m );
925 TEST_ASSERT( mbedtls_mpi_mod_modulus_setup(
Gabor Mezei02e5d432022-10-03 16:45:11 +0200926 &m, buff_m, copy_limbs,
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200927 MBEDTLS_MPI_MOD_EXT_REP_BE,
928 MBEDTLS_MPI_MOD_REP_MONTGOMERY )
929 == 0 );
930
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200931 TEST_ASSERT( mbedtls_mpi_core_read_be( X, limbs,
Gabor Mezei027d6962022-09-16 17:16:27 +0200932 input_X->x, input_X->len )
933 == 0 );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200934
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200935 TEST_ASSERT( mbedtls_mpi_core_read_be( Y, limbs,
Gabor Mezei027d6962022-09-16 17:16:27 +0200936 input_Y->x, input_Y->len )
937 == 0 );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200938
Gabor Mezei821d1512022-09-27 12:41:28 +0200939 /* condition is false */
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200940 TEST_CF_SECRET( X, len );
941 TEST_CF_SECRET( Y, len );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200942
Gabor Mezei821d1512022-09-27 12:41:28 +0200943 mbedtls_mpi_mod_raw_cond_assign( X, Y, &m, 0 );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200944
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200945 TEST_CF_PUBLIC( X, len );
946 TEST_CF_PUBLIC( Y, len );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200947
Gabor Mezei02e5d432022-10-03 16:45:11 +0200948 TEST_ASSERT( memcmp( X, Y, len ) != 0 );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200949
Gabor Mezei821d1512022-09-27 12:41:28 +0200950 /* condition is true */
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200951 TEST_CF_SECRET( X, len );
952 TEST_CF_SECRET( Y, len );
Gabor Mezei821d1512022-09-27 12:41:28 +0200953
954 mbedtls_mpi_mod_raw_cond_assign( X, Y, &m, 1 );
955
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200956 TEST_CF_PUBLIC( X, len );
957 TEST_CF_PUBLIC( Y, len );
Gabor Mezei821d1512022-09-27 12:41:28 +0200958
Gabor Mezei02e5d432022-10-03 16:45:11 +0200959 if( is_fail )
960 TEST_ASSERT( memcmp( X, Y, len ) != 0 );
961 else
962 ASSERT_COMPARE( X, len, Y, len );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200963
964exit:
Gabor Mezei8b05e3b2022-09-28 12:37:02 +0200965 mbedtls_free( X );
966 mbedtls_free( Y );
967
968 mbedtls_mpi_mod_modulus_free( &m );
Gabor Mezeiec5685f2022-09-30 14:41:13 +0200969 mbedtls_free( buff_m );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200970}
971/* END_CASE */
972
973/* BEGIN_CASE */
974void mpi_mod_raw_cond_swap( data_t * input_X,
Gabor Mezei02e5d432022-10-03 16:45:11 +0200975 data_t * input_Y,
976 int input_len,
977 int is_fail )
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200978{
Gabor Mezeia7584882022-09-27 13:18:02 +0200979 mbedtls_mpi_uint *tmp_X = NULL;
980 mbedtls_mpi_uint *tmp_Y = NULL;
981 mbedtls_mpi_uint *X = NULL;
982 mbedtls_mpi_uint *Y = NULL;
Gabor Mezeiec5685f2022-09-30 14:41:13 +0200983 mbedtls_mpi_uint *buff_m = NULL;
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200984 mbedtls_mpi_mod_modulus m;
Gabor Mezeif5ca7262022-09-30 14:28:26 +0200985 size_t limbs_X = CHARS_TO_LIMBS( input_X->len );
986 size_t limbs_Y = CHARS_TO_LIMBS( input_Y->len );
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200987 size_t limbs = limbs_X;
Gabor Mezei02e5d432022-10-03 16:45:11 +0200988 size_t copy_limbs = CHARS_TO_LIMBS( input_len );
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200989 size_t len = limbs * sizeof( mbedtls_mpi_uint );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200990
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200991 TEST_ASSERT( limbs_X == limbs_Y );
Gabor Mezei02e5d432022-10-03 16:45:11 +0200992 TEST_ASSERT( copy_limbs <= limbs );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200993
Gabor Mezei6546a6c2022-09-30 14:55:16 +0200994 ASSERT_ALLOC( tmp_X, len );
995 ASSERT_ALLOC( tmp_Y, len );
Gabor Mezeia7584882022-09-27 13:18:02 +0200996
Gabor Mezei02e5d432022-10-03 16:45:11 +0200997 ASSERT_ALLOC( buff_m, input_len );
998 memset( buff_m, 0, input_len );
Gabor Mezeib27b1c52022-09-12 16:36:48 +0200999 mbedtls_mpi_mod_modulus_init( &m );
1000 TEST_ASSERT( mbedtls_mpi_mod_modulus_setup(
Gabor Mezei02e5d432022-10-03 16:45:11 +02001001 &m, buff_m, copy_limbs,
Gabor Mezeib27b1c52022-09-12 16:36:48 +02001002 MBEDTLS_MPI_MOD_EXT_REP_BE,
1003 MBEDTLS_MPI_MOD_REP_MONTGOMERY )
1004 == 0 );
1005
Gabor Mezei6546a6c2022-09-30 14:55:16 +02001006 TEST_ASSERT( mbedtls_mpi_core_read_be( tmp_X, limbs, input_X->x, input_X->len )
Gabor Mezei027d6962022-09-16 17:16:27 +02001007 == 0 );
Gabor Mezei6546a6c2022-09-30 14:55:16 +02001008 ASSERT_ALLOC( X, len );
1009 memcpy( X, tmp_X, len );
Gabor Mezeib27b1c52022-09-12 16:36:48 +02001010
Gabor Mezei6546a6c2022-09-30 14:55:16 +02001011 TEST_ASSERT( mbedtls_mpi_core_read_be( tmp_Y, limbs, input_Y->x, input_Y->len )
Gabor Mezei027d6962022-09-16 17:16:27 +02001012 == 0 );
Gabor Mezei6546a6c2022-09-30 14:55:16 +02001013 ASSERT_ALLOC( Y, len );
1014 memcpy( Y, tmp_Y, len );
Gabor Mezeib27b1c52022-09-12 16:36:48 +02001015
Gabor Mezei821d1512022-09-27 12:41:28 +02001016 /* condition is false */
Gabor Mezei6546a6c2022-09-30 14:55:16 +02001017 TEST_CF_SECRET( X, len );
1018 TEST_CF_SECRET( Y, len );
Gabor Mezeib27b1c52022-09-12 16:36:48 +02001019
Gabor Mezei821d1512022-09-27 12:41:28 +02001020 mbedtls_mpi_mod_raw_cond_swap( X, Y, &m, 0 );
Gabor Mezeib27b1c52022-09-12 16:36:48 +02001021
Gabor Mezei6546a6c2022-09-30 14:55:16 +02001022 TEST_CF_PUBLIC( X, len );
1023 TEST_CF_PUBLIC( Y, len );
Gabor Mezeib27b1c52022-09-12 16:36:48 +02001024
Gabor Mezei6546a6c2022-09-30 14:55:16 +02001025 ASSERT_COMPARE( X, len, tmp_X, len );
1026 ASSERT_COMPARE( Y, len, tmp_Y, len );
Gabor Mezeib27b1c52022-09-12 16:36:48 +02001027
Gabor Mezei821d1512022-09-27 12:41:28 +02001028 /* condition is true */
Gabor Mezei6546a6c2022-09-30 14:55:16 +02001029 TEST_CF_SECRET( X, len );
1030 TEST_CF_SECRET( Y, len );
Gabor Mezei821d1512022-09-27 12:41:28 +02001031
1032 mbedtls_mpi_mod_raw_cond_swap( X, Y, &m, 1 );
1033
Gabor Mezei6546a6c2022-09-30 14:55:16 +02001034 TEST_CF_PUBLIC( X, len );
1035 TEST_CF_PUBLIC( Y, len );
Gabor Mezei821d1512022-09-27 12:41:28 +02001036
Gabor Mezei02e5d432022-10-03 16:45:11 +02001037 if( is_fail )
1038 {
1039 TEST_ASSERT( memcmp( X, tmp_X, len ) != 0 );
1040 TEST_ASSERT( memcmp( X, tmp_Y, len ) != 0 );
1041 TEST_ASSERT( memcmp( Y, tmp_X, len ) != 0 );
1042 TEST_ASSERT( memcmp( Y, tmp_Y, len ) != 0 );
1043 }
1044 else
1045 {
1046 ASSERT_COMPARE( X, len, tmp_Y, len );
1047 ASSERT_COMPARE( Y, len, tmp_X, len );
1048 }
Gabor Mezeib27b1c52022-09-12 16:36:48 +02001049
1050exit:
Gabor Mezei8b05e3b2022-09-28 12:37:02 +02001051 mbedtls_free( tmp_X );
1052 mbedtls_free( tmp_Y );
1053 mbedtls_free( X );
1054 mbedtls_free( Y );
1055
1056 mbedtls_mpi_mod_modulus_free( &m );
Gabor Mezeiec5685f2022-09-30 14:41:13 +02001057 mbedtls_free( buff_m );
Gabor Mezeib27b1c52022-09-12 16:36:48 +02001058}
1059/* END_CASE */
1060
1061/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001062void mpi_lt_mpi_ct( int size_X, char * input_X,
1063 int size_Y, char * input_Y,
1064 int input_ret, int input_err )
Janos Follath385d5b82019-09-11 16:07:14 +01001065{
Gilles Peskine0deccf12020-09-02 15:18:07 +02001066 unsigned ret = -1;
Janos Follath0e5532d2019-10-11 14:21:53 +01001067 unsigned input_uret = input_ret;
Janos Follath385d5b82019-09-11 16:07:14 +01001068 mbedtls_mpi X, Y;
1069 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
1070
Werner Lewis19b4cd82022-07-07 11:02:27 +01001071 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1072 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +01001073
Gilles Peskine9018b112020-01-21 16:30:53 +01001074 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
1075 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +01001076
Janos Follath0e5532d2019-10-11 14:21:53 +01001077 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follath385d5b82019-09-11 16:07:14 +01001078 if( input_err == 0 )
Janos Follath0e5532d2019-10-11 14:21:53 +01001079 TEST_ASSERT( ret == input_uret );
Janos Follath385d5b82019-09-11 16:07:14 +01001080
1081exit:
1082 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
1083}
1084/* END_CASE */
1085
1086/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001087void mpi_cmp_abs( char * input_X, char * input_Y,
1088 int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001089{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 mbedtls_mpi X, Y;
1091 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +00001092
Werner Lewis19b4cd82022-07-07 11:02:27 +01001093 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1094 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001096
Paul Bakkerbd51b262014-07-10 15:26:12 +02001097exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +00001099}
Paul Bakker33b43f12013-08-20 11:48:36 +02001100/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001101
Paul Bakker33b43f12013-08-20 11:48:36 +02001102/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001103void mpi_copy( char *src_hex, char *dst_hex )
Paul Bakker367dae42009-06-28 21:50:27 +00001104{
Gilles Peskined0722f82021-06-10 23:00:33 +02001105 mbedtls_mpi src, dst, ref;
Gilles Peskine90ec8e82021-06-10 15:17:30 +02001106 mbedtls_mpi_init( &src );
1107 mbedtls_mpi_init( &dst );
Gilles Peskined0722f82021-06-10 23:00:33 +02001108 mbedtls_mpi_init( &ref );
Paul Bakker367dae42009-06-28 21:50:27 +00001109
Werner Lewis19b4cd82022-07-07 11:02:27 +01001110 TEST_ASSERT( mbedtls_test_read_mpi( &src, src_hex ) == 0 );
1111 TEST_ASSERT( mbedtls_test_read_mpi( &ref, dst_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +02001112
1113 /* mbedtls_mpi_copy() */
Werner Lewis19b4cd82022-07-07 11:02:27 +01001114 TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
Gilles Peskine90ec8e82021-06-10 15:17:30 +02001115 TEST_ASSERT( mbedtls_mpi_copy( &dst, &src ) == 0 );
Gilles Peskine90ec8e82021-06-10 15:17:30 +02001116 TEST_ASSERT( sign_is_valid( &dst ) );
1117 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001118
Gilles Peskined0722f82021-06-10 23:00:33 +02001119 /* mbedtls_mpi_safe_cond_assign(), assignment done */
1120 mbedtls_mpi_free( &dst );
Werner Lewis19b4cd82022-07-07 11:02:27 +01001121 TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +02001122 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 1 ) == 0 );
1123 TEST_ASSERT( sign_is_valid( &dst ) );
1124 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
1125
1126 /* mbedtls_mpi_safe_cond_assign(), assignment not done */
1127 mbedtls_mpi_free( &dst );
Werner Lewis19b4cd82022-07-07 11:02:27 +01001128 TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +02001129 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 0 ) == 0 );
1130 TEST_ASSERT( sign_is_valid( &dst ) );
1131 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &ref ) == 0 );
1132
Paul Bakkerbd51b262014-07-10 15:26:12 +02001133exit:
Gilles Peskine90ec8e82021-06-10 15:17:30 +02001134 mbedtls_mpi_free( &src );
1135 mbedtls_mpi_free( &dst );
Gilles Peskined0722f82021-06-10 23:00:33 +02001136 mbedtls_mpi_free( &ref );
Gilles Peskine7428b452020-01-20 21:01:51 +01001137}
1138/* END_CASE */
1139
1140/* BEGIN_CASE */
Gilles Peskine90ec8e82021-06-10 15:17:30 +02001141void mpi_copy_self( char *input_X )
Gilles Peskine7428b452020-01-20 21:01:51 +01001142{
Gilles Peskine90ec8e82021-06-10 15:17:30 +02001143 mbedtls_mpi X, A;
1144 mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +00001146
Werner Lewis19b4cd82022-07-07 11:02:27 +01001147 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
Gilles Peskine90ec8e82021-06-10 15:17:30 +02001149
Werner Lewis19b4cd82022-07-07 11:02:27 +01001150 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_X ) == 0 );
Gilles Peskine90ec8e82021-06-10 15:17:30 +02001151 TEST_ASSERT( sign_is_valid( &X ) );
1152 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001153
Paul Bakkerbd51b262014-07-10 15:26:12 +02001154exit:
Gilles Peskine90ec8e82021-06-10 15:17:30 +02001155 mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +00001157}
Paul Bakker33b43f12013-08-20 11:48:36 +02001158/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +00001159
Paul Bakker33b43f12013-08-20 11:48:36 +02001160/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001161void mpi_swap( char *X_hex, char *Y_hex )
Gilles Peskinefc1eeef2021-06-10 22:29:57 +02001162{
1163 mbedtls_mpi X, Y, X0, Y0;
1164 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
1165 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
1166
Werner Lewis19b4cd82022-07-07 11:02:27 +01001167 TEST_ASSERT( mbedtls_test_read_mpi( &X0, X_hex ) == 0 );
1168 TEST_ASSERT( mbedtls_test_read_mpi( &Y0, Y_hex ) == 0 );
Gilles Peskinefc1eeef2021-06-10 22:29:57 +02001169
Gilles Peskined0722f82021-06-10 23:00:33 +02001170 /* mbedtls_mpi_swap() */
Tom Cosgrovec71ca0c2022-09-15 15:38:17 +01001171 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
1172 TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 );
Gilles Peskinefc1eeef2021-06-10 22:29:57 +02001173 mbedtls_mpi_swap( &X, &Y );
1174 TEST_ASSERT( sign_is_valid( &X ) );
1175 TEST_ASSERT( sign_is_valid( &Y ) );
1176 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
1177 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
1178
Gilles Peskined0722f82021-06-10 23:00:33 +02001179 /* mbedtls_mpi_safe_cond_swap(), swap done */
1180 mbedtls_mpi_free( &X );
1181 mbedtls_mpi_free( &Y );
Tom Cosgrovec71ca0c2022-09-15 15:38:17 +01001182 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
1183 TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +02001184 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
1185 TEST_ASSERT( sign_is_valid( &X ) );
1186 TEST_ASSERT( sign_is_valid( &Y ) );
1187 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
1188 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
1189
1190 /* mbedtls_mpi_safe_cond_swap(), swap not done */
1191 mbedtls_mpi_free( &X );
1192 mbedtls_mpi_free( &Y );
Tom Cosgrovec71ca0c2022-09-15 15:38:17 +01001193 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
1194 TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +02001195 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
1196 TEST_ASSERT( sign_is_valid( &X ) );
1197 TEST_ASSERT( sign_is_valid( &Y ) );
1198 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
1199 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &Y0 ) == 0 );
1200
Gilles Peskinefc1eeef2021-06-10 22:29:57 +02001201exit:
1202 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
1203 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
1204}
1205/* END_CASE */
1206
1207/* BEGIN_CASE */
1208void mpi_swap_self( char *X_hex )
1209{
1210 mbedtls_mpi X, X0;
1211 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
1212
Tom Cosgrovec71ca0c2022-09-15 15:38:17 +01001213 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
Werner Lewis19b4cd82022-07-07 11:02:27 +01001214 TEST_ASSERT( mbedtls_test_read_mpi( &X0, X_hex ) == 0 );
Gilles Peskinefc1eeef2021-06-10 22:29:57 +02001215
1216 mbedtls_mpi_swap( &X, &X );
1217 TEST_ASSERT( sign_is_valid( &X ) );
1218 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
1219
1220exit:
1221 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
1222}
1223/* END_CASE */
1224
1225/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001226void mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +01001227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 mbedtls_mpi X;
1229 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +01001230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Gilles Peskinee1091752021-06-15 21:19:18 +02001232 if( used > 0 )
1233 {
1234 size_t used_bit_count = used * 8 * sizeof( mbedtls_mpi_uint );
1235 TEST_ASSERT( mbedtls_mpi_set_bit( &X, used_bit_count - 1, 1 ) == 0 );
1236 }
1237 TEST_EQUAL( X.n, (size_t) before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Gilles Peskinee1091752021-06-15 21:19:18 +02001239 TEST_EQUAL( X.n, (size_t) after );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +01001240
Paul Bakkerbd51b262014-07-10 15:26:12 +02001241exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +01001243}
1244/* END_CASE */
1245
1246/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001247void mpi_add_mpi( char * input_X, char * input_Y,
1248 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001249{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 mbedtls_mpi X, Y, Z, A;
1251 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001252
Werner Lewis19b4cd82022-07-07 11:02:27 +01001253 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1254 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1255 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001257 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001259
Gilles Peskine56f943a2020-07-23 01:18:11 +02001260 /* result == first operand */
1261 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001262 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001263 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis19b4cd82022-07-07 11:02:27 +01001264 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001265
1266 /* result == second operand */
1267 TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001268 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001269 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
1270
Paul Bakkerbd51b262014-07-10 15:26:12 +02001271exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001273}
Paul Bakker33b43f12013-08-20 11:48:36 +02001274/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001275
Paul Bakker33b43f12013-08-20 11:48:36 +02001276/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001277void mpi_add_mpi_inplace( char * input_X, char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +01001278{
1279 mbedtls_mpi X, A;
1280 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
1281
Werner Lewis19b4cd82022-07-07 11:02:27 +01001282 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +01001283
Werner Lewis19b4cd82022-07-07 11:02:27 +01001284 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +01001285 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
1286 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001287 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath6cbacec2015-10-25 12:29:13 +01001288
Werner Lewis19b4cd82022-07-07 11:02:27 +01001289 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +01001290 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001291 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath6cbacec2015-10-25 12:29:13 +01001292 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
1293
Werner Lewis19b4cd82022-07-07 11:02:27 +01001294 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +01001295 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001296 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath044a86b2015-10-25 10:58:03 +01001297 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
1298
1299exit:
1300 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
1301}
1302/* END_CASE */
1303
1304
1305/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001306void mpi_add_abs( char * input_X, char * input_Y,
1307 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001308{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 mbedtls_mpi X, Y, Z, A;
1310 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001311
Werner Lewis19b4cd82022-07-07 11:02:27 +01001312 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1313 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1314 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001316 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001318
Gilles Peskine56f943a2020-07-23 01:18:11 +02001319 /* result == first operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001321 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis19b4cd82022-07-07 11:02:27 +01001323 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001324
1325 /* result == second operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001327 TEST_ASSERT( sign_is_valid( &Y ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001329
Paul Bakkerbd51b262014-07-10 15:26:12 +02001330exit:
Gilles Peskine56f943a2020-07-23 01:18:11 +02001331 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +00001332}
Paul Bakker33b43f12013-08-20 11:48:36 +02001333/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +00001334
Paul Bakker33b43f12013-08-20 11:48:36 +02001335/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001336void mpi_add_int( char * input_X, int input_Y,
1337 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001338{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 mbedtls_mpi X, Z, A;
1340 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001341
Werner Lewis19b4cd82022-07-07 11:02:27 +01001342 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1343 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001345 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001347
Paul Bakkerbd51b262014-07-10 15:26:12 +02001348exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001350}
Paul Bakker33b43f12013-08-20 11:48:36 +02001351/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001352
Paul Bakker33b43f12013-08-20 11:48:36 +02001353/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001354void mpi_sub_mpi( char * input_X, char * input_Y,
1355 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001356{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 mbedtls_mpi X, Y, Z, A;
1358 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001359
Werner Lewis19b4cd82022-07-07 11:02:27 +01001360 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1361 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1362 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001364 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001366
Gilles Peskine56f943a2020-07-23 01:18:11 +02001367 /* result == first operand */
1368 TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001369 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001370 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis19b4cd82022-07-07 11:02:27 +01001371 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001372
1373 /* result == second operand */
1374 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001375 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001376 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
1377
Paul Bakkerbd51b262014-07-10 15:26:12 +02001378exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001380}
Paul Bakker33b43f12013-08-20 11:48:36 +02001381/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001382
Paul Bakker33b43f12013-08-20 11:48:36 +02001383/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001384void mpi_sub_abs( char * input_X, char * input_Y,
1385 char * input_A, int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001386{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001388 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001390
Werner Lewis19b4cd82022-07-07 11:02:27 +01001391 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1392 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1393 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001396 TEST_ASSERT( res == sub_result );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001397 TEST_ASSERT( sign_is_valid( &Z ) );
Paul Bakker367dae42009-06-28 21:50:27 +00001398 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001400
Gilles Peskine56f943a2020-07-23 01:18:11 +02001401 /* result == first operand */
1402 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001403 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001404 if( sub_result == 0 )
1405 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis19b4cd82022-07-07 11:02:27 +01001406 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001407
1408 /* result == second operand */
1409 TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001410 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001411 if( sub_result == 0 )
1412 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
1413
Paul Bakkerbd51b262014-07-10 15:26:12 +02001414exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001416}
Paul Bakker33b43f12013-08-20 11:48:36 +02001417/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001418
Paul Bakker33b43f12013-08-20 11:48:36 +02001419/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001420void mpi_sub_int( char * input_X, int input_Y,
1421 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001422{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 mbedtls_mpi X, Z, A;
1424 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001425
Werner Lewis19b4cd82022-07-07 11:02:27 +01001426 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1427 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001429 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001431
Paul Bakkerbd51b262014-07-10 15:26:12 +02001432exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001434}
Paul Bakker33b43f12013-08-20 11:48:36 +02001435/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001436
Paul Bakker33b43f12013-08-20 11:48:36 +02001437/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001438void mpi_mul_mpi( char * input_X, char * input_Y,
1439 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001440{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 mbedtls_mpi X, Y, Z, A;
1442 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001443
Werner Lewis19b4cd82022-07-07 11:02:27 +01001444 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1445 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1446 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001448 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001450
Paul Bakkerbd51b262014-07-10 15:26:12 +02001451exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001453}
Paul Bakker33b43f12013-08-20 11:48:36 +02001454/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001455
Paul Bakker33b43f12013-08-20 11:48:36 +02001456/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001457void mpi_mul_int( char * input_X, int input_Y,
1458 char * input_A, char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +00001459{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 mbedtls_mpi X, Z, A;
1461 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001462
Werner Lewis19b4cd82022-07-07 11:02:27 +01001463 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1464 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001466 TEST_ASSERT( sign_is_valid( &Z ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001467 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001469 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001471 else
1472 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001473
Paul Bakkerbd51b262014-07-10 15:26:12 +02001474exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001476}
Paul Bakker33b43f12013-08-20 11:48:36 +02001477/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001478
Paul Bakker33b43f12013-08-20 11:48:36 +02001479/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001480void mpi_div_mpi( char * input_X, char * input_Y,
1481 char * input_A, char * input_B,
1482 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001483{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001485 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
1487 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001488
Werner Lewis19b4cd82022-07-07 11:02:27 +01001489 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1490 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1491 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
1492 TEST_ASSERT( mbedtls_test_read_mpi( &B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001494 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001495 if( res == 0 )
1496 {
Gilles Peskinedffc7102021-06-10 15:34:15 +02001497 TEST_ASSERT( sign_is_valid( &Q ) );
1498 TEST_ASSERT( sign_is_valid( &R ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1500 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001501 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001502
Paul Bakkerbd51b262014-07-10 15:26:12 +02001503exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
1505 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001506}
Paul Bakker33b43f12013-08-20 11:48:36 +02001507/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001508
Paul Bakker33b43f12013-08-20 11:48:36 +02001509/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001510void mpi_div_int( char * input_X, int input_Y,
1511 char * input_A, char * input_B,
1512 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001513{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001515 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
1517 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001518
Werner Lewis19b4cd82022-07-07 11:02:27 +01001519 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1520 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
1521 TEST_ASSERT( mbedtls_test_read_mpi( &B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001523 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001524 if( res == 0 )
1525 {
Gilles Peskinedffc7102021-06-10 15:34:15 +02001526 TEST_ASSERT( sign_is_valid( &Q ) );
1527 TEST_ASSERT( sign_is_valid( &R ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1529 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001530 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001531
Paul Bakkerbd51b262014-07-10 15:26:12 +02001532exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
1534 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001535}
Paul Bakker33b43f12013-08-20 11:48:36 +02001536/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001537
Paul Bakker33b43f12013-08-20 11:48:36 +02001538/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001539void mpi_mod_mpi( char * input_X, char * input_Y,
1540 char * input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001541{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001543 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001545
Werner Lewis19b4cd82022-07-07 11:02:27 +01001546 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1547 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1548 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001550 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001551 if( res == 0 )
1552 {
Gilles Peskinedffc7102021-06-10 15:34:15 +02001553 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001555 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001556
Paul Bakkerbd51b262014-07-10 15:26:12 +02001557exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001559}
Paul Bakker33b43f12013-08-20 11:48:36 +02001560/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001561
Paul Bakker33b43f12013-08-20 11:48:36 +02001562/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001563void mpi_mod_int( char * input_X, int input_Y,
1564 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001565{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001567 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 mbedtls_mpi_uint r;
1569 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001570
Werner Lewis19b4cd82022-07-07 11:02:27 +01001571 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001573 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001574 if( res == 0 )
1575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001577 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001578
Paul Bakkerbd51b262014-07-10 15:26:12 +02001579exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001581}
Paul Bakker33b43f12013-08-20 11:48:36 +02001582/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001583
Paul Bakker33b43f12013-08-20 11:48:36 +02001584/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001585void mpi_exp_mod( char * input_A, char * input_E,
1586 char * input_N, char * input_X,
1587 int exp_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001588{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001590 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1592 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001593
Werner Lewis19b4cd82022-07-07 11:02:27 +01001594 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
1595 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
1596 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
1597 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001598
Gilles Peskine342f71b2021-06-09 18:31:35 +02001599 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, NULL );
Gilles Peskine722c62c2021-06-15 21:55:05 +02001600 TEST_ASSERT( res == exp_result );
Gilles Peskine342f71b2021-06-09 18:31:35 +02001601 if( res == 0 )
1602 {
1603 TEST_ASSERT( sign_is_valid( &Z ) );
1604 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
1605 }
1606
1607 /* Now test again with the speed-up parameter supplied as an output. */
1608 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Gilles Peskine722c62c2021-06-15 21:55:05 +02001609 TEST_ASSERT( res == exp_result );
Gilles Peskine342f71b2021-06-09 18:31:35 +02001610 if( res == 0 )
1611 {
1612 TEST_ASSERT( sign_is_valid( &Z ) );
1613 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
1614 }
1615
1616 /* Now test again with the speed-up parameter supplied in calculated form. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Gilles Peskine722c62c2021-06-15 21:55:05 +02001618 TEST_ASSERT( res == exp_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001619 if( res == 0 )
1620 {
Gilles Peskinedffc7102021-06-10 15:34:15 +02001621 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001623 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001624
Paul Bakkerbd51b262014-07-10 15:26:12 +02001625exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1627 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001628}
Paul Bakker33b43f12013-08-20 11:48:36 +02001629/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001630
Paul Bakker33b43f12013-08-20 11:48:36 +02001631/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001632void mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
1633 char * input_RR, int exp_result )
Chris Jonesd10b3312020-12-02 10:41:50 +00001634{
1635 mbedtls_mpi A, E, N, RR, Z;
1636 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1637 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
1638
Chris Jonesaa850cd2020-12-03 11:35:41 +00001639 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jonesd10b3312020-12-02 10:41:50 +00001640 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001641 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001642 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001643
1644 /* Set E to 2^(E_bytes - 1) + 1 */
1645 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
1646 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001647 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001648
1649 /* Set N to 2^(N_bytes - 1) + 1 */
1650 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
1651 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001652 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
1653
1654 if( strlen( input_RR ) )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001655 TEST_ASSERT( mbedtls_test_read_mpi( &RR, input_RR ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001656
Chris Jonesaa850cd2020-12-03 11:35:41 +00001657 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jonesd10b3312020-12-02 10:41:50 +00001658
1659exit:
1660 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1661 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
1662}
1663/* END_CASE */
1664
1665/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001666void mpi_inv_mod( char * input_X, char * input_Y,
1667 char * input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001668{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001670 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001672
Werner Lewis19b4cd82022-07-07 11:02:27 +01001673 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1674 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1675 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001677 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001678 if( res == 0 )
1679 {
Gilles Peskinedffc7102021-06-10 15:34:15 +02001680 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001682 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001683
Paul Bakkerbd51b262014-07-10 15:26:12 +02001684exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001686}
Paul Bakker33b43f12013-08-20 11:48:36 +02001687/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001690void mpi_is_prime( char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001691{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001693 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001695
Werner Lewis19b4cd82022-07-07 11:02:27 +01001696 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Ronald Cron351f0ee2020-06-10 12:12:18 +02001697 res = mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001698 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001699
Paul Bakkerbd51b262014-07-10 15:26:12 +02001700exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001702}
Paul Bakker33b43f12013-08-20 11:48:36 +02001703/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001706void mpi_is_prime_det( data_t * input_X, data_t * witnesses,
1707 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001708{
1709 mbedtls_mpi X;
1710 int res;
1711 mbedtls_test_mpi_random rand;
1712
1713 mbedtls_mpi_init( &X );
1714 rand.data = witnesses;
1715 rand.pos = 0;
1716 rand.chunk_len = chunk_len;
1717
1718 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001719 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1720 mbedtls_test_mpi_miller_rabin_determinizer,
1721 &rand );
1722 TEST_ASSERT( res == 0 );
1723
1724 rand.data = witnesses;
1725 rand.pos = 0;
1726 rand.chunk_len = chunk_len;
1727
Janos Follatha0b67c22018-09-18 14:48:23 +01001728 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1729 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001730 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001731 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001732
1733exit:
1734 mbedtls_mpi_free( &X );
1735}
1736/* END_CASE */
1737
1738/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001739void mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001740{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001742 int my_ret;
1743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001745
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001746 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags,
1747 mbedtls_test_rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001748 TEST_ASSERT( my_ret == ref_ret );
1749
1750 if( ref_ret == 0 )
1751 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001752 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001753
1754 TEST_ASSERT( actual_bits >= (size_t) bits );
1755 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001756 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001757
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001758 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1759 mbedtls_test_rnd_std_rand,
1760 NULL ) == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001761 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001762 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001763 /* X = ( X - 1 ) / 2 */
1764 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001765 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1766 mbedtls_test_rnd_std_rand,
1767 NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001768 }
1769 }
1770
Paul Bakkerbd51b262014-07-10 15:26:12 +02001771exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001773}
1774/* END_CASE */
1775
Paul Bakker33b43f12013-08-20 11:48:36 +02001776/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001777void mpi_shift_l( char * input_X, int shift_X,
1778 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001779{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 mbedtls_mpi X, A;
1781 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001782
Werner Lewis19b4cd82022-07-07 11:02:27 +01001783 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1784 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001786 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001788
Paul Bakkerbd51b262014-07-10 15:26:12 +02001789exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001791}
Paul Bakker33b43f12013-08-20 11:48:36 +02001792/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001793
Paul Bakker33b43f12013-08-20 11:48:36 +02001794/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001795void mpi_shift_r( char * input_X, int shift_X,
1796 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001797{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 mbedtls_mpi X, A;
1799 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001800
Werner Lewis19b4cd82022-07-07 11:02:27 +01001801 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1802 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001804 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001806
Paul Bakkerbd51b262014-07-10 15:26:12 +02001807exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001809}
Paul Bakker33b43f12013-08-20 11:48:36 +02001810/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001811
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001812/* BEGIN_CASE */
Gilles Peskine422e8672021-04-02 00:02:27 +02001813void mpi_fill_random( int wanted_bytes, int rng_bytes,
1814 int before, int expected_ret )
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001815{
1816 mbedtls_mpi X;
1817 int ret;
1818 size_t bytes_left = rng_bytes;
1819 mbedtls_mpi_init( &X );
1820
Gilles Peskine422e8672021-04-02 00:02:27 +02001821 if( before != 0 )
1822 {
1823 /* Set X to sign(before) * 2^(|before|-1) */
1824 TEST_ASSERT( mbedtls_mpi_lset( &X, before > 0 ? 1 : -1 ) == 0 );
1825 if( before < 0 )
1826 before = - before;
1827 TEST_ASSERT( mbedtls_mpi_shift_l( &X, before - 1 ) == 0 );
1828 }
1829
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001830 ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
1831 f_rng_bytes_left, &bytes_left );
1832 TEST_ASSERT( ret == expected_ret );
1833
1834 if( expected_ret == 0 )
1835 {
1836 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
1837 * as a big-endian representation of the number. We know when
1838 * our RNG function returns null bytes, so we know how many
1839 * leading zero bytes the number has. */
1840 size_t leading_zeros = 0;
1841 if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
1842 leading_zeros = 1;
1843 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
1844 (size_t) wanted_bytes );
1845 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001846 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001847 }
1848
1849exit:
1850 mbedtls_mpi_free( &X );
1851}
1852/* END_CASE */
1853
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001854/* BEGIN_CASE */
1855void mpi_random_many( int min, data_t *bound_bytes, int iterations )
1856{
1857 /* Generate numbers in the range 1..bound-1. Do it iterations times.
1858 * This function assumes that the value of bound is at least 2 and
1859 * that iterations is large enough that a one-in-2^iterations chance
1860 * effectively never occurs.
1861 */
1862
1863 mbedtls_mpi upper_bound;
1864 size_t n_bits;
1865 mbedtls_mpi result;
1866 size_t b;
1867 /* If upper_bound is small, stats[b] is the number of times the value b
1868 * has been generated. Otherwise stats[b] is the number of times a
1869 * value with bit b set has been generated. */
1870 size_t *stats = NULL;
1871 size_t stats_len;
1872 int full_stats;
1873 size_t i;
1874
1875 mbedtls_mpi_init( &upper_bound );
1876 mbedtls_mpi_init( &result );
1877
1878 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1879 bound_bytes->x, bound_bytes->len ) );
1880 n_bits = mbedtls_mpi_bitlen( &upper_bound );
1881 /* Consider a bound "small" if it's less than 2^5. This value is chosen
1882 * to be small enough that the probability of missing one value is
1883 * negligible given the number of iterations. It must be less than
1884 * 256 because some of the code below assumes that "small" values
1885 * fit in a byte. */
1886 if( n_bits <= 5 )
1887 {
1888 full_stats = 1;
1889 stats_len = bound_bytes->x[bound_bytes->len - 1];
1890 }
1891 else
1892 {
1893 full_stats = 0;
1894 stats_len = n_bits;
1895 }
1896 ASSERT_ALLOC( stats, stats_len );
1897
1898 for( i = 0; i < (size_t) iterations; i++ )
1899 {
1900 mbedtls_test_set_step( i );
1901 TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
1902 mbedtls_test_rnd_std_rand, NULL ) );
1903
Gilles Peskinedffc7102021-06-10 15:34:15 +02001904 TEST_ASSERT( sign_is_valid( &result ) );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001905 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
1906 TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
1907 if( full_stats )
1908 {
1909 uint8_t value;
1910 TEST_EQUAL( 0, mbedtls_mpi_write_binary( &result, &value, 1 ) );
1911 TEST_ASSERT( value < stats_len );
1912 ++stats[value];
1913 }
1914 else
1915 {
1916 for( b = 0; b < n_bits; b++ )
1917 stats[b] += mbedtls_mpi_get_bit( &result, b );
1918 }
1919 }
1920
1921 if( full_stats )
1922 {
Gilles Peskined463edf2021-04-13 20:45:05 +02001923 for( b = min; b < stats_len; b++ )
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001924 {
1925 mbedtls_test_set_step( 1000000 + b );
1926 /* Assert that each value has been reached at least once.
1927 * This is almost guaranteed if the iteration count is large
1928 * enough. This is a very crude way of checking the distribution.
1929 */
1930 TEST_ASSERT( stats[b] > 0 );
1931 }
1932 }
1933 else
1934 {
Gilles Peskineceefe5d2021-06-02 21:24:04 +02001935 int statistically_safe_all_the_way =
1936 is_significantly_above_a_power_of_2( bound_bytes );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001937 for( b = 0; b < n_bits; b++ )
1938 {
1939 mbedtls_test_set_step( 1000000 + b );
1940 /* Assert that each bit has been set in at least one result and
1941 * clear in at least one result. Provided that iterations is not
1942 * too small, it would be extremely unlikely for this not to be
1943 * the case if the results are uniformly distributed.
1944 *
1945 * As an exception, the top bit may legitimately never be set
1946 * if bound is a power of 2 or only slightly above.
1947 */
Gilles Peskineceefe5d2021-06-02 21:24:04 +02001948 if( statistically_safe_all_the_way || b != n_bits - 1 )
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001949 {
1950 TEST_ASSERT( stats[b] > 0 );
1951 }
1952 TEST_ASSERT( stats[b] < (size_t) iterations );
1953 }
1954 }
1955
1956exit:
1957 mbedtls_mpi_free( &upper_bound );
1958 mbedtls_mpi_free( &result );
1959 mbedtls_free( stats );
1960}
1961/* END_CASE */
1962
Gilles Peskine1e918f42021-03-29 22:14:51 +02001963/* BEGIN_CASE */
Gilles Peskine422e8672021-04-02 00:02:27 +02001964void mpi_random_sizes( int min, data_t *bound_bytes, int nlimbs, int before )
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001965{
1966 mbedtls_mpi upper_bound;
1967 mbedtls_mpi result;
1968
1969 mbedtls_mpi_init( &upper_bound );
1970 mbedtls_mpi_init( &result );
1971
Gilles Peskine422e8672021-04-02 00:02:27 +02001972 if( before != 0 )
1973 {
1974 /* Set result to sign(before) * 2^(|before|-1) */
1975 TEST_ASSERT( mbedtls_mpi_lset( &result, before > 0 ? 1 : -1 ) == 0 );
1976 if( before < 0 )
1977 before = - before;
1978 TEST_ASSERT( mbedtls_mpi_shift_l( &result, before - 1 ) == 0 );
1979 }
1980
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001981 TEST_EQUAL( 0, mbedtls_mpi_grow( &result, nlimbs ) );
1982 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1983 bound_bytes->x, bound_bytes->len ) );
1984 TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
1985 mbedtls_test_rnd_std_rand, NULL ) );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001986 TEST_ASSERT( sign_is_valid( &result ) );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001987 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
1988 TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
1989
1990exit:
1991 mbedtls_mpi_free( &upper_bound );
1992 mbedtls_mpi_free( &result );
1993}
1994/* END_CASE */
1995
1996/* BEGIN_CASE */
Gilles Peskine1e918f42021-03-29 22:14:51 +02001997void mpi_random_fail( int min, data_t *bound_bytes, int expected_ret )
1998{
1999 mbedtls_mpi upper_bound;
2000 mbedtls_mpi result;
2001 int actual_ret;
2002
2003 mbedtls_mpi_init( &upper_bound );
2004 mbedtls_mpi_init( &result );
2005
2006 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
2007 bound_bytes->x, bound_bytes->len ) );
2008 actual_ret = mbedtls_mpi_random( &result, min, &upper_bound,
2009 mbedtls_test_rnd_std_rand, NULL );
2010 TEST_EQUAL( expected_ret, actual_ret );
2011
2012exit:
2013 mbedtls_mpi_free( &upper_bound );
2014 mbedtls_mpi_free( &result );
2015}
2016/* END_CASE */
2017
Tom Cosgrove0cc78652022-08-23 16:26:52 +01002018/* BEGIN_CASE */
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01002019void mpi_core_add_if( char * input_A, char * input_B,
Tom Cosgrove50c477b2022-09-15 14:28:30 +01002020 char * input_S4, int carry4,
2021 char * input_S8, int carry8 )
Tom Cosgrove0cc78652022-08-23 16:26:52 +01002022{
Tom Cosgrove50c477b2022-09-15 14:28:30 +01002023 mbedtls_mpi S4, S8, A, B;
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01002024 mbedtls_mpi_uint *a = NULL; /* first value to add */
2025 mbedtls_mpi_uint *b = NULL; /* second value to add */
Tom Cosgrove50c477b2022-09-15 14:28:30 +01002026 mbedtls_mpi_uint *sum = NULL;
2027 mbedtls_mpi_uint *d = NULL; /* destination - the in/out first operand */
Tom Cosgrove0cc78652022-08-23 16:26:52 +01002028
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01002029 mbedtls_mpi_init( &A );
2030 mbedtls_mpi_init( &B );
Tom Cosgrove50c477b2022-09-15 14:28:30 +01002031 mbedtls_mpi_init( &S4 );
2032 mbedtls_mpi_init( &S8 );
Tom Cosgrove0cc78652022-08-23 16:26:52 +01002033
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01002034 TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
2035 TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) );
Tom Cosgrove50c477b2022-09-15 14:28:30 +01002036 TEST_EQUAL( 0, mbedtls_test_read_mpi( &S4, input_S4 ) );
2037 TEST_EQUAL( 0, mbedtls_test_read_mpi( &S8, input_S8 ) );
Tom Cosgrove0cc78652022-08-23 16:26:52 +01002038
Tom Cosgrove50c477b2022-09-15 14:28:30 +01002039 /* We only need to work with one of (S4, carry4) or (S8, carry8) depending
Tom Cosgrove0cc78652022-08-23 16:26:52 +01002040 * on sizeof(mbedtls_mpi_uint)
2041 */
Tom Cosgrove50c477b2022-09-15 14:28:30 +01002042 mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &S4 : &S8;
Tom Cosgrove0cc78652022-08-23 16:26:52 +01002043 mbedtls_mpi_uint carry = ( sizeof(mbedtls_mpi_uint) == 4 ) ? carry4 : carry8;
2044
2045 /* All of the inputs are +ve (or zero) */
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01002046 TEST_EQUAL( 1, A.s );
2047 TEST_EQUAL( 1, B.s );
Tom Cosgrove9339f052022-09-01 13:02:53 +01002048 TEST_EQUAL( 1, X->s );
Tom Cosgrove0cc78652022-08-23 16:26:52 +01002049
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01002050 /* Test cases are such that A <= B, so #limbs should be <= */
Tom Cosgrove1feb5ac2022-09-15 14:22:35 +01002051 TEST_LE_U( A.n, B.n );
2052 TEST_LE_U( X->n, B.n );
Tom Cosgrove0cc78652022-08-23 16:26:52 +01002053
2054 /* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */
Tom Cosgrove0cc78652022-08-23 16:26:52 +01002055
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01002056 /* mbedtls_mpi_core_add_if() uses input arrays of mbedtls_mpi_uints which
2057 * must be the same size. The MPIs we've read in will only have arrays
2058 * large enough for the number they represent. Therefore we create new
2059 * raw arrays of mbedtls_mpi_uints and populate them from the MPIs we've
2060 * just read in.
2061 *
2062 * We generated test data such that B was always >= A, so that's how many
2063 * limbs each of these need.
2064 */
Tom Cosgrove1135b202022-09-02 11:46:18 +01002065 size_t limbs = B.n;
Tom Cosgrove1135b202022-09-02 11:46:18 +01002066 size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01002067
Tom Cosgrove2b177922022-09-15 14:07:18 +01002068 /* ASSERT_ALLOC() uses calloc() under the hood, so these do get zeroed */
2069 ASSERT_ALLOC( a, bytes );
2070 ASSERT_ALLOC( b, bytes );
Tom Cosgrove50c477b2022-09-15 14:28:30 +01002071 ASSERT_ALLOC( sum, bytes );
Tom Cosgrove2b177922022-09-15 14:07:18 +01002072 ASSERT_ALLOC( d, bytes );
Tom Cosgrove0cc78652022-08-23 16:26:52 +01002073
2074 /* Populate the arrays. As the mbedtls_mpi_uint[]s in mbedtls_mpis (and as
2075 * processed by mbedtls_mpi_core_add_if()) are little endian, we can just
Tom Cosgrove2b177922022-09-15 14:07:18 +01002076 * copy what we have as long as MSBs are 0 (which they are from ASSERT_ALLOC())
Tom Cosgrove0cc78652022-08-23 16:26:52 +01002077 */
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01002078 memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) );
2079 memcpy( b, B.p, B.n * sizeof(mbedtls_mpi_uint) );
Tom Cosgrove50c477b2022-09-15 14:28:30 +01002080 memcpy( sum, X->p, X->n * sizeof(mbedtls_mpi_uint) );
Tom Cosgrove0cc78652022-08-23 16:26:52 +01002081
Tom Cosgrovedbc15612022-09-15 15:36:23 +01002082 /* The test cases have a <= b to avoid repetition, so we test a + b then,
2083 * if a != b, b + a. If a == b, we can test when a and b are aliased */
Tom Cosgrove17f1fdc2022-09-15 15:23:56 +01002084
2085 /* a + b */
2086
2087 /* cond = 0 => d unchanged, no carry */
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01002088 memcpy( d, a, bytes );
Tom Cosgrove1135b202022-09-02 11:46:18 +01002089 TEST_EQUAL( 0, mbedtls_mpi_core_add_if( d, b, limbs, 0 ) );
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01002090 ASSERT_COMPARE( d, bytes, a, bytes );
Tom Cosgrove0cc78652022-08-23 16:26:52 +01002091
Tom Cosgrove17f1fdc2022-09-15 15:23:56 +01002092 /* cond = 1 => correct result and carry */
Tom Cosgrove1135b202022-09-02 11:46:18 +01002093 TEST_EQUAL( carry, mbedtls_mpi_core_add_if( d, b, limbs, 1 ) );
Tom Cosgrove50c477b2022-09-15 14:28:30 +01002094 ASSERT_COMPARE( d, bytes, sum, bytes );
Tom Cosgrove0cc78652022-08-23 16:26:52 +01002095
Tom Cosgrove1135b202022-09-02 11:46:18 +01002096 if ( A.n == B.n && memcmp( A.p, B.p, bytes ) == 0 )
2097 {
Tom Cosgrovedbc15612022-09-15 15:36:23 +01002098 /* a == b, so test where a and b are aliased */
2099
Tom Cosgrove17f1fdc2022-09-15 15:23:56 +01002100 /* cond = 0 => d unchanged, no carry */
Tom Cosgrove1135b202022-09-02 11:46:18 +01002101 TEST_EQUAL( 0, mbedtls_mpi_core_add_if( b, b, limbs, 0 ) );
2102 ASSERT_COMPARE( b, bytes, B.p, bytes );
2103
Tom Cosgrove17f1fdc2022-09-15 15:23:56 +01002104 /* cond = 1 => correct result and carry */
Tom Cosgrove1135b202022-09-02 11:46:18 +01002105 TEST_EQUAL( carry, mbedtls_mpi_core_add_if( b, b, limbs, 1 ) );
Tom Cosgrove50c477b2022-09-15 14:28:30 +01002106 ASSERT_COMPARE( b, bytes, sum, bytes );
Tom Cosgrove1135b202022-09-02 11:46:18 +01002107 }
Tom Cosgrovedbc15612022-09-15 15:36:23 +01002108 else
2109 {
2110 /* a != b, so test b + a */
2111
2112 /* cond = 0 => d unchanged, no carry */
2113 memcpy( d, b, bytes );
2114 TEST_EQUAL( 0, mbedtls_mpi_core_add_if( d, a, limbs, 0 ) );
2115 ASSERT_COMPARE( d, bytes, b, bytes );
2116
2117 /* cond = 1 => correct result and carry */
2118 TEST_EQUAL( carry, mbedtls_mpi_core_add_if( d, a, limbs, 1 ) );
2119 ASSERT_COMPARE( d, bytes, sum, bytes );
2120 }
Tom Cosgrove1135b202022-09-02 11:46:18 +01002121
Tom Cosgrove0cc78652022-08-23 16:26:52 +01002122exit:
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01002123 mbedtls_free( a );
2124 mbedtls_free( b );
Tom Cosgrove50c477b2022-09-15 14:28:30 +01002125 mbedtls_free( sum );
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01002126 mbedtls_free( d );
Tom Cosgrove0cc78652022-08-23 16:26:52 +01002127
Tom Cosgrove50c477b2022-09-15 14:28:30 +01002128 mbedtls_mpi_free( &S4 );
2129 mbedtls_mpi_free( &S8 );
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01002130 mbedtls_mpi_free( &A );
2131 mbedtls_mpi_free( &B );
Tom Cosgrove0cc78652022-08-23 16:26:52 +01002132}
2133/* END_CASE */
2134
Tom Cosgrove2a65b852022-08-17 05:43:54 +01002135/* BEGIN_CASE */
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01002136void mpi_core_sub( char * input_A, char * input_B,
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01002137 char * input_X4, char * input_X8,
2138 int carry )
Tom Cosgrove2a65b852022-08-17 05:43:54 +01002139{
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01002140 mbedtls_mpi A, B, X4, X8;
2141 mbedtls_mpi_uint *a = NULL;
2142 mbedtls_mpi_uint *b = NULL;
2143 mbedtls_mpi_uint *x = NULL; /* expected */
2144 mbedtls_mpi_uint *r = NULL; /* result */
Tom Cosgrove2a65b852022-08-17 05:43:54 +01002145
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01002146 mbedtls_mpi_init( &A );
2147 mbedtls_mpi_init( &B );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01002148 mbedtls_mpi_init( &X4 );
2149 mbedtls_mpi_init( &X8 );
2150
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01002151 TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
2152 TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) );
Tom Cosgrove9339f052022-09-01 13:02:53 +01002153 TEST_EQUAL( 0, mbedtls_test_read_mpi( &X4, input_X4 ) );
2154 TEST_EQUAL( 0, mbedtls_test_read_mpi( &X8, input_X8 ) );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01002155
2156 /* All of the inputs are +ve (or zero) */
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01002157 TEST_EQUAL( 1, A.s );
2158 TEST_EQUAL( 1, B.s );
Tom Cosgrove9339f052022-09-01 13:02:53 +01002159 TEST_EQUAL( 1, X4.s );
2160 TEST_EQUAL( 1, X8.s );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01002161
2162 /* Get the number of limbs we will need */
Tom Cosgrovee2159f22022-09-15 14:40:10 +01002163 size_t limbs = MAX( A.n, B.n );
Tom Cosgroveb0fb17a2022-09-01 15:04:43 +01002164 size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
Tom Cosgrove2a65b852022-08-17 05:43:54 +01002165
2166 /* We only need to work with X4 or X8, depending on sizeof(mbedtls_mpi_uint) */
2167 mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &X4 : &X8;
2168
2169 /* The result shouldn't have more limbs than the longest input */
Tom Cosgrove1feb5ac2022-09-15 14:22:35 +01002170 TEST_LE_U( X->n, limbs );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01002171
2172 /* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */
Tom Cosgrove2a65b852022-08-17 05:43:54 +01002173
Tom Cosgrove2b177922022-09-15 14:07:18 +01002174 /* ASSERT_ALLOC() uses calloc() under the hood, so these do get zeroed */
2175 ASSERT_ALLOC( a, bytes );
2176 ASSERT_ALLOC( b, bytes );
2177 ASSERT_ALLOC( x, bytes );
2178 ASSERT_ALLOC( r, bytes );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01002179
2180 /* Populate the arrays. As the mbedtls_mpi_uint[]s in mbedtls_mpis (and as
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01002181 * processed by mbedtls_mpi_core_sub()) are little endian, we can just
Tom Cosgrove2b177922022-09-15 14:07:18 +01002182 * copy what we have as long as MSBs are 0 (which they are from ASSERT_ALLOC())
Tom Cosgrove2a65b852022-08-17 05:43:54 +01002183 */
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01002184 memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) );
2185 memcpy( b, B.p, B.n * sizeof(mbedtls_mpi_uint) );
2186 memcpy( x, X->p, X->n * sizeof(mbedtls_mpi_uint) );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01002187
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01002188 /* 1a) r = a - b => we should get the correct carry */
Tom Cosgrovebe7209d2022-09-15 14:32:38 +01002189 TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, a, b, limbs ) );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01002190
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01002191 /* 1b) r = a - b => we should get the correct result */
2192 ASSERT_COMPARE( r, bytes, x, bytes );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01002193
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01002194 /* 2 and 3 test "r may be aliased to a or b" */
2195 /* 2a) r = a; r -= b => we should get the correct carry (use r to avoid clobbering a) */
2196 memcpy( r, a, bytes );
Tom Cosgrovebe7209d2022-09-15 14:32:38 +01002197 TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, r, b, limbs ) );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01002198
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01002199 /* 2b) r -= b => we should get the correct result */
2200 ASSERT_COMPARE( r, bytes, x, bytes );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01002201
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01002202 /* 3a) r = b; r = a - r => we should get the correct carry (use r to avoid clobbering b) */
2203 memcpy( r, b, bytes );
Tom Cosgrovebe7209d2022-09-15 14:32:38 +01002204 TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, a, r, limbs ) );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01002205
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01002206 /* 3b) r = a - b => we should get the correct result */
2207 ASSERT_COMPARE( r, bytes, x, bytes );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01002208
Tom Cosgrovef2b38182022-09-20 09:08:31 +01002209 /* 4 tests "r may be aliased to [...] both" */
2210 if ( A.n == B.n && memcmp( A.p, B.p, bytes ) == 0 )
2211 {
2212 memcpy( r, b, bytes );
2213 TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, r, r, limbs ) );
2214 ASSERT_COMPARE( r, bytes, x, bytes );
2215 }
2216
Tom Cosgrove2a65b852022-08-17 05:43:54 +01002217exit:
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01002218 mbedtls_free( a );
2219 mbedtls_free( b );
2220 mbedtls_free( x );
2221 mbedtls_free( r );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01002222
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01002223 mbedtls_mpi_free( &A );
2224 mbedtls_mpi_free( &B );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01002225 mbedtls_mpi_free( &X4 );
2226 mbedtls_mpi_free( &X8 );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01002227}
2228/* END_CASE */
2229
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002230/* BEGIN_CASE */
Tom Cosgrove42dfac62022-09-02 11:16:39 +01002231void mpi_core_mla( char * input_A, char * input_B, char * input_S,
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01002232 char * input_X4, char * input_cy4,
2233 char * input_X8, char * input_cy8 )
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002234{
Tom Cosgrove42dfac62022-09-02 11:16:39 +01002235 /* We are testing A += B * s; A, B are MPIs, s is a scalar.
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002236 *
Tom Cosgrove359feb02022-09-15 14:52:34 +01002237 * However, we encode s as an MPI in the .data file as the test framework
2238 * currently only supports `int`-typed scalars, and that doesn't cover the
2239 * full range of `mbedtls_mpi_uint`.
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002240 *
2241 * We also have the different results for sizeof(mbedtls_mpi_uint) == 4 or 8.
2242 */
Tom Cosgrove42dfac62022-09-02 11:16:39 +01002243 mbedtls_mpi A, B, S, X4, X8, cy4, cy8;
2244 mbedtls_mpi_uint *a = NULL;
2245 mbedtls_mpi_uint *x = NULL;
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002246
Tom Cosgrove42dfac62022-09-02 11:16:39 +01002247 mbedtls_mpi_init( &A );
2248 mbedtls_mpi_init( &B );
2249 mbedtls_mpi_init( &S );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002250 mbedtls_mpi_init( &X4 );
2251 mbedtls_mpi_init( &X8 );
2252 mbedtls_mpi_init( &cy4 );
2253 mbedtls_mpi_init( &cy8 );
2254
Tom Cosgrove42dfac62022-09-02 11:16:39 +01002255 TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
2256 TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) );
2257 TEST_EQUAL( 0, mbedtls_test_read_mpi( &S, input_S ) );
Tom Cosgrove9339f052022-09-01 13:02:53 +01002258 TEST_EQUAL( 0, mbedtls_test_read_mpi( &X4, input_X4 ) );
2259 TEST_EQUAL( 0, mbedtls_test_read_mpi( &cy4, input_cy4 ) );
2260 TEST_EQUAL( 0, mbedtls_test_read_mpi( &X8, input_X8 ) );
2261 TEST_EQUAL( 0, mbedtls_test_read_mpi( &cy8, input_cy8 ) );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002262
Tom Cosgrove42dfac62022-09-02 11:16:39 +01002263 /* The MPI encoding of scalar s must be only 1 limb */
2264 TEST_EQUAL( 1, S.n );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002265
2266 /* We only need to work with X4 or X8, and cy4 or cy8, depending on sizeof(mbedtls_mpi_uint) */
2267 mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &X4 : &X8;
2268 mbedtls_mpi *cy = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &cy4 : &cy8;
2269
2270 /* The carry should only have one limb */
Tom Cosgrove9339f052022-09-01 13:02:53 +01002271 TEST_EQUAL( 1, cy->n );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002272
2273 /* All of the inputs are +ve (or zero) */
Tom Cosgrove42dfac62022-09-02 11:16:39 +01002274 TEST_EQUAL( 1, A.s );
2275 TEST_EQUAL( 1, B.s );
2276 TEST_EQUAL( 1, S.s );
Tom Cosgrove9339f052022-09-01 13:02:53 +01002277 TEST_EQUAL( 1, X->s );
2278 TEST_EQUAL( 1, cy->s );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002279
2280 /* Get the (max) number of limbs we will need */
Tom Cosgrovee2159f22022-09-15 14:40:10 +01002281 size_t limbs = MAX( A.n, B.n );
Tom Cosgroveb0fb17a2022-09-01 15:04:43 +01002282 size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002283
2284 /* The result shouldn't have more limbs than the longest input */
Tom Cosgrove1feb5ac2022-09-15 14:22:35 +01002285 TEST_LE_U( X->n, limbs );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002286
2287 /* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002288
Tom Cosgrove2b177922022-09-15 14:07:18 +01002289 /* ASSERT_ALLOC() uses calloc() under the hood, so these do get zeroed */
2290 ASSERT_ALLOC( a, bytes );
2291 ASSERT_ALLOC( x, bytes );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002292
2293 /* Populate the arrays. As the mbedtls_mpi_uint[]s in mbedtls_mpis (and as
Tom Cosgrove42dfac62022-09-02 11:16:39 +01002294 * processed by mbedtls_mpi_core_mla()) are little endian, we can just
Tom Cosgrove2b177922022-09-15 14:07:18 +01002295 * copy what we have as long as MSBs are 0 (which they are from ASSERT_ALLOC()).
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002296 */
Tom Cosgrove42dfac62022-09-02 11:16:39 +01002297 memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) );
2298 memcpy( x, X->p, X->n * sizeof(mbedtls_mpi_uint) );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002299
Tom Cosgrove42dfac62022-09-02 11:16:39 +01002300 /* 1a) A += B * s => we should get the correct carry */
2301 TEST_EQUAL( mbedtls_mpi_core_mla( a, limbs, B.p, B.n, *S.p ), *cy->p );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002302
Tom Cosgrove42dfac62022-09-02 11:16:39 +01002303 /* 1b) A += B * s => we should get the correct result */
2304 ASSERT_COMPARE( a, bytes, x, bytes );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002305
Tom Cosgroveb0b77e12022-09-20 13:33:40 +01002306 if ( A.n == B.n && memcmp( A.p, B.p, bytes ) == 0 )
2307 {
2308 /* Check when A and B are aliased */
2309 memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) );
2310 TEST_EQUAL( mbedtls_mpi_core_mla( a, limbs, a, limbs, *S.p ), *cy->p );
2311 ASSERT_COMPARE( a, bytes, x, bytes );
2312 }
2313
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002314exit:
Tom Cosgrove42dfac62022-09-02 11:16:39 +01002315 mbedtls_free( a );
2316 mbedtls_free( x );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002317
Tom Cosgrove42dfac62022-09-02 11:16:39 +01002318 mbedtls_mpi_free( &A );
2319 mbedtls_mpi_free( &B );
2320 mbedtls_mpi_free( &S );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002321 mbedtls_mpi_free( &X4 );
2322 mbedtls_mpi_free( &X8 );
Tom Cosgrove42dfac62022-09-02 11:16:39 +01002323 mbedtls_mpi_free( &cy4 );
2324 mbedtls_mpi_free( &cy8 );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002325}
2326/* END_CASE */
2327
Tom Cosgrove79b70f62022-08-17 06:17:00 +01002328/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01002329void mpi_montg_init( char * input_N, char * input_mm )
Tom Cosgrove79b70f62022-08-17 06:17:00 +01002330{
2331 mbedtls_mpi N, mm;
2332
2333 mbedtls_mpi_init( &N );
2334 mbedtls_mpi_init( &mm );
2335
Tom Cosgrove9339f052022-09-01 13:02:53 +01002336 TEST_EQUAL( 0, mbedtls_test_read_mpi( &N, input_N ) );
2337 TEST_EQUAL( 0, mbedtls_test_read_mpi( &mm, input_mm ) );
Tom Cosgrove79b70f62022-08-17 06:17:00 +01002338
2339 /* The MPI encoding of mm should be 1 limb (sizeof(mbedtls_mpi_uint) == 8) or
2340 * 2 limbs (sizeof(mbedtls_mpi_uint) == 4).
2341 *
2342 * The data file contains the expected result for sizeof(mbedtls_mpi_uint) == 8;
2343 * for sizeof(mbedtls_mpi_uint) == 4 it's just the LSW of this.
2344 */
Tom Cosgroveb2c06f42022-08-24 17:45:58 +01002345 TEST_ASSERT( mm.n == 1 || mm.n == 2 );
Tom Cosgrove79b70f62022-08-17 06:17:00 +01002346
2347 /* All of the inputs are +ve (or zero) */
Tom Cosgrove9339f052022-09-01 13:02:53 +01002348 TEST_EQUAL( 1, N.s );
2349 TEST_EQUAL( 1, mm.s );
Tom Cosgrove79b70f62022-08-17 06:17:00 +01002350
Tom Cosgroveb7438d12022-09-15 15:05:59 +01002351 /* mbedtls_mpi_core_montmul_init() only returns a result, no error possible */
2352 mbedtls_mpi_uint result = mbedtls_mpi_core_montmul_init( N.p );
Tom Cosgrove79b70f62022-08-17 06:17:00 +01002353
2354 /* Check we got the correct result */
2355 TEST_EQUAL( result, mm.p[0] );
2356
2357exit:
2358 mbedtls_mpi_free( &N );
2359 mbedtls_mpi_free( &mm );
2360}
2361/* END_CASE */
2362
Tom Cosgrovef334d962022-08-17 06:29:32 +01002363/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01002364void mpi_core_montmul( int limbs_AN4, int limbs_B4,
2365 int limbs_AN8, int limbs_B8,
2366 char * input_A,
2367 char * input_B,
2368 char * input_N,
2369 char * input_X4,
2370 char * input_X8 )
Tom Cosgrovef334d962022-08-17 06:29:32 +01002371{
Tom Cosgrove93842842022-08-05 16:59:43 +01002372 mbedtls_mpi A, B, N, X4, X8, T, R;
Tom Cosgrovef334d962022-08-17 06:29:32 +01002373
2374 mbedtls_mpi_init( &A );
2375 mbedtls_mpi_init( &B );
2376 mbedtls_mpi_init( &N );
2377 mbedtls_mpi_init( &X4 ); /* expected result, sizeof(mbedtls_mpi_uint) == 4 */
2378 mbedtls_mpi_init( &X8 ); /* expected result, sizeof(mbedtls_mpi_uint) == 8 */
2379 mbedtls_mpi_init( &T );
Tom Cosgrove93842842022-08-05 16:59:43 +01002380 mbedtls_mpi_init( &R ); /* for the result */
Tom Cosgrovef334d962022-08-17 06:29:32 +01002381
Tom Cosgrove9339f052022-09-01 13:02:53 +01002382 TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
2383 TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) );
2384 TEST_EQUAL( 0, mbedtls_test_read_mpi( &N, input_N ) );
2385 TEST_EQUAL( 0, mbedtls_test_read_mpi( &X4, input_X4 ) );
2386 TEST_EQUAL( 0, mbedtls_test_read_mpi( &X8, input_X8 ) );
Tom Cosgrovef334d962022-08-17 06:29:32 +01002387
2388 mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &X4 : &X8;
2389
2390 int limbs_AN = ( sizeof(mbedtls_mpi_uint) == 4 ) ? limbs_AN4 : limbs_AN8;
2391 int limbs_B = ( sizeof(mbedtls_mpi_uint) == 4 ) ? limbs_B4 : limbs_B8;
2392
Tom Cosgrove1feb5ac2022-09-15 14:22:35 +01002393 TEST_LE_U( A.n, (size_t)limbs_AN );
2394 TEST_LE_U( X->n, (size_t)limbs_AN );
2395 TEST_LE_U( B.n, (size_t)limbs_B );
2396 TEST_LE_U( limbs_B, limbs_AN );
Tom Cosgrovef334d962022-08-17 06:29:32 +01002397
2398 /* All of the inputs are +ve (or zero) */
Tom Cosgrove9339f052022-09-01 13:02:53 +01002399 TEST_EQUAL( 1, A.s );
2400 TEST_EQUAL( 1, B.s );
2401 TEST_EQUAL( 1, N.s );
2402 TEST_EQUAL( 1, X->s );
Tom Cosgrovef334d962022-08-17 06:29:32 +01002403
Tom Cosgrove9339f052022-09-01 13:02:53 +01002404 TEST_EQUAL( 0, mbedtls_mpi_grow( &A, limbs_AN ) );
2405 TEST_EQUAL( 0, mbedtls_mpi_grow( &N, limbs_AN ) );
2406 TEST_EQUAL( 0, mbedtls_mpi_grow( X, limbs_AN ) );
2407 TEST_EQUAL( 0, mbedtls_mpi_grow( &B, limbs_B ) );
Tom Cosgrovef334d962022-08-17 06:29:32 +01002408
Tom Cosgrove9339f052022-09-01 13:02:53 +01002409 TEST_EQUAL( 0, mbedtls_mpi_grow( &T, limbs_AN * 2 + 1 ) );
Tom Cosgrovef334d962022-08-17 06:29:32 +01002410
2411 /* Calculate the Montgomery constant (this is unit tested separately) */
Tom Cosgroveb7438d12022-09-15 15:05:59 +01002412 mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init( N.p );
Tom Cosgrovef334d962022-08-17 06:29:32 +01002413
Tom Cosgrove9339f052022-09-01 13:02:53 +01002414 TEST_EQUAL( 0, mbedtls_mpi_grow( &R, limbs_AN ) ); /* ensure it's got the right number of limbs */
Tom Cosgrovef334d962022-08-17 06:29:32 +01002415
Tom Cosgrove93842842022-08-05 16:59:43 +01002416 mbedtls_mpi_core_montmul( R.p, A.p, B.p, B.n, N.p, N.n, mm, T.p );
Tom Cosgroveb0fb17a2022-09-01 15:04:43 +01002417 size_t bytes = N.n * sizeof(mbedtls_mpi_uint);
2418 ASSERT_COMPARE( R.p, bytes, X->p, bytes );
Tom Cosgrovef334d962022-08-17 06:29:32 +01002419
Tom Cosgroveea45c1d2022-09-20 13:17:51 +01002420 /* The output (R, above) may be aliased to A - use R to save the value of A */
2421
2422 memcpy( R.p, A.p, bytes );
2423
2424 mbedtls_mpi_core_montmul( A.p, A.p, B.p, B.n, N.p, N.n, mm, T.p );
2425 ASSERT_COMPARE( A.p, bytes, X->p, bytes );
2426
2427 memcpy( A.p, R.p, bytes ); /* restore A */
2428
2429 /* The output may be aliased to N - use R to save the value of N */
2430
2431 memcpy( R.p, N.p, bytes );
2432
2433 mbedtls_mpi_core_montmul( N.p, A.p, B.p, B.n, N.p, N.n, mm, T.p );
2434 ASSERT_COMPARE( N.p, bytes, X->p, bytes );
2435
2436 memcpy( N.p, R.p, bytes );
2437
Tom Cosgroveea45c1d2022-09-20 13:17:51 +01002438 if (limbs_AN == limbs_B)
2439 {
Tom Cosgrove4386ead2022-09-29 14:40:21 +01002440 /* Test when A aliased to B (requires A == B on input values) */
2441 if ( memcmp( A.p, B.p, bytes ) == 0 )
2442 {
2443 /* Test with A aliased to B and output, since this is permitted -
2444 * don't bother with yet another test with only A and B aliased */
2445
2446 mbedtls_mpi_core_montmul( B.p, B.p, B.p, B.n, N.p, N.n, mm, T.p );
2447 ASSERT_COMPARE( B.p, bytes, X->p, bytes );
2448
2449 memcpy( B.p, A.p, bytes ); /* restore B from equal value A */
2450 }
2451
2452 /* The output may be aliased to B - last test, so we don't save B */
2453
Tom Cosgroveea45c1d2022-09-20 13:17:51 +01002454 mbedtls_mpi_core_montmul( B.p, A.p, B.p, B.n, N.p, N.n, mm, T.p );
2455 ASSERT_COMPARE( B.p, bytes, X->p, bytes );
2456 }
2457
Tom Cosgrovef334d962022-08-17 06:29:32 +01002458exit:
2459 mbedtls_mpi_free( &A );
2460 mbedtls_mpi_free( &B );
2461 mbedtls_mpi_free( &N );
2462 mbedtls_mpi_free( &X4 );
2463 mbedtls_mpi_free( &X8 );
2464 mbedtls_mpi_free( &T );
Tom Cosgrove93842842022-08-05 16:59:43 +01002465 mbedtls_mpi_free( &R );
Tom Cosgrovef334d962022-08-17 06:29:32 +01002466}
2467/* END_CASE */
2468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01002470void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00002471{
Andres AG93012e82016-09-09 09:10:28 +01002472 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00002473}
Paul Bakker33b43f12013-08-20 11:48:36 +02002474/* END_CASE */