4 #ifdef POLARSSL_BIGNUM_C
5 #ifdef POLARSSL_GENPRIME
21 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
25 #if defined(WANT_NOT_RND_MPI)
26 #if defined(POLARSSL_BIGNUM_C)
29 #error "not_rnd_mpi() need bignum.c"
35 typedef UINT32 uint32_t;
48 #define GET_UINT32_BE(n,b,i) \
50 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
51 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
52 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
53 | ( (uint32_t) (b)[(i) + 3] ); \
58 #define PUT_UINT32_BE(n,b,i) \
60 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
61 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
62 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
63 (b)[(i) + 3] = (unsigned char) ( (n) ); \
67 static int unhexify(
unsigned char *obuf,
const char *ibuf)
70 int len = strlen(ibuf) / 2;
71 assert(!(strlen(ibuf) %1));
76 if( c >=
'0' && c <=
'9' )
78 else if( c >=
'a' && c <=
'f' )
80 else if( c >=
'A' && c <=
'F' )
86 if( c2 >=
'0' && c2 <=
'9' )
88 else if( c2 >=
'a' && c2 <=
'f' )
90 else if( c2 >=
'A' && c2 <=
'F' )
95 *obuf++ = ( c << 4 ) | c2;
101 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
113 *obuf++ =
'a' + h - 10;
118 *obuf++ =
'a' + l - 10;
134 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
138 if( rng_state != NULL )
141 for( i = 0; i < len; ++i )
152 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
154 if( rng_state != NULL )
157 memset( output, 0, len );
184 if( rng_state == NULL )
193 memcpy( output, info->
buf, use_len );
194 info->
buf += use_len;
198 if( len - use_len > 0 )
199 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
228 uint32_t i, *k, sum, delta=0x9E3779B9;
229 unsigned char result[4];
231 if( rng_state == NULL )
238 size_t use_len = ( len > 4 ) ? 4 : len;
241 for( i = 0; i < 32; i++ )
243 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
245 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
249 memcpy( output, result, use_len );
256 #if defined(WANT_NOT_RND_MPI)
265 #define ciL (sizeof(t_uint))
266 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
267 static int not_rnd_mpi(
void *in,
unsigned char *out,
size_t len )
269 char *str = (
char *) in;
278 X.
n = CHARS_TO_LIMBS( len );
284 assert( strlen( str ) / 2 == len );
296 #ifdef POLARSSL_RSA_C
297 #ifdef POLARSSL_BIGNUM_C
298 #ifdef POLARSSL_GENPRIME
300 #define TEST_SUITE_ACTIVE
308 if( test_errors == 1 )
309 printf(
"FAILED\n" );
310 printf(
" %s\n", test );
315 #define TEST_ASSERT( TEST ) \
316 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
317 if( test_errors) return; \
322 if( (*str)[0] !=
'"' ||
323 (*str)[strlen( *str ) - 1] !=
'"' )
325 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
330 (*str)[strlen( *str ) - 1] =
'\0';
342 for( i = 0; i < strlen( str ); i++ )
344 if( i == 0 && str[i] ==
'-' )
350 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
351 str[i - 1] ==
'0' && str[i] ==
'x' )
357 if( str[i] <
'0' || str[i] >
'9' )
367 *value = strtol( str, NULL, 16 );
369 *value = strtol( str, NULL, 10 );
374 if( strcmp( str,
"POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE" ) == 0 )
379 if( strcmp( str,
"POLARSSL_MD_SHA224" ) == 0 )
384 if( strcmp( str,
"RSA_PKCS_V15" ) == 0 )
389 if( strcmp( str,
"POLARSSL_MD_SHA384" ) == 0 )
394 if( strcmp( str,
"POLARSSL_MD_MD2" ) == 0 )
399 if( strcmp( str,
"POLARSSL_MD_MD5" ) == 0 )
404 if( strcmp( str,
"POLARSSL_ERR_RSA_INVALID_PADDING" ) == 0 )
409 if( strcmp( str,
"POLARSSL_ERR_RSA_KEY_CHECK_FAILED" ) == 0 )
414 if( strcmp( str,
"POLARSSL_MD_MD4" ) == 0 )
419 if( strcmp( str,
"POLARSSL_ERR_RSA_BAD_INPUT_DATA" ) == 0 )
424 if( strcmp( str,
"POLARSSL_MD_SHA512" ) == 0 )
429 if( strcmp( str,
"POLARSSL_MD_SHA1" ) == 0 )
434 if( strcmp( str,
"POLARSSL_MD_SHA256" ) == 0 )
439 if( strcmp( str,
"POLARSSL_ERR_RSA_VERIFY_FAILED" ) == 0 )
444 if( strcmp( str,
"POLARSSL_ERR_RSA_RNG_FAILED" ) == 0 )
451 printf(
"Expected integer for parameter and got: %s\n", str );
455 void test_suite_rsa_pkcs1_sign(
char *message_hex_string,
int padding_mode,
int digest,
456 int mod,
int radix_P,
char *input_P,
int radix_Q,
457 char *input_Q,
int radix_N,
char *input_N,
int radix_E,
458 char *input_E,
char *result_hex_str,
int result )
460 unsigned char message_str[1000];
461 unsigned char hash_result[1000];
462 unsigned char output[1000];
463 unsigned char output_str[1000];
472 memset( message_str, 0x00, 1000 );
473 memset( hash_result, 0x00, 1000 );
474 memset( output, 0x00, 1000 );
475 memset( output_str, 0x00, 1000 );
495 msg_len =
unhexify( message_str, message_hex_string );
505 TEST_ASSERT( strcasecmp( (
char *) output_str, result_hex_str ) == 0 );
512 void test_suite_rsa_pkcs1_verify(
char *message_hex_string,
int padding_mode,
int digest,
513 int mod,
int radix_N,
char *input_N,
int radix_E,
514 char *input_E,
char *result_hex_str,
int result )
516 unsigned char message_str[1000];
517 unsigned char hash_result[1000];
518 unsigned char result_str[1000];
523 memset( message_str, 0x00, 1000 );
524 memset( hash_result, 0x00, 1000 );
525 memset( result_str, 0x00, 1000 );
533 msg_len =
unhexify( message_str, message_hex_string );
534 unhexify( result_str, result_hex_str );
544 void test_suite_rsa_pkcs1_sign_raw(
char *message_hex_string,
char *hash_result_string,
545 int padding_mode,
int mod,
int radix_P,
char *input_P,
546 int radix_Q,
char *input_Q,
int radix_N,
547 char *input_N,
int radix_E,
char *input_E,
548 char *result_hex_str )
550 unsigned char message_str[1000];
551 unsigned char hash_result[1000];
552 unsigned char output[1000];
553 unsigned char output_str[1000];
562 memset( message_str, 0x00, 1000 );
563 memset( hash_result, 0x00, 1000 );
564 memset( output, 0x00, 1000 );
565 memset( output_str, 0x00, 1000 );
585 unhexify( message_str, message_hex_string );
586 hash_len =
unhexify( hash_result, hash_result_string );
592 TEST_ASSERT( strcasecmp( (
char *) output_str, result_hex_str ) == 0 );
598 void test_suite_rsa_pkcs1_verify_raw(
char *message_hex_string,
char *hash_result_string,
599 int padding_mode,
int mod,
int radix_N,
600 char *input_N,
int radix_E,
char *input_E,
601 char *result_hex_str,
int correct )
603 unsigned char message_str[1000];
604 unsigned char hash_result[1000];
605 unsigned char result_str[1000];
610 memset( message_str, 0x00, 1000 );
611 memset( hash_result, 0x00, 1000 );
612 memset( result_str, 0x00, 1000 );
620 unhexify( message_str, message_hex_string );
621 hash_len =
unhexify( hash_result, hash_result_string );
622 unhexify( result_str, result_hex_str );
629 void test_suite_rsa_pkcs1_encrypt(
char *message_hex_string,
int padding_mode,
int mod,
630 int radix_N,
char *input_N,
int radix_E,
char *input_E,
631 char *result_hex_str,
int result )
633 unsigned char message_str[1000];
634 unsigned char output[1000];
635 unsigned char output_str[1000];
643 memset( message_str, 0x00, 1000 );
644 memset( output, 0x00, 1000 );
645 memset( output_str, 0x00, 1000 );
653 msg_len =
unhexify( message_str, message_hex_string );
660 TEST_ASSERT( strcasecmp( (
char *) output_str, result_hex_str ) == 0 );
666 void test_suite_rsa_pkcs1_encrypt_bad_rng(
char *message_hex_string,
int padding_mode,
667 int mod,
int radix_N,
char *input_N,
668 int radix_E,
char *input_E,
669 char *result_hex_str,
int result )
671 unsigned char message_str[1000];
672 unsigned char output[1000];
673 unsigned char output_str[1000];
678 memset( message_str, 0x00, 1000 );
679 memset( output, 0x00, 1000 );
680 memset( output_str, 0x00, 1000 );
688 msg_len =
unhexify( message_str, message_hex_string );
695 TEST_ASSERT( strcasecmp( (
char *) output_str, result_hex_str ) == 0 );
701 void test_suite_rsa_pkcs1_decrypt(
char *message_hex_string,
int padding_mode,
int mod,
702 int radix_P,
char *input_P,
int radix_Q,
char *input_Q,
703 int radix_N,
char *input_N,
int radix_E,
char *input_E,
704 int max_output,
char *result_hex_str,
int result )
706 unsigned char message_str[1000];
707 unsigned char output[1000];
708 unsigned char output_str[1000];
717 memset( message_str, 0x00, 1000 );
718 memset( output, 0x00, 1000 );
719 memset( output_str, 0x00, 1000 );
739 unhexify( message_str, message_hex_string );
747 TEST_ASSERT( strncasecmp( (
char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 );
754 void test_suite_rsa_public(
char *message_hex_string,
int mod,
int radix_N,
char *input_N,
755 int radix_E,
char *input_E,
char *result_hex_str,
int result )
757 unsigned char message_str[1000];
758 unsigned char output[1000];
759 unsigned char output_str[1000];
763 memset( message_str, 0x00, 1000 );
764 memset( output, 0x00, 1000 );
765 memset( output_str, 0x00, 1000 );
773 unhexify( message_str, message_hex_string );
780 TEST_ASSERT( strcasecmp( (
char *) output_str, result_hex_str ) == 0 );
786 void test_suite_rsa_private(
char *message_hex_string,
int mod,
int radix_P,
char *input_P,
787 int radix_Q,
char *input_Q,
int radix_N,
char *input_N,
788 int radix_E,
char *input_E,
char *result_hex_str,
int result )
790 unsigned char message_str[1000];
791 unsigned char output[1000];
792 unsigned char output_str[1000];
801 memset( message_str, 0x00, 1000 );
821 unhexify( message_str, message_hex_string );
824 for( i = 0; i < 3; i++ )
826 memset( output, 0x00, 1000 );
827 memset( output_str, 0x00, 1000 );
829 message_str, output ) == result );
835 result_hex_str ) == 0 );
843 void test_suite_rsa_check_privkey_null()
851 void test_suite_rsa_check_pubkey(
int radix_N,
char *input_N,
int radix_E,
char *input_E,
858 if( strlen( input_N ) )
862 if( strlen( input_E ) )
872 void test_suite_rsa_check_privkey(
int mod,
int radix_P,
char *input_P,
int radix_Q,
873 char *input_Q,
int radix_N,
char *input_N,
874 int radix_E,
char *input_E,
int radix_D,
char *input_D,
875 int radix_DP,
char *input_DP,
int radix_DQ,
876 char *input_DQ,
int radix_QP,
char *input_QP,
884 if( strlen( input_P ) )
888 if( strlen( input_Q ) )
892 if( strlen( input_N ) )
896 if( strlen( input_E ) )
900 if( strlen( input_D ) )
904 if( strlen( input_DP ) )
908 if( strlen( input_DQ ) )
912 if( strlen( input_QP ) )
922 #ifdef POLARSSL_CTR_CRBG_C
923 #ifdef POLARSSL_ENTROPY_C
924 void test_suite_rsa_gen_key(
int nrbits,
int exponent,
int result)
929 const char *pers =
"test_suite_rsa";
933 (
const unsigned char *) pers, strlen( pers ) ) == 0 );
949 #ifdef POLARSSL_SELF_TEST
950 void test_suite_rsa_selftest()
967 if( strcmp( str,
"POLARSSL_MD2_C" ) == 0 )
969 #if defined(POLARSSL_MD2_C)
975 if( strcmp( str,
"POLARSSL_SHA512_C" ) == 0 )
977 #if defined(POLARSSL_SHA512_C)
983 if( strcmp( str,
"POLARSSL_CTR_DRBG_C" ) == 0 )
985 #if defined(POLARSSL_CTR_DRBG_C)
991 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
993 #if defined(POLARSSL_SHA1_C)
999 if( strcmp( str,
"POLARSSL_MD4_C" ) == 0 )
1001 #if defined(POLARSSL_MD4_C)
1007 if( strcmp( str,
"POLARSSL_PKCS1_V15" ) == 0 )
1009 #if defined(POLARSSL_PKCS1_V15)
1015 if( strcmp( str,
"POLARSSL_SELF_TEST" ) == 0 )
1017 #if defined(POLARSSL_SELF_TEST)
1023 if( strcmp( str,
"POLARSSL_SHA256_C" ) == 0 )
1025 #if defined(POLARSSL_SHA256_C)
1031 if( strcmp( str,
"POLARSSL_MD5_C" ) == 0 )
1033 #if defined(POLARSSL_MD5_C)
1039 if( strcmp( str,
"POLARSSL_ENTROPY_C" ) == 0 )
1041 #if defined(POLARSSL_ENTROPY_C)
1058 #if defined(TEST_SUITE_ACTIVE)
1059 if( strcmp( params[0],
"rsa_pkcs1_sign" ) == 0 )
1062 char *param1 = params[1];
1067 char *param6 = params[6];
1069 char *param8 = params[8];
1071 char *param10 = params[10];
1073 char *param12 = params[12];
1074 char *param13 = params[13];
1079 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 15 );
1084 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1085 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1086 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1087 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1089 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1091 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1093 if(
verify_int( params[11], ¶m11 ) != 0 )
return( 2 );
1096 if(
verify_int( params[14], ¶m14 ) != 0 )
return( 2 );
1098 test_suite_rsa_pkcs1_sign( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14 );
1104 if( strcmp( params[0],
"rsa_pkcs1_verify" ) == 0 )
1107 char *param1 = params[1];
1112 char *param6 = params[6];
1114 char *param8 = params[8];
1115 char *param9 = params[9];
1120 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1125 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1126 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1127 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1128 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1130 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1133 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
1135 test_suite_rsa_pkcs1_verify( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1141 if( strcmp( params[0],
"rsa_pkcs1_sign_raw" ) == 0 )
1144 char *param1 = params[1];
1145 char *param2 = params[2];
1149 char *param6 = params[6];
1151 char *param8 = params[8];
1153 char *param10 = params[10];
1155 char *param12 = params[12];
1156 char *param13 = params[13];
1160 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 14 );
1166 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1167 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1168 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1170 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1172 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1174 if(
verify_int( params[11], ¶m11 ) != 0 )
return( 2 );
1178 test_suite_rsa_pkcs1_sign_raw( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13 );
1184 if( strcmp( params[0],
"rsa_pkcs1_verify_raw" ) == 0 )
1187 char *param1 = params[1];
1188 char *param2 = params[2];
1192 char *param6 = params[6];
1194 char *param8 = params[8];
1195 char *param9 = params[9];
1200 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1206 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1207 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1208 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1210 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1213 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
1215 test_suite_rsa_pkcs1_verify_raw( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1221 if( strcmp( params[0],
"rsa_pkcs1_encrypt" ) == 0 )
1224 char *param1 = params[1];
1228 char *param5 = params[5];
1230 char *param7 = params[7];
1231 char *param8 = params[8];
1236 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 10 );
1241 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1242 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1243 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1245 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1248 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1250 test_suite_rsa_pkcs1_encrypt( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
1256 if( strcmp( params[0],
"rsa_pkcs1_encrypt_bad_rng" ) == 0 )
1259 char *param1 = params[1];
1263 char *param5 = params[5];
1265 char *param7 = params[7];
1266 char *param8 = params[8];
1271 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 10 );
1276 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1277 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1278 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1280 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1283 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1285 test_suite_rsa_pkcs1_encrypt_bad_rng( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
1291 if( strcmp( params[0],
"rsa_pkcs1_decrypt" ) == 0 )
1294 char *param1 = params[1];
1298 char *param5 = params[5];
1300 char *param7 = params[7];
1302 char *param9 = params[9];
1304 char *param11 = params[11];
1306 char *param13 = params[13];
1311 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 15 );
1316 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1317 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1318 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1320 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1322 if(
verify_int( params[8], ¶m8 ) != 0 )
return( 2 );
1324 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
1326 if(
verify_int( params[12], ¶m12 ) != 0 )
return( 2 );
1328 if(
verify_int( params[14], ¶m14 ) != 0 )
return( 2 );
1330 test_suite_rsa_pkcs1_decrypt( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14 );
1336 if( strcmp( params[0],
"rsa_public" ) == 0 )
1339 char *param1 = params[1];
1342 char *param4 = params[4];
1344 char *param6 = params[6];
1345 char *param7 = params[7];
1350 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 9 );
1355 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1356 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1358 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1361 if(
verify_int( params[8], ¶m8 ) != 0 )
return( 2 );
1363 test_suite_rsa_public( param1, param2, param3, param4, param5, param6, param7, param8 );
1369 if( strcmp( params[0],
"rsa_private" ) == 0 )
1372 char *param1 = params[1];
1375 char *param4 = params[4];
1377 char *param6 = params[6];
1379 char *param8 = params[8];
1381 char *param10 = params[10];
1382 char *param11 = params[11];
1387 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 13 );
1392 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1393 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1395 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1397 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1399 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1402 if(
verify_int( params[12], ¶m12 ) != 0 )
return( 2 );
1404 test_suite_rsa_private( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12 );
1410 if( strcmp( params[0],
"rsa_check_privkey_null" ) == 0 )
1416 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1421 test_suite_rsa_check_privkey_null( );
1427 if( strcmp( params[0],
"rsa_check_pubkey" ) == 0 )
1431 char *param2 = params[2];
1433 char *param4 = params[4];
1438 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1442 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1444 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1446 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1448 test_suite_rsa_check_pubkey( param1, param2, param3, param4, param5 );
1454 if( strcmp( params[0],
"rsa_check_privkey" ) == 0 )
1459 char *param3 = params[3];
1461 char *param5 = params[5];
1463 char *param7 = params[7];
1465 char *param9 = params[9];
1467 char *param11 = params[11];
1469 char *param13 = params[13];
1471 char *param15 = params[15];
1473 char *param17 = params[17];
1478 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 19 );
1482 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1483 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1485 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1487 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1489 if(
verify_int( params[8], ¶m8 ) != 0 )
return( 2 );
1491 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
1493 if(
verify_int( params[12], ¶m12 ) != 0 )
return( 2 );
1495 if(
verify_int( params[14], ¶m14 ) != 0 )
return( 2 );
1497 if(
verify_int( params[16], ¶m16 ) != 0 )
return( 2 );
1499 if(
verify_int( params[18], ¶m18 ) != 0 )
return( 2 );
1501 test_suite_rsa_check_privkey( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, param15, param16, param17, param18 );
1507 if( strcmp( params[0],
"rsa_gen_key" ) == 0 )
1509 #ifdef POLARSSL_CTR_CRBG_C
1510 #ifdef POLARSSL_ENTROPY_C
1518 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1522 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1523 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1524 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1526 test_suite_rsa_gen_key( param1, param2, param3 );
1534 if( strcmp( params[0],
"rsa_selftest" ) == 0 )
1536 #ifdef POLARSSL_SELF_TEST
1541 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1546 test_suite_rsa_selftest( );
1555 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
1569 ret = fgets( buf, len, f );
1573 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
1574 buf[strlen(buf) - 1] =
'\0';
1575 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
1576 buf[strlen(buf) - 1] =
'\0';
1587 params[cnt++] = cur;
1589 while( *p !=
'\0' && p < buf + len )
1599 if( p + 1 < buf + len )
1602 params[cnt++] = cur;
1611 for( i = 0; i < cnt; i++ )
1618 if( *p ==
'\\' && *(p + 1) ==
'n' )
1623 else if( *p ==
'\\' && *(p + 1) ==
':' )
1628 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1644 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1645 const char *filename =
"/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_rsa.data";
1650 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1651 unsigned char alloc_buf[1000000];
1652 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
1655 file = fopen( filename,
"r" );
1658 fprintf( stderr,
"Failed to open\n" );
1662 while( !feof( file ) )
1666 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1668 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
1669 fprintf( stdout,
" " );
1670 for( i = strlen( buf ) + 1; i < 67; i++ )
1671 fprintf( stdout,
"." );
1672 fprintf( stdout,
" " );
1677 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1681 if( strcmp( params[0],
"depends_on" ) == 0 )
1683 for( i = 1; i < cnt; i++ )
1687 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1698 if( skip == 1 || ret == 3 )
1701 fprintf( stdout,
"----\n" );
1704 else if( ret == 0 && test_errors == 0 )
1706 fprintf( stdout,
"PASS\n" );
1711 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1718 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1720 if( strlen(buf) != 0 )
1722 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1728 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1729 if( total_errors == 0 )
1730 fprintf( stdout,
"PASSED" );
1732 fprintf( stdout,
"FAILED" );
1734 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1735 total_tests - total_errors, total_tests, total_skipped );
1737 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1738 #if defined(POLARSSL_MEMORY_DEBUG)
1739 memory_buffer_alloc_status();
1741 memory_buffer_alloc_free();
1744 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 )
#define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE
The output buffer for decryption is not large enough.
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.
int rsa_self_test(int verbose)
Checkup routine.
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.
Configuration options (set of defines)
int rsa_check_pubkey(const rsa_context *ctx)
Check a public RSA key.
Entropy context structure.
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.
Entropy accumulator implementation.
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_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.
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.
#define PUT_UINT32_BE(n, b, i)
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 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.
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.
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.
SHA-1 cryptographic hash function.
#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED
Key failed to pass the libraries validity check.
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.
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.
SHA-384 and SHA-512 cryptographic hash function.
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 (Max ENTROPY_BLOCK_SIZE) (Thread-safe if POLARSSL_THREADING_C i...
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.
int get_line(FILE *f, char *buf, size_t len)
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.