2018-01-27 23:32:46 +01:00
|
|
|
/* BEGIN_HEADER */
|
2018-06-12 16:06:52 +02:00
|
|
|
#include <stdint.h>
|
2018-07-03 12:16:15 +02:00
|
|
|
|
2018-08-11 01:22:42 +02:00
|
|
|
#include "mbedtls/asn1.h"
|
2018-06-28 00:16:11 +02:00
|
|
|
#include "mbedtls/asn1write.h"
|
2018-08-11 01:22:42 +02:00
|
|
|
#include "mbedtls/oid.h"
|
|
|
|
|
2019-08-07 12:08:04 +02:00
|
|
|
/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
|
|
|
|
* uses mbedtls_ctr_drbg internally. */
|
|
|
|
#include "mbedtls/ctr_drbg.h"
|
|
|
|
|
|
|
|
#include "psa/crypto.h"
|
2020-09-17 15:28:26 +02:00
|
|
|
#include "psa_crypto_slot_management.h"
|
2019-08-07 12:08:04 +02:00
|
|
|
|
2021-02-13 00:25:53 +01:00
|
|
|
#include "test/asn1_helpers.h"
|
2021-02-09 20:35:42 +01:00
|
|
|
#include "test/psa_crypto_helpers.h"
|
2021-02-13 00:41:11 +01:00
|
|
|
#include "test/psa_exercise_key.h"
|
2021-07-06 23:20:22 +02:00
|
|
|
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
|
|
|
#include "test/drivers/test_driver.h"
|
2021-07-04 22:48:48 +02:00
|
|
|
#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
|
|
|
|
#else
|
|
|
|
#define TEST_DRIVER_LOCATION 0x7fffff
|
2021-07-06 23:20:22 +02:00
|
|
|
#endif
|
2021-02-09 20:35:42 +01:00
|
|
|
|
2021-05-27 13:21:20 +02:00
|
|
|
/* If this comes up, it's a bug in the test code or in the test data. */
|
|
|
|
#define UNUSED 0xdeadbeef
|
|
|
|
|
2021-06-23 13:49:59 +02:00
|
|
|
/* Assert that an operation is (not) active.
|
|
|
|
* This serves as a proxy for checking if the operation is aborted. */
|
|
|
|
#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
|
|
|
|
#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
|
2021-02-09 20:35:42 +01:00
|
|
|
|
2018-06-27 18:20:43 +02:00
|
|
|
/** An invalid export length that will never be set by psa_export_key(). */
|
|
|
|
static const size_t INVALID_EXPORT_LENGTH = ~0U;
|
|
|
|
|
2018-08-14 15:17:54 +02:00
|
|
|
/** Test if a buffer contains a constant byte value.
|
|
|
|
*
|
|
|
|
* `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
|
2018-06-20 00:11:45 +02:00
|
|
|
*
|
|
|
|
* \param buffer Pointer to the beginning of the buffer.
|
2018-08-14 15:17:54 +02:00
|
|
|
* \param c Expected value of every byte.
|
2018-06-20 00:11:45 +02:00
|
|
|
* \param size Size of the buffer in bytes.
|
|
|
|
*
|
2018-06-21 09:21:51 +02:00
|
|
|
* \return 1 if the buffer is all-bits-zero.
|
|
|
|
* \return 0 if there is at least one nonzero byte.
|
2018-06-20 00:11:45 +02:00
|
|
|
*/
|
2018-08-14 15:17:54 +02:00
|
|
|
static int mem_is_char( void *buffer, unsigned char c, size_t size )
|
2018-06-20 00:11:45 +02:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for( i = 0; i < size; i++ )
|
|
|
|
{
|
2018-08-14 15:17:54 +02:00
|
|
|
if( ( (unsigned char *) buffer )[i] != c )
|
2018-06-21 09:21:51 +02:00
|
|
|
return( 0 );
|
2018-06-20 00:11:45 +02:00
|
|
|
}
|
2018-06-21 09:21:51 +02:00
|
|
|
return( 1 );
|
2018-06-20 00:11:45 +02:00
|
|
|
}
|
2022-01-17 15:29:38 +01:00
|
|
|
#if defined(MBEDTLS_ASN1_WRITE_C)
|
2018-06-28 00:16:11 +02:00
|
|
|
/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
|
|
|
|
static int asn1_write_10x( unsigned char **p,
|
|
|
|
unsigned char *start,
|
|
|
|
size_t bits,
|
|
|
|
unsigned char x )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int len = bits / 8 + 1;
|
2018-06-28 19:04:07 +02:00
|
|
|
if( bits == 0 )
|
|
|
|
return( MBEDTLS_ERR_ASN1_INVALID_DATA );
|
|
|
|
if( bits <= 8 && x >= 1 << ( bits - 1 ) )
|
2018-06-28 00:16:11 +02:00
|
|
|
return( MBEDTLS_ERR_ASN1_INVALID_DATA );
|
2018-07-17 16:36:59 +02:00
|
|
|
if( *p < start || *p - start < (ptrdiff_t) len )
|
2018-06-28 00:16:11 +02:00
|
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
|
|
|
*p -= len;
|
|
|
|
( *p )[len-1] = x;
|
|
|
|
if( bits % 8 == 0 )
|
|
|
|
( *p )[1] |= 1;
|
|
|
|
else
|
|
|
|
( *p )[0] |= 1 << ( bits % 8 );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
|
|
|
|
MBEDTLS_ASN1_INTEGER ) );
|
|
|
|
return( len );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int construct_fake_rsa_key( unsigned char *buffer,
|
|
|
|
size_t buffer_size,
|
|
|
|
unsigned char **p,
|
|
|
|
size_t bits,
|
|
|
|
int keypair )
|
|
|
|
{
|
|
|
|
size_t half_bits = ( bits + 1 ) / 2;
|
|
|
|
int ret;
|
|
|
|
int len = 0;
|
|
|
|
/* Construct something that looks like a DER encoding of
|
|
|
|
* as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
|
|
|
|
* RSAPrivateKey ::= SEQUENCE {
|
|
|
|
* version Version,
|
|
|
|
* modulus INTEGER, -- n
|
|
|
|
* publicExponent INTEGER, -- e
|
|
|
|
* privateExponent INTEGER, -- d
|
|
|
|
* prime1 INTEGER, -- p
|
|
|
|
* prime2 INTEGER, -- q
|
|
|
|
* exponent1 INTEGER, -- d mod (p-1)
|
|
|
|
* exponent2 INTEGER, -- d mod (q-1)
|
|
|
|
* coefficient INTEGER, -- (inverse of q) mod p
|
|
|
|
* otherPrimeInfos OtherPrimeInfos OPTIONAL
|
|
|
|
* }
|
|
|
|
* Or, for a public key, the same structure with only
|
|
|
|
* version, modulus and publicExponent.
|
|
|
|
*/
|
|
|
|
*p = buffer + buffer_size;
|
|
|
|
if( keypair )
|
|
|
|
{
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, /* pq */
|
|
|
|
asn1_write_10x( p, buffer, half_bits, 1 ) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, /* dq */
|
|
|
|
asn1_write_10x( p, buffer, half_bits, 1 ) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, /* dp */
|
|
|
|
asn1_write_10x( p, buffer, half_bits, 1 ) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, /* q */
|
|
|
|
asn1_write_10x( p, buffer, half_bits, 1 ) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
|
|
|
|
asn1_write_10x( p, buffer, half_bits, 3 ) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, /* d */
|
|
|
|
asn1_write_10x( p, buffer, bits, 1 ) );
|
|
|
|
}
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
|
|
|
|
asn1_write_10x( p, buffer, 17, 1 ) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, /* n */
|
|
|
|
asn1_write_10x( p, buffer, bits, 1 ) );
|
|
|
|
if( keypair )
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
|
|
|
|
mbedtls_asn1_write_int( p, buffer, 0 ) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
|
|
|
|
{
|
|
|
|
const unsigned char tag =
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
|
|
|
|
}
|
|
|
|
return( len );
|
|
|
|
}
|
2022-01-17 15:29:38 +01:00
|
|
|
#endif /* MBEDTLS_ASN1_WRITE_C */
|
2018-06-28 00:16:11 +02:00
|
|
|
|
2019-02-25 17:42:03 +01:00
|
|
|
int exercise_mac_setup( psa_key_type_t key_type,
|
|
|
|
const unsigned char *key_bytes,
|
|
|
|
size_t key_length,
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
psa_mac_operation_t *operation,
|
|
|
|
psa_status_t *status )
|
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2019-02-25 17:42:03 +01:00
|
|
|
|
2019-11-26 17:01:59 +01:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
|
2019-02-25 17:42:03 +01:00
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
*status = psa_mac_sign_setup( operation, key, alg );
|
2019-02-25 22:11:18 +01:00
|
|
|
/* Whether setup succeeded or failed, abort must succeed. */
|
|
|
|
PSA_ASSERT( psa_mac_abort( operation ) );
|
|
|
|
/* If setup failed, reproduce the failure, so that the caller can
|
|
|
|
* test the resulting state of the operation object. */
|
|
|
|
if( *status != PSA_SUCCESS )
|
2019-02-25 17:42:03 +01:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
|
2019-02-25 17:42:03 +01:00
|
|
|
}
|
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2019-02-25 17:42:03 +01:00
|
|
|
return( 1 );
|
|
|
|
|
|
|
|
exit:
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2019-02-25 17:42:03 +01:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int exercise_cipher_setup( psa_key_type_t key_type,
|
|
|
|
const unsigned char *key_bytes,
|
|
|
|
size_t key_length,
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
psa_cipher_operation_t *operation,
|
|
|
|
psa_status_t *status )
|
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2019-02-25 17:42:03 +01:00
|
|
|
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
|
2019-02-25 17:42:03 +01:00
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
*status = psa_cipher_encrypt_setup( operation, key, alg );
|
2019-02-25 22:11:18 +01:00
|
|
|
/* Whether setup succeeded or failed, abort must succeed. */
|
|
|
|
PSA_ASSERT( psa_cipher_abort( operation ) );
|
|
|
|
/* If setup failed, reproduce the failure, so that the caller can
|
|
|
|
* test the resulting state of the operation object. */
|
|
|
|
if( *status != PSA_SUCCESS )
|
2019-02-25 17:42:03 +01:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
|
2019-02-25 22:11:18 +01:00
|
|
|
*status );
|
2019-02-25 17:42:03 +01:00
|
|
|
}
|
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2019-02-25 17:42:03 +01:00
|
|
|
return( 1 );
|
|
|
|
|
|
|
|
exit:
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2019-02-25 17:42:03 +01:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
|
2019-04-18 22:28:52 +02:00
|
|
|
{
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2020-07-23 17:13:42 +02:00
|
|
|
mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
|
2019-04-18 22:28:52 +02:00
|
|
|
uint8_t buffer[1];
|
|
|
|
size_t length;
|
|
|
|
int ok = 0;
|
|
|
|
|
2020-07-23 17:13:42 +02:00
|
|
|
psa_set_key_id( &attributes, key_id );
|
2019-04-18 22:28:52 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
|
|
|
|
psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
|
2020-08-04 14:58:35 +02:00
|
|
|
TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
|
2021-03-17 17:11:05 +01:00
|
|
|
PSA_ERROR_INVALID_HANDLE );
|
2020-07-23 17:13:42 +02:00
|
|
|
TEST_EQUAL(
|
|
|
|
MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
|
|
|
|
TEST_EQUAL(
|
|
|
|
MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
|
2019-04-19 14:06:53 +02:00
|
|
|
TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
|
2019-04-18 22:28:52 +02:00
|
|
|
TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
|
|
|
|
TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
|
|
|
|
TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
|
|
|
|
TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
|
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
|
2021-03-17 17:11:05 +01:00
|
|
|
PSA_ERROR_INVALID_HANDLE );
|
2020-08-04 14:58:35 +02:00
|
|
|
TEST_EQUAL( psa_export_public_key( key,
|
2019-04-18 22:28:52 +02:00
|
|
|
buffer, sizeof( buffer ), &length ),
|
2021-03-17 17:11:05 +01:00
|
|
|
PSA_ERROR_INVALID_HANDLE );
|
2019-04-18 22:28:52 +02:00
|
|
|
|
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
exit:
|
2020-11-19 17:55:23 +01:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
2019-04-18 22:28:52 +02:00
|
|
|
psa_reset_key_attributes( &attributes );
|
2020-11-19 17:55:23 +01:00
|
|
|
|
2019-04-18 22:28:52 +02:00
|
|
|
return( ok );
|
|
|
|
}
|
|
|
|
|
2019-08-02 20:30:01 +02:00
|
|
|
/* Assert that a key isn't reported as having a slot number. */
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
|
|
|
#define ASSERT_NO_SLOT_NUMBER( attributes ) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
|
|
|
|
TEST_EQUAL( psa_get_key_slot_number( \
|
|
|
|
attributes, \
|
|
|
|
&ASSERT_NO_SLOT_NUMBER_slot_number ), \
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT ); \
|
|
|
|
} \
|
|
|
|
while( 0 )
|
|
|
|
#else /* MBEDTLS_PSA_CRYPTO_SE_C */
|
|
|
|
#define ASSERT_NO_SLOT_NUMBER( attributes ) \
|
|
|
|
( (void) 0 )
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
|
|
|
|
2018-12-03 15:36:32 +01:00
|
|
|
/* An overapproximation of the amount of storage needed for a key of the
|
|
|
|
* given type and with the given content. The API doesn't make it easy
|
|
|
|
* to find a good value for the size. The current implementation doesn't
|
|
|
|
* care about the value anyway. */
|
|
|
|
#define KEY_BITS_FROM_DATA( type, data ) \
|
|
|
|
( data )->len
|
|
|
|
|
2018-11-07 17:05:30 +01:00
|
|
|
typedef enum {
|
|
|
|
IMPORT_KEY = 0,
|
|
|
|
GENERATE_KEY = 1,
|
|
|
|
DERIVE_KEY = 2
|
|
|
|
} generate_method;
|
|
|
|
|
2021-09-15 17:40:40 +02:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
DO_NOT_SET_LENGTHS = 0,
|
|
|
|
SET_LENGTHS_BEFORE_NONCE = 1,
|
|
|
|
SET_LENGTHS_AFTER_NONCE = 2
|
2021-09-22 13:54:42 +02:00
|
|
|
} set_lengths_method_t;
|
2021-09-15 17:40:40 +02:00
|
|
|
|
2021-09-19 14:11:50 +02:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
USE_NULL_TAG = 0,
|
|
|
|
USE_GIVEN_TAG = 1,
|
2021-09-22 13:54:42 +02:00
|
|
|
} tag_usage_method_t;
|
2021-09-19 14:11:50 +02:00
|
|
|
|
2021-07-21 19:46:06 +02:00
|
|
|
/*!
|
|
|
|
* \brief Internal Function for AEAD multipart tests.
|
|
|
|
* \param key_type_arg Type of key passed in
|
|
|
|
* \param key_data The encryption / decryption key data
|
|
|
|
* \param alg_arg The type of algorithm used
|
|
|
|
* \param nonce Nonce data
|
|
|
|
* \param additional_data Additional data
|
2021-09-19 23:38:27 +02:00
|
|
|
* \param ad_part_len_arg If not -1, the length of chunks to
|
2021-07-21 19:46:06 +02:00
|
|
|
* feed additional data in to be encrypted /
|
|
|
|
* decrypted. If -1, no chunking.
|
|
|
|
* \param input_data Data to encrypt / decrypt
|
2021-09-19 23:38:27 +02:00
|
|
|
* \param data_part_len_arg If not -1, the length of chunks to feed
|
|
|
|
* the data in to be encrypted / decrypted. If
|
|
|
|
* -1, no chunking
|
2021-09-22 13:54:42 +02:00
|
|
|
* \param set_lengths_method A member of the set_lengths_method_t enum is
|
2021-09-19 23:38:27 +02:00
|
|
|
* expected here, this controls whether or not
|
|
|
|
* to set lengths, and in what order with
|
|
|
|
* respect to set nonce.
|
2021-07-21 19:46:06 +02:00
|
|
|
* \param expected_output Expected output
|
|
|
|
* \param is_encrypt If non-zero this is an encryption operation.
|
2021-07-22 22:52:01 +02:00
|
|
|
* \param do_zero_parts If non-zero, interleave zero length chunks
|
2021-09-19 23:38:27 +02:00
|
|
|
* with normal length chunks.
|
2021-07-21 19:46:06 +02:00
|
|
|
* \return int Zero on failure, non-zero on success.
|
|
|
|
*/
|
|
|
|
static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *nonce,
|
|
|
|
data_t *additional_data,
|
2021-09-15 15:15:55 +02:00
|
|
|
int ad_part_len_arg,
|
2021-07-21 19:46:06 +02:00
|
|
|
data_t *input_data,
|
2021-09-15 15:15:55 +02:00
|
|
|
int data_part_len_arg,
|
2021-09-22 13:54:42 +02:00
|
|
|
set_lengths_method_t set_lengths_method,
|
2021-07-21 19:46:06 +02:00
|
|
|
data_t *expected_output,
|
2021-07-22 18:10:45 +02:00
|
|
|
int is_encrypt,
|
2021-09-15 17:40:40 +02:00
|
|
|
int do_zero_parts )
|
2019-07-31 15:53:45 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
2019-07-31 15:53:45 +02:00
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-09-22 17:44:21 +02:00
|
|
|
psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
unsigned char *output_data = NULL;
|
|
|
|
unsigned char *part_data = NULL;
|
|
|
|
unsigned char *final_data = NULL;
|
2021-07-21 19:46:06 +02:00
|
|
|
size_t data_true_size = 0;
|
2021-06-16 17:52:21 +02:00
|
|
|
size_t part_data_size = 0;
|
2021-07-21 19:46:06 +02:00
|
|
|
size_t output_size = 0;
|
|
|
|
size_t final_output_size = 0;
|
2021-06-16 17:52:21 +02:00
|
|
|
size_t output_length = 0;
|
|
|
|
size_t key_bits = 0;
|
|
|
|
size_t tag_length = 0;
|
2021-09-15 15:15:55 +02:00
|
|
|
size_t part_offset = 0;
|
2021-06-16 17:52:21 +02:00
|
|
|
size_t part_length = 0;
|
|
|
|
size_t output_part_length = 0;
|
2021-07-21 19:46:06 +02:00
|
|
|
size_t tag_size = 0;
|
2021-09-15 15:15:55 +02:00
|
|
|
size_t ad_part_len = 0;
|
|
|
|
size_t data_part_len = 0;
|
2021-07-21 19:46:06 +02:00
|
|
|
uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
2019-07-31 15:53:45 +02:00
|
|
|
|
2021-07-21 19:46:06 +02:00
|
|
|
int test_ok = 0;
|
2021-09-15 15:15:55 +02:00
|
|
|
size_t part_count = 0;
|
2021-07-21 19:46:06 +02:00
|
|
|
|
2019-07-31 15:53:45 +02:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
2021-07-21 19:46:06 +02:00
|
|
|
if( is_encrypt )
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
|
|
|
|
else
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
|
|
|
|
|
2019-07-31 15:53:45 +02:00
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_type( &attributes, key_type );
|
2019-07-31 15:53:45 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
2019-07-31 15:53:45 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
|
|
|
key_bits = psa_get_key_bits( &attributes );
|
2019-07-31 15:53:45 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
|
2019-07-31 15:53:45 +02:00
|
|
|
|
2021-07-21 19:46:06 +02:00
|
|
|
if( is_encrypt )
|
|
|
|
{
|
|
|
|
/* Tag gets written at end of buffer. */
|
|
|
|
output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
|
|
|
|
( input_data->len +
|
|
|
|
tag_length ) );
|
|
|
|
data_true_size = input_data->len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
|
|
|
|
( input_data->len -
|
|
|
|
tag_length ) );
|
2020-11-19 17:55:23 +01:00
|
|
|
|
2021-07-21 19:46:06 +02:00
|
|
|
/* Do not want to attempt to decrypt tag. */
|
|
|
|
data_true_size = input_data->len - tag_length;
|
|
|
|
}
|
2019-07-31 15:53:45 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
ASSERT_ALLOC( output_data, output_size );
|
2018-01-28 13:16:24 +01:00
|
|
|
|
2021-07-21 19:46:06 +02:00
|
|
|
if( is_encrypt )
|
|
|
|
{
|
2021-09-13 20:13:22 +02:00
|
|
|
final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
|
2021-07-21 19:46:06 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-13 20:13:22 +02:00
|
|
|
final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
|
2021-07-21 19:46:06 +02:00
|
|
|
}
|
2019-07-31 15:53:45 +02:00
|
|
|
|
2021-07-21 19:46:06 +02:00
|
|
|
ASSERT_ALLOC( final_data, final_output_size );
|
2019-04-18 21:44:46 +02:00
|
|
|
|
2021-07-21 19:46:06 +02:00
|
|
|
if( is_encrypt )
|
|
|
|
status = psa_aead_encrypt_setup( &operation, key, alg );
|
|
|
|
else
|
|
|
|
status = psa_aead_decrypt_setup( &operation, key, alg );
|
2018-01-28 13:16:24 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* If the operation is not supported, just skip and not fail in case the
|
|
|
|
* encryption involves a common limitation of cryptography hardwares and
|
|
|
|
* an alternative implementation. */
|
|
|
|
if( status == PSA_ERROR_NOT_SUPPORTED )
|
|
|
|
{
|
|
|
|
MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
|
|
|
|
MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
|
|
|
|
}
|
2020-11-19 17:55:23 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( status );
|
2018-01-28 13:16:24 +01:00
|
|
|
|
2021-09-15 17:40:40 +02:00
|
|
|
if( set_lengths_method == DO_NOT_SET_LENGTHS )
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
|
2021-07-22 18:54:42 +02:00
|
|
|
{
|
2021-09-15 17:40:40 +02:00
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
data_true_size ) );
|
2021-07-22 18:54:42 +02:00
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
}
|
2021-09-15 17:40:40 +02:00
|
|
|
else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
|
2021-06-16 17:52:21 +02:00
|
|
|
{
|
2021-07-22 18:54:42 +02:00
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
2021-09-15 17:40:40 +02:00
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
data_true_size ) );
|
2021-06-16 17:52:21 +02:00
|
|
|
}
|
2019-07-30 17:26:54 +02:00
|
|
|
|
2021-09-15 15:15:55 +02:00
|
|
|
if( ad_part_len_arg != -1 )
|
2021-06-16 17:52:21 +02:00
|
|
|
{
|
|
|
|
/* Pass additional data in parts */
|
2021-09-15 15:15:55 +02:00
|
|
|
ad_part_len = (size_t) ad_part_len_arg;
|
2019-07-30 17:26:54 +02:00
|
|
|
|
2021-09-15 17:50:01 +02:00
|
|
|
for( part_offset = 0, part_count = 0;
|
|
|
|
part_offset < additional_data->len;
|
|
|
|
part_offset += part_length, part_count++ )
|
2021-06-16 17:52:21 +02:00
|
|
|
{
|
2021-09-15 17:50:01 +02:00
|
|
|
if( do_zero_parts && ( part_count & 0x01 ) )
|
2021-06-16 17:52:21 +02:00
|
|
|
{
|
2021-07-22 18:10:45 +02:00
|
|
|
part_length = 0;
|
2021-06-16 17:52:21 +02:00
|
|
|
}
|
2021-09-15 17:52:11 +02:00
|
|
|
else if( additional_data->len - part_offset < ad_part_len )
|
|
|
|
{
|
|
|
|
part_length = additional_data->len - part_offset;
|
|
|
|
}
|
2021-06-16 17:52:21 +02:00
|
|
|
else
|
|
|
|
{
|
2021-09-15 17:52:11 +02:00
|
|
|
part_length = ad_part_len;
|
2021-06-16 17:52:21 +02:00
|
|
|
}
|
2019-07-30 17:26:54 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_aead_update_ad( &operation,
|
|
|
|
additional_data->x + part_offset,
|
|
|
|
part_length ) );
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2019-07-30 17:26:54 +02:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Pass additional data in one go. */
|
|
|
|
PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
|
|
|
|
additional_data->len ) );
|
2019-07-30 17:26:54 +02:00
|
|
|
}
|
|
|
|
|
2021-09-15 15:15:55 +02:00
|
|
|
if( data_part_len_arg != -1 )
|
2021-06-16 17:52:21 +02:00
|
|
|
{
|
|
|
|
/* Pass data in parts */
|
2021-09-15 15:15:55 +02:00
|
|
|
data_part_len = ( size_t ) data_part_len_arg;
|
2021-06-16 17:52:21 +02:00
|
|
|
part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
|
2021-07-21 19:46:06 +02:00
|
|
|
( size_t ) data_part_len );
|
2020-11-19 17:55:23 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
ASSERT_ALLOC( part_data, part_data_size );
|
2019-07-30 17:26:54 +02:00
|
|
|
|
2021-09-15 17:50:01 +02:00
|
|
|
for( part_offset = 0, part_count = 0;
|
|
|
|
part_offset < data_true_size;
|
|
|
|
part_offset += part_length, part_count++ )
|
2021-06-16 17:52:21 +02:00
|
|
|
{
|
2021-09-15 17:50:01 +02:00
|
|
|
if( do_zero_parts && ( part_count & 0x01 ) )
|
2021-06-16 17:52:21 +02:00
|
|
|
{
|
2021-07-22 18:10:45 +02:00
|
|
|
part_length = 0;
|
2021-06-16 17:52:21 +02:00
|
|
|
}
|
2021-09-15 17:52:11 +02:00
|
|
|
else if( ( data_true_size - part_offset ) < data_part_len )
|
|
|
|
{
|
|
|
|
part_length = ( data_true_size - part_offset );
|
|
|
|
}
|
2021-06-16 17:52:21 +02:00
|
|
|
else
|
|
|
|
{
|
2021-09-15 17:52:11 +02:00
|
|
|
part_length = data_part_len;
|
2021-06-16 17:52:21 +02:00
|
|
|
}
|
2018-06-28 00:16:11 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_aead_update( &operation,
|
|
|
|
( input_data->x + part_offset ),
|
|
|
|
part_length, part_data,
|
|
|
|
part_data_size,
|
|
|
|
&output_part_length ) );
|
2018-06-28 00:16:11 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
if( output_data && output_part_length )
|
|
|
|
{
|
2022-01-31 13:51:56 +01:00
|
|
|
memcpy( ( output_data + output_length ), part_data,
|
2021-06-16 17:52:21 +02:00
|
|
|
output_part_length );
|
|
|
|
}
|
2018-01-28 13:16:24 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
output_length += output_part_length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-21 19:46:06 +02:00
|
|
|
/* Pass all data in one go. */
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_aead_update( &operation, input_data->x,
|
2021-07-21 19:46:06 +02:00
|
|
|
data_true_size, output_data,
|
2021-06-16 17:52:21 +02:00
|
|
|
output_size, &output_length ) );
|
|
|
|
}
|
2018-03-28 12:46:26 +02:00
|
|
|
|
2021-07-21 19:46:06 +02:00
|
|
|
if( is_encrypt )
|
|
|
|
PSA_ASSERT( psa_aead_finish( &operation, final_data,
|
|
|
|
final_output_size,
|
|
|
|
&output_part_length,
|
|
|
|
tag_buffer, tag_length,
|
|
|
|
&tag_size ) );
|
|
|
|
else
|
2021-06-16 17:52:21 +02:00
|
|
|
{
|
2021-09-17 20:19:02 +02:00
|
|
|
PSA_ASSERT( psa_aead_verify( &operation, final_data,
|
2021-07-21 19:46:06 +02:00
|
|
|
final_output_size,
|
|
|
|
&output_part_length,
|
|
|
|
( input_data->x + data_true_size ),
|
2021-09-17 20:19:02 +02:00
|
|
|
tag_length ) );
|
2021-06-16 17:52:21 +02:00
|
|
|
}
|
|
|
|
|
2021-07-21 19:46:06 +02:00
|
|
|
if( output_data && output_part_length )
|
|
|
|
memcpy( ( output_data + output_length ), final_data,
|
|
|
|
output_part_length );
|
2021-06-16 17:52:21 +02:00
|
|
|
|
2021-07-21 19:46:06 +02:00
|
|
|
output_length += output_part_length;
|
2021-06-16 17:52:21 +02:00
|
|
|
|
|
|
|
|
2021-07-21 19:46:06 +02:00
|
|
|
/* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
|
|
|
|
* should be exact.*/
|
|
|
|
if( is_encrypt )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( tag_length, tag_size );
|
2021-06-16 17:52:21 +02:00
|
|
|
|
2021-07-21 19:46:06 +02:00
|
|
|
if( output_data && tag_length )
|
|
|
|
memcpy( ( output_data + output_length ), tag_buffer,
|
|
|
|
tag_length );
|
2021-06-16 17:52:21 +02:00
|
|
|
|
2021-07-21 19:46:06 +02:00
|
|
|
output_length += tag_length;
|
2021-06-16 17:52:21 +02:00
|
|
|
|
2021-07-21 19:46:06 +02:00
|
|
|
TEST_EQUAL( output_length,
|
2022-04-14 00:12:57 +02:00
|
|
|
PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
|
|
|
|
input_data->len ) );
|
|
|
|
TEST_LE_U( output_length,
|
|
|
|
PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
|
2021-06-16 17:52:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-21 19:46:06 +02:00
|
|
|
TEST_EQUAL( output_length,
|
|
|
|
PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
|
|
|
|
input_data->len ) );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( output_length,
|
|
|
|
PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
|
2021-06-16 17:52:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-21 19:46:06 +02:00
|
|
|
ASSERT_COMPARE( expected_output->x, expected_output->len,
|
|
|
|
output_data, output_length );
|
2021-06-16 17:52:21 +02:00
|
|
|
|
|
|
|
|
2021-07-21 19:46:06 +02:00
|
|
|
test_ok = 1;
|
2021-06-16 17:52:21 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( key );
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
mbedtls_free( output_data );
|
|
|
|
mbedtls_free( part_data );
|
|
|
|
mbedtls_free( final_data );
|
|
|
|
PSA_DONE( );
|
2021-07-21 19:46:06 +02:00
|
|
|
|
|
|
|
return( test_ok );
|
2021-06-16 17:52:21 +02:00
|
|
|
}
|
|
|
|
|
2022-02-28 16:23:59 +01:00
|
|
|
/*!
|
|
|
|
* \brief Internal Function for MAC multipart tests.
|
|
|
|
* \param key_type_arg Type of key passed in
|
|
|
|
* \param key_data The encryption / decryption key data
|
|
|
|
* \param alg_arg The type of algorithm used
|
|
|
|
* \param input_data Data to encrypt / decrypt
|
|
|
|
* \param data_part_len_arg If not -1, the length of chunks to feed
|
|
|
|
* the data in to be encrypted / decrypted. If
|
|
|
|
* -1, no chunking
|
|
|
|
* \param expected_output Expected output
|
|
|
|
* \param is_verify If non-zero this is an verify operation.
|
|
|
|
* \param do_zero_parts If non-zero, interleave zero length chunks
|
|
|
|
* with normal length chunks.
|
|
|
|
* \return int Zero on failure, non-zero on success.
|
|
|
|
*/
|
|
|
|
static int mac_multipart_internal_func( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *input_data,
|
|
|
|
int data_part_len_arg,
|
|
|
|
data_t *expected_output,
|
|
|
|
int is_verify,
|
|
|
|
int do_zero_parts )
|
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
|
|
|
unsigned char mac[PSA_MAC_MAX_SIZE];
|
|
|
|
size_t part_offset = 0;
|
|
|
|
size_t part_length = 0;
|
|
|
|
size_t data_part_len = 0;
|
|
|
|
size_t mac_len = 0;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
|
|
|
|
|
|
|
int test_ok = 0;
|
|
|
|
size_t part_count = 0;
|
|
|
|
|
2022-03-07 10:11:11 +01:00
|
|
|
PSA_INIT( );
|
2022-02-28 16:23:59 +01:00
|
|
|
|
|
|
|
if( is_verify )
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
|
|
|
|
else
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
|
|
|
|
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
|
|
|
|
if( is_verify )
|
|
|
|
status = psa_mac_verify_setup( &operation, key, alg );
|
|
|
|
else
|
|
|
|
status = psa_mac_sign_setup( &operation, key, alg );
|
|
|
|
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
|
|
|
|
if( data_part_len_arg != -1 )
|
|
|
|
{
|
|
|
|
/* Pass data in parts */
|
|
|
|
data_part_len = ( size_t ) data_part_len_arg;
|
|
|
|
|
|
|
|
for( part_offset = 0, part_count = 0;
|
|
|
|
part_offset < input_data->len;
|
|
|
|
part_offset += part_length, part_count++ )
|
|
|
|
{
|
|
|
|
if( do_zero_parts && ( part_count & 0x01 ) )
|
|
|
|
{
|
|
|
|
part_length = 0;
|
|
|
|
}
|
|
|
|
else if( ( input_data->len - part_offset ) < data_part_len )
|
|
|
|
{
|
|
|
|
part_length = ( input_data->len - part_offset );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
part_length = data_part_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_mac_update( &operation,
|
|
|
|
( input_data->x + part_offset ),
|
|
|
|
part_length ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Pass all data in one go. */
|
|
|
|
PSA_ASSERT( psa_mac_update( &operation, input_data->x,
|
|
|
|
input_data->len ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( is_verify )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_mac_verify_finish( &operation, expected_output->x,
|
|
|
|
expected_output->len ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_mac_sign_finish( &operation, mac,
|
|
|
|
PSA_MAC_MAX_SIZE, &mac_len ) );
|
|
|
|
|
|
|
|
ASSERT_COMPARE( expected_output->x, expected_output->len,
|
|
|
|
mac, mac_len );
|
|
|
|
}
|
|
|
|
|
|
|
|
test_ok = 1;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( key );
|
|
|
|
psa_mac_abort( &operation );
|
|
|
|
PSA_DONE( );
|
|
|
|
|
|
|
|
return( test_ok );
|
|
|
|
}
|
|
|
|
|
2022-06-15 17:39:01 +02:00
|
|
|
#if defined(PSA_WANT_ALG_JPAKE)
|
2022-06-15 15:27:48 +02:00
|
|
|
static int ecjpake_do_round( psa_algorithm_t alg, unsigned int primitive,
|
|
|
|
psa_pake_operation_t *server,
|
|
|
|
psa_pake_operation_t *client,
|
|
|
|
int client_input_first,
|
|
|
|
int round, int inject_error )
|
|
|
|
{
|
|
|
|
unsigned char *buffer0 = NULL, *buffer1 = NULL;
|
|
|
|
size_t buffer_length = (
|
|
|
|
PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
|
|
|
|
PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
|
|
|
|
PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
|
|
|
|
size_t buffer0_off = 0;
|
|
|
|
size_t buffer1_off = 0;
|
|
|
|
size_t s_g1_len, s_g2_len, s_a_len;
|
|
|
|
size_t s_g1_off, s_g2_off, s_a_off;
|
|
|
|
size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
|
|
|
|
size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
|
|
|
|
size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
|
|
|
|
size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
|
|
|
|
size_t c_g1_len, c_g2_len, c_a_len;
|
|
|
|
size_t c_g1_off, c_g2_off, c_a_off;
|
|
|
|
size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
|
|
|
|
size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
|
|
|
|
size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
|
|
|
|
size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
|
|
|
|
psa_status_t expected_status = PSA_SUCCESS;
|
2022-06-20 14:56:50 +02:00
|
|
|
psa_status_t status;
|
|
|
|
int ret = 0;
|
2022-06-15 15:27:48 +02:00
|
|
|
|
|
|
|
ASSERT_ALLOC( buffer0, buffer_length );
|
|
|
|
ASSERT_ALLOC( buffer1, buffer_length );
|
|
|
|
|
|
|
|
switch( round )
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
/* Server first round Output */
|
|
|
|
PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer0 + buffer0_off,
|
|
|
|
512 - buffer0_off, &s_g1_len ) );
|
|
|
|
s_g1_off = buffer0_off;
|
|
|
|
buffer0_off += s_g1_len;
|
|
|
|
PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer0 + buffer0_off,
|
|
|
|
512 - buffer0_off, &s_x1_pk_len ) );
|
|
|
|
s_x1_pk_off = buffer0_off;
|
|
|
|
buffer0_off += s_x1_pk_len;
|
|
|
|
PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer0 + buffer0_off,
|
|
|
|
512 - buffer0_off, &s_x1_pr_len ) );
|
|
|
|
s_x1_pr_off = buffer0_off;
|
|
|
|
buffer0_off += s_x1_pr_len;
|
|
|
|
PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer0 + buffer0_off,
|
|
|
|
512 - buffer0_off, &s_g2_len ) );
|
|
|
|
s_g2_off = buffer0_off;
|
|
|
|
buffer0_off += s_g2_len;
|
|
|
|
PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer0 + buffer0_off,
|
|
|
|
512 - buffer0_off, &s_x2_pk_len ) );
|
|
|
|
s_x2_pk_off = buffer0_off;
|
|
|
|
buffer0_off += s_x2_pk_len;
|
|
|
|
PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer0 + buffer0_off,
|
|
|
|
512 - buffer0_off, &s_x2_pr_len ) );
|
|
|
|
s_x2_pr_off = buffer0_off;
|
|
|
|
buffer0_off += s_x2_pr_len;
|
|
|
|
|
|
|
|
if( inject_error == 1 )
|
|
|
|
{
|
2022-06-21 13:37:06 +02:00
|
|
|
buffer0[s_x1_pk_off + 8] >>= 4;
|
2022-06-15 15:27:48 +02:00
|
|
|
buffer0[s_x2_pk_off + 7] <<= 4;
|
|
|
|
expected_status = PSA_ERROR_DATA_INVALID;
|
|
|
|
}
|
|
|
|
|
2022-09-05 17:59:54 +02:00
|
|
|
/*
|
|
|
|
* When injecting errors in inputs, the implementation is
|
|
|
|
* free to detect it right away of with a delay.
|
|
|
|
* This permits delaying the error until the end of the input
|
|
|
|
* sequence, if no error appears then, this will be treated
|
|
|
|
* as an error.
|
|
|
|
*/
|
|
|
|
|
2022-06-15 15:27:48 +02:00
|
|
|
if( client_input_first == 1 )
|
|
|
|
{
|
|
|
|
/* Client first round Input */
|
2022-06-20 14:56:50 +02:00
|
|
|
status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer0 + s_g1_off, s_g1_len );
|
|
|
|
if( inject_error == 1 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
2022-06-15 15:27:48 +02:00
|
|
|
|
2022-06-20 14:56:50 +02:00
|
|
|
status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer0 + s_x1_pk_off,
|
|
|
|
s_x1_pk_len );
|
|
|
|
if( inject_error == 1 && status != PSA_SUCCESS )
|
2022-06-15 15:27:48 +02:00
|
|
|
{
|
2022-06-20 14:56:50 +02:00
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
2022-06-15 15:27:48 +02:00
|
|
|
}
|
2022-06-20 14:56:50 +02:00
|
|
|
|
|
|
|
status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer0 + s_x1_pr_off,
|
|
|
|
s_x1_pr_len );
|
|
|
|
if( inject_error == 1 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer0 + s_g2_off,
|
|
|
|
s_g2_len );
|
|
|
|
if( inject_error == 1 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer0 + s_x2_pk_off,
|
|
|
|
s_x2_pk_len );
|
|
|
|
if( inject_error == 1 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer0 + s_x2_pr_off,
|
|
|
|
s_x2_pr_len );
|
|
|
|
if( inject_error == 1 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Error didn't trigger, exit with error */
|
|
|
|
if( inject_error == 1 )
|
|
|
|
goto exit;
|
2022-06-15 15:27:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Client first round Output */
|
|
|
|
PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer1 + buffer1_off,
|
|
|
|
512 - buffer1_off, &c_g1_len ) );
|
|
|
|
c_g1_off = buffer1_off;
|
|
|
|
buffer1_off += c_g1_len;
|
|
|
|
PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer1 + buffer1_off,
|
|
|
|
512 - buffer1_off, &c_x1_pk_len ) );
|
|
|
|
c_x1_pk_off = buffer1_off;
|
|
|
|
buffer1_off += c_x1_pk_len;
|
|
|
|
PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer1 + buffer1_off,
|
|
|
|
512 - buffer1_off, &c_x1_pr_len ) );
|
|
|
|
c_x1_pr_off = buffer1_off;
|
|
|
|
buffer1_off += c_x1_pr_len;
|
|
|
|
PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer1 + buffer1_off,
|
|
|
|
512 - buffer1_off, &c_g2_len ) );
|
|
|
|
c_g2_off = buffer1_off;
|
|
|
|
buffer1_off += c_g2_len;
|
|
|
|
PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer1 + buffer1_off,
|
|
|
|
512 - buffer1_off, &c_x2_pk_len ) );
|
|
|
|
c_x2_pk_off = buffer1_off;
|
|
|
|
buffer1_off += c_x2_pk_len;
|
|
|
|
PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer1 + buffer1_off,
|
|
|
|
512 - buffer1_off, &c_x2_pr_len ) );
|
|
|
|
c_x2_pr_off = buffer1_off;
|
|
|
|
buffer1_off += c_x2_pr_len;
|
|
|
|
|
|
|
|
if( client_input_first == 0 )
|
|
|
|
{
|
|
|
|
/* Client first round Input */
|
2022-06-20 14:56:50 +02:00
|
|
|
status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer0 + s_g1_off, s_g1_len );
|
|
|
|
if( inject_error == 1 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
2022-06-15 15:27:48 +02:00
|
|
|
|
2022-06-20 14:56:50 +02:00
|
|
|
status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer0 + s_x1_pk_off,
|
|
|
|
s_x1_pk_len );
|
|
|
|
if( inject_error == 1 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer0 + s_x1_pr_off,
|
|
|
|
s_x1_pr_len );
|
|
|
|
if( inject_error == 1 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer0 + s_g2_off,
|
|
|
|
s_g2_len );
|
|
|
|
if( inject_error == 1 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer0 + s_x2_pk_off,
|
|
|
|
s_x2_pk_len );
|
|
|
|
if( inject_error == 1 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer0 + s_x2_pr_off,
|
|
|
|
s_x2_pr_len );
|
|
|
|
if( inject_error == 1 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
2022-06-15 15:27:48 +02:00
|
|
|
break;
|
2022-06-20 14:56:50 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Error didn't trigger, exit with error */
|
|
|
|
if( inject_error == 1 )
|
|
|
|
goto exit;
|
2022-06-15 15:27:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( inject_error == 2 )
|
|
|
|
{
|
|
|
|
buffer1[c_x1_pk_off + 12] >>= 4;
|
|
|
|
buffer1[c_x2_pk_off + 7] <<= 4;
|
|
|
|
expected_status = PSA_ERROR_DATA_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Server first round Input */
|
2022-06-20 14:56:50 +02:00
|
|
|
status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer1 + c_g1_off, c_g1_len );
|
|
|
|
if( inject_error == 2 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer1 + c_x1_pk_off, c_x1_pk_len );
|
|
|
|
if( inject_error == 2 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer1 + c_x1_pr_off, c_x1_pr_len );
|
|
|
|
if( inject_error == 2 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer1 + c_g2_off, c_g2_len );
|
|
|
|
if( inject_error == 2 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer1 + c_x2_pk_off, c_x2_pk_len );
|
|
|
|
if( inject_error == 2 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer1 + c_x2_pr_off, c_x2_pr_len );
|
|
|
|
if( inject_error == 2 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Error didn't trigger, exit with error */
|
|
|
|
if( inject_error == 2 )
|
|
|
|
goto exit;
|
2022-06-15 15:27:48 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
/* Server second round Output */
|
|
|
|
buffer0_off = 0;
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer0 + buffer0_off,
|
|
|
|
512 - buffer0_off, &s_a_len ) );
|
|
|
|
s_a_off = buffer0_off;
|
|
|
|
buffer0_off += s_a_len;
|
|
|
|
PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer0 + buffer0_off,
|
|
|
|
512 - buffer0_off, &s_x2s_pk_len ) );
|
|
|
|
s_x2s_pk_off = buffer0_off;
|
|
|
|
buffer0_off += s_x2s_pk_len;
|
|
|
|
PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer0 + buffer0_off,
|
|
|
|
512 - buffer0_off, &s_x2s_pr_len ) );
|
|
|
|
s_x2s_pr_off = buffer0_off;
|
|
|
|
buffer0_off += s_x2s_pr_len;
|
|
|
|
|
|
|
|
if( inject_error == 3 )
|
|
|
|
{
|
2022-06-21 13:37:06 +02:00
|
|
|
buffer0[s_x2s_pk_off + 12] += 0x33;
|
2022-06-15 15:27:48 +02:00
|
|
|
expected_status = PSA_ERROR_DATA_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( client_input_first == 1 )
|
|
|
|
{
|
|
|
|
/* Client second round Input */
|
2022-06-20 14:56:50 +02:00
|
|
|
status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer0 + s_a_off, s_a_len );
|
|
|
|
if( inject_error == 3 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
2022-06-15 15:27:48 +02:00
|
|
|
|
2022-06-20 14:56:50 +02:00
|
|
|
status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer0 + s_x2s_pk_off,
|
|
|
|
s_x2s_pk_len );
|
|
|
|
if( inject_error == 3 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer0 + s_x2s_pr_off,
|
|
|
|
s_x2s_pr_len );
|
|
|
|
if( inject_error == 3 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
2022-06-15 15:27:48 +02:00
|
|
|
break;
|
2022-06-20 14:56:50 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Error didn't trigger, exit with error */
|
|
|
|
if( inject_error == 3 )
|
|
|
|
goto exit;
|
2022-06-15 15:27:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Client second round Output */
|
|
|
|
buffer1_off = 0;
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer1 + buffer1_off,
|
|
|
|
512 - buffer1_off, &c_a_len ) );
|
|
|
|
c_a_off = buffer1_off;
|
|
|
|
buffer1_off += c_a_len;
|
|
|
|
PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer1 + buffer1_off,
|
|
|
|
512 - buffer1_off, &c_x2s_pk_len ) );
|
|
|
|
c_x2s_pk_off = buffer1_off;
|
|
|
|
buffer1_off += c_x2s_pk_len;
|
|
|
|
PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer1 + buffer1_off,
|
|
|
|
512 - buffer1_off, &c_x2s_pr_len ) );
|
|
|
|
c_x2s_pr_off = buffer1_off;
|
|
|
|
buffer1_off += c_x2s_pr_len;
|
|
|
|
|
|
|
|
if( client_input_first == 0 )
|
|
|
|
{
|
|
|
|
/* Client second round Input */
|
2022-06-20 14:56:50 +02:00
|
|
|
status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer0 + s_a_off, s_a_len );
|
|
|
|
if( inject_error == 3 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
2022-06-15 15:27:48 +02:00
|
|
|
|
2022-06-20 14:56:50 +02:00
|
|
|
status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer0 + s_x2s_pk_off,
|
|
|
|
s_x2s_pk_len );
|
|
|
|
if( inject_error == 3 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
2022-06-15 15:27:48 +02:00
|
|
|
break;
|
2022-06-20 14:56:50 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer0 + s_x2s_pr_off,
|
|
|
|
s_x2s_pr_len );
|
|
|
|
if( inject_error == 3 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Error didn't trigger, exit with error */
|
|
|
|
if( inject_error == 3 )
|
|
|
|
goto exit;
|
2022-06-15 15:27:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( inject_error == 4 )
|
|
|
|
{
|
2022-06-21 13:37:06 +02:00
|
|
|
buffer1[c_x2s_pk_off + 7] += 0x28;
|
2022-06-15 15:27:48 +02:00
|
|
|
expected_status = PSA_ERROR_DATA_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Server second round Input */
|
2022-06-20 14:56:50 +02:00
|
|
|
status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
buffer1 + c_a_off, c_a_len );
|
|
|
|
if( inject_error == 4 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
buffer1 + c_x2s_pk_off, c_x2s_pk_len );
|
|
|
|
if( inject_error == 4 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
buffer1 + c_x2s_pr_off, c_x2s_pr_len );
|
|
|
|
if( inject_error == 4 && status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Error didn't trigger, exit with error */
|
|
|
|
if( inject_error == 4 )
|
|
|
|
goto exit;
|
2022-06-15 15:27:48 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free( buffer0 );
|
|
|
|
mbedtls_free( buffer1 );
|
|
|
|
return( ret );
|
|
|
|
}
|
2022-06-15 17:39:01 +02:00
|
|
|
#endif /* PSA_WANT_ALG_JPAKE */
|
2022-06-15 15:27:48 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:MBEDTLS_PSA_CRYPTO_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void static_checks( )
|
|
|
|
{
|
|
|
|
size_t max_truncated_mac_size =
|
|
|
|
PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
|
|
|
|
|
|
|
|
/* Check that the length for a truncated MAC always fits in the algorithm
|
|
|
|
* encoding. The shifted mask is the maximum truncated value. The
|
|
|
|
* untruncated algorithm may be one byte larger. */
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size );
|
2021-06-16 17:52:21 +02:00
|
|
|
}
|
2018-06-26 16:12:43 +02:00
|
|
|
/* END_CASE */
|
|
|
|
|
2018-06-18 22:20:03 +02:00
|
|
|
/* BEGIN_CASE */
|
2021-06-16 17:52:21 +02:00
|
|
|
void import_with_policy( int type_arg,
|
|
|
|
int usage_arg, int alg_arg,
|
|
|
|
int expected_status_arg )
|
2018-06-18 22:20:03 +02:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_key_type_t type = type_arg;
|
2018-06-18 22:20:03 +02:00
|
|
|
psa_key_usage_t usage = usage_arg;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
const uint8_t key_material[16] = {0};
|
|
|
|
psa_status_t status;
|
2018-06-18 22:20:03 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-06-18 22:20:03 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_type( &attributes, type );
|
2019-04-18 21:44:46 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, usage );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
2018-06-18 22:20:03 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
status = psa_import_key( &attributes,
|
|
|
|
key_material, sizeof( key_material ),
|
|
|
|
&key );
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
2018-06-18 22:20:03 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
|
|
|
|
TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
|
2021-05-13 12:05:01 +02:00
|
|
|
TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
|
2021-06-28 14:05:00 +02:00
|
|
|
mbedtls_test_update_key_usage_flags( usage ) );
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
|
|
|
|
ASSERT_NO_SLOT_NUMBER( &got_attributes );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_destroy_key( key ) );
|
|
|
|
test_operations_on_invalid_key( key );
|
2018-06-18 22:20:03 +02:00
|
|
|
|
|
|
|
exit:
|
2020-11-19 17:55:23 +01:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_reset_key_attributes( &got_attributes );
|
2020-11-19 17:55:23 +01:00
|
|
|
|
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-06-18 22:20:03 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-11-26 18:07:46 +01:00
|
|
|
/* BEGIN_CASE */
|
2021-06-16 17:52:21 +02:00
|
|
|
void import_with_data( data_t *data, int type_arg,
|
|
|
|
int attr_bits_arg,
|
|
|
|
int expected_status_arg )
|
2018-06-18 22:20:03 +02:00
|
|
|
{
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t type = type_arg;
|
|
|
|
size_t attr_bits = attr_bits_arg;
|
2021-02-18 12:03:50 +01:00
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_status_t status;
|
2018-06-18 22:20:03 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-06-18 22:20:03 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_type( &attributes, type );
|
|
|
|
psa_set_key_bits( &attributes, attr_bits );
|
2018-06-18 22:20:03 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
status = psa_import_key( &attributes, data->x, data->len, &key );
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
2021-02-18 12:03:50 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
|
|
|
|
TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
|
|
|
|
if( attr_bits != 0 )
|
|
|
|
TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
|
|
|
|
ASSERT_NO_SLOT_NUMBER( &got_attributes );
|
2018-07-06 16:53:09 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_destroy_key( key ) );
|
|
|
|
test_operations_on_invalid_key( key );
|
2018-07-06 16:53:09 +02:00
|
|
|
|
|
|
|
exit:
|
2021-06-16 17:52:21 +02:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
|
|
|
psa_reset_key_attributes( &got_attributes );
|
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-07-06 16:53:09 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void import_large_key( int type_arg, int byte_size_arg,
|
|
|
|
int expected_status_arg )
|
2018-07-06 16:53:09 +02:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_key_type_t type = type_arg;
|
|
|
|
size_t byte_size = byte_size_arg;
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-07-06 16:53:09 +02:00
|
|
|
psa_status_t status;
|
2021-06-16 17:52:21 +02:00
|
|
|
uint8_t *buffer = NULL;
|
|
|
|
size_t buffer_size = byte_size + 1;
|
|
|
|
size_t n;
|
2018-07-06 16:53:09 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Skip the test case if the target running the test cannot
|
2021-12-21 06:14:10 +01:00
|
|
|
* accommodate large keys due to heap size constraints */
|
2021-06-16 17:52:21 +02:00
|
|
|
ASSERT_ALLOC_WEAK( buffer, buffer_size );
|
|
|
|
memset( buffer, 'K', byte_size );
|
2018-07-06 16:53:09 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-07-06 16:53:09 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Try importing the key */
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
|
|
|
|
psa_set_key_type( &attributes, type );
|
|
|
|
status = psa_import_key( &attributes, buffer, byte_size, &key );
|
|
|
|
TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
|
|
|
|
TEST_EQUAL( status, expected_status );
|
2018-07-06 16:53:09 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
|
|
|
TEST_EQUAL( psa_get_key_type( &attributes ), type );
|
|
|
|
TEST_EQUAL( psa_get_key_bits( &attributes ),
|
|
|
|
PSA_BYTES_TO_BITS( byte_size ) );
|
|
|
|
ASSERT_NO_SLOT_NUMBER( &attributes );
|
|
|
|
memset( buffer, 0, byte_size + 1 );
|
|
|
|
PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
|
|
|
|
for( n = 0; n < byte_size; n++ )
|
|
|
|
TEST_EQUAL( buffer[n], 'K' );
|
|
|
|
for( n = byte_size; n < buffer_size; n++ )
|
|
|
|
TEST_EQUAL( buffer[n], 0 );
|
|
|
|
}
|
2018-07-06 16:53:09 +02:00
|
|
|
|
|
|
|
exit:
|
2021-06-16 17:52:21 +02:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2021-06-16 17:52:21 +02:00
|
|
|
mbedtls_free( buffer );
|
2018-07-06 16:53:09 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-01-17 15:29:38 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
|
2021-06-16 17:52:21 +02:00
|
|
|
void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
|
2018-07-06 16:53:09 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
size_t bits = bits_arg;
|
2021-02-18 12:03:50 +01:00
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_status_t status;
|
|
|
|
psa_key_type_t type =
|
|
|
|
keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
|
|
|
|
size_t buffer_size = /* Slight overapproximations */
|
|
|
|
keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
|
|
|
|
unsigned char *buffer = NULL;
|
|
|
|
unsigned char *p;
|
|
|
|
int ret;
|
|
|
|
size_t length;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-06-18 22:20:03 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2021-06-16 17:52:21 +02:00
|
|
|
ASSERT_ALLOC( buffer, buffer_size );
|
2018-07-06 16:53:09 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
|
|
|
|
bits, keypair ) ) >= 0 );
|
|
|
|
length = ret;
|
2018-07-06 16:53:09 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Try importing the key */
|
|
|
|
psa_set_key_type( &attributes, type );
|
|
|
|
status = psa_import_key( &attributes, p, length, &key );
|
|
|
|
TEST_EQUAL( status, expected_status );
|
2018-07-06 16:53:09 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
PSA_ASSERT( psa_destroy_key( key ) );
|
2018-07-06 16:53:09 +02:00
|
|
|
|
|
|
|
exit:
|
2021-06-16 17:52:21 +02:00
|
|
|
mbedtls_free( buffer );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-07-06 16:53:09 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2021-06-16 17:52:21 +02:00
|
|
|
void import_export( data_t *data,
|
|
|
|
int type_arg,
|
|
|
|
int usage_arg, int alg_arg,
|
2021-07-06 23:20:22 +02:00
|
|
|
int lifetime_arg,
|
2021-06-16 17:52:21 +02:00
|
|
|
int expected_bits,
|
|
|
|
int export_size_delta,
|
|
|
|
int expected_export_status_arg,
|
|
|
|
int canonical_input )
|
2018-07-06 16:53:09 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_key_type_t type = type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t expected_export_status = expected_export_status_arg;
|
2018-07-06 16:53:09 +02:00
|
|
|
psa_status_t status;
|
2021-07-06 23:20:22 +02:00
|
|
|
psa_key_lifetime_t lifetime = lifetime_arg;
|
2021-06-16 17:52:21 +02:00
|
|
|
unsigned char *exported = NULL;
|
|
|
|
unsigned char *reexported = NULL;
|
|
|
|
size_t export_size;
|
|
|
|
size_t exported_length = INVALID_EXPORT_LENGTH;
|
|
|
|
size_t reexported_length;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-07-06 16:53:09 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
export_size = (ptrdiff_t) data->len + export_size_delta;
|
|
|
|
ASSERT_ALLOC( exported, export_size );
|
|
|
|
if( ! canonical_input )
|
|
|
|
ASSERT_ALLOC( reexported, export_size );
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-07-06 16:53:09 +02:00
|
|
|
|
2021-07-06 23:20:22 +02:00
|
|
|
psa_set_key_lifetime( &attributes, lifetime );
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, usage_arg );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, type );
|
2018-07-06 16:53:09 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Import the key */
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
|
2018-07-06 16:53:09 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Test the key information */
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
|
|
|
|
TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
|
|
|
|
TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
|
|
|
|
ASSERT_NO_SLOT_NUMBER( &got_attributes );
|
2018-07-06 16:53:09 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Export the key */
|
|
|
|
status = psa_export_key( key, exported, export_size, &exported_length );
|
|
|
|
TEST_EQUAL( status, expected_export_status );
|
2018-07-06 16:53:09 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* The exported length must be set by psa_export_key() to a value between 0
|
|
|
|
* and export_size. On errors, the exported length must be 0. */
|
|
|
|
TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
|
|
|
|
TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( exported_length, export_size );
|
2021-06-16 17:52:21 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( mem_is_char( exported + exported_length, 0,
|
|
|
|
export_size - exported_length ) );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( exported_length, 0 );
|
|
|
|
goto destroy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Run sanity checks on the exported key. For non-canonical inputs,
|
|
|
|
* this validates the canonical representations. For canonical inputs,
|
|
|
|
* this doesn't directly validate the implementation, but it still helps
|
|
|
|
* by cross-validating the test data with the sanity check code. */
|
2021-07-06 23:20:22 +02:00
|
|
|
if( !psa_key_lifetime_is_external( lifetime ) )
|
|
|
|
{
|
|
|
|
if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
|
|
|
|
goto exit;
|
|
|
|
}
|
2021-06-16 17:52:21 +02:00
|
|
|
|
|
|
|
if( canonical_input )
|
|
|
|
ASSERT_COMPARE( data->x, data->len, exported, exported_length );
|
2018-07-06 16:53:09 +02:00
|
|
|
else
|
2021-06-16 17:52:21 +02:00
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
|
|
|
|
&key2 ) );
|
|
|
|
PSA_ASSERT( psa_export_key( key2,
|
|
|
|
reexported,
|
|
|
|
export_size,
|
|
|
|
&reexported_length ) );
|
|
|
|
ASSERT_COMPARE( exported, exported_length,
|
2021-07-06 23:20:22 +02:00
|
|
|
reexported, reexported_length );
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_destroy_key( key2 ) );
|
|
|
|
}
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( exported_length,
|
2021-07-06 23:20:22 +02:00
|
|
|
PSA_EXPORT_KEY_OUTPUT_SIZE( type,
|
2021-09-10 02:52:44 +02:00
|
|
|
psa_get_key_bits( &got_attributes ) ) );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE );
|
2021-06-16 17:52:21 +02:00
|
|
|
|
|
|
|
destroy:
|
|
|
|
/* Destroy the key */
|
|
|
|
PSA_ASSERT( psa_destroy_key( key ) );
|
|
|
|
test_operations_on_invalid_key( key );
|
2018-07-06 16:53:09 +02:00
|
|
|
|
|
|
|
exit:
|
2020-11-19 17:55:23 +01:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_reset_key_attributes( &got_attributes );
|
2021-07-06 23:20:22 +02:00
|
|
|
psa_destroy_key( key ) ;
|
2021-06-16 17:52:21 +02:00
|
|
|
mbedtls_free( exported );
|
|
|
|
mbedtls_free( reexported );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-07-06 16:53:09 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2021-06-16 17:52:21 +02:00
|
|
|
void import_export_public_key( data_t *data,
|
|
|
|
int type_arg,
|
|
|
|
int alg_arg,
|
2021-07-06 23:20:22 +02:00
|
|
|
int lifetime_arg,
|
2021-06-16 17:52:21 +02:00
|
|
|
int export_size_delta,
|
|
|
|
int expected_export_status_arg,
|
|
|
|
data_t *expected_public_key )
|
2018-07-06 16:53:09 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_key_type_t type = type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t expected_export_status = expected_export_status_arg;
|
2018-07-06 16:53:09 +02:00
|
|
|
psa_status_t status;
|
2021-07-06 23:20:22 +02:00
|
|
|
psa_key_lifetime_t lifetime = lifetime_arg;
|
2021-06-16 17:52:21 +02:00
|
|
|
unsigned char *exported = NULL;
|
|
|
|
size_t export_size = expected_public_key->len + export_size_delta;
|
|
|
|
size_t exported_length = INVALID_EXPORT_LENGTH;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-07-06 16:53:09 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-07-06 16:53:09 +02:00
|
|
|
|
2021-07-06 23:20:22 +02:00
|
|
|
psa_set_key_lifetime( &attributes, lifetime );
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, type );
|
2018-07-06 16:53:09 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Import the key */
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
|
2018-07-06 16:53:09 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Export the public key */
|
|
|
|
ASSERT_ALLOC( exported, export_size );
|
|
|
|
status = psa_export_public_key( key,
|
|
|
|
exported, export_size,
|
|
|
|
&exported_length );
|
|
|
|
TEST_EQUAL( status, expected_export_status );
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
|
|
|
|
size_t bits;
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
|
|
|
bits = psa_get_key_bits( &attributes );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( expected_public_key->len,
|
|
|
|
PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
|
|
|
|
TEST_LE_U( expected_public_key->len,
|
|
|
|
PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
|
|
|
|
TEST_LE_U( expected_public_key->len,
|
|
|
|
PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
|
2021-06-16 17:52:21 +02:00
|
|
|
ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
|
|
|
|
exported, exported_length );
|
|
|
|
}
|
2018-06-18 22:20:03 +02:00
|
|
|
exit:
|
2021-06-16 17:52:21 +02:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
|
|
|
|
mbedtls_free( exported );
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-06-18 22:20:03 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-06-11 15:50:16 +02:00
|
|
|
/* BEGIN_CASE */
|
2021-06-16 17:52:21 +02:00
|
|
|
void import_and_exercise_key( data_t *data,
|
|
|
|
int type_arg,
|
|
|
|
int bits_arg,
|
|
|
|
int alg_arg )
|
2018-07-12 17:17:20 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_key_type_t type = type_arg;
|
|
|
|
size_t bits = bits_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-07-12 17:17:20 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-07-12 17:17:20 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, usage );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, type );
|
2018-07-12 17:17:20 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Import the key */
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
|
2019-06-11 15:50:16 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Test the key information */
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
|
|
|
|
TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
|
|
|
|
TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
|
2019-06-11 15:50:16 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Do something with the key according to its type and permitted usage. */
|
|
|
|
if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
|
|
|
|
goto exit;
|
2019-06-11 15:50:16 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_destroy_key( key ) );
|
|
|
|
test_operations_on_invalid_key( key );
|
2018-07-12 17:17:20 +02:00
|
|
|
|
|
|
|
exit:
|
2021-06-16 17:52:21 +02:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
|
|
|
psa_reset_key_attributes( &got_attributes );
|
|
|
|
|
|
|
|
psa_reset_key_attributes( &attributes );
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-07-12 17:17:20 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-09-18 12:01:02 +02:00
|
|
|
/* BEGIN_CASE */
|
2021-06-16 17:52:21 +02:00
|
|
|
void effective_key_attributes( int type_arg, int expected_type_arg,
|
|
|
|
int bits_arg, int expected_bits_arg,
|
|
|
|
int usage_arg, int expected_usage_arg,
|
|
|
|
int alg_arg, int expected_alg_arg )
|
2018-09-18 12:01:02 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_key_type_t key_type = type_arg;
|
|
|
|
psa_key_type_t expected_key_type = expected_type_arg;
|
|
|
|
size_t bits = bits_arg;
|
|
|
|
size_t expected_bits = expected_bits_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_algorithm_t expected_alg = expected_alg_arg;
|
|
|
|
psa_key_usage_t usage = usage_arg;
|
|
|
|
psa_key_usage_t expected_usage = expected_usage_arg;
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-09-18 12:01:02 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-09-18 12:01:02 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, usage );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_set_key_type( &attributes, key_type );
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_bits( &attributes, bits );
|
2018-09-18 12:01:02 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_generate_key( &attributes, &key ) );
|
|
|
|
psa_reset_key_attributes( &attributes );
|
2018-09-18 12:01:02 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
|
|
|
TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
|
|
|
|
TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
|
|
|
|
TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
|
|
|
|
TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
|
2018-09-18 12:01:02 +02:00
|
|
|
|
|
|
|
exit:
|
2021-06-16 17:52:21 +02:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-09-18 12:01:02 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-05-10 19:33:38 +02:00
|
|
|
/* BEGIN_CASE */
|
2021-06-16 17:52:21 +02:00
|
|
|
void check_key_policy( int type_arg, int bits_arg,
|
|
|
|
int usage_arg, int alg_arg )
|
2019-05-10 19:33:38 +02:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
|
2021-06-28 14:36:03 +02:00
|
|
|
usage_arg,
|
|
|
|
mbedtls_test_update_key_usage_flags( usage_arg ),
|
2021-05-13 16:17:16 +02:00
|
|
|
alg_arg, alg_arg );
|
2021-06-16 17:52:21 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2019-05-10 19:33:38 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void key_attributes_init( )
|
|
|
|
{
|
|
|
|
/* Test each valid way of initializing the object, except for `= {0}`, as
|
|
|
|
* Clang 5 complains when `-Wmissing-field-initializers` is used, even
|
|
|
|
* though it's OK by the C standard. We could test for this, but we'd need
|
2021-12-21 06:14:10 +01:00
|
|
|
* to suppress the Clang warning for the test. */
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_key_attributes_t func = psa_key_attributes_init( );
|
|
|
|
psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_attributes_t zero;
|
2019-05-10 19:33:38 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
memset( &zero, 0, sizeof( zero ) );
|
2019-05-10 19:33:38 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
|
|
|
|
TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
|
|
|
|
TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
|
2019-05-10 19:33:38 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_get_key_type( &func ), 0 );
|
|
|
|
TEST_EQUAL( psa_get_key_type( &init ), 0 );
|
|
|
|
TEST_EQUAL( psa_get_key_type( &zero ), 0 );
|
2019-05-10 19:33:38 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_get_key_bits( &func ), 0 );
|
|
|
|
TEST_EQUAL( psa_get_key_bits( &init ), 0 );
|
|
|
|
TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
|
|
|
|
TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
|
|
|
|
TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
|
2020-11-19 17:55:23 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
|
|
|
|
TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
|
|
|
|
TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
|
2019-05-10 19:33:38 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-04-11 21:25:46 +02:00
|
|
|
/* BEGIN_CASE */
|
2021-05-13 16:17:16 +02:00
|
|
|
void mac_key_policy( int policy_usage_arg,
|
|
|
|
int policy_alg_arg,
|
|
|
|
int key_type_arg,
|
2021-06-16 17:52:21 +02:00
|
|
|
data_t *key_data,
|
2021-05-13 16:17:16 +02:00
|
|
|
int exercise_alg_arg,
|
2021-08-24 11:01:23 +02:00
|
|
|
int expected_status_sign_arg,
|
|
|
|
int expected_status_verify_arg )
|
2019-04-11 21:25:46 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
2021-05-13 16:17:16 +02:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t policy_alg = policy_alg_arg;
|
|
|
|
psa_algorithm_t exercise_alg = exercise_alg_arg;
|
|
|
|
psa_key_usage_t policy_usage = policy_usage_arg;
|
2019-04-11 21:25:46 +02:00
|
|
|
psa_status_t status;
|
2021-08-24 11:01:23 +02:00
|
|
|
psa_status_t expected_status_sign = expected_status_sign_arg;
|
|
|
|
psa_status_t expected_status_verify = expected_status_verify_arg;
|
2021-06-16 17:52:21 +02:00
|
|
|
unsigned char mac[PSA_MAC_MAX_SIZE];
|
2019-04-11 21:25:46 +02:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, policy_usage );
|
|
|
|
psa_set_key_algorithm( &attributes, policy_alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2019-04-11 21:25:46 +02:00
|
|
|
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
2020-08-04 14:58:35 +02:00
|
|
|
&key ) );
|
2019-04-11 21:25:46 +02:00
|
|
|
|
2021-06-29 11:06:16 +02:00
|
|
|
TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
|
|
|
|
mbedtls_test_update_key_usage_flags( policy_usage ) );
|
2021-05-13 16:17:16 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
status = psa_mac_sign_setup( &operation, key, exercise_alg );
|
2021-08-24 11:01:23 +02:00
|
|
|
TEST_EQUAL( status, expected_status_sign );
|
2021-02-18 12:03:50 +01:00
|
|
|
|
2021-08-30 17:09:03 +02:00
|
|
|
/* Calculate the MAC, one-shot case. */
|
|
|
|
uint8_t input[128] = {0};
|
|
|
|
size_t mac_len;
|
|
|
|
TEST_EQUAL( psa_mac_compute( key, exercise_alg,
|
|
|
|
input, 128,
|
|
|
|
mac, PSA_MAC_MAX_SIZE, &mac_len ),
|
|
|
|
expected_status_sign );
|
|
|
|
|
2022-02-07 12:20:21 +01:00
|
|
|
/* Calculate the MAC, multi-part case. */
|
|
|
|
PSA_ASSERT( psa_mac_abort( &operation ) );
|
|
|
|
status = psa_mac_sign_setup( &operation, key, exercise_alg );
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
status = psa_mac_update( &operation, input, 128 );
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
|
|
|
|
&mac_len ),
|
|
|
|
expected_status_sign );
|
|
|
|
else
|
|
|
|
TEST_EQUAL( status, expected_status_sign );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status_sign );
|
|
|
|
}
|
|
|
|
PSA_ASSERT( psa_mac_abort( &operation ) );
|
|
|
|
|
2021-08-30 17:09:03 +02:00
|
|
|
/* Verify correct MAC, one-shot case. */
|
|
|
|
status = psa_mac_verify( key, exercise_alg, input, 128,
|
|
|
|
mac, mac_len );
|
|
|
|
|
|
|
|
if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
|
2021-06-16 17:52:21 +02:00
|
|
|
else
|
2021-08-30 17:09:03 +02:00
|
|
|
TEST_EQUAL( status, expected_status_verify );
|
2019-04-11 21:25:46 +02:00
|
|
|
|
2022-02-07 12:20:21 +01:00
|
|
|
/* Verify correct MAC, multi-part case. */
|
|
|
|
status = psa_mac_verify_setup( &operation, key, exercise_alg );
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
status = psa_mac_update( &operation, input, 128 );
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
status = psa_mac_verify_finish( &operation, mac, mac_len );
|
|
|
|
if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
else
|
|
|
|
TEST_EQUAL( status, expected_status_verify );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status_verify );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status_verify );
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_mac_abort( &operation );
|
|
|
|
|
|
|
|
memset( mac, 0, sizeof( mac ) );
|
|
|
|
status = psa_mac_verify_setup( &operation, key, exercise_alg );
|
2021-08-24 11:01:23 +02:00
|
|
|
TEST_EQUAL( status, expected_status_verify );
|
2019-04-11 21:25:46 +02:00
|
|
|
|
|
|
|
exit:
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_mac_abort( &operation );
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2019-04-11 21:25:46 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-01-28 13:03:09 +01:00
|
|
|
/* BEGIN_CASE */
|
2021-05-13 16:17:16 +02:00
|
|
|
void cipher_key_policy( int policy_usage_arg,
|
2021-06-16 17:52:21 +02:00
|
|
|
int policy_alg,
|
|
|
|
int key_type,
|
|
|
|
data_t *key_data,
|
|
|
|
int exercise_alg )
|
2019-01-28 13:03:09 +01:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
2021-05-13 16:17:16 +02:00
|
|
|
psa_key_usage_t policy_usage = policy_usage_arg;
|
2022-02-07 14:50:35 +01:00
|
|
|
size_t output_buffer_size = 0;
|
|
|
|
size_t input_buffer_size = 0;
|
|
|
|
size_t output_length = 0;
|
|
|
|
uint8_t *output = NULL;
|
|
|
|
uint8_t *input = NULL;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_status_t status;
|
2019-01-28 13:03:09 +01:00
|
|
|
|
2022-02-07 14:50:35 +01:00
|
|
|
input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg );
|
|
|
|
output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg,
|
|
|
|
input_buffer_size );
|
|
|
|
|
|
|
|
ASSERT_ALLOC( input, input_buffer_size );
|
|
|
|
ASSERT_ALLOC( output, output_buffer_size );
|
|
|
|
|
2019-01-28 13:03:09 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, policy_usage );
|
|
|
|
psa_set_key_algorithm( &attributes, policy_alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2019-01-28 13:03:09 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
2021-02-18 12:03:50 +01:00
|
|
|
|
2021-06-28 14:05:00 +02:00
|
|
|
/* Check if no key usage flag implication is done */
|
|
|
|
TEST_EQUAL( policy_usage,
|
|
|
|
mbedtls_test_update_key_usage_flags( policy_usage ) );
|
2021-05-13 16:17:16 +02:00
|
|
|
|
2022-02-07 14:50:35 +01:00
|
|
|
/* Encrypt check, one-shot */
|
|
|
|
status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&output_length);
|
|
|
|
if( policy_alg == exercise_alg &&
|
|
|
|
( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
else
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
|
|
|
|
|
|
|
|
/* Encrypt check, multi-part */
|
2021-06-16 17:52:21 +02:00
|
|
|
status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
|
|
|
|
if( policy_alg == exercise_alg &&
|
|
|
|
( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
else
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
|
|
|
|
psa_cipher_abort( &operation );
|
2019-01-28 13:03:09 +01:00
|
|
|
|
2022-02-07 14:50:35 +01:00
|
|
|
/* Decrypt check, one-shot */
|
|
|
|
status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size,
|
|
|
|
input, input_buffer_size,
|
|
|
|
&output_length);
|
|
|
|
if( policy_alg == exercise_alg &&
|
|
|
|
( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
else
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
|
|
|
|
|
|
|
|
/* Decrypt check, multi-part */
|
2021-06-16 17:52:21 +02:00
|
|
|
status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
|
|
|
|
if( policy_alg == exercise_alg &&
|
|
|
|
( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
else
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
|
2019-01-28 13:03:09 +01:00
|
|
|
|
|
|
|
exit:
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_cipher_abort( &operation );
|
2022-02-07 14:50:35 +01:00
|
|
|
mbedtls_free( input );
|
|
|
|
mbedtls_free( output );
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2019-01-28 13:03:09 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-05-03 17:14:08 +02:00
|
|
|
/* BEGIN_CASE */
|
2021-05-13 16:17:16 +02:00
|
|
|
void aead_key_policy( int policy_usage_arg,
|
2021-06-16 17:52:21 +02:00
|
|
|
int policy_alg,
|
|
|
|
int key_type,
|
|
|
|
data_t *key_data,
|
|
|
|
int nonce_length_arg,
|
|
|
|
int tag_length_arg,
|
|
|
|
int exercise_alg,
|
|
|
|
int expected_status_arg )
|
2019-05-03 17:14:08 +02:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2022-02-07 14:51:11 +01:00
|
|
|
psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
|
2021-05-13 16:17:16 +02:00
|
|
|
psa_key_usage_t policy_usage = policy_usage_arg;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_status_t status;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
unsigned char nonce[16] = {0};
|
|
|
|
size_t nonce_length = nonce_length_arg;
|
|
|
|
unsigned char tag[16];
|
|
|
|
size_t tag_length = tag_length_arg;
|
|
|
|
size_t output_length;
|
2019-05-03 17:14:08 +02:00
|
|
|
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( nonce_length, sizeof( nonce ) );
|
|
|
|
TEST_LE_U( tag_length, sizeof( tag ) );
|
2019-05-28 14:08:50 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2019-05-28 14:08:50 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, policy_usage );
|
|
|
|
psa_set_key_algorithm( &attributes, policy_alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2019-05-03 17:14:08 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
2019-01-04 12:47:44 +01:00
|
|
|
|
2021-06-28 14:05:00 +02:00
|
|
|
/* Check if no key usage implication is done */
|
|
|
|
TEST_EQUAL( policy_usage,
|
|
|
|
mbedtls_test_update_key_usage_flags( policy_usage ) );
|
2021-05-13 16:17:16 +02:00
|
|
|
|
2022-02-07 14:51:11 +01:00
|
|
|
/* Encrypt check, one-shot */
|
2021-06-16 17:52:21 +02:00
|
|
|
status = psa_aead_encrypt( key, exercise_alg,
|
|
|
|
nonce, nonce_length,
|
|
|
|
NULL, 0,
|
|
|
|
NULL, 0,
|
|
|
|
tag, tag_length,
|
|
|
|
&output_length );
|
|
|
|
if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
else
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
|
2019-01-04 12:47:44 +01:00
|
|
|
|
2022-02-07 14:51:11 +01:00
|
|
|
/* Encrypt check, multi-part */
|
|
|
|
status = psa_aead_encrypt_setup( &operation, key, exercise_alg );
|
|
|
|
if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
else
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
|
|
|
|
|
|
|
|
/* Decrypt check, one-shot */
|
2021-06-16 17:52:21 +02:00
|
|
|
memset( tag, 0, sizeof( tag ) );
|
|
|
|
status = psa_aead_decrypt( key, exercise_alg,
|
|
|
|
nonce, nonce_length,
|
|
|
|
NULL, 0,
|
|
|
|
tag, tag_length,
|
|
|
|
NULL, 0,
|
|
|
|
&output_length );
|
|
|
|
if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
|
|
|
|
else if( expected_status == PSA_SUCCESS )
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
else
|
|
|
|
TEST_EQUAL( status, expected_status );
|
2019-02-15 14:52:25 +01:00
|
|
|
|
2022-02-07 14:51:11 +01:00
|
|
|
/* Decrypt check, multi-part */
|
|
|
|
PSA_ASSERT( psa_aead_abort( &operation ) );
|
|
|
|
status = psa_aead_decrypt_setup( &operation, key, exercise_alg );
|
|
|
|
if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
|
|
|
|
else
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
exit:
|
2022-02-07 14:51:11 +01:00
|
|
|
PSA_ASSERT( psa_aead_abort( &operation ) );
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_destroy_key( key );
|
|
|
|
PSA_DONE( );
|
2019-01-04 12:47:44 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-06-20 16:05:20 +02:00
|
|
|
/* BEGIN_CASE */
|
2021-05-13 16:17:16 +02:00
|
|
|
void asymmetric_encryption_key_policy( int policy_usage_arg,
|
2021-06-16 17:52:21 +02:00
|
|
|
int policy_alg,
|
|
|
|
int key_type,
|
|
|
|
data_t *key_data,
|
|
|
|
int exercise_alg )
|
2018-06-20 16:05:20 +02:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-05-13 16:17:16 +02:00
|
|
|
psa_key_usage_t policy_usage = policy_usage_arg;
|
2018-06-20 16:05:20 +02:00
|
|
|
psa_status_t status;
|
2021-06-16 17:52:21 +02:00
|
|
|
size_t key_bits;
|
|
|
|
size_t buffer_length;
|
|
|
|
unsigned char *buffer = NULL;
|
|
|
|
size_t output_length;
|
2018-06-20 16:05:20 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-06-20 16:05:20 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, policy_usage );
|
|
|
|
psa_set_key_algorithm( &attributes, policy_alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2018-06-20 16:05:20 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
2019-02-25 22:11:18 +01:00
|
|
|
|
2021-06-28 14:05:00 +02:00
|
|
|
/* Check if no key usage implication is done */
|
|
|
|
TEST_EQUAL( policy_usage,
|
|
|
|
mbedtls_test_update_key_usage_flags( policy_usage ) );
|
2021-05-13 16:17:16 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
|
|
|
key_bits = psa_get_key_bits( &attributes );
|
|
|
|
buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
|
|
|
|
exercise_alg );
|
|
|
|
ASSERT_ALLOC( buffer, buffer_length );
|
2019-02-25 22:11:18 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
status = psa_asymmetric_encrypt( key, exercise_alg,
|
|
|
|
NULL, 0,
|
|
|
|
NULL, 0,
|
|
|
|
buffer, buffer_length,
|
|
|
|
&output_length );
|
|
|
|
if( policy_alg == exercise_alg &&
|
|
|
|
( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
else
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
|
|
|
|
|
|
|
|
if( buffer_length != 0 )
|
|
|
|
memset( buffer, 0, buffer_length );
|
|
|
|
status = psa_asymmetric_decrypt( key, exercise_alg,
|
|
|
|
buffer, buffer_length,
|
|
|
|
NULL, 0,
|
|
|
|
buffer, buffer_length,
|
|
|
|
&output_length );
|
|
|
|
if( policy_alg == exercise_alg &&
|
|
|
|
( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
|
|
|
|
else
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
|
2019-02-25 17:42:03 +01:00
|
|
|
|
2018-06-20 16:05:20 +02:00
|
|
|
exit:
|
2021-06-16 17:52:21 +02:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
|
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2021-06-16 17:52:21 +02:00
|
|
|
mbedtls_free( buffer );
|
2018-06-20 16:05:20 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-11-28 19:33:58 +01:00
|
|
|
/* BEGIN_CASE */
|
2021-05-13 16:17:16 +02:00
|
|
|
void asymmetric_signature_key_policy( int policy_usage_arg,
|
2021-06-16 17:52:21 +02:00
|
|
|
int policy_alg,
|
|
|
|
int key_type,
|
|
|
|
data_t *key_data,
|
|
|
|
int exercise_alg,
|
2021-05-13 16:17:16 +02:00
|
|
|
int payload_length_arg,
|
|
|
|
int expected_usage_arg )
|
2019-11-28 19:33:58 +01:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-05-13 16:17:16 +02:00
|
|
|
psa_key_usage_t policy_usage = policy_usage_arg;
|
|
|
|
psa_key_usage_t expected_usage = expected_usage_arg;
|
2019-11-28 19:33:58 +01:00
|
|
|
psa_status_t status;
|
2021-06-16 17:52:21 +02:00
|
|
|
unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
|
|
|
|
/* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
|
|
|
|
* compatible with the policy and `payload_length_arg` is supposed to be
|
|
|
|
* a valid input length to sign. If `payload_length_arg <= 0`,
|
|
|
|
* `exercise_alg` is supposed to be forbidden by the policy. */
|
|
|
|
int compatible_alg = payload_length_arg > 0;
|
|
|
|
size_t payload_length = compatible_alg ? payload_length_arg : 0;
|
|
|
|
unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
|
|
|
|
size_t signature_length;
|
2019-11-28 19:33:58 +01:00
|
|
|
|
2021-06-28 14:05:00 +02:00
|
|
|
/* Check if all implicit usage flags are deployed
|
2021-05-13 16:17:16 +02:00
|
|
|
in the expected usage flags. */
|
2021-06-28 14:05:00 +02:00
|
|
|
TEST_EQUAL( expected_usage,
|
|
|
|
mbedtls_test_update_key_usage_flags( policy_usage ) );
|
2021-05-13 16:17:16 +02:00
|
|
|
|
2019-11-28 19:33:58 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, policy_usage );
|
|
|
|
psa_set_key_algorithm( &attributes, policy_alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2019-11-28 19:33:58 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
2020-01-28 20:43:00 +01:00
|
|
|
|
2021-05-13 16:17:16 +02:00
|
|
|
TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
status = psa_sign_hash( key, exercise_alg,
|
|
|
|
payload, payload_length,
|
|
|
|
signature, sizeof( signature ),
|
|
|
|
&signature_length );
|
2021-05-13 16:17:16 +02:00
|
|
|
if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( status );
|
|
|
|
else
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
|
2020-01-28 20:43:00 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
memset( signature, 0, sizeof( signature ) );
|
|
|
|
status = psa_verify_hash( key, exercise_alg,
|
|
|
|
payload, payload_length,
|
|
|
|
signature, sizeof( signature ) );
|
2021-05-13 16:17:16 +02:00
|
|
|
if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
else
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
|
2020-01-28 20:43:00 +01:00
|
|
|
|
2021-09-22 16:15:05 +02:00
|
|
|
if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
|
2021-06-28 14:53:49 +02:00
|
|
|
PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
|
2021-05-13 16:17:16 +02:00
|
|
|
{
|
|
|
|
status = psa_sign_message( key, exercise_alg,
|
|
|
|
payload, payload_length,
|
|
|
|
signature, sizeof( signature ),
|
|
|
|
&signature_length );
|
|
|
|
if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
else
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
|
|
|
|
|
|
|
|
memset( signature, 0, sizeof( signature ) );
|
|
|
|
status = psa_verify_message( key, exercise_alg,
|
|
|
|
payload, payload_length,
|
|
|
|
signature, sizeof( signature ) );
|
|
|
|
if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
else
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
|
|
|
|
}
|
|
|
|
|
2020-01-28 20:43:00 +01:00
|
|
|
exit:
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_destroy_key( key );
|
2020-01-28 20:43:00 +01:00
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-11-28 19:33:58 +01:00
|
|
|
/* BEGIN_CASE */
|
2021-06-16 17:52:21 +02:00
|
|
|
void derive_key_policy( int policy_usage,
|
|
|
|
int policy_alg,
|
|
|
|
int key_type,
|
|
|
|
data_t *key_data,
|
|
|
|
int exercise_alg )
|
2019-11-28 19:33:58 +01:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
|
|
|
|
psa_status_t status;
|
2019-11-28 19:33:58 +01:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, policy_usage );
|
|
|
|
psa_set_key_algorithm( &attributes, policy_alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2019-11-28 19:33:58 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
2019-11-28 19:33:58 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
|
2019-11-28 19:33:58 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
|
|
|
|
PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
|
2019-11-28 19:33:58 +01:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_input_bytes(
|
|
|
|
&operation,
|
|
|
|
PSA_KEY_DERIVATION_INPUT_SEED,
|
|
|
|
(const uint8_t*) "", 0) );
|
2019-11-28 19:33:58 +01:00
|
|
|
}
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
status = psa_key_derivation_input_key( &operation,
|
|
|
|
PSA_KEY_DERIVATION_INPUT_SECRET,
|
|
|
|
key );
|
2018-11-01 09:44:32 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
if( policy_alg == exercise_alg &&
|
|
|
|
( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
else
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
|
2018-11-01 09:44:32 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
exit:
|
|
|
|
psa_key_derivation_abort( &operation );
|
|
|
|
psa_destroy_key( key );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2019-02-19 12:44:55 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void agreement_key_policy( int policy_usage,
|
|
|
|
int policy_alg,
|
|
|
|
int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
int exercise_alg,
|
|
|
|
int expected_status_arg )
|
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
|
|
|
|
psa_status_t status;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2019-02-19 12:44:55 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2019-02-19 12:44:55 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, policy_usage );
|
|
|
|
psa_set_key_algorithm( &attributes, policy_alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2019-02-19 12:44:55 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
2018-11-01 09:44:32 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
|
|
|
|
status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
|
|
|
|
|
|
|
|
TEST_EQUAL( status, expected_status );
|
2018-11-01 09:44:32 +01:00
|
|
|
|
|
|
|
exit:
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_key_derivation_abort( &operation );
|
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-11-01 09:44:32 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void key_policy_alg2( int key_type_arg, data_t *key_data,
|
|
|
|
int usage_arg, int alg_arg, int alg2_arg )
|
2018-10-18 17:01:10 +02:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_usage_t usage = usage_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_algorithm_t alg2 = alg2_arg;
|
2018-10-18 17:01:10 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-10-18 17:01:10 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, usage );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_enrollment_algorithm( &attributes, alg2 );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
2018-10-18 17:01:10 +02:00
|
|
|
|
2021-06-28 14:05:00 +02:00
|
|
|
/* Update the usage flags to obtain implicit usage flags */
|
|
|
|
usage = mbedtls_test_update_key_usage_flags( usage );
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
|
|
|
|
TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
|
|
|
|
TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
|
|
|
|
TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
|
2018-10-18 17:01:10 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
|
|
|
|
goto exit;
|
|
|
|
if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
|
|
|
|
goto exit;
|
2018-10-24 17:16:19 +02:00
|
|
|
|
2018-10-18 17:01:10 +02:00
|
|
|
exit:
|
2021-06-16 17:52:21 +02:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
|
|
|
psa_reset_key_attributes( &got_attributes );
|
|
|
|
|
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-10-18 17:01:10 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void raw_agreement_key_policy( int policy_usage,
|
|
|
|
int policy_alg,
|
|
|
|
int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
int exercise_alg,
|
|
|
|
int expected_status_arg )
|
2018-10-25 09:22:01 +02:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
|
|
|
|
psa_status_t status;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2018-10-25 09:22:01 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-10-25 09:22:01 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, policy_usage );
|
|
|
|
psa_set_key_algorithm( &attributes, policy_alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
|
|
|
|
status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
|
|
|
|
|
|
|
|
TEST_EQUAL( status, expected_status );
|
2018-10-25 09:22:01 +02:00
|
|
|
|
|
|
|
exit:
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_key_derivation_abort( &operation );
|
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-10-25 09:22:01 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void copy_success( int source_usage_arg,
|
|
|
|
int source_alg_arg, int source_alg2_arg,
|
2021-07-04 22:48:48 +02:00
|
|
|
unsigned int source_lifetime_arg,
|
2021-06-16 17:52:21 +02:00
|
|
|
int type_arg, data_t *material,
|
|
|
|
int copy_attributes,
|
|
|
|
int target_usage_arg,
|
|
|
|
int target_alg_arg, int target_alg2_arg,
|
2021-07-04 22:48:48 +02:00
|
|
|
unsigned int target_lifetime_arg,
|
2021-06-16 17:52:21 +02:00
|
|
|
int expected_usage_arg,
|
|
|
|
int expected_alg_arg, int expected_alg2_arg )
|
2019-01-19 12:03:41 +01:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_usage_t expected_usage = expected_usage_arg;
|
|
|
|
psa_algorithm_t expected_alg = expected_alg_arg;
|
|
|
|
psa_algorithm_t expected_alg2 = expected_alg2_arg;
|
2021-07-04 22:48:48 +02:00
|
|
|
psa_key_lifetime_t source_lifetime = source_lifetime_arg;
|
|
|
|
psa_key_lifetime_t target_lifetime = target_lifetime_arg;
|
2021-06-16 17:52:21 +02:00
|
|
|
mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
uint8_t *export_buffer = NULL;
|
2019-01-19 12:03:41 +01:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Prepare the source key. */
|
|
|
|
psa_set_key_usage_flags( &source_attributes, source_usage_arg );
|
|
|
|
psa_set_key_algorithm( &source_attributes, source_alg_arg );
|
|
|
|
psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
|
|
|
|
psa_set_key_type( &source_attributes, type_arg );
|
2021-07-04 22:48:48 +02:00
|
|
|
psa_set_key_lifetime( &source_attributes, source_lifetime);
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &source_attributes,
|
|
|
|
material->x, material->len,
|
|
|
|
&source_key ) );
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
|
2019-01-19 12:03:41 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Prepare the target attributes. */
|
|
|
|
if( copy_attributes )
|
|
|
|
{
|
|
|
|
target_attributes = source_attributes;
|
|
|
|
}
|
2021-07-04 22:48:48 +02:00
|
|
|
psa_set_key_lifetime( &target_attributes, target_lifetime);
|
2021-06-16 17:52:21 +02:00
|
|
|
|
|
|
|
if( target_usage_arg != -1 )
|
|
|
|
psa_set_key_usage_flags( &target_attributes, target_usage_arg );
|
|
|
|
if( target_alg_arg != -1 )
|
|
|
|
psa_set_key_algorithm( &target_attributes, target_alg_arg );
|
|
|
|
if( target_alg2_arg != -1 )
|
|
|
|
psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
|
|
|
|
|
2021-07-04 22:48:48 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Copy the key. */
|
|
|
|
PSA_ASSERT( psa_copy_key( source_key,
|
|
|
|
&target_attributes, &target_key ) );
|
|
|
|
|
|
|
|
/* Destroy the source to ensure that this doesn't affect the target. */
|
|
|
|
PSA_ASSERT( psa_destroy_key( source_key ) );
|
|
|
|
|
|
|
|
/* Test that the target slot has the expected content and policy. */
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
|
|
|
|
TEST_EQUAL( psa_get_key_type( &source_attributes ),
|
|
|
|
psa_get_key_type( &target_attributes ) );
|
|
|
|
TEST_EQUAL( psa_get_key_bits( &source_attributes ),
|
|
|
|
psa_get_key_bits( &target_attributes ) );
|
|
|
|
TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
|
|
|
|
TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
|
|
|
|
TEST_EQUAL( expected_alg2,
|
|
|
|
psa_get_key_enrollment_algorithm( &target_attributes ) );
|
|
|
|
if( expected_usage & PSA_KEY_USAGE_EXPORT )
|
|
|
|
{
|
|
|
|
size_t length;
|
|
|
|
ASSERT_ALLOC( export_buffer, material->len );
|
|
|
|
PSA_ASSERT( psa_export_key( target_key, export_buffer,
|
|
|
|
material->len, &length ) );
|
|
|
|
ASSERT_COMPARE( material->x, material->len,
|
|
|
|
export_buffer, length );
|
|
|
|
}
|
|
|
|
|
2021-07-04 22:48:48 +02:00
|
|
|
if( !psa_key_lifetime_is_external( target_lifetime ) )
|
|
|
|
{
|
|
|
|
if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
|
|
|
|
goto exit;
|
|
|
|
if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
|
|
|
|
goto exit;
|
|
|
|
}
|
2019-01-19 12:03:41 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_destroy_key( target_key ) );
|
2019-01-19 12:03:41 +01:00
|
|
|
|
|
|
|
exit:
|
2021-06-16 17:52:21 +02:00
|
|
|
/*
|
|
|
|
* Source and target key attributes may have been returned by
|
|
|
|
* psa_get_key_attributes() thus reset them as required.
|
|
|
|
*/
|
|
|
|
psa_reset_key_attributes( &source_attributes );
|
|
|
|
psa_reset_key_attributes( &target_attributes );
|
|
|
|
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2021-06-16 17:52:21 +02:00
|
|
|
mbedtls_free( export_buffer );
|
2019-01-19 12:03:41 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void copy_fail( int source_usage_arg,
|
|
|
|
int source_alg_arg, int source_alg2_arg,
|
2021-07-04 22:48:48 +02:00
|
|
|
int source_lifetime_arg,
|
2021-06-16 17:52:21 +02:00
|
|
|
int type_arg, data_t *material,
|
|
|
|
int target_type_arg, int target_bits_arg,
|
|
|
|
int target_usage_arg,
|
|
|
|
int target_alg_arg, int target_alg2_arg,
|
|
|
|
int target_id_arg, int target_lifetime_arg,
|
|
|
|
int expected_status_arg )
|
2019-01-19 12:03:41 +01:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
|
2019-01-19 12:03:41 +01:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Prepare the source key. */
|
|
|
|
psa_set_key_usage_flags( &source_attributes, source_usage_arg );
|
|
|
|
psa_set_key_algorithm( &source_attributes, source_alg_arg );
|
|
|
|
psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
|
|
|
|
psa_set_key_type( &source_attributes, type_arg );
|
2021-07-04 22:48:48 +02:00
|
|
|
psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &source_attributes,
|
|
|
|
material->x, material->len,
|
|
|
|
&source_key ) );
|
2019-01-19 12:03:41 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Prepare the target attributes. */
|
|
|
|
psa_set_key_id( &target_attributes, key_id );
|
|
|
|
psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
|
|
|
|
psa_set_key_type( &target_attributes, target_type_arg );
|
|
|
|
psa_set_key_bits( &target_attributes, target_bits_arg );
|
|
|
|
psa_set_key_usage_flags( &target_attributes, target_usage_arg );
|
|
|
|
psa_set_key_algorithm( &target_attributes, target_alg_arg );
|
|
|
|
psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
|
2019-01-19 12:03:41 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Try to copy the key. */
|
|
|
|
TEST_EQUAL( psa_copy_key( source_key,
|
|
|
|
&target_attributes, &target_key ),
|
|
|
|
expected_status_arg );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_destroy_key( source_key ) );
|
2019-01-19 12:03:41 +01:00
|
|
|
|
|
|
|
exit:
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_reset_key_attributes( &source_attributes );
|
|
|
|
psa_reset_key_attributes( &target_attributes );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2019-01-19 12:03:41 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-01-04 12:48:03 +01:00
|
|
|
/* BEGIN_CASE */
|
2021-06-16 17:52:21 +02:00
|
|
|
void hash_operation_init( )
|
2019-01-04 12:48:03 +01:00
|
|
|
{
|
2019-02-15 15:05:35 +01:00
|
|
|
const uint8_t input[1] = { 0 };
|
2019-01-04 12:48:03 +01:00
|
|
|
/* Test each valid way of initializing the object, except for `= {0}`, as
|
|
|
|
* Clang 5 complains when `-Wmissing-field-initializers` is used, even
|
|
|
|
* though it's OK by the C standard. We could test for this, but we'd need
|
2021-12-21 06:14:10 +01:00
|
|
|
* to suppress the Clang warning for the test. */
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_hash_operation_t func = psa_hash_operation_init( );
|
|
|
|
psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
|
|
|
|
psa_hash_operation_t zero;
|
2019-01-04 12:48:03 +01:00
|
|
|
|
|
|
|
memset( &zero, 0, sizeof( zero ) );
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* A freshly-initialized hash operation should not be usable. */
|
|
|
|
TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
|
2019-02-15 15:05:35 +01:00
|
|
|
PSA_ERROR_BAD_STATE );
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
|
2019-02-15 15:05:35 +01:00
|
|
|
PSA_ERROR_BAD_STATE );
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
|
2019-02-15 15:05:35 +01:00
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* A default hash operation should be abortable without error. */
|
|
|
|
PSA_ASSERT( psa_hash_abort( &func ) );
|
|
|
|
PSA_ASSERT( psa_hash_abort( &init ) );
|
|
|
|
PSA_ASSERT( psa_hash_abort( &zero ) );
|
2019-01-04 12:48:03 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-06-20 16:05:20 +02:00
|
|
|
/* BEGIN_CASE */
|
2021-06-16 17:52:21 +02:00
|
|
|
void hash_setup( int alg_arg,
|
|
|
|
int expected_status_arg )
|
2018-06-20 16:05:20 +02:00
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2022-02-07 15:47:44 +01:00
|
|
|
uint8_t *output = NULL;
|
|
|
|
size_t output_size = 0;
|
|
|
|
size_t output_length = 0;
|
2018-06-21 09:25:10 +02:00
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
|
|
|
psa_status_t status;
|
2018-06-20 16:05:20 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-06-20 16:05:20 +02:00
|
|
|
|
2022-02-07 15:47:44 +01:00
|
|
|
/* Hash Setup, one-shot */
|
2022-02-25 15:47:34 +01:00
|
|
|
output_size = PSA_HASH_LENGTH( alg );
|
2022-02-07 15:47:44 +01:00
|
|
|
ASSERT_ALLOC( output, output_size );
|
|
|
|
|
|
|
|
status = psa_hash_compute( alg, NULL, 0,
|
|
|
|
output, output_size, &output_length );
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
|
|
|
|
/* Hash Setup, multi-part */
|
2021-06-16 17:52:21 +02:00
|
|
|
status = psa_hash_setup( &operation, alg );
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( status, expected_status );
|
2018-06-20 16:05:20 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Whether setup succeeded or failed, abort must succeed. */
|
|
|
|
PSA_ASSERT( psa_hash_abort( &operation ) );
|
|
|
|
|
|
|
|
/* If setup failed, reproduce the failure, so as to
|
|
|
|
* test the resulting state of the operation object. */
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
|
|
|
|
|
|
|
|
/* Now the operation object should be reusable. */
|
|
|
|
#if defined(KNOWN_SUPPORTED_HASH_ALG)
|
|
|
|
PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
|
|
|
|
PSA_ASSERT( psa_hash_abort( &operation ) );
|
2019-02-25 17:42:03 +01:00
|
|
|
#endif
|
|
|
|
|
2018-08-14 15:17:54 +02:00
|
|
|
exit:
|
2022-02-07 15:47:44 +01:00
|
|
|
mbedtls_free( output );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-08-14 15:17:54 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void hash_compute_fail( int alg_arg, data_t *input,
|
|
|
|
int output_size_arg, int expected_status_arg )
|
2019-02-15 15:05:35 +01:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
uint8_t *output = NULL;
|
|
|
|
size_t output_size = output_size_arg;
|
|
|
|
size_t output_length = INVALID_EXPORT_LENGTH;
|
2022-02-07 11:18:45 +01:00
|
|
|
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
psa_status_t status;
|
2019-02-15 15:05:35 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
ASSERT_ALLOC( output, output_size );
|
2019-02-15 15:05:35 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-06-20 16:05:20 +02:00
|
|
|
|
2022-02-07 11:18:45 +01:00
|
|
|
/* Hash Compute, one-shot */
|
2021-06-16 17:52:21 +02:00
|
|
|
status = psa_hash_compute( alg, input->x, input->len,
|
|
|
|
output, output_size, &output_length );
|
|
|
|
TEST_EQUAL( status, expected_status );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( output_length, output_size );
|
2019-05-28 14:08:50 +02:00
|
|
|
|
2022-02-07 11:18:45 +01:00
|
|
|
/* Hash Compute, multi-part */
|
|
|
|
status = psa_hash_setup( &operation, alg );
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
status = psa_hash_update( &operation, input->x, input->len );
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
status = psa_hash_finish( &operation, output, output_size,
|
|
|
|
&output_length );
|
|
|
|
if( status == PSA_SUCCESS )
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( output_length, output_size );
|
2022-02-07 11:18:45 +01:00
|
|
|
else
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
}
|
|
|
|
|
2018-08-14 15:17:54 +02:00
|
|
|
exit:
|
2022-02-07 11:18:45 +01:00
|
|
|
PSA_ASSERT( psa_hash_abort( &operation ) );
|
2021-06-16 17:52:21 +02:00
|
|
|
mbedtls_free( output );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-08-14 15:17:54 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2021-06-16 17:52:21 +02:00
|
|
|
void hash_compare_fail( int alg_arg, data_t *input,
|
|
|
|
data_t *reference_hash,
|
|
|
|
int expected_status_arg )
|
2018-08-14 15:17:54 +02:00
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2022-02-07 11:23:20 +01:00
|
|
|
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_status_t status;
|
2018-08-14 15:17:54 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
2022-02-07 11:23:20 +01:00
|
|
|
/* Hash Compare, one-shot */
|
2021-06-16 17:52:21 +02:00
|
|
|
status = psa_hash_compare( alg, input->x, input->len,
|
|
|
|
reference_hash->x, reference_hash->len );
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
|
2022-02-07 11:23:20 +01:00
|
|
|
/* Hash Compare, multi-part */
|
|
|
|
status = psa_hash_setup( &operation, alg );
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
status = psa_hash_update( &operation, input->x, input->len );
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
status = psa_hash_verify( &operation, reference_hash->x,
|
|
|
|
reference_hash->len );
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
exit:
|
2022-02-07 11:23:20 +01:00
|
|
|
PSA_ASSERT( psa_hash_abort( &operation ) );
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void hash_compute_compare( int alg_arg, data_t *input,
|
|
|
|
data_t *expected_output )
|
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
uint8_t output[PSA_HASH_MAX_SIZE + 1];
|
|
|
|
size_t output_length = INVALID_EXPORT_LENGTH;
|
2022-02-07 11:40:23 +01:00
|
|
|
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
size_t i;
|
2018-08-14 15:17:54 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-08-14 15:17:54 +02:00
|
|
|
|
2022-02-07 11:40:23 +01:00
|
|
|
/* Compute with tight buffer, one-shot */
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
|
|
|
|
output, PSA_HASH_LENGTH( alg ),
|
|
|
|
&output_length ) );
|
|
|
|
TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
|
|
|
|
ASSERT_COMPARE( output, output_length,
|
|
|
|
expected_output->x, expected_output->len );
|
2018-08-14 15:17:54 +02:00
|
|
|
|
2022-02-07 11:40:23 +01:00
|
|
|
/* Compute with tight buffer, multi-part */
|
|
|
|
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
|
|
|
|
PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
|
|
|
|
PSA_ASSERT( psa_hash_finish( &operation, output,
|
|
|
|
PSA_HASH_LENGTH( alg ),
|
|
|
|
&output_length ) );
|
|
|
|
TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
|
|
|
|
ASSERT_COMPARE( output, output_length,
|
|
|
|
expected_output->x, expected_output->len );
|
|
|
|
|
|
|
|
/* Compute with larger buffer, one-shot */
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
|
|
|
|
output, sizeof( output ),
|
|
|
|
&output_length ) );
|
|
|
|
TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
|
|
|
|
ASSERT_COMPARE( output, output_length,
|
|
|
|
expected_output->x, expected_output->len );
|
2018-08-14 15:17:54 +02:00
|
|
|
|
2022-02-07 11:40:23 +01:00
|
|
|
/* Compute with larger buffer, multi-part */
|
|
|
|
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
|
|
|
|
PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
|
|
|
|
PSA_ASSERT( psa_hash_finish( &operation, output,
|
|
|
|
sizeof( output ), &output_length ) );
|
|
|
|
TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
|
|
|
|
ASSERT_COMPARE( output, output_length,
|
|
|
|
expected_output->x, expected_output->len );
|
|
|
|
|
|
|
|
/* Compare with correct hash, one-shot */
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
|
|
|
|
output, output_length ) );
|
2020-08-25 23:38:39 +02:00
|
|
|
|
2022-02-07 11:40:23 +01:00
|
|
|
/* Compare with correct hash, multi-part */
|
|
|
|
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
|
|
|
|
PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
|
|
|
|
PSA_ASSERT( psa_hash_verify( &operation, output,
|
|
|
|
output_length ) );
|
|
|
|
|
|
|
|
/* Compare with trailing garbage, one-shot */
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
|
|
|
|
output, output_length + 1 ),
|
|
|
|
PSA_ERROR_INVALID_SIGNATURE );
|
2018-08-14 15:17:54 +02:00
|
|
|
|
2022-02-07 11:40:23 +01:00
|
|
|
/* Compare with trailing garbage, multi-part */
|
|
|
|
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
|
|
|
|
PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
|
|
|
|
TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
|
|
|
|
PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
|
|
|
|
/* Compare with truncated hash, one-shot */
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
|
|
|
|
output, output_length - 1 ),
|
|
|
|
PSA_ERROR_INVALID_SIGNATURE );
|
2020-08-25 23:44:59 +02:00
|
|
|
|
2022-02-07 11:40:23 +01:00
|
|
|
/* Compare with truncated hash, multi-part */
|
|
|
|
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
|
|
|
|
PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
|
|
|
|
TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
|
|
|
|
PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Compare with corrupted value */
|
|
|
|
for( i = 0; i < output_length; i++ )
|
|
|
|
{
|
|
|
|
mbedtls_test_set_step( i );
|
|
|
|
output[i] ^= 1;
|
2022-02-07 11:40:23 +01:00
|
|
|
|
|
|
|
/* One-shot */
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
|
|
|
|
output, output_length ),
|
|
|
|
PSA_ERROR_INVALID_SIGNATURE );
|
2022-02-07 11:40:23 +01:00
|
|
|
|
|
|
|
/* Multi-Part */
|
|
|
|
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
|
|
|
|
PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
|
|
|
|
TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
|
|
|
|
PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
output[i] ^= 1;
|
2020-08-25 23:44:59 +02:00
|
|
|
}
|
2018-08-14 15:17:54 +02:00
|
|
|
|
2018-06-20 16:05:20 +02:00
|
|
|
exit:
|
2022-02-07 11:40:23 +01:00
|
|
|
PSA_ASSERT( psa_hash_abort( &operation ) );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-06-20 16:05:20 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
|
|
|
|
void hash_bad_order( )
|
2018-02-08 10:02:12 +01:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_algorithm_t alg = PSA_ALG_SHA_256;
|
|
|
|
unsigned char input[] = "";
|
|
|
|
/* SHA-256 hash of an empty string */
|
|
|
|
const unsigned char valid_hash[] = {
|
|
|
|
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
|
|
|
|
0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
|
|
|
|
0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
|
|
|
|
unsigned char hash[sizeof(valid_hash)] = { 0 };
|
|
|
|
size_t hash_len;
|
|
|
|
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
2018-06-28 00:07:19 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-02-08 10:02:12 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Call setup twice in a row. */
|
|
|
|
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
|
2021-06-24 12:36:14 +02:00
|
|
|
ASSERT_OPERATION_IS_ACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_hash_setup( &operation, alg ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
2021-06-24 12:36:14 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_hash_abort( &operation ) );
|
2021-06-24 12:36:14 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2018-04-02 17:34:15 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Call update without calling setup beforehand. */
|
|
|
|
TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_hash_abort( &operation ) );
|
2018-06-18 17:03:37 +02:00
|
|
|
|
2021-06-24 12:36:14 +02:00
|
|
|
/* Check that update calls abort on error. */
|
|
|
|
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
|
2021-06-24 19:14:52 +02:00
|
|
|
operation.id = UINT_MAX;
|
2021-06-24 12:36:14 +02:00
|
|
|
ASSERT_OPERATION_IS_ACTIVE( operation );
|
|
|
|
TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
|
|
|
PSA_ASSERT( psa_hash_abort( &operation ) );
|
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Call update after finish. */
|
|
|
|
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
|
|
|
|
PSA_ASSERT( psa_hash_finish( &operation,
|
|
|
|
hash, sizeof( hash ), &hash_len ) );
|
|
|
|
TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_hash_abort( &operation ) );
|
|
|
|
|
|
|
|
/* Call verify without calling setup beforehand. */
|
|
|
|
TEST_EQUAL( psa_hash_verify( &operation,
|
|
|
|
valid_hash, sizeof( valid_hash ) ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_hash_abort( &operation ) );
|
|
|
|
|
|
|
|
/* Call verify after finish. */
|
|
|
|
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
|
|
|
|
PSA_ASSERT( psa_hash_finish( &operation,
|
|
|
|
hash, sizeof( hash ), &hash_len ) );
|
|
|
|
TEST_EQUAL( psa_hash_verify( &operation,
|
|
|
|
valid_hash, sizeof( valid_hash ) ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_hash_abort( &operation ) );
|
|
|
|
|
|
|
|
/* Call verify twice in a row. */
|
|
|
|
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
|
2021-06-24 12:36:14 +02:00
|
|
|
ASSERT_OPERATION_IS_ACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_hash_verify( &operation,
|
|
|
|
valid_hash, sizeof( valid_hash ) ) );
|
2021-06-24 12:36:14 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_hash_verify( &operation,
|
|
|
|
valid_hash, sizeof( valid_hash ) ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
2021-06-24 12:36:14 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_hash_abort( &operation ) );
|
|
|
|
|
|
|
|
/* Call finish without calling setup beforehand. */
|
|
|
|
TEST_EQUAL( psa_hash_finish( &operation,
|
|
|
|
hash, sizeof( hash ), &hash_len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_hash_abort( &operation ) );
|
|
|
|
|
|
|
|
/* Call finish twice in a row. */
|
|
|
|
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
|
|
|
|
PSA_ASSERT( psa_hash_finish( &operation,
|
|
|
|
hash, sizeof( hash ), &hash_len ) );
|
|
|
|
TEST_EQUAL( psa_hash_finish( &operation,
|
|
|
|
hash, sizeof( hash ), &hash_len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_hash_abort( &operation ) );
|
|
|
|
|
|
|
|
/* Call finish after calling verify. */
|
|
|
|
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
|
|
|
|
PSA_ASSERT( psa_hash_verify( &operation,
|
|
|
|
valid_hash, sizeof( valid_hash ) ) );
|
|
|
|
TEST_EQUAL( psa_hash_finish( &operation,
|
|
|
|
hash, sizeof( hash ), &hash_len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_hash_abort( &operation ) );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
|
|
|
|
void hash_verify_bad_args( )
|
|
|
|
{
|
|
|
|
psa_algorithm_t alg = PSA_ALG_SHA_256;
|
|
|
|
/* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
|
|
|
|
* appended to it */
|
|
|
|
unsigned char hash[] = {
|
|
|
|
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
|
|
|
|
0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
|
|
|
|
0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
|
|
|
|
size_t expected_size = PSA_HASH_LENGTH( alg );
|
|
|
|
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
2018-02-08 10:02:12 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
/* psa_hash_verify with a smaller hash than expected */
|
|
|
|
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
|
2021-06-24 12:36:14 +02:00
|
|
|
ASSERT_OPERATION_IS_ACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
|
2020-08-26 00:01:39 +02:00
|
|
|
PSA_ERROR_INVALID_SIGNATURE );
|
2021-06-24 12:36:14 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
|
|
|
PSA_ASSERT( psa_hash_abort( &operation ) );
|
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2020-08-26 00:01:39 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* psa_hash_verify with a non-matching hash */
|
|
|
|
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
|
|
|
|
TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
|
2020-08-26 00:01:39 +02:00
|
|
|
PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* psa_hash_verify with a hash longer than expected */
|
|
|
|
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
|
|
|
|
TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
|
|
|
|
PSA_ERROR_INVALID_SIGNATURE );
|
2020-08-26 00:01:39 +02:00
|
|
|
|
2018-02-08 10:02:12 +01:00
|
|
|
exit:
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-02-08 10:02:12 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
|
|
|
|
void hash_finish_bad_args( )
|
2019-01-04 12:48:27 +01:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_algorithm_t alg = PSA_ALG_SHA_256;
|
|
|
|
unsigned char hash[PSA_HASH_MAX_SIZE];
|
|
|
|
size_t expected_size = PSA_HASH_LENGTH( alg );
|
|
|
|
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
|
|
|
size_t hash_len;
|
2019-01-04 12:48:27 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2019-01-04 12:48:27 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* psa_hash_finish with a smaller hash buffer than expected */
|
|
|
|
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
|
|
|
|
TEST_EQUAL( psa_hash_finish( &operation,
|
|
|
|
hash, expected_size - 1, &hash_len ),
|
|
|
|
PSA_ERROR_BUFFER_TOO_SMALL );
|
2019-02-15 15:12:05 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
exit:
|
|
|
|
PSA_DONE( );
|
2019-01-04 12:48:27 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
|
|
|
|
void hash_clone_source_state( )
|
2018-06-20 16:05:20 +02:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_algorithm_t alg = PSA_ALG_SHA_256;
|
|
|
|
unsigned char hash[PSA_HASH_MAX_SIZE];
|
|
|
|
psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
|
|
|
|
psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
|
|
|
|
psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
|
|
|
|
psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
|
|
|
|
psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
|
|
|
|
size_t hash_len;
|
2018-06-20 16:05:20 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
|
2018-06-20 16:05:20 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
|
|
|
|
PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
|
|
|
|
PSA_ASSERT( psa_hash_finish( &op_finished,
|
|
|
|
hash, sizeof( hash ), &hash_len ) );
|
|
|
|
PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
|
|
|
|
PSA_ASSERT( psa_hash_abort( &op_aborted ) );
|
2018-06-20 16:05:20 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
|
|
|
|
PSA_ASSERT( psa_hash_finish( &op_init,
|
|
|
|
hash, sizeof( hash ), &hash_len ) );
|
|
|
|
PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
|
|
|
|
PSA_ASSERT( psa_hash_finish( &op_finished,
|
|
|
|
hash, sizeof( hash ), &hash_len ) );
|
|
|
|
PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
|
|
|
|
PSA_ASSERT( psa_hash_finish( &op_aborted,
|
|
|
|
hash, sizeof( hash ), &hash_len ) );
|
2019-02-25 17:42:03 +01:00
|
|
|
|
2018-06-20 16:05:20 +02:00
|
|
|
exit:
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_hash_abort( &op_source );
|
|
|
|
psa_hash_abort( &op_init );
|
|
|
|
psa_hash_abort( &op_setup );
|
|
|
|
psa_hash_abort( &op_finished );
|
|
|
|
psa_hash_abort( &op_aborted );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-06-20 16:05:20 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
|
|
|
|
void hash_clone_target_state( )
|
2019-02-15 15:12:05 +01:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_algorithm_t alg = PSA_ALG_SHA_256;
|
|
|
|
unsigned char hash[PSA_HASH_MAX_SIZE];
|
|
|
|
psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
|
|
|
|
psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
|
|
|
|
psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
|
|
|
|
psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
|
|
|
|
psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
|
|
|
|
size_t hash_len;
|
2019-02-15 15:12:05 +01:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
|
|
|
|
PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
|
|
|
|
PSA_ASSERT( psa_hash_finish( &op_finished,
|
|
|
|
hash, sizeof( hash ), &hash_len ) );
|
|
|
|
PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
|
|
|
|
PSA_ASSERT( psa_hash_abort( &op_aborted ) );
|
2019-02-19 10:25:10 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
|
|
|
|
PSA_ASSERT( psa_hash_finish( &op_target,
|
|
|
|
hash, sizeof( hash ), &hash_len ) );
|
2019-02-19 10:25:10 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
|
|
|
|
TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
|
2019-02-15 15:12:05 +01:00
|
|
|
PSA_ERROR_BAD_STATE );
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
|
2019-02-15 15:12:05 +01:00
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
exit:
|
|
|
|
psa_hash_abort( &op_target );
|
|
|
|
psa_hash_abort( &op_init );
|
|
|
|
psa_hash_abort( &op_setup );
|
|
|
|
psa_hash_abort( &op_finished );
|
|
|
|
psa_hash_abort( &op_aborted );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2019-02-15 15:12:05 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mac_operation_init( )
|
|
|
|
{
|
|
|
|
const uint8_t input[1] = { 0 };
|
|
|
|
|
|
|
|
/* Test each valid way of initializing the object, except for `= {0}`, as
|
|
|
|
* Clang 5 complains when `-Wmissing-field-initializers` is used, even
|
|
|
|
* though it's OK by the C standard. We could test for this, but we'd need
|
2021-12-21 06:14:10 +01:00
|
|
|
* to suppress the Clang warning for the test. */
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_mac_operation_t func = psa_mac_operation_init( );
|
|
|
|
psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
|
|
|
|
psa_mac_operation_t zero;
|
2019-02-15 15:12:05 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
memset( &zero, 0, sizeof( zero ) );
|
2019-02-15 15:12:05 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* A freshly-initialized MAC operation should not be usable. */
|
|
|
|
TEST_EQUAL( psa_mac_update( &func,
|
|
|
|
input, sizeof( input ) ),
|
2019-02-15 15:12:05 +01:00
|
|
|
PSA_ERROR_BAD_STATE );
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_mac_update( &init,
|
|
|
|
input, sizeof( input ) ),
|
2019-02-15 15:12:05 +01:00
|
|
|
PSA_ERROR_BAD_STATE );
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_mac_update( &zero,
|
|
|
|
input, sizeof( input ) ),
|
2019-02-15 15:12:05 +01:00
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* A default MAC operation should be abortable without error. */
|
|
|
|
PSA_ASSERT( psa_mac_abort( &func ) );
|
|
|
|
PSA_ASSERT( psa_mac_abort( &init ) );
|
|
|
|
PSA_ASSERT( psa_mac_abort( &zero ) );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2019-02-15 15:12:05 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mac_setup( int key_type_arg,
|
|
|
|
data_t *key,
|
|
|
|
int alg_arg,
|
|
|
|
int expected_status_arg )
|
|
|
|
{
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
|
|
|
#if defined(KNOWN_SUPPORTED_MAC_ALG)
|
|
|
|
const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
|
|
|
|
#endif
|
2019-02-15 15:12:05 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2019-02-15 15:12:05 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
|
|
|
|
&operation, &status ) )
|
|
|
|
goto exit;
|
|
|
|
TEST_EQUAL( status, expected_status );
|
2018-06-20 16:05:20 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* The operation object should be reusable. */
|
|
|
|
#if defined(KNOWN_SUPPORTED_MAC_ALG)
|
|
|
|
if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
|
|
|
|
smoke_test_key_data,
|
|
|
|
sizeof( smoke_test_key_data ),
|
|
|
|
KNOWN_SUPPORTED_MAC_ALG,
|
|
|
|
&operation, &status ) )
|
|
|
|
goto exit;
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
#endif
|
2019-05-28 14:08:50 +02:00
|
|
|
|
2018-06-20 16:05:20 +02:00
|
|
|
exit:
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-06-20 16:05:20 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
|
|
|
|
void mac_bad_order( )
|
2018-02-03 22:44:14 +01:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
|
|
|
|
psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
|
|
|
|
const uint8_t key_data[] = {
|
|
|
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
|
|
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
|
|
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
|
|
|
uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
|
|
|
|
size_t sign_mac_length = 0;
|
|
|
|
const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
|
|
|
|
const uint8_t verify_mac[] = {
|
|
|
|
0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
|
|
|
|
0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
|
|
|
|
0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
|
2018-02-03 22:44:14 +01:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2018-07-05 14:22:45 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
|
2020-08-04 14:58:35 +02:00
|
|
|
&key ) );
|
2018-03-28 12:46:26 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Call update without calling setup beforehand. */
|
|
|
|
TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_mac_abort( &operation ) );
|
2018-03-28 12:46:26 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Call sign finish without calling setup beforehand. */
|
|
|
|
TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
|
|
|
|
&sign_mac_length),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_mac_abort( &operation ) );
|
2020-07-06 14:08:59 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Call verify finish without calling setup beforehand. */
|
|
|
|
TEST_EQUAL( psa_mac_verify_finish( &operation,
|
|
|
|
verify_mac, sizeof( verify_mac ) ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_mac_abort( &operation ) );
|
2018-02-03 23:57:22 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Call setup twice in a row. */
|
|
|
|
PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
|
2021-06-24 12:36:14 +02:00
|
|
|
ASSERT_OPERATION_IS_ACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
2021-06-24 12:36:14 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_mac_abort( &operation ) );
|
2021-06-24 12:36:14 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2021-01-21 12:26:17 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Call update after sign finish. */
|
|
|
|
PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
|
|
|
|
PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
|
|
|
|
PSA_ASSERT( psa_mac_sign_finish( &operation,
|
|
|
|
sign_mac, sizeof( sign_mac ),
|
|
|
|
&sign_mac_length ) );
|
|
|
|
TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_mac_abort( &operation ) );
|
2018-06-08 14:40:59 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Call update after verify finish. */
|
|
|
|
PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
|
|
|
|
PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
|
|
|
|
PSA_ASSERT( psa_mac_verify_finish( &operation,
|
|
|
|
verify_mac, sizeof( verify_mac ) ) );
|
|
|
|
TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_mac_abort( &operation ) );
|
|
|
|
|
|
|
|
/* Call sign finish twice in a row. */
|
|
|
|
PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
|
|
|
|
PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
|
|
|
|
PSA_ASSERT( psa_mac_sign_finish( &operation,
|
|
|
|
sign_mac, sizeof( sign_mac ),
|
|
|
|
&sign_mac_length ) );
|
|
|
|
TEST_EQUAL( psa_mac_sign_finish( &operation,
|
|
|
|
sign_mac, sizeof( sign_mac ),
|
|
|
|
&sign_mac_length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_mac_abort( &operation ) );
|
|
|
|
|
|
|
|
/* Call verify finish twice in a row. */
|
|
|
|
PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
|
|
|
|
PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
|
|
|
|
PSA_ASSERT( psa_mac_verify_finish( &operation,
|
|
|
|
verify_mac, sizeof( verify_mac ) ) );
|
|
|
|
TEST_EQUAL( psa_mac_verify_finish( &operation,
|
|
|
|
verify_mac, sizeof( verify_mac ) ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_mac_abort( &operation ) );
|
|
|
|
|
|
|
|
/* Setup sign but try verify. */
|
|
|
|
PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
|
|
|
|
PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
|
2021-06-23 13:49:59 +02:00
|
|
|
ASSERT_OPERATION_IS_ACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_mac_verify_finish( &operation,
|
|
|
|
verify_mac, sizeof( verify_mac ) ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
2021-06-23 13:49:59 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_mac_abort( &operation ) );
|
2021-06-23 13:49:59 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
|
|
|
|
/* Setup verify but try sign. */
|
|
|
|
PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
|
|
|
|
PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
|
2021-06-23 13:49:59 +02:00
|
|
|
ASSERT_OPERATION_IS_ACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_mac_sign_finish( &operation,
|
|
|
|
sign_mac, sizeof( sign_mac ),
|
|
|
|
&sign_mac_length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
2021-06-23 13:49:59 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_mac_abort( &operation ) );
|
2021-06-23 13:49:59 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_destroy_key( key ) );
|
2018-06-08 14:40:59 +02:00
|
|
|
|
2018-06-06 15:36:50 +02:00
|
|
|
exit:
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-06-06 15:36:50 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-02-28 16:23:59 +01:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mac_sign_verify_multi( int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *input,
|
|
|
|
int is_verify,
|
|
|
|
data_t *expected_mac )
|
|
|
|
{
|
|
|
|
size_t data_part_len = 0;
|
|
|
|
|
|
|
|
for( data_part_len = 1; data_part_len <= input->len; data_part_len++ )
|
|
|
|
{
|
|
|
|
/* Split data into length(data_part_len) parts. */
|
|
|
|
mbedtls_test_set_step( 2000 + data_part_len );
|
|
|
|
|
2022-03-03 16:29:14 +01:00
|
|
|
if( mac_multipart_internal_func( key_type_arg, key_data,
|
|
|
|
alg_arg,
|
|
|
|
input, data_part_len,
|
|
|
|
expected_mac,
|
|
|
|
is_verify, 0 ) == 0 )
|
2022-02-28 16:23:59 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* length(0) part, length(data_part_len) part, length(0) part... */
|
|
|
|
mbedtls_test_set_step( 3000 + data_part_len );
|
|
|
|
|
2022-03-03 16:29:14 +01:00
|
|
|
if( mac_multipart_internal_func( key_type_arg, key_data,
|
|
|
|
alg_arg,
|
|
|
|
input, data_part_len,
|
|
|
|
expected_mac,
|
|
|
|
is_verify, 1 ) == 0 )
|
2022-02-28 16:23:59 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Goto is required to silence warnings about unused labels, as we
|
|
|
|
* don't actually do any test assertions in this function. */
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-06-06 15:36:50 +02:00
|
|
|
/* BEGIN_CASE */
|
2021-06-16 17:52:21 +02:00
|
|
|
void mac_sign( int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *input,
|
|
|
|
data_t *expected_mac )
|
2018-06-06 15:36:50 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-06-06 15:36:50 +02:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
uint8_t *actual_mac = NULL;
|
|
|
|
size_t mac_buffer_size =
|
|
|
|
PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
|
|
|
|
size_t mac_length = 0;
|
|
|
|
const size_t output_sizes_to_test[] = {
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
expected_mac->len - 1,
|
|
|
|
expected_mac->len,
|
|
|
|
expected_mac->len + 1,
|
|
|
|
};
|
|
|
|
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE );
|
2021-06-16 17:52:21 +02:00
|
|
|
/* We expect PSA_MAC_LENGTH to be exact. */
|
|
|
|
TEST_ASSERT( expected_mac->len == mac_buffer_size );
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2018-07-05 14:22:45 +02:00
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
|
2020-07-06 14:08:59 +02:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
const size_t output_size = output_sizes_to_test[i];
|
|
|
|
psa_status_t expected_status =
|
|
|
|
( output_size >= expected_mac->len ? PSA_SUCCESS :
|
|
|
|
PSA_ERROR_BUFFER_TOO_SMALL );
|
2021-01-21 12:26:17 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
mbedtls_test_set_step( output_size );
|
|
|
|
ASSERT_ALLOC( actual_mac, output_size );
|
2021-01-21 12:26:17 +01:00
|
|
|
|
2021-03-01 15:35:48 +01:00
|
|
|
/* Calculate the MAC, one-shot case. */
|
|
|
|
TEST_EQUAL( psa_mac_compute( key, alg,
|
|
|
|
input->x, input->len,
|
|
|
|
actual_mac, output_size, &mac_length ),
|
|
|
|
expected_status );
|
|
|
|
if( expected_status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
ASSERT_COMPARE( expected_mac->x, expected_mac->len,
|
|
|
|
actual_mac, mac_length );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( output_size > 0 )
|
|
|
|
memset( actual_mac, 0, output_size );
|
|
|
|
|
|
|
|
/* Calculate the MAC, multi-part case. */
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
|
|
|
|
PSA_ASSERT( psa_mac_update( &operation,
|
|
|
|
input->x, input->len ) );
|
|
|
|
TEST_EQUAL( psa_mac_sign_finish( &operation,
|
|
|
|
actual_mac, output_size,
|
|
|
|
&mac_length ),
|
|
|
|
expected_status );
|
|
|
|
PSA_ASSERT( psa_mac_abort( &operation ) );
|
2018-06-06 15:19:24 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
if( expected_status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
ASSERT_COMPARE( expected_mac->x, expected_mac->len,
|
|
|
|
actual_mac, mac_length );
|
|
|
|
}
|
|
|
|
mbedtls_free( actual_mac );
|
|
|
|
actual_mac = NULL;
|
|
|
|
}
|
2018-02-03 22:44:14 +01:00
|
|
|
|
|
|
|
exit:
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_mac_abort( &operation );
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2021-06-16 17:52:21 +02:00
|
|
|
mbedtls_free( actual_mac );
|
2018-02-03 22:44:14 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2021-06-16 17:52:21 +02:00
|
|
|
void mac_verify( int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *input,
|
|
|
|
data_t *expected_mac )
|
2018-02-03 22:44:14 +01:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-02-03 22:44:14 +01:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
uint8_t *perturbed_mac = NULL;
|
|
|
|
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE );
|
2018-02-03 22:44:14 +01:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-02-03 22:44:14 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2018-07-05 14:22:45 +02:00
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
2018-03-28 12:46:26 +02:00
|
|
|
|
2021-03-01 15:35:48 +01:00
|
|
|
/* Verify correct MAC, one-shot case. */
|
|
|
|
PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
|
|
|
|
expected_mac->x, expected_mac->len ) );
|
|
|
|
|
|
|
|
/* Verify correct MAC, multi-part case. */
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
|
|
|
|
PSA_ASSERT( psa_mac_update( &operation,
|
|
|
|
input->x, input->len ) );
|
|
|
|
PSA_ASSERT( psa_mac_verify_finish( &operation,
|
|
|
|
expected_mac->x,
|
|
|
|
expected_mac->len ) );
|
2018-03-28 12:46:26 +02:00
|
|
|
|
2021-03-01 15:35:48 +01:00
|
|
|
/* Test a MAC that's too short, one-shot case. */
|
|
|
|
TEST_EQUAL( psa_mac_verify( key, alg,
|
|
|
|
input->x, input->len,
|
|
|
|
expected_mac->x,
|
|
|
|
expected_mac->len - 1 ),
|
|
|
|
PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
|
|
|
|
/* Test a MAC that's too short, multi-part case. */
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
|
|
|
|
PSA_ASSERT( psa_mac_update( &operation,
|
|
|
|
input->x, input->len ) );
|
|
|
|
TEST_EQUAL( psa_mac_verify_finish( &operation,
|
|
|
|
expected_mac->x,
|
|
|
|
expected_mac->len - 1 ),
|
|
|
|
PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
|
2021-03-01 15:35:48 +01:00
|
|
|
/* Test a MAC that's too long, one-shot case. */
|
2021-06-16 17:52:21 +02:00
|
|
|
ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
|
|
|
|
memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
|
2021-03-01 15:35:48 +01:00
|
|
|
TEST_EQUAL( psa_mac_verify( key, alg,
|
|
|
|
input->x, input->len,
|
|
|
|
perturbed_mac, expected_mac->len + 1 ),
|
|
|
|
PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
|
|
|
|
/* Test a MAC that's too long, multi-part case. */
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
|
|
|
|
PSA_ASSERT( psa_mac_update( &operation,
|
|
|
|
input->x, input->len ) );
|
|
|
|
TEST_EQUAL( psa_mac_verify_finish( &operation,
|
|
|
|
perturbed_mac,
|
|
|
|
expected_mac->len + 1 ),
|
|
|
|
PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
|
|
|
|
/* Test changing one byte. */
|
|
|
|
for( size_t i = 0; i < expected_mac->len; i++ )
|
2020-09-07 17:14:14 +02:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
mbedtls_test_set_step( i );
|
|
|
|
perturbed_mac[i] ^= 1;
|
2021-03-01 15:35:48 +01:00
|
|
|
|
|
|
|
TEST_EQUAL( psa_mac_verify( key, alg,
|
|
|
|
input->x, input->len,
|
|
|
|
perturbed_mac, expected_mac->len ),
|
|
|
|
PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
|
|
|
|
PSA_ASSERT( psa_mac_update( &operation,
|
|
|
|
input->x, input->len ) );
|
|
|
|
TEST_EQUAL( psa_mac_verify_finish( &operation,
|
|
|
|
perturbed_mac,
|
|
|
|
expected_mac->len ),
|
|
|
|
PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
perturbed_mac[i] ^= 1;
|
2020-07-06 14:08:59 +02:00
|
|
|
}
|
2018-03-28 12:46:26 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
exit:
|
|
|
|
psa_mac_abort( &operation );
|
|
|
|
psa_destroy_key( key );
|
|
|
|
PSA_DONE( );
|
|
|
|
mbedtls_free( perturbed_mac );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-01-21 12:26:17 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void cipher_operation_init( )
|
|
|
|
{
|
|
|
|
const uint8_t input[1] = { 0 };
|
|
|
|
unsigned char output[1] = { 0 };
|
|
|
|
size_t output_length;
|
|
|
|
/* Test each valid way of initializing the object, except for `= {0}`, as
|
|
|
|
* Clang 5 complains when `-Wmissing-field-initializers` is used, even
|
|
|
|
* though it's OK by the C standard. We could test for this, but we'd need
|
2021-12-21 06:14:10 +01:00
|
|
|
* to suppress the Clang warning for the test. */
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_cipher_operation_t func = psa_cipher_operation_init( );
|
|
|
|
psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
|
|
|
|
psa_cipher_operation_t zero;
|
2021-01-21 12:26:17 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
memset( &zero, 0, sizeof( zero ) );
|
2018-06-06 15:19:24 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* A freshly-initialized cipher operation should not be usable. */
|
|
|
|
TEST_EQUAL( psa_cipher_update( &func,
|
|
|
|
input, sizeof( input ),
|
|
|
|
output, sizeof( output ),
|
|
|
|
&output_length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
TEST_EQUAL( psa_cipher_update( &init,
|
|
|
|
input, sizeof( input ),
|
|
|
|
output, sizeof( output ),
|
|
|
|
&output_length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
TEST_EQUAL( psa_cipher_update( &zero,
|
|
|
|
input, sizeof( input ),
|
|
|
|
output, sizeof( output ),
|
|
|
|
&output_length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
2018-02-03 22:44:14 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* A default cipher operation should be abortable without error. */
|
|
|
|
PSA_ASSERT( psa_cipher_abort( &func ) );
|
|
|
|
PSA_ASSERT( psa_cipher_abort( &init ) );
|
|
|
|
PSA_ASSERT( psa_cipher_abort( &zero ) );
|
2018-02-03 22:44:14 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-03-28 00:21:33 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2021-06-16 17:52:21 +02:00
|
|
|
void cipher_setup( int key_type_arg,
|
|
|
|
data_t *key,
|
|
|
|
int alg_arg,
|
|
|
|
int expected_status_arg )
|
2018-03-28 00:21:33 +02:00
|
|
|
{
|
2018-06-06 15:19:24 +02:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2018-06-21 09:25:10 +02:00
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2019-01-04 12:48:27 +01:00
|
|
|
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_status_t status;
|
|
|
|
#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
|
|
|
|
const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
|
|
|
|
#endif
|
2018-04-30 17:06:50 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-03-28 00:21:33 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
|
|
|
|
&operation, &status ) )
|
|
|
|
goto exit;
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( status, expected_status );
|
2018-06-06 15:19:24 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* The operation object should be reusable. */
|
|
|
|
#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
|
|
|
|
if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
|
|
|
|
smoke_test_key_data,
|
|
|
|
sizeof( smoke_test_key_data ),
|
|
|
|
KNOWN_SUPPORTED_CIPHER_ALG,
|
|
|
|
&operation, &status ) )
|
|
|
|
goto exit;
|
|
|
|
TEST_EQUAL( status, PSA_SUCCESS );
|
|
|
|
#endif
|
2018-04-30 17:06:50 +02:00
|
|
|
|
2018-03-28 00:21:33 +02:00
|
|
|
exit:
|
2020-08-25 23:15:20 +02:00
|
|
|
psa_cipher_abort( &operation );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-03-28 00:21:33 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
|
|
|
|
void cipher_bad_order( )
|
2018-03-28 14:14:59 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_key_type_t key_type = PSA_KEY_TYPE_AES;
|
|
|
|
psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
|
|
|
unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
|
|
|
|
const uint8_t key_data[] = {
|
|
|
|
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
|
|
|
|
0xaa, 0xaa, 0xaa, 0xaa };
|
|
|
|
const uint8_t text[] = {
|
|
|
|
0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
|
|
|
|
0xbb, 0xbb, 0xbb, 0xbb };
|
|
|
|
uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
|
|
|
|
size_t length = 0;
|
2018-03-28 14:14:59 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
|
2020-08-04 14:58:35 +02:00
|
|
|
&key ) );
|
2018-06-06 15:19:24 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Call encrypt setup twice in a row. */
|
|
|
|
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
|
2021-06-24 12:36:14 +02:00
|
|
|
ASSERT_OPERATION_IS_ACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
2021-06-24 12:36:14 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation ) );
|
2021-06-24 12:36:14 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2018-06-06 15:19:24 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Call decrypt setup twice in a row. */
|
|
|
|
PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
|
2021-06-24 12:36:14 +02:00
|
|
|
ASSERT_OPERATION_IS_ACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
2021-06-24 12:36:14 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation ) );
|
2021-06-24 12:36:14 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Generate an IV without calling setup beforehand. */
|
|
|
|
TEST_EQUAL( psa_cipher_generate_iv( &operation,
|
|
|
|
buffer, sizeof( buffer ),
|
|
|
|
&length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation ) );
|
2021-01-21 12:26:17 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Generate an IV twice in a row. */
|
|
|
|
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
|
|
|
|
PSA_ASSERT( psa_cipher_generate_iv( &operation,
|
|
|
|
buffer, sizeof( buffer ),
|
|
|
|
&length ) );
|
2021-06-23 13:49:59 +02:00
|
|
|
ASSERT_OPERATION_IS_ACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_cipher_generate_iv( &operation,
|
|
|
|
buffer, sizeof( buffer ),
|
|
|
|
&length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
2021-06-23 13:49:59 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation ) );
|
2021-06-23 13:49:59 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2018-06-06 18:44:09 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Generate an IV after it's already set. */
|
|
|
|
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
|
|
|
|
PSA_ASSERT( psa_cipher_set_iv( &operation,
|
|
|
|
iv, sizeof( iv ) ) );
|
|
|
|
TEST_EQUAL( psa_cipher_generate_iv( &operation,
|
|
|
|
buffer, sizeof( buffer ),
|
|
|
|
&length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation ) );
|
2018-06-06 15:19:24 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Set an IV without calling setup beforehand. */
|
|
|
|
TEST_EQUAL( psa_cipher_set_iv( &operation,
|
|
|
|
iv, sizeof( iv ) ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation ) );
|
2018-06-06 15:19:24 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Set an IV after it's already set. */
|
|
|
|
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
|
|
|
|
PSA_ASSERT( psa_cipher_set_iv( &operation,
|
|
|
|
iv, sizeof( iv ) ) );
|
2021-06-23 13:49:59 +02:00
|
|
|
ASSERT_OPERATION_IS_ACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_cipher_set_iv( &operation,
|
|
|
|
iv, sizeof( iv ) ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
2021-06-23 13:49:59 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation ) );
|
2021-06-23 13:49:59 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2018-06-06 15:19:24 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Set an IV after it's already generated. */
|
|
|
|
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
|
|
|
|
PSA_ASSERT( psa_cipher_generate_iv( &operation,
|
|
|
|
buffer, sizeof( buffer ),
|
|
|
|
&length ) );
|
|
|
|
TEST_EQUAL( psa_cipher_set_iv( &operation,
|
|
|
|
iv, sizeof( iv ) ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation ) );
|
2020-07-06 14:08:59 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Call update without calling setup beforehand. */
|
|
|
|
TEST_EQUAL( psa_cipher_update( &operation,
|
|
|
|
text, sizeof( text ),
|
|
|
|
buffer, sizeof( buffer ),
|
|
|
|
&length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation ) );
|
|
|
|
|
|
|
|
/* Call update without an IV where an IV is required. */
|
2021-06-23 13:48:52 +02:00
|
|
|
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
|
2021-06-23 13:49:59 +02:00
|
|
|
ASSERT_OPERATION_IS_ACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_cipher_update( &operation,
|
|
|
|
text, sizeof( text ),
|
|
|
|
buffer, sizeof( buffer ),
|
|
|
|
&length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
2021-06-23 13:49:59 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation ) );
|
2021-06-23 13:49:59 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2021-01-21 12:26:17 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Call update after finish. */
|
|
|
|
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
|
|
|
|
PSA_ASSERT( psa_cipher_set_iv( &operation,
|
|
|
|
iv, sizeof( iv ) ) );
|
|
|
|
PSA_ASSERT( psa_cipher_finish( &operation,
|
|
|
|
buffer, sizeof( buffer ), &length ) );
|
|
|
|
TEST_EQUAL( psa_cipher_update( &operation,
|
|
|
|
text, sizeof( text ),
|
|
|
|
buffer, sizeof( buffer ),
|
|
|
|
&length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation ) );
|
2018-06-06 15:19:24 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Call finish without calling setup beforehand. */
|
|
|
|
TEST_EQUAL( psa_cipher_finish( &operation,
|
|
|
|
buffer, sizeof( buffer ), &length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation ) );
|
2018-06-06 18:44:09 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Call finish without an IV where an IV is required. */
|
|
|
|
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
|
|
|
|
/* Not calling update means we are encrypting an empty buffer, which is OK
|
|
|
|
* for cipher modes with padding. */
|
2021-06-23 13:49:59 +02:00
|
|
|
ASSERT_OPERATION_IS_ACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_cipher_finish( &operation,
|
|
|
|
buffer, sizeof( buffer ), &length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
2021-06-23 13:49:59 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation ) );
|
2021-06-23 13:49:59 +02:00
|
|
|
ASSERT_OPERATION_IS_INACTIVE( operation );
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* Call finish twice in a row. */
|
|
|
|
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
|
|
|
|
PSA_ASSERT( psa_cipher_set_iv( &operation,
|
|
|
|
iv, sizeof( iv ) ) );
|
|
|
|
PSA_ASSERT( psa_cipher_finish( &operation,
|
|
|
|
buffer, sizeof( buffer ), &length ) );
|
|
|
|
TEST_EQUAL( psa_cipher_finish( &operation,
|
|
|
|
buffer, sizeof( buffer ), &length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_destroy_key( key ) );
|
2018-06-06 15:36:50 +02:00
|
|
|
|
|
|
|
exit:
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_cipher_abort( &operation );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-06-06 15:36:50 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2021-06-25 15:49:14 +02:00
|
|
|
void cipher_encrypt_fail( int alg_arg,
|
|
|
|
int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
data_t *input,
|
|
|
|
int expected_status_arg )
|
2018-06-06 15:36:50 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_status_t status;
|
2018-06-06 15:36:50 +02:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2022-02-07 15:19:29 +01:00
|
|
|
unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0};
|
|
|
|
size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
|
|
|
|
size_t iv_length = 0;
|
2021-06-16 17:52:21 +02:00
|
|
|
unsigned char *output = NULL;
|
|
|
|
size_t output_buffer_size = 0;
|
2021-03-01 15:11:46 +01:00
|
|
|
size_t output_length = 0;
|
2022-02-07 15:19:29 +01:00
|
|
|
size_t function_output_length;
|
|
|
|
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
2021-03-01 15:11:46 +01:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
|
|
|
if ( PSA_ERROR_BAD_STATE != expected_status )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
|
|
|
|
input->len );
|
|
|
|
ASSERT_ALLOC( output, output_buffer_size );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
}
|
|
|
|
|
2022-02-07 15:19:29 +01:00
|
|
|
/* Encrypt, one-shot */
|
2021-03-01 15:11:46 +01:00
|
|
|
status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
|
|
|
|
output_buffer_size, &output_length );
|
|
|
|
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
|
2022-02-07 15:19:29 +01:00
|
|
|
/* Encrypt, multi-part */
|
|
|
|
status = psa_cipher_encrypt_setup( &operation, key, alg );
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
if( alg != PSA_ALG_ECB_NO_PADDING )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_cipher_generate_iv( &operation,
|
|
|
|
iv, iv_size,
|
|
|
|
&iv_length ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_cipher_update( &operation, input->x, input->len,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length );
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
output_length += function_output_length;
|
|
|
|
|
|
|
|
status = psa_cipher_finish( &operation, output + output_length,
|
|
|
|
output_buffer_size - output_length,
|
|
|
|
&function_output_length );
|
|
|
|
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
}
|
|
|
|
|
2021-03-01 15:11:46 +01:00
|
|
|
exit:
|
2022-02-07 15:19:29 +01:00
|
|
|
psa_cipher_abort( &operation );
|
2021-03-01 15:11:46 +01:00
|
|
|
mbedtls_free( output );
|
|
|
|
psa_destroy_key( key );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-10-21 10:04:57 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
|
|
|
|
data_t *input, int iv_length,
|
|
|
|
int expected_result )
|
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
size_t output_buffer_size = 0;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
|
|
|
|
output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
|
|
|
|
ASSERT_ALLOC( output, output_buffer_size );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
|
|
|
|
TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
|
|
|
|
iv_length ) );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_cipher_abort( &operation );
|
|
|
|
mbedtls_free( output );
|
|
|
|
psa_destroy_key( key );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-03-01 15:11:46 +01:00
|
|
|
/* BEGIN_CASE */
|
2022-04-20 17:07:52 +02:00
|
|
|
void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data,
|
|
|
|
data_t *plaintext, data_t *ciphertext )
|
2021-03-01 15:11:46 +01:00
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-07-15 09:38:11 +02:00
|
|
|
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
|
|
|
uint8_t iv[1] = { 0x5a };
|
2021-03-01 15:11:46 +01:00
|
|
|
unsigned char *output = NULL;
|
|
|
|
size_t output_buffer_size = 0;
|
2022-04-20 17:09:38 +02:00
|
|
|
size_t output_length, length;
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2022-04-20 16:55:03 +02:00
|
|
|
/* Validate size macros */
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( ciphertext->len,
|
|
|
|
PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) );
|
|
|
|
TEST_LE_U( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ),
|
2022-04-20 17:07:52 +02:00
|
|
|
PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( plaintext->len,
|
|
|
|
PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) );
|
|
|
|
TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ),
|
|
|
|
PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) );
|
2018-07-05 14:22:45 +02:00
|
|
|
|
2021-03-01 15:11:46 +01:00
|
|
|
|
2022-04-20 16:55:03 +02:00
|
|
|
/* Set up key and output buffer */
|
2022-04-20 17:07:52 +02:00
|
|
|
psa_set_key_usage_flags( &attributes,
|
|
|
|
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
2022-04-20 17:07:52 +02:00
|
|
|
output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
|
|
|
|
plaintext->len );
|
2022-04-20 16:55:03 +02:00
|
|
|
ASSERT_ALLOC( output, output_buffer_size );
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2022-04-20 16:55:03 +02:00
|
|
|
/* set_iv() is not allowed */
|
2021-07-15 09:38:11 +02:00
|
|
|
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
|
|
|
|
TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
2022-04-20 17:07:52 +02:00
|
|
|
PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
|
2021-07-15 09:38:11 +02:00
|
|
|
TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
2022-04-20 16:55:03 +02:00
|
|
|
|
|
|
|
/* generate_iv() is not allowed */
|
2021-07-15 09:38:11 +02:00
|
|
|
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
|
|
|
|
TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
|
2022-04-20 17:09:38 +02:00
|
|
|
&length ),
|
2022-04-20 17:07:52 +02:00
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
|
2021-07-15 09:38:11 +02:00
|
|
|
TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
|
2022-04-20 17:09:38 +02:00
|
|
|
&length ),
|
2021-07-15 09:38:11 +02:00
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
2022-04-20 17:09:38 +02:00
|
|
|
/* Multipart encryption */
|
2022-02-07 14:51:37 +01:00
|
|
|
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
|
2022-04-20 17:09:38 +02:00
|
|
|
output_length = 0;
|
|
|
|
length = ~0;
|
|
|
|
PSA_ASSERT( psa_cipher_update( &operation,
|
|
|
|
plaintext->x, plaintext->len,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&length ) );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( length, output_buffer_size );
|
2022-04-20 17:09:38 +02:00
|
|
|
output_length += length;
|
|
|
|
PSA_ASSERT( psa_cipher_finish( &operation,
|
|
|
|
output + output_length,
|
|
|
|
output_buffer_size - output_length,
|
|
|
|
&length ) );
|
|
|
|
output_length += length;
|
|
|
|
ASSERT_COMPARE( ciphertext->x, ciphertext->len,
|
|
|
|
output, output_length );
|
2022-02-07 14:51:37 +01:00
|
|
|
|
2022-04-20 17:09:38 +02:00
|
|
|
/* Multipart encryption */
|
|
|
|
PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
|
|
|
|
output_length = 0;
|
|
|
|
length = ~0;
|
|
|
|
PSA_ASSERT( psa_cipher_update( &operation,
|
|
|
|
ciphertext->x, ciphertext->len,
|
2022-02-07 14:51:37 +01:00
|
|
|
output, output_buffer_size,
|
2022-04-20 17:09:38 +02:00
|
|
|
&length ) );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( length, output_buffer_size );
|
2022-04-20 17:09:38 +02:00
|
|
|
output_length += length;
|
|
|
|
PSA_ASSERT( psa_cipher_finish( &operation,
|
|
|
|
output + output_length,
|
|
|
|
output_buffer_size - output_length,
|
|
|
|
&length ) );
|
|
|
|
output_length += length;
|
|
|
|
ASSERT_COMPARE( plaintext->x, plaintext->len,
|
|
|
|
output, output_length );
|
2022-02-07 14:51:37 +01:00
|
|
|
|
2022-04-20 16:55:03 +02:00
|
|
|
/* One-shot encryption */
|
2022-04-20 17:07:52 +02:00
|
|
|
output_length = ~0;
|
|
|
|
PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&output_length ) );
|
|
|
|
ASSERT_COMPARE( ciphertext->x, ciphertext->len,
|
|
|
|
output, output_length );
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2022-04-20 17:07:52 +02:00
|
|
|
/* One-shot decryption */
|
|
|
|
output_length = ~0;
|
|
|
|
PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&output_length ) );
|
|
|
|
ASSERT_COMPARE( plaintext->x, plaintext->len,
|
2022-02-07 14:51:37 +01:00
|
|
|
output, output_length );
|
|
|
|
|
2021-03-01 15:11:46 +01:00
|
|
|
exit:
|
2022-02-07 14:51:37 +01:00
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation ) );
|
2021-03-01 15:11:46 +01:00
|
|
|
mbedtls_free( output );
|
2022-04-20 16:55:03 +02:00
|
|
|
psa_cipher_abort( &operation );
|
2021-03-01 15:11:46 +01:00
|
|
|
psa_destroy_key( key );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2020-07-06 14:08:59 +02:00
|
|
|
|
2021-07-14 13:31:21 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
|
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
|
|
|
psa_status_t status;
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
/* Usage of either of these two size macros would cause divide by zero
|
|
|
|
* with incorrect key types previously. Input length should be irrelevant
|
|
|
|
* here. */
|
|
|
|
TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
|
|
|
|
0 );
|
|
|
|
TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
|
|
|
|
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
|
|
|
|
/* Should fail due to invalid alg type (to support invalid key type).
|
|
|
|
* Encrypt or decrypt will end up in the same place. */
|
|
|
|
status = psa_cipher_encrypt_setup( &operation, key, alg );
|
|
|
|
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_cipher_abort( &operation );
|
|
|
|
psa_destroy_key( key );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-03-01 15:11:46 +01:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void cipher_encrypt_validation( int alg_arg,
|
|
|
|
int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
data_t *input )
|
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
|
|
|
|
unsigned char *output1 = NULL;
|
|
|
|
size_t output1_buffer_size = 0;
|
|
|
|
size_t output1_length = 0;
|
|
|
|
unsigned char *output2 = NULL;
|
|
|
|
size_t output2_buffer_size = 0;
|
|
|
|
size_t output2_length = 0;
|
|
|
|
size_t function_output_length = 0;
|
|
|
|
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
|
|
|
|
output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
|
|
|
|
PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
|
|
|
|
ASSERT_ALLOC( output1, output1_buffer_size );
|
|
|
|
ASSERT_ALLOC( output2, output2_buffer_size );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
|
2021-06-25 15:47:50 +02:00
|
|
|
/* The one-shot cipher encryption uses generated iv so validating
|
|
|
|
the output is not possible. Validating with multipart encryption. */
|
2021-03-01 15:11:46 +01:00
|
|
|
PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
|
|
|
|
output1_buffer_size, &output1_length ) );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( output1_length,
|
|
|
|
PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
|
|
|
|
TEST_LE_U( output1_length,
|
|
|
|
PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
|
2021-03-01 15:11:46 +01:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
|
|
|
|
PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
|
2018-06-06 18:44:09 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_update( &operation,
|
|
|
|
input->x, input->len,
|
2021-03-01 15:11:46 +01:00
|
|
|
output2, output2_buffer_size,
|
2018-12-18 00:18:46 +01:00
|
|
|
&function_output_length ) );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
|
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
|
2021-03-01 15:11:46 +01:00
|
|
|
output2_length += function_output_length;
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2021-03-01 15:11:46 +01:00
|
|
|
PSA_ASSERT( psa_cipher_finish( &operation,
|
|
|
|
output2 + output2_length,
|
|
|
|
output2_buffer_size - output2_length,
|
|
|
|
&function_output_length ) );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
|
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
|
2021-03-01 15:11:46 +01:00
|
|
|
output2_length += function_output_length;
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2021-03-01 15:11:46 +01:00
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation ) );
|
|
|
|
ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
|
|
|
|
output2, output2_length );
|
2018-06-06 15:36:50 +02:00
|
|
|
|
2018-03-28 14:14:59 +02:00
|
|
|
exit:
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_cipher_abort( &operation );
|
2021-03-01 15:11:46 +01:00
|
|
|
mbedtls_free( output1 );
|
|
|
|
mbedtls_free( output2 );
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-03-28 14:14:59 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-04-30 17:06:50 +02:00
|
|
|
|
2018-06-11 19:33:02 +02:00
|
|
|
/* BEGIN_CASE */
|
2021-06-16 17:52:21 +02:00
|
|
|
void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
|
|
|
|
data_t *key_data, data_t *iv,
|
|
|
|
data_t *input,
|
|
|
|
int first_part_size_arg,
|
|
|
|
int output1_length_arg, int output2_length_arg,
|
2021-06-25 18:21:33 +02:00
|
|
|
data_t *expected_output,
|
|
|
|
int expected_status_arg )
|
2018-06-11 19:33:02 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-06-11 19:33:02 +02:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-06-25 18:21:33 +02:00
|
|
|
psa_status_t status;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2021-06-16 17:52:21 +02:00
|
|
|
size_t first_part_size = first_part_size_arg;
|
|
|
|
size_t output1_length = output1_length_arg;
|
|
|
|
size_t output2_length = output2_length_arg;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
size_t output_buffer_size = 0;
|
|
|
|
size_t function_output_length = 0;
|
|
|
|
size_t total_output_length = 0;
|
|
|
|
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-06-11 19:33:02 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-06-11 19:33:02 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_type( &attributes, key_type );
|
2021-02-15 15:19:25 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
2021-02-15 15:19:25 +01:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
|
2018-06-11 19:33:02 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
if( iv->len > 0 )
|
2018-06-11 19:33:02 +02:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
|
|
|
|
}
|
2018-06-11 19:33:02 +02:00
|
|
|
|
2021-03-01 15:11:46 +01:00
|
|
|
output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
|
|
|
|
PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
|
2021-06-16 17:52:21 +02:00
|
|
|
ASSERT_ALLOC( output, output_buffer_size );
|
2019-05-14 16:09:40 +02:00
|
|
|
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( first_part_size, input->len );
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length ) );
|
|
|
|
TEST_ASSERT( function_output_length == output1_length );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
|
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
|
2021-06-16 17:52:21 +02:00
|
|
|
total_output_length += function_output_length;
|
2021-01-21 12:26:17 +01:00
|
|
|
|
2021-06-25 18:21:33 +02:00
|
|
|
if( first_part_size < input->len )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_cipher_update( &operation,
|
|
|
|
input->x + first_part_size,
|
|
|
|
input->len - first_part_size,
|
|
|
|
( output_buffer_size == 0 ? NULL :
|
|
|
|
output + total_output_length ),
|
|
|
|
output_buffer_size - total_output_length,
|
|
|
|
&function_output_length ) );
|
|
|
|
TEST_ASSERT( function_output_length == output2_length );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
|
|
|
|
alg,
|
|
|
|
input->len - first_part_size ) );
|
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
|
2021-06-25 18:21:33 +02:00
|
|
|
total_output_length += function_output_length;
|
|
|
|
}
|
2018-06-18 15:41:12 +02:00
|
|
|
|
2021-06-25 18:21:33 +02:00
|
|
|
status = psa_cipher_finish( &operation,
|
|
|
|
( output_buffer_size == 0 ? NULL :
|
|
|
|
output + total_output_length ),
|
|
|
|
output_buffer_size - total_output_length,
|
|
|
|
&function_output_length );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
|
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
|
2021-06-16 17:52:21 +02:00
|
|
|
total_output_length += function_output_length;
|
2021-06-25 18:21:33 +02:00
|
|
|
TEST_EQUAL( status, expected_status );
|
2021-06-16 17:52:21 +02:00
|
|
|
|
2021-06-25 18:21:33 +02:00
|
|
|
if( expected_status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation ) );
|
|
|
|
|
|
|
|
ASSERT_COMPARE( expected_output->x, expected_output->len,
|
|
|
|
output, total_output_length );
|
|
|
|
}
|
2018-06-18 15:41:12 +02:00
|
|
|
|
2018-06-11 19:33:02 +02:00
|
|
|
exit:
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_cipher_abort( &operation );
|
|
|
|
mbedtls_free( output );
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-06-11 19:33:02 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2021-06-16 17:52:21 +02:00
|
|
|
void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
|
|
|
|
data_t *key_data, data_t *iv,
|
|
|
|
data_t *input,
|
|
|
|
int first_part_size_arg,
|
|
|
|
int output1_length_arg, int output2_length_arg,
|
2021-06-25 18:21:33 +02:00
|
|
|
data_t *expected_output,
|
|
|
|
int expected_status_arg )
|
2018-06-11 19:33:02 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-06-11 19:33:02 +02:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-06-25 18:21:33 +02:00
|
|
|
psa_status_t status;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2021-06-16 17:52:21 +02:00
|
|
|
size_t first_part_size = first_part_size_arg;
|
|
|
|
size_t output1_length = output1_length_arg;
|
|
|
|
size_t output2_length = output2_length_arg;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
size_t output_buffer_size = 0;
|
|
|
|
size_t function_output_length = 0;
|
|
|
|
size_t total_output_length = 0;
|
|
|
|
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-06-11 19:33:02 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-06-11 19:33:02 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2018-06-11 19:33:02 +02:00
|
|
|
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
2020-08-04 14:58:35 +02:00
|
|
|
&key ) );
|
2018-06-11 19:33:02 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
|
2018-06-11 19:33:02 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
if( iv->len > 0 )
|
2021-01-11 19:36:04 +01:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
|
2021-01-11 20:33:20 +01:00
|
|
|
}
|
2021-01-11 19:36:04 +01:00
|
|
|
|
2021-03-01 15:11:46 +01:00
|
|
|
output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
|
|
|
|
PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
|
2021-06-16 17:52:21 +02:00
|
|
|
ASSERT_ALLOC( output, output_buffer_size );
|
|
|
|
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( first_part_size, input->len );
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_update( &operation,
|
|
|
|
input->x, first_part_size,
|
|
|
|
output, output_buffer_size,
|
|
|
|
&function_output_length ) );
|
|
|
|
TEST_ASSERT( function_output_length == output1_length );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
|
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
|
2021-06-16 17:52:21 +02:00
|
|
|
total_output_length += function_output_length;
|
|
|
|
|
2021-06-25 18:21:33 +02:00
|
|
|
if( first_part_size < input->len )
|
2020-09-07 17:14:14 +02:00
|
|
|
{
|
2021-06-25 18:21:33 +02:00
|
|
|
PSA_ASSERT( psa_cipher_update( &operation,
|
|
|
|
input->x + first_part_size,
|
|
|
|
input->len - first_part_size,
|
|
|
|
( output_buffer_size == 0 ? NULL :
|
|
|
|
output + total_output_length ),
|
|
|
|
output_buffer_size - total_output_length,
|
|
|
|
&function_output_length ) );
|
|
|
|
TEST_ASSERT( function_output_length == output2_length );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
|
|
|
|
alg,
|
|
|
|
input->len - first_part_size ) );
|
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
|
2021-06-25 18:21:33 +02:00
|
|
|
total_output_length += function_output_length;
|
2020-07-06 14:08:59 +02:00
|
|
|
}
|
2021-06-16 17:52:21 +02:00
|
|
|
|
2018-06-08 14:20:49 +02:00
|
|
|
status = psa_cipher_finish( &operation,
|
2021-03-24 00:41:51 +01:00
|
|
|
( output_buffer_size == 0 ? NULL :
|
|
|
|
output + total_output_length ),
|
2019-02-19 19:05:33 +01:00
|
|
|
output_buffer_size - total_output_length,
|
2018-06-08 14:20:49 +02:00
|
|
|
&function_output_length );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
|
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
|
2021-06-16 17:52:21 +02:00
|
|
|
total_output_length += function_output_length;
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( status, expected_status );
|
2021-06-16 17:52:21 +02:00
|
|
|
|
2018-06-06 15:36:50 +02:00
|
|
|
if( expected_status == PSA_SUCCESS )
|
|
|
|
{
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation ) );
|
2021-06-25 18:21:33 +02:00
|
|
|
|
2018-09-27 13:57:19 +02:00
|
|
|
ASSERT_COMPARE( expected_output->x, expected_output->len,
|
|
|
|
output, total_output_length );
|
2018-06-06 15:36:50 +02:00
|
|
|
}
|
2018-06-18 15:41:12 +02:00
|
|
|
|
2018-06-11 19:33:02 +02:00
|
|
|
exit:
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_cipher_abort( &operation );
|
|
|
|
mbedtls_free( output );
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-06-11 19:33:02 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2021-06-25 15:49:14 +02:00
|
|
|
void cipher_decrypt_fail( int alg_arg,
|
|
|
|
int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
data_t *iv,
|
|
|
|
data_t *input_arg,
|
|
|
|
int expected_status_arg )
|
2018-06-11 19:33:02 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_status_t status;
|
2018-06-11 19:33:02 +02:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2021-03-01 15:11:46 +01:00
|
|
|
unsigned char *input = NULL;
|
|
|
|
size_t input_buffer_size = 0;
|
2021-06-16 17:52:21 +02:00
|
|
|
unsigned char *output = NULL;
|
2022-02-07 15:41:19 +01:00
|
|
|
unsigned char *output_multi = NULL;
|
2021-06-16 17:52:21 +02:00
|
|
|
size_t output_buffer_size = 0;
|
2021-03-01 15:11:46 +01:00
|
|
|
size_t output_length = 0;
|
2022-02-07 15:41:19 +01:00
|
|
|
size_t function_output_length;
|
|
|
|
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-06-11 19:33:02 +02:00
|
|
|
|
2021-03-01 15:11:46 +01:00
|
|
|
if ( PSA_ERROR_BAD_STATE != expected_status )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-06-11 19:33:02 +02:00
|
|
|
|
2021-03-01 15:11:46 +01:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2018-06-11 19:33:02 +02:00
|
|
|
|
2021-03-01 15:11:46 +01:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
}
|
2021-06-16 17:52:21 +02:00
|
|
|
|
2021-03-01 15:11:46 +01:00
|
|
|
/* Allocate input buffer and copy the iv and the plaintext */
|
|
|
|
input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
|
|
|
|
if ( input_buffer_size > 0 )
|
2021-03-19 18:46:15 +01:00
|
|
|
{
|
2021-03-01 15:11:46 +01:00
|
|
|
ASSERT_ALLOC( input, input_buffer_size );
|
|
|
|
memcpy( input, iv->x, iv->len );
|
|
|
|
memcpy( input + iv->len, input_arg->x, input_arg->len );
|
2021-03-19 18:46:15 +01:00
|
|
|
}
|
2018-06-11 19:33:02 +02:00
|
|
|
|
2021-03-01 15:11:46 +01:00
|
|
|
output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
|
2021-06-16 17:52:21 +02:00
|
|
|
ASSERT_ALLOC( output, output_buffer_size );
|
|
|
|
|
2022-02-07 15:41:19 +01:00
|
|
|
/* Decrypt, one-short */
|
2021-03-01 15:11:46 +01:00
|
|
|
status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
|
|
|
|
output_buffer_size, &output_length );
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( status, expected_status );
|
2021-01-11 19:36:04 +01:00
|
|
|
|
2022-02-07 15:41:19 +01:00
|
|
|
/* Decrypt, multi-part */
|
|
|
|
status = psa_cipher_decrypt_setup( &operation, key, alg );
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg,
|
|
|
|
input_arg->len ) +
|
|
|
|
PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
|
|
|
|
ASSERT_ALLOC( output_multi, output_buffer_size );
|
|
|
|
|
|
|
|
if( iv->len > 0 )
|
|
|
|
{
|
|
|
|
status = psa_cipher_set_iv( &operation, iv->x, iv->len );
|
|
|
|
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
status = psa_cipher_update( &operation,
|
|
|
|
input_arg->x, input_arg->len,
|
|
|
|
output_multi, output_buffer_size,
|
|
|
|
&function_output_length );
|
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
output_length = function_output_length;
|
|
|
|
|
|
|
|
status = psa_cipher_finish( &operation,
|
|
|
|
output_multi + output_length,
|
|
|
|
output_buffer_size - output_length,
|
|
|
|
&function_output_length );
|
|
|
|
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
}
|
|
|
|
|
2021-03-01 15:11:46 +01:00
|
|
|
exit:
|
2022-02-07 15:41:19 +01:00
|
|
|
psa_cipher_abort( &operation );
|
2021-03-01 15:11:46 +01:00
|
|
|
mbedtls_free( input );
|
|
|
|
mbedtls_free( output );
|
2022-02-07 15:41:19 +01:00
|
|
|
mbedtls_free( output_multi );
|
2021-03-01 15:11:46 +01:00
|
|
|
psa_destroy_key( key );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void cipher_decrypt( int alg_arg,
|
|
|
|
int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
data_t *iv,
|
|
|
|
data_t *input_arg,
|
|
|
|
data_t *expected_output )
|
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
unsigned char *input = NULL;
|
|
|
|
size_t input_buffer_size = 0;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
size_t output_buffer_size = 0;
|
|
|
|
size_t output_length = 0;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
/* Allocate input buffer and copy the iv and the plaintext */
|
|
|
|
input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
|
|
|
|
if ( input_buffer_size > 0 )
|
2021-01-11 19:36:04 +01:00
|
|
|
{
|
2021-03-01 15:11:46 +01:00
|
|
|
ASSERT_ALLOC( input, input_buffer_size );
|
|
|
|
memcpy( input, iv->x, iv->len );
|
|
|
|
memcpy( input + iv->len, input_arg->x, input_arg->len );
|
2021-01-11 20:33:20 +01:00
|
|
|
}
|
2021-01-11 19:36:04 +01:00
|
|
|
|
2021-03-01 15:11:46 +01:00
|
|
|
output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
|
|
|
|
ASSERT_ALLOC( output, output_buffer_size );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
|
|
|
|
output_buffer_size, &output_length ) );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( output_length,
|
|
|
|
PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
|
|
|
|
TEST_LE_U( output_length,
|
|
|
|
PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
|
2021-03-01 15:11:46 +01:00
|
|
|
|
|
|
|
ASSERT_COMPARE( expected_output->x, expected_output->len,
|
|
|
|
output, output_length );
|
2018-06-11 19:33:02 +02:00
|
|
|
exit:
|
2021-03-01 15:11:46 +01:00
|
|
|
mbedtls_free( input );
|
2021-06-16 17:52:21 +02:00
|
|
|
mbedtls_free( output );
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-06-11 19:33:02 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-04-27 11:06:22 +02:00
|
|
|
/* BEGIN_CASE */
|
2021-03-01 15:11:46 +01:00
|
|
|
void cipher_verify_output( int alg_arg,
|
|
|
|
int key_type_arg,
|
2021-06-16 17:52:21 +02:00
|
|
|
data_t *key_data,
|
|
|
|
data_t *input )
|
2021-04-27 11:06:22 +02:00
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-06-16 17:52:21 +02:00
|
|
|
unsigned char *output1 = NULL;
|
|
|
|
size_t output1_size = 0;
|
|
|
|
size_t output1_length = 0;
|
|
|
|
unsigned char *output2 = NULL;
|
|
|
|
size_t output2_size = 0;
|
|
|
|
size_t output2_length = 0;
|
2021-04-27 11:06:22 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
|
2021-04-27 11:06:22 +02:00
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
2021-06-16 17:52:21 +02:00
|
|
|
output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
|
|
|
|
ASSERT_ALLOC( output1, output1_size );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-03-01 15:11:46 +01:00
|
|
|
PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
|
|
|
|
output1, output1_size,
|
|
|
|
&output1_length ) );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( output1_length,
|
|
|
|
PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
|
|
|
|
TEST_LE_U( output1_length,
|
|
|
|
PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
output2_size = output1_length;
|
|
|
|
ASSERT_ALLOC( output2, output2_size );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-03-01 15:11:46 +01:00
|
|
|
PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
|
|
|
|
output2, output2_size,
|
|
|
|
&output2_length ) );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( output2_length,
|
|
|
|
PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
|
|
|
|
TEST_LE_U( output2_length,
|
|
|
|
PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
ASSERT_COMPARE( input->x, input->len, output2, output2_length );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
|
|
|
exit:
|
2021-06-16 17:52:21 +02:00
|
|
|
mbedtls_free( output1 );
|
|
|
|
mbedtls_free( output2 );
|
2021-04-27 11:06:22 +02:00
|
|
|
psa_destroy_key( key );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2021-06-16 17:52:21 +02:00
|
|
|
void cipher_verify_output_multipart( int alg_arg,
|
|
|
|
int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
data_t *input,
|
|
|
|
int first_part_size_arg )
|
2021-04-27 11:06:22 +02:00
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-06-16 17:52:21 +02:00
|
|
|
size_t first_part_size = first_part_size_arg;
|
|
|
|
unsigned char iv[16] = {0};
|
|
|
|
size_t iv_size = 16;
|
|
|
|
size_t iv_length = 0;
|
|
|
|
unsigned char *output1 = NULL;
|
|
|
|
size_t output1_buffer_size = 0;
|
|
|
|
size_t output1_length = 0;
|
|
|
|
unsigned char *output2 = NULL;
|
|
|
|
size_t output2_buffer_size = 0;
|
|
|
|
size_t output2_length = 0;
|
|
|
|
size_t function_output_length;
|
|
|
|
psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
|
|
|
|
psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
|
2021-04-27 11:06:22 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
|
2021-04-27 11:06:22 +02:00
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
|
|
|
|
PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
if( alg != PSA_ALG_ECB_NO_PADDING )
|
2021-04-27 11:06:22 +02:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_generate_iv( &operation1,
|
|
|
|
iv, iv_size,
|
|
|
|
&iv_length ) );
|
2021-04-27 11:06:22 +02:00
|
|
|
}
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( output1_buffer_size,
|
|
|
|
PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
|
2021-06-16 17:52:21 +02:00
|
|
|
ASSERT_ALLOC( output1, output1_buffer_size );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( first_part_size, input->len );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
|
|
|
|
output1, output1_buffer_size,
|
|
|
|
&function_output_length ) );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
|
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
|
2021-06-16 17:52:21 +02:00
|
|
|
output1_length += function_output_length;
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_update( &operation1,
|
|
|
|
input->x + first_part_size,
|
|
|
|
input->len - first_part_size,
|
|
|
|
output1, output1_buffer_size,
|
|
|
|
&function_output_length ) );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
|
|
|
|
alg,
|
|
|
|
input->len - first_part_size ) );
|
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
|
2021-06-16 17:52:21 +02:00
|
|
|
output1_length += function_output_length;
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_finish( &operation1,
|
|
|
|
output1 + output1_length,
|
|
|
|
output1_buffer_size - output1_length,
|
|
|
|
&function_output_length ) );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
|
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
|
2021-06-16 17:52:21 +02:00
|
|
|
output1_length += function_output_length;
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation1 ) );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
output2_buffer_size = output1_length;
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( output2_buffer_size,
|
|
|
|
PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
|
|
|
|
TEST_LE_U( output2_buffer_size,
|
|
|
|
PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
|
2021-06-16 17:52:21 +02:00
|
|
|
ASSERT_ALLOC( output2, output2_buffer_size );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
if( iv_length > 0 )
|
2021-04-27 11:06:22 +02:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_set_iv( &operation2,
|
|
|
|
iv, iv_length ) );
|
2021-04-27 11:06:22 +02:00
|
|
|
}
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
|
|
|
|
output2, output2_buffer_size,
|
|
|
|
&function_output_length ) );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
|
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
|
2021-06-16 17:52:21 +02:00
|
|
|
output2_length += function_output_length;
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_update( &operation2,
|
|
|
|
output1 + first_part_size,
|
|
|
|
output1_length - first_part_size,
|
|
|
|
output2, output2_buffer_size,
|
|
|
|
&function_output_length ) );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
|
|
|
|
alg,
|
|
|
|
output1_length - first_part_size ) );
|
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
|
2021-06-16 17:52:21 +02:00
|
|
|
output2_length += function_output_length;
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_finish( &operation2,
|
|
|
|
output2 + output2_length,
|
|
|
|
output2_buffer_size - output2_length,
|
|
|
|
&function_output_length ) );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
|
|
|
|
TEST_LE_U( function_output_length,
|
|
|
|
PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
|
2021-06-16 17:52:21 +02:00
|
|
|
output2_length += function_output_length;
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_cipher_abort( &operation2 ) );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
ASSERT_COMPARE( input->x, input->len, output2, output2_length );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
exit:
|
|
|
|
psa_cipher_abort( &operation1 );
|
|
|
|
psa_cipher_abort( &operation2 );
|
|
|
|
mbedtls_free( output1 );
|
|
|
|
mbedtls_free( output2 );
|
|
|
|
psa_destroy_key( key );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *nonce,
|
|
|
|
data_t *additional_data,
|
|
|
|
data_t *input_data,
|
|
|
|
int expected_result_arg )
|
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
size_t key_bits;
|
|
|
|
unsigned char *output_data = NULL;
|
|
|
|
size_t output_size = 0;
|
|
|
|
size_t output_length = 0;
|
|
|
|
unsigned char *output_data2 = NULL;
|
|
|
|
size_t output_length2 = 0;
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
|
|
|
psa_status_t expected_result = expected_result_arg;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
|
|
|
key_bits = psa_get_key_bits( &attributes );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
|
|
|
|
alg );
|
|
|
|
/* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
|
|
|
|
* should be exact. */
|
|
|
|
if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
|
|
|
|
expected_result != PSA_ERROR_NOT_SUPPORTED )
|
2021-04-27 11:06:22 +02:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( output_size,
|
|
|
|
PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( output_size,
|
|
|
|
PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
|
2021-04-27 11:06:22 +02:00
|
|
|
}
|
2021-06-16 17:52:21 +02:00
|
|
|
ASSERT_ALLOC( output_data, output_size );
|
|
|
|
|
|
|
|
status = psa_aead_encrypt( key, alg,
|
|
|
|
nonce->x, nonce->len,
|
|
|
|
additional_data->x,
|
|
|
|
additional_data->len,
|
|
|
|
input_data->x, input_data->len,
|
|
|
|
output_data, output_size,
|
|
|
|
&output_length );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* If the operation is not supported, just skip and not fail in case the
|
|
|
|
* encryption involves a common limitation of cryptography hardwares and
|
|
|
|
* an alternative implementation. */
|
|
|
|
if( status == PSA_ERROR_NOT_SUPPORTED )
|
2021-04-27 20:10:18 +02:00
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
|
|
|
|
MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
|
2021-04-27 20:10:18 +02:00
|
|
|
}
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( status, expected_result );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
|
|
|
if( PSA_SUCCESS == expected_result )
|
|
|
|
{
|
2021-06-16 17:52:21 +02:00
|
|
|
ASSERT_ALLOC( output_data2, output_length );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
|
|
|
/* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
|
|
|
|
* should be exact. */
|
|
|
|
TEST_EQUAL( input_data->len,
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( input_data->len,
|
|
|
|
PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( psa_aead_decrypt( key, alg,
|
|
|
|
nonce->x, nonce->len,
|
|
|
|
additional_data->x,
|
|
|
|
additional_data->len,
|
|
|
|
output_data, output_length,
|
|
|
|
output_data2, output_length,
|
|
|
|
&output_length2 ),
|
|
|
|
expected_result );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
ASSERT_COMPARE( input_data->x, input_data->len,
|
|
|
|
output_data2, output_length2 );
|
|
|
|
}
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
exit:
|
|
|
|
psa_destroy_key( key );
|
|
|
|
mbedtls_free( output_data );
|
|
|
|
mbedtls_free( output_data2 );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void aead_encrypt( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *nonce,
|
|
|
|
data_t *additional_data,
|
|
|
|
data_t *input_data,
|
|
|
|
data_t *expected_result )
|
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
size_t key_bits;
|
|
|
|
unsigned char *output_data = NULL;
|
|
|
|
size_t output_size = 0;
|
|
|
|
size_t output_length = 0;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
|
|
|
key_bits = psa_get_key_bits( &attributes );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
|
|
|
|
alg );
|
|
|
|
/* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
|
|
|
|
* should be exact. */
|
|
|
|
TEST_EQUAL( output_size,
|
|
|
|
PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( output_size,
|
|
|
|
PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
|
2021-06-16 17:52:21 +02:00
|
|
|
ASSERT_ALLOC( output_data, output_size );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
status = psa_aead_encrypt( key, alg,
|
|
|
|
nonce->x, nonce->len,
|
|
|
|
additional_data->x, additional_data->len,
|
|
|
|
input_data->x, input_data->len,
|
|
|
|
output_data, output_size,
|
|
|
|
&output_length );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* If the operation is not supported, just skip and not fail in case the
|
|
|
|
* encryption involves a common limitation of cryptography hardwares and
|
|
|
|
* an alternative implementation. */
|
|
|
|
if( status == PSA_ERROR_NOT_SUPPORTED )
|
|
|
|
{
|
|
|
|
MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
|
|
|
|
MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
|
2021-04-27 11:06:22 +02:00
|
|
|
}
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
PSA_ASSERT( status );
|
|
|
|
ASSERT_COMPARE( expected_result->x, expected_result->len,
|
|
|
|
output_data, output_length );
|
|
|
|
|
2021-04-27 11:06:22 +02:00
|
|
|
exit:
|
|
|
|
psa_destroy_key( key );
|
|
|
|
mbedtls_free( output_data );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2021-06-16 17:52:21 +02:00
|
|
|
void aead_decrypt( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *nonce,
|
|
|
|
data_t *additional_data,
|
|
|
|
data_t *input_data,
|
|
|
|
data_t *expected_data,
|
|
|
|
int expected_result_arg )
|
2021-04-27 11:06:22 +02:00
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-06-16 17:52:21 +02:00
|
|
|
size_t key_bits;
|
2021-04-27 11:06:22 +02:00
|
|
|
unsigned char *output_data = NULL;
|
|
|
|
size_t output_size = 0;
|
|
|
|
size_t output_length = 0;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t expected_result = expected_result_arg;
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
|
|
|
key_bits = psa_get_key_bits( &attributes );
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
|
|
|
|
alg );
|
|
|
|
if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
|
|
|
|
expected_result != PSA_ERROR_NOT_SUPPORTED )
|
|
|
|
{
|
|
|
|
/* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
|
|
|
|
* should be exact. */
|
|
|
|
TEST_EQUAL( output_size,
|
|
|
|
PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( output_size,
|
|
|
|
PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
|
2021-06-16 17:52:21 +02:00
|
|
|
}
|
2021-04-27 11:06:22 +02:00
|
|
|
ASSERT_ALLOC( output_data, output_size );
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
status = psa_aead_decrypt( key, alg,
|
|
|
|
nonce->x, nonce->len,
|
|
|
|
additional_data->x,
|
|
|
|
additional_data->len,
|
|
|
|
input_data->x, input_data->len,
|
|
|
|
output_data, output_size,
|
|
|
|
&output_length );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
|
|
|
/* If the operation is not supported, just skip and not fail in case the
|
2021-06-16 17:52:21 +02:00
|
|
|
* decryption involves a common limitation of cryptography hardwares and
|
2021-04-27 11:06:22 +02:00
|
|
|
* an alternative implementation. */
|
|
|
|
if( status == PSA_ERROR_NOT_SUPPORTED )
|
|
|
|
{
|
|
|
|
MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
|
|
|
|
MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
TEST_EQUAL( status, expected_result );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
if( expected_result == PSA_SUCCESS )
|
|
|
|
ASSERT_COMPARE( expected_data->x, expected_data->len,
|
|
|
|
output_data, output_length );
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
exit:
|
|
|
|
psa_destroy_key( key );
|
|
|
|
mbedtls_free( output_data );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *nonce,
|
|
|
|
data_t *additional_data,
|
|
|
|
data_t *input_data,
|
2021-07-21 19:46:06 +02:00
|
|
|
int do_set_lengths,
|
|
|
|
data_t *expected_output )
|
2021-06-16 17:52:21 +02:00
|
|
|
{
|
|
|
|
size_t ad_part_len = 0;
|
|
|
|
size_t data_part_len = 0;
|
2021-09-22 13:54:42 +02:00
|
|
|
set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-09-23 19:24:36 +02:00
|
|
|
for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
|
2021-04-27 11:06:22 +02:00
|
|
|
{
|
2021-09-23 19:24:36 +02:00
|
|
|
mbedtls_test_set_step( ad_part_len );
|
2021-09-15 17:40:40 +02:00
|
|
|
|
2021-09-23 19:24:36 +02:00
|
|
|
if( do_set_lengths )
|
|
|
|
{
|
|
|
|
if( ad_part_len & 0x01 )
|
|
|
|
set_lengths_method = SET_LENGTHS_AFTER_NONCE;
|
|
|
|
else
|
|
|
|
set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
|
2021-04-27 11:06:22 +02:00
|
|
|
}
|
2021-09-23 19:24:36 +02:00
|
|
|
|
|
|
|
/* Split ad into length(ad_part_len) parts. */
|
|
|
|
if( !aead_multipart_internal_func( key_type_arg, key_data,
|
|
|
|
alg_arg, nonce,
|
|
|
|
additional_data,
|
|
|
|
ad_part_len,
|
|
|
|
input_data, -1,
|
|
|
|
set_lengths_method,
|
|
|
|
expected_output,
|
|
|
|
1, 0 ) )
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* length(0) part, length(ad_part_len) part, length(0) part... */
|
|
|
|
mbedtls_test_set_step( 1000 + ad_part_len );
|
|
|
|
|
|
|
|
if( !aead_multipart_internal_func( key_type_arg, key_data,
|
|
|
|
alg_arg, nonce,
|
|
|
|
additional_data,
|
|
|
|
ad_part_len,
|
|
|
|
input_data, -1,
|
|
|
|
set_lengths_method,
|
|
|
|
expected_output,
|
|
|
|
1, 1 ) )
|
|
|
|
break;
|
2021-04-27 11:06:22 +02:00
|
|
|
}
|
|
|
|
|
2021-09-23 19:24:36 +02:00
|
|
|
for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
|
2021-06-16 17:52:21 +02:00
|
|
|
{
|
2021-09-23 19:24:36 +02:00
|
|
|
/* Split data into length(data_part_len) parts. */
|
|
|
|
mbedtls_test_set_step( 2000 + data_part_len );
|
2021-09-15 17:40:40 +02:00
|
|
|
|
2021-09-23 19:24:36 +02:00
|
|
|
if( do_set_lengths )
|
|
|
|
{
|
|
|
|
if( data_part_len & 0x01 )
|
|
|
|
set_lengths_method = SET_LENGTHS_AFTER_NONCE;
|
|
|
|
else
|
|
|
|
set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
|
2021-04-27 11:06:22 +02:00
|
|
|
}
|
|
|
|
|
2021-09-23 19:24:36 +02:00
|
|
|
if( !aead_multipart_internal_func( key_type_arg, key_data,
|
|
|
|
alg_arg, nonce,
|
|
|
|
additional_data, -1,
|
|
|
|
input_data, data_part_len,
|
|
|
|
set_lengths_method,
|
|
|
|
expected_output,
|
|
|
|
1, 0 ) )
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* length(0) part, length(data_part_len) part, length(0) part... */
|
|
|
|
mbedtls_test_set_step( 3000 + data_part_len );
|
|
|
|
|
|
|
|
if( !aead_multipart_internal_func( key_type_arg, key_data,
|
|
|
|
alg_arg, nonce,
|
|
|
|
additional_data, -1,
|
|
|
|
input_data, data_part_len,
|
|
|
|
set_lengths_method,
|
|
|
|
expected_output,
|
|
|
|
1, 1 ) )
|
|
|
|
break;
|
|
|
|
}
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-23 17:06:01 +02:00
|
|
|
/* Goto is required to silence warnings about unused labels, as we
|
|
|
|
* don't actually do any test assertions in this function. */
|
|
|
|
goto exit;
|
2021-06-16 17:52:21 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-06-16 17:52:21 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *nonce,
|
|
|
|
data_t *additional_data,
|
|
|
|
data_t *input_data,
|
2021-07-21 19:46:06 +02:00
|
|
|
int do_set_lengths,
|
2021-09-17 20:19:02 +02:00
|
|
|
data_t *expected_output )
|
2021-06-16 17:52:21 +02:00
|
|
|
{
|
|
|
|
size_t ad_part_len = 0;
|
|
|
|
size_t data_part_len = 0;
|
2021-09-22 13:54:42 +02:00
|
|
|
set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
|
2021-06-16 17:52:21 +02:00
|
|
|
|
2021-09-23 19:24:36 +02:00
|
|
|
for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
|
2021-04-27 20:10:18 +02:00
|
|
|
{
|
2021-09-23 19:24:36 +02:00
|
|
|
/* Split ad into length(ad_part_len) parts. */
|
|
|
|
mbedtls_test_set_step( ad_part_len );
|
2021-09-15 17:40:40 +02:00
|
|
|
|
2021-09-23 19:24:36 +02:00
|
|
|
if( do_set_lengths )
|
|
|
|
{
|
|
|
|
if( ad_part_len & 0x01 )
|
|
|
|
set_lengths_method = SET_LENGTHS_AFTER_NONCE;
|
|
|
|
else
|
|
|
|
set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
|
2021-06-16 17:52:21 +02:00
|
|
|
}
|
2021-09-23 19:24:36 +02:00
|
|
|
|
|
|
|
if( !aead_multipart_internal_func( key_type_arg, key_data,
|
|
|
|
alg_arg, nonce,
|
|
|
|
additional_data,
|
|
|
|
ad_part_len,
|
|
|
|
input_data, -1,
|
|
|
|
set_lengths_method,
|
|
|
|
expected_output,
|
|
|
|
0, 0 ) )
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* length(0) part, length(ad_part_len) part, length(0) part... */
|
|
|
|
mbedtls_test_set_step( 1000 + ad_part_len );
|
|
|
|
|
|
|
|
if( !aead_multipart_internal_func( key_type_arg, key_data,
|
|
|
|
alg_arg, nonce,
|
|
|
|
additional_data,
|
|
|
|
ad_part_len,
|
|
|
|
input_data, -1,
|
|
|
|
set_lengths_method,
|
|
|
|
expected_output,
|
|
|
|
0, 1 ) )
|
|
|
|
break;
|
2021-04-27 20:10:18 +02:00
|
|
|
}
|
2021-04-27 11:06:22 +02:00
|
|
|
|
2021-09-23 19:24:36 +02:00
|
|
|
for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
|
2021-04-27 11:06:22 +02:00
|
|
|
{
|
2021-09-23 19:24:36 +02:00
|
|
|
/* Split data into length(data_part_len) parts. */
|
|
|
|
mbedtls_test_set_step( 2000 + data_part_len );
|
2021-09-15 17:40:40 +02:00
|
|
|
|
2021-09-23 19:24:36 +02:00
|
|
|
if( do_set_lengths )
|
|
|
|
{
|
|
|
|
if( data_part_len & 0x01 )
|
|
|
|
set_lengths_method = SET_LENGTHS_AFTER_NONCE;
|
|
|
|
else
|
|
|
|
set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
|
2021-06-16 17:52:21 +02:00
|
|
|
}
|
2021-09-23 19:24:36 +02:00
|
|
|
|
|
|
|
if( !aead_multipart_internal_func( key_type_arg, key_data,
|
|
|
|
alg_arg, nonce,
|
|
|
|
additional_data, -1,
|
|
|
|
input_data, data_part_len,
|
|
|
|
set_lengths_method,
|
|
|
|
expected_output,
|
|
|
|
0, 0 ) )
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* length(0) part, length(data_part_len) part, length(0) part... */
|
|
|
|
mbedtls_test_set_step( 3000 + data_part_len );
|
|
|
|
|
|
|
|
if( !aead_multipart_internal_func( key_type_arg, key_data,
|
|
|
|
alg_arg, nonce,
|
|
|
|
additional_data, -1,
|
|
|
|
input_data, data_part_len,
|
|
|
|
set_lengths_method,
|
|
|
|
expected_output,
|
|
|
|
0, 1 ) )
|
|
|
|
break;
|
2021-04-27 11:06:22 +02:00
|
|
|
}
|
|
|
|
|
2021-06-23 17:06:01 +02:00
|
|
|
/* Goto is required to silence warnings about unused labels, as we
|
|
|
|
* don't actually do any test assertions in this function. */
|
2021-06-16 17:52:21 +02:00
|
|
|
goto exit;
|
2021-04-27 11:06:22 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-06-04 17:42:21 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg,
|
2021-08-24 19:11:37 +02:00
|
|
|
int nonce_length,
|
|
|
|
int expected_nonce_length_arg,
|
2021-06-23 18:14:40 +02:00
|
|
|
data_t *additional_data,
|
|
|
|
data_t *input_data,
|
|
|
|
int expected_status_arg )
|
2021-06-04 17:42:21 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-09-22 17:44:21 +02:00
|
|
|
psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
|
2021-06-04 17:42:21 +02:00
|
|
|
uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
2021-07-23 18:40:41 +02:00
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2021-08-24 19:11:37 +02:00
|
|
|
size_t actual_nonce_length = 0;
|
|
|
|
size_t expected_nonce_length = expected_nonce_length_arg;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
unsigned char *ciphertext = NULL;
|
2021-06-23 18:14:40 +02:00
|
|
|
size_t output_size = 0;
|
2021-08-24 19:11:37 +02:00
|
|
|
size_t ciphertext_size = 0;
|
|
|
|
size_t ciphertext_length = 0;
|
2021-06-23 18:14:40 +02:00
|
|
|
size_t tag_length = 0;
|
|
|
|
uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
|
2021-06-04 17:42:21 +02:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
|
|
|
|
psa_set_key_algorithm( & attributes, alg );
|
|
|
|
psa_set_key_type( & attributes, key_type );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
|
|
|
|
2021-06-23 18:14:40 +02:00
|
|
|
output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
|
|
|
|
|
2021-08-24 19:11:37 +02:00
|
|
|
ASSERT_ALLOC( output, output_size );
|
2021-06-23 18:14:40 +02:00
|
|
|
|
2021-08-24 19:11:37 +02:00
|
|
|
ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
|
2021-06-23 18:14:40 +02:00
|
|
|
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
|
2021-06-23 18:14:40 +02:00
|
|
|
|
2021-08-24 19:11:37 +02:00
|
|
|
ASSERT_ALLOC( ciphertext, ciphertext_size );
|
2021-06-23 18:14:40 +02:00
|
|
|
|
2021-06-04 17:42:21 +02:00
|
|
|
status = psa_aead_encrypt_setup( &operation, key, alg );
|
|
|
|
|
|
|
|
/* If the operation is not supported, just skip and not fail in case the
|
|
|
|
* encryption involves a common limitation of cryptography hardwares and
|
|
|
|
* an alternative implementation. */
|
|
|
|
if( status == PSA_ERROR_NOT_SUPPORTED )
|
|
|
|
{
|
|
|
|
MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
|
2021-08-24 19:11:37 +02:00
|
|
|
MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
|
2021-06-04 17:42:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
|
|
|
|
status = psa_aead_generate_nonce( &operation, nonce_buffer,
|
2021-08-24 19:11:37 +02:00
|
|
|
nonce_length,
|
|
|
|
&actual_nonce_length );
|
2021-06-04 17:42:21 +02:00
|
|
|
|
2021-07-23 18:40:41 +02:00
|
|
|
TEST_EQUAL( status, expected_status );
|
2021-06-23 18:14:40 +02:00
|
|
|
|
2021-08-24 19:11:37 +02:00
|
|
|
TEST_EQUAL( actual_nonce_length, expected_nonce_length );
|
2021-07-16 19:20:16 +02:00
|
|
|
|
2021-09-22 18:23:03 +02:00
|
|
|
if( expected_status == PSA_SUCCESS )
|
|
|
|
TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
|
|
|
|
alg ) );
|
|
|
|
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE );
|
2021-07-16 19:52:03 +02:00
|
|
|
|
2021-07-23 18:40:41 +02:00
|
|
|
if( expected_status == PSA_SUCCESS )
|
2021-06-23 18:14:40 +02:00
|
|
|
{
|
|
|
|
/* Ensure we can still complete operation. */
|
2021-10-06 22:49:41 +02:00
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len ) );
|
2021-06-23 18:14:40 +02:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
|
|
|
|
additional_data->len ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
|
2021-08-24 19:11:37 +02:00
|
|
|
output, output_size,
|
|
|
|
&ciphertext_length ) );
|
2021-06-23 18:14:40 +02:00
|
|
|
|
2021-08-24 19:11:37 +02:00
|
|
|
PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
|
|
|
|
&ciphertext_length, tag_buffer,
|
2021-06-23 18:14:40 +02:00
|
|
|
PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
|
|
|
|
}
|
2021-06-04 17:42:21 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( key );
|
2021-08-24 19:11:37 +02:00
|
|
|
mbedtls_free( output );
|
|
|
|
mbedtls_free( ciphertext );
|
2021-06-04 17:42:21 +02:00
|
|
|
psa_aead_abort( &operation );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-07-23 18:28:31 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg,
|
2021-09-10 17:21:22 +02:00
|
|
|
int nonce_length_arg,
|
2021-12-25 17:21:47 +01:00
|
|
|
int set_lengths_method_arg,
|
2021-07-23 18:28:31 +02:00
|
|
|
data_t *additional_data,
|
|
|
|
data_t *input_data,
|
|
|
|
int expected_status_arg )
|
|
|
|
{
|
|
|
|
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-09-22 17:44:21 +02:00
|
|
|
psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
|
2021-07-23 18:28:31 +02:00
|
|
|
uint8_t *nonce_buffer = NULL;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2021-08-25 13:57:18 +02:00
|
|
|
unsigned char *output = NULL;
|
|
|
|
unsigned char *ciphertext = NULL;
|
2021-09-10 17:21:22 +02:00
|
|
|
size_t nonce_length;
|
2021-07-23 18:28:31 +02:00
|
|
|
size_t output_size = 0;
|
2021-08-25 13:57:18 +02:00
|
|
|
size_t ciphertext_size = 0;
|
|
|
|
size_t ciphertext_length = 0;
|
2021-07-23 18:28:31 +02:00
|
|
|
size_t tag_length = 0;
|
|
|
|
uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
|
2021-09-10 17:21:22 +02:00
|
|
|
size_t index = 0;
|
2021-12-25 17:21:47 +01:00
|
|
|
set_lengths_method_t set_lengths_method = set_lengths_method_arg;
|
2021-07-23 18:28:31 +02:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
|
|
|
|
|
|
|
output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
|
|
|
|
|
2021-08-25 13:57:18 +02:00
|
|
|
ASSERT_ALLOC( output, output_size );
|
2021-07-23 18:28:31 +02:00
|
|
|
|
2021-08-25 13:57:18 +02:00
|
|
|
ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
|
2021-07-23 18:28:31 +02:00
|
|
|
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
|
2021-07-23 18:28:31 +02:00
|
|
|
|
2021-08-25 13:57:18 +02:00
|
|
|
ASSERT_ALLOC( ciphertext, ciphertext_size );
|
2021-07-23 18:28:31 +02:00
|
|
|
|
|
|
|
status = psa_aead_encrypt_setup( &operation, key, alg );
|
|
|
|
|
|
|
|
/* If the operation is not supported, just skip and not fail in case the
|
|
|
|
* encryption involves a common limitation of cryptography hardwares and
|
|
|
|
* an alternative implementation. */
|
|
|
|
if( status == PSA_ERROR_NOT_SUPPORTED )
|
|
|
|
{
|
|
|
|
MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
|
2021-09-10 17:21:22 +02:00
|
|
|
MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
|
2021-07-23 18:28:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
|
2021-09-10 17:21:22 +02:00
|
|
|
/* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
|
|
|
|
if( nonce_length_arg == -1 )
|
2021-07-23 18:28:31 +02:00
|
|
|
{
|
2021-08-25 18:24:37 +02:00
|
|
|
/* Arbitrary size buffer, to test zero length valid buffer. */
|
|
|
|
ASSERT_ALLOC( nonce_buffer, 4 );
|
2021-09-10 17:21:22 +02:00
|
|
|
nonce_length = 0;
|
2021-08-16 19:42:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-10 17:21:22 +02:00
|
|
|
/* If length is zero, then this will return NULL. */
|
|
|
|
nonce_length = ( size_t ) nonce_length_arg;
|
2021-08-25 13:57:18 +02:00
|
|
|
ASSERT_ALLOC( nonce_buffer, nonce_length );
|
2021-08-16 19:42:41 +02:00
|
|
|
|
2021-09-10 17:21:22 +02:00
|
|
|
if( nonce_buffer )
|
2021-08-16 19:42:41 +02:00
|
|
|
{
|
2021-09-10 17:21:22 +02:00
|
|
|
for( index = 0; index < nonce_length - 1; ++index )
|
|
|
|
{
|
|
|
|
nonce_buffer[index] = 'a' + index;
|
|
|
|
}
|
2021-08-16 19:42:41 +02:00
|
|
|
}
|
2021-07-23 18:28:31 +02:00
|
|
|
}
|
|
|
|
|
2021-12-25 17:21:47 +01:00
|
|
|
if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len ) );
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:57:18 +02:00
|
|
|
status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
|
2021-07-23 18:28:31 +02:00
|
|
|
|
2021-07-23 18:40:41 +02:00
|
|
|
TEST_EQUAL( status, expected_status );
|
2021-07-23 18:28:31 +02:00
|
|
|
|
|
|
|
if( expected_status == PSA_SUCCESS )
|
|
|
|
{
|
2021-12-25 17:21:47 +01:00
|
|
|
if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len ) );
|
|
|
|
}
|
|
|
|
if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
|
|
|
|
expected_status = PSA_ERROR_BAD_STATE;
|
2021-07-23 18:28:31 +02:00
|
|
|
|
2021-12-25 17:21:47 +01:00
|
|
|
/* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
|
|
|
|
TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
|
|
|
|
additional_data->len ),
|
|
|
|
expected_status );
|
2021-07-23 18:28:31 +02:00
|
|
|
|
2021-12-25 17:21:47 +01:00
|
|
|
TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
|
2021-08-25 13:57:18 +02:00
|
|
|
output, output_size,
|
2021-12-25 17:21:47 +01:00
|
|
|
&ciphertext_length ),
|
|
|
|
expected_status );
|
2021-07-23 18:28:31 +02:00
|
|
|
|
2021-12-25 17:21:47 +01:00
|
|
|
TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
|
2021-08-25 13:57:18 +02:00
|
|
|
&ciphertext_length, tag_buffer,
|
2021-12-25 17:21:47 +01:00
|
|
|
PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
|
|
|
|
expected_status );
|
2021-07-23 18:28:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( key );
|
2021-08-25 13:57:18 +02:00
|
|
|
mbedtls_free( output );
|
|
|
|
mbedtls_free( ciphertext );
|
2021-07-23 18:28:31 +02:00
|
|
|
mbedtls_free( nonce_buffer );
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-07-23 19:30:59 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg,
|
2021-09-01 13:04:23 +02:00
|
|
|
int output_size_arg,
|
2021-07-23 19:30:59 +02:00
|
|
|
data_t *nonce,
|
|
|
|
data_t *additional_data,
|
|
|
|
data_t *input_data,
|
|
|
|
int expected_status_arg )
|
|
|
|
{
|
|
|
|
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-09-22 17:44:21 +02:00
|
|
|
psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
|
2021-07-23 19:30:59 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2021-09-01 13:04:23 +02:00
|
|
|
unsigned char *output = NULL;
|
|
|
|
unsigned char *ciphertext = NULL;
|
|
|
|
size_t output_size = output_size_arg;
|
|
|
|
size_t ciphertext_size = 0;
|
|
|
|
size_t ciphertext_length = 0;
|
2021-07-23 19:30:59 +02:00
|
|
|
size_t tag_length = 0;
|
|
|
|
uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
|
|
|
|
2021-09-01 13:04:23 +02:00
|
|
|
ASSERT_ALLOC( output, output_size );
|
2021-07-23 19:30:59 +02:00
|
|
|
|
2021-09-01 13:04:23 +02:00
|
|
|
ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
|
2021-07-23 19:30:59 +02:00
|
|
|
|
2021-09-01 13:04:23 +02:00
|
|
|
ASSERT_ALLOC( ciphertext, ciphertext_size );
|
2021-07-23 19:30:59 +02:00
|
|
|
|
|
|
|
status = psa_aead_encrypt_setup( &operation, key, alg );
|
|
|
|
|
|
|
|
/* If the operation is not supported, just skip and not fail in case the
|
|
|
|
* encryption involves a common limitation of cryptography hardwares and
|
|
|
|
* an alternative implementation. */
|
|
|
|
if( status == PSA_ERROR_NOT_SUPPORTED )
|
|
|
|
{
|
|
|
|
MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
|
|
|
|
MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
|
|
|
|
}
|
|
|
|
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
|
2021-10-07 16:04:57 +02:00
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len ) );
|
|
|
|
|
2021-07-23 19:30:59 +02:00
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
|
|
|
|
additional_data->len ) );
|
|
|
|
|
|
|
|
status = psa_aead_update( &operation, input_data->x, input_data->len,
|
2021-09-01 13:04:23 +02:00
|
|
|
output, output_size, &ciphertext_length );
|
2021-07-23 19:30:59 +02:00
|
|
|
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
|
|
|
|
if( expected_status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
/* Ensure we can still complete operation. */
|
2021-09-01 13:04:23 +02:00
|
|
|
PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
|
|
|
|
&ciphertext_length, tag_buffer,
|
2021-07-23 19:30:59 +02:00
|
|
|
PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( key );
|
2021-09-01 13:04:23 +02:00
|
|
|
mbedtls_free( output );
|
|
|
|
mbedtls_free( ciphertext );
|
2021-07-23 19:30:59 +02:00
|
|
|
psa_aead_abort( &operation );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-07-23 19:52:31 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg,
|
2021-09-10 19:36:00 +02:00
|
|
|
int finish_ciphertext_size_arg,
|
2021-09-13 19:27:22 +02:00
|
|
|
int tag_size_arg,
|
2021-07-23 19:52:31 +02:00
|
|
|
data_t *nonce,
|
|
|
|
data_t *additional_data,
|
|
|
|
data_t *input_data,
|
|
|
|
int expected_status_arg )
|
|
|
|
{
|
|
|
|
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-09-22 17:44:21 +02:00
|
|
|
psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
|
2021-07-23 19:52:31 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2021-09-10 19:36:00 +02:00
|
|
|
unsigned char *ciphertext = NULL;
|
|
|
|
unsigned char *finish_ciphertext = NULL;
|
2021-09-13 19:27:22 +02:00
|
|
|
unsigned char *tag_buffer = NULL;
|
2021-09-10 19:36:00 +02:00
|
|
|
size_t ciphertext_size = 0;
|
|
|
|
size_t ciphertext_length = 0;
|
|
|
|
size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
|
2021-09-13 19:27:22 +02:00
|
|
|
size_t tag_size = ( size_t ) tag_size_arg;
|
2021-07-23 19:52:31 +02:00
|
|
|
size_t tag_length = 0;
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
|
|
|
|
2021-09-10 19:36:00 +02:00
|
|
|
ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
|
2021-07-23 19:52:31 +02:00
|
|
|
|
2021-09-10 19:36:00 +02:00
|
|
|
ASSERT_ALLOC( ciphertext, ciphertext_size );
|
2021-07-23 19:52:31 +02:00
|
|
|
|
2021-09-10 19:36:00 +02:00
|
|
|
ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
|
2021-07-23 19:52:31 +02:00
|
|
|
|
2021-09-13 19:27:22 +02:00
|
|
|
ASSERT_ALLOC( tag_buffer, tag_size );
|
|
|
|
|
2021-07-23 19:52:31 +02:00
|
|
|
status = psa_aead_encrypt_setup( &operation, key, alg );
|
|
|
|
|
|
|
|
/* If the operation is not supported, just skip and not fail in case the
|
|
|
|
* encryption involves a common limitation of cryptography hardwares and
|
|
|
|
* an alternative implementation. */
|
|
|
|
if( status == PSA_ERROR_NOT_SUPPORTED )
|
|
|
|
{
|
|
|
|
MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
|
|
|
|
MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
|
|
|
|
}
|
|
|
|
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
2021-10-07 18:07:23 +02:00
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len ) );
|
|
|
|
|
2021-07-23 19:52:31 +02:00
|
|
|
PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
|
|
|
|
additional_data->len ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
|
2021-09-10 19:36:00 +02:00
|
|
|
ciphertext, ciphertext_size, &ciphertext_length ) );
|
2021-07-23 19:52:31 +02:00
|
|
|
|
|
|
|
/* Ensure we can still complete operation. */
|
2021-09-10 19:36:00 +02:00
|
|
|
status = psa_aead_finish( &operation, finish_ciphertext,
|
|
|
|
finish_ciphertext_size,
|
|
|
|
&ciphertext_length, tag_buffer,
|
2021-09-13 19:27:22 +02:00
|
|
|
tag_size, &tag_length );
|
2021-07-23 19:52:31 +02:00
|
|
|
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( key );
|
2021-09-10 19:36:00 +02:00
|
|
|
mbedtls_free( ciphertext );
|
|
|
|
mbedtls_free( finish_ciphertext );
|
2021-09-20 10:42:21 +02:00
|
|
|
mbedtls_free( tag_buffer );
|
2021-07-23 19:52:31 +02:00
|
|
|
psa_aead_abort( &operation );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-07-23 19:30:59 +02:00
|
|
|
|
2021-09-17 20:19:02 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void aead_multipart_verify( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *nonce,
|
|
|
|
data_t *additional_data,
|
|
|
|
data_t *input_data,
|
|
|
|
data_t *tag,
|
2021-09-19 14:11:50 +02:00
|
|
|
int tag_usage_arg,
|
2021-12-19 17:00:12 +01:00
|
|
|
int expected_setup_status_arg,
|
2021-09-17 20:19:02 +02:00
|
|
|
int expected_status_arg )
|
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-09-22 17:44:21 +02:00
|
|
|
psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
|
2021-09-17 20:19:02 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2021-12-19 17:00:12 +01:00
|
|
|
psa_status_t expected_setup_status = expected_setup_status_arg;
|
2021-09-17 20:19:02 +02:00
|
|
|
unsigned char *plaintext = NULL;
|
|
|
|
unsigned char *finish_plaintext = NULL;
|
|
|
|
size_t plaintext_size = 0;
|
|
|
|
size_t plaintext_length = 0;
|
|
|
|
size_t verify_plaintext_size = 0;
|
2021-09-22 13:54:42 +02:00
|
|
|
tag_usage_method_t tag_usage = tag_usage_arg;
|
2021-09-19 14:11:50 +02:00
|
|
|
unsigned char *tag_buffer = NULL;
|
|
|
|
size_t tag_size = 0;
|
2021-09-17 20:19:02 +02:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
|
|
|
|
|
|
|
plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
|
|
|
|
input_data->len );
|
|
|
|
|
|
|
|
ASSERT_ALLOC( plaintext, plaintext_size );
|
|
|
|
|
|
|
|
verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
|
|
|
|
|
|
|
|
ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
|
|
|
|
|
|
|
|
status = psa_aead_decrypt_setup( &operation, key, alg );
|
|
|
|
|
|
|
|
/* If the operation is not supported, just skip and not fail in case the
|
|
|
|
* encryption involves a common limitation of cryptography hardwares and
|
|
|
|
* an alternative implementation. */
|
|
|
|
if( status == PSA_ERROR_NOT_SUPPORTED )
|
|
|
|
{
|
|
|
|
MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
|
|
|
|
MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
|
|
|
|
}
|
2021-12-19 17:00:12 +01:00
|
|
|
TEST_EQUAL( status, expected_setup_status );
|
|
|
|
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
2021-09-17 20:19:02 +02:00
|
|
|
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
2021-10-06 18:15:02 +02:00
|
|
|
status = psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len );
|
2021-12-19 17:00:12 +01:00
|
|
|
PSA_ASSERT( status );
|
2021-10-06 18:15:02 +02:00
|
|
|
|
2021-09-17 20:19:02 +02:00
|
|
|
PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
|
|
|
|
additional_data->len ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_update( &operation, input_data->x,
|
|
|
|
input_data->len,
|
|
|
|
plaintext, plaintext_size,
|
|
|
|
&plaintext_length ) );
|
|
|
|
|
2021-09-19 14:11:50 +02:00
|
|
|
if( tag_usage == USE_GIVEN_TAG )
|
|
|
|
{
|
|
|
|
tag_buffer = tag->x;
|
|
|
|
tag_size = tag->len;
|
|
|
|
}
|
|
|
|
|
2021-09-17 20:19:02 +02:00
|
|
|
status = psa_aead_verify( &operation, finish_plaintext,
|
|
|
|
verify_plaintext_size,
|
|
|
|
&plaintext_length,
|
2021-09-19 14:11:50 +02:00
|
|
|
tag_buffer, tag_size );
|
2021-09-17 20:19:02 +02:00
|
|
|
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( key );
|
|
|
|
mbedtls_free( plaintext );
|
|
|
|
mbedtls_free( finish_plaintext );
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-09-19 18:33:03 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void aead_multipart_setup( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg, int expected_status_arg )
|
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-09-22 17:44:21 +02:00
|
|
|
psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
|
2021-09-19 18:33:03 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes,
|
|
|
|
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
|
|
|
|
status = psa_aead_encrypt_setup( &operation, key, alg );
|
|
|
|
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
|
|
|
status = psa_aead_decrypt_setup( &operation, key, alg );
|
|
|
|
|
|
|
|
TEST_EQUAL(status, expected_status );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( key );
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-06-21 19:32:46 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void aead_multipart_state_test( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *nonce,
|
|
|
|
data_t *additional_data,
|
|
|
|
data_t *input_data )
|
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-09-22 17:44:21 +02:00
|
|
|
psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
|
2021-06-21 19:32:46 +02:00
|
|
|
unsigned char *output_data = NULL;
|
|
|
|
unsigned char *final_data = NULL;
|
|
|
|
size_t output_size = 0;
|
|
|
|
size_t finish_output_size = 0;
|
|
|
|
size_t output_length = 0;
|
|
|
|
size_t key_bits = 0;
|
|
|
|
size_t tag_length = 0;
|
|
|
|
size_t tag_size = 0;
|
|
|
|
size_t nonce_length = 0;
|
|
|
|
uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
|
|
|
|
uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
|
|
|
|
size_t output_part_length = 0;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( & attributes,
|
|
|
|
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
|
|
|
|
psa_set_key_algorithm( & attributes, alg );
|
|
|
|
psa_set_key_type( & attributes, key_type );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
|
|
|
key_bits = psa_get_key_bits( &attributes );
|
|
|
|
|
|
|
|
tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
|
|
|
|
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE );
|
2021-06-21 19:32:46 +02:00
|
|
|
|
|
|
|
output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
|
|
|
|
|
|
|
|
ASSERT_ALLOC( output_data, output_size );
|
|
|
|
|
|
|
|
finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
|
|
|
|
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
|
2021-06-21 19:32:46 +02:00
|
|
|
|
|
|
|
ASSERT_ALLOC( final_data, finish_output_size );
|
|
|
|
|
|
|
|
/* Test all operations error without calling setup first. */
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
|
|
|
|
PSA_AEAD_NONCE_MAX_SIZE,
|
|
|
|
&nonce_length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-07-16 18:38:47 +02:00
|
|
|
/* ------------------------------------------------------- */
|
|
|
|
|
2021-06-21 19:32:46 +02:00
|
|
|
TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-07-16 18:38:47 +02:00
|
|
|
/* ------------------------------------------------------- */
|
|
|
|
|
2021-06-21 19:32:46 +02:00
|
|
|
TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
|
|
|
|
additional_data->len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-07-16 18:38:47 +02:00
|
|
|
/* ------------------------------------------------------- */
|
|
|
|
|
2021-06-21 19:32:46 +02:00
|
|
|
TEST_EQUAL( psa_aead_update( &operation, input_data->x,
|
|
|
|
input_data->len, output_data,
|
|
|
|
output_size, &output_length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-07-16 18:38:47 +02:00
|
|
|
/* ------------------------------------------------------- */
|
|
|
|
|
2021-06-21 19:32:46 +02:00
|
|
|
TEST_EQUAL( psa_aead_finish( &operation, final_data,
|
|
|
|
finish_output_size,
|
|
|
|
&output_part_length,
|
|
|
|
tag_buffer, tag_length,
|
|
|
|
&tag_size ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-07-16 18:38:47 +02:00
|
|
|
/* ------------------------------------------------------- */
|
|
|
|
|
2021-06-21 19:32:46 +02:00
|
|
|
TEST_EQUAL( psa_aead_verify( &operation, final_data,
|
|
|
|
finish_output_size,
|
|
|
|
&output_part_length,
|
|
|
|
tag_buffer,
|
|
|
|
tag_length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
|
|
|
/* Test for double setups. */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-07-16 18:38:47 +02:00
|
|
|
/* ------------------------------------------------------- */
|
|
|
|
|
2021-06-21 19:32:46 +02:00
|
|
|
PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-07-16 18:53:40 +02:00
|
|
|
/* ------------------------------------------------------- */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
|
|
|
/* ------------------------------------------------------- */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-06-21 19:32:46 +02:00
|
|
|
/* Test for not setting a nonce. */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
|
|
|
|
additional_data->len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-09-01 13:08:29 +02:00
|
|
|
/* ------------------------------------------------------- */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_update( &operation, input_data->x,
|
|
|
|
input_data->len, output_data,
|
|
|
|
output_size, &output_length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-09-21 19:37:10 +02:00
|
|
|
/* ------------------------------------------------------- */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_finish( &operation, final_data,
|
|
|
|
finish_output_size,
|
|
|
|
&output_part_length,
|
|
|
|
tag_buffer, tag_length,
|
|
|
|
&tag_size ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
|
|
|
/* ------------------------------------------------------- */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_verify( &operation, final_data,
|
|
|
|
finish_output_size,
|
|
|
|
&output_part_length,
|
|
|
|
tag_buffer,
|
|
|
|
tag_length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-06-21 19:32:46 +02:00
|
|
|
/* Test for double setting nonce. */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-07-16 18:53:40 +02:00
|
|
|
/* Test for double generating nonce. */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
|
|
|
|
PSA_AEAD_NONCE_MAX_SIZE,
|
|
|
|
&nonce_length ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
|
|
|
|
PSA_AEAD_NONCE_MAX_SIZE,
|
|
|
|
&nonce_length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
|
|
|
/* Test for generate nonce then set and vice versa */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
|
|
|
|
PSA_AEAD_NONCE_MAX_SIZE,
|
|
|
|
&nonce_length ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-12-15 15:28:49 +01:00
|
|
|
/* Test for generating nonce after calling set lengths */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
|
|
|
|
PSA_AEAD_NONCE_MAX_SIZE,
|
|
|
|
&nonce_length ) );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2022-01-19 18:44:49 +01:00
|
|
|
/* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
|
2021-12-15 15:28:49 +01:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
if( operation.alg == PSA_ALG_CCM )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
|
|
|
|
input_data->len ),
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
|
|
|
|
PSA_AEAD_NONCE_MAX_SIZE,
|
|
|
|
&nonce_length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
|
|
|
|
input_data->len ) );
|
|
|
|
PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
|
|
|
|
PSA_AEAD_NONCE_MAX_SIZE,
|
|
|
|
&nonce_length ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2022-01-19 18:44:49 +01:00
|
|
|
/* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
|
2021-12-15 15:28:49 +01:00
|
|
|
#if SIZE_MAX > UINT32_MAX
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
|
|
|
|
input_data->len ),
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
|
|
|
|
PSA_AEAD_NONCE_MAX_SIZE,
|
|
|
|
&nonce_length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
|
|
|
|
input_data->len ) );
|
|
|
|
PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
|
|
|
|
PSA_AEAD_NONCE_MAX_SIZE,
|
|
|
|
&nonce_length ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
#endif
|
|
|
|
|
2022-01-19 18:44:49 +01:00
|
|
|
/* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
|
2021-12-15 15:28:49 +01:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
|
|
|
|
PSA_AEAD_NONCE_MAX_SIZE,
|
|
|
|
&nonce_length ) );
|
|
|
|
|
|
|
|
if( operation.alg == PSA_ALG_CCM )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
|
|
|
|
input_data->len ),
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
|
|
|
|
input_data->len ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-12-26 01:00:20 +01:00
|
|
|
/* ------------------------------------------------------- */
|
2021-12-25 23:50:53 +01:00
|
|
|
/* Test for setting nonce after calling set lengths */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-12-26 01:00:20 +01:00
|
|
|
/* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
|
2021-12-25 23:50:53 +01:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
if( operation.alg == PSA_ALG_CCM )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
|
|
|
|
input_data->len ),
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
|
|
|
|
input_data->len ) );
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-12-26 01:00:20 +01:00
|
|
|
/* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
|
2021-12-25 23:50:53 +01:00
|
|
|
#if SIZE_MAX > UINT32_MAX
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
|
|
|
|
input_data->len ),
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
|
|
|
|
input_data->len ) );
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
#endif
|
|
|
|
|
2022-01-19 18:44:49 +01:00
|
|
|
/* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
|
2021-12-25 23:50:53 +01:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
|
|
|
if( operation.alg == PSA_ALG_CCM )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
|
|
|
|
input_data->len ),
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
|
|
|
|
input_data->len ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
2021-12-15 15:28:49 +01:00
|
|
|
|
2022-01-19 18:44:49 +01:00
|
|
|
/* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
|
2021-12-26 01:00:20 +01:00
|
|
|
#if SIZE_MAX > UINT32_MAX
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
if( operation.alg == PSA_ALG_GCM )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
SIZE_MAX ),
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
|
|
|
else if ( operation.alg != PSA_ALG_CCM )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
SIZE_MAX ) );
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2022-01-19 18:44:49 +01:00
|
|
|
/* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
|
2021-12-26 01:00:20 +01:00
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
|
|
|
if( operation.alg == PSA_ALG_GCM )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
SIZE_MAX ),
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
}
|
|
|
|
else if ( operation.alg != PSA_ALG_CCM )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
SIZE_MAX ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
#endif
|
2021-12-15 15:28:49 +01:00
|
|
|
|
2021-07-16 18:53:40 +02:00
|
|
|
/* ------------------------------------------------------- */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
|
|
|
|
PSA_AEAD_NONCE_MAX_SIZE,
|
|
|
|
&nonce_length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-06-22 18:25:57 +02:00
|
|
|
/* Test for generating nonce in decrypt setup. */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
|
|
|
|
PSA_AEAD_NONCE_MAX_SIZE,
|
|
|
|
&nonce_length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-06-21 19:32:46 +02:00
|
|
|
/* Test for setting lengths twice. */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
2021-06-23 19:13:04 +02:00
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-12-15 15:28:49 +01:00
|
|
|
/* Test for setting lengths after setting nonce + already starting data. */
|
2021-06-21 19:32:46 +02:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
2021-12-15 15:28:49 +01:00
|
|
|
if( operation.alg == PSA_ALG_CCM )
|
|
|
|
{
|
2021-09-19 19:15:59 +02:00
|
|
|
|
2021-12-15 15:28:49 +01:00
|
|
|
TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
|
|
|
|
additional_data->len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
|
|
|
|
additional_data->len ) );
|
2021-09-19 19:15:59 +02:00
|
|
|
|
2021-12-15 15:28:49 +01:00
|
|
|
TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
2021-09-19 19:15:59 +02:00
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
|
|
|
/* ------------------------------------------------------- */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
2021-12-15 15:28:49 +01:00
|
|
|
if( operation.alg == PSA_ALG_CCM )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( psa_aead_update( &operation, input_data->x,
|
|
|
|
input_data->len, output_data,
|
|
|
|
output_size, &output_length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
2021-06-21 19:32:46 +02:00
|
|
|
|
2021-12-15 15:28:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_aead_update( &operation, input_data->x,
|
|
|
|
input_data->len, output_data,
|
|
|
|
output_size, &output_length ) );
|
2021-06-21 19:32:46 +02:00
|
|
|
|
2021-12-15 15:28:49 +01:00
|
|
|
TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
|
|
|
/* ------------------------------------------------------- */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
|
|
|
if( operation.alg == PSA_ALG_CCM )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_aead_finish( &operation, final_data,
|
|
|
|
finish_output_size,
|
|
|
|
&output_part_length,
|
|
|
|
tag_buffer, tag_length,
|
|
|
|
&tag_size ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_aead_finish( &operation, final_data,
|
|
|
|
finish_output_size,
|
|
|
|
&output_part_length,
|
|
|
|
tag_buffer, tag_length,
|
|
|
|
&tag_size ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
|
|
|
/* Test for setting lengths after generating nonce + already starting data. */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
|
|
|
|
PSA_AEAD_NONCE_MAX_SIZE,
|
|
|
|
&nonce_length ) );
|
|
|
|
if( operation.alg == PSA_ALG_CCM )
|
|
|
|
{
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
|
|
|
|
additional_data->len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
|
|
|
|
additional_data->len ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
|
|
|
/* ------------------------------------------------------- */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
|
|
|
|
PSA_AEAD_NONCE_MAX_SIZE,
|
|
|
|
&nonce_length ) );
|
|
|
|
if( operation.alg == PSA_ALG_CCM )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( psa_aead_update( &operation, input_data->x,
|
|
|
|
input_data->len, output_data,
|
|
|
|
output_size, &output_length ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_aead_update( &operation, input_data->x,
|
|
|
|
input_data->len, output_data,
|
|
|
|
output_size, &output_length ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
|
|
|
/* ------------------------------------------------------- */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
|
|
|
|
PSA_AEAD_NONCE_MAX_SIZE,
|
|
|
|
&nonce_length ) );
|
|
|
|
if( operation.alg == PSA_ALG_CCM )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_aead_finish( &operation, final_data,
|
|
|
|
finish_output_size,
|
|
|
|
&output_part_length,
|
|
|
|
tag_buffer, tag_length,
|
|
|
|
&tag_size ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_aead_finish( &operation, final_data,
|
|
|
|
finish_output_size,
|
|
|
|
&output_part_length,
|
|
|
|
tag_buffer, tag_length,
|
|
|
|
&tag_size ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
2021-06-21 19:32:46 +02:00
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-07-21 20:01:17 +02:00
|
|
|
/* Test for not sending any additional data or data after setting non zero
|
|
|
|
* lengths for them. (encrypt) */
|
2021-06-21 19:32:46 +02:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_finish( &operation, final_data,
|
|
|
|
finish_output_size,
|
|
|
|
&output_part_length,
|
|
|
|
tag_buffer, tag_length,
|
|
|
|
&tag_size ),
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-07-21 20:01:17 +02:00
|
|
|
/* Test for not sending any additional data or data after setting non-zero
|
|
|
|
* lengths for them. (decrypt) */
|
2021-06-21 19:32:46 +02:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_verify( &operation, final_data,
|
|
|
|
finish_output_size,
|
|
|
|
&output_part_length,
|
|
|
|
tag_buffer,
|
|
|
|
tag_length ),
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-07-21 20:01:17 +02:00
|
|
|
/* Test for not sending any additional data after setting a non-zero length
|
|
|
|
* for it. */
|
2021-06-21 19:32:46 +02:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_update( &operation, input_data->x,
|
|
|
|
input_data->len, output_data,
|
|
|
|
output_size, &output_length ),
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-09-19 19:15:59 +02:00
|
|
|
/* Test for not sending any data after setting a non-zero length for it.*/
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
|
|
|
|
additional_data->len ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_finish( &operation, final_data,
|
|
|
|
finish_output_size,
|
|
|
|
&output_part_length,
|
|
|
|
tag_buffer, tag_length,
|
|
|
|
&tag_size ),
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-09-01 16:06:26 +02:00
|
|
|
/* Test for sending too much additional data after setting lengths. */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
|
|
|
|
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
|
|
|
|
additional_data->len ),
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-09-22 15:56:40 +02:00
|
|
|
/* ------------------------------------------------------- */
|
2021-09-17 19:03:52 +02:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
|
|
|
|
additional_data->len ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
|
|
|
|
1 ),
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-09-01 16:06:26 +02:00
|
|
|
/* Test for sending too much data after setting lengths. */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_update( &operation, input_data->x,
|
|
|
|
input_data->len, output_data,
|
|
|
|
output_size, &output_length ),
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-09-22 15:56:40 +02:00
|
|
|
/* ------------------------------------------------------- */
|
2021-09-17 19:03:52 +02:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
|
|
|
|
input_data->len ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
|
|
|
|
additional_data->len ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_update( &operation, input_data->x,
|
|
|
|
input_data->len, output_data,
|
|
|
|
output_size, &output_length ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_update( &operation, input_data->x,
|
|
|
|
1, output_data,
|
|
|
|
output_size, &output_length ),
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-06-21 19:32:46 +02:00
|
|
|
/* Test sending additional data after data. */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
2021-12-15 15:28:49 +01:00
|
|
|
if( operation.alg != PSA_ALG_CCM )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_aead_update( &operation, input_data->x,
|
|
|
|
input_data->len, output_data,
|
|
|
|
output_size, &output_length ) );
|
2021-06-21 19:32:46 +02:00
|
|
|
|
2021-12-15 15:28:49 +01:00
|
|
|
TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
|
|
|
|
additional_data->len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
2021-06-21 19:32:46 +02:00
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
2021-06-22 20:15:20 +02:00
|
|
|
/* Test calling finish on decryption. */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_finish( &operation, final_data,
|
|
|
|
finish_output_size,
|
|
|
|
&output_part_length,
|
|
|
|
tag_buffer, tag_length,
|
|
|
|
&tag_size ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
|
|
|
/* Test calling verify on encryption. */
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_aead_verify( &operation, final_data,
|
|
|
|
finish_output_size,
|
|
|
|
&output_part_length,
|
|
|
|
tag_buffer,
|
|
|
|
tag_length ),
|
2021-06-23 09:33:22 +02:00
|
|
|
PSA_ERROR_BAD_STATE );
|
2021-06-22 20:15:20 +02:00
|
|
|
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
|
|
|
|
|
2021-06-21 19:32:46 +02:00
|
|
|
exit:
|
|
|
|
psa_destroy_key( key );
|
|
|
|
psa_aead_abort( &operation );
|
|
|
|
mbedtls_free( output_data );
|
|
|
|
mbedtls_free( final_data );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-02-03 23:57:22 +01:00
|
|
|
/* BEGIN_CASE */
|
2018-06-18 16:04:39 +02:00
|
|
|
void signature_size( int type_arg,
|
|
|
|
int bits,
|
|
|
|
int alg_arg,
|
|
|
|
int expected_size_arg )
|
2018-02-03 23:57:22 +01:00
|
|
|
{
|
|
|
|
psa_key_type_t type = type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2019-11-26 17:01:59 +01:00
|
|
|
size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
|
2019-11-26 17:37:37 +01:00
|
|
|
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( actual_size, (size_t) expected_size_arg );
|
2019-11-26 17:37:37 +01:00
|
|
|
|
2018-02-03 23:57:22 +01:00
|
|
|
exit:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-02-03 22:44:14 +01:00
|
|
|
/* BEGIN_CASE */
|
2021-04-16 14:21:21 +02:00
|
|
|
void sign_hash_deterministic( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg, data_t *input_data,
|
|
|
|
data_t *output_data )
|
2018-02-03 22:44:14 +01:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-02-03 22:44:14 +01:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2018-02-03 23:57:22 +01:00
|
|
|
size_t key_bits;
|
|
|
|
unsigned char *signature = NULL;
|
|
|
|
size_t signature_size;
|
2018-02-03 23:58:03 +01:00
|
|
|
size_t signature_length = 0xdeadbeef;
|
2019-04-18 21:44:46 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-02-03 22:44:14 +01:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-02-03 22:44:14 +01:00
|
|
|
|
2019-11-26 17:01:59 +01:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2018-03-28 12:46:26 +02:00
|
|
|
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
2020-08-04 14:58:35 +02:00
|
|
|
&key ) );
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
2019-04-18 21:44:46 +02:00
|
|
|
key_bits = psa_get_key_bits( &attributes );
|
2018-02-03 23:57:22 +01:00
|
|
|
|
2021-12-21 06:14:10 +01:00
|
|
|
/* Allocate a buffer which has the size advertised by the
|
2018-06-28 12:23:00 +02:00
|
|
|
* library. */
|
2019-11-26 17:01:59 +01:00
|
|
|
signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
|
2018-06-18 16:04:39 +02:00
|
|
|
key_bits, alg );
|
2018-02-03 23:57:22 +01:00
|
|
|
TEST_ASSERT( signature_size != 0 );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
|
2018-09-27 13:54:18 +02:00
|
|
|
ASSERT_ALLOC( signature, signature_size );
|
2018-02-03 22:44:14 +01:00
|
|
|
|
2018-06-28 12:23:00 +02:00
|
|
|
/* Perform the signature. */
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_sign_hash( key, alg,
|
2019-11-26 17:01:59 +01:00
|
|
|
input_data->x, input_data->len,
|
|
|
|
signature, signature_size,
|
|
|
|
&signature_length ) );
|
2018-06-29 17:30:48 +02:00
|
|
|
/* Verify that the signature is what is expected. */
|
2018-09-27 13:57:19 +02:00
|
|
|
ASSERT_COMPARE( output_data->x, output_data->len,
|
|
|
|
signature, signature_length );
|
2018-02-03 22:44:14 +01:00
|
|
|
|
|
|
|
exit:
|
2020-11-19 17:55:23 +01:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
2019-04-26 16:03:33 +02:00
|
|
|
psa_reset_key_attributes( &attributes );
|
2020-11-19 17:55:23 +01:00
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2018-02-03 23:57:22 +01:00
|
|
|
mbedtls_free( signature );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-02-03 22:44:14 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2021-04-16 14:21:21 +02:00
|
|
|
void sign_hash_fail( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg, data_t *input_data,
|
|
|
|
int signature_size_arg, int expected_status_arg )
|
2018-02-03 22:44:14 +01:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-02-03 22:44:14 +01:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2018-06-28 12:23:00 +02:00
|
|
|
size_t signature_size = signature_size_arg;
|
2018-02-03 22:44:14 +01:00
|
|
|
psa_status_t actual_status;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2018-03-07 16:43:36 +01:00
|
|
|
unsigned char *signature = NULL;
|
2018-02-03 23:58:03 +01:00
|
|
|
size_t signature_length = 0xdeadbeef;
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-02-03 22:44:14 +01:00
|
|
|
|
2018-09-27 13:54:18 +02:00
|
|
|
ASSERT_ALLOC( signature, signature_size );
|
2018-02-03 22:44:14 +01:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-02-03 22:44:14 +01:00
|
|
|
|
2019-11-26 17:01:59 +01:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2018-03-28 12:46:26 +02:00
|
|
|
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
2020-08-04 14:58:35 +02:00
|
|
|
&key ) );
|
2018-02-03 22:44:14 +01:00
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
actual_status = psa_sign_hash( key, alg,
|
2019-11-26 17:01:59 +01:00
|
|
|
input_data->x, input_data->len,
|
|
|
|
signature, signature_size,
|
|
|
|
&signature_length );
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( actual_status, expected_status );
|
2018-06-28 12:23:00 +02:00
|
|
|
/* The value of *signature_length is unspecified on error, but
|
|
|
|
* whatever it is, it should be less than signature_size, so that
|
|
|
|
* if the caller tries to read *signature_length bytes without
|
|
|
|
* checking the error code then they don't overflow a buffer. */
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( signature_length, signature_size );
|
2018-02-03 22:44:14 +01:00
|
|
|
|
|
|
|
exit:
|
2019-04-26 16:03:33 +02:00
|
|
|
psa_reset_key_attributes( &attributes );
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2018-02-03 22:44:14 +01:00
|
|
|
mbedtls_free( signature );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-06-29 17:30:48 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2021-04-16 14:21:21 +02:00
|
|
|
void sign_verify_hash( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg, data_t *input_data )
|
2018-06-29 17:30:48 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-06-29 17:30:48 +02:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
size_t key_bits;
|
|
|
|
unsigned char *signature = NULL;
|
|
|
|
size_t signature_size;
|
|
|
|
size_t signature_length = 0xdeadbeef;
|
2019-04-18 21:44:46 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-06-29 17:30:48 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-06-29 17:30:48 +02:00
|
|
|
|
2019-11-26 17:01:59 +01:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2018-06-29 17:30:48 +02:00
|
|
|
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
2020-08-04 14:58:35 +02:00
|
|
|
&key ) );
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
2019-04-18 21:44:46 +02:00
|
|
|
key_bits = psa_get_key_bits( &attributes );
|
2018-06-29 17:30:48 +02:00
|
|
|
|
2021-12-21 06:14:10 +01:00
|
|
|
/* Allocate a buffer which has the size advertised by the
|
2018-06-29 17:30:48 +02:00
|
|
|
* library. */
|
2019-11-26 17:01:59 +01:00
|
|
|
signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
|
2018-06-29 17:30:48 +02:00
|
|
|
key_bits, alg );
|
|
|
|
TEST_ASSERT( signature_size != 0 );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
|
2018-09-27 13:54:18 +02:00
|
|
|
ASSERT_ALLOC( signature, signature_size );
|
2018-06-29 17:30:48 +02:00
|
|
|
|
|
|
|
/* Perform the signature. */
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_sign_hash( key, alg,
|
2019-11-26 17:01:59 +01:00
|
|
|
input_data->x, input_data->len,
|
|
|
|
signature, signature_size,
|
|
|
|
&signature_length ) );
|
2018-06-29 17:30:48 +02:00
|
|
|
/* Check that the signature length looks sensible. */
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( signature_length, signature_size );
|
2018-06-29 17:30:48 +02:00
|
|
|
TEST_ASSERT( signature_length > 0 );
|
|
|
|
|
|
|
|
/* Use the library to verify that the signature is correct. */
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_verify_hash( key, alg,
|
2019-11-26 17:01:59 +01:00
|
|
|
input_data->x, input_data->len,
|
|
|
|
signature, signature_length ) );
|
2018-06-29 17:30:48 +02:00
|
|
|
|
|
|
|
if( input_data->len != 0 )
|
|
|
|
{
|
|
|
|
/* Flip a bit in the input and verify that the signature is now
|
|
|
|
* detected as invalid. Flip a bit at the beginning, not at the end,
|
|
|
|
* because ECDSA may ignore the last few bits of the input. */
|
|
|
|
input_data->x[0] ^= 1;
|
2020-08-04 14:58:35 +02:00
|
|
|
TEST_EQUAL( psa_verify_hash( key, alg,
|
2019-11-26 17:01:59 +01:00
|
|
|
input_data->x, input_data->len,
|
|
|
|
signature, signature_length ),
|
2018-12-18 00:33:25 +01:00
|
|
|
PSA_ERROR_INVALID_SIGNATURE );
|
2018-06-29 17:30:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2020-11-19 17:55:23 +01:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
2019-04-26 16:03:33 +02:00
|
|
|
psa_reset_key_attributes( &attributes );
|
2020-11-19 17:55:23 +01:00
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2018-06-29 17:30:48 +02:00
|
|
|
mbedtls_free( signature );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-02-03 22:44:14 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-03-28 00:21:33 +02:00
|
|
|
|
2018-05-08 10:18:38 +02:00
|
|
|
/* BEGIN_CASE */
|
2021-04-16 14:21:21 +02:00
|
|
|
void verify_hash( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg, data_t *hash_data,
|
|
|
|
data_t *signature_data )
|
2018-05-08 10:18:38 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-05-08 10:18:38 +02:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-05-08 10:18:38 +02:00
|
|
|
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
|
2018-06-28 00:07:19 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-05-08 10:18:38 +02:00
|
|
|
|
2019-11-26 17:01:59 +01:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2018-05-08 10:18:38 +02:00
|
|
|
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
2020-08-04 14:58:35 +02:00
|
|
|
&key ) );
|
2018-05-08 10:18:38 +02:00
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_verify_hash( key, alg,
|
2019-11-26 17:01:59 +01:00
|
|
|
hash_data->x, hash_data->len,
|
|
|
|
signature_data->x, signature_data->len ) );
|
2019-11-26 19:12:16 +01:00
|
|
|
|
2018-05-08 10:18:38 +02:00
|
|
|
exit:
|
2019-04-26 16:03:33 +02:00
|
|
|
psa_reset_key_attributes( &attributes );
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-05-08 10:18:38 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-05-02 22:16:26 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2021-04-16 14:21:21 +02:00
|
|
|
void verify_hash_fail( int key_type_arg, data_t *key_data,
|
|
|
|
int alg_arg, data_t *hash_data,
|
|
|
|
data_t *signature_data,
|
|
|
|
int expected_status_arg )
|
2018-05-02 22:16:26 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-05-02 22:16:26 +02:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t actual_status;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-05-02 22:16:26 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-05-02 22:16:26 +02:00
|
|
|
|
2019-11-26 17:01:59 +01:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2018-06-04 15:45:27 +02:00
|
|
|
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
2020-08-04 14:58:35 +02:00
|
|
|
&key ) );
|
2018-05-02 22:16:26 +02:00
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
actual_status = psa_verify_hash( key, alg,
|
2019-11-26 17:01:59 +01:00
|
|
|
hash_data->x, hash_data->len,
|
|
|
|
signature_data->x, signature_data->len );
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( actual_status, expected_status );
|
2018-05-02 22:16:26 +02:00
|
|
|
|
|
|
|
exit:
|
2019-04-26 16:03:33 +02:00
|
|
|
psa_reset_key_attributes( &attributes );
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-05-02 22:16:26 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-04-15 18:19:50 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void sign_message_deterministic( int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *input_data,
|
|
|
|
data_t *output_data )
|
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
size_t key_bits;
|
|
|
|
unsigned char *signature = NULL;
|
|
|
|
size_t signature_size;
|
|
|
|
size_t signature_length = 0xdeadbeef;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
|
|
|
key_bits = psa_get_key_bits( &attributes );
|
|
|
|
|
|
|
|
signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
|
|
|
|
TEST_ASSERT( signature_size != 0 );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
|
2021-04-15 18:19:50 +02:00
|
|
|
ASSERT_ALLOC( signature, signature_size );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_sign_message( key, alg,
|
|
|
|
input_data->x, input_data->len,
|
|
|
|
signature, signature_size,
|
|
|
|
&signature_length ) );
|
|
|
|
|
|
|
|
ASSERT_COMPARE( output_data->x, output_data->len,
|
|
|
|
signature, signature_length );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
|
|
|
|
psa_destroy_key( key );
|
|
|
|
mbedtls_free( signature );
|
|
|
|
PSA_DONE( );
|
|
|
|
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void sign_message_fail( int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *input_data,
|
|
|
|
int signature_size_arg,
|
|
|
|
int expected_status_arg )
|
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
size_t signature_size = signature_size_arg;
|
|
|
|
psa_status_t actual_status;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
unsigned char *signature = NULL;
|
|
|
|
size_t signature_length = 0xdeadbeef;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
|
|
|
ASSERT_ALLOC( signature, signature_size );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
|
|
|
|
actual_status = psa_sign_message( key, alg,
|
|
|
|
input_data->x, input_data->len,
|
|
|
|
signature, signature_size,
|
|
|
|
&signature_length );
|
|
|
|
TEST_EQUAL( actual_status, expected_status );
|
|
|
|
/* The value of *signature_length is unspecified on error, but
|
|
|
|
* whatever it is, it should be less than signature_size, so that
|
|
|
|
* if the caller tries to read *signature_length bytes without
|
|
|
|
* checking the error code then they don't overflow a buffer. */
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( signature_length, signature_size );
|
2021-04-15 18:19:50 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
psa_destroy_key( key );
|
|
|
|
mbedtls_free( signature );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void sign_verify_message( int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *input_data )
|
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
size_t key_bits;
|
|
|
|
unsigned char *signature = NULL;
|
|
|
|
size_t signature_size;
|
|
|
|
size_t signature_length = 0xdeadbeef;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
|
|
|
|
PSA_KEY_USAGE_VERIFY_MESSAGE );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
|
|
|
key_bits = psa_get_key_bits( &attributes );
|
|
|
|
|
|
|
|
signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
|
|
|
|
TEST_ASSERT( signature_size != 0 );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
|
2021-04-15 18:19:50 +02:00
|
|
|
ASSERT_ALLOC( signature, signature_size );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_sign_message( key, alg,
|
|
|
|
input_data->x, input_data->len,
|
|
|
|
signature, signature_size,
|
|
|
|
&signature_length ) );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( signature_length, signature_size );
|
2021-04-15 18:19:50 +02:00
|
|
|
TEST_ASSERT( signature_length > 0 );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_verify_message( key, alg,
|
|
|
|
input_data->x, input_data->len,
|
|
|
|
signature, signature_length ) );
|
|
|
|
|
|
|
|
if( input_data->len != 0 )
|
|
|
|
{
|
|
|
|
/* Flip a bit in the input and verify that the signature is now
|
|
|
|
* detected as invalid. Flip a bit at the beginning, not at the end,
|
|
|
|
* because ECDSA may ignore the last few bits of the input. */
|
|
|
|
input_data->x[0] ^= 1;
|
|
|
|
TEST_EQUAL( psa_verify_message( key, alg,
|
|
|
|
input_data->x, input_data->len,
|
|
|
|
signature, signature_length ),
|
|
|
|
PSA_ERROR_INVALID_SIGNATURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
|
|
|
|
psa_destroy_key( key );
|
|
|
|
mbedtls_free( signature );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void verify_message( int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *input_data,
|
|
|
|
data_t *signature_data )
|
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
|
2021-04-15 18:19:50 +02:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_verify_message( key, alg,
|
|
|
|
input_data->x, input_data->len,
|
|
|
|
signature_data->x, signature_data->len ) );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
psa_destroy_key( key );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void verify_message_fail( int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *hash_data,
|
|
|
|
data_t *signature_data,
|
|
|
|
int expected_status_arg )
|
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t actual_status;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
|
|
|
&key ) );
|
|
|
|
|
|
|
|
actual_status = psa_verify_message( key, alg,
|
|
|
|
hash_data->x, hash_data->len,
|
|
|
|
signature_data->x,
|
|
|
|
signature_data->len );
|
|
|
|
TEST_EQUAL( actual_status, expected_status );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
psa_destroy_key( key );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-06-29 19:12:28 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void asymmetric_encrypt( int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *input_data,
|
2018-06-30 18:42:41 +02:00
|
|
|
data_t *label,
|
2018-06-29 19:12:28 +02:00
|
|
|
int expected_output_length_arg,
|
|
|
|
int expected_status_arg )
|
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-06-29 19:12:28 +02:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
size_t expected_output_length = expected_output_length_arg;
|
|
|
|
size_t key_bits;
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
size_t output_size;
|
|
|
|
size_t output_length = ~0;
|
|
|
|
psa_status_t actual_status;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2019-04-18 21:44:46 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-06-29 19:12:28 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-12-03 15:36:32 +01:00
|
|
|
|
2018-06-29 19:12:28 +02:00
|
|
|
/* Import the key */
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
2020-08-04 14:58:35 +02:00
|
|
|
&key ) );
|
2018-06-29 19:12:28 +02:00
|
|
|
|
|
|
|
/* Determine the maximum output length */
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
2019-04-18 21:44:46 +02:00
|
|
|
key_bits = psa_get_key_bits( &attributes );
|
2021-01-21 12:26:17 +01:00
|
|
|
|
2018-06-29 19:12:28 +02:00
|
|
|
output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
|
2018-09-27 13:54:18 +02:00
|
|
|
ASSERT_ALLOC( output, output_size );
|
2018-06-29 19:12:28 +02:00
|
|
|
|
|
|
|
/* Encrypt the input */
|
2020-08-04 14:58:35 +02:00
|
|
|
actual_status = psa_asymmetric_encrypt( key, alg,
|
2018-06-29 19:12:28 +02:00
|
|
|
input_data->x, input_data->len,
|
2018-06-30 18:42:41 +02:00
|
|
|
label->x, label->len,
|
2018-06-29 19:12:28 +02:00
|
|
|
output, output_size,
|
|
|
|
&output_length );
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( actual_status, expected_status );
|
|
|
|
TEST_EQUAL( output_length, expected_output_length );
|
2018-06-29 19:12:28 +02:00
|
|
|
|
2018-06-30 18:42:41 +02:00
|
|
|
/* If the label is empty, the test framework puts a non-null pointer
|
|
|
|
* in label->x. Test that a null pointer works as well. */
|
|
|
|
if( label->len == 0 )
|
|
|
|
{
|
|
|
|
output_length = ~0;
|
2018-09-26 18:19:24 +02:00
|
|
|
if( output_size != 0 )
|
|
|
|
memset( output, 0, output_size );
|
2020-08-04 14:58:35 +02:00
|
|
|
actual_status = psa_asymmetric_encrypt( key, alg,
|
2018-06-30 18:42:41 +02:00
|
|
|
input_data->x, input_data->len,
|
|
|
|
NULL, label->len,
|
|
|
|
output, output_size,
|
|
|
|
&output_length );
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( actual_status, expected_status );
|
|
|
|
TEST_EQUAL( output_length, expected_output_length );
|
2018-06-30 18:42:41 +02:00
|
|
|
}
|
|
|
|
|
2018-06-29 19:12:28 +02:00
|
|
|
exit:
|
2020-11-19 17:55:23 +01:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
2019-04-26 16:03:33 +02:00
|
|
|
psa_reset_key_attributes( &attributes );
|
2020-11-19 17:55:23 +01:00
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2018-06-29 19:12:28 +02:00
|
|
|
mbedtls_free( output );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-06-29 19:12:28 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-05-02 22:16:26 +02:00
|
|
|
/* BEGIN_CASE */
|
2018-06-30 18:42:41 +02:00
|
|
|
void asymmetric_encrypt_decrypt( int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *input_data,
|
|
|
|
data_t *label )
|
2018-05-02 22:16:26 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-05-02 22:16:26 +02:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2018-06-30 18:54:48 +02:00
|
|
|
size_t key_bits;
|
2018-05-02 22:16:26 +02:00
|
|
|
unsigned char *output = NULL;
|
2018-06-30 18:54:48 +02:00
|
|
|
size_t output_size;
|
|
|
|
size_t output_length = ~0;
|
2018-05-02 22:56:12 +02:00
|
|
|
unsigned char *output2 = NULL;
|
2018-06-30 18:54:48 +02:00
|
|
|
size_t output2_size;
|
|
|
|
size_t output2_length = ~0;
|
2019-04-18 21:44:46 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-05-02 22:16:26 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-05-02 22:16:26 +02:00
|
|
|
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2018-05-02 22:16:26 +02:00
|
|
|
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
2020-08-04 14:58:35 +02:00
|
|
|
&key ) );
|
2018-06-30 18:54:48 +02:00
|
|
|
|
|
|
|
/* Determine the maximum ciphertext length */
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
2019-04-18 21:44:46 +02:00
|
|
|
key_bits = psa_get_key_bits( &attributes );
|
2021-01-21 12:26:17 +01:00
|
|
|
|
2018-06-30 18:54:48 +02:00
|
|
|
output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
|
2018-09-27 13:54:18 +02:00
|
|
|
ASSERT_ALLOC( output, output_size );
|
2021-01-21 12:26:17 +01:00
|
|
|
|
2018-06-30 18:54:48 +02:00
|
|
|
output2_size = input_data->len;
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( output2_size,
|
|
|
|
PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
|
|
|
|
TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
|
2018-09-27 13:54:18 +02:00
|
|
|
ASSERT_ALLOC( output2, output2_size );
|
2018-06-30 18:54:48 +02:00
|
|
|
|
2018-06-08 18:11:54 +02:00
|
|
|
/* We test encryption by checking that encrypt-then-decrypt gives back
|
|
|
|
* the original plaintext because of the non-optional random
|
|
|
|
* part of encryption process which prevents using fixed vectors. */
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
|
2018-12-18 00:18:46 +01:00
|
|
|
input_data->x, input_data->len,
|
|
|
|
label->x, label->len,
|
|
|
|
output, output_size,
|
|
|
|
&output_length ) );
|
2018-06-30 18:54:48 +02:00
|
|
|
/* We don't know what ciphertext length to expect, but check that
|
|
|
|
* it looks sensible. */
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( output_length, output_size );
|
2018-06-18 15:41:12 +02:00
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
|
2018-12-18 00:18:46 +01:00
|
|
|
output, output_length,
|
|
|
|
label->x, label->len,
|
|
|
|
output2, output2_size,
|
|
|
|
&output2_length ) );
|
2018-09-27 13:57:19 +02:00
|
|
|
ASSERT_COMPARE( input_data->x, input_data->len,
|
|
|
|
output2, output2_length );
|
2018-05-02 22:16:26 +02:00
|
|
|
|
|
|
|
exit:
|
2020-11-19 17:55:23 +01:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
2019-04-26 16:03:33 +02:00
|
|
|
psa_reset_key_attributes( &attributes );
|
2020-11-19 17:55:23 +01:00
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2018-06-12 16:06:52 +02:00
|
|
|
mbedtls_free( output );
|
|
|
|
mbedtls_free( output2 );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-05-02 22:16:26 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-30 18:42:41 +02:00
|
|
|
void asymmetric_decrypt( int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *input_data,
|
|
|
|
data_t *label,
|
2018-06-29 21:54:10 +02:00
|
|
|
data_t *expected_data )
|
2018-05-02 22:16:26 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-05-02 22:16:26 +02:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2021-01-21 12:26:17 +01:00
|
|
|
size_t key_bits;
|
2018-05-02 22:16:26 +02:00
|
|
|
unsigned char *output = NULL;
|
2018-06-04 15:31:13 +02:00
|
|
|
size_t output_size = 0;
|
2018-06-30 18:54:48 +02:00
|
|
|
size_t output_length = ~0;
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-05-02 22:16:26 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-05-02 22:16:26 +02:00
|
|
|
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2018-12-18 00:18:46 +01:00
|
|
|
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
2020-08-04 14:58:35 +02:00
|
|
|
&key ) );
|
2018-12-18 00:18:46 +01:00
|
|
|
|
2021-01-21 12:26:17 +01:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
|
|
|
key_bits = psa_get_key_bits( &attributes );
|
|
|
|
|
|
|
|
/* Determine the maximum ciphertext length */
|
|
|
|
output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
|
2021-01-21 12:26:17 +01:00
|
|
|
ASSERT_ALLOC( output, output_size );
|
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
|
2018-12-18 00:18:46 +01:00
|
|
|
input_data->x, input_data->len,
|
|
|
|
label->x, label->len,
|
|
|
|
output,
|
|
|
|
output_size,
|
|
|
|
&output_length ) );
|
2018-09-27 13:57:19 +02:00
|
|
|
ASSERT_COMPARE( expected_data->x, expected_data->len,
|
|
|
|
output, output_length );
|
2018-05-02 22:16:26 +02:00
|
|
|
|
2018-06-30 18:42:41 +02:00
|
|
|
/* If the label is empty, the test framework puts a non-null pointer
|
|
|
|
* in label->x. Test that a null pointer works as well. */
|
|
|
|
if( label->len == 0 )
|
|
|
|
{
|
|
|
|
output_length = ~0;
|
2018-09-26 18:19:24 +02:00
|
|
|
if( output_size != 0 )
|
|
|
|
memset( output, 0, output_size );
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
|
2018-12-18 00:18:46 +01:00
|
|
|
input_data->x, input_data->len,
|
|
|
|
NULL, label->len,
|
|
|
|
output,
|
|
|
|
output_size,
|
|
|
|
&output_length ) );
|
2018-09-27 13:57:19 +02:00
|
|
|
ASSERT_COMPARE( expected_data->x, expected_data->len,
|
|
|
|
output, output_length );
|
2018-06-30 18:42:41 +02:00
|
|
|
}
|
|
|
|
|
2018-05-02 22:16:26 +02:00
|
|
|
exit:
|
2019-04-26 16:03:33 +02:00
|
|
|
psa_reset_key_attributes( &attributes );
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2018-06-12 16:06:52 +02:00
|
|
|
mbedtls_free( output );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-05-02 22:16:26 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-30 18:42:41 +02:00
|
|
|
void asymmetric_decrypt_fail( int key_type_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
int alg_arg,
|
|
|
|
data_t *input_data,
|
|
|
|
data_t *label,
|
2019-02-06 13:57:46 +01:00
|
|
|
int output_size_arg,
|
2018-06-18 15:41:12 +02:00
|
|
|
int expected_status_arg )
|
2018-05-02 22:16:26 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-05-02 22:16:26 +02:00
|
|
|
psa_key_type_t key_type = key_type_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
unsigned char *output = NULL;
|
2019-02-06 13:57:46 +01:00
|
|
|
size_t output_size = output_size_arg;
|
2018-06-30 18:54:48 +02:00
|
|
|
size_t output_length = ~0;
|
2018-05-02 22:16:26 +02:00
|
|
|
psa_status_t actual_status;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-05-02 22:16:26 +02:00
|
|
|
|
2018-09-27 13:54:18 +02:00
|
|
|
ASSERT_ALLOC( output, output_size );
|
2018-05-31 13:25:48 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-05-02 22:16:26 +02:00
|
|
|
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2018-06-04 15:45:27 +02:00
|
|
|
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
2020-08-04 14:58:35 +02:00
|
|
|
&key ) );
|
2018-06-18 15:41:12 +02:00
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
actual_status = psa_asymmetric_decrypt( key, alg,
|
2018-06-18 16:35:34 +02:00
|
|
|
input_data->x, input_data->len,
|
2018-06-30 18:42:41 +02:00
|
|
|
label->x, label->len,
|
2018-06-18 16:35:34 +02:00
|
|
|
output, output_size,
|
2018-06-18 15:41:12 +02:00
|
|
|
&output_length );
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( actual_status, expected_status );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( output_length, output_size );
|
2018-05-02 22:16:26 +02:00
|
|
|
|
2018-06-30 18:42:41 +02:00
|
|
|
/* If the label is empty, the test framework puts a non-null pointer
|
|
|
|
* in label->x. Test that a null pointer works as well. */
|
|
|
|
if( label->len == 0 )
|
|
|
|
{
|
|
|
|
output_length = ~0;
|
2018-09-26 18:19:24 +02:00
|
|
|
if( output_size != 0 )
|
|
|
|
memset( output, 0, output_size );
|
2020-08-04 14:58:35 +02:00
|
|
|
actual_status = psa_asymmetric_decrypt( key, alg,
|
2018-06-30 18:42:41 +02:00
|
|
|
input_data->x, input_data->len,
|
|
|
|
NULL, label->len,
|
|
|
|
output, output_size,
|
|
|
|
&output_length );
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( actual_status, expected_status );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( output_length, output_size );
|
2018-06-30 18:42:41 +02:00
|
|
|
}
|
|
|
|
|
2018-05-02 22:16:26 +02:00
|
|
|
exit:
|
2019-04-26 16:03:33 +02:00
|
|
|
psa_reset_key_attributes( &attributes );
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2018-06-18 15:41:12 +02:00
|
|
|
mbedtls_free( output );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-05-02 22:16:26 +02:00
|
|
|
}
|
2018-05-31 13:25:48 +02:00
|
|
|
/* END_CASE */
|
2018-06-19 22:00:52 +02:00
|
|
|
|
2019-01-04 15:11:48 +01:00
|
|
|
/* BEGIN_CASE */
|
2019-05-16 16:59:18 +02:00
|
|
|
void key_derivation_init( )
|
2019-01-04 15:11:48 +01:00
|
|
|
{
|
|
|
|
/* Test each valid way of initializing the object, except for `= {0}`, as
|
|
|
|
* Clang 5 complains when `-Wmissing-field-initializers` is used, even
|
|
|
|
* though it's OK by the C standard. We could test for this, but we'd need
|
2021-12-21 06:14:10 +01:00
|
|
|
* to suppress the Clang warning for the test. */
|
2019-02-07 17:33:37 +01:00
|
|
|
size_t capacity;
|
Rename generator functions to psa_key_derivation_xxx
Generators are mostly about key derivation (currently: only about key
derivation). "Generator" is not a commonly used term in cryptography.
So favor "derivation" as terminology. Call a generator a key
derivation operation structure, since it behaves like other multipart
operation structures. Furthermore, the function names are not fully
consistent.
In this commit, I rename the functions to consistently have the prefix
"psa_key_derivation_". I used the following command:
perl -i -pe '%t = (
psa_crypto_generator_t => "psa_key_derivation_operation_t",
psa_crypto_generator_init => "psa_key_derivation_init",
psa_key_derivation_setup => "psa_key_derivation_setup",
psa_key_derivation_input_key => "psa_key_derivation_input_key",
psa_key_derivation_input_bytes => "psa_key_derivation_input_bytes",
psa_key_agreement => "psa_key_derivation_key_agreement",
psa_set_generator_capacity => "psa_key_derivation_set_capacity",
psa_get_generator_capacity => "psa_key_derivation_get_capacity",
psa_generator_read => "psa_key_derivation_output_bytes",
psa_generate_derived_key => "psa_key_derivation_output_key",
psa_generator_abort => "psa_key_derivation_abort",
PSA_CRYPTO_GENERATOR_INIT => "PSA_KEY_DERIVATION_OPERATION_INIT",
PSA_GENERATOR_UNBRIDLED_CAPACITY => "PSA_KEY_DERIVATION_UNLIMITED_CAPACITY",
); s/\b(@{[join("|", keys %t)]})\b/$t{$1}/ge' $(git ls-files)
2019-05-16 15:28:51 +02:00
|
|
|
psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
|
|
|
|
psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
|
|
|
|
psa_key_derivation_operation_t zero;
|
2019-01-04 15:11:48 +01:00
|
|
|
|
|
|
|
memset( &zero, 0, sizeof( zero ) );
|
|
|
|
|
2019-05-16 17:31:03 +02:00
|
|
|
/* A default operation should not be able to report its capacity. */
|
Rename generator functions to psa_key_derivation_xxx
Generators are mostly about key derivation (currently: only about key
derivation). "Generator" is not a commonly used term in cryptography.
So favor "derivation" as terminology. Call a generator a key
derivation operation structure, since it behaves like other multipart
operation structures. Furthermore, the function names are not fully
consistent.
In this commit, I rename the functions to consistently have the prefix
"psa_key_derivation_". I used the following command:
perl -i -pe '%t = (
psa_crypto_generator_t => "psa_key_derivation_operation_t",
psa_crypto_generator_init => "psa_key_derivation_init",
psa_key_derivation_setup => "psa_key_derivation_setup",
psa_key_derivation_input_key => "psa_key_derivation_input_key",
psa_key_derivation_input_bytes => "psa_key_derivation_input_bytes",
psa_key_agreement => "psa_key_derivation_key_agreement",
psa_set_generator_capacity => "psa_key_derivation_set_capacity",
psa_get_generator_capacity => "psa_key_derivation_get_capacity",
psa_generator_read => "psa_key_derivation_output_bytes",
psa_generate_derived_key => "psa_key_derivation_output_key",
psa_generator_abort => "psa_key_derivation_abort",
PSA_CRYPTO_GENERATOR_INIT => "PSA_KEY_DERIVATION_OPERATION_INIT",
PSA_GENERATOR_UNBRIDLED_CAPACITY => "PSA_KEY_DERIVATION_UNLIMITED_CAPACITY",
); s/\b(@{[join("|", keys %t)]})\b/$t{$1}/ge' $(git ls-files)
2019-05-16 15:28:51 +02:00
|
|
|
TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
|
2019-02-15 14:05:49 +01:00
|
|
|
PSA_ERROR_BAD_STATE );
|
Rename generator functions to psa_key_derivation_xxx
Generators are mostly about key derivation (currently: only about key
derivation). "Generator" is not a commonly used term in cryptography.
So favor "derivation" as terminology. Call a generator a key
derivation operation structure, since it behaves like other multipart
operation structures. Furthermore, the function names are not fully
consistent.
In this commit, I rename the functions to consistently have the prefix
"psa_key_derivation_". I used the following command:
perl -i -pe '%t = (
psa_crypto_generator_t => "psa_key_derivation_operation_t",
psa_crypto_generator_init => "psa_key_derivation_init",
psa_key_derivation_setup => "psa_key_derivation_setup",
psa_key_derivation_input_key => "psa_key_derivation_input_key",
psa_key_derivation_input_bytes => "psa_key_derivation_input_bytes",
psa_key_agreement => "psa_key_derivation_key_agreement",
psa_set_generator_capacity => "psa_key_derivation_set_capacity",
psa_get_generator_capacity => "psa_key_derivation_get_capacity",
psa_generator_read => "psa_key_derivation_output_bytes",
psa_generate_derived_key => "psa_key_derivation_output_key",
psa_generator_abort => "psa_key_derivation_abort",
PSA_CRYPTO_GENERATOR_INIT => "PSA_KEY_DERIVATION_OPERATION_INIT",
PSA_GENERATOR_UNBRIDLED_CAPACITY => "PSA_KEY_DERIVATION_UNLIMITED_CAPACITY",
); s/\b(@{[join("|", keys %t)]})\b/$t{$1}/ge' $(git ls-files)
2019-05-16 15:28:51 +02:00
|
|
|
TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
|
2019-02-15 14:05:49 +01:00
|
|
|
PSA_ERROR_BAD_STATE );
|
Rename generator functions to psa_key_derivation_xxx
Generators are mostly about key derivation (currently: only about key
derivation). "Generator" is not a commonly used term in cryptography.
So favor "derivation" as terminology. Call a generator a key
derivation operation structure, since it behaves like other multipart
operation structures. Furthermore, the function names are not fully
consistent.
In this commit, I rename the functions to consistently have the prefix
"psa_key_derivation_". I used the following command:
perl -i -pe '%t = (
psa_crypto_generator_t => "psa_key_derivation_operation_t",
psa_crypto_generator_init => "psa_key_derivation_init",
psa_key_derivation_setup => "psa_key_derivation_setup",
psa_key_derivation_input_key => "psa_key_derivation_input_key",
psa_key_derivation_input_bytes => "psa_key_derivation_input_bytes",
psa_key_agreement => "psa_key_derivation_key_agreement",
psa_set_generator_capacity => "psa_key_derivation_set_capacity",
psa_get_generator_capacity => "psa_key_derivation_get_capacity",
psa_generator_read => "psa_key_derivation_output_bytes",
psa_generate_derived_key => "psa_key_derivation_output_key",
psa_generator_abort => "psa_key_derivation_abort",
PSA_CRYPTO_GENERATOR_INIT => "PSA_KEY_DERIVATION_OPERATION_INIT",
PSA_GENERATOR_UNBRIDLED_CAPACITY => "PSA_KEY_DERIVATION_UNLIMITED_CAPACITY",
); s/\b(@{[join("|", keys %t)]})\b/$t{$1}/ge' $(git ls-files)
2019-05-16 15:28:51 +02:00
|
|
|
TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
|
2019-02-15 14:05:49 +01:00
|
|
|
PSA_ERROR_BAD_STATE );
|
2019-02-07 17:33:37 +01:00
|
|
|
|
2019-05-16 17:31:03 +02:00
|
|
|
/* A default operation should be abortable without error. */
|
Rename generator functions to psa_key_derivation_xxx
Generators are mostly about key derivation (currently: only about key
derivation). "Generator" is not a commonly used term in cryptography.
So favor "derivation" as terminology. Call a generator a key
derivation operation structure, since it behaves like other multipart
operation structures. Furthermore, the function names are not fully
consistent.
In this commit, I rename the functions to consistently have the prefix
"psa_key_derivation_". I used the following command:
perl -i -pe '%t = (
psa_crypto_generator_t => "psa_key_derivation_operation_t",
psa_crypto_generator_init => "psa_key_derivation_init",
psa_key_derivation_setup => "psa_key_derivation_setup",
psa_key_derivation_input_key => "psa_key_derivation_input_key",
psa_key_derivation_input_bytes => "psa_key_derivation_input_bytes",
psa_key_agreement => "psa_key_derivation_key_agreement",
psa_set_generator_capacity => "psa_key_derivation_set_capacity",
psa_get_generator_capacity => "psa_key_derivation_get_capacity",
psa_generator_read => "psa_key_derivation_output_bytes",
psa_generate_derived_key => "psa_key_derivation_output_key",
psa_generator_abort => "psa_key_derivation_abort",
PSA_CRYPTO_GENERATOR_INIT => "PSA_KEY_DERIVATION_OPERATION_INIT",
PSA_GENERATOR_UNBRIDLED_CAPACITY => "PSA_KEY_DERIVATION_UNLIMITED_CAPACITY",
); s/\b(@{[join("|", keys %t)]})\b/$t{$1}/ge' $(git ls-files)
2019-05-16 15:28:51 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_abort(&func) );
|
|
|
|
PSA_ASSERT( psa_key_derivation_abort(&init) );
|
|
|
|
PSA_ASSERT( psa_key_derivation_abort(&zero) );
|
2019-01-04 15:11:48 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-06-13 17:32:24 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void derive_setup( int alg_arg, int expected_status_arg )
|
2018-07-12 17:17:20 +02:00
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2019-05-16 17:31:03 +02:00
|
|
|
psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
|
2018-07-12 17:17:20 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-07-12 17:17:20 +02:00
|
|
|
|
2019-06-13 17:32:24 +02:00
|
|
|
TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
|
2018-12-18 00:33:25 +01:00
|
|
|
expected_status );
|
2018-07-12 17:17:20 +02:00
|
|
|
|
|
|
|
exit:
|
2019-05-16 17:31:03 +02:00
|
|
|
psa_key_derivation_abort( &operation );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-07-12 17:17:20 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-06-14 10:59:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void derive_set_capacity( int alg_arg, int capacity_arg,
|
|
|
|
int expected_status_arg )
|
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
size_t capacity = capacity_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
|
|
|
|
expected_status );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_key_derivation_abort( &operation );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-06-12 13:34:34 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void derive_input( int alg_arg,
|
2019-09-23 13:49:33 +02:00
|
|
|
int step_arg1, int key_type_arg1, data_t *input1,
|
2019-06-12 13:34:34 +02:00
|
|
|
int expected_status_arg1,
|
2019-09-24 17:19:33 +02:00
|
|
|
int step_arg2, int key_type_arg2, data_t *input2,
|
2019-06-12 13:34:34 +02:00
|
|
|
int expected_status_arg2,
|
2019-09-24 17:19:33 +02:00
|
|
|
int step_arg3, int key_type_arg3, data_t *input3,
|
2019-09-24 17:45:07 +02:00
|
|
|
int expected_status_arg3,
|
|
|
|
int output_key_type_arg, int expected_output_status_arg )
|
2019-06-12 13:34:34 +02:00
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2019-09-23 13:49:33 +02:00
|
|
|
psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
|
|
|
|
psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
|
2019-06-12 13:34:34 +02:00
|
|
|
psa_status_t expected_statuses[] = {expected_status_arg1,
|
|
|
|
expected_status_arg2,
|
|
|
|
expected_status_arg3};
|
|
|
|
data_t *inputs[] = {input1, input2, input3};
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
|
|
|
|
MBEDTLS_SVC_KEY_ID_INIT,
|
|
|
|
MBEDTLS_SVC_KEY_ID_INIT };
|
2019-06-12 13:34:34 +02:00
|
|
|
psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
size_t i;
|
2019-09-24 17:45:07 +02:00
|
|
|
psa_key_type_t output_key_type = output_key_type_arg;
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
|
2019-09-24 17:45:07 +02:00
|
|
|
psa_status_t expected_output_status = expected_output_status_arg;
|
|
|
|
psa_status_t actual_output_status;
|
2019-06-12 13:34:34 +02:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
|
|
|
|
|
|
|
|
for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
|
|
|
|
{
|
2021-05-27 13:21:20 +02:00
|
|
|
mbedtls_test_set_step( i );
|
|
|
|
if( steps[i] == 0 )
|
|
|
|
{
|
|
|
|
/* Skip this step */
|
|
|
|
}
|
|
|
|
else if( key_types[i] != PSA_KEY_TYPE_NONE )
|
2019-09-23 13:49:33 +02:00
|
|
|
{
|
|
|
|
psa_set_key_type( &attributes, key_types[i] );
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes,
|
|
|
|
inputs[i]->x, inputs[i]->len,
|
2020-08-04 14:58:35 +02:00
|
|
|
&keys[i] ) );
|
2020-10-05 16:03:42 +02:00
|
|
|
if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
|
|
|
|
steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
|
|
|
|
{
|
|
|
|
// When taking a private key as secret input, use key agreement
|
|
|
|
// to add the shared secret to the derivation
|
2021-02-12 23:48:20 +01:00
|
|
|
TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
|
|
|
|
&operation, keys[i] ),
|
2020-10-05 16:03:42 +02:00
|
|
|
expected_statuses[i] );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
|
2020-08-04 14:58:35 +02:00
|
|
|
keys[i] ),
|
2020-10-05 16:03:42 +02:00
|
|
|
expected_statuses[i] );
|
|
|
|
}
|
2019-09-23 13:49:33 +02:00
|
|
|
}
|
|
|
|
else
|
2019-06-12 13:34:34 +02:00
|
|
|
{
|
2019-09-23 13:49:33 +02:00
|
|
|
TEST_EQUAL( psa_key_derivation_input_bytes(
|
|
|
|
&operation, steps[i],
|
|
|
|
inputs[i]->x, inputs[i]->len ),
|
|
|
|
expected_statuses[i] );
|
2019-06-12 13:34:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 17:45:07 +02:00
|
|
|
if( output_key_type != PSA_KEY_TYPE_NONE )
|
|
|
|
{
|
|
|
|
psa_reset_key_attributes( &attributes );
|
2021-11-16 13:12:49 +01:00
|
|
|
psa_set_key_type( &attributes, output_key_type );
|
2019-09-24 17:45:07 +02:00
|
|
|
psa_set_key_bits( &attributes, 8 );
|
|
|
|
actual_output_status =
|
|
|
|
psa_key_derivation_output_key( &attributes, &operation,
|
2020-08-04 14:58:35 +02:00
|
|
|
&output_key );
|
2019-09-24 17:45:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uint8_t buffer[1];
|
|
|
|
actual_output_status =
|
|
|
|
psa_key_derivation_output_bytes( &operation,
|
|
|
|
buffer, sizeof( buffer ) );
|
|
|
|
}
|
|
|
|
TEST_EQUAL( actual_output_status, expected_output_status );
|
|
|
|
|
2019-06-12 13:34:34 +02:00
|
|
|
exit:
|
|
|
|
psa_key_derivation_abort( &operation );
|
2020-08-04 14:58:35 +02:00
|
|
|
for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
|
|
|
|
psa_destroy_key( keys[i] );
|
|
|
|
psa_destroy_key( output_key );
|
2019-06-12 13:34:34 +02:00
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-07-03 16:02:16 +02:00
|
|
|
/* BEGIN_CASE */
|
2021-05-27 11:55:02 +02:00
|
|
|
void derive_over_capacity( int alg_arg )
|
2018-10-22 16:24:55 +02:00
|
|
|
{
|
2019-07-03 16:02:16 +02:00
|
|
|
psa_algorithm_t alg = alg_arg;
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-10-31 11:15:58 +01:00
|
|
|
size_t key_type = PSA_KEY_TYPE_DERIVE;
|
2019-05-16 17:31:03 +02:00
|
|
|
psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
|
2019-07-03 16:02:16 +02:00
|
|
|
unsigned char input1[] = "Input 1";
|
|
|
|
size_t input1_length = sizeof( input1 );
|
|
|
|
unsigned char input2[] = "Input 2";
|
|
|
|
size_t input2_length = sizeof( input2 );
|
2018-10-25 13:46:09 +02:00
|
|
|
uint8_t buffer[42];
|
2018-11-01 11:27:20 +01:00
|
|
|
size_t capacity = sizeof( buffer );
|
2018-11-01 11:24:23 +01:00
|
|
|
const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
|
|
|
|
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
|
|
|
|
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-10-25 13:46:09 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-10-25 13:46:09 +02:00
|
|
|
|
2019-04-19 19:58:20 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, key_type );
|
2018-10-22 16:24:55 +02:00
|
|
|
|
2019-05-15 20:15:10 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes,
|
|
|
|
key_data, sizeof( key_data ),
|
2020-08-04 14:58:35 +02:00
|
|
|
&key ) );
|
2018-10-25 13:46:09 +02:00
|
|
|
|
|
|
|
/* valid key derivation */
|
2021-02-12 23:48:20 +01:00
|
|
|
if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
|
|
|
|
input1, input1_length,
|
|
|
|
input2, input2_length,
|
|
|
|
capacity ) )
|
2019-07-03 16:02:16 +02:00
|
|
|
goto exit;
|
2018-10-25 13:46:09 +02:00
|
|
|
|
2019-05-16 17:31:03 +02:00
|
|
|
/* state of operation shouldn't allow additional generation */
|
2019-07-03 16:02:16 +02:00
|
|
|
TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
|
2018-12-18 00:33:25 +01:00
|
|
|
PSA_ERROR_BAD_STATE );
|
2018-10-25 13:46:09 +02:00
|
|
|
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
|
2018-10-25 13:46:09 +02:00
|
|
|
|
2019-05-16 17:31:03 +02:00
|
|
|
TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
|
2019-02-14 12:48:10 +01:00
|
|
|
PSA_ERROR_INSUFFICIENT_DATA );
|
2018-10-25 13:46:09 +02:00
|
|
|
|
|
|
|
exit:
|
2019-05-16 17:31:03 +02:00
|
|
|
psa_key_derivation_abort( &operation );
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-10-25 13:46:09 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2021-05-27 11:55:02 +02:00
|
|
|
void derive_actions_without_setup( )
|
2018-10-25 13:46:09 +02:00
|
|
|
{
|
|
|
|
uint8_t output_buffer[16];
|
|
|
|
size_t buffer_size = 16;
|
|
|
|
size_t capacity = 0;
|
2019-05-16 17:31:03 +02:00
|
|
|
psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
|
2018-10-25 13:46:09 +02:00
|
|
|
|
2019-05-16 17:53:40 +02:00
|
|
|
TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
|
|
|
|
output_buffer, buffer_size )
|
2019-02-15 14:05:49 +01:00
|
|
|
== PSA_ERROR_BAD_STATE );
|
2018-10-25 13:46:09 +02:00
|
|
|
|
2019-05-16 17:31:03 +02:00
|
|
|
TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
|
2019-02-15 14:05:49 +01:00
|
|
|
== PSA_ERROR_BAD_STATE );
|
2018-10-25 13:46:09 +02:00
|
|
|
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_abort( &operation ) );
|
2018-10-25 13:46:09 +02:00
|
|
|
|
2019-05-16 17:53:40 +02:00
|
|
|
TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
|
|
|
|
output_buffer, buffer_size )
|
2019-02-15 14:05:49 +01:00
|
|
|
== PSA_ERROR_BAD_STATE );
|
2018-10-25 13:46:09 +02:00
|
|
|
|
2019-05-16 17:31:03 +02:00
|
|
|
TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
|
2019-02-15 14:05:49 +01:00
|
|
|
== PSA_ERROR_BAD_STATE );
|
2018-10-25 13:46:09 +02:00
|
|
|
|
|
|
|
exit:
|
2019-05-16 17:31:03 +02:00
|
|
|
psa_key_derivation_abort( &operation );
|
2018-10-22 16:24:55 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-07-12 17:24:54 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void derive_output( int alg_arg,
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
int step1_arg, data_t *input1, int expected_status_arg1,
|
|
|
|
int step2_arg, data_t *input2, int expected_status_arg2,
|
|
|
|
int step3_arg, data_t *input3, int expected_status_arg3,
|
|
|
|
int step4_arg, data_t *input4, int expected_status_arg4,
|
|
|
|
data_t *key_agreement_peer_key,
|
2018-07-12 17:24:54 +02:00
|
|
|
int requested_capacity_arg,
|
|
|
|
data_t *expected_output1,
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
data_t *expected_output2,
|
|
|
|
int other_key_input_type,
|
|
|
|
int key_input_type,
|
|
|
|
int derive_type )
|
2018-07-12 17:24:54 +02:00
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2022-03-31 11:13:47 +02:00
|
|
|
psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg};
|
|
|
|
data_t *inputs[] = {input1, input2, input3, input4};
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT,
|
|
|
|
MBEDTLS_SVC_KEY_ID_INIT,
|
|
|
|
MBEDTLS_SVC_KEY_ID_INIT,
|
|
|
|
MBEDTLS_SVC_KEY_ID_INIT};
|
|
|
|
psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2,
|
|
|
|
expected_status_arg3, expected_status_arg4};
|
2018-07-12 17:24:54 +02:00
|
|
|
size_t requested_capacity = requested_capacity_arg;
|
2019-05-16 17:31:03 +02:00
|
|
|
psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
|
2018-07-12 17:24:54 +02:00
|
|
|
uint8_t *expected_outputs[2] =
|
|
|
|
{expected_output1->x, expected_output2->x};
|
|
|
|
size_t output_sizes[2] =
|
|
|
|
{expected_output1->len, expected_output2->len};
|
|
|
|
size_t output_buffer_size = 0;
|
|
|
|
uint8_t *output_buffer = NULL;
|
|
|
|
size_t expected_capacity;
|
|
|
|
size_t current_capacity;
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
|
2022-04-20 10:06:38 +02:00
|
|
|
mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-07-12 17:24:54 +02:00
|
|
|
psa_status_t status;
|
2019-05-29 17:35:49 +02:00
|
|
|
size_t i;
|
2018-07-12 17:24:54 +02:00
|
|
|
|
|
|
|
for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
|
|
|
|
{
|
|
|
|
if( output_sizes[i] > output_buffer_size )
|
|
|
|
output_buffer_size = output_sizes[i];
|
|
|
|
if( output_sizes[i] == 0 )
|
|
|
|
expected_outputs[i] = NULL;
|
|
|
|
}
|
2018-09-27 13:54:18 +02:00
|
|
|
ASSERT_ALLOC( output_buffer, output_buffer_size );
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-07-12 17:24:54 +02:00
|
|
|
|
|
|
|
/* Extraction phase. */
|
2019-05-29 17:35:49 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
|
|
|
|
PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
|
|
|
|
requested_capacity ) );
|
|
|
|
for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
|
2019-01-07 22:59:38 +01:00
|
|
|
{
|
2019-05-29 17:35:49 +02:00
|
|
|
switch( steps[i] )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case PSA_KEY_DERIVATION_INPUT_SECRET:
|
2022-04-19 13:27:47 +02:00
|
|
|
switch( key_input_type )
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
{
|
|
|
|
case 0: // input bytes
|
2022-05-19 10:28:58 +02:00
|
|
|
TEST_EQUAL( psa_key_derivation_input_bytes(
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
&operation, steps[i],
|
2022-05-19 10:28:58 +02:00
|
|
|
inputs[i]->x, inputs[i]->len ),
|
|
|
|
statuses[i] );
|
|
|
|
|
|
|
|
if( statuses[i] != PSA_SUCCESS )
|
|
|
|
goto exit;
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
break;
|
|
|
|
case 1: // input key
|
|
|
|
psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE );
|
|
|
|
psa_set_key_algorithm( &attributes1, alg );
|
|
|
|
psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes1,
|
|
|
|
inputs[i]->x, inputs[i]->len,
|
|
|
|
&keys[i] ) );
|
|
|
|
|
2022-04-19 13:27:47 +02:00
|
|
|
if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) );
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ),
|
|
|
|
PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
}
|
|
|
|
|
2022-04-19 13:27:47 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_input_key( &operation,
|
|
|
|
steps[i],
|
|
|
|
keys[i] ) );
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
TEST_ASSERT( ! "default case not supported" );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
|
2022-04-19 13:27:47 +02:00
|
|
|
switch( other_key_input_type )
|
2021-01-21 12:26:17 +01:00
|
|
|
{
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
case 0: // input bytes
|
2022-04-19 13:27:47 +02:00
|
|
|
TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
|
|
|
|
steps[i],
|
|
|
|
inputs[i]->x,
|
|
|
|
inputs[i]->len ),
|
|
|
|
statuses[i] );
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
break;
|
2022-04-20 09:14:51 +02:00
|
|
|
case 1: // input key, type DERIVE
|
|
|
|
case 11: // input key, type RAW
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE );
|
|
|
|
psa_set_key_algorithm( &attributes2, alg );
|
|
|
|
psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE );
|
|
|
|
|
|
|
|
// other secret of type RAW_DATA passed with input_key
|
2022-04-20 09:14:51 +02:00
|
|
|
if( other_key_input_type == 11 )
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes2,
|
2022-04-19 13:27:47 +02:00
|
|
|
inputs[i]->x, inputs[i]->len,
|
|
|
|
&keys[i] ) );
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
|
2022-04-19 13:27:47 +02:00
|
|
|
TEST_EQUAL( psa_key_derivation_input_key( &operation,
|
|
|
|
steps[i],
|
|
|
|
keys[i] ),
|
|
|
|
statuses[i] );
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
break;
|
|
|
|
case 2: // key agreement
|
|
|
|
psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE );
|
|
|
|
psa_set_key_algorithm( &attributes3, alg );
|
|
|
|
psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes3,
|
2022-04-19 13:27:47 +02:00
|
|
|
inputs[i]->x, inputs[i]->len,
|
|
|
|
&keys[i] ) );
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
|
|
|
|
TEST_EQUAL( psa_key_derivation_key_agreement(
|
|
|
|
&operation,
|
|
|
|
PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
|
|
|
|
keys[i], key_agreement_peer_key->x,
|
|
|
|
key_agreement_peer_key->len ), statuses[i] );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
TEST_ASSERT( ! "default case not supported" );
|
|
|
|
break;
|
2021-01-21 12:26:17 +01:00
|
|
|
}
|
|
|
|
|
2022-04-19 13:27:47 +02:00
|
|
|
if( statuses[i] != PSA_SUCCESS )
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
goto exit;
|
2019-05-29 17:35:49 +02:00
|
|
|
break;
|
|
|
|
default:
|
2022-05-11 12:22:57 +02:00
|
|
|
TEST_EQUAL( psa_key_derivation_input_bytes(
|
2019-05-29 17:35:49 +02:00
|
|
|
&operation, steps[i],
|
2022-05-11 12:22:57 +02:00
|
|
|
inputs[i]->x, inputs[i]->len ), statuses[i] );
|
|
|
|
|
|
|
|
if( statuses[i] != PSA_SUCCESS )
|
|
|
|
goto exit;
|
2019-05-29 17:35:49 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-01-07 22:59:38 +01:00
|
|
|
}
|
2019-05-29 17:35:49 +02:00
|
|
|
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
|
2019-05-16 17:53:40 +02:00
|
|
|
¤t_capacity ) );
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( current_capacity, requested_capacity );
|
2018-07-12 17:24:54 +02:00
|
|
|
expected_capacity = requested_capacity;
|
|
|
|
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
if( derive_type == 1 ) // output key
|
2018-07-12 17:24:54 +02:00
|
|
|
{
|
2022-04-20 10:06:38 +02:00
|
|
|
psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
|
|
|
|
|
|
|
|
/* For output key derivation secret must be provided using
|
|
|
|
input key, otherwise operation is not permitted. */
|
2022-04-21 11:40:18 +02:00
|
|
|
if( key_input_type == 1 )
|
2022-04-20 10:06:38 +02:00
|
|
|
expected_status = PSA_SUCCESS;
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT );
|
|
|
|
psa_set_key_algorithm( &attributes4, alg );
|
|
|
|
psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE );
|
2022-06-03 15:01:14 +02:00
|
|
|
psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) );
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
|
|
|
|
TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation,
|
2022-04-20 10:06:38 +02:00
|
|
|
&derived_key ), expected_status );
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
}
|
|
|
|
else // output bytes
|
|
|
|
{
|
|
|
|
/* Expansion phase. */
|
|
|
|
for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
|
2018-07-12 17:24:54 +02:00
|
|
|
{
|
test PSA key derivation: add positive and negative cases for mixed-psk
Mix-PSK-to-MS test vectors are generated using python-tls library:
https://github.com/python-tls/tls
Steps to generate test vectors:
1. git clone git@github.com:python-tls/tls.git
2. cd tls
3. python3 setup.py build
4. sudo python3 setup.py install
5. Use the python script below to generate Master Secret (see description for details):
"""
Script to derive MS using mixed PSK to MS algorithm.
Script can be used to generate expected result for mixed PSK to MS tests.
Script uses python tls library:
https://github.com/python-tls/tls
Example usage:
derive_ms.py <secret> <other_secret> <seed> <label> <hash>
derive_ms.py 01020304 ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f 6d617374657220736563726574 SHA256
secret : 01020304
other_secret : ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7
pms : 0030ce2fa604b6a3e08fc42eda74ab647adace1168b199ed178dbaae12521d68271d7df56eb56c55878034cf01bd887ba4d7000401020304
seed : 5bc0b19b4a8b24b07afe7ec65c471e94a7d518fcef06c3574315255c52afe21b5bc0b19b872b9b26508458f03603744d575f463a11ae7f1b090c012606fd3e9f
label : 6d617374657220736563726574
output : 168fecea35190f9df34c042f24ecaa5e7825337f2cd82719464df5462f16aae84cb38a65c0d612ca9273f998ad32c05b
"""
from cryptography.hazmat.primitives import hashes
from tls._common.prf import prf
import os
import sys
def build_pms(other_secret: bytes, secret: bytes) -> bytes:
other_secret_size = len(other_secret).to_bytes(2, byteorder='big')
secret_size = len(secret).to_bytes(2, byteorder='big')
return(other_secret_size + other_secret + secret_size + secret)
def derive_ms(secret: bytes, other_secret: bytes, seed: bytes, label: bytes, hash: hashes.HashAlgorithm) -> bytes:
return prf(build_pms(other_secret, secret), label, seed, hash, 48)
def main():
#check args
if len(sys.argv) != 6:
print("Invalid number of arguments. Expected: <secret> <other_secret> <seed> <label> <hash>" )
return
if sys.argv[5] != 'SHA384' and sys.argv[5] != 'SHA256':
print("Invalid hash algorithm. Expected: SHA256 or SHA384" )
return
secret = bytes.fromhex(sys.argv[1])
other_secret = bytes.fromhex(sys.argv[2])
seed = bytes.fromhex(sys.argv[3])
label = bytes.fromhex(sys.argv[4])
hash_func = hashes.SHA384() if sys.argv[5] == 'SHA384' else hashes.SHA256()
pms = build_pms(other_secret, secret)
actual_output = derive_ms(secret, other_secret, seed, label, hash_func)
print('secret : ' + secret.hex())
print('other_secret : ' + other_secret.hex())
print('pms : ' + pms.hex())
print('seed : ' + seed.hex())
print('label : ' + label.hex())
print('output : ' + actual_output.hex())
if __name__ == "__main__":
main()
Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
2022-04-01 13:40:48 +02:00
|
|
|
/* Read some bytes. */
|
|
|
|
status = psa_key_derivation_output_bytes( &operation,
|
|
|
|
output_buffer, output_sizes[i] );
|
|
|
|
if( expected_capacity == 0 && output_sizes[i] == 0 )
|
|
|
|
{
|
|
|
|
/* Reading 0 bytes when 0 bytes are available can go either way. */
|
|
|
|
TEST_ASSERT( status == PSA_SUCCESS ||
|
|
|
|
status == PSA_ERROR_INSUFFICIENT_DATA );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if( expected_capacity == 0 ||
|
|
|
|
output_sizes[i] > expected_capacity )
|
|
|
|
{
|
|
|
|
/* Capacity exceeded. */
|
|
|
|
TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
|
|
|
|
expected_capacity = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Success. Check the read data. */
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
if( output_sizes[i] != 0 )
|
|
|
|
ASSERT_COMPARE( output_buffer, output_sizes[i],
|
|
|
|
expected_outputs[i], output_sizes[i] );
|
|
|
|
/* Check the operation status. */
|
|
|
|
expected_capacity -= output_sizes[i];
|
|
|
|
PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
|
|
|
|
¤t_capacity ) );
|
|
|
|
TEST_EQUAL( expected_capacity, current_capacity );
|
2018-07-12 17:24:54 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_abort( &operation ) );
|
2018-07-12 17:24:54 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free( output_buffer );
|
2019-05-16 17:31:03 +02:00
|
|
|
psa_key_derivation_abort( &operation );
|
2020-08-04 14:58:35 +02:00
|
|
|
for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
|
|
|
|
psa_destroy_key( keys[i] );
|
2022-04-20 10:06:38 +02:00
|
|
|
psa_destroy_key( derived_key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-07-12 17:24:54 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-07-17 21:06:59 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void derive_full( int alg_arg,
|
|
|
|
data_t *key_data,
|
2019-06-25 14:24:52 +02:00
|
|
|
data_t *input1,
|
|
|
|
data_t *input2,
|
2018-07-17 21:06:59 +02:00
|
|
|
int requested_capacity_arg )
|
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-07-17 21:06:59 +02:00
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
size_t requested_capacity = requested_capacity_arg;
|
2019-05-16 17:31:03 +02:00
|
|
|
psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
|
2018-07-17 21:06:59 +02:00
|
|
|
unsigned char output_buffer[16];
|
|
|
|
size_t expected_capacity = requested_capacity;
|
|
|
|
size_t current_capacity;
|
2019-04-18 12:53:30 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-07-17 21:06:59 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-07-17 21:06:59 +02:00
|
|
|
|
2019-04-18 12:53:30 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
|
2018-07-17 21:06:59 +02:00
|
|
|
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
2020-08-04 14:58:35 +02:00
|
|
|
&key ) );
|
2018-07-17 21:06:59 +02:00
|
|
|
|
2021-02-12 23:48:20 +01:00
|
|
|
if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
|
|
|
|
input1->x, input1->len,
|
|
|
|
input2->x, input2->len,
|
|
|
|
requested_capacity ) )
|
2019-07-03 13:41:36 +02:00
|
|
|
goto exit;
|
2019-06-25 14:24:52 +02:00
|
|
|
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
|
2019-05-16 17:53:40 +02:00
|
|
|
¤t_capacity ) );
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( current_capacity, expected_capacity );
|
2018-07-17 21:06:59 +02:00
|
|
|
|
|
|
|
/* Expansion phase. */
|
|
|
|
while( current_capacity > 0 )
|
|
|
|
{
|
|
|
|
size_t read_size = sizeof( output_buffer );
|
|
|
|
if( read_size > current_capacity )
|
|
|
|
read_size = current_capacity;
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
|
2019-05-16 17:53:40 +02:00
|
|
|
output_buffer,
|
|
|
|
read_size ) );
|
2018-07-17 21:06:59 +02:00
|
|
|
expected_capacity -= read_size;
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
|
2019-05-16 17:53:40 +02:00
|
|
|
¤t_capacity ) );
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( current_capacity, expected_capacity );
|
2018-07-17 21:06:59 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 17:31:03 +02:00
|
|
|
/* Check that the operation refuses to go over capacity. */
|
|
|
|
TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
|
2019-02-14 12:48:10 +01:00
|
|
|
PSA_ERROR_INSUFFICIENT_DATA );
|
2018-07-17 21:06:59 +02:00
|
|
|
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_abort( &operation ) );
|
2018-07-17 21:06:59 +02:00
|
|
|
|
|
|
|
exit:
|
2019-05-16 17:31:03 +02:00
|
|
|
psa_key_derivation_abort( &operation );
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-07-17 21:06:59 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-07-03 14:51:30 +02:00
|
|
|
/* BEGIN_CASE */
|
2018-07-12 17:29:22 +02:00
|
|
|
void derive_key_exercise( int alg_arg,
|
|
|
|
data_t *key_data,
|
2019-07-03 14:51:30 +02:00
|
|
|
data_t *input1,
|
|
|
|
data_t *input2,
|
2018-07-12 17:29:22 +02:00
|
|
|
int derived_type_arg,
|
|
|
|
int derived_bits_arg,
|
|
|
|
int derived_usage_arg,
|
|
|
|
int derived_alg_arg )
|
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-07-12 17:29:22 +02:00
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_key_type_t derived_type = derived_type_arg;
|
|
|
|
size_t derived_bits = derived_bits_arg;
|
|
|
|
psa_key_usage_t derived_usage = derived_usage_arg;
|
|
|
|
psa_algorithm_t derived_alg = derived_alg_arg;
|
|
|
|
size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
|
2019-05-16 17:31:03 +02:00
|
|
|
psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
|
2019-04-18 12:53:30 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2019-04-18 21:44:46 +02:00
|
|
|
psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-07-12 17:29:22 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-07-12 17:29:22 +02:00
|
|
|
|
2019-04-18 12:53:30 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
2020-08-04 14:58:35 +02:00
|
|
|
&base_key ) );
|
2018-07-12 17:29:22 +02:00
|
|
|
|
|
|
|
/* Derive a key. */
|
2021-02-12 23:48:20 +01:00
|
|
|
if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
|
|
|
|
input1->x, input1->len,
|
|
|
|
input2->x, input2->len,
|
|
|
|
capacity ) )
|
2019-07-03 14:51:30 +02:00
|
|
|
goto exit;
|
|
|
|
|
2019-04-18 12:53:30 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, derived_usage );
|
|
|
|
psa_set_key_algorithm( &attributes, derived_alg );
|
|
|
|
psa_set_key_type( &attributes, derived_type );
|
2019-04-26 13:49:28 +02:00
|
|
|
psa_set_key_bits( &attributes, derived_bits );
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
|
2020-08-04 14:58:35 +02:00
|
|
|
&derived_key ) );
|
2018-07-12 17:29:22 +02:00
|
|
|
|
|
|
|
/* Test the key information */
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
|
2019-04-18 21:44:46 +02:00
|
|
|
TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
|
|
|
|
TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
|
2018-07-12 17:29:22 +02:00
|
|
|
|
|
|
|
/* Exercise the derived key. */
|
2021-02-12 23:48:20 +01:00
|
|
|
if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
|
2018-07-12 17:29:22 +02:00
|
|
|
goto exit;
|
|
|
|
|
|
|
|
exit:
|
2020-11-19 17:55:23 +01:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
2019-04-26 16:03:33 +02:00
|
|
|
psa_reset_key_attributes( &got_attributes );
|
2020-11-19 17:55:23 +01:00
|
|
|
|
|
|
|
psa_key_derivation_abort( &operation );
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( base_key );
|
|
|
|
psa_destroy_key( derived_key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-07-12 17:29:22 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-07-03 15:17:09 +02:00
|
|
|
/* BEGIN_CASE */
|
2018-07-12 17:29:22 +02:00
|
|
|
void derive_key_export( int alg_arg,
|
|
|
|
data_t *key_data,
|
2019-07-03 15:17:09 +02:00
|
|
|
data_t *input1,
|
|
|
|
data_t *input2,
|
2018-07-12 17:29:22 +02:00
|
|
|
int bytes1_arg,
|
|
|
|
int bytes2_arg )
|
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-07-12 17:29:22 +02:00
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
size_t bytes1 = bytes1_arg;
|
|
|
|
size_t bytes2 = bytes2_arg;
|
|
|
|
size_t capacity = bytes1 + bytes2;
|
2019-05-16 17:31:03 +02:00
|
|
|
psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
|
2018-09-27 13:54:18 +02:00
|
|
|
uint8_t *output_buffer = NULL;
|
|
|
|
uint8_t *export_buffer = NULL;
|
2019-04-18 12:53:30 +02:00
|
|
|
psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-07-12 17:29:22 +02:00
|
|
|
size_t length;
|
|
|
|
|
2018-09-27 13:54:18 +02:00
|
|
|
ASSERT_ALLOC( output_buffer, capacity );
|
|
|
|
ASSERT_ALLOC( export_buffer, capacity );
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-07-12 17:29:22 +02:00
|
|
|
|
2019-04-18 12:53:30 +02:00
|
|
|
psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
|
|
|
|
psa_set_key_algorithm( &base_attributes, alg );
|
|
|
|
psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
|
2020-08-04 14:58:35 +02:00
|
|
|
&base_key ) );
|
2018-07-12 17:29:22 +02:00
|
|
|
|
|
|
|
/* Derive some material and output it. */
|
2021-02-12 23:48:20 +01:00
|
|
|
if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
|
|
|
|
input1->x, input1->len,
|
|
|
|
input2->x, input2->len,
|
|
|
|
capacity ) )
|
2019-07-03 15:17:09 +02:00
|
|
|
goto exit;
|
|
|
|
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
|
2019-05-16 17:53:40 +02:00
|
|
|
output_buffer,
|
|
|
|
capacity ) );
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_abort( &operation ) );
|
2018-07-12 17:29:22 +02:00
|
|
|
|
|
|
|
/* Derive the same output again, but this time store it in key objects. */
|
2021-02-12 23:48:20 +01:00
|
|
|
if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
|
|
|
|
input1->x, input1->len,
|
|
|
|
input2->x, input2->len,
|
|
|
|
capacity ) )
|
2019-07-03 15:17:09 +02:00
|
|
|
goto exit;
|
|
|
|
|
2019-04-18 12:53:30 +02:00
|
|
|
psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
|
|
|
|
psa_set_key_algorithm( &derived_attributes, 0 );
|
|
|
|
psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
|
2019-04-26 13:49:28 +02:00
|
|
|
psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
|
2020-08-04 14:58:35 +02:00
|
|
|
&derived_key ) );
|
|
|
|
PSA_ASSERT( psa_export_key( derived_key,
|
2018-12-18 00:18:46 +01:00
|
|
|
export_buffer, bytes1,
|
|
|
|
&length ) );
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( length, bytes1 );
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_destroy_key( derived_key ) );
|
2019-04-26 13:49:28 +02:00
|
|
|
psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
|
2020-08-04 14:58:35 +02:00
|
|
|
&derived_key ) );
|
|
|
|
PSA_ASSERT( psa_export_key( derived_key,
|
2018-12-18 00:18:46 +01:00
|
|
|
export_buffer + bytes1, bytes2,
|
|
|
|
&length ) );
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( length, bytes2 );
|
2018-07-12 17:29:22 +02:00
|
|
|
|
|
|
|
/* Compare the outputs from the two runs. */
|
2018-12-18 00:40:50 +01:00
|
|
|
ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
|
|
|
|
export_buffer, capacity );
|
2018-07-12 17:29:22 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free( output_buffer );
|
|
|
|
mbedtls_free( export_buffer );
|
2019-05-16 17:31:03 +02:00
|
|
|
psa_key_derivation_abort( &operation );
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( base_key );
|
|
|
|
psa_destroy_key( derived_key );
|
2019-07-30 17:26:54 +02:00
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-11-24 16:29:10 +01:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void derive_key_type( int alg_arg,
|
|
|
|
data_t *key_data,
|
|
|
|
data_t *input1,
|
|
|
|
data_t *input2,
|
|
|
|
int key_type_arg, int bits_arg,
|
|
|
|
data_t *expected_export )
|
|
|
|
{
|
|
|
|
mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
const psa_algorithm_t alg = alg_arg;
|
|
|
|
const psa_key_type_t key_type = key_type_arg;
|
|
|
|
const size_t bits = bits_arg;
|
|
|
|
psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
|
|
|
|
const size_t export_buffer_size =
|
|
|
|
PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits );
|
|
|
|
uint8_t *export_buffer = NULL;
|
|
|
|
psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
size_t export_length;
|
|
|
|
|
|
|
|
ASSERT_ALLOC( export_buffer, export_buffer_size );
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
|
|
|
|
psa_set_key_algorithm( &base_attributes, alg );
|
|
|
|
psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
|
|
|
|
PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
|
|
|
|
&base_key ) );
|
|
|
|
|
2022-03-08 11:37:54 +01:00
|
|
|
if( mbedtls_test_psa_setup_key_derivation_wrap(
|
2021-11-24 16:29:10 +01:00
|
|
|
&operation, base_key, alg,
|
|
|
|
input1->x, input1->len,
|
|
|
|
input2->x, input2->len,
|
2022-03-08 11:37:54 +01:00
|
|
|
PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 )
|
2021-11-24 16:29:10 +01:00
|
|
|
goto exit;
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
|
|
|
|
psa_set_key_algorithm( &derived_attributes, 0 );
|
|
|
|
psa_set_key_type( &derived_attributes, key_type );
|
|
|
|
psa_set_key_bits( &derived_attributes, bits );
|
|
|
|
PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
|
|
|
|
&derived_key ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_export_key( derived_key,
|
|
|
|
export_buffer, export_buffer_size,
|
|
|
|
&export_length ) );
|
|
|
|
ASSERT_COMPARE( export_buffer, export_length,
|
|
|
|
expected_export->x, expected_export->len );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free( export_buffer );
|
|
|
|
psa_key_derivation_abort( &operation );
|
|
|
|
psa_destroy_key( base_key );
|
|
|
|
psa_destroy_key( derived_key );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-07-30 17:26:54 +02:00
|
|
|
/* BEGIN_CASE */
|
2019-07-31 15:14:44 +02:00
|
|
|
void derive_key( int alg_arg,
|
|
|
|
data_t *key_data, data_t *input1, data_t *input2,
|
|
|
|
int type_arg, int bits_arg,
|
2021-01-21 14:24:39 +01:00
|
|
|
int expected_status_arg,
|
|
|
|
int is_large_output )
|
2019-07-30 17:26:54 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
|
2019-07-30 17:26:54 +02:00
|
|
|
psa_algorithm_t alg = alg_arg;
|
2019-07-31 15:14:44 +02:00
|
|
|
psa_key_type_t type = type_arg;
|
2019-07-30 17:26:54 +02:00
|
|
|
size_t bits = bits_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
|
|
|
|
psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
|
|
|
|
psa_set_key_algorithm( &base_attributes, alg );
|
|
|
|
psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
|
|
|
|
PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
|
2020-08-04 14:58:35 +02:00
|
|
|
&base_key ) );
|
2019-07-30 17:26:54 +02:00
|
|
|
|
2021-02-12 23:48:20 +01:00
|
|
|
if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
|
|
|
|
input1->x, input1->len,
|
|
|
|
input2->x, input2->len,
|
|
|
|
SIZE_MAX ) )
|
2019-07-30 17:26:54 +02:00
|
|
|
goto exit;
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
|
|
|
|
psa_set_key_algorithm( &derived_attributes, 0 );
|
2019-07-31 15:14:44 +02:00
|
|
|
psa_set_key_type( &derived_attributes, type );
|
2019-07-30 17:26:54 +02:00
|
|
|
psa_set_key_bits( &derived_attributes, bits );
|
2021-01-21 14:24:39 +01:00
|
|
|
|
|
|
|
psa_status_t status =
|
|
|
|
psa_key_derivation_output_key( &derived_attributes,
|
|
|
|
&operation,
|
|
|
|
&derived_key );
|
|
|
|
if( is_large_output > 0 )
|
|
|
|
TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
|
|
|
|
TEST_EQUAL( status, expected_status );
|
2019-07-30 17:26:54 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_key_derivation_abort( &operation );
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( base_key );
|
|
|
|
psa_destroy_key( derived_key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-07-12 17:29:22 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-09-18 12:01:02 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void key_agreement_setup( int alg_arg,
|
2020-10-05 16:02:45 +02:00
|
|
|
int our_key_type_arg, int our_key_alg_arg,
|
|
|
|
data_t *our_key_data, data_t *peer_key_data,
|
2018-09-18 12:01:02 +02:00
|
|
|
int expected_status_arg )
|
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-09-18 12:01:02 +02:00
|
|
|
psa_algorithm_t alg = alg_arg;
|
2020-10-15 17:07:12 +02:00
|
|
|
psa_algorithm_t our_key_alg = our_key_alg_arg;
|
2018-09-18 12:01:02 +02:00
|
|
|
psa_key_type_t our_key_type = our_key_type_arg;
|
2019-05-16 17:31:03 +02:00
|
|
|
psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
|
2019-04-18 12:53:30 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2019-04-11 21:27:06 +02:00
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
psa_status_t status;
|
2018-09-18 12:01:02 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-09-18 12:01:02 +02:00
|
|
|
|
2019-04-18 12:53:30 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
|
2020-10-15 17:07:12 +02:00
|
|
|
psa_set_key_algorithm( &attributes, our_key_alg );
|
2019-04-18 12:53:30 +02:00
|
|
|
psa_set_key_type( &attributes, our_key_type );
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes,
|
|
|
|
our_key_data->x, our_key_data->len,
|
|
|
|
&our_key ) );
|
2018-09-18 12:01:02 +02:00
|
|
|
|
2019-04-11 21:27:06 +02:00
|
|
|
/* The tests currently include inputs that should fail at either step.
|
|
|
|
* Test cases that fail at the setup step should be changed to call
|
|
|
|
* key_derivation_setup instead, and this function should be renamed
|
|
|
|
* to key_agreement_fail. */
|
2019-05-16 17:31:03 +02:00
|
|
|
status = psa_key_derivation_setup( &operation, alg );
|
2019-04-11 21:27:06 +02:00
|
|
|
if( status == PSA_SUCCESS )
|
|
|
|
{
|
2019-05-16 17:53:40 +02:00
|
|
|
TEST_EQUAL( psa_key_derivation_key_agreement(
|
|
|
|
&operation, PSA_KEY_DERIVATION_INPUT_SECRET,
|
|
|
|
our_key,
|
|
|
|
peer_key_data->x, peer_key_data->len ),
|
2019-04-11 21:27:06 +02:00
|
|
|
expected_status );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( status == expected_status );
|
|
|
|
}
|
2018-09-18 12:01:02 +02:00
|
|
|
|
|
|
|
exit:
|
2019-05-16 17:31:03 +02:00
|
|
|
psa_key_derivation_abort( &operation );
|
2018-09-18 12:01:02 +02:00
|
|
|
psa_destroy_key( our_key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-09-18 12:01:02 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-04-11 22:12:38 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void raw_key_agreement( int alg_arg,
|
|
|
|
int our_key_type_arg, data_t *our_key_data,
|
|
|
|
data_t *peer_key_data,
|
|
|
|
data_t *expected_output )
|
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
|
2019-04-11 22:12:38 +02:00
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_key_type_t our_key_type = our_key_type_arg;
|
2019-04-18 12:53:30 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2019-04-11 22:12:38 +02:00
|
|
|
unsigned char *output = NULL;
|
|
|
|
size_t output_length = ~0;
|
2021-01-21 12:26:17 +01:00
|
|
|
size_t key_bits;
|
2019-04-11 22:12:38 +02:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
2019-04-18 12:53:30 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, our_key_type );
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes,
|
|
|
|
our_key_data->x, our_key_data->len,
|
|
|
|
&our_key ) );
|
2019-04-11 22:12:38 +02:00
|
|
|
|
2021-01-21 12:26:17 +01:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
|
|
|
|
key_bits = psa_get_key_bits( &attributes );
|
|
|
|
|
2022-04-13 23:25:52 +02:00
|
|
|
/* Validate size macros */
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( expected_output->len,
|
|
|
|
PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
|
|
|
|
TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ),
|
|
|
|
PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
|
2022-04-13 23:25:52 +02:00
|
|
|
|
|
|
|
/* Good case with exact output size */
|
|
|
|
ASSERT_ALLOC( output, expected_output->len );
|
2019-05-16 18:00:41 +02:00
|
|
|
PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
|
|
|
|
peer_key_data->x, peer_key_data->len,
|
|
|
|
output, expected_output->len,
|
|
|
|
&output_length ) );
|
2019-04-11 22:12:38 +02:00
|
|
|
ASSERT_COMPARE( output, output_length,
|
|
|
|
expected_output->x, expected_output->len );
|
2022-04-13 23:25:52 +02:00
|
|
|
mbedtls_free( output );
|
|
|
|
output = NULL;
|
|
|
|
output_length = ~0;
|
|
|
|
|
|
|
|
/* Larger buffer */
|
|
|
|
ASSERT_ALLOC( output, expected_output->len + 1 );
|
|
|
|
PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
|
|
|
|
peer_key_data->x, peer_key_data->len,
|
|
|
|
output, expected_output->len + 1,
|
|
|
|
&output_length ) );
|
|
|
|
ASSERT_COMPARE( output, output_length,
|
|
|
|
expected_output->x, expected_output->len );
|
|
|
|
mbedtls_free( output );
|
|
|
|
output = NULL;
|
|
|
|
output_length = ~0;
|
|
|
|
|
|
|
|
/* Buffer too small */
|
|
|
|
ASSERT_ALLOC( output, expected_output->len - 1 );
|
|
|
|
TEST_EQUAL( psa_raw_key_agreement( alg, our_key,
|
|
|
|
peer_key_data->x, peer_key_data->len,
|
|
|
|
output, expected_output->len - 1,
|
|
|
|
&output_length ),
|
|
|
|
PSA_ERROR_BUFFER_TOO_SMALL );
|
|
|
|
/* Not required by the spec, but good robustness */
|
2022-04-14 00:12:57 +02:00
|
|
|
TEST_LE_U( output_length, expected_output->len - 1 );
|
2022-04-13 23:25:52 +02:00
|
|
|
mbedtls_free( output );
|
|
|
|
output = NULL;
|
2019-04-11 22:12:38 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free( output );
|
|
|
|
psa_destroy_key( our_key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2019-04-11 22:12:38 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-09-18 12:11:34 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void key_agreement_capacity( int alg_arg,
|
|
|
|
int our_key_type_arg, data_t *our_key_data,
|
|
|
|
data_t *peer_key_data,
|
|
|
|
int expected_capacity_arg )
|
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-09-18 12:11:34 +02:00
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_key_type_t our_key_type = our_key_type_arg;
|
2019-05-16 17:31:03 +02:00
|
|
|
psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
|
2019-04-18 12:53:30 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-09-18 12:11:34 +02:00
|
|
|
size_t actual_capacity;
|
2018-10-25 22:36:12 +02:00
|
|
|
unsigned char output[16];
|
2018-09-18 12:11:34 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-09-18 12:11:34 +02:00
|
|
|
|
2019-04-18 12:53:30 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, our_key_type );
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes,
|
|
|
|
our_key_data->x, our_key_data->len,
|
|
|
|
&our_key ) );
|
2018-09-18 12:11:34 +02:00
|
|
|
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
|
2019-05-16 17:53:40 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_key_agreement(
|
|
|
|
&operation,
|
|
|
|
PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
|
|
|
|
peer_key_data->x, peer_key_data->len ) );
|
2019-04-11 22:13:20 +02:00
|
|
|
if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
|
|
|
|
{
|
|
|
|
/* The test data is for info="" */
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
|
2019-05-16 16:05:19 +02:00
|
|
|
PSA_KEY_DERIVATION_INPUT_INFO,
|
2019-04-11 22:13:20 +02:00
|
|
|
NULL, 0 ) );
|
|
|
|
}
|
2018-09-18 12:11:34 +02:00
|
|
|
|
2021-12-21 06:14:10 +01:00
|
|
|
/* Test the advertised capacity. */
|
Rename generator functions to psa_key_derivation_xxx
Generators are mostly about key derivation (currently: only about key
derivation). "Generator" is not a commonly used term in cryptography.
So favor "derivation" as terminology. Call a generator a key
derivation operation structure, since it behaves like other multipart
operation structures. Furthermore, the function names are not fully
consistent.
In this commit, I rename the functions to consistently have the prefix
"psa_key_derivation_". I used the following command:
perl -i -pe '%t = (
psa_crypto_generator_t => "psa_key_derivation_operation_t",
psa_crypto_generator_init => "psa_key_derivation_init",
psa_key_derivation_setup => "psa_key_derivation_setup",
psa_key_derivation_input_key => "psa_key_derivation_input_key",
psa_key_derivation_input_bytes => "psa_key_derivation_input_bytes",
psa_key_agreement => "psa_key_derivation_key_agreement",
psa_set_generator_capacity => "psa_key_derivation_set_capacity",
psa_get_generator_capacity => "psa_key_derivation_get_capacity",
psa_generator_read => "psa_key_derivation_output_bytes",
psa_generate_derived_key => "psa_key_derivation_output_key",
psa_generator_abort => "psa_key_derivation_abort",
PSA_CRYPTO_GENERATOR_INIT => "PSA_KEY_DERIVATION_OPERATION_INIT",
PSA_GENERATOR_UNBRIDLED_CAPACITY => "PSA_KEY_DERIVATION_UNLIMITED_CAPACITY",
); s/\b(@{[join("|", keys %t)]})\b/$t{$1}/ge' $(git ls-files)
2019-05-16 15:28:51 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_get_capacity(
|
2019-05-16 17:31:03 +02:00
|
|
|
&operation, &actual_capacity ) );
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
|
2018-09-18 12:11:34 +02:00
|
|
|
|
2018-10-25 22:36:12 +02:00
|
|
|
/* Test the actual capacity by reading the output. */
|
|
|
|
while( actual_capacity > sizeof( output ) )
|
|
|
|
{
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
|
2019-05-16 17:53:40 +02:00
|
|
|
output, sizeof( output ) ) );
|
2018-10-25 22:36:12 +02:00
|
|
|
actual_capacity -= sizeof( output );
|
|
|
|
}
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
|
2019-05-16 17:53:40 +02:00
|
|
|
output, actual_capacity ) );
|
2019-05-16 17:31:03 +02:00
|
|
|
TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
|
2019-02-14 12:48:10 +01:00
|
|
|
PSA_ERROR_INSUFFICIENT_DATA );
|
2018-10-25 22:36:12 +02:00
|
|
|
|
2018-09-18 12:11:34 +02:00
|
|
|
exit:
|
2019-05-16 17:31:03 +02:00
|
|
|
psa_key_derivation_abort( &operation );
|
2018-09-18 12:11:34 +02:00
|
|
|
psa_destroy_key( our_key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-09-18 12:11:34 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void key_agreement_output( int alg_arg,
|
|
|
|
int our_key_type_arg, data_t *our_key_data,
|
|
|
|
data_t *peer_key_data,
|
2018-10-25 22:37:15 +02:00
|
|
|
data_t *expected_output1, data_t *expected_output2 )
|
2018-09-18 12:11:34 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-09-18 12:11:34 +02:00
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_key_type_t our_key_type = our_key_type_arg;
|
2019-05-16 17:31:03 +02:00
|
|
|
psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
|
2019-04-18 12:53:30 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-10-25 22:37:15 +02:00
|
|
|
uint8_t *actual_output = NULL;
|
2018-09-18 12:11:34 +02:00
|
|
|
|
2018-10-25 22:37:15 +02:00
|
|
|
ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
|
|
|
|
expected_output2->len ) );
|
2018-09-18 12:11:34 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-09-18 12:11:34 +02:00
|
|
|
|
2019-04-18 12:53:30 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, our_key_type );
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes,
|
|
|
|
our_key_data->x, our_key_data->len,
|
|
|
|
&our_key ) );
|
2018-09-18 12:11:34 +02:00
|
|
|
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
|
2019-05-16 17:53:40 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_key_agreement(
|
|
|
|
&operation,
|
|
|
|
PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
|
|
|
|
peer_key_data->x, peer_key_data->len ) );
|
2019-04-11 22:13:20 +02:00
|
|
|
if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
|
|
|
|
{
|
|
|
|
/* The test data is for info="" */
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
|
2019-05-16 16:05:19 +02:00
|
|
|
PSA_KEY_DERIVATION_INPUT_INFO,
|
2019-04-11 22:13:20 +02:00
|
|
|
NULL, 0 ) );
|
|
|
|
}
|
2018-09-18 12:11:34 +02:00
|
|
|
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
|
2019-05-16 17:53:40 +02:00
|
|
|
actual_output,
|
|
|
|
expected_output1->len ) );
|
2018-12-18 00:40:50 +01:00
|
|
|
ASSERT_COMPARE( actual_output, expected_output1->len,
|
|
|
|
expected_output1->x, expected_output1->len );
|
2018-10-25 22:37:15 +02:00
|
|
|
if( expected_output2->len != 0 )
|
|
|
|
{
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
|
2019-05-16 17:53:40 +02:00
|
|
|
actual_output,
|
|
|
|
expected_output2->len ) );
|
2018-12-18 00:40:50 +01:00
|
|
|
ASSERT_COMPARE( actual_output, expected_output2->len,
|
|
|
|
expected_output2->x, expected_output2->len );
|
2018-10-25 22:37:15 +02:00
|
|
|
}
|
2018-09-18 12:11:34 +02:00
|
|
|
|
|
|
|
exit:
|
2019-05-16 17:31:03 +02:00
|
|
|
psa_key_derivation_abort( &operation );
|
2018-09-18 12:11:34 +02:00
|
|
|
psa_destroy_key( our_key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-09-18 12:11:34 +02:00
|
|
|
mbedtls_free( actual_output );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-06-19 22:00:52 +02:00
|
|
|
/* BEGIN_CASE */
|
2018-06-21 10:22:13 +02:00
|
|
|
void generate_random( int bytes_arg )
|
2018-06-19 22:00:52 +02:00
|
|
|
{
|
2018-06-21 10:22:13 +02:00
|
|
|
size_t bytes = bytes_arg;
|
2018-09-27 13:54:18 +02:00
|
|
|
unsigned char *output = NULL;
|
|
|
|
unsigned char *changed = NULL;
|
2018-06-21 10:22:13 +02:00
|
|
|
size_t i;
|
|
|
|
unsigned run;
|
2018-06-19 22:00:52 +02:00
|
|
|
|
2020-03-03 16:51:50 +01:00
|
|
|
TEST_ASSERT( bytes_arg >= 0 );
|
|
|
|
|
2021-02-08 19:50:26 +01:00
|
|
|
ASSERT_ALLOC( output, bytes );
|
2018-09-27 13:54:18 +02:00
|
|
|
ASSERT_ALLOC( changed, bytes );
|
2018-06-19 22:00:52 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-06-19 22:00:52 +02:00
|
|
|
|
2018-06-21 10:22:13 +02:00
|
|
|
/* Run several times, to ensure that every output byte will be
|
|
|
|
* nonzero at least once with overwhelming probability
|
|
|
|
* (2^(-8*number_of_runs)). */
|
|
|
|
for( run = 0; run < 10; run++ )
|
|
|
|
{
|
2018-09-26 18:19:24 +02:00
|
|
|
if( bytes != 0 )
|
|
|
|
memset( output, 0, bytes );
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_generate_random( output, bytes ) );
|
2018-06-19 22:00:52 +02:00
|
|
|
|
2018-06-21 10:22:13 +02:00
|
|
|
for( i = 0; i < bytes; i++ )
|
|
|
|
{
|
|
|
|
if( output[i] != 0 )
|
|
|
|
++changed[i];
|
|
|
|
}
|
|
|
|
}
|
2018-06-19 22:00:52 +02:00
|
|
|
|
2018-06-21 10:22:13 +02:00
|
|
|
/* Check that every byte was changed to nonzero at least once. This
|
|
|
|
* validates that psa_generate_random is overwriting every byte of
|
|
|
|
* the output buffer. */
|
|
|
|
for( i = 0; i < bytes; i++ )
|
2018-06-19 22:00:52 +02:00
|
|
|
{
|
2018-06-21 10:22:13 +02:00
|
|
|
TEST_ASSERT( changed[i] != 0 );
|
2018-06-19 22:00:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-06-21 10:22:13 +02:00
|
|
|
mbedtls_free( output );
|
|
|
|
mbedtls_free( changed );
|
2018-06-19 22:00:52 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-06-20 00:20:32 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void generate_key( int type_arg,
|
|
|
|
int bits_arg,
|
|
|
|
int usage_arg,
|
|
|
|
int alg_arg,
|
2021-01-21 14:24:39 +01:00
|
|
|
int expected_status_arg,
|
|
|
|
int is_large_key )
|
2018-06-20 00:20:32 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2018-06-20 00:20:32 +02:00
|
|
|
psa_key_type_t type = type_arg;
|
|
|
|
psa_key_usage_t usage = usage_arg;
|
|
|
|
size_t bits = bits_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2019-04-18 12:53:30 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2019-04-18 21:44:46 +02:00
|
|
|
psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-06-20 00:20:32 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-06-20 00:20:32 +02:00
|
|
|
|
2019-04-18 12:53:30 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, usage );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, type );
|
2019-04-26 13:49:28 +02:00
|
|
|
psa_set_key_bits( &attributes, bits );
|
2018-06-20 00:20:32 +02:00
|
|
|
|
|
|
|
/* Generate a key */
|
2021-01-21 14:24:39 +01:00
|
|
|
psa_status_t status = psa_generate_key( &attributes, &key );
|
|
|
|
|
|
|
|
if( is_large_key > 0 )
|
|
|
|
TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
|
|
|
|
TEST_EQUAL( status , expected_status );
|
2019-04-26 17:34:02 +02:00
|
|
|
if( expected_status != PSA_SUCCESS )
|
2019-04-18 12:53:30 +02:00
|
|
|
goto exit;
|
2018-06-20 00:20:32 +02:00
|
|
|
|
|
|
|
/* Test the key information */
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
|
2019-04-18 21:44:46 +02:00
|
|
|
TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
|
|
|
|
TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
|
2018-06-20 00:20:32 +02:00
|
|
|
|
2018-06-20 18:16:48 +02:00
|
|
|
/* Do something with the key according to its type and permitted usage. */
|
2021-02-12 23:48:20 +01:00
|
|
|
if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
|
2018-07-01 22:31:34 +02:00
|
|
|
goto exit;
|
2018-06-20 00:20:32 +02:00
|
|
|
|
|
|
|
exit:
|
2020-11-19 17:55:23 +01:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
2019-04-26 16:03:33 +02:00
|
|
|
psa_reset_key_attributes( &got_attributes );
|
2020-11-19 17:55:23 +01:00
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-06-20 00:20:32 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-09-06 15:24:41 +02:00
|
|
|
|
2021-03-18 18:50:08 +01:00
|
|
|
/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN:MBEDTLS_GENPRIME */
|
2019-04-26 17:34:02 +02:00
|
|
|
void generate_key_rsa( int bits_arg,
|
|
|
|
data_t *e_arg,
|
|
|
|
int expected_status_arg )
|
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2019-05-16 19:39:54 +02:00
|
|
|
psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
|
2019-04-26 17:34:02 +02:00
|
|
|
size_t bits = bits_arg;
|
|
|
|
psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
|
|
|
|
psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
uint8_t *exported = NULL;
|
|
|
|
size_t exported_size =
|
2020-12-18 14:23:51 +01:00
|
|
|
PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
|
2019-04-26 17:34:02 +02:00
|
|
|
size_t exported_length = SIZE_MAX;
|
|
|
|
uint8_t *e_read_buffer = NULL;
|
|
|
|
int is_default_public_exponent = 0;
|
2019-04-28 11:44:17 +02:00
|
|
|
size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
|
2019-04-26 17:34:02 +02:00
|
|
|
size_t e_read_length = SIZE_MAX;
|
|
|
|
|
|
|
|
if( e_arg->len == 0 ||
|
|
|
|
( e_arg->len == 3 &&
|
|
|
|
e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
|
|
|
|
{
|
|
|
|
is_default_public_exponent = 1;
|
|
|
|
e_read_size = 0;
|
|
|
|
}
|
|
|
|
ASSERT_ALLOC( e_read_buffer, e_read_size );
|
|
|
|
ASSERT_ALLOC( exported, exported_size );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, usage );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
|
|
|
|
e_arg->x, e_arg->len ) );
|
|
|
|
psa_set_key_bits( &attributes, bits );
|
|
|
|
|
|
|
|
/* Generate a key */
|
2020-08-04 14:58:35 +02:00
|
|
|
TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
|
2019-04-26 17:34:02 +02:00
|
|
|
if( expected_status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
/* Test the key information */
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
2019-04-26 17:34:02 +02:00
|
|
|
TEST_EQUAL( psa_get_key_type( &attributes ), type );
|
|
|
|
TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
|
|
|
|
PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
|
|
|
|
e_read_buffer, e_read_size,
|
|
|
|
&e_read_length ) );
|
|
|
|
if( is_default_public_exponent )
|
|
|
|
TEST_EQUAL( e_read_length, 0 );
|
|
|
|
else
|
|
|
|
ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
|
|
|
|
|
|
|
|
/* Do something with the key according to its type and permitted usage. */
|
2021-02-12 23:48:20 +01:00
|
|
|
if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
|
2019-04-26 17:34:02 +02:00
|
|
|
goto exit;
|
|
|
|
|
|
|
|
/* Export the key and check the public exponent. */
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_export_public_key( key,
|
2019-04-26 17:34:02 +02:00
|
|
|
exported, exported_size,
|
|
|
|
&exported_length ) );
|
|
|
|
{
|
|
|
|
uint8_t *p = exported;
|
|
|
|
uint8_t *end = exported + exported_length;
|
|
|
|
size_t len;
|
|
|
|
/* RSAPublicKey ::= SEQUENCE {
|
|
|
|
* modulus INTEGER, -- n
|
|
|
|
* publicExponent INTEGER } -- e
|
|
|
|
*/
|
|
|
|
TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
|
2019-05-16 17:53:40 +02:00
|
|
|
MBEDTLS_ASN1_SEQUENCE |
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED ) );
|
2021-02-13 00:25:53 +01:00
|
|
|
TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
|
2019-04-26 17:34:02 +02:00
|
|
|
TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_INTEGER ) );
|
|
|
|
if( len >= 1 && p[0] == 0 )
|
|
|
|
{
|
|
|
|
++p;
|
|
|
|
--len;
|
|
|
|
}
|
|
|
|
if( e_arg->len == 0 )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( len, 3 );
|
|
|
|
TEST_EQUAL( p[0], 1 );
|
|
|
|
TEST_EQUAL( p[1], 0 );
|
|
|
|
TEST_EQUAL( p[2], 1 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2020-11-19 17:55:23 +01:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes() or
|
|
|
|
* set by psa_set_key_domain_parameters() thus reset them as required.
|
|
|
|
*/
|
2019-04-26 17:34:02 +02:00
|
|
|
psa_reset_key_attributes( &attributes );
|
2020-11-19 17:55:23 +01:00
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2019-04-26 17:34:02 +02:00
|
|
|
mbedtls_free( e_read_buffer );
|
|
|
|
mbedtls_free( exported );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-06-18 18:27:26 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
|
2019-04-19 14:06:53 +02:00
|
|
|
void persistent_key_load_key_from_storage( data_t *data,
|
|
|
|
int type_arg, int bits_arg,
|
|
|
|
int usage_flags_arg, int alg_arg,
|
|
|
|
int generation_method )
|
2018-06-18 18:27:26 +02:00
|
|
|
{
|
2020-08-28 19:01:50 +02:00
|
|
|
mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
|
2019-04-19 14:06:53 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
|
2019-04-19 14:06:53 +02:00
|
|
|
psa_key_type_t type = type_arg;
|
|
|
|
size_t bits = bits_arg;
|
|
|
|
psa_key_usage_t usage_flags = usage_flags_arg;
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
2019-05-16 17:31:03 +02:00
|
|
|
psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
|
2018-06-18 18:27:26 +02:00
|
|
|
unsigned char *first_export = NULL;
|
|
|
|
unsigned char *second_export = NULL;
|
2020-12-18 14:23:51 +01:00
|
|
|
size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
|
2018-06-18 18:27:26 +02:00
|
|
|
size_t first_exported_length;
|
|
|
|
size_t second_exported_length;
|
|
|
|
|
2019-04-19 14:06:53 +02:00
|
|
|
if( usage_flags & PSA_KEY_USAGE_EXPORT )
|
|
|
|
{
|
|
|
|
ASSERT_ALLOC( first_export, export_size );
|
|
|
|
ASSERT_ALLOC( second_export, export_size );
|
|
|
|
}
|
2018-06-18 18:27:26 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init() );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
2019-05-15 16:12:22 +02:00
|
|
|
psa_set_key_id( &attributes, key_id );
|
2019-04-19 14:06:53 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, usage_flags );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, type );
|
2019-04-26 13:49:28 +02:00
|
|
|
psa_set_key_bits( &attributes, bits );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
2018-11-07 17:05:30 +01:00
|
|
|
switch( generation_method )
|
|
|
|
{
|
|
|
|
case IMPORT_KEY:
|
|
|
|
/* Import the key */
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
|
2020-08-04 14:58:35 +02:00
|
|
|
&key ) );
|
2018-11-07 17:05:30 +01:00
|
|
|
break;
|
2018-06-18 18:27:26 +02:00
|
|
|
|
2018-11-07 17:05:30 +01:00
|
|
|
case GENERATE_KEY:
|
|
|
|
/* Generate a key */
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_generate_key( &attributes, &key ) );
|
2018-11-07 17:05:30 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DERIVE_KEY:
|
2021-02-15 10:51:43 +01:00
|
|
|
#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
|
2019-04-19 14:06:53 +02:00
|
|
|
{
|
|
|
|
/* Create base key */
|
|
|
|
psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
|
|
|
|
psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_set_key_usage_flags( &base_attributes,
|
|
|
|
PSA_KEY_USAGE_DERIVE );
|
|
|
|
psa_set_key_algorithm( &base_attributes, derive_alg );
|
|
|
|
psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &base_attributes,
|
|
|
|
data->x, data->len,
|
|
|
|
&base_key ) );
|
2019-04-19 14:06:53 +02:00
|
|
|
/* Derive a key. */
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
|
2019-05-16 17:53:40 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_input_key(
|
|
|
|
&operation,
|
|
|
|
PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
|
2019-04-19 14:06:53 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_input_bytes(
|
2019-05-16 17:31:03 +02:00
|
|
|
&operation, PSA_KEY_DERIVATION_INPUT_INFO,
|
2019-04-19 14:06:53 +02:00
|
|
|
NULL, 0 ) );
|
2019-05-16 17:53:40 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_output_key( &attributes,
|
|
|
|
&operation,
|
2020-08-04 14:58:35 +02:00
|
|
|
&key ) );
|
2019-05-16 17:31:03 +02:00
|
|
|
PSA_ASSERT( psa_key_derivation_abort( &operation ) );
|
2019-04-19 14:06:53 +02:00
|
|
|
PSA_ASSERT( psa_destroy_key( base_key ) );
|
2020-08-04 14:58:35 +02:00
|
|
|
base_key = MBEDTLS_SVC_KEY_ID_INIT;
|
2019-04-19 14:06:53 +02:00
|
|
|
}
|
2021-01-12 00:02:15 +01:00
|
|
|
#else
|
|
|
|
TEST_ASSUME( ! "KDF not supported in this configuration" );
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
TEST_ASSERT( ! "generation_method not implemented in test" );
|
|
|
|
break;
|
2018-11-07 17:05:30 +01:00
|
|
|
}
|
2019-04-19 14:06:53 +02:00
|
|
|
psa_reset_key_attributes( &attributes );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
2019-04-19 14:06:53 +02:00
|
|
|
/* Export the key if permitted by the key policy. */
|
|
|
|
if( usage_flags & PSA_KEY_USAGE_EXPORT )
|
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_export_key( key,
|
2019-04-19 14:06:53 +02:00
|
|
|
first_export, export_size,
|
|
|
|
&first_exported_length ) );
|
|
|
|
if( generation_method == IMPORT_KEY )
|
|
|
|
ASSERT_COMPARE( data->x, data->len,
|
|
|
|
first_export, first_exported_length );
|
|
|
|
}
|
2018-06-18 18:27:26 +02:00
|
|
|
|
|
|
|
/* Shutdown and restart */
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_purge_key( key ) );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE();
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init() );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
|
|
|
/* Check key slot still contains key data */
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
2020-07-23 17:13:42 +02:00
|
|
|
TEST_ASSERT( mbedtls_svc_key_id_equal(
|
|
|
|
psa_get_key_id( &attributes ), key_id ) );
|
2019-04-19 14:06:53 +02:00
|
|
|
TEST_EQUAL( psa_get_key_lifetime( &attributes ),
|
|
|
|
PSA_KEY_LIFETIME_PERSISTENT );
|
|
|
|
TEST_EQUAL( psa_get_key_type( &attributes ), type );
|
|
|
|
TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
|
2021-05-13 12:05:01 +02:00
|
|
|
TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
|
2021-06-28 14:05:00 +02:00
|
|
|
mbedtls_test_update_key_usage_flags( usage_flags ) );
|
2019-04-19 14:06:53 +02:00
|
|
|
TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
2019-04-19 14:06:53 +02:00
|
|
|
/* Export the key again if permitted by the key policy. */
|
|
|
|
if( usage_flags & PSA_KEY_USAGE_EXPORT )
|
2018-11-07 17:05:30 +01:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_export_key( key,
|
2019-04-19 14:06:53 +02:00
|
|
|
second_export, export_size,
|
|
|
|
&second_exported_length ) );
|
2018-11-07 17:05:30 +01:00
|
|
|
ASSERT_COMPARE( first_export, first_exported_length,
|
|
|
|
second_export, second_exported_length );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do something with the key according to its type and permitted usage. */
|
2021-02-12 23:48:20 +01:00
|
|
|
if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
|
2018-11-07 17:05:30 +01:00
|
|
|
goto exit;
|
2018-06-18 18:27:26 +02:00
|
|
|
|
|
|
|
exit:
|
2020-11-19 17:55:23 +01:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
2019-04-26 16:03:33 +02:00
|
|
|
psa_reset_key_attributes( &attributes );
|
2020-11-19 17:55:23 +01:00
|
|
|
|
2018-06-18 18:27:26 +02:00
|
|
|
mbedtls_free( first_export );
|
|
|
|
mbedtls_free( second_export );
|
2019-05-16 17:31:03 +02:00
|
|
|
psa_key_derivation_abort( &operation );
|
2019-04-19 14:06:53 +02:00
|
|
|
psa_destroy_key( base_key );
|
2020-08-04 14:58:35 +02:00
|
|
|
psa_destroy_key( key );
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE();
|
2018-06-18 18:27:26 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2022-05-25 11:28:39 +02:00
|
|
|
|
2022-06-10 08:58:32 +02:00
|
|
|
/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
|
2022-05-25 11:28:39 +02:00
|
|
|
void ecjpake_setup( int alg_arg, int primitive_arg, int hash_arg, int role_arg,
|
2022-06-08 17:59:07 +02:00
|
|
|
int input_first, data_t *pw_data,
|
2022-05-25 11:28:39 +02:00
|
|
|
int expected_status_arg )
|
|
|
|
{
|
|
|
|
psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
|
|
|
|
psa_pake_operation_t operation = psa_pake_operation_init();
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_algorithm_t hash_alg = hash_arg;
|
|
|
|
psa_pake_role_t role = role_arg;
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t expected_status = expected_status_arg;
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
unsigned char *output_buffer = NULL;
|
|
|
|
size_t output_len = 0;
|
|
|
|
|
|
|
|
PSA_INIT( );
|
|
|
|
|
|
|
|
ASSERT_ALLOC( output_buffer,
|
2022-06-08 17:59:07 +02:00
|
|
|
PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
|
|
|
|
PSA_PAKE_STEP_KEY_SHARE) );
|
2022-05-25 11:28:39 +02:00
|
|
|
|
|
|
|
if( pw_data->len > 0 )
|
|
|
|
{
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
|
|
|
|
&key ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
psa_pake_cs_set_algorithm( &cipher_suite, alg );
|
|
|
|
psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
|
|
|
|
psa_pake_cs_set_hash( &cipher_suite, hash_alg );
|
|
|
|
|
2022-06-08 17:36:23 +02:00
|
|
|
PSA_ASSERT( psa_pake_abort( &operation ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
TEST_EQUAL( psa_pake_set_role( &operation, role ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
2022-06-08 17:59:07 +02:00
|
|
|
TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
NULL, 0, NULL ),
|
2022-06-08 17:36:23 +02:00
|
|
|
PSA_ERROR_BAD_STATE );
|
2022-06-08 17:59:07 +02:00
|
|
|
TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
|
2022-06-08 17:36:23 +02:00
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_pake_abort( &operation ) );
|
|
|
|
|
2022-05-25 11:28:39 +02:00
|
|
|
status = psa_pake_setup( &operation, &cipher_suite );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
|
2022-06-08 17:46:24 +02:00
|
|
|
TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
2022-05-25 11:28:39 +02:00
|
|
|
status = psa_pake_set_role( &operation, role );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
|
|
|
|
if( pw_data->len > 0 )
|
|
|
|
{
|
|
|
|
status = psa_pake_set_password_key( &operation, key );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( status, expected_status );
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
PSA_ASSERT( status );
|
|
|
|
}
|
|
|
|
|
2022-06-08 17:31:49 +02:00
|
|
|
TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
|
|
|
|
const uint8_t unsupported_id[] = "abcd";
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ),
|
|
|
|
PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ),
|
|
|
|
PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
|
2022-06-08 17:59:07 +02:00
|
|
|
/* First round */
|
|
|
|
if( input_first )
|
2022-05-25 11:28:39 +02:00
|
|
|
{
|
2022-06-08 17:59:07 +02:00
|
|
|
/* Invalid parameters */
|
|
|
|
TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
NULL, 0 ),
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
|
|
|
|
output_buffer, 66 ),
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
/* Invalid first step */
|
|
|
|
TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
output_buffer, 66 ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
|
|
|
TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
output_buffer, 66 ),
|
|
|
|
expected_status);
|
|
|
|
|
|
|
|
if( expected_status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
/* Buffer too large */
|
|
|
|
TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
output_buffer, 512 ),
|
|
|
|
PSA_ERROR_INSUFFICIENT_MEMORY );
|
|
|
|
|
|
|
|
/* The operation should be aborted at this point */
|
|
|
|
TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
output_buffer, 66 ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
2022-05-25 11:28:39 +02:00
|
|
|
}
|
|
|
|
else
|
2022-06-08 17:59:07 +02:00
|
|
|
{
|
|
|
|
/* Invalid parameters */
|
|
|
|
TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
NULL, 0, NULL ),
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
|
|
|
|
output_buffer, 512, &output_len ),
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT );
|
|
|
|
/* Invalid first step */
|
|
|
|
TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
|
|
|
|
output_buffer, 512, &output_len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
2022-05-25 11:28:39 +02:00
|
|
|
|
2022-06-08 17:59:07 +02:00
|
|
|
TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
|
|
|
|
output_buffer, 512, &output_len ),
|
|
|
|
expected_status );
|
2022-06-08 17:43:20 +02:00
|
|
|
|
2022-06-08 17:59:07 +02:00
|
|
|
if( expected_status == PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( output_len > 0 );
|
|
|
|
|
|
|
|
/* Buffer too small */
|
|
|
|
TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
output_buffer, 5, &output_len ),
|
|
|
|
PSA_ERROR_BUFFER_TOO_SMALL );
|
|
|
|
|
|
|
|
/* The operation should be aborted at this point */
|
|
|
|
TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
|
|
|
|
output_buffer, 512, &output_len ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
}
|
|
|
|
}
|
2022-05-25 11:28:39 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
PSA_ASSERT( psa_destroy_key( key ) );
|
|
|
|
PSA_ASSERT( psa_pake_abort( &operation ) );
|
|
|
|
mbedtls_free( output_buffer );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-06-15 15:28:32 +02:00
|
|
|
/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
|
|
|
|
void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg,
|
|
|
|
int client_input_first, int inject_error,
|
|
|
|
data_t *pw_data )
|
|
|
|
{
|
|
|
|
psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
|
|
|
|
psa_pake_operation_t server = psa_pake_operation_init();
|
|
|
|
psa_pake_operation_t client = psa_pake_operation_init();
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_algorithm_t hash_alg = hash_arg;
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
|
|
|
|
PSA_INIT( );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
|
|
|
|
&key ) );
|
|
|
|
|
|
|
|
psa_pake_cs_set_algorithm( &cipher_suite, alg );
|
|
|
|
psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
|
|
|
|
psa_pake_cs_set_hash( &cipher_suite, hash_alg );
|
|
|
|
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
|
|
|
|
PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
|
|
|
|
PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
|
|
|
|
PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client,
|
|
|
|
client_input_first, 1,
|
|
|
|
inject_error ), 1 );
|
|
|
|
|
|
|
|
if( inject_error == 1 || inject_error == 2 )
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client,
|
|
|
|
client_input_first, 2,
|
|
|
|
inject_error ), 1 );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_destroy_key( key );
|
|
|
|
psa_pake_abort( &server );
|
|
|
|
psa_pake_abort( &client );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-06-10 08:58:32 +02:00
|
|
|
/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
|
2022-05-25 11:28:39 +02:00
|
|
|
void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg,
|
2022-06-15 15:27:48 +02:00
|
|
|
int derive_alg_arg, data_t *pw_data,
|
|
|
|
int client_input_first )
|
2022-05-25 11:28:39 +02:00
|
|
|
{
|
|
|
|
psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
|
|
|
|
psa_pake_operation_t server = psa_pake_operation_init();
|
|
|
|
psa_pake_operation_t client = psa_pake_operation_init();
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_algorithm_t hash_alg = hash_arg;
|
|
|
|
psa_algorithm_t derive_alg = derive_alg_arg;
|
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_derivation_operation_t server_derive =
|
|
|
|
PSA_KEY_DERIVATION_OPERATION_INIT;
|
|
|
|
psa_key_derivation_operation_t client_derive =
|
|
|
|
PSA_KEY_DERIVATION_OPERATION_INIT;
|
|
|
|
|
|
|
|
PSA_INIT( );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
|
|
|
|
&key ) );
|
|
|
|
|
|
|
|
psa_pake_cs_set_algorithm( &cipher_suite, alg );
|
|
|
|
psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
|
|
|
|
psa_pake_cs_set_hash( &cipher_suite, hash_alg );
|
|
|
|
|
2022-06-15 11:32:11 +02:00
|
|
|
/* Get shared key */
|
|
|
|
PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) );
|
|
|
|
PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) );
|
|
|
|
|
|
|
|
if( PSA_ALG_IS_TLS12_PRF( derive_alg ) ||
|
|
|
|
PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) )
|
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive,
|
|
|
|
PSA_KEY_DERIVATION_INPUT_SEED,
|
|
|
|
(const uint8_t*) "", 0) );
|
|
|
|
PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive,
|
|
|
|
PSA_KEY_DERIVATION_INPUT_SEED,
|
|
|
|
(const uint8_t*) "", 0) );
|
|
|
|
}
|
|
|
|
|
2022-05-25 11:28:39 +02:00
|
|
|
PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
|
|
|
|
PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
|
|
|
|
PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
|
|
|
|
PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
|
|
|
|
|
2022-06-15 11:32:11 +02:00
|
|
|
TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
2022-06-15 15:27:48 +02:00
|
|
|
/* First round */
|
|
|
|
TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client,
|
|
|
|
client_input_first, 1, 0 ), 1 );
|
2022-05-25 11:28:39 +02:00
|
|
|
|
2022-06-15 11:32:11 +02:00
|
|
|
TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
|
|
|
|
PSA_ERROR_BAD_STATE );
|
|
|
|
|
2022-06-15 15:27:48 +02:00
|
|
|
/* Second round */
|
|
|
|
TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client,
|
|
|
|
client_input_first, 2, 0 ), 1 );
|
2022-05-25 11:28:39 +02:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) );
|
|
|
|
PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
psa_key_derivation_abort( &server_derive );
|
|
|
|
psa_key_derivation_abort( &client_derive );
|
|
|
|
psa_destroy_key( key );
|
|
|
|
psa_pake_abort( &server );
|
|
|
|
psa_pake_abort( &client );
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|