3 #ifdef POLARSSL_PK_WRITE_C
4 #ifdef POLARSSL_BIGNUM_C
15 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
21 typedef UINT32 uint32_t;
34 #define GET_UINT32_BE(n,b,i) \
36 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
37 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
38 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
39 | ( (uint32_t) (b)[(i) + 3] ); \
44 #define PUT_UINT32_BE(n,b,i) \
46 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
47 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
48 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
49 (b)[(i) + 3] = (unsigned char) ( (n) ); \
53 static int unhexify(
unsigned char *obuf,
const char *ibuf)
56 int len = strlen(ibuf) / 2;
57 assert(!(strlen(ibuf) %1));
62 if( c >=
'0' && c <=
'9' )
64 else if( c >=
'a' && c <=
'f' )
66 else if( c >=
'A' && c <=
'F' )
72 if( c2 >=
'0' && c2 <=
'9' )
74 else if( c2 >=
'a' && c2 <=
'f' )
76 else if( c2 >=
'A' && c2 <=
'F' )
81 *obuf++ = ( c << 4 ) | c2;
87 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
99 *obuf++ =
'a' + h - 10;
104 *obuf++ =
'a' + l - 10;
120 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
124 if( rng_state != NULL )
127 for( i = 0; i < len; ++i )
138 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
140 if( rng_state != NULL )
143 memset( output, 0, len );
170 if( rng_state == NULL )
179 memcpy( output, info->
buf, use_len );
180 info->
buf += use_len;
184 if( len - use_len > 0 )
185 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
214 uint32_t i, *k, sum, delta=0x9E3779B9;
215 unsigned char result[4];
217 if( rng_state == NULL )
224 size_t use_len = ( len > 4 ) ? 4 : len;
227 for( i = 0; i < 32; i++ )
229 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
231 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
235 memcpy( output, result, use_len );
251 static int not_rnd(
void *in,
unsigned char *out,
size_t len )
254 const char *ibuf = in;
256 assert( len == strlen(ibuf) / 2 );
257 assert(!(strlen(ibuf) %1));
259 obuf = out + (len - 1);
263 if( c >=
'0' && c <=
'9' )
265 else if( c >=
'a' && c <=
'f' )
267 else if( c >=
'A' && c <=
'F' )
273 if( c2 >=
'0' && c2 <=
'9' )
275 else if( c2 >=
'a' && c2 <=
'f' )
277 else if( c2 >=
'A' && c2 <=
'F' )
282 *obuf-- = ( c << 4 ) | c2;
294 #ifdef POLARSSL_PK_WRITE_C
295 #ifdef POLARSSL_BIGNUM_C
296 #ifdef POLARSSL_FS_IO
298 #define TEST_SUITE_ACTIVE
307 printf(
"FAILED\n" );
308 printf(
" %s\n", test );
313 #define TEST_ASSERT( TEST ) \
314 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
315 if( test_errors) return; \
320 if( (*str)[0] !=
'"' ||
321 (*str)[strlen( *str ) - 1] !=
'"' )
323 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
328 (*str)[strlen( *str ) - 1] =
'\0';
340 for( i = 0; i < strlen( str ); i++ )
342 if( i == 0 && str[i] ==
'-' )
348 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
349 str[i - 1] ==
'0' && str[i] ==
'x' )
355 if( str[i] <
'0' || str[i] >
'9' )
365 *value = strtol( str, NULL, 16 );
367 *value = strtol( str, NULL, 10 );
374 printf(
"Expected integer for parameter and got: %s\n", str );
378 void test_suite_pk_write_pubkey_check(
char *key_file )
381 unsigned char buf[5000];
382 unsigned char check_buf[5000];
386 memset( buf, 0,
sizeof( buf ) );
387 memset( check_buf, 0,
sizeof( check_buf ) );
395 f = fopen( key_file,
"r" );
397 fread( check_buf, 1,
sizeof( check_buf ) - 1, f );
400 TEST_ASSERT( strncmp( (
char *) buf, (
char *) check_buf,
sizeof( buf ) ) == 0 );
405 void test_suite_pk_write_key_check(
char *key_file )
408 unsigned char buf[5000];
409 unsigned char check_buf[5000];
413 memset( buf, 0,
sizeof( buf ) );
414 memset( check_buf, 0,
sizeof( check_buf ) );
422 f = fopen( key_file,
"r" );
424 fread( check_buf, 1,
sizeof( check_buf ) - 1, f );
427 TEST_ASSERT( strncmp( (
char *) buf, (
char *) check_buf,
sizeof( buf ) ) == 0 );
443 if( strcmp( str,
"POLARSSL_ECP_C" ) == 0 )
445 #if defined(POLARSSL_ECP_C)
451 if( strcmp( str,
"POLARSSL_RSA_C" ) == 0 )
453 #if defined(POLARSSL_RSA_C)
459 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
461 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
467 if( strcmp( str,
"POLARSSL_BASE64_C" ) == 0 )
469 #if defined(POLARSSL_BASE64_C)
486 #if defined(TEST_SUITE_ACTIVE)
487 if( strcmp( params[0],
"pk_write_pubkey_check" ) == 0 )
490 char *param1 = params[1];
494 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
500 test_suite_pk_write_pubkey_check( param1 );
506 if( strcmp( params[0],
"pk_write_key_check" ) == 0 )
509 char *param1 = params[1];
513 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
519 test_suite_pk_write_key_check( param1 );
527 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
541 ret = fgets( buf, len, f );
545 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
546 buf[strlen(buf) - 1] =
'\0';
547 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
548 buf[strlen(buf) - 1] =
'\0';
561 while( *p !=
'\0' && p < buf + len )
571 if( p + 1 < buf + len )
583 for( i = 0; i < cnt; i++ )
590 if( *p ==
'\\' && *(p + 1) ==
'n' )
595 else if( *p ==
'\\' && *(p + 1) ==
':' )
600 else if( *p ==
'\\' && *(p + 1) ==
'?' )
616 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
617 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_pkwrite.data";
622 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
623 unsigned char alloc_buf[1000000];
624 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
627 file = fopen( filename,
"r" );
630 fprintf( stderr,
"Failed to open\n" );
634 while( !feof( file ) )
638 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
640 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
641 fprintf( stdout,
" " );
642 for( i = strlen( buf ) + 1; i < 67; i++ )
643 fprintf( stdout,
"." );
644 fprintf( stdout,
" " );
649 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
653 if( strcmp( params[0],
"depends_on" ) == 0 )
655 for( i = 1; i < cnt; i++ )
659 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
670 if( skip == 1 || ret == 3 )
673 fprintf( stdout,
"----\n" );
678 fprintf( stdout,
"PASS\n" );
683 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
690 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
692 if( strlen(buf) != 0 )
694 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
700 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
701 if( total_errors == 0 )
702 fprintf( stdout,
"PASSED" );
704 fprintf( stdout,
"FAILED" );
706 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
707 total_tests - total_errors, total_tests, total_skipped );
709 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
710 #if defined(POLARSSL_MEMORY_DEBUG)
711 memory_buffer_alloc_status();
713 memory_buffer_alloc_free();
716 return( total_errors != 0 );
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
Info structure for the pseudo random function.
Configuration options (set of defines)
int pk_write_key_pem(pk_context *key, unsigned char *buf, size_t size)
Write a private key to a PKCS#1 or SEC1 PEM string.
static int test_assert(int correct, char *test)
int main(int argc, char *argv[])
Object Identifier (OID) database.
Public Key abstraction layer.
#define TEST_ASSERT(TEST)
#define PUT_UINT32_BE(n, b, i)
Privacy Enhanced Mail (PEM) decoding.
int parse_arguments(char *buf, size_t len, char *params[50])
int pk_parse_public_keyfile(pk_context *ctx, const char *path)
Load and parse a public key.
static int unhexify(unsigned char *obuf, const char *ibuf)
static int not_rnd(void *in, unsigned char *out, size_t len)
This function returns a buffer given as a hex string.
int verify_string(char **str)
void pk_free(pk_context *ctx)
Free a pk_context.
int pk_write_pubkey_pem(pk_context *key, unsigned char *buf, size_t size)
Write a public key to a PEM string.
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
int dispatch_test(int cnt, char *params[50])
void pk_init(pk_context *ctx)
Initialize a pk_context (as NONE)
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int verify_int(char *str, int *value)
int pk_parse_keyfile(pk_context *ctx, const char *path, const char *password)
Load and parse a private key.
int get_line(FILE *f, char *buf, size_t len)