PolarSSL v1.3.8
test_suite_rsa.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_RSA_C
8 #ifdef POLARSSL_BIGNUM_C
9 #ifdef POLARSSL_GENPRIME
10 
11 #include <polarssl/rsa.h>
12 #include <polarssl/md2.h>
13 #include <polarssl/md4.h>
14 #include <polarssl/md5.h>
15 #include <polarssl/sha1.h>
16 #include <polarssl/sha256.h>
17 #include <polarssl/sha512.h>
18 #include <polarssl/entropy.h>
19 #include <polarssl/ctr_drbg.h>
20 #endif /* POLARSSL_RSA_C */
21 #endif /* POLARSSL_BIGNUM_C */
22 #endif /* POLARSSL_GENPRIME */
23 
24 
25 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
26 #include "polarssl/memory.h"
27 #endif
28 
29 #if defined(POLARSSL_PLATFORM_C)
30 #include "polarssl/platform.h"
31 #else
32 #define polarssl_malloc malloc
33 #define polarssl_free free
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 
135 static unsigned char *zero_alloc( size_t len )
136 {
137  void *p;
138  size_t actual_len = len != 0 ? len : 1;
139 
140  p = polarssl_malloc( actual_len );
141  assert( p != NULL );
142 
143  memset( p, 0x00, actual_len );
144 
145  return( p );
146 }
147 
158 static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
159 {
160  unsigned char *obuf;
161 
162  *olen = strlen(ibuf) / 2;
163 
164  if( *olen == 0 )
165  return( zero_alloc( *olen ) );
166 
167  obuf = polarssl_malloc( *olen );
168  assert( obuf != NULL );
169 
170  (void) unhexify( obuf, ibuf );
171 
172  return( obuf );
173 }
174 
184 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
185 {
186 #if !defined(__OpenBSD__)
187  size_t i;
188 
189  if( rng_state != NULL )
190  rng_state = NULL;
191 
192  for( i = 0; i < len; ++i )
193  output[i] = rand();
194 #else
195  if( rng_state != NULL )
196  rng_state = NULL;
197 
198  arc4random_buf( output, len );
199 #endif /* !OpenBSD */
200 
201  return( 0 );
202 }
203 
209 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
210 {
211  if( rng_state != NULL )
212  rng_state = NULL;
213 
214  memset( output, 0, len );
215 
216  return( 0 );
217 }
218 
219 typedef struct
220 {
221  unsigned char *buf;
222  size_t length;
223 } rnd_buf_info;
224 
236 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
237 {
238  rnd_buf_info *info = (rnd_buf_info *) rng_state;
239  size_t use_len;
240 
241  if( rng_state == NULL )
242  return( rnd_std_rand( NULL, output, len ) );
243 
244  use_len = len;
245  if( len > info->length )
246  use_len = info->length;
247 
248  if( use_len )
249  {
250  memcpy( output, info->buf, use_len );
251  info->buf += use_len;
252  info->length -= use_len;
253  }
254 
255  if( len - use_len > 0 )
256  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
257 
258  return( 0 );
259 }
260 
268 typedef struct
269 {
270  uint32_t key[16];
271  uint32_t v0, v1;
273 
282 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
283 {
284  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
285  uint32_t i, *k, sum, delta=0x9E3779B9;
286  unsigned char result[4], *out = output;
287 
288  if( rng_state == NULL )
289  return( rnd_std_rand( NULL, output, len ) );
290 
291  k = info->key;
292 
293  while( len > 0 )
294  {
295  size_t use_len = ( len > 4 ) ? 4 : len;
296  sum = 0;
297 
298  for( i = 0; i < 32; i++ )
299  {
300  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
301  sum += delta;
302  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
303  }
304 
305  PUT_UINT32_BE( info->v0, result, 0 );
306  memcpy( out, result, use_len );
307  len -= use_len;
308  out += 4;
309  }
310 
311  return( 0 );
312 }
313 
314 
315 #include <stdio.h>
316 #include <string.h>
317 
318 #if defined(POLARSSL_PLATFORM_C)
319 #include "polarssl/platform.h"
320 #else
321 #define polarssl_printf printf
322 #define polarssl_malloc malloc
323 #define polarssl_free free
324 #endif
325 
326 static int test_errors = 0;
327 
328 #ifdef POLARSSL_RSA_C
329 #ifdef POLARSSL_BIGNUM_C
330 #ifdef POLARSSL_GENPRIME
331 
332 #define TEST_SUITE_ACTIVE
333 
334 static int test_assert( int correct, const char *test )
335 {
336  if( correct )
337  return( 0 );
338 
339  test_errors++;
340  if( test_errors == 1 )
341  printf( "FAILED\n" );
342  printf( " %s\n", test );
343 
344  return( 1 );
345 }
346 
347 #define TEST_ASSERT( TEST ) \
348  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
349  if( test_errors) goto exit; \
350  } while (0)
351 
352 int verify_string( char **str )
353 {
354  if( (*str)[0] != '"' ||
355  (*str)[strlen( *str ) - 1] != '"' )
356  {
357  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
358  return( -1 );
359  }
360 
361  (*str)++;
362  (*str)[strlen( *str ) - 1] = '\0';
363 
364  return( 0 );
365 }
366 
367 int verify_int( char *str, int *value )
368 {
369  size_t i;
370  int minus = 0;
371  int digits = 1;
372  int hex = 0;
373 
374  for( i = 0; i < strlen( str ); i++ )
375  {
376  if( i == 0 && str[i] == '-' )
377  {
378  minus = 1;
379  continue;
380  }
381 
382  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
383  str[i - 1] == '0' && str[i] == 'x' )
384  {
385  hex = 1;
386  continue;
387  }
388 
389  if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
390  ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
391  ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
392  {
393  digits = 0;
394  break;
395  }
396  }
397 
398  if( digits )
399  {
400  if( hex )
401  *value = strtol( str, NULL, 16 );
402  else
403  *value = strtol( str, NULL, 10 );
404 
405  return( 0 );
406  }
407 
408  if( strcmp( str, "POLARSSL_ERR_RSA_VERIFY_FAILED" ) == 0 )
409  {
410  *value = ( POLARSSL_ERR_RSA_VERIFY_FAILED );
411  return( 0 );
412  }
413  if( strcmp( str, "POLARSSL_MD_MD2" ) == 0 )
414  {
415  *value = ( POLARSSL_MD_MD2 );
416  return( 0 );
417  }
418  if( strcmp( str, "POLARSSL_ERR_RSA_KEY_CHECK_FAILED" ) == 0 )
419  {
421  return( 0 );
422  }
423  if( strcmp( str, "POLARSSL_MD_SHA384" ) == 0 )
424  {
425  *value = ( POLARSSL_MD_SHA384 );
426  return( 0 );
427  }
428  if( strcmp( str, "POLARSSL_MD_MD4" ) == 0 )
429  {
430  *value = ( POLARSSL_MD_MD4 );
431  return( 0 );
432  }
433  if( strcmp( str, "POLARSSL_MD_SHA224" ) == 0 )
434  {
435  *value = ( POLARSSL_MD_SHA224 );
436  return( 0 );
437  }
438  if( strcmp( str, "POLARSSL_ERR_RSA_INVALID_PADDING" ) == 0 )
439  {
441  return( 0 );
442  }
443  if( strcmp( str, "POLARSSL_MD_MD5" ) == 0 )
444  {
445  *value = ( POLARSSL_MD_MD5 );
446  return( 0 );
447  }
448  if( strcmp( str, "POLARSSL_ERR_RSA_RNG_FAILED" ) == 0 )
449  {
450  *value = ( POLARSSL_ERR_RSA_RNG_FAILED );
451  return( 0 );
452  }
453  if( strcmp( str, "POLARSSL_ERR_RSA_BAD_INPUT_DATA" ) == 0 )
454  {
455  *value = ( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
456  return( 0 );
457  }
458  if( strcmp( str, "RSA_PKCS_V15" ) == 0 )
459  {
460  *value = ( RSA_PKCS_V15 );
461  return( 0 );
462  }
463  if( strcmp( str, "POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE" ) == 0 )
464  {
466  return( 0 );
467  }
468  if( strcmp( str, "POLARSSL_MD_SHA512" ) == 0 )
469  {
470  *value = ( POLARSSL_MD_SHA512 );
471  return( 0 );
472  }
473  if( strcmp( str, "POLARSSL_MD_SHA1" ) == 0 )
474  {
475  *value = ( POLARSSL_MD_SHA1 );
476  return( 0 );
477  }
478  if( strcmp( str, "POLARSSL_MD_SHA256" ) == 0 )
479  {
480  *value = ( POLARSSL_MD_SHA256 );
481  return( 0 );
482  }
483 
484 
485  printf( "Expected integer for parameter and got: %s\n", str );
486  return( -1 );
487 }
488 
489 void test_suite_rsa_pkcs1_sign( char *message_hex_string, int padding_mode, int digest,
490  int mod, int radix_P, char *input_P, int radix_Q,
491  char *input_Q, int radix_N, char *input_N, int radix_E,
492  char *input_E, char *result_hex_str, int result )
493 {
494  unsigned char message_str[1000];
495  unsigned char hash_result[1000];
496  unsigned char output[1000];
497  unsigned char output_str[1000];
498  rsa_context ctx;
499  mpi P1, Q1, H, G;
500  int msg_len;
501  rnd_pseudo_info rnd_info;
502 
503  mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
504  rsa_init( &ctx, padding_mode, 0 );
505 
506  memset( message_str, 0x00, 1000 );
507  memset( hash_result, 0x00, 1000 );
508  memset( output, 0x00, 1000 );
509  memset( output_str, 0x00, 1000 );
510  memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
511 
512  ctx.len = mod / 8;
513  TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
514  TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
515  TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
516  TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
517 
518  TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
519  TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
520  TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
521  TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
522  TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
523  TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
524  TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
525  TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
526 
527  TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
528 
529  msg_len = unhexify( message_str, message_hex_string );
530 
531  if( md_info_from_type( digest ) != NULL )
532  TEST_ASSERT( md( md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
533 
534  TEST_ASSERT( rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info, RSA_PRIVATE, digest, 0, hash_result, output ) == result );
535  if( result == 0 )
536  {
537  hexify( output_str, output, ctx.len );
538 
539  TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
540  }
541 
542 exit:
543  mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
544  rsa_free( &ctx );
545 }
546 
547 void test_suite_rsa_pkcs1_verify( char *message_hex_string, int padding_mode, int digest,
548  int mod, int radix_N, char *input_N, int radix_E,
549  char *input_E, char *result_hex_str, int result )
550 {
551  unsigned char message_str[1000];
552  unsigned char hash_result[1000];
553  unsigned char result_str[1000];
554  rsa_context ctx;
555  int msg_len;
556 
557  rsa_init( &ctx, padding_mode, 0 );
558  memset( message_str, 0x00, 1000 );
559  memset( hash_result, 0x00, 1000 );
560  memset( result_str, 0x00, 1000 );
561 
562  ctx.len = mod / 8;
563  TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
564  TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
565 
566  TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
567 
568  msg_len = unhexify( message_str, message_hex_string );
569  unhexify( result_str, result_hex_str );
570 
571  if( md_info_from_type( digest ) != NULL )
572  TEST_ASSERT( md( md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
573 
574  TEST_ASSERT( rsa_pkcs1_verify( &ctx, NULL, NULL, RSA_PUBLIC, digest, 0, hash_result, result_str ) == result );
575 
576 exit:
577  rsa_free( &ctx );
578 }
579 
580 void test_suite_rsa_pkcs1_sign_raw( char *message_hex_string, char *hash_result_string,
581  int padding_mode, int mod, int radix_P, char *input_P,
582  int radix_Q, char *input_Q, int radix_N,
583  char *input_N, int radix_E, char *input_E,
584  char *result_hex_str )
585 {
586  unsigned char message_str[1000];
587  unsigned char hash_result[1000];
588  unsigned char output[1000];
589  unsigned char output_str[1000];
590  rsa_context ctx;
591  mpi P1, Q1, H, G;
592  int hash_len;
593  rnd_pseudo_info rnd_info;
594 
595  mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
596  rsa_init( &ctx, padding_mode, 0 );
597 
598  memset( message_str, 0x00, 1000 );
599  memset( hash_result, 0x00, 1000 );
600  memset( output, 0x00, 1000 );
601  memset( output_str, 0x00, 1000 );
602  memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
603 
604  ctx.len = mod / 8;
605  TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
606  TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
607  TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
608  TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
609 
610  TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
611  TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
612  TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
613  TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
614  TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
615  TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
616  TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
617  TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
618 
619  TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
620 
621  unhexify( message_str, message_hex_string );
622  hash_len = unhexify( hash_result, hash_result_string );
623 
624  TEST_ASSERT( rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info, RSA_PRIVATE, POLARSSL_MD_NONE, hash_len, hash_result, output ) == 0 );
625 
626  hexify( output_str, output, ctx.len );
627 
628  TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
629 
630  /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
631  if( padding_mode == RSA_PKCS_V15 )
632  {
633  memset( output, 0x00, 1000 );
634  memset( output_str, 0x00, 1000 );
635 
637  &rnd_pseudo_rand, &rnd_info, RSA_PRIVATE,
638  hash_len, hash_result, output ) == 0 );
639 
640  hexify( output_str, output, ctx.len );
641 
642  TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
643  }
644 
645 exit:
646  mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
647  rsa_free( &ctx );
648 }
649 
650 void test_suite_rsa_pkcs1_verify_raw( char *message_hex_string, char *hash_result_string,
651  int padding_mode, int mod, int radix_N,
652  char *input_N, int radix_E, char *input_E,
653  char *result_hex_str, int correct )
654 {
655  unsigned char message_str[1000];
656  unsigned char hash_result[1000];
657  unsigned char result_str[1000];
658  unsigned char output[1000];
659  rsa_context ctx;
660  size_t hash_len, olen;
661 
662  rsa_init( &ctx, padding_mode, 0 );
663  memset( message_str, 0x00, 1000 );
664  memset( hash_result, 0x00, 1000 );
665  memset( result_str, 0x00, 1000 );
666  memset( output, 0x00, sizeof( output ) );
667 
668  ctx.len = mod / 8;
669  TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
670  TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
671 
672  TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
673 
674  unhexify( message_str, message_hex_string );
675  hash_len = unhexify( hash_result, hash_result_string );
676  unhexify( result_str, result_hex_str );
677 
678  TEST_ASSERT( rsa_pkcs1_verify( &ctx, NULL, NULL, RSA_PUBLIC, POLARSSL_MD_NONE, hash_len, hash_result, result_str ) == correct );
679 
680  /* For PKCS#1 v1.5, there is an alternative way to verify signatures */
681  if( padding_mode == RSA_PKCS_V15 )
682  {
683  int ok;
684 
686  NULL, NULL, RSA_PUBLIC,
687  &olen, result_str, output, sizeof( output ) ) == 0 );
688 
689  ok = olen == hash_len && memcmp( output, hash_result, olen ) == 0;
690  if( correct == 0 )
691  TEST_ASSERT( ok == 1 );
692  else
693  TEST_ASSERT( ok == 0 );
694  }
695 
696 exit:
697  rsa_free( &ctx );
698 }
699 
700 void test_suite_rsa_pkcs1_encrypt( char *message_hex_string, int padding_mode, int mod,
701  int radix_N, char *input_N, int radix_E, char *input_E,
702  char *result_hex_str, int result )
703 {
704  unsigned char message_str[1000];
705  unsigned char output[1000];
706  unsigned char output_str[1000];
707  rsa_context ctx;
708  size_t msg_len;
709  rnd_pseudo_info rnd_info;
710 
711  memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
712 
713  rsa_init( &ctx, padding_mode, 0 );
714  memset( message_str, 0x00, 1000 );
715  memset( output, 0x00, 1000 );
716  memset( output_str, 0x00, 1000 );
717 
718  ctx.len = mod / 8;
719  TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
720  TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
721 
722  TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
723 
724  msg_len = unhexify( message_str, message_hex_string );
725 
726  TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info, RSA_PUBLIC, msg_len, message_str, output ) == result );
727  if( result == 0 )
728  {
729  hexify( output_str, output, ctx.len );
730 
731  TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
732  }
733 
734 exit:
735  rsa_free( &ctx );
736 }
737 
738 void test_suite_rsa_pkcs1_encrypt_bad_rng( char *message_hex_string, int padding_mode,
739  int mod, int radix_N, char *input_N,
740  int radix_E, char *input_E,
741  char *result_hex_str, int result )
742 {
743  unsigned char message_str[1000];
744  unsigned char output[1000];
745  unsigned char output_str[1000];
746  rsa_context ctx;
747  size_t msg_len;
748 
749  rsa_init( &ctx, padding_mode, 0 );
750  memset( message_str, 0x00, 1000 );
751  memset( output, 0x00, 1000 );
752  memset( output_str, 0x00, 1000 );
753 
754  ctx.len = mod / 8;
755  TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
756  TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
757 
758  TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
759 
760  msg_len = unhexify( message_str, message_hex_string );
761 
762  TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL, RSA_PUBLIC, msg_len, message_str, output ) == result );
763  if( result == 0 )
764  {
765  hexify( output_str, output, ctx.len );
766 
767  TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
768  }
769 
770 exit:
771  rsa_free( &ctx );
772 }
773 
774 void test_suite_rsa_pkcs1_decrypt( char *message_hex_string, int padding_mode, int mod,
775  int radix_P, char *input_P, int radix_Q, char *input_Q,
776  int radix_N, char *input_N, int radix_E, char *input_E,
777  int max_output, char *result_hex_str, int result )
778 {
779  unsigned char message_str[1000];
780  unsigned char output[1000];
781  unsigned char output_str[1000];
782  rsa_context ctx;
783  mpi P1, Q1, H, G;
784  size_t output_len;
785  rnd_pseudo_info rnd_info;
786 
787  mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
788  rsa_init( &ctx, padding_mode, 0 );
789 
790  memset( message_str, 0x00, 1000 );
791  memset( output, 0x00, 1000 );
792  memset( output_str, 0x00, 1000 );
793  memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
794 
795  ctx.len = mod / 8;
796  TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
797  TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
798  TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
799  TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
800 
801  TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
802  TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
803  TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
804  TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
805  TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
806  TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
807  TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
808  TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
809 
810  TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
811 
812  unhexify( message_str, message_hex_string );
813  output_len = 0;
814 
815  TEST_ASSERT( rsa_pkcs1_decrypt( &ctx, rnd_pseudo_rand, &rnd_info, RSA_PRIVATE, &output_len, message_str, output, max_output ) == result );
816  if( result == 0 )
817  {
818  hexify( output_str, output, ctx.len );
819 
820  TEST_ASSERT( strncasecmp( (char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 );
821  }
822 
823 exit:
824  mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
825  rsa_free( &ctx );
826 }
827 
828 void test_suite_rsa_public( char *message_hex_string, int mod, int radix_N, char *input_N,
829  int radix_E, char *input_E, char *result_hex_str, int result )
830 {
831  unsigned char message_str[1000];
832  unsigned char output[1000];
833  unsigned char output_str[1000];
834  rsa_context ctx, ctx2; /* Also test rsa_copy() while at it */
835 
836  rsa_init( &ctx, RSA_PKCS_V15, 0 );
837  rsa_init( &ctx2, RSA_PKCS_V15, 0 );
838  memset( message_str, 0x00, 1000 );
839  memset( output, 0x00, 1000 );
840  memset( output_str, 0x00, 1000 );
841 
842  ctx.len = mod / 8;
843  TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
844  TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
845 
846  TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
847 
848  unhexify( message_str, message_hex_string );
849 
850  TEST_ASSERT( rsa_public( &ctx, message_str, output ) == result );
851  if( result == 0 )
852  {
853  hexify( output_str, output, ctx.len );
854 
855  TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
856  }
857 
858  /* And now with the copy */
859  TEST_ASSERT( rsa_copy( &ctx2, &ctx ) == 0 );
860  /* clear the original to be sure */
861  rsa_free( &ctx );
862 
863  TEST_ASSERT( rsa_check_pubkey( &ctx2 ) == 0 );
864 
865  memset( output, 0x00, 1000 );
866  memset( output_str, 0x00, 1000 );
867  TEST_ASSERT( rsa_public( &ctx2, message_str, output ) == result );
868  if( result == 0 )
869  {
870  hexify( output_str, output, ctx2.len );
871 
872  TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
873  }
874 
875 exit:
876  rsa_free( &ctx );
877  rsa_free( &ctx2 );
878 }
879 
880 void test_suite_rsa_private( char *message_hex_string, int mod, int radix_P, char *input_P,
881  int radix_Q, char *input_Q, int radix_N, char *input_N,
882  int radix_E, char *input_E, char *result_hex_str, int result )
883 {
884  unsigned char message_str[1000];
885  unsigned char output[1000];
886  unsigned char output_str[1000];
887  rsa_context ctx, ctx2; /* Also test rsa_copy() while at it */
888  mpi P1, Q1, H, G;
889  rnd_pseudo_info rnd_info;
890  int i;
891 
892  mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
893  rsa_init( &ctx, RSA_PKCS_V15, 0 );
894  rsa_init( &ctx2, RSA_PKCS_V15, 0 );
895 
896  memset( message_str, 0x00, 1000 );
897  memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
898 
899  ctx.len = mod / 8;
900  TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
901  TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
902  TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
903  TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
904 
905  TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
906  TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
907  TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
908  TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
909  TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
910  TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
911  TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
912  TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
913 
914  TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
915 
916  unhexify( message_str, message_hex_string );
917 
918  /* repeat three times to test updating of blinding values */
919  for( i = 0; i < 3; i++ )
920  {
921  memset( output, 0x00, 1000 );
922  memset( output_str, 0x00, 1000 );
923  TEST_ASSERT( rsa_private( &ctx, rnd_pseudo_rand, &rnd_info,
924  message_str, output ) == result );
925  if( result == 0 )
926  {
927  hexify( output_str, output, ctx.len );
928 
929  TEST_ASSERT( strcasecmp( (char *) output_str,
930  result_hex_str ) == 0 );
931  }
932  }
933 
934  /* And now one more time with the copy */
935  TEST_ASSERT( rsa_copy( &ctx2, &ctx ) == 0 );
936  /* clear the original to be sure */
937  rsa_free( &ctx );
938 
939  TEST_ASSERT( rsa_check_privkey( &ctx2 ) == 0 );
940 
941  memset( output, 0x00, 1000 );
942  memset( output_str, 0x00, 1000 );
943  TEST_ASSERT( rsa_private( &ctx2, rnd_pseudo_rand, &rnd_info,
944  message_str, output ) == result );
945  if( result == 0 )
946  {
947  hexify( output_str, output, ctx2.len );
948 
949  TEST_ASSERT( strcasecmp( (char *) output_str,
950  result_hex_str ) == 0 );
951  }
952 
953 exit:
954  mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
955  rsa_free( &ctx ); rsa_free( &ctx2 );
956 }
957 
958 void test_suite_rsa_check_privkey_null()
959 {
960  rsa_context ctx;
961  memset( &ctx, 0x00, sizeof( rsa_context ) );
962 
964 
965 exit:
966  return;
967 }
968 
969 void test_suite_rsa_check_pubkey( int radix_N, char *input_N, int radix_E, char *input_E,
970  int result )
971 {
972  rsa_context ctx;
973 
974  rsa_init( &ctx, RSA_PKCS_V15, 0 );
975 
976  if( strlen( input_N ) )
977  {
978  TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
979  }
980  if( strlen( input_E ) )
981  {
982  TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
983  }
984 
985  TEST_ASSERT( rsa_check_pubkey( &ctx ) == result );
986 
987 exit:
988  rsa_free( &ctx );
989 }
990 
991 void test_suite_rsa_check_privkey( int mod, int radix_P, char *input_P, int radix_Q,
992  char *input_Q, int radix_N, char *input_N,
993  int radix_E, char *input_E, int radix_D, char *input_D,
994  int radix_DP, char *input_DP, int radix_DQ,
995  char *input_DQ, int radix_QP, char *input_QP,
996  int result )
997 {
998  rsa_context ctx;
999 
1000  rsa_init( &ctx, RSA_PKCS_V15, 0 );
1001 
1002  ctx.len = mod / 8;
1003  if( strlen( input_P ) )
1004  {
1005  TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
1006  }
1007  if( strlen( input_Q ) )
1008  {
1009  TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
1010  }
1011  if( strlen( input_N ) )
1012  {
1013  TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
1014  }
1015  if( strlen( input_E ) )
1016  {
1017  TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
1018  }
1019  if( strlen( input_D ) )
1020  {
1021  TEST_ASSERT( mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
1022  }
1023  if( strlen( input_DP ) )
1024  {
1025  TEST_ASSERT( mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
1026  }
1027  if( strlen( input_DQ ) )
1028  {
1029  TEST_ASSERT( mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
1030  }
1031  if( strlen( input_QP ) )
1032  {
1033  TEST_ASSERT( mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
1034  }
1035 
1036  TEST_ASSERT( rsa_check_privkey( &ctx ) == result );
1037 
1038 exit:
1039  rsa_free( &ctx );
1040 }
1041 
1042 #ifdef POLARSSL_CTR_DRBG_C
1043 #ifdef POLARSSL_ENTROPY_C
1044 void test_suite_rsa_gen_key( int nrbits, int exponent, int result)
1045 {
1046  rsa_context ctx;
1047  entropy_context entropy;
1048  ctr_drbg_context ctr_drbg;
1049  const char *pers = "test_suite_rsa";
1050 
1051  entropy_init( &entropy );
1052  TEST_ASSERT( ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
1053  (const unsigned char *) pers, strlen( pers ) ) == 0 );
1054 
1055  rsa_init( &ctx, 0, 0 );
1056 
1057  TEST_ASSERT( rsa_gen_key( &ctx, ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
1058  if( result == 0 )
1059  {
1060  TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
1061  }
1062 
1063 exit:
1064  rsa_free( &ctx );
1065  ctr_drbg_free( &ctr_drbg );
1066  entropy_free( &entropy );
1067 }
1068 #endif /* POLARSSL_CTR_DRBG_C */
1069 #endif /* POLARSSL_ENTROPY_C */
1070 
1071 #ifdef POLARSSL_SELF_TEST
1072 void test_suite_rsa_selftest()
1073 {
1074  TEST_ASSERT( rsa_self_test( 0 ) == 0 );
1075 
1076 exit:
1077  return;
1078 }
1079 #endif /* POLARSSL_SELF_TEST */
1080 
1081 
1082 #endif /* POLARSSL_RSA_C */
1083 #endif /* POLARSSL_BIGNUM_C */
1084 #endif /* POLARSSL_GENPRIME */
1085 
1086 
1087 int dep_check( char *str )
1088 {
1089  if( str == NULL )
1090  return( 1 );
1091 
1092  if( strcmp( str, "POLARSSL_MD4_C" ) == 0 )
1093  {
1094 #if defined(POLARSSL_MD4_C)
1095  return( 0 );
1096 #else
1097  return( 1 );
1098 #endif
1099  }
1100  if( strcmp( str, "POLARSSL_SHA1_C" ) == 0 )
1101  {
1102 #if defined(POLARSSL_SHA1_C)
1103  return( 0 );
1104 #else
1105  return( 1 );
1106 #endif
1107  }
1108  if( strcmp( str, "POLARSSL_MD2_C" ) == 0 )
1109  {
1110 #if defined(POLARSSL_MD2_C)
1111  return( 0 );
1112 #else
1113  return( 1 );
1114 #endif
1115  }
1116  if( strcmp( str, "POLARSSL_SHA512_C" ) == 0 )
1117  {
1118 #if defined(POLARSSL_SHA512_C)
1119  return( 0 );
1120 #else
1121  return( 1 );
1122 #endif
1123  }
1124  if( strcmp( str, "POLARSSL_SHA256_C" ) == 0 )
1125  {
1126 #if defined(POLARSSL_SHA256_C)
1127  return( 0 );
1128 #else
1129  return( 1 );
1130 #endif
1131  }
1132  if( strcmp( str, "POLARSSL_PKCS1_V15" ) == 0 )
1133  {
1134 #if defined(POLARSSL_PKCS1_V15)
1135  return( 0 );
1136 #else
1137  return( 1 );
1138 #endif
1139  }
1140  if( strcmp( str, "POLARSSL_SELF_TEST" ) == 0 )
1141  {
1142 #if defined(POLARSSL_SELF_TEST)
1143  return( 0 );
1144 #else
1145  return( 1 );
1146 #endif
1147  }
1148  if( strcmp( str, "POLARSSL_MD5_C" ) == 0 )
1149  {
1150 #if defined(POLARSSL_MD5_C)
1151  return( 0 );
1152 #else
1153  return( 1 );
1154 #endif
1155  }
1156 
1157 
1158  return( 1 );
1159 }
1160 
1161 int dispatch_test(int cnt, char *params[50])
1162 {
1163  int ret;
1164  ((void) cnt);
1165  ((void) params);
1166 
1167 #if defined(TEST_SUITE_ACTIVE)
1168  if( strcmp( params[0], "rsa_pkcs1_sign" ) == 0 )
1169  {
1170 
1171  char *param1 = params[1];
1172  int param2;
1173  int param3;
1174  int param4;
1175  int param5;
1176  char *param6 = params[6];
1177  int param7;
1178  char *param8 = params[8];
1179  int param9;
1180  char *param10 = params[10];
1181  int param11;
1182  char *param12 = params[12];
1183  char *param13 = params[13];
1184  int param14;
1185 
1186  if( cnt != 15 )
1187  {
1188  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 15 );
1189  return( 2 );
1190  }
1191 
1192  if( verify_string( &param1 ) != 0 ) return( 2 );
1193  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1194  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1195  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1196  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1197  if( verify_string( &param6 ) != 0 ) return( 2 );
1198  if( verify_int( params[7], &param7 ) != 0 ) return( 2 );
1199  if( verify_string( &param8 ) != 0 ) return( 2 );
1200  if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
1201  if( verify_string( &param10 ) != 0 ) return( 2 );
1202  if( verify_int( params[11], &param11 ) != 0 ) return( 2 );
1203  if( verify_string( &param12 ) != 0 ) return( 2 );
1204  if( verify_string( &param13 ) != 0 ) return( 2 );
1205  if( verify_int( params[14], &param14 ) != 0 ) return( 2 );
1206 
1207  test_suite_rsa_pkcs1_sign( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14 );
1208  return ( 0 );
1209 
1210  return ( 3 );
1211  }
1212  else
1213  if( strcmp( params[0], "rsa_pkcs1_verify" ) == 0 )
1214  {
1215 
1216  char *param1 = params[1];
1217  int param2;
1218  int param3;
1219  int param4;
1220  int param5;
1221  char *param6 = params[6];
1222  int param7;
1223  char *param8 = params[8];
1224  char *param9 = params[9];
1225  int param10;
1226 
1227  if( cnt != 11 )
1228  {
1229  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1230  return( 2 );
1231  }
1232 
1233  if( verify_string( &param1 ) != 0 ) return( 2 );
1234  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1235  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1236  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1237  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1238  if( verify_string( &param6 ) != 0 ) return( 2 );
1239  if( verify_int( params[7], &param7 ) != 0 ) return( 2 );
1240  if( verify_string( &param8 ) != 0 ) return( 2 );
1241  if( verify_string( &param9 ) != 0 ) return( 2 );
1242  if( verify_int( params[10], &param10 ) != 0 ) return( 2 );
1243 
1244  test_suite_rsa_pkcs1_verify( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1245  return ( 0 );
1246 
1247  return ( 3 );
1248  }
1249  else
1250  if( strcmp( params[0], "rsa_pkcs1_sign_raw" ) == 0 )
1251  {
1252 
1253  char *param1 = params[1];
1254  char *param2 = params[2];
1255  int param3;
1256  int param4;
1257  int param5;
1258  char *param6 = params[6];
1259  int param7;
1260  char *param8 = params[8];
1261  int param9;
1262  char *param10 = params[10];
1263  int param11;
1264  char *param12 = params[12];
1265  char *param13 = params[13];
1266 
1267  if( cnt != 14 )
1268  {
1269  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 14 );
1270  return( 2 );
1271  }
1272 
1273  if( verify_string( &param1 ) != 0 ) return( 2 );
1274  if( verify_string( &param2 ) != 0 ) return( 2 );
1275  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1276  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1277  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1278  if( verify_string( &param6 ) != 0 ) return( 2 );
1279  if( verify_int( params[7], &param7 ) != 0 ) return( 2 );
1280  if( verify_string( &param8 ) != 0 ) return( 2 );
1281  if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
1282  if( verify_string( &param10 ) != 0 ) return( 2 );
1283  if( verify_int( params[11], &param11 ) != 0 ) return( 2 );
1284  if( verify_string( &param12 ) != 0 ) return( 2 );
1285  if( verify_string( &param13 ) != 0 ) return( 2 );
1286 
1287  test_suite_rsa_pkcs1_sign_raw( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13 );
1288  return ( 0 );
1289 
1290  return ( 3 );
1291  }
1292  else
1293  if( strcmp( params[0], "rsa_pkcs1_verify_raw" ) == 0 )
1294  {
1295 
1296  char *param1 = params[1];
1297  char *param2 = params[2];
1298  int param3;
1299  int param4;
1300  int param5;
1301  char *param6 = params[6];
1302  int param7;
1303  char *param8 = params[8];
1304  char *param9 = params[9];
1305  int param10;
1306 
1307  if( cnt != 11 )
1308  {
1309  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1310  return( 2 );
1311  }
1312 
1313  if( verify_string( &param1 ) != 0 ) return( 2 );
1314  if( verify_string( &param2 ) != 0 ) return( 2 );
1315  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1316  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1317  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1318  if( verify_string( &param6 ) != 0 ) return( 2 );
1319  if( verify_int( params[7], &param7 ) != 0 ) return( 2 );
1320  if( verify_string( &param8 ) != 0 ) return( 2 );
1321  if( verify_string( &param9 ) != 0 ) return( 2 );
1322  if( verify_int( params[10], &param10 ) != 0 ) return( 2 );
1323 
1324  test_suite_rsa_pkcs1_verify_raw( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1325  return ( 0 );
1326 
1327  return ( 3 );
1328  }
1329  else
1330  if( strcmp( params[0], "rsa_pkcs1_encrypt" ) == 0 )
1331  {
1332 
1333  char *param1 = params[1];
1334  int param2;
1335  int param3;
1336  int param4;
1337  char *param5 = params[5];
1338  int param6;
1339  char *param7 = params[7];
1340  char *param8 = params[8];
1341  int param9;
1342 
1343  if( cnt != 10 )
1344  {
1345  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 10 );
1346  return( 2 );
1347  }
1348 
1349  if( verify_string( &param1 ) != 0 ) return( 2 );
1350  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1351  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1352  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1353  if( verify_string( &param5 ) != 0 ) return( 2 );
1354  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
1355  if( verify_string( &param7 ) != 0 ) return( 2 );
1356  if( verify_string( &param8 ) != 0 ) return( 2 );
1357  if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
1358 
1359  test_suite_rsa_pkcs1_encrypt( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
1360  return ( 0 );
1361 
1362  return ( 3 );
1363  }
1364  else
1365  if( strcmp( params[0], "rsa_pkcs1_encrypt_bad_rng" ) == 0 )
1366  {
1367 
1368  char *param1 = params[1];
1369  int param2;
1370  int param3;
1371  int param4;
1372  char *param5 = params[5];
1373  int param6;
1374  char *param7 = params[7];
1375  char *param8 = params[8];
1376  int param9;
1377 
1378  if( cnt != 10 )
1379  {
1380  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 10 );
1381  return( 2 );
1382  }
1383 
1384  if( verify_string( &param1 ) != 0 ) return( 2 );
1385  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1386  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1387  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1388  if( verify_string( &param5 ) != 0 ) return( 2 );
1389  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
1390  if( verify_string( &param7 ) != 0 ) return( 2 );
1391  if( verify_string( &param8 ) != 0 ) return( 2 );
1392  if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
1393 
1394  test_suite_rsa_pkcs1_encrypt_bad_rng( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
1395  return ( 0 );
1396 
1397  return ( 3 );
1398  }
1399  else
1400  if( strcmp( params[0], "rsa_pkcs1_decrypt" ) == 0 )
1401  {
1402 
1403  char *param1 = params[1];
1404  int param2;
1405  int param3;
1406  int param4;
1407  char *param5 = params[5];
1408  int param6;
1409  char *param7 = params[7];
1410  int param8;
1411  char *param9 = params[9];
1412  int param10;
1413  char *param11 = params[11];
1414  int param12;
1415  char *param13 = params[13];
1416  int param14;
1417 
1418  if( cnt != 15 )
1419  {
1420  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 15 );
1421  return( 2 );
1422  }
1423 
1424  if( verify_string( &param1 ) != 0 ) return( 2 );
1425  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1426  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1427  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1428  if( verify_string( &param5 ) != 0 ) return( 2 );
1429  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
1430  if( verify_string( &param7 ) != 0 ) return( 2 );
1431  if( verify_int( params[8], &param8 ) != 0 ) return( 2 );
1432  if( verify_string( &param9 ) != 0 ) return( 2 );
1433  if( verify_int( params[10], &param10 ) != 0 ) return( 2 );
1434  if( verify_string( &param11 ) != 0 ) return( 2 );
1435  if( verify_int( params[12], &param12 ) != 0 ) return( 2 );
1436  if( verify_string( &param13 ) != 0 ) return( 2 );
1437  if( verify_int( params[14], &param14 ) != 0 ) return( 2 );
1438 
1439  test_suite_rsa_pkcs1_decrypt( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14 );
1440  return ( 0 );
1441 
1442  return ( 3 );
1443  }
1444  else
1445  if( strcmp( params[0], "rsa_public" ) == 0 )
1446  {
1447 
1448  char *param1 = params[1];
1449  int param2;
1450  int param3;
1451  char *param4 = params[4];
1452  int param5;
1453  char *param6 = params[6];
1454  char *param7 = params[7];
1455  int param8;
1456 
1457  if( cnt != 9 )
1458  {
1459  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 9 );
1460  return( 2 );
1461  }
1462 
1463  if( verify_string( &param1 ) != 0 ) return( 2 );
1464  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1465  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1466  if( verify_string( &param4 ) != 0 ) return( 2 );
1467  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1468  if( verify_string( &param6 ) != 0 ) return( 2 );
1469  if( verify_string( &param7 ) != 0 ) return( 2 );
1470  if( verify_int( params[8], &param8 ) != 0 ) return( 2 );
1471 
1472  test_suite_rsa_public( param1, param2, param3, param4, param5, param6, param7, param8 );
1473  return ( 0 );
1474 
1475  return ( 3 );
1476  }
1477  else
1478  if( strcmp( params[0], "rsa_private" ) == 0 )
1479  {
1480 
1481  char *param1 = params[1];
1482  int param2;
1483  int param3;
1484  char *param4 = params[4];
1485  int param5;
1486  char *param6 = params[6];
1487  int param7;
1488  char *param8 = params[8];
1489  int param9;
1490  char *param10 = params[10];
1491  char *param11 = params[11];
1492  int param12;
1493 
1494  if( cnt != 13 )
1495  {
1496  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 13 );
1497  return( 2 );
1498  }
1499 
1500  if( verify_string( &param1 ) != 0 ) return( 2 );
1501  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1502  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1503  if( verify_string( &param4 ) != 0 ) return( 2 );
1504  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1505  if( verify_string( &param6 ) != 0 ) return( 2 );
1506  if( verify_int( params[7], &param7 ) != 0 ) return( 2 );
1507  if( verify_string( &param8 ) != 0 ) return( 2 );
1508  if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
1509  if( verify_string( &param10 ) != 0 ) return( 2 );
1510  if( verify_string( &param11 ) != 0 ) return( 2 );
1511  if( verify_int( params[12], &param12 ) != 0 ) return( 2 );
1512 
1513  test_suite_rsa_private( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12 );
1514  return ( 0 );
1515 
1516  return ( 3 );
1517  }
1518  else
1519  if( strcmp( params[0], "rsa_check_privkey_null" ) == 0 )
1520  {
1521 
1522 
1523  if( cnt != 1 )
1524  {
1525  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1526  return( 2 );
1527  }
1528 
1529 
1530  test_suite_rsa_check_privkey_null( );
1531  return ( 0 );
1532 
1533  return ( 3 );
1534  }
1535  else
1536  if( strcmp( params[0], "rsa_check_pubkey" ) == 0 )
1537  {
1538 
1539  int param1;
1540  char *param2 = params[2];
1541  int param3;
1542  char *param4 = params[4];
1543  int param5;
1544 
1545  if( cnt != 6 )
1546  {
1547  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1548  return( 2 );
1549  }
1550 
1551  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1552  if( verify_string( &param2 ) != 0 ) return( 2 );
1553  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1554  if( verify_string( &param4 ) != 0 ) return( 2 );
1555  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1556 
1557  test_suite_rsa_check_pubkey( param1, param2, param3, param4, param5 );
1558  return ( 0 );
1559 
1560  return ( 3 );
1561  }
1562  else
1563  if( strcmp( params[0], "rsa_check_privkey" ) == 0 )
1564  {
1565 
1566  int param1;
1567  int param2;
1568  char *param3 = params[3];
1569  int param4;
1570  char *param5 = params[5];
1571  int param6;
1572  char *param7 = params[7];
1573  int param8;
1574  char *param9 = params[9];
1575  int param10;
1576  char *param11 = params[11];
1577  int param12;
1578  char *param13 = params[13];
1579  int param14;
1580  char *param15 = params[15];
1581  int param16;
1582  char *param17 = params[17];
1583  int param18;
1584 
1585  if( cnt != 19 )
1586  {
1587  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 19 );
1588  return( 2 );
1589  }
1590 
1591  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1592  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1593  if( verify_string( &param3 ) != 0 ) return( 2 );
1594  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1595  if( verify_string( &param5 ) != 0 ) return( 2 );
1596  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
1597  if( verify_string( &param7 ) != 0 ) return( 2 );
1598  if( verify_int( params[8], &param8 ) != 0 ) return( 2 );
1599  if( verify_string( &param9 ) != 0 ) return( 2 );
1600  if( verify_int( params[10], &param10 ) != 0 ) return( 2 );
1601  if( verify_string( &param11 ) != 0 ) return( 2 );
1602  if( verify_int( params[12], &param12 ) != 0 ) return( 2 );
1603  if( verify_string( &param13 ) != 0 ) return( 2 );
1604  if( verify_int( params[14], &param14 ) != 0 ) return( 2 );
1605  if( verify_string( &param15 ) != 0 ) return( 2 );
1606  if( verify_int( params[16], &param16 ) != 0 ) return( 2 );
1607  if( verify_string( &param17 ) != 0 ) return( 2 );
1608  if( verify_int( params[18], &param18 ) != 0 ) return( 2 );
1609 
1610  test_suite_rsa_check_privkey( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, param15, param16, param17, param18 );
1611  return ( 0 );
1612 
1613  return ( 3 );
1614  }
1615  else
1616  if( strcmp( params[0], "rsa_gen_key" ) == 0 )
1617  {
1618  #ifdef POLARSSL_CTR_DRBG_C
1619  #ifdef POLARSSL_ENTROPY_C
1620 
1621  int param1;
1622  int param2;
1623  int param3;
1624 
1625  if( cnt != 4 )
1626  {
1627  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1628  return( 2 );
1629  }
1630 
1631  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1632  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1633  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1634 
1635  test_suite_rsa_gen_key( param1, param2, param3 );
1636  return ( 0 );
1637  #endif /* POLARSSL_CTR_DRBG_C */
1638  #endif /* POLARSSL_ENTROPY_C */
1639 
1640  return ( 3 );
1641  }
1642  else
1643  if( strcmp( params[0], "rsa_selftest" ) == 0 )
1644  {
1645  #ifdef POLARSSL_SELF_TEST
1646 
1647 
1648  if( cnt != 1 )
1649  {
1650  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1651  return( 2 );
1652  }
1653 
1654 
1655  test_suite_rsa_selftest( );
1656  return ( 0 );
1657  #endif /* POLARSSL_SELF_TEST */
1658 
1659  return ( 3 );
1660  }
1661  else
1662 
1663  {
1664  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
1665  fflush( stdout );
1666  return( 1 );
1667  }
1668 #else
1669  return( 3 );
1670 #endif
1671  return( ret );
1672 }
1673 
1674 int get_line( FILE *f, char *buf, size_t len )
1675 {
1676  char *ret;
1677 
1678  ret = fgets( buf, len, f );
1679  if( ret == NULL )
1680  return( -1 );
1681 
1682  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
1683  buf[strlen(buf) - 1] = '\0';
1684  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
1685  buf[strlen(buf) - 1] = '\0';
1686 
1687  return( 0 );
1688 }
1689 
1690 int parse_arguments( char *buf, size_t len, char *params[50] )
1691 {
1692  int cnt = 0, i;
1693  char *cur = buf;
1694  char *p = buf, *q;
1695 
1696  params[cnt++] = cur;
1697 
1698  while( *p != '\0' && p < buf + len )
1699  {
1700  if( *p == '\\' )
1701  {
1702  p++;
1703  p++;
1704  continue;
1705  }
1706  if( *p == ':' )
1707  {
1708  if( p + 1 < buf + len )
1709  {
1710  cur = p + 1;
1711  params[cnt++] = cur;
1712  }
1713  *p = '\0';
1714  }
1715 
1716  p++;
1717  }
1718 
1719  // Replace newlines, question marks and colons in strings
1720  for( i = 0; i < cnt; i++ )
1721  {
1722  p = params[i];
1723  q = params[i];
1724 
1725  while( *p != '\0' )
1726  {
1727  if( *p == '\\' && *(p + 1) == 'n' )
1728  {
1729  p += 2;
1730  *(q++) = '\n';
1731  }
1732  else if( *p == '\\' && *(p + 1) == ':' )
1733  {
1734  p += 2;
1735  *(q++) = ':';
1736  }
1737  else if( *p == '\\' && *(p + 1) == '?' )
1738  {
1739  p += 2;
1740  *(q++) = '?';
1741  }
1742  else
1743  *(q++) = *(p++);
1744  }
1745  *q = '\0';
1746  }
1747 
1748  return( cnt );
1749 }
1750 
1751 int main()
1752 {
1753  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1754  const char *filename = "/tmp/B.43ee1cd1-d3e3-4a1b-8449-a820e9bbf54f/BUILD/polarssl-1.3.8/tests/suites/test_suite_rsa.data";
1755  FILE *file;
1756  char buf[5000];
1757  char *params[50];
1758 
1759 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1760  unsigned char alloc_buf[1000000];
1761  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1762 #endif
1763 
1764  file = fopen( filename, "r" );
1765  if( file == NULL )
1766  {
1767  fprintf( stderr, "Failed to open\n" );
1768  return( 1 );
1769  }
1770 
1771  while( !feof( file ) )
1772  {
1773  int skip = 0;
1774 
1775  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1776  break;
1777  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1778  fprintf( stdout, " " );
1779  for( i = strlen( buf ) + 1; i < 67; i++ )
1780  fprintf( stdout, "." );
1781  fprintf( stdout, " " );
1782  fflush( stdout );
1783 
1784  total_tests++;
1785 
1786  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1787  break;
1788  cnt = parse_arguments( buf, strlen(buf), params );
1789 
1790  if( strcmp( params[0], "depends_on" ) == 0 )
1791  {
1792  for( i = 1; i < cnt; i++ )
1793  if( dep_check( params[i] ) != 0 )
1794  skip = 1;
1795 
1796  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1797  break;
1798  cnt = parse_arguments( buf, strlen(buf), params );
1799  }
1800 
1801  if( skip == 0 )
1802  {
1803  test_errors = 0;
1804  ret = dispatch_test( cnt, params );
1805  }
1806 
1807  if( skip == 1 || ret == 3 )
1808  {
1809  total_skipped++;
1810  fprintf( stdout, "----\n" );
1811  fflush( stdout );
1812  }
1813  else if( ret == 0 && test_errors == 0 )
1814  {
1815  fprintf( stdout, "PASS\n" );
1816  fflush( stdout );
1817  }
1818  else if( ret == 2 )
1819  {
1820  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1821  fclose(file);
1822  exit( 2 );
1823  }
1824  else
1825  total_errors++;
1826 
1827  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1828  break;
1829  if( strlen(buf) != 0 )
1830  {
1831  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1832  return( 1 );
1833  }
1834  }
1835  fclose(file);
1836 
1837  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1838  if( total_errors == 0 )
1839  fprintf( stdout, "PASSED" );
1840  else
1841  fprintf( stdout, "FAILED" );
1842 
1843  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1844  total_tests - total_errors, total_tests, total_skipped );
1845 
1846 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1847 #if defined(POLARSSL_MEMORY_DEBUG)
1848  memory_buffer_alloc_status();
1849 #endif
1851 #endif
1852 
1853  return( total_errors != 0 );
1854 }
1855 
1856 
int md(const md_info_t *md_info, const unsigned char *input, size_t ilen, unsigned char *output)
Output = message_digest( input buffer )
#define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE
The output buffer for decryption is not large enough.
Definition: rsa.h:53
static int unhexify(unsigned char *obuf, const char *ibuf)
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
int ctr_drbg_random(void *p_rng, unsigned char *output, size_t output_len)
CTR_DRBG generate random.
static int test_errors
int rsa_self_test(int verbose)
Checkup routine.
Memory allocation layer (Deprecated to platform layer)
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
int rsa_copy(rsa_context *dst, const rsa_context *src)
Copy the components of an RSA context.
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.
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
int rsa_rsaes_pkcs1_v15_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)
Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
#define RSA_PUBLIC
Definition: rsa.h:59
mpi DQ
Definition: rsa.h:95
Configuration options (set of defines)
int rsa_check_pubkey(const rsa_context *ctx)
Check a public RSA key.
void ctr_drbg_free(ctr_drbg_context *ctx)
Clear CTR_CRBG context data.
Entropy context structure.
Definition: entropy.h:121
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.
#define POLARSSL_ERR_RSA_RNG_FAILED
The random generator failed to generate non-zeros.
Definition: rsa.h:54
MPI structure.
Definition: bignum.h:182
PolarSSL Platform abstraction layer.
int parse_arguments(char *buf, size_t len, char *params[50])
static int test_assert(int correct, const char *test)
Entropy accumulator implementation.
void mpi_init(mpi *X)
Initialize one MPI.
size_t len
Definition: rsa.h:86
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
mpi P
Definition: rsa.h:92
int rsa_rsaes_pkcs1_v15_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)
Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
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.
int dep_check(char *str)
void rsa_free(rsa_context *ctx)
Free the components of an RSA key.
int rsa_private(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, const unsigned char *input, unsigned char *output)
Do an RSA private key operation.
RSA context structure.
Definition: rsa.h:83
mpi D
Definition: rsa.h:91
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.
#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
#define RSA_PRIVATE
Definition: rsa.h:60
mpi N
Definition: rsa.h:88
#define PUT_UINT32_BE(n, b, i)
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.
int main()
mpi E
Definition: rsa.h:89
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:94
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
#define POLARSSL_ERR_RSA_VERIFY_FAILED
The PKCS#1 verification failed.
Definition: rsa.h:52
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
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.
CTR_DRBG context structure.
Definition: ctr_drbg.h:89
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
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:46
#define polarssl_malloc
SHA-1 cryptographic hash function.
#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED
Key failed to pass the libraries validity check.
Definition: rsa.h:49
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.
unsigned char * buf
SHA-384 and SHA-512 cryptographic hash function.
int get_line(FILE *f, char *buf, size_t len)
int ctr_drbg_init(ctr_drbg_context *ctx, int(*f_entropy)(void *, unsigned char *, size_t), void *p_entropy, const unsigned char *custom, size_t len)
CTR_DRBG initialization.
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)
void entropy_init(entropy_context *ctx)
Initialize the context.
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 entropy_func(void *data, unsigned char *output, size_t len)
Retrieve entropy from the accumulator (Maximum length: ENTROPY_BLOCK_SIZE) (Thread-safe if POLARSSL_T...
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
void entropy_free(entropy_context *ctx)
Free the data in the context.
CTR_DRBG based on AES-256 (NIST SP 800-90)
int rsa_public(rsa_context *ctx, const unsigned char *input, unsigned char *output)
Do an RSA public key operation.