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