00001 #include <polarssl/config.h>
00002
00003 #ifdef POLARSSL_PKCS1_V21
00004 #ifdef POLARSSL_RSA_C
00005 #ifdef POLARSSL_BIGNUM_C
00006 #ifdef POLARSSL_SHA1_C
00007 #ifdef POLARSSL_GENPRIME
00008
00009 #include <polarssl/rsa.h>
00010 #include <polarssl/md.h>
00011 #include <polarssl/md2.h>
00012 #include <polarssl/md4.h>
00013 #include <polarssl/md5.h>
00014 #include <polarssl/sha1.h>
00015 #include <polarssl/sha256.h>
00016 #include <polarssl/sha512.h>
00017 #endif
00018 #endif
00019 #endif
00020 #endif
00021 #endif
00022
00023
00024 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
00025 #include "polarssl/memory.h"
00026 #endif
00027
00028 #if defined(WANT_NOT_RND_MPI)
00029 #if defined(POLARSSL_BIGNUM_C)
00030 #include "polarssl/bignum.h"
00031 #else
00032 #error "not_rnd_mpi() need bignum.c"
00033 #endif
00034 #endif
00035
00036 #ifdef _MSC_VER
00037 #include <basetsd.h>
00038 typedef UINT32 uint32_t;
00039 #else
00040 #include <inttypes.h>
00041 #endif
00042
00043 #include <assert.h>
00044 #include <stdlib.h>
00045 #include <string.h>
00046
00047
00048
00049
00050 #ifndef GET_UINT32_BE
00051 #define GET_UINT32_BE(n,b,i) \
00052 { \
00053 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
00054 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
00055 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
00056 | ( (uint32_t) (b)[(i) + 3] ); \
00057 }
00058 #endif
00059
00060 #ifndef PUT_UINT32_BE
00061 #define PUT_UINT32_BE(n,b,i) \
00062 { \
00063 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
00064 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
00065 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
00066 (b)[(i) + 3] = (unsigned char) ( (n) ); \
00067 }
00068 #endif
00069
00070 static int unhexify(unsigned char *obuf, const char *ibuf)
00071 {
00072 unsigned char c, c2;
00073 int len = strlen(ibuf) / 2;
00074 assert(!(strlen(ibuf) %1));
00075
00076 while (*ibuf != 0)
00077 {
00078 c = *ibuf++;
00079 if( c >= '0' && c <= '9' )
00080 c -= '0';
00081 else if( c >= 'a' && c <= 'f' )
00082 c -= 'a' - 10;
00083 else if( c >= 'A' && c <= 'F' )
00084 c -= 'A' - 10;
00085 else
00086 assert( 0 );
00087
00088 c2 = *ibuf++;
00089 if( c2 >= '0' && c2 <= '9' )
00090 c2 -= '0';
00091 else if( c2 >= 'a' && c2 <= 'f' )
00092 c2 -= 'a' - 10;
00093 else if( c2 >= 'A' && c2 <= 'F' )
00094 c2 -= 'A' - 10;
00095 else
00096 assert( 0 );
00097
00098 *obuf++ = ( c << 4 ) | c2;
00099 }
00100
00101 return len;
00102 }
00103
00104 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
00105 {
00106 unsigned char l, h;
00107
00108 while (len != 0)
00109 {
00110 h = (*ibuf) / 16;
00111 l = (*ibuf) % 16;
00112
00113 if( h < 10 )
00114 *obuf++ = '0' + h;
00115 else
00116 *obuf++ = 'a' + h - 10;
00117
00118 if( l < 10 )
00119 *obuf++ = '0' + l;
00120 else
00121 *obuf++ = 'a' + l - 10;
00122
00123 ++ibuf;
00124 len--;
00125 }
00126 }
00127
00137 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
00138 {
00139 size_t i;
00140
00141 if( rng_state != NULL )
00142 rng_state = NULL;
00143
00144 for( i = 0; i < len; ++i )
00145 output[i] = rand();
00146
00147 return( 0 );
00148 }
00149
00155 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
00156 {
00157 if( rng_state != NULL )
00158 rng_state = NULL;
00159
00160 memset( output, 0, len );
00161
00162 return( 0 );
00163 }
00164
00165 typedef struct
00166 {
00167 unsigned char *buf;
00168 size_t length;
00169 } rnd_buf_info;
00170
00182 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
00183 {
00184 rnd_buf_info *info = (rnd_buf_info *) rng_state;
00185 size_t use_len;
00186
00187 if( rng_state == NULL )
00188 return( rnd_std_rand( NULL, output, len ) );
00189
00190 use_len = len;
00191 if( len > info->length )
00192 use_len = info->length;
00193
00194 if( use_len )
00195 {
00196 memcpy( output, info->buf, use_len );
00197 info->buf += use_len;
00198 info->length -= use_len;
00199 }
00200
00201 if( len - use_len > 0 )
00202 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
00203
00204 return( 0 );
00205 }
00206
00214 typedef struct
00215 {
00216 uint32_t key[16];
00217 uint32_t v0, v1;
00218 } rnd_pseudo_info;
00219
00228 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
00229 {
00230 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
00231 uint32_t i, *k, sum, delta=0x9E3779B9;
00232 unsigned char result[4];
00233
00234 if( rng_state == NULL )
00235 return( rnd_std_rand( NULL, output, len ) );
00236
00237 k = info->key;
00238
00239 while( len > 0 )
00240 {
00241 size_t use_len = ( len > 4 ) ? 4 : len;
00242 sum = 0;
00243
00244 for( i = 0; i < 32; i++ )
00245 {
00246 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
00247 sum += delta;
00248 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
00249 }
00250
00251 PUT_UINT32_BE( info->v0, result, 0 );
00252 memcpy( output, result, use_len );
00253 len -= use_len;
00254 }
00255
00256 return( 0 );
00257 }
00258
00259 #if defined(WANT_NOT_RND_MPI)
00260
00268 #define ciL (sizeof(t_uint))
00269 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
00270 static int not_rnd_mpi( void *in, unsigned char *out, size_t len )
00271 {
00272 char *str = (char *) in;
00273 mpi X;
00274
00275
00276
00277
00278
00279 X.s = 1;
00280 X.p = (t_uint *) out;
00281 X.n = CHARS_TO_LIMBS( len );
00282
00283
00284
00285
00286
00287 assert( strlen( str ) / 2 == len );
00288
00289 return( mpi_read_string( &X, 16, str ) );
00290 }
00291 #endif
00292
00293
00294 #include <stdio.h>
00295 #include <string.h>
00296
00297 static int test_errors = 0;
00298
00299 #ifdef POLARSSL_PKCS1_V21
00300 #ifdef POLARSSL_RSA_C
00301 #ifdef POLARSSL_BIGNUM_C
00302 #ifdef POLARSSL_SHA1_C
00303 #ifdef POLARSSL_GENPRIME
00304
00305 #define TEST_SUITE_ACTIVE
00306
00307 static int test_assert( int correct, char *test )
00308 {
00309 if( correct )
00310 return( 0 );
00311
00312 test_errors++;
00313 if( test_errors == 1 )
00314 printf( "FAILED\n" );
00315 printf( " %s\n", test );
00316
00317 return( 1 );
00318 }
00319
00320 #define TEST_ASSERT( TEST ) \
00321 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
00322 if( test_errors) return; \
00323 } while (0)
00324
00325 int verify_string( char **str )
00326 {
00327 if( (*str)[0] != '"' ||
00328 (*str)[strlen( *str ) - 1] != '"' )
00329 {
00330 printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
00331 return( -1 );
00332 }
00333
00334 (*str)++;
00335 (*str)[strlen( *str ) - 1] = '\0';
00336
00337 return( 0 );
00338 }
00339
00340 int verify_int( char *str, int *value )
00341 {
00342 size_t i;
00343 int minus = 0;
00344 int digits = 1;
00345 int hex = 0;
00346
00347 for( i = 0; i < strlen( str ); i++ )
00348 {
00349 if( i == 0 && str[i] == '-' )
00350 {
00351 minus = 1;
00352 continue;
00353 }
00354
00355 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
00356 str[i - 1] == '0' && str[i] == 'x' )
00357 {
00358 hex = 1;
00359 continue;
00360 }
00361
00362 if( str[i] < '0' || str[i] > '9' )
00363 {
00364 digits = 0;
00365 break;
00366 }
00367 }
00368
00369 if( digits )
00370 {
00371 if( hex )
00372 *value = strtol( str, NULL, 16 );
00373 else
00374 *value = strtol( str, NULL, 10 );
00375
00376 return( 0 );
00377 }
00378
00379 if( strcmp( str, "POLARSSL_MD_SHA512" ) == 0 )
00380 {
00381 *value = ( POLARSSL_MD_SHA512 );
00382 return( 0 );
00383 }
00384 if( strcmp( str, "POLARSSL_ERR_RSA_BAD_INPUT_DATA" ) == 0 )
00385 {
00386 *value = ( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
00387 return( 0 );
00388 }
00389 if( strcmp( str, "POLARSSL_MD_SHA1" ) == 0 )
00390 {
00391 *value = ( POLARSSL_MD_SHA1 );
00392 return( 0 );
00393 }
00394
00395
00396 printf( "Expected integer for parameter and got: %s\n", str );
00397 return( -1 );
00398 }
00399
00400 void test_suite_pkcs1_rsaes_oaep_encrypt( int mod, int radix_N, char *input_N, int radix_E,
00401 char *input_E, int hash,
00402 char *message_hex_string, char *seed,
00403 char *result_hex_str, int result )
00404 {
00405 unsigned char message_str[1000];
00406 unsigned char output[1000];
00407 unsigned char output_str[1000];
00408 unsigned char rnd_buf[1000];
00409 rsa_context ctx;
00410 size_t msg_len;
00411 rnd_buf_info info;
00412
00413 info.length = unhexify( rnd_buf, seed );
00414 info.buf = rnd_buf;
00415
00416 rsa_init( &ctx, RSA_PKCS_V21, hash );
00417 memset( message_str, 0x00, 1000 );
00418 memset( output, 0x00, 1000 );
00419 memset( output_str, 0x00, 1000 );
00420
00421 ctx.len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 );
00422 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
00423 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
00424
00425 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
00426
00427 msg_len = unhexify( message_str, message_hex_string );
00428
00429 TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, &rnd_buffer_rand, &info, RSA_PUBLIC, msg_len, message_str, output ) == result );
00430 if( result == 0 )
00431 {
00432 hexify( output_str, output, ctx.len );
00433
00434 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
00435 }
00436
00437 rsa_free( &ctx );
00438 }
00439
00440 void test_suite_pkcs1_rsaes_oaep_decrypt( int mod, int radix_P, char *input_P,
00441 int radix_Q, char *input_Q, int radix_N,
00442 char *input_N, int radix_E, char *input_E,
00443 int hash, char *result_hex_str, char *seed,
00444 char *message_hex_string, int result )
00445 {
00446 unsigned char message_str[1000];
00447 unsigned char output[1000];
00448 unsigned char output_str[1000];
00449 rsa_context ctx;
00450 mpi P1, Q1, H, G;
00451 size_t output_len;
00452 rnd_pseudo_info rnd_info;
00453 ((void) seed);
00454
00455 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
00456 rsa_init( &ctx, RSA_PKCS_V21, hash );
00457
00458 memset( message_str, 0x00, 1000 );
00459 memset( output, 0x00, 1000 );
00460 memset( output_str, 0x00, 1000 );
00461 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
00462
00463 ctx.len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 );
00464 TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
00465 TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
00466 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
00467 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
00468
00469 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
00470 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
00471 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
00472 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
00473 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
00474 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
00475 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
00476 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
00477
00478 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
00479
00480 unhexify( message_str, message_hex_string );
00481
00482 TEST_ASSERT( rsa_pkcs1_decrypt( &ctx, &rnd_pseudo_rand, &rnd_info, RSA_PRIVATE, &output_len, message_str, output, 1000 ) == result );
00483 if( result == 0 )
00484 {
00485 hexify( output_str, output, ctx.len );
00486
00487 TEST_ASSERT( strncasecmp( (char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 );
00488 }
00489
00490 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
00491 rsa_free( &ctx );
00492 }
00493
00494 void test_suite_pkcs1_rsassa_pss_sign( int mod, int radix_P, char *input_P, int radix_Q,
00495 char *input_Q, int radix_N, char *input_N,
00496 int radix_E, char *input_E, int digest, int hash,
00497 char *message_hex_string, char *salt,
00498 char *result_hex_str, int result )
00499 {
00500 unsigned char message_str[1000];
00501 unsigned char hash_result[1000];
00502 unsigned char output[1000];
00503 unsigned char output_str[1000];
00504 unsigned char rnd_buf[1000];
00505 rsa_context ctx;
00506 mpi P1, Q1, H, G;
00507 size_t msg_len;
00508 rnd_buf_info info;
00509
00510 info.length = unhexify( rnd_buf, salt );
00511 info.buf = rnd_buf;
00512
00513 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
00514 rsa_init( &ctx, RSA_PKCS_V21, hash );
00515
00516 memset( message_str, 0x00, 1000 );
00517 memset( hash_result, 0x00, 1000 );
00518 memset( output, 0x00, 1000 );
00519 memset( output_str, 0x00, 1000 );
00520
00521 ctx.len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 );
00522 TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
00523 TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
00524 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
00525 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
00526
00527 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
00528 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
00529 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
00530 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
00531 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
00532 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
00533 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
00534 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
00535
00536 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
00537
00538 msg_len = unhexify( message_str, message_hex_string );
00539
00540 if( md_info_from_type( digest ) != NULL )
00541 TEST_ASSERT( md( md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
00542
00543 TEST_ASSERT( rsa_pkcs1_sign( &ctx, &rnd_buffer_rand, &info, RSA_PRIVATE, digest, 0, hash_result, output ) == result );
00544 if( result == 0 )
00545 {
00546 hexify( output_str, output, ctx.len);
00547
00548 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
00549 }
00550
00551 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
00552 rsa_free( &ctx );
00553 }
00554
00555 void test_suite_pkcs1_rsassa_pss_verify( int mod, int radix_N, char *input_N, int radix_E,
00556 char *input_E, int digest, int hash,
00557 char *message_hex_string, char *salt,
00558 char *result_hex_str, int result )
00559 {
00560 unsigned char message_str[1000];
00561 unsigned char hash_result[1000];
00562 unsigned char result_str[1000];
00563 rsa_context ctx;
00564 size_t msg_len;
00565 ((void) salt);
00566
00567 rsa_init( &ctx, RSA_PKCS_V21, hash );
00568 memset( message_str, 0x00, 1000 );
00569 memset( hash_result, 0x00, 1000 );
00570 memset( result_str, 0x00, 1000 );
00571
00572 ctx.len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 );
00573 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
00574 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
00575
00576 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
00577
00578 msg_len = unhexify( message_str, message_hex_string );
00579 unhexify( result_str, result_hex_str );
00580
00581 if( md_info_from_type( digest ) != NULL )
00582 TEST_ASSERT( md( md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
00583
00584 TEST_ASSERT( rsa_pkcs1_verify( &ctx, NULL, NULL, RSA_PUBLIC, digest, 0, hash_result, result_str ) == result );
00585
00586 rsa_free( &ctx );
00587 }
00588
00589
00590 #endif
00591 #endif
00592 #endif
00593 #endif
00594 #endif
00595
00596
00597 int dep_check( char *str )
00598 {
00599 if( str == NULL )
00600 return( 1 );
00601
00602
00603
00604 return( 1 );
00605 }
00606
00607 int dispatch_test(int cnt, char *params[50])
00608 {
00609 int ret;
00610 ((void) cnt);
00611 ((void) params);
00612
00613 #if defined(TEST_SUITE_ACTIVE)
00614 if( strcmp( params[0], "pkcs1_rsaes_oaep_encrypt" ) == 0 )
00615 {
00616
00617 int param1;
00618 int param2;
00619 char *param3 = params[3];
00620 int param4;
00621 char *param5 = params[5];
00622 int param6;
00623 char *param7 = params[7];
00624 char *param8 = params[8];
00625 char *param9 = params[9];
00626 int param10;
00627
00628 if( cnt != 11 )
00629 {
00630 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 11 );
00631 return( 2 );
00632 }
00633
00634 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
00635 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
00636 if( verify_string( ¶m3 ) != 0 ) return( 2 );
00637 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
00638 if( verify_string( ¶m5 ) != 0 ) return( 2 );
00639 if( verify_int( params[6], ¶m6 ) != 0 ) return( 2 );
00640 if( verify_string( ¶m7 ) != 0 ) return( 2 );
00641 if( verify_string( ¶m8 ) != 0 ) return( 2 );
00642 if( verify_string( ¶m9 ) != 0 ) return( 2 );
00643 if( verify_int( params[10], ¶m10 ) != 0 ) return( 2 );
00644
00645 test_suite_pkcs1_rsaes_oaep_encrypt( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
00646 return ( 0 );
00647
00648 return ( 3 );
00649 }
00650 else
00651 if( strcmp( params[0], "pkcs1_rsaes_oaep_decrypt" ) == 0 )
00652 {
00653
00654 int param1;
00655 int param2;
00656 char *param3 = params[3];
00657 int param4;
00658 char *param5 = params[5];
00659 int param6;
00660 char *param7 = params[7];
00661 int param8;
00662 char *param9 = params[9];
00663 int param10;
00664 char *param11 = params[11];
00665 char *param12 = params[12];
00666 char *param13 = params[13];
00667 int param14;
00668
00669 if( cnt != 15 )
00670 {
00671 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 15 );
00672 return( 2 );
00673 }
00674
00675 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
00676 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
00677 if( verify_string( ¶m3 ) != 0 ) return( 2 );
00678 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
00679 if( verify_string( ¶m5 ) != 0 ) return( 2 );
00680 if( verify_int( params[6], ¶m6 ) != 0 ) return( 2 );
00681 if( verify_string( ¶m7 ) != 0 ) return( 2 );
00682 if( verify_int( params[8], ¶m8 ) != 0 ) return( 2 );
00683 if( verify_string( ¶m9 ) != 0 ) return( 2 );
00684 if( verify_int( params[10], ¶m10 ) != 0 ) return( 2 );
00685 if( verify_string( ¶m11 ) != 0 ) return( 2 );
00686 if( verify_string( ¶m12 ) != 0 ) return( 2 );
00687 if( verify_string( ¶m13 ) != 0 ) return( 2 );
00688 if( verify_int( params[14], ¶m14 ) != 0 ) return( 2 );
00689
00690 test_suite_pkcs1_rsaes_oaep_decrypt( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14 );
00691 return ( 0 );
00692
00693 return ( 3 );
00694 }
00695 else
00696 if( strcmp( params[0], "pkcs1_rsassa_pss_sign" ) == 0 )
00697 {
00698
00699 int param1;
00700 int param2;
00701 char *param3 = params[3];
00702 int param4;
00703 char *param5 = params[5];
00704 int param6;
00705 char *param7 = params[7];
00706 int param8;
00707 char *param9 = params[9];
00708 int param10;
00709 int param11;
00710 char *param12 = params[12];
00711 char *param13 = params[13];
00712 char *param14 = params[14];
00713 int param15;
00714
00715 if( cnt != 16 )
00716 {
00717 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 16 );
00718 return( 2 );
00719 }
00720
00721 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
00722 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
00723 if( verify_string( ¶m3 ) != 0 ) return( 2 );
00724 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
00725 if( verify_string( ¶m5 ) != 0 ) return( 2 );
00726 if( verify_int( params[6], ¶m6 ) != 0 ) return( 2 );
00727 if( verify_string( ¶m7 ) != 0 ) return( 2 );
00728 if( verify_int( params[8], ¶m8 ) != 0 ) return( 2 );
00729 if( verify_string( ¶m9 ) != 0 ) return( 2 );
00730 if( verify_int( params[10], ¶m10 ) != 0 ) return( 2 );
00731 if( verify_int( params[11], ¶m11 ) != 0 ) return( 2 );
00732 if( verify_string( ¶m12 ) != 0 ) return( 2 );
00733 if( verify_string( ¶m13 ) != 0 ) return( 2 );
00734 if( verify_string( ¶m14 ) != 0 ) return( 2 );
00735 if( verify_int( params[15], ¶m15 ) != 0 ) return( 2 );
00736
00737 test_suite_pkcs1_rsassa_pss_sign( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, param15 );
00738 return ( 0 );
00739
00740 return ( 3 );
00741 }
00742 else
00743 if( strcmp( params[0], "pkcs1_rsassa_pss_verify" ) == 0 )
00744 {
00745
00746 int param1;
00747 int param2;
00748 char *param3 = params[3];
00749 int param4;
00750 char *param5 = params[5];
00751 int param6;
00752 int param7;
00753 char *param8 = params[8];
00754 char *param9 = params[9];
00755 char *param10 = params[10];
00756 int param11;
00757
00758 if( cnt != 12 )
00759 {
00760 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 12 );
00761 return( 2 );
00762 }
00763
00764 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
00765 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
00766 if( verify_string( ¶m3 ) != 0 ) return( 2 );
00767 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
00768 if( verify_string( ¶m5 ) != 0 ) return( 2 );
00769 if( verify_int( params[6], ¶m6 ) != 0 ) return( 2 );
00770 if( verify_int( params[7], ¶m7 ) != 0 ) return( 2 );
00771 if( verify_string( ¶m8 ) != 0 ) return( 2 );
00772 if( verify_string( ¶m9 ) != 0 ) return( 2 );
00773 if( verify_string( ¶m10 ) != 0 ) return( 2 );
00774 if( verify_int( params[11], ¶m11 ) != 0 ) return( 2 );
00775
00776 test_suite_pkcs1_rsassa_pss_verify( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11 );
00777 return ( 0 );
00778
00779 return ( 3 );
00780 }
00781 else
00782
00783 {
00784 fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
00785 fflush( stdout );
00786 return( 1 );
00787 }
00788 #else
00789 return( 3 );
00790 #endif
00791 return( ret );
00792 }
00793
00794 int get_line( FILE *f, char *buf, size_t len )
00795 {
00796 char *ret;
00797
00798 ret = fgets( buf, len, f );
00799 if( ret == NULL )
00800 return( -1 );
00801
00802 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
00803 buf[strlen(buf) - 1] = '\0';
00804 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
00805 buf[strlen(buf) - 1] = '\0';
00806
00807 return( 0 );
00808 }
00809
00810 int parse_arguments( char *buf, size_t len, char *params[50] )
00811 {
00812 int cnt = 0, i;
00813 char *cur = buf;
00814 char *p = buf, *q;
00815
00816 params[cnt++] = cur;
00817
00818 while( *p != '\0' && p < buf + len )
00819 {
00820 if( *p == '\\' )
00821 {
00822 *p++;
00823 *p++;
00824 continue;
00825 }
00826 if( *p == ':' )
00827 {
00828 if( p + 1 < buf + len )
00829 {
00830 cur = p + 1;
00831 params[cnt++] = cur;
00832 }
00833 *p = '\0';
00834 }
00835
00836 *p++;
00837 }
00838
00839
00840 for( i = 0; i < cnt; i++ )
00841 {
00842 p = params[i];
00843 q = params[i];
00844
00845 while( *p != '\0' )
00846 {
00847 if( *p == '\\' && *(p + 1) == 'n' )
00848 {
00849 p += 2;
00850 *(q++) = '\n';
00851 }
00852 else if( *p == '\\' && *(p + 1) == ':' )
00853 {
00854 p += 2;
00855 *(q++) = ':';
00856 }
00857 else if( *p == '\\' && *(p + 1) == '?' )
00858 {
00859 p += 2;
00860 *(q++) = '?';
00861 }
00862 else
00863 *(q++) = *(p++);
00864 }
00865 *q = '\0';
00866 }
00867
00868 return( cnt );
00869 }
00870
00871 int main()
00872 {
00873 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
00874 const char *filename = "/home/abuild/rpmbuild/BUILD/polarssl-1.3.2/tests/suites/test_suite_pkcs1_v21.data";
00875 FILE *file;
00876 char buf[5000];
00877 char *params[50];
00878
00879 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
00880 unsigned char alloc_buf[1000000];
00881 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
00882 #endif
00883
00884 file = fopen( filename, "r" );
00885 if( file == NULL )
00886 {
00887 fprintf( stderr, "Failed to open\n" );
00888 return( 1 );
00889 }
00890
00891 while( !feof( file ) )
00892 {
00893 int skip = 0;
00894
00895 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00896 break;
00897 fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
00898 fprintf( stdout, " " );
00899 for( i = strlen( buf ) + 1; i < 67; i++ )
00900 fprintf( stdout, "." );
00901 fprintf( stdout, " " );
00902 fflush( stdout );
00903
00904 total_tests++;
00905
00906 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00907 break;
00908 cnt = parse_arguments( buf, strlen(buf), params );
00909
00910 if( strcmp( params[0], "depends_on" ) == 0 )
00911 {
00912 for( i = 1; i < cnt; i++ )
00913 if( dep_check( params[i] ) != 0 )
00914 skip = 1;
00915
00916 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00917 break;
00918 cnt = parse_arguments( buf, strlen(buf), params );
00919 }
00920
00921 if( skip == 0 )
00922 {
00923 test_errors = 0;
00924 ret = dispatch_test( cnt, params );
00925 }
00926
00927 if( skip == 1 || ret == 3 )
00928 {
00929 total_skipped++;
00930 fprintf( stdout, "----\n" );
00931 fflush( stdout );
00932 }
00933 else if( ret == 0 && test_errors == 0 )
00934 {
00935 fprintf( stdout, "PASS\n" );
00936 fflush( stdout );
00937 }
00938 else if( ret == 2 )
00939 {
00940 fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
00941 fclose(file);
00942 exit( 2 );
00943 }
00944 else
00945 total_errors++;
00946
00947 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00948 break;
00949 if( strlen(buf) != 0 )
00950 {
00951 fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
00952 return( 1 );
00953 }
00954 }
00955 fclose(file);
00956
00957 fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
00958 if( total_errors == 0 )
00959 fprintf( stdout, "PASSED" );
00960 else
00961 fprintf( stdout, "FAILED" );
00962
00963 fprintf( stdout, " (%d / %d tests (%d skipped))\n",
00964 total_tests - total_errors, total_tests, total_skipped );
00965
00966 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
00967 #if defined(POLARSSL_MEMORY_DEBUG)
00968 memory_buffer_alloc_status();
00969 #endif
00970 memory_buffer_alloc_free();
00971 #endif
00972
00973 return( total_errors != 0 );
00974 }
00975
00976