3 #ifdef POLARSSL_ECDSA_C
6 #define WANT_NOT_RND_MPI
10 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
14 #if defined(WANT_NOT_RND_MPI)
15 #if defined(POLARSSL_BIGNUM_C)
18 #error "not_rnd_mpi() need bignum.c"
24 typedef UINT32 uint32_t;
37 #define GET_UINT32_BE(n,b,i) \
39 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
40 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
41 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
42 | ( (uint32_t) (b)[(i) + 3] ); \
47 #define PUT_UINT32_BE(n,b,i) \
49 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
50 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
51 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
52 (b)[(i) + 3] = (unsigned char) ( (n) ); \
56 static int unhexify(
unsigned char *obuf,
const char *ibuf)
59 int len = strlen(ibuf) / 2;
60 assert(!(strlen(ibuf) %1));
65 if( c >=
'0' && c <=
'9' )
67 else if( c >=
'a' && c <=
'f' )
69 else if( c >=
'A' && c <=
'F' )
75 if( c2 >=
'0' && c2 <=
'9' )
77 else if( c2 >=
'a' && c2 <=
'f' )
79 else if( c2 >=
'A' && c2 <=
'F' )
84 *obuf++ = ( c << 4 ) | c2;
90 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
102 *obuf++ =
'a' + h - 10;
107 *obuf++ =
'a' + l - 10;
123 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
127 if( rng_state != NULL )
130 for( i = 0; i < len; ++i )
141 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
143 if( rng_state != NULL )
146 memset( output, 0, len );
173 if( rng_state == NULL )
182 memcpy( output, info->
buf, use_len );
183 info->
buf += use_len;
187 if( len - use_len > 0 )
188 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
217 uint32_t i, *k, sum, delta=0x9E3779B9;
218 unsigned char result[4];
220 if( rng_state == NULL )
227 size_t use_len = ( len > 4 ) ? 4 : len;
230 for( i = 0; i < 32; i++ )
232 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
234 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
238 memcpy( output, result, use_len );
245 #if defined(WANT_NOT_RND_MPI)
254 #define ciL (sizeof(t_uint))
255 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
256 static int not_rnd_mpi(
void *in,
unsigned char *out,
size_t len )
258 char *str = (
char *) in;
267 X.
n = CHARS_TO_LIMBS( len );
273 assert( strlen( str ) / 2 == len );
285 #ifdef POLARSSL_ECDSA_C
287 #define TEST_SUITE_ACTIVE
295 if( test_errors == 1 )
296 printf(
"FAILED\n" );
297 printf(
" %s\n", test );
302 #define TEST_ASSERT( TEST ) \
303 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
304 if( test_errors) return; \
309 if( (*str)[0] !=
'"' ||
310 (*str)[strlen( *str ) - 1] !=
'"' )
312 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
317 (*str)[strlen( *str ) - 1] =
'\0';
329 for( i = 0; i < strlen( str ); i++ )
331 if( i == 0 && str[i] ==
'-' )
337 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
338 str[i - 1] ==
'0' && str[i] ==
'x' )
344 if( str[i] <
'0' || str[i] >
'9' )
354 *value = strtol( str, NULL, 16 );
356 *value = strtol( str, NULL, 10 );
361 if( strcmp( str,
"POLARSSL_ECP_DP_SECP224R1" ) == 0 )
366 if( strcmp( str,
"POLARSSL_ECP_DP_SECP521R1" ) == 0 )
371 if( strcmp( str,
"POLARSSL_ECP_DP_SECP256R1" ) == 0 )
376 if( strcmp( str,
"POLARSSL_ECP_DP_SECP384R1" ) == 0 )
381 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1" ) == 0 )
388 printf(
"Expected integer for parameter and got: %s\n", str );
392 void test_suite_ecdsa_prim_random(
int id )
398 unsigned char buf[66];
404 memset( buf, 0,
sizeof( buf ) );
421 void test_suite_ecdsa_prim_test_vectors(
int id,
char *d_str,
char *xQ_str,
char *yQ_str,
422 char *k_str,
char *hash_str,
char *r_str,
427 mpi d, r, s, r_check, s_check;
428 unsigned char buf[66];
435 memset( buf, 0,
sizeof( buf ) );
445 ¬_rnd_mpi, k_str ) == 0 );
458 void test_suite_ecdsa_write_read_random(
int id )
462 unsigned char hash[66];
463 unsigned char sig[200];
468 memset( hash, 0,
sizeof( hash ) );
469 memset( sig, 0x2a,
sizeof( sig ) );
481 sig, sig_len ) == 0 );
484 for( i = sig_len; i <
sizeof( sig ); i++ )
489 sig, sig_len - 1 ) != 0 );
491 sig, sig_len + 1 ) != 0 );
496 sig, sig_len ) != 0 );
502 sig, sig_len ) != 0 );
508 sig, sig_len ) != 0 );
523 if( strcmp( str,
"POLARSSL_ECP_DP_SECP256R1_ENABLED" ) == 0 )
525 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
531 if( strcmp( str,
"POLARSSL_ECP_DP_SECP224R1_ENABLED" ) == 0 )
533 #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
539 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
541 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
547 if( strcmp( str,
"POLARSSL_ECP_DP_SECP384R1_ENABLED" ) == 0 )
549 #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
555 if( strcmp( str,
"POLARSSL_ECP_DP_SECP521R1_ENABLED" ) == 0 )
557 #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
574 #if defined(TEST_SUITE_ACTIVE)
575 if( strcmp( params[0],
"ecdsa_prim_random" ) == 0 )
582 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
586 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
588 test_suite_ecdsa_prim_random( param1 );
594 if( strcmp( params[0],
"ecdsa_prim_test_vectors" ) == 0 )
598 char *param2 = params[2];
599 char *param3 = params[3];
600 char *param4 = params[4];
601 char *param5 = params[5];
602 char *param6 = params[6];
603 char *param7 = params[7];
604 char *param8 = params[8];
608 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 9 );
612 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
621 test_suite_ecdsa_prim_test_vectors( param1, param2, param3, param4, param5, param6, param7, param8 );
627 if( strcmp( params[0],
"ecdsa_write_read_random" ) == 0 )
634 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
638 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
640 test_suite_ecdsa_write_read_random( param1 );
648 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
662 ret = fgets( buf, len, f );
666 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
667 buf[strlen(buf) - 1] =
'\0';
668 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
669 buf[strlen(buf) - 1] =
'\0';
682 while( *p !=
'\0' && p < buf + len )
692 if( p + 1 < buf + len )
704 for( i = 0; i < cnt; i++ )
711 if( *p ==
'\\' && *(p + 1) ==
'n' )
716 else if( *p ==
'\\' && *(p + 1) ==
':' )
721 else if( *p ==
'\\' && *(p + 1) ==
'?' )
737 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
738 const char *filename =
"/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_ecdsa.data";
743 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
744 unsigned char alloc_buf[1000000];
745 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
748 file = fopen( filename,
"r" );
751 fprintf( stderr,
"Failed to open\n" );
755 while( !feof( file ) )
759 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
761 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
762 fprintf( stdout,
" " );
763 for( i = strlen( buf ) + 1; i < 67; i++ )
764 fprintf( stdout,
"." );
765 fprintf( stdout,
" " );
770 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
774 if( strcmp( params[0],
"depends_on" ) == 0 )
776 for( i = 1; i < cnt; i++ )
780 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
791 if( skip == 1 || ret == 3 )
794 fprintf( stdout,
"----\n" );
797 else if( ret == 0 && test_errors == 0 )
799 fprintf( stdout,
"PASS\n" );
804 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
811 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
813 if( strlen(buf) != 0 )
815 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
821 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
822 if( total_errors == 0 )
823 fprintf( stdout,
"PASSED" );
825 fprintf( stdout,
"FAILED" );
827 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
828 total_tests - total_errors, total_tests, total_skipped );
830 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
831 #if defined(POLARSSL_MEMORY_DEBUG)
832 memory_buffer_alloc_status();
834 memory_buffer_alloc_free();
837 return( total_errors != 0 );
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int ecdsa_verify(ecp_group *grp, const unsigned char *buf, size_t blen, const ecp_point *Q, const mpi *r, const mpi *s)
Verify ECDSA signature of a previously hashed message.
Info structure for the pseudo random function.
static int unhexify(unsigned char *obuf, const char *ibuf)
int ecdsa_write_signature(ecdsa_context *ctx, const unsigned char *hash, size_t hlen, unsigned char *sig, size_t *slen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Compute ECDSA signature and write it to buffer, serialized as defined in RFC 4492 page 20...
int ecdsa_sign(ecp_group *grp, mpi *r, mpi *s, const mpi *d, const unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Compute ECDSA signature of a previously hashed message.
Configuration options (set of defines)
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
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 test_assert(int correct, char *test)
void mpi_init(mpi *X)
Initialize one MPI.
int main(int argc, char *argv[])
int mpi_cmp_mpi(const mpi *X, const mpi *Y)
Compare signed values.
Multi-precision integer library.
#define TEST_ASSERT(TEST)
ECP point structure (jacobian coordinates)
void ecp_point_init(ecp_point *pt)
Initialize a point (as zero)
int ecp_point_read_string(ecp_point *P, int radix, const char *x, const char *y)
Import a non-zero point from two ASCII strings.
void mpi_free(mpi *X)
Unallocate one MPI.
void ecp_group_free(ecp_group *grp)
Free the components of an ECP group.
int ecdsa_read_signature(ecdsa_context *ctx, const unsigned char *hash, size_t hlen, const unsigned char *sig, size_t slen)
Read and verify an ECDSA signature.
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
void ecdsa_init(ecdsa_context *ctx)
Initialize context.
int ecp_gen_keypair(ecp_group *grp, mpi *d, ecp_point *Q, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Generate a keypair.
int parse_arguments(char *buf, size_t len, char *params[50])
int ecp_use_known_dp(ecp_group *grp, ecp_group_id index)
Set a group using well-known domain parameters.
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
int verify_string(char **str)
int ecdsa_genkey(ecdsa_context *ctx, ecp_group_id gid, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Generate an ECDSA keypair on the given curve.
void ecp_group_init(ecp_group *grp)
Initialize a group (to something meaningless)
int dispatch_test(int cnt, char *params[50])
void ecdsa_free(ecdsa_context *ctx)
Free context.
#define PUT_UINT32_BE(n, b, i)
int verify_int(char *str, int *value)
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
int get_line(FILE *f, char *buf, size_t len)
void ecp_point_free(ecp_point *pt)
Free the components of a point.