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
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23 #include <test/helpers.h>
24
25 #include <psa/crypto.h>
26 #include <psa/crypto_extra.h>
27
28 #if defined(PSA_CRYPTO_DRIVER_TEST)
29 #include <test/drivers/test_driver.h>
30 #endif
31
32 typedef struct {
33 psa_key_id_t builtin_key_id;
34 psa_key_lifetime_t lifetime;
35 psa_drv_slot_number_t slot_number;
36 } mbedtls_psa_builtin_key_description_t;
37
38 static const mbedtls_psa_builtin_key_description_t builtin_keys[] = {
39 #if defined(PSA_CRYPTO_DRIVER_TEST)
40 /* For testing, assign the AES builtin key slot to the boundary values.
41 * ECDSA can be exercised on key ID MBEDTLS_PSA_KEY_ID_BUILTIN_MIN + 1. */
42 { MBEDTLS_PSA_KEY_ID_BUILTIN_MIN - 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_MIN,
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_MIN + 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_ECDSA_KEY_SLOT },
54 { MBEDTLS_PSA_KEY_ID_BUILTIN_MAX - 1,
55 PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
56 PSA_KEY_PERSISTENCE_READ_ONLY, PSA_CRYPTO_TEST_DRIVER_LOCATION),
57 PSA_CRYPTO_TEST_DRIVER_BUILTIN_AES_KEY_SLOT },
58 { MBEDTLS_PSA_KEY_ID_BUILTIN_MAX,
59 PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
60 PSA_KEY_PERSISTENCE_READ_ONLY, PSA_CRYPTO_TEST_DRIVER_LOCATION),
61 PSA_CRYPTO_TEST_DRIVER_BUILTIN_AES_KEY_SLOT },
62 { MBEDTLS_PSA_KEY_ID_BUILTIN_MAX + 1,
63 PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
64 PSA_KEY_PERSISTENCE_READ_ONLY, PSA_CRYPTO_TEST_DRIVER_LOCATION),
65 PSA_CRYPTO_TEST_DRIVER_BUILTIN_AES_KEY_SLOT },
66 #else
67 { 0, 0, 0 }
68 #endif
69 };
70
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)71 psa_status_t mbedtls_psa_platform_get_builtin_key(
72 mbedtls_svc_key_id_t key_id,
73 psa_key_lifetime_t *lifetime,
74 psa_drv_slot_number_t *slot_number)
75 {
76 psa_key_id_t app_key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key_id);
77 const mbedtls_psa_builtin_key_description_t *builtin_key;
78
79 for (size_t i = 0;
80 i < (sizeof(builtin_keys) / sizeof(builtin_keys[0])); i++) {
81 builtin_key = &builtin_keys[i];
82 if (builtin_key->builtin_key_id == app_key_id) {
83 *lifetime = builtin_key->lifetime;
84 *slot_number = builtin_key->slot_number;
85 return PSA_SUCCESS;
86 }
87 }
88
89 return PSA_ERROR_DOES_NOT_EXIST;
90 }
91