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