3 #ifdef POLARSSL_PKCS1_V21
5 #ifdef POLARSSL_BIGNUM_C
7 #ifdef POLARSSL_GENPRIME
24 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
28 #if defined(WANT_NOT_RND_MPI)
29 #if defined(POLARSSL_BIGNUM_C)
32 #error "not_rnd_mpi() need bignum.c"
38 typedef UINT32 uint32_t;
51 #define GET_UINT32_BE(n,b,i) \
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] ); \
61 #define PUT_UINT32_BE(n,b,i) \
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) ); \
70 static int unhexify(
unsigned char *obuf,
const char *ibuf)
73 int len = strlen(ibuf) / 2;
74 assert(!(strlen(ibuf) %1));
79 if( c >=
'0' && c <=
'9' )
81 else if( c >=
'a' && c <=
'f' )
83 else if( c >=
'A' && c <=
'F' )
89 if( c2 >=
'0' && c2 <=
'9' )
91 else if( c2 >=
'a' && c2 <=
'f' )
93 else if( c2 >=
'A' && c2 <=
'F' )
98 *obuf++ = ( c << 4 ) | c2;
104 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
116 *obuf++ =
'a' + h - 10;
121 *obuf++ =
'a' + l - 10;
137 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
141 if( rng_state != NULL )
144 for( i = 0; i < len; ++i )
155 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
157 if( rng_state != NULL )
160 memset( output, 0, len );
187 if( rng_state == NULL )
196 memcpy( output, info->
buf, use_len );
197 info->
buf += use_len;
201 if( len - use_len > 0 )
202 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
231 uint32_t i, *k, sum, delta=0x9E3779B9;
232 unsigned char result[4];
234 if( rng_state == NULL )
241 size_t use_len = ( len > 4 ) ? 4 : len;
244 for( i = 0; i < 32; i++ )
246 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
248 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
252 memcpy( output, result, use_len );
259 #if defined(WANT_NOT_RND_MPI)
268 #define ciL (sizeof(t_uint))
269 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
270 static int not_rnd_mpi(
void *in,
unsigned char *out,
size_t len )
272 char *str = (
char *) in;
281 X.
n = CHARS_TO_LIMBS( len );
287 assert( strlen( str ) / 2 == len );
299 #ifdef POLARSSL_PKCS1_V21
300 #ifdef POLARSSL_RSA_C
301 #ifdef POLARSSL_BIGNUM_C
302 #ifdef POLARSSL_SHA1_C
303 #ifdef POLARSSL_GENPRIME
305 #define TEST_SUITE_ACTIVE
313 if( test_errors == 1 )
314 printf(
"FAILED\n" );
315 printf(
" %s\n", test );
320 #define TEST_ASSERT( TEST ) \
321 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
322 if( test_errors) return; \
327 if( (*str)[0] !=
'"' ||
328 (*str)[strlen( *str ) - 1] !=
'"' )
330 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
335 (*str)[strlen( *str ) - 1] =
'\0';
347 for( i = 0; i < strlen( str ); i++ )
349 if( i == 0 && str[i] ==
'-' )
355 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
356 str[i - 1] ==
'0' && str[i] ==
'x' )
362 if( str[i] <
'0' || str[i] >
'9' )
372 *value = strtol( str, NULL, 16 );
374 *value = strtol( str, NULL, 10 );
379 if( strcmp( str,
"POLARSSL_MD_SHA512" ) == 0 )
384 if( strcmp( str,
"POLARSSL_MD_SHA1" ) == 0 )
389 if( strcmp( str,
"POLARSSL_ERR_RSA_BAD_INPUT_DATA" ) == 0 )
396 printf(
"Expected integer for parameter and got: %s\n", str );
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 )
405 unsigned char message_str[1000];
406 unsigned char output[1000];
407 unsigned char output_str[1000];
408 unsigned char rnd_buf[1000];
417 memset( message_str, 0x00, 1000 );
418 memset( output, 0x00, 1000 );
419 memset( output_str, 0x00, 1000 );
421 ctx.
len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 );
427 msg_len =
unhexify( message_str, message_hex_string );
434 TEST_ASSERT( strcasecmp( (
char *) output_str, result_hex_str ) == 0 );
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 )
446 unsigned char message_str[1000];
447 unsigned char output[1000];
448 unsigned char output_str[1000];
458 memset( message_str, 0x00, 1000 );
459 memset( output, 0x00, 1000 );
460 memset( output_str, 0x00, 1000 );
463 ctx.
len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 );
480 unhexify( message_str, message_hex_string );
487 TEST_ASSERT( strncasecmp( (
char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 );
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 )
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];
516 memset( message_str, 0x00, 1000 );
517 memset( hash_result, 0x00, 1000 );
518 memset( output, 0x00, 1000 );
519 memset( output_str, 0x00, 1000 );
521 ctx.
len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 );
538 msg_len =
unhexify( message_str, message_hex_string );
548 TEST_ASSERT( strcasecmp( (
char *) output_str, result_hex_str ) == 0 );
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 )
560 unsigned char message_str[1000];
561 unsigned char hash_result[1000];
562 unsigned char result_str[1000];
568 memset( message_str, 0x00, 1000 );
569 memset( hash_result, 0x00, 1000 );
570 memset( result_str, 0x00, 1000 );
572 ctx.
len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 );
578 msg_len =
unhexify( message_str, message_hex_string );
579 unhexify( result_str, result_hex_str );
613 #if defined(TEST_SUITE_ACTIVE)
614 if( strcmp( params[0],
"pkcs1_rsaes_oaep_encrypt" ) == 0 )
619 char *param3 = params[3];
621 char *param5 = params[5];
623 char *param7 = params[7];
624 char *param8 = params[8];
625 char *param9 = params[9];
630 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 11 );
634 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
635 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
637 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
639 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
643 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
645 test_suite_pkcs1_rsaes_oaep_encrypt( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
651 if( strcmp( params[0],
"pkcs1_rsaes_oaep_decrypt" ) == 0 )
656 char *param3 = params[3];
658 char *param5 = params[5];
660 char *param7 = params[7];
662 char *param9 = params[9];
664 char *param11 = params[11];
665 char *param12 = params[12];
666 char *param13 = params[13];
671 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 15 );
675 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
676 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
678 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
680 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
682 if(
verify_int( params[8], ¶m8 ) != 0 )
return( 2 );
684 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
688 if(
verify_int( params[14], ¶m14 ) != 0 )
return( 2 );
690 test_suite_pkcs1_rsaes_oaep_decrypt( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14 );
696 if( strcmp( params[0],
"pkcs1_rsassa_pss_sign" ) == 0 )
701 char *param3 = params[3];
703 char *param5 = params[5];
705 char *param7 = params[7];
707 char *param9 = params[9];
710 char *param12 = params[12];
711 char *param13 = params[13];
712 char *param14 = params[14];
717 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 16 );
721 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
722 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
724 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
726 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
728 if(
verify_int( params[8], ¶m8 ) != 0 )
return( 2 );
730 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
731 if(
verify_int( params[11], ¶m11 ) != 0 )
return( 2 );
735 if(
verify_int( params[15], ¶m15 ) != 0 )
return( 2 );
737 test_suite_pkcs1_rsassa_pss_sign( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, param15 );
743 if( strcmp( params[0],
"pkcs1_rsassa_pss_verify" ) == 0 )
748 char *param3 = params[3];
750 char *param5 = params[5];
753 char *param8 = params[8];
754 char *param9 = params[9];
755 char *param10 = params[10];
760 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 12 );
764 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
765 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
767 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
769 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
770 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
774 if(
verify_int( params[11], ¶m11 ) != 0 )
return( 2 );
776 test_suite_pkcs1_rsassa_pss_verify( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11 );
784 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
798 ret = fgets( buf, len, f );
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';
818 while( *p !=
'\0' && p < buf + len )
828 if( p + 1 < buf + len )
840 for( i = 0; i < cnt; i++ )
847 if( *p ==
'\\' && *(p + 1) ==
'n' )
852 else if( *p ==
'\\' && *(p + 1) ==
':' )
857 else if( *p ==
'\\' && *(p + 1) ==
'?' )
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";
879 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
880 unsigned char alloc_buf[1000000];
881 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
884 file = fopen( filename,
"r" );
887 fprintf( stderr,
"Failed to open\n" );
891 while( !feof( file ) )
895 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
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,
" " );
906 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
910 if( strcmp( params[0],
"depends_on" ) == 0 )
912 for( i = 1; i < cnt; i++ )
916 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
927 if( skip == 1 || ret == 3 )
930 fprintf( stdout,
"----\n" );
933 else if( ret == 0 && test_errors == 0 )
935 fprintf( stdout,
"PASS\n" );
940 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
947 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
949 if( strlen(buf) != 0 )
951 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
957 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
958 if( total_errors == 0 )
959 fprintf( stdout,
"PASSED" );
961 fprintf( stdout,
"FAILED" );
963 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
964 total_tests - total_errors, total_tests, total_skipped );
966 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
967 #if defined(POLARSSL_MEMORY_DEBUG)
968 memory_buffer_alloc_status();
970 memory_buffer_alloc_free();
973 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[])
Multi-precision integer library.
#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.
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 subtraction: X = A - b.
MD2 message digest algorithm (hash function)
int get_line(FILE *f, char *buf, size_t len)