3 #ifdef POLARSSL_ECDSA_C
9 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
15 typedef UINT32 uint32_t;
28 #define GET_UINT32_BE(n,b,i) \
30 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
31 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
32 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
33 | ( (uint32_t) (b)[(i) + 3] ); \
38 #define PUT_UINT32_BE(n,b,i) \
40 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
41 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
42 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
43 (b)[(i) + 3] = (unsigned char) ( (n) ); \
47 static int unhexify(
unsigned char *obuf,
const char *ibuf)
50 int len = strlen(ibuf) / 2;
51 assert(!(strlen(ibuf) %1));
56 if( c >=
'0' && c <=
'9' )
58 else if( c >=
'a' && c <=
'f' )
60 else if( c >=
'A' && c <=
'F' )
66 if( c2 >=
'0' && c2 <=
'9' )
68 else if( c2 >=
'a' && c2 <=
'f' )
70 else if( c2 >=
'A' && c2 <=
'F' )
75 *obuf++ = ( c << 4 ) | c2;
81 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
93 *obuf++ =
'a' + h - 10;
98 *obuf++ =
'a' + l - 10;
114 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
118 if( rng_state != NULL )
121 for( i = 0; i < len; ++i )
132 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
134 if( rng_state != NULL )
137 memset( output, 0, len );
164 if( rng_state == NULL )
173 memcpy( output, info->
buf, use_len );
174 info->
buf += use_len;
178 if( len - use_len > 0 )
179 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
208 uint32_t i, *k, sum, delta=0x9E3779B9;
209 unsigned char result[4];
211 if( rng_state == NULL )
218 size_t use_len = ( len > 4 ) ? 4 : len;
221 for( i = 0; i < 32; i++ )
223 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
225 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
229 memcpy( output, result, use_len );
245 static int not_rnd(
void *in,
unsigned char *out,
size_t len )
248 const char *ibuf = in;
250 assert( len == strlen(ibuf) / 2 );
251 assert(!(strlen(ibuf) %1));
253 obuf = out + (len - 1);
257 if( c >=
'0' && c <=
'9' )
259 else if( c >=
'a' && c <=
'f' )
261 else if( c >=
'A' && c <=
'F' )
267 if( c2 >=
'0' && c2 <=
'9' )
269 else if( c2 >=
'a' && c2 <=
'f' )
271 else if( c2 >=
'A' && c2 <=
'F' )
276 *obuf-- = ( c << 4 ) | c2;
288 #ifdef POLARSSL_ECDSA_C
290 #define TEST_SUITE_ACTIVE
299 printf(
"FAILED\n" );
300 printf(
" %s\n", test );
305 #define TEST_ASSERT( TEST ) \
306 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
307 if( test_errors) return; \
312 if( (*str)[0] !=
'"' ||
313 (*str)[strlen( *str ) - 1] !=
'"' )
315 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
320 (*str)[strlen( *str ) - 1] =
'\0';
332 for( i = 0; i < strlen( str ); i++ )
334 if( i == 0 && str[i] ==
'-' )
340 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
341 str[i - 1] ==
'0' && str[i] ==
'x' )
347 if( str[i] <
'0' || str[i] >
'9' )
357 *value = strtol( str, NULL, 16 );
359 *value = strtol( str, NULL, 10 );
364 if( strcmp( str,
"POLARSSL_ECP_DP_SECP384R1" ) == 0 )
369 if( strcmp( str,
"POLARSSL_ECP_DP_SECP224R1" ) == 0 )
374 if( strcmp( str,
"POLARSSL_ECP_DP_SECP256R1" ) == 0 )
379 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1" ) == 0 )
384 if( strcmp( str,
"POLARSSL_ECP_DP_SECP521R1" ) == 0 )
391 printf(
"Expected integer for parameter and got: %s\n", str );
395 void test_suite_ecdsa_prim_random(
int id )
401 unsigned char buf[66];
407 memset( buf, 0,
sizeof( buf ) );
424 void test_suite_ecdsa_prim_test_vectors(
int id,
char *d_str,
char *xQ_str,
char *yQ_str,
425 char *k_str,
char *hash_str,
char *r_str,
430 mpi d, r, s, r_check, s_check;
431 unsigned char buf[66];
438 memset( buf, 0,
sizeof( buf ) );
461 void test_suite_ecdsa_write_read_random(
int id )
465 unsigned char hash[66];
466 unsigned char sig[200];
471 memset( hash, 0,
sizeof( hash ) );
472 memset( sig, 0x2a,
sizeof( sig ) );
484 sig, sig_len ) == 0 );
487 for( i = sig_len; i <
sizeof( sig ); i++ )
492 sig, sig_len - 1 ) != 0 );
494 sig, sig_len + 1 ) != 0 );
499 sig, sig_len ) != 0 );
505 sig, sig_len ) != 0 );
511 sig, sig_len ) != 0 );
526 if( strcmp( str,
"POLARSSL_ECP_DP_SECP521R1_ENABLED" ) == 0 )
528 #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
534 if( strcmp( str,
"POLARSSL_ECP_DP_SECP256R1_ENABLED" ) == 0 )
536 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
542 if( strcmp( str,
"POLARSSL_ECP_DP_SECP224R1_ENABLED" ) == 0 )
544 #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
550 if( strcmp( str,
"POLARSSL_ECP_DP_SECP384R1_ENABLED" ) == 0 )
552 #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
558 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
560 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
577 #if defined(TEST_SUITE_ACTIVE)
578 if( strcmp( params[0],
"ecdsa_prim_random" ) == 0 )
585 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
589 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
591 test_suite_ecdsa_prim_random( param1 );
597 if( strcmp( params[0],
"ecdsa_prim_test_vectors" ) == 0 )
601 char *param2 = params[2];
602 char *param3 = params[3];
603 char *param4 = params[4];
604 char *param5 = params[5];
605 char *param6 = params[6];
606 char *param7 = params[7];
607 char *param8 = params[8];
611 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 9 );
615 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
624 test_suite_ecdsa_prim_test_vectors( param1, param2, param3, param4, param5, param6, param7, param8 );
630 if( strcmp( params[0],
"ecdsa_write_read_random" ) == 0 )
637 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
641 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
643 test_suite_ecdsa_write_read_random( param1 );
651 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
665 ret = fgets( buf, len, f );
669 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
670 buf[strlen(buf) - 1] =
'\0';
671 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
672 buf[strlen(buf) - 1] =
'\0';
685 while( *p !=
'\0' && p < buf + len )
695 if( p + 1 < buf + len )
707 for( i = 0; i < cnt; i++ )
714 if( *p ==
'\\' && *(p + 1) ==
'n' )
719 else if( *p ==
'\\' && *(p + 1) ==
':' )
724 else if( *p ==
'\\' && *(p + 1) ==
'?' )
740 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
741 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_ecdsa.data";
746 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
747 unsigned char alloc_buf[1000000];
748 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
751 file = fopen( filename,
"r" );
754 fprintf( stderr,
"Failed to open\n" );
758 while( !feof( file ) )
762 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
764 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
765 fprintf( stdout,
" " );
766 for( i = strlen( buf ) + 1; i < 67; i++ )
767 fprintf( stdout,
"." );
768 fprintf( stdout,
" " );
773 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
777 if( strcmp( params[0],
"depends_on" ) == 0 )
779 for( i = 1; i < cnt; i++ )
783 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
794 if( skip == 1 || ret == 3 )
797 fprintf( stdout,
"----\n" );
802 fprintf( stdout,
"PASS\n" );
807 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
814 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
816 if( strlen(buf) != 0 )
818 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
824 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
825 if( total_errors == 0 )
826 fprintf( stdout,
"PASSED" );
828 fprintf( stdout,
"FAILED" );
830 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
831 total_tests - total_errors, total_tests, total_skipped );
833 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
834 #if defined(POLARSSL_MEMORY_DEBUG)
835 memory_buffer_alloc_status();
837 memory_buffer_alloc_free();
840 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.
#define TEST_ASSERT(TEST)
ECP point structure (jacobian coordinates)
void ecp_point_init(ecp_point *pt)
Initialize a point (as zero)
static int not_rnd(void *in, unsigned char *out, size_t len)
This function returns a buffer given as a hex string.
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.