mbedtls/tests/suites/test_suite_rsa.function
Paul Bakker 37940d9ff6 - Added test coverage for X509parse
- Fixed segfault in rsa_check_privkey() and rsa_check_pubkey() and added test
2009-07-10 22:38:58 +00:00

198 lines
6.6 KiB
Plaintext

BEGIN_HEADER
#include <polarssl/rsa.h>
#include <polarssl/sha1.h>
#include <polarssl/sha2.h>
#include <polarssl/sha4.h>
END_HEADER
BEGIN_CASE
rsa_pkcs1_sign:message_hex_string:digest:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:result_hex_str
{
unsigned char message_str[1000];
unsigned char hash_result[1000];
unsigned char output[1000];
unsigned char output_str[1000];
rsa_context ctx;
mpi P1, Q1, H, G;
mpi_init( &P1, &Q1, &H, &G, NULL );
rsa_init( &ctx, RSA_PKCS_V15, 0, NULL, NULL );
memset( message_str, 0x00, 1000 );
memset( hash_result, 0x00, 1000 );
memset( output, 0x00, 1000 );
memset( output_str, 0x00, 1000 );
ctx.len = {mod} / 8;
TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
int msg_len = unhexify( message_str, {message_hex_string} );
if( {digest} == SIG_RSA_SHA1 )
sha1( message_str, msg_len, hash_result );
else if( {digest} == SIG_RSA_SHA224 )
sha2( message_str, msg_len, hash_result, 1 );
else if( {digest} == SIG_RSA_SHA256 )
sha2( message_str, msg_len, hash_result, 0 );
else if( {digest} == SIG_RSA_SHA384 )
sha4( message_str, msg_len, hash_result, 1 );
else if( {digest} == SIG_RSA_SHA512 )
sha4( message_str, msg_len, hash_result, 0 );
else
TEST_ASSERT( 0 );
TEST_ASSERT( rsa_pkcs1_sign( &ctx, RSA_PRIVATE, {digest}, 0, hash_result, output ) == 0 );
hexify( output_str, output, ctx.len );
TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
}
END_CASE
BEGIN_CASE
rsa_pkcs1_verify:message_hex_string:digest:mod:radix_N:input_N:radix_E:input_E:result_hex_str:correct
{
unsigned char message_str[1000];
unsigned char hash_result[1000];
unsigned char result_str[1000];
rsa_context ctx;
rsa_init( &ctx, RSA_PKCS_V15, 0, NULL, NULL );
memset( message_str, 0x00, 1000 );
memset( hash_result, 0x00, 1000 );
memset( result_str, 0x00, 1000 );
ctx.len = {mod} / 8;
TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
int msg_len = unhexify( message_str, {message_hex_string} );
unhexify( result_str, {result_hex_str} );
if( {digest} == SIG_RSA_SHA1 )
sha1( message_str, msg_len, hash_result );
else if( {digest} == SIG_RSA_SHA224 )
sha2( message_str, msg_len, hash_result, 1 );
else if( {digest} == SIG_RSA_SHA256 )
sha2( message_str, msg_len, hash_result, 0 );
else if( {digest} == SIG_RSA_SHA384 )
sha4( message_str, msg_len, hash_result, 1 );
else if( {digest} == SIG_RSA_SHA512 )
sha4( message_str, msg_len, hash_result, 0 );
else
TEST_ASSERT( 0 );
TEST_ASSERT( rsa_pkcs1_verify( &ctx, RSA_PUBLIC, {digest}, 0, hash_result, result_str ) == {correct} );
}
END_CASE
BEGIN_CASE
rsa_pkcs1_encrypt:message_hex_string:mod:radix_N:input_N:radix_E:input_E:result_hex_str
{
unsigned char message_str[1000];
unsigned char hash_result[1000];
unsigned char output[1000];
unsigned char output_str[1000];
rsa_context ctx;
rsa_init( &ctx, RSA_PKCS_V15, 0, NULL, NULL );
memset( message_str, 0x00, 1000 );
memset( hash_result, 0x00, 1000 );
memset( output, 0x00, 1000 );
memset( output_str, 0x00, 1000 );
ctx.len = {mod} / 8;
TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
int msg_len = unhexify( message_str, {message_hex_string} );
TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, RSA_PUBLIC, msg_len, message_str, output ) == 0 );
hexify( output_str, output, ctx.len );
TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
}
END_CASE
BEGIN_CASE
rsa_pkcs1_decrypt:message_hex_string:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:result_hex_str
{
unsigned char message_str[1000];
unsigned char hash_result[1000];
unsigned char output[1000];
unsigned char output_str[1000];
rsa_context ctx;
mpi P1, Q1, H, G;
mpi_init( &P1, &Q1, &H, &G, NULL );
rsa_init( &ctx, RSA_PKCS_V15, 0, NULL, NULL );
memset( message_str, 0x00, 1000 );
memset( hash_result, 0x00, 1000 );
memset( output, 0x00, 1000 );
memset( output_str, 0x00, 1000 );
ctx.len = {mod} / 8;
TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
unhexify( message_str, {message_hex_string} );
int output_len = 0;
TEST_ASSERT( rsa_pkcs1_decrypt( &ctx, RSA_PRIVATE, &output_len, message_str, output, 1000 ) == 0 );
hexify( output_str, output, ctx.len );
TEST_ASSERT( strncasecmp( (char *) output_str, {result_hex_str}, strlen( {result_hex_str} ) ) == 0 );
}
END_CASE
BEGIN_CASE
rsa_check_privkey_null:
{
rsa_context ctx;
memset( &ctx, 0x00, sizeof( rsa_context ) );
TEST_ASSERT( rsa_check_privkey( &ctx ) == POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
}
END_CASE
BEGIN_CASE
rsa_selftest:
{
TEST_ASSERT( rsa_self_test( 0 ) == 0 );
}
END_CASE