2013-09-21 12:31:05 +02:00
|
|
|
/* BEGIN_HEADER */
|
2017-11-03 19:20:27 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/pk.h"
|
2017-11-03 19:20:27 +01:00
|
|
|
#include "mbedtls/pk_info.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)
|
2017-11-03 19:20:27 +01:00
|
|
|
|
|
|
|
static 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 ) );
|
|
|
|
}
|
2017-11-03 19:20:27 +01:00
|
|
|
static 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 ) );
|
|
|
|
}
|
2017-11-03 19:20:27 +01:00
|
|
|
static 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
|
|
|
}
|
2017-11-03 19:20:27 +01:00
|
|
|
|
|
|
|
/* Prepare a raw RSA context with a small random key. */
|
|
|
|
static void pk_rsa_prepare( mbedtls_rsa_context *raw )
|
|
|
|
{
|
|
|
|
mbedtls_rsa_init( raw, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
|
|
|
|
mbedtls_rsa_gen_key( raw, rnd_std_rand, NULL, RSA_KEY_SIZE, 3 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test the RSA context tested_ctx by comparing its operation with a
|
|
|
|
generic RSA context which is initialized with the key in raw. */
|
|
|
|
static void pk_rsa_match( mbedtls_rsa_context *raw,
|
|
|
|
mbedtls_pk_context *tested_ctx,
|
|
|
|
int sign_ret, int verify_ret,
|
|
|
|
int encrypt_ret, int decrypt_ret,
|
|
|
|
int debug_ret )
|
|
|
|
{
|
|
|
|
mbedtls_pk_context basic_ctx;
|
|
|
|
mbedtls_pk_debug_item dbg_items[10];
|
|
|
|
unsigned char hash[50], sig[1000];
|
|
|
|
unsigned char msg[50], ciph[1000], test[1000];
|
|
|
|
size_t sig_len, ciph_len, test_len;
|
|
|
|
|
|
|
|
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 basic PK RSA context with raw key */
|
|
|
|
mbedtls_pk_init( &basic_ctx );
|
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &basic_ctx,
|
|
|
|
mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_rsa_copy( mbedtls_pk_rsa( basic_ctx ), raw ) == 0 );
|
|
|
|
|
|
|
|
/* Test administrative functions */
|
|
|
|
TEST_ASSERT( mbedtls_pk_can_do( tested_ctx, MBEDTLS_PK_RSA ) );
|
|
|
|
TEST_ASSERT( mbedtls_pk_get_bitlen( tested_ctx ) == RSA_KEY_SIZE );
|
|
|
|
TEST_ASSERT( mbedtls_pk_get_len( tested_ctx ) == RSA_KEY_LEN );
|
|
|
|
TEST_ASSERT( mbedtls_pk_signature_size( tested_ctx ) == RSA_KEY_LEN );
|
|
|
|
|
|
|
|
/* Test signature */
|
|
|
|
TEST_ASSERT( mbedtls_pk_sign( tested_ctx, MBEDTLS_MD_NONE, hash, sizeof hash,
|
|
|
|
sig, &sig_len, rnd_std_rand, NULL ) == sign_ret );
|
|
|
|
if( sign_ret == 0 )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_HAVE_INT64)
|
|
|
|
TEST_ASSERT( mbedtls_pk_sign( tested_ctx, MBEDTLS_MD_NONE, hash, (size_t)-1,
|
|
|
|
NULL, NULL, rnd_std_rand, NULL ) ==
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA );
|
|
|
|
#endif /* MBEDTLS_HAVE_INT64 */
|
|
|
|
TEST_ASSERT( sig_len == RSA_KEY_LEN );
|
|
|
|
TEST_ASSERT( mbedtls_pk_verify( &basic_ctx, MBEDTLS_MD_NONE,
|
|
|
|
hash, sizeof hash, sig, sig_len ) == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test verification */
|
|
|
|
TEST_ASSERT( mbedtls_pk_sign( &basic_ctx, MBEDTLS_MD_NONE, hash, sizeof hash,
|
|
|
|
sig, &sig_len, rnd_std_rand, NULL ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_pk_verify( tested_ctx, MBEDTLS_MD_NONE,
|
|
|
|
hash, sizeof hash, sig, sig_len ) == verify_ret );
|
|
|
|
if( verify_ret == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( mbedtls_pk_verify( tested_ctx, MBEDTLS_MD_NONE,
|
|
|
|
hash, sizeof hash, sig, sig_len - 1 ) == MBEDTLS_ERR_RSA_VERIFY_FAILED );
|
|
|
|
sig[sig_len-1] ^= 1;
|
|
|
|
TEST_ASSERT( mbedtls_pk_verify( tested_ctx, MBEDTLS_MD_NONE,
|
|
|
|
hash, sizeof hash, sig, sig_len ) == MBEDTLS_ERR_RSA_INVALID_PADDING );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test encryption */
|
|
|
|
TEST_ASSERT( mbedtls_pk_encrypt( tested_ctx, msg, sizeof msg,
|
|
|
|
ciph, &ciph_len, sizeof ciph,
|
|
|
|
rnd_std_rand, NULL ) == encrypt_ret );
|
|
|
|
if( encrypt_ret == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( mbedtls_pk_decrypt( &basic_ctx, ciph, ciph_len,
|
|
|
|
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 decryption */
|
|
|
|
TEST_ASSERT( mbedtls_pk_encrypt( &basic_ctx, msg, sizeof msg,
|
|
|
|
ciph, &ciph_len, sizeof ciph,
|
|
|
|
rnd_std_rand, NULL ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_pk_decrypt( tested_ctx, ciph, ciph_len,
|
|
|
|
test, &test_len, sizeof test,
|
|
|
|
rnd_std_rand, NULL ) == decrypt_ret );
|
|
|
|
if( decrypt_ret == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( test_len == sizeof msg );
|
|
|
|
TEST_ASSERT( memcmp( test, msg, test_len ) == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test debug */
|
|
|
|
TEST_ASSERT( mbedtls_pk_debug( tested_ctx, dbg_items ) == debug_ret );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_pk_free( &basic_ctx );
|
|
|
|
}
|
|
|
|
|
|
|
|
#define OPAQUE_MOCK_CAN_DO MBEDTLS_PK_RSA
|
|
|
|
#define OPAQUE_MOCK_BITLEN 'b'
|
|
|
|
#define OPAQUE_MOCK_MD_ALG MBEDTLS_MD_SHA256
|
|
|
|
#define OPAQUE_MOCK_SIGNATURE_SIZE 4
|
|
|
|
#define OPAQUE_MOCK_GOOD_SIGNATURE "good"
|
|
|
|
static const unsigned char opaque_mock_hash[8] = "HASHhash";
|
|
|
|
static const unsigned char opaque_mock_reference_input[10] = "INPUTinput";
|
|
|
|
static const unsigned char opaque_mock_input[10] = "INPUTinput";
|
|
|
|
static const unsigned char opaque_mock_reference_encrypted[12] = "C:JOQVUjoqvu";
|
|
|
|
static const unsigned char opaque_mock_reference_decrypted[12] = "P:HMOTShmots";
|
|
|
|
static char opaque_mock_fake_ctx = 'c';
|
|
|
|
static mbedtls_pk_debug_item opaque_mock_pk_debug_item;
|
|
|
|
static int opaque_mock_debug_called_correctly = 0;
|
|
|
|
static int opaque_mock_free_called_correctly = 0;
|
|
|
|
|
|
|
|
static size_t opaque_mock_get_bitlen( const void *ctx )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( ctx == &opaque_mock_fake_ctx );
|
|
|
|
return( OPAQUE_MOCK_BITLEN );
|
|
|
|
exit:
|
|
|
|
return( INT_MIN );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int opaque_mock_can_do( const void *ctx,
|
|
|
|
mbedtls_pk_type_t type )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( ctx == &opaque_mock_fake_ctx );
|
|
|
|
return( type == OPAQUE_MOCK_CAN_DO );
|
|
|
|
exit:
|
|
|
|
return( INT_MIN );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int opaque_mock_verify_func( void *ctx, mbedtls_md_type_t md_alg,
|
|
|
|
const unsigned char *hash, size_t hash_len,
|
|
|
|
const unsigned char *sig, size_t sig_len )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( ctx == &opaque_mock_fake_ctx );
|
|
|
|
TEST_ASSERT( md_alg == OPAQUE_MOCK_MD_ALG );
|
|
|
|
TEST_ASSERT( hash_len == sizeof( opaque_mock_hash ) );
|
|
|
|
TEST_ASSERT( hash == opaque_mock_hash );
|
|
|
|
if( sig_len != OPAQUE_MOCK_SIGNATURE_SIZE )
|
|
|
|
return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
|
|
|
|
if( memcmp( sig, OPAQUE_MOCK_GOOD_SIGNATURE, OPAQUE_MOCK_SIGNATURE_SIZE ) )
|
|
|
|
return( MBEDTLS_ERR_PK_INVALID_SIGNATURE );
|
|
|
|
return( 0 );
|
|
|
|
exit:
|
|
|
|
return( INT_MIN );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int opaque_mock_sign_func( void *ctx, mbedtls_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 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( ctx == &opaque_mock_fake_ctx );
|
|
|
|
TEST_ASSERT( md_alg == OPAQUE_MOCK_MD_ALG );
|
|
|
|
TEST_ASSERT( hash_len == sizeof( opaque_mock_hash ) );
|
|
|
|
TEST_ASSERT( hash == opaque_mock_hash );
|
|
|
|
memcpy( sig, OPAQUE_MOCK_GOOD_SIGNATURE, OPAQUE_MOCK_SIGNATURE_SIZE );
|
|
|
|
*sig_len = OPAQUE_MOCK_SIGNATURE_SIZE;
|
|
|
|
(void) f_rng;
|
|
|
|
(void) p_rng;
|
|
|
|
return( 0 );
|
|
|
|
exit:
|
|
|
|
return( INT_MIN );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int opaque_mock_decrypt_func( void *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 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( ctx == &opaque_mock_fake_ctx );
|
|
|
|
TEST_ASSERT( ilen == sizeof( opaque_mock_reference_input ) );
|
|
|
|
TEST_ASSERT( !memcmp( input, opaque_mock_reference_input,
|
|
|
|
sizeof( opaque_mock_reference_input ) ) );
|
|
|
|
if( osize < sizeof( opaque_mock_reference_decrypted ) )
|
|
|
|
return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
|
|
|
|
*olen = sizeof( opaque_mock_reference_decrypted );
|
|
|
|
memcpy( output, opaque_mock_reference_decrypted, sizeof( opaque_mock_reference_decrypted ) );
|
|
|
|
(void) f_rng;
|
|
|
|
(void) p_rng;
|
|
|
|
return( 0 );
|
|
|
|
exit:
|
|
|
|
return( INT_MIN );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int opaque_mock_encrypt_func( void *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 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( ctx == &opaque_mock_fake_ctx );
|
|
|
|
TEST_ASSERT( ilen == sizeof( opaque_mock_reference_input ) );
|
|
|
|
TEST_ASSERT( !memcmp( input, opaque_mock_reference_input,
|
|
|
|
sizeof( opaque_mock_reference_input ) ) );
|
|
|
|
if( osize < sizeof( opaque_mock_reference_encrypted ) )
|
|
|
|
return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
|
|
|
|
*olen = sizeof( opaque_mock_reference_encrypted );
|
|
|
|
memcpy( output, opaque_mock_reference_encrypted, sizeof( opaque_mock_reference_encrypted ) );
|
|
|
|
(void) f_rng;
|
|
|
|
(void) p_rng;
|
|
|
|
return( 0 );
|
|
|
|
exit:
|
|
|
|
return( INT_MIN );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int opaque_mock_check_pair_func( const mbedtls_pk_context *pub,
|
|
|
|
const void *prv )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( prv == &opaque_mock_fake_ctx );
|
|
|
|
if( mbedtls_pk_get_type( pub ) != MBEDTLS_PK_RSA )
|
|
|
|
return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
|
|
|
|
return( 0 );
|
|
|
|
exit:
|
|
|
|
return( INT_MIN );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *opaque_mock_ctx_alloc_func( void )
|
|
|
|
{
|
|
|
|
return( &opaque_mock_fake_ctx );
|
|
|
|
}
|
|
|
|
static void *opaque_mock_ctx_alloc_fail( void )
|
|
|
|
{
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void opaque_mock_ctx_free_func( void *ctx )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( ctx == &opaque_mock_fake_ctx );
|
|
|
|
opaque_mock_free_called_correctly = 1;
|
|
|
|
exit:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void opaque_mock_debug_func( const void *ctx,
|
|
|
|
mbedtls_pk_debug_item *items )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( ctx == &opaque_mock_fake_ctx );
|
|
|
|
TEST_ASSERT( items == &opaque_mock_pk_debug_item );
|
|
|
|
opaque_mock_debug_called_correctly = 1;
|
|
|
|
exit:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t opaque_mock_signature_size_func( const void *ctx )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( ctx == &opaque_mock_fake_ctx );
|
|
|
|
return( OPAQUE_MOCK_SIGNATURE_SIZE );
|
|
|
|
exit:
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_RSA_C */
|
2017-11-03 19:20:27 +01:00
|
|
|
|
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 );
|
2017-11-02 17:16:43 +01:00
|
|
|
if( sign_ret == 0 )
|
|
|
|
TEST_ASSERT( sig_len <= mbedtls_pk_signature_size( &pk ) );
|
2013-09-21 12:31:05 +02:00
|
|
|
|
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;
|
2017-08-23 07:32:42 +02:00
|
|
|
mbedtls_mpi N, P, Q, E;
|
2015-04-08 12:49:31 +02:00
|
|
|
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 );
|
2017-08-23 07:32:42 +02:00
|
|
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
|
|
|
|
mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
|
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 ) );
|
|
|
|
|
2017-08-23 07:32:42 +02:00
|
|
|
clear_len = unhexify( clear, clear_hex );
|
2013-10-17 12:34:16 +02:00
|
|
|
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 */
|
2017-08-23 07:32:42 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
|
2013-10-17 12:34:16 +02:00
|
|
|
|
|
|
|
/* load private key */
|
2017-08-23 07:32:42 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_rsa_import( rsa, &N, &P, &Q, NULL, &E ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_rsa_get_len( rsa ) == (size_t) ( mod / 8 ) );
|
2017-10-10 17:56:22 +02:00
|
|
|
TEST_ASSERT( mbedtls_rsa_complete( rsa ) == 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:
|
2017-08-23 07:32:42 +02:00
|
|
|
mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
|
|
|
|
mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
|
2015-04-08 12:49:31 +02:00
|
|
|
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.
|
|
|
|
*/
|
2017-11-03 19:20:27 +01:00
|
|
|
mbedtls_pk_context alt;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_context raw;
|
2014-04-03 22:09:18 +02:00
|
|
|
|
2017-11-03 19:20:27 +01:00
|
|
|
/* Generate an RSA key to use in both contexts */
|
|
|
|
pk_rsa_prepare( &raw );
|
2014-04-03 22:09:18 +02:00
|
|
|
|
2017-11-03 19:20:27 +01:00
|
|
|
/* Set up the alt context with the generated key */
|
|
|
|
mbedtls_pk_init( &alt );
|
2015-05-14 19:41:36 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_setup_rsa_alt( &alt, (void *) &raw,
|
2017-11-03 19:20:27 +01:00
|
|
|
mbedtls_rsa_decrypt_func,
|
|
|
|
mbedtls_rsa_sign_func,
|
|
|
|
mbedtls_rsa_key_len_func ) == 0 );
|
2014-04-03 22:09:18 +02:00
|
|
|
|
2017-11-03 19:20:27 +01:00
|
|
|
/* Check the metadata in the alt context */
|
2015-04-08 12:49:31 +02:00
|
|
|
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
|
|
|
|
2017-11-03 19:20:27 +01:00
|
|
|
/* Exercise the alt context */
|
|
|
|
pk_rsa_match( &raw, &alt,
|
|
|
|
0, MBEDTLS_ERR_PK_TYPE_MISMATCH,
|
|
|
|
MBEDTLS_ERR_PK_TYPE_MISMATCH, 0,
|
|
|
|
MBEDTLS_ERR_PK_TYPE_MISMATCH );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_rsa_free( &raw );
|
|
|
|
mbedtls_pk_free( &alt );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void pk_opaque_mock( )
|
|
|
|
{
|
|
|
|
mbedtls_pk_info_t info =
|
2018-01-22 13:14:34 +01:00
|
|
|
MBEDTLS_PK_OPAQUE_INFO_1(
|
|
|
|
"mock"
|
|
|
|
, opaque_mock_get_bitlen
|
|
|
|
, opaque_mock_can_do
|
|
|
|
, opaque_mock_signature_size_func
|
|
|
|
, opaque_mock_verify_func
|
|
|
|
, opaque_mock_sign_func
|
|
|
|
, opaque_mock_decrypt_func
|
|
|
|
, opaque_mock_encrypt_func
|
|
|
|
, opaque_mock_check_pair_func
|
|
|
|
, opaque_mock_ctx_alloc_func
|
|
|
|
, opaque_mock_ctx_free_func
|
|
|
|
, opaque_mock_debug_func
|
|
|
|
);
|
2017-11-03 19:20:27 +01:00
|
|
|
mbedtls_pk_context ctx;
|
|
|
|
unsigned char sig[OPAQUE_MOCK_SIGNATURE_SIZE] = OPAQUE_MOCK_GOOD_SIGNATURE;
|
|
|
|
unsigned char input[sizeof( opaque_mock_reference_input )];
|
|
|
|
unsigned char output[sizeof( opaque_mock_reference_decrypted )] = "garbage";
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
mbedtls_pk_init( &ctx );
|
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &ctx, &info ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_get_type( &ctx ) == MBEDTLS_PK_OPAQUE );
|
|
|
|
TEST_ASSERT( mbedtls_pk_get_name( &ctx ) == info.name );
|
|
|
|
TEST_ASSERT( mbedtls_pk_get_bitlen( &ctx ) == OPAQUE_MOCK_BITLEN );
|
|
|
|
TEST_ASSERT( mbedtls_pk_can_do( &ctx, OPAQUE_MOCK_CAN_DO ) == 1 );
|
|
|
|
TEST_ASSERT( mbedtls_pk_can_do( &ctx, OPAQUE_MOCK_CAN_DO ^ 1 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_pk_signature_size( &ctx ) == OPAQUE_MOCK_SIGNATURE_SIZE );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_verify( &ctx, OPAQUE_MOCK_MD_ALG,
|
|
|
|
opaque_mock_hash, sizeof( opaque_mock_hash ),
|
|
|
|
sig, OPAQUE_MOCK_SIGNATURE_SIZE ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_pk_verify( &ctx, OPAQUE_MOCK_MD_ALG,
|
|
|
|
opaque_mock_hash, sizeof( opaque_mock_hash ),
|
|
|
|
sig, OPAQUE_MOCK_SIGNATURE_SIZE - 1 ) ==
|
|
|
|
MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
|
|
|
|
sig[0] ^= 1;
|
|
|
|
TEST_ASSERT( mbedtls_pk_verify( &ctx, OPAQUE_MOCK_MD_ALG,
|
|
|
|
opaque_mock_hash, sizeof( opaque_mock_hash ),
|
|
|
|
sig, OPAQUE_MOCK_SIGNATURE_SIZE ) ==
|
|
|
|
MBEDTLS_ERR_PK_INVALID_SIGNATURE );
|
|
|
|
len = -42;
|
|
|
|
TEST_ASSERT( mbedtls_pk_sign( &ctx, OPAQUE_MOCK_MD_ALG,
|
|
|
|
opaque_mock_hash, sizeof( opaque_mock_hash ),
|
|
|
|
sig, &len, NULL, NULL ) == 0 );
|
|
|
|
TEST_ASSERT( len == OPAQUE_MOCK_SIGNATURE_SIZE );
|
|
|
|
|
|
|
|
memcpy( input, opaque_mock_reference_input,
|
|
|
|
sizeof( opaque_mock_reference_input ) );
|
|
|
|
len = -42;
|
|
|
|
TEST_ASSERT( mbedtls_pk_encrypt( &ctx, input, sizeof( input ),
|
|
|
|
output, &len,
|
|
|
|
sizeof( opaque_mock_reference_encrypted ),
|
|
|
|
NULL, NULL ) == 0);
|
|
|
|
TEST_ASSERT( memcmp( input, opaque_mock_reference_input,
|
|
|
|
sizeof( opaque_mock_reference_input ) ) == 0 );
|
|
|
|
TEST_ASSERT( len == sizeof( opaque_mock_reference_encrypted ) );
|
|
|
|
TEST_ASSERT( memcmp( output, opaque_mock_reference_encrypted,
|
|
|
|
sizeof( opaque_mock_reference_encrypted ) ) == 0 );
|
|
|
|
len = -42;
|
|
|
|
TEST_ASSERT( mbedtls_pk_decrypt( &ctx, input, sizeof( input ),
|
|
|
|
output, &len,
|
|
|
|
sizeof( opaque_mock_reference_decrypted ),
|
|
|
|
NULL, NULL ) == 0);
|
|
|
|
TEST_ASSERT( memcmp( input, opaque_mock_reference_input,
|
|
|
|
sizeof( opaque_mock_reference_input ) ) == 0 );
|
|
|
|
TEST_ASSERT( len == sizeof( opaque_mock_reference_decrypted ) );
|
|
|
|
TEST_ASSERT( memcmp( output, opaque_mock_reference_decrypted,
|
|
|
|
sizeof( opaque_mock_reference_decrypted ) ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_check_pair( NULL, &ctx ) ==
|
2017-02-15 11:52:32 +01:00
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA );
|
2017-11-03 19:20:27 +01:00
|
|
|
TEST_ASSERT( mbedtls_pk_check_pair( &ctx, &ctx ) == 0 );
|
|
|
|
{
|
|
|
|
mbedtls_pk_context pub;
|
|
|
|
mbedtls_pk_init( &pub );
|
|
|
|
TEST_ASSERT( mbedtls_pk_check_pair( &pub, &ctx ) ==
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &pub,
|
|
|
|
mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_pk_check_pair( &pub, &ctx ) == 0 );
|
|
|
|
mbedtls_pk_free( &pub );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &pub,
|
|
|
|
mbedtls_pk_info_from_type( MBEDTLS_PK_ECDSA ) ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_pk_check_pair( &pub, &ctx ) ==
|
|
|
|
MBEDTLS_ERR_PK_TYPE_MISMATCH );
|
|
|
|
mbedtls_pk_free( &pub );
|
|
|
|
}
|
2014-04-03 22:09:18 +02:00
|
|
|
|
2017-11-03 19:20:27 +01:00
|
|
|
opaque_mock_debug_called_correctly = 0;
|
|
|
|
TEST_ASSERT( mbedtls_pk_debug( &ctx, &opaque_mock_pk_debug_item ) == 0 );
|
|
|
|
TEST_ASSERT( opaque_mock_debug_called_correctly );
|
2014-04-03 22:09:18 +02:00
|
|
|
|
2017-11-03 19:20:27 +01:00
|
|
|
opaque_mock_free_called_correctly = 0;
|
|
|
|
mbedtls_pk_free( &ctx );
|
|
|
|
TEST_ASSERT( opaque_mock_free_called_correctly );
|
|
|
|
return;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_pk_free( &ctx );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void pk_opaque_minimal( )
|
|
|
|
{
|
|
|
|
mbedtls_pk_info_t info =
|
2018-01-22 13:14:34 +01:00
|
|
|
MBEDTLS_PK_OPAQUE_INFO_1(
|
|
|
|
"mock"
|
|
|
|
, opaque_mock_get_bitlen
|
|
|
|
, opaque_mock_can_do
|
|
|
|
, NULL
|
|
|
|
, NULL
|
|
|
|
, NULL
|
|
|
|
, NULL
|
|
|
|
, NULL
|
|
|
|
, NULL
|
|
|
|
, NULL
|
|
|
|
, opaque_mock_ctx_free_func
|
|
|
|
, NULL
|
|
|
|
);
|
2017-11-03 19:20:27 +01:00
|
|
|
mbedtls_pk_context ctx;
|
|
|
|
|
|
|
|
mbedtls_pk_init( &ctx );
|
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &ctx, &info ) == 0 );
|
|
|
|
ctx.pk_ctx = &opaque_mock_fake_ctx;
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_get_type( &ctx ) == MBEDTLS_PK_OPAQUE );
|
|
|
|
TEST_ASSERT( mbedtls_pk_get_name( &ctx ) == info.name );
|
|
|
|
TEST_ASSERT( mbedtls_pk_get_bitlen( &ctx ) == OPAQUE_MOCK_BITLEN );
|
|
|
|
TEST_ASSERT( mbedtls_pk_can_do( &ctx, OPAQUE_MOCK_CAN_DO ) == 1 );
|
|
|
|
TEST_ASSERT( mbedtls_pk_can_do( &ctx, OPAQUE_MOCK_CAN_DO ^ 1 ) == 0 );
|
2018-01-22 13:04:46 +01:00
|
|
|
TEST_ASSERT( mbedtls_pk_signature_size( &ctx ) == 0 );
|
2017-11-03 19:20:27 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_verify( &ctx, OPAQUE_MOCK_MD_ALG,
|
|
|
|
NULL, 0, NULL, 0 ) ==
|
|
|
|
MBEDTLS_ERR_PK_TYPE_MISMATCH );
|
|
|
|
TEST_ASSERT( mbedtls_pk_sign( &ctx, OPAQUE_MOCK_MD_ALG, NULL, 0,
|
|
|
|
NULL, NULL, NULL, NULL ) ==
|
|
|
|
MBEDTLS_ERR_PK_TYPE_MISMATCH );
|
|
|
|
TEST_ASSERT( mbedtls_pk_encrypt( &ctx, NULL, 0,
|
|
|
|
NULL, NULL, 0, NULL, NULL ) ==
|
|
|
|
MBEDTLS_ERR_PK_TYPE_MISMATCH);
|
|
|
|
TEST_ASSERT( mbedtls_pk_decrypt( &ctx, NULL, 0,
|
|
|
|
NULL, NULL, 0, NULL, NULL ) ==
|
|
|
|
MBEDTLS_ERR_PK_TYPE_MISMATCH);
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_check_pair( NULL, &ctx ) ==
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA );
|
|
|
|
TEST_ASSERT( mbedtls_pk_check_pair( &ctx, &ctx ) == 0 );
|
|
|
|
{
|
|
|
|
mbedtls_pk_context pub;
|
|
|
|
mbedtls_pk_init( &pub );
|
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &pub,
|
|
|
|
mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_pk_check_pair( &pub, &ctx ) ==
|
|
|
|
MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
|
|
|
|
mbedtls_pk_free( &pub );
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_debug( &ctx, &opaque_mock_pk_debug_item ) ==
|
|
|
|
MBEDTLS_ERR_PK_TYPE_MISMATCH );
|
|
|
|
|
|
|
|
opaque_mock_free_called_correctly = 0;
|
|
|
|
mbedtls_pk_free( &ctx );
|
|
|
|
TEST_ASSERT( opaque_mock_free_called_correctly );
|
|
|
|
return;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_pk_free( &ctx );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void pk_opaque_fail_allocation( )
|
|
|
|
{
|
|
|
|
mbedtls_pk_info_t info =
|
2018-01-22 13:14:34 +01:00
|
|
|
MBEDTLS_PK_OPAQUE_INFO_1(
|
|
|
|
"mock"
|
|
|
|
, opaque_mock_get_bitlen
|
|
|
|
, opaque_mock_can_do
|
|
|
|
, NULL
|
|
|
|
, NULL
|
|
|
|
, NULL
|
|
|
|
, NULL
|
|
|
|
, NULL
|
|
|
|
, NULL
|
|
|
|
, opaque_mock_ctx_alloc_fail
|
|
|
|
, NULL
|
|
|
|
, NULL
|
|
|
|
);
|
2017-11-03 19:20:27 +01:00
|
|
|
mbedtls_pk_context ctx;
|
|
|
|
mbedtls_pk_init( &ctx );
|
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &ctx, &info ) ==
|
|
|
|
MBEDTLS_ERR_PK_ALLOC_FAILED );
|
|
|
|
TEST_ASSERT( ctx.pk_info == NULL );
|
|
|
|
TEST_ASSERT( ctx.pk_ctx == NULL );
|
|
|
|
exit:
|
|
|
|
mbedtls_pk_free( &ctx );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_PK_RSA_ALT_SUPPORT */
|
|
|
|
void pk_opaque_wrapper( )
|
|
|
|
{
|
|
|
|
/* Test an opaque context that's a wrapper around the usual RSA
|
|
|
|
implementation against an independent raw RSA context. */
|
|
|
|
mbedtls_pk_context opaque;
|
|
|
|
mbedtls_rsa_context raw;
|
|
|
|
const mbedtls_pk_info_t *mbedtls_rsa_info =
|
|
|
|
mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
|
|
|
|
mbedtls_pk_info_t pk_rsa_opaque_info =
|
2018-01-22 13:14:34 +01:00
|
|
|
MBEDTLS_PK_OPAQUE_INFO_1(
|
|
|
|
"RSA-opaque-wrapper"
|
|
|
|
, mbedtls_rsa_info->get_bitlen
|
|
|
|
, mbedtls_rsa_info->can_do
|
|
|
|
, mbedtls_rsa_info->signature_size_func
|
|
|
|
, mbedtls_rsa_info->verify_func
|
|
|
|
, mbedtls_rsa_info->sign_func
|
|
|
|
, mbedtls_rsa_info->decrypt_func
|
|
|
|
, mbedtls_rsa_info->encrypt_func
|
|
|
|
, NULL // we don't test check_pair here
|
|
|
|
, mbedtls_rsa_info->ctx_alloc_func
|
|
|
|
, mbedtls_rsa_info->ctx_free_func
|
|
|
|
, mbedtls_rsa_info->debug_func
|
|
|
|
);
|
2017-11-03 19:20:27 +01:00
|
|
|
|
|
|
|
/* Generate an RSA key to use in both contexts */
|
|
|
|
pk_rsa_prepare( &raw );
|
|
|
|
|
|
|
|
/* Set up the opaque context with the generated key */
|
|
|
|
mbedtls_pk_init( &opaque );
|
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &opaque, &pk_rsa_opaque_info ) == 0 );
|
|
|
|
mbedtls_rsa_copy( opaque.pk_ctx, &raw );
|
|
|
|
|
|
|
|
/* Check the metadata in the opaque context */
|
|
|
|
TEST_ASSERT( mbedtls_pk_get_type( &opaque ) == MBEDTLS_PK_OPAQUE );
|
|
|
|
TEST_ASSERT( strcmp( mbedtls_pk_get_name( &opaque ),
|
|
|
|
"RSA-opaque-wrapper" ) == 0 );
|
|
|
|
|
|
|
|
/* Exercise the opaque context */
|
|
|
|
pk_rsa_match( &raw, &opaque, 0, 0, 0, 0, 0 );
|
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 );
|
2017-11-03 19:20:27 +01:00
|
|
|
mbedtls_pk_free( &opaque );
|
2014-04-03 22:09:18 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|