3 #ifdef POLARSSL_PK_WRITE_C
4 #ifdef POLARSSL_BIGNUM_C
15 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
19 #if defined(WANT_NOT_RND_MPI)
20 #if defined(POLARSSL_BIGNUM_C)
23 #error "not_rnd_mpi() need bignum.c"
29 typedef UINT32 uint32_t;
42 #define GET_UINT32_BE(n,b,i) \
44 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
45 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
46 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
47 | ( (uint32_t) (b)[(i) + 3] ); \
52 #define PUT_UINT32_BE(n,b,i) \
54 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
55 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
56 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
57 (b)[(i) + 3] = (unsigned char) ( (n) ); \
61 static int unhexify(
unsigned char *obuf,
const char *ibuf)
64 int len = strlen(ibuf) / 2;
65 assert(!(strlen(ibuf) %1));
70 if( c >=
'0' && c <=
'9' )
72 else if( c >=
'a' && c <=
'f' )
74 else if( c >=
'A' && c <=
'F' )
80 if( c2 >=
'0' && c2 <=
'9' )
82 else if( c2 >=
'a' && c2 <=
'f' )
84 else if( c2 >=
'A' && c2 <=
'F' )
89 *obuf++ = ( c << 4 ) | c2;
95 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
107 *obuf++ =
'a' + h - 10;
112 *obuf++ =
'a' + l - 10;
128 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
132 if( rng_state != NULL )
135 for( i = 0; i < len; ++i )
146 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
148 if( rng_state != NULL )
151 memset( output, 0, len );
178 if( rng_state == NULL )
187 memcpy( output, info->
buf, use_len );
188 info->
buf += use_len;
192 if( len - use_len > 0 )
193 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
222 uint32_t i, *k, sum, delta=0x9E3779B9;
223 unsigned char result[4];
225 if( rng_state == NULL )
232 size_t use_len = ( len > 4 ) ? 4 : len;
235 for( i = 0; i < 32; i++ )
237 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
239 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
243 memcpy( output, result, use_len );
250 #if defined(WANT_NOT_RND_MPI)
259 #define ciL (sizeof(t_uint))
260 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
261 static int not_rnd_mpi(
void *in,
unsigned char *out,
size_t len )
263 char *str = (
char *) in;
272 X.
n = CHARS_TO_LIMBS( len );
278 assert( strlen( str ) / 2 == len );
290 #ifdef POLARSSL_PK_WRITE_C
291 #ifdef POLARSSL_BIGNUM_C
292 #ifdef POLARSSL_FS_IO
294 #define TEST_SUITE_ACTIVE
302 if( test_errors == 1 )
303 printf(
"FAILED\n" );
304 printf(
" %s\n", test );
309 #define TEST_ASSERT( TEST ) \
310 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
311 if( test_errors) return; \
316 if( (*str)[0] !=
'"' ||
317 (*str)[strlen( *str ) - 1] !=
'"' )
319 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
324 (*str)[strlen( *str ) - 1] =
'\0';
336 for( i = 0; i < strlen( str ); i++ )
338 if( i == 0 && str[i] ==
'-' )
344 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
345 str[i - 1] ==
'0' && str[i] ==
'x' )
351 if( str[i] <
'0' || str[i] >
'9' )
361 *value = strtol( str, NULL, 16 );
363 *value = strtol( str, NULL, 10 );
370 printf(
"Expected integer for parameter and got: %s\n", str );
374 void test_suite_pk_write_pubkey_check(
char *key_file )
377 unsigned char buf[5000];
378 unsigned char check_buf[5000];
382 memset( buf, 0,
sizeof( buf ) );
383 memset( check_buf, 0,
sizeof( check_buf ) );
391 f = fopen( key_file,
"r" );
393 fread( check_buf, 1,
sizeof( check_buf ) - 1, f );
396 TEST_ASSERT( strncmp( (
char *) buf, (
char *) check_buf,
sizeof( buf ) ) == 0 );
401 void test_suite_pk_write_key_check(
char *key_file )
404 unsigned char buf[5000];
405 unsigned char check_buf[5000];
409 memset( buf, 0,
sizeof( buf ) );
410 memset( check_buf, 0,
sizeof( check_buf ) );
418 f = fopen( key_file,
"r" );
420 fread( check_buf, 1,
sizeof( check_buf ) - 1, f );
423 TEST_ASSERT( strncmp( (
char *) buf, (
char *) check_buf,
sizeof( buf ) ) == 0 );
439 if( strcmp( str,
"POLARSSL_BASE64_C" ) == 0 )
441 #if defined(POLARSSL_BASE64_C)
447 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
449 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
455 if( strcmp( str,
"POLARSSL_ECP_C" ) == 0 )
457 #if defined(POLARSSL_ECP_C)
463 if( strcmp( str,
"POLARSSL_RSA_C" ) == 0 )
465 #if defined(POLARSSL_RSA_C)
482 #if defined(TEST_SUITE_ACTIVE)
483 if( strcmp( params[0],
"pk_write_pubkey_check" ) == 0 )
486 char *param1 = params[1];
490 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
496 test_suite_pk_write_pubkey_check( param1 );
502 if( strcmp( params[0],
"pk_write_key_check" ) == 0 )
505 char *param1 = params[1];
509 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
515 test_suite_pk_write_key_check( param1 );
523 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
537 ret = fgets( buf, len, f );
541 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
542 buf[strlen(buf) - 1] =
'\0';
543 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
544 buf[strlen(buf) - 1] =
'\0';
557 while( *p !=
'\0' && p < buf + len )
567 if( p + 1 < buf + len )
579 for( i = 0; i < cnt; i++ )
586 if( *p ==
'\\' && *(p + 1) ==
'n' )
591 else if( *p ==
'\\' && *(p + 1) ==
':' )
596 else if( *p ==
'\\' && *(p + 1) ==
'?' )
612 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
613 const char *filename =
"/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_pkwrite.data";
618 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
619 unsigned char alloc_buf[1000000];
620 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
623 file = fopen( filename,
"r" );
626 fprintf( stderr,
"Failed to open\n" );
630 while( !feof( file ) )
634 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
636 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
637 fprintf( stdout,
" " );
638 for( i = strlen( buf ) + 1; i < 67; i++ )
639 fprintf( stdout,
"." );
640 fprintf( stdout,
" " );
645 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
649 if( strcmp( params[0],
"depends_on" ) == 0 )
651 for( i = 1; i < cnt; i++ )
655 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
666 if( skip == 1 || ret == 3 )
669 fprintf( stdout,
"----\n" );
672 else if( ret == 0 && test_errors == 0 )
674 fprintf( stdout,
"PASS\n" );
679 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
686 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
688 if( strlen(buf) != 0 )
690 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
696 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
697 if( total_errors == 0 )
698 fprintf( stdout,
"PASSED" );
700 fprintf( stdout,
"FAILED" );
702 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
703 total_tests - total_errors, total_tests, total_skipped );
705 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
706 #if defined(POLARSSL_MEMORY_DEBUG)
707 memory_buffer_alloc_status();
709 memory_buffer_alloc_free();
712 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.
Multi-precision integer library.
#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)
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII 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)