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 {
34     psa_key_id_t builtin_key_id;
35     psa_key_lifetime_t lifetime;
36     psa_drv_slot_number_t slot_number;
37 } mbedtls_psa_builtin_key_description_t;
38 
39 static const mbedtls_psa_builtin_key_description_t builtin_keys[] = {
40 #if defined(PSA_CRYPTO_DRIVER_TEST)
41     /* For testing, assign the AES builtin key slot to the boundary values.
42      * ECDSA can be exercised on key ID MBEDTLS_PSA_KEY_ID_BUILTIN_MIN + 1. */
43     { MBEDTLS_PSA_KEY_ID_BUILTIN_MIN - 1,
44       PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
45         PSA_KEY_PERSISTENCE_READ_ONLY, PSA_CRYPTO_TEST_DRIVER_LOCATION ),
46       PSA_CRYPTO_TEST_DRIVER_BUILTIN_AES_KEY_SLOT },
47     { MBEDTLS_PSA_KEY_ID_BUILTIN_MIN,
48       PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
49         PSA_KEY_PERSISTENCE_READ_ONLY, PSA_CRYPTO_TEST_DRIVER_LOCATION ),
50       PSA_CRYPTO_TEST_DRIVER_BUILTIN_AES_KEY_SLOT },
51     { MBEDTLS_PSA_KEY_ID_BUILTIN_MIN + 1,
52       PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
53         PSA_KEY_PERSISTENCE_READ_ONLY, PSA_CRYPTO_TEST_DRIVER_LOCATION ),
54       PSA_CRYPTO_TEST_DRIVER_BUILTIN_ECDSA_KEY_SLOT},
55     { MBEDTLS_PSA_KEY_ID_BUILTIN_MAX - 1,
56       PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
57         PSA_KEY_PERSISTENCE_READ_ONLY, PSA_CRYPTO_TEST_DRIVER_LOCATION ),
58       PSA_CRYPTO_TEST_DRIVER_BUILTIN_AES_KEY_SLOT},
59     { MBEDTLS_PSA_KEY_ID_BUILTIN_MAX,
60       PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
61         PSA_KEY_PERSISTENCE_READ_ONLY, PSA_CRYPTO_TEST_DRIVER_LOCATION ),
62       PSA_CRYPTO_TEST_DRIVER_BUILTIN_AES_KEY_SLOT},
63     { MBEDTLS_PSA_KEY_ID_BUILTIN_MAX + 1,
64       PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
65         PSA_KEY_PERSISTENCE_READ_ONLY, PSA_CRYPTO_TEST_DRIVER_LOCATION ),
66       PSA_CRYPTO_TEST_DRIVER_BUILTIN_AES_KEY_SLOT},
67 #else
68     {0, 0, 0}
69 #endif
70 };
71 
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)72 psa_status_t mbedtls_psa_platform_get_builtin_key(
73     mbedtls_svc_key_id_t key_id,
74     psa_key_lifetime_t *lifetime,
75     psa_drv_slot_number_t *slot_number )
76 {
77     psa_key_id_t app_key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key_id );
78     const mbedtls_psa_builtin_key_description_t *builtin_key;
79 
80     for( size_t i = 0;
81          i < ( sizeof( builtin_keys ) / sizeof( builtin_keys[0] ) ); i++ )
82     {
83         builtin_key = &builtin_keys[i];
84         if( builtin_key->builtin_key_id == app_key_id )
85         {
86             *lifetime = builtin_key->lifetime;
87             *slot_number = builtin_key->slot_number;
88             return( PSA_SUCCESS );
89         }
90     }
91 
92     return( PSA_ERROR_DOES_NOT_EXIST );
93 }
94