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 #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
14 
15 #include <psa/crypto.h>
16 #include <psa/crypto_extra.h>
17 
18 #if defined(PSA_CRYPTO_DRIVER_TEST)
19 #include <test/drivers/test_driver.h>
20 #endif
21 
22 typedef struct {
23     psa_key_id_t builtin_key_id;
24     psa_key_lifetime_t lifetime;
25     psa_drv_slot_number_t slot_number;
26 } mbedtls_psa_builtin_key_description_t;
27 
28 static const mbedtls_psa_builtin_key_description_t builtin_keys[] = {
29 #if defined(PSA_CRYPTO_DRIVER_TEST)
30     /* For testing, assign the AES builtin key slot to the boundary values.
31      * ECDSA can be exercised on key ID MBEDTLS_PSA_KEY_ID_BUILTIN_MIN + 1. */
32     { MBEDTLS_PSA_KEY_ID_BUILTIN_MIN - 1,
33       PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
34           PSA_KEY_PERSISTENCE_READ_ONLY, PSA_CRYPTO_TEST_DRIVER_LOCATION),
35       PSA_CRYPTO_TEST_DRIVER_BUILTIN_AES_KEY_SLOT },
36     { MBEDTLS_PSA_KEY_ID_BUILTIN_MIN,
37       PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
38           PSA_KEY_PERSISTENCE_READ_ONLY, PSA_CRYPTO_TEST_DRIVER_LOCATION),
39       PSA_CRYPTO_TEST_DRIVER_BUILTIN_AES_KEY_SLOT },
40     { MBEDTLS_PSA_KEY_ID_BUILTIN_MIN + 1,
41       PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
42           PSA_KEY_PERSISTENCE_READ_ONLY, PSA_CRYPTO_TEST_DRIVER_LOCATION),
43       PSA_CRYPTO_TEST_DRIVER_BUILTIN_ECDSA_KEY_SLOT },
44     { MBEDTLS_PSA_KEY_ID_BUILTIN_MAX - 1,
45       PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
46           PSA_KEY_PERSISTENCE_READ_ONLY, PSA_CRYPTO_TEST_DRIVER_LOCATION),
47       PSA_CRYPTO_TEST_DRIVER_BUILTIN_AES_KEY_SLOT },
48     { MBEDTLS_PSA_KEY_ID_BUILTIN_MAX,
49       PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
50           PSA_KEY_PERSISTENCE_READ_ONLY, PSA_CRYPTO_TEST_DRIVER_LOCATION),
51       PSA_CRYPTO_TEST_DRIVER_BUILTIN_AES_KEY_SLOT },
52     { MBEDTLS_PSA_KEY_ID_BUILTIN_MAX + 1,
53       PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
54           PSA_KEY_PERSISTENCE_READ_ONLY, PSA_CRYPTO_TEST_DRIVER_LOCATION),
55       PSA_CRYPTO_TEST_DRIVER_BUILTIN_AES_KEY_SLOT },
56 #else
57     { 0, 0, 0 }
58 #endif
59 };
60 
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)61 psa_status_t mbedtls_psa_platform_get_builtin_key(
62     mbedtls_svc_key_id_t key_id,
63     psa_key_lifetime_t *lifetime,
64     psa_drv_slot_number_t *slot_number)
65 {
66     psa_key_id_t app_key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key_id);
67     const mbedtls_psa_builtin_key_description_t *builtin_key;
68 
69     for (size_t i = 0;
70          i < (sizeof(builtin_keys) / sizeof(builtin_keys[0])); i++) {
71         builtin_key = &builtin_keys[i];
72         if (builtin_key->builtin_key_id == app_key_id) {
73             *lifetime = builtin_key->lifetime;
74             *slot_number = builtin_key->slot_number;
75             return PSA_SUCCESS;
76         }
77     }
78 
79     return PSA_ERROR_DOES_NOT_EXIST;
80 }
81 
82 #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
83