7 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len );
11 #if defined(POLARSSL_RSA_C)
15 #if defined(POLARSSL_ECP_C)
34 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
38 #if defined(WANT_NOT_RND_MPI)
39 #if defined(POLARSSL_BIGNUM_C)
42 #error "not_rnd_mpi() need bignum.c"
48 typedef UINT32 uint32_t;
61 #define GET_UINT32_BE(n,b,i) \
63 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
64 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
65 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
66 | ( (uint32_t) (b)[(i) + 3] ); \
71 #define PUT_UINT32_BE(n,b,i) \
73 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
74 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
75 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
76 (b)[(i) + 3] = (unsigned char) ( (n) ); \
80 static int unhexify(
unsigned char *obuf,
const char *ibuf)
83 int len = strlen(ibuf) / 2;
84 assert(!(strlen(ibuf) %1));
89 if( c >=
'0' && c <=
'9' )
91 else if( c >=
'a' && c <=
'f' )
93 else if( c >=
'A' && c <=
'F' )
99 if( c2 >=
'0' && c2 <=
'9' )
101 else if( c2 >=
'a' && c2 <=
'f' )
103 else if( c2 >=
'A' && c2 <=
'F' )
108 *obuf++ = ( c << 4 ) | c2;
114 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
126 *obuf++ =
'a' + h - 10;
131 *obuf++ =
'a' + l - 10;
147 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
151 if( rng_state != NULL )
154 for( i = 0; i < len; ++i )
165 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
167 if( rng_state != NULL )
170 memset( output, 0, len );
197 if( rng_state == NULL )
206 memcpy( output, info->
buf, use_len );
207 info->
buf += use_len;
211 if( len - use_len > 0 )
212 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
241 uint32_t i, *k, sum, delta=0x9E3779B9;
242 unsigned char result[4];
244 if( rng_state == NULL )
251 size_t use_len = ( len > 4 ) ? 4 : len;
254 for( i = 0; i < 32; i++ )
256 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
258 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
262 memcpy( output, result, use_len );
269 #if defined(WANT_NOT_RND_MPI)
278 #define ciL (sizeof(t_uint))
279 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
280 static int not_rnd_mpi(
void *in,
unsigned char *out,
size_t len )
282 char *str = (
char *) in;
291 X.
n = CHARS_TO_LIMBS( len );
297 assert( strlen( str ) / 2 == len );
311 #define TEST_SUITE_ACTIVE
319 if( test_errors == 1 )
320 printf(
"FAILED\n" );
321 printf(
" %s\n", test );
326 #define TEST_ASSERT( TEST ) \
327 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
328 if( test_errors) return; \
333 if( (*str)[0] !=
'"' ||
334 (*str)[strlen( *str ) - 1] !=
'"' )
336 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
341 (*str)[strlen( *str ) - 1] =
'\0';
353 for( i = 0; i < strlen( str ); i++ )
355 if( i == 0 && str[i] ==
'-' )
361 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
362 str[i - 1] ==
'0' && str[i] ==
'x' )
368 if( str[i] <
'0' || str[i] >
'9' )
378 *value = strtol( str, NULL, 16 );
380 *value = strtol( str, NULL, 10 );
385 #ifdef POLARSSL_RSA_C
386 if( strcmp( str,
"POLARSSL_ERR_RSA_VERIFY_FAILED" ) == 0 )
391 #endif // POLARSSL_RSA_C
392 if( strcmp( str,
"POLARSSL_PK_RSA" ) == 0 )
397 if( strcmp( str,
"POLARSSL_ERR_PK_TYPE_MISMATCH" ) == 0 )
402 #ifdef POLARSSL_RSA_C
403 if( strcmp( str,
"POLARSSL_ERR_RSA_INVALID_PADDING" ) == 0 )
408 #endif // POLARSSL_RSA_C
409 if( strcmp( str,
"POLARSSL_PK_ECKEY" ) == 0 )
414 if( strcmp( str,
"POLARSSL_PK_ECDSA" ) == 0 )
419 #ifdef POLARSSL_ECDSA_C
420 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1" ) == 0 )
425 #endif // POLARSSL_ECDSA_C
426 #ifdef POLARSSL_RSA_C
427 if( strcmp( str,
"POLARSSL_MD_SHA1" ) == 0 )
432 #endif // POLARSSL_RSA_C
433 if( strcmp( str,
"POLARSSL_PK_ECKEY_DH" ) == 0 )
438 #ifdef POLARSSL_ECDSA_C
439 if( strcmp( str,
"POLARSSL_ERR_ECP_VERIFY_FAILED" ) == 0 )
444 #endif // POLARSSL_ECDSA_C
447 printf(
"Expected integer for parameter and got: %s\n", str );
451 void test_suite_pk_utils(
int type,
int size,
int len,
char *name )
469 #ifdef POLARSSL_RSA_C
470 void test_suite_pk_rsa_verify_test_vec(
char *message_hex_string,
int digest,
471 int mod,
int radix_N,
char *input_N,
int radix_E,
472 char *input_E,
char *result_hex_str,
int result )
474 unsigned char message_str[1000];
475 unsigned char hash_result[1000];
476 unsigned char result_str[1000];
483 memset( message_str, 0x00, 1000 );
484 memset( hash_result, 0x00, 1000 );
485 memset( result_str, 0x00, 1000 );
494 msg_len =
unhexify( message_str, message_hex_string );
495 unhexify( result_str, result_hex_str );
507 #ifdef POLARSSL_ECDSA_C
508 void test_suite_pk_ec_test_vec(
int type,
int id,
char *key_str,
509 char *hash_str,
char * sig_str,
int ret )
513 unsigned char hash[100], sig[500], key[500];
514 size_t hash_len, sig_len, key_len;
518 memset( hash, 0,
sizeof( hash ) ); hash_len =
unhexify(hash, hash_str);
519 memset( sig, 0,
sizeof( sig ) ); sig_len =
unhexify(sig, sig_str);
520 memset( key, 0,
sizeof( key ) ); key_len =
unhexify(key, key_str);
529 key, key_len ) == 0 );
532 hash, hash_len, sig, sig_len ) == ret );
538 void test_suite_pk_sign_verify(
int type,
int sign_ret,
int verify_ret )
541 unsigned char hash[50], sig[5000];
546 memset( hash, 0x2a,
sizeof hash );
547 memset( sig, 0,
sizeof sig );
556 hash,
sizeof hash, sig, sig_len ) == verify_ret );
561 #ifdef POLARSSL_RSA_C
562 void test_suite_pk_rsa_encrypt_test_vec(
char *message_hex,
int mod,
563 int radix_N,
char *input_N,
564 int radix_E,
char *input_E,
565 char *result_hex,
int ret )
567 unsigned char message[1000];
568 unsigned char output[1000];
569 unsigned char result[1000];
570 size_t msg_len, olen, res_len;
576 memset( message, 0,
sizeof( message ) );
577 memset( output, 0,
sizeof( output ) );
578 memset( result, 0,
sizeof( result ) );
580 msg_len =
unhexify( message, message_hex );
581 res_len =
unhexify( result, result_hex );
592 output, &olen,
sizeof( output ),
595 TEST_ASSERT( memcmp( output, result, olen ) == 0 );
601 #ifdef POLARSSL_RSA_C
602 void test_suite_pk_rsa_decrypt_test_vec(
char *cipher_hex,
int mod,
603 int radix_P,
char *input_P,
604 int radix_Q,
char *input_Q,
605 int radix_N,
char *input_N,
606 int radix_E,
char *input_E,
607 char *clear_hex,
int ret )
609 unsigned char clear[1000];
610 unsigned char output[1000];
611 unsigned char cipher[1000];
612 size_t clear_len, olen, cipher_len;
622 memset( clear, 0,
sizeof( clear ) );
623 memset( cipher, 0,
sizeof( cipher ) );
625 clear_len =
unhexify( clear, clear_hex );
626 cipher_len =
unhexify( cipher, cipher_hex );
650 memset( output, 0,
sizeof( output ) );
653 output, &olen,
sizeof( output ),
666 void test_suite_pk_ec_nocrypt(
int type )
669 unsigned char output[100];
670 unsigned char input[100];
678 memset( output, 0,
sizeof( output ) );
679 memset( input, 0,
sizeof( input ) );
684 output, &olen,
sizeof( output ),
688 output, &olen,
sizeof( output ),
703 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
705 #if defined(POLARSSL_SHA1_C)
711 if( strcmp( str,
"POLARSSL_ECP_C" ) == 0 )
713 #if defined(POLARSSL_ECP_C)
719 if( strcmp( str,
"POLARSSL_ECDSA_C" ) == 0 )
721 #if defined(POLARSSL_ECDSA_C)
727 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
729 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
735 if( strcmp( str,
"POLARSSL_PKCS1_V15" ) == 0 )
737 #if defined(POLARSSL_PKCS1_V15)
743 if( strcmp( str,
"POLARSSL_RSA_C" ) == 0 )
745 #if defined(POLARSSL_RSA_C)
762 #if defined(TEST_SUITE_ACTIVE)
763 if( strcmp( params[0],
"pk_utils" ) == 0 )
769 char *param4 = params[4];
773 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
777 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
778 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
779 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
782 test_suite_pk_utils( param1, param2, param3, param4 );
788 if( strcmp( params[0],
"pk_rsa_verify_test_vec" ) == 0 )
790 #ifdef POLARSSL_RSA_C
792 char *param1 = params[1];
796 char *param5 = params[5];
798 char *param7 = params[7];
799 char *param8 = params[8];
804 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 10 );
809 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
810 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
811 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
813 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
816 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
818 test_suite_pk_rsa_verify_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
825 if( strcmp( params[0],
"pk_ec_test_vec" ) == 0 )
827 #ifdef POLARSSL_ECDSA_C
831 char *param3 = params[3];
832 char *param4 = params[4];
833 char *param5 = params[5];
838 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
842 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
843 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
847 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
849 test_suite_pk_ec_test_vec( param1, param2, param3, param4, param5, param6 );
856 if( strcmp( params[0],
"pk_sign_verify" ) == 0 )
865 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
869 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
870 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
871 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
873 test_suite_pk_sign_verify( param1, param2, param3 );
879 if( strcmp( params[0],
"pk_rsa_encrypt_test_vec" ) == 0 )
881 #ifdef POLARSSL_RSA_C
883 char *param1 = params[1];
886 char *param4 = params[4];
888 char *param6 = params[6];
889 char *param7 = params[7];
894 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 9 );
899 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
900 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
902 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
905 if(
verify_int( params[8], ¶m8 ) != 0 )
return( 2 );
907 test_suite_pk_rsa_encrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8 );
914 if( strcmp( params[0],
"pk_rsa_decrypt_test_vec" ) == 0 )
916 #ifdef POLARSSL_RSA_C
918 char *param1 = params[1];
921 char *param4 = params[4];
923 char *param6 = params[6];
925 char *param8 = params[8];
927 char *param10 = params[10];
928 char *param11 = params[11];
933 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 13 );
938 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
939 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
941 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
943 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
945 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
948 if(
verify_int( params[12], ¶m12 ) != 0 )
return( 2 );
950 test_suite_pk_rsa_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12 );
957 if( strcmp( params[0],
"pk_ec_nocrypt" ) == 0 )
964 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
968 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
970 test_suite_pk_ec_nocrypt( param1 );
978 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
992 ret = fgets( buf, len, f );
996 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
997 buf[strlen(buf) - 1] =
'\0';
998 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
999 buf[strlen(buf) - 1] =
'\0';
1010 params[cnt++] = cur;
1012 while( *p !=
'\0' && p < buf + len )
1022 if( p + 1 < buf + len )
1025 params[cnt++] = cur;
1034 for( i = 0; i < cnt; i++ )
1041 if( *p ==
'\\' && *(p + 1) ==
'n' )
1046 else if( *p ==
'\\' && *(p + 1) ==
':' )
1051 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1067 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1068 const char *filename =
"/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_pk.data";
1073 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1074 unsigned char alloc_buf[1000000];
1075 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
1078 file = fopen( filename,
"r" );
1081 fprintf( stderr,
"Failed to open\n" );
1085 while( !feof( file ) )
1089 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1091 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
1092 fprintf( stdout,
" " );
1093 for( i = strlen( buf ) + 1; i < 67; i++ )
1094 fprintf( stdout,
"." );
1095 fprintf( stdout,
" " );
1100 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1104 if( strcmp( params[0],
"depends_on" ) == 0 )
1106 for( i = 1; i < cnt; i++ )
1110 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1121 if( skip == 1 || ret == 3 )
1124 fprintf( stdout,
"----\n" );
1127 else if( ret == 0 && test_errors == 0 )
1129 fprintf( stdout,
"PASS\n" );
1134 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1141 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1143 if( strlen(buf) != 0 )
1145 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1151 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1152 if( total_errors == 0 )
1153 fprintf( stdout,
"PASSED" );
1155 fprintf( stdout,
"FAILED" );
1157 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1158 total_tests - total_errors, total_tests, total_skipped );
1160 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1161 #if defined(POLARSSL_MEMORY_DEBUG)
1162 memory_buffer_alloc_status();
1164 memory_buffer_alloc_free();
1167 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 )
static size_t pk_get_len(const pk_context *ctx)
Get the length in bytes of the underlying key.
#define PUT_UINT32_BE(n, b, i)
const pk_info_t * pk_info_from_type(pk_type_t pk_type)
Return information associated with the given PK type.
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
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 int unhexify(unsigned char *obuf, const char *ibuf)
size_t pk_get_size(const pk_context *ctx)
Get the size in bits of the underlying key.
int pk_decrypt(pk_context *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, size_t osize, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Decrypt message.
Configuration options (set of defines)
#define pk_ec(pk)
Quick access to an EC context inside a PK context.
pk_type_t pk_get_type(const pk_context *ctx)
Get the key type.
static int test_assert(int correct, char *test)
const char * pk_get_name(const pk_context *ctx)
Access the type name.
int ecp_point_read_binary(const ecp_group *grp, ecp_point *P, const unsigned char *buf, size_t ilen)
Import a point from unsigned binary data.
void mpi_init(mpi *X)
Initialize one MPI.
int main(int argc, char *argv[])
Public Key abstraction layer.
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.
#define POLARSSL_ERR_ECP_VERIFY_FAILED
The signature is not valid.
#define POLARSSL_ERR_RSA_INVALID_PADDING
Input data contains invalid padding and is rejected.
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 pk_verify(pk_context *ctx, md_type_t md_alg, const unsigned char *hash, size_t hash_len, const unsigned char *sig, size_t sig_len)
Verify signature.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
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.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int pk_can_do(pk_context *ctx, pk_type_t type)
Tell if a context can do the operation given by type.
int ecp_gen_keypair(ecp_group *grp, mpi *d, ecp_point *Q, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Generate a keypair.
int parse_arguments(char *buf, size_t len, char *params[50])
#define POLARSSL_ERR_RSA_VERIFY_FAILED
The PKCS#1 verification failed.
int pk_init_ctx(pk_context *ctx, const pk_info_t *info)
Initialize a PK context with the information given and allocates the type-specific PK subcontext...
int ecp_use_known_dp(ecp_group *grp, ecp_group_id index)
Set a group using well-known domain parameters.
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
int verify_string(char **str)
#define pk_rsa(pk)
Quick access to an RSA context inside a PK 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.
#define POLARSSL_ERR_PK_TYPE_MISMATCH
Type mismatch, eg attempt to encrypt with an ECDSA key.
void pk_free(pk_context *ctx)
Free a pk_context.
int pk_sign(pk_context *ctx, md_type_t md_alg, const unsigned char *hash, size_t hash_len, unsigned char *sig, size_t *sig_len, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Make signature.
int dispatch_test(int cnt, char *params[50])
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.
int mpi_mod_mpi(mpi *R, const mpi *A, const mpi *B)
Modulo: R = A mod B.
int pk_encrypt(pk_context *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, size_t osize, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Encrypt message.
void pk_init(pk_context *ctx)
Initialize a pk_context (as NONE)
int mpi_mul_mpi(mpi *X, const mpi *A, const mpi *B)
Baseline multiplication: X = A * B.
int verify_int(char *str, int *value)
int mpi_sub_int(mpi *X, const mpi *A, t_sint b)
Signed subtraction: X = A - b.
int get_line(FILE *f, char *buf, size_t len)