1/* BEGIN_HEADER */
2#include "psa/crypto.h"
3/* END_HEADER */
4
5/* BEGIN_DEPENDENCIES
6 * depends_on:MBEDTLS_PSA_CRYPTO_CLIENT
7 * END_DEPENDENCIES
8 */
9
10/* BEGIN_CASE */
11void attributes_set_get( int owner_id_arg, int id_arg, int lifetime_arg,
12                         int usage_flags_arg, int alg_arg,
13                         int type_arg, int bits_arg )
14{
15    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
16    mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( owner_id_arg, id_arg );
17    psa_key_lifetime_t lifetime = lifetime_arg;
18    psa_key_usage_t usage_flags = usage_flags_arg;
19    psa_algorithm_t alg = alg_arg;
20    psa_key_type_t type = type_arg;
21    size_t bits = bits_arg;
22
23    TEST_EQUAL(
24        MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
25    TEST_EQUAL(
26        MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
27    TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
28    TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
29    TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
30    TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
31    TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
32
33    psa_set_key_id( &attributes, id );
34    psa_set_key_lifetime( &attributes, lifetime );
35    psa_set_key_usage_flags( &attributes, usage_flags );
36    psa_set_key_algorithm( &attributes, alg );
37    psa_set_key_type( &attributes, type );
38    psa_set_key_bits( &attributes, bits );
39
40    TEST_ASSERT( mbedtls_svc_key_id_equal(
41                     psa_get_key_id( &attributes ), id ) );
42    TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
43    TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
44    TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
45    TEST_EQUAL( psa_get_key_type( &attributes ), type );
46    TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
47
48    psa_reset_key_attributes( &attributes );
49
50    TEST_EQUAL(
51        MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
52    TEST_EQUAL(
53        MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
54    TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
55    TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
56    TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
57    TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
58    TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
59}
60/* END_CASE */
61
62/* BEGIN_CASE */
63void persistence_attributes( int id1_arg, int owner_id1_arg, int lifetime_arg,
64                             int id2_arg, int owner_id2_arg,
65                             int expected_id_arg, int expected_owner_id_arg,
66                             int expected_lifetime_arg )
67{
68    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
69    mbedtls_svc_key_id_t id1 =
70        mbedtls_svc_key_id_make( owner_id1_arg, id1_arg );
71    psa_key_lifetime_t lifetime = lifetime_arg;
72    mbedtls_svc_key_id_t id2 =
73        mbedtls_svc_key_id_make( owner_id2_arg, id2_arg );
74    mbedtls_svc_key_id_t expected_id =
75        mbedtls_svc_key_id_make( expected_owner_id_arg, expected_id_arg );
76    psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
77
78    if( id1_arg != -1 )
79        psa_set_key_id( &attributes, id1 );
80    if( lifetime_arg != -1 )
81        psa_set_key_lifetime( &attributes, lifetime );
82    if( id2_arg != -1 )
83        psa_set_key_id( &attributes, id2 );
84
85    TEST_ASSERT( mbedtls_svc_key_id_equal(
86                     psa_get_key_id( &attributes ), expected_id ) );
87    TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
88}
89/* END_CASE */
90
91/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
92void slot_number_attribute( )
93{
94    psa_key_slot_number_t slot_number = 0xdeadbeef;
95    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
96
97    /* Initially, there is no slot number. */
98    TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
99                PSA_ERROR_INVALID_ARGUMENT );
100
101    /* Test setting a slot number. */
102    psa_set_key_slot_number( &attributes, 0 );
103    PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
104    TEST_EQUAL( slot_number, 0 );
105
106    /* Test changing the slot number. */
107    psa_set_key_slot_number( &attributes, 42 );
108    PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
109    TEST_EQUAL( slot_number, 42 );
110
111    /* Test clearing the slot number. */
112    psa_clear_key_slot_number( &attributes );
113    TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
114                PSA_ERROR_INVALID_ARGUMENT );
115
116    /* Clearing again should have no effect. */
117    psa_clear_key_slot_number( &attributes );
118    TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
119                PSA_ERROR_INVALID_ARGUMENT );
120
121    /* Test that reset clears the slot number. */
122    psa_set_key_slot_number( &attributes, 42 );
123    PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
124    TEST_EQUAL( slot_number, 42 );
125    psa_reset_key_attributes( &attributes );
126    TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
127                PSA_ERROR_INVALID_ARGUMENT );
128}
129/* END_CASE */
130