blob: 8b9d7463ac0b50f7cfaa773dfecd4b32ccde3338 [file] [log] [blame]
Gilles Peskine961849f2018-11-30 18:54:54 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskine961849f2018-11-30 18:54:54 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskine961849f2018-11-30 18:54:54 +010019 */
20
21#ifndef PSA_CRYPTO_SLOT_MANAGEMENT_H
22#define PSA_CRYPTO_SLOT_MANAGEMENT_H
23
Gilles Peskine011e4282019-06-26 18:34:38 +020024#include "psa/crypto.h"
Ronald Cronc4d1b512020-07-31 11:26:37 +020025#include "psa_crypto_core.h"
Gilles Peskine011e4282019-06-26 18:34:38 +020026#include "psa_crypto_se.h"
27
Gilles Peskine961849f2018-11-30 18:54:54 +010028/* Number of key slots (plus one because 0 is not used).
29 * The value is a compile-time constant for now, for simplicity. */
30#define PSA_KEY_SLOT_COUNT 32
31
Ronald Cron2a993152020-07-17 14:13:26 +020032/** Range of volatile key identifiers.
33 *
34 * The last PSA_KEY_SLOT_COUNT identifiers of the implementation range
35 * of key identifiers are reserved for volatile key identifiers.
Ronald Cron96783552020-10-19 12:06:30 +020036 * A volatile key identifier is equal to #PSA_KEY_ID_VOLATILE_MIN plus the
Ronald Cron2a993152020-07-17 14:13:26 +020037 * index of the key slot containing the volatile key definition.
38 */
39
40/** The minimum value for a volatile key identifier.
41 */
42#define PSA_KEY_ID_VOLATILE_MIN ( PSA_KEY_ID_VENDOR_MAX - \
43 PSA_KEY_SLOT_COUNT + 1 )
44
45/** The maximum value for a volatile key identifier.
46 */
47#define PSA_KEY_ID_VOLATILE_MAX PSA_KEY_ID_VENDOR_MAX
48
Ronald Cron97c8ad52020-10-15 11:17:11 +020049/** Test whether a key identifier is a volatile key identifier.
50 *
51 * \param key_id Key identifier to test.
52 *
53 * \retval 1
54 * The key identifier is a volatile key identifier.
55 * \retval 0
56 * The key identifier is not a volatile key identifier.
57 */
58static inline int psa_key_id_is_volatile( psa_key_id_t key_id )
59{
60 return( ( key_id >= PSA_KEY_ID_VOLATILE_MIN ) &&
61 ( key_id <= PSA_KEY_ID_VOLATILE_MAX ) );
62}
63
Ronald Cron5c522922020-11-14 16:35:34 +010064/** Get the description of a key given its identifier and lock it.
Gilles Peskine09829032018-12-10 17:00:38 +010065 *
Ronald Cron5c522922020-11-14 16:35:34 +010066 * The descriptions of volatile keys and loaded persistent keys are stored in
67 * key slots. This function returns a pointer to the key slot containing the
68 * description of a key given its identifier.
Ronald Cronc4d1b512020-07-31 11:26:37 +020069 *
Ronald Cron5c522922020-11-14 16:35:34 +010070 * In case of a persistent key, the function loads the description of the key
71 * into a key slot if not already done.
Ronald Cronc4d1b512020-07-31 11:26:37 +020072 *
Ronald Cron5c522922020-11-14 16:35:34 +010073 * On success, the returned key slot is locked. It is the responsibility of
74 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +020075 *
Ronald Cronc4d1b512020-07-31 11:26:37 +020076 * \param key Key identifier to query.
Gilles Peskine09829032018-12-10 17:00:38 +010077 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
Ronald Cronc4d1b512020-07-31 11:26:37 +020078 * key slot containing the description of the key
79 * identified by \p key.
Gilles Peskine09829032018-12-10 17:00:38 +010080 *
Ronald Cronc4d1b512020-07-31 11:26:37 +020081 * \retval #PSA_SUCCESS
82 * The pointer to the key slot containing the description of the key
Ronald Cron7587ae42020-11-11 15:04:25 +010083 * identified by \p key was returned. The key slot counter was
84 * implemented.
Ronald Cronc4d1b512020-07-31 11:26:37 +020085 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine09829032018-12-10 17:00:38 +010086 * The library has not been initialized.
Ronald Cronc4d1b512020-07-31 11:26:37 +020087 * \retval #PSA_ERROR_INVALID_HANDLE
88 * \p key is not a valid key identifier.
89 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
90 * \p key is a persistent key identifier. The implementation does not
91 * have sufficient resources to load the persistent key. This can be
92 * due to a lack of empty key slot, or available memory.
93 * \retval #PSA_ERROR_DOES_NOT_EXIST
94 * There is no key with key identifier \p key.
95 * \retval #PSA_ERROR_CORRUPTION_DETECTED
96 * \retval #PSA_ERROR_STORAGE_FAILURE
97 * \retval #PSA_ERROR_DATA_CORRUPT
Gilles Peskine09829032018-12-10 17:00:38 +010098 */
Ronald Cron5c522922020-11-14 16:35:34 +010099psa_status_t psa_get_and_lock_key_slot( mbedtls_svc_key_id_t key,
100 psa_key_slot_t **p_slot );
Gilles Peskine66fb1262018-12-10 16:29:04 +0100101
Gilles Peskine09829032018-12-10 17:00:38 +0100102/** Initialize the key slot structures.
103 *
Ronald Cron96783552020-10-19 12:06:30 +0200104 * \retval #PSA_SUCCESS
Gilles Peskine09829032018-12-10 17:00:38 +0100105 * Currently this function always succeeds.
106 */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100107psa_status_t psa_initialize_key_slots( void );
108
Gilles Peskine09829032018-12-10 17:00:38 +0100109/** Delete all data from key slots in memory.
110 *
111 * This does not affect persistent storage. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100112void psa_wipe_all_key_slots( void );
113
Gilles Peskine41e50d22019-07-31 15:01:55 +0200114/** Find a free key slot.
115 *
116 * This function returns a key slot that is available for use and is in its
Ronald Cron5c522922020-11-14 16:35:34 +0100117 * ground state (all-bits-zero). On success, the key slot is locked. It is
118 * the responsibility of the caller to unlock the key slot when it does not
119 * access it anymore.
Gilles Peskinef46f81c2019-05-27 14:53:10 +0200120 *
Ronald Cron2a993152020-07-17 14:13:26 +0200121 * \param[out] volatile_key_id On success, volatile key identifier
122 * associated to the returned slot.
123 * \param[out] p_slot On success, a pointer to the slot.
Gilles Peskinef46f81c2019-05-27 14:53:10 +0200124 *
125 * \retval #PSA_SUCCESS
126 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine267c6562019-05-27 19:01:54 +0200127 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef46f81c2019-05-27 14:53:10 +0200128 */
Ronald Cronc4d1b512020-07-31 11:26:37 +0200129psa_status_t psa_get_empty_key_slot( psa_key_id_t *volatile_key_id,
Gilles Peskineedbed562019-08-07 18:19:59 +0200130 psa_key_slot_t **p_slot );
Gilles Peskinef46f81c2019-05-27 14:53:10 +0200131
Ronald Cron5c522922020-11-14 16:35:34 +0100132/** Lock a key slot.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200133 *
Ronald Cron5c522922020-11-14 16:35:34 +0100134 * This function increments the key slot lock counter by one.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200135 *
136 * \param[in] slot The key slot.
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100137 *
138 * \retval #PSA_SUCCESS
Ronald Cron5c522922020-11-14 16:35:34 +0100139 The key slot lock counter was incremented.
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100140 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Ronald Cron5c522922020-11-14 16:35:34 +0100141 * The lock counter already reached its maximum value and was not
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100142 * increased.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200143 */
Ronald Cron5c522922020-11-14 16:35:34 +0100144static inline psa_status_t psa_lock_key_slot( psa_key_slot_t *slot )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200145{
Ronald Cron5c522922020-11-14 16:35:34 +0100146 if( slot->lock_count >= SIZE_MAX )
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100147 return( PSA_ERROR_CORRUPTION_DETECTED );
148
Ronald Cron5c522922020-11-14 16:35:34 +0100149 slot->lock_count++;
Ronald Croncbf6a1d2020-11-13 15:59:59 +0100150
151 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200152}
153
Ronald Cron5c522922020-11-14 16:35:34 +0100154/** Unlock a key slot.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200155 *
Ronald Cron5c522922020-11-14 16:35:34 +0100156 * This function decrements the key slot lock counter by one.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200157 *
158 * \note To ease the handling of errors in retrieving a key slot
159 * a NULL input pointer is valid, and the function returns
160 * successfully without doing anything in that case.
161 *
162 * \param[in] slot The key slot.
163 * \retval #PSA_SUCCESS
Ronald Cron5c522922020-11-14 16:35:34 +0100164 * \p slot is NULL or the key slot lock counter has been
Ronald Cronf95a2b12020-10-22 15:24:49 +0200165 * decremented successfully.
166 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Ronald Cron5c522922020-11-14 16:35:34 +0100167 * The lock counter was equal to 0.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200168 *
169 */
Ronald Cron5c522922020-11-14 16:35:34 +0100170psa_status_t psa_unlock_key_slot( psa_key_slot_t *slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200171
Gilles Peskine011e4282019-06-26 18:34:38 +0200172/** Test whether a lifetime designates a key in an external cryptoprocessor.
173 *
174 * \param lifetime The lifetime to test.
175 *
176 * \retval 1
177 * The lifetime designates an external key. There should be a
178 * registered driver for this lifetime, otherwise the key cannot
179 * be created or manipulated.
180 * \retval 0
181 * The lifetime designates a key that is volatile or in internal
182 * storage.
183 */
184static inline int psa_key_lifetime_is_external( psa_key_lifetime_t lifetime )
185{
Steven Cooremanc59de6a2020-06-08 18:28:25 +0200186 return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime )
187 != PSA_KEY_LOCATION_LOCAL_STORAGE );
Gilles Peskine011e4282019-06-26 18:34:38 +0200188}
189
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200190/** Validate a key's location.
Gilles Peskined167b942019-04-19 18:19:40 +0200191 *
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200192 * This function checks whether the key's attributes point to a location that
193 * is known to the PSA Core, and returns the driver function table if the key
194 * is to be found in an external location.
Gilles Peskined167b942019-04-19 18:19:40 +0200195 *
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200196 * \param[in] lifetime The key lifetime attribute.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200197 * \param[out] p_drv On success, when a key is located in external
198 * storage, returns a pointer to the driver table
199 * associated with the key's storage location.
Gilles Peskine011e4282019-06-26 18:34:38 +0200200 *
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200201 * \retval #PSA_SUCCESS
202 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskined167b942019-04-19 18:19:40 +0200203 */
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200204psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime,
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200205 psa_se_drv_table_entry_t **p_drv );
206
Ronald Crond2ed4812020-07-17 16:11:30 +0200207/** Validate the persistence of a key.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200208 *
Ronald Cron27238fc2020-07-23 12:30:41 +0200209 * \param[in] lifetime The key lifetime attribute.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200210 *
211 * \retval #PSA_SUCCESS
Ronald Crond2ed4812020-07-17 16:11:30 +0200212 * \retval #PSA_ERROR_INVALID_ARGUMENT The key is persistent but persistent
213 * keys are not supported.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200214 */
Ronald Crond2ed4812020-07-17 16:11:30 +0200215psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime );
216
217/** Validate a key identifier.
218 *
Ronald Cronfc9c5562020-10-15 19:24:49 +0200219 * \param[in] key The key identifier.
220 * \param[in] vendor_ok Non-zero to indicate that key identifiers in the
221 * vendor range are allowed, volatile key identifiers
222 * excepted \c 0 otherwise.
Ronald Crond2ed4812020-07-17 16:11:30 +0200223 *
224 * \retval #PSA_SUCCESS The identifier is valid.
225 * \retval #PSA_ERROR_INVALID_ARGUMENT The key identifier is not valid.
226 */
Ronald Croncbd7bea2020-11-11 14:57:44 +0100227psa_status_t psa_validate_key_id( mbedtls_svc_key_id_t key, int vendor_ok );
Gilles Peskined167b942019-04-19 18:19:40 +0200228
Gilles Peskine961849f2018-11-30 18:54:54 +0100229#endif /* PSA_CRYPTO_SLOT_MANAGEMENT_H */