00001 #include <polarssl/config.h>
00002
00003 #ifdef POLARSSL_ECP_C
00004
00005 #include <polarssl/ecp.h>
00006
00007 #define POLARSSL_ECP_PF_UNKNOWN -1
00008 #endif
00009
00010
00011 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
00012 #include "polarssl/memory.h"
00013 #endif
00014
00015 #if defined(WANT_NOT_RND_MPI)
00016 #if defined(POLARSSL_BIGNUM_C)
00017 #include "polarssl/bignum.h"
00018 #else
00019 #error "not_rnd_mpi() need bignum.c"
00020 #endif
00021 #endif
00022
00023 #ifdef _MSC_VER
00024 #include <basetsd.h>
00025 typedef UINT32 uint32_t;
00026 #else
00027 #include <inttypes.h>
00028 #endif
00029
00030 #include <assert.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033
00034
00035
00036
00037 #ifndef GET_UINT32_BE
00038 #define GET_UINT32_BE(n,b,i) \
00039 { \
00040 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
00041 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
00042 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
00043 | ( (uint32_t) (b)[(i) + 3] ); \
00044 }
00045 #endif
00046
00047 #ifndef PUT_UINT32_BE
00048 #define PUT_UINT32_BE(n,b,i) \
00049 { \
00050 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
00051 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
00052 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
00053 (b)[(i) + 3] = (unsigned char) ( (n) ); \
00054 }
00055 #endif
00056
00057 static int unhexify(unsigned char *obuf, const char *ibuf)
00058 {
00059 unsigned char c, c2;
00060 int len = strlen(ibuf) / 2;
00061 assert(!(strlen(ibuf) %1));
00062
00063 while (*ibuf != 0)
00064 {
00065 c = *ibuf++;
00066 if( c >= '0' && c <= '9' )
00067 c -= '0';
00068 else if( c >= 'a' && c <= 'f' )
00069 c -= 'a' - 10;
00070 else if( c >= 'A' && c <= 'F' )
00071 c -= 'A' - 10;
00072 else
00073 assert( 0 );
00074
00075 c2 = *ibuf++;
00076 if( c2 >= '0' && c2 <= '9' )
00077 c2 -= '0';
00078 else if( c2 >= 'a' && c2 <= 'f' )
00079 c2 -= 'a' - 10;
00080 else if( c2 >= 'A' && c2 <= 'F' )
00081 c2 -= 'A' - 10;
00082 else
00083 assert( 0 );
00084
00085 *obuf++ = ( c << 4 ) | c2;
00086 }
00087
00088 return len;
00089 }
00090
00091 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
00092 {
00093 unsigned char l, h;
00094
00095 while (len != 0)
00096 {
00097 h = (*ibuf) / 16;
00098 l = (*ibuf) % 16;
00099
00100 if( h < 10 )
00101 *obuf++ = '0' + h;
00102 else
00103 *obuf++ = 'a' + h - 10;
00104
00105 if( l < 10 )
00106 *obuf++ = '0' + l;
00107 else
00108 *obuf++ = 'a' + l - 10;
00109
00110 ++ibuf;
00111 len--;
00112 }
00113 }
00114
00124 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
00125 {
00126 size_t i;
00127
00128 if( rng_state != NULL )
00129 rng_state = NULL;
00130
00131 for( i = 0; i < len; ++i )
00132 output[i] = rand();
00133
00134 return( 0 );
00135 }
00136
00142 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
00143 {
00144 if( rng_state != NULL )
00145 rng_state = NULL;
00146
00147 memset( output, 0, len );
00148
00149 return( 0 );
00150 }
00151
00152 typedef struct
00153 {
00154 unsigned char *buf;
00155 size_t length;
00156 } rnd_buf_info;
00157
00169 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
00170 {
00171 rnd_buf_info *info = (rnd_buf_info *) rng_state;
00172 size_t use_len;
00173
00174 if( rng_state == NULL )
00175 return( rnd_std_rand( NULL, output, len ) );
00176
00177 use_len = len;
00178 if( len > info->length )
00179 use_len = info->length;
00180
00181 if( use_len )
00182 {
00183 memcpy( output, info->buf, use_len );
00184 info->buf += use_len;
00185 info->length -= use_len;
00186 }
00187
00188 if( len - use_len > 0 )
00189 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
00190
00191 return( 0 );
00192 }
00193
00201 typedef struct
00202 {
00203 uint32_t key[16];
00204 uint32_t v0, v1;
00205 } rnd_pseudo_info;
00206
00215 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
00216 {
00217 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
00218 uint32_t i, *k, sum, delta=0x9E3779B9;
00219 unsigned char result[4];
00220
00221 if( rng_state == NULL )
00222 return( rnd_std_rand( NULL, output, len ) );
00223
00224 k = info->key;
00225
00226 while( len > 0 )
00227 {
00228 size_t use_len = ( len > 4 ) ? 4 : len;
00229 sum = 0;
00230
00231 for( i = 0; i < 32; i++ )
00232 {
00233 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
00234 sum += delta;
00235 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
00236 }
00237
00238 PUT_UINT32_BE( info->v0, result, 0 );
00239 memcpy( output, result, use_len );
00240 len -= use_len;
00241 }
00242
00243 return( 0 );
00244 }
00245
00246 #if defined(WANT_NOT_RND_MPI)
00247
00255 #define ciL (sizeof(t_uint))
00256 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
00257 static int not_rnd_mpi( void *in, unsigned char *out, size_t len )
00258 {
00259 char *str = (char *) in;
00260 mpi X;
00261
00262
00263
00264
00265
00266 X.s = 1;
00267 X.p = (t_uint *) out;
00268 X.n = CHARS_TO_LIMBS( len );
00269
00270
00271
00272
00273
00274 assert( strlen( str ) / 2 == len );
00275
00276 return( mpi_read_string( &X, 16, str ) );
00277 }
00278 #endif
00279
00280
00281 #include <stdio.h>
00282 #include <string.h>
00283
00284 static int test_errors = 0;
00285
00286 #ifdef POLARSSL_ECP_C
00287
00288 #define TEST_SUITE_ACTIVE
00289
00290 static int test_assert( int correct, char *test )
00291 {
00292 if( correct )
00293 return( 0 );
00294
00295 test_errors++;
00296 if( test_errors == 1 )
00297 printf( "FAILED\n" );
00298 printf( " %s\n", test );
00299
00300 return( 1 );
00301 }
00302
00303 #define TEST_ASSERT( TEST ) \
00304 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
00305 if( test_errors) return; \
00306 } while (0)
00307
00308 int verify_string( char **str )
00309 {
00310 if( (*str)[0] != '"' ||
00311 (*str)[strlen( *str ) - 1] != '"' )
00312 {
00313 printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
00314 return( -1 );
00315 }
00316
00317 (*str)++;
00318 (*str)[strlen( *str ) - 1] = '\0';
00319
00320 return( 0 );
00321 }
00322
00323 int verify_int( char *str, int *value )
00324 {
00325 size_t i;
00326 int minus = 0;
00327 int digits = 1;
00328 int hex = 0;
00329
00330 for( i = 0; i < strlen( str ); i++ )
00331 {
00332 if( i == 0 && str[i] == '-' )
00333 {
00334 minus = 1;
00335 continue;
00336 }
00337
00338 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
00339 str[i - 1] == '0' && str[i] == 'x' )
00340 {
00341 hex = 1;
00342 continue;
00343 }
00344
00345 if( str[i] < '0' || str[i] > '9' )
00346 {
00347 digits = 0;
00348 break;
00349 }
00350 }
00351
00352 if( digits )
00353 {
00354 if( hex )
00355 *value = strtol( str, NULL, 16 );
00356 else
00357 *value = strtol( str, NULL, 10 );
00358
00359 return( 0 );
00360 }
00361
00362 if( strcmp( str, "POLARSSL_ECP_DP_SECP384R1" ) == 0 )
00363 {
00364 *value = ( POLARSSL_ECP_DP_SECP384R1 );
00365 return( 0 );
00366 }
00367 if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1" ) == 0 )
00368 {
00369 *value = ( POLARSSL_ECP_DP_SECP192R1 );
00370 return( 0 );
00371 }
00372 if( strcmp( str, "POLARSSL_ECP_PF_UNCOMPRESSED" ) == 0 )
00373 {
00374 *value = ( POLARSSL_ECP_PF_UNCOMPRESSED );
00375 return( 0 );
00376 }
00377 if( strcmp( str, "POLARSSL_ECP_DP_SECP256R1" ) == 0 )
00378 {
00379 *value = ( POLARSSL_ECP_DP_SECP256R1 );
00380 return( 0 );
00381 }
00382 if( strcmp( str, "POLARSSL_ECP_DP_BP256R1" ) == 0 )
00383 {
00384 *value = ( POLARSSL_ECP_DP_BP256R1 );
00385 return( 0 );
00386 }
00387 if( strcmp( str, "POLARSSL_ECP_PF_COMPRESSED" ) == 0 )
00388 {
00389 *value = ( POLARSSL_ECP_PF_COMPRESSED );
00390 return( 0 );
00391 }
00392 if( strcmp( str, "POLARSSL_ERR_ECP_BUFFER_TOO_SMALL" ) == 0 )
00393 {
00394 *value = ( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
00395 return( 0 );
00396 }
00397 if( strcmp( str, "POLARSSL_ERR_ECP_INVALID_KEY" ) == 0 )
00398 {
00399 *value = ( POLARSSL_ERR_ECP_INVALID_KEY );
00400 return( 0 );
00401 }
00402 if( strcmp( str, "POLARSSL_ECP_DP_SECP224R1" ) == 0 )
00403 {
00404 *value = ( POLARSSL_ECP_DP_SECP224R1 );
00405 return( 0 );
00406 }
00407 if( strcmp( str, "POLARSSL_ECP_DP_SECP521R1" ) == 0 )
00408 {
00409 *value = ( POLARSSL_ECP_DP_SECP521R1 );
00410 return( 0 );
00411 }
00412 if( strcmp( str, "POLARSSL_ECP_PF_UNKNOWN" ) == 0 )
00413 {
00414 *value = ( POLARSSL_ECP_PF_UNKNOWN );
00415 return( 0 );
00416 }
00417 if( strcmp( str, "POLARSSL_ECP_DP_BP512R1" ) == 0 )
00418 {
00419 *value = ( POLARSSL_ECP_DP_BP512R1 );
00420 return( 0 );
00421 }
00422 if( strcmp( str, "POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE" ) == 0 )
00423 {
00424 *value = ( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
00425 return( 0 );
00426 }
00427 if( strcmp( str, "POLARSSL_ERR_ECP_BAD_INPUT_DATA" ) == 0 )
00428 {
00429 *value = ( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
00430 return( 0 );
00431 }
00432 if( strcmp( str, "POLARSSL_ECP_DP_BP384R1" ) == 0 )
00433 {
00434 *value = ( POLARSSL_ECP_DP_BP384R1 );
00435 return( 0 );
00436 }
00437 if( strcmp( str, "-1" ) == 0 )
00438 {
00439 *value = ( -1 );
00440 return( 0 );
00441 }
00442
00443
00444 printf( "Expected integer for parameter and got: %s\n", str );
00445 return( -1 );
00446 }
00447
00448 void test_suite_ecp_small_add( int a_zero, char *x_a, char *y_a, int b_zero, char *x_b,
00449 char *y_b, int c_zero, int x_c, int y_c )
00450 {
00451 ecp_group grp;
00452 ecp_point A, B, C;
00453
00454 ecp_group_init( &grp );
00455 ecp_point_init( &A ); ecp_point_init( &B ); ecp_point_init( &C );
00456
00457 TEST_ASSERT( ecp_group_read_string( &grp, 10,
00458 "47", "4", "17", "42", "13" ) == 0 );
00459
00460 if( a_zero )
00461 ecp_set_zero( &A );
00462 else
00463 TEST_ASSERT( ecp_point_read_string( &A, 10, x_a, y_a ) == 0 );
00464
00465 if( b_zero )
00466 ecp_set_zero( &B );
00467 else
00468 TEST_ASSERT( ecp_point_read_string( &B, 10, x_b, y_b ) == 0 );
00469
00470 TEST_ASSERT( ecp_add( &grp, &C, &A, &B ) == 0 );
00471
00472 if( c_zero )
00473 TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
00474 else
00475 {
00476 TEST_ASSERT( mpi_cmp_int( &C.X, x_c ) == 0 );
00477 TEST_ASSERT( mpi_cmp_int( &C.Y, y_c ) == 0 );
00478 }
00479
00480 TEST_ASSERT( ecp_add( &grp, &C, &B, &A ) == 0 );
00481
00482 if( c_zero )
00483 TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
00484 else
00485 {
00486 TEST_ASSERT( mpi_cmp_int( &C.X, x_c ) == 0 );
00487 TEST_ASSERT( mpi_cmp_int( &C.Y, y_c ) == 0 );
00488 }
00489
00490 ecp_group_free( &grp );
00491 ecp_point_free( &A ); ecp_point_free( &B ); ecp_point_free( &C );
00492 }
00493
00494 void test_suite_ecp_small_sub( int a_zero, char *x_a, char *y_a, int b_zero, char *x_b,
00495 char *y_b, int c_zero, int x_c, int y_c )
00496 {
00497 ecp_group grp;
00498 ecp_point A, B, C;
00499
00500 ecp_group_init( &grp );
00501 ecp_point_init( &A ); ecp_point_init( &B ); ecp_point_init( &C );
00502
00503 TEST_ASSERT( ecp_group_read_string( &grp, 10,
00504 "47", "4", "17", "42", "13" ) == 0 );
00505
00506 if( a_zero )
00507 ecp_set_zero( &A );
00508 else
00509 TEST_ASSERT( ecp_point_read_string( &A, 10, x_a, y_a ) == 0 );
00510
00511 if( b_zero )
00512 ecp_set_zero( &B );
00513 else
00514 TEST_ASSERT( ecp_point_read_string( &B, 10, x_b, y_b ) == 0 );
00515
00516 TEST_ASSERT( ecp_sub( &grp, &C, &A, &B ) == 0 );
00517
00518 if( c_zero )
00519 TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
00520 else
00521 {
00522 TEST_ASSERT( mpi_cmp_int( &C.X, x_c ) == 0 );
00523 TEST_ASSERT( mpi_cmp_int( &C.Y, y_c ) == 0 );
00524 }
00525
00526 ecp_group_free( &grp );
00527 ecp_point_free( &A ); ecp_point_free( &B ); ecp_point_free( &C );
00528 }
00529
00530 void test_suite_ecp_small_mul( int m_str, int r_zero, int x_r, int y_r, int ret )
00531 {
00532 ecp_group grp;
00533 ecp_point R;
00534 mpi m;
00535 rnd_pseudo_info rnd_info;
00536
00537 ecp_group_init( &grp );
00538 ecp_point_init( &R );
00539 mpi_init( &m );
00540 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
00541
00542 TEST_ASSERT( ecp_group_read_string( &grp, 10,
00543 "47", "4", "17", "42", "13" ) == 0 );
00544
00545 TEST_ASSERT( mpi_lset( &m, m_str ) == 0 );
00546
00547 TEST_ASSERT( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) == ret );
00548
00549 if( r_zero )
00550 TEST_ASSERT( mpi_cmp_int( &R.Z, 0 ) == 0 );
00551 else
00552 {
00553 TEST_ASSERT( mpi_cmp_int( &R.X, x_r ) == 0 );
00554 TEST_ASSERT( mpi_cmp_int( &R.Y, y_r ) == 0 );
00555 }
00556
00557
00558 ecp_point_free( &R );
00559
00560 TEST_ASSERT( ecp_mul( &grp, &R, &m, &grp.G,
00561 &rnd_pseudo_rand, &rnd_info ) == ret );
00562
00563 if( r_zero )
00564 TEST_ASSERT( mpi_cmp_int( &R.Z, 0 ) == 0 );
00565 else
00566 {
00567 TEST_ASSERT( mpi_cmp_int( &R.X, x_r ) == 0 );
00568 TEST_ASSERT( mpi_cmp_int( &R.Y, y_r ) == 0 );
00569 }
00570
00571 ecp_group_free( &grp );
00572 ecp_point_free( &R );
00573 mpi_free( &m );
00574 }
00575
00576 void test_suite_ecp_small_check_pub( int x, int y, int z, int ret )
00577 {
00578 ecp_group grp;
00579 ecp_point P;
00580
00581 ecp_group_init( &grp );
00582 ecp_point_init( &P );
00583
00584 TEST_ASSERT( ecp_group_read_string( &grp, 10,
00585 "47", "4", "17", "42", "13" ) == 0 );
00586
00587 TEST_ASSERT( mpi_lset( &P.X, x ) == 0 );
00588 TEST_ASSERT( mpi_lset( &P.Y, y ) == 0 );
00589 TEST_ASSERT( mpi_lset( &P.Z, z ) == 0 );
00590
00591 TEST_ASSERT( ecp_check_pubkey( &grp, &P ) == ret );
00592
00593 ecp_group_free( &grp );
00594 ecp_point_free( &P );
00595 }
00596
00597 void test_suite_ecp_test_vect( int id, char *dA_str, char *xA_str, char *yA_str,
00598 char *dB_str, char *xB_str, char *yB_str, char *xZ_str,
00599 char *yZ_str )
00600 {
00601 ecp_group grp;
00602 ecp_point R;
00603 mpi dA, xA, yA, dB, xB, yB, xZ, yZ;
00604 rnd_pseudo_info rnd_info;
00605
00606 ecp_group_init( &grp ); ecp_point_init( &R );
00607 mpi_init( &dA ); mpi_init( &xA ); mpi_init( &yA ); mpi_init( &dB );
00608 mpi_init( &xB ); mpi_init( &yB ); mpi_init( &xZ ); mpi_init( &yZ );
00609 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
00610
00611 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
00612
00613 TEST_ASSERT( ecp_check_pubkey( &grp, &grp.G ) == 0 );
00614
00615 TEST_ASSERT( mpi_read_string( &dA, 16, dA_str ) == 0 );
00616 TEST_ASSERT( mpi_read_string( &xA, 16, xA_str ) == 0 );
00617 TEST_ASSERT( mpi_read_string( &yA, 16, yA_str ) == 0 );
00618 TEST_ASSERT( mpi_read_string( &dB, 16, dB_str ) == 0 );
00619 TEST_ASSERT( mpi_read_string( &xB, 16, xB_str ) == 0 );
00620 TEST_ASSERT( mpi_read_string( &yB, 16, yB_str ) == 0 );
00621 TEST_ASSERT( mpi_read_string( &xZ, 16, xZ_str ) == 0 );
00622 TEST_ASSERT( mpi_read_string( &yZ, 16, yZ_str ) == 0 );
00623
00624 TEST_ASSERT( ecp_mul( &grp, &R, &dA, &grp.G,
00625 &rnd_pseudo_rand, &rnd_info ) == 0 );
00626 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xA ) == 0 );
00627 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yA ) == 0 );
00628 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
00629 TEST_ASSERT( ecp_mul( &grp, &R, &dB, &R, NULL, NULL ) == 0 );
00630 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xZ ) == 0 );
00631 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
00632 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
00633
00634 TEST_ASSERT( ecp_mul( &grp, &R, &dB, &grp.G, NULL, NULL ) == 0 );
00635 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xB ) == 0 );
00636 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yB ) == 0 );
00637 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
00638 TEST_ASSERT( ecp_mul( &grp, &R, &dA, &R,
00639 &rnd_pseudo_rand, &rnd_info ) == 0 );
00640 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xZ ) == 0 );
00641 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
00642 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
00643
00644 ecp_group_free( &grp ); ecp_point_free( &R );
00645 mpi_free( &dA ); mpi_free( &xA ); mpi_free( &yA ); mpi_free( &dB );
00646 mpi_free( &xB ); mpi_free( &yB ); mpi_free( &xZ ); mpi_free( &yZ );
00647 }
00648
00649 void test_suite_ecp_fast_mod( int id, char *N_str )
00650 {
00651 ecp_group grp;
00652 mpi N, R;
00653
00654 mpi_init( &N ); mpi_init( &R );
00655 ecp_group_init( &grp );
00656
00657 TEST_ASSERT( mpi_read_string( &N, 16, N_str ) == 0 );
00658 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
00659 TEST_ASSERT( grp.modp != NULL );
00660
00661
00662
00663
00664 TEST_ASSERT( mpi_mod_mpi( &R, &N, &grp.P ) == 0 );
00665
00666 TEST_ASSERT( grp.modp( &N ) == 0 );
00667 TEST_ASSERT( mpi_msb( &N ) <= grp.pbits + 3 );
00668
00669
00670
00671
00672 TEST_ASSERT( mpi_mod_mpi( &N, &N, &grp.P ) == 0 );
00673 TEST_ASSERT( mpi_cmp_mpi( &N, &R ) == 0 );
00674
00675 mpi_free( &N ); mpi_free( &R );
00676 ecp_group_free( &grp );
00677 }
00678
00679 void test_suite_ecp_write_binary( int id, char *x, char *y, char *z, int format,
00680 char *out, int blen, int ret )
00681 {
00682 ecp_group grp;
00683 ecp_point P;
00684 unsigned char buf[256], str[512];
00685 size_t olen;
00686
00687 memset( buf, 0, sizeof( buf ) );
00688 memset( str, 0, sizeof( str ) );
00689
00690 ecp_group_init( &grp ); ecp_point_init( &P );
00691
00692 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
00693
00694 TEST_ASSERT( mpi_read_string( &P.X, 16, x ) == 0 );
00695 TEST_ASSERT( mpi_read_string( &P.Y, 16, y ) == 0 );
00696 TEST_ASSERT( mpi_read_string( &P.Z, 16, z ) == 0 );
00697
00698 TEST_ASSERT( ecp_point_write_binary( &grp, &P, format,
00699 &olen, buf, blen ) == ret );
00700
00701 if( ret == 0 )
00702 {
00703 hexify( str, buf, olen );
00704 TEST_ASSERT( strcasecmp( (char *) str, out ) == 0 );
00705 }
00706
00707 ecp_group_free( &grp ); ecp_point_free( &P );
00708 }
00709
00710 void test_suite_ecp_read_binary( int id, char *input, char *x, char *y, char *z,
00711 int ret )
00712 {
00713 ecp_group grp;
00714 ecp_point P;
00715 mpi X, Y, Z;
00716 int ilen;
00717 unsigned char buf[256];
00718
00719 memset( buf, 0, sizeof( buf ) );
00720
00721 ecp_group_init( &grp ); ecp_point_init( &P );
00722 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
00723
00724 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
00725
00726 TEST_ASSERT( mpi_read_string( &X, 16, x ) == 0 );
00727 TEST_ASSERT( mpi_read_string( &Y, 16, y ) == 0 );
00728 TEST_ASSERT( mpi_read_string( &Z, 16, z ) == 0 );
00729
00730 ilen = unhexify( buf, input );
00731
00732 TEST_ASSERT( ecp_point_read_binary( &grp, &P, buf, ilen ) == ret );
00733
00734 if( ret == 0 )
00735 {
00736 TEST_ASSERT( mpi_cmp_mpi( &P.X, &X ) == 0 );
00737 TEST_ASSERT( mpi_cmp_mpi( &P.Y, &Y ) == 0 );
00738 TEST_ASSERT( mpi_cmp_mpi( &P.Z, &Z ) == 0 );
00739 }
00740
00741 ecp_group_free( &grp ); ecp_point_free( &P );
00742 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
00743 }
00744
00745 void test_suite_ecp_tls_read_point( int id, char *input, char *x, char *y, char *z,
00746 int ret )
00747 {
00748 ecp_group grp;
00749 ecp_point P;
00750 mpi X, Y, Z;
00751 size_t ilen;
00752 unsigned char buf[256];
00753 const unsigned char *vbuf = buf;
00754
00755 memset( buf, 0, sizeof( buf ) );
00756
00757 ecp_group_init( &grp ); ecp_point_init( &P );
00758 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
00759
00760 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
00761
00762 TEST_ASSERT( mpi_read_string( &X, 16, x ) == 0 );
00763 TEST_ASSERT( mpi_read_string( &Y, 16, y ) == 0 );
00764 TEST_ASSERT( mpi_read_string( &Z, 16, z ) == 0 );
00765
00766 ilen = unhexify( buf, input );
00767
00768 TEST_ASSERT( ecp_tls_read_point( &grp, &P, &vbuf, ilen ) == ret );
00769
00770 if( ret == 0 )
00771 {
00772 TEST_ASSERT( mpi_cmp_mpi( &P.X, &X ) == 0 );
00773 TEST_ASSERT( mpi_cmp_mpi( &P.Y, &Y ) == 0 );
00774 TEST_ASSERT( mpi_cmp_mpi( &P.Z, &Z ) == 0 );
00775 TEST_ASSERT( *vbuf == 0x00 );
00776 }
00777
00778 ecp_group_free( &grp ); ecp_point_free( &P );
00779 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
00780 }
00781
00782 void test_suite_ecp_tls_write_read_point( int id )
00783 {
00784 ecp_group grp;
00785 ecp_point pt;
00786 unsigned char buf[256];
00787 const unsigned char *vbuf;
00788 size_t olen;
00789
00790 ecp_group_init( &grp );
00791 ecp_point_init( &pt );
00792
00793 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
00794
00795 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
00796 TEST_ASSERT( ecp_tls_write_point( &grp, &grp.G,
00797 POLARSSL_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
00798 TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen )
00799 == POLARSSL_ERR_ECP_BAD_INPUT_DATA );
00800 TEST_ASSERT( vbuf == buf + olen );
00801
00802 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
00803 TEST_ASSERT( ecp_tls_write_point( &grp, &grp.G,
00804 POLARSSL_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
00805 TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
00806 TEST_ASSERT( mpi_cmp_mpi( &grp.G.X, &pt.X ) == 0 );
00807 TEST_ASSERT( mpi_cmp_mpi( &grp.G.Y, &pt.Y ) == 0 );
00808 TEST_ASSERT( mpi_cmp_mpi( &grp.G.Z, &pt.Z ) == 0 );
00809 TEST_ASSERT( vbuf == buf + olen );
00810
00811 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
00812 TEST_ASSERT( ecp_set_zero( &pt ) == 0 );
00813 TEST_ASSERT( ecp_tls_write_point( &grp, &pt,
00814 POLARSSL_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
00815 TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
00816 TEST_ASSERT( ecp_is_zero( &pt ) );
00817 TEST_ASSERT( vbuf == buf + olen );
00818
00819 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
00820 TEST_ASSERT( ecp_set_zero( &pt ) == 0 );
00821 TEST_ASSERT( ecp_tls_write_point( &grp, &pt,
00822 POLARSSL_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
00823 TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
00824 TEST_ASSERT( ecp_is_zero( &pt ) );
00825 TEST_ASSERT( vbuf == buf + olen );
00826
00827 ecp_group_free( &grp );
00828 ecp_point_free( &pt );
00829 }
00830
00831 void test_suite_ecp_tls_read_group( char *record, int result, int bits )
00832 {
00833 ecp_group grp;
00834 unsigned char buf[10];
00835 const unsigned char *vbuf = buf;
00836 int len, ret;
00837
00838 ecp_group_init( &grp );
00839 memset( buf, 0x00, sizeof( buf ) );
00840
00841 len = unhexify( buf, record );
00842
00843 ret = ecp_tls_read_group( &grp, &vbuf, len );
00844
00845 TEST_ASSERT( ret == result );
00846 if( ret == 0)
00847 {
00848 TEST_ASSERT( mpi_msb( &grp.P ) == (size_t) bits );
00849 TEST_ASSERT( *vbuf == 0x00 );
00850 }
00851
00852 ecp_group_free( &grp );
00853 }
00854
00855 void test_suite_ecp_tls_write_read_group( int id )
00856 {
00857 ecp_group grp1, grp2;
00858 unsigned char buf[10];
00859 const unsigned char *vbuf = buf;
00860 size_t len;
00861 int ret;
00862
00863 ecp_group_init( &grp1 );
00864 ecp_group_init( &grp2 );
00865 memset( buf, 0x00, sizeof( buf ) );
00866
00867 TEST_ASSERT( ecp_use_known_dp( &grp1, id ) == 0 );
00868
00869 TEST_ASSERT( ecp_tls_write_group( &grp1, &len, buf, 10 ) == 0 );
00870 TEST_ASSERT( ( ret = ecp_tls_read_group( &grp2, &vbuf, len ) ) == 0 );
00871
00872 if( ret == 0 )
00873 {
00874 TEST_ASSERT( mpi_cmp_mpi( &grp1.N, &grp2.N ) == 0 );
00875 TEST_ASSERT( grp1.id == grp2.id );
00876 }
00877
00878 ecp_group_free( &grp1 );
00879 ecp_group_free( &grp2 );
00880 }
00881
00882 void test_suite_ecp_check_privkey( int id )
00883 {
00884 ecp_group grp;
00885 mpi d;
00886
00887 ecp_group_init( &grp );
00888 mpi_init( &d );
00889
00890 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
00891
00892 TEST_ASSERT( mpi_lset( &d, 0 ) == 0 );
00893 TEST_ASSERT( ecp_check_privkey( &grp, &d ) == POLARSSL_ERR_ECP_INVALID_KEY );
00894
00895 TEST_ASSERT( mpi_copy( &d, &grp.N ) == 0 );
00896 TEST_ASSERT( ecp_check_privkey( &grp, &d ) == POLARSSL_ERR_ECP_INVALID_KEY );
00897
00898 ecp_group_free( &grp );
00899 mpi_free( &d );
00900 }
00901
00902 void test_suite_ecp_gen_keypair( int id )
00903 {
00904 ecp_group grp;
00905 ecp_point Q;
00906 mpi d;
00907 rnd_pseudo_info rnd_info;
00908
00909 ecp_group_init( &grp );
00910 ecp_point_init( &Q );
00911 mpi_init( &d );
00912 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
00913
00914 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
00915
00916 TEST_ASSERT( ecp_gen_keypair( &grp, &d, &Q, &rnd_pseudo_rand, &rnd_info )
00917 == 0 );
00918
00919 TEST_ASSERT( ecp_check_pubkey( &grp, &Q ) == 0 );
00920 TEST_ASSERT( ecp_check_privkey( &grp, &d ) == 0 );
00921
00922 ecp_group_free( &grp );
00923 ecp_point_free( &Q );
00924 mpi_free( &d );
00925 }
00926
00927 #ifdef POLARSSL_SELF_TEST
00928 void test_suite_ecp_selftest()
00929 {
00930 TEST_ASSERT( ecp_self_test( 0 ) == 0 );
00931 }
00932 #endif
00933
00934
00935 #endif
00936
00937
00938 int dep_check( char *str )
00939 {
00940 if( str == NULL )
00941 return( 1 );
00942
00943 if( strcmp( str, "POLARSSL_ECP_DP_SECP384R1_ENABLED" ) == 0 )
00944 {
00945 #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
00946 return( 0 );
00947 #else
00948 return( 1 );
00949 #endif
00950 }
00951 if( strcmp( str, "POLARSSL_ECP_DP_BP256R1_ENABLED" ) == 0 )
00952 {
00953 #if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
00954 return( 0 );
00955 #else
00956 return( 1 );
00957 #endif
00958 }
00959 if( strcmp( str, "POLARSSL_ECP_DP_BP384R1_ENABLED" ) == 0 )
00960 {
00961 #if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
00962 return( 0 );
00963 #else
00964 return( 1 );
00965 #endif
00966 }
00967 if( strcmp( str, "POLARSSL_ECP_DP_BP512R1_ENABLED" ) == 0 )
00968 {
00969 #if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
00970 return( 0 );
00971 #else
00972 return( 1 );
00973 #endif
00974 }
00975 if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
00976 {
00977 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
00978 return( 0 );
00979 #else
00980 return( 1 );
00981 #endif
00982 }
00983 if( strcmp( str, "POLARSSL_ECP_DP_SECP521R1_ENABLED" ) == 0 )
00984 {
00985 #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
00986 return( 0 );
00987 #else
00988 return( 1 );
00989 #endif
00990 }
00991 if( strcmp( str, "POLARSSL_ECP_DP_SECP224R1_ENABLED" ) == 0 )
00992 {
00993 #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
00994 return( 0 );
00995 #else
00996 return( 1 );
00997 #endif
00998 }
00999 if( strcmp( str, "POLARSSL_ECP_DP_SECP256R1_ENABLED" ) == 0 )
01000 {
01001 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
01002 return( 0 );
01003 #else
01004 return( 1 );
01005 #endif
01006 }
01007
01008
01009 return( 1 );
01010 }
01011
01012 int dispatch_test(int cnt, char *params[50])
01013 {
01014 int ret;
01015 ((void) cnt);
01016 ((void) params);
01017
01018 #if defined(TEST_SUITE_ACTIVE)
01019 if( strcmp( params[0], "ecp_small_add" ) == 0 )
01020 {
01021
01022 int param1;
01023 char *param2 = params[2];
01024 char *param3 = params[3];
01025 int param4;
01026 char *param5 = params[5];
01027 char *param6 = params[6];
01028 int param7;
01029 int param8;
01030 int param9;
01031
01032 if( cnt != 10 )
01033 {
01034 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 10 );
01035 return( 2 );
01036 }
01037
01038 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01039 if( verify_string( ¶m2 ) != 0 ) return( 2 );
01040 if( verify_string( ¶m3 ) != 0 ) return( 2 );
01041 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
01042 if( verify_string( ¶m5 ) != 0 ) return( 2 );
01043 if( verify_string( ¶m6 ) != 0 ) return( 2 );
01044 if( verify_int( params[7], ¶m7 ) != 0 ) return( 2 );
01045 if( verify_int( params[8], ¶m8 ) != 0 ) return( 2 );
01046 if( verify_int( params[9], ¶m9 ) != 0 ) return( 2 );
01047
01048 test_suite_ecp_small_add( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
01049 return ( 0 );
01050
01051 return ( 3 );
01052 }
01053 else
01054 if( strcmp( params[0], "ecp_small_sub" ) == 0 )
01055 {
01056
01057 int param1;
01058 char *param2 = params[2];
01059 char *param3 = params[3];
01060 int param4;
01061 char *param5 = params[5];
01062 char *param6 = params[6];
01063 int param7;
01064 int param8;
01065 int param9;
01066
01067 if( cnt != 10 )
01068 {
01069 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 10 );
01070 return( 2 );
01071 }
01072
01073 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01074 if( verify_string( ¶m2 ) != 0 ) return( 2 );
01075 if( verify_string( ¶m3 ) != 0 ) return( 2 );
01076 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
01077 if( verify_string( ¶m5 ) != 0 ) return( 2 );
01078 if( verify_string( ¶m6 ) != 0 ) return( 2 );
01079 if( verify_int( params[7], ¶m7 ) != 0 ) return( 2 );
01080 if( verify_int( params[8], ¶m8 ) != 0 ) return( 2 );
01081 if( verify_int( params[9], ¶m9 ) != 0 ) return( 2 );
01082
01083 test_suite_ecp_small_sub( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
01084 return ( 0 );
01085
01086 return ( 3 );
01087 }
01088 else
01089 if( strcmp( params[0], "ecp_small_mul" ) == 0 )
01090 {
01091
01092 int param1;
01093 int param2;
01094 int param3;
01095 int param4;
01096 int param5;
01097
01098 if( cnt != 6 )
01099 {
01100 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
01101 return( 2 );
01102 }
01103
01104 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01105 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
01106 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
01107 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
01108 if( verify_int( params[5], ¶m5 ) != 0 ) return( 2 );
01109
01110 test_suite_ecp_small_mul( param1, param2, param3, param4, param5 );
01111 return ( 0 );
01112
01113 return ( 3 );
01114 }
01115 else
01116 if( strcmp( params[0], "ecp_small_check_pub" ) == 0 )
01117 {
01118
01119 int param1;
01120 int param2;
01121 int param3;
01122 int param4;
01123
01124 if( cnt != 5 )
01125 {
01126 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
01127 return( 2 );
01128 }
01129
01130 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01131 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
01132 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
01133 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
01134
01135 test_suite_ecp_small_check_pub( param1, param2, param3, param4 );
01136 return ( 0 );
01137
01138 return ( 3 );
01139 }
01140 else
01141 if( strcmp( params[0], "ecp_test_vect" ) == 0 )
01142 {
01143
01144 int param1;
01145 char *param2 = params[2];
01146 char *param3 = params[3];
01147 char *param4 = params[4];
01148 char *param5 = params[5];
01149 char *param6 = params[6];
01150 char *param7 = params[7];
01151 char *param8 = params[8];
01152 char *param9 = params[9];
01153
01154 if( cnt != 10 )
01155 {
01156 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 10 );
01157 return( 2 );
01158 }
01159
01160 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01161 if( verify_string( ¶m2 ) != 0 ) return( 2 );
01162 if( verify_string( ¶m3 ) != 0 ) return( 2 );
01163 if( verify_string( ¶m4 ) != 0 ) return( 2 );
01164 if( verify_string( ¶m5 ) != 0 ) return( 2 );
01165 if( verify_string( ¶m6 ) != 0 ) return( 2 );
01166 if( verify_string( ¶m7 ) != 0 ) return( 2 );
01167 if( verify_string( ¶m8 ) != 0 ) return( 2 );
01168 if( verify_string( ¶m9 ) != 0 ) return( 2 );
01169
01170 test_suite_ecp_test_vect( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
01171 return ( 0 );
01172
01173 return ( 3 );
01174 }
01175 else
01176 if( strcmp( params[0], "ecp_fast_mod" ) == 0 )
01177 {
01178
01179 int param1;
01180 char *param2 = params[2];
01181
01182 if( cnt != 3 )
01183 {
01184 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
01185 return( 2 );
01186 }
01187
01188 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01189 if( verify_string( ¶m2 ) != 0 ) return( 2 );
01190
01191 test_suite_ecp_fast_mod( param1, param2 );
01192 return ( 0 );
01193
01194 return ( 3 );
01195 }
01196 else
01197 if( strcmp( params[0], "ecp_write_binary" ) == 0 )
01198 {
01199
01200 int param1;
01201 char *param2 = params[2];
01202 char *param3 = params[3];
01203 char *param4 = params[4];
01204 int param5;
01205 char *param6 = params[6];
01206 int param7;
01207 int param8;
01208
01209 if( cnt != 9 )
01210 {
01211 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 9 );
01212 return( 2 );
01213 }
01214
01215 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01216 if( verify_string( ¶m2 ) != 0 ) return( 2 );
01217 if( verify_string( ¶m3 ) != 0 ) return( 2 );
01218 if( verify_string( ¶m4 ) != 0 ) return( 2 );
01219 if( verify_int( params[5], ¶m5 ) != 0 ) return( 2 );
01220 if( verify_string( ¶m6 ) != 0 ) return( 2 );
01221 if( verify_int( params[7], ¶m7 ) != 0 ) return( 2 );
01222 if( verify_int( params[8], ¶m8 ) != 0 ) return( 2 );
01223
01224 test_suite_ecp_write_binary( param1, param2, param3, param4, param5, param6, param7, param8 );
01225 return ( 0 );
01226
01227 return ( 3 );
01228 }
01229 else
01230 if( strcmp( params[0], "ecp_read_binary" ) == 0 )
01231 {
01232
01233 int param1;
01234 char *param2 = params[2];
01235 char *param3 = params[3];
01236 char *param4 = params[4];
01237 char *param5 = params[5];
01238 int param6;
01239
01240 if( cnt != 7 )
01241 {
01242 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
01243 return( 2 );
01244 }
01245
01246 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01247 if( verify_string( ¶m2 ) != 0 ) return( 2 );
01248 if( verify_string( ¶m3 ) != 0 ) return( 2 );
01249 if( verify_string( ¶m4 ) != 0 ) return( 2 );
01250 if( verify_string( ¶m5 ) != 0 ) return( 2 );
01251 if( verify_int( params[6], ¶m6 ) != 0 ) return( 2 );
01252
01253 test_suite_ecp_read_binary( param1, param2, param3, param4, param5, param6 );
01254 return ( 0 );
01255
01256 return ( 3 );
01257 }
01258 else
01259 if( strcmp( params[0], "ecp_tls_read_point" ) == 0 )
01260 {
01261
01262 int param1;
01263 char *param2 = params[2];
01264 char *param3 = params[3];
01265 char *param4 = params[4];
01266 char *param5 = params[5];
01267 int param6;
01268
01269 if( cnt != 7 )
01270 {
01271 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
01272 return( 2 );
01273 }
01274
01275 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01276 if( verify_string( ¶m2 ) != 0 ) return( 2 );
01277 if( verify_string( ¶m3 ) != 0 ) return( 2 );
01278 if( verify_string( ¶m4 ) != 0 ) return( 2 );
01279 if( verify_string( ¶m5 ) != 0 ) return( 2 );
01280 if( verify_int( params[6], ¶m6 ) != 0 ) return( 2 );
01281
01282 test_suite_ecp_tls_read_point( param1, param2, param3, param4, param5, param6 );
01283 return ( 0 );
01284
01285 return ( 3 );
01286 }
01287 else
01288 if( strcmp( params[0], "ecp_tls_write_read_point" ) == 0 )
01289 {
01290
01291 int param1;
01292
01293 if( cnt != 2 )
01294 {
01295 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
01296 return( 2 );
01297 }
01298
01299 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01300
01301 test_suite_ecp_tls_write_read_point( param1 );
01302 return ( 0 );
01303
01304 return ( 3 );
01305 }
01306 else
01307 if( strcmp( params[0], "ecp_tls_read_group" ) == 0 )
01308 {
01309
01310 char *param1 = params[1];
01311 int param2;
01312 int param3;
01313
01314 if( cnt != 4 )
01315 {
01316 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
01317 return( 2 );
01318 }
01319
01320 if( verify_string( ¶m1 ) != 0 ) return( 2 );
01321 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
01322 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
01323
01324 test_suite_ecp_tls_read_group( param1, param2, param3 );
01325 return ( 0 );
01326
01327 return ( 3 );
01328 }
01329 else
01330 if( strcmp( params[0], "ecp_tls_write_read_group" ) == 0 )
01331 {
01332
01333 int param1;
01334
01335 if( cnt != 2 )
01336 {
01337 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
01338 return( 2 );
01339 }
01340
01341 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01342
01343 test_suite_ecp_tls_write_read_group( param1 );
01344 return ( 0 );
01345
01346 return ( 3 );
01347 }
01348 else
01349 if( strcmp( params[0], "ecp_check_privkey" ) == 0 )
01350 {
01351
01352 int param1;
01353
01354 if( cnt != 2 )
01355 {
01356 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
01357 return( 2 );
01358 }
01359
01360 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01361
01362 test_suite_ecp_check_privkey( param1 );
01363 return ( 0 );
01364
01365 return ( 3 );
01366 }
01367 else
01368 if( strcmp( params[0], "ecp_gen_keypair" ) == 0 )
01369 {
01370
01371 int param1;
01372
01373 if( cnt != 2 )
01374 {
01375 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
01376 return( 2 );
01377 }
01378
01379 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
01380
01381 test_suite_ecp_gen_keypair( param1 );
01382 return ( 0 );
01383
01384 return ( 3 );
01385 }
01386 else
01387 if( strcmp( params[0], "ecp_selftest" ) == 0 )
01388 {
01389 #ifdef POLARSSL_SELF_TEST
01390
01391
01392 if( cnt != 1 )
01393 {
01394 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
01395 return( 2 );
01396 }
01397
01398
01399 test_suite_ecp_selftest( );
01400 return ( 0 );
01401 #endif
01402
01403 return ( 3 );
01404 }
01405 else
01406
01407 {
01408 fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
01409 fflush( stdout );
01410 return( 1 );
01411 }
01412 #else
01413 return( 3 );
01414 #endif
01415 return( ret );
01416 }
01417
01418 int get_line( FILE *f, char *buf, size_t len )
01419 {
01420 char *ret;
01421
01422 ret = fgets( buf, len, f );
01423 if( ret == NULL )
01424 return( -1 );
01425
01426 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
01427 buf[strlen(buf) - 1] = '\0';
01428 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
01429 buf[strlen(buf) - 1] = '\0';
01430
01431 return( 0 );
01432 }
01433
01434 int parse_arguments( char *buf, size_t len, char *params[50] )
01435 {
01436 int cnt = 0, i;
01437 char *cur = buf;
01438 char *p = buf, *q;
01439
01440 params[cnt++] = cur;
01441
01442 while( *p != '\0' && p < buf + len )
01443 {
01444 if( *p == '\\' )
01445 {
01446 *p++;
01447 *p++;
01448 continue;
01449 }
01450 if( *p == ':' )
01451 {
01452 if( p + 1 < buf + len )
01453 {
01454 cur = p + 1;
01455 params[cnt++] = cur;
01456 }
01457 *p = '\0';
01458 }
01459
01460 *p++;
01461 }
01462
01463
01464 for( i = 0; i < cnt; i++ )
01465 {
01466 p = params[i];
01467 q = params[i];
01468
01469 while( *p != '\0' )
01470 {
01471 if( *p == '\\' && *(p + 1) == 'n' )
01472 {
01473 p += 2;
01474 *(q++) = '\n';
01475 }
01476 else if( *p == '\\' && *(p + 1) == ':' )
01477 {
01478 p += 2;
01479 *(q++) = ':';
01480 }
01481 else if( *p == '\\' && *(p + 1) == '?' )
01482 {
01483 p += 2;
01484 *(q++) = '?';
01485 }
01486 else
01487 *(q++) = *(p++);
01488 }
01489 *q = '\0';
01490 }
01491
01492 return( cnt );
01493 }
01494
01495 int main()
01496 {
01497 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
01498 const char *filename = "/home/abuild/rpmbuild/BUILD/polarssl-1.3.2/tests/suites/test_suite_ecp.data";
01499 FILE *file;
01500 char buf[5000];
01501 char *params[50];
01502
01503 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
01504 unsigned char alloc_buf[1000000];
01505 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
01506 #endif
01507
01508 file = fopen( filename, "r" );
01509 if( file == NULL )
01510 {
01511 fprintf( stderr, "Failed to open\n" );
01512 return( 1 );
01513 }
01514
01515 while( !feof( file ) )
01516 {
01517 int skip = 0;
01518
01519 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01520 break;
01521 fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
01522 fprintf( stdout, " " );
01523 for( i = strlen( buf ) + 1; i < 67; i++ )
01524 fprintf( stdout, "." );
01525 fprintf( stdout, " " );
01526 fflush( stdout );
01527
01528 total_tests++;
01529
01530 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01531 break;
01532 cnt = parse_arguments( buf, strlen(buf), params );
01533
01534 if( strcmp( params[0], "depends_on" ) == 0 )
01535 {
01536 for( i = 1; i < cnt; i++ )
01537 if( dep_check( params[i] ) != 0 )
01538 skip = 1;
01539
01540 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01541 break;
01542 cnt = parse_arguments( buf, strlen(buf), params );
01543 }
01544
01545 if( skip == 0 )
01546 {
01547 test_errors = 0;
01548 ret = dispatch_test( cnt, params );
01549 }
01550
01551 if( skip == 1 || ret == 3 )
01552 {
01553 total_skipped++;
01554 fprintf( stdout, "----\n" );
01555 fflush( stdout );
01556 }
01557 else if( ret == 0 && test_errors == 0 )
01558 {
01559 fprintf( stdout, "PASS\n" );
01560 fflush( stdout );
01561 }
01562 else if( ret == 2 )
01563 {
01564 fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
01565 fclose(file);
01566 exit( 2 );
01567 }
01568 else
01569 total_errors++;
01570
01571 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01572 break;
01573 if( strlen(buf) != 0 )
01574 {
01575 fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
01576 return( 1 );
01577 }
01578 }
01579 fclose(file);
01580
01581 fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
01582 if( total_errors == 0 )
01583 fprintf( stdout, "PASSED" );
01584 else
01585 fprintf( stdout, "FAILED" );
01586
01587 fprintf( stdout, " (%d / %d tests (%d skipped))\n",
01588 total_tests - total_errors, total_tests, total_skipped );
01589
01590 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
01591 #if defined(POLARSSL_MEMORY_DEBUG)
01592 memory_buffer_alloc_status();
01593 #endif
01594 memory_buffer_alloc_free();
01595 #endif
01596
01597 return( total_errors != 0 );
01598 }
01599
01600