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