PolarSSL v1.3.2
test_suite_pk.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_PK_C
4 
5 #include <polarssl/pk.h>
6 
7 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len );
8 
9 static int pk_genkey( pk_context *pk )
10 {
11 #if defined(POLARSSL_RSA_C)
12  if( pk_get_type( pk ) == POLARSSL_PK_RSA )
13  return rsa_gen_key( pk_rsa( *pk ), rnd_std_rand, NULL, 512, 3 );
14 #endif
15 #if defined(POLARSSL_ECP_C)
16  if( pk_get_type( pk ) == POLARSSL_PK_ECKEY ||
19  {
20  int ret;
21  if( ( ret = ecp_use_known_dp( &pk_ec( *pk )->grp,
22  POLARSSL_ECP_DP_SECP192R1 ) ) != 0 )
23  return( ret );
24 
25  return ecp_gen_keypair( &pk_ec( *pk )->grp, &pk_ec( *pk )->d,
26  &pk_ec( *pk )->Q, rnd_std_rand, NULL );
27  }
28 #endif
29  return( -1 );
30 }
31 #endif /* POLARSSL_PK_C */
32 
33 
34 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
35 #include "polarssl/memory.h"
36 #endif
37 
38 #if defined(WANT_NOT_RND_MPI)
39 #if defined(POLARSSL_BIGNUM_C)
40 #include "polarssl/bignum.h"
41 #else
42 #error "not_rnd_mpi() need bignum.c"
43 #endif
44 #endif
45 
46 #ifdef _MSC_VER
47 #include <basetsd.h>
48 typedef UINT32 uint32_t;
49 #else
50 #include <inttypes.h>
51 #endif
52 
53 #include <assert.h>
54 #include <stdlib.h>
55 #include <string.h>
56 
57 /*
58  * 32-bit integer manipulation macros (big endian)
59  */
60 #ifndef GET_UINT32_BE
61 #define GET_UINT32_BE(n,b,i) \
62 { \
63  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
64  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
65  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
66  | ( (uint32_t) (b)[(i) + 3] ); \
67 }
68 #endif
69 
70 #ifndef PUT_UINT32_BE
71 #define PUT_UINT32_BE(n,b,i) \
72 { \
73  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
74  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
75  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
76  (b)[(i) + 3] = (unsigned char) ( (n) ); \
77 }
78 #endif
79 
80 static int unhexify(unsigned char *obuf, const char *ibuf)
81 {
82  unsigned char c, c2;
83  int len = strlen(ibuf) / 2;
84  assert(!(strlen(ibuf) %1)); // must be even number of bytes
85 
86  while (*ibuf != 0)
87  {
88  c = *ibuf++;
89  if( c >= '0' && c <= '9' )
90  c -= '0';
91  else if( c >= 'a' && c <= 'f' )
92  c -= 'a' - 10;
93  else if( c >= 'A' && c <= 'F' )
94  c -= 'A' - 10;
95  else
96  assert( 0 );
97 
98  c2 = *ibuf++;
99  if( c2 >= '0' && c2 <= '9' )
100  c2 -= '0';
101  else if( c2 >= 'a' && c2 <= 'f' )
102  c2 -= 'a' - 10;
103  else if( c2 >= 'A' && c2 <= 'F' )
104  c2 -= 'A' - 10;
105  else
106  assert( 0 );
107 
108  *obuf++ = ( c << 4 ) | c2;
109  }
110 
111  return len;
112 }
113 
114 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
115 {
116  unsigned char l, h;
117 
118  while (len != 0)
119  {
120  h = (*ibuf) / 16;
121  l = (*ibuf) % 16;
122 
123  if( h < 10 )
124  *obuf++ = '0' + h;
125  else
126  *obuf++ = 'a' + h - 10;
127 
128  if( l < 10 )
129  *obuf++ = '0' + l;
130  else
131  *obuf++ = 'a' + l - 10;
132 
133  ++ibuf;
134  len--;
135  }
136 }
137 
147 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
148 {
149  size_t i;
150 
151  if( rng_state != NULL )
152  rng_state = NULL;
153 
154  for( i = 0; i < len; ++i )
155  output[i] = rand();
156 
157  return( 0 );
158 }
159 
165 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
166 {
167  if( rng_state != NULL )
168  rng_state = NULL;
169 
170  memset( output, 0, len );
171 
172  return( 0 );
173 }
174 
175 typedef struct
176 {
177  unsigned char *buf;
178  size_t length;
179 } rnd_buf_info;
180 
192 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
193 {
194  rnd_buf_info *info = (rnd_buf_info *) rng_state;
195  size_t use_len;
196 
197  if( rng_state == NULL )
198  return( rnd_std_rand( NULL, output, len ) );
199 
200  use_len = len;
201  if( len > info->length )
202  use_len = info->length;
203 
204  if( use_len )
205  {
206  memcpy( output, info->buf, use_len );
207  info->buf += use_len;
208  info->length -= use_len;
209  }
210 
211  if( len - use_len > 0 )
212  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
213 
214  return( 0 );
215 }
216 
224 typedef struct
225 {
226  uint32_t key[16];
227  uint32_t v0, v1;
229 
238 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
239 {
240  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
241  uint32_t i, *k, sum, delta=0x9E3779B9;
242  unsigned char result[4];
243 
244  if( rng_state == NULL )
245  return( rnd_std_rand( NULL, output, len ) );
246 
247  k = info->key;
248 
249  while( len > 0 )
250  {
251  size_t use_len = ( len > 4 ) ? 4 : len;
252  sum = 0;
253 
254  for( i = 0; i < 32; i++ )
255  {
256  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
257  sum += delta;
258  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
259  }
260 
261  PUT_UINT32_BE( info->v0, result, 0 );
262  memcpy( output, result, use_len );
263  len -= use_len;
264  }
265 
266  return( 0 );
267 }
268 
269 #if defined(WANT_NOT_RND_MPI)
270 
278 #define ciL (sizeof(t_uint)) /* chars in limb */
279 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
280 static int not_rnd_mpi( void *in, unsigned char *out, size_t len )
281 {
282  char *str = (char *) in;
283  mpi X;
284 
285  /*
286  * The 'in' pointer we get is from an MPI prepared by mpi_fill_random(),
287  * just reconstruct the rest in order to be able to call mpi_read_string()
288  */
289  X.s = 1;
290  X.p = (t_uint *) out;
291  X.n = CHARS_TO_LIMBS( len );
292 
293  /*
294  * If str is too long, mpi_read_string() will try to allocate a new buffer
295  * for X.p, which we want to avoid at all costs.
296  */
297  assert( strlen( str ) / 2 == len );
298 
299  return( mpi_read_string( &X, 16, str ) );
300 }
301 #endif /* WANT_NOT_RND_MPI */
302 
303 
304 #include <stdio.h>
305 #include <string.h>
306 
307 static int test_errors = 0;
308 
309 #ifdef POLARSSL_PK_C
310 
311 #define TEST_SUITE_ACTIVE
312 
313 static int test_assert( int correct, char *test )
314 {
315  if( correct )
316  return( 0 );
317 
318  test_errors++;
319  if( test_errors == 1 )
320  printf( "FAILED\n" );
321  printf( " %s\n", test );
322 
323  return( 1 );
324 }
325 
326 #define TEST_ASSERT( TEST ) \
327  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
328  if( test_errors) return; \
329  } while (0)
330 
331 int verify_string( char **str )
332 {
333  if( (*str)[0] != '"' ||
334  (*str)[strlen( *str ) - 1] != '"' )
335  {
336  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
337  return( -1 );
338  }
339 
340  (*str)++;
341  (*str)[strlen( *str ) - 1] = '\0';
342 
343  return( 0 );
344 }
345 
346 int verify_int( char *str, int *value )
347 {
348  size_t i;
349  int minus = 0;
350  int digits = 1;
351  int hex = 0;
352 
353  for( i = 0; i < strlen( str ); i++ )
354  {
355  if( i == 0 && str[i] == '-' )
356  {
357  minus = 1;
358  continue;
359  }
360 
361  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
362  str[i - 1] == '0' && str[i] == 'x' )
363  {
364  hex = 1;
365  continue;
366  }
367 
368  if( str[i] < '0' || str[i] > '9' )
369  {
370  digits = 0;
371  break;
372  }
373  }
374 
375  if( digits )
376  {
377  if( hex )
378  *value = strtol( str, NULL, 16 );
379  else
380  *value = strtol( str, NULL, 10 );
381 
382  return( 0 );
383  }
384 
385  if( strcmp( str, "POLARSSL_PK_ECKEY_DH" ) == 0 )
386  {
387  *value = ( POLARSSL_PK_ECKEY_DH );
388  return( 0 );
389  }
390  if( strcmp( str, "POLARSSL_PK_ECKEY" ) == 0 )
391  {
392  *value = ( POLARSSL_PK_ECKEY );
393  return( 0 );
394  }
395 #ifdef POLARSSL_ECDSA_C
396  if( strcmp( str, "POLARSSL_ERR_ECP_VERIFY_FAILED" ) == 0 )
397  {
398  *value = ( POLARSSL_ERR_ECP_VERIFY_FAILED );
399  return( 0 );
400  }
401 #endif // POLARSSL_ECDSA_C
402  if( strcmp( str, "POLARSSL_PK_RSA" ) == 0 )
403  {
404  *value = ( POLARSSL_PK_RSA );
405  return( 0 );
406  }
407 #ifdef POLARSSL_ECDSA_C
408  if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1" ) == 0 )
409  {
410  *value = ( POLARSSL_ECP_DP_SECP192R1 );
411  return( 0 );
412  }
413 #endif // POLARSSL_ECDSA_C
414 #ifdef POLARSSL_RSA_C
415  if( strcmp( str, "POLARSSL_ERR_RSA_INVALID_PADDING" ) == 0 )
416  {
418  return( 0 );
419  }
420 #endif // POLARSSL_RSA_C
421 #ifdef POLARSSL_RSA_C
422  if( strcmp( str, "POLARSSL_ERR_RSA_VERIFY_FAILED" ) == 0 )
423  {
424  *value = ( POLARSSL_ERR_RSA_VERIFY_FAILED );
425  return( 0 );
426  }
427 #endif // POLARSSL_RSA_C
428 #ifdef POLARSSL_RSA_C
429  if( strcmp( str, "POLARSSL_MD_SHA1" ) == 0 )
430  {
431  *value = ( POLARSSL_MD_SHA1 );
432  return( 0 );
433  }
434 #endif // POLARSSL_RSA_C
435  if( strcmp( str, "POLARSSL_PK_ECDSA" ) == 0 )
436  {
437  *value = ( POLARSSL_PK_ECDSA );
438  return( 0 );
439  }
440  if( strcmp( str, "POLARSSL_ERR_PK_TYPE_MISMATCH" ) == 0 )
441  {
442  *value = ( POLARSSL_ERR_PK_TYPE_MISMATCH );
443  return( 0 );
444  }
445 
446 
447  printf( "Expected integer for parameter and got: %s\n", str );
448  return( -1 );
449 }
450 
451 void test_suite_pk_utils( int type, int size, int len, char *name )
452 {
453  pk_context pk;
454 
455  pk_init( &pk );
456 
457  TEST_ASSERT( pk_init_ctx( &pk, pk_info_from_type( type ) ) == 0 );
458  TEST_ASSERT( pk_genkey( &pk ) == 0 );
459 
460  TEST_ASSERT( (int) pk_get_type( &pk ) == type );
461  TEST_ASSERT( pk_can_do( &pk, type ) );
462  TEST_ASSERT( pk_get_size( &pk ) == (unsigned) size );
463  TEST_ASSERT( pk_get_len( &pk ) == (unsigned) len );
464  TEST_ASSERT( strcmp( pk_get_name( &pk), name ) == 0 );
465 
466  pk_free( &pk );
467 }
468 
469 #ifdef POLARSSL_RSA_C
470 void test_suite_pk_rsa_verify_test_vec( char *message_hex_string, int digest,
471  int mod, int radix_N, char *input_N, int radix_E,
472  char *input_E, char *result_hex_str, int result )
473 {
474  unsigned char message_str[1000];
475  unsigned char hash_result[1000];
476  unsigned char result_str[1000];
477  rsa_context *rsa;
478  pk_context pk;
479  int msg_len;
480 
481  pk_init( &pk );
482 
483  memset( message_str, 0x00, 1000 );
484  memset( hash_result, 0x00, 1000 );
485  memset( result_str, 0x00, 1000 );
486 
488  rsa = pk_rsa( pk );
489 
490  rsa->len = mod / 8;
491  TEST_ASSERT( mpi_read_string( &rsa->N, radix_N, input_N ) == 0 );
492  TEST_ASSERT( mpi_read_string( &rsa->E, radix_E, input_E ) == 0 );
493 
494  msg_len = unhexify( message_str, message_hex_string );
495  unhexify( result_str, result_hex_str );
496 
497  if( md_info_from_type( digest ) != NULL )
498  TEST_ASSERT( md( md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
499 
500  TEST_ASSERT( pk_verify( &pk, digest, hash_result, 0,
501  result_str, pk_get_len( &pk ) ) == result );
502 
503  pk_free( &pk );
504 }
505 #endif /* POLARSSL_RSA_C */
506 
507 #ifdef POLARSSL_ECDSA_C
508 void test_suite_pk_ec_test_vec( int type, int id, char *key_str,
509  char *hash_str, char * sig_str, int ret )
510 {
511  pk_context pk;
512  ecp_keypair *eckey;
513  unsigned char hash[100], sig[500], key[500];
514  size_t hash_len, sig_len, key_len;
515 
516  pk_init( &pk );
517 
518  memset( hash, 0, sizeof( hash ) ); hash_len = unhexify(hash, hash_str);
519  memset( sig, 0, sizeof( sig ) ); sig_len = unhexify(sig, sig_str);
520  memset( key, 0, sizeof( key ) ); key_len = unhexify(key, key_str);
521 
522  TEST_ASSERT( pk_init_ctx( &pk, pk_info_from_type( type ) ) == 0 );
523 
525  eckey = pk_ec( pk );
526 
527  TEST_ASSERT( ecp_use_known_dp( &eckey->grp, id ) == 0 );
528  TEST_ASSERT( ecp_point_read_binary( &eckey->grp, &eckey->Q,
529  key, key_len ) == 0 );
530 
532  hash, hash_len, sig, sig_len ) == ret );
533 
534  pk_free( &pk );
535 }
536 #endif /* POLARSSL_ECDSA_C */
537 
538 void test_suite_pk_sign_verify( int type, int sign_ret, int verify_ret )
539 {
540  pk_context pk;
541  unsigned char hash[50], sig[5000];
542  size_t sig_len;
543 
544  pk_init( &pk );
545 
546  memset( hash, 0x2a, sizeof hash );
547  memset( sig, 0, sizeof sig );
548 
549  TEST_ASSERT( pk_init_ctx( &pk, pk_info_from_type( type ) ) == 0 );
550  TEST_ASSERT( pk_genkey( &pk ) == 0 );
551 
552  TEST_ASSERT( pk_sign( &pk, POLARSSL_MD_NONE, hash, sizeof hash,
553  sig, &sig_len, rnd_std_rand, NULL ) == sign_ret );
554 
556  hash, sizeof hash, sig, sig_len ) == verify_ret );
557 
558  pk_free( &pk );
559 }
560 
561 #ifdef POLARSSL_RSA_C
562 void test_suite_pk_rsa_encrypt_test_vec( char *message_hex, int mod,
563  int radix_N, char *input_N,
564  int radix_E, char *input_E,
565  char *result_hex, int ret )
566 {
567  unsigned char message[1000];
568  unsigned char output[1000];
569  unsigned char result[1000];
570  size_t msg_len, olen, res_len;
571  rnd_pseudo_info rnd_info;
572  rsa_context *rsa;
573  pk_context pk;
574 
575  memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
576  memset( message, 0, sizeof( message ) );
577  memset( output, 0, sizeof( output ) );
578  memset( result, 0, sizeof( result ) );
579 
580  msg_len = unhexify( message, message_hex );
581  res_len = unhexify( result, result_hex );
582 
583  pk_init( &pk );
585  rsa = pk_rsa( pk );
586 
587  rsa->len = mod / 8;
588  TEST_ASSERT( mpi_read_string( &rsa->N, radix_N, input_N ) == 0 );
589  TEST_ASSERT( mpi_read_string( &rsa->E, radix_E, input_E ) == 0 );
590 
591  TEST_ASSERT( pk_encrypt( &pk, message, msg_len,
592  output, &olen, sizeof( output ),
593  rnd_pseudo_rand, &rnd_info ) == ret );
594  TEST_ASSERT( olen == res_len );
595  TEST_ASSERT( memcmp( output, result, olen ) == 0 );
596 
597  pk_free( &pk );
598 }
599 #endif /* POLARSSL_RSA_C */
600 
601 #ifdef POLARSSL_RSA_C
602 void test_suite_pk_rsa_decrypt_test_vec( char *cipher_hex, int mod,
603  int radix_P, char *input_P,
604  int radix_Q, char *input_Q,
605  int radix_N, char *input_N,
606  int radix_E, char *input_E,
607  char *clear_hex, int ret )
608 {
609  unsigned char clear[1000];
610  unsigned char output[1000];
611  unsigned char cipher[1000];
612  size_t clear_len, olen, cipher_len;
613  rnd_pseudo_info rnd_info;
614  mpi P1, Q1, H, G;
615  rsa_context *rsa;
616  pk_context pk;
617 
618  pk_init( &pk );
619  mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
620 
621  memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
622  memset( clear, 0, sizeof( clear ) );
623  memset( cipher, 0, sizeof( cipher ) );
624 
625  clear_len = unhexify( clear, clear_hex );
626  cipher_len = unhexify( cipher, cipher_hex );
627 
628  /* init pk-rsa context */
630  rsa = pk_rsa( pk );
631 
632  /* load public key */
633  rsa->len = mod / 8;
634  TEST_ASSERT( mpi_read_string( &rsa->N, radix_N, input_N ) == 0 );
635  TEST_ASSERT( mpi_read_string( &rsa->E, radix_E, input_E ) == 0 );
636 
637  /* load private key */
638  TEST_ASSERT( mpi_read_string( &rsa->P, radix_P, input_P ) == 0 );
639  TEST_ASSERT( mpi_read_string( &rsa->Q, radix_Q, input_Q ) == 0 );
640  TEST_ASSERT( mpi_sub_int( &P1, &rsa->P, 1 ) == 0 );
641  TEST_ASSERT( mpi_sub_int( &Q1, &rsa->Q, 1 ) == 0 );
642  TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
643  TEST_ASSERT( mpi_gcd( &G, &rsa->E, &H ) == 0 );
644  TEST_ASSERT( mpi_inv_mod( &rsa->D , &rsa->E, &H ) == 0 );
645  TEST_ASSERT( mpi_mod_mpi( &rsa->DP, &rsa->D, &P1 ) == 0 );
646  TEST_ASSERT( mpi_mod_mpi( &rsa->DQ, &rsa->D, &Q1 ) == 0 );
647  TEST_ASSERT( mpi_inv_mod( &rsa->QP, &rsa->Q, &rsa->P ) == 0 );
648 
649  /* decryption test */
650  memset( output, 0, sizeof( output ) );
651  olen = 0;
652  TEST_ASSERT( pk_decrypt( &pk, cipher, cipher_len,
653  output, &olen, sizeof( output ),
654  rnd_pseudo_rand, &rnd_info ) == ret );
655  if( ret == 0 )
656  {
657  TEST_ASSERT( olen == clear_len );
658  TEST_ASSERT( memcmp( output, clear, olen ) == 0 );
659  }
660 
661  mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
662  pk_free( &pk );
663 }
664 #endif /* POLARSSL_RSA_C */
665 
666 void test_suite_pk_ec_nocrypt( int type )
667 {
668  pk_context pk;
669  unsigned char output[100];
670  unsigned char input[100];
671  rnd_pseudo_info rnd_info;
672  size_t olen = 0;
674 
675  pk_init( &pk );
676 
677  memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
678  memset( output, 0, sizeof( output ) );
679  memset( input, 0, sizeof( input ) );
680 
681  TEST_ASSERT( pk_init_ctx( &pk, pk_info_from_type( type ) ) == 0 );
682 
683  TEST_ASSERT( pk_encrypt( &pk, input, sizeof( input ),
684  output, &olen, sizeof( output ),
685  rnd_pseudo_rand, &rnd_info ) == ret );
686 
687  TEST_ASSERT( pk_decrypt( &pk, input, sizeof( input ),
688  output, &olen, sizeof( output ),
689  rnd_pseudo_rand, &rnd_info ) == ret );
690 
691  pk_free( &pk );
692 }
693 
694 
695 #endif /* POLARSSL_PK_C */
696 
697 
698 int dep_check( char *str )
699 {
700  if( str == NULL )
701  return( 1 );
702 
703  if( strcmp( str, "POLARSSL_ECP_C" ) == 0 )
704  {
705 #if defined(POLARSSL_ECP_C)
706  return( 0 );
707 #else
708  return( 1 );
709 #endif
710  }
711  if( strcmp( str, "POLARSSL_SHA1_C" ) == 0 )
712  {
713 #if defined(POLARSSL_SHA1_C)
714  return( 0 );
715 #else
716  return( 1 );
717 #endif
718  }
719  if( strcmp( str, "POLARSSL_RSA_C" ) == 0 )
720  {
721 #if defined(POLARSSL_RSA_C)
722  return( 0 );
723 #else
724  return( 1 );
725 #endif
726  }
727  if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
728  {
729 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
730  return( 0 );
731 #else
732  return( 1 );
733 #endif
734  }
735  if( strcmp( str, "POLARSSL_ECDSA_C" ) == 0 )
736  {
737 #if defined(POLARSSL_ECDSA_C)
738  return( 0 );
739 #else
740  return( 1 );
741 #endif
742  }
743  if( strcmp( str, "POLARSSL_PKCS1_V15" ) == 0 )
744  {
745 #if defined(POLARSSL_PKCS1_V15)
746  return( 0 );
747 #else
748  return( 1 );
749 #endif
750  }
751 
752 
753  return( 1 );
754 }
755 
756 int dispatch_test(int cnt, char *params[50])
757 {
758  int ret;
759  ((void) cnt);
760  ((void) params);
761 
762 #if defined(TEST_SUITE_ACTIVE)
763  if( strcmp( params[0], "pk_utils" ) == 0 )
764  {
765 
766  int param1;
767  int param2;
768  int param3;
769  char *param4 = params[4];
770 
771  if( cnt != 5 )
772  {
773  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
774  return( 2 );
775  }
776 
777  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
778  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
779  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
780  if( verify_string( &param4 ) != 0 ) return( 2 );
781 
782  test_suite_pk_utils( param1, param2, param3, param4 );
783  return ( 0 );
784 
785  return ( 3 );
786  }
787  else
788  if( strcmp( params[0], "pk_rsa_verify_test_vec" ) == 0 )
789  {
790  #ifdef POLARSSL_RSA_C
791 
792  char *param1 = params[1];
793  int param2;
794  int param3;
795  int param4;
796  char *param5 = params[5];
797  int param6;
798  char *param7 = params[7];
799  char *param8 = params[8];
800  int param9;
801 
802  if( cnt != 10 )
803  {
804  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 10 );
805  return( 2 );
806  }
807 
808  if( verify_string( &param1 ) != 0 ) return( 2 );
809  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
810  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
811  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
812  if( verify_string( &param5 ) != 0 ) return( 2 );
813  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
814  if( verify_string( &param7 ) != 0 ) return( 2 );
815  if( verify_string( &param8 ) != 0 ) return( 2 );
816  if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
817 
818  test_suite_pk_rsa_verify_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
819  return ( 0 );
820  #endif /* POLARSSL_RSA_C */
821 
822  return ( 3 );
823  }
824  else
825  if( strcmp( params[0], "pk_ec_test_vec" ) == 0 )
826  {
827  #ifdef POLARSSL_ECDSA_C
828 
829  int param1;
830  int param2;
831  char *param3 = params[3];
832  char *param4 = params[4];
833  char *param5 = params[5];
834  int param6;
835 
836  if( cnt != 7 )
837  {
838  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
839  return( 2 );
840  }
841 
842  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
843  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
844  if( verify_string( &param3 ) != 0 ) return( 2 );
845  if( verify_string( &param4 ) != 0 ) return( 2 );
846  if( verify_string( &param5 ) != 0 ) return( 2 );
847  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
848 
849  test_suite_pk_ec_test_vec( param1, param2, param3, param4, param5, param6 );
850  return ( 0 );
851  #endif /* POLARSSL_ECDSA_C */
852 
853  return ( 3 );
854  }
855  else
856  if( strcmp( params[0], "pk_sign_verify" ) == 0 )
857  {
858 
859  int param1;
860  int param2;
861  int param3;
862 
863  if( cnt != 4 )
864  {
865  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
866  return( 2 );
867  }
868 
869  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
870  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
871  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
872 
873  test_suite_pk_sign_verify( param1, param2, param3 );
874  return ( 0 );
875 
876  return ( 3 );
877  }
878  else
879  if( strcmp( params[0], "pk_rsa_encrypt_test_vec" ) == 0 )
880  {
881  #ifdef POLARSSL_RSA_C
882 
883  char *param1 = params[1];
884  int param2;
885  int param3;
886  char *param4 = params[4];
887  int param5;
888  char *param6 = params[6];
889  char *param7 = params[7];
890  int param8;
891 
892  if( cnt != 9 )
893  {
894  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 9 );
895  return( 2 );
896  }
897 
898  if( verify_string( &param1 ) != 0 ) return( 2 );
899  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
900  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
901  if( verify_string( &param4 ) != 0 ) return( 2 );
902  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
903  if( verify_string( &param6 ) != 0 ) return( 2 );
904  if( verify_string( &param7 ) != 0 ) return( 2 );
905  if( verify_int( params[8], &param8 ) != 0 ) return( 2 );
906 
907  test_suite_pk_rsa_encrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8 );
908  return ( 0 );
909  #endif /* POLARSSL_RSA_C */
910 
911  return ( 3 );
912  }
913  else
914  if( strcmp( params[0], "pk_rsa_decrypt_test_vec" ) == 0 )
915  {
916  #ifdef POLARSSL_RSA_C
917 
918  char *param1 = params[1];
919  int param2;
920  int param3;
921  char *param4 = params[4];
922  int param5;
923  char *param6 = params[6];
924  int param7;
925  char *param8 = params[8];
926  int param9;
927  char *param10 = params[10];
928  char *param11 = params[11];
929  int param12;
930 
931  if( cnt != 13 )
932  {
933  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 13 );
934  return( 2 );
935  }
936 
937  if( verify_string( &param1 ) != 0 ) return( 2 );
938  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
939  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
940  if( verify_string( &param4 ) != 0 ) return( 2 );
941  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
942  if( verify_string( &param6 ) != 0 ) return( 2 );
943  if( verify_int( params[7], &param7 ) != 0 ) return( 2 );
944  if( verify_string( &param8 ) != 0 ) return( 2 );
945  if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
946  if( verify_string( &param10 ) != 0 ) return( 2 );
947  if( verify_string( &param11 ) != 0 ) return( 2 );
948  if( verify_int( params[12], &param12 ) != 0 ) return( 2 );
949 
950  test_suite_pk_rsa_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12 );
951  return ( 0 );
952  #endif /* POLARSSL_RSA_C */
953 
954  return ( 3 );
955  }
956  else
957  if( strcmp( params[0], "pk_ec_nocrypt" ) == 0 )
958  {
959 
960  int param1;
961 
962  if( cnt != 2 )
963  {
964  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
965  return( 2 );
966  }
967 
968  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
969 
970  test_suite_pk_ec_nocrypt( param1 );
971  return ( 0 );
972 
973  return ( 3 );
974  }
975  else
976 
977  {
978  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
979  fflush( stdout );
980  return( 1 );
981  }
982 #else
983  return( 3 );
984 #endif
985  return( ret );
986 }
987 
988 int get_line( FILE *f, char *buf, size_t len )
989 {
990  char *ret;
991 
992  ret = fgets( buf, len, f );
993  if( ret == NULL )
994  return( -1 );
995 
996  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
997  buf[strlen(buf) - 1] = '\0';
998  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
999  buf[strlen(buf) - 1] = '\0';
1000 
1001  return( 0 );
1002 }
1003 
1004 int parse_arguments( char *buf, size_t len, char *params[50] )
1005 {
1006  int cnt = 0, i;
1007  char *cur = buf;
1008  char *p = buf, *q;
1009 
1010  params[cnt++] = cur;
1011 
1012  while( *p != '\0' && p < buf + len )
1013  {
1014  if( *p == '\\' )
1015  {
1016  *p++;
1017  *p++;
1018  continue;
1019  }
1020  if( *p == ':' )
1021  {
1022  if( p + 1 < buf + len )
1023  {
1024  cur = p + 1;
1025  params[cnt++] = cur;
1026  }
1027  *p = '\0';
1028  }
1029 
1030  *p++;
1031  }
1032 
1033  // Replace newlines, question marks and colons in strings
1034  for( i = 0; i < cnt; i++ )
1035  {
1036  p = params[i];
1037  q = params[i];
1038 
1039  while( *p != '\0' )
1040  {
1041  if( *p == '\\' && *(p + 1) == 'n' )
1042  {
1043  p += 2;
1044  *(q++) = '\n';
1045  }
1046  else if( *p == '\\' && *(p + 1) == ':' )
1047  {
1048  p += 2;
1049  *(q++) = ':';
1050  }
1051  else if( *p == '\\' && *(p + 1) == '?' )
1052  {
1053  p += 2;
1054  *(q++) = '?';
1055  }
1056  else
1057  *(q++) = *(p++);
1058  }
1059  *q = '\0';
1060  }
1061 
1062  return( cnt );
1063 }
1064 
1065 int main()
1066 {
1067  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1068  const char *filename = "/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_pk.data";
1069  FILE *file;
1070  char buf[5000];
1071  char *params[50];
1072 
1073 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1074  unsigned char alloc_buf[1000000];
1075  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1076 #endif
1077 
1078  file = fopen( filename, "r" );
1079  if( file == NULL )
1080  {
1081  fprintf( stderr, "Failed to open\n" );
1082  return( 1 );
1083  }
1084 
1085  while( !feof( file ) )
1086  {
1087  int skip = 0;
1088 
1089  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1090  break;
1091  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1092  fprintf( stdout, " " );
1093  for( i = strlen( buf ) + 1; i < 67; i++ )
1094  fprintf( stdout, "." );
1095  fprintf( stdout, " " );
1096  fflush( stdout );
1097 
1098  total_tests++;
1099 
1100  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1101  break;
1102  cnt = parse_arguments( buf, strlen(buf), params );
1103 
1104  if( strcmp( params[0], "depends_on" ) == 0 )
1105  {
1106  for( i = 1; i < cnt; i++ )
1107  if( dep_check( params[i] ) != 0 )
1108  skip = 1;
1109 
1110  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1111  break;
1112  cnt = parse_arguments( buf, strlen(buf), params );
1113  }
1114 
1115  if( skip == 0 )
1116  {
1117  test_errors = 0;
1118  ret = dispatch_test( cnt, params );
1119  }
1120 
1121  if( skip == 1 || ret == 3 )
1122  {
1123  total_skipped++;
1124  fprintf( stdout, "----\n" );
1125  fflush( stdout );
1126  }
1127  else if( ret == 0 && test_errors == 0 )
1128  {
1129  fprintf( stdout, "PASS\n" );
1130  fflush( stdout );
1131  }
1132  else if( ret == 2 )
1133  {
1134  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1135  fclose(file);
1136  exit( 2 );
1137  }
1138  else
1139  total_errors++;
1140 
1141  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1142  break;
1143  if( strlen(buf) != 0 )
1144  {
1145  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1146  return( 1 );
1147  }
1148  }
1149  fclose(file);
1150 
1151  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1152  if( total_errors == 0 )
1153  fprintf( stdout, "PASSED" );
1154  else
1155  fprintf( stdout, "FAILED" );
1156 
1157  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1158  total_tests - total_errors, total_tests, total_skipped );
1159 
1160 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1161 #if defined(POLARSSL_MEMORY_DEBUG)
1162  memory_buffer_alloc_status();
1163 #endif
1164  memory_buffer_alloc_free();
1165 #endif
1166 
1167  return( total_errors != 0 );
1168 }
1169 
1170 
int md(const md_info_t *md_info, const unsigned char *input, size_t ilen, unsigned char *output)
Output = message_digest( input buffer )
static size_t pk_get_len(const pk_context *ctx)
Get the length in bytes of the underlying key.
Definition: pk.h:264
#define PUT_UINT32_BE(n, b, i)
Definition: test_suite_pk.c:71
const pk_info_t * pk_info_from_type(pk_type_t pk_type)
Return information associated with the given PK type.
Memory allocation layer.
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
uint32_t t_uint
Definition: bignum.h:149
int mpi_gcd(mpi *G, const mpi *A, const mpi *B)
Greatest common divisor: G = gcd(A, B)
Info structure for the pseudo random function.
static int test_errors
static int unhexify(unsigned char *obuf, const char *ibuf)
Definition: test_suite_pk.c:80
size_t pk_get_size(const pk_context *ctx)
Get the size in bits of the underlying key.
int s
Definition: bignum.h:173
ecp_group grp
Definition: ecp.h:146
int pk_decrypt(pk_context *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, size_t osize, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Decrypt message.
mpi DQ
Definition: rsa.h:89
Configuration options (set of defines)
ECP key pair structure.
Definition: ecp.h:144
MPI structure.
Definition: bignum.h:171
#define pk_ec(pk)
Quick access to an EC context inside a PK context.
Definition: pk.h:79
pk_type_t pk_get_type(const pk_context *ctx)
Get the key type.
static int test_assert(int correct, char *test)
const char * pk_get_name(const pk_context *ctx)
Access the type name.
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.
void mpi_init(mpi *X)
Initialize one MPI.
int main(int argc, char *argv[])
Public Key abstraction layer.
Multi-precision integer library.
size_t len
Definition: rsa.h:80
int dep_check(char *str)
mpi P
Definition: rsa.h:86
#define TEST_ASSERT(TEST)
mpi Q
Definition: rsa.h:87
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
#define POLARSSL_ERR_ECP_VERIFY_FAILED
The signature is not valid.
Definition: ecp.h:38
RSA context structure.
Definition: rsa.h:77
mpi D
Definition: rsa.h:85
#define POLARSSL_ERR_RSA_INVALID_PADDING
Input data contains invalid padding and is rejected.
Definition: rsa.h:43
mpi QP
Definition: rsa.h:90
mpi N
Definition: rsa.h:82
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 pk_verify(pk_context *ctx, md_type_t md_alg, const unsigned char *hash, size_t hash_len, const unsigned char *sig, size_t sig_len)
Verify signature.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int mpi_inv_mod(mpi *X, const mpi *A, const mpi *N)
Modular inverse: X = A^-1 mod N.
void mpi_free(mpi *X)
Unallocate one MPI.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int pk_can_do(pk_context *ctx, pk_type_t type)
Tell if a context can do the operation given by type.
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.
mpi E
Definition: rsa.h:83
int parse_arguments(char *buf, size_t len, char *params[50])
mpi DP
Definition: rsa.h:88
#define POLARSSL_ERR_RSA_VERIFY_FAILED
The PKCS#1 verification failed.
Definition: rsa.h:48
int pk_init_ctx(pk_context *ctx, const pk_info_t *info)
Initialize a PK context with the information given and allocates the type-specific PK subcontext...
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 verify_string(char **str)
#define pk_rsa(pk)
Quick access to an RSA context inside a PK context.
Definition: pk.h:69
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_ERR_PK_TYPE_MISMATCH
Type mismatch, eg attempt to encrypt with an ECDSA key.
Definition: pk.h:48
void pk_free(pk_context *ctx)
Free a pk_context.
int pk_sign(pk_context *ctx, md_type_t md_alg, const unsigned char *hash, size_t hash_len, unsigned char *sig, size_t *sig_len, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Make signature.
int dispatch_test(int cnt, char *params[50])
int rsa_gen_key(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, unsigned int nbits, int exponent)
Generate an RSA keypair.
size_t n
Definition: bignum.h:174
int mpi_mod_mpi(mpi *R, const mpi *A, const mpi *B)
Modulo: R = A mod B.
int pk_encrypt(pk_context *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, size_t osize, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Encrypt message.
void pk_init(pk_context *ctx)
Initialize a pk_context (as NONE)
unsigned char * buf
ecp_point Q
Definition: ecp.h:148
int mpi_mul_mpi(mpi *X, const mpi *A, const mpi *B)
Baseline multiplication: X = A * B.
int verify_int(char *str, int *value)
int mpi_sub_int(mpi *X, const mpi *A, t_sint b)
Signed subtraction: X = A - b.
Public key container.
Definition: pk.h:177
int get_line(FILE *f, char *buf, size_t len)