3 #ifdef POLARSSL_PK_PARSE_C
4 #ifdef POLARSSL_BIGNUM_C
13 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
19 typedef UINT32 uint32_t;
32 #define GET_UINT32_BE(n,b,i) \
34 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
35 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
36 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
37 | ( (uint32_t) (b)[(i) + 3] ); \
42 #define PUT_UINT32_BE(n,b,i) \
44 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
45 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
46 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
47 (b)[(i) + 3] = (unsigned char) ( (n) ); \
51 static int unhexify(
unsigned char *obuf,
const char *ibuf)
54 int len = strlen(ibuf) / 2;
55 assert(!(strlen(ibuf) %1));
60 if( c >=
'0' && c <=
'9' )
62 else if( c >=
'a' && c <=
'f' )
64 else if( c >=
'A' && c <=
'F' )
70 if( c2 >=
'0' && c2 <=
'9' )
72 else if( c2 >=
'a' && c2 <=
'f' )
74 else if( c2 >=
'A' && c2 <=
'F' )
79 *obuf++ = ( c << 4 ) | c2;
85 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
97 *obuf++ =
'a' + h - 10;
102 *obuf++ =
'a' + l - 10;
118 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
122 if( rng_state != NULL )
125 for( i = 0; i < len; ++i )
136 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
138 if( rng_state != NULL )
141 memset( output, 0, len );
168 if( rng_state == NULL )
177 memcpy( output, info->
buf, use_len );
178 info->
buf += use_len;
182 if( len - use_len > 0 )
183 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
212 uint32_t i, *k, sum, delta=0x9E3779B9;
213 unsigned char result[4];
215 if( rng_state == NULL )
222 size_t use_len = ( len > 4 ) ? 4 : len;
225 for( i = 0; i < 32; i++ )
227 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
229 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
233 memcpy( output, result, use_len );
249 static int not_rnd(
void *in,
unsigned char *out,
size_t len )
252 const char *ibuf = in;
254 assert( len == strlen(ibuf) / 2 );
255 assert(!(strlen(ibuf) %1));
257 obuf = out + (len - 1);
261 if( c >=
'0' && c <=
'9' )
263 else if( c >=
'a' && c <=
'f' )
265 else if( c >=
'A' && c <=
'F' )
271 if( c2 >=
'0' && c2 <=
'9' )
273 else if( c2 >=
'a' && c2 <=
'f' )
275 else if( c2 >=
'A' && c2 <=
'F' )
280 *obuf-- = ( c << 4 ) | c2;
292 #ifdef POLARSSL_PK_PARSE_C
293 #ifdef POLARSSL_BIGNUM_C
295 #define TEST_SUITE_ACTIVE
304 printf(
"FAILED\n" );
305 printf(
" %s\n", test );
310 #define TEST_ASSERT( TEST ) \
311 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
312 if( test_errors) return; \
317 if( (*str)[0] !=
'"' ||
318 (*str)[strlen( *str ) - 1] !=
'"' )
320 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
325 (*str)[strlen( *str ) - 1] =
'\0';
337 for( i = 0; i < strlen( str ); i++ )
339 if( i == 0 && str[i] ==
'-' )
345 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
346 str[i - 1] ==
'0' && str[i] ==
'x' )
352 if( str[i] <
'0' || str[i] >
'9' )
362 *value = strtol( str, NULL, 16 );
364 *value = strtol( str, NULL, 10 );
369 if( strcmp( str,
"POLARSSL_ERR_PK_PASSWORD_MISMATCH" ) == 0 )
374 if( strcmp( str,
"POLARSSL_ERR_PK_KEY_INVALID_FORMAT" ) == 0 )
379 if( strcmp( str,
"POLARSSL_ERR_PK_PASSWORD_REQUIRED" ) == 0 )
386 printf(
"Expected integer for parameter and got: %s\n", str );
390 #ifdef POLARSSL_RSA_C
391 #ifdef POLARSSL_FS_IO
392 void test_suite_pk_parse_keyfile_rsa(
char *key_file,
char *password,
int result )
396 char *pwd = password;
400 if( strcmp( pwd,
"NULL" ) == 0 )
420 #ifdef POLARSSL_RSA_C
421 #ifdef POLARSSL_FS_IO
422 void test_suite_pk_parse_public_keyfile_rsa(
char *key_file,
int result )
446 #ifdef POLARSSL_FS_IO
447 #ifdef POLARSSL_ECP_C
448 void test_suite_pk_parse_public_keyfile_ec(
char *key_file,
int result )
463 eckey =
pk_ec( ctx );
472 #ifdef POLARSSL_FS_IO
473 #ifdef POLARSSL_ECP_C
474 void test_suite_pk_parse_keyfile_ec(
char *key_file,
char *password,
int result )
489 eckey =
pk_ec( ctx );
498 #ifdef POLARSSL_RSA_C
499 void test_suite_pk_parse_key_rsa(
char *key_data,
char *result_str,
int result )
502 unsigned char buf[2000];
503 unsigned char output[2000];
509 memset( buf, 0, 2000 );
510 memset( output, 0, 2000 );
512 data_len =
unhexify( buf, key_data );
515 if( ( result ) == 0 )
534 if( strcmp( str,
"POLARSSL_ECP_DP_SECP384R1_ENABLED" ) == 0 )
536 #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
542 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
544 #if defined(POLARSSL_SHA1_C)
550 if( strcmp( str,
"POLARSSL_ARC4_C" ) == 0 )
552 #if defined(POLARSSL_ARC4_C)
558 if( strcmp( str,
"POLARSSL_DES_C" ) == 0 )
560 #if defined(POLARSSL_DES_C)
566 if( strcmp( str,
"POLARSSL_ECP_DP_BP384R1_ENABLED" ) == 0 )
568 #if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
574 if( strcmp( str,
"POLARSSL_ECP_DP_BP512R1_ENABLED" ) == 0 )
576 #if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
582 if( strcmp( str,
"POLARSSL_CIPHER_PADDING_PKCS7" ) == 0 )
584 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
590 if( strcmp( str,
"POLARSSL_PEM_PARSE_C" ) == 0 )
592 #if defined(POLARSSL_PEM_PARSE_C)
598 if( strcmp( str,
"POLARSSL_PKCS5_C" ) == 0 )
600 #if defined(POLARSSL_PKCS5_C)
606 if( strcmp( str,
"POLARSSL_ECP_DP_SECP256R1_ENABLED" ) == 0 )
608 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
614 if( strcmp( str,
"POLARSSL_ECP_DP_BP256R1_ENABLED" ) == 0 )
616 #if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
622 if( strcmp( str,
"POLARSSL_AES_C" ) == 0 )
624 #if defined(POLARSSL_AES_C)
630 if( strcmp( str,
"POLARSSL_PKCS12_C" ) == 0 )
632 #if defined(POLARSSL_PKCS12_C)
638 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CBC" ) == 0 )
640 #if defined(POLARSSL_CIPHER_MODE_CBC)
646 if( strcmp( str,
"POLARSSL_MD5_C" ) == 0 )
648 #if defined(POLARSSL_MD5_C)
654 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
656 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
662 if( strcmp( str,
"POLARSSL_ECP_C" ) == 0 )
664 #if defined(POLARSSL_ECP_C)
670 if( strcmp( str,
"POLARSSL_ECP_DP_SECP224R1_ENABLED" ) == 0 )
672 #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
678 if( strcmp( str,
"POLARSSL_ECP_DP_SECP521R1_ENABLED" ) == 0 )
680 #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
697 #if defined(TEST_SUITE_ACTIVE)
698 if( strcmp( params[0],
"pk_parse_keyfile_rsa" ) == 0 )
700 #ifdef POLARSSL_RSA_C
701 #ifdef POLARSSL_FS_IO
703 char *param1 = params[1];
704 char *param2 = params[2];
709 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
715 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
717 test_suite_pk_parse_keyfile_rsa( param1, param2, param3 );
725 if( strcmp( params[0],
"pk_parse_public_keyfile_rsa" ) == 0 )
727 #ifdef POLARSSL_RSA_C
728 #ifdef POLARSSL_FS_IO
730 char *param1 = params[1];
735 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
740 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
742 test_suite_pk_parse_public_keyfile_rsa( param1, param2 );
750 if( strcmp( params[0],
"pk_parse_public_keyfile_ec" ) == 0 )
752 #ifdef POLARSSL_FS_IO
753 #ifdef POLARSSL_ECP_C
755 char *param1 = params[1];
760 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
765 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
767 test_suite_pk_parse_public_keyfile_ec( param1, param2 );
775 if( strcmp( params[0],
"pk_parse_keyfile_ec" ) == 0 )
777 #ifdef POLARSSL_FS_IO
778 #ifdef POLARSSL_ECP_C
780 char *param1 = params[1];
781 char *param2 = params[2];
786 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
792 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
794 test_suite_pk_parse_keyfile_ec( param1, param2, param3 );
802 if( strcmp( params[0],
"pk_parse_key_rsa" ) == 0 )
804 #ifdef POLARSSL_RSA_C
806 char *param1 = params[1];
807 char *param2 = params[2];
812 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
818 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
820 test_suite_pk_parse_key_rsa( param1, param2, param3 );
829 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
843 ret = fgets( buf, len, f );
847 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
848 buf[strlen(buf) - 1] =
'\0';
849 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
850 buf[strlen(buf) - 1] =
'\0';
863 while( *p !=
'\0' && p < buf + len )
873 if( p + 1 < buf + len )
885 for( i = 0; i < cnt; i++ )
892 if( *p ==
'\\' && *(p + 1) ==
'n' )
897 else if( *p ==
'\\' && *(p + 1) ==
':' )
902 else if( *p ==
'\\' && *(p + 1) ==
'?' )
918 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
919 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_pkparse.data";
924 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
925 unsigned char alloc_buf[1000000];
926 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
929 file = fopen( filename,
"r" );
932 fprintf( stderr,
"Failed to open\n" );
936 while( !feof( file ) )
940 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
942 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
943 fprintf( stdout,
" " );
944 for( i = strlen( buf ) + 1; i < 67; i++ )
945 fprintf( stdout,
"." );
946 fprintf( stdout,
" " );
951 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
955 if( strcmp( params[0],
"depends_on" ) == 0 )
957 for( i = 1; i < cnt; i++ )
961 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
972 if( skip == 1 || ret == 3 )
975 fprintf( stdout,
"----\n" );
980 fprintf( stdout,
"PASS\n" );
985 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
992 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
994 if( strlen(buf) != 0 )
996 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1002 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1003 if( total_errors == 0 )
1004 fprintf( stdout,
"PASSED" );
1006 fprintf( stdout,
"FAILED" );
1008 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1009 total_tests - total_errors, total_tests, total_skipped );
1011 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1012 #if defined(POLARSSL_MEMORY_DEBUG)
1013 memory_buffer_alloc_status();
1015 memory_buffer_alloc_free();
1018 return( total_errors != 0 );