1 /** \file platform_builtin_keys.c
2  *
3  * \brief Test driver implementation of the builtin key support
4  */
5 
6 /*
7  *  Copyright The Mbed TLS Contributors
8  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9  */
10 
11 #include <test/helpers.h>
12 
13 #include <psa/crypto.h>
14 #include <psa/crypto_extra.h>
15 
16 #if defined(PSA_CRYPTO_DRIVER_TEST)
17 #include <test/drivers/test_driver.h>
18 #endif
19 
20 typedef struct {
21     psa_key_id_t builtin_key_id;
22     psa_key_lifetime_t lifetime;
23     psa_drv_slot_number_t slot_number;
24 } mbedtls_psa_builtin_key_description_t;
25 
26 static const mbedtls_psa_builtin_key_description_t builtin_keys[] = {
27 #if defined(PSA_CRYPTO_DRIVER_TEST)
28     /* For testing, assign the AES builtin key slot to the boundary values.
29      * ECDSA can be exercised on key ID MBEDTLS_PSA_KEY_ID_BUILTIN_MIN + 1. */
30     { MBEDTLS_PSA_KEY_ID_BUILTIN_MIN - 1,
31       PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
32           PSA_KEY_PERSISTENCE_READ_ONLY, PSA_CRYPTO_TEST_DRIVER_LOCATION),
33       PSA_CRYPTO_TEST_DRIVER_BUILTIN_AES_KEY_SLOT },
34     { MBEDTLS_PSA_KEY_ID_BUILTIN_MIN,
35       PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
36           PSA_KEY_PERSISTENCE_READ_ONLY, PSA_CRYPTO_TEST_DRIVER_LOCATION),
37       PSA_CRYPTO_TEST_DRIVER_BUILTIN_AES_KEY_SLOT },
38     { MBEDTLS_PSA_KEY_ID_BUILTIN_MIN + 1,
39       PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
40           PSA_KEY_PERSISTENCE_READ_ONLY, PSA_CRYPTO_TEST_DRIVER_LOCATION),
41       PSA_CRYPTO_TEST_DRIVER_BUILTIN_ECDSA_KEY_SLOT },
42     { MBEDTLS_PSA_KEY_ID_BUILTIN_MAX - 1,
43       PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
44           PSA_KEY_PERSISTENCE_READ_ONLY, PSA_CRYPTO_TEST_DRIVER_LOCATION),
45       PSA_CRYPTO_TEST_DRIVER_BUILTIN_AES_KEY_SLOT },
46     { MBEDTLS_PSA_KEY_ID_BUILTIN_MAX,
47       PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
48           PSA_KEY_PERSISTENCE_READ_ONLY, PSA_CRYPTO_TEST_DRIVER_LOCATION),
49       PSA_CRYPTO_TEST_DRIVER_BUILTIN_AES_KEY_SLOT },
50     { MBEDTLS_PSA_KEY_ID_BUILTIN_MAX + 1,
51       PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
52           PSA_KEY_PERSISTENCE_READ_ONLY, PSA_CRYPTO_TEST_DRIVER_LOCATION),
53       PSA_CRYPTO_TEST_DRIVER_BUILTIN_AES_KEY_SLOT },
54 #else
55     { 0, 0, 0 }
56 #endif
57 };
58 
mbedtls_psa_platform_get_builtin_key(mbedtls_svc_key_id_t key_id,psa_key_lifetime_t * lifetime,psa_drv_slot_number_t * slot_number)59 psa_status_t mbedtls_psa_platform_get_builtin_key(
60     mbedtls_svc_key_id_t key_id,
61     psa_key_lifetime_t *lifetime,
62     psa_drv_slot_number_t *slot_number)
63 {
64     psa_key_id_t app_key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key_id);
65     const mbedtls_psa_builtin_key_description_t *builtin_key;
66 
67     for (size_t i = 0;
68          i < (sizeof(builtin_keys) / sizeof(builtin_keys[0])); i++) {
69         builtin_key = &builtin_keys[i];
70         if (builtin_key->builtin_key_id == app_key_id) {
71             *lifetime = builtin_key->lifetime;
72             *slot_number = builtin_key->slot_number;
73             return PSA_SUCCESS;
74         }
75     }
76 
77     return PSA_ERROR_DOES_NOT_EXIST;
78 }
79