aboutsummaryrefslogtreecommitdiff
path: root/tests/suites/test_suite_psa_crypto_persistent_key.function
blob: bb8792313d45f1123983d8781efc17d3987f0736 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* BEGIN_HEADER */

/* The tests in this module verify the contents of key store files. They
 * access internal key storage functions directly. Some of the tests depend
 * on the storage format. On the other hand, these tests treat the storage
 * subsystem as a black box, and in particular have no reliance on the
 * internals of the ITS implementation.
 *
 * Note that if you need to make a change that affects how files are
 * stored, this may indicate that the key store is changing in a
 * backward-incompatible way! Think carefully about backward compatibility
 * before changing how test data is constructed or validated.
 */

#include <stdint.h>

#include "psa_crypto_slot_management.h"
#include "psa_crypto_storage.h"

#include "mbedtls/md.h"

#define PSA_KEY_STORAGE_MAGIC_HEADER "PSA\0KEY"
#define PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ( sizeof( PSA_KEY_STORAGE_MAGIC_HEADER ) )

/* Enforce the storage format for keys. The storage format is not a public
 * documented interface, but it must be preserved between versions so that
 * upgrades work smoothly, so it's a stable interface nonetheless.
 */
typedef struct {
    uint8_t magic[PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH];
    uint8_t version[4];
    uint8_t lifetime[sizeof( psa_key_lifetime_t )];
    uint8_t type[4];
    uint8_t policy[sizeof( psa_key_policy_t )];
    uint8_t data_len[4];
    uint8_t key_data[];
} psa_persistent_key_storage_format;

/* END_HEADER */

/* BEGIN_DEPENDENCIES
 * depends_on:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_PSA_CRYPTO_STORAGE_C
 * END_DEPENDENCIES
 */

/* BEGIN_CASE */
void format_storage_data_check( data_t *key_data,
                                data_t *expected_file_data,
                                int key_lifetime, int key_type, int key_bits,
                                int key_usage, int key_alg, int key_alg2 )
{
    uint8_t *file_data = NULL;
    size_t file_data_length =
        key_data->len + sizeof( psa_persistent_key_storage_format );
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;

    psa_set_key_lifetime( &attributes, key_lifetime );
    psa_set_key_type( &attributes, key_type );
    psa_set_key_bits( &attributes, key_bits );
    psa_set_key_usage_flags( &attributes, key_usage );
    psa_set_key_algorithm( &attributes, key_alg );
    psa_set_key_enrollment_algorithm( &attributes, key_alg2 );

    ASSERT_ALLOC( file_data, file_data_length );
    psa_format_key_data_for_storage( key_data->x, key_data->len,
                                     &attributes.core,
                                     file_data );

    ASSERT_COMPARE( expected_file_data->x, expected_file_data->len,
                    file_data, file_data_length );

exit:
    mbedtls_free( file_data );
}
/* END_CASE */

/* BEGIN_CASE */
void parse_storage_data_check( data_t *file_data,
                               data_t *expected_key_data,
                               int expected_key_lifetime,
                               int expected_key_type,
                               int expected_key_bits,
                               int expected_key_usage,
                               int expected_key_alg,
                               int expected_key_alg2,
                               int expected_status )
{
    uint8_t *key_data = NULL;
    size_t key_data_length = 0;
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
    psa_status_t status;

    status = psa_parse_key_data_from_storage( file_data->x, file_data->len,
                                              &key_data, &key_data_length,
                                              &attributes.core );

    TEST_EQUAL( status, expected_status );
    if( status != PSA_SUCCESS )
        goto exit;

    TEST_EQUAL( psa_get_key_lifetime( &attributes ),
                (psa_key_type_t) expected_key_lifetime );
    TEST_EQUAL( psa_get_key_type( &attributes ),
                (psa_key_type_t) expected_key_type );
    TEST_EQUAL( psa_get_key_bits( &attributes ),
                (psa_key_bits_t) expected_key_bits );
    TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
                (uint32_t) expected_key_usage );
    TEST_EQUAL( psa_get_key_algorithm( &attributes ),
                (uint32_t) expected_key_alg );
    TEST_EQUAL( psa_get_key_enrollment_algorithm( &attributes ),
                (uint32_t) expected_key_alg2 );
    ASSERT_COMPARE( expected_key_data->x, expected_key_data->len,
                    key_data, key_data_length );

exit:
    mbedtls_free( key_data );
}
/* END_CASE */

/* BEGIN_CASE */
void save_large_persistent_key( int data_length_arg, int expected_status )
{
    mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 42 );
    uint8_t *data = NULL;
    size_t data_length = data_length_arg;
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;

    ASSERT_ALLOC( data, data_length );

    PSA_ASSERT( psa_crypto_init() );

    psa_set_key_id( &attributes, key_id );
    psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );

    TEST_EQUAL( psa_import_key( &attributes, data, data_length, &key_id ),
                expected_status );

    if( expected_status == PSA_SUCCESS )
        PSA_ASSERT( psa_destroy_key( key_id ) );

exit:
    mbedtls_free( data );
    PSA_DONE();
    psa_destroy_persistent_key( key_id );
}
/* END_CASE */

/* BEGIN_CASE */
void persistent_key_destroy( int owner_id_arg, int key_id_arg, int restart,
                             int first_type_arg, data_t *first_data,
                             int second_type_arg, data_t *second_data )
{
    mbedtls_svc_key_id_t key_id =
        mbedtls_svc_key_id_make( owner_id_arg, key_id_arg );
    mbedtls_svc_key_id_t returned_key_id = MBEDTLS_SVC_KEY_ID_INIT;
    psa_key_type_t first_type = (psa_key_type_t) first_type_arg;
    psa_key_type_t second_type = (psa_key_type_t) second_type_arg;
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;

    PSA_ASSERT( psa_crypto_init() );

    psa_set_key_id( &attributes, key_id );
    psa_set_key_type( &attributes, first_type );

    PSA_ASSERT( psa_import_key( &attributes, first_data->x, first_data->len,
                                &returned_key_id ) );

    if( restart )
    {
        psa_close_key( key_id );
        PSA_DONE();
        PSA_ASSERT( psa_crypto_init() );
    }
    TEST_EQUAL( psa_is_key_present_in_storage( key_id ), 1 );

    /* Destroy the key */
    PSA_ASSERT( psa_destroy_key( key_id ) );

    /* Check key slot storage is removed */
    TEST_EQUAL( psa_is_key_present_in_storage( key_id ), 0 );

    /* Shutdown and restart */
    PSA_DONE();
    PSA_ASSERT( psa_crypto_init() );

    /* Create another key in the same slot */
    psa_set_key_id( &attributes, key_id );
    psa_set_key_type( &attributes, second_type );
    PSA_ASSERT( psa_import_key( &attributes, second_data->x, second_data->len,
                                &returned_key_id ) );

    PSA_ASSERT( psa_destroy_key( key_id ) );

exit:
    PSA_DONE();
    psa_destroy_persistent_key( key_id );
}
/* END_CASE */

/* BEGIN_CASE */
void persistent_key_import( int owner_id_arg, int key_id_arg, int type_arg,
                            data_t *data, int restart, int expected_status )
{
    mbedtls_svc_key_id_t key_id =
        mbedtls_svc_key_id_make( owner_id_arg, key_id_arg );
    mbedtls_svc_key_id_t returned_key_id;
    psa_key_type_t type = (psa_key_type_t) type_arg;
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;

    PSA_ASSERT( psa_crypto_init() );

    psa_set_key_id( &attributes, key_id );
    psa_set_key_type( &attributes, type );
    TEST_EQUAL( psa_import_key( &attributes, data->x, data->len, &returned_key_id ),
                expected_status );

    if( expected_status != PSA_SUCCESS )
    {
        TEST_ASSERT( mbedtls_svc_key_id_is_null( returned_key_id ) );
        TEST_EQUAL( psa_is_key_present_in_storage( key_id ), 0 );
        goto exit;
    }

    TEST_ASSERT( mbedtls_svc_key_id_equal( returned_key_id, key_id ) );

    if( restart )
    {
        PSA_ASSERT( psa_purge_key( key_id ) );
        PSA_DONE();
        PSA_ASSERT( psa_crypto_init() );
    }

    psa_reset_key_attributes( &attributes );
    PSA_ASSERT( psa_get_key_attributes( key_id, &attributes ) );
    TEST_ASSERT( mbedtls_svc_key_id_equal( psa_get_key_id( &attributes ),
                                           key_id ) );
    TEST_EQUAL( psa_get_key_lifetime( &attributes ),
                PSA_KEY_LIFETIME_PERSISTENT );
    TEST_EQUAL( psa_get_key_type( &attributes ), type );
    TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
    TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );

    PSA_ASSERT( psa_destroy_key( key_id ) );

exit:
    /*
     * Key attributes may have been returned by psa_get_key_attributes()
     * thus reset them as required.
     */
    psa_reset_key_attributes( &attributes );

    psa_destroy_persistent_key( key_id );
    PSA_DONE();
}
/* END_CASE */

/* BEGIN_CASE */
void import_export_persistent_key( data_t *data, int type_arg,
                                   int expected_bits,
                                   int restart, int key_not_exist )
{
    mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 42 );
    psa_key_type_t type = (psa_key_type_t) type_arg;
    mbedtls_svc_key_id_t returned_key_id = MBEDTLS_SVC_KEY_ID_INIT;
    unsigned char *exported = NULL;
    size_t export_size = data->len;
    size_t exported_length;
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;

    ASSERT_ALLOC( exported, export_size );

    PSA_ASSERT( psa_crypto_init( ) );

    psa_set_key_id( &attributes, key_id );
    psa_set_key_type( &attributes, type );
    psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );

    /* Import the key */
    PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
                                &returned_key_id ) );


    if( restart )
    {
        PSA_ASSERT( psa_purge_key( key_id ) );
        PSA_DONE();
        PSA_ASSERT( psa_crypto_init() );
    }

    /* Test the key information */
    psa_reset_key_attributes( &attributes );
    PSA_ASSERT( psa_get_key_attributes( key_id, &attributes ) );
    TEST_ASSERT( mbedtls_svc_key_id_equal(
                     psa_get_key_id( &attributes ), key_id ) );
    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 ), (size_t) expected_bits );
    TEST_EQUAL( psa_get_key_usage_flags( &attributes ), PSA_KEY_USAGE_EXPORT );
    TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );

    TEST_EQUAL( psa_is_key_present_in_storage( key_id ), 1 );

    if( key_not_exist )
    {
        psa_destroy_persistent_key( key_id );
    }
    /* Export the key */
    PSA_ASSERT( psa_export_key( key_id, exported, export_size,
                                &exported_length ) );

    ASSERT_COMPARE( data->x, data->len, exported, exported_length );

    /* Destroy the key */
    PSA_ASSERT( psa_destroy_key( key_id ) );
    TEST_EQUAL( psa_is_key_present_in_storage( key_id ), 0 );

exit:
    /*
     * Key attributes may have been returned by psa_get_key_attributes()
     * thus reset them as required.
     */
    psa_reset_key_attributes( &attributes );

    mbedtls_free( exported );
    PSA_DONE( );
    psa_destroy_persistent_key( key_id );
}
/* END_CASE */

/* BEGIN_CASE */
void destroy_nonexistent( int id_arg, int expected_status_arg )
{
    mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( 1, id_arg );
    psa_status_t expected_status = expected_status_arg;

    PSA_INIT( );

    TEST_EQUAL( expected_status, psa_destroy_key( id ) );

exit:
    PSA_DONE( );
}
/* END_CASE */