PolarSSL v1.3.9
test_suite_pk.c
Go to the documentation of this file.
1 #if !defined(POLARSSL_CONFIG_FILE)
2 #include <polarssl/config.h>
3 #else
4 #include POLARSSL_CONFIG_FILE
5 #endif
6 
7 #ifdef POLARSSL_PK_C
8 
9 #include <polarssl/pk.h>
10 
11 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len );
12 
13 #define RSA_KEY_SIZE 512
14 #define RSA_KEY_LEN 64
15 
16 static int pk_genkey( pk_context *pk )
17 {
18  ((void) pk);
19 
20 #if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
21  if( pk_get_type( pk ) == POLARSSL_PK_RSA )
22  return rsa_gen_key( pk_rsa( *pk ), rnd_std_rand, NULL, RSA_KEY_SIZE, 3 );
23 #endif
24 #if defined(POLARSSL_ECP_C)
25  if( pk_get_type( pk ) == POLARSSL_PK_ECKEY ||
28  {
29  int ret;
30  if( ( ret = ecp_use_known_dp( &pk_ec( *pk )->grp,
31  POLARSSL_ECP_DP_SECP192R1 ) ) != 0 )
32  return( ret );
33 
34  return ecp_gen_keypair( &pk_ec( *pk )->grp, &pk_ec( *pk )->d,
35  &pk_ec( *pk )->Q, rnd_std_rand, NULL );
36  }
37 #endif
38  return( -1 );
39 }
40 
41 #if defined(POLARSSL_RSA_C)
42 int rsa_decrypt_func( void *ctx, int mode, size_t *olen,
43  const unsigned char *input, unsigned char *output,
44  size_t output_max_len )
45 {
46  return( rsa_pkcs1_decrypt( (rsa_context *) ctx, NULL, NULL, mode, olen,
47  input, output, output_max_len ) );
48 }
49 int rsa_sign_func( void *ctx,
50  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
51  int mode, md_type_t md_alg, unsigned int hashlen,
52  const unsigned char *hash, unsigned char *sig )
53 {
54  return( rsa_pkcs1_sign( (rsa_context *) ctx, f_rng, p_rng, mode,
55  md_alg, hashlen, hash, sig ) );
56 }
57 size_t rsa_key_len_func( void *ctx )
58 {
59  return( ((const rsa_context *) ctx)->len );
60 }
61 #endif /* POLARSSL_RSA_C */
62 #endif /* POLARSSL_PK_C */
63 
64 
65 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
66 #include "polarssl/memory.h"
67 #endif
68 
69 #if defined(POLARSSL_PLATFORM_C)
70 #include "polarssl/platform.h"
71 #else
72 #define polarssl_malloc malloc
73 #define polarssl_free free
74 #endif
75 
76 #ifdef _MSC_VER
77 #include <basetsd.h>
78 typedef UINT32 uint32_t;
79 #else
80 #include <inttypes.h>
81 #endif
82 
83 #include <assert.h>
84 #include <stdlib.h>
85 #include <string.h>
86 
87 /*
88  * 32-bit integer manipulation macros (big endian)
89  */
90 #ifndef GET_UINT32_BE
91 #define GET_UINT32_BE(n,b,i) \
92 { \
93  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
94  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
95  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
96  | ( (uint32_t) (b)[(i) + 3] ); \
97 }
98 #endif
99 
100 #ifndef PUT_UINT32_BE
101 #define PUT_UINT32_BE(n,b,i) \
102 { \
103  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
104  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
105  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
106  (b)[(i) + 3] = (unsigned char) ( (n) ); \
107 }
108 #endif
109 
110 static int unhexify(unsigned char *obuf, const char *ibuf)
111 {
112  unsigned char c, c2;
113  int len = strlen(ibuf) / 2;
114  assert(!(strlen(ibuf) %1)); // must be even number of bytes
115 
116  while (*ibuf != 0)
117  {
118  c = *ibuf++;
119  if( c >= '0' && c <= '9' )
120  c -= '0';
121  else if( c >= 'a' && c <= 'f' )
122  c -= 'a' - 10;
123  else if( c >= 'A' && c <= 'F' )
124  c -= 'A' - 10;
125  else
126  assert( 0 );
127 
128  c2 = *ibuf++;
129  if( c2 >= '0' && c2 <= '9' )
130  c2 -= '0';
131  else if( c2 >= 'a' && c2 <= 'f' )
132  c2 -= 'a' - 10;
133  else if( c2 >= 'A' && c2 <= 'F' )
134  c2 -= 'A' - 10;
135  else
136  assert( 0 );
137 
138  *obuf++ = ( c << 4 ) | c2;
139  }
140 
141  return len;
142 }
143 
144 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
145 {
146  unsigned char l, h;
147 
148  while (len != 0)
149  {
150  h = (*ibuf) / 16;
151  l = (*ibuf) % 16;
152 
153  if( h < 10 )
154  *obuf++ = '0' + h;
155  else
156  *obuf++ = 'a' + h - 10;
157 
158  if( l < 10 )
159  *obuf++ = '0' + l;
160  else
161  *obuf++ = 'a' + l - 10;
162 
163  ++ibuf;
164  len--;
165  }
166 }
167 
175 static unsigned char *zero_alloc( size_t len )
176 {
177  void *p;
178  size_t actual_len = len != 0 ? len : 1;
179 
180  p = polarssl_malloc( actual_len );
181  assert( p != NULL );
182 
183  memset( p, 0x00, actual_len );
184 
185  return( p );
186 }
187 
198 static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
199 {
200  unsigned char *obuf;
201 
202  *olen = strlen(ibuf) / 2;
203 
204  if( *olen == 0 )
205  return( zero_alloc( *olen ) );
206 
207  obuf = polarssl_malloc( *olen );
208  assert( obuf != NULL );
209 
210  (void) unhexify( obuf, ibuf );
211 
212  return( obuf );
213 }
214 
224 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
225 {
226 #if !defined(__OpenBSD__)
227  size_t i;
228 
229  if( rng_state != NULL )
230  rng_state = NULL;
231 
232  for( i = 0; i < len; ++i )
233  output[i] = rand();
234 #else
235  if( rng_state != NULL )
236  rng_state = NULL;
237 
238  arc4random_buf( output, len );
239 #endif /* !OpenBSD */
240 
241  return( 0 );
242 }
243 
249 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
250 {
251  if( rng_state != NULL )
252  rng_state = NULL;
253 
254  memset( output, 0, len );
255 
256  return( 0 );
257 }
258 
259 typedef struct
260 {
261  unsigned char *buf;
262  size_t length;
263 } rnd_buf_info;
264 
276 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
277 {
278  rnd_buf_info *info = (rnd_buf_info *) rng_state;
279  size_t use_len;
280 
281  if( rng_state == NULL )
282  return( rnd_std_rand( NULL, output, len ) );
283 
284  use_len = len;
285  if( len > info->length )
286  use_len = info->length;
287 
288  if( use_len )
289  {
290  memcpy( output, info->buf, use_len );
291  info->buf += use_len;
292  info->length -= use_len;
293  }
294 
295  if( len - use_len > 0 )
296  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
297 
298  return( 0 );
299 }
300 
308 typedef struct
309 {
310  uint32_t key[16];
311  uint32_t v0, v1;
313 
322 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
323 {
324  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
325  uint32_t i, *k, sum, delta=0x9E3779B9;
326  unsigned char result[4], *out = output;
327 
328  if( rng_state == NULL )
329  return( rnd_std_rand( NULL, output, len ) );
330 
331  k = info->key;
332 
333  while( len > 0 )
334  {
335  size_t use_len = ( len > 4 ) ? 4 : len;
336  sum = 0;
337 
338  for( i = 0; i < 32; i++ )
339  {
340  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
341  sum += delta;
342  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
343  }
344 
345  PUT_UINT32_BE( info->v0, result, 0 );
346  memcpy( out, result, use_len );
347  len -= use_len;
348  out += 4;
349  }
350 
351  return( 0 );
352 }
353 
354 
355 #include <stdio.h>
356 #include <string.h>
357 
358 #if defined(POLARSSL_PLATFORM_C)
359 #include "polarssl/platform.h"
360 #else
361 #define polarssl_printf printf
362 #define polarssl_malloc malloc
363 #define polarssl_free free
364 #endif
365 
366 static int test_errors = 0;
367 
368 #ifdef POLARSSL_PK_C
369 
370 #define TEST_SUITE_ACTIVE
371 
372 static int test_assert( int correct, const char *test )
373 {
374  if( correct )
375  return( 0 );
376 
377  test_errors++;
378  if( test_errors == 1 )
379  printf( "FAILED\n" );
380  printf( " %s\n", test );
381 
382  return( 1 );
383 }
384 
385 #define TEST_ASSERT( TEST ) \
386  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
387  if( test_errors) goto exit; \
388  } while (0)
389 
390 int verify_string( char **str )
391 {
392  if( (*str)[0] != '"' ||
393  (*str)[strlen( *str ) - 1] != '"' )
394  {
395  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
396  return( -1 );
397  }
398 
399  (*str)++;
400  (*str)[strlen( *str ) - 1] = '\0';
401 
402  return( 0 );
403 }
404 
405 int verify_int( char *str, int *value )
406 {
407  size_t i;
408  int minus = 0;
409  int digits = 1;
410  int hex = 0;
411 
412  for( i = 0; i < strlen( str ); i++ )
413  {
414  if( i == 0 && str[i] == '-' )
415  {
416  minus = 1;
417  continue;
418  }
419 
420  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
421  str[i - 1] == '0' && str[i] == 'x' )
422  {
423  hex = 1;
424  continue;
425  }
426 
427  if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
428  ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
429  ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
430  {
431  digits = 0;
432  break;
433  }
434  }
435 
436  if( digits )
437  {
438  if( hex )
439  *value = strtol( str, NULL, 16 );
440  else
441  *value = strtol( str, NULL, 10 );
442 
443  return( 0 );
444  }
445 
446 #ifdef POLARSSL_ECDSA_C
447  if( strcmp( str, "POLARSSL_ERR_ECP_VERIFY_FAILED" ) == 0 )
448  {
449  *value = ( POLARSSL_ERR_ECP_VERIFY_FAILED );
450  return( 0 );
451  }
452 #endif // POLARSSL_ECDSA_C
453 #ifdef POLARSSL_ECDSA_C
454  if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1" ) == 0 )
455  {
456  *value = ( POLARSSL_ECP_DP_SECP192R1 );
457  return( 0 );
458  }
459 #endif // POLARSSL_ECDSA_C
460 #ifdef POLARSSL_RSA_C
461  if( strcmp( str, "RSA_SALT_LEN_ANY" ) == 0 )
462  {
463  *value = ( RSA_SALT_LEN_ANY );
464  return( 0 );
465  }
466 #endif // POLARSSL_RSA_C
467 #ifdef POLARSSL_RSA_C
468  if( strcmp( str, "POLARSSL_ERR_RSA_INVALID_PADDING" ) == 0 )
469  {
471  return( 0 );
472  }
473 #endif // POLARSSL_RSA_C
474 #ifdef POLARSSL_RSA_C
475  if( strcmp( str, "POLARSSL_ERR_PK_BAD_INPUT_DATA" ) == 0 )
476  {
477  *value = ( POLARSSL_ERR_PK_BAD_INPUT_DATA );
478  return( 0 );
479  }
480 #endif // POLARSSL_RSA_C
481  if( strcmp( str, "POLARSSL_PK_ECKEY_DH" ) == 0 )
482  {
483  *value = ( POLARSSL_PK_ECKEY_DH );
484  return( 0 );
485  }
486  if( strcmp( str, "POLARSSL_PK_RSA" ) == 0 )
487  {
488  *value = ( POLARSSL_PK_RSA );
489  return( 0 );
490  }
491  if( strcmp( str, "POLARSSL_PK_ECKEY" ) == 0 )
492  {
493  *value = ( POLARSSL_PK_ECKEY );
494  return( 0 );
495  }
496 #ifdef POLARSSL_RSA_C
497  if( strcmp( str, "POLARSSL_MD_SHA1" ) == 0 )
498  {
499  *value = ( POLARSSL_MD_SHA1 );
500  return( 0 );
501  }
502 #endif // POLARSSL_RSA_C
503  if( strcmp( str, "POLARSSL_PK_ECDSA" ) == 0 )
504  {
505  *value = ( POLARSSL_PK_ECDSA );
506  return( 0 );
507  }
508 #ifdef POLARSSL_RSA_C
509  if( strcmp( str, "-1" ) == 0 )
510  {
511  *value = ( -1 );
512  return( 0 );
513  }
514 #endif // POLARSSL_RSA_C
515 #ifdef POLARSSL_RSA_C
516  if( strcmp( str, "POLARSSL_MD_SHA256" ) == 0 )
517  {
518  *value = ( POLARSSL_MD_SHA256 );
519  return( 0 );
520  }
521 #endif // POLARSSL_RSA_C
522 #ifdef POLARSSL_RSA_C
523  if( strcmp( str, "POLARSSL_PK_RSASSA_PSS" ) == 0 )
524  {
525  *value = ( POLARSSL_PK_RSASSA_PSS );
526  return( 0 );
527  }
528 #endif // POLARSSL_RSA_C
529  if( strcmp( str, "POLARSSL_ERR_PK_TYPE_MISMATCH" ) == 0 )
530  {
531  *value = ( POLARSSL_ERR_PK_TYPE_MISMATCH );
532  return( 0 );
533  }
534 #ifdef POLARSSL_RSA_C
535  if( strcmp( str, "POLARSSL_ERR_RSA_VERIFY_FAILED" ) == 0 )
536  {
537  *value = ( POLARSSL_ERR_RSA_VERIFY_FAILED );
538  return( 0 );
539  }
540 #endif // POLARSSL_RSA_C
541 #ifdef POLARSSL_RSA_C
542  if( strcmp( str, "POLARSSL_MD_NONE" ) == 0 )
543  {
544  *value = ( POLARSSL_MD_NONE );
545  return( 0 );
546  }
547 #endif // POLARSSL_RSA_C
548 
549 
550  printf( "Expected integer for parameter and got: %s\n", str );
551  return( -1 );
552 }
553 
554 void test_suite_pk_utils( int type, int size, int len, char *name )
555 {
556  pk_context pk;
557 
558  pk_init( &pk );
559 
560  TEST_ASSERT( pk_init_ctx( &pk, pk_info_from_type( type ) ) == 0 );
561  TEST_ASSERT( pk_genkey( &pk ) == 0 );
562 
563  TEST_ASSERT( (int) pk_get_type( &pk ) == type );
564  TEST_ASSERT( pk_can_do( &pk, type ) );
565  TEST_ASSERT( pk_get_size( &pk ) == (unsigned) size );
566  TEST_ASSERT( pk_get_len( &pk ) == (unsigned) len );
567  TEST_ASSERT( strcmp( pk_get_name( &pk), name ) == 0 );
568 
569 exit:
570  pk_free( &pk );
571 }
572 
573 #ifdef POLARSSL_RSA_C
574 void test_suite_pk_rsa_verify_test_vec( char *message_hex_string, int digest,
575  int mod, int radix_N, char *input_N, int radix_E,
576  char *input_E, char *result_hex_str, int result )
577 {
578  unsigned char message_str[1000];
579  unsigned char hash_result[1000];
580  unsigned char result_str[1000];
581  rsa_context *rsa;
582  pk_context pk;
583  int msg_len;
584 
585  pk_init( &pk );
586 
587  memset( message_str, 0x00, 1000 );
588  memset( hash_result, 0x00, 1000 );
589  memset( result_str, 0x00, 1000 );
590 
592  rsa = pk_rsa( pk );
593 
594  rsa->len = mod / 8;
595  TEST_ASSERT( mpi_read_string( &rsa->N, radix_N, input_N ) == 0 );
596  TEST_ASSERT( mpi_read_string( &rsa->E, radix_E, input_E ) == 0 );
597 
598  msg_len = unhexify( message_str, message_hex_string );
599  unhexify( result_str, result_hex_str );
600 
601  if( md_info_from_type( digest ) != NULL )
602  TEST_ASSERT( md( md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
603 
604  TEST_ASSERT( pk_verify( &pk, digest, hash_result, 0,
605  result_str, pk_get_len( &pk ) ) == result );
606 
607 exit:
608  pk_free( &pk );
609 }
610 #endif /* POLARSSL_RSA_C */
611 
612 #ifdef POLARSSL_RSA_C
613 void test_suite_pk_rsa_verify_ext_test_vec( char *message_hex_string, int digest,
614  int mod, int radix_N, char *input_N, int radix_E,
615  char *input_E, char *result_hex_str,
616  int pk_type, int mgf1_hash_id, int salt_len,
617  int result )
618 {
619  unsigned char message_str[1000];
620  unsigned char hash_result[1000];
621  unsigned char result_str[1000];
622  rsa_context *rsa;
623  pk_context pk;
624  pk_rsassa_pss_options pss_opts;
625  void *options;
626  int msg_len;
627  size_t hash_len;
628 
629  pk_init( &pk );
630 
631  memset( message_str, 0x00, 1000 );
632  memset( hash_result, 0x00, 1000 );
633  memset( result_str, 0x00, 1000 );
634 
636  rsa = pk_rsa( pk );
637 
638  rsa->len = mod / 8;
639  TEST_ASSERT( mpi_read_string( &rsa->N, radix_N, input_N ) == 0 );
640  TEST_ASSERT( mpi_read_string( &rsa->E, radix_E, input_E ) == 0 );
641 
642  msg_len = unhexify( message_str, message_hex_string );
643  unhexify( result_str, result_hex_str );
644 
645  if( digest != POLARSSL_MD_NONE )
646  {
647  TEST_ASSERT( md( md_info_from_type( digest ),
648  message_str, msg_len, hash_result ) == 0 );
649  hash_len = 0;
650  }
651  else
652  {
653  memcpy( hash_result, message_str, msg_len );
654  hash_len = msg_len;
655  }
656 
657  if( mgf1_hash_id < 0 )
658  {
659  options = NULL;
660  }
661  else
662  {
663  options = &pss_opts;
664 
665  pss_opts.mgf1_hash_id = mgf1_hash_id;
666  pss_opts.expected_salt_len = salt_len;
667  }
668 
669  TEST_ASSERT( pk_verify_ext( pk_type, options, &pk,
670  digest, hash_result, hash_len,
671  result_str, pk_get_len( &pk ) ) == result );
672 
673 exit:
674  pk_free( &pk );
675 }
676 #endif /* POLARSSL_RSA_C */
677 
678 #ifdef POLARSSL_ECDSA_C
679 void test_suite_pk_ec_test_vec( int type, int id, char *key_str,
680  char *hash_str, char * sig_str, int ret )
681 {
682  pk_context pk;
683  ecp_keypair *eckey;
684  unsigned char hash[100], sig[500], key[500];
685  size_t hash_len, sig_len, key_len;
686 
687  pk_init( &pk );
688 
689  memset( hash, 0, sizeof( hash ) ); hash_len = unhexify(hash, hash_str);
690  memset( sig, 0, sizeof( sig ) ); sig_len = unhexify(sig, sig_str);
691  memset( key, 0, sizeof( key ) ); key_len = unhexify(key, key_str);
692 
693  TEST_ASSERT( pk_init_ctx( &pk, pk_info_from_type( type ) ) == 0 );
694 
696  eckey = pk_ec( pk );
697 
698  TEST_ASSERT( ecp_use_known_dp( &eckey->grp, id ) == 0 );
699  TEST_ASSERT( ecp_point_read_binary( &eckey->grp, &eckey->Q,
700  key, key_len ) == 0 );
701 
703  hash, hash_len, sig, sig_len ) == ret );
704 
705 exit:
706  pk_free( &pk );
707 }
708 #endif /* POLARSSL_ECDSA_C */
709 
710 void test_suite_pk_sign_verify( int type, int sign_ret, int verify_ret )
711 {
712  pk_context pk;
713  unsigned char hash[50], sig[5000];
714  size_t sig_len;
715 
716  pk_init( &pk );
717 
718  memset( hash, 0x2a, sizeof hash );
719  memset( sig, 0, sizeof sig );
720 
721  TEST_ASSERT( pk_init_ctx( &pk, pk_info_from_type( type ) ) == 0 );
722  TEST_ASSERT( pk_genkey( &pk ) == 0 );
723 
724  TEST_ASSERT( pk_sign( &pk, POLARSSL_MD_NONE, hash, sizeof hash,
725  sig, &sig_len, rnd_std_rand, NULL ) == sign_ret );
726 
728  hash, sizeof hash, sig, sig_len ) == verify_ret );
729 
730 exit:
731  pk_free( &pk );
732 }
733 
734 #ifdef POLARSSL_RSA_C
735 void test_suite_pk_rsa_encrypt_test_vec( char *message_hex, int mod,
736  int radix_N, char *input_N,
737  int radix_E, char *input_E,
738  char *result_hex, int ret )
739 {
740  unsigned char message[1000];
741  unsigned char output[1000];
742  unsigned char result[1000];
743  size_t msg_len, olen, res_len;
744  rnd_pseudo_info rnd_info;
745  rsa_context *rsa;
746  pk_context pk;
747 
748  memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
749  memset( message, 0, sizeof( message ) );
750  memset( output, 0, sizeof( output ) );
751  memset( result, 0, sizeof( result ) );
752 
753  msg_len = unhexify( message, message_hex );
754  res_len = unhexify( result, result_hex );
755 
756  pk_init( &pk );
758  rsa = pk_rsa( pk );
759 
760  rsa->len = mod / 8;
761  TEST_ASSERT( mpi_read_string( &rsa->N, radix_N, input_N ) == 0 );
762  TEST_ASSERT( mpi_read_string( &rsa->E, radix_E, input_E ) == 0 );
763 
764  TEST_ASSERT( pk_encrypt( &pk, message, msg_len,
765  output, &olen, sizeof( output ),
766  rnd_pseudo_rand, &rnd_info ) == ret );
767  TEST_ASSERT( olen == res_len );
768  TEST_ASSERT( memcmp( output, result, olen ) == 0 );
769 
770 exit:
771  pk_free( &pk );
772 }
773 #endif /* POLARSSL_RSA_C */
774 
775 #ifdef POLARSSL_RSA_C
776 void test_suite_pk_rsa_decrypt_test_vec( char *cipher_hex, int mod,
777  int radix_P, char *input_P,
778  int radix_Q, char *input_Q,
779  int radix_N, char *input_N,
780  int radix_E, char *input_E,
781  char *clear_hex, int ret )
782 {
783  unsigned char clear[1000];
784  unsigned char output[1000];
785  unsigned char cipher[1000];
786  size_t clear_len, olen, cipher_len;
787  rnd_pseudo_info rnd_info;
788  mpi P1, Q1, H, G;
789  rsa_context *rsa;
790  pk_context pk;
791 
792  pk_init( &pk );
793  mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
794 
795  memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
796  memset( clear, 0, sizeof( clear ) );
797  memset( cipher, 0, sizeof( cipher ) );
798 
799  clear_len = unhexify( clear, clear_hex );
800  cipher_len = unhexify( cipher, cipher_hex );
801 
802  /* init pk-rsa context */
804  rsa = pk_rsa( pk );
805 
806  /* load public key */
807  rsa->len = mod / 8;
808  TEST_ASSERT( mpi_read_string( &rsa->N, radix_N, input_N ) == 0 );
809  TEST_ASSERT( mpi_read_string( &rsa->E, radix_E, input_E ) == 0 );
810 
811  /* load private key */
812  TEST_ASSERT( mpi_read_string( &rsa->P, radix_P, input_P ) == 0 );
813  TEST_ASSERT( mpi_read_string( &rsa->Q, radix_Q, input_Q ) == 0 );
814  TEST_ASSERT( mpi_sub_int( &P1, &rsa->P, 1 ) == 0 );
815  TEST_ASSERT( mpi_sub_int( &Q1, &rsa->Q, 1 ) == 0 );
816  TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
817  TEST_ASSERT( mpi_gcd( &G, &rsa->E, &H ) == 0 );
818  TEST_ASSERT( mpi_inv_mod( &rsa->D , &rsa->E, &H ) == 0 );
819  TEST_ASSERT( mpi_mod_mpi( &rsa->DP, &rsa->D, &P1 ) == 0 );
820  TEST_ASSERT( mpi_mod_mpi( &rsa->DQ, &rsa->D, &Q1 ) == 0 );
821  TEST_ASSERT( mpi_inv_mod( &rsa->QP, &rsa->Q, &rsa->P ) == 0 );
822 
823  /* decryption test */
824  memset( output, 0, sizeof( output ) );
825  olen = 0;
826  TEST_ASSERT( pk_decrypt( &pk, cipher, cipher_len,
827  output, &olen, sizeof( output ),
828  rnd_pseudo_rand, &rnd_info ) == ret );
829  if( ret == 0 )
830  {
831  TEST_ASSERT( olen == clear_len );
832  TEST_ASSERT( memcmp( output, clear, olen ) == 0 );
833  }
834 
835 exit:
836  mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
837  pk_free( &pk );
838 }
839 #endif /* POLARSSL_RSA_C */
840 
841 void test_suite_pk_ec_nocrypt( int type )
842 {
843  pk_context pk;
844  unsigned char output[100];
845  unsigned char input[100];
846  rnd_pseudo_info rnd_info;
847  size_t olen = 0;
849 
850  pk_init( &pk );
851 
852  memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
853  memset( output, 0, sizeof( output ) );
854  memset( input, 0, sizeof( input ) );
855 
856  TEST_ASSERT( pk_init_ctx( &pk, pk_info_from_type( type ) ) == 0 );
857 
858  TEST_ASSERT( pk_encrypt( &pk, input, sizeof( input ),
859  output, &olen, sizeof( output ),
860  rnd_pseudo_rand, &rnd_info ) == ret );
861 
862  TEST_ASSERT( pk_decrypt( &pk, input, sizeof( input ),
863  output, &olen, sizeof( output ),
864  rnd_pseudo_rand, &rnd_info ) == ret );
865 
866 exit:
867  pk_free( &pk );
868 }
869 
870 #ifdef POLARSSL_RSA_C
871 void test_suite_pk_rsa_alt( )
872 {
873  /*
874  * An rsa_alt context can only do private operations (decrypt, sign).
875  * Test it against the public operations (encrypt, verify) of a
876  * corresponding rsa context.
877  */
878  rsa_context raw;
879  pk_context rsa, alt;
880  pk_debug_item dbg_items[10];
881  unsigned char hash[50], sig[1000];
882  unsigned char msg[50], ciph[1000], test[1000];
883  size_t sig_len, ciph_len, test_len;
885 
887  pk_init( &rsa ); pk_init( &alt );
888 
889  memset( hash, 0x2a, sizeof hash );
890  memset( sig, 0, sizeof sig );
891  memset( msg, 0x2a, sizeof msg );
892  memset( ciph, 0, sizeof ciph );
893  memset( test, 0, sizeof test );
894 
895  /* Initiliaze PK RSA context with random key */
896  TEST_ASSERT( pk_init_ctx( &rsa,
898  TEST_ASSERT( pk_genkey( &rsa ) == 0 );
899 
900  /* Extract key to the raw rsa context */
901  TEST_ASSERT( rsa_copy( &raw, pk_rsa( rsa ) ) == 0 );
902 
903  /* Initialize PK RSA_ALT context */
904  TEST_ASSERT( pk_init_ctx_rsa_alt( &alt, (void *) &raw,
906 
907  /* Test administrative functions */
909  TEST_ASSERT( pk_get_size( &alt ) == RSA_KEY_SIZE );
910  TEST_ASSERT( pk_get_len( &alt ) == RSA_KEY_LEN );
912  TEST_ASSERT( strcmp( pk_get_name( &alt ), "RSA-alt" ) == 0 );
913 
914  /* Test signature */
915  TEST_ASSERT( pk_sign( &alt, POLARSSL_MD_NONE, hash, sizeof hash,
916  sig, &sig_len, rnd_std_rand, NULL ) == 0 );
917  TEST_ASSERT( sig_len == RSA_KEY_LEN );
919  hash, sizeof hash, sig, sig_len ) == 0 );
920 
921  /* Test decrypt */
922  TEST_ASSERT( pk_encrypt( &rsa, msg, sizeof msg,
923  ciph, &ciph_len, sizeof ciph,
924  rnd_std_rand, NULL ) == 0 );
925  TEST_ASSERT( pk_decrypt( &alt, ciph, ciph_len,
926  test, &test_len, sizeof test,
927  rnd_std_rand, NULL ) == 0 );
928  TEST_ASSERT( test_len == sizeof msg );
929  TEST_ASSERT( memcmp( test, msg, test_len ) == 0 );
930 
931  /* Test forbidden operations */
932  TEST_ASSERT( pk_encrypt( &alt, msg, sizeof msg,
933  ciph, &ciph_len, sizeof ciph,
934  rnd_std_rand, NULL ) == ret );
936  hash, sizeof hash, sig, sig_len ) == ret );
937  TEST_ASSERT( pk_debug( &alt, dbg_items ) == ret );
938 
939 exit:
940  rsa_free( &raw );
941  pk_free( &rsa ); pk_free( &alt );
942 }
943 #endif /* POLARSSL_RSA_C */
944 
945 
946 #endif /* POLARSSL_PK_C */
947 
948 
949 int dep_check( char *str )
950 {
951  if( str == NULL )
952  return( 1 );
953 
954  if( strcmp( str, "POLARSSL_PKCS1_V15" ) == 0 )
955  {
956 #if defined(POLARSSL_PKCS1_V15)
957  return( 0 );
958 #else
959  return( 1 );
960 #endif
961  }
962  if( strcmp( str, "POLARSSL_GENPRIME" ) == 0 )
963  {
964 #if defined(POLARSSL_GENPRIME)
965  return( 0 );
966 #else
967  return( 1 );
968 #endif
969  }
970  if( strcmp( str, "POLARSSL_ECP_C" ) == 0 )
971  {
972 #if defined(POLARSSL_ECP_C)
973  return( 0 );
974 #else
975  return( 1 );
976 #endif
977  }
978  if( strcmp( str, "POLARSSL_RSA_C" ) == 0 )
979  {
980 #if defined(POLARSSL_RSA_C)
981  return( 0 );
982 #else
983  return( 1 );
984 #endif
985  }
986  if( strcmp( str, "POLARSSL_PKCS1_V21" ) == 0 )
987  {
988 #if defined(POLARSSL_PKCS1_V21)
989  return( 0 );
990 #else
991  return( 1 );
992 #endif
993  }
994  if( strcmp( str, "POLARSSL_SHA256_C" ) == 0 )
995  {
996 #if defined(POLARSSL_SHA256_C)
997  return( 0 );
998 #else
999  return( 1 );
1000 #endif
1001  }
1002  if( strcmp( str, "POLARSSL_SHA1_C" ) == 0 )
1003  {
1004 #if defined(POLARSSL_SHA1_C)
1005  return( 0 );
1006 #else
1007  return( 1 );
1008 #endif
1009  }
1010  if( strcmp( str, "POLARSSL_ECDSA_C" ) == 0 )
1011  {
1012 #if defined(POLARSSL_ECDSA_C)
1013  return( 0 );
1014 #else
1015  return( 1 );
1016 #endif
1017  }
1018  if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
1019  {
1020 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
1021  return( 0 );
1022 #else
1023  return( 1 );
1024 #endif
1025  }
1026 
1027 
1028  return( 1 );
1029 }
1030 
1031 int dispatch_test(int cnt, char *params[50])
1032 {
1033  int ret;
1034  ((void) cnt);
1035  ((void) params);
1036 
1037 #if defined(TEST_SUITE_ACTIVE)
1038  if( strcmp( params[0], "pk_utils" ) == 0 )
1039  {
1040 
1041  int param1;
1042  int param2;
1043  int param3;
1044  char *param4 = params[4];
1045 
1046  if( cnt != 5 )
1047  {
1048  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1049  return( 2 );
1050  }
1051 
1052  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1053  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1054  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1055  if( verify_string( &param4 ) != 0 ) return( 2 );
1056 
1057  test_suite_pk_utils( param1, param2, param3, param4 );
1058  return ( 0 );
1059 
1060  return ( 3 );
1061  }
1062  else
1063  if( strcmp( params[0], "pk_rsa_verify_test_vec" ) == 0 )
1064  {
1065  #ifdef POLARSSL_RSA_C
1066 
1067  char *param1 = params[1];
1068  int param2;
1069  int param3;
1070  int param4;
1071  char *param5 = params[5];
1072  int param6;
1073  char *param7 = params[7];
1074  char *param8 = params[8];
1075  int param9;
1076 
1077  if( cnt != 10 )
1078  {
1079  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 10 );
1080  return( 2 );
1081  }
1082 
1083  if( verify_string( &param1 ) != 0 ) return( 2 );
1084  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1085  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1086  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1087  if( verify_string( &param5 ) != 0 ) return( 2 );
1088  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
1089  if( verify_string( &param7 ) != 0 ) return( 2 );
1090  if( verify_string( &param8 ) != 0 ) return( 2 );
1091  if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
1092 
1093  test_suite_pk_rsa_verify_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
1094  return ( 0 );
1095  #endif /* POLARSSL_RSA_C */
1096 
1097  return ( 3 );
1098  }
1099  else
1100  if( strcmp( params[0], "pk_rsa_verify_ext_test_vec" ) == 0 )
1101  {
1102  #ifdef POLARSSL_RSA_C
1103 
1104  char *param1 = params[1];
1105  int param2;
1106  int param3;
1107  int param4;
1108  char *param5 = params[5];
1109  int param6;
1110  char *param7 = params[7];
1111  char *param8 = params[8];
1112  int param9;
1113  int param10;
1114  int param11;
1115  int param12;
1116 
1117  if( cnt != 13 )
1118  {
1119  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 13 );
1120  return( 2 );
1121  }
1122 
1123  if( verify_string( &param1 ) != 0 ) return( 2 );
1124  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1125  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1126  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1127  if( verify_string( &param5 ) != 0 ) return( 2 );
1128  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
1129  if( verify_string( &param7 ) != 0 ) return( 2 );
1130  if( verify_string( &param8 ) != 0 ) return( 2 );
1131  if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
1132  if( verify_int( params[10], &param10 ) != 0 ) return( 2 );
1133  if( verify_int( params[11], &param11 ) != 0 ) return( 2 );
1134  if( verify_int( params[12], &param12 ) != 0 ) return( 2 );
1135 
1136  test_suite_pk_rsa_verify_ext_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12 );
1137  return ( 0 );
1138  #endif /* POLARSSL_RSA_C */
1139 
1140  return ( 3 );
1141  }
1142  else
1143  if( strcmp( params[0], "pk_ec_test_vec" ) == 0 )
1144  {
1145  #ifdef POLARSSL_ECDSA_C
1146 
1147  int param1;
1148  int param2;
1149  char *param3 = params[3];
1150  char *param4 = params[4];
1151  char *param5 = params[5];
1152  int param6;
1153 
1154  if( cnt != 7 )
1155  {
1156  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1157  return( 2 );
1158  }
1159 
1160  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1161  if( verify_int( params[2], &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_int( params[6], &param6 ) != 0 ) return( 2 );
1166 
1167  test_suite_pk_ec_test_vec( param1, param2, param3, param4, param5, param6 );
1168  return ( 0 );
1169  #endif /* POLARSSL_ECDSA_C */
1170 
1171  return ( 3 );
1172  }
1173  else
1174  if( strcmp( params[0], "pk_sign_verify" ) == 0 )
1175  {
1176 
1177  int param1;
1178  int param2;
1179  int param3;
1180 
1181  if( cnt != 4 )
1182  {
1183  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1184  return( 2 );
1185  }
1186 
1187  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1188  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1189  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1190 
1191  test_suite_pk_sign_verify( param1, param2, param3 );
1192  return ( 0 );
1193 
1194  return ( 3 );
1195  }
1196  else
1197  if( strcmp( params[0], "pk_rsa_encrypt_test_vec" ) == 0 )
1198  {
1199  #ifdef POLARSSL_RSA_C
1200 
1201  char *param1 = params[1];
1202  int param2;
1203  int param3;
1204  char *param4 = params[4];
1205  int param5;
1206  char *param6 = params[6];
1207  char *param7 = params[7];
1208  int param8;
1209 
1210  if( cnt != 9 )
1211  {
1212  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 9 );
1213  return( 2 );
1214  }
1215 
1216  if( verify_string( &param1 ) != 0 ) return( 2 );
1217  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1218  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1219  if( verify_string( &param4 ) != 0 ) return( 2 );
1220  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1221  if( verify_string( &param6 ) != 0 ) return( 2 );
1222  if( verify_string( &param7 ) != 0 ) return( 2 );
1223  if( verify_int( params[8], &param8 ) != 0 ) return( 2 );
1224 
1225  test_suite_pk_rsa_encrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8 );
1226  return ( 0 );
1227  #endif /* POLARSSL_RSA_C */
1228 
1229  return ( 3 );
1230  }
1231  else
1232  if( strcmp( params[0], "pk_rsa_decrypt_test_vec" ) == 0 )
1233  {
1234  #ifdef POLARSSL_RSA_C
1235 
1236  char *param1 = params[1];
1237  int param2;
1238  int param3;
1239  char *param4 = params[4];
1240  int param5;
1241  char *param6 = params[6];
1242  int param7;
1243  char *param8 = params[8];
1244  int param9;
1245  char *param10 = params[10];
1246  char *param11 = params[11];
1247  int param12;
1248 
1249  if( cnt != 13 )
1250  {
1251  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 13 );
1252  return( 2 );
1253  }
1254 
1255  if( verify_string( &param1 ) != 0 ) return( 2 );
1256  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1257  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1258  if( verify_string( &param4 ) != 0 ) return( 2 );
1259  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1260  if( verify_string( &param6 ) != 0 ) return( 2 );
1261  if( verify_int( params[7], &param7 ) != 0 ) return( 2 );
1262  if( verify_string( &param8 ) != 0 ) return( 2 );
1263  if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
1264  if( verify_string( &param10 ) != 0 ) return( 2 );
1265  if( verify_string( &param11 ) != 0 ) return( 2 );
1266  if( verify_int( params[12], &param12 ) != 0 ) return( 2 );
1267 
1268  test_suite_pk_rsa_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12 );
1269  return ( 0 );
1270  #endif /* POLARSSL_RSA_C */
1271 
1272  return ( 3 );
1273  }
1274  else
1275  if( strcmp( params[0], "pk_ec_nocrypt" ) == 0 )
1276  {
1277 
1278  int param1;
1279 
1280  if( cnt != 2 )
1281  {
1282  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
1283  return( 2 );
1284  }
1285 
1286  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1287 
1288  test_suite_pk_ec_nocrypt( param1 );
1289  return ( 0 );
1290 
1291  return ( 3 );
1292  }
1293  else
1294  if( strcmp( params[0], "pk_rsa_alt" ) == 0 )
1295  {
1296  #ifdef POLARSSL_RSA_C
1297 
1298 
1299  if( cnt != 1 )
1300  {
1301  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1302  return( 2 );
1303  }
1304 
1305 
1306  test_suite_pk_rsa_alt( );
1307  return ( 0 );
1308  #endif /* POLARSSL_RSA_C */
1309 
1310  return ( 3 );
1311  }
1312  else
1313 
1314  {
1315  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
1316  fflush( stdout );
1317  return( 1 );
1318  }
1319 #else
1320  return( 3 );
1321 #endif
1322  return( ret );
1323 }
1324 
1325 int get_line( FILE *f, char *buf, size_t len )
1326 {
1327  char *ret;
1328 
1329  ret = fgets( buf, len, f );
1330  if( ret == NULL )
1331  return( -1 );
1332 
1333  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
1334  buf[strlen(buf) - 1] = '\0';
1335  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
1336  buf[strlen(buf) - 1] = '\0';
1337 
1338  return( 0 );
1339 }
1340 
1341 int parse_arguments( char *buf, size_t len, char *params[50] )
1342 {
1343  int cnt = 0, i;
1344  char *cur = buf;
1345  char *p = buf, *q;
1346 
1347  params[cnt++] = cur;
1348 
1349  while( *p != '\0' && p < buf + len )
1350  {
1351  if( *p == '\\' )
1352  {
1353  p++;
1354  p++;
1355  continue;
1356  }
1357  if( *p == ':' )
1358  {
1359  if( p + 1 < buf + len )
1360  {
1361  cur = p + 1;
1362  params[cnt++] = cur;
1363  }
1364  *p = '\0';
1365  }
1366 
1367  p++;
1368  }
1369 
1370  // Replace newlines, question marks and colons in strings
1371  for( i = 0; i < cnt; i++ )
1372  {
1373  p = params[i];
1374  q = params[i];
1375 
1376  while( *p != '\0' )
1377  {
1378  if( *p == '\\' && *(p + 1) == 'n' )
1379  {
1380  p += 2;
1381  *(q++) = '\n';
1382  }
1383  else if( *p == '\\' && *(p + 1) == ':' )
1384  {
1385  p += 2;
1386  *(q++) = ':';
1387  }
1388  else if( *p == '\\' && *(p + 1) == '?' )
1389  {
1390  p += 2;
1391  *(q++) = '?';
1392  }
1393  else
1394  *(q++) = *(p++);
1395  }
1396  *q = '\0';
1397  }
1398 
1399  return( cnt );
1400 }
1401 
1402 int main()
1403 {
1404  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1405  const char *filename = "/builddir/build/BUILD/polarssl-1.3.9/tests/suites/test_suite_pk.data";
1406  FILE *file;
1407  char buf[5000];
1408  char *params[50];
1409 
1410 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1411  unsigned char alloc_buf[1000000];
1412  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1413 #endif
1414 
1415  file = fopen( filename, "r" );
1416  if( file == NULL )
1417  {
1418  fprintf( stderr, "Failed to open\n" );
1419  return( 1 );
1420  }
1421 
1422  while( !feof( file ) )
1423  {
1424  int skip = 0;
1425 
1426  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1427  break;
1428  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1429  fprintf( stdout, " " );
1430  for( i = strlen( buf ) + 1; i < 67; i++ )
1431  fprintf( stdout, "." );
1432  fprintf( stdout, " " );
1433  fflush( stdout );
1434 
1435  total_tests++;
1436 
1437  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1438  break;
1439  cnt = parse_arguments( buf, strlen(buf), params );
1440 
1441  if( strcmp( params[0], "depends_on" ) == 0 )
1442  {
1443  for( i = 1; i < cnt; i++ )
1444  if( dep_check( params[i] ) != 0 )
1445  skip = 1;
1446 
1447  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1448  break;
1449  cnt = parse_arguments( buf, strlen(buf), params );
1450  }
1451 
1452  if( skip == 0 )
1453  {
1454  test_errors = 0;
1455  ret = dispatch_test( cnt, params );
1456  }
1457 
1458  if( skip == 1 || ret == 3 )
1459  {
1460  total_skipped++;
1461  fprintf( stdout, "----\n" );
1462  fflush( stdout );
1463  }
1464  else if( ret == 0 && test_errors == 0 )
1465  {
1466  fprintf( stdout, "PASS\n" );
1467  fflush( stdout );
1468  }
1469  else if( ret == 2 )
1470  {
1471  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1472  fclose(file);
1473  exit( 2 );
1474  }
1475  else
1476  total_errors++;
1477 
1478  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1479  break;
1480  if( strlen(buf) != 0 )
1481  {
1482  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1483  return( 1 );
1484  }
1485  }
1486  fclose(file);
1487 
1488  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1489  if( total_errors == 0 )
1490  fprintf( stdout, "PASSED" );
1491  else
1492  fprintf( stdout, "FAILED" );
1493 
1494  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1495  total_tests - total_errors, total_tests, total_skipped );
1496 
1497 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1498 #if defined(POLARSSL_MEMORY_DEBUG)
1499  memory_buffer_alloc_status();
1500 #endif
1502 #endif
1503 
1504  return( total_errors != 0 );
1505 }
1506 
1507 
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:281
#define PUT_UINT32_BE(n, b, i)
const pk_info_t * pk_info_from_type(pk_type_t pk_type)
Return information associated with the given PK type.
Memory allocation layer (Deprecated to platform layer)
int rsa_copy(rsa_context *dst, const rsa_context *src)
Copy the components of an RSA context.
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
#define polarssl_malloc
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)
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
size_t pk_get_size(const pk_context *ctx)
Get the size in bits of the underlying key.
Options for RSASSA-PSS signature verification.
Definition: pk.h:109
ecp_group grp
Definition: ecp.h:165
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 (including padding if relevant).
mpi DQ
Definition: rsa.h:95
int pk_debug(const pk_context *ctx, pk_debug_item *items)
Export debug information.
Configuration options (set of defines)
int dep_check(char *str)
ECP key pair structure.
Definition: ecp.h:163
int rsa_pkcs1_decrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
Generic wrapper to perform a PKCS#1 decryption using the mode from the context.
int parse_arguments(char *buf, size_t len, char *params[50])
MPI structure.
Definition: bignum.h:182
PolarSSL Platform abstraction layer.
#define pk_ec(pk)
Quick access to an EC context inside a PK context.
Definition: pk.h:84
int main()
pk_type_t pk_get_type(const pk_context *ctx)
Get the key type.
static int test_assert(int correct, const 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.
int expected_salt_len
Definition: pk.h:112
void mpi_init(mpi *X)
Initialize one MPI.
Public Key abstraction layer.
int pk_init_ctx_rsa_alt(pk_context *ctx, void *key, pk_rsa_alt_decrypt_func decrypt_func, pk_rsa_alt_sign_func sign_func, pk_rsa_alt_key_len_func key_len_func)
Initialize an RSA-alt context.
#define POLARSSL_ERR_PK_BAD_INPUT_DATA
Bad input parameters to function.
Definition: pk.h:53
size_t len
Definition: rsa.h:86
int pk_verify_ext(pk_type_t type, const void *options, 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, with options.
md_type_t
Definition: md.h:51
mpi P
Definition: rsa.h:92
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
#define TEST_ASSERT(TEST)
mpi Q
Definition: rsa.h:93
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
void rsa_free(rsa_context *ctx)
Free the components of an RSA key.
md_type_t mgf1_hash_id
Definition: pk.h:111
#define POLARSSL_ERR_ECP_VERIFY_FAILED
The signature is not valid.
Definition: ecp.h:38
size_t(* rsa_key_len_func)(void *ctx)
Definition: ssl.h:470
RSA context structure.
Definition: rsa.h:83
mpi D
Definition: rsa.h:91
#define POLARSSL_ERR_RSA_INVALID_PADDING
Input data contains invalid padding and is rejected.
Definition: rsa.h:47
mpi QP
Definition: rsa.h:96
#define RSA_PKCS_V15
Definition: rsa.h:62
Item to send to the debug module.
Definition: pk.h:129
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
mpi N
Definition: rsa.h:88
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 (including padding if relevant).
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int dispatch_test(int cnt, char *params[50])
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(* rsa_sign_func)(void *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Definition: ssl.h:466
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:89
mpi DP
Definition: rsa.h:94
#define POLARSSL_ERR_RSA_VERIFY_FAILED
The PKCS#1 verification failed.
Definition: rsa.h:52
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 rsa_pkcs1_sign(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Generic wrapper to perform a PKCS#1 signature using the mode from the context.
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.
#define RSA_SALT_LEN_ANY
Definition: rsa.h:68
int get_line(FILE *f, char *buf, size_t len)
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
int verify_string(char **str)
#define pk_rsa(pk)
Quick access to an RSA context inside a PK context.
Definition: pk.h:74
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:52
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, including padding if relevant.
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.
void rsa_init(rsa_context *ctx, int padding, int hash_id)
Initialize an RSA context.
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 (including padding if relevant).
void pk_init(pk_context *ctx)
Initialize a pk_context (as NONE)
unsigned char * buf
ecp_point Q
Definition: ecp.h:167
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:194
int(* rsa_decrypt_func)(void *ctx, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
Definition: ssl.h:463