PolarSSL v1.3.2
test_suite_ecdh.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_ECDH_C
4 
5 #include <polarssl/ecdh.h>
6 #define WANT_NOT_RND_MPI
7 #endif /* POLARSSL_ECDH_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_ECDH_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_SECP192R1" ) == 0 )
362  {
363  *value = ( POLARSSL_ECP_DP_SECP192R1 );
364  return( 0 );
365  }
366  if( strcmp( str, "POLARSSL_ECP_DP_SECP256R1" ) == 0 )
367  {
368  *value = ( POLARSSL_ECP_DP_SECP256R1 );
369  return( 0 );
370  }
371  if( strcmp( str, "POLARSSL_ECP_DP_SECP384R1" ) == 0 )
372  {
373  *value = ( POLARSSL_ECP_DP_SECP384R1 );
374  return( 0 );
375  }
376  if( strcmp( str, "POLARSSL_ECP_DP_SECP224R1" ) == 0 )
377  {
378  *value = ( POLARSSL_ECP_DP_SECP224R1 );
379  return( 0 );
380  }
381  if( strcmp( str, "POLARSSL_ECP_DP_SECP521R1" ) == 0 )
382  {
383  *value = ( POLARSSL_ECP_DP_SECP521R1 );
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_ecdh_primitive_random( int id )
393 {
394  ecp_group grp;
395  ecp_point qA, qB;
396  mpi dA, dB, zA, zB;
397  rnd_pseudo_info rnd_info;
398 
399  ecp_group_init( &grp );
400  ecp_point_init( &qA ); ecp_point_init( &qB );
401  mpi_init( &dA ); mpi_init( &dB );
402  mpi_init( &zA ); mpi_init( &zB );
403  memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
404 
405  TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
406 
407  TEST_ASSERT( ecdh_gen_public( &grp, &dA, &qA, &rnd_pseudo_rand, &rnd_info )
408  == 0 );
409  TEST_ASSERT( ecdh_gen_public( &grp, &dB, &qB, &rnd_pseudo_rand, &rnd_info )
410  == 0 );
411  TEST_ASSERT( ecdh_compute_shared( &grp, &zA, &qB, &dA,
412  &rnd_pseudo_rand, &rnd_info ) == 0 );
413  TEST_ASSERT( ecdh_compute_shared( &grp, &zB, &qA, &dB,
414  NULL, NULL ) == 0 );
415 
416  TEST_ASSERT( mpi_cmp_mpi( &zA, &zB ) == 0 );
417 
418  ecp_group_free( &grp );
419  ecp_point_free( &qA ); ecp_point_free( &qB );
420  mpi_free( &dA ); mpi_free( &dB );
421  mpi_free( &zA ); mpi_free( &zB );
422 }
423 
424 void test_suite_ecdh_primitive_testvec( int id, char *dA_str, char *xA_str, char *yA_str,
425  char *dB_str, char *xB_str, char *yB_str,
426  char *z_str )
427 {
428  ecp_group grp;
429  ecp_point qA, qB;
430  mpi dA, dB, zA, zB, check;
431 
432  ecp_group_init( &grp );
433  ecp_point_init( &qA ); ecp_point_init( &qB );
434  mpi_init( &dA ); mpi_init( &dB );
435  mpi_init( &zA ); mpi_init( &zB ); mpi_init( &check );
436 
437  TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
438 
439  TEST_ASSERT( ecdh_gen_public( &grp, &dA, &qA, &not_rnd_mpi, dA_str ) == 0 );
440  TEST_ASSERT( ! ecp_is_zero( &qA ) );
441  TEST_ASSERT( mpi_read_string( &check, 16, xA_str ) == 0 );
442  TEST_ASSERT( mpi_cmp_mpi( &qA.X, &check ) == 0 );
443  TEST_ASSERT( mpi_read_string( &check, 16, yA_str ) == 0 );
444  TEST_ASSERT( mpi_cmp_mpi( &qA.Y, &check ) == 0 );
445 
446  TEST_ASSERT( ecdh_gen_public( &grp, &dB, &qB, &not_rnd_mpi, dB_str ) == 0 );
447  TEST_ASSERT( ! ecp_is_zero( &qB ) );
448  TEST_ASSERT( mpi_read_string( &check, 16, xB_str ) == 0 );
449  TEST_ASSERT( mpi_cmp_mpi( &qB.X, &check ) == 0 );
450  TEST_ASSERT( mpi_read_string( &check, 16, yB_str ) == 0 );
451  TEST_ASSERT( mpi_cmp_mpi( &qB.Y, &check ) == 0 );
452 
453  TEST_ASSERT( mpi_read_string( &check, 16, z_str ) == 0 );
454  TEST_ASSERT( ecdh_compute_shared( &grp, &zA, &qB, &dA, NULL, NULL ) == 0 );
455  TEST_ASSERT( mpi_cmp_mpi( &zA, &check ) == 0 );
456  TEST_ASSERT( ecdh_compute_shared( &grp, &zB, &qA, &dB, NULL, NULL ) == 0 );
457  TEST_ASSERT( mpi_cmp_mpi( &zB, &check ) == 0 );
458 
459  ecp_group_free( &grp );
460  ecp_point_free( &qA ); ecp_point_free( &qB );
461  mpi_free( &dA ); mpi_free( &dB );
462  mpi_free( &zA ); mpi_free( &zB ); mpi_free( &check );
463 }
464 
465 void test_suite_ecdh_exchange( int id )
466 {
467  ecdh_context srv, cli;
468  unsigned char buf[1000];
469  const unsigned char *vbuf;
470  size_t len;
471  rnd_pseudo_info rnd_info;
472 
473  ecdh_init( &srv );
474  ecdh_init( &cli );
475  memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
476 
477  TEST_ASSERT( ecp_use_known_dp( &srv.grp, id ) == 0 );
478 
479  memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
480  TEST_ASSERT( ecdh_make_params( &srv, &len, buf, 1000,
481  &rnd_pseudo_rand, &rnd_info ) == 0 );
482  TEST_ASSERT( ecdh_read_params( &cli, &vbuf, buf + len ) == 0 );
483 
484  memset( buf, 0x00, sizeof( buf ) );
485  TEST_ASSERT( ecdh_make_public( &cli, &len, buf, 1000,
486  &rnd_pseudo_rand, &rnd_info ) == 0 );
487  TEST_ASSERT( ecdh_read_public( &srv, buf, len ) == 0 );
488 
489  TEST_ASSERT( ecdh_calc_secret( &srv, &len, buf, 1000,
490  &rnd_pseudo_rand, &rnd_info ) == 0 );
491  TEST_ASSERT( ecdh_calc_secret( &cli, &len, buf, 1000, NULL, NULL ) == 0 );
492  TEST_ASSERT( mpi_cmp_mpi( &srv.z, &cli.z ) == 0 );
493 
494  ecdh_free( &srv );
495  ecdh_free( &cli );
496 }
497 
498 
499 #endif /* POLARSSL_ECDH_C */
500 
501 
502 int dep_check( char *str )
503 {
504  if( str == NULL )
505  return( 1 );
506 
507  if( strcmp( str, "POLARSSL_ECP_DP_SECP256R1_ENABLED" ) == 0 )
508  {
509 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
510  return( 0 );
511 #else
512  return( 1 );
513 #endif
514  }
515  if( strcmp( str, "POLARSSL_ECP_DP_SECP224R1_ENABLED" ) == 0 )
516  {
517 #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
518  return( 0 );
519 #else
520  return( 1 );
521 #endif
522  }
523  if( strcmp( str, "POLARSSL_ECP_DP_SECP521R1_ENABLED" ) == 0 )
524  {
525 #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
526  return( 0 );
527 #else
528  return( 1 );
529 #endif
530  }
531  if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
532  {
533 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
534  return( 0 );
535 #else
536  return( 1 );
537 #endif
538  }
539  if( strcmp( str, "POLARSSL_ECP_DP_SECP384R1_ENABLED" ) == 0 )
540  {
541 #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
542  return( 0 );
543 #else
544  return( 1 );
545 #endif
546  }
547 
548 
549  return( 1 );
550 }
551 
552 int dispatch_test(int cnt, char *params[50])
553 {
554  int ret;
555  ((void) cnt);
556  ((void) params);
557 
558 #if defined(TEST_SUITE_ACTIVE)
559  if( strcmp( params[0], "ecdh_primitive_random" ) == 0 )
560  {
561 
562  int param1;
563 
564  if( cnt != 2 )
565  {
566  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
567  return( 2 );
568  }
569 
570  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
571 
572  test_suite_ecdh_primitive_random( param1 );
573  return ( 0 );
574 
575  return ( 3 );
576  }
577  else
578  if( strcmp( params[0], "ecdh_primitive_testvec" ) == 0 )
579  {
580 
581  int param1;
582  char *param2 = params[2];
583  char *param3 = params[3];
584  char *param4 = params[4];
585  char *param5 = params[5];
586  char *param6 = params[6];
587  char *param7 = params[7];
588  char *param8 = params[8];
589 
590  if( cnt != 9 )
591  {
592  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 9 );
593  return( 2 );
594  }
595 
596  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
597  if( verify_string( &param2 ) != 0 ) return( 2 );
598  if( verify_string( &param3 ) != 0 ) return( 2 );
599  if( verify_string( &param4 ) != 0 ) return( 2 );
600  if( verify_string( &param5 ) != 0 ) return( 2 );
601  if( verify_string( &param6 ) != 0 ) return( 2 );
602  if( verify_string( &param7 ) != 0 ) return( 2 );
603  if( verify_string( &param8 ) != 0 ) return( 2 );
604 
605  test_suite_ecdh_primitive_testvec( param1, param2, param3, param4, param5, param6, param7, param8 );
606  return ( 0 );
607 
608  return ( 3 );
609  }
610  else
611  if( strcmp( params[0], "ecdh_exchange" ) == 0 )
612  {
613 
614  int param1;
615 
616  if( cnt != 2 )
617  {
618  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
619  return( 2 );
620  }
621 
622  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
623 
624  test_suite_ecdh_exchange( param1 );
625  return ( 0 );
626 
627  return ( 3 );
628  }
629  else
630 
631  {
632  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
633  fflush( stdout );
634  return( 1 );
635  }
636 #else
637  return( 3 );
638 #endif
639  return( ret );
640 }
641 
642 int get_line( FILE *f, char *buf, size_t len )
643 {
644  char *ret;
645 
646  ret = fgets( buf, len, f );
647  if( ret == NULL )
648  return( -1 );
649 
650  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
651  buf[strlen(buf) - 1] = '\0';
652  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
653  buf[strlen(buf) - 1] = '\0';
654 
655  return( 0 );
656 }
657 
658 int parse_arguments( char *buf, size_t len, char *params[50] )
659 {
660  int cnt = 0, i;
661  char *cur = buf;
662  char *p = buf, *q;
663 
664  params[cnt++] = cur;
665 
666  while( *p != '\0' && p < buf + len )
667  {
668  if( *p == '\\' )
669  {
670  *p++;
671  *p++;
672  continue;
673  }
674  if( *p == ':' )
675  {
676  if( p + 1 < buf + len )
677  {
678  cur = p + 1;
679  params[cnt++] = cur;
680  }
681  *p = '\0';
682  }
683 
684  *p++;
685  }
686 
687  // Replace newlines, question marks and colons in strings
688  for( i = 0; i < cnt; i++ )
689  {
690  p = params[i];
691  q = params[i];
692 
693  while( *p != '\0' )
694  {
695  if( *p == '\\' && *(p + 1) == 'n' )
696  {
697  p += 2;
698  *(q++) = '\n';
699  }
700  else if( *p == '\\' && *(p + 1) == ':' )
701  {
702  p += 2;
703  *(q++) = ':';
704  }
705  else if( *p == '\\' && *(p + 1) == '?' )
706  {
707  p += 2;
708  *(q++) = '?';
709  }
710  else
711  *(q++) = *(p++);
712  }
713  *q = '\0';
714  }
715 
716  return( cnt );
717 }
718 
719 int main()
720 {
721  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
722  const char *filename = "/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_ecdh.data";
723  FILE *file;
724  char buf[5000];
725  char *params[50];
726 
727 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
728  unsigned char alloc_buf[1000000];
729  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
730 #endif
731 
732  file = fopen( filename, "r" );
733  if( file == NULL )
734  {
735  fprintf( stderr, "Failed to open\n" );
736  return( 1 );
737  }
738 
739  while( !feof( file ) )
740  {
741  int skip = 0;
742 
743  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
744  break;
745  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
746  fprintf( stdout, " " );
747  for( i = strlen( buf ) + 1; i < 67; i++ )
748  fprintf( stdout, "." );
749  fprintf( stdout, " " );
750  fflush( stdout );
751 
752  total_tests++;
753 
754  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
755  break;
756  cnt = parse_arguments( buf, strlen(buf), params );
757 
758  if( strcmp( params[0], "depends_on" ) == 0 )
759  {
760  for( i = 1; i < cnt; i++ )
761  if( dep_check( params[i] ) != 0 )
762  skip = 1;
763 
764  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
765  break;
766  cnt = parse_arguments( buf, strlen(buf), params );
767  }
768 
769  if( skip == 0 )
770  {
771  test_errors = 0;
772  ret = dispatch_test( cnt, params );
773  }
774 
775  if( skip == 1 || ret == 3 )
776  {
777  total_skipped++;
778  fprintf( stdout, "----\n" );
779  fflush( stdout );
780  }
781  else if( ret == 0 && test_errors == 0 )
782  {
783  fprintf( stdout, "PASS\n" );
784  fflush( stdout );
785  }
786  else if( ret == 2 )
787  {
788  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
789  fclose(file);
790  exit( 2 );
791  }
792  else
793  total_errors++;
794 
795  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
796  break;
797  if( strlen(buf) != 0 )
798  {
799  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
800  return( 1 );
801  }
802  }
803  fclose(file);
804 
805  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
806  if( total_errors == 0 )
807  fprintf( stdout, "PASSED" );
808  else
809  fprintf( stdout, "FAILED" );
810 
811  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
812  total_tests - total_errors, total_tests, total_skipped );
813 
814 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
815 #if defined(POLARSSL_MEMORY_DEBUG)
816  memory_buffer_alloc_status();
817 #endif
818  memory_buffer_alloc_free();
819 #endif
820 
821  return( total_errors != 0 );
822 }
823 
824 
int ecdh_make_params(ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Setup and write the ServerKeyExhange parameters.
static int test_errors
int ecdh_make_public(ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Setup and export the client&#39;s public value.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
Memory allocation layer.
uint32_t t_uint
Definition: bignum.h:149
int ecdh_calc_secret(ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Derive and export the shared secret.
Info structure for the pseudo random function.
int ecdh_read_public(ecdh_context *ctx, const unsigned char *buf, size_t blen)
Parse and import the client&#39;s public value.
int s
Definition: bignum.h:173
int ecdh_compute_shared(ecp_group *grp, mpi *z, const ecp_point *Q, const mpi *d, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Compute shared secret.
int ecdh_gen_public(ecp_group *grp, mpi *d, ecp_point *Q, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Generate a public key.
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
ECP group structure.
Definition: ecp.h:117
Configuration options (set of defines)
MPI structure.
Definition: bignum.h:171
static int test_assert(int correct, char *test)
#define PUT_UINT32_BE(n, b, i)
void mpi_init(mpi *X)
Initialize one MPI.
mpi X
Definition: ecp.h:96
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
int ecp_is_zero(ecp_point *pt)
Tell if a point is zero.
void ecp_point_init(ecp_point *pt)
Initialize a point (as zero)
void mpi_free(mpi *X)
Unallocate one MPI.
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
void ecp_group_free(ecp_group *grp)
Free the components of an ECP group.
mpi z
Definition: ecdh.h:45
static int unhexify(unsigned char *obuf, const char *ibuf)
int parse_arguments(char *buf, size_t len, char *params[50])
int ecdh_read_params(ecdh_context *ctx, const unsigned char **buf, const unsigned char *end)
Parse the ServerKeyExhange parameters.
Elliptic curve Diffie-Hellman.
ECDH context structure.
Definition: ecdh.h:39
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.
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
t_uint * p
Definition: bignum.h:175
int verify_string(char **str)
void ecp_group_init(ecp_group *grp)
Initialize a group (to something meaningless)
mpi Y
Definition: ecp.h:97
int dispatch_test(int cnt, char *params[50])
size_t n
Definition: bignum.h:174
unsigned char * buf
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
void ecdh_init(ecdh_context *ctx)
Initialize context.
int verify_int(char *str, int *value)
void ecdh_free(ecdh_context *ctx)
Free context.
ecp_group grp
Definition: ecdh.h:41
int get_line(FILE *f, char *buf, size_t len)
void ecp_point_free(ecp_point *pt)
Free the components of a point.