Merged Public Key framwork tests

This commit is contained in:
Paul Bakker 2013-10-28 14:03:26 +01:00
commit 08bb187bb6
7 changed files with 427 additions and 4 deletions

View File

@ -1,6 +1,9 @@
PolarSSL ChangeLog (Sorted per branch, date) PolarSSL ChangeLog (Sorted per branch, date)
= PolarSSL 1.3 branch = PolarSSL 1.3 branch
Features
* PK tests added to test framework
Bugfix Bugfix
* Server does not send out extensions not advertised by client * Server does not send out extensions not advertised by client
* Prevent possible alignment warnings on casting from char * to 'aligned *' * Prevent possible alignment warnings on casting from char * to 'aligned *'

View File

@ -33,9 +33,14 @@
#include <stdio.h> #include <stdio.h>
/* /*
* Uncomment to force use of a specific curve * Uncomment to show key and signature details
#define ECPARAMS POLARSSL_ECP_DP_SECP256R1
*/ */
#define VERBOSE
/*
* Uncomment to force use of a specific curve
*/
#define ECPARAMS POLARSSL_ECP_DP_SECP192R1
#if !defined(ECPARAMS) #if !defined(ECPARAMS)
#define ECPARAMS ecp_curve_list()->grp_id #define ECPARAMS ecp_curve_list()->grp_id
@ -53,6 +58,38 @@ int main( int argc, char *argv[] )
return( 0 ); return( 0 );
} }
#else #else
#if defined(VERBOSE)
static void dump_buf( char *title, unsigned char *buf, size_t len )
{
size_t i;
printf( "%s", title );
for( i = 0; i < len; i++ )
printf("%c%c", "0123456789ABCDEF" [buf[i] / 16],
"0123456789ABCDEF" [buf[i] % 16] );
printf( "\n" );
}
static void dump_pubkey( char *title, ecdsa_context *key )
{
unsigned char buf[300];
size_t len;
if( ecp_point_write_binary( &key->grp, &key->Q,
POLARSSL_ECP_PF_UNCOMPRESSED, &len, buf, sizeof buf ) != 0 )
{
printf("internal error\n");
return;
}
dump_buf( title, buf, len );
}
#else
#define dump_buf( a, b, c )
#define dump_pubkey( a, b )
#endif
int main( int argc, char *argv[] ) int main( int argc, char *argv[] )
{ {
int ret; int ret;
@ -109,6 +146,8 @@ int main( int argc, char *argv[] )
printf( " ok (key size: %d bits)\n", (int) ctx_sign.grp.pbits ); printf( " ok (key size: %d bits)\n", (int) ctx_sign.grp.pbits );
dump_pubkey( " + Public key: ", &ctx_sign );
/* /*
* Sign some message hash * Sign some message hash
*/ */
@ -125,6 +164,9 @@ int main( int argc, char *argv[] )
} }
printf( " ok (signature length = %zu)\n", sig_len ); printf( " ok (signature length = %zu)\n", sig_len );
dump_buf( " + Hash: ", hash, sizeof hash );
dump_buf( " + Signature: ", sig, sig_len );
/* /*
* Signature is serialized as defined by RFC 4492 p. 20, * Signature is serialized as defined by RFC 4492 p. 20,
* but one can also access 'r' and 's' directly from the context * but one can also access 'r' and 's' directly from the context

View File

@ -68,6 +68,7 @@ add_test_suite(mpi)
add_test_suite(pbkdf2) add_test_suite(pbkdf2)
add_test_suite(pkcs1_v21) add_test_suite(pkcs1_v21)
add_test_suite(pkcs5) add_test_suite(pkcs5)
add_test_suite(pk)
add_test_suite(pkparse) add_test_suite(pkparse)
add_test_suite(pkwrite) add_test_suite(pkwrite)
add_test_suite(shax) add_test_suite(shax)

View File

@ -48,6 +48,7 @@ APPS = test_suite_aes.ecb test_suite_aes.cbc \
test_suite_mpi test_suite_pbkdf2 \ test_suite_mpi test_suite_pbkdf2 \
test_suite_pkcs1_v21 test_suite_pkcs5 \ test_suite_pkcs1_v21 test_suite_pkcs5 \
test_suite_pkparse test_suite_pkwrite \ test_suite_pkparse test_suite_pkwrite \
test_suite_pk \
test_suite_rsa test_suite_shax \ test_suite_rsa test_suite_shax \
test_suite_x509parse test_suite_x509write \ test_suite_x509parse test_suite_x509write \
test_suite_xtea test_suite_version test_suite_xtea test_suite_version

View File

@ -144,7 +144,12 @@ while($test_cases =~ /\/\* BEGIN_CASE *([\w:]*) \*\/\n(.*?)\n\/\* END_CASE \*\//
my @res = $test_data =~ /^$mapping_regex/msg; my @res = $test_data =~ /^$mapping_regex/msg;
foreach my $value (@res) foreach my $value (@res)
{ {
$mapping_values{$value} = 1 if ($value !~ /^\d+$/); next unless ($value !~ /^\d+$/);
if ( $mapping_values{$value} ) {
${ $mapping_values{$value} }{$function_pre_code} = 1;
} else {
$mapping_values{$value} = { $function_pre_code => 1 };
}
} }
} }
@ -204,13 +209,23 @@ END
# Make mapping code # Make mapping code
while( my ($key, $value) = each(%mapping_values) ) while( my ($key, $value) = each(%mapping_values) )
{ {
$mapping_code .= << "END"; my $key_mapping_code = << "END";
if( strcmp( str, "$key" ) == 0 ) if( strcmp( str, "$key" ) == 0 )
{ {
*value = ( $key ); *value = ( $key );
return( 0 ); return( 0 );
} }
END END
# handle depenencies, unless used at least one without depends
if ($value->{""}) {
$mapping_code .= $key_mapping_code;
next;
}
for my $ifdef ( keys %$value ) {
(my $endif = $ifdef) =~ s!ifdef!endif //!g;
$mapping_code .= $ifdef . $key_mapping_code . $endif;
}
} }
$dispatch_code =~ s/^(.+)/ $1/mg; $dispatch_code =~ s/^(.+)/ $1/mg;

View File

@ -0,0 +1,79 @@
PK utils: RSA
depends_on:POLARSSL_RSA_C
pk_utils:POLARSSL_PK_RSA:512:64:"RSA"
PK utils: ECKEY
depends_on:POLARSSL_ECP_C
pk_utils:POLARSSL_PK_ECKEY:192:24:"EC"
PK utils: ECKEY_DH
depends_on:POLARSSL_ECP_C
pk_utils:POLARSSL_PK_ECKEY_DH:192:24:"EC_DH"
PK utils: ECDSA
depends_on:POLARSSL_ECDSA_C
pk_utils:POLARSSL_PK_ECDSA:192:24:"ECDSA"
RSA verify test vector #1 (good)
depends_on:POLARSSL_SHA1_C:POLARSSL_PKCS1_V15
pk_rsa_verify_test_vec:"206ef4bf396c6087f8229ef196fd35f37ccb8de5efcdb238f20d556668f114257a11fbe038464a67830378e62ae9791453953dac1dbd7921837ba98e84e856eb80ed9487e656d0b20c28c8ba5e35db1abbed83ed1c7720a97701f709e3547a4bfcabca9c89c57ad15c3996577a0ae36d7c7b699035242f37954646c1cd5c08ac":POLARSSL_MD_SHA1:1024:16:"e28a13548525e5f36dccb24ecb7cc332cc689dfd64012604c9c7816d72a16c3f5fcdc0e86e7c03280b1c69b586ce0cd8aec722cc73a5d3b730310bf7dfebdc77ce5d94bbc369dc18a2f7b07bd505ab0f82224aef09fdc1e5063234255e0b3c40a52e9e8ae60898eb88a766bdd788fe9493d8fd86bcdd2884d5c06216c65469e5":16:"3":"5abc01f5de25b70867ff0c24e222c61f53c88daf42586fddcd56f3c4588f074be3c328056c063388688b6385a8167957c6e5355a510e005b8a851d69c96b36ec6036644078210e5d7d326f96365ee0648882921492bc7b753eb9c26cdbab37555f210df2ca6fec1b25b463d38b81c0dcea202022b04af5da58aa03d77be949b7":0
RSA verify test vector #2 (bad)
depends_on:POLARSSL_SHA1_C:POLARSSL_PKCS1_V15
pk_rsa_verify_test_vec:"d6248c3e96b1a7e5fea978870fcc4c9786b4e5156e16b7faef4557d667f730b8bc4c784ef00c624df5309513c3a5de8ca94c2152e0459618666d3148092562ebc256ffca45b27fd2d63c68bd5e0a0aefbe496e9e63838a361b1db6fc272464f191490bf9c029643c49d2d9cd08833b8a70b4b3431f56fb1eb55ccd39e77a9c92":POLARSSL_MD_SHA1:1024:16:"e28a13548525e5f36dccb24ecb7cc332cc689dfd64012604c9c7816d72a16c3f5fcdc0e86e7c03280b1c69b586ce0cd8aec722cc73a5d3b730310bf7dfebdc77ce5d94bbc369dc18a2f7b07bd505ab0f82224aef09fdc1e5063234255e0b3c40a52e9e8ae60898eb88a766bdd788fe9493d8fd86bcdd2884d5c06216c65469e5":16:"3":"3203b7647fb7e345aa457681e5131777f1adc371f2fba8534928c4e52ef6206a856425d6269352ecbf64db2f6ad82397768cafdd8cd272e512d617ad67992226da6bc291c31404c17fd4b7e2beb20eff284a44f4d7af47fd6629e2c95809fa7f2241a04f70ac70d3271bb13258af1ed5c5988c95df7fa26603515791075feccd":POLARSSL_ERR_RSA_VERIFY_FAILED
ECDSA verify test vector #1 (good)
depends_on:POLARSSL_ECP_DP_SECP192R1_ENABLED
pk_ec_test_vec:POLARSSL_PK_ECDSA:POLARSSL_ECP_DP_SECP192R1:"046FDD3028FA94A863CD4F78DBFF8B3AA561FC6D9CCBBCA88E0AE6FA437F5415F957542D0717FF8B84562DAE99872EF841":"546869732073686F756C64206265207468652068617368206F662061206D6573736167652E00":"30350218185B2A7FB5CD9C9A8488B119B68B47D6EC833509CE9FA1FF021900FB7D259A744A2348BD45D241A39DC915B81CC2084100FA24":0
ECDSA verify test vector #2 (bad)
depends_on:POLARSSL_ECP_DP_SECP192R1_ENABLED
pk_ec_test_vec:POLARSSL_PK_ECDSA:POLARSSL_ECP_DP_SECP192R1:"046FDD3028FA94A863CD4F78DBFF8B3AA561FC6D9CCBBCA88E0AE6FA437F5415F957542D0717FF8B84562DAE99872EF841":"546869732073686F756C64206265207468652068617368206F662061206D6573736167652E00":"30350218185B2A7FB5CD9C9A8488B119B68B47D6EC833509CE9FA1FF021900FB7D259A744A2348BD45D241A39DC915B81CC2084100FA25":POLARSSL_ERR_ECP_VERIFY_FAILED
EC(DSA) verify test vector #1 (good)
depends_on:POLARSSL_ECP_DP_SECP192R1_ENABLED
pk_ec_test_vec:POLARSSL_PK_ECKEY:POLARSSL_ECP_DP_SECP192R1:"046FDD3028FA94A863CD4F78DBFF8B3AA561FC6D9CCBBCA88E0AE6FA437F5415F957542D0717FF8B84562DAE99872EF841":"546869732073686F756C64206265207468652068617368206F662061206D6573736167652E00":"30350218185B2A7FB5CD9C9A8488B119B68B47D6EC833509CE9FA1FF021900FB7D259A744A2348BD45D241A39DC915B81CC2084100FA24":0
EC(DSA) verify test vector #2 (bad)
depends_on:POLARSSL_ECP_DP_SECP192R1_ENABLED
pk_ec_test_vec:POLARSSL_PK_ECKEY:POLARSSL_ECP_DP_SECP192R1:"046FDD3028FA94A863CD4F78DBFF8B3AA561FC6D9CCBBCA88E0AE6FA437F5415F957542D0717FF8B84562DAE99872EF841":"546869732073686F756C64206265207468652068617368206F662061206D6573736167652E00":"30350218185B2A7FB5CD9C9A8488B119B68B47D6EC833509CE9FA1FF021900FB7D259A744A2348BD45D241A39DC915B81CC2084100FA25":POLARSSL_ERR_ECP_VERIFY_FAILED
ECDSA sign-verify
depends_on:POLARSSL_ECDSA_C:POLARSSL_ECP_DP_SECP192R1_ENABLED
pk_sign_verify:POLARSSL_PK_ECDSA:0:0
EC(DSA) sign-verify
depends_on:POLARSSL_ECDSA_C:POLARSSL_ECP_DP_SECP192R1_ENABLED
pk_sign_verify:POLARSSL_PK_ECKEY:0:0
EC_DH (no) sign-verify
depends_on:POLARSSL_ECP_C:POLARSSL_ECP_DP_SECP192R1_ENABLED
pk_sign_verify:POLARSSL_PK_ECKEY_DH:POLARSSL_ERR_PK_TYPE_MISMATCH:POLARSSL_ERR_PK_TYPE_MISMATCH
RSA sign-verify
depends_on:POLARSSL_RSA_C:POLARSSL_PKCS1_V15
pk_sign_verify:POLARSSL_PK_RSA:0:0
RSA encrypt test vector
depends_on:POLARSSL_PKCS1_V15
pk_rsa_encrypt_test_vec:"4E636AF98E40F3ADCFCCB698F4E80B9F":2048:16:"b38ac65c8141f7f5c96e14470e851936a67bf94cc6821a39ac12c05f7c0b06d9e6ddba2224703b02e25f31452f9c4a8417b62675fdc6df46b94813bc7b9769a892c482b830bfe0ad42e46668ace68903617faf6681f4babf1cc8e4b0420d3c7f61dc45434c6b54e2c3ee0fc07908509d79c9826e673bf8363255adb0add2401039a7bcd1b4ecf0fbe6ec8369d2da486eec59559dd1d54c9b24190965eafbdab203b35255765261cd0909acf93c3b8b8428cbb448de4715d1b813d0c94829c229543d391ce0adab5351f97a3810c1f73d7b1458b97daed4209c50e16d064d2d5bfda8c23893d755222793146d0a78c3d64f35549141486c3b0961a7b4c1a2034f":16:"3":"b0c0b193ba4a5b4502bfacd1a9c2697da5510f3e3ab7274cf404418afd2c62c89b98d83bbc21c8c1bf1afe6d8bf40425e053e9c03e03a3be0edbe1eda073fade1cc286cc0305a493d98fe795634c3cad7feb513edb742d66d910c87d07f6b0055c3488bb262b5fd1ce8747af64801fb39d2d3a3e57086ffe55ab8d0a2ca86975629a0f85767a4990c532a7c2dab1647997ebb234d0b28a0008bfebfc905e7ba5b30b60566a5e0190417465efdbf549934b8f0c5c9f36b7c5b6373a47ae553ced0608a161b1b70dfa509375cf7a3598223a6d7b7a1d1a06ac74d345a9bb7c0e44c8388858a4f1d8115f2bd769ffa69020385fa286302c80e950f9e2751308666c":0
RSA decrypt test vector #1
depends_on:POLARSSL_PKCS1_V15
pk_rsa_decrypt_test_vec:"a42eda41e56235e666e7faaa77100197f657288a1bf183e4820f0c37ce2c456b960278d6003e0bbcd4be4a969f8e8fd9231e1f492414f00ed09844994c86ec32db7cde3bec7f0c3dbf6ae55baeb2712fa609f5fc3207a824eb3dace31849cd6a6084318523912bccb84cf42e3c6d6d1685131d69bb545acec827d2b0dfdd5568b7dcc4f5a11d6916583fefa689d367f8c9e1d95dcd2240895a9470b0c1730f97cd6e8546860bd254801769f54be96e16362ddcbf34d56035028890199e0f48db38642cb66a4181e028a6443a404fea284ce02b4614b683367d40874e505611d23142d49f06feea831d52d347b13610b413c4efc43a6de9f0b08d2a951dc503b6":2048:16:"e79a373182bfaa722eb035f772ad2a9464bd842de59432c18bbab3a7dfeae318c9b915ee487861ab665a40bd6cda560152578e8579016c929df99fea05b4d64efca1d543850bc8164b40d71ed7f3fa4105df0fb9b9ad2a18ce182c8a4f4f975bea9aa0b9a1438a27a28e97ac8330ef37383414d1bd64607d6979ac050424fd17":16:"c6749cbb0db8c5a177672d4728a8b22392b2fc4d3b8361d5c0d5055a1b4e46d821f757c24eef2a51c561941b93b3ace7340074c058c9bb48e7e7414f42c41da4cccb5c2ba91deb30c586b7fb18af12a52995592ad139d3be429add6547e044becedaf31fa3b39421e24ee034fbf367d11f6b8f88ee483d163b431e1654ad3e89":16:"b38ac65c8141f7f5c96e14470e851936a67bf94cc6821a39ac12c05f7c0b06d9e6ddba2224703b02e25f31452f9c4a8417b62675fdc6df46b94813bc7b9769a892c482b830bfe0ad42e46668ace68903617faf6681f4babf1cc8e4b0420d3c7f61dc45434c6b54e2c3ee0fc07908509d79c9826e673bf8363255adb0add2401039a7bcd1b4ecf0fbe6ec8369d2da486eec59559dd1d54c9b24190965eafbdab203b35255765261cd0909acf93c3b8b8428cbb448de4715d1b813d0c94829c229543d391ce0adab5351f97a3810c1f73d7b1458b97daed4209c50e16d064d2d5bfda8c23893d755222793146d0a78c3d64f35549141486c3b0961a7b4c1a2034f":16:"3":"4E636AF98E40F3ADCFCCB698F4E80B9F":0
RSA decrypt test vector #2
depends_on:POLARSSL_PKCS1_V15
pk_rsa_decrypt_test_vec:"a42eda41e56235e666e7faaa77100197f657288a1bf183e4820f0c37ce2c456b960278d6003e0bbcd4be4a969f8e8fd9231e1f492414f00ed09844994c86ec32db7cde3bec7f0c3dbf6ae55baeb2712fa609f5fc3207a824eb3dace31849cd6a6084318523912bccb84cf42e3c6d6d1685131d69bb545acec827d2b0dfdd5568b7dcc4f5a11d6916583fefa689d367f8c9e1d95dcd2240895a9470b0c1730f97cd6e8546860bd254801769f54be96e16362ddcbf34d56035028890199e0f48db38642cb66a4181e028a6443a404feb284ce02b4614b683367d40874e505611d23142d49f06feea831d52d347b13610b413c4efc43a6de9f0b08d2a951dc503b6":2048:16:"e79a373182bfaa722eb035f772ad2a9464bd842de59432c18bbab3a7dfeae318c9b915ee487861ab665a40bd6cda560152578e8579016c929df99fea05b4d64efca1d543850bc8164b40d71ed7f3fa4105df0fb9b9ad2a18ce182c8a4f4f975bea9aa0b9a1438a27a28e97ac8330ef37383414d1bd64607d6979ac050424fd17":16:"c6749cbb0db8c5a177672d4728a8b22392b2fc4d3b8361d5c0d5055a1b4e46d821f757c24eef2a51c561941b93b3ace7340074c058c9bb48e7e7414f42c41da4cccb5c2ba91deb30c586b7fb18af12a52995592ad139d3be429add6547e044becedaf31fa3b39421e24ee034fbf367d11f6b8f88ee483d163b431e1654ad3e89":16:"b38ac65c8141f7f5c96e14470e851936a67bf94cc6821a39ac12c05f7c0b06d9e6ddba2224703b02e25f31452f9c4a8417b62675fdc6df46b94813bc7b9769a892c482b830bfe0ad42e46668ace68903617faf6681f4babf1cc8e4b0420d3c7f61dc45434c6b54e2c3ee0fc07908509d79c9826e673bf8363255adb0add2401039a7bcd1b4ecf0fbe6ec8369d2da486eec59559dd1d54c9b24190965eafbdab203b35255765261cd0909acf93c3b8b8428cbb448de4715d1b813d0c94829c229543d391ce0adab5351f97a3810c1f73d7b1458b97daed4209c50e16d064d2d5bfda8c23893d755222793146d0a78c3d64f35549141486c3b0961a7b4c1a2034f":16:"3":"4E636AF98E40F3ADCFCCB698F4E80B9F":POLARSSL_ERR_RSA_INVALID_PADDING
EC nocrypt
depends_on:POLARSSL_ECP_C
pk_ec_nocrypt:POLARSSL_PK_ECKEY
EC-DH nocrypt
depends_on:POLARSSL_ECP_C
pk_ec_nocrypt:POLARSSL_PK_ECKEY_DH
ECDSA nocrypt
depends_on:POLARSSL_ECDSA_C
pk_ec_nocrypt:POLARSSL_PK_ECDSA

View File

@ -0,0 +1,282 @@
/* BEGIN_HEADER */
#include <polarssl/pk.h>
static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len );
static int pk_genkey( pk_context *pk )
{
#if defined(POLARSSL_RSA_C)
if( pk_get_type( pk ) == POLARSSL_PK_RSA )
return rsa_gen_key( pk_rsa( *pk ), rnd_std_rand, NULL, 512, 3 );
#endif
#if defined(POLARSSL_ECP_C)
if( pk_get_type( pk ) == POLARSSL_PK_ECKEY ||
pk_get_type( pk ) == POLARSSL_PK_ECKEY_DH ||
pk_get_type( pk ) == POLARSSL_PK_ECDSA )
{
int ret;
if( ( ret = ecp_use_known_dp( &pk_ec( *pk )->grp,
POLARSSL_ECP_DP_SECP192R1 ) ) != 0 )
return( ret );
return ecp_gen_keypair( &pk_ec( *pk )->grp, &pk_ec( *pk )->d,
&pk_ec( *pk )->Q, rnd_std_rand, NULL );
}
#endif
return( -1 );
}
/* END_HEADER */
/* BEGIN_DEPENDENCIES
* depends_on:POLARSSL_PK_C
* END_DEPENDENCIES
*/
/* BEGIN_CASE */
void pk_utils( int type, int size, int len, char *name )
{
pk_context pk;
pk_init( &pk );
TEST_ASSERT( pk_init_ctx( &pk, pk_info_from_type( type ) ) == 0 );
TEST_ASSERT( pk_genkey( &pk ) == 0 );
TEST_ASSERT( (int) pk_get_type( &pk ) == type );
TEST_ASSERT( pk_can_do( &pk, type ) );
TEST_ASSERT( pk_get_size( &pk ) == (unsigned) size );
TEST_ASSERT( pk_get_len( &pk ) == (unsigned) len );
TEST_ASSERT( strcmp( pk_get_name( &pk), name ) == 0 );
pk_free( &pk );
}
/* END_CASE */
/* BEGIN_CASE depends_on:POLARSSL_RSA_C */
void pk_rsa_verify_test_vec( char *message_hex_string, int digest,
int mod, int radix_N, char *input_N, int radix_E,
char *input_E, char *result_hex_str, int result )
{
unsigned char message_str[1000];
unsigned char hash_result[1000];
unsigned char result_str[1000];
rsa_context *rsa;
pk_context pk;
int msg_len;
pk_init( &pk );
memset( message_str, 0x00, 1000 );
memset( hash_result, 0x00, 1000 );
memset( result_str, 0x00, 1000 );
TEST_ASSERT( pk_init_ctx( &pk, pk_info_from_type( POLARSSL_PK_RSA ) ) == 0 );
rsa = pk_rsa( pk );
rsa->len = mod / 8;
TEST_ASSERT( mpi_read_string( &rsa->N, radix_N, input_N ) == 0 );
TEST_ASSERT( mpi_read_string( &rsa->E, radix_E, input_E ) == 0 );
msg_len = unhexify( message_str, message_hex_string );
unhexify( result_str, result_hex_str );
if( md_info_from_type( digest ) != NULL )
TEST_ASSERT( md( md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
TEST_ASSERT( pk_verify( &pk, digest, hash_result, 0,
result_str, pk_get_len( &pk ) ) == result );
pk_free( &pk );
}
/* END_CASE */
/* BEGIN_CASE depends_on:POLARSSL_ECDSA_C */
void pk_ec_test_vec( int type, int id, char *key_str,
char *hash_str, char * sig_str, int ret )
{
pk_context pk;
ecp_keypair *eckey;
unsigned char hash[100], sig[500], key[500];
size_t hash_len, sig_len, key_len;
pk_init( &pk );
memset( hash, 0, sizeof( hash ) ); hash_len = unhexify(hash, hash_str);
memset( sig, 0, sizeof( sig ) ); sig_len = unhexify(sig, sig_str);
memset( key, 0, sizeof( key ) ); key_len = unhexify(key, key_str);
TEST_ASSERT( pk_init_ctx( &pk, pk_info_from_type( type ) ) == 0 );
TEST_ASSERT( pk_can_do( &pk, POLARSSL_PK_ECDSA ) );
eckey = pk_ec( pk );
TEST_ASSERT( ecp_use_known_dp( &eckey->grp, id ) == 0 );
TEST_ASSERT( ecp_point_read_binary( &eckey->grp, &eckey->Q,
key, key_len ) == 0 );
TEST_ASSERT( pk_verify( &pk, POLARSSL_MD_NONE,
hash, hash_len, sig, sig_len ) == ret );
pk_free( &pk );
}
/* END_CASE */
/* BEGIN_CASE */
void pk_sign_verify( int type, int sign_ret, int verify_ret )
{
pk_context pk;
unsigned char hash[50], sig[5000];
size_t sig_len;
pk_init( &pk );
memset( hash, 0x2a, sizeof hash );
memset( sig, 0, sizeof sig );
TEST_ASSERT( pk_init_ctx( &pk, pk_info_from_type( type ) ) == 0 );
TEST_ASSERT( pk_genkey( &pk ) == 0 );
TEST_ASSERT( pk_sign( &pk, POLARSSL_MD_NONE, hash, sizeof hash,
sig, &sig_len, rnd_std_rand, NULL ) == sign_ret );
TEST_ASSERT( pk_verify( &pk, POLARSSL_MD_NONE,
hash, sizeof hash, sig, sig_len ) == verify_ret );
pk_free( &pk );
}
/* END_CASE */
/* BEGIN_CASE depends_on:POLARSSL_RSA_C */
void pk_rsa_encrypt_test_vec( char *message_hex, int mod,
int radix_N, char *input_N,
int radix_E, char *input_E,
char *result_hex, int ret )
{
unsigned char message[1000];
unsigned char output[1000];
unsigned char result[1000];
size_t msg_len, olen, res_len;
rnd_pseudo_info rnd_info;
rsa_context *rsa;
pk_context pk;
memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
memset( message, 0, sizeof( message ) );
memset( output, 0, sizeof( output ) );
memset( result, 0, sizeof( result ) );
msg_len = unhexify( message, message_hex );
res_len = unhexify( result, result_hex );
pk_init( &pk );
TEST_ASSERT( pk_init_ctx( &pk, pk_info_from_type( POLARSSL_PK_RSA ) ) == 0 );
rsa = pk_rsa( pk );
rsa->len = mod / 8;
TEST_ASSERT( mpi_read_string( &rsa->N, radix_N, input_N ) == 0 );
TEST_ASSERT( mpi_read_string( &rsa->E, radix_E, input_E ) == 0 );
TEST_ASSERT( pk_encrypt( &pk, message, msg_len,
output, &olen, sizeof( output ),
rnd_pseudo_rand, &rnd_info ) == ret );
TEST_ASSERT( olen == res_len );
TEST_ASSERT( memcmp( output, result, olen ) == 0 );
pk_free( &pk );
}
/* END_CASE */
/* BEGIN_CASE depends_on:POLARSSL_RSA_C */
void pk_rsa_decrypt_test_vec( char *cipher_hex, int mod,
int radix_P, char *input_P,
int radix_Q, char *input_Q,
int radix_N, char *input_N,
int radix_E, char *input_E,
char *clear_hex, int ret )
{
unsigned char clear[1000];
unsigned char output[1000];
unsigned char cipher[1000];
size_t clear_len, olen, cipher_len;
rnd_pseudo_info rnd_info;
mpi P1, Q1, H, G;
rsa_context *rsa;
pk_context pk;
pk_init( &pk );
mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
memset( clear, 0, sizeof( clear ) );
memset( cipher, 0, sizeof( cipher ) );
clear_len = unhexify( clear, clear_hex );
cipher_len = unhexify( cipher, cipher_hex );
/* init pk-rsa context */
TEST_ASSERT( pk_init_ctx( &pk, pk_info_from_type( POLARSSL_PK_RSA ) ) == 0 );
rsa = pk_rsa( pk );
/* load public key */
rsa->len = mod / 8;
TEST_ASSERT( mpi_read_string( &rsa->N, radix_N, input_N ) == 0 );
TEST_ASSERT( mpi_read_string( &rsa->E, radix_E, input_E ) == 0 );
/* load private key */
TEST_ASSERT( mpi_read_string( &rsa->P, radix_P, input_P ) == 0 );
TEST_ASSERT( mpi_read_string( &rsa->Q, radix_Q, input_Q ) == 0 );
TEST_ASSERT( mpi_sub_int( &P1, &rsa->P, 1 ) == 0 );
TEST_ASSERT( mpi_sub_int( &Q1, &rsa->Q, 1 ) == 0 );
TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
TEST_ASSERT( mpi_gcd( &G, &rsa->E, &H ) == 0 );
TEST_ASSERT( mpi_inv_mod( &rsa->D , &rsa->E, &H ) == 0 );
TEST_ASSERT( mpi_mod_mpi( &rsa->DP, &rsa->D, &P1 ) == 0 );
TEST_ASSERT( mpi_mod_mpi( &rsa->DQ, &rsa->D, &Q1 ) == 0 );
TEST_ASSERT( mpi_inv_mod( &rsa->QP, &rsa->Q, &rsa->P ) == 0 );
/* decryption test */
memset( output, 0, sizeof( output ) );
olen = 0;
TEST_ASSERT( pk_decrypt( &pk, cipher, cipher_len,
output, &olen, sizeof( output ),
rnd_pseudo_rand, &rnd_info ) == ret );
if( ret == 0 )
{
TEST_ASSERT( olen == clear_len );
TEST_ASSERT( memcmp( output, clear, olen ) == 0 );
}
mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
pk_free( &pk );
}
/* END_CASE */
/* BEGIN_CASE */
void pk_ec_nocrypt( int type )
{
pk_context pk;
unsigned char output[100];
unsigned char input[100];
rnd_pseudo_info rnd_info;
size_t olen = 0;
int ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
pk_init( &pk );
memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
memset( output, 0, sizeof( output ) );
memset( input, 0, sizeof( input ) );
TEST_ASSERT( pk_init_ctx( &pk, pk_info_from_type( type ) ) == 0 );
TEST_ASSERT( pk_encrypt( &pk, input, sizeof( input ),
output, &olen, sizeof( output ),
rnd_pseudo_rand, &rnd_info ) == ret );
TEST_ASSERT( pk_decrypt( &pk, input, sizeof( input ),
output, &olen, sizeof( output ),
rnd_pseudo_rand, &rnd_info ) == ret );
pk_free( &pk );
}
/* END_CASE */