3 #ifdef POLARSSL_PKCS1_V21
5 #ifdef POLARSSL_BIGNUM_C
7 #ifdef POLARSSL_GENPRIME
24 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
30 typedef UINT32 uint32_t;
43 #define GET_UINT32_BE(n,b,i) \
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] ); \
53 #define PUT_UINT32_BE(n,b,i) \
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) ); \
62 static int unhexify(
unsigned char *obuf,
const char *ibuf)
65 int len = strlen(ibuf) / 2;
66 assert(!(strlen(ibuf) %1));
71 if( c >=
'0' && c <=
'9' )
73 else if( c >=
'a' && c <=
'f' )
75 else if( c >=
'A' && c <=
'F' )
81 if( c2 >=
'0' && c2 <=
'9' )
83 else if( c2 >=
'a' && c2 <=
'f' )
85 else if( c2 >=
'A' && c2 <=
'F' )
90 *obuf++ = ( c << 4 ) | c2;
96 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
108 *obuf++ =
'a' + h - 10;
113 *obuf++ =
'a' + l - 10;
129 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
133 if( rng_state != NULL )
136 for( i = 0; i < len; ++i )
147 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
149 if( rng_state != NULL )
152 memset( output, 0, len );
179 if( rng_state == NULL )
188 memcpy( output, info->
buf, use_len );
189 info->
buf += use_len;
193 if( len - use_len > 0 )
194 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
223 uint32_t i, *k, sum, delta=0x9E3779B9;
224 unsigned char result[4];
226 if( rng_state == NULL )
233 size_t use_len = ( len > 4 ) ? 4 : len;
236 for( i = 0; i < 32; i++ )
238 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
240 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
244 memcpy( output, result, use_len );
260 static int not_rnd(
void *in,
unsigned char *out,
size_t len )
263 const char *ibuf = in;
265 assert( len == strlen(ibuf) / 2 );
266 assert(!(strlen(ibuf) %1));
268 obuf = out + (len - 1);
272 if( c >=
'0' && c <=
'9' )
274 else if( c >=
'a' && c <=
'f' )
276 else if( c >=
'A' && c <=
'F' )
282 if( c2 >=
'0' && c2 <=
'9' )
284 else if( c2 >=
'a' && c2 <=
'f' )
286 else if( c2 >=
'A' && c2 <=
'F' )
291 *obuf-- = ( c << 4 ) | c2;
303 #ifdef POLARSSL_PKCS1_V21
304 #ifdef POLARSSL_RSA_C
305 #ifdef POLARSSL_BIGNUM_C
306 #ifdef POLARSSL_SHA1_C
307 #ifdef POLARSSL_GENPRIME
309 #define TEST_SUITE_ACTIVE
318 printf(
"FAILED\n" );
319 printf(
" %s\n", test );
324 #define TEST_ASSERT( TEST ) \
325 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
326 if( test_errors) return; \
331 if( (*str)[0] !=
'"' ||
332 (*str)[strlen( *str ) - 1] !=
'"' )
334 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
339 (*str)[strlen( *str ) - 1] =
'\0';
351 for( i = 0; i < strlen( str ); i++ )
353 if( i == 0 && str[i] ==
'-' )
359 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
360 str[i - 1] ==
'0' && str[i] ==
'x' )
366 if( str[i] <
'0' || str[i] >
'9' )
376 *value = strtol( str, NULL, 16 );
378 *value = strtol( str, NULL, 10 );
383 if( strcmp( str,
"POLARSSL_ERR_RSA_BAD_INPUT_DATA" ) == 0 )
388 if( strcmp( str,
"POLARSSL_MD_SHA512" ) == 0 )
393 if( strcmp( str,
"POLARSSL_MD_SHA1" ) == 0 )
400 printf(
"Expected integer for parameter and got: %s\n", str );
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 )
409 unsigned char message_str[1000];
410 unsigned char output[1000];
411 unsigned char output_str[1000];
412 unsigned char rnd_buf[1000];
421 memset( message_str, 0x00, 1000 );
422 memset( output, 0x00, 1000 );
423 memset( output_str, 0x00, 1000 );
425 ctx.
len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 );
431 msg_len =
unhexify( message_str, message_hex_string );
438 TEST_ASSERT( strcasecmp( (
char *) output_str, result_hex_str ) == 0 );
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 )
450 unsigned char message_str[1000];
451 unsigned char output[1000];
452 unsigned char output_str[1000];
462 memset( message_str, 0x00, 1000 );
463 memset( output, 0x00, 1000 );
464 memset( output_str, 0x00, 1000 );
467 ctx.
len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 );
484 unhexify( message_str, message_hex_string );
491 TEST_ASSERT( strncasecmp( (
char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 );
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 )
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];
520 memset( message_str, 0x00, 1000 );
521 memset( hash_result, 0x00, 1000 );
522 memset( output, 0x00, 1000 );
523 memset( output_str, 0x00, 1000 );
525 ctx.
len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 );
542 msg_len =
unhexify( message_str, message_hex_string );
552 TEST_ASSERT( strcasecmp( (
char *) output_str, result_hex_str ) == 0 );
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 )
564 unsigned char message_str[1000];
565 unsigned char hash_result[1000];
566 unsigned char result_str[1000];
572 memset( message_str, 0x00, 1000 );
573 memset( hash_result, 0x00, 1000 );
574 memset( result_str, 0x00, 1000 );
576 ctx.
len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 );
582 msg_len =
unhexify( message_str, message_hex_string );
583 unhexify( result_str, result_hex_str );
617 #if defined(TEST_SUITE_ACTIVE)
618 if( strcmp( params[0],
"pkcs1_rsaes_oaep_encrypt" ) == 0 )
623 char *param3 = params[3];
625 char *param5 = params[5];
627 char *param7 = params[7];
628 char *param8 = params[8];
629 char *param9 = params[9];
634 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 11 );
638 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
639 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
641 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
643 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
647 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
649 test_suite_pkcs1_rsaes_oaep_encrypt( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
655 if( strcmp( params[0],
"pkcs1_rsaes_oaep_decrypt" ) == 0 )
660 char *param3 = params[3];
662 char *param5 = params[5];
664 char *param7 = params[7];
666 char *param9 = params[9];
668 char *param11 = params[11];
669 char *param12 = params[12];
670 char *param13 = params[13];
675 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 15 );
679 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
680 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
682 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
684 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
686 if(
verify_int( params[8], ¶m8 ) != 0 )
return( 2 );
688 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
692 if(
verify_int( params[14], ¶m14 ) != 0 )
return( 2 );
694 test_suite_pkcs1_rsaes_oaep_decrypt( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14 );
700 if( strcmp( params[0],
"pkcs1_rsassa_pss_sign" ) == 0 )
705 char *param3 = params[3];
707 char *param5 = params[5];
709 char *param7 = params[7];
711 char *param9 = params[9];
714 char *param12 = params[12];
715 char *param13 = params[13];
716 char *param14 = params[14];
721 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 16 );
725 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
726 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
728 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
730 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
732 if(
verify_int( params[8], ¶m8 ) != 0 )
return( 2 );
734 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
735 if(
verify_int( params[11], ¶m11 ) != 0 )
return( 2 );
739 if(
verify_int( params[15], ¶m15 ) != 0 )
return( 2 );
741 test_suite_pkcs1_rsassa_pss_sign( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, param15 );
747 if( strcmp( params[0],
"pkcs1_rsassa_pss_verify" ) == 0 )
752 char *param3 = params[3];
754 char *param5 = params[5];
757 char *param8 = params[8];
758 char *param9 = params[9];
759 char *param10 = params[10];
764 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 12 );
768 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
769 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
771 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
773 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
774 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
778 if(
verify_int( params[11], ¶m11 ) != 0 )
return( 2 );
780 test_suite_pkcs1_rsassa_pss_verify( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11 );
788 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
802 ret = fgets( buf, len, f );
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';
822 while( *p !=
'\0' && p < buf + len )
832 if( p + 1 < buf + len )
844 for( i = 0; i < cnt; i++ )
851 if( *p ==
'\\' && *(p + 1) ==
'n' )
856 else if( *p ==
'\\' && *(p + 1) ==
':' )
861 else if( *p ==
'\\' && *(p + 1) ==
'?' )
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";
883 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
884 unsigned char alloc_buf[1000000];
885 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
888 file = fopen( filename,
"r" );
891 fprintf( stderr,
"Failed to open\n" );
895 while( !feof( file ) )
899 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
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,
" " );
910 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
914 if( strcmp( params[0],
"depends_on" ) == 0 )
916 for( i = 1; i < cnt; i++ )
920 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
931 if( skip == 1 || ret == 3 )
934 fprintf( stdout,
"----\n" );
939 fprintf( stdout,
"PASS\n" );
944 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
951 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
953 if( strlen(buf) != 0 )
955 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
961 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
962 if( total_errors == 0 )
963 fprintf( stdout,
"PASSED" );
965 fprintf( stdout,
"FAILED" );
967 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
968 total_tests - total_errors, total_tests, total_skipped );
970 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
971 #if defined(POLARSSL_MEMORY_DEBUG)
972 memory_buffer_alloc_status();
974 memory_buffer_alloc_free();
977 return( total_errors != 0 );
int md(const md_info_t *md_info, const unsigned char *input, size_t ilen, unsigned char *output)
Output = message_digest( input buffer )
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)
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.
static int test_assert(int correct, char *test)
void mpi_init(mpi *X)
Initialize one MPI.
int main(int argc, char *argv[])
#define TEST_ASSERT(TEST)
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
void rsa_free(rsa_context *ctx)
Free the components of an RSA key.
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.
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.
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. ...
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.
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.
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)