1 #if !defined(POLARSSL_CONFIG_FILE)
4 #include POLARSSL_CONFIG_FILE
8 #ifdef POLARSSL_BIGNUM_C
9 #ifdef POLARSSL_GENPRIME
25 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
29 #if defined(POLARSSL_PLATFORM_C)
32 #define polarssl_malloc malloc
33 #define polarssl_free free
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;
138 size_t actual_len = len != 0 ? len : 1;
143 memset( p, 0x00, actual_len );
162 *olen = strlen(ibuf) / 2;
168 assert( obuf != NULL );
184 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
186 #if !defined(__OpenBSD__)
189 if( rng_state != NULL )
192 for( i = 0; i < len; ++i )
195 if( rng_state != NULL )
198 arc4random_buf( output, len );
209 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
211 if( rng_state != NULL )
214 memset( output, 0, len );
241 if( rng_state == NULL )
250 memcpy( output, info->
buf, use_len );
251 info->
buf += use_len;
255 if( len - use_len > 0 )
256 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
285 uint32_t i, *k, sum, delta=0x9E3779B9;
286 unsigned char result[4], *out = output;
288 if( rng_state == NULL )
295 size_t use_len = ( len > 4 ) ? 4 : len;
298 for( i = 0; i < 32; i++ )
300 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
302 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
306 memcpy( out, result, use_len );
318 #if defined(POLARSSL_PLATFORM_C)
321 #define polarssl_printf printf
322 #define polarssl_malloc malloc
323 #define polarssl_free free
328 #ifdef POLARSSL_RSA_C
329 #ifdef POLARSSL_BIGNUM_C
330 #ifdef POLARSSL_GENPRIME
332 #define TEST_SUITE_ACTIVE
334 static int test_assert(
int correct,
const char *test )
341 printf(
"FAILED\n" );
342 printf(
" %s\n", test );
347 #define TEST_ASSERT( TEST ) \
348 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
349 if( test_errors) goto exit; \
354 if( (*str)[0] !=
'"' ||
355 (*str)[strlen( *str ) - 1] !=
'"' )
357 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
362 (*str)[strlen( *str ) - 1] =
'\0';
374 for( i = 0; i < strlen( str ); i++ )
376 if( i == 0 && str[i] ==
'-' )
382 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
383 str[i - 1] ==
'0' && str[i] ==
'x' )
389 if( ! ( ( str[i] >=
'0' && str[i] <=
'9' ) ||
390 ( hex && ( ( str[i] >=
'a' && str[i] <=
'f' ) ||
391 ( str[i] >=
'A' && str[i] <=
'F' ) ) ) ) )
401 *value = strtol( str, NULL, 16 );
403 *value = strtol( str, NULL, 10 );
408 if( strcmp( str,
"POLARSSL_MD_MD5" ) == 0 )
413 if( strcmp( str,
"POLARSSL_MD_SHA512" ) == 0 )
418 if( strcmp( str,
"RSA_PKCS_V15" ) == 0 )
423 if( strcmp( str,
"POLARSSL_MD_SHA256" ) == 0 )
428 if( strcmp( str,
"POLARSSL_ERR_RSA_RNG_FAILED" ) == 0 )
433 if( strcmp( str,
"POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE" ) == 0 )
438 if( strcmp( str,
"POLARSSL_ERR_RSA_INVALID_PADDING" ) == 0 )
443 if( strcmp( str,
"POLARSSL_MD_MD2" ) == 0 )
448 if( strcmp( str,
"POLARSSL_ERR_RSA_VERIFY_FAILED" ) == 0 )
453 if( strcmp( str,
"POLARSSL_ERR_RSA_KEY_CHECK_FAILED" ) == 0 )
458 if( strcmp( str,
"POLARSSL_MD_MD4" ) == 0 )
463 if( strcmp( str,
"POLARSSL_ERR_RSA_BAD_INPUT_DATA" ) == 0 )
468 if( strcmp( str,
"POLARSSL_MD_SHA224" ) == 0 )
473 if( strcmp( str,
"POLARSSL_MD_SHA1" ) == 0 )
478 if( strcmp( str,
"POLARSSL_MD_SHA384" ) == 0 )
485 printf(
"Expected integer for parameter and got: %s\n", str );
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 )
494 unsigned char message_str[1000];
495 unsigned char hash_result[1000];
496 unsigned char output[1000];
497 unsigned char output_str[1000];
506 memset( message_str, 0x00, 1000 );
507 memset( hash_result, 0x00, 1000 );
508 memset( output, 0x00, 1000 );
509 memset( output_str, 0x00, 1000 );
529 msg_len =
unhexify( message_str, message_hex_string );
539 TEST_ASSERT( strcasecmp( (
char *) output_str, result_hex_str ) == 0 );
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 )
551 unsigned char message_str[1000];
552 unsigned char hash_result[1000];
553 unsigned char result_str[1000];
558 memset( message_str, 0x00, 1000 );
559 memset( hash_result, 0x00, 1000 );
560 memset( result_str, 0x00, 1000 );
568 msg_len =
unhexify( message_str, message_hex_string );
569 unhexify( result_str, result_hex_str );
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 )
586 unsigned char message_str[1000];
587 unsigned char hash_result[1000];
588 unsigned char output[1000];
589 unsigned char output_str[1000];
598 memset( message_str, 0x00, 1000 );
599 memset( hash_result, 0x00, 1000 );
600 memset( output, 0x00, 1000 );
601 memset( output_str, 0x00, 1000 );
621 unhexify( message_str, message_hex_string );
622 hash_len =
unhexify( hash_result, hash_result_string );
628 TEST_ASSERT( strcasecmp( (
char *) output_str, result_hex_str ) == 0 );
633 memset( output, 0x00, 1000 );
634 memset( output_str, 0x00, 1000 );
638 hash_len, hash_result, output ) == 0 );
642 TEST_ASSERT( strcasecmp( (
char *) output_str, result_hex_str ) == 0 );
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 )
655 unsigned char message_str[1000];
656 unsigned char hash_result[1000];
657 unsigned char result_str[1000];
658 unsigned char output[1000];
660 size_t hash_len, olen;
663 memset( message_str, 0x00, 1000 );
664 memset( hash_result, 0x00, 1000 );
665 memset( result_str, 0x00, 1000 );
666 memset( output, 0x00,
sizeof( output ) );
674 unhexify( message_str, message_hex_string );
675 hash_len =
unhexify( hash_result, hash_result_string );
676 unhexify( result_str, result_hex_str );
687 &olen, result_str, output,
sizeof( output ) ) == 0 );
689 ok = olen == hash_len && memcmp( output, hash_result, olen ) == 0;
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 )
704 unsigned char message_str[1000];
705 unsigned char output[1000];
706 unsigned char output_str[1000];
714 memset( message_str, 0x00, 1000 );
715 memset( output, 0x00, 1000 );
716 memset( output_str, 0x00, 1000 );
724 msg_len =
unhexify( message_str, message_hex_string );
731 TEST_ASSERT( strcasecmp( (
char *) output_str, result_hex_str ) == 0 );
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 )
743 unsigned char message_str[1000];
744 unsigned char output[1000];
745 unsigned char output_str[1000];
750 memset( message_str, 0x00, 1000 );
751 memset( output, 0x00, 1000 );
752 memset( output_str, 0x00, 1000 );
760 msg_len =
unhexify( message_str, message_hex_string );
767 TEST_ASSERT( strcasecmp( (
char *) output_str, result_hex_str ) == 0 );
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 )
779 unsigned char message_str[1000];
780 unsigned char output[1000];
781 unsigned char output_str[1000];
790 memset( message_str, 0x00, 1000 );
791 memset( output, 0x00, 1000 );
792 memset( output_str, 0x00, 1000 );
812 unhexify( message_str, message_hex_string );
820 TEST_ASSERT( strncasecmp( (
char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 );
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 )
831 unsigned char message_str[1000];
832 unsigned char output[1000];
833 unsigned char output_str[1000];
838 memset( message_str, 0x00, 1000 );
839 memset( output, 0x00, 1000 );
840 memset( output_str, 0x00, 1000 );
848 unhexify( message_str, message_hex_string );
855 TEST_ASSERT( strcasecmp( (
char *) output_str, result_hex_str ) == 0 );
865 memset( output, 0x00, 1000 );
866 memset( output_str, 0x00, 1000 );
872 TEST_ASSERT( strcasecmp( (
char *) output_str, result_hex_str ) == 0 );
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 )
884 unsigned char message_str[1000];
885 unsigned char output[1000];
886 unsigned char output_str[1000];
896 memset( message_str, 0x00, 1000 );
916 unhexify( message_str, message_hex_string );
919 for( i = 0; i < 3; i++ )
921 memset( output, 0x00, 1000 );
922 memset( output_str, 0x00, 1000 );
924 message_str, output ) == result );
930 result_hex_str ) == 0 );
941 memset( output, 0x00, 1000 );
942 memset( output_str, 0x00, 1000 );
944 message_str, output ) == result );
950 result_hex_str ) == 0 );
958 void test_suite_rsa_check_privkey_null()
969 void test_suite_rsa_check_pubkey(
int radix_N,
char *input_N,
int radix_E,
char *input_E,
976 if( strlen( input_N ) )
980 if( strlen( input_E ) )
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,
1003 if( strlen( input_P ) )
1007 if( strlen( input_Q ) )
1011 if( strlen( input_N ) )
1015 if( strlen( input_E ) )
1019 if( strlen( input_D ) )
1023 if( strlen( input_DP ) )
1027 if( strlen( input_DQ ) )
1031 if( strlen( input_QP ) )
1042 #ifdef POLARSSL_CTR_DRBG_C
1043 #ifdef POLARSSL_ENTROPY_C
1044 void test_suite_rsa_gen_key(
int nrbits,
int exponent,
int result)
1049 const char *pers =
"test_suite_rsa";
1053 (
const unsigned char *) pers, strlen( pers ) ) == 0 );
1071 #ifdef POLARSSL_SELF_TEST
1072 void test_suite_rsa_selftest()
1092 if( strcmp( str,
"POLARSSL_MD2_C" ) == 0 )
1094 #if defined(POLARSSL_MD2_C)
1100 if( strcmp( str,
"POLARSSL_SHA512_C" ) == 0 )
1102 #if defined(POLARSSL_SHA512_C)
1108 if( strcmp( str,
"POLARSSL_PKCS1_V15" ) == 0 )
1110 #if defined(POLARSSL_PKCS1_V15)
1116 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
1118 #if defined(POLARSSL_SHA1_C)
1124 if( strcmp( str,
"POLARSSL_MD4_C" ) == 0 )
1126 #if defined(POLARSSL_MD4_C)
1132 if( strcmp( str,
"POLARSSL_SHA256_C" ) == 0 )
1134 #if defined(POLARSSL_SHA256_C)
1140 if( strcmp( str,
"POLARSSL_MD5_C" ) == 0 )
1142 #if defined(POLARSSL_MD5_C)
1148 if( strcmp( str,
"POLARSSL_SELF_TEST" ) == 0 )
1150 #if defined(POLARSSL_SELF_TEST)
1167 #if defined(TEST_SUITE_ACTIVE)
1168 if( strcmp( params[0],
"rsa_pkcs1_sign" ) == 0 )
1171 char *param1 = params[1];
1176 char *param6 = params[6];
1178 char *param8 = params[8];
1180 char *param10 = params[10];
1182 char *param12 = params[12];
1183 char *param13 = params[13];
1188 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 15 );
1193 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1194 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1195 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1196 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1198 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1200 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1202 if(
verify_int( params[11], ¶m11 ) != 0 )
return( 2 );
1205 if(
verify_int( params[14], ¶m14 ) != 0 )
return( 2 );
1207 test_suite_rsa_pkcs1_sign( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14 );
1213 if( strcmp( params[0],
"rsa_pkcs1_verify" ) == 0 )
1216 char *param1 = params[1];
1221 char *param6 = params[6];
1223 char *param8 = params[8];
1224 char *param9 = params[9];
1229 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1234 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1235 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1236 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1237 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1239 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1242 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
1244 test_suite_rsa_pkcs1_verify( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1250 if( strcmp( params[0],
"rsa_pkcs1_sign_raw" ) == 0 )
1253 char *param1 = params[1];
1254 char *param2 = params[2];
1258 char *param6 = params[6];
1260 char *param8 = params[8];
1262 char *param10 = params[10];
1264 char *param12 = params[12];
1265 char *param13 = params[13];
1269 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 14 );
1275 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1276 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1277 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1279 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1281 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1283 if(
verify_int( params[11], ¶m11 ) != 0 )
return( 2 );
1287 test_suite_rsa_pkcs1_sign_raw( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13 );
1293 if( strcmp( params[0],
"rsa_pkcs1_verify_raw" ) == 0 )
1296 char *param1 = params[1];
1297 char *param2 = params[2];
1301 char *param6 = params[6];
1303 char *param8 = params[8];
1304 char *param9 = params[9];
1309 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1315 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1316 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1317 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1319 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1322 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
1324 test_suite_rsa_pkcs1_verify_raw( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1330 if( strcmp( params[0],
"rsa_pkcs1_encrypt" ) == 0 )
1333 char *param1 = params[1];
1337 char *param5 = params[5];
1339 char *param7 = params[7];
1340 char *param8 = params[8];
1345 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 10 );
1350 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1351 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1352 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1354 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1357 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1359 test_suite_rsa_pkcs1_encrypt( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
1365 if( strcmp( params[0],
"rsa_pkcs1_encrypt_bad_rng" ) == 0 )
1368 char *param1 = params[1];
1372 char *param5 = params[5];
1374 char *param7 = params[7];
1375 char *param8 = params[8];
1380 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 10 );
1385 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1386 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1387 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1389 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1392 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1394 test_suite_rsa_pkcs1_encrypt_bad_rng( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
1400 if( strcmp( params[0],
"rsa_pkcs1_decrypt" ) == 0 )
1403 char *param1 = params[1];
1407 char *param5 = params[5];
1409 char *param7 = params[7];
1411 char *param9 = params[9];
1413 char *param11 = params[11];
1415 char *param13 = params[13];
1420 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 15 );
1425 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1426 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1427 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1429 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1431 if(
verify_int( params[8], ¶m8 ) != 0 )
return( 2 );
1433 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
1435 if(
verify_int( params[12], ¶m12 ) != 0 )
return( 2 );
1437 if(
verify_int( params[14], ¶m14 ) != 0 )
return( 2 );
1439 test_suite_rsa_pkcs1_decrypt( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14 );
1445 if( strcmp( params[0],
"rsa_public" ) == 0 )
1448 char *param1 = params[1];
1451 char *param4 = params[4];
1453 char *param6 = params[6];
1454 char *param7 = params[7];
1459 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 9 );
1464 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1465 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1467 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1470 if(
verify_int( params[8], ¶m8 ) != 0 )
return( 2 );
1472 test_suite_rsa_public( param1, param2, param3, param4, param5, param6, param7, param8 );
1478 if( strcmp( params[0],
"rsa_private" ) == 0 )
1481 char *param1 = params[1];
1484 char *param4 = params[4];
1486 char *param6 = params[6];
1488 char *param8 = params[8];
1490 char *param10 = params[10];
1491 char *param11 = params[11];
1496 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 13 );
1501 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1502 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1504 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1506 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1508 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1511 if(
verify_int( params[12], ¶m12 ) != 0 )
return( 2 );
1513 test_suite_rsa_private( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12 );
1519 if( strcmp( params[0],
"rsa_check_privkey_null" ) == 0 )
1525 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1530 test_suite_rsa_check_privkey_null( );
1536 if( strcmp( params[0],
"rsa_check_pubkey" ) == 0 )
1540 char *param2 = params[2];
1542 char *param4 = params[4];
1547 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1551 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1553 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1555 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1557 test_suite_rsa_check_pubkey( param1, param2, param3, param4, param5 );
1563 if( strcmp( params[0],
"rsa_check_privkey" ) == 0 )
1568 char *param3 = params[3];
1570 char *param5 = params[5];
1572 char *param7 = params[7];
1574 char *param9 = params[9];
1576 char *param11 = params[11];
1578 char *param13 = params[13];
1580 char *param15 = params[15];
1582 char *param17 = params[17];
1587 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 19 );
1591 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1592 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1594 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1596 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1598 if(
verify_int( params[8], ¶m8 ) != 0 )
return( 2 );
1600 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
1602 if(
verify_int( params[12], ¶m12 ) != 0 )
return( 2 );
1604 if(
verify_int( params[14], ¶m14 ) != 0 )
return( 2 );
1606 if(
verify_int( params[16], ¶m16 ) != 0 )
return( 2 );
1608 if(
verify_int( params[18], ¶m18 ) != 0 )
return( 2 );
1610 test_suite_rsa_check_privkey( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, param15, param16, param17, param18 );
1616 if( strcmp( params[0],
"rsa_gen_key" ) == 0 )
1618 #ifdef POLARSSL_CTR_DRBG_C
1619 #ifdef POLARSSL_ENTROPY_C
1627 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1631 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1632 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1633 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1635 test_suite_rsa_gen_key( param1, param2, param3 );
1643 if( strcmp( params[0],
"rsa_selftest" ) == 0 )
1645 #ifdef POLARSSL_SELF_TEST
1650 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1655 test_suite_rsa_selftest( );
1664 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
1678 ret = fgets( buf, len, f );
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';
1696 params[cnt++] = cur;
1698 while( *p !=
'\0' && p < buf + len )
1708 if( p + 1 < buf + len )
1711 params[cnt++] = cur;
1720 for( i = 0; i < cnt; i++ )
1727 if( *p ==
'\\' && *(p + 1) ==
'n' )
1732 else if( *p ==
'\\' && *(p + 1) ==
':' )
1737 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1753 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1754 const char *filename =
"/builddir/build/BUILD/polarssl-1.3.9/tests/suites/test_suite_rsa.data";
1759 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1760 unsigned char alloc_buf[1000000];
1764 file = fopen( filename,
"r" );
1767 fprintf( stderr,
"Failed to open\n" );
1771 while( !feof( file ) )
1775 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
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,
" " );
1786 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1790 if( strcmp( params[0],
"depends_on" ) == 0 )
1792 for( i = 1; i < cnt; i++ )
1796 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1807 if( skip == 1 || ret == 3 )
1810 fprintf( stdout,
"----\n" );
1815 fprintf( stdout,
"PASS\n" );
1820 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1827 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1829 if( strlen(buf) != 0 )
1831 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1837 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1838 if( total_errors == 0 )
1839 fprintf( stdout,
"PASSED" );
1841 fprintf( stdout,
"FAILED" );
1843 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1844 total_tests - total_errors, total_tests, total_skipped );
1846 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1847 #if defined(POLARSSL_MEMORY_DEBUG)
1848 memory_buffer_alloc_status();
1853 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.
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)
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.
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.
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.
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
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)
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 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 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 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 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.