PolarSSL v1.3.9
test_suite_dhm.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_DHM_C
8 #ifdef POLARSSL_BIGNUM_C
9 
10 #include <polarssl/dhm.h>
11 #endif /* POLARSSL_DHM_C */
12 #endif /* POLARSSL_BIGNUM_C */
13 
14 
15 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
16 #include "polarssl/memory.h"
17 #endif
18 
19 #if defined(POLARSSL_PLATFORM_C)
20 #include "polarssl/platform.h"
21 #else
22 #define polarssl_malloc malloc
23 #define polarssl_free free
24 #endif
25 
26 #ifdef _MSC_VER
27 #include <basetsd.h>
28 typedef UINT32 uint32_t;
29 #else
30 #include <inttypes.h>
31 #endif
32 
33 #include <assert.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 /*
38  * 32-bit integer manipulation macros (big endian)
39  */
40 #ifndef GET_UINT32_BE
41 #define GET_UINT32_BE(n,b,i) \
42 { \
43  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
44  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
45  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
46  | ( (uint32_t) (b)[(i) + 3] ); \
47 }
48 #endif
49 
50 #ifndef PUT_UINT32_BE
51 #define PUT_UINT32_BE(n,b,i) \
52 { \
53  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
54  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
55  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
56  (b)[(i) + 3] = (unsigned char) ( (n) ); \
57 }
58 #endif
59 
60 static int unhexify(unsigned char *obuf, const char *ibuf)
61 {
62  unsigned char c, c2;
63  int len = strlen(ibuf) / 2;
64  assert(!(strlen(ibuf) %1)); // must be even number of bytes
65 
66  while (*ibuf != 0)
67  {
68  c = *ibuf++;
69  if( c >= '0' && c <= '9' )
70  c -= '0';
71  else if( c >= 'a' && c <= 'f' )
72  c -= 'a' - 10;
73  else if( c >= 'A' && c <= 'F' )
74  c -= 'A' - 10;
75  else
76  assert( 0 );
77 
78  c2 = *ibuf++;
79  if( c2 >= '0' && c2 <= '9' )
80  c2 -= '0';
81  else if( c2 >= 'a' && c2 <= 'f' )
82  c2 -= 'a' - 10;
83  else if( c2 >= 'A' && c2 <= 'F' )
84  c2 -= 'A' - 10;
85  else
86  assert( 0 );
87 
88  *obuf++ = ( c << 4 ) | c2;
89  }
90 
91  return len;
92 }
93 
94 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
95 {
96  unsigned char l, h;
97 
98  while (len != 0)
99  {
100  h = (*ibuf) / 16;
101  l = (*ibuf) % 16;
102 
103  if( h < 10 )
104  *obuf++ = '0' + h;
105  else
106  *obuf++ = 'a' + h - 10;
107 
108  if( l < 10 )
109  *obuf++ = '0' + l;
110  else
111  *obuf++ = 'a' + l - 10;
112 
113  ++ibuf;
114  len--;
115  }
116 }
117 
125 static unsigned char *zero_alloc( size_t len )
126 {
127  void *p;
128  size_t actual_len = len != 0 ? len : 1;
129 
130  p = polarssl_malloc( actual_len );
131  assert( p != NULL );
132 
133  memset( p, 0x00, actual_len );
134 
135  return( p );
136 }
137 
148 static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
149 {
150  unsigned char *obuf;
151 
152  *olen = strlen(ibuf) / 2;
153 
154  if( *olen == 0 )
155  return( zero_alloc( *olen ) );
156 
157  obuf = polarssl_malloc( *olen );
158  assert( obuf != NULL );
159 
160  (void) unhexify( obuf, ibuf );
161 
162  return( obuf );
163 }
164 
174 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
175 {
176 #if !defined(__OpenBSD__)
177  size_t i;
178 
179  if( rng_state != NULL )
180  rng_state = NULL;
181 
182  for( i = 0; i < len; ++i )
183  output[i] = rand();
184 #else
185  if( rng_state != NULL )
186  rng_state = NULL;
187 
188  arc4random_buf( output, len );
189 #endif /* !OpenBSD */
190 
191  return( 0 );
192 }
193 
199 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
200 {
201  if( rng_state != NULL )
202  rng_state = NULL;
203 
204  memset( output, 0, len );
205 
206  return( 0 );
207 }
208 
209 typedef struct
210 {
211  unsigned char *buf;
212  size_t length;
213 } rnd_buf_info;
214 
226 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
227 {
228  rnd_buf_info *info = (rnd_buf_info *) rng_state;
229  size_t use_len;
230 
231  if( rng_state == NULL )
232  return( rnd_std_rand( NULL, output, len ) );
233 
234  use_len = len;
235  if( len > info->length )
236  use_len = info->length;
237 
238  if( use_len )
239  {
240  memcpy( output, info->buf, use_len );
241  info->buf += use_len;
242  info->length -= use_len;
243  }
244 
245  if( len - use_len > 0 )
246  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
247 
248  return( 0 );
249 }
250 
258 typedef struct
259 {
260  uint32_t key[16];
261  uint32_t v0, v1;
263 
272 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
273 {
274  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
275  uint32_t i, *k, sum, delta=0x9E3779B9;
276  unsigned char result[4], *out = output;
277 
278  if( rng_state == NULL )
279  return( rnd_std_rand( NULL, output, len ) );
280 
281  k = info->key;
282 
283  while( len > 0 )
284  {
285  size_t use_len = ( len > 4 ) ? 4 : len;
286  sum = 0;
287 
288  for( i = 0; i < 32; i++ )
289  {
290  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
291  sum += delta;
292  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
293  }
294 
295  PUT_UINT32_BE( info->v0, result, 0 );
296  memcpy( out, result, use_len );
297  len -= use_len;
298  out += 4;
299  }
300 
301  return( 0 );
302 }
303 
304 
305 #include <stdio.h>
306 #include <string.h>
307 
308 #if defined(POLARSSL_PLATFORM_C)
309 #include "polarssl/platform.h"
310 #else
311 #define polarssl_printf printf
312 #define polarssl_malloc malloc
313 #define polarssl_free free
314 #endif
315 
316 static int test_errors = 0;
317 
318 #ifdef POLARSSL_DHM_C
319 #ifdef POLARSSL_BIGNUM_C
320 
321 #define TEST_SUITE_ACTIVE
322 
323 static int test_assert( int correct, const char *test )
324 {
325  if( correct )
326  return( 0 );
327 
328  test_errors++;
329  if( test_errors == 1 )
330  printf( "FAILED\n" );
331  printf( " %s\n", test );
332 
333  return( 1 );
334 }
335 
336 #define TEST_ASSERT( TEST ) \
337  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
338  if( test_errors) goto exit; \
339  } while (0)
340 
341 int verify_string( char **str )
342 {
343  if( (*str)[0] != '"' ||
344  (*str)[strlen( *str ) - 1] != '"' )
345  {
346  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
347  return( -1 );
348  }
349 
350  (*str)++;
351  (*str)[strlen( *str ) - 1] = '\0';
352 
353  return( 0 );
354 }
355 
356 int verify_int( char *str, int *value )
357 {
358  size_t i;
359  int minus = 0;
360  int digits = 1;
361  int hex = 0;
362 
363  for( i = 0; i < strlen( str ); i++ )
364  {
365  if( i == 0 && str[i] == '-' )
366  {
367  minus = 1;
368  continue;
369  }
370 
371  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
372  str[i - 1] == '0' && str[i] == 'x' )
373  {
374  hex = 1;
375  continue;
376  }
377 
378  if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
379  ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
380  ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
381  {
382  digits = 0;
383  break;
384  }
385  }
386 
387  if( digits )
388  {
389  if( hex )
390  *value = strtol( str, NULL, 16 );
391  else
392  *value = strtol( str, NULL, 10 );
393 
394  return( 0 );
395  }
396 
397 
398 
399  printf( "Expected integer for parameter and got: %s\n", str );
400  return( -1 );
401 }
402 
403 void test_suite_dhm_do_dhm( int radix_P, char *input_P,
404  int radix_G, char *input_G )
405 {
406  dhm_context ctx_srv;
407  dhm_context ctx_cli;
408  unsigned char ske[1000];
409  unsigned char *p = ske;
410  unsigned char pub_cli[1000];
411  unsigned char sec_srv[1000];
412  unsigned char sec_cli[1000];
413  size_t ske_len = 0;
414  size_t pub_cli_len = 0;
415  size_t sec_srv_len = 1000;
416  size_t sec_cli_len = 1000;
417  int x_size, i;
418  rnd_pseudo_info rnd_info;
419 
420  dhm_init( &ctx_srv );
421  dhm_init( &ctx_cli );
422  memset( ske, 0x00, 1000 );
423  memset( pub_cli, 0x00, 1000 );
424  memset( sec_srv, 0x00, 1000 );
425  memset( sec_cli, 0x00, 1000 );
426  memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
427 
428  /*
429  * Set params
430  */
431  TEST_ASSERT( mpi_read_string( &ctx_srv.P, radix_P, input_P ) == 0 );
432  TEST_ASSERT( mpi_read_string( &ctx_srv.G, radix_G, input_G ) == 0 );
433  x_size = mpi_size( &ctx_srv.P );
434  pub_cli_len = x_size;
435 
436  /*
437  * First key exchange
438  */
439  TEST_ASSERT( dhm_make_params( &ctx_srv, x_size, ske, &ske_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
440  ske[ske_len++] = 0;
441  ske[ske_len++] = 0;
442  TEST_ASSERT( dhm_read_params( &ctx_cli, &p, ske + ske_len ) == 0 );
443 
444  TEST_ASSERT( dhm_make_public( &ctx_cli, x_size, pub_cli, pub_cli_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
445  TEST_ASSERT( dhm_read_public( &ctx_srv, pub_cli, pub_cli_len ) == 0 );
446 
447  TEST_ASSERT( dhm_calc_secret( &ctx_srv, sec_srv, &sec_srv_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
448  TEST_ASSERT( dhm_calc_secret( &ctx_cli, sec_cli, &sec_cli_len, NULL, NULL ) == 0 );
449 
450  TEST_ASSERT( sec_srv_len == sec_cli_len );
451  TEST_ASSERT( sec_srv_len != 0 );
452  TEST_ASSERT( memcmp( sec_srv, sec_cli, sec_srv_len ) == 0 );
453 
454  /* Re-do calc_secret on server a few times to test update of blinding values */
455  for( i = 0; i < 3; i++ )
456  {
457  sec_srv_len = 1000;
458  TEST_ASSERT( dhm_calc_secret( &ctx_srv, sec_srv, &sec_srv_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
459 
460  TEST_ASSERT( sec_srv_len == sec_cli_len );
461  TEST_ASSERT( sec_srv_len != 0 );
462  TEST_ASSERT( memcmp( sec_srv, sec_cli, sec_srv_len ) == 0 );
463  }
464 
465  /*
466  * Second key exchange to test change of blinding values on server
467  */
468  sec_cli_len = 1000;
469  sec_srv_len = 1000;
470  p = ske;
471 
472  TEST_ASSERT( dhm_make_params( &ctx_srv, x_size, ske, &ske_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
473  ske[ske_len++] = 0;
474  ske[ske_len++] = 0;
475  TEST_ASSERT( dhm_read_params( &ctx_cli, &p, ske + ske_len ) == 0 );
476 
477  TEST_ASSERT( dhm_make_public( &ctx_cli, x_size, pub_cli, pub_cli_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
478  TEST_ASSERT( dhm_read_public( &ctx_srv, pub_cli, pub_cli_len ) == 0 );
479 
480  TEST_ASSERT( dhm_calc_secret( &ctx_srv, sec_srv, &sec_srv_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
481  TEST_ASSERT( dhm_calc_secret( &ctx_cli, sec_cli, &sec_cli_len, NULL, NULL ) == 0 );
482 
483  TEST_ASSERT( sec_srv_len == sec_cli_len );
484  TEST_ASSERT( sec_srv_len != 0 );
485  TEST_ASSERT( memcmp( sec_srv, sec_cli, sec_srv_len ) == 0 );
486 
487 exit:
488  dhm_free( &ctx_srv );
489  dhm_free( &ctx_cli );
490 }
491 
492 #ifdef POLARSSL_FS_IO
493 void test_suite_dhm_file( char *filename, char *p, char *g, int len )
494 {
495  dhm_context ctx;
496  mpi P, G;
497 
498  dhm_init( &ctx );
499  mpi_init( &P ); mpi_init( &G );
500 
501  TEST_ASSERT( mpi_read_string( &P, 16, p ) == 0 );
502  TEST_ASSERT( mpi_read_string( &G, 16, g ) == 0 );
503 
504  TEST_ASSERT( dhm_parse_dhmfile( &ctx, filename ) == 0 );
505 
506  TEST_ASSERT( ctx.len == (size_t) len );
507  TEST_ASSERT( mpi_cmp_mpi( &ctx.P, &P ) == 0 );
508  TEST_ASSERT( mpi_cmp_mpi( &ctx.G, &G ) == 0 );
509 
510 exit:
511  mpi_free( &P ); mpi_free( &G );
512  dhm_free( &ctx );
513 }
514 #endif /* POLARSSL_FS_IO */
515 
516 #ifdef POLARSSL_SELF_TEST
517 void test_suite_dhm_selftest()
518 {
519  TEST_ASSERT( dhm_self_test( 0 ) == 0 );
520 
521 exit:
522  return;
523 }
524 #endif /* POLARSSL_SELF_TEST */
525 
526 
527 #endif /* POLARSSL_DHM_C */
528 #endif /* POLARSSL_BIGNUM_C */
529 
530 
531 int dep_check( char *str )
532 {
533  if( str == NULL )
534  return( 1 );
535 
536 
537 
538  return( 1 );
539 }
540 
541 int dispatch_test(int cnt, char *params[50])
542 {
543  int ret;
544  ((void) cnt);
545  ((void) params);
546 
547 #if defined(TEST_SUITE_ACTIVE)
548  if( strcmp( params[0], "dhm_do_dhm" ) == 0 )
549  {
550 
551  int param1;
552  char *param2 = params[2];
553  int param3;
554  char *param4 = params[4];
555 
556  if( cnt != 5 )
557  {
558  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
559  return( 2 );
560  }
561 
562  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
563  if( verify_string( &param2 ) != 0 ) return( 2 );
564  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
565  if( verify_string( &param4 ) != 0 ) return( 2 );
566 
567  test_suite_dhm_do_dhm( param1, param2, param3, param4 );
568  return ( 0 );
569 
570  return ( 3 );
571  }
572  else
573  if( strcmp( params[0], "dhm_file" ) == 0 )
574  {
575  #ifdef POLARSSL_FS_IO
576 
577  char *param1 = params[1];
578  char *param2 = params[2];
579  char *param3 = params[3];
580  int param4;
581 
582  if( cnt != 5 )
583  {
584  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
585  return( 2 );
586  }
587 
588  if( verify_string( &param1 ) != 0 ) return( 2 );
589  if( verify_string( &param2 ) != 0 ) return( 2 );
590  if( verify_string( &param3 ) != 0 ) return( 2 );
591  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
592 
593  test_suite_dhm_file( param1, param2, param3, param4 );
594  return ( 0 );
595  #endif /* POLARSSL_FS_IO */
596 
597  return ( 3 );
598  }
599  else
600  if( strcmp( params[0], "dhm_selftest" ) == 0 )
601  {
602  #ifdef POLARSSL_SELF_TEST
603 
604 
605  if( cnt != 1 )
606  {
607  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
608  return( 2 );
609  }
610 
611 
612  test_suite_dhm_selftest( );
613  return ( 0 );
614  #endif /* POLARSSL_SELF_TEST */
615 
616  return ( 3 );
617  }
618  else
619 
620  {
621  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
622  fflush( stdout );
623  return( 1 );
624  }
625 #else
626  return( 3 );
627 #endif
628  return( ret );
629 }
630 
631 int get_line( FILE *f, char *buf, size_t len )
632 {
633  char *ret;
634 
635  ret = fgets( buf, len, f );
636  if( ret == NULL )
637  return( -1 );
638 
639  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
640  buf[strlen(buf) - 1] = '\0';
641  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
642  buf[strlen(buf) - 1] = '\0';
643 
644  return( 0 );
645 }
646 
647 int parse_arguments( char *buf, size_t len, char *params[50] )
648 {
649  int cnt = 0, i;
650  char *cur = buf;
651  char *p = buf, *q;
652 
653  params[cnt++] = cur;
654 
655  while( *p != '\0' && p < buf + len )
656  {
657  if( *p == '\\' )
658  {
659  p++;
660  p++;
661  continue;
662  }
663  if( *p == ':' )
664  {
665  if( p + 1 < buf + len )
666  {
667  cur = p + 1;
668  params[cnt++] = cur;
669  }
670  *p = '\0';
671  }
672 
673  p++;
674  }
675 
676  // Replace newlines, question marks and colons in strings
677  for( i = 0; i < cnt; i++ )
678  {
679  p = params[i];
680  q = params[i];
681 
682  while( *p != '\0' )
683  {
684  if( *p == '\\' && *(p + 1) == 'n' )
685  {
686  p += 2;
687  *(q++) = '\n';
688  }
689  else if( *p == '\\' && *(p + 1) == ':' )
690  {
691  p += 2;
692  *(q++) = ':';
693  }
694  else if( *p == '\\' && *(p + 1) == '?' )
695  {
696  p += 2;
697  *(q++) = '?';
698  }
699  else
700  *(q++) = *(p++);
701  }
702  *q = '\0';
703  }
704 
705  return( cnt );
706 }
707 
708 int main()
709 {
710  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
711  const char *filename = "/tmp/B.9tfrh6rj/BUILD/polarssl-1.3.9/tests/suites/test_suite_dhm.data";
712  FILE *file;
713  char buf[5000];
714  char *params[50];
715 
716 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
717  unsigned char alloc_buf[1000000];
718  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
719 #endif
720 
721  file = fopen( filename, "r" );
722  if( file == NULL )
723  {
724  fprintf( stderr, "Failed to open\n" );
725  return( 1 );
726  }
727 
728  while( !feof( file ) )
729  {
730  int skip = 0;
731 
732  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
733  break;
734  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
735  fprintf( stdout, " " );
736  for( i = strlen( buf ) + 1; i < 67; i++ )
737  fprintf( stdout, "." );
738  fprintf( stdout, " " );
739  fflush( stdout );
740 
741  total_tests++;
742 
743  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
744  break;
745  cnt = parse_arguments( buf, strlen(buf), params );
746 
747  if( strcmp( params[0], "depends_on" ) == 0 )
748  {
749  for( i = 1; i < cnt; i++ )
750  if( dep_check( params[i] ) != 0 )
751  skip = 1;
752 
753  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
754  break;
755  cnt = parse_arguments( buf, strlen(buf), params );
756  }
757 
758  if( skip == 0 )
759  {
760  test_errors = 0;
761  ret = dispatch_test( cnt, params );
762  }
763 
764  if( skip == 1 || ret == 3 )
765  {
766  total_skipped++;
767  fprintf( stdout, "----\n" );
768  fflush( stdout );
769  }
770  else if( ret == 0 && test_errors == 0 )
771  {
772  fprintf( stdout, "PASS\n" );
773  fflush( stdout );
774  }
775  else if( ret == 2 )
776  {
777  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
778  fclose(file);
779  exit( 2 );
780  }
781  else
782  total_errors++;
783 
784  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
785  break;
786  if( strlen(buf) != 0 )
787  {
788  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
789  return( 1 );
790  }
791  }
792  fclose(file);
793 
794  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
795  if( total_errors == 0 )
796  fprintf( stdout, "PASSED" );
797  else
798  fprintf( stdout, "FAILED" );
799 
800  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
801  total_tests - total_errors, total_tests, total_skipped );
802 
803 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
804 #if defined(POLARSSL_MEMORY_DEBUG)
805  memory_buffer_alloc_status();
806 #endif
808 #endif
809 
810  return( total_errors != 0 );
811 }
812 
813 
int dispatch_test(int cnt, char *params[50])
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 P
Definition: dhm.h:159
Memory allocation layer (Deprecated to platform layer)
int parse_arguments(char *buf, size_t len, char *params[50])
#define PUT_UINT32_BE(n, b, i)
int get_line(FILE *f, char *buf, size_t len)
Info structure for the pseudo random function.
DHM context structure.
Definition: dhm.h:156
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
int dhm_self_test(int verbose)
Checkup routine.
Configuration options (set of defines)
MPI structure.
Definition: bignum.h:182
PolarSSL Platform abstraction layer.
static int test_assert(int correct, const char *test)
void mpi_init(mpi *X)
Initialize one MPI.
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
int mpi_cmp_mpi(const mpi *X, const mpi *Y)
Compare signed values.
size_t len
Definition: dhm.h:158
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int dhm_read_params(dhm_context *ctx, unsigned char **p, const unsigned char *end)
Parse the ServerKeyExchange parameters.
mpi G
Definition: dhm.h:160
void dhm_init(dhm_context *ctx)
Initialize DHM context.
void mpi_free(mpi *X)
Unallocate one MPI.
int dep_check(char *str)
#define TEST_ASSERT(TEST)
Diffie-Hellman-Merkle key exchange.
#define polarssl_malloc
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
static int test_errors
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)
int main()
int dhm_make_public(dhm_context *ctx, int x_size, unsigned char *output, size_t olen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Create own private value X and export G^X.
size_t mpi_size(const mpi *X)
Return the total size in bytes.
static int unhexify(unsigned char *obuf, const char *ibuf)
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
unsigned char * buf
void dhm_free(dhm_context *ctx)
Free and clear the components of a DHM key.
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
int verify_int(char *str, int *value)
int dhm_parse_dhmfile(dhm_context *dhm, const char *path)
Load and parse DHM parameters.
int dhm_make_params(dhm_context *ctx, int x_size, unsigned char *output, size_t *olen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Setup and write the ServerKeyExchange parameters.
int dhm_read_public(dhm_context *ctx, const unsigned char *input, size_t ilen)
Import the peer's public value G^Y.
int dhm_calc_secret(dhm_context *ctx, unsigned char *output, size_t *olen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Derive and export the shared secret (G^Y)^X mod P.