blob: 901f9575f53636d9fb7a547a740999922a8395ab [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 */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100767void mpi_lt_mpi_ct( int size_X, char * input_X,
768 int size_Y, char * input_Y,
769 int input_ret, int input_err )
Janos Follath385d5b82019-09-11 16:07:14 +0100770{
Gilles Peskine0deccf12020-09-02 15:18:07 +0200771 unsigned ret = -1;
Janos Follath0e5532d2019-10-11 14:21:53 +0100772 unsigned input_uret = input_ret;
Janos Follath385d5b82019-09-11 16:07:14 +0100773 mbedtls_mpi X, Y;
774 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
775
Werner Lewis19b4cd82022-07-07 11:02:27 +0100776 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
777 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100778
Gilles Peskine9018b112020-01-21 16:30:53 +0100779 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
780 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100781
Janos Follath0e5532d2019-10-11 14:21:53 +0100782 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follath385d5b82019-09-11 16:07:14 +0100783 if( input_err == 0 )
Janos Follath0e5532d2019-10-11 14:21:53 +0100784 TEST_ASSERT( ret == input_uret );
Janos Follath385d5b82019-09-11 16:07:14 +0100785
786exit:
787 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
788}
789/* END_CASE */
790
791/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100792void mpi_cmp_abs( char * input_X, char * input_Y,
793 int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000794{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 mbedtls_mpi X, Y;
796 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000797
Werner Lewis19b4cd82022-07-07 11:02:27 +0100798 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
799 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000801
Paul Bakkerbd51b262014-07-10 15:26:12 +0200802exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000804}
Paul Bakker33b43f12013-08-20 11:48:36 +0200805/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000806
Paul Bakker33b43f12013-08-20 11:48:36 +0200807/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100808void mpi_copy( char *src_hex, char *dst_hex )
Paul Bakker367dae42009-06-28 21:50:27 +0000809{
Gilles Peskined0722f82021-06-10 23:00:33 +0200810 mbedtls_mpi src, dst, ref;
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200811 mbedtls_mpi_init( &src );
812 mbedtls_mpi_init( &dst );
Gilles Peskined0722f82021-06-10 23:00:33 +0200813 mbedtls_mpi_init( &ref );
Paul Bakker367dae42009-06-28 21:50:27 +0000814
Werner Lewis19b4cd82022-07-07 11:02:27 +0100815 TEST_ASSERT( mbedtls_test_read_mpi( &src, src_hex ) == 0 );
816 TEST_ASSERT( mbedtls_test_read_mpi( &ref, dst_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +0200817
818 /* mbedtls_mpi_copy() */
Werner Lewis19b4cd82022-07-07 11:02:27 +0100819 TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200820 TEST_ASSERT( mbedtls_mpi_copy( &dst, &src ) == 0 );
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200821 TEST_ASSERT( sign_is_valid( &dst ) );
822 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000823
Gilles Peskined0722f82021-06-10 23:00:33 +0200824 /* mbedtls_mpi_safe_cond_assign(), assignment done */
825 mbedtls_mpi_free( &dst );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100826 TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +0200827 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 1 ) == 0 );
828 TEST_ASSERT( sign_is_valid( &dst ) );
829 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
830
831 /* mbedtls_mpi_safe_cond_assign(), assignment not done */
832 mbedtls_mpi_free( &dst );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100833 TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +0200834 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 0 ) == 0 );
835 TEST_ASSERT( sign_is_valid( &dst ) );
836 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &ref ) == 0 );
837
Paul Bakkerbd51b262014-07-10 15:26:12 +0200838exit:
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200839 mbedtls_mpi_free( &src );
840 mbedtls_mpi_free( &dst );
Gilles Peskined0722f82021-06-10 23:00:33 +0200841 mbedtls_mpi_free( &ref );
Gilles Peskine7428b452020-01-20 21:01:51 +0100842}
843/* END_CASE */
844
845/* BEGIN_CASE */
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200846void mpi_copy_self( char *input_X )
Gilles Peskine7428b452020-01-20 21:01:51 +0100847{
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200848 mbedtls_mpi X, A;
849 mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000851
Werner Lewis19b4cd82022-07-07 11:02:27 +0100852 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200854
Werner Lewis19b4cd82022-07-07 11:02:27 +0100855 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_X ) == 0 );
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200856 TEST_ASSERT( sign_is_valid( &X ) );
857 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000858
Paul Bakkerbd51b262014-07-10 15:26:12 +0200859exit:
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200860 mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000862}
Paul Bakker33b43f12013-08-20 11:48:36 +0200863/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000864
Paul Bakker33b43f12013-08-20 11:48:36 +0200865/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100866void mpi_swap( char *X_hex, char *Y_hex )
Gilles Peskinefc1eeef2021-06-10 22:29:57 +0200867{
868 mbedtls_mpi X, Y, X0, Y0;
869 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
870 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
871
Werner Lewis19b4cd82022-07-07 11:02:27 +0100872 TEST_ASSERT( mbedtls_test_read_mpi( &X0, X_hex ) == 0 );
873 TEST_ASSERT( mbedtls_test_read_mpi( &Y0, Y_hex ) == 0 );
Gilles Peskinefc1eeef2021-06-10 22:29:57 +0200874
Gilles Peskined0722f82021-06-10 23:00:33 +0200875 /* mbedtls_mpi_swap() */
Werner Lewis19b4cd82022-07-07 11:02:27 +0100876 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
877 TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 );
Gilles Peskinefc1eeef2021-06-10 22:29:57 +0200878 mbedtls_mpi_swap( &X, &Y );
879 TEST_ASSERT( sign_is_valid( &X ) );
880 TEST_ASSERT( sign_is_valid( &Y ) );
881 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
882 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
883
Gilles Peskined0722f82021-06-10 23:00:33 +0200884 /* mbedtls_mpi_safe_cond_swap(), swap done */
885 mbedtls_mpi_free( &X );
886 mbedtls_mpi_free( &Y );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100887 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
888 TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +0200889 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
890 TEST_ASSERT( sign_is_valid( &X ) );
891 TEST_ASSERT( sign_is_valid( &Y ) );
892 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
893 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
894
895 /* mbedtls_mpi_safe_cond_swap(), swap not done */
896 mbedtls_mpi_free( &X );
897 mbedtls_mpi_free( &Y );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100898 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
899 TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +0200900 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
901 TEST_ASSERT( sign_is_valid( &X ) );
902 TEST_ASSERT( sign_is_valid( &Y ) );
903 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
904 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &Y0 ) == 0 );
905
Gilles Peskinefc1eeef2021-06-10 22:29:57 +0200906exit:
907 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
908 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
909}
910/* END_CASE */
911
912/* BEGIN_CASE */
913void mpi_swap_self( char *X_hex )
914{
915 mbedtls_mpi X, X0;
916 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
917
Werner Lewis19b4cd82022-07-07 11:02:27 +0100918 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
919 TEST_ASSERT( mbedtls_test_read_mpi( &X0, X_hex ) == 0 );
Gilles Peskinefc1eeef2021-06-10 22:29:57 +0200920
921 mbedtls_mpi_swap( &X, &X );
922 TEST_ASSERT( sign_is_valid( &X ) );
923 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
924
925exit:
926 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
927}
928/* END_CASE */
929
930/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100931void mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100932{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 mbedtls_mpi X;
934 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Gilles Peskinee1091752021-06-15 21:19:18 +0200937 if( used > 0 )
938 {
939 size_t used_bit_count = used * 8 * sizeof( mbedtls_mpi_uint );
940 TEST_ASSERT( mbedtls_mpi_set_bit( &X, used_bit_count - 1, 1 ) == 0 );
941 }
942 TEST_EQUAL( X.n, (size_t) before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Gilles Peskinee1091752021-06-15 21:19:18 +0200944 TEST_EQUAL( X.n, (size_t) after );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100945
Paul Bakkerbd51b262014-07-10 15:26:12 +0200946exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100948}
949/* END_CASE */
950
951/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100952void mpi_add_mpi( char * input_X, char * input_Y,
953 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000954{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 mbedtls_mpi X, Y, Z, A;
956 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000957
Werner Lewis19b4cd82022-07-07 11:02:27 +0100958 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
959 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
960 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200962 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000964
Gilles Peskine56f943a2020-07-23 01:18:11 +0200965 /* result == first operand */
966 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200967 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200968 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100969 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200970
971 /* result == second operand */
972 TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200973 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200974 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
975
Paul Bakkerbd51b262014-07-10 15:26:12 +0200976exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000978}
Paul Bakker33b43f12013-08-20 11:48:36 +0200979/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000980
Paul Bakker33b43f12013-08-20 11:48:36 +0200981/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +0100982void mpi_add_mpi_inplace( char * input_X, char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100983{
984 mbedtls_mpi X, A;
985 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
986
Werner Lewis19b4cd82022-07-07 11:02:27 +0100987 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100988
Werner Lewis19b4cd82022-07-07 11:02:27 +0100989 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100990 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
991 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200992 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath6cbacec2015-10-25 12:29:13 +0100993
Werner Lewis19b4cd82022-07-07 11:02:27 +0100994 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100995 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200996 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath6cbacec2015-10-25 12:29:13 +0100997 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
998
Werner Lewis19b4cd82022-07-07 11:02:27 +0100999 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +01001000 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001001 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath044a86b2015-10-25 10:58:03 +01001002 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
1003
1004exit:
1005 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
1006}
1007/* END_CASE */
1008
1009
1010/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001011void mpi_add_abs( char * input_X, char * input_Y,
1012 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001013{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 mbedtls_mpi X, Y, Z, A;
1015 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001016
Werner Lewis19b4cd82022-07-07 11:02:27 +01001017 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1018 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1019 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001021 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001023
Gilles Peskine56f943a2020-07-23 01:18:11 +02001024 /* result == first operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001026 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis19b4cd82022-07-07 11:02:27 +01001028 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001029
1030 /* result == second operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001032 TEST_ASSERT( sign_is_valid( &Y ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001034
Paul Bakkerbd51b262014-07-10 15:26:12 +02001035exit:
Gilles Peskine56f943a2020-07-23 01:18:11 +02001036 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +00001037}
Paul Bakker33b43f12013-08-20 11:48:36 +02001038/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +00001039
Paul Bakker33b43f12013-08-20 11:48:36 +02001040/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001041void mpi_add_int( char * input_X, int input_Y,
1042 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001043{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 mbedtls_mpi X, Z, A;
1045 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001046
Werner Lewis19b4cd82022-07-07 11:02:27 +01001047 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1048 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001050 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001052
Paul Bakkerbd51b262014-07-10 15:26:12 +02001053exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001055}
Paul Bakker33b43f12013-08-20 11:48:36 +02001056/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001057
Paul Bakker33b43f12013-08-20 11:48:36 +02001058/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001059void mpi_sub_mpi( char * input_X, char * input_Y,
1060 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001061{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 mbedtls_mpi X, Y, Z, A;
1063 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001064
Werner Lewis19b4cd82022-07-07 11:02:27 +01001065 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1066 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1067 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001069 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001071
Gilles Peskine56f943a2020-07-23 01:18:11 +02001072 /* result == first operand */
1073 TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001074 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001075 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis19b4cd82022-07-07 11:02:27 +01001076 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001077
1078 /* result == second operand */
1079 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001080 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001081 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
1082
Paul Bakkerbd51b262014-07-10 15:26:12 +02001083exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001085}
Paul Bakker33b43f12013-08-20 11:48:36 +02001086/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001087
Paul Bakker33b43f12013-08-20 11:48:36 +02001088/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001089void mpi_sub_abs( char * input_X, char * input_Y,
1090 char * input_A, int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001091{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001093 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001095
Werner Lewis19b4cd82022-07-07 11:02:27 +01001096 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1097 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1098 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001101 TEST_ASSERT( res == sub_result );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001102 TEST_ASSERT( sign_is_valid( &Z ) );
Paul Bakker367dae42009-06-28 21:50:27 +00001103 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001105
Gilles Peskine56f943a2020-07-23 01:18:11 +02001106 /* result == first operand */
1107 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001108 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001109 if( sub_result == 0 )
1110 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis19b4cd82022-07-07 11:02:27 +01001111 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001112
1113 /* result == second operand */
1114 TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001115 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001116 if( sub_result == 0 )
1117 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
1118
Paul Bakkerbd51b262014-07-10 15:26:12 +02001119exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001121}
Paul Bakker33b43f12013-08-20 11:48:36 +02001122/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001123
Paul Bakker33b43f12013-08-20 11:48:36 +02001124/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001125void mpi_sub_int( char * input_X, int input_Y,
1126 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001127{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 mbedtls_mpi X, Z, A;
1129 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001130
Werner Lewis19b4cd82022-07-07 11:02:27 +01001131 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1132 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001134 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001136
Paul Bakkerbd51b262014-07-10 15:26:12 +02001137exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001139}
Paul Bakker33b43f12013-08-20 11:48:36 +02001140/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001141
Paul Bakker33b43f12013-08-20 11:48:36 +02001142/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001143void mpi_mul_mpi( char * input_X, char * input_Y,
1144 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001145{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 mbedtls_mpi X, Y, Z, A;
1147 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001148
Werner Lewis19b4cd82022-07-07 11:02:27 +01001149 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1150 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1151 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001153 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001155
Paul Bakkerbd51b262014-07-10 15:26:12 +02001156exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001158}
Paul Bakker33b43f12013-08-20 11:48:36 +02001159/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001160
Paul Bakker33b43f12013-08-20 11:48:36 +02001161/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001162void mpi_mul_int( char * input_X, int input_Y,
1163 char * input_A, char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +00001164{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 mbedtls_mpi X, Z, A;
1166 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001167
Werner Lewis19b4cd82022-07-07 11:02:27 +01001168 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1169 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001171 TEST_ASSERT( sign_is_valid( &Z ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001172 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001174 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001176 else
1177 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001178
Paul Bakkerbd51b262014-07-10 15:26:12 +02001179exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001181}
Paul Bakker33b43f12013-08-20 11:48:36 +02001182/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001183
Paul Bakker33b43f12013-08-20 11:48:36 +02001184/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001185void mpi_div_mpi( char * input_X, char * input_Y,
1186 char * input_A, char * input_B,
1187 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001188{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001190 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
1192 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001193
Werner Lewis19b4cd82022-07-07 11:02:27 +01001194 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1195 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1196 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
1197 TEST_ASSERT( mbedtls_test_read_mpi( &B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001199 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001200 if( res == 0 )
1201 {
Gilles Peskinedffc7102021-06-10 15:34:15 +02001202 TEST_ASSERT( sign_is_valid( &Q ) );
1203 TEST_ASSERT( sign_is_valid( &R ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1205 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001206 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001207
Paul Bakkerbd51b262014-07-10 15:26:12 +02001208exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
1210 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001211}
Paul Bakker33b43f12013-08-20 11:48:36 +02001212/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001213
Paul Bakker33b43f12013-08-20 11:48:36 +02001214/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001215void mpi_div_int( char * input_X, int input_Y,
1216 char * input_A, char * input_B,
1217 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001218{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001220 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
1222 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001223
Werner Lewis19b4cd82022-07-07 11:02:27 +01001224 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1225 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
1226 TEST_ASSERT( mbedtls_test_read_mpi( &B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001228 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001229 if( res == 0 )
1230 {
Gilles Peskinedffc7102021-06-10 15:34:15 +02001231 TEST_ASSERT( sign_is_valid( &Q ) );
1232 TEST_ASSERT( sign_is_valid( &R ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1234 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001235 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001236
Paul Bakkerbd51b262014-07-10 15:26:12 +02001237exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
1239 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001240}
Paul Bakker33b43f12013-08-20 11:48:36 +02001241/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001242
Paul Bakker33b43f12013-08-20 11:48:36 +02001243/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001244void mpi_mod_mpi( char * input_X, char * input_Y,
1245 char * input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001246{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001248 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001250
Werner Lewis19b4cd82022-07-07 11:02:27 +01001251 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1252 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1253 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001255 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001256 if( res == 0 )
1257 {
Gilles Peskinedffc7102021-06-10 15:34:15 +02001258 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001260 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001261
Paul Bakkerbd51b262014-07-10 15:26:12 +02001262exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001264}
Paul Bakker33b43f12013-08-20 11:48:36 +02001265/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001266
Paul Bakker33b43f12013-08-20 11:48:36 +02001267/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001268void mpi_mod_int( char * input_X, int input_Y,
1269 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001270{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001272 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 mbedtls_mpi_uint r;
1274 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001275
Werner Lewis19b4cd82022-07-07 11:02:27 +01001276 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001278 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001279 if( res == 0 )
1280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001282 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001283
Paul Bakkerbd51b262014-07-10 15:26:12 +02001284exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001286}
Paul Bakker33b43f12013-08-20 11:48:36 +02001287/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001288
Paul Bakker33b43f12013-08-20 11:48:36 +02001289/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001290void mpi_exp_mod( char * input_A, char * input_E,
1291 char * input_N, char * input_X,
1292 int exp_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001293{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001295 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1297 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001298
Werner Lewis19b4cd82022-07-07 11:02:27 +01001299 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
1300 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
1301 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
1302 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001303
Gilles Peskine342f71b2021-06-09 18:31:35 +02001304 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, NULL );
Gilles Peskine722c62c2021-06-15 21:55:05 +02001305 TEST_ASSERT( res == exp_result );
Gilles Peskine342f71b2021-06-09 18:31:35 +02001306 if( res == 0 )
1307 {
1308 TEST_ASSERT( sign_is_valid( &Z ) );
1309 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
1310 }
1311
1312 /* Now test again with the speed-up parameter supplied as an output. */
1313 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Gilles Peskine722c62c2021-06-15 21:55:05 +02001314 TEST_ASSERT( res == exp_result );
Gilles Peskine342f71b2021-06-09 18:31:35 +02001315 if( res == 0 )
1316 {
1317 TEST_ASSERT( sign_is_valid( &Z ) );
1318 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
1319 }
1320
1321 /* Now test again with the speed-up parameter supplied in calculated form. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Gilles Peskine722c62c2021-06-15 21:55:05 +02001323 TEST_ASSERT( res == exp_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001324 if( res == 0 )
1325 {
Gilles Peskinedffc7102021-06-10 15:34:15 +02001326 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001328 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001329
Paul Bakkerbd51b262014-07-10 15:26:12 +02001330exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1332 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001333}
Paul Bakker33b43f12013-08-20 11:48:36 +02001334/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001335
Paul Bakker33b43f12013-08-20 11:48:36 +02001336/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001337void mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
1338 char * input_RR, int exp_result )
Chris Jonesd10b3312020-12-02 10:41:50 +00001339{
1340 mbedtls_mpi A, E, N, RR, Z;
1341 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1342 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
1343
Chris Jonesaa850cd2020-12-03 11:35:41 +00001344 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jonesd10b3312020-12-02 10:41:50 +00001345 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001346 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001347 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001348
1349 /* Set E to 2^(E_bytes - 1) + 1 */
1350 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
1351 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001352 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001353
1354 /* Set N to 2^(N_bytes - 1) + 1 */
1355 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
1356 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001357 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
1358
1359 if( strlen( input_RR ) )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001360 TEST_ASSERT( mbedtls_test_read_mpi( &RR, input_RR ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001361
Chris Jonesaa850cd2020-12-03 11:35:41 +00001362 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jonesd10b3312020-12-02 10:41:50 +00001363
1364exit:
1365 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1366 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
1367}
1368/* END_CASE */
1369
1370/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001371void mpi_inv_mod( char * input_X, char * input_Y,
1372 char * input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001375 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001377
Werner Lewis19b4cd82022-07-07 11:02:27 +01001378 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1379 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1380 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001382 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001383 if( res == 0 )
1384 {
Gilles Peskinedffc7102021-06-10 15:34:15 +02001385 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001387 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001388
Paul Bakkerbd51b262014-07-10 15:26:12 +02001389exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001391}
Paul Bakker33b43f12013-08-20 11:48:36 +02001392/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001395void mpi_is_prime( char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001396{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001398 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001400
Werner Lewis19b4cd82022-07-07 11:02:27 +01001401 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Ronald Cron351f0ee2020-06-10 12:12:18 +02001402 res = mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001403 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001404
Paul Bakkerbd51b262014-07-10 15:26:12 +02001405exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001407}
Paul Bakker33b43f12013-08-20 11:48:36 +02001408/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001411void mpi_is_prime_det( data_t * input_X, data_t * witnesses,
1412 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001413{
1414 mbedtls_mpi X;
1415 int res;
1416 mbedtls_test_mpi_random rand;
1417
1418 mbedtls_mpi_init( &X );
1419 rand.data = witnesses;
1420 rand.pos = 0;
1421 rand.chunk_len = chunk_len;
1422
1423 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001424 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1425 mbedtls_test_mpi_miller_rabin_determinizer,
1426 &rand );
1427 TEST_ASSERT( res == 0 );
1428
1429 rand.data = witnesses;
1430 rand.pos = 0;
1431 rand.chunk_len = chunk_len;
1432
Janos Follatha0b67c22018-09-18 14:48:23 +01001433 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1434 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001435 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001436 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001437
1438exit:
1439 mbedtls_mpi_free( &X );
1440}
1441/* END_CASE */
1442
1443/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001444void mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001445{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001447 int my_ret;
1448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001450
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001451 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags,
1452 mbedtls_test_rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001453 TEST_ASSERT( my_ret == ref_ret );
1454
1455 if( ref_ret == 0 )
1456 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001457 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001458
1459 TEST_ASSERT( actual_bits >= (size_t) bits );
1460 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001461 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001462
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001463 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1464 mbedtls_test_rnd_std_rand,
1465 NULL ) == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001466 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001467 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001468 /* X = ( X - 1 ) / 2 */
1469 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001470 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1471 mbedtls_test_rnd_std_rand,
1472 NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001473 }
1474 }
1475
Paul Bakkerbd51b262014-07-10 15:26:12 +02001476exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001478}
1479/* END_CASE */
1480
Paul Bakker33b43f12013-08-20 11:48:36 +02001481/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001482void mpi_shift_l( char * input_X, int shift_X,
1483 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001484{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 mbedtls_mpi X, A;
1486 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001487
Werner Lewis19b4cd82022-07-07 11:02:27 +01001488 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1489 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001491 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001493
Paul Bakkerbd51b262014-07-10 15:26:12 +02001494exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001496}
Paul Bakker33b43f12013-08-20 11:48:36 +02001497/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001498
Paul Bakker33b43f12013-08-20 11:48:36 +02001499/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001500void mpi_shift_r( char * input_X, int shift_X,
1501 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001502{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 mbedtls_mpi X, A;
1504 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001505
Werner Lewis19b4cd82022-07-07 11:02:27 +01001506 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1507 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001509 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001511
Paul Bakkerbd51b262014-07-10 15:26:12 +02001512exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001514}
Paul Bakker33b43f12013-08-20 11:48:36 +02001515/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001516
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001517/* BEGIN_CASE */
Gilles Peskine422e8672021-04-02 00:02:27 +02001518void mpi_fill_random( int wanted_bytes, int rng_bytes,
1519 int before, int expected_ret )
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001520{
1521 mbedtls_mpi X;
1522 int ret;
1523 size_t bytes_left = rng_bytes;
1524 mbedtls_mpi_init( &X );
1525
Gilles Peskine422e8672021-04-02 00:02:27 +02001526 if( before != 0 )
1527 {
1528 /* Set X to sign(before) * 2^(|before|-1) */
1529 TEST_ASSERT( mbedtls_mpi_lset( &X, before > 0 ? 1 : -1 ) == 0 );
1530 if( before < 0 )
1531 before = - before;
1532 TEST_ASSERT( mbedtls_mpi_shift_l( &X, before - 1 ) == 0 );
1533 }
1534
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001535 ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
1536 f_rng_bytes_left, &bytes_left );
1537 TEST_ASSERT( ret == expected_ret );
1538
1539 if( expected_ret == 0 )
1540 {
1541 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
1542 * as a big-endian representation of the number. We know when
1543 * our RNG function returns null bytes, so we know how many
1544 * leading zero bytes the number has. */
1545 size_t leading_zeros = 0;
1546 if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
1547 leading_zeros = 1;
1548 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
1549 (size_t) wanted_bytes );
1550 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001551 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001552 }
1553
1554exit:
1555 mbedtls_mpi_free( &X );
1556}
1557/* END_CASE */
1558
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001559/* BEGIN_CASE */
1560void mpi_random_many( int min, data_t *bound_bytes, int iterations )
1561{
1562 /* Generate numbers in the range 1..bound-1. Do it iterations times.
1563 * This function assumes that the value of bound is at least 2 and
1564 * that iterations is large enough that a one-in-2^iterations chance
1565 * effectively never occurs.
1566 */
1567
1568 mbedtls_mpi upper_bound;
1569 size_t n_bits;
1570 mbedtls_mpi result;
1571 size_t b;
1572 /* If upper_bound is small, stats[b] is the number of times the value b
1573 * has been generated. Otherwise stats[b] is the number of times a
1574 * value with bit b set has been generated. */
1575 size_t *stats = NULL;
1576 size_t stats_len;
1577 int full_stats;
1578 size_t i;
1579
1580 mbedtls_mpi_init( &upper_bound );
1581 mbedtls_mpi_init( &result );
1582
1583 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1584 bound_bytes->x, bound_bytes->len ) );
1585 n_bits = mbedtls_mpi_bitlen( &upper_bound );
1586 /* Consider a bound "small" if it's less than 2^5. This value is chosen
1587 * to be small enough that the probability of missing one value is
1588 * negligible given the number of iterations. It must be less than
1589 * 256 because some of the code below assumes that "small" values
1590 * fit in a byte. */
1591 if( n_bits <= 5 )
1592 {
1593 full_stats = 1;
1594 stats_len = bound_bytes->x[bound_bytes->len - 1];
1595 }
1596 else
1597 {
1598 full_stats = 0;
1599 stats_len = n_bits;
1600 }
1601 ASSERT_ALLOC( stats, stats_len );
1602
1603 for( i = 0; i < (size_t) iterations; i++ )
1604 {
1605 mbedtls_test_set_step( i );
1606 TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
1607 mbedtls_test_rnd_std_rand, NULL ) );
1608
Gilles Peskinedffc7102021-06-10 15:34:15 +02001609 TEST_ASSERT( sign_is_valid( &result ) );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001610 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
1611 TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
1612 if( full_stats )
1613 {
1614 uint8_t value;
1615 TEST_EQUAL( 0, mbedtls_mpi_write_binary( &result, &value, 1 ) );
1616 TEST_ASSERT( value < stats_len );
1617 ++stats[value];
1618 }
1619 else
1620 {
1621 for( b = 0; b < n_bits; b++ )
1622 stats[b] += mbedtls_mpi_get_bit( &result, b );
1623 }
1624 }
1625
1626 if( full_stats )
1627 {
Gilles Peskined463edf2021-04-13 20:45:05 +02001628 for( b = min; b < stats_len; b++ )
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001629 {
1630 mbedtls_test_set_step( 1000000 + b );
1631 /* Assert that each value has been reached at least once.
1632 * This is almost guaranteed if the iteration count is large
1633 * enough. This is a very crude way of checking the distribution.
1634 */
1635 TEST_ASSERT( stats[b] > 0 );
1636 }
1637 }
1638 else
1639 {
Gilles Peskineceefe5d2021-06-02 21:24:04 +02001640 int statistically_safe_all_the_way =
1641 is_significantly_above_a_power_of_2( bound_bytes );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001642 for( b = 0; b < n_bits; b++ )
1643 {
1644 mbedtls_test_set_step( 1000000 + b );
1645 /* Assert that each bit has been set in at least one result and
1646 * clear in at least one result. Provided that iterations is not
1647 * too small, it would be extremely unlikely for this not to be
1648 * the case if the results are uniformly distributed.
1649 *
1650 * As an exception, the top bit may legitimately never be set
1651 * if bound is a power of 2 or only slightly above.
1652 */
Gilles Peskineceefe5d2021-06-02 21:24:04 +02001653 if( statistically_safe_all_the_way || b != n_bits - 1 )
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001654 {
1655 TEST_ASSERT( stats[b] > 0 );
1656 }
1657 TEST_ASSERT( stats[b] < (size_t) iterations );
1658 }
1659 }
1660
1661exit:
1662 mbedtls_mpi_free( &upper_bound );
1663 mbedtls_mpi_free( &result );
1664 mbedtls_free( stats );
1665}
1666/* END_CASE */
1667
Gilles Peskine1e918f42021-03-29 22:14:51 +02001668/* BEGIN_CASE */
Gilles Peskine422e8672021-04-02 00:02:27 +02001669void mpi_random_sizes( int min, data_t *bound_bytes, int nlimbs, int before )
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001670{
1671 mbedtls_mpi upper_bound;
1672 mbedtls_mpi result;
1673
1674 mbedtls_mpi_init( &upper_bound );
1675 mbedtls_mpi_init( &result );
1676
Gilles Peskine422e8672021-04-02 00:02:27 +02001677 if( before != 0 )
1678 {
1679 /* Set result to sign(before) * 2^(|before|-1) */
1680 TEST_ASSERT( mbedtls_mpi_lset( &result, before > 0 ? 1 : -1 ) == 0 );
1681 if( before < 0 )
1682 before = - before;
1683 TEST_ASSERT( mbedtls_mpi_shift_l( &result, before - 1 ) == 0 );
1684 }
1685
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001686 TEST_EQUAL( 0, mbedtls_mpi_grow( &result, nlimbs ) );
1687 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1688 bound_bytes->x, bound_bytes->len ) );
1689 TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
1690 mbedtls_test_rnd_std_rand, NULL ) );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001691 TEST_ASSERT( sign_is_valid( &result ) );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001692 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
1693 TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
1694
1695exit:
1696 mbedtls_mpi_free( &upper_bound );
1697 mbedtls_mpi_free( &result );
1698}
1699/* END_CASE */
1700
1701/* BEGIN_CASE */
Gilles Peskine1e918f42021-03-29 22:14:51 +02001702void mpi_random_fail( int min, data_t *bound_bytes, int expected_ret )
1703{
1704 mbedtls_mpi upper_bound;
1705 mbedtls_mpi result;
1706 int actual_ret;
1707
1708 mbedtls_mpi_init( &upper_bound );
1709 mbedtls_mpi_init( &result );
1710
1711 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1712 bound_bytes->x, bound_bytes->len ) );
1713 actual_ret = mbedtls_mpi_random( &result, min, &upper_bound,
1714 mbedtls_test_rnd_std_rand, NULL );
1715 TEST_EQUAL( expected_ret, actual_ret );
1716
1717exit:
1718 mbedtls_mpi_free( &upper_bound );
1719 mbedtls_mpi_free( &result );
1720}
1721/* END_CASE */
1722
Tom Cosgrove0cc78652022-08-23 16:26:52 +01001723/* BEGIN_CASE */
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01001724void mpi_core_add_if( char * input_A, char * input_B,
Tom Cosgrove50c477b2022-09-15 14:28:30 +01001725 char * input_S4, int carry4,
1726 char * input_S8, int carry8 )
Tom Cosgrove0cc78652022-08-23 16:26:52 +01001727{
Tom Cosgrove50c477b2022-09-15 14:28:30 +01001728 mbedtls_mpi S4, S8, A, B;
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01001729 mbedtls_mpi_uint *a = NULL; /* first value to add */
1730 mbedtls_mpi_uint *b = NULL; /* second value to add */
Tom Cosgrove50c477b2022-09-15 14:28:30 +01001731 mbedtls_mpi_uint *sum = NULL;
1732 mbedtls_mpi_uint *d = NULL; /* destination - the in/out first operand */
Tom Cosgrove0cc78652022-08-23 16:26:52 +01001733
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01001734 mbedtls_mpi_init( &A );
1735 mbedtls_mpi_init( &B );
Tom Cosgrove50c477b2022-09-15 14:28:30 +01001736 mbedtls_mpi_init( &S4 );
1737 mbedtls_mpi_init( &S8 );
Tom Cosgrove0cc78652022-08-23 16:26:52 +01001738
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01001739 TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
1740 TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) );
Tom Cosgrove50c477b2022-09-15 14:28:30 +01001741 TEST_EQUAL( 0, mbedtls_test_read_mpi( &S4, input_S4 ) );
1742 TEST_EQUAL( 0, mbedtls_test_read_mpi( &S8, input_S8 ) );
Tom Cosgrove0cc78652022-08-23 16:26:52 +01001743
Tom Cosgrove50c477b2022-09-15 14:28:30 +01001744 /* We only need to work with one of (S4, carry4) or (S8, carry8) depending
Tom Cosgrove0cc78652022-08-23 16:26:52 +01001745 * on sizeof(mbedtls_mpi_uint)
1746 */
Tom Cosgrove50c477b2022-09-15 14:28:30 +01001747 mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &S4 : &S8;
Tom Cosgrove0cc78652022-08-23 16:26:52 +01001748 mbedtls_mpi_uint carry = ( sizeof(mbedtls_mpi_uint) == 4 ) ? carry4 : carry8;
1749
1750 /* All of the inputs are +ve (or zero) */
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01001751 TEST_EQUAL( 1, A.s );
1752 TEST_EQUAL( 1, B.s );
Tom Cosgrove9339f052022-09-01 13:02:53 +01001753 TEST_EQUAL( 1, X->s );
Tom Cosgrove0cc78652022-08-23 16:26:52 +01001754
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01001755 /* Test cases are such that A <= B, so #limbs should be <= */
Tom Cosgrove1feb5ac2022-09-15 14:22:35 +01001756 TEST_LE_U( A.n, B.n );
1757 TEST_LE_U( X->n, B.n );
Tom Cosgrove0cc78652022-08-23 16:26:52 +01001758
1759 /* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */
Tom Cosgrove0cc78652022-08-23 16:26:52 +01001760
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01001761 /* mbedtls_mpi_core_add_if() uses input arrays of mbedtls_mpi_uints which
1762 * must be the same size. The MPIs we've read in will only have arrays
1763 * large enough for the number they represent. Therefore we create new
1764 * raw arrays of mbedtls_mpi_uints and populate them from the MPIs we've
1765 * just read in.
1766 *
1767 * We generated test data such that B was always >= A, so that's how many
1768 * limbs each of these need.
1769 */
Tom Cosgrove1135b202022-09-02 11:46:18 +01001770 size_t limbs = B.n;
Tom Cosgrove1135b202022-09-02 11:46:18 +01001771 size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01001772
Tom Cosgrove2b177922022-09-15 14:07:18 +01001773 /* ASSERT_ALLOC() uses calloc() under the hood, so these do get zeroed */
1774 ASSERT_ALLOC( a, bytes );
1775 ASSERT_ALLOC( b, bytes );
Tom Cosgrove50c477b2022-09-15 14:28:30 +01001776 ASSERT_ALLOC( sum, bytes );
Tom Cosgrove2b177922022-09-15 14:07:18 +01001777 ASSERT_ALLOC( d, bytes );
Tom Cosgrove0cc78652022-08-23 16:26:52 +01001778
1779 /* Populate the arrays. As the mbedtls_mpi_uint[]s in mbedtls_mpis (and as
1780 * processed by mbedtls_mpi_core_add_if()) are little endian, we can just
Tom Cosgrove2b177922022-09-15 14:07:18 +01001781 * copy what we have as long as MSBs are 0 (which they are from ASSERT_ALLOC())
Tom Cosgrove0cc78652022-08-23 16:26:52 +01001782 */
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01001783 memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) );
1784 memcpy( b, B.p, B.n * sizeof(mbedtls_mpi_uint) );
Tom Cosgrove50c477b2022-09-15 14:28:30 +01001785 memcpy( sum, X->p, X->n * sizeof(mbedtls_mpi_uint) );
Tom Cosgrove0cc78652022-08-23 16:26:52 +01001786
Tom Cosgrovedbc15612022-09-15 15:36:23 +01001787 /* The test cases have a <= b to avoid repetition, so we test a + b then,
1788 * if a != b, b + a. If a == b, we can test when a and b are aliased */
Tom Cosgrove17f1fdc2022-09-15 15:23:56 +01001789
1790 /* a + b */
1791
1792 /* cond = 0 => d unchanged, no carry */
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01001793 memcpy( d, a, bytes );
Tom Cosgrove1135b202022-09-02 11:46:18 +01001794 TEST_EQUAL( 0, mbedtls_mpi_core_add_if( d, b, limbs, 0 ) );
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01001795 ASSERT_COMPARE( d, bytes, a, bytes );
Tom Cosgrove0cc78652022-08-23 16:26:52 +01001796
Tom Cosgrove17f1fdc2022-09-15 15:23:56 +01001797 /* cond = 1 => correct result and carry */
Tom Cosgrove1135b202022-09-02 11:46:18 +01001798 TEST_EQUAL( carry, mbedtls_mpi_core_add_if( d, b, limbs, 1 ) );
Tom Cosgrove50c477b2022-09-15 14:28:30 +01001799 ASSERT_COMPARE( d, bytes, sum, bytes );
Tom Cosgrove0cc78652022-08-23 16:26:52 +01001800
Tom Cosgrove1135b202022-09-02 11:46:18 +01001801 if ( A.n == B.n && memcmp( A.p, B.p, bytes ) == 0 )
1802 {
Tom Cosgrovedbc15612022-09-15 15:36:23 +01001803 /* a == b, so test where a and b are aliased */
1804
Tom Cosgrove17f1fdc2022-09-15 15:23:56 +01001805 /* cond = 0 => d unchanged, no carry */
Tom Cosgrove1135b202022-09-02 11:46:18 +01001806 TEST_EQUAL( 0, mbedtls_mpi_core_add_if( b, b, limbs, 0 ) );
1807 ASSERT_COMPARE( b, bytes, B.p, bytes );
1808
Tom Cosgrove17f1fdc2022-09-15 15:23:56 +01001809 /* cond = 1 => correct result and carry */
Tom Cosgrove1135b202022-09-02 11:46:18 +01001810 TEST_EQUAL( carry, mbedtls_mpi_core_add_if( b, b, limbs, 1 ) );
Tom Cosgrove50c477b2022-09-15 14:28:30 +01001811 ASSERT_COMPARE( b, bytes, sum, bytes );
Tom Cosgrove1135b202022-09-02 11:46:18 +01001812 }
Tom Cosgrovedbc15612022-09-15 15:36:23 +01001813 else
1814 {
1815 /* a != b, so test b + a */
1816
1817 /* cond = 0 => d unchanged, no carry */
1818 memcpy( d, b, bytes );
1819 TEST_EQUAL( 0, mbedtls_mpi_core_add_if( d, a, limbs, 0 ) );
1820 ASSERT_COMPARE( d, bytes, b, bytes );
1821
1822 /* cond = 1 => correct result and carry */
1823 TEST_EQUAL( carry, mbedtls_mpi_core_add_if( d, a, limbs, 1 ) );
1824 ASSERT_COMPARE( d, bytes, sum, bytes );
1825 }
Tom Cosgrove1135b202022-09-02 11:46:18 +01001826
Tom Cosgrove0cc78652022-08-23 16:26:52 +01001827exit:
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01001828 mbedtls_free( a );
1829 mbedtls_free( b );
Tom Cosgrove50c477b2022-09-15 14:28:30 +01001830 mbedtls_free( sum );
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01001831 mbedtls_free( d );
Tom Cosgrove0cc78652022-08-23 16:26:52 +01001832
Tom Cosgrove50c477b2022-09-15 14:28:30 +01001833 mbedtls_mpi_free( &S4 );
1834 mbedtls_mpi_free( &S8 );
Tom Cosgroveeceb4cc2022-09-02 10:46:09 +01001835 mbedtls_mpi_free( &A );
1836 mbedtls_mpi_free( &B );
Tom Cosgrove0cc78652022-08-23 16:26:52 +01001837}
1838/* END_CASE */
1839
Tom Cosgrove2a65b852022-08-17 05:43:54 +01001840/* BEGIN_CASE */
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01001841void mpi_core_sub( char * input_A, char * input_B,
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001842 char * input_X4, char * input_X8,
1843 int carry )
Tom Cosgrove2a65b852022-08-17 05:43:54 +01001844{
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01001845 mbedtls_mpi A, B, X4, X8;
1846 mbedtls_mpi_uint *a = NULL;
1847 mbedtls_mpi_uint *b = NULL;
1848 mbedtls_mpi_uint *x = NULL; /* expected */
1849 mbedtls_mpi_uint *r = NULL; /* result */
Tom Cosgrove2a65b852022-08-17 05:43:54 +01001850
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01001851 mbedtls_mpi_init( &A );
1852 mbedtls_mpi_init( &B );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01001853 mbedtls_mpi_init( &X4 );
1854 mbedtls_mpi_init( &X8 );
1855
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01001856 TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
1857 TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) );
Tom Cosgrove9339f052022-09-01 13:02:53 +01001858 TEST_EQUAL( 0, mbedtls_test_read_mpi( &X4, input_X4 ) );
1859 TEST_EQUAL( 0, mbedtls_test_read_mpi( &X8, input_X8 ) );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01001860
1861 /* All of the inputs are +ve (or zero) */
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01001862 TEST_EQUAL( 1, A.s );
1863 TEST_EQUAL( 1, B.s );
Tom Cosgrove9339f052022-09-01 13:02:53 +01001864 TEST_EQUAL( 1, X4.s );
1865 TEST_EQUAL( 1, X8.s );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01001866
1867 /* Get the number of limbs we will need */
Tom Cosgrovee2159f22022-09-15 14:40:10 +01001868 size_t limbs = MAX( A.n, B.n );
Tom Cosgroveb0fb17a2022-09-01 15:04:43 +01001869 size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
Tom Cosgrove2a65b852022-08-17 05:43:54 +01001870
1871 /* We only need to work with X4 or X8, depending on sizeof(mbedtls_mpi_uint) */
1872 mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &X4 : &X8;
1873
1874 /* The result shouldn't have more limbs than the longest input */
Tom Cosgrove1feb5ac2022-09-15 14:22:35 +01001875 TEST_LE_U( X->n, limbs );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01001876
1877 /* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */
Tom Cosgrove2a65b852022-08-17 05:43:54 +01001878
Tom Cosgrove2b177922022-09-15 14:07:18 +01001879 /* ASSERT_ALLOC() uses calloc() under the hood, so these do get zeroed */
1880 ASSERT_ALLOC( a, bytes );
1881 ASSERT_ALLOC( b, bytes );
1882 ASSERT_ALLOC( x, bytes );
1883 ASSERT_ALLOC( r, bytes );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01001884
1885 /* Populate the arrays. As the mbedtls_mpi_uint[]s in mbedtls_mpis (and as
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01001886 * processed by mbedtls_mpi_core_sub()) are little endian, we can just
Tom Cosgrove2b177922022-09-15 14:07:18 +01001887 * copy what we have as long as MSBs are 0 (which they are from ASSERT_ALLOC())
Tom Cosgrove2a65b852022-08-17 05:43:54 +01001888 */
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01001889 memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) );
1890 memcpy( b, B.p, B.n * sizeof(mbedtls_mpi_uint) );
1891 memcpy( x, X->p, X->n * sizeof(mbedtls_mpi_uint) );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01001892
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01001893 /* 1a) r = a - b => we should get the correct carry */
Tom Cosgrovebe7209d2022-09-15 14:32:38 +01001894 TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, a, b, limbs ) );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01001895
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01001896 /* 1b) r = a - b => we should get the correct result */
1897 ASSERT_COMPARE( r, bytes, x, bytes );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01001898
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01001899 /* 2 and 3 test "r may be aliased to a or b" */
1900 /* 2a) r = a; r -= b => we should get the correct carry (use r to avoid clobbering a) */
1901 memcpy( r, a, bytes );
Tom Cosgrovebe7209d2022-09-15 14:32:38 +01001902 TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, r, b, limbs ) );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01001903
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01001904 /* 2b) r -= b => we should get the correct result */
1905 ASSERT_COMPARE( r, bytes, x, bytes );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01001906
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01001907 /* 3a) r = b; r = a - r => we should get the correct carry (use r to avoid clobbering b) */
1908 memcpy( r, b, bytes );
Tom Cosgrovebe7209d2022-09-15 14:32:38 +01001909 TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, a, r, limbs ) );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01001910
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01001911 /* 3b) r = a - b => we should get the correct result */
1912 ASSERT_COMPARE( r, bytes, x, bytes );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01001913
1914exit:
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01001915 mbedtls_free( a );
1916 mbedtls_free( b );
1917 mbedtls_free( x );
1918 mbedtls_free( r );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01001919
Tom Cosgrovea043aeb2022-09-02 10:59:59 +01001920 mbedtls_mpi_free( &A );
1921 mbedtls_mpi_free( &B );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01001922 mbedtls_mpi_free( &X4 );
1923 mbedtls_mpi_free( &X8 );
Tom Cosgrove2a65b852022-08-17 05:43:54 +01001924}
1925/* END_CASE */
1926
Tom Cosgrove659c84a2022-08-17 05:45:19 +01001927/* BEGIN_CASE */
Tom Cosgrove42dfac62022-09-02 11:16:39 +01001928void mpi_core_mla( char * input_A, char * input_B, char * input_S,
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01001929 char * input_X4, char * input_cy4,
1930 char * input_X8, char * input_cy8 )
Tom Cosgrove659c84a2022-08-17 05:45:19 +01001931{
Tom Cosgrove42dfac62022-09-02 11:16:39 +01001932 /* We are testing A += B * s; A, B are MPIs, s is a scalar.
Tom Cosgrove659c84a2022-08-17 05:45:19 +01001933 *
Tom Cosgrove359feb02022-09-15 14:52:34 +01001934 * However, we encode s as an MPI in the .data file as the test framework
1935 * currently only supports `int`-typed scalars, and that doesn't cover the
1936 * full range of `mbedtls_mpi_uint`.
Tom Cosgrove659c84a2022-08-17 05:45:19 +01001937 *
1938 * We also have the different results for sizeof(mbedtls_mpi_uint) == 4 or 8.
1939 */
Tom Cosgrove42dfac62022-09-02 11:16:39 +01001940 mbedtls_mpi A, B, S, X4, X8, cy4, cy8;
1941 mbedtls_mpi_uint *a = NULL;
1942 mbedtls_mpi_uint *x = NULL;
Tom Cosgrove659c84a2022-08-17 05:45:19 +01001943
Tom Cosgrove42dfac62022-09-02 11:16:39 +01001944 mbedtls_mpi_init( &A );
1945 mbedtls_mpi_init( &B );
1946 mbedtls_mpi_init( &S );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01001947 mbedtls_mpi_init( &X4 );
1948 mbedtls_mpi_init( &X8 );
1949 mbedtls_mpi_init( &cy4 );
1950 mbedtls_mpi_init( &cy8 );
1951
Tom Cosgrove42dfac62022-09-02 11:16:39 +01001952 TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
1953 TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) );
1954 TEST_EQUAL( 0, mbedtls_test_read_mpi( &S, input_S ) );
Tom Cosgrove9339f052022-09-01 13:02:53 +01001955 TEST_EQUAL( 0, mbedtls_test_read_mpi( &X4, input_X4 ) );
1956 TEST_EQUAL( 0, mbedtls_test_read_mpi( &cy4, input_cy4 ) );
1957 TEST_EQUAL( 0, mbedtls_test_read_mpi( &X8, input_X8 ) );
1958 TEST_EQUAL( 0, mbedtls_test_read_mpi( &cy8, input_cy8 ) );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01001959
Tom Cosgrove42dfac62022-09-02 11:16:39 +01001960 /* The MPI encoding of scalar s must be only 1 limb */
1961 TEST_EQUAL( 1, S.n );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01001962
1963 /* We only need to work with X4 or X8, and cy4 or cy8, depending on sizeof(mbedtls_mpi_uint) */
1964 mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &X4 : &X8;
1965 mbedtls_mpi *cy = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &cy4 : &cy8;
1966
1967 /* The carry should only have one limb */
Tom Cosgrove9339f052022-09-01 13:02:53 +01001968 TEST_EQUAL( 1, cy->n );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01001969
1970 /* All of the inputs are +ve (or zero) */
Tom Cosgrove42dfac62022-09-02 11:16:39 +01001971 TEST_EQUAL( 1, A.s );
1972 TEST_EQUAL( 1, B.s );
1973 TEST_EQUAL( 1, S.s );
Tom Cosgrove9339f052022-09-01 13:02:53 +01001974 TEST_EQUAL( 1, X->s );
1975 TEST_EQUAL( 1, cy->s );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01001976
1977 /* Get the (max) number of limbs we will need */
Tom Cosgrovee2159f22022-09-15 14:40:10 +01001978 size_t limbs = MAX( A.n, B.n );
Tom Cosgroveb0fb17a2022-09-01 15:04:43 +01001979 size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
Tom Cosgrove659c84a2022-08-17 05:45:19 +01001980
1981 /* The result shouldn't have more limbs than the longest input */
Tom Cosgrove1feb5ac2022-09-15 14:22:35 +01001982 TEST_LE_U( X->n, limbs );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01001983
1984 /* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */
Tom Cosgrove659c84a2022-08-17 05:45:19 +01001985
Tom Cosgrove2b177922022-09-15 14:07:18 +01001986 /* ASSERT_ALLOC() uses calloc() under the hood, so these do get zeroed */
1987 ASSERT_ALLOC( a, bytes );
1988 ASSERT_ALLOC( x, bytes );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01001989
1990 /* Populate the arrays. As the mbedtls_mpi_uint[]s in mbedtls_mpis (and as
Tom Cosgrove42dfac62022-09-02 11:16:39 +01001991 * processed by mbedtls_mpi_core_mla()) are little endian, we can just
Tom Cosgrove2b177922022-09-15 14:07:18 +01001992 * copy what we have as long as MSBs are 0 (which they are from ASSERT_ALLOC()).
Tom Cosgrove659c84a2022-08-17 05:45:19 +01001993 */
Tom Cosgrove42dfac62022-09-02 11:16:39 +01001994 memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) );
1995 memcpy( x, X->p, X->n * sizeof(mbedtls_mpi_uint) );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01001996
Tom Cosgrove42dfac62022-09-02 11:16:39 +01001997 /* 1a) A += B * s => we should get the correct carry */
1998 TEST_EQUAL( mbedtls_mpi_core_mla( a, limbs, B.p, B.n, *S.p ), *cy->p );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01001999
Tom Cosgrove42dfac62022-09-02 11:16:39 +01002000 /* 1b) A += B * s => we should get the correct result */
2001 ASSERT_COMPARE( a, bytes, x, bytes );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002002
2003exit:
Tom Cosgrove42dfac62022-09-02 11:16:39 +01002004 mbedtls_free( a );
2005 mbedtls_free( x );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002006
Tom Cosgrove42dfac62022-09-02 11:16:39 +01002007 mbedtls_mpi_free( &A );
2008 mbedtls_mpi_free( &B );
2009 mbedtls_mpi_free( &S );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002010 mbedtls_mpi_free( &X4 );
2011 mbedtls_mpi_free( &X8 );
Tom Cosgrove42dfac62022-09-02 11:16:39 +01002012 mbedtls_mpi_free( &cy4 );
2013 mbedtls_mpi_free( &cy8 );
Tom Cosgrove659c84a2022-08-17 05:45:19 +01002014}
2015/* END_CASE */
2016
Tom Cosgrove79b70f62022-08-17 06:17:00 +01002017/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01002018void mpi_montg_init( char * input_N, char * input_mm )
Tom Cosgrove79b70f62022-08-17 06:17:00 +01002019{
2020 mbedtls_mpi N, mm;
2021
2022 mbedtls_mpi_init( &N );
2023 mbedtls_mpi_init( &mm );
2024
Tom Cosgrove9339f052022-09-01 13:02:53 +01002025 TEST_EQUAL( 0, mbedtls_test_read_mpi( &N, input_N ) );
2026 TEST_EQUAL( 0, mbedtls_test_read_mpi( &mm, input_mm ) );
Tom Cosgrove79b70f62022-08-17 06:17:00 +01002027
2028 /* The MPI encoding of mm should be 1 limb (sizeof(mbedtls_mpi_uint) == 8) or
2029 * 2 limbs (sizeof(mbedtls_mpi_uint) == 4).
2030 *
2031 * The data file contains the expected result for sizeof(mbedtls_mpi_uint) == 8;
2032 * for sizeof(mbedtls_mpi_uint) == 4 it's just the LSW of this.
2033 */
Tom Cosgroveb2c06f42022-08-24 17:45:58 +01002034 TEST_ASSERT( mm.n == 1 || mm.n == 2 );
Tom Cosgrove79b70f62022-08-17 06:17:00 +01002035
2036 /* All of the inputs are +ve (or zero) */
Tom Cosgrove9339f052022-09-01 13:02:53 +01002037 TEST_EQUAL( 1, N.s );
2038 TEST_EQUAL( 1, mm.s );
Tom Cosgrove79b70f62022-08-17 06:17:00 +01002039
Tom Cosgroveb7438d12022-09-15 15:05:59 +01002040 /* mbedtls_mpi_core_montmul_init() only returns a result, no error possible */
2041 mbedtls_mpi_uint result = mbedtls_mpi_core_montmul_init( N.p );
Tom Cosgrove79b70f62022-08-17 06:17:00 +01002042
2043 /* Check we got the correct result */
2044 TEST_EQUAL( result, mm.p[0] );
2045
2046exit:
2047 mbedtls_mpi_free( &N );
2048 mbedtls_mpi_free( &mm );
2049}
2050/* END_CASE */
2051
Tom Cosgrovef334d962022-08-17 06:29:32 +01002052/* BEGIN_CASE */
Tom Cosgrove1b2947a2022-09-02 10:24:55 +01002053void mpi_core_montmul( int limbs_AN4, int limbs_B4,
2054 int limbs_AN8, int limbs_B8,
2055 char * input_A,
2056 char * input_B,
2057 char * input_N,
2058 char * input_X4,
2059 char * input_X8 )
Tom Cosgrovef334d962022-08-17 06:29:32 +01002060{
Tom Cosgrove93842842022-08-05 16:59:43 +01002061 mbedtls_mpi A, B, N, X4, X8, T, R;
Tom Cosgrovef334d962022-08-17 06:29:32 +01002062
2063 mbedtls_mpi_init( &A );
2064 mbedtls_mpi_init( &B );
2065 mbedtls_mpi_init( &N );
2066 mbedtls_mpi_init( &X4 ); /* expected result, sizeof(mbedtls_mpi_uint) == 4 */
2067 mbedtls_mpi_init( &X8 ); /* expected result, sizeof(mbedtls_mpi_uint) == 8 */
2068 mbedtls_mpi_init( &T );
Tom Cosgrove93842842022-08-05 16:59:43 +01002069 mbedtls_mpi_init( &R ); /* for the result */
Tom Cosgrovef334d962022-08-17 06:29:32 +01002070
Tom Cosgrove9339f052022-09-01 13:02:53 +01002071 TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
2072 TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) );
2073 TEST_EQUAL( 0, mbedtls_test_read_mpi( &N, input_N ) );
2074 TEST_EQUAL( 0, mbedtls_test_read_mpi( &X4, input_X4 ) );
2075 TEST_EQUAL( 0, mbedtls_test_read_mpi( &X8, input_X8 ) );
Tom Cosgrovef334d962022-08-17 06:29:32 +01002076
2077 mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &X4 : &X8;
2078
2079 int limbs_AN = ( sizeof(mbedtls_mpi_uint) == 4 ) ? limbs_AN4 : limbs_AN8;
2080 int limbs_B = ( sizeof(mbedtls_mpi_uint) == 4 ) ? limbs_B4 : limbs_B8;
2081
Tom Cosgrove1feb5ac2022-09-15 14:22:35 +01002082 TEST_LE_U( A.n, (size_t)limbs_AN );
2083 TEST_LE_U( X->n, (size_t)limbs_AN );
2084 TEST_LE_U( B.n, (size_t)limbs_B );
2085 TEST_LE_U( limbs_B, limbs_AN );
Tom Cosgrovef334d962022-08-17 06:29:32 +01002086
2087 /* All of the inputs are +ve (or zero) */
Tom Cosgrove9339f052022-09-01 13:02:53 +01002088 TEST_EQUAL( 1, A.s );
2089 TEST_EQUAL( 1, B.s );
2090 TEST_EQUAL( 1, N.s );
2091 TEST_EQUAL( 1, X->s );
Tom Cosgrovef334d962022-08-17 06:29:32 +01002092
Tom Cosgrove9339f052022-09-01 13:02:53 +01002093 TEST_EQUAL( 0, mbedtls_mpi_grow( &A, limbs_AN ) );
2094 TEST_EQUAL( 0, mbedtls_mpi_grow( &N, limbs_AN ) );
2095 TEST_EQUAL( 0, mbedtls_mpi_grow( X, limbs_AN ) );
2096 TEST_EQUAL( 0, mbedtls_mpi_grow( &B, limbs_B ) );
Tom Cosgrovef334d962022-08-17 06:29:32 +01002097
Tom Cosgrove9339f052022-09-01 13:02:53 +01002098 TEST_EQUAL( 0, mbedtls_mpi_grow( &T, limbs_AN * 2 + 1 ) );
Tom Cosgrovef334d962022-08-17 06:29:32 +01002099
2100 /* Calculate the Montgomery constant (this is unit tested separately) */
Tom Cosgroveb7438d12022-09-15 15:05:59 +01002101 mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init( N.p );
Tom Cosgrovef334d962022-08-17 06:29:32 +01002102
Tom Cosgrove9339f052022-09-01 13:02:53 +01002103 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 +01002104
Tom Cosgrove93842842022-08-05 16:59:43 +01002105 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 +01002106 size_t bytes = N.n * sizeof(mbedtls_mpi_uint);
2107 ASSERT_COMPARE( R.p, bytes, X->p, bytes );
Tom Cosgrovef334d962022-08-17 06:29:32 +01002108
2109exit:
2110 mbedtls_mpi_free( &A );
2111 mbedtls_mpi_free( &B );
2112 mbedtls_mpi_free( &N );
2113 mbedtls_mpi_free( &X4 );
2114 mbedtls_mpi_free( &X8 );
2115 mbedtls_mpi_free( &T );
Tom Cosgrove93842842022-08-05 16:59:43 +01002116 mbedtls_mpi_free( &R );
Tom Cosgrovef334d962022-08-17 06:29:32 +01002117}
2118/* END_CASE */
2119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01002121void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00002122{
Andres AG93012e82016-09-09 09:10:28 +01002123 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00002124}
Paul Bakker33b43f12013-08-20 11:48:36 +02002125/* END_CASE */