2013-09-21 12:31:05 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/pk.h"
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2014-11-12 00:01:34 +01:00
|
|
|
/* For error codes */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/ecp.h"
|
|
|
|
#include "mbedtls/rsa.h"
|
2014-11-12 00:01:34 +01:00
|
|
|
|
2017-02-15 11:52:32 +01:00
|
|
|
/* For detecting 64-bit compilation */
|
|
|
|
#include "mbedtls/bignum.h"
|
|
|
|
|
2013-09-21 12:31:05 +02:00
|
|
|
static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len );
|
|
|
|
|
2014-04-03 22:09:18 +02:00
|
|
|
#define RSA_KEY_SIZE 512
|
|
|
|
#define RSA_KEY_LEN 64
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
static int pk_genkey( mbedtls_pk_context *pk )
|
2013-09-21 12:31:05 +02:00
|
|
|
{
|
2013-12-19 17:29:52 +01:00
|
|
|
((void) pk);
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
|
|
|
|
if( mbedtls_pk_get_type( pk ) == MBEDTLS_PK_RSA )
|
|
|
|
return mbedtls_rsa_gen_key( mbedtls_pk_rsa( *pk ), rnd_std_rand, NULL, RSA_KEY_SIZE, 3 );
|
2013-09-21 12:31:05 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
|
|
|
if( mbedtls_pk_get_type( pk ) == MBEDTLS_PK_ECKEY ||
|
|
|
|
mbedtls_pk_get_type( pk ) == MBEDTLS_PK_ECKEY_DH ||
|
|
|
|
mbedtls_pk_get_type( pk ) == MBEDTLS_PK_ECDSA )
|
2013-09-21 12:31:05 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2015-05-11 18:46:47 +02:00
|
|
|
if( ( ret = mbedtls_ecp_group_load( &mbedtls_pk_ec( *pk )->grp,
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_ECP_DP_SECP192R1 ) ) != 0 )
|
2013-09-21 12:31:05 +02:00
|
|
|
return( ret );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
return mbedtls_ecp_gen_keypair( &mbedtls_pk_ec( *pk )->grp, &mbedtls_pk_ec( *pk )->d,
|
|
|
|
&mbedtls_pk_ec( *pk )->Q, rnd_std_rand, NULL );
|
2013-09-21 12:31:05 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return( -1 );
|
|
|
|
}
|
2014-04-03 22:09:18 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_RSA_C)
|
|
|
|
int mbedtls_rsa_decrypt_func( void *ctx, int mode, size_t *olen,
|
2014-04-03 22:09:18 +02:00
|
|
|
const unsigned char *input, unsigned char *output,
|
|
|
|
size_t output_max_len )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( mbedtls_rsa_pkcs1_decrypt( (mbedtls_rsa_context *) ctx, NULL, NULL, mode, olen,
|
2014-04-03 22:09:18 +02:00
|
|
|
input, output, output_max_len ) );
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_rsa_sign_func( void *ctx,
|
2014-04-03 22:09:18 +02:00
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
|
2015-04-08 12:49:31 +02:00
|
|
|
int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
|
2014-04-03 22:09:18 +02:00
|
|
|
const unsigned char *hash, unsigned char *sig )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( mbedtls_rsa_pkcs1_sign( (mbedtls_rsa_context *) ctx, f_rng, p_rng, mode,
|
2014-04-03 22:09:18 +02:00
|
|
|
md_alg, hashlen, hash, sig ) );
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
size_t mbedtls_rsa_key_len_func( void *ctx )
|
2014-04-03 22:09:18 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( ((const mbedtls_rsa_context *) ctx)->len );
|
2014-04-03 22:09:18 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_RSA_C */
|
2013-09-21 12:31:05 +02:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 12:49:31 +02:00
|
|
|
* depends_on:MBEDTLS_PK_C
|
2013-09-21 12:31:05 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void pk_utils( int type, int size, int len, char *name )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_context pk;
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_init( &pk );
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2015-05-14 19:41:36 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( type ) ) == 0 );
|
2013-09-21 12:31:05 +02:00
|
|
|
TEST_ASSERT( pk_genkey( &pk ) == 0 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( (int) mbedtls_pk_get_type( &pk ) == type );
|
|
|
|
TEST_ASSERT( mbedtls_pk_can_do( &pk, type ) );
|
2015-06-18 16:43:38 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_get_bitlen( &pk ) == (unsigned) size );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_get_len( &pk ) == (unsigned) len );
|
|
|
|
TEST_ASSERT( strcmp( mbedtls_pk_get_name( &pk), name ) == 0 );
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( &pk );
|
2013-09-21 12:31:05 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_FS_IO */
|
|
|
|
void mbedtls_pk_check_pair( char *pub_file, char *prv_file, int ret )
|
2014-11-06 16:51:20 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_context pub, prv, alt;
|
2014-11-06 16:51:20 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_init( &pub );
|
|
|
|
mbedtls_pk_init( &prv );
|
|
|
|
mbedtls_pk_init( &alt );
|
2014-11-06 16:51:20 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_parse_public_keyfile( &pub, pub_file ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_pk_parse_keyfile( &prv, prv_file, NULL ) == 0 );
|
2014-11-06 16:51:20 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_check_pair( &pub, &prv ) == ret );
|
2014-11-06 16:51:20 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
|
|
|
|
if( mbedtls_pk_get_type( &prv ) == MBEDTLS_PK_RSA )
|
2014-11-08 17:08:08 +01:00
|
|
|
{
|
2015-05-14 19:41:36 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_setup_rsa_alt( &alt, mbedtls_pk_rsa( prv ),
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_decrypt_func, mbedtls_rsa_sign_func, mbedtls_rsa_key_len_func ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_pk_check_pair( &pub, &alt ) == ret );
|
2014-11-08 17:08:08 +01:00
|
|
|
}
|
2014-11-12 00:01:34 +01:00
|
|
|
#endif
|
2014-11-08 17:08:08 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( &pub );
|
|
|
|
mbedtls_pk_free( &prv );
|
|
|
|
mbedtls_pk_free( &alt );
|
2014-11-06 16:51:20 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
|
2014-01-27 11:45:49 +01:00
|
|
|
void pk_rsa_verify_test_vec( char *message_hex_string, int digest,
|
2013-09-21 12:31:05 +02:00
|
|
|
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];
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_context *rsa;
|
|
|
|
mbedtls_pk_context pk;
|
2013-09-21 12:31:05 +02:00
|
|
|
int msg_len;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_init( &pk );
|
2013-09-21 12:31:05 +02:00
|
|
|
|
|
|
|
memset( message_str, 0x00, 1000 );
|
|
|
|
memset( hash_result, 0x00, 1000 );
|
|
|
|
memset( result_str, 0x00, 1000 );
|
|
|
|
|
2015-05-14 19:41:36 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
rsa = mbedtls_pk_rsa( pk );
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2014-01-27 11:45:49 +01:00
|
|
|
rsa->len = mod / 8;
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &rsa->N, radix_N, input_N ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &rsa->E, radix_E, input_E ) == 0 );
|
2013-09-21 12:31:05 +02:00
|
|
|
|
|
|
|
msg_len = unhexify( message_str, message_hex_string );
|
|
|
|
unhexify( result_str, result_hex_str );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_md_info_from_type( digest ) != NULL )
|
|
|
|
TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_verify( &pk, digest, hash_result, 0,
|
|
|
|
result_str, mbedtls_pk_get_len( &pk ) ) == result );
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( &pk );
|
2013-09-21 12:31:05 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
|
2014-06-05 13:41:44 +02:00
|
|
|
void pk_rsa_verify_ext_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 pk_type, int mgf1_hash_id, int salt_len,
|
|
|
|
int result )
|
|
|
|
{
|
|
|
|
unsigned char message_str[1000];
|
|
|
|
unsigned char hash_result[1000];
|
|
|
|
unsigned char result_str[1000];
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_context *rsa;
|
|
|
|
mbedtls_pk_context pk;
|
|
|
|
mbedtls_pk_rsassa_pss_options pss_opts;
|
2014-06-05 13:41:44 +02:00
|
|
|
void *options;
|
|
|
|
int msg_len;
|
|
|
|
size_t hash_len;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_init( &pk );
|
2014-06-05 13:41:44 +02:00
|
|
|
|
|
|
|
memset( message_str, 0x00, 1000 );
|
|
|
|
memset( hash_result, 0x00, 1000 );
|
|
|
|
memset( result_str, 0x00, 1000 );
|
|
|
|
|
2015-05-14 19:41:36 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
rsa = mbedtls_pk_rsa( pk );
|
2014-06-05 13:41:44 +02:00
|
|
|
|
|
|
|
rsa->len = mod / 8;
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &rsa->N, radix_N, input_N ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &rsa->E, radix_E, input_E ) == 0 );
|
2014-06-05 13:41:44 +02:00
|
|
|
|
|
|
|
msg_len = unhexify( message_str, message_hex_string );
|
|
|
|
unhexify( result_str, result_hex_str );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( digest != MBEDTLS_MD_NONE )
|
2014-06-05 13:41:44 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ),
|
2014-06-05 13:41:44 +02:00
|
|
|
message_str, msg_len, hash_result ) == 0 );
|
|
|
|
hash_len = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy( hash_result, message_str, msg_len );
|
|
|
|
hash_len = msg_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( mgf1_hash_id < 0 )
|
|
|
|
{
|
|
|
|
options = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
options = &pss_opts;
|
|
|
|
|
|
|
|
pss_opts.mgf1_hash_id = mgf1_hash_id;
|
|
|
|
pss_opts.expected_salt_len = salt_len;
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_verify_ext( pk_type, options, &pk,
|
2014-06-05 13:41:44 +02:00
|
|
|
digest, hash_result, hash_len,
|
2015-04-08 12:49:31 +02:00
|
|
|
result_str, mbedtls_pk_get_len( &pk ) ) == result );
|
2014-06-05 13:41:44 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( &pk );
|
2014-06-05 13:41:44 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ECDSA_C */
|
2013-09-21 12:31:05 +02:00
|
|
|
void pk_ec_test_vec( int type, int id, char *key_str,
|
|
|
|
char *hash_str, char * sig_str, int ret )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_context pk;
|
|
|
|
mbedtls_ecp_keypair *eckey;
|
2013-09-21 12:31:05 +02:00
|
|
|
unsigned char hash[100], sig[500], key[500];
|
|
|
|
size_t hash_len, sig_len, key_len;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_init( &pk );
|
2013-09-21 12:31:05 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2015-05-14 19:41:36 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( type ) ) == 0 );
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_can_do( &pk, MBEDTLS_PK_ECDSA ) );
|
|
|
|
eckey = mbedtls_pk_ec( pk );
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2015-05-11 18:46:47 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &eckey->grp, id ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_point_read_binary( &eckey->grp, &eckey->Q,
|
2013-09-21 12:31:05 +02:00
|
|
|
key, key_len ) == 0 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_NONE,
|
2013-09-21 12:31:05 +02:00
|
|
|
hash, hash_len, sig, sig_len ) == ret );
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( &pk );
|
2013-09-21 12:31:05 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
|
2013-09-21 12:31:05 +02:00
|
|
|
void pk_sign_verify( int type, int sign_ret, int verify_ret )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_context pk;
|
2013-09-21 12:31:05 +02:00
|
|
|
unsigned char hash[50], sig[5000];
|
|
|
|
size_t sig_len;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_init( &pk );
|
2013-09-21 12:31:05 +02:00
|
|
|
|
|
|
|
memset( hash, 0x2a, sizeof hash );
|
|
|
|
memset( sig, 0, sizeof sig );
|
|
|
|
|
2015-05-14 19:41:36 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( type ) ) == 0 );
|
2013-09-21 12:31:05 +02:00
|
|
|
TEST_ASSERT( pk_genkey( &pk ) == 0 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_sign( &pk, MBEDTLS_MD_SHA256, hash, sizeof hash,
|
2013-09-21 12:31:05 +02:00
|
|
|
sig, &sig_len, rnd_std_rand, NULL ) == sign_ret );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256,
|
2013-09-21 12:31:05 +02:00
|
|
|
hash, sizeof hash, sig, sig_len ) == verify_ret );
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( &pk );
|
2013-09-21 12:31:05 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2013-10-17 12:34:16 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
|
2013-10-17 12:34:16 +02:00
|
|
|
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;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_context *rsa;
|
|
|
|
mbedtls_pk_context pk;
|
2013-10-17 12:34:16 +02:00
|
|
|
|
|
|
|
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 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_init( &pk );
|
2015-05-14 19:41:36 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
rsa = mbedtls_pk_rsa( pk );
|
2013-10-17 12:34:16 +02:00
|
|
|
|
|
|
|
rsa->len = mod / 8;
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &rsa->N, radix_N, input_N ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &rsa->E, radix_E, input_E ) == 0 );
|
2013-10-17 12:34:16 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_encrypt( &pk, message, msg_len,
|
2013-10-17 12:34:16 +02:00
|
|
|
output, &olen, sizeof( output ),
|
|
|
|
rnd_pseudo_rand, &rnd_info ) == ret );
|
|
|
|
TEST_ASSERT( olen == res_len );
|
|
|
|
TEST_ASSERT( memcmp( output, result, olen ) == 0 );
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( &pk );
|
2013-10-17 12:34:16 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
|
2013-10-17 12:34:16 +02:00
|
|
|
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;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi P1, Q1, H, G;
|
|
|
|
mbedtls_rsa_context *rsa;
|
|
|
|
mbedtls_pk_context pk;
|
2013-10-17 12:34:16 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_init( &pk );
|
|
|
|
mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
|
2013-10-17 12:34:16 +02:00
|
|
|
|
|
|
|
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 */
|
2015-05-14 19:41:36 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
rsa = mbedtls_pk_rsa( pk );
|
2013-10-17 12:34:16 +02:00
|
|
|
|
|
|
|
/* load public key */
|
|
|
|
rsa->len = mod / 8;
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &rsa->N, radix_N, input_N ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &rsa->E, radix_E, input_E ) == 0 );
|
2013-10-17 12:34:16 +02:00
|
|
|
|
|
|
|
/* load private key */
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &rsa->P, radix_P, input_P ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &rsa->Q, radix_Q, input_Q ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &rsa->P, 1 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &rsa->Q, 1 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_gcd( &G, &rsa->E, &H ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_inv_mod( &rsa->D , &rsa->E, &H ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_mod_mpi( &rsa->DP, &rsa->D, &P1 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_mod_mpi( &rsa->DQ, &rsa->D, &Q1 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_inv_mod( &rsa->QP, &rsa->Q, &rsa->P ) == 0 );
|
2013-10-17 12:34:16 +02:00
|
|
|
|
|
|
|
/* decryption test */
|
|
|
|
memset( output, 0, sizeof( output ) );
|
|
|
|
olen = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_decrypt( &pk, cipher, cipher_len,
|
2013-10-17 12:34:16 +02:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
|
|
|
|
mbedtls_pk_free( &pk );
|
2013-10-17 12:34:16 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2013-10-17 12:46:39 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void pk_ec_nocrypt( int type )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_context pk;
|
2013-10-17 12:46:39 +02:00
|
|
|
unsigned char output[100];
|
|
|
|
unsigned char input[100];
|
|
|
|
rnd_pseudo_info rnd_info;
|
|
|
|
size_t olen = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
int ret = MBEDTLS_ERR_PK_TYPE_MISMATCH;
|
2013-10-17 12:46:39 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_init( &pk );
|
2013-10-17 12:46:39 +02:00
|
|
|
|
|
|
|
memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
|
|
|
|
memset( output, 0, sizeof( output ) );
|
|
|
|
memset( input, 0, sizeof( input ) );
|
|
|
|
|
2015-05-14 19:41:36 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( type ) ) == 0 );
|
2013-10-17 12:46:39 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_encrypt( &pk, input, sizeof( input ),
|
2013-10-17 12:46:39 +02:00
|
|
|
output, &olen, sizeof( output ),
|
|
|
|
rnd_pseudo_rand, &rnd_info ) == ret );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_decrypt( &pk, input, sizeof( input ),
|
2013-10-17 12:46:39 +02:00
|
|
|
output, &olen, sizeof( output ),
|
|
|
|
rnd_pseudo_rand, &rnd_info ) == ret );
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( &pk );
|
2013-10-17 12:46:39 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2014-04-03 22:09:18 +02:00
|
|
|
|
2017-02-15 11:52:32 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_HAVE_INT64 */
|
|
|
|
void pk_rsa_overflow( )
|
|
|
|
{
|
|
|
|
mbedtls_pk_context pk;
|
|
|
|
size_t hash_len = (size_t)-1;
|
|
|
|
|
|
|
|
mbedtls_pk_init( &pk );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &pk,
|
|
|
|
mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 );
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PKCS1_V21)
|
|
|
|
TEST_ASSERT( mbedtls_pk_verify_ext( MBEDTLS_PK_RSASSA_PSS, NULL, &pk,
|
|
|
|
MBEDTLS_MD_NONE, NULL, hash_len, NULL, 0 ) ==
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA );
|
|
|
|
#endif /* MBEDTLS_PKCS1_V21 */
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_NONE, NULL, hash_len,
|
|
|
|
NULL, 0 ) == MBEDTLS_ERR_PK_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_sign( &pk, MBEDTLS_MD_NONE, NULL, hash_len, NULL, 0,
|
|
|
|
rnd_std_rand, NULL ) == MBEDTLS_ERR_PK_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_pk_free( &pk );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_PK_RSA_ALT_SUPPORT */
|
2014-04-03 22:09:18 +02:00
|
|
|
void pk_rsa_alt( )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* An rsa_alt context can only do private operations (decrypt, sign).
|
|
|
|
* Test it against the public operations (encrypt, verify) of a
|
|
|
|
* corresponding rsa context.
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_context raw;
|
|
|
|
mbedtls_pk_context rsa, alt;
|
|
|
|
mbedtls_pk_debug_item dbg_items[10];
|
2014-04-03 22:09:18 +02:00
|
|
|
unsigned char hash[50], sig[1000];
|
|
|
|
unsigned char msg[50], ciph[1000], test[1000];
|
|
|
|
size_t sig_len, ciph_len, test_len;
|
2015-04-08 12:49:31 +02:00
|
|
|
int ret = MBEDTLS_ERR_PK_TYPE_MISMATCH;
|
2014-04-03 22:09:18 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_init( &raw, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
|
|
|
|
mbedtls_pk_init( &rsa ); mbedtls_pk_init( &alt );
|
2014-04-03 22:09:18 +02:00
|
|
|
|
|
|
|
memset( hash, 0x2a, sizeof hash );
|
|
|
|
memset( sig, 0, sizeof sig );
|
|
|
|
memset( msg, 0x2a, sizeof msg );
|
|
|
|
memset( ciph, 0, sizeof ciph );
|
|
|
|
memset( test, 0, sizeof test );
|
|
|
|
|
|
|
|
/* Initiliaze PK RSA context with random key */
|
2015-05-14 19:41:36 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &rsa,
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 );
|
2014-04-03 22:09:18 +02:00
|
|
|
TEST_ASSERT( pk_genkey( &rsa ) == 0 );
|
|
|
|
|
|
|
|
/* Extract key to the raw rsa context */
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_rsa_copy( &raw, mbedtls_pk_rsa( rsa ) ) == 0 );
|
2014-04-03 22:09:18 +02:00
|
|
|
|
|
|
|
/* Initialize PK RSA_ALT context */
|
2015-05-14 19:41:36 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_setup_rsa_alt( &alt, (void *) &raw,
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_decrypt_func, mbedtls_rsa_sign_func, mbedtls_rsa_key_len_func ) == 0 );
|
2014-04-03 22:09:18 +02:00
|
|
|
|
|
|
|
/* Test administrative functions */
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_can_do( &alt, MBEDTLS_PK_RSA ) );
|
2015-06-18 16:43:38 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_get_bitlen( &alt ) == RSA_KEY_SIZE );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_get_len( &alt ) == RSA_KEY_LEN );
|
|
|
|
TEST_ASSERT( mbedtls_pk_get_type( &alt ) == MBEDTLS_PK_RSA_ALT );
|
|
|
|
TEST_ASSERT( strcmp( mbedtls_pk_get_name( &alt ), "RSA-alt" ) == 0 );
|
2014-04-03 22:09:18 +02:00
|
|
|
|
|
|
|
/* Test signature */
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_sign( &alt, MBEDTLS_MD_NONE, hash, sizeof hash,
|
2014-04-03 22:09:18 +02:00
|
|
|
sig, &sig_len, rnd_std_rand, NULL ) == 0 );
|
2017-02-15 11:52:32 +01:00
|
|
|
#if defined(MBEDTLS_HAVE_INT64)
|
|
|
|
TEST_ASSERT( mbedtls_pk_sign( &alt, MBEDTLS_MD_NONE, hash, (size_t)-1,
|
|
|
|
NULL, NULL, rnd_std_rand, NULL ) ==
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA );
|
|
|
|
#endif /* MBEDTLS_HAVE_INT64 */
|
2014-04-03 22:09:18 +02:00
|
|
|
TEST_ASSERT( sig_len == RSA_KEY_LEN );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_verify( &rsa, MBEDTLS_MD_NONE,
|
2014-04-03 22:09:18 +02:00
|
|
|
hash, sizeof hash, sig, sig_len ) == 0 );
|
|
|
|
|
|
|
|
/* Test decrypt */
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_encrypt( &rsa, msg, sizeof msg,
|
2014-04-03 22:09:18 +02:00
|
|
|
ciph, &ciph_len, sizeof ciph,
|
|
|
|
rnd_std_rand, NULL ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_decrypt( &alt, ciph, ciph_len,
|
2014-04-03 22:09:18 +02:00
|
|
|
test, &test_len, sizeof test,
|
|
|
|
rnd_std_rand, NULL ) == 0 );
|
|
|
|
TEST_ASSERT( test_len == sizeof msg );
|
|
|
|
TEST_ASSERT( memcmp( test, msg, test_len ) == 0 );
|
|
|
|
|
|
|
|
/* Test forbidden operations */
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_encrypt( &alt, msg, sizeof msg,
|
2014-04-03 22:09:18 +02:00
|
|
|
ciph, &ciph_len, sizeof ciph,
|
|
|
|
rnd_std_rand, NULL ) == ret );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_verify( &alt, MBEDTLS_MD_NONE,
|
2014-04-03 22:09:18 +02:00
|
|
|
hash, sizeof hash, sig, sig_len ) == ret );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_debug( &alt, dbg_items ) == ret );
|
2014-04-03 22:09:18 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_free( &raw );
|
|
|
|
mbedtls_pk_free( &rsa ); mbedtls_pk_free( &alt );
|
2014-04-03 22:09:18 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|