blob: d7fe6b9bf0edf296ebe7ab526e3f695434653ed6 [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"
Janos Follath0b741612018-09-05 17:04:49 +01003
Chris Jonesd6e113f2020-12-03 14:24:33 +00004#if MBEDTLS_MPI_MAX_BITS > 256
5#define MPI_MAX_BITS_LARGER_THAN_256
6#endif
7
Janos Follath0b741612018-09-05 17:04:49 +01008typedef struct mbedtls_test_mpi_random
9{
10 uint8_t *data;
11 uint32_t data_len;
12 size_t pos;
13 size_t chunk_len;
14} mbedtls_test_mpi_random;
15
16/*
17 * This function is called by the Miller-Rabin primality test each time it
18 * chooses a random witness. The witnesses (or non-witnesses as provided by the
19 * test) are stored in the data member of the state structure. Each number is in
20 * the format that mbedtls_mpi_read_string understands and is chunk_len long.
21 */
22int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
23 unsigned char* buf,
24 size_t len )
25{
26 mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
27
28 if( random == NULL || random->data == NULL || buf == NULL )
29 return( -1 );
30
31 if( random->pos + random->chunk_len > random->data_len
32 || random->chunk_len > len )
33 {
34 return( -1 );
35 }
36
37 memset( buf, 0, len );
38
39 /* The witness is written to the end of the buffer, since the buffer is
40 * used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
41 * Writing the witness to the start of the buffer would result in the
42 * buffer being 'witness 000...000', which would be treated as
43 * witness * 2^n for some n. */
44 memcpy( buf + len - random->chunk_len, &random->data[random->pos],
45 random->chunk_len );
46
47 random->pos += random->chunk_len;
48
49 return( 0 );
50}
Paul Bakker33b43f12013-08-20 11:48:36 +020051/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +000052
Paul Bakker33b43f12013-08-20 11:48:36 +020053/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +020055 * END_DEPENDENCIES
56 */
Paul Bakker5690efc2011-05-26 13:16:06 +000057
Paul Bakker33b43f12013-08-20 11:48:36 +020058/* BEGIN_CASE */
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020059void mpi_null( )
60{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020061 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020062
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020063 mbedtls_mpi_init( &X );
64 mbedtls_mpi_init( &Y );
65 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020066
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020067 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
68 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +020069 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020070 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020071
72exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020073 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020074}
75/* END_CASE */
76
77/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +020078void mpi_read_write_string( int radix_X, char *input_X, int radix_A,
79 char *input_A, int output_size, int result_read,
80 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +000081{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +000083 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +010084 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +000085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +000087
Janos Follathf56da142019-03-06 12:29:37 +000088 memset( str, '!', sizeof( str ) );
89
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +020091 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +000092 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +010093 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +020094 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +000095 {
Paul Bakker33b43f12013-08-20 11:48:36 +020096 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Janos Follathf56da142019-03-06 12:29:37 +000097 TEST_ASSERT( str[len] == '!' );
Paul Bakkerba48cb22009-07-12 11:01:32 +000098 }
99 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000100
Paul Bakkerbd51b262014-07-10 15:26:12 +0200101exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000103}
Paul Bakker33b43f12013-08-20 11:48:36 +0200104/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000105
Paul Bakker33b43f12013-08-20 11:48:36 +0200106/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107void mbedtls_mpi_read_binary( char *input_X, int radix_A, char *input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000108{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000110 unsigned char str[1000];
111 unsigned char buf[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100112 size_t len;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000113 size_t input_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000116
Paul Bakker33b43f12013-08-20 11:48:36 +0200117 input_len = unhexify( buf, input_X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf, input_len ) == 0 );
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100120 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, (char *) str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200121 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000122
Paul Bakkerbd51b262014-07-10 15:26:12 +0200123exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000125}
Paul Bakker33b43f12013-08-20 11:48:36 +0200126/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000127
Paul Bakker33b43f12013-08-20 11:48:36 +0200128/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129void mbedtls_mpi_write_binary( int radix_X, char *input_X, char *input_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200130 int output_size, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000131{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000133 unsigned char str[1000];
134 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000135 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000136
137 memset( buf, 0x00, 1000 );
138 memset( str, 0x00, 1000 );
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200145 if( buflen > (size_t) output_size )
146 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200149 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000150 {
151 hexify( str, buf, buflen );
Paul Bakkere896fea2009-07-06 06:40:23 +0000152
Paul Bakker33b43f12013-08-20 11:48:36 +0200153 TEST_ASSERT( strcasecmp( (char *) str, input_A ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000154 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000155
Paul Bakkerbd51b262014-07-10 15:26:12 +0200156exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000158}
Paul Bakker33b43f12013-08-20 11:48:36 +0200159/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
162void mbedtls_mpi_read_file( int radix_X, char *input_file, char *input_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200163 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000164{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000166 unsigned char str[1000];
167 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000168 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000169 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000170 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000171
172 memset( buf, 0x00, 1000 );
173 memset( str, 0x00, 1000 );
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000176
Paul Bakker33b43f12013-08-20 11:48:36 +0200177 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200178 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000180 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000181 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000182
Paul Bakker33b43f12013-08-20 11:48:36 +0200183 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 buflen = mbedtls_mpi_size( &X );
186 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000187
Paul Bakkerba48cb22009-07-12 11:01:32 +0000188 hexify( str, buf, buflen );
Paul Bakkere896fea2009-07-06 06:40:23 +0000189
Paul Bakker33b43f12013-08-20 11:48:36 +0200190 TEST_ASSERT( strcasecmp( (char *) str, input_A ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000191 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000192
Paul Bakkerbd51b262014-07-10 15:26:12 +0200193exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000195}
Paul Bakker33b43f12013-08-20 11:48:36 +0200196/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
199void mbedtls_mpi_write_file( int radix_X, char *input_X, int output_radix,
Paul Bakker33b43f12013-08-20 11:48:36 +0200200 char *output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000201{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000203 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200204 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000209
Paul Bakker33b43f12013-08-20 11:48:36 +0200210 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000211 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200212 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000213 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200214 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000215
Paul Bakker33b43f12013-08-20 11:48:36 +0200216 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000217 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200218 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000219 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200220 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000223
Paul Bakkerbd51b262014-07-10 15:26:12 +0200224exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000226}
Paul Bakker33b43f12013-08-20 11:48:36 +0200227/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000228
Paul Bakker33b43f12013-08-20 11:48:36 +0200229/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230void mbedtls_mpi_get_bit( int radix_X, char *input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000231{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_mpi X;
233 mbedtls_mpi_init( &X );
234 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
235 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000236
Paul Bakkerbd51b262014-07-10 15:26:12 +0200237exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000239}
Paul Bakker33b43f12013-08-20 11:48:36 +0200240/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000241
Paul Bakker33b43f12013-08-20 11:48:36 +0200242/* BEGIN_CASE */
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100243void mbedtls_mpi_set_bit( int radix_X, char *input_X, int pos, int val,
244 int radix_Y, char *output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000245{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_mpi X, Y;
247 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
250 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100251 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
252
253 if( result == 0 )
254 {
255 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
256 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000257
Paul Bakkerbd51b262014-07-10 15:26:12 +0200258exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000260}
Paul Bakker33b43f12013-08-20 11:48:36 +0200261/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000262
Paul Bakker33b43f12013-08-20 11:48:36 +0200263/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264void mbedtls_mpi_lsb( int radix_X, char *input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000265{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 mbedtls_mpi X;
267 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
270 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000271
Paul Bakkerbd51b262014-07-10 15:26:12 +0200272exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000274}
Paul Bakker33b43f12013-08-20 11:48:36 +0200275/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000276
Paul Bakker33b43f12013-08-20 11:48:36 +0200277/* BEGIN_CASE */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200278void mbedtls_mpi_bitlen( int radix_X, char *input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000279{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_mpi X;
281 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200284 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000285
Paul Bakkerbd51b262014-07-10 15:26:12 +0200286exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000288}
Paul Bakker33b43f12013-08-20 11:48:36 +0200289/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000290
Paul Bakker33b43f12013-08-20 11:48:36 +0200291/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292void mbedtls_mpi_gcd( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200293 int radix_A, char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000294{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 mbedtls_mpi A, X, Y, Z;
296 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
299 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
300 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
301 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
302 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000303
Paul Bakkerbd51b262014-07-10 15:26:12 +0200304exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000306}
Paul Bakker33b43f12013-08-20 11:48:36 +0200307/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000308
Paul Bakker33b43f12013-08-20 11:48:36 +0200309/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000311{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 mbedtls_mpi X;
313 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
316 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000317
Paul Bakkerbd51b262014-07-10 15:26:12 +0200318exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000320}
Paul Bakker33b43f12013-08-20 11:48:36 +0200321/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000322
Paul Bakker33b43f12013-08-20 11:48:36 +0200323/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324void mbedtls_mpi_cmp_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200325 int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000326{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_mpi X, Y;
328 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
331 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
332 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000333
Paul Bakkerbd51b262014-07-10 15:26:12 +0200334exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000336}
Paul Bakker33b43f12013-08-20 11:48:36 +0200337/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000338
Paul Bakker33b43f12013-08-20 11:48:36 +0200339/* BEGIN_CASE */
Janos Follathaaa3f222019-10-14 09:21:49 +0100340void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
341 int size_Y, char * input_Y,
Janos Follathc3b376e2019-10-11 14:21:53 +0100342 int input_ret, int input_err )
Janos Follath883801d2019-09-11 16:07:14 +0100343{
Janos Follathc3b376e2019-10-11 14:21:53 +0100344 unsigned ret;
345 unsigned input_uret = input_ret;
Janos Follath883801d2019-09-11 16:07:14 +0100346 mbedtls_mpi X, Y;
347 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
348
Janos Follathaaa3f222019-10-14 09:21:49 +0100349 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
350 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
Janos Follath883801d2019-09-11 16:07:14 +0100351
Gilles Peskine16ba09c2020-01-21 16:30:53 +0100352 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
353 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follath883801d2019-09-11 16:07:14 +0100354
Janos Follathc3b376e2019-10-11 14:21:53 +0100355 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follath883801d2019-09-11 16:07:14 +0100356 if( input_err == 0 )
Janos Follathc3b376e2019-10-11 14:21:53 +0100357 TEST_ASSERT( ret == input_uret );
Janos Follath883801d2019-09-11 16:07:14 +0100358
359exit:
360 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
361}
362/* END_CASE */
363
364/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365void mbedtls_mpi_cmp_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200366 int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000367{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 mbedtls_mpi X, Y;
369 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
372 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
373 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000374
Paul Bakkerbd51b262014-07-10 15:26:12 +0200375exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000377}
Paul Bakker33b43f12013-08-20 11:48:36 +0200378/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000379
Paul Bakker33b43f12013-08-20 11:48:36 +0200380/* BEGIN_CASE */
Gilles Peskine84b8e252020-01-20 21:01:51 +0100381void mbedtls_mpi_copy_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000382{
Gilles Peskine84b8e252020-01-20 21:01:51 +0100383 mbedtls_mpi X, Y;
384 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
Gilles Peskine84b8e252020-01-20 21:01:51 +0100387 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
Gilles Peskine84b8e252020-01-20 21:01:51 +0100390 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
391 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000392
Paul Bakkerbd51b262014-07-10 15:26:12 +0200393exit:
Gilles Peskine84b8e252020-01-20 21:01:51 +0100394 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
395}
396/* END_CASE */
397
398/* BEGIN_CASE */
399void mbedtls_mpi_copy_binary( char *input_X, char *input_Y )
400{
401 mbedtls_mpi X, Y, X0;
402 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &X0 );
403
404 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
405 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
406 TEST_ASSERT( mbedtls_mpi_read_string( &X0, 16, input_X ) == 0 );
407 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
408
409 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
410 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
411 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
412
413exit:
414 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000415}
Paul Bakker33b43f12013-08-20 11:48:36 +0200416/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000417
Paul Bakker33b43f12013-08-20 11:48:36 +0200418/* BEGIN_CASE */
419void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000420{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_mpi X;
422 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
425 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
426 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000427
Paul Bakkerbd51b262014-07-10 15:26:12 +0200428exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000430}
Paul Bakker33b43f12013-08-20 11:48:36 +0200431/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000432
Paul Bakker33b43f12013-08-20 11:48:36 +0200433/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100435{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_mpi X;
437 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100440 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
442 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100443 TEST_ASSERT( X.n == (size_t) after );
444
Paul Bakkerbd51b262014-07-10 15:26:12 +0200445exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100447}
448/* END_CASE */
449
450/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451void mbedtls_mpi_safe_cond_assign( int x_sign, char *x_str,
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100452 int y_sign, char *y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100453{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_mpi X, Y, XX;
455 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100458 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100460 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
464 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
467 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100468
Paul Bakkerbd51b262014-07-10 15:26:12 +0200469exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100471}
472/* END_CASE */
473
474/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475void mbedtls_mpi_safe_cond_swap( int x_sign, char *x_str,
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100476 int y_sign, char *y_str )
477{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
481 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100484 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100486 Y.s = y_sign;
487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
489 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
492 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
493 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
496 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
497 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100498
Paul Bakkerbd51b262014-07-10 15:26:12 +0200499exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
501 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100502}
503/* END_CASE */
504
505/* BEGIN_CASE */
Gilles Peskine84b8e252020-01-20 21:01:51 +0100506void mbedtls_mpi_swap_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000507{
Gilles Peskine84b8e252020-01-20 21:01:51 +0100508 mbedtls_mpi X, Y;
509 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
512 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
Gilles Peskine84b8e252020-01-20 21:01:51 +0100513 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
514 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_Y ) == 0 );
515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 mbedtls_mpi_swap( &X, &Y );
Gilles Peskine84b8e252020-01-20 21:01:51 +0100517 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_Y ) == 0 );
518 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000519
Paul Bakkerbd51b262014-07-10 15:26:12 +0200520exit:
Gilles Peskine84b8e252020-01-20 21:01:51 +0100521 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
522}
523/* END_CASE */
524
525/* BEGIN_CASE */
526void mbedtls_mpi_swap_binary( char *input_X, char *input_Y )
527{
528 mbedtls_mpi X, Y, X0, Y0;
529 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
530 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
531
532 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
533 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
534 TEST_ASSERT( mbedtls_mpi_read_string( &X0, 16, input_X ) == 0 );
535 TEST_ASSERT( mbedtls_mpi_read_string( &Y0, 16, input_Y ) == 0 );
536
537 mbedtls_mpi_swap( &X, &Y );
538 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
539 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
540
541exit:
542 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
543 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
544}
545/* END_CASE */
546
547/* BEGIN_CASE */
548void mpi_swap_self( char *input_X )
549{
550 mbedtls_mpi X, X0;
551 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
552
553 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
554 TEST_ASSERT( mbedtls_mpi_read_string( &X0, 16, input_X ) == 0 );
555
556 mbedtls_mpi_swap( &X, &X );
557 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
558
559exit:
560 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000561}
Paul Bakker33b43f12013-08-20 11:48:36 +0200562/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000563
Paul Bakker33b43f12013-08-20 11:48:36 +0200564/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565void mbedtls_mpi_add_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200566 int radix_A, char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000567{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_mpi X, Y, Z, A;
569 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
572 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
573 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
574 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
575 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000576
Paul Bakkerbd51b262014-07-10 15:26:12 +0200577exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000579}
Paul Bakker33b43f12013-08-20 11:48:36 +0200580/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000581
Paul Bakker33b43f12013-08-20 11:48:36 +0200582/* BEGIN_CASE */
Janos Follath044a86b2015-10-25 10:58:03 +0100583void mbedtls_mpi_add_mpi_inplace( int radix_X, char *input_X, int radix_A, char *input_A )
584{
585 mbedtls_mpi X, A;
586 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
587
Janos Follath044a86b2015-10-25 10:58:03 +0100588 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100589
590 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
591 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
592 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
593
594 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
595 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
596 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
597
598 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100599 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
600 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
601
602exit:
603 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
604}
605/* END_CASE */
606
607
608/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609void mbedtls_mpi_add_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200610 int radix_A, char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000611{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 mbedtls_mpi X, Y, Z, A;
613 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
616 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
617 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
618 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
619 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000620
Paul Bakkerbd51b262014-07-10 15:26:12 +0200621exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000623}
Paul Bakker33b43f12013-08-20 11:48:36 +0200624/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000625
Paul Bakker33b43f12013-08-20 11:48:36 +0200626/* BEGIN_CASE */
627void mpi_add_abs_add_first( int radix_X, char *input_X, int radix_Y,
628 char *input_Y, int radix_A, char *input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000629{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 mbedtls_mpi X, Y, A;
631 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
634 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
635 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
636 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
637 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000638
Paul Bakkerbd51b262014-07-10 15:26:12 +0200639exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000641}
Paul Bakker33b43f12013-08-20 11:48:36 +0200642/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000643
Paul Bakker33b43f12013-08-20 11:48:36 +0200644/* BEGIN_CASE */
645void mpi_add_abs_add_second( int radix_X, char *input_X, int radix_Y,
646 char *input_Y, int radix_A, char *input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000647{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_mpi X, Y, A;
649 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
652 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
653 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
654 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
655 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000656
Paul Bakkerbd51b262014-07-10 15:26:12 +0200657exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000659}
Paul Bakker33b43f12013-08-20 11:48:36 +0200660/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000661
Paul Bakker33b43f12013-08-20 11:48:36 +0200662/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663void mbedtls_mpi_add_int( int radix_X, char *input_X, int input_Y, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200664 char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000665{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 mbedtls_mpi X, Z, A;
667 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
670 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
671 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
672 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000673
Paul Bakkerbd51b262014-07-10 15:26:12 +0200674exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000676}
Paul Bakker33b43f12013-08-20 11:48:36 +0200677/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000678
Paul Bakker33b43f12013-08-20 11:48:36 +0200679/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680void mbedtls_mpi_sub_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200681 int radix_A, char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000682{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 mbedtls_mpi X, Y, Z, A;
684 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
687 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
688 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
689 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
690 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000691
Paul Bakkerbd51b262014-07-10 15:26:12 +0200692exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000694}
Paul Bakker33b43f12013-08-20 11:48:36 +0200695/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000696
Paul Bakker33b43f12013-08-20 11:48:36 +0200697/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698void mbedtls_mpi_sub_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200699 int radix_A, char *input_A, int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000700{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000702 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
706 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
707 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200710 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000711 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000713
Paul Bakkerbd51b262014-07-10 15:26:12 +0200714exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000716}
Paul Bakker33b43f12013-08-20 11:48:36 +0200717/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000718
Paul Bakker33b43f12013-08-20 11:48:36 +0200719/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720void mbedtls_mpi_sub_int( int radix_X, char *input_X, int input_Y, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200721 char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000722{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_mpi X, Z, A;
724 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
727 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
728 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
729 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000730
Paul Bakkerbd51b262014-07-10 15:26:12 +0200731exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000733}
Paul Bakker33b43f12013-08-20 11:48:36 +0200734/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000735
Paul Bakker33b43f12013-08-20 11:48:36 +0200736/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737void mbedtls_mpi_mul_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200738 int radix_A, char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000739{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 mbedtls_mpi X, Y, Z, A;
741 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
744 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
745 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
746 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
747 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000748
Paul Bakkerbd51b262014-07-10 15:26:12 +0200749exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000751}
Paul Bakker33b43f12013-08-20 11:48:36 +0200752/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000753
Paul Bakker33b43f12013-08-20 11:48:36 +0200754/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755void mbedtls_mpi_mul_int( int radix_X, char *input_X, int input_Y, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200756 char *input_A, char *result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +0000757{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 mbedtls_mpi X, Z, A;
759 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
762 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
763 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200764 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200766 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200768 else
769 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000770
Paul Bakkerbd51b262014-07-10 15:26:12 +0200771exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000773}
Paul Bakker33b43f12013-08-20 11:48:36 +0200774/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000775
Paul Bakker33b43f12013-08-20 11:48:36 +0200776/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777void mbedtls_mpi_div_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200778 int radix_A, char *input_A, int radix_B, char *input_B,
779 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000780{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000782 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
784 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
787 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
788 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
789 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
790 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200791 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000792 if( res == 0 )
793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
795 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000796 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000797
Paul Bakkerbd51b262014-07-10 15:26:12 +0200798exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
800 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000801}
Paul Bakker33b43f12013-08-20 11:48:36 +0200802/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000803
Paul Bakker33b43f12013-08-20 11:48:36 +0200804/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805void mbedtls_mpi_div_int( int radix_X, char *input_X, int input_Y, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200806 char *input_A, int radix_B, char *input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000807{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000809 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
811 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
814 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
815 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
816 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200817 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000818 if( res == 0 )
819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
821 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000822 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000823
Paul Bakkerbd51b262014-07-10 15:26:12 +0200824exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
826 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000827}
Paul Bakker33b43f12013-08-20 11:48:36 +0200828/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000829
Paul Bakker33b43f12013-08-20 11:48:36 +0200830/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831void mbedtls_mpi_mod_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200832 int radix_A, char *input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000833{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000835 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
839 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
840 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
841 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200842 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000843 if( res == 0 )
844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000846 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000847
Paul Bakkerbd51b262014-07-10 15:26:12 +0200848exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000850}
Paul Bakker33b43f12013-08-20 11:48:36 +0200851/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000852
Paul Bakker33b43f12013-08-20 11:48:36 +0200853/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854void mbedtls_mpi_mod_int( int radix_X, char *input_X, int input_Y, int input_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200855 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000856{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000858 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 mbedtls_mpi_uint r;
860 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
863 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200864 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000865 if( res == 0 )
866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +0000868 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000869
Paul Bakkerbd51b262014-07-10 15:26:12 +0200870exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000872}
Paul Bakker33b43f12013-08-20 11:48:36 +0200873/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000874
Chris Jones942774e2020-12-03 15:22:25 +0000875/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876void mbedtls_mpi_exp_mod( int radix_A, char *input_A, int radix_E, char *input_E,
Paul Bakker33b43f12013-08-20 11:48:36 +0200877 int radix_N, char *input_N, int radix_RR, char *input_RR,
878 int radix_X, char *input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000879{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +0000881 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
883 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
886 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
887 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
888 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000889
Paul Bakker33b43f12013-08-20 11:48:36 +0200890 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +0200894 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000895 if( res == 0 )
896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000898 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000899
Paul Bakkerbd51b262014-07-10 15:26:12 +0200900exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
902 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000903}
Paul Bakker33b43f12013-08-20 11:48:36 +0200904/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000905
Paul Bakker33b43f12013-08-20 11:48:36 +0200906/* BEGIN_CASE */
Chris Jones65ad4cf2020-12-02 10:41:50 +0000907void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
Chris Jones2f7d1472020-12-03 11:35:41 +0000908 int radix_RR, char * input_RR, int exp_result )
Chris Jones65ad4cf2020-12-02 10:41:50 +0000909{
910 mbedtls_mpi A, E, N, RR, Z;
911 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
912 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
913
Chris Jones2f7d1472020-12-03 11:35:41 +0000914 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jones65ad4cf2020-12-02 10:41:50 +0000915 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jones65ad4cf2020-12-02 10:41:50 +0000916 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jones65ad4cf2020-12-02 10:41:50 +0000917 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jones2f7d1472020-12-03 11:35:41 +0000918
919 /* Set E to 2^(E_bytes - 1) + 1 */
920 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
921 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jones65ad4cf2020-12-02 10:41:50 +0000922 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jones2f7d1472020-12-03 11:35:41 +0000923
924 /* Set N to 2^(N_bytes - 1) + 1 */
925 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
926 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jones65ad4cf2020-12-02 10:41:50 +0000927 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
928
929 if( strlen( input_RR ) )
930 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
931
Chris Jones2f7d1472020-12-03 11:35:41 +0000932 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jones65ad4cf2020-12-02 10:41:50 +0000933
934exit:
935 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
936 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
937}
938/* END_CASE */
939
940/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941void mbedtls_mpi_inv_mod( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200942 int radix_A, char *input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000943{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000945 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
949 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
950 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
951 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200952 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000953 if( res == 0 )
954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000956 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000957
Paul Bakkerbd51b262014-07-10 15:26:12 +0200958exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000960}
Paul Bakker33b43f12013-08-20 11:48:36 +0200961/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
964void mbedtls_mpi_is_prime( int radix_X, char *input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000965{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000967 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
971 res = mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +0200972 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000973
Paul Bakkerbd51b262014-07-10 15:26:12 +0200974exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000976}
Paul Bakker33b43f12013-08-20 11:48:36 +0200977/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath0b741612018-09-05 17:04:49 +0100980void mbedtls_mpi_is_prime_det( char *input_X, char *witnesses,
981 int chunk_len, int div_result )
982{
983 mbedtls_mpi X;
984 int res;
985 mbedtls_test_mpi_random rand;
Gilles Peskine0eaa6d52018-11-05 16:37:06 +0100986 uint8_t *witness_buf = NULL;
987 uint8_t *input_buf = NULL;
Janos Follath0b741612018-09-05 17:04:49 +0100988 size_t witness_len;
989 size_t input_len;
990
Gilles Peskine0eaa6d52018-11-05 16:37:06 +0100991 witness_buf = unhexify_alloc( witnesses, &witness_len );
992 input_buf = unhexify_alloc( input_X, &input_len );
Janos Follath0b741612018-09-05 17:04:49 +0100993
994 mbedtls_mpi_init( &X );
995 rand.data = witness_buf;
996 rand.data_len = witness_len;
997 rand.pos = 0;
998 rand.chunk_len = chunk_len;
999
1000 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_buf, input_len ) == 0 );
1001 res = mbedtls_mpi_is_prime( &X, mbedtls_test_mpi_miller_rabin_determinizer,
1002 &rand );
1003 TEST_ASSERT( res == div_result );
1004
1005exit:
1006 mbedtls_mpi_free( &X );
Gilles Peskine0eaa6d52018-11-05 16:37:06 +01001007 mbedtls_free( witness_buf );
1008 mbedtls_free( input_buf );
Janos Follath0b741612018-09-05 17:04:49 +01001009}
1010/* END_CASE */
1011
1012/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013void mbedtls_mpi_gen_prime( int bits, int safe, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001014{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001016 int my_ret;
1017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 my_ret = mbedtls_mpi_gen_prime( &X, bits, safe, rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001021 TEST_ASSERT( my_ret == ref_ret );
1022
1023 if( ref_ret == 0 )
1024 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001025 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001026
1027 TEST_ASSERT( actual_bits >= (size_t) bits );
1028 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
1029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 TEST_ASSERT( mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001031 if( safe )
1032 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001033 /* X = ( X - 1 ) / 2 */
1034 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 TEST_ASSERT( mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001036 }
1037 }
1038
Paul Bakkerbd51b262014-07-10 15:26:12 +02001039exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001041}
1042/* END_CASE */
1043
Paul Bakker33b43f12013-08-20 11:48:36 +02001044/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045void mbedtls_mpi_shift_l( int radix_X, char *input_X, int shift_X, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +02001046 char *input_A)
Paul Bakker367dae42009-06-28 21:50:27 +00001047{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 mbedtls_mpi X, A;
1049 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1052 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1053 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
1054 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001055
Paul Bakkerbd51b262014-07-10 15:26:12 +02001056exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001058}
Paul Bakker33b43f12013-08-20 11:48:36 +02001059/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001060
Paul Bakker33b43f12013-08-20 11:48:36 +02001061/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062void mbedtls_mpi_shift_r( int radix_X, char *input_X, int shift_X, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +02001063 char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001064{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 mbedtls_mpi X, A;
1066 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1069 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1070 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
1071 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001072
Paul Bakkerbd51b262014-07-10 15:26:12 +02001073exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001075}
Paul Bakker33b43f12013-08-20 11:48:36 +02001076/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +02001079void mpi_selftest()
Paul Bakkere896fea2009-07-06 06:40:23 +00001080{
Andres AG93012e82016-09-09 09:10:28 +01001081 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001082}
Paul Bakker33b43f12013-08-20 11:48:36 +02001083/* END_CASE */