PolarSSL v1.3.1
test_suite_pkcs1_v21.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_PKCS1_V21
4 #ifdef POLARSSL_RSA_C
5 #ifdef POLARSSL_BIGNUM_C
6 #ifdef POLARSSL_SHA1_C
7 #ifdef POLARSSL_GENPRIME
8 
9 #include <polarssl/rsa.h>
10 #include <polarssl/md.h>
11 #include <polarssl/md2.h>
12 #include <polarssl/md4.h>
13 #include <polarssl/md5.h>
14 #include <polarssl/sha1.h>
15 #include <polarssl/sha256.h>
16 #include <polarssl/sha512.h>
17 #endif /* POLARSSL_PKCS1_V21 */
18 #endif /* POLARSSL_RSA_C */
19 #endif /* POLARSSL_BIGNUM_C */
20 #endif /* POLARSSL_SHA1_C */
21 #endif /* POLARSSL_GENPRIME */
22 
23 
24 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
25 #include "polarssl/memory.h"
26 #endif
27 
28 #ifdef _MSC_VER
29 #include <basetsd.h>
30 typedef UINT32 uint32_t;
31 #else
32 #include <inttypes.h>
33 #endif
34 
35 #include <assert.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 /*
40  * 32-bit integer manipulation macros (big endian)
41  */
42 #ifndef GET_UINT32_BE
43 #define GET_UINT32_BE(n,b,i) \
44 { \
45  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
46  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
47  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
48  | ( (uint32_t) (b)[(i) + 3] ); \
49 }
50 #endif
51 
52 #ifndef PUT_UINT32_BE
53 #define PUT_UINT32_BE(n,b,i) \
54 { \
55  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
56  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
57  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
58  (b)[(i) + 3] = (unsigned char) ( (n) ); \
59 }
60 #endif
61 
62 static int unhexify(unsigned char *obuf, const char *ibuf)
63 {
64  unsigned char c, c2;
65  int len = strlen(ibuf) / 2;
66  assert(!(strlen(ibuf) %1)); // must be even number of bytes
67 
68  while (*ibuf != 0)
69  {
70  c = *ibuf++;
71  if( c >= '0' && c <= '9' )
72  c -= '0';
73  else if( c >= 'a' && c <= 'f' )
74  c -= 'a' - 10;
75  else if( c >= 'A' && c <= 'F' )
76  c -= 'A' - 10;
77  else
78  assert( 0 );
79 
80  c2 = *ibuf++;
81  if( c2 >= '0' && c2 <= '9' )
82  c2 -= '0';
83  else if( c2 >= 'a' && c2 <= 'f' )
84  c2 -= 'a' - 10;
85  else if( c2 >= 'A' && c2 <= 'F' )
86  c2 -= 'A' - 10;
87  else
88  assert( 0 );
89 
90  *obuf++ = ( c << 4 ) | c2;
91  }
92 
93  return len;
94 }
95 
96 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
97 {
98  unsigned char l, h;
99 
100  while (len != 0)
101  {
102  h = (*ibuf) / 16;
103  l = (*ibuf) % 16;
104 
105  if( h < 10 )
106  *obuf++ = '0' + h;
107  else
108  *obuf++ = 'a' + h - 10;
109 
110  if( l < 10 )
111  *obuf++ = '0' + l;
112  else
113  *obuf++ = 'a' + l - 10;
114 
115  ++ibuf;
116  len--;
117  }
118 }
119 
129 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
130 {
131  size_t i;
132 
133  if( rng_state != NULL )
134  rng_state = NULL;
135 
136  for( i = 0; i < len; ++i )
137  output[i] = rand();
138 
139  return( 0 );
140 }
141 
147 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
148 {
149  if( rng_state != NULL )
150  rng_state = NULL;
151 
152  memset( output, 0, len );
153 
154  return( 0 );
155 }
156 
157 typedef struct
158 {
159  unsigned char *buf;
160  size_t length;
161 } rnd_buf_info;
162 
174 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
175 {
176  rnd_buf_info *info = (rnd_buf_info *) rng_state;
177  size_t use_len;
178 
179  if( rng_state == NULL )
180  return( rnd_std_rand( NULL, output, len ) );
181 
182  use_len = len;
183  if( len > info->length )
184  use_len = info->length;
185 
186  if( use_len )
187  {
188  memcpy( output, info->buf, use_len );
189  info->buf += use_len;
190  info->length -= use_len;
191  }
192 
193  if( len - use_len > 0 )
194  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
195 
196  return( 0 );
197 }
198 
206 typedef struct
207 {
208  uint32_t key[16];
209  uint32_t v0, v1;
211 
220 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
221 {
222  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
223  uint32_t i, *k, sum, delta=0x9E3779B9;
224  unsigned char result[4];
225 
226  if( rng_state == NULL )
227  return( rnd_std_rand( NULL, output, len ) );
228 
229  k = info->key;
230 
231  while( len > 0 )
232  {
233  size_t use_len = ( len > 4 ) ? 4 : len;
234  sum = 0;
235 
236  for( i = 0; i < 32; i++ )
237  {
238  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
239  sum += delta;
240  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
241  }
242 
243  PUT_UINT32_BE( info->v0, result, 0 );
244  memcpy( output, result, use_len );
245  len -= use_len;
246  }
247 
248  return( 0 );
249 }
250 
260 static int not_rnd( void *in, unsigned char *out, size_t len )
261 {
262  unsigned char *obuf;
263  const char *ibuf = in;
264  unsigned char c, c2;
265  assert( len == strlen(ibuf) / 2 );
266  assert(!(strlen(ibuf) %1)); // must be even number of bytes
267 
268  obuf = out + (len - 1); // sic
269  while (*ibuf != 0)
270  {
271  c = *ibuf++;
272  if( c >= '0' && c <= '9' )
273  c -= '0';
274  else if( c >= 'a' && c <= 'f' )
275  c -= 'a' - 10;
276  else if( c >= 'A' && c <= 'F' )
277  c -= 'A' - 10;
278  else
279  assert( 0 );
280 
281  c2 = *ibuf++;
282  if( c2 >= '0' && c2 <= '9' )
283  c2 -= '0';
284  else if( c2 >= 'a' && c2 <= 'f' )
285  c2 -= 'a' - 10;
286  else if( c2 >= 'A' && c2 <= 'F' )
287  c2 -= 'A' - 10;
288  else
289  assert( 0 );
290 
291  *obuf-- = ( c << 4 ) | c2; // sic
292  }
293 
294  return( 0 );
295 }
296 
297 
298 #include <stdio.h>
299 #include <string.h>
300 
301 static int test_errors = 0;
302 
303 #ifdef POLARSSL_PKCS1_V21
304 #ifdef POLARSSL_RSA_C
305 #ifdef POLARSSL_BIGNUM_C
306 #ifdef POLARSSL_SHA1_C
307 #ifdef POLARSSL_GENPRIME
308 
309 #define TEST_SUITE_ACTIVE
310 
311 static int test_assert( int correct, char *test )
312 {
313  if( correct )
314  return( 0 );
315 
316  test_errors++;
317  if( test_errors == 1 )
318  printf( "FAILED\n" );
319  printf( " %s\n", test );
320 
321  return( 1 );
322 }
323 
324 #define TEST_ASSERT( TEST ) \
325  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
326  if( test_errors) return; \
327  } while (0)
328 
329 int verify_string( char **str )
330 {
331  if( (*str)[0] != '"' ||
332  (*str)[strlen( *str ) - 1] != '"' )
333  {
334  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
335  return( -1 );
336  }
337 
338  (*str)++;
339  (*str)[strlen( *str ) - 1] = '\0';
340 
341  return( 0 );
342 }
343 
344 int verify_int( char *str, int *value )
345 {
346  size_t i;
347  int minus = 0;
348  int digits = 1;
349  int hex = 0;
350 
351  for( i = 0; i < strlen( str ); i++ )
352  {
353  if( i == 0 && str[i] == '-' )
354  {
355  minus = 1;
356  continue;
357  }
358 
359  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
360  str[i - 1] == '0' && str[i] == 'x' )
361  {
362  hex = 1;
363  continue;
364  }
365 
366  if( str[i] < '0' || str[i] > '9' )
367  {
368  digits = 0;
369  break;
370  }
371  }
372 
373  if( digits )
374  {
375  if( hex )
376  *value = strtol( str, NULL, 16 );
377  else
378  *value = strtol( str, NULL, 10 );
379 
380  return( 0 );
381  }
382 
383  if( strcmp( str, "POLARSSL_ERR_RSA_BAD_INPUT_DATA" ) == 0 )
384  {
385  *value = ( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
386  return( 0 );
387  }
388  if( strcmp( str, "POLARSSL_MD_SHA512" ) == 0 )
389  {
390  *value = ( POLARSSL_MD_SHA512 );
391  return( 0 );
392  }
393  if( strcmp( str, "POLARSSL_MD_SHA1" ) == 0 )
394  {
395  *value = ( POLARSSL_MD_SHA1 );
396  return( 0 );
397  }
398 
399 
400  printf( "Expected integer for parameter and got: %s\n", str );
401  return( -1 );
402 }
403 
404 void test_suite_pkcs1_rsaes_oaep_encrypt( int mod, int radix_N, char *input_N, int radix_E,
405  char *input_E, int hash,
406  char *message_hex_string, char *seed,
407  char *result_hex_str, int result )
408 {
409  unsigned char message_str[1000];
410  unsigned char output[1000];
411  unsigned char output_str[1000];
412  unsigned char rnd_buf[1000];
413  rsa_context ctx;
414  size_t msg_len;
415  rnd_buf_info info;
416 
417  info.length = unhexify( rnd_buf, seed );
418  info.buf = rnd_buf;
419 
420  rsa_init( &ctx, RSA_PKCS_V21, hash );
421  memset( message_str, 0x00, 1000 );
422  memset( output, 0x00, 1000 );
423  memset( output_str, 0x00, 1000 );
424 
425  ctx.len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 );
426  TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
427  TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
428 
429  TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
430 
431  msg_len = unhexify( message_str, message_hex_string );
432 
433  TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, &rnd_buffer_rand, &info, RSA_PUBLIC, msg_len, message_str, output ) == result );
434  if( result == 0 )
435  {
436  hexify( output_str, output, ctx.len );
437 
438  TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
439  }
440 
441  rsa_free( &ctx );
442 }
443 
444 void test_suite_pkcs1_rsaes_oaep_decrypt( int mod, int radix_P, char *input_P,
445  int radix_Q, char *input_Q, int radix_N,
446  char *input_N, int radix_E, char *input_E,
447  int hash, char *result_hex_str, char *seed,
448  char *message_hex_string, int result )
449 {
450  unsigned char message_str[1000];
451  unsigned char output[1000];
452  unsigned char output_str[1000];
453  rsa_context ctx;
454  mpi P1, Q1, H, G;
455  size_t output_len;
456  rnd_pseudo_info rnd_info;
457  ((void) seed);
458 
459  mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
460  rsa_init( &ctx, RSA_PKCS_V21, hash );
461 
462  memset( message_str, 0x00, 1000 );
463  memset( output, 0x00, 1000 );
464  memset( output_str, 0x00, 1000 );
465  memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
466 
467  ctx.len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 );
468  TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
469  TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
470  TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
471  TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
472 
473  TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
474  TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
475  TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
476  TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
477  TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
478  TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
479  TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
480  TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
481 
482  TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
483 
484  unhexify( message_str, message_hex_string );
485 
486  TEST_ASSERT( rsa_pkcs1_decrypt( &ctx, &rnd_pseudo_rand, &rnd_info, RSA_PRIVATE, &output_len, message_str, output, 1000 ) == result );
487  if( result == 0 )
488  {
489  hexify( output_str, output, ctx.len );
490 
491  TEST_ASSERT( strncasecmp( (char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 );
492  }
493 
494  mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
495  rsa_free( &ctx );
496 }
497 
498 void test_suite_pkcs1_rsassa_pss_sign( int mod, int radix_P, char *input_P, int radix_Q,
499  char *input_Q, int radix_N, char *input_N,
500  int radix_E, char *input_E, int digest, int hash,
501  char *message_hex_string, char *salt,
502  char *result_hex_str, int result )
503 {
504  unsigned char message_str[1000];
505  unsigned char hash_result[1000];
506  unsigned char output[1000];
507  unsigned char output_str[1000];
508  unsigned char rnd_buf[1000];
509  rsa_context ctx;
510  mpi P1, Q1, H, G;
511  size_t msg_len;
512  rnd_buf_info info;
513 
514  info.length = unhexify( rnd_buf, salt );
515  info.buf = rnd_buf;
516 
517  mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
518  rsa_init( &ctx, RSA_PKCS_V21, hash );
519 
520  memset( message_str, 0x00, 1000 );
521  memset( hash_result, 0x00, 1000 );
522  memset( output, 0x00, 1000 );
523  memset( output_str, 0x00, 1000 );
524 
525  ctx.len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 );
526  TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
527  TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
528  TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
529  TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
530 
531  TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
532  TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
533  TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
534  TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
535  TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
536  TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
537  TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
538  TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
539 
540  TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
541 
542  msg_len = unhexify( message_str, message_hex_string );
543 
544  if( md_info_from_type( digest ) != NULL )
545  TEST_ASSERT( md( md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
546 
547  TEST_ASSERT( rsa_pkcs1_sign( &ctx, &rnd_buffer_rand, &info, RSA_PRIVATE, digest, 0, hash_result, output ) == result );
548  if( result == 0 )
549  {
550  hexify( output_str, output, ctx.len);
551 
552  TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
553  }
554 
555  mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
556  rsa_free( &ctx );
557 }
558 
559 void test_suite_pkcs1_rsassa_pss_verify( int mod, int radix_N, char *input_N, int radix_E,
560  char *input_E, int digest, int hash,
561  char *message_hex_string, char *salt,
562  char *result_hex_str, int result )
563 {
564  unsigned char message_str[1000];
565  unsigned char hash_result[1000];
566  unsigned char result_str[1000];
567  rsa_context ctx;
568  size_t msg_len;
569  ((void) salt);
570 
571  rsa_init( &ctx, RSA_PKCS_V21, hash );
572  memset( message_str, 0x00, 1000 );
573  memset( hash_result, 0x00, 1000 );
574  memset( result_str, 0x00, 1000 );
575 
576  ctx.len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 );
577  TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
578  TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
579 
580  TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
581 
582  msg_len = unhexify( message_str, message_hex_string );
583  unhexify( result_str, result_hex_str );
584 
585  if( md_info_from_type( digest ) != NULL )
586  TEST_ASSERT( md( md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
587 
588  TEST_ASSERT( rsa_pkcs1_verify( &ctx, NULL, NULL, RSA_PUBLIC, digest, 0, hash_result, result_str ) == result );
589 
590  rsa_free( &ctx );
591 }
592 
593 
594 #endif /* POLARSSL_PKCS1_V21 */
595 #endif /* POLARSSL_RSA_C */
596 #endif /* POLARSSL_BIGNUM_C */
597 #endif /* POLARSSL_SHA1_C */
598 #endif /* POLARSSL_GENPRIME */
599 
600 
601 int dep_check( char *str )
602 {
603  if( str == NULL )
604  return( 1 );
605 
606 
607 
608  return( 1 );
609 }
610 
611 int dispatch_test(int cnt, char *params[50])
612 {
613  int ret;
614  ((void) cnt);
615  ((void) params);
616 
617 #if defined(TEST_SUITE_ACTIVE)
618  if( strcmp( params[0], "pkcs1_rsaes_oaep_encrypt" ) == 0 )
619  {
620 
621  int param1;
622  int param2;
623  char *param3 = params[3];
624  int param4;
625  char *param5 = params[5];
626  int param6;
627  char *param7 = params[7];
628  char *param8 = params[8];
629  char *param9 = params[9];
630  int param10;
631 
632  if( cnt != 11 )
633  {
634  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 11 );
635  return( 2 );
636  }
637 
638  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
639  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
640  if( verify_string( &param3 ) != 0 ) return( 2 );
641  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
642  if( verify_string( &param5 ) != 0 ) return( 2 );
643  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
644  if( verify_string( &param7 ) != 0 ) return( 2 );
645  if( verify_string( &param8 ) != 0 ) return( 2 );
646  if( verify_string( &param9 ) != 0 ) return( 2 );
647  if( verify_int( params[10], &param10 ) != 0 ) return( 2 );
648 
649  test_suite_pkcs1_rsaes_oaep_encrypt( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
650  return ( 0 );
651 
652  return ( 3 );
653  }
654  else
655  if( strcmp( params[0], "pkcs1_rsaes_oaep_decrypt" ) == 0 )
656  {
657 
658  int param1;
659  int param2;
660  char *param3 = params[3];
661  int param4;
662  char *param5 = params[5];
663  int param6;
664  char *param7 = params[7];
665  int param8;
666  char *param9 = params[9];
667  int param10;
668  char *param11 = params[11];
669  char *param12 = params[12];
670  char *param13 = params[13];
671  int param14;
672 
673  if( cnt != 15 )
674  {
675  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 15 );
676  return( 2 );
677  }
678 
679  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
680  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
681  if( verify_string( &param3 ) != 0 ) return( 2 );
682  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
683  if( verify_string( &param5 ) != 0 ) return( 2 );
684  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
685  if( verify_string( &param7 ) != 0 ) return( 2 );
686  if( verify_int( params[8], &param8 ) != 0 ) return( 2 );
687  if( verify_string( &param9 ) != 0 ) return( 2 );
688  if( verify_int( params[10], &param10 ) != 0 ) return( 2 );
689  if( verify_string( &param11 ) != 0 ) return( 2 );
690  if( verify_string( &param12 ) != 0 ) return( 2 );
691  if( verify_string( &param13 ) != 0 ) return( 2 );
692  if( verify_int( params[14], &param14 ) != 0 ) return( 2 );
693 
694  test_suite_pkcs1_rsaes_oaep_decrypt( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14 );
695  return ( 0 );
696 
697  return ( 3 );
698  }
699  else
700  if( strcmp( params[0], "pkcs1_rsassa_pss_sign" ) == 0 )
701  {
702 
703  int param1;
704  int param2;
705  char *param3 = params[3];
706  int param4;
707  char *param5 = params[5];
708  int param6;
709  char *param7 = params[7];
710  int param8;
711  char *param9 = params[9];
712  int param10;
713  int param11;
714  char *param12 = params[12];
715  char *param13 = params[13];
716  char *param14 = params[14];
717  int param15;
718 
719  if( cnt != 16 )
720  {
721  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 16 );
722  return( 2 );
723  }
724 
725  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
726  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
727  if( verify_string( &param3 ) != 0 ) return( 2 );
728  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
729  if( verify_string( &param5 ) != 0 ) return( 2 );
730  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
731  if( verify_string( &param7 ) != 0 ) return( 2 );
732  if( verify_int( params[8], &param8 ) != 0 ) return( 2 );
733  if( verify_string( &param9 ) != 0 ) return( 2 );
734  if( verify_int( params[10], &param10 ) != 0 ) return( 2 );
735  if( verify_int( params[11], &param11 ) != 0 ) return( 2 );
736  if( verify_string( &param12 ) != 0 ) return( 2 );
737  if( verify_string( &param13 ) != 0 ) return( 2 );
738  if( verify_string( &param14 ) != 0 ) return( 2 );
739  if( verify_int( params[15], &param15 ) != 0 ) return( 2 );
740 
741  test_suite_pkcs1_rsassa_pss_sign( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, param15 );
742  return ( 0 );
743 
744  return ( 3 );
745  }
746  else
747  if( strcmp( params[0], "pkcs1_rsassa_pss_verify" ) == 0 )
748  {
749 
750  int param1;
751  int param2;
752  char *param3 = params[3];
753  int param4;
754  char *param5 = params[5];
755  int param6;
756  int param7;
757  char *param8 = params[8];
758  char *param9 = params[9];
759  char *param10 = params[10];
760  int param11;
761 
762  if( cnt != 12 )
763  {
764  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 12 );
765  return( 2 );
766  }
767 
768  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
769  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
770  if( verify_string( &param3 ) != 0 ) return( 2 );
771  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
772  if( verify_string( &param5 ) != 0 ) return( 2 );
773  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
774  if( verify_int( params[7], &param7 ) != 0 ) return( 2 );
775  if( verify_string( &param8 ) != 0 ) return( 2 );
776  if( verify_string( &param9 ) != 0 ) return( 2 );
777  if( verify_string( &param10 ) != 0 ) return( 2 );
778  if( verify_int( params[11], &param11 ) != 0 ) return( 2 );
779 
780  test_suite_pkcs1_rsassa_pss_verify( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11 );
781  return ( 0 );
782 
783  return ( 3 );
784  }
785  else
786 
787  {
788  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
789  fflush( stdout );
790  return( 1 );
791  }
792 #else
793  return( 3 );
794 #endif
795  return( ret );
796 }
797 
798 int get_line( FILE *f, char *buf, size_t len )
799 {
800  char *ret;
801 
802  ret = fgets( buf, len, f );
803  if( ret == NULL )
804  return( -1 );
805 
806  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
807  buf[strlen(buf) - 1] = '\0';
808  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
809  buf[strlen(buf) - 1] = '\0';
810 
811  return( 0 );
812 }
813 
814 int parse_arguments( char *buf, size_t len, char *params[50] )
815 {
816  int cnt = 0, i;
817  char *cur = buf;
818  char *p = buf, *q;
819 
820  params[cnt++] = cur;
821 
822  while( *p != '\0' && p < buf + len )
823  {
824  if( *p == '\\' )
825  {
826  *p++;
827  *p++;
828  continue;
829  }
830  if( *p == ':' )
831  {
832  if( p + 1 < buf + len )
833  {
834  cur = p + 1;
835  params[cnt++] = cur;
836  }
837  *p = '\0';
838  }
839 
840  *p++;
841  }
842 
843  // Replace newlines, question marks and colons in strings
844  for( i = 0; i < cnt; i++ )
845  {
846  p = params[i];
847  q = params[i];
848 
849  while( *p != '\0' )
850  {
851  if( *p == '\\' && *(p + 1) == 'n' )
852  {
853  p += 2;
854  *(q++) = '\n';
855  }
856  else if( *p == '\\' && *(p + 1) == ':' )
857  {
858  p += 2;
859  *(q++) = ':';
860  }
861  else if( *p == '\\' && *(p + 1) == '?' )
862  {
863  p += 2;
864  *(q++) = '?';
865  }
866  else
867  *(q++) = *(p++);
868  }
869  *q = '\0';
870  }
871 
872  return( cnt );
873 }
874 
875 int main()
876 {
877  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
878  const char *filename = "/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_pkcs1_v21.data";
879  FILE *file;
880  char buf[5000];
881  char *params[50];
882 
883 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
884  unsigned char alloc_buf[1000000];
885  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
886 #endif
887 
888  file = fopen( filename, "r" );
889  if( file == NULL )
890  {
891  fprintf( stderr, "Failed to open\n" );
892  return( 1 );
893  }
894 
895  while( !feof( file ) )
896  {
897  int skip = 0;
898 
899  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
900  break;
901  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
902  fprintf( stdout, " " );
903  for( i = strlen( buf ) + 1; i < 67; i++ )
904  fprintf( stdout, "." );
905  fprintf( stdout, " " );
906  fflush( stdout );
907 
908  total_tests++;
909 
910  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
911  break;
912  cnt = parse_arguments( buf, strlen(buf), params );
913 
914  if( strcmp( params[0], "depends_on" ) == 0 )
915  {
916  for( i = 1; i < cnt; i++ )
917  if( dep_check( params[i] ) != 0 )
918  skip = 1;
919 
920  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
921  break;
922  cnt = parse_arguments( buf, strlen(buf), params );
923  }
924 
925  if( skip == 0 )
926  {
927  test_errors = 0;
928  ret = dispatch_test( cnt, params );
929  }
930 
931  if( skip == 1 || ret == 3 )
932  {
933  total_skipped++;
934  fprintf( stdout, "----\n" );
935  fflush( stdout );
936  }
937  else if( ret == 0 && test_errors == 0 )
938  {
939  fprintf( stdout, "PASS\n" );
940  fflush( stdout );
941  }
942  else if( ret == 2 )
943  {
944  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
945  fclose(file);
946  exit( 2 );
947  }
948  else
949  total_errors++;
950 
951  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
952  break;
953  if( strlen(buf) != 0 )
954  {
955  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
956  return( 1 );
957  }
958  }
959  fclose(file);
960 
961  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
962  if( total_errors == 0 )
963  fprintf( stdout, "PASSED" );
964  else
965  fprintf( stdout, "FAILED" );
966 
967  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
968  total_tests - total_errors, total_tests, total_skipped );
969 
970 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
971 #if defined(POLARSSL_MEMORY_DEBUG)
972  memory_buffer_alloc_status();
973 #endif
974  memory_buffer_alloc_free();
975 #endif
976 
977  return( total_errors != 0 );
978 }
979 
980 
int md(const md_info_t *md_info, const unsigned char *input, size_t ilen, unsigned char *output)
Output = message_digest( input buffer )
Memory allocation layer.
int rsa_check_privkey(const rsa_context *ctx)
Check a private RSA key.
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 void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
#define RSA_PUBLIC
Definition: rsa.h:55
#define RSA_PKCS_V21
Definition: rsa.h:59
mpi DQ
Definition: rsa.h:89
Configuration options (set of defines)
int rsa_check_pubkey(const rsa_context *ctx)
Check a public RSA key.
#define PUT_UINT32_BE(n, b, i)
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.
MPI structure.
Definition: bignum.h:168
static int test_assert(int correct, char *test)
void mpi_init(mpi *X)
Initialize one MPI.
int main(int argc, char *argv[])
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.
static int test_errors
void rsa_free(rsa_context *ctx)
Free the components of an RSA key.
RSA context structure.
Definition: rsa.h:77
mpi D
Definition: rsa.h:85
int rsa_pkcs1_encrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t ilen, const unsigned char *input, unsigned char *output)
Generic wrapper to perform a PKCS#1 encryption using the mode from the context.
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
mpi QP
Definition: rsa.h:90
#define RSA_PRIVATE
Definition: rsa.h:56
mpi N
Definition: rsa.h:82
static int not_rnd(void *in, unsigned char *out, size_t len)
This function returns a buffer given as a hex string.
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.
mpi E
Definition: rsa.h:83
int parse_arguments(char *buf, size_t len, char *params[50])
int rsa_pkcs1_verify(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, const unsigned char *sig)
Generic wrapper to perform a PKCS#1 verification using the mode from the context. ...
mpi DP
Definition: rsa.h:88
static int unhexify(unsigned char *obuf, const char *ibuf)
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 mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
Generic message digest wrapper.
The RSA public-key cryptosystem.
int verify_string(char **str)
#define POLARSSL_ERR_RSA_BAD_INPUT_DATA
Bad input parameters to function.
Definition: rsa.h:42
SHA-1 cryptographic hash function.
int dispatch_test(int cnt, char *params[50])
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.
unsigned char * buf
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
SHA-384 and SHA-512 cryptographic hash function.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
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 mpi_mul_mpi(mpi *X, const mpi *A, const mpi *B)
Baseline multiplication: X = A * B.
int verify_int(char *str, int *value)
MD4 message digest algorithm (hash function)
MD5 message digest algorithm (hash function)
SHA-224 and SHA-256 cryptographic hash function.
int mpi_sub_int(mpi *X, const mpi *A, t_sint b)
Signed substraction: X = A - b.
MD2 message digest algorithm (hash function)
int get_line(FILE *f, char *buf, size_t len)