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