PolarSSL v1.3.9
test_suite_ecdh.c
Go to the documentation of this file.
1 #if !defined(POLARSSL_CONFIG_FILE)
2 #include <polarssl/config.h>
3 #else
4 #include POLARSSL_CONFIG_FILE
5 #endif
6 
7 #ifdef POLARSSL_ECDH_C
8 
9 #include <polarssl/ecdh.h>
10 #endif /* POLARSSL_ECDH_C */
11 
12 
13 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
14 #include "polarssl/memory.h"
15 #endif
16 
17 #if defined(POLARSSL_PLATFORM_C)
18 #include "polarssl/platform.h"
19 #else
20 #define polarssl_malloc malloc
21 #define polarssl_free free
22 #endif
23 
24 #ifdef _MSC_VER
25 #include <basetsd.h>
26 typedef UINT32 uint32_t;
27 #else
28 #include <inttypes.h>
29 #endif
30 
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <string.h>
34 
35 /*
36  * 32-bit integer manipulation macros (big endian)
37  */
38 #ifndef GET_UINT32_BE
39 #define GET_UINT32_BE(n,b,i) \
40 { \
41  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
42  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
43  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
44  | ( (uint32_t) (b)[(i) + 3] ); \
45 }
46 #endif
47 
48 #ifndef PUT_UINT32_BE
49 #define PUT_UINT32_BE(n,b,i) \
50 { \
51  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
52  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
53  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
54  (b)[(i) + 3] = (unsigned char) ( (n) ); \
55 }
56 #endif
57 
58 static int unhexify(unsigned char *obuf, const char *ibuf)
59 {
60  unsigned char c, c2;
61  int len = strlen(ibuf) / 2;
62  assert(!(strlen(ibuf) %1)); // must be even number of bytes
63 
64  while (*ibuf != 0)
65  {
66  c = *ibuf++;
67  if( c >= '0' && c <= '9' )
68  c -= '0';
69  else if( c >= 'a' && c <= 'f' )
70  c -= 'a' - 10;
71  else if( c >= 'A' && c <= 'F' )
72  c -= 'A' - 10;
73  else
74  assert( 0 );
75 
76  c2 = *ibuf++;
77  if( c2 >= '0' && c2 <= '9' )
78  c2 -= '0';
79  else if( c2 >= 'a' && c2 <= 'f' )
80  c2 -= 'a' - 10;
81  else if( c2 >= 'A' && c2 <= 'F' )
82  c2 -= 'A' - 10;
83  else
84  assert( 0 );
85 
86  *obuf++ = ( c << 4 ) | c2;
87  }
88 
89  return len;
90 }
91 
92 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
93 {
94  unsigned char l, h;
95 
96  while (len != 0)
97  {
98  h = (*ibuf) / 16;
99  l = (*ibuf) % 16;
100 
101  if( h < 10 )
102  *obuf++ = '0' + h;
103  else
104  *obuf++ = 'a' + h - 10;
105 
106  if( l < 10 )
107  *obuf++ = '0' + l;
108  else
109  *obuf++ = 'a' + l - 10;
110 
111  ++ibuf;
112  len--;
113  }
114 }
115 
123 static unsigned char *zero_alloc( size_t len )
124 {
125  void *p;
126  size_t actual_len = len != 0 ? len : 1;
127 
128  p = polarssl_malloc( actual_len );
129  assert( p != NULL );
130 
131  memset( p, 0x00, actual_len );
132 
133  return( p );
134 }
135 
146 static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
147 {
148  unsigned char *obuf;
149 
150  *olen = strlen(ibuf) / 2;
151 
152  if( *olen == 0 )
153  return( zero_alloc( *olen ) );
154 
155  obuf = polarssl_malloc( *olen );
156  assert( obuf != NULL );
157 
158  (void) unhexify( obuf, ibuf );
159 
160  return( obuf );
161 }
162 
172 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
173 {
174 #if !defined(__OpenBSD__)
175  size_t i;
176 
177  if( rng_state != NULL )
178  rng_state = NULL;
179 
180  for( i = 0; i < len; ++i )
181  output[i] = rand();
182 #else
183  if( rng_state != NULL )
184  rng_state = NULL;
185 
186  arc4random_buf( output, len );
187 #endif /* !OpenBSD */
188 
189  return( 0 );
190 }
191 
197 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
198 {
199  if( rng_state != NULL )
200  rng_state = NULL;
201 
202  memset( output, 0, len );
203 
204  return( 0 );
205 }
206 
207 typedef struct
208 {
209  unsigned char *buf;
210  size_t length;
211 } rnd_buf_info;
212 
224 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
225 {
226  rnd_buf_info *info = (rnd_buf_info *) rng_state;
227  size_t use_len;
228 
229  if( rng_state == NULL )
230  return( rnd_std_rand( NULL, output, len ) );
231 
232  use_len = len;
233  if( len > info->length )
234  use_len = info->length;
235 
236  if( use_len )
237  {
238  memcpy( output, info->buf, use_len );
239  info->buf += use_len;
240  info->length -= use_len;
241  }
242 
243  if( len - use_len > 0 )
244  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
245 
246  return( 0 );
247 }
248 
256 typedef struct
257 {
258  uint32_t key[16];
259  uint32_t v0, v1;
261 
270 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
271 {
272  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
273  uint32_t i, *k, sum, delta=0x9E3779B9;
274  unsigned char result[4], *out = output;
275 
276  if( rng_state == NULL )
277  return( rnd_std_rand( NULL, output, len ) );
278 
279  k = info->key;
280 
281  while( len > 0 )
282  {
283  size_t use_len = ( len > 4 ) ? 4 : len;
284  sum = 0;
285 
286  for( i = 0; i < 32; i++ )
287  {
288  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
289  sum += delta;
290  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
291  }
292 
293  PUT_UINT32_BE( info->v0, result, 0 );
294  memcpy( out, result, use_len );
295  len -= use_len;
296  out += 4;
297  }
298 
299  return( 0 );
300 }
301 
302 
303 #include <stdio.h>
304 #include <string.h>
305 
306 #if defined(POLARSSL_PLATFORM_C)
307 #include "polarssl/platform.h"
308 #else
309 #define polarssl_printf printf
310 #define polarssl_malloc malloc
311 #define polarssl_free free
312 #endif
313 
314 static int test_errors = 0;
315 
316 #ifdef POLARSSL_ECDH_C
317 
318 #define TEST_SUITE_ACTIVE
319 
320 static int test_assert( int correct, const char *test )
321 {
322  if( correct )
323  return( 0 );
324 
325  test_errors++;
326  if( test_errors == 1 )
327  printf( "FAILED\n" );
328  printf( " %s\n", test );
329 
330  return( 1 );
331 }
332 
333 #define TEST_ASSERT( TEST ) \
334  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
335  if( test_errors) goto exit; \
336  } while (0)
337 
338 int verify_string( char **str )
339 {
340  if( (*str)[0] != '"' ||
341  (*str)[strlen( *str ) - 1] != '"' )
342  {
343  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
344  return( -1 );
345  }
346 
347  (*str)++;
348  (*str)[strlen( *str ) - 1] = '\0';
349 
350  return( 0 );
351 }
352 
353 int verify_int( char *str, int *value )
354 {
355  size_t i;
356  int minus = 0;
357  int digits = 1;
358  int hex = 0;
359 
360  for( i = 0; i < strlen( str ); i++ )
361  {
362  if( i == 0 && str[i] == '-' )
363  {
364  minus = 1;
365  continue;
366  }
367 
368  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
369  str[i - 1] == '0' && str[i] == 'x' )
370  {
371  hex = 1;
372  continue;
373  }
374 
375  if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
376  ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
377  ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
378  {
379  digits = 0;
380  break;
381  }
382  }
383 
384  if( digits )
385  {
386  if( hex )
387  *value = strtol( str, NULL, 16 );
388  else
389  *value = strtol( str, NULL, 10 );
390 
391  return( 0 );
392  }
393 
394  if( strcmp( str, "POLARSSL_ECP_DP_SECP256R1" ) == 0 )
395  {
396  *value = ( POLARSSL_ECP_DP_SECP256R1 );
397  return( 0 );
398  }
399  if( strcmp( str, "POLARSSL_ECP_DP_SECP224R1" ) == 0 )
400  {
401  *value = ( POLARSSL_ECP_DP_SECP224R1 );
402  return( 0 );
403  }
404  if( strcmp( str, "POLARSSL_ECP_DP_SECP521R1" ) == 0 )
405  {
406  *value = ( POLARSSL_ECP_DP_SECP521R1 );
407  return( 0 );
408  }
409  if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1" ) == 0 )
410  {
411  *value = ( POLARSSL_ECP_DP_SECP192R1 );
412  return( 0 );
413  }
414  if( strcmp( str, "POLARSSL_ECP_DP_SECP384R1" ) == 0 )
415  {
416  *value = ( POLARSSL_ECP_DP_SECP384R1 );
417  return( 0 );
418  }
419 
420 
421  printf( "Expected integer for parameter and got: %s\n", str );
422  return( -1 );
423 }
424 
425 void test_suite_ecdh_primitive_random( int id )
426 {
427  ecp_group grp;
428  ecp_point qA, qB;
429  mpi dA, dB, zA, zB;
430  rnd_pseudo_info rnd_info;
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 );
436  memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
437 
438  TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
439 
440  TEST_ASSERT( ecdh_gen_public( &grp, &dA, &qA, &rnd_pseudo_rand, &rnd_info )
441  == 0 );
442  TEST_ASSERT( ecdh_gen_public( &grp, &dB, &qB, &rnd_pseudo_rand, &rnd_info )
443  == 0 );
444  TEST_ASSERT( ecdh_compute_shared( &grp, &zA, &qB, &dA,
445  &rnd_pseudo_rand, &rnd_info ) == 0 );
446  TEST_ASSERT( ecdh_compute_shared( &grp, &zB, &qA, &dB,
447  NULL, NULL ) == 0 );
448 
449  TEST_ASSERT( mpi_cmp_mpi( &zA, &zB ) == 0 );
450 
451 exit:
452  ecp_group_free( &grp );
453  ecp_point_free( &qA ); ecp_point_free( &qB );
454  mpi_free( &dA ); mpi_free( &dB );
455  mpi_free( &zA ); mpi_free( &zB );
456 }
457 
458 void test_suite_ecdh_primitive_testvec( int id, char *dA_str, char *xA_str, char *yA_str,
459  char *dB_str, char *xB_str, char *yB_str,
460  char *z_str )
461 {
462  ecp_group grp;
463  ecp_point qA, qB;
464  mpi dA, dB, zA, zB, check;
465  unsigned char rnd_buf_A[POLARSSL_ECP_MAX_BYTES];
466  unsigned char rnd_buf_B[POLARSSL_ECP_MAX_BYTES];
467  rnd_buf_info rnd_info_A, rnd_info_B;
468 
469  ecp_group_init( &grp );
470  ecp_point_init( &qA ); ecp_point_init( &qB );
471  mpi_init( &dA ); mpi_init( &dB );
472  mpi_init( &zA ); mpi_init( &zB ); mpi_init( &check );
473 
474  TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
475 
476  rnd_info_A.buf = rnd_buf_A;
477  rnd_info_A.length = unhexify( rnd_buf_A, dA_str );
478 
479  /* Fix rnd_buf_A by shifting it left if necessary */
480  if( grp.nbits % 8 != 0 )
481  {
482  unsigned char shift = 8 - ( grp.nbits % 8 );
483  size_t i;
484 
485  for( i = 0; i < rnd_info_A.length - 1; i++ )
486  rnd_buf_A[i] = rnd_buf_A[i] << shift
487  | rnd_buf_A[i+1] >> ( 8 - shift );
488 
489  rnd_buf_A[rnd_info_A.length-1] <<= shift;
490  }
491 
492  rnd_info_B.buf = rnd_buf_B;
493  rnd_info_B.length = unhexify( rnd_buf_B, dB_str );
494 
495  /* Fix rnd_buf_B by shifting it left if necessary */
496  if( grp.nbits % 8 != 0 )
497  {
498  unsigned char shift = 8 - ( grp.nbits % 8 );
499  size_t i;
500 
501  for( i = 0; i < rnd_info_B.length - 1; i++ )
502  rnd_buf_B[i] = rnd_buf_B[i] << shift
503  | rnd_buf_B[i+1] >> ( 8 - shift );
504 
505  rnd_buf_B[rnd_info_B.length-1] <<= shift;
506  }
507 
508  TEST_ASSERT( ecdh_gen_public( &grp, &dA, &qA,
509  rnd_buffer_rand, &rnd_info_A ) == 0 );
510  TEST_ASSERT( ! ecp_is_zero( &qA ) );
511  TEST_ASSERT( mpi_read_string( &check, 16, xA_str ) == 0 );
512  TEST_ASSERT( mpi_cmp_mpi( &qA.X, &check ) == 0 );
513  TEST_ASSERT( mpi_read_string( &check, 16, yA_str ) == 0 );
514  TEST_ASSERT( mpi_cmp_mpi( &qA.Y, &check ) == 0 );
515 
516  TEST_ASSERT( ecdh_gen_public( &grp, &dB, &qB,
517  rnd_buffer_rand, &rnd_info_B ) == 0 );
518  TEST_ASSERT( ! ecp_is_zero( &qB ) );
519  TEST_ASSERT( mpi_read_string( &check, 16, xB_str ) == 0 );
520  TEST_ASSERT( mpi_cmp_mpi( &qB.X, &check ) == 0 );
521  TEST_ASSERT( mpi_read_string( &check, 16, yB_str ) == 0 );
522  TEST_ASSERT( mpi_cmp_mpi( &qB.Y, &check ) == 0 );
523 
524  TEST_ASSERT( mpi_read_string( &check, 16, z_str ) == 0 );
525  TEST_ASSERT( ecdh_compute_shared( &grp, &zA, &qB, &dA, NULL, NULL ) == 0 );
526  TEST_ASSERT( mpi_cmp_mpi( &zA, &check ) == 0 );
527  TEST_ASSERT( ecdh_compute_shared( &grp, &zB, &qA, &dB, NULL, NULL ) == 0 );
528  TEST_ASSERT( mpi_cmp_mpi( &zB, &check ) == 0 );
529 
530 exit:
531  ecp_group_free( &grp );
532  ecp_point_free( &qA ); ecp_point_free( &qB );
533  mpi_free( &dA ); mpi_free( &dB );
534  mpi_free( &zA ); mpi_free( &zB ); mpi_free( &check );
535 }
536 
537 void test_suite_ecdh_exchange( int id )
538 {
539  ecdh_context srv, cli;
540  unsigned char buf[1000];
541  const unsigned char *vbuf;
542  size_t len;
543  rnd_pseudo_info rnd_info;
544 
545  ecdh_init( &srv );
546  ecdh_init( &cli );
547  memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
548 
549  TEST_ASSERT( ecp_use_known_dp( &srv.grp, id ) == 0 );
550 
551  memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
552  TEST_ASSERT( ecdh_make_params( &srv, &len, buf, 1000,
553  &rnd_pseudo_rand, &rnd_info ) == 0 );
554  TEST_ASSERT( ecdh_read_params( &cli, &vbuf, buf + len ) == 0 );
555 
556  memset( buf, 0x00, sizeof( buf ) );
557  TEST_ASSERT( ecdh_make_public( &cli, &len, buf, 1000,
558  &rnd_pseudo_rand, &rnd_info ) == 0 );
559  TEST_ASSERT( ecdh_read_public( &srv, buf, len ) == 0 );
560 
561  TEST_ASSERT( ecdh_calc_secret( &srv, &len, buf, 1000,
562  &rnd_pseudo_rand, &rnd_info ) == 0 );
563  TEST_ASSERT( ecdh_calc_secret( &cli, &len, buf, 1000, NULL, NULL ) == 0 );
564  TEST_ASSERT( mpi_cmp_mpi( &srv.z, &cli.z ) == 0 );
565 
566 exit:
567  ecdh_free( &srv );
568  ecdh_free( &cli );
569 }
570 
571 
572 #endif /* POLARSSL_ECDH_C */
573 
574 
575 int dep_check( char *str )
576 {
577  if( str == NULL )
578  return( 1 );
579 
580  if( strcmp( str, "POLARSSL_ECP_DP_SECP521R1_ENABLED" ) == 0 )
581  {
582 #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
583  return( 0 );
584 #else
585  return( 1 );
586 #endif
587  }
588  if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
589  {
590 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
591  return( 0 );
592 #else
593  return( 1 );
594 #endif
595  }
596  if( strcmp( str, "POLARSSL_ECP_DP_SECP384R1_ENABLED" ) == 0 )
597  {
598 #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
599  return( 0 );
600 #else
601  return( 1 );
602 #endif
603  }
604  if( strcmp( str, "POLARSSL_ECP_DP_SECP256R1_ENABLED" ) == 0 )
605  {
606 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
607  return( 0 );
608 #else
609  return( 1 );
610 #endif
611  }
612  if( strcmp( str, "POLARSSL_ECP_DP_SECP224R1_ENABLED" ) == 0 )
613  {
614 #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
615  return( 0 );
616 #else
617  return( 1 );
618 #endif
619  }
620 
621 
622  return( 1 );
623 }
624 
625 int dispatch_test(int cnt, char *params[50])
626 {
627  int ret;
628  ((void) cnt);
629  ((void) params);
630 
631 #if defined(TEST_SUITE_ACTIVE)
632  if( strcmp( params[0], "ecdh_primitive_random" ) == 0 )
633  {
634 
635  int param1;
636 
637  if( cnt != 2 )
638  {
639  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
640  return( 2 );
641  }
642 
643  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
644 
645  test_suite_ecdh_primitive_random( param1 );
646  return ( 0 );
647 
648  return ( 3 );
649  }
650  else
651  if( strcmp( params[0], "ecdh_primitive_testvec" ) == 0 )
652  {
653 
654  int param1;
655  char *param2 = params[2];
656  char *param3 = params[3];
657  char *param4 = params[4];
658  char *param5 = params[5];
659  char *param6 = params[6];
660  char *param7 = params[7];
661  char *param8 = params[8];
662 
663  if( cnt != 9 )
664  {
665  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 9 );
666  return( 2 );
667  }
668 
669  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
670  if( verify_string( &param2 ) != 0 ) return( 2 );
671  if( verify_string( &param3 ) != 0 ) return( 2 );
672  if( verify_string( &param4 ) != 0 ) return( 2 );
673  if( verify_string( &param5 ) != 0 ) return( 2 );
674  if( verify_string( &param6 ) != 0 ) return( 2 );
675  if( verify_string( &param7 ) != 0 ) return( 2 );
676  if( verify_string( &param8 ) != 0 ) return( 2 );
677 
678  test_suite_ecdh_primitive_testvec( param1, param2, param3, param4, param5, param6, param7, param8 );
679  return ( 0 );
680 
681  return ( 3 );
682  }
683  else
684  if( strcmp( params[0], "ecdh_exchange" ) == 0 )
685  {
686 
687  int param1;
688 
689  if( cnt != 2 )
690  {
691  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
692  return( 2 );
693  }
694 
695  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
696 
697  test_suite_ecdh_exchange( param1 );
698  return ( 0 );
699 
700  return ( 3 );
701  }
702  else
703 
704  {
705  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
706  fflush( stdout );
707  return( 1 );
708  }
709 #else
710  return( 3 );
711 #endif
712  return( ret );
713 }
714 
715 int get_line( FILE *f, char *buf, size_t len )
716 {
717  char *ret;
718 
719  ret = fgets( buf, len, f );
720  if( ret == NULL )
721  return( -1 );
722 
723  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
724  buf[strlen(buf) - 1] = '\0';
725  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
726  buf[strlen(buf) - 1] = '\0';
727 
728  return( 0 );
729 }
730 
731 int parse_arguments( char *buf, size_t len, char *params[50] )
732 {
733  int cnt = 0, i;
734  char *cur = buf;
735  char *p = buf, *q;
736 
737  params[cnt++] = cur;
738 
739  while( *p != '\0' && p < buf + len )
740  {
741  if( *p == '\\' )
742  {
743  p++;
744  p++;
745  continue;
746  }
747  if( *p == ':' )
748  {
749  if( p + 1 < buf + len )
750  {
751  cur = p + 1;
752  params[cnt++] = cur;
753  }
754  *p = '\0';
755  }
756 
757  p++;
758  }
759 
760  // Replace newlines, question marks and colons in strings
761  for( i = 0; i < cnt; i++ )
762  {
763  p = params[i];
764  q = params[i];
765 
766  while( *p != '\0' )
767  {
768  if( *p == '\\' && *(p + 1) == 'n' )
769  {
770  p += 2;
771  *(q++) = '\n';
772  }
773  else if( *p == '\\' && *(p + 1) == ':' )
774  {
775  p += 2;
776  *(q++) = ':';
777  }
778  else if( *p == '\\' && *(p + 1) == '?' )
779  {
780  p += 2;
781  *(q++) = '?';
782  }
783  else
784  *(q++) = *(p++);
785  }
786  *q = '\0';
787  }
788 
789  return( cnt );
790 }
791 
792 int main()
793 {
794  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
795  const char *filename = "/home/iurt/rpmbuild/BUILD/polarssl-1.3.9/tests/suites/test_suite_ecdh.data";
796  FILE *file;
797  char buf[5000];
798  char *params[50];
799 
800 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
801  unsigned char alloc_buf[1000000];
802  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
803 #endif
804 
805  file = fopen( filename, "r" );
806  if( file == NULL )
807  {
808  fprintf( stderr, "Failed to open\n" );
809  return( 1 );
810  }
811 
812  while( !feof( file ) )
813  {
814  int skip = 0;
815 
816  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
817  break;
818  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
819  fprintf( stdout, " " );
820  for( i = strlen( buf ) + 1; i < 67; i++ )
821  fprintf( stdout, "." );
822  fprintf( stdout, " " );
823  fflush( stdout );
824 
825  total_tests++;
826 
827  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
828  break;
829  cnt = parse_arguments( buf, strlen(buf), params );
830 
831  if( strcmp( params[0], "depends_on" ) == 0 )
832  {
833  for( i = 1; i < cnt; i++ )
834  if( dep_check( params[i] ) != 0 )
835  skip = 1;
836 
837  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
838  break;
839  cnt = parse_arguments( buf, strlen(buf), params );
840  }
841 
842  if( skip == 0 )
843  {
844  test_errors = 0;
845  ret = dispatch_test( cnt, params );
846  }
847 
848  if( skip == 1 || ret == 3 )
849  {
850  total_skipped++;
851  fprintf( stdout, "----\n" );
852  fflush( stdout );
853  }
854  else if( ret == 0 && test_errors == 0 )
855  {
856  fprintf( stdout, "PASS\n" );
857  fflush( stdout );
858  }
859  else if( ret == 2 )
860  {
861  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
862  fclose(file);
863  exit( 2 );
864  }
865  else
866  total_errors++;
867 
868  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
869  break;
870  if( strlen(buf) != 0 )
871  {
872  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
873  return( 1 );
874  }
875  }
876  fclose(file);
877 
878  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
879  if( total_errors == 0 )
880  fprintf( stdout, "PASSED" );
881  else
882  fprintf( stdout, "FAILED" );
883 
884  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
885  total_tests - total_errors, total_tests, total_skipped );
886 
887 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
888 #if defined(POLARSSL_MEMORY_DEBUG)
889  memory_buffer_alloc_status();
890 #endif
892 #endif
893 
894  return( total_errors != 0 );
895 }
896 
897 
int dep_check(char *str)
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)
Generate a public key and a TLS ServerKeyExchange payload.
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)
Generate a public key and a TLS ClientKeyExchange payload.
int get_line(FILE *f, char *buf, size_t len)
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
Memory allocation layer (Deprecated to platform layer)
#define polarssl_malloc
int dispatch_test(int cnt, char *params[50])
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.
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
int ecdh_read_public(ecdh_context *ctx, const unsigned char *buf, size_t blen)
Parse and process a TLS ClientKeyExchange payload.
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
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 Raw function that only does the core computation.
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:136
Configuration options (set of defines)
MPI structure.
Definition: bignum.h:182
PolarSSL Platform abstraction layer.
static int test_assert(int correct, const char *test)
#define PUT_UINT32_BE(n, b, i)
void mpi_init(mpi *X)
Initialize one MPI.
mpi X
Definition: ecp.h:106
int mpi_cmp_mpi(const mpi *X, const mpi *Y)
Compare signed values.
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
#define TEST_ASSERT(TEST)
ECP point structure (jacobian coordinates)
Definition: ecp.h:104
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.
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
void ecp_group_free(ecp_group *grp)
Free the components of an ECP group.
mpi z
Definition: ecdh.h:54
static int unhexify(unsigned char *obuf, const char *ibuf)
int ecdh_read_params(ecdh_context *ctx, const unsigned char **buf, const unsigned char *end)
Parse and procress a TLS ServerKeyExhange payload.
Elliptic curve Diffie-Hellman.
#define POLARSSL_ECP_MAX_BYTES
Definition: ecp.h:186
ECDH context structure.
Definition: ecdh.h:48
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.
int verify_string(char **str)
void ecp_group_init(ecp_group *grp)
Initialize a group (to something meaningless)
size_t nbits
Definition: ecp.h:145
mpi Y
Definition: ecp.h:107
int parse_arguments(char *buf, size_t len, char *params[50])
unsigned char * buf
int main()
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:50
void ecp_point_free(ecp_point *pt)
Free the components of a point.