148 lines
4.3 KiB
Text
148 lines
4.3 KiB
Text
|
/* 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 */
|
||
|
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 */
|