PolarSSL v1.3.2
test_suite_ecdsa.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_ECDSA_C
4 
5 #include <polarssl/ecdsa.h>
6 #define WANT_NOT_RND_MPI
7 #endif /* POLARSSL_ECDSA_C */
8 
9 
10 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
11 #include "polarssl/memory.h"
12 #endif
13 
14 #if defined(WANT_NOT_RND_MPI)
15 #if defined(POLARSSL_BIGNUM_C)
16 #include "polarssl/bignum.h"
17 #else
18 #error "not_rnd_mpi() need bignum.c"
19 #endif
20 #endif
21 
22 #ifdef _MSC_VER
23 #include <basetsd.h>
24 typedef UINT32 uint32_t;
25 #else
26 #include <inttypes.h>
27 #endif
28 
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 /*
34  * 32-bit integer manipulation macros (big endian)
35  */
36 #ifndef GET_UINT32_BE
37 #define GET_UINT32_BE(n,b,i) \
38 { \
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] ); \
43 }
44 #endif
45 
46 #ifndef PUT_UINT32_BE
47 #define PUT_UINT32_BE(n,b,i) \
48 { \
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) ); \
53 }
54 #endif
55 
56 static int unhexify(unsigned char *obuf, const char *ibuf)
57 {
58  unsigned char c, c2;
59  int len = strlen(ibuf) / 2;
60  assert(!(strlen(ibuf) %1)); // must be even number of bytes
61 
62  while (*ibuf != 0)
63  {
64  c = *ibuf++;
65  if( c >= '0' && c <= '9' )
66  c -= '0';
67  else if( c >= 'a' && c <= 'f' )
68  c -= 'a' - 10;
69  else if( c >= 'A' && c <= 'F' )
70  c -= 'A' - 10;
71  else
72  assert( 0 );
73 
74  c2 = *ibuf++;
75  if( c2 >= '0' && c2 <= '9' )
76  c2 -= '0';
77  else if( c2 >= 'a' && c2 <= 'f' )
78  c2 -= 'a' - 10;
79  else if( c2 >= 'A' && c2 <= 'F' )
80  c2 -= 'A' - 10;
81  else
82  assert( 0 );
83 
84  *obuf++ = ( c << 4 ) | c2;
85  }
86 
87  return len;
88 }
89 
90 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
91 {
92  unsigned char l, h;
93 
94  while (len != 0)
95  {
96  h = (*ibuf) / 16;
97  l = (*ibuf) % 16;
98 
99  if( h < 10 )
100  *obuf++ = '0' + h;
101  else
102  *obuf++ = 'a' + h - 10;
103 
104  if( l < 10 )
105  *obuf++ = '0' + l;
106  else
107  *obuf++ = 'a' + l - 10;
108 
109  ++ibuf;
110  len--;
111  }
112 }
113 
123 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
124 {
125  size_t i;
126 
127  if( rng_state != NULL )
128  rng_state = NULL;
129 
130  for( i = 0; i < len; ++i )
131  output[i] = rand();
132 
133  return( 0 );
134 }
135 
141 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
142 {
143  if( rng_state != NULL )
144  rng_state = NULL;
145 
146  memset( output, 0, len );
147 
148  return( 0 );
149 }
150 
151 typedef struct
152 {
153  unsigned char *buf;
154  size_t length;
155 } rnd_buf_info;
156 
168 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
169 {
170  rnd_buf_info *info = (rnd_buf_info *) rng_state;
171  size_t use_len;
172 
173  if( rng_state == NULL )
174  return( rnd_std_rand( NULL, output, len ) );
175 
176  use_len = len;
177  if( len > info->length )
178  use_len = info->length;
179 
180  if( use_len )
181  {
182  memcpy( output, info->buf, use_len );
183  info->buf += use_len;
184  info->length -= use_len;
185  }
186 
187  if( len - use_len > 0 )
188  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
189 
190  return( 0 );
191 }
192 
200 typedef struct
201 {
202  uint32_t key[16];
203  uint32_t v0, v1;
205 
214 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
215 {
216  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
217  uint32_t i, *k, sum, delta=0x9E3779B9;
218  unsigned char result[4];
219 
220  if( rng_state == NULL )
221  return( rnd_std_rand( NULL, output, len ) );
222 
223  k = info->key;
224 
225  while( len > 0 )
226  {
227  size_t use_len = ( len > 4 ) ? 4 : len;
228  sum = 0;
229 
230  for( i = 0; i < 32; i++ )
231  {
232  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
233  sum += delta;
234  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
235  }
236 
237  PUT_UINT32_BE( info->v0, result, 0 );
238  memcpy( output, result, use_len );
239  len -= use_len;
240  }
241 
242  return( 0 );
243 }
244 
245 #if defined(WANT_NOT_RND_MPI)
246 
254 #define ciL (sizeof(t_uint)) /* chars in limb */
255 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
256 static int not_rnd_mpi( void *in, unsigned char *out, size_t len )
257 {
258  char *str = (char *) in;
259  mpi X;
260 
261  /*
262  * The 'in' pointer we get is from an MPI prepared by mpi_fill_random(),
263  * just reconstruct the rest in order to be able to call mpi_read_string()
264  */
265  X.s = 1;
266  X.p = (t_uint *) out;
267  X.n = CHARS_TO_LIMBS( len );
268 
269  /*
270  * If str is too long, mpi_read_string() will try to allocate a new buffer
271  * for X.p, which we want to avoid at all costs.
272  */
273  assert( strlen( str ) / 2 == len );
274 
275  return( mpi_read_string( &X, 16, str ) );
276 }
277 #endif /* WANT_NOT_RND_MPI */
278 
279 
280 #include <stdio.h>
281 #include <string.h>
282 
283 static int test_errors = 0;
284 
285 #ifdef POLARSSL_ECDSA_C
286 
287 #define TEST_SUITE_ACTIVE
288 
289 static int test_assert( int correct, char *test )
290 {
291  if( correct )
292  return( 0 );
293 
294  test_errors++;
295  if( test_errors == 1 )
296  printf( "FAILED\n" );
297  printf( " %s\n", test );
298 
299  return( 1 );
300 }
301 
302 #define TEST_ASSERT( TEST ) \
303  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
304  if( test_errors) return; \
305  } while (0)
306 
307 int verify_string( char **str )
308 {
309  if( (*str)[0] != '"' ||
310  (*str)[strlen( *str ) - 1] != '"' )
311  {
312  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
313  return( -1 );
314  }
315 
316  (*str)++;
317  (*str)[strlen( *str ) - 1] = '\0';
318 
319  return( 0 );
320 }
321 
322 int verify_int( char *str, int *value )
323 {
324  size_t i;
325  int minus = 0;
326  int digits = 1;
327  int hex = 0;
328 
329  for( i = 0; i < strlen( str ); i++ )
330  {
331  if( i == 0 && str[i] == '-' )
332  {
333  minus = 1;
334  continue;
335  }
336 
337  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
338  str[i - 1] == '0' && str[i] == 'x' )
339  {
340  hex = 1;
341  continue;
342  }
343 
344  if( str[i] < '0' || str[i] > '9' )
345  {
346  digits = 0;
347  break;
348  }
349  }
350 
351  if( digits )
352  {
353  if( hex )
354  *value = strtol( str, NULL, 16 );
355  else
356  *value = strtol( str, NULL, 10 );
357 
358  return( 0 );
359  }
360 
361  if( strcmp( str, "POLARSSL_ECP_DP_SECP224R1" ) == 0 )
362  {
363  *value = ( POLARSSL_ECP_DP_SECP224R1 );
364  return( 0 );
365  }
366  if( strcmp( str, "POLARSSL_ECP_DP_SECP521R1" ) == 0 )
367  {
368  *value = ( POLARSSL_ECP_DP_SECP521R1 );
369  return( 0 );
370  }
371  if( strcmp( str, "POLARSSL_ECP_DP_SECP256R1" ) == 0 )
372  {
373  *value = ( POLARSSL_ECP_DP_SECP256R1 );
374  return( 0 );
375  }
376  if( strcmp( str, "POLARSSL_ECP_DP_SECP384R1" ) == 0 )
377  {
378  *value = ( POLARSSL_ECP_DP_SECP384R1 );
379  return( 0 );
380  }
381  if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1" ) == 0 )
382  {
383  *value = ( POLARSSL_ECP_DP_SECP192R1 );
384  return( 0 );
385  }
386 
387 
388  printf( "Expected integer for parameter and got: %s\n", str );
389  return( -1 );
390 }
391 
392 void test_suite_ecdsa_prim_random( int id )
393 {
394  ecp_group grp;
395  ecp_point Q;
396  mpi d, r, s;
397  rnd_pseudo_info rnd_info;
398  unsigned char buf[66];
399 
400  ecp_group_init( &grp );
401  ecp_point_init( &Q );
402  mpi_init( &d ); mpi_init( &r ); mpi_init( &s );
403  memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
404  memset( buf, 0, sizeof( buf ) );
405 
406  /* prepare material for signature */
407  TEST_ASSERT( rnd_pseudo_rand( &rnd_info, buf, sizeof( buf ) ) == 0 );
408  TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
409  TEST_ASSERT( ecp_gen_keypair( &grp, &d, &Q, &rnd_pseudo_rand, &rnd_info )
410  == 0 );
411 
412  TEST_ASSERT( ecdsa_sign( &grp, &r, &s, &d, buf, sizeof( buf ),
413  &rnd_pseudo_rand, &rnd_info ) == 0 );
414  TEST_ASSERT( ecdsa_verify( &grp, buf, sizeof( buf ), &Q, &r, &s ) == 0 );
415 
416  ecp_group_free( &grp );
417  ecp_point_free( &Q );
418  mpi_free( &d ); mpi_free( &r ); mpi_free( &s );
419 }
420 
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,
423  char *s_str )
424 {
425  ecp_group grp;
426  ecp_point Q;
427  mpi d, r, s, r_check, s_check;
428  unsigned char buf[66];
429  size_t len;
430 
431  ecp_group_init( &grp );
432  ecp_point_init( &Q );
433  mpi_init( &d ); mpi_init( &r ); mpi_init( &s );
434  mpi_init( &r_check ); mpi_init( &s_check );
435  memset( buf, 0, sizeof( buf ) );
436 
437  TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
438  TEST_ASSERT( ecp_point_read_string( &Q, 16, xQ_str, yQ_str ) == 0 );
439  TEST_ASSERT( mpi_read_string( &d, 16, d_str ) == 0 );
440  TEST_ASSERT( mpi_read_string( &r_check, 16, r_str ) == 0 );
441  TEST_ASSERT( mpi_read_string( &s_check, 16, s_str ) == 0 );
442  len = unhexify(buf, hash_str);
443 
444  TEST_ASSERT( ecdsa_sign( &grp, &r, &s, &d, buf, len,
445  &not_rnd_mpi, k_str ) == 0 );
446 
447  TEST_ASSERT( mpi_cmp_mpi( &r, &r_check ) == 0 );
448  TEST_ASSERT( mpi_cmp_mpi( &s, &s_check ) == 0 );
449 
450  TEST_ASSERT( ecdsa_verify( &grp, buf, len, &Q, &r_check, &s_check ) == 0 );
451 
452  ecp_group_free( &grp );
453  ecp_point_free( &Q );
454  mpi_free( &d ); mpi_free( &r ); mpi_free( &s );
455  mpi_free( &r_check ); mpi_free( &s_check );
456 }
457 
458 void test_suite_ecdsa_write_read_random( int id )
459 {
460  ecdsa_context ctx;
461  rnd_pseudo_info rnd_info;
462  unsigned char hash[66];
463  unsigned char sig[200];
464  size_t sig_len, i;
465 
466  ecdsa_init( &ctx );
467  memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
468  memset( hash, 0, sizeof( hash ) );
469  memset( sig, 0x2a, sizeof( sig ) );
470 
471  /* prepare material for signature */
472  TEST_ASSERT( rnd_pseudo_rand( &rnd_info, hash, sizeof( hash ) ) == 0 );
473 
474  /* generate signing key */
475  TEST_ASSERT( ecdsa_genkey( &ctx, id, &rnd_pseudo_rand, &rnd_info ) == 0 );
476 
477  /* generate and write signature, then read and verify it */
478  TEST_ASSERT( ecdsa_write_signature( &ctx, hash, sizeof( hash ),
479  sig, &sig_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
480  TEST_ASSERT( ecdsa_read_signature( &ctx, hash, sizeof( hash ),
481  sig, sig_len ) == 0 );
482 
483  /* check we didn't write past the announced length */
484  for( i = sig_len; i < sizeof( sig ); i++ )
485  TEST_ASSERT( sig[i] == 0x2a );
486 
487  /* try verification with invalid length */
488  TEST_ASSERT( ecdsa_read_signature( &ctx, hash, sizeof( hash ),
489  sig, sig_len - 1 ) != 0 );
490  TEST_ASSERT( ecdsa_read_signature( &ctx, hash, sizeof( hash ),
491  sig, sig_len + 1 ) != 0 );
492 
493  /* try invalid sequence tag */
494  sig[0]++;
495  TEST_ASSERT( ecdsa_read_signature( &ctx, hash, sizeof( hash ),
496  sig, sig_len ) != 0 );
497  sig[0]--;
498 
499  /* try modifying r */
500  sig[10]++;
501  TEST_ASSERT( ecdsa_read_signature( &ctx, hash, sizeof( hash ),
502  sig, sig_len ) != 0 );
503  sig[10]--;
504 
505  /* try modifying s */
506  sig[sig_len - 1]++;
507  TEST_ASSERT( ecdsa_read_signature( &ctx, hash, sizeof( hash ),
508  sig, sig_len ) != 0 );
509  sig[sig_len - 1]--;
510 
511  ecdsa_free( &ctx );
512 }
513 
514 
515 #endif /* POLARSSL_ECDSA_C */
516 
517 
518 int dep_check( char *str )
519 {
520  if( str == NULL )
521  return( 1 );
522 
523  if( strcmp( str, "POLARSSL_ECP_DP_SECP256R1_ENABLED" ) == 0 )
524  {
525 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
526  return( 0 );
527 #else
528  return( 1 );
529 #endif
530  }
531  if( strcmp( str, "POLARSSL_ECP_DP_SECP224R1_ENABLED" ) == 0 )
532  {
533 #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
534  return( 0 );
535 #else
536  return( 1 );
537 #endif
538  }
539  if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
540  {
541 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
542  return( 0 );
543 #else
544  return( 1 );
545 #endif
546  }
547  if( strcmp( str, "POLARSSL_ECP_DP_SECP384R1_ENABLED" ) == 0 )
548  {
549 #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
550  return( 0 );
551 #else
552  return( 1 );
553 #endif
554  }
555  if( strcmp( str, "POLARSSL_ECP_DP_SECP521R1_ENABLED" ) == 0 )
556  {
557 #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
558  return( 0 );
559 #else
560  return( 1 );
561 #endif
562  }
563 
564 
565  return( 1 );
566 }
567 
568 int dispatch_test(int cnt, char *params[50])
569 {
570  int ret;
571  ((void) cnt);
572  ((void) params);
573 
574 #if defined(TEST_SUITE_ACTIVE)
575  if( strcmp( params[0], "ecdsa_prim_random" ) == 0 )
576  {
577 
578  int param1;
579 
580  if( cnt != 2 )
581  {
582  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
583  return( 2 );
584  }
585 
586  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
587 
588  test_suite_ecdsa_prim_random( param1 );
589  return ( 0 );
590 
591  return ( 3 );
592  }
593  else
594  if( strcmp( params[0], "ecdsa_prim_test_vectors" ) == 0 )
595  {
596 
597  int param1;
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];
605 
606  if( cnt != 9 )
607  {
608  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 9 );
609  return( 2 );
610  }
611 
612  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
613  if( verify_string( &param2 ) != 0 ) return( 2 );
614  if( verify_string( &param3 ) != 0 ) return( 2 );
615  if( verify_string( &param4 ) != 0 ) return( 2 );
616  if( verify_string( &param5 ) != 0 ) return( 2 );
617  if( verify_string( &param6 ) != 0 ) return( 2 );
618  if( verify_string( &param7 ) != 0 ) return( 2 );
619  if( verify_string( &param8 ) != 0 ) return( 2 );
620 
621  test_suite_ecdsa_prim_test_vectors( param1, param2, param3, param4, param5, param6, param7, param8 );
622  return ( 0 );
623 
624  return ( 3 );
625  }
626  else
627  if( strcmp( params[0], "ecdsa_write_read_random" ) == 0 )
628  {
629 
630  int param1;
631 
632  if( cnt != 2 )
633  {
634  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
635  return( 2 );
636  }
637 
638  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
639 
640  test_suite_ecdsa_write_read_random( param1 );
641  return ( 0 );
642 
643  return ( 3 );
644  }
645  else
646 
647  {
648  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
649  fflush( stdout );
650  return( 1 );
651  }
652 #else
653  return( 3 );
654 #endif
655  return( ret );
656 }
657 
658 int get_line( FILE *f, char *buf, size_t len )
659 {
660  char *ret;
661 
662  ret = fgets( buf, len, f );
663  if( ret == NULL )
664  return( -1 );
665 
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';
670 
671  return( 0 );
672 }
673 
674 int parse_arguments( char *buf, size_t len, char *params[50] )
675 {
676  int cnt = 0, i;
677  char *cur = buf;
678  char *p = buf, *q;
679 
680  params[cnt++] = cur;
681 
682  while( *p != '\0' && p < buf + len )
683  {
684  if( *p == '\\' )
685  {
686  *p++;
687  *p++;
688  continue;
689  }
690  if( *p == ':' )
691  {
692  if( p + 1 < buf + len )
693  {
694  cur = p + 1;
695  params[cnt++] = cur;
696  }
697  *p = '\0';
698  }
699 
700  *p++;
701  }
702 
703  // Replace newlines, question marks and colons in strings
704  for( i = 0; i < cnt; i++ )
705  {
706  p = params[i];
707  q = params[i];
708 
709  while( *p != '\0' )
710  {
711  if( *p == '\\' && *(p + 1) == 'n' )
712  {
713  p += 2;
714  *(q++) = '\n';
715  }
716  else if( *p == '\\' && *(p + 1) == ':' )
717  {
718  p += 2;
719  *(q++) = ':';
720  }
721  else if( *p == '\\' && *(p + 1) == '?' )
722  {
723  p += 2;
724  *(q++) = '?';
725  }
726  else
727  *(q++) = *(p++);
728  }
729  *q = '\0';
730  }
731 
732  return( cnt );
733 }
734 
735 int main()
736 {
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";
739  FILE *file;
740  char buf[5000];
741  char *params[50];
742 
743 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
744  unsigned char alloc_buf[1000000];
745  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
746 #endif
747 
748  file = fopen( filename, "r" );
749  if( file == NULL )
750  {
751  fprintf( stderr, "Failed to open\n" );
752  return( 1 );
753  }
754 
755  while( !feof( file ) )
756  {
757  int skip = 0;
758 
759  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
760  break;
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, " " );
766  fflush( stdout );
767 
768  total_tests++;
769 
770  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
771  break;
772  cnt = parse_arguments( buf, strlen(buf), params );
773 
774  if( strcmp( params[0], "depends_on" ) == 0 )
775  {
776  for( i = 1; i < cnt; i++ )
777  if( dep_check( params[i] ) != 0 )
778  skip = 1;
779 
780  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
781  break;
782  cnt = parse_arguments( buf, strlen(buf), params );
783  }
784 
785  if( skip == 0 )
786  {
787  test_errors = 0;
788  ret = dispatch_test( cnt, params );
789  }
790 
791  if( skip == 1 || ret == 3 )
792  {
793  total_skipped++;
794  fprintf( stdout, "----\n" );
795  fflush( stdout );
796  }
797  else if( ret == 0 && test_errors == 0 )
798  {
799  fprintf( stdout, "PASS\n" );
800  fflush( stdout );
801  }
802  else if( ret == 2 )
803  {
804  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
805  fclose(file);
806  exit( 2 );
807  }
808  else
809  total_errors++;
810 
811  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
812  break;
813  if( strlen(buf) != 0 )
814  {
815  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
816  return( 1 );
817  }
818  }
819  fclose(file);
820 
821  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
822  if( total_errors == 0 )
823  fprintf( stdout, "PASSED" );
824  else
825  fprintf( stdout, "FAILED" );
826 
827  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
828  total_tests - total_errors, total_tests, total_skipped );
829 
830 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
831 #if defined(POLARSSL_MEMORY_DEBUG)
832  memory_buffer_alloc_status();
833 #endif
834  memory_buffer_alloc_free();
835 #endif
836 
837  return( total_errors != 0 );
838 }
839 
840 
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.
Memory allocation layer.
uint32_t t_uint
Definition: bignum.h:149
Info structure for the pseudo random function.
static int unhexify(unsigned char *obuf, const char *ibuf)
static int test_errors
int s
Definition: bignum.h:173
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...
Elliptic curve DSA.
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.
ECP group structure.
Definition: ecp.h:117
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.
MPI structure.
Definition: bignum.h:171
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.
int dep_check(char *str)
#define TEST_ASSERT(TEST)
ECP point structure (jacobian coordinates)
Definition: ecp.h:94
void ecp_point_init(ecp_point *pt)
Initialize a point (as zero)
ECDSA context structure.
Definition: ecdsa.h:37
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.
t_uint * p
Definition: bignum.h:175
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])
size_t n
Definition: bignum.h:174
unsigned char * buf
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.