1/* BEGIN_HEADER */
2
3#include "psa/crypto.h"
4#include "test/psa_crypto_helpers.h"
5
6#define INVALID_KEY_ID mbedtls_svc_key_id_make( 0, 0xfedcba98 )
7
8/* END_HEADER */
9
10/* BEGIN_DEPENDENCIES
11 * depends_on:MBEDTLS_PSA_CRYPTO_C
12 * END_DEPENDENCIES
13 */
14
15/* BEGIN_CASE */
16void generate_key( int key_type_arg, int bits_arg, int expected_status_arg)
17{
18    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
19    mbedtls_svc_key_id_t key_id = INVALID_KEY_ID;
20
21    // key lifetiem, usage flags, algorithm are irrelevant for this test
22    psa_key_type_t key_type = key_type_arg;
23    size_t bits = bits_arg;
24    psa_status_t expected_status = expected_status_arg;
25
26    PSA_ASSERT( psa_crypto_init( ) );
27    psa_set_key_type( &attributes, key_type );
28    psa_set_key_bits( &attributes, bits );
29    TEST_EQUAL( psa_generate_key( &attributes, &key_id ),
30                expected_status );
31
32    // Verify attributes of the created key on success
33    if ( expected_status == PSA_SUCCESS )
34    {
35        psa_reset_key_attributes(&attributes);
36        PSA_ASSERT( psa_get_key_attributes( key_id, &attributes ) );
37        TEST_EQUAL( psa_get_key_lifetime( &attributes ), PSA_KEY_LIFETIME_VOLATILE );
38        TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
39        TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
40        TEST_EQUAL( psa_get_key_type( &attributes ), key_type );
41        TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
42    }
43
44exit:
45    psa_reset_key_attributes(&attributes);
46    psa_destroy_key( key_id );
47    PSA_DONE( );
48}
49/* END_CASE */
50