PolarSSL v1.3.2
test_suite_ecp.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_ECP_C
4 
5 #include <polarssl/ecp.h>
6 
7 #define POLARSSL_ECP_PF_UNKNOWN -1
8 #endif /* POLARSSL_ECP_C */
9 
10 
11 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
12 #include "polarssl/memory.h"
13 #endif
14 
15 #if defined(WANT_NOT_RND_MPI)
16 #if defined(POLARSSL_BIGNUM_C)
17 #include "polarssl/bignum.h"
18 #else
19 #error "not_rnd_mpi() need bignum.c"
20 #endif
21 #endif
22 
23 #ifdef _MSC_VER
24 #include <basetsd.h>
25 typedef UINT32 uint32_t;
26 #else
27 #include <inttypes.h>
28 #endif
29 
30 #include <assert.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 /*
35  * 32-bit integer manipulation macros (big endian)
36  */
37 #ifndef GET_UINT32_BE
38 #define GET_UINT32_BE(n,b,i) \
39 { \
40  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
41  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
42  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
43  | ( (uint32_t) (b)[(i) + 3] ); \
44 }
45 #endif
46 
47 #ifndef PUT_UINT32_BE
48 #define PUT_UINT32_BE(n,b,i) \
49 { \
50  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
51  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
52  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
53  (b)[(i) + 3] = (unsigned char) ( (n) ); \
54 }
55 #endif
56 
57 static int unhexify(unsigned char *obuf, const char *ibuf)
58 {
59  unsigned char c, c2;
60  int len = strlen(ibuf) / 2;
61  assert(!(strlen(ibuf) %1)); // must be even number of bytes
62 
63  while (*ibuf != 0)
64  {
65  c = *ibuf++;
66  if( c >= '0' && c <= '9' )
67  c -= '0';
68  else if( c >= 'a' && c <= 'f' )
69  c -= 'a' - 10;
70  else if( c >= 'A' && c <= 'F' )
71  c -= 'A' - 10;
72  else
73  assert( 0 );
74 
75  c2 = *ibuf++;
76  if( c2 >= '0' && c2 <= '9' )
77  c2 -= '0';
78  else if( c2 >= 'a' && c2 <= 'f' )
79  c2 -= 'a' - 10;
80  else if( c2 >= 'A' && c2 <= 'F' )
81  c2 -= 'A' - 10;
82  else
83  assert( 0 );
84 
85  *obuf++ = ( c << 4 ) | c2;
86  }
87 
88  return len;
89 }
90 
91 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
92 {
93  unsigned char l, h;
94 
95  while (len != 0)
96  {
97  h = (*ibuf) / 16;
98  l = (*ibuf) % 16;
99 
100  if( h < 10 )
101  *obuf++ = '0' + h;
102  else
103  *obuf++ = 'a' + h - 10;
104 
105  if( l < 10 )
106  *obuf++ = '0' + l;
107  else
108  *obuf++ = 'a' + l - 10;
109 
110  ++ibuf;
111  len--;
112  }
113 }
114 
124 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
125 {
126  size_t i;
127 
128  if( rng_state != NULL )
129  rng_state = NULL;
130 
131  for( i = 0; i < len; ++i )
132  output[i] = rand();
133 
134  return( 0 );
135 }
136 
142 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
143 {
144  if( rng_state != NULL )
145  rng_state = NULL;
146 
147  memset( output, 0, len );
148 
149  return( 0 );
150 }
151 
152 typedef struct
153 {
154  unsigned char *buf;
155  size_t length;
156 } rnd_buf_info;
157 
169 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
170 {
171  rnd_buf_info *info = (rnd_buf_info *) rng_state;
172  size_t use_len;
173 
174  if( rng_state == NULL )
175  return( rnd_std_rand( NULL, output, len ) );
176 
177  use_len = len;
178  if( len > info->length )
179  use_len = info->length;
180 
181  if( use_len )
182  {
183  memcpy( output, info->buf, use_len );
184  info->buf += use_len;
185  info->length -= use_len;
186  }
187 
188  if( len - use_len > 0 )
189  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
190 
191  return( 0 );
192 }
193 
201 typedef struct
202 {
203  uint32_t key[16];
204  uint32_t v0, v1;
206 
215 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
216 {
217  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
218  uint32_t i, *k, sum, delta=0x9E3779B9;
219  unsigned char result[4];
220 
221  if( rng_state == NULL )
222  return( rnd_std_rand( NULL, output, len ) );
223 
224  k = info->key;
225 
226  while( len > 0 )
227  {
228  size_t use_len = ( len > 4 ) ? 4 : len;
229  sum = 0;
230 
231  for( i = 0; i < 32; i++ )
232  {
233  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
234  sum += delta;
235  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
236  }
237 
238  PUT_UINT32_BE( info->v0, result, 0 );
239  memcpy( output, result, use_len );
240  len -= use_len;
241  }
242 
243  return( 0 );
244 }
245 
246 #if defined(WANT_NOT_RND_MPI)
247 
255 #define ciL (sizeof(t_uint)) /* chars in limb */
256 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
257 static int not_rnd_mpi( void *in, unsigned char *out, size_t len )
258 {
259  char *str = (char *) in;
260  mpi X;
261 
262  /*
263  * The 'in' pointer we get is from an MPI prepared by mpi_fill_random(),
264  * just reconstruct the rest in order to be able to call mpi_read_string()
265  */
266  X.s = 1;
267  X.p = (t_uint *) out;
268  X.n = CHARS_TO_LIMBS( len );
269 
270  /*
271  * If str is too long, mpi_read_string() will try to allocate a new buffer
272  * for X.p, which we want to avoid at all costs.
273  */
274  assert( strlen( str ) / 2 == len );
275 
276  return( mpi_read_string( &X, 16, str ) );
277 }
278 #endif /* WANT_NOT_RND_MPI */
279 
280 
281 #include <stdio.h>
282 #include <string.h>
283 
284 static int test_errors = 0;
285 
286 #ifdef POLARSSL_ECP_C
287 
288 #define TEST_SUITE_ACTIVE
289 
290 static int test_assert( int correct, char *test )
291 {
292  if( correct )
293  return( 0 );
294 
295  test_errors++;
296  if( test_errors == 1 )
297  printf( "FAILED\n" );
298  printf( " %s\n", test );
299 
300  return( 1 );
301 }
302 
303 #define TEST_ASSERT( TEST ) \
304  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
305  if( test_errors) return; \
306  } while (0)
307 
308 int verify_string( char **str )
309 {
310  if( (*str)[0] != '"' ||
311  (*str)[strlen( *str ) - 1] != '"' )
312  {
313  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
314  return( -1 );
315  }
316 
317  (*str)++;
318  (*str)[strlen( *str ) - 1] = '\0';
319 
320  return( 0 );
321 }
322 
323 int verify_int( char *str, int *value )
324 {
325  size_t i;
326  int minus = 0;
327  int digits = 1;
328  int hex = 0;
329 
330  for( i = 0; i < strlen( str ); i++ )
331  {
332  if( i == 0 && str[i] == '-' )
333  {
334  minus = 1;
335  continue;
336  }
337 
338  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
339  str[i - 1] == '0' && str[i] == 'x' )
340  {
341  hex = 1;
342  continue;
343  }
344 
345  if( str[i] < '0' || str[i] > '9' )
346  {
347  digits = 0;
348  break;
349  }
350  }
351 
352  if( digits )
353  {
354  if( hex )
355  *value = strtol( str, NULL, 16 );
356  else
357  *value = strtol( str, NULL, 10 );
358 
359  return( 0 );
360  }
361 
362  if( strcmp( str, "POLARSSL_ECP_PF_COMPRESSED" ) == 0 )
363  {
364  *value = ( POLARSSL_ECP_PF_COMPRESSED );
365  return( 0 );
366  }
367  if( strcmp( str, "POLARSSL_ECP_PF_UNCOMPRESSED" ) == 0 )
368  {
369  *value = ( POLARSSL_ECP_PF_UNCOMPRESSED );
370  return( 0 );
371  }
372  if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1" ) == 0 )
373  {
374  *value = ( POLARSSL_ECP_DP_SECP192R1 );
375  return( 0 );
376  }
377  if( strcmp( str, "-1" ) == 0 )
378  {
379  *value = ( -1 );
380  return( 0 );
381  }
382  if( strcmp( str, "POLARSSL_ECP_PF_UNKNOWN" ) == 0 )
383  {
384  *value = ( POLARSSL_ECP_PF_UNKNOWN );
385  return( 0 );
386  }
387  if( strcmp( str, "POLARSSL_ECP_DP_BP512R1" ) == 0 )
388  {
389  *value = ( POLARSSL_ECP_DP_BP512R1 );
390  return( 0 );
391  }
392  if( strcmp( str, "POLARSSL_ECP_DP_SECP384R1" ) == 0 )
393  {
394  *value = ( POLARSSL_ECP_DP_SECP384R1 );
395  return( 0 );
396  }
397  if( strcmp( str, "POLARSSL_ERR_ECP_BUFFER_TOO_SMALL" ) == 0 )
398  {
400  return( 0 );
401  }
402  if( strcmp( str, "POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE" ) == 0 )
403  {
405  return( 0 );
406  }
407  if( strcmp( str, "POLARSSL_ECP_DP_BP384R1" ) == 0 )
408  {
409  *value = ( POLARSSL_ECP_DP_BP384R1 );
410  return( 0 );
411  }
412  if( strcmp( str, "POLARSSL_ECP_DP_SECP224R1" ) == 0 )
413  {
414  *value = ( POLARSSL_ECP_DP_SECP224R1 );
415  return( 0 );
416  }
417  if( strcmp( str, "POLARSSL_ECP_DP_SECP256R1" ) == 0 )
418  {
419  *value = ( POLARSSL_ECP_DP_SECP256R1 );
420  return( 0 );
421  }
422  if( strcmp( str, "POLARSSL_ERR_ECP_INVALID_KEY" ) == 0 )
423  {
424  *value = ( POLARSSL_ERR_ECP_INVALID_KEY );
425  return( 0 );
426  }
427  if( strcmp( str, "POLARSSL_ECP_DP_BP256R1" ) == 0 )
428  {
429  *value = ( POLARSSL_ECP_DP_BP256R1 );
430  return( 0 );
431  }
432  if( strcmp( str, "POLARSSL_ERR_ECP_BAD_INPUT_DATA" ) == 0 )
433  {
434  *value = ( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
435  return( 0 );
436  }
437  if( strcmp( str, "POLARSSL_ECP_DP_SECP521R1" ) == 0 )
438  {
439  *value = ( POLARSSL_ECP_DP_SECP521R1 );
440  return( 0 );
441  }
442 
443 
444  printf( "Expected integer for parameter and got: %s\n", str );
445  return( -1 );
446 }
447 
448 void test_suite_ecp_small_add( int a_zero, char *x_a, char *y_a, int b_zero, char *x_b,
449  char *y_b, int c_zero, int x_c, int y_c )
450 {
451  ecp_group grp;
452  ecp_point A, B, C;
453 
454  ecp_group_init( &grp );
455  ecp_point_init( &A ); ecp_point_init( &B ); ecp_point_init( &C );
456 
458  "47", "4", "17", "42", "13" ) == 0 );
459 
460  if( a_zero )
461  ecp_set_zero( &A );
462  else
463  TEST_ASSERT( ecp_point_read_string( &A, 10, x_a, y_a ) == 0 );
464 
465  if( b_zero )
466  ecp_set_zero( &B );
467  else
468  TEST_ASSERT( ecp_point_read_string( &B, 10, x_b, y_b ) == 0 );
469 
470  TEST_ASSERT( ecp_add( &grp, &C, &A, &B ) == 0 );
471 
472  if( c_zero )
473  TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
474  else
475  {
476  TEST_ASSERT( mpi_cmp_int( &C.X, x_c ) == 0 );
477  TEST_ASSERT( mpi_cmp_int( &C.Y, y_c ) == 0 );
478  }
479 
480  TEST_ASSERT( ecp_add( &grp, &C, &B, &A ) == 0 );
481 
482  if( c_zero )
483  TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
484  else
485  {
486  TEST_ASSERT( mpi_cmp_int( &C.X, x_c ) == 0 );
487  TEST_ASSERT( mpi_cmp_int( &C.Y, y_c ) == 0 );
488  }
489 
490  ecp_group_free( &grp );
491  ecp_point_free( &A ); ecp_point_free( &B ); ecp_point_free( &C );
492 }
493 
494 void test_suite_ecp_small_sub( int a_zero, char *x_a, char *y_a, int b_zero, char *x_b,
495  char *y_b, int c_zero, int x_c, int y_c )
496 {
497  ecp_group grp;
498  ecp_point A, B, C;
499 
500  ecp_group_init( &grp );
501  ecp_point_init( &A ); ecp_point_init( &B ); ecp_point_init( &C );
502 
504  "47", "4", "17", "42", "13" ) == 0 );
505 
506  if( a_zero )
507  ecp_set_zero( &A );
508  else
509  TEST_ASSERT( ecp_point_read_string( &A, 10, x_a, y_a ) == 0 );
510 
511  if( b_zero )
512  ecp_set_zero( &B );
513  else
514  TEST_ASSERT( ecp_point_read_string( &B, 10, x_b, y_b ) == 0 );
515 
516  TEST_ASSERT( ecp_sub( &grp, &C, &A, &B ) == 0 );
517 
518  if( c_zero )
519  TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
520  else
521  {
522  TEST_ASSERT( mpi_cmp_int( &C.X, x_c ) == 0 );
523  TEST_ASSERT( mpi_cmp_int( &C.Y, y_c ) == 0 );
524  }
525 
526  ecp_group_free( &grp );
527  ecp_point_free( &A ); ecp_point_free( &B ); ecp_point_free( &C );
528 }
529 
530 void test_suite_ecp_small_mul( int m_str, int r_zero, int x_r, int y_r, int ret )
531 {
532  ecp_group grp;
533  ecp_point R;
534  mpi m;
535  rnd_pseudo_info rnd_info;
536 
537  ecp_group_init( &grp );
538  ecp_point_init( &R );
539  mpi_init( &m );
540  memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
541 
543  "47", "4", "17", "42", "13" ) == 0 );
544 
545  TEST_ASSERT( mpi_lset( &m, m_str ) == 0 );
546 
547  TEST_ASSERT( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) == ret );
548 
549  if( r_zero )
550  TEST_ASSERT( mpi_cmp_int( &R.Z, 0 ) == 0 );
551  else
552  {
553  TEST_ASSERT( mpi_cmp_int( &R.X, x_r ) == 0 );
554  TEST_ASSERT( mpi_cmp_int( &R.Y, y_r ) == 0 );
555  }
556 
557  /* try again with randomization */
558  ecp_point_free( &R );
559 
560  TEST_ASSERT( ecp_mul( &grp, &R, &m, &grp.G,
561  &rnd_pseudo_rand, &rnd_info ) == ret );
562 
563  if( r_zero )
564  TEST_ASSERT( mpi_cmp_int( &R.Z, 0 ) == 0 );
565  else
566  {
567  TEST_ASSERT( mpi_cmp_int( &R.X, x_r ) == 0 );
568  TEST_ASSERT( mpi_cmp_int( &R.Y, y_r ) == 0 );
569  }
570 
571  ecp_group_free( &grp );
572  ecp_point_free( &R );
573  mpi_free( &m );
574 }
575 
576 void test_suite_ecp_small_check_pub( int x, int y, int z, int ret )
577 {
578  ecp_group grp;
579  ecp_point P;
580 
581  ecp_group_init( &grp );
582  ecp_point_init( &P );
583 
585  "47", "4", "17", "42", "13" ) == 0 );
586 
587  TEST_ASSERT( mpi_lset( &P.X, x ) == 0 );
588  TEST_ASSERT( mpi_lset( &P.Y, y ) == 0 );
589  TEST_ASSERT( mpi_lset( &P.Z, z ) == 0 );
590 
591  TEST_ASSERT( ecp_check_pubkey( &grp, &P ) == ret );
592 
593  ecp_group_free( &grp );
594  ecp_point_free( &P );
595 }
596 
597 void test_suite_ecp_test_vect( int id, char *dA_str, char *xA_str, char *yA_str,
598  char *dB_str, char *xB_str, char *yB_str, char *xZ_str,
599  char *yZ_str )
600 {
601  ecp_group grp;
602  ecp_point R;
603  mpi dA, xA, yA, dB, xB, yB, xZ, yZ;
604  rnd_pseudo_info rnd_info;
605 
606  ecp_group_init( &grp ); ecp_point_init( &R );
607  mpi_init( &dA ); mpi_init( &xA ); mpi_init( &yA ); mpi_init( &dB );
608  mpi_init( &xB ); mpi_init( &yB ); mpi_init( &xZ ); mpi_init( &yZ );
609  memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
610 
611  TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
612 
613  TEST_ASSERT( ecp_check_pubkey( &grp, &grp.G ) == 0 );
614 
615  TEST_ASSERT( mpi_read_string( &dA, 16, dA_str ) == 0 );
616  TEST_ASSERT( mpi_read_string( &xA, 16, xA_str ) == 0 );
617  TEST_ASSERT( mpi_read_string( &yA, 16, yA_str ) == 0 );
618  TEST_ASSERT( mpi_read_string( &dB, 16, dB_str ) == 0 );
619  TEST_ASSERT( mpi_read_string( &xB, 16, xB_str ) == 0 );
620  TEST_ASSERT( mpi_read_string( &yB, 16, yB_str ) == 0 );
621  TEST_ASSERT( mpi_read_string( &xZ, 16, xZ_str ) == 0 );
622  TEST_ASSERT( mpi_read_string( &yZ, 16, yZ_str ) == 0 );
623 
624  TEST_ASSERT( ecp_mul( &grp, &R, &dA, &grp.G,
625  &rnd_pseudo_rand, &rnd_info ) == 0 );
626  TEST_ASSERT( mpi_cmp_mpi( &R.X, &xA ) == 0 );
627  TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yA ) == 0 );
628  TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
629  TEST_ASSERT( ecp_mul( &grp, &R, &dB, &R, NULL, NULL ) == 0 );
630  TEST_ASSERT( mpi_cmp_mpi( &R.X, &xZ ) == 0 );
631  TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
632  TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
633 
634  TEST_ASSERT( ecp_mul( &grp, &R, &dB, &grp.G, NULL, NULL ) == 0 );
635  TEST_ASSERT( mpi_cmp_mpi( &R.X, &xB ) == 0 );
636  TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yB ) == 0 );
637  TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
638  TEST_ASSERT( ecp_mul( &grp, &R, &dA, &R,
639  &rnd_pseudo_rand, &rnd_info ) == 0 );
640  TEST_ASSERT( mpi_cmp_mpi( &R.X, &xZ ) == 0 );
641  TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
642  TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
643 
644  ecp_group_free( &grp ); ecp_point_free( &R );
645  mpi_free( &dA ); mpi_free( &xA ); mpi_free( &yA ); mpi_free( &dB );
646  mpi_free( &xB ); mpi_free( &yB ); mpi_free( &xZ ); mpi_free( &yZ );
647 }
648 
649 void test_suite_ecp_fast_mod( int id, char *N_str )
650 {
651  ecp_group grp;
652  mpi N, R;
653 
654  mpi_init( &N ); mpi_init( &R );
655  ecp_group_init( &grp );
656 
657  TEST_ASSERT( mpi_read_string( &N, 16, N_str ) == 0 );
658  TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
659  TEST_ASSERT( grp.modp != NULL );
660 
661  /*
662  * Store correct result before we touch N
663  */
664  TEST_ASSERT( mpi_mod_mpi( &R, &N, &grp.P ) == 0 );
665 
666  TEST_ASSERT( grp.modp( &N ) == 0 );
667  TEST_ASSERT( mpi_msb( &N ) <= grp.pbits + 3 );
668 
669  /*
670  * Use mod rather than addition/substraction in case previous test fails
671  */
672  TEST_ASSERT( mpi_mod_mpi( &N, &N, &grp.P ) == 0 );
673  TEST_ASSERT( mpi_cmp_mpi( &N, &R ) == 0 );
674 
675  mpi_free( &N ); mpi_free( &R );
676  ecp_group_free( &grp );
677 }
678 
679 void test_suite_ecp_write_binary( int id, char *x, char *y, char *z, int format,
680  char *out, int blen, int ret )
681 {
682  ecp_group grp;
683  ecp_point P;
684  unsigned char buf[256], str[512];
685  size_t olen;
686 
687  memset( buf, 0, sizeof( buf ) );
688  memset( str, 0, sizeof( str ) );
689 
690  ecp_group_init( &grp ); ecp_point_init( &P );
691 
692  TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
693 
694  TEST_ASSERT( mpi_read_string( &P.X, 16, x ) == 0 );
695  TEST_ASSERT( mpi_read_string( &P.Y, 16, y ) == 0 );
696  TEST_ASSERT( mpi_read_string( &P.Z, 16, z ) == 0 );
697 
698  TEST_ASSERT( ecp_point_write_binary( &grp, &P, format,
699  &olen, buf, blen ) == ret );
700 
701  if( ret == 0 )
702  {
703  hexify( str, buf, olen );
704  TEST_ASSERT( strcasecmp( (char *) str, out ) == 0 );
705  }
706 
707  ecp_group_free( &grp ); ecp_point_free( &P );
708 }
709 
710 void test_suite_ecp_read_binary( int id, char *input, char *x, char *y, char *z,
711  int ret )
712 {
713  ecp_group grp;
714  ecp_point P;
715  mpi X, Y, Z;
716  int ilen;
717  unsigned char buf[256];
718 
719  memset( buf, 0, sizeof( buf ) );
720 
721  ecp_group_init( &grp ); ecp_point_init( &P );
722  mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
723 
724  TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
725 
726  TEST_ASSERT( mpi_read_string( &X, 16, x ) == 0 );
727  TEST_ASSERT( mpi_read_string( &Y, 16, y ) == 0 );
728  TEST_ASSERT( mpi_read_string( &Z, 16, z ) == 0 );
729 
730  ilen = unhexify( buf, input );
731 
732  TEST_ASSERT( ecp_point_read_binary( &grp, &P, buf, ilen ) == ret );
733 
734  if( ret == 0 )
735  {
736  TEST_ASSERT( mpi_cmp_mpi( &P.X, &X ) == 0 );
737  TEST_ASSERT( mpi_cmp_mpi( &P.Y, &Y ) == 0 );
738  TEST_ASSERT( mpi_cmp_mpi( &P.Z, &Z ) == 0 );
739  }
740 
741  ecp_group_free( &grp ); ecp_point_free( &P );
742  mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
743 }
744 
745 void test_suite_ecp_tls_read_point( int id, char *input, char *x, char *y, char *z,
746  int ret )
747 {
748  ecp_group grp;
749  ecp_point P;
750  mpi X, Y, Z;
751  size_t ilen;
752  unsigned char buf[256];
753  const unsigned char *vbuf = buf;
754 
755  memset( buf, 0, sizeof( buf ) );
756 
757  ecp_group_init( &grp ); ecp_point_init( &P );
758  mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
759 
760  TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
761 
762  TEST_ASSERT( mpi_read_string( &X, 16, x ) == 0 );
763  TEST_ASSERT( mpi_read_string( &Y, 16, y ) == 0 );
764  TEST_ASSERT( mpi_read_string( &Z, 16, z ) == 0 );
765 
766  ilen = unhexify( buf, input );
767 
768  TEST_ASSERT( ecp_tls_read_point( &grp, &P, &vbuf, ilen ) == ret );
769 
770  if( ret == 0 )
771  {
772  TEST_ASSERT( mpi_cmp_mpi( &P.X, &X ) == 0 );
773  TEST_ASSERT( mpi_cmp_mpi( &P.Y, &Y ) == 0 );
774  TEST_ASSERT( mpi_cmp_mpi( &P.Z, &Z ) == 0 );
775  TEST_ASSERT( *vbuf == 0x00 );
776  }
777 
778  ecp_group_free( &grp ); ecp_point_free( &P );
779  mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
780 }
781 
782 void test_suite_ecp_tls_write_read_point( int id )
783 {
784  ecp_group grp;
785  ecp_point pt;
786  unsigned char buf[256];
787  const unsigned char *vbuf;
788  size_t olen;
789 
790  ecp_group_init( &grp );
791  ecp_point_init( &pt );
792 
793  TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
794 
795  memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
796  TEST_ASSERT( ecp_tls_write_point( &grp, &grp.G,
797  POLARSSL_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
798  TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen )
800  TEST_ASSERT( vbuf == buf + olen );
801 
802  memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
803  TEST_ASSERT( ecp_tls_write_point( &grp, &grp.G,
804  POLARSSL_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
805  TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
806  TEST_ASSERT( mpi_cmp_mpi( &grp.G.X, &pt.X ) == 0 );
807  TEST_ASSERT( mpi_cmp_mpi( &grp.G.Y, &pt.Y ) == 0 );
808  TEST_ASSERT( mpi_cmp_mpi( &grp.G.Z, &pt.Z ) == 0 );
809  TEST_ASSERT( vbuf == buf + olen );
810 
811  memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
812  TEST_ASSERT( ecp_set_zero( &pt ) == 0 );
813  TEST_ASSERT( ecp_tls_write_point( &grp, &pt,
814  POLARSSL_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
815  TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
816  TEST_ASSERT( ecp_is_zero( &pt ) );
817  TEST_ASSERT( vbuf == buf + olen );
818 
819  memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
820  TEST_ASSERT( ecp_set_zero( &pt ) == 0 );
821  TEST_ASSERT( ecp_tls_write_point( &grp, &pt,
822  POLARSSL_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
823  TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
824  TEST_ASSERT( ecp_is_zero( &pt ) );
825  TEST_ASSERT( vbuf == buf + olen );
826 
827  ecp_group_free( &grp );
828  ecp_point_free( &pt );
829 }
830 
831 void test_suite_ecp_tls_read_group( char *record, int result, int bits )
832 {
833  ecp_group grp;
834  unsigned char buf[10];
835  const unsigned char *vbuf = buf;
836  int len, ret;
837 
838  ecp_group_init( &grp );
839  memset( buf, 0x00, sizeof( buf ) );
840 
841  len = unhexify( buf, record );
842 
843  ret = ecp_tls_read_group( &grp, &vbuf, len );
844 
845  TEST_ASSERT( ret == result );
846  if( ret == 0)
847  {
848  TEST_ASSERT( mpi_msb( &grp.P ) == (size_t) bits );
849  TEST_ASSERT( *vbuf == 0x00 );
850  }
851 
852  ecp_group_free( &grp );
853 }
854 
855 void test_suite_ecp_tls_write_read_group( int id )
856 {
857  ecp_group grp1, grp2;
858  unsigned char buf[10];
859  const unsigned char *vbuf = buf;
860  size_t len;
861  int ret;
862 
863  ecp_group_init( &grp1 );
864  ecp_group_init( &grp2 );
865  memset( buf, 0x00, sizeof( buf ) );
866 
867  TEST_ASSERT( ecp_use_known_dp( &grp1, id ) == 0 );
868 
869  TEST_ASSERT( ecp_tls_write_group( &grp1, &len, buf, 10 ) == 0 );
870  TEST_ASSERT( ( ret = ecp_tls_read_group( &grp2, &vbuf, len ) ) == 0 );
871 
872  if( ret == 0 )
873  {
874  TEST_ASSERT( mpi_cmp_mpi( &grp1.N, &grp2.N ) == 0 );
875  TEST_ASSERT( grp1.id == grp2.id );
876  }
877 
878  ecp_group_free( &grp1 );
879  ecp_group_free( &grp2 );
880 }
881 
882 void test_suite_ecp_check_privkey( int id )
883 {
884  ecp_group grp;
885  mpi d;
886 
887  ecp_group_init( &grp );
888  mpi_init( &d );
889 
890  TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
891 
892  TEST_ASSERT( mpi_lset( &d, 0 ) == 0 );
894 
895  TEST_ASSERT( mpi_copy( &d, &grp.N ) == 0 );
897 
898  ecp_group_free( &grp );
899  mpi_free( &d );
900 }
901 
902 void test_suite_ecp_gen_keypair( int id )
903 {
904  ecp_group grp;
905  ecp_point Q;
906  mpi d;
907  rnd_pseudo_info rnd_info;
908 
909  ecp_group_init( &grp );
910  ecp_point_init( &Q );
911  mpi_init( &d );
912  memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
913 
914  TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
915 
916  TEST_ASSERT( ecp_gen_keypair( &grp, &d, &Q, &rnd_pseudo_rand, &rnd_info )
917  == 0 );
918 
919  TEST_ASSERT( ecp_check_pubkey( &grp, &Q ) == 0 );
920  TEST_ASSERT( ecp_check_privkey( &grp, &d ) == 0 );
921 
922  ecp_group_free( &grp );
923  ecp_point_free( &Q );
924  mpi_free( &d );
925 }
926 
927 #ifdef POLARSSL_SELF_TEST
928 void test_suite_ecp_selftest()
929 {
930  TEST_ASSERT( ecp_self_test( 0 ) == 0 );
931 }
932 #endif /* POLARSSL_SELF_TEST */
933 
934 
935 #endif /* POLARSSL_ECP_C */
936 
937 
938 int dep_check( char *str )
939 {
940  if( str == NULL )
941  return( 1 );
942 
943  if( strcmp( str, "POLARSSL_ECP_DP_SECP521R1_ENABLED" ) == 0 )
944  {
945 #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
946  return( 0 );
947 #else
948  return( 1 );
949 #endif
950  }
951  if( strcmp( str, "POLARSSL_ECP_DP_SECP384R1_ENABLED" ) == 0 )
952  {
953 #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
954  return( 0 );
955 #else
956  return( 1 );
957 #endif
958  }
959  if( strcmp( str, "POLARSSL_ECP_DP_BP256R1_ENABLED" ) == 0 )
960  {
961 #if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
962  return( 0 );
963 #else
964  return( 1 );
965 #endif
966  }
967  if( strcmp( str, "POLARSSL_ECP_DP_SECP224R1_ENABLED" ) == 0 )
968  {
969 #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
970  return( 0 );
971 #else
972  return( 1 );
973 #endif
974  }
975  if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
976  {
977 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
978  return( 0 );
979 #else
980  return( 1 );
981 #endif
982  }
983  if( strcmp( str, "POLARSSL_ECP_DP_BP512R1_ENABLED" ) == 0 )
984  {
985 #if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
986  return( 0 );
987 #else
988  return( 1 );
989 #endif
990  }
991  if( strcmp( str, "POLARSSL_ECP_DP_SECP256R1_ENABLED" ) == 0 )
992  {
993 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
994  return( 0 );
995 #else
996  return( 1 );
997 #endif
998  }
999  if( strcmp( str, "POLARSSL_ECP_DP_BP384R1_ENABLED" ) == 0 )
1000  {
1001 #if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
1002  return( 0 );
1003 #else
1004  return( 1 );
1005 #endif
1006  }
1007 
1008 
1009  return( 1 );
1010 }
1011 
1012 int dispatch_test(int cnt, char *params[50])
1013 {
1014  int ret;
1015  ((void) cnt);
1016  ((void) params);
1017 
1018 #if defined(TEST_SUITE_ACTIVE)
1019  if( strcmp( params[0], "ecp_small_add" ) == 0 )
1020  {
1021 
1022  int param1;
1023  char *param2 = params[2];
1024  char *param3 = params[3];
1025  int param4;
1026  char *param5 = params[5];
1027  char *param6 = params[6];
1028  int param7;
1029  int param8;
1030  int param9;
1031 
1032  if( cnt != 10 )
1033  {
1034  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 10 );
1035  return( 2 );
1036  }
1037 
1038  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1039  if( verify_string( &param2 ) != 0 ) return( 2 );
1040  if( verify_string( &param3 ) != 0 ) return( 2 );
1041  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1042  if( verify_string( &param5 ) != 0 ) return( 2 );
1043  if( verify_string( &param6 ) != 0 ) return( 2 );
1044  if( verify_int( params[7], &param7 ) != 0 ) return( 2 );
1045  if( verify_int( params[8], &param8 ) != 0 ) return( 2 );
1046  if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
1047 
1048  test_suite_ecp_small_add( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
1049  return ( 0 );
1050 
1051  return ( 3 );
1052  }
1053  else
1054  if( strcmp( params[0], "ecp_small_sub" ) == 0 )
1055  {
1056 
1057  int param1;
1058  char *param2 = params[2];
1059  char *param3 = params[3];
1060  int param4;
1061  char *param5 = params[5];
1062  char *param6 = params[6];
1063  int param7;
1064  int param8;
1065  int param9;
1066 
1067  if( cnt != 10 )
1068  {
1069  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 10 );
1070  return( 2 );
1071  }
1072 
1073  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1074  if( verify_string( &param2 ) != 0 ) return( 2 );
1075  if( verify_string( &param3 ) != 0 ) return( 2 );
1076  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1077  if( verify_string( &param5 ) != 0 ) return( 2 );
1078  if( verify_string( &param6 ) != 0 ) return( 2 );
1079  if( verify_int( params[7], &param7 ) != 0 ) return( 2 );
1080  if( verify_int( params[8], &param8 ) != 0 ) return( 2 );
1081  if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
1082 
1083  test_suite_ecp_small_sub( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
1084  return ( 0 );
1085 
1086  return ( 3 );
1087  }
1088  else
1089  if( strcmp( params[0], "ecp_small_mul" ) == 0 )
1090  {
1091 
1092  int param1;
1093  int param2;
1094  int param3;
1095  int param4;
1096  int param5;
1097 
1098  if( cnt != 6 )
1099  {
1100  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1101  return( 2 );
1102  }
1103 
1104  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1105  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1106  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1107  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1108  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1109 
1110  test_suite_ecp_small_mul( param1, param2, param3, param4, param5 );
1111  return ( 0 );
1112 
1113  return ( 3 );
1114  }
1115  else
1116  if( strcmp( params[0], "ecp_small_check_pub" ) == 0 )
1117  {
1118 
1119  int param1;
1120  int param2;
1121  int param3;
1122  int param4;
1123 
1124  if( cnt != 5 )
1125  {
1126  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1127  return( 2 );
1128  }
1129 
1130  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1131  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1132  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1133  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1134 
1135  test_suite_ecp_small_check_pub( param1, param2, param3, param4 );
1136  return ( 0 );
1137 
1138  return ( 3 );
1139  }
1140  else
1141  if( strcmp( params[0], "ecp_test_vect" ) == 0 )
1142  {
1143 
1144  int param1;
1145  char *param2 = params[2];
1146  char *param3 = params[3];
1147  char *param4 = params[4];
1148  char *param5 = params[5];
1149  char *param6 = params[6];
1150  char *param7 = params[7];
1151  char *param8 = params[8];
1152  char *param9 = params[9];
1153 
1154  if( cnt != 10 )
1155  {
1156  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 10 );
1157  return( 2 );
1158  }
1159 
1160  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1161  if( verify_string( &param2 ) != 0 ) return( 2 );
1162  if( verify_string( &param3 ) != 0 ) return( 2 );
1163  if( verify_string( &param4 ) != 0 ) return( 2 );
1164  if( verify_string( &param5 ) != 0 ) return( 2 );
1165  if( verify_string( &param6 ) != 0 ) return( 2 );
1166  if( verify_string( &param7 ) != 0 ) return( 2 );
1167  if( verify_string( &param8 ) != 0 ) return( 2 );
1168  if( verify_string( &param9 ) != 0 ) return( 2 );
1169 
1170  test_suite_ecp_test_vect( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
1171  return ( 0 );
1172 
1173  return ( 3 );
1174  }
1175  else
1176  if( strcmp( params[0], "ecp_fast_mod" ) == 0 )
1177  {
1178 
1179  int param1;
1180  char *param2 = params[2];
1181 
1182  if( cnt != 3 )
1183  {
1184  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
1185  return( 2 );
1186  }
1187 
1188  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1189  if( verify_string( &param2 ) != 0 ) return( 2 );
1190 
1191  test_suite_ecp_fast_mod( param1, param2 );
1192  return ( 0 );
1193 
1194  return ( 3 );
1195  }
1196  else
1197  if( strcmp( params[0], "ecp_write_binary" ) == 0 )
1198  {
1199 
1200  int param1;
1201  char *param2 = params[2];
1202  char *param3 = params[3];
1203  char *param4 = params[4];
1204  int param5;
1205  char *param6 = params[6];
1206  int param7;
1207  int param8;
1208 
1209  if( cnt != 9 )
1210  {
1211  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 9 );
1212  return( 2 );
1213  }
1214 
1215  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1216  if( verify_string( &param2 ) != 0 ) return( 2 );
1217  if( verify_string( &param3 ) != 0 ) return( 2 );
1218  if( verify_string( &param4 ) != 0 ) return( 2 );
1219  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1220  if( verify_string( &param6 ) != 0 ) return( 2 );
1221  if( verify_int( params[7], &param7 ) != 0 ) return( 2 );
1222  if( verify_int( params[8], &param8 ) != 0 ) return( 2 );
1223 
1224  test_suite_ecp_write_binary( param1, param2, param3, param4, param5, param6, param7, param8 );
1225  return ( 0 );
1226 
1227  return ( 3 );
1228  }
1229  else
1230  if( strcmp( params[0], "ecp_read_binary" ) == 0 )
1231  {
1232 
1233  int param1;
1234  char *param2 = params[2];
1235  char *param3 = params[3];
1236  char *param4 = params[4];
1237  char *param5 = params[5];
1238  int param6;
1239 
1240  if( cnt != 7 )
1241  {
1242  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1243  return( 2 );
1244  }
1245 
1246  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1247  if( verify_string( &param2 ) != 0 ) return( 2 );
1248  if( verify_string( &param3 ) != 0 ) return( 2 );
1249  if( verify_string( &param4 ) != 0 ) return( 2 );
1250  if( verify_string( &param5 ) != 0 ) return( 2 );
1251  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
1252 
1253  test_suite_ecp_read_binary( param1, param2, param3, param4, param5, param6 );
1254  return ( 0 );
1255 
1256  return ( 3 );
1257  }
1258  else
1259  if( strcmp( params[0], "ecp_tls_read_point" ) == 0 )
1260  {
1261 
1262  int param1;
1263  char *param2 = params[2];
1264  char *param3 = params[3];
1265  char *param4 = params[4];
1266  char *param5 = params[5];
1267  int param6;
1268 
1269  if( cnt != 7 )
1270  {
1271  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1272  return( 2 );
1273  }
1274 
1275  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1276  if( verify_string( &param2 ) != 0 ) return( 2 );
1277  if( verify_string( &param3 ) != 0 ) return( 2 );
1278  if( verify_string( &param4 ) != 0 ) return( 2 );
1279  if( verify_string( &param5 ) != 0 ) return( 2 );
1280  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
1281 
1282  test_suite_ecp_tls_read_point( param1, param2, param3, param4, param5, param6 );
1283  return ( 0 );
1284 
1285  return ( 3 );
1286  }
1287  else
1288  if( strcmp( params[0], "ecp_tls_write_read_point" ) == 0 )
1289  {
1290 
1291  int param1;
1292 
1293  if( cnt != 2 )
1294  {
1295  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
1296  return( 2 );
1297  }
1298 
1299  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1300 
1301  test_suite_ecp_tls_write_read_point( param1 );
1302  return ( 0 );
1303 
1304  return ( 3 );
1305  }
1306  else
1307  if( strcmp( params[0], "ecp_tls_read_group" ) == 0 )
1308  {
1309 
1310  char *param1 = params[1];
1311  int param2;
1312  int param3;
1313 
1314  if( cnt != 4 )
1315  {
1316  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1317  return( 2 );
1318  }
1319 
1320  if( verify_string( &param1 ) != 0 ) return( 2 );
1321  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1322  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1323 
1324  test_suite_ecp_tls_read_group( param1, param2, param3 );
1325  return ( 0 );
1326 
1327  return ( 3 );
1328  }
1329  else
1330  if( strcmp( params[0], "ecp_tls_write_read_group" ) == 0 )
1331  {
1332 
1333  int param1;
1334 
1335  if( cnt != 2 )
1336  {
1337  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
1338  return( 2 );
1339  }
1340 
1341  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1342 
1343  test_suite_ecp_tls_write_read_group( param1 );
1344  return ( 0 );
1345 
1346  return ( 3 );
1347  }
1348  else
1349  if( strcmp( params[0], "ecp_check_privkey" ) == 0 )
1350  {
1351 
1352  int param1;
1353 
1354  if( cnt != 2 )
1355  {
1356  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
1357  return( 2 );
1358  }
1359 
1360  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1361 
1362  test_suite_ecp_check_privkey( param1 );
1363  return ( 0 );
1364 
1365  return ( 3 );
1366  }
1367  else
1368  if( strcmp( params[0], "ecp_gen_keypair" ) == 0 )
1369  {
1370 
1371  int param1;
1372 
1373  if( cnt != 2 )
1374  {
1375  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
1376  return( 2 );
1377  }
1378 
1379  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1380 
1381  test_suite_ecp_gen_keypair( param1 );
1382  return ( 0 );
1383 
1384  return ( 3 );
1385  }
1386  else
1387  if( strcmp( params[0], "ecp_selftest" ) == 0 )
1388  {
1389  #ifdef POLARSSL_SELF_TEST
1390 
1391 
1392  if( cnt != 1 )
1393  {
1394  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1395  return( 2 );
1396  }
1397 
1398 
1399  test_suite_ecp_selftest( );
1400  return ( 0 );
1401  #endif /* POLARSSL_SELF_TEST */
1402 
1403  return ( 3 );
1404  }
1405  else
1406 
1407  {
1408  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
1409  fflush( stdout );
1410  return( 1 );
1411  }
1412 #else
1413  return( 3 );
1414 #endif
1415  return( ret );
1416 }
1417 
1418 int get_line( FILE *f, char *buf, size_t len )
1419 {
1420  char *ret;
1421 
1422  ret = fgets( buf, len, f );
1423  if( ret == NULL )
1424  return( -1 );
1425 
1426  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
1427  buf[strlen(buf) - 1] = '\0';
1428  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
1429  buf[strlen(buf) - 1] = '\0';
1430 
1431  return( 0 );
1432 }
1433 
1434 int parse_arguments( char *buf, size_t len, char *params[50] )
1435 {
1436  int cnt = 0, i;
1437  char *cur = buf;
1438  char *p = buf, *q;
1439 
1440  params[cnt++] = cur;
1441 
1442  while( *p != '\0' && p < buf + len )
1443  {
1444  if( *p == '\\' )
1445  {
1446  *p++;
1447  *p++;
1448  continue;
1449  }
1450  if( *p == ':' )
1451  {
1452  if( p + 1 < buf + len )
1453  {
1454  cur = p + 1;
1455  params[cnt++] = cur;
1456  }
1457  *p = '\0';
1458  }
1459 
1460  *p++;
1461  }
1462 
1463  // Replace newlines, question marks and colons in strings
1464  for( i = 0; i < cnt; i++ )
1465  {
1466  p = params[i];
1467  q = params[i];
1468 
1469  while( *p != '\0' )
1470  {
1471  if( *p == '\\' && *(p + 1) == 'n' )
1472  {
1473  p += 2;
1474  *(q++) = '\n';
1475  }
1476  else if( *p == '\\' && *(p + 1) == ':' )
1477  {
1478  p += 2;
1479  *(q++) = ':';
1480  }
1481  else if( *p == '\\' && *(p + 1) == '?' )
1482  {
1483  p += 2;
1484  *(q++) = '?';
1485  }
1486  else
1487  *(q++) = *(p++);
1488  }
1489  *q = '\0';
1490  }
1491 
1492  return( cnt );
1493 }
1494 
1495 int main()
1496 {
1497  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1498  const char *filename = "/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_ecp.data";
1499  FILE *file;
1500  char buf[5000];
1501  char *params[50];
1502 
1503 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1504  unsigned char alloc_buf[1000000];
1505  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1506 #endif
1507 
1508  file = fopen( filename, "r" );
1509  if( file == NULL )
1510  {
1511  fprintf( stderr, "Failed to open\n" );
1512  return( 1 );
1513  }
1514 
1515  while( !feof( file ) )
1516  {
1517  int skip = 0;
1518 
1519  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1520  break;
1521  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1522  fprintf( stdout, " " );
1523  for( i = strlen( buf ) + 1; i < 67; i++ )
1524  fprintf( stdout, "." );
1525  fprintf( stdout, " " );
1526  fflush( stdout );
1527 
1528  total_tests++;
1529 
1530  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1531  break;
1532  cnt = parse_arguments( buf, strlen(buf), params );
1533 
1534  if( strcmp( params[0], "depends_on" ) == 0 )
1535  {
1536  for( i = 1; i < cnt; i++ )
1537  if( dep_check( params[i] ) != 0 )
1538  skip = 1;
1539 
1540  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1541  break;
1542  cnt = parse_arguments( buf, strlen(buf), params );
1543  }
1544 
1545  if( skip == 0 )
1546  {
1547  test_errors = 0;
1548  ret = dispatch_test( cnt, params );
1549  }
1550 
1551  if( skip == 1 || ret == 3 )
1552  {
1553  total_skipped++;
1554  fprintf( stdout, "----\n" );
1555  fflush( stdout );
1556  }
1557  else if( ret == 0 && test_errors == 0 )
1558  {
1559  fprintf( stdout, "PASS\n" );
1560  fflush( stdout );
1561  }
1562  else if( ret == 2 )
1563  {
1564  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1565  fclose(file);
1566  exit( 2 );
1567  }
1568  else
1569  total_errors++;
1570 
1571  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1572  break;
1573  if( strlen(buf) != 0 )
1574  {
1575  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1576  return( 1 );
1577  }
1578  }
1579  fclose(file);
1580 
1581  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1582  if( total_errors == 0 )
1583  fprintf( stdout, "PASSED" );
1584  else
1585  fprintf( stdout, "FAILED" );
1586 
1587  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1588  total_tests - total_errors, total_tests, total_skipped );
1589 
1590 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1591 #if defined(POLARSSL_MEMORY_DEBUG)
1592  memory_buffer_alloc_status();
1593 #endif
1594  memory_buffer_alloc_free();
1595 #endif
1596 
1597  return( total_errors != 0 );
1598 }
1599 
1600 
int mpi_cmp_int(const mpi *X, t_sint z)
Compare signed values.
size_t pbits
Definition: ecp.h:125
static int unhexify(unsigned char *obuf, const char *ibuf)
int ecp_sub(const ecp_group *grp, ecp_point *R, const ecp_point *P, const ecp_point *Q)
Subtraction: R = P - Q.
int ecp_check_privkey(const ecp_group *grp, const mpi *d)
Check that an mpi is a valid private key for this curve.
#define POLARSSL_ERR_ECP_BAD_INPUT_DATA
Bad input parameters to function.
Definition: ecp.h:35
Memory allocation layer.
#define POLARSSL_ECP_PF_COMPRESSED
Compressed point format.
Definition: ecp.h:175
uint32_t t_uint
Definition: bignum.h:149
Info structure for the pseudo random function.
int ecp_self_test(int verbose)
Checkup routine.
Elliptic curves over GF(p)
int s
Definition: bignum.h:173
mpi P
Definition: ecp.h:120
int(* modp)(mpi *)
Definition: ecp.h:128
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
#define POLARSSL_ECP_PF_UNCOMPRESSED
Uncompressed point format.
Definition: ecp.h:174
ECP group structure.
Definition: ecp.h:117
Configuration options (set of defines)
int mpi_lset(mpi *X, t_sint z)
Set value from integer.
MPI structure.
Definition: bignum.h:171
int ecp_mul(ecp_group *grp, ecp_point *R, const mpi *m, const ecp_point *P, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Multiplication by an integer: R = m * P (Not thread-safe to use same group in multiple threads) ...
static int test_assert(int correct, char *test)
int ecp_point_read_binary(const ecp_group *grp, ecp_point *P, const unsigned char *buf, size_t ilen)
Import a point from unsigned binary data.
#define POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE
Requested curve not available.
Definition: ecp.h:37
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.
#define POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
The buffer is too small to write to.
Definition: ecp.h:36
#define PUT_UINT32_BE(n, b, i)
int dep_check(char *str)
#define TEST_ASSERT(TEST)
ecp_point G
Definition: ecp.h:123
ecp_group_id id
Definition: ecp.h:119
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)
mpi N
Definition: ecp.h:124
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.
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
int ecp_tls_write_point(const ecp_group *grp, const ecp_point *pt, int format, size_t *olen, unsigned char *buf, size_t blen)
Export a point as a TLS ECPoint record.
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])
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
size_t mpi_msb(const mpi *X)
Return the number of bits up to and including the most significant &#39;1&#39; bit&#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.
t_uint * p
Definition: bignum.h:175
int ecp_tls_write_group(const ecp_group *grp, size_t *olen, unsigned char *buf, size_t blen)
Write the TLS ECParameters record for a group.
int verify_string(char **str)
int ecp_point_write_binary(const ecp_group *grp, const ecp_point *P, int format, size_t *olen, unsigned char *buf, size_t buflen)
Export a point into unsigned binary data.
void ecp_group_init(ecp_group *grp)
Initialize a group (to something meaningless)
mpi Y
Definition: ecp.h:97
int mpi_copy(mpi *X, const mpi *Y)
Copy the contents of Y into X.
int dispatch_test(int cnt, char *params[50])
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
size_t n
Definition: bignum.h:174
int mpi_mod_mpi(mpi *R, const mpi *A, const mpi *B)
Modulo: R = A mod B.
unsigned char * buf
int ecp_tls_read_group(ecp_group *grp, const unsigned char **buf, size_t len)
Set a group from a TLS ECParameters record.
static int test_errors
mpi Z
Definition: ecp.h:98
int ecp_check_pubkey(const ecp_group *grp, const ecp_point *pt)
Check that a point is a valid public key on this curve.
int ecp_add(const ecp_group *grp, ecp_point *R, const ecp_point *P, const ecp_point *Q)
Addition: R = P + Q.
int verify_int(char *str, int *value)
int ecp_set_zero(ecp_point *pt)
Set a point to zero.
#define POLARSSL_ERR_ECP_INVALID_KEY
Invalid private or public key.
Definition: ecp.h:41
int ecp_tls_read_point(const ecp_group *grp, ecp_point *pt, const unsigned char **buf, size_t len)
Import a point from a TLS ECPoint record.
int get_line(FILE *f, char *buf, size_t len)
int ecp_group_read_string(ecp_group *grp, int radix, const char *p, const char *b, const char *gx, const char *gy, const char *n)
Import an ECP group from null-terminated ASCII strings.
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
void ecp_point_free(ecp_point *pt)
Free the components of a point.