1 #if !defined(POLARSSL_CONFIG_FILE)
4 #include POLARSSL_CONFIG_FILE
11 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len );
13 #define RSA_KEY_SIZE 512
14 #define RSA_KEY_LEN 64
20 #if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
24 #if defined(POLARSSL_ECP_C)
41 #if defined(POLARSSL_RSA_C)
43 const unsigned char *input,
unsigned char *output,
44 size_t output_max_len )
47 input, output, output_max_len ) );
50 int (*f_rng)(
void *,
unsigned char *,
size_t),
void *p_rng,
51 int mode,
md_type_t md_alg,
unsigned int hashlen,
52 const unsigned char *hash,
unsigned char *sig )
55 md_alg, hashlen, hash, sig ) );
65 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
69 #if defined(POLARSSL_PLATFORM_C)
72 #define polarssl_malloc malloc
73 #define polarssl_free free
78 typedef UINT32 uint32_t;
91 #define GET_UINT32_BE(n,b,i) \
93 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
94 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
95 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
96 | ( (uint32_t) (b)[(i) + 3] ); \
100 #ifndef PUT_UINT32_BE
101 #define PUT_UINT32_BE(n,b,i) \
103 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
104 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
105 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
106 (b)[(i) + 3] = (unsigned char) ( (n) ); \
110 static int unhexify(
unsigned char *obuf,
const char *ibuf)
113 int len = strlen(ibuf) / 2;
114 assert(!(strlen(ibuf) %1));
119 if( c >=
'0' && c <=
'9' )
121 else if( c >=
'a' && c <=
'f' )
123 else if( c >=
'A' && c <=
'F' )
129 if( c2 >=
'0' && c2 <=
'9' )
131 else if( c2 >=
'a' && c2 <=
'f' )
133 else if( c2 >=
'A' && c2 <=
'F' )
138 *obuf++ = ( c << 4 ) | c2;
144 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
156 *obuf++ =
'a' + h - 10;
161 *obuf++ =
'a' + l - 10;
178 size_t actual_len = len != 0 ? len : 1;
183 memset( p, 0x00, actual_len );
202 *olen = strlen(ibuf) / 2;
208 assert( obuf != NULL );
224 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
226 #if !defined(__OpenBSD__)
229 if( rng_state != NULL )
232 for( i = 0; i < len; ++i )
235 if( rng_state != NULL )
238 arc4random_buf( output, len );
249 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
251 if( rng_state != NULL )
254 memset( output, 0, len );
281 if( rng_state == NULL )
290 memcpy( output, info->
buf, use_len );
291 info->
buf += use_len;
295 if( len - use_len > 0 )
296 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
325 uint32_t i, *k, sum, delta=0x9E3779B9;
326 unsigned char result[4], *out = output;
328 if( rng_state == NULL )
335 size_t use_len = ( len > 4 ) ? 4 : len;
338 for( i = 0; i < 32; i++ )
340 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
342 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
346 memcpy( out, result, use_len );
358 #if defined(POLARSSL_PLATFORM_C)
361 #define polarssl_printf printf
362 #define polarssl_malloc malloc
363 #define polarssl_free free
370 #define TEST_SUITE_ACTIVE
372 static int test_assert(
int correct,
const char *test )
378 if( test_errors == 1 )
379 printf(
"FAILED\n" );
380 printf(
" %s\n", test );
385 #define TEST_ASSERT( TEST ) \
386 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
387 if( test_errors) goto exit; \
392 if( (*str)[0] !=
'"' ||
393 (*str)[strlen( *str ) - 1] !=
'"' )
395 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
400 (*str)[strlen( *str ) - 1] =
'\0';
412 for( i = 0; i < strlen( str ); i++ )
414 if( i == 0 && str[i] ==
'-' )
420 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
421 str[i - 1] ==
'0' && str[i] ==
'x' )
427 if( ! ( ( str[i] >=
'0' && str[i] <=
'9' ) ||
428 ( hex && ( ( str[i] >=
'a' && str[i] <=
'f' ) ||
429 ( str[i] >=
'A' && str[i] <=
'F' ) ) ) ) )
439 *value = strtol( str, NULL, 16 );
441 *value = strtol( str, NULL, 10 );
446 #ifdef POLARSSL_RSA_C
447 if( strcmp( str,
"POLARSSL_PK_RSASSA_PSS" ) == 0 )
452 #endif // POLARSSL_RSA_C
453 if( strcmp( str,
"POLARSSL_PK_ECKEY" ) == 0 )
458 #ifdef POLARSSL_RSA_C
459 if( strcmp( str,
"POLARSSL_ERR_RSA_INVALID_PADDING" ) == 0 )
464 #endif // POLARSSL_RSA_C
465 #ifdef POLARSSL_ECDSA_C
466 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1" ) == 0 )
471 #endif // POLARSSL_ECDSA_C
472 if( strcmp( str,
"POLARSSL_PK_ECDSA" ) == 0 )
477 #ifdef POLARSSL_RSA_C
478 if( strcmp( str,
"POLARSSL_MD_NONE" ) == 0 )
483 #endif // POLARSSL_RSA_C
484 if( strcmp( str,
"POLARSSL_PK_ECKEY_DH" ) == 0 )
489 #ifdef POLARSSL_RSA_C
490 if( strcmp( str,
"POLARSSL_ERR_PK_BAD_INPUT_DATA" ) == 0 )
495 #endif // POLARSSL_RSA_C
496 #ifdef POLARSSL_RSA_C
497 if( strcmp( str,
"RSA_SALT_LEN_ANY" ) == 0 )
502 #endif // POLARSSL_RSA_C
503 if( strcmp( str,
"POLARSSL_PK_RSA" ) == 0 )
508 #ifdef POLARSSL_RSA_C
509 if( strcmp( str,
"POLARSSL_MD_SHA1" ) == 0 )
514 #endif // POLARSSL_RSA_C
515 #ifdef POLARSSL_RSA_C
516 if( strcmp( str,
"POLARSSL_MD_SHA256" ) == 0 )
521 #endif // POLARSSL_RSA_C
522 #ifdef POLARSSL_RSA_C
523 if( strcmp( str,
"POLARSSL_ERR_RSA_VERIFY_FAILED" ) == 0 )
528 #endif // POLARSSL_RSA_C
529 #ifdef POLARSSL_ECDSA_C
530 if( strcmp( str,
"POLARSSL_ERR_ECP_VERIFY_FAILED" ) == 0 )
535 #endif // POLARSSL_ECDSA_C
536 if( strcmp( str,
"POLARSSL_ERR_PK_TYPE_MISMATCH" ) == 0 )
541 #ifdef POLARSSL_RSA_C
542 if( strcmp( str,
"-1" ) == 0 )
547 #endif // POLARSSL_RSA_C
550 printf(
"Expected integer for parameter and got: %s\n", str );
554 void test_suite_pk_utils(
int type,
int size,
int len,
char *name )
573 #ifdef POLARSSL_RSA_C
574 void test_suite_pk_rsa_verify_test_vec(
char *message_hex_string,
int digest,
575 int mod,
int radix_N,
char *input_N,
int radix_E,
576 char *input_E,
char *result_hex_str,
int result )
578 unsigned char message_str[1000];
579 unsigned char hash_result[1000];
580 unsigned char result_str[1000];
587 memset( message_str, 0x00, 1000 );
588 memset( hash_result, 0x00, 1000 );
589 memset( result_str, 0x00, 1000 );
598 msg_len =
unhexify( message_str, message_hex_string );
599 unhexify( result_str, result_hex_str );
612 #ifdef POLARSSL_RSA_C
613 void test_suite_pk_rsa_verify_ext_test_vec(
char *message_hex_string,
int digest,
614 int mod,
int radix_N,
char *input_N,
int radix_E,
615 char *input_E,
char *result_hex_str,
616 int pk_type,
int mgf1_hash_id,
int salt_len,
619 unsigned char message_str[1000];
620 unsigned char hash_result[1000];
621 unsigned char result_str[1000];
631 memset( message_str, 0x00, 1000 );
632 memset( hash_result, 0x00, 1000 );
633 memset( result_str, 0x00, 1000 );
642 msg_len =
unhexify( message_str, message_hex_string );
643 unhexify( result_str, result_hex_str );
648 message_str, msg_len, hash_result ) == 0 );
653 memcpy( hash_result, message_str, msg_len );
657 if( mgf1_hash_id < 0 )
670 digest, hash_result, hash_len,
678 #ifdef POLARSSL_ECDSA_C
679 void test_suite_pk_ec_test_vec(
int type,
int id,
char *key_str,
680 char *hash_str,
char * sig_str,
int ret )
684 unsigned char hash[100], sig[500], key[500];
685 size_t hash_len, sig_len, key_len;
689 memset( hash, 0,
sizeof( hash ) ); hash_len =
unhexify(hash, hash_str);
690 memset( sig, 0,
sizeof( sig ) ); sig_len =
unhexify(sig, sig_str);
691 memset( key, 0,
sizeof( key ) ); key_len =
unhexify(key, key_str);
700 key, key_len ) == 0 );
703 hash, hash_len, sig, sig_len ) == ret );
710 void test_suite_pk_sign_verify(
int type,
int sign_ret,
int verify_ret )
713 unsigned char hash[50], sig[5000];
718 memset( hash, 0x2a,
sizeof hash );
719 memset( sig, 0,
sizeof sig );
728 hash,
sizeof hash, sig, sig_len ) == verify_ret );
734 #ifdef POLARSSL_RSA_C
735 void test_suite_pk_rsa_encrypt_test_vec(
char *message_hex,
int mod,
736 int radix_N,
char *input_N,
737 int radix_E,
char *input_E,
738 char *result_hex,
int ret )
740 unsigned char message[1000];
741 unsigned char output[1000];
742 unsigned char result[1000];
743 size_t msg_len, olen, res_len;
749 memset( message, 0,
sizeof( message ) );
750 memset( output, 0,
sizeof( output ) );
751 memset( result, 0,
sizeof( result ) );
753 msg_len =
unhexify( message, message_hex );
754 res_len =
unhexify( result, result_hex );
765 output, &olen,
sizeof( output ),
768 TEST_ASSERT( memcmp( output, result, olen ) == 0 );
775 #ifdef POLARSSL_RSA_C
776 void test_suite_pk_rsa_decrypt_test_vec(
char *cipher_hex,
int mod,
777 int radix_P,
char *input_P,
778 int radix_Q,
char *input_Q,
779 int radix_N,
char *input_N,
780 int radix_E,
char *input_E,
781 char *clear_hex,
int ret )
783 unsigned char clear[1000];
784 unsigned char output[1000];
785 unsigned char cipher[1000];
786 size_t clear_len, olen, cipher_len;
796 memset( clear, 0,
sizeof( clear ) );
797 memset( cipher, 0,
sizeof( cipher ) );
799 clear_len =
unhexify( clear, clear_hex );
800 cipher_len =
unhexify( cipher, cipher_hex );
824 memset( output, 0,
sizeof( output ) );
827 output, &olen,
sizeof( output ),
841 void test_suite_pk_ec_nocrypt(
int type )
844 unsigned char output[100];
845 unsigned char input[100];
853 memset( output, 0,
sizeof( output ) );
854 memset( input, 0,
sizeof( input ) );
859 output, &olen,
sizeof( output ),
863 output, &olen,
sizeof( output ),
870 #ifdef POLARSSL_RSA_C
871 void test_suite_pk_rsa_alt( )
881 unsigned char hash[50], sig[1000];
882 unsigned char msg[50], ciph[1000], test[1000];
883 size_t sig_len, ciph_len, test_len;
889 memset( hash, 0x2a,
sizeof hash );
890 memset( sig, 0,
sizeof sig );
891 memset( msg, 0x2a,
sizeof msg );
892 memset( ciph, 0,
sizeof ciph );
893 memset( test, 0,
sizeof test );
919 hash,
sizeof hash, sig, sig_len ) == 0 );
923 ciph, &ciph_len,
sizeof ciph,
926 test, &test_len,
sizeof test,
933 ciph, &ciph_len,
sizeof ciph,
936 hash,
sizeof hash, sig, sig_len ) == ret );
954 if( strcmp( str,
"POLARSSL_GENPRIME" ) == 0 )
956 #if defined(POLARSSL_GENPRIME)
962 if( strcmp( str,
"POLARSSL_ECDSA_C" ) == 0 )
964 #if defined(POLARSSL_ECDSA_C)
970 if( strcmp( str,
"POLARSSL_SHA256_C" ) == 0 )
972 #if defined(POLARSSL_SHA256_C)
978 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
980 #if defined(POLARSSL_SHA1_C)
986 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
988 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
994 if( strcmp( str,
"POLARSSL_PKCS1_V15" ) == 0 )
996 #if defined(POLARSSL_PKCS1_V15)
1002 if( strcmp( str,
"POLARSSL_ECP_C" ) == 0 )
1004 #if defined(POLARSSL_ECP_C)
1010 if( strcmp( str,
"POLARSSL_PKCS1_V21" ) == 0 )
1012 #if defined(POLARSSL_PKCS1_V21)
1018 if( strcmp( str,
"POLARSSL_RSA_C" ) == 0 )
1020 #if defined(POLARSSL_RSA_C)
1037 #if defined(TEST_SUITE_ACTIVE)
1038 if( strcmp( params[0],
"pk_utils" ) == 0 )
1044 char *param4 = params[4];
1048 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1052 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1053 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1054 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1057 test_suite_pk_utils( param1, param2, param3, param4 );
1063 if( strcmp( params[0],
"pk_rsa_verify_test_vec" ) == 0 )
1065 #ifdef POLARSSL_RSA_C
1067 char *param1 = params[1];
1071 char *param5 = params[5];
1073 char *param7 = params[7];
1074 char *param8 = params[8];
1079 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 10 );
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 );
1088 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1091 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1093 test_suite_pk_rsa_verify_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9 );
1100 if( strcmp( params[0],
"pk_rsa_verify_ext_test_vec" ) == 0 )
1102 #ifdef POLARSSL_RSA_C
1104 char *param1 = params[1];
1108 char *param5 = params[5];
1110 char *param7 = params[7];
1111 char *param8 = params[8];
1119 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 13 );
1124 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1125 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1126 if(
verify_int( params[4], ¶m4 ) != 0 )
return( 2 );
1128 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1131 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1132 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
1133 if(
verify_int( params[11], ¶m11 ) != 0 )
return( 2 );
1134 if(
verify_int( params[12], ¶m12 ) != 0 )
return( 2 );
1136 test_suite_pk_rsa_verify_ext_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12 );
1143 if( strcmp( params[0],
"pk_ec_test_vec" ) == 0 )
1145 #ifdef POLARSSL_ECDSA_C
1149 char *param3 = params[3];
1150 char *param4 = params[4];
1151 char *param5 = params[5];
1156 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1160 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1161 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1165 if(
verify_int( params[6], ¶m6 ) != 0 )
return( 2 );
1167 test_suite_pk_ec_test_vec( param1, param2, param3, param4, param5, param6 );
1174 if( strcmp( params[0],
"pk_sign_verify" ) == 0 )
1183 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1187 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1188 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1189 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1191 test_suite_pk_sign_verify( param1, param2, param3 );
1197 if( strcmp( params[0],
"pk_rsa_encrypt_test_vec" ) == 0 )
1199 #ifdef POLARSSL_RSA_C
1201 char *param1 = params[1];
1204 char *param4 = params[4];
1206 char *param6 = params[6];
1207 char *param7 = params[7];
1212 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 9 );
1217 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1218 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1220 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1223 if(
verify_int( params[8], ¶m8 ) != 0 )
return( 2 );
1225 test_suite_pk_rsa_encrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8 );
1232 if( strcmp( params[0],
"pk_rsa_decrypt_test_vec" ) == 0 )
1234 #ifdef POLARSSL_RSA_C
1236 char *param1 = params[1];
1239 char *param4 = params[4];
1241 char *param6 = params[6];
1243 char *param8 = params[8];
1245 char *param10 = params[10];
1246 char *param11 = params[11];
1251 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 13 );
1256 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
1257 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
1259 if(
verify_int( params[5], ¶m5 ) != 0 )
return( 2 );
1261 if(
verify_int( params[7], ¶m7 ) != 0 )
return( 2 );
1263 if(
verify_int( params[9], ¶m9 ) != 0 )
return( 2 );
1266 if(
verify_int( params[12], ¶m12 ) != 0 )
return( 2 );
1268 test_suite_pk_rsa_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12 );
1275 if( strcmp( params[0],
"pk_ec_nocrypt" ) == 0 )
1282 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 2 );
1286 if(
verify_int( params[1], ¶m1 ) != 0 )
return( 2 );
1288 test_suite_pk_ec_nocrypt( param1 );
1294 if( strcmp( params[0],
"pk_rsa_alt" ) == 0 )
1296 #ifdef POLARSSL_RSA_C
1301 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1306 test_suite_pk_rsa_alt( );
1315 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
1329 ret = fgets( buf, len, f );
1333 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
1334 buf[strlen(buf) - 1] =
'\0';
1335 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
1336 buf[strlen(buf) - 1] =
'\0';
1347 params[cnt++] = cur;
1349 while( *p !=
'\0' && p < buf + len )
1359 if( p + 1 < buf + len )
1362 params[cnt++] = cur;
1371 for( i = 0; i < cnt; i++ )
1378 if( *p ==
'\\' && *(p + 1) ==
'n' )
1383 else if( *p ==
'\\' && *(p + 1) ==
':' )
1388 else if( *p ==
'\\' && *(p + 1) ==
'?' )
1404 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1405 const char *filename =
"/builddir/build/BUILD/polarssl-1.3.9/tests/suites/test_suite_pk.data";
1410 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1411 unsigned char alloc_buf[1000000];
1415 file = fopen( filename,
"r" );
1418 fprintf( stderr,
"Failed to open\n" );
1422 while( !feof( file ) )
1426 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1428 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
1429 fprintf( stdout,
" " );
1430 for( i = strlen( buf ) + 1; i < 67; i++ )
1431 fprintf( stdout,
"." );
1432 fprintf( stdout,
" " );
1437 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1441 if( strcmp( params[0],
"depends_on" ) == 0 )
1443 for( i = 1; i < cnt; i++ )
1447 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1458 if( skip == 1 || ret == 3 )
1461 fprintf( stdout,
"----\n" );
1464 else if( ret == 0 && test_errors == 0 )
1466 fprintf( stdout,
"PASS\n" );
1471 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1478 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1480 if( strlen(buf) != 0 )
1482 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1488 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1489 if( total_errors == 0 )
1490 fprintf( stdout,
"PASSED" );
1492 fprintf( stdout,
"FAILED" );
1494 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1495 total_tests - total_errors, total_tests, total_skipped );
1497 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1498 #if defined(POLARSSL_MEMORY_DEBUG)
1499 memory_buffer_alloc_status();
1504 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.
Memory allocation layer (Deprecated to platform layer)
int rsa_copy(rsa_context *dst, const rsa_context *src)
Copy the components of an RSA context.
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)
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
size_t pk_get_size(const pk_context *ctx)
Get the size in bits of the underlying key.
Options for RSASSA-PSS signature verification.
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 (including padding if relevant).
int pk_debug(const pk_context *ctx, pk_debug_item *items)
Export debug information.
Configuration options (set of defines)
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.
int parse_arguments(char *buf, size_t len, char *params[50])
#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, const 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.
Public Key abstraction layer.
int pk_init_ctx_rsa_alt(pk_context *ctx, void *key, pk_rsa_alt_decrypt_func decrypt_func, pk_rsa_alt_sign_func sign_func, pk_rsa_alt_key_len_func key_len_func)
Initialize an RSA-alt context.
#define POLARSSL_ERR_PK_BAD_INPUT_DATA
Bad input parameters to function.
int pk_verify_ext(pk_type_t type, const void *options, 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, with options.
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.
#define POLARSSL_ERR_ECP_VERIFY_FAILED
The signature is not valid.
size_t(* rsa_key_len_func)(void *ctx)
#define POLARSSL_ERR_RSA_INVALID_PADDING
Input data contains invalid padding and is rejected.
Item to send to the debug module.
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
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 (including padding if relevant).
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
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.
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(* rsa_sign_func)(void *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)
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.
#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 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 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 get_line(FILE *f, char *buf, size_t len)
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
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, including padding if relevant.
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.
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 (including padding if relevant).
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(* rsa_decrypt_func)(void *ctx, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)