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 */
|
2019-01-31 14:20:20 +01:00
|
|
|
#include "mbedtls/asn1.h"
|
|
|
|
#include "mbedtls/base64.h"
|
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-05-08 12:15:49 +02:00
|
|
|
#include <limits.h>
|
2017-06-09 15:26:59 +02:00
|
|
|
#include <stdint.h>
|
2017-02-15 11:52:32 +01:00
|
|
|
|
2019-12-19 00:09:47 +01:00
|
|
|
/* Needed only for test case data under #if defined(MBEDTLS_USE_PSA_CRYPTO),
|
|
|
|
* but the test code generator requires test case data to be valid C code
|
|
|
|
* unconditionally (https://github.com/ARMmbed/mbedtls/issues/2023). */
|
|
|
|
#include "psa/crypto.h"
|
|
|
|
|
2019-05-23 20:25:48 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
#include "mbedtls/psa_util.h"
|
2020-05-27 09:22:32 +02:00
|
|
|
#include "test/psa_crypto_helpers.h"
|
2019-08-01 12:47:40 +02:00
|
|
|
#define PSA_INIT( ) PSA_ASSERT( psa_crypto_init( ) )
|
|
|
|
#else
|
|
|
|
/* Define empty macros so that we can use them in the preamble and teardown
|
|
|
|
* of every test function that uses PSA conditionally based on
|
|
|
|
* MBEDTLS_USE_PSA_CRYPTO. */
|
|
|
|
#define PSA_INIT( ) ( (void) 0 )
|
|
|
|
#define PSA_DONE( ) ( (void) 0 )
|
2019-05-23 20:25:48 +02:00
|
|
|
#endif
|
|
|
|
|
2014-04-03 22:09:18 +02:00
|
|
|
#define RSA_KEY_SIZE 512
|
|
|
|
#define RSA_KEY_LEN 64
|
|
|
|
|
2019-11-05 16:42:13 +01:00
|
|
|
/** Generate a key of the desired type.
|
|
|
|
*
|
|
|
|
* \param pk The PK object to fill. It must have been initialized
|
|
|
|
* with mbedtls_pk_setup().
|
|
|
|
* \param parameter - For RSA keys, the key size in bits.
|
|
|
|
* - For EC keys, the curve (\c MBEDTLS_ECP_DP_xxx).
|
|
|
|
*
|
|
|
|
* \return The status from the underlying type-specific key
|
|
|
|
* generation function.
|
|
|
|
* \return -1 if the key type is not recognized.
|
|
|
|
*/
|
|
|
|
static int pk_genkey( mbedtls_pk_context *pk, int parameter )
|
2013-09-21 12:31:05 +02:00
|
|
|
{
|
2013-12-19 17:29:52 +01:00
|
|
|
((void) pk);
|
2019-11-05 16:42:13 +01:00
|
|
|
(void) parameter;
|
2013-12-19 17:29:52 +01:00
|
|
|
|
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 )
|
2019-11-05 16:42:13 +01:00
|
|
|
return mbedtls_rsa_gen_key( mbedtls_pk_rsa( *pk ),
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_std_rand, NULL,
|
2019-11-05 16:42:13 +01:00
|
|
|
parameter, 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,
|
2019-11-05 16:42:13 +01:00
|
|
|
parameter ) ) != 0 )
|
2013-09-21 12:31:05 +02:00
|
|
|
return( ret );
|
|
|
|
|
2020-06-10 14:08:26 +02:00
|
|
|
return mbedtls_ecp_gen_keypair( &mbedtls_pk_ec( *pk )->grp,
|
|
|
|
&mbedtls_pk_ec( *pk )->d,
|
|
|
|
&mbedtls_pk_ec( *pk )->Q,
|
|
|
|
mbedtls_test_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 )
|
|
|
|
{
|
2017-09-07 11:57:48 +02:00
|
|
|
return( mbedtls_rsa_pkcs1_decrypt( (mbedtls_rsa_context *) ctx,
|
2020-06-10 14:08:26 +02:00
|
|
|
mbedtls_test_rnd_std_rand, NULL, mode,
|
|
|
|
olen, input, output, output_max_len ) );
|
2014-04-03 22:09:18 +02:00
|
|
|
}
|
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 )
|
|
|
|
{
|
2017-05-03 17:43:15 +02:00
|
|
|
((void) f_rng);
|
|
|
|
((void) p_rng);
|
2020-06-10 14:08:26 +02:00
|
|
|
return( mbedtls_rsa_pkcs1_sign( (mbedtls_rsa_context *) ctx,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL, mode,
|
|
|
|
md_alg, hashlen, hash, sig ) );
|
2014-04-03 22:09:18 +02:00
|
|
|
}
|
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 */
|
2018-10-31 10:28:01 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
|
|
|
|
/*
|
2019-05-27 14:53:13 +02:00
|
|
|
* Generate a key using PSA and return a handle to that key,
|
|
|
|
* or 0 if the key generation failed.
|
2018-10-31 16:22:49 +01:00
|
|
|
* The key uses NIST P-256 and is usable for signing with SHA-256.
|
2018-10-31 10:28:01 +01:00
|
|
|
*/
|
2019-01-08 15:36:01 +01:00
|
|
|
psa_key_handle_t pk_psa_genkey( void )
|
2018-10-31 10:28:01 +01:00
|
|
|
{
|
2019-01-08 15:36:01 +01:00
|
|
|
psa_key_handle_t key;
|
2019-05-27 14:53:13 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2019-12-03 21:19:09 +01:00
|
|
|
const psa_key_type_t type =
|
2020-06-02 18:19:28 +02:00
|
|
|
PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_FAMILY_SECP_R1 );
|
2018-10-31 10:28:01 +01:00
|
|
|
const size_t bits = 256;
|
|
|
|
|
2019-11-26 17:01:59 +01:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
|
2019-05-27 14:53:13 +02:00
|
|
|
psa_set_key_algorithm( &attributes, PSA_ALG_ECDSA(PSA_ALG_SHA_256) );
|
|
|
|
psa_set_key_type( &attributes, type );
|
|
|
|
psa_set_key_bits( &attributes, bits );
|
|
|
|
PSA_ASSERT( psa_generate_key( &attributes, &key ) );
|
2018-10-31 10:28:01 +01:00
|
|
|
|
2019-05-27 14:53:13 +02:00
|
|
|
exit:
|
2018-10-31 10:28:01 +01:00
|
|
|
return( key );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
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
|
|
|
|
*/
|
|
|
|
|
2018-10-31 10:28:01 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_ECDSA_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED */
|
2018-10-31 09:57:45 +01:00
|
|
|
void pk_psa_utils( )
|
|
|
|
{
|
2018-10-31 11:14:36 +01:00
|
|
|
mbedtls_pk_context pk, pk2;
|
2019-01-08 15:36:01 +01:00
|
|
|
psa_key_handle_t key;
|
2019-05-27 14:53:13 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-10-31 09:57:45 +01:00
|
|
|
|
2018-11-06 09:34:30 +01:00
|
|
|
const char * const name = "Opaque";
|
2018-10-31 10:36:51 +01:00
|
|
|
const size_t bitlen = 256; /* harcoded in genkey() */
|
|
|
|
|
2018-10-31 11:14:36 +01:00
|
|
|
mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
|
|
|
|
unsigned char b1[1], b2[1];
|
|
|
|
size_t len;
|
|
|
|
mbedtls_pk_debug_item dbg;
|
|
|
|
|
2019-08-01 12:47:40 +02:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2019-02-13 12:30:22 +01:00
|
|
|
|
2018-10-31 09:57:45 +01:00
|
|
|
mbedtls_pk_init( &pk );
|
2018-10-31 11:14:36 +01:00
|
|
|
mbedtls_pk_init( &pk2 );
|
2018-10-31 09:57:45 +01:00
|
|
|
|
2019-06-07 15:39:07 +02:00
|
|
|
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
2019-06-07 15:38:59 +02:00
|
|
|
|
2020-07-30 17:48:03 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_setup_opaque( &pk, PSA_KEY_HANDLE_INIT ) ==
|
2018-10-31 10:57:29 +01:00
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
mbedtls_pk_free( &pk );
|
|
|
|
mbedtls_pk_init( &pk );
|
|
|
|
|
2018-10-31 10:28:01 +01:00
|
|
|
key = pk_psa_genkey();
|
2019-05-27 14:53:13 +02:00
|
|
|
if( key == 0 )
|
|
|
|
goto exit;
|
2018-10-31 10:28:01 +01:00
|
|
|
|
2018-11-06 09:34:30 +01:00
|
|
|
TEST_ASSERT( mbedtls_pk_setup_opaque( &pk, key ) == 0 );
|
2018-10-31 09:57:45 +01:00
|
|
|
|
2018-11-06 09:34:30 +01:00
|
|
|
TEST_ASSERT( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_OPAQUE );
|
2018-10-31 09:57:45 +01:00
|
|
|
TEST_ASSERT( strcmp( mbedtls_pk_get_name( &pk), name ) == 0 );
|
|
|
|
|
2018-10-31 10:36:51 +01:00
|
|
|
TEST_ASSERT( mbedtls_pk_get_bitlen( &pk ) == bitlen );
|
|
|
|
TEST_ASSERT( mbedtls_pk_get_len( &pk ) == bitlen / 8 );
|
|
|
|
|
2018-10-31 10:57:29 +01:00
|
|
|
TEST_ASSERT( mbedtls_pk_can_do( &pk, MBEDTLS_PK_ECKEY ) == 1 );
|
|
|
|
TEST_ASSERT( mbedtls_pk_can_do( &pk, MBEDTLS_PK_ECDSA ) == 1 );
|
|
|
|
TEST_ASSERT( mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) == 0 );
|
|
|
|
|
2018-10-31 11:14:36 +01:00
|
|
|
/* unsupported operations: verify, decrypt, encrypt */
|
|
|
|
TEST_ASSERT( mbedtls_pk_verify( &pk, md_alg,
|
|
|
|
b1, sizeof( b1), b2, sizeof( b2 ) )
|
|
|
|
== MBEDTLS_ERR_PK_TYPE_MISMATCH );
|
|
|
|
TEST_ASSERT( mbedtls_pk_decrypt( &pk, b1, sizeof( b1 ),
|
|
|
|
b2, &len, sizeof( b2 ),
|
|
|
|
NULL, NULL )
|
|
|
|
== MBEDTLS_ERR_PK_TYPE_MISMATCH );
|
|
|
|
TEST_ASSERT( mbedtls_pk_encrypt( &pk, b1, sizeof( b1 ),
|
|
|
|
b2, &len, sizeof( b2 ),
|
|
|
|
NULL, NULL )
|
|
|
|
== MBEDTLS_ERR_PK_TYPE_MISMATCH );
|
|
|
|
|
|
|
|
/* unsupported functions: check_pair, debug */
|
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &pk2,
|
|
|
|
mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY ) ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_pk_check_pair( &pk, &pk2 )
|
|
|
|
== MBEDTLS_ERR_PK_TYPE_MISMATCH );
|
|
|
|
TEST_ASSERT( mbedtls_pk_debug( &pk, &dbg )
|
|
|
|
== MBEDTLS_ERR_PK_TYPE_MISMATCH );
|
|
|
|
|
2018-10-31 10:28:01 +01:00
|
|
|
/* test that freeing the context does not destroy the key */
|
2018-10-31 09:57:45 +01:00
|
|
|
mbedtls_pk_free( &pk );
|
2019-05-27 14:53:13 +02:00
|
|
|
TEST_ASSERT( PSA_SUCCESS == psa_get_key_attributes( key, &attributes ) );
|
2018-10-31 10:28:01 +01:00
|
|
|
TEST_ASSERT( PSA_SUCCESS == psa_destroy_key( key ) );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_pk_free( &pk ); /* redundant except upon error */
|
2018-10-31 11:14:36 +01:00
|
|
|
mbedtls_pk_free( &pk2 );
|
2019-08-01 12:47:40 +02:00
|
|
|
PSA_DONE( );
|
2018-10-31 09:57:45 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
|
2019-01-31 14:20:20 +01:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void valid_parameters( )
|
|
|
|
{
|
|
|
|
mbedtls_pk_context pk;
|
|
|
|
unsigned char buf[1];
|
|
|
|
size_t len;
|
|
|
|
void *options = NULL;
|
|
|
|
|
|
|
|
mbedtls_pk_init( &pk );
|
|
|
|
|
|
|
|
TEST_VALID_PARAM( mbedtls_pk_free( NULL ) );
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
|
|
|
|
TEST_VALID_PARAM( mbedtls_pk_restart_free( NULL ) );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &pk, NULL ) ==
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
/* In informational functions, we accept NULL where a context pointer
|
|
|
|
* is expected because that's what the library has done forever.
|
|
|
|
* We do not document that NULL is accepted, so we may wish to change
|
|
|
|
* the behavior in a future version. */
|
|
|
|
TEST_ASSERT( mbedtls_pk_get_bitlen( NULL ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_pk_get_len( NULL ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_pk_can_do( NULL, MBEDTLS_PK_NONE ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_sign_restartable( &pk,
|
|
|
|
MBEDTLS_MD_NONE,
|
|
|
|
NULL, 0,
|
|
|
|
buf, &len,
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_std_rand, NULL,
|
2019-01-31 14:20:20 +01:00
|
|
|
NULL ) ==
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_sign_restartable( &pk,
|
|
|
|
MBEDTLS_MD_NONE,
|
|
|
|
NULL, 0,
|
|
|
|
buf, &len,
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_std_rand, NULL,
|
2019-01-31 14:20:20 +01:00
|
|
|
NULL ) ==
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_sign( &pk,
|
|
|
|
MBEDTLS_MD_NONE,
|
|
|
|
NULL, 0,
|
|
|
|
buf, &len,
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_std_rand, NULL ) ==
|
2019-01-31 14:20:20 +01:00
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_verify_restartable( &pk,
|
|
|
|
MBEDTLS_MD_NONE,
|
|
|
|
NULL, 0,
|
|
|
|
buf, sizeof( buf ),
|
|
|
|
NULL ) ==
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_verify( &pk,
|
|
|
|
MBEDTLS_MD_NONE,
|
|
|
|
NULL, 0,
|
|
|
|
buf, sizeof( buf ) ) ==
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_verify_ext( MBEDTLS_PK_NONE, options,
|
|
|
|
&pk,
|
|
|
|
MBEDTLS_MD_NONE,
|
|
|
|
NULL, 0,
|
|
|
|
buf, sizeof( buf ) ) ==
|
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_encrypt( &pk,
|
|
|
|
NULL, 0,
|
|
|
|
NULL, &len, 0,
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_std_rand, NULL ) ==
|
2019-01-31 14:20:20 +01:00
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_decrypt( &pk,
|
|
|
|
NULL, 0,
|
|
|
|
NULL, &len, 0,
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_std_rand, NULL ) ==
|
2019-01-31 14:20:20 +01:00
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PK_PARSE_C)
|
|
|
|
TEST_ASSERT( mbedtls_pk_parse_key( &pk, NULL, 0, NULL, 1 ) ==
|
|
|
|
MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_parse_public_key( &pk, NULL, 0 ) ==
|
|
|
|
MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
|
|
|
|
#endif /* MBEDTLS_PK_PARSE_C */
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PK_WRITE_C */
|
|
|
|
void valid_parameters_pkwrite( data_t *key_data )
|
|
|
|
{
|
|
|
|
mbedtls_pk_context pk;
|
|
|
|
|
|
|
|
/* For the write tests to be effective, we need a valid key pair. */
|
|
|
|
mbedtls_pk_init( &pk );
|
|
|
|
TEST_ASSERT( mbedtls_pk_parse_key( &pk,
|
|
|
|
key_data->x, key_data->len,
|
|
|
|
NULL, 0 ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_write_key_der( &pk, NULL, 0 ) ==
|
|
|
|
MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_write_pubkey_der( &pk, NULL, 0 ) ==
|
|
|
|
MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PEM_WRITE_C)
|
|
|
|
TEST_ASSERT( mbedtls_pk_write_key_pem( &pk, NULL, 0 ) ==
|
|
|
|
MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_write_pubkey_pem( &pk, NULL, 0 ) ==
|
|
|
|
MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
|
|
|
|
#endif /* MBEDTLS_PEM_WRITE_C */
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_pk_free( &pk );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
|
|
|
|
void invalid_parameters( )
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
unsigned char *null_buf = NULL;
|
|
|
|
unsigned char buf[1];
|
|
|
|
unsigned char *p = buf;
|
|
|
|
char str[1] = {0};
|
|
|
|
mbedtls_pk_context pk;
|
|
|
|
mbedtls_md_type_t valid_md = MBEDTLS_MD_SHA256;
|
|
|
|
void *options = buf;
|
|
|
|
|
|
|
|
(void) null_buf;
|
|
|
|
(void) p;
|
|
|
|
(void) str;
|
|
|
|
|
|
|
|
mbedtls_pk_init( &pk );
|
|
|
|
|
|
|
|
TEST_INVALID_PARAM( mbedtls_pk_init( NULL ) );
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
|
|
|
|
TEST_INVALID_PARAM( mbedtls_pk_restart_init( NULL ) );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_setup( NULL, NULL ) );
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_setup_rsa_alt( NULL, buf,
|
|
|
|
NULL, NULL, NULL ) );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_verify_restartable( NULL,
|
|
|
|
MBEDTLS_MD_NONE,
|
|
|
|
buf, sizeof( buf ),
|
|
|
|
buf, sizeof( buf ),
|
|
|
|
NULL ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_verify_restartable( &pk,
|
|
|
|
MBEDTLS_MD_NONE,
|
|
|
|
NULL, sizeof( buf ),
|
|
|
|
buf, sizeof( buf ),
|
|
|
|
NULL ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_verify_restartable( &pk,
|
|
|
|
valid_md,
|
|
|
|
NULL, 0,
|
|
|
|
buf, sizeof( buf ),
|
|
|
|
NULL ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_verify_restartable( &pk,
|
|
|
|
MBEDTLS_MD_NONE,
|
|
|
|
buf, sizeof( buf ),
|
|
|
|
NULL, sizeof( buf ),
|
|
|
|
NULL ) );
|
|
|
|
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_verify( NULL,
|
|
|
|
MBEDTLS_MD_NONE,
|
|
|
|
buf, sizeof( buf ),
|
|
|
|
buf, sizeof( buf ) ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_verify( &pk,
|
|
|
|
MBEDTLS_MD_NONE,
|
|
|
|
NULL, sizeof( buf ),
|
|
|
|
buf, sizeof( buf ) ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_verify( &pk,
|
|
|
|
valid_md,
|
|
|
|
NULL, 0,
|
|
|
|
buf, sizeof( buf ) ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_verify( &pk,
|
|
|
|
MBEDTLS_MD_NONE,
|
|
|
|
buf, sizeof( buf ),
|
|
|
|
NULL, sizeof( buf ) ) );
|
|
|
|
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_verify_ext( MBEDTLS_PK_NONE, options,
|
|
|
|
NULL,
|
|
|
|
MBEDTLS_MD_NONE,
|
|
|
|
buf, sizeof( buf ),
|
|
|
|
buf, sizeof( buf ) ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_verify_ext( MBEDTLS_PK_NONE, options,
|
|
|
|
&pk,
|
|
|
|
MBEDTLS_MD_NONE,
|
|
|
|
NULL, sizeof( buf ),
|
|
|
|
buf, sizeof( buf ) ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_verify_ext( MBEDTLS_PK_NONE, options,
|
|
|
|
&pk,
|
|
|
|
valid_md,
|
|
|
|
NULL, 0,
|
|
|
|
buf, sizeof( buf ) ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_verify_ext( MBEDTLS_PK_NONE, options,
|
|
|
|
&pk,
|
|
|
|
MBEDTLS_MD_NONE,
|
|
|
|
buf, sizeof( buf ),
|
|
|
|
NULL, sizeof( buf ) ) );
|
|
|
|
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
2020-06-10 14:08:26 +02:00
|
|
|
mbedtls_pk_sign_restartable( NULL, MBEDTLS_MD_NONE, buf, sizeof( buf ),
|
|
|
|
buf, &len, mbedtls_test_rnd_std_rand,
|
|
|
|
NULL, NULL ) );
|
|
|
|
|
2019-01-31 14:20:20 +01:00
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
2020-06-10 14:08:26 +02:00
|
|
|
mbedtls_pk_sign_restartable( &pk, MBEDTLS_MD_NONE, NULL, sizeof( buf ),
|
|
|
|
buf, &len, mbedtls_test_rnd_std_rand,
|
|
|
|
NULL, NULL ) );
|
|
|
|
|
2019-01-31 14:20:20 +01:00
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
2020-06-10 14:08:26 +02:00
|
|
|
mbedtls_pk_sign_restartable( &pk, valid_md, NULL, 0, buf, &len,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL, NULL ) );
|
|
|
|
|
2019-01-31 14:20:20 +01:00
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
2020-06-10 14:08:26 +02:00
|
|
|
mbedtls_pk_sign_restartable( &pk, MBEDTLS_MD_NONE, buf, sizeof( buf ),
|
|
|
|
NULL, &len, mbedtls_test_rnd_std_rand,
|
|
|
|
NULL, NULL ) );
|
2019-01-31 14:20:20 +01:00
|
|
|
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
2020-06-10 14:08:26 +02:00
|
|
|
mbedtls_pk_sign( NULL, MBEDTLS_MD_NONE, buf, sizeof( buf ),
|
|
|
|
buf, &len, mbedtls_test_rnd_std_rand, NULL ) );
|
|
|
|
|
2019-01-31 14:20:20 +01:00
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
2020-06-10 14:08:26 +02:00
|
|
|
mbedtls_pk_sign( &pk, MBEDTLS_MD_NONE, NULL, sizeof( buf ),
|
|
|
|
buf, &len, mbedtls_test_rnd_std_rand, NULL ) );
|
|
|
|
|
2019-01-31 14:20:20 +01:00
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
2020-06-10 14:08:26 +02:00
|
|
|
mbedtls_pk_sign( &pk, valid_md, NULL, 0, buf, &len,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL ) );
|
|
|
|
|
2019-01-31 14:20:20 +01:00
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
2020-06-10 14:08:26 +02:00
|
|
|
mbedtls_pk_sign( &pk, MBEDTLS_MD_NONE, buf, sizeof( buf ), NULL, &len,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL ) );
|
2019-01-31 14:20:20 +01:00
|
|
|
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
2020-06-10 14:08:26 +02:00
|
|
|
mbedtls_pk_decrypt( NULL, buf, sizeof( buf ), buf, &len, sizeof( buf ),
|
|
|
|
mbedtls_test_rnd_std_rand, NULL ) );
|
|
|
|
|
2019-01-31 14:20:20 +01:00
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
2020-06-10 14:08:26 +02:00
|
|
|
mbedtls_pk_decrypt( &pk, NULL, sizeof( buf ), buf, &len, sizeof( buf ),
|
|
|
|
mbedtls_test_rnd_std_rand, NULL ) );
|
|
|
|
|
2019-01-31 14:20:20 +01:00
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
2020-06-10 14:08:26 +02:00
|
|
|
mbedtls_pk_decrypt( &pk, buf, sizeof( buf ), NULL, &len, sizeof( buf ),
|
|
|
|
mbedtls_test_rnd_std_rand, NULL ) );
|
|
|
|
|
2019-01-31 14:20:20 +01:00
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
2020-06-10 14:08:26 +02:00
|
|
|
mbedtls_pk_decrypt( &pk, buf, sizeof( buf ), buf, NULL, sizeof( buf ),
|
|
|
|
mbedtls_test_rnd_std_rand, NULL ) );
|
2019-01-31 14:20:20 +01:00
|
|
|
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
2020-06-10 14:08:26 +02:00
|
|
|
mbedtls_pk_encrypt( NULL, buf, sizeof( buf ), buf, &len, sizeof( buf ),
|
|
|
|
mbedtls_test_rnd_std_rand, NULL ) );
|
|
|
|
|
2019-01-31 14:20:20 +01:00
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
2020-06-10 14:08:26 +02:00
|
|
|
mbedtls_pk_encrypt( &pk, NULL, sizeof( buf ), buf, &len, sizeof( buf ),
|
|
|
|
mbedtls_test_rnd_std_rand, NULL ) );
|
|
|
|
|
2019-01-31 14:20:20 +01:00
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
2020-06-10 14:08:26 +02:00
|
|
|
mbedtls_pk_encrypt( &pk, buf, sizeof( buf ), NULL, &len, sizeof( buf ),
|
|
|
|
mbedtls_test_rnd_std_rand, NULL ) );
|
|
|
|
|
2019-01-31 14:20:20 +01:00
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
2020-06-10 14:08:26 +02:00
|
|
|
mbedtls_pk_encrypt( &pk, buf, sizeof( buf ), buf, NULL, sizeof( buf ),
|
|
|
|
mbedtls_test_rnd_std_rand, NULL ) );
|
2019-01-31 14:20:20 +01:00
|
|
|
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_check_pair( NULL, &pk ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_check_pair( &pk, NULL ) );
|
|
|
|
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_debug( NULL, NULL ) );
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PK_PARSE_C)
|
|
|
|
#if defined(MBEDTLS_FS_IO)
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_load_file( NULL, &p, &len ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_load_file( str, NULL, &len ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_load_file( str, &p, NULL ) );
|
|
|
|
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_parse_keyfile( NULL, str, NULL ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_parse_keyfile( &pk, NULL, NULL ) );
|
|
|
|
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_parse_public_keyfile( NULL, str ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_parse_public_keyfile( &pk, NULL ) );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_parse_subpubkey( NULL, buf, &pk ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_parse_subpubkey( &null_buf, buf, &pk ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_parse_subpubkey( &p, NULL, &pk ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_parse_subpubkey( &p, buf, NULL ) );
|
|
|
|
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_parse_key( NULL,
|
|
|
|
buf, sizeof( buf ),
|
|
|
|
buf, sizeof( buf ) ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_parse_key( &pk,
|
|
|
|
NULL, sizeof( buf ),
|
|
|
|
buf, sizeof( buf ) ) );
|
|
|
|
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_parse_public_key( NULL,
|
|
|
|
buf, sizeof( buf ) ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_parse_public_key( &pk,
|
|
|
|
NULL, sizeof( buf ) ) );
|
|
|
|
#endif /* MBEDTLS_PK_PARSE_C */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PK_WRITE_C)
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_write_pubkey( NULL, p, &pk ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_write_pubkey( &null_buf, p, &pk ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_write_pubkey( &p, NULL, &pk ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_write_pubkey( &p, p, NULL ) );
|
|
|
|
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_write_pubkey_der( NULL,
|
|
|
|
buf, sizeof( buf ) ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_write_pubkey_der( &pk,
|
|
|
|
NULL, sizeof( buf ) ) );
|
|
|
|
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_write_key_der( NULL,
|
|
|
|
buf, sizeof( buf ) ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_write_key_der( &pk,
|
|
|
|
NULL, sizeof( buf ) ) );
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PEM_WRITE_C)
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_write_pubkey_pem( NULL,
|
|
|
|
buf, sizeof( buf ) ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_write_pubkey_pem( &pk,
|
|
|
|
NULL, sizeof( buf ) ) );
|
|
|
|
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_write_key_pem( NULL,
|
|
|
|
buf, sizeof( buf ) ) );
|
|
|
|
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
|
|
|
|
mbedtls_pk_write_key_pem( &pk,
|
|
|
|
NULL, sizeof( buf ) ) );
|
|
|
|
#endif /* MBEDTLS_PEM_WRITE_C */
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_PK_WRITE_C */
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-09-21 12:31:05 +02:00
|
|
|
/* BEGIN_CASE */
|
2019-11-05 16:42:13 +01:00
|
|
|
void pk_utils( int type, int parameter, int bitlen, int len, char * name )
|
2013-09-21 12:31:05 +02:00
|
|
|
{
|
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 );
|
2019-11-05 16:42:13 +01:00
|
|
|
TEST_ASSERT( pk_genkey( &pk, parameter ) == 0 );
|
2013-09-21 12:31:05 +02:00
|
|
|
|
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 ) );
|
2019-11-05 16:42:13 +01:00
|
|
|
TEST_ASSERT( mbedtls_pk_get_bitlen( &pk ) == (unsigned) bitlen );
|
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 */
|
2017-05-30 15:23:15 +02:00
|
|
|
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 ),
|
2017-09-07 11:57:48 +02:00
|
|
|
mbedtls_rsa_decrypt_func, mbedtls_rsa_sign_func,
|
|
|
|
mbedtls_rsa_key_len_func ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
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 */
|
2018-06-29 12:05:32 +02:00
|
|
|
void pk_rsa_verify_test_vec( data_t * message_str, int digest, int mod,
|
2017-06-09 05:32:58 +02:00
|
|
|
int radix_N, char * input_N, int radix_E,
|
2018-06-29 12:05:32 +02:00
|
|
|
char * input_E, data_t * result_str,
|
2017-05-30 15:23:15 +02:00
|
|
|
int result )
|
2013-09-21 12:31:05 +02:00
|
|
|
{
|
2018-11-21 12:44:31 +01:00
|
|
|
unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_context *rsa;
|
|
|
|
mbedtls_pk_context pk;
|
2017-08-17 14:33:31 +02:00
|
|
|
mbedtls_pk_restart_ctx *rs_ctx = NULL;
|
2017-05-08 10:09:36 +02:00
|
|
|
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
|
2017-08-17 14:33:31 +02:00
|
|
|
mbedtls_pk_restart_ctx ctx;
|
2017-05-08 10:09:36 +02:00
|
|
|
|
|
|
|
rs_ctx = &ctx;
|
2017-08-17 14:33:31 +02:00
|
|
|
mbedtls_pk_restart_init( rs_ctx );
|
2018-07-02 12:23:23 +02:00
|
|
|
// this setting would ensure restart would happen if ECC was used
|
|
|
|
mbedtls_ecp_set_max_ops( 1 );
|
2017-05-08 10:09:36 +02:00
|
|
|
#endif
|
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
|
|
|
|
2018-11-21 12:44:31 +01:00
|
|
|
memset( hash_result, 0x00, MBEDTLS_MD_MAX_SIZE );
|
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( 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
|
|
|
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_md_info_from_type( digest ) != NULL )
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str->x, message_str->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,
|
2017-06-09 05:32:58 +02:00
|
|
|
result_str->x, mbedtls_pk_get_len( &pk ) ) == result );
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2017-05-08 10:09:36 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, digest, hash_result, 0,
|
2018-09-11 11:08:12 +02:00
|
|
|
result_str->x, mbedtls_pk_get_len( &pk ), rs_ctx ) == result );
|
2017-05-08 10:09:36 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2017-05-08 10:09:36 +02:00
|
|
|
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
|
2017-08-17 14:33:31 +02:00
|
|
|
mbedtls_pk_restart_free( rs_ctx );
|
2017-05-08 10:09:36 +02:00
|
|
|
#endif
|
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 */
|
2018-06-29 12:05:32 +02:00
|
|
|
void pk_rsa_verify_ext_test_vec( data_t * message_str, int digest,
|
2017-06-09 05:32:58 +02:00
|
|
|
int mod, int radix_N, char * input_N,
|
|
|
|
int radix_E, char * input_E,
|
2018-06-29 12:05:32 +02:00
|
|
|
data_t * result_str, int pk_type,
|
2017-05-30 15:23:15 +02:00
|
|
|
int mgf1_hash_id, int salt_len, int result )
|
2014-06-05 13:41:44 +02:00
|
|
|
{
|
2018-11-21 12:44:31 +01:00
|
|
|
unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
|
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;
|
|
|
|
size_t hash_len;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_init( &pk );
|
2014-06-05 13:41:44 +02:00
|
|
|
|
2018-11-21 12:44:31 +01:00
|
|
|
memset( hash_result, 0x00, sizeof( hash_result ) );
|
2014-06-05 13:41:44 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
|
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 ),
|
2017-06-09 05:32:58 +02:00
|
|
|
message_str->x, message_str->len, hash_result ) == 0 );
|
2014-06-05 13:41:44 +02:00
|
|
|
hash_len = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-09 05:32:58 +02:00
|
|
|
memcpy( hash_result, message_str->x, message_str->len );
|
|
|
|
hash_len = message_str->len;
|
2014-06-05 13:41:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2017-06-09 05:32:58 +02:00
|
|
|
result_str->x, 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 */
|
2018-06-29 12:05:32 +02:00
|
|
|
void pk_ec_test_vec( int type, int id, data_t * key, data_t * hash,
|
|
|
|
data_t * sig, int ret )
|
2013-09-21 12:31:05 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_context pk;
|
|
|
|
mbedtls_ecp_keypair *eckey;
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_init( &pk );
|
2019-08-01 12:47:40 +02:00
|
|
|
PSA_INIT( );
|
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
|
|
|
|
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,
|
2017-06-09 05:32:58 +02:00
|
|
|
key->x, key->len ) == 0 );
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2020-09-08 14:58:28 +02:00
|
|
|
// MBEDTLS_MD_NONE is used since it will be ignored.
|
|
|
|
TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_NONE,
|
2017-06-09 05:32:58 +02:00
|
|
|
hash->x, hash->len, sig->x, sig->len ) == ret );
|
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 );
|
2019-08-01 12:47:40 +02:00
|
|
|
PSA_DONE( );
|
2013-09-21 12:31:05 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2017-05-08 10:09:36 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_C:MBEDTLS_ECDSA_DETERMINISTIC */
|
|
|
|
void pk_sign_verify_restart( int pk_type, int grp_id, char *d_str,
|
|
|
|
char *QX_str, char *QY_str,
|
2020-06-25 09:03:34 +02:00
|
|
|
int md_alg, char *msg, data_t *sig_check,
|
2017-05-08 10:09:36 +02:00
|
|
|
int max_ops, int min_restart, int max_restart )
|
|
|
|
{
|
|
|
|
int ret, cnt_restart;
|
2017-08-17 14:33:31 +02:00
|
|
|
mbedtls_pk_restart_ctx rs_ctx;
|
2017-05-08 10:09:36 +02:00
|
|
|
mbedtls_pk_context prv, pub;
|
|
|
|
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
|
|
|
|
unsigned char sig[MBEDTLS_ECDSA_MAX_LEN];
|
2020-06-25 09:03:34 +02:00
|
|
|
size_t hlen, slen;
|
2017-05-08 10:09:36 +02:00
|
|
|
const mbedtls_md_info_t *md_info;
|
|
|
|
|
2017-08-17 14:33:31 +02:00
|
|
|
mbedtls_pk_restart_init( &rs_ctx );
|
2017-05-08 10:09:36 +02:00
|
|
|
mbedtls_pk_init( &prv );
|
|
|
|
mbedtls_pk_init( &pub );
|
|
|
|
memset( hash, 0, sizeof( hash ) );
|
|
|
|
memset( sig, 0, sizeof( sig ) );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &prv, mbedtls_pk_info_from_type( pk_type ) ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &mbedtls_pk_ec( prv )->grp, grp_id ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &mbedtls_pk_ec( prv )->d, 16, d_str ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &pub, mbedtls_pk_info_from_type( pk_type ) ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &mbedtls_pk_ec( pub )->grp, grp_id ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_point_read_string( &mbedtls_pk_ec( pub )->Q, 16, QX_str, QY_str ) == 0 );
|
|
|
|
|
|
|
|
md_info = mbedtls_md_info_from_type( md_alg );
|
|
|
|
TEST_ASSERT( md_info != NULL );
|
|
|
|
|
|
|
|
hlen = mbedtls_md_get_size( md_info );
|
2020-01-21 16:52:08 +01:00
|
|
|
TEST_ASSERT( mbedtls_md( md_info,
|
|
|
|
(const unsigned char *) msg, strlen( msg ),
|
|
|
|
hash ) == 0 );
|
2017-05-08 10:09:36 +02:00
|
|
|
|
|
|
|
mbedtls_ecp_set_max_ops( max_ops );
|
|
|
|
|
|
|
|
slen = sizeof( sig );
|
|
|
|
cnt_restart = 0;
|
|
|
|
do {
|
|
|
|
ret = mbedtls_pk_sign_restartable( &prv, md_alg, hash, hlen,
|
|
|
|
sig, &slen, NULL, NULL, &rs_ctx );
|
|
|
|
} while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
|
|
|
|
|
|
|
|
TEST_ASSERT( ret == 0 );
|
2020-06-25 09:03:34 +02:00
|
|
|
TEST_ASSERT( slen == sig_check->len );
|
|
|
|
TEST_ASSERT( memcmp( sig, sig_check->x, slen ) == 0 );
|
2017-05-08 10:09:36 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( cnt_restart >= min_restart );
|
|
|
|
TEST_ASSERT( cnt_restart <= max_restart );
|
|
|
|
|
|
|
|
cnt_restart = 0;
|
|
|
|
do {
|
|
|
|
ret = mbedtls_pk_verify_restartable( &pub, md_alg,
|
|
|
|
hash, hlen, sig, slen, &rs_ctx );
|
|
|
|
} while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
|
|
|
|
|
|
|
|
TEST_ASSERT( ret == 0 );
|
|
|
|
TEST_ASSERT( cnt_restart >= min_restart );
|
|
|
|
TEST_ASSERT( cnt_restart <= max_restart );
|
|
|
|
|
|
|
|
hash[0]++;
|
|
|
|
do {
|
|
|
|
ret = mbedtls_pk_verify_restartable( &pub, md_alg,
|
|
|
|
hash, hlen, sig, slen, &rs_ctx );
|
|
|
|
} while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
|
|
|
|
TEST_ASSERT( ret != 0 );
|
|
|
|
hash[0]--;
|
|
|
|
|
|
|
|
sig[0]++;
|
|
|
|
do {
|
|
|
|
ret = mbedtls_pk_verify_restartable( &pub, md_alg,
|
|
|
|
hash, hlen, sig, slen, &rs_ctx );
|
|
|
|
} while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
|
|
|
|
TEST_ASSERT( ret != 0 );
|
|
|
|
sig[0]--;
|
|
|
|
|
2017-08-28 12:20:39 +02:00
|
|
|
/* Do we leak memory when aborting? try verify then sign
|
|
|
|
* This test only makes sense when we actually restart */
|
|
|
|
if( min_restart > 0 )
|
|
|
|
{
|
|
|
|
ret = mbedtls_pk_verify_restartable( &pub, md_alg,
|
|
|
|
hash, hlen, sig, slen, &rs_ctx );
|
|
|
|
TEST_ASSERT( ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
|
|
|
|
mbedtls_pk_restart_free( &rs_ctx );
|
2017-05-08 10:09:36 +02:00
|
|
|
|
2017-08-28 12:20:39 +02:00
|
|
|
slen = sizeof( sig );
|
|
|
|
ret = mbedtls_pk_sign_restartable( &prv, md_alg, hash, hlen,
|
|
|
|
sig, &slen, NULL, NULL, &rs_ctx );
|
|
|
|
TEST_ASSERT( ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
|
|
|
|
}
|
2017-05-08 10:09:36 +02:00
|
|
|
|
|
|
|
exit:
|
2017-08-17 14:33:31 +02:00
|
|
|
mbedtls_pk_restart_free( &rs_ctx );
|
2017-05-08 10:09:36 +02:00
|
|
|
mbedtls_pk_free( &prv );
|
|
|
|
mbedtls_pk_free( &pub );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
|
2019-11-05 16:42:13 +01:00
|
|
|
void pk_sign_verify( int type, int parameter, int sign_ret, int verify_ret )
|
2013-09-21 12:31:05 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_context pk;
|
2013-09-21 12:31:05 +02:00
|
|
|
size_t sig_len;
|
2019-11-05 16:32:32 +01:00
|
|
|
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
|
|
|
|
unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
|
2017-05-08 10:09:36 +02:00
|
|
|
void *rs_ctx = NULL;
|
|
|
|
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
|
2017-08-17 14:33:31 +02:00
|
|
|
mbedtls_pk_restart_ctx ctx;
|
2017-05-08 10:09:36 +02:00
|
|
|
|
|
|
|
rs_ctx = &ctx;
|
2017-08-17 14:33:31 +02:00
|
|
|
mbedtls_pk_restart_init( rs_ctx );
|
2018-07-02 12:23:23 +02:00
|
|
|
/* This value is large enough that the operation will complete in one run.
|
|
|
|
* See comments at the top of ecp_test_vect_restart in
|
|
|
|
* test_suite_ecp.function for estimates of operation counts. */
|
2017-05-08 10:09:36 +02:00
|
|
|
mbedtls_ecp_set_max_ops( 42000 );
|
|
|
|
#endif
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_init( &pk );
|
2019-08-01 12:47:40 +02:00
|
|
|
PSA_INIT( );
|
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 );
|
2019-11-05 16:42:13 +01:00
|
|
|
TEST_ASSERT( pk_genkey( &pk, parameter ) == 0 );
|
2013-09-21 12:31:05 +02:00
|
|
|
|
2017-05-08 10:09:36 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_sign_restartable( &pk, MBEDTLS_MD_SHA256,
|
|
|
|
hash, sizeof hash, sig, &sig_len,
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_std_rand, NULL, rs_ctx ) == sign_ret );
|
2019-11-05 16:32:32 +01:00
|
|
|
if( sign_ret == 0 )
|
|
|
|
TEST_ASSERT( sig_len <= MBEDTLS_PK_SIGNATURE_MAX_SIZE );
|
|
|
|
else
|
2019-11-12 11:08:23 +01:00
|
|
|
sig_len = MBEDTLS_PK_SIGNATURE_MAX_SIZE;
|
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 );
|
|
|
|
|
2017-05-08 10:09:36 +02:00
|
|
|
if( verify_ret == 0 )
|
|
|
|
{
|
|
|
|
hash[0]++;
|
|
|
|
TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256,
|
|
|
|
hash, sizeof hash, sig, sig_len ) != 0 );
|
|
|
|
hash[0]--;
|
|
|
|
|
|
|
|
sig[0]++;
|
|
|
|
TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256,
|
|
|
|
hash, sizeof hash, sig, sig_len ) != 0 );
|
|
|
|
sig[0]--;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_sign( &pk, MBEDTLS_MD_SHA256, hash, sizeof hash,
|
2020-06-10 14:08:26 +02:00
|
|
|
sig, &sig_len,
|
|
|
|
mbedtls_test_rnd_std_rand,
|
|
|
|
NULL ) == sign_ret );
|
2019-11-05 16:32:32 +01:00
|
|
|
if( sign_ret == 0 )
|
|
|
|
TEST_ASSERT( sig_len <= MBEDTLS_PK_SIGNATURE_MAX_SIZE );
|
|
|
|
else
|
2019-11-12 11:08:23 +01:00
|
|
|
sig_len = MBEDTLS_PK_SIGNATURE_MAX_SIZE;
|
2017-05-08 10:09:36 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, MBEDTLS_MD_SHA256,
|
|
|
|
hash, sizeof hash, sig, sig_len, rs_ctx ) == verify_ret );
|
|
|
|
|
|
|
|
if( verify_ret == 0 )
|
|
|
|
{
|
|
|
|
hash[0]++;
|
|
|
|
TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, MBEDTLS_MD_SHA256,
|
|
|
|
hash, sizeof hash, sig, sig_len, rs_ctx ) != 0 );
|
|
|
|
hash[0]--;
|
|
|
|
|
|
|
|
sig[0]++;
|
|
|
|
TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, MBEDTLS_MD_SHA256,
|
|
|
|
hash, sizeof hash, sig, sig_len, rs_ctx ) != 0 );
|
|
|
|
sig[0]--;
|
|
|
|
}
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2017-05-08 10:09:36 +02:00
|
|
|
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
|
2017-08-17 14:33:31 +02:00
|
|
|
mbedtls_pk_restart_free( rs_ctx );
|
2017-05-08 10:09:36 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( &pk );
|
2019-08-01 12:47:40 +02:00
|
|
|
PSA_DONE( );
|
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 */
|
2018-06-29 12:05:32 +02:00
|
|
|
void pk_rsa_encrypt_test_vec( data_t * message, int mod, int radix_N,
|
2017-06-09 05:32:58 +02:00
|
|
|
char * input_N, int radix_E, char * input_E,
|
2018-06-29 12:05:32 +02:00
|
|
|
data_t * result, int ret )
|
2013-10-17 12:34:16 +02:00
|
|
|
{
|
2018-11-21 12:44:31 +01:00
|
|
|
unsigned char output[300];
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_context *rsa;
|
|
|
|
mbedtls_pk_context pk;
|
2017-05-30 15:23:15 +02:00
|
|
|
size_t olen;
|
2013-10-17 12:34:16 +02:00
|
|
|
|
2020-06-10 12:12:18 +02:00
|
|
|
memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
|
2013-10-17 12:34:16 +02:00
|
|
|
memset( output, 0, sizeof( output ) );
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_encrypt( &pk, message->x, message->len,
|
2020-06-10 14:08:26 +02:00
|
|
|
output, &olen, sizeof( output ),
|
|
|
|
mbedtls_test_rnd_pseudo_rand, &rnd_info ) == ret );
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT( olen == result->len );
|
|
|
|
TEST_ASSERT( memcmp( output, result->x, olen ) == 0 );
|
2013-10-17 12:34:16 +02:00
|
|
|
|
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 */
|
2018-06-29 12:05:32 +02:00
|
|
|
void pk_rsa_decrypt_test_vec( data_t * cipher, int mod, int radix_P,
|
2017-06-09 05:32:58 +02:00
|
|
|
char * input_P, int radix_Q, char * input_Q,
|
|
|
|
int radix_N, char * input_N, int radix_E,
|
2018-06-29 12:05:32 +02:00
|
|
|
char * input_E, data_t * clear, int ret )
|
2013-10-17 12:34:16 +02:00
|
|
|
{
|
2018-11-21 12:44:31 +01:00
|
|
|
unsigned char output[256];
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_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;
|
2017-05-30 15:23:15 +02:00
|
|
|
size_t olen;
|
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
|
|
|
|
2020-06-10 12:12:18 +02:00
|
|
|
memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
|
2013-10-17 12:34:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* 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;
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_decrypt( &pk, cipher->x, cipher->len,
|
2020-06-10 14:08:26 +02:00
|
|
|
output, &olen, sizeof( output ),
|
|
|
|
mbedtls_test_rnd_pseudo_rand, &rnd_info ) == ret );
|
2013-10-17 12:34:16 +02:00
|
|
|
if( ret == 0 )
|
|
|
|
{
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT( olen == clear->len );
|
|
|
|
TEST_ASSERT( memcmp( output, clear->x, olen ) == 0 );
|
2013-10-17 12:34:16 +02:00
|
|
|
}
|
|
|
|
|
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];
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
2013-10-17 12:46:39 +02:00
|
|
|
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
|
|
|
|
2020-06-10 12:12:18 +02:00
|
|
|
memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
|
2013-10-17 12:46:39 +02:00
|
|
|
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 ),
|
2020-06-10 14:08:26 +02:00
|
|
|
output, &olen, sizeof( output ),
|
|
|
|
mbedtls_test_rnd_pseudo_rand, &rnd_info ) == ret );
|
2013-10-17 12:46:39 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_decrypt( &pk, input, sizeof( input ),
|
2020-06-10 14:08:26 +02:00
|
|
|
output, &olen, sizeof( output ),
|
|
|
|
mbedtls_test_rnd_pseudo_rand, &rnd_info ) == ret );
|
2013-10-17 12:46:39 +02:00
|
|
|
|
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-05-08 12:15:49 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
|
2017-02-15 11:52:32 +01:00
|
|
|
void pk_rsa_overflow( )
|
|
|
|
{
|
|
|
|
mbedtls_pk_context pk;
|
2018-02-26 11:02:10 +01:00
|
|
|
size_t hash_len = SIZE_MAX, sig_len = SIZE_MAX;
|
|
|
|
unsigned char hash[50], sig[100];
|
2017-05-08 12:15:49 +02:00
|
|
|
|
|
|
|
if( SIZE_MAX <= UINT_MAX )
|
|
|
|
return;
|
2017-02-15 11:52:32 +01:00
|
|
|
|
2018-02-26 11:02:10 +01:00
|
|
|
memset( hash, 0x2a, sizeof hash );
|
|
|
|
memset( sig, 0, sizeof sig );
|
2017-02-15 11:52:32 +01:00
|
|
|
|
|
|
|
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,
|
2018-02-26 11:02:10 +01:00
|
|
|
MBEDTLS_MD_NONE, hash, hash_len, sig, sig_len ) ==
|
2017-02-15 11:52:32 +01:00
|
|
|
MBEDTLS_ERR_PK_BAD_INPUT_DATA );
|
|
|
|
#endif /* MBEDTLS_PKCS1_V21 */
|
|
|
|
|
2018-02-26 11:02:10 +01:00
|
|
|
TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_NONE, hash, hash_len,
|
|
|
|
sig, sig_len ) == MBEDTLS_ERR_PK_BAD_INPUT_DATA );
|
2017-02-15 11:52:32 +01:00
|
|
|
|
2020-06-10 14:08:26 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_sign( &pk, MBEDTLS_MD_NONE, hash, hash_len, sig,
|
|
|
|
&sig_len, mbedtls_test_rnd_std_rand, NULL )
|
|
|
|
== MBEDTLS_ERR_PK_BAD_INPUT_DATA );
|
2017-02-15 11:52:32 +01:00
|
|
|
|
|
|
|
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 */
|
2017-05-30 15:23:15 +02:00
|
|
|
void pk_rsa_alt( )
|
2014-04-03 22:09:18 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 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];
|
2018-11-21 12:44:31 +01:00
|
|
|
unsigned char hash[50], sig[64];
|
|
|
|
unsigned char msg[50], ciph[64], test[50];
|
2014-04-03 22:09:18 +02:00
|
|
|
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 );
|
2019-11-05 16:42:13 +01:00
|
|
|
TEST_ASSERT( pk_genkey( &rsa, RSA_KEY_SIZE ) == 0 );
|
2014-04-03 22:09:18 +02:00
|
|
|
|
|
|
|
/* 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 */
|
2017-05-08 12:15:49 +02:00
|
|
|
#if SIZE_MAX > UINT_MAX
|
2020-06-10 14:08:26 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_sign( &alt, MBEDTLS_MD_NONE, hash, SIZE_MAX, sig,
|
|
|
|
&sig_len, mbedtls_test_rnd_std_rand, NULL )
|
|
|
|
== MBEDTLS_ERR_PK_BAD_INPUT_DATA );
|
2017-05-08 12:15:49 +02:00
|
|
|
#endif /* SIZE_MAX > UINT_MAX */
|
2020-06-10 14:08:26 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_sign( &alt, MBEDTLS_MD_NONE, hash, sizeof hash, sig,
|
|
|
|
&sig_len, mbedtls_test_rnd_std_rand, NULL )
|
|
|
|
== 0 );
|
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,
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_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,
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_std_rand, NULL ) == 0 );
|
2014-04-03 22:09:18 +02:00
|
|
|
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,
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_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 */
|
2018-10-31 16:22:49 +01:00
|
|
|
|
2019-12-19 00:09:47 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C:MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_ECDSA_C */
|
|
|
|
void pk_psa_sign( int grpid_arg,
|
|
|
|
int psa_curve_arg, int expected_bits_arg )
|
2018-10-31 16:22:49 +01:00
|
|
|
{
|
2019-12-19 00:09:47 +01:00
|
|
|
mbedtls_ecp_group_id grpid = grpid_arg;
|
2018-10-31 16:22:49 +01:00
|
|
|
mbedtls_pk_context pk;
|
2019-12-19 00:09:47 +01:00
|
|
|
unsigned char hash[32];
|
|
|
|
unsigned char sig[MBEDTLS_ECDSA_MAX_LEN];
|
|
|
|
unsigned char pkey_legacy[200];
|
|
|
|
unsigned char pkey_psa[200];
|
2019-02-01 12:48:19 +01:00
|
|
|
unsigned char *pkey_legacy_start, *pkey_psa_start;
|
|
|
|
size_t sig_len, klen_legacy, klen_psa;
|
|
|
|
int ret;
|
|
|
|
psa_key_handle_t handle;
|
2019-12-18 20:44:15 +01:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2019-12-19 00:09:47 +01:00
|
|
|
psa_key_type_t expected_type = PSA_KEY_TYPE_ECC_KEY_PAIR( psa_curve_arg );
|
|
|
|
size_t expected_bits = expected_bits_arg;
|
2018-10-31 16:22:49 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This tests making signatures with a wrapped PSA key:
|
2019-02-01 12:48:19 +01:00
|
|
|
* - generate a fresh ECP legacy PK context
|
2018-10-31 16:22:49 +01:00
|
|
|
* - wrap it in a PK context and make a signature this way
|
|
|
|
* - extract the public key
|
|
|
|
* - parse it to a PK context and verify the signature this way
|
|
|
|
*/
|
|
|
|
|
2019-08-01 12:47:40 +02:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
2019-02-01 12:48:19 +01:00
|
|
|
/* Create legacy EC public/private key in PK context. */
|
2018-10-31 16:22:49 +01:00
|
|
|
mbedtls_pk_init( &pk );
|
2019-02-01 12:48:19 +01:00
|
|
|
TEST_ASSERT( mbedtls_pk_setup( &pk,
|
|
|
|
mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY ) ) == 0 );
|
2019-12-18 20:44:15 +01:00
|
|
|
TEST_ASSERT( mbedtls_ecp_gen_key( grpid,
|
2019-02-01 12:48:19 +01:00
|
|
|
(mbedtls_ecp_keypair*) pk.pk_ctx,
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_std_rand, NULL ) == 0 );
|
2019-02-01 12:48:19 +01:00
|
|
|
|
|
|
|
/* Export underlying public key for re-importing in a legacy context. */
|
|
|
|
ret = mbedtls_pk_write_pubkey_der( &pk, pkey_legacy,
|
|
|
|
sizeof( pkey_legacy ) );
|
|
|
|
TEST_ASSERT( ret >= 0 );
|
|
|
|
klen_legacy = (size_t) ret;
|
|
|
|
/* mbedtls_pk_write_pubkey_der() writes backwards in the data buffer. */
|
|
|
|
pkey_legacy_start = pkey_legacy + sizeof( pkey_legacy ) - klen_legacy;
|
|
|
|
|
|
|
|
/* Turn PK context into an opaque one. */
|
|
|
|
TEST_ASSERT( mbedtls_pk_wrap_as_opaque( &pk, &handle,
|
|
|
|
PSA_ALG_SHA_256 ) == 0 );
|
2018-10-31 16:22:49 +01:00
|
|
|
|
2019-12-18 20:44:15 +01:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
|
2019-12-19 00:09:47 +01:00
|
|
|
TEST_EQUAL( psa_get_key_type( &attributes ), expected_type );
|
2019-12-18 20:44:15 +01:00
|
|
|
TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
|
|
|
|
TEST_EQUAL( psa_get_key_lifetime( &attributes ),
|
|
|
|
PSA_KEY_LIFETIME_VOLATILE );
|
|
|
|
|
2018-10-31 16:22:49 +01:00
|
|
|
memset( hash, 0x2a, sizeof hash );
|
|
|
|
memset( sig, 0, sizeof sig );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_pk_sign( &pk, MBEDTLS_MD_SHA256,
|
|
|
|
hash, sizeof hash, sig, &sig_len,
|
|
|
|
NULL, NULL ) == 0 );
|
|
|
|
|
2019-02-01 12:48:19 +01:00
|
|
|
/* Export underlying public key for re-importing in a psa context. */
|
|
|
|
ret = mbedtls_pk_write_pubkey_der( &pk, pkey_psa,
|
|
|
|
sizeof( pkey_psa ) );
|
|
|
|
TEST_ASSERT( ret >= 0 );
|
|
|
|
klen_psa = (size_t) ret;
|
|
|
|
/* mbedtls_pk_write_pubkey_der() writes backwards in the data buffer. */
|
|
|
|
pkey_psa_start = pkey_psa + sizeof( pkey_psa ) - klen_psa;
|
2018-10-31 16:22:49 +01:00
|
|
|
|
2019-02-01 12:48:19 +01:00
|
|
|
TEST_ASSERT( klen_psa == klen_legacy );
|
|
|
|
TEST_ASSERT( memcmp( pkey_psa_start, pkey_legacy_start, klen_psa ) == 0 );
|
2018-10-31 16:22:49 +01:00
|
|
|
|
2019-02-01 12:48:19 +01:00
|
|
|
mbedtls_pk_free( &pk );
|
|
|
|
TEST_ASSERT( PSA_SUCCESS == psa_destroy_key( handle ) );
|
2018-10-31 16:22:49 +01:00
|
|
|
|
2019-02-01 12:48:19 +01:00
|
|
|
mbedtls_pk_init( &pk );
|
|
|
|
TEST_ASSERT( mbedtls_pk_parse_public_key( &pk, pkey_legacy_start,
|
|
|
|
klen_legacy ) == 0 );
|
2018-10-31 16:22:49 +01:00
|
|
|
TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256,
|
|
|
|
hash, sizeof hash, sig, sig_len ) == 0 );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_pk_free( &pk );
|
2019-08-01 12:47:40 +02:00
|
|
|
PSA_DONE( );
|
2018-10-31 16:22:49 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|