1/* BEGIN_HEADER */
2
3#include <psa/crypto.h>
4
5#include <test/psa_crypto_helpers.h>
6#include <test/psa_exercise_key.h>
7
8#include <psa_crypto_its.h>
9
10#define TEST_FLAG_EXERCISE      0x00000001
11#define TEST_FLAG_READ_ONLY     0x00000002
12
13/** Write a key with the given attributes and key material to storage.
14 * Test that it has the expected representation.
15 *
16 * On error, including if the key representation in storage differs,
17 * mark the test case as failed and return 0. On success, return 1.
18 */
19static int test_written_key( const psa_key_attributes_t *attributes,
20                             const data_t *material,
21                             psa_storage_uid_t uid,
22                             const data_t *expected_representation )
23{
24    mbedtls_svc_key_id_t created_key_id = MBEDTLS_SVC_KEY_ID_INIT;
25    uint8_t *actual_representation = NULL;
26    size_t length;
27    struct psa_storage_info_t storage_info;
28    int ok = 0;
29
30    /* Create a key with the given parameters. */
31    PSA_ASSERT( psa_import_key( attributes, material->x, material->len,
32                                &created_key_id ) );
33    TEST_ASSERT( mbedtls_svc_key_id_equal( psa_get_key_id( attributes ),
34                                           created_key_id ) );
35
36    /* Check that the key is represented as expected. */
37    PSA_ASSERT( psa_its_get_info( uid, &storage_info ) );
38    TEST_EQUAL( storage_info.size, expected_representation->len );
39    ASSERT_ALLOC( actual_representation, storage_info.size );
40    PSA_ASSERT( psa_its_get( uid, 0, storage_info.size,
41                             actual_representation, &length ) );
42    ASSERT_COMPARE( expected_representation->x, expected_representation->len,
43                    actual_representation, length );
44
45    ok = 1;
46
47exit:
48    mbedtls_free( actual_representation );
49    return( ok );
50}
51
52/** Check if a key is exportable. */
53static int can_export( const psa_key_attributes_t *attributes )
54{
55    if( psa_get_key_usage_flags( attributes ) & PSA_KEY_USAGE_EXPORT )
56        return( 1 );
57    else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( attributes ) ) )
58        return( 1 );
59    else
60        return( 0 );
61}
62
63/** Write a key with the given representation to storage, then check
64 * that it has the given attributes and (if exportable) key material.
65 *
66 * On error, including if the key representation in storage differs,
67 * mark the test case as failed and return 0. On success, return 1.
68 */
69static int test_read_key( const psa_key_attributes_t *expected_attributes,
70                          const data_t *expected_material,
71                          psa_storage_uid_t uid,
72                          const data_t *representation,
73                          int flags )
74{
75    psa_key_attributes_t actual_attributes = PSA_KEY_ATTRIBUTES_INIT;
76    mbedtls_svc_key_id_t key_id = psa_get_key_id( expected_attributes );
77    struct psa_storage_info_t storage_info;
78    int ok = 0;
79    uint8_t *exported_material = NULL;
80    size_t length;
81
82    /* Prime the storage with a key file. */
83    PSA_ASSERT( psa_its_set( uid, representation->len, representation->x, 0 ) );
84
85    /* Check that the injected key exists and looks as expected. */
86    PSA_ASSERT( psa_get_key_attributes( key_id, &actual_attributes ) );
87    TEST_ASSERT( mbedtls_svc_key_id_equal( key_id,
88                                           psa_get_key_id( &actual_attributes ) ) );
89    TEST_EQUAL( psa_get_key_lifetime( expected_attributes ),
90                psa_get_key_lifetime( &actual_attributes ) );
91    TEST_EQUAL( psa_get_key_type( expected_attributes ),
92                psa_get_key_type( &actual_attributes ) );
93    TEST_EQUAL( psa_get_key_bits( expected_attributes ),
94                psa_get_key_bits( &actual_attributes ) );
95    TEST_EQUAL( psa_get_key_usage_flags( expected_attributes ),
96                psa_get_key_usage_flags( &actual_attributes ) );
97    TEST_EQUAL( psa_get_key_algorithm( expected_attributes ),
98                psa_get_key_algorithm( &actual_attributes ) );
99    TEST_EQUAL( psa_get_key_enrollment_algorithm( expected_attributes ),
100                psa_get_key_enrollment_algorithm( &actual_attributes ) );
101    if( can_export( expected_attributes ) )
102    {
103        ASSERT_ALLOC( exported_material, expected_material->len );
104        PSA_ASSERT( psa_export_key( key_id,
105                                    exported_material, expected_material->len,
106                                    &length ) );
107        ASSERT_COMPARE( expected_material->x, expected_material->len,
108                        exported_material, length );
109    }
110
111    if( flags & TEST_FLAG_EXERCISE )
112    {
113        TEST_ASSERT( mbedtls_test_psa_exercise_key(
114                         key_id,
115                         psa_get_key_usage_flags( expected_attributes ),
116                         psa_get_key_algorithm( expected_attributes ) ) );
117    }
118
119
120    if( flags & TEST_FLAG_READ_ONLY )
121    {
122        /* Read-only keys cannot be removed through the API.
123         * The key will be removed through ITS in the cleanup code below. */
124        TEST_EQUAL( PSA_ERROR_NOT_PERMITTED, psa_destroy_key( key_id ) );
125    }
126    else
127    {
128        /* Destroy the key. Confirm through direct access to the storage. */
129        PSA_ASSERT( psa_destroy_key( key_id ) );
130        TEST_EQUAL( PSA_ERROR_DOES_NOT_EXIST,
131                    psa_its_get_info( uid, &storage_info ) );
132    }
133
134    ok = 1;
135
136exit:
137    psa_reset_key_attributes( &actual_attributes );
138    psa_its_remove( uid );
139    mbedtls_free( exported_material );
140    return( ok );
141}
142
143/* END_HEADER */
144
145/* BEGIN_DEPENDENCIES
146 * depends_on:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_PSA_CRYPTO_STORAGE_C
147 * END_DEPENDENCIES
148 */
149
150/* BEGIN_CASE */
151void key_storage_save( int lifetime_arg, int type_arg, int bits_arg,
152                       int usage_arg, int alg_arg, int alg2_arg,
153                       data_t *material,
154                       data_t *representation )
155{
156    /* Forward compatibility: save a key in the current format and
157     * check that it has the expected format so that future versions
158     * will still be able to read it. */
159
160    psa_key_lifetime_t lifetime = lifetime_arg;
161    psa_key_type_t type = type_arg;
162    size_t bits = bits_arg;
163    psa_key_usage_t usage = usage_arg;
164    psa_algorithm_t alg = alg_arg;
165    psa_algorithm_t alg2 = alg2_arg;
166    mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 0, 1 );
167    psa_storage_uid_t uid = 1;
168    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
169
170    PSA_INIT( );
171    TEST_USES_KEY_ID( key_id );
172
173    psa_set_key_lifetime( &attributes, lifetime );
174    psa_set_key_id( &attributes, key_id );
175    psa_set_key_type( &attributes, type );
176    psa_set_key_bits( &attributes, bits );
177    psa_set_key_usage_flags( &attributes, usage );
178    psa_set_key_algorithm( &attributes, alg );
179    psa_set_key_enrollment_algorithm( &attributes, alg2 );
180
181    /* This is the current storage format. Test that we know exactly how
182     * the key is stored. The stability of the test data in future
183     * versions of Mbed TLS will guarantee that future versions
184     * can read back what this version wrote. */
185    TEST_ASSERT( test_written_key( &attributes, material,
186                                   uid, representation ) );
187
188exit:
189    psa_reset_key_attributes( &attributes );
190    psa_destroy_key( key_id );
191    PSA_DONE( );
192}
193/* END_CASE */
194
195/* BEGIN_CASE */
196void key_storage_read( int lifetime_arg, int type_arg, int bits_arg,
197                       int usage_arg, int alg_arg, int alg2_arg,
198                       data_t *material,
199                       data_t *representation, int flags )
200{
201    /* Backward compatibility: read a key in the format of a past version
202     * and check that this version can use it. */
203
204    psa_key_lifetime_t lifetime = lifetime_arg;
205    psa_key_type_t type = type_arg;
206    size_t bits = bits_arg;
207    psa_key_usage_t usage = usage_arg;
208    psa_algorithm_t alg = alg_arg;
209    psa_algorithm_t alg2 = alg2_arg;
210    mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 0, 1 );
211    psa_storage_uid_t uid = 1;
212    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
213
214    PSA_INIT( );
215    TEST_USES_KEY_ID( key_id );
216
217    psa_set_key_lifetime( &attributes, lifetime );
218    psa_set_key_id( &attributes, key_id );
219    psa_set_key_type( &attributes, type );
220    psa_set_key_bits( &attributes, bits );
221    psa_set_key_usage_flags( &attributes, usage );
222    psa_set_key_algorithm( &attributes, alg );
223    psa_set_key_enrollment_algorithm( &attributes, alg2 );
224
225    /* Test that we can use a key with the given representation. This
226     * guarantees backward compatibility with keys that were stored by
227     * past versions of Mbed TLS. */
228    TEST_ASSERT( test_read_key( &attributes, material,
229                                uid, representation, flags ) );
230
231exit:
232    psa_reset_key_attributes( &attributes );
233    PSA_DONE( );
234}
235/* END_CASE */
236