mbed TLS Build

Dependents:   Slave-prot-prod

Committer:
markrad
Date:
Thu Jan 05 00:18:44 2017 +0000
Revision:
0:cdf462088d13
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 0:cdf462088d13 1 /* BEGIN_HEADER */
markrad 0:cdf462088d13 2 #include "mbedtls/bignum.h"
markrad 0:cdf462088d13 3 /* END_HEADER */
markrad 0:cdf462088d13 4
markrad 0:cdf462088d13 5 /* BEGIN_DEPENDENCIES
markrad 0:cdf462088d13 6 * depends_on:MBEDTLS_BIGNUM_C
markrad 0:cdf462088d13 7 * END_DEPENDENCIES
markrad 0:cdf462088d13 8 */
markrad 0:cdf462088d13 9
markrad 0:cdf462088d13 10 /* BEGIN_CASE */
markrad 0:cdf462088d13 11 void mpi_null( )
markrad 0:cdf462088d13 12 {
markrad 0:cdf462088d13 13 mbedtls_mpi X, Y, Z;
markrad 0:cdf462088d13 14
markrad 0:cdf462088d13 15 mbedtls_mpi_init( &X );
markrad 0:cdf462088d13 16 mbedtls_mpi_init( &Y );
markrad 0:cdf462088d13 17 mbedtls_mpi_init( &Z );
markrad 0:cdf462088d13 18
markrad 0:cdf462088d13 19 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
markrad 0:cdf462088d13 20 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
markrad 0:cdf462088d13 21 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
markrad 0:cdf462088d13 22 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
markrad 0:cdf462088d13 23
markrad 0:cdf462088d13 24 exit:
markrad 0:cdf462088d13 25 mbedtls_mpi_free( &X );
markrad 0:cdf462088d13 26 }
markrad 0:cdf462088d13 27 /* END_CASE */
markrad 0:cdf462088d13 28
markrad 0:cdf462088d13 29 /* BEGIN_CASE */
markrad 0:cdf462088d13 30 void mpi_read_write_string( int radix_X, char *input_X, int radix_A,
markrad 0:cdf462088d13 31 char *input_A, int output_size, int result_read,
markrad 0:cdf462088d13 32 int result_write )
markrad 0:cdf462088d13 33 {
markrad 0:cdf462088d13 34 mbedtls_mpi X;
markrad 0:cdf462088d13 35 char str[1000];
markrad 0:cdf462088d13 36 size_t len;
markrad 0:cdf462088d13 37
markrad 0:cdf462088d13 38 mbedtls_mpi_init( &X );
markrad 0:cdf462088d13 39
markrad 0:cdf462088d13 40 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
markrad 0:cdf462088d13 41 if( result_read == 0 )
markrad 0:cdf462088d13 42 {
markrad 0:cdf462088d13 43 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
markrad 0:cdf462088d13 44 if( result_write == 0 )
markrad 0:cdf462088d13 45 {
markrad 0:cdf462088d13 46 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
markrad 0:cdf462088d13 47 }
markrad 0:cdf462088d13 48 }
markrad 0:cdf462088d13 49
markrad 0:cdf462088d13 50 exit:
markrad 0:cdf462088d13 51 mbedtls_mpi_free( &X );
markrad 0:cdf462088d13 52 }
markrad 0:cdf462088d13 53 /* END_CASE */
markrad 0:cdf462088d13 54
markrad 0:cdf462088d13 55 /* BEGIN_CASE */
markrad 0:cdf462088d13 56 void mbedtls_mpi_read_binary( char *input_X, int radix_A, char *input_A )
markrad 0:cdf462088d13 57 {
markrad 0:cdf462088d13 58 mbedtls_mpi X;
markrad 0:cdf462088d13 59 unsigned char str[1000];
markrad 0:cdf462088d13 60 unsigned char buf[1000];
markrad 0:cdf462088d13 61 size_t len;
markrad 0:cdf462088d13 62 size_t input_len;
markrad 0:cdf462088d13 63
markrad 0:cdf462088d13 64 mbedtls_mpi_init( &X );
markrad 0:cdf462088d13 65
markrad 0:cdf462088d13 66 input_len = unhexify( buf, input_X );
markrad 0:cdf462088d13 67
markrad 0:cdf462088d13 68 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf, input_len ) == 0 );
markrad 0:cdf462088d13 69 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, (char *) str, sizeof( str ), &len ) == 0 );
markrad 0:cdf462088d13 70 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
markrad 0:cdf462088d13 71
markrad 0:cdf462088d13 72 exit:
markrad 0:cdf462088d13 73 mbedtls_mpi_free( &X );
markrad 0:cdf462088d13 74 }
markrad 0:cdf462088d13 75 /* END_CASE */
markrad 0:cdf462088d13 76
markrad 0:cdf462088d13 77 /* BEGIN_CASE */
markrad 0:cdf462088d13 78 void mbedtls_mpi_write_binary( int radix_X, char *input_X, char *input_A,
markrad 0:cdf462088d13 79 int output_size, int result )
markrad 0:cdf462088d13 80 {
markrad 0:cdf462088d13 81 mbedtls_mpi X;
markrad 0:cdf462088d13 82 unsigned char str[1000];
markrad 0:cdf462088d13 83 unsigned char buf[1000];
markrad 0:cdf462088d13 84 size_t buflen;
markrad 0:cdf462088d13 85
markrad 0:cdf462088d13 86 memset( buf, 0x00, 1000 );
markrad 0:cdf462088d13 87 memset( str, 0x00, 1000 );
markrad 0:cdf462088d13 88
markrad 0:cdf462088d13 89 mbedtls_mpi_init( &X );
markrad 0:cdf462088d13 90
markrad 0:cdf462088d13 91 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 92
markrad 0:cdf462088d13 93 buflen = mbedtls_mpi_size( &X );
markrad 0:cdf462088d13 94 if( buflen > (size_t) output_size )
markrad 0:cdf462088d13 95 buflen = (size_t) output_size;
markrad 0:cdf462088d13 96
markrad 0:cdf462088d13 97 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
markrad 0:cdf462088d13 98 if( result == 0)
markrad 0:cdf462088d13 99 {
markrad 0:cdf462088d13 100 hexify( str, buf, buflen );
markrad 0:cdf462088d13 101
markrad 0:cdf462088d13 102 TEST_ASSERT( strcasecmp( (char *) str, input_A ) == 0 );
markrad 0:cdf462088d13 103 }
markrad 0:cdf462088d13 104
markrad 0:cdf462088d13 105 exit:
markrad 0:cdf462088d13 106 mbedtls_mpi_free( &X );
markrad 0:cdf462088d13 107 }
markrad 0:cdf462088d13 108 /* END_CASE */
markrad 0:cdf462088d13 109
markrad 0:cdf462088d13 110 /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
markrad 0:cdf462088d13 111 void mbedtls_mpi_read_file( int radix_X, char *input_file, char *input_A,
markrad 0:cdf462088d13 112 int result )
markrad 0:cdf462088d13 113 {
markrad 0:cdf462088d13 114 mbedtls_mpi X;
markrad 0:cdf462088d13 115 unsigned char str[1000];
markrad 0:cdf462088d13 116 unsigned char buf[1000];
markrad 0:cdf462088d13 117 size_t buflen;
markrad 0:cdf462088d13 118 FILE *file;
markrad 0:cdf462088d13 119 int ret;
markrad 0:cdf462088d13 120
markrad 0:cdf462088d13 121 memset( buf, 0x00, 1000 );
markrad 0:cdf462088d13 122 memset( str, 0x00, 1000 );
markrad 0:cdf462088d13 123
markrad 0:cdf462088d13 124 mbedtls_mpi_init( &X );
markrad 0:cdf462088d13 125
markrad 0:cdf462088d13 126 file = fopen( input_file, "r" );
markrad 0:cdf462088d13 127 TEST_ASSERT( file != NULL );
markrad 0:cdf462088d13 128 ret = mbedtls_mpi_read_file( &X, radix_X, file );
markrad 0:cdf462088d13 129 fclose(file);
markrad 0:cdf462088d13 130 TEST_ASSERT( ret == result );
markrad 0:cdf462088d13 131
markrad 0:cdf462088d13 132 if( result == 0 )
markrad 0:cdf462088d13 133 {
markrad 0:cdf462088d13 134 buflen = mbedtls_mpi_size( &X );
markrad 0:cdf462088d13 135 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
markrad 0:cdf462088d13 136
markrad 0:cdf462088d13 137 hexify( str, buf, buflen );
markrad 0:cdf462088d13 138
markrad 0:cdf462088d13 139 TEST_ASSERT( strcasecmp( (char *) str, input_A ) == 0 );
markrad 0:cdf462088d13 140 }
markrad 0:cdf462088d13 141
markrad 0:cdf462088d13 142 exit:
markrad 0:cdf462088d13 143 mbedtls_mpi_free( &X );
markrad 0:cdf462088d13 144 }
markrad 0:cdf462088d13 145 /* END_CASE */
markrad 0:cdf462088d13 146
markrad 0:cdf462088d13 147 /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
markrad 0:cdf462088d13 148 void mbedtls_mpi_write_file( int radix_X, char *input_X, int output_radix,
markrad 0:cdf462088d13 149 char *output_file )
markrad 0:cdf462088d13 150 {
markrad 0:cdf462088d13 151 mbedtls_mpi X, Y;
markrad 0:cdf462088d13 152 FILE *file_out, *file_in;
markrad 0:cdf462088d13 153 int ret;
markrad 0:cdf462088d13 154
markrad 0:cdf462088d13 155 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
markrad 0:cdf462088d13 156
markrad 0:cdf462088d13 157 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 158
markrad 0:cdf462088d13 159 file_out = fopen( output_file, "w" );
markrad 0:cdf462088d13 160 TEST_ASSERT( file_out != NULL );
markrad 0:cdf462088d13 161 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
markrad 0:cdf462088d13 162 fclose(file_out);
markrad 0:cdf462088d13 163 TEST_ASSERT( ret == 0 );
markrad 0:cdf462088d13 164
markrad 0:cdf462088d13 165 file_in = fopen( output_file, "r" );
markrad 0:cdf462088d13 166 TEST_ASSERT( file_in != NULL );
markrad 0:cdf462088d13 167 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
markrad 0:cdf462088d13 168 fclose(file_in);
markrad 0:cdf462088d13 169 TEST_ASSERT( ret == 0 );
markrad 0:cdf462088d13 170
markrad 0:cdf462088d13 171 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
markrad 0:cdf462088d13 172
markrad 0:cdf462088d13 173 exit:
markrad 0:cdf462088d13 174 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
markrad 0:cdf462088d13 175 }
markrad 0:cdf462088d13 176 /* END_CASE */
markrad 0:cdf462088d13 177
markrad 0:cdf462088d13 178 /* BEGIN_CASE */
markrad 0:cdf462088d13 179 void mbedtls_mpi_get_bit( int radix_X, char *input_X, int pos, int val )
markrad 0:cdf462088d13 180 {
markrad 0:cdf462088d13 181 mbedtls_mpi X;
markrad 0:cdf462088d13 182 mbedtls_mpi_init( &X );
markrad 0:cdf462088d13 183 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 184 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
markrad 0:cdf462088d13 185
markrad 0:cdf462088d13 186 exit:
markrad 0:cdf462088d13 187 mbedtls_mpi_free( &X );
markrad 0:cdf462088d13 188 }
markrad 0:cdf462088d13 189 /* END_CASE */
markrad 0:cdf462088d13 190
markrad 0:cdf462088d13 191 /* BEGIN_CASE */
markrad 0:cdf462088d13 192 void mbedtls_mpi_set_bit( int radix_X, char *input_X, int pos, int val,
markrad 0:cdf462088d13 193 int radix_Y, char *output_Y, int result )
markrad 0:cdf462088d13 194 {
markrad 0:cdf462088d13 195 mbedtls_mpi X, Y;
markrad 0:cdf462088d13 196 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
markrad 0:cdf462088d13 197
markrad 0:cdf462088d13 198 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 199 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
markrad 0:cdf462088d13 200 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
markrad 0:cdf462088d13 201
markrad 0:cdf462088d13 202 if( result == 0 )
markrad 0:cdf462088d13 203 {
markrad 0:cdf462088d13 204 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
markrad 0:cdf462088d13 205 }
markrad 0:cdf462088d13 206
markrad 0:cdf462088d13 207 exit:
markrad 0:cdf462088d13 208 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
markrad 0:cdf462088d13 209 }
markrad 0:cdf462088d13 210 /* END_CASE */
markrad 0:cdf462088d13 211
markrad 0:cdf462088d13 212 /* BEGIN_CASE */
markrad 0:cdf462088d13 213 void mbedtls_mpi_lsb( int radix_X, char *input_X, int nr_bits )
markrad 0:cdf462088d13 214 {
markrad 0:cdf462088d13 215 mbedtls_mpi X;
markrad 0:cdf462088d13 216 mbedtls_mpi_init( &X );
markrad 0:cdf462088d13 217
markrad 0:cdf462088d13 218 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 219 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
markrad 0:cdf462088d13 220
markrad 0:cdf462088d13 221 exit:
markrad 0:cdf462088d13 222 mbedtls_mpi_free( &X );
markrad 0:cdf462088d13 223 }
markrad 0:cdf462088d13 224 /* END_CASE */
markrad 0:cdf462088d13 225
markrad 0:cdf462088d13 226 /* BEGIN_CASE */
markrad 0:cdf462088d13 227 void mbedtls_mpi_bitlen( int radix_X, char *input_X, int nr_bits )
markrad 0:cdf462088d13 228 {
markrad 0:cdf462088d13 229 mbedtls_mpi X;
markrad 0:cdf462088d13 230 mbedtls_mpi_init( &X );
markrad 0:cdf462088d13 231
markrad 0:cdf462088d13 232 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 233 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
markrad 0:cdf462088d13 234
markrad 0:cdf462088d13 235 exit:
markrad 0:cdf462088d13 236 mbedtls_mpi_free( &X );
markrad 0:cdf462088d13 237 }
markrad 0:cdf462088d13 238 /* END_CASE */
markrad 0:cdf462088d13 239
markrad 0:cdf462088d13 240 /* BEGIN_CASE */
markrad 0:cdf462088d13 241 void mbedtls_mpi_gcd( int radix_X, char *input_X, int radix_Y, char *input_Y,
markrad 0:cdf462088d13 242 int radix_A, char *input_A )
markrad 0:cdf462088d13 243 {
markrad 0:cdf462088d13 244 mbedtls_mpi A, X, Y, Z;
markrad 0:cdf462088d13 245 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
markrad 0:cdf462088d13 246
markrad 0:cdf462088d13 247 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 248 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
markrad 0:cdf462088d13 249 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
markrad 0:cdf462088d13 250 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
markrad 0:cdf462088d13 251 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
markrad 0:cdf462088d13 252
markrad 0:cdf462088d13 253 exit:
markrad 0:cdf462088d13 254 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
markrad 0:cdf462088d13 255 }
markrad 0:cdf462088d13 256 /* END_CASE */
markrad 0:cdf462088d13 257
markrad 0:cdf462088d13 258 /* BEGIN_CASE */
markrad 0:cdf462088d13 259 void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
markrad 0:cdf462088d13 260 {
markrad 0:cdf462088d13 261 mbedtls_mpi X;
markrad 0:cdf462088d13 262 mbedtls_mpi_init( &X );
markrad 0:cdf462088d13 263
markrad 0:cdf462088d13 264 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
markrad 0:cdf462088d13 265 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
markrad 0:cdf462088d13 266
markrad 0:cdf462088d13 267 exit:
markrad 0:cdf462088d13 268 mbedtls_mpi_free( &X );
markrad 0:cdf462088d13 269 }
markrad 0:cdf462088d13 270 /* END_CASE */
markrad 0:cdf462088d13 271
markrad 0:cdf462088d13 272 /* BEGIN_CASE */
markrad 0:cdf462088d13 273 void mbedtls_mpi_cmp_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
markrad 0:cdf462088d13 274 int input_A )
markrad 0:cdf462088d13 275 {
markrad 0:cdf462088d13 276 mbedtls_mpi X, Y;
markrad 0:cdf462088d13 277 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
markrad 0:cdf462088d13 278
markrad 0:cdf462088d13 279 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 280 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
markrad 0:cdf462088d13 281 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
markrad 0:cdf462088d13 282
markrad 0:cdf462088d13 283 exit:
markrad 0:cdf462088d13 284 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
markrad 0:cdf462088d13 285 }
markrad 0:cdf462088d13 286 /* END_CASE */
markrad 0:cdf462088d13 287
markrad 0:cdf462088d13 288 /* BEGIN_CASE */
markrad 0:cdf462088d13 289 void mbedtls_mpi_cmp_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
markrad 0:cdf462088d13 290 int input_A )
markrad 0:cdf462088d13 291 {
markrad 0:cdf462088d13 292 mbedtls_mpi X, Y;
markrad 0:cdf462088d13 293 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
markrad 0:cdf462088d13 294
markrad 0:cdf462088d13 295 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 296 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
markrad 0:cdf462088d13 297 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
markrad 0:cdf462088d13 298
markrad 0:cdf462088d13 299 exit:
markrad 0:cdf462088d13 300 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
markrad 0:cdf462088d13 301 }
markrad 0:cdf462088d13 302 /* END_CASE */
markrad 0:cdf462088d13 303
markrad 0:cdf462088d13 304 /* BEGIN_CASE */
markrad 0:cdf462088d13 305 void mbedtls_mpi_copy( int input_X, int input_A )
markrad 0:cdf462088d13 306 {
markrad 0:cdf462088d13 307 mbedtls_mpi X, Y, A;
markrad 0:cdf462088d13 308 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
markrad 0:cdf462088d13 309
markrad 0:cdf462088d13 310 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
markrad 0:cdf462088d13 311 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_A ) == 0 );
markrad 0:cdf462088d13 312 TEST_ASSERT( mbedtls_mpi_lset( &A, input_A ) == 0 );
markrad 0:cdf462088d13 313 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
markrad 0:cdf462088d13 314 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
markrad 0:cdf462088d13 315 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
markrad 0:cdf462088d13 316 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
markrad 0:cdf462088d13 317 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) != 0 );
markrad 0:cdf462088d13 318
markrad 0:cdf462088d13 319 exit:
markrad 0:cdf462088d13 320 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
markrad 0:cdf462088d13 321 }
markrad 0:cdf462088d13 322 /* END_CASE */
markrad 0:cdf462088d13 323
markrad 0:cdf462088d13 324 /* BEGIN_CASE */
markrad 0:cdf462088d13 325 void mpi_copy_self( int input_X )
markrad 0:cdf462088d13 326 {
markrad 0:cdf462088d13 327 mbedtls_mpi X;
markrad 0:cdf462088d13 328 mbedtls_mpi_init( &X );
markrad 0:cdf462088d13 329
markrad 0:cdf462088d13 330 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
markrad 0:cdf462088d13 331 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
markrad 0:cdf462088d13 332 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
markrad 0:cdf462088d13 333
markrad 0:cdf462088d13 334 exit:
markrad 0:cdf462088d13 335 mbedtls_mpi_free( &X );
markrad 0:cdf462088d13 336 }
markrad 0:cdf462088d13 337 /* END_CASE */
markrad 0:cdf462088d13 338
markrad 0:cdf462088d13 339 /* BEGIN_CASE */
markrad 0:cdf462088d13 340 void mbedtls_mpi_shrink( int before, int used, int min, int after )
markrad 0:cdf462088d13 341 {
markrad 0:cdf462088d13 342 mbedtls_mpi X;
markrad 0:cdf462088d13 343 mbedtls_mpi_init( &X );
markrad 0:cdf462088d13 344
markrad 0:cdf462088d13 345 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
markrad 0:cdf462088d13 346 TEST_ASSERT( used <= before );
markrad 0:cdf462088d13 347 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
markrad 0:cdf462088d13 348 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
markrad 0:cdf462088d13 349 TEST_ASSERT( X.n == (size_t) after );
markrad 0:cdf462088d13 350
markrad 0:cdf462088d13 351 exit:
markrad 0:cdf462088d13 352 mbedtls_mpi_free( &X );
markrad 0:cdf462088d13 353 }
markrad 0:cdf462088d13 354 /* END_CASE */
markrad 0:cdf462088d13 355
markrad 0:cdf462088d13 356 /* BEGIN_CASE */
markrad 0:cdf462088d13 357 void mbedtls_mpi_safe_cond_assign( int x_sign, char *x_str,
markrad 0:cdf462088d13 358 int y_sign, char *y_str )
markrad 0:cdf462088d13 359 {
markrad 0:cdf462088d13 360 mbedtls_mpi X, Y, XX;
markrad 0:cdf462088d13 361 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
markrad 0:cdf462088d13 362
markrad 0:cdf462088d13 363 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
markrad 0:cdf462088d13 364 X.s = x_sign;
markrad 0:cdf462088d13 365 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
markrad 0:cdf462088d13 366 Y.s = y_sign;
markrad 0:cdf462088d13 367 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
markrad 0:cdf462088d13 368
markrad 0:cdf462088d13 369 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
markrad 0:cdf462088d13 370 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
markrad 0:cdf462088d13 371
markrad 0:cdf462088d13 372 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
markrad 0:cdf462088d13 373 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
markrad 0:cdf462088d13 374
markrad 0:cdf462088d13 375 exit:
markrad 0:cdf462088d13 376 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
markrad 0:cdf462088d13 377 }
markrad 0:cdf462088d13 378 /* END_CASE */
markrad 0:cdf462088d13 379
markrad 0:cdf462088d13 380 /* BEGIN_CASE */
markrad 0:cdf462088d13 381 void mbedtls_mpi_safe_cond_swap( int x_sign, char *x_str,
markrad 0:cdf462088d13 382 int y_sign, char *y_str )
markrad 0:cdf462088d13 383 {
markrad 0:cdf462088d13 384 mbedtls_mpi X, Y, XX, YY;
markrad 0:cdf462088d13 385
markrad 0:cdf462088d13 386 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
markrad 0:cdf462088d13 387 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
markrad 0:cdf462088d13 388
markrad 0:cdf462088d13 389 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
markrad 0:cdf462088d13 390 X.s = x_sign;
markrad 0:cdf462088d13 391 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
markrad 0:cdf462088d13 392 Y.s = y_sign;
markrad 0:cdf462088d13 393
markrad 0:cdf462088d13 394 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
markrad 0:cdf462088d13 395 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
markrad 0:cdf462088d13 396
markrad 0:cdf462088d13 397 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
markrad 0:cdf462088d13 398 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
markrad 0:cdf462088d13 399 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
markrad 0:cdf462088d13 400
markrad 0:cdf462088d13 401 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
markrad 0:cdf462088d13 402 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
markrad 0:cdf462088d13 403 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
markrad 0:cdf462088d13 404
markrad 0:cdf462088d13 405 exit:
markrad 0:cdf462088d13 406 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
markrad 0:cdf462088d13 407 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
markrad 0:cdf462088d13 408 }
markrad 0:cdf462088d13 409 /* END_CASE */
markrad 0:cdf462088d13 410
markrad 0:cdf462088d13 411 /* BEGIN_CASE */
markrad 0:cdf462088d13 412 void mbedtls_mpi_swap( int input_X, int input_Y )
markrad 0:cdf462088d13 413 {
markrad 0:cdf462088d13 414 mbedtls_mpi X, Y, A;
markrad 0:cdf462088d13 415 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
markrad 0:cdf462088d13 416
markrad 0:cdf462088d13 417 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
markrad 0:cdf462088d13 418 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
markrad 0:cdf462088d13 419 TEST_ASSERT( mbedtls_mpi_lset( &A, input_X ) == 0 );
markrad 0:cdf462088d13 420 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
markrad 0:cdf462088d13 421 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
markrad 0:cdf462088d13 422 mbedtls_mpi_swap( &X, &Y );
markrad 0:cdf462088d13 423 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
markrad 0:cdf462088d13 424 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
markrad 0:cdf462088d13 425
markrad 0:cdf462088d13 426 exit:
markrad 0:cdf462088d13 427 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
markrad 0:cdf462088d13 428 }
markrad 0:cdf462088d13 429 /* END_CASE */
markrad 0:cdf462088d13 430
markrad 0:cdf462088d13 431 /* BEGIN_CASE */
markrad 0:cdf462088d13 432 void mbedtls_mpi_add_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
markrad 0:cdf462088d13 433 int radix_A, char *input_A )
markrad 0:cdf462088d13 434 {
markrad 0:cdf462088d13 435 mbedtls_mpi X, Y, Z, A;
markrad 0:cdf462088d13 436 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
markrad 0:cdf462088d13 437
markrad 0:cdf462088d13 438 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 439 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
markrad 0:cdf462088d13 440 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
markrad 0:cdf462088d13 441 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
markrad 0:cdf462088d13 442 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
markrad 0:cdf462088d13 443
markrad 0:cdf462088d13 444 exit:
markrad 0:cdf462088d13 445 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
markrad 0:cdf462088d13 446 }
markrad 0:cdf462088d13 447 /* END_CASE */
markrad 0:cdf462088d13 448
markrad 0:cdf462088d13 449 /* BEGIN_CASE */
markrad 0:cdf462088d13 450 void mbedtls_mpi_add_mpi_inplace( int radix_X, char *input_X, int radix_A, char *input_A )
markrad 0:cdf462088d13 451 {
markrad 0:cdf462088d13 452 mbedtls_mpi X, A;
markrad 0:cdf462088d13 453 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
markrad 0:cdf462088d13 454
markrad 0:cdf462088d13 455 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
markrad 0:cdf462088d13 456
markrad 0:cdf462088d13 457 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 458 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
markrad 0:cdf462088d13 459 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
markrad 0:cdf462088d13 460
markrad 0:cdf462088d13 461 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 462 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
markrad 0:cdf462088d13 463 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
markrad 0:cdf462088d13 464
markrad 0:cdf462088d13 465 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 466 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
markrad 0:cdf462088d13 467 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
markrad 0:cdf462088d13 468
markrad 0:cdf462088d13 469 exit:
markrad 0:cdf462088d13 470 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
markrad 0:cdf462088d13 471 }
markrad 0:cdf462088d13 472 /* END_CASE */
markrad 0:cdf462088d13 473
markrad 0:cdf462088d13 474
markrad 0:cdf462088d13 475 /* BEGIN_CASE */
markrad 0:cdf462088d13 476 void mbedtls_mpi_add_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
markrad 0:cdf462088d13 477 int radix_A, char *input_A )
markrad 0:cdf462088d13 478 {
markrad 0:cdf462088d13 479 mbedtls_mpi X, Y, Z, A;
markrad 0:cdf462088d13 480 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
markrad 0:cdf462088d13 481
markrad 0:cdf462088d13 482 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 483 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
markrad 0:cdf462088d13 484 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
markrad 0:cdf462088d13 485 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
markrad 0:cdf462088d13 486 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
markrad 0:cdf462088d13 487
markrad 0:cdf462088d13 488 exit:
markrad 0:cdf462088d13 489 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
markrad 0:cdf462088d13 490 }
markrad 0:cdf462088d13 491 /* END_CASE */
markrad 0:cdf462088d13 492
markrad 0:cdf462088d13 493 /* BEGIN_CASE */
markrad 0:cdf462088d13 494 void mpi_add_abs_add_first( int radix_X, char *input_X, int radix_Y,
markrad 0:cdf462088d13 495 char *input_Y, int radix_A, char *input_A )
markrad 0:cdf462088d13 496 {
markrad 0:cdf462088d13 497 mbedtls_mpi X, Y, A;
markrad 0:cdf462088d13 498 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
markrad 0:cdf462088d13 499
markrad 0:cdf462088d13 500 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 501 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
markrad 0:cdf462088d13 502 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
markrad 0:cdf462088d13 503 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
markrad 0:cdf462088d13 504 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
markrad 0:cdf462088d13 505
markrad 0:cdf462088d13 506 exit:
markrad 0:cdf462088d13 507 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
markrad 0:cdf462088d13 508 }
markrad 0:cdf462088d13 509 /* END_CASE */
markrad 0:cdf462088d13 510
markrad 0:cdf462088d13 511 /* BEGIN_CASE */
markrad 0:cdf462088d13 512 void mpi_add_abs_add_second( int radix_X, char *input_X, int radix_Y,
markrad 0:cdf462088d13 513 char *input_Y, int radix_A, char *input_A )
markrad 0:cdf462088d13 514 {
markrad 0:cdf462088d13 515 mbedtls_mpi X, Y, A;
markrad 0:cdf462088d13 516 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
markrad 0:cdf462088d13 517
markrad 0:cdf462088d13 518 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 519 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
markrad 0:cdf462088d13 520 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
markrad 0:cdf462088d13 521 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
markrad 0:cdf462088d13 522 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
markrad 0:cdf462088d13 523
markrad 0:cdf462088d13 524 exit:
markrad 0:cdf462088d13 525 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
markrad 0:cdf462088d13 526 }
markrad 0:cdf462088d13 527 /* END_CASE */
markrad 0:cdf462088d13 528
markrad 0:cdf462088d13 529 /* BEGIN_CASE */
markrad 0:cdf462088d13 530 void mbedtls_mpi_add_int( int radix_X, char *input_X, int input_Y, int radix_A,
markrad 0:cdf462088d13 531 char *input_A )
markrad 0:cdf462088d13 532 {
markrad 0:cdf462088d13 533 mbedtls_mpi X, Z, A;
markrad 0:cdf462088d13 534 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
markrad 0:cdf462088d13 535
markrad 0:cdf462088d13 536 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 537 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
markrad 0:cdf462088d13 538 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
markrad 0:cdf462088d13 539 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
markrad 0:cdf462088d13 540
markrad 0:cdf462088d13 541 exit:
markrad 0:cdf462088d13 542 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
markrad 0:cdf462088d13 543 }
markrad 0:cdf462088d13 544 /* END_CASE */
markrad 0:cdf462088d13 545
markrad 0:cdf462088d13 546 /* BEGIN_CASE */
markrad 0:cdf462088d13 547 void mbedtls_mpi_sub_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
markrad 0:cdf462088d13 548 int radix_A, char *input_A )
markrad 0:cdf462088d13 549 {
markrad 0:cdf462088d13 550 mbedtls_mpi X, Y, Z, A;
markrad 0:cdf462088d13 551 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
markrad 0:cdf462088d13 552
markrad 0:cdf462088d13 553 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 554 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
markrad 0:cdf462088d13 555 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
markrad 0:cdf462088d13 556 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
markrad 0:cdf462088d13 557 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
markrad 0:cdf462088d13 558
markrad 0:cdf462088d13 559 exit:
markrad 0:cdf462088d13 560 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
markrad 0:cdf462088d13 561 }
markrad 0:cdf462088d13 562 /* END_CASE */
markrad 0:cdf462088d13 563
markrad 0:cdf462088d13 564 /* BEGIN_CASE */
markrad 0:cdf462088d13 565 void mbedtls_mpi_sub_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
markrad 0:cdf462088d13 566 int radix_A, char *input_A, int sub_result )
markrad 0:cdf462088d13 567 {
markrad 0:cdf462088d13 568 mbedtls_mpi X, Y, Z, A;
markrad 0:cdf462088d13 569 int res;
markrad 0:cdf462088d13 570 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
markrad 0:cdf462088d13 571
markrad 0:cdf462088d13 572 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 573 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
markrad 0:cdf462088d13 574 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
markrad 0:cdf462088d13 575
markrad 0:cdf462088d13 576 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
markrad 0:cdf462088d13 577 TEST_ASSERT( res == sub_result );
markrad 0:cdf462088d13 578 if( res == 0 )
markrad 0:cdf462088d13 579 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
markrad 0:cdf462088d13 580
markrad 0:cdf462088d13 581 exit:
markrad 0:cdf462088d13 582 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
markrad 0:cdf462088d13 583 }
markrad 0:cdf462088d13 584 /* END_CASE */
markrad 0:cdf462088d13 585
markrad 0:cdf462088d13 586 /* BEGIN_CASE */
markrad 0:cdf462088d13 587 void mbedtls_mpi_sub_int( int radix_X, char *input_X, int input_Y, int radix_A,
markrad 0:cdf462088d13 588 char *input_A )
markrad 0:cdf462088d13 589 {
markrad 0:cdf462088d13 590 mbedtls_mpi X, Z, A;
markrad 0:cdf462088d13 591 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
markrad 0:cdf462088d13 592
markrad 0:cdf462088d13 593 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 594 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
markrad 0:cdf462088d13 595 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
markrad 0:cdf462088d13 596 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
markrad 0:cdf462088d13 597
markrad 0:cdf462088d13 598 exit:
markrad 0:cdf462088d13 599 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
markrad 0:cdf462088d13 600 }
markrad 0:cdf462088d13 601 /* END_CASE */
markrad 0:cdf462088d13 602
markrad 0:cdf462088d13 603 /* BEGIN_CASE */
markrad 0:cdf462088d13 604 void mbedtls_mpi_mul_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
markrad 0:cdf462088d13 605 int radix_A, char *input_A )
markrad 0:cdf462088d13 606 {
markrad 0:cdf462088d13 607 mbedtls_mpi X, Y, Z, A;
markrad 0:cdf462088d13 608 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
markrad 0:cdf462088d13 609
markrad 0:cdf462088d13 610 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 611 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
markrad 0:cdf462088d13 612 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
markrad 0:cdf462088d13 613 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
markrad 0:cdf462088d13 614 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
markrad 0:cdf462088d13 615
markrad 0:cdf462088d13 616 exit:
markrad 0:cdf462088d13 617 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
markrad 0:cdf462088d13 618 }
markrad 0:cdf462088d13 619 /* END_CASE */
markrad 0:cdf462088d13 620
markrad 0:cdf462088d13 621 /* BEGIN_CASE */
markrad 0:cdf462088d13 622 void mbedtls_mpi_mul_int( int radix_X, char *input_X, int input_Y, int radix_A,
markrad 0:cdf462088d13 623 char *input_A, char *result_comparison )
markrad 0:cdf462088d13 624 {
markrad 0:cdf462088d13 625 mbedtls_mpi X, Z, A;
markrad 0:cdf462088d13 626 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
markrad 0:cdf462088d13 627
markrad 0:cdf462088d13 628 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 629 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
markrad 0:cdf462088d13 630 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
markrad 0:cdf462088d13 631 if( strcmp( result_comparison, "==" ) == 0 )
markrad 0:cdf462088d13 632 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
markrad 0:cdf462088d13 633 else if( strcmp( result_comparison, "!=" ) == 0 )
markrad 0:cdf462088d13 634 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
markrad 0:cdf462088d13 635 else
markrad 0:cdf462088d13 636 TEST_ASSERT( "unknown operator" == 0 );
markrad 0:cdf462088d13 637
markrad 0:cdf462088d13 638 exit:
markrad 0:cdf462088d13 639 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
markrad 0:cdf462088d13 640 }
markrad 0:cdf462088d13 641 /* END_CASE */
markrad 0:cdf462088d13 642
markrad 0:cdf462088d13 643 /* BEGIN_CASE */
markrad 0:cdf462088d13 644 void mbedtls_mpi_div_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
markrad 0:cdf462088d13 645 int radix_A, char *input_A, int radix_B, char *input_B,
markrad 0:cdf462088d13 646 int div_result )
markrad 0:cdf462088d13 647 {
markrad 0:cdf462088d13 648 mbedtls_mpi X, Y, Q, R, A, B;
markrad 0:cdf462088d13 649 int res;
markrad 0:cdf462088d13 650 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
markrad 0:cdf462088d13 651 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
markrad 0:cdf462088d13 652
markrad 0:cdf462088d13 653 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 654 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
markrad 0:cdf462088d13 655 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
markrad 0:cdf462088d13 656 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
markrad 0:cdf462088d13 657 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
markrad 0:cdf462088d13 658 TEST_ASSERT( res == div_result );
markrad 0:cdf462088d13 659 if( res == 0 )
markrad 0:cdf462088d13 660 {
markrad 0:cdf462088d13 661 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
markrad 0:cdf462088d13 662 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
markrad 0:cdf462088d13 663 }
markrad 0:cdf462088d13 664
markrad 0:cdf462088d13 665 exit:
markrad 0:cdf462088d13 666 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
markrad 0:cdf462088d13 667 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
markrad 0:cdf462088d13 668 }
markrad 0:cdf462088d13 669 /* END_CASE */
markrad 0:cdf462088d13 670
markrad 0:cdf462088d13 671 /* BEGIN_CASE */
markrad 0:cdf462088d13 672 void mbedtls_mpi_div_int( int radix_X, char *input_X, int input_Y, int radix_A,
markrad 0:cdf462088d13 673 char *input_A, int radix_B, char *input_B, int div_result )
markrad 0:cdf462088d13 674 {
markrad 0:cdf462088d13 675 mbedtls_mpi X, Q, R, A, B;
markrad 0:cdf462088d13 676 int res;
markrad 0:cdf462088d13 677 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
markrad 0:cdf462088d13 678 mbedtls_mpi_init( &B );
markrad 0:cdf462088d13 679
markrad 0:cdf462088d13 680 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 681 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
markrad 0:cdf462088d13 682 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
markrad 0:cdf462088d13 683 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
markrad 0:cdf462088d13 684 TEST_ASSERT( res == div_result );
markrad 0:cdf462088d13 685 if( res == 0 )
markrad 0:cdf462088d13 686 {
markrad 0:cdf462088d13 687 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
markrad 0:cdf462088d13 688 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
markrad 0:cdf462088d13 689 }
markrad 0:cdf462088d13 690
markrad 0:cdf462088d13 691 exit:
markrad 0:cdf462088d13 692 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
markrad 0:cdf462088d13 693 mbedtls_mpi_free( &B );
markrad 0:cdf462088d13 694 }
markrad 0:cdf462088d13 695 /* END_CASE */
markrad 0:cdf462088d13 696
markrad 0:cdf462088d13 697 /* BEGIN_CASE */
markrad 0:cdf462088d13 698 void mbedtls_mpi_mod_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
markrad 0:cdf462088d13 699 int radix_A, char *input_A, int div_result )
markrad 0:cdf462088d13 700 {
markrad 0:cdf462088d13 701 mbedtls_mpi X, Y, A;
markrad 0:cdf462088d13 702 int res;
markrad 0:cdf462088d13 703 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
markrad 0:cdf462088d13 704
markrad 0:cdf462088d13 705 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 706 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
markrad 0:cdf462088d13 707 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
markrad 0:cdf462088d13 708 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
markrad 0:cdf462088d13 709 TEST_ASSERT( res == div_result );
markrad 0:cdf462088d13 710 if( res == 0 )
markrad 0:cdf462088d13 711 {
markrad 0:cdf462088d13 712 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
markrad 0:cdf462088d13 713 }
markrad 0:cdf462088d13 714
markrad 0:cdf462088d13 715 exit:
markrad 0:cdf462088d13 716 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
markrad 0:cdf462088d13 717 }
markrad 0:cdf462088d13 718 /* END_CASE */
markrad 0:cdf462088d13 719
markrad 0:cdf462088d13 720 /* BEGIN_CASE */
markrad 0:cdf462088d13 721 void mbedtls_mpi_mod_int( int radix_X, char *input_X, int input_Y, int input_A,
markrad 0:cdf462088d13 722 int div_result )
markrad 0:cdf462088d13 723 {
markrad 0:cdf462088d13 724 mbedtls_mpi X;
markrad 0:cdf462088d13 725 int res;
markrad 0:cdf462088d13 726 mbedtls_mpi_uint r;
markrad 0:cdf462088d13 727 mbedtls_mpi_init( &X );
markrad 0:cdf462088d13 728
markrad 0:cdf462088d13 729 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 730 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
markrad 0:cdf462088d13 731 TEST_ASSERT( res == div_result );
markrad 0:cdf462088d13 732 if( res == 0 )
markrad 0:cdf462088d13 733 {
markrad 0:cdf462088d13 734 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
markrad 0:cdf462088d13 735 }
markrad 0:cdf462088d13 736
markrad 0:cdf462088d13 737 exit:
markrad 0:cdf462088d13 738 mbedtls_mpi_free( &X );
markrad 0:cdf462088d13 739 }
markrad 0:cdf462088d13 740 /* END_CASE */
markrad 0:cdf462088d13 741
markrad 0:cdf462088d13 742 /* BEGIN_CASE */
markrad 0:cdf462088d13 743 void mbedtls_mpi_exp_mod( int radix_A, char *input_A, int radix_E, char *input_E,
markrad 0:cdf462088d13 744 int radix_N, char *input_N, int radix_RR, char *input_RR,
markrad 0:cdf462088d13 745 int radix_X, char *input_X, int div_result )
markrad 0:cdf462088d13 746 {
markrad 0:cdf462088d13 747 mbedtls_mpi A, E, N, RR, Z, X;
markrad 0:cdf462088d13 748 int res;
markrad 0:cdf462088d13 749 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
markrad 0:cdf462088d13 750 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
markrad 0:cdf462088d13 751
markrad 0:cdf462088d13 752 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
markrad 0:cdf462088d13 753 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
markrad 0:cdf462088d13 754 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
markrad 0:cdf462088d13 755 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 756
markrad 0:cdf462088d13 757 if( strlen( input_RR ) )
markrad 0:cdf462088d13 758 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
markrad 0:cdf462088d13 759
markrad 0:cdf462088d13 760 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
markrad 0:cdf462088d13 761 TEST_ASSERT( res == div_result );
markrad 0:cdf462088d13 762 if( res == 0 )
markrad 0:cdf462088d13 763 {
markrad 0:cdf462088d13 764 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
markrad 0:cdf462088d13 765 }
markrad 0:cdf462088d13 766
markrad 0:cdf462088d13 767 exit:
markrad 0:cdf462088d13 768 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
markrad 0:cdf462088d13 769 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
markrad 0:cdf462088d13 770 }
markrad 0:cdf462088d13 771 /* END_CASE */
markrad 0:cdf462088d13 772
markrad 0:cdf462088d13 773 /* BEGIN_CASE */
markrad 0:cdf462088d13 774 void mbedtls_mpi_inv_mod( int radix_X, char *input_X, int radix_Y, char *input_Y,
markrad 0:cdf462088d13 775 int radix_A, char *input_A, int div_result )
markrad 0:cdf462088d13 776 {
markrad 0:cdf462088d13 777 mbedtls_mpi X, Y, Z, A;
markrad 0:cdf462088d13 778 int res;
markrad 0:cdf462088d13 779 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
markrad 0:cdf462088d13 780
markrad 0:cdf462088d13 781 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 782 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
markrad 0:cdf462088d13 783 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
markrad 0:cdf462088d13 784 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
markrad 0:cdf462088d13 785 TEST_ASSERT( res == div_result );
markrad 0:cdf462088d13 786 if( res == 0 )
markrad 0:cdf462088d13 787 {
markrad 0:cdf462088d13 788 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
markrad 0:cdf462088d13 789 }
markrad 0:cdf462088d13 790
markrad 0:cdf462088d13 791 exit:
markrad 0:cdf462088d13 792 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
markrad 0:cdf462088d13 793 }
markrad 0:cdf462088d13 794 /* END_CASE */
markrad 0:cdf462088d13 795
markrad 0:cdf462088d13 796 /* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
markrad 0:cdf462088d13 797 void mbedtls_mpi_is_prime( int radix_X, char *input_X, int div_result )
markrad 0:cdf462088d13 798 {
markrad 0:cdf462088d13 799 mbedtls_mpi X;
markrad 0:cdf462088d13 800 int res;
markrad 0:cdf462088d13 801 mbedtls_mpi_init( &X );
markrad 0:cdf462088d13 802
markrad 0:cdf462088d13 803 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 804 res = mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL );
markrad 0:cdf462088d13 805 TEST_ASSERT( res == div_result );
markrad 0:cdf462088d13 806
markrad 0:cdf462088d13 807 exit:
markrad 0:cdf462088d13 808 mbedtls_mpi_free( &X );
markrad 0:cdf462088d13 809 }
markrad 0:cdf462088d13 810 /* END_CASE */
markrad 0:cdf462088d13 811
markrad 0:cdf462088d13 812 /* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
markrad 0:cdf462088d13 813 void mbedtls_mpi_gen_prime( int bits, int safe, int ref_ret )
markrad 0:cdf462088d13 814 {
markrad 0:cdf462088d13 815 mbedtls_mpi X;
markrad 0:cdf462088d13 816 int my_ret;
markrad 0:cdf462088d13 817
markrad 0:cdf462088d13 818 mbedtls_mpi_init( &X );
markrad 0:cdf462088d13 819
markrad 0:cdf462088d13 820 my_ret = mbedtls_mpi_gen_prime( &X, bits, safe, rnd_std_rand, NULL );
markrad 0:cdf462088d13 821 TEST_ASSERT( my_ret == ref_ret );
markrad 0:cdf462088d13 822
markrad 0:cdf462088d13 823 if( ref_ret == 0 )
markrad 0:cdf462088d13 824 {
markrad 0:cdf462088d13 825 size_t actual_bits = mbedtls_mpi_bitlen( &X );
markrad 0:cdf462088d13 826
markrad 0:cdf462088d13 827 TEST_ASSERT( actual_bits >= (size_t) bits );
markrad 0:cdf462088d13 828 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
markrad 0:cdf462088d13 829
markrad 0:cdf462088d13 830 TEST_ASSERT( mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL ) == 0 );
markrad 0:cdf462088d13 831 if( safe )
markrad 0:cdf462088d13 832 {
markrad 0:cdf462088d13 833 mbedtls_mpi_shift_r( &X, 1 ); /* X = ( X - 1 ) / 2 */
markrad 0:cdf462088d13 834 TEST_ASSERT( mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL ) == 0 );
markrad 0:cdf462088d13 835 }
markrad 0:cdf462088d13 836 }
markrad 0:cdf462088d13 837
markrad 0:cdf462088d13 838 exit:
markrad 0:cdf462088d13 839 mbedtls_mpi_free( &X );
markrad 0:cdf462088d13 840 }
markrad 0:cdf462088d13 841 /* END_CASE */
markrad 0:cdf462088d13 842
markrad 0:cdf462088d13 843 /* BEGIN_CASE */
markrad 0:cdf462088d13 844 void mbedtls_mpi_shift_l( int radix_X, char *input_X, int shift_X, int radix_A,
markrad 0:cdf462088d13 845 char *input_A)
markrad 0:cdf462088d13 846 {
markrad 0:cdf462088d13 847 mbedtls_mpi X, A;
markrad 0:cdf462088d13 848 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
markrad 0:cdf462088d13 849
markrad 0:cdf462088d13 850 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 851 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
markrad 0:cdf462088d13 852 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
markrad 0:cdf462088d13 853 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
markrad 0:cdf462088d13 854
markrad 0:cdf462088d13 855 exit:
markrad 0:cdf462088d13 856 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
markrad 0:cdf462088d13 857 }
markrad 0:cdf462088d13 858 /* END_CASE */
markrad 0:cdf462088d13 859
markrad 0:cdf462088d13 860 /* BEGIN_CASE */
markrad 0:cdf462088d13 861 void mbedtls_mpi_shift_r( int radix_X, char *input_X, int shift_X, int radix_A,
markrad 0:cdf462088d13 862 char *input_A )
markrad 0:cdf462088d13 863 {
markrad 0:cdf462088d13 864 mbedtls_mpi X, A;
markrad 0:cdf462088d13 865 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
markrad 0:cdf462088d13 866
markrad 0:cdf462088d13 867 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
markrad 0:cdf462088d13 868 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
markrad 0:cdf462088d13 869 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
markrad 0:cdf462088d13 870 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
markrad 0:cdf462088d13 871
markrad 0:cdf462088d13 872 exit:
markrad 0:cdf462088d13 873 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
markrad 0:cdf462088d13 874 }
markrad 0:cdf462088d13 875 /* END_CASE */
markrad 0:cdf462088d13 876
markrad 0:cdf462088d13 877 /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
markrad 0:cdf462088d13 878 void mpi_selftest()
markrad 0:cdf462088d13 879 {
markrad 0:cdf462088d13 880 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
markrad 0:cdf462088d13 881 }
markrad 0:cdf462088d13 882 /* END_CASE */