blob: b1d66e4ee042e123bf46a27e497e7a6bc76f3974 [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.
36 * A volatile key identifier is equal to PSA_KEY_ID_VOLATILE_MIN plus the
37 * 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 Cronc4d1b512020-07-31 11:26:37 +020049/** Retrieve the description of a key given its identifier.
Gilles Peskine09829032018-12-10 17:00:38 +010050 *
Ronald Cronc4d1b512020-07-31 11:26:37 +020051 * The descriptions of volatile keys and loaded persistent keys are
52 * stored in key slots. This function returns a pointer to the key slot
53 * containing the description of a key given its identifier.
54 *
55 * In case of a persistent key, the function loads the description of the key
56 * into a key slot if not already done.
57 *
58 * \param key Key identifier to query.
Gilles Peskine09829032018-12-10 17:00:38 +010059 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
Ronald Cronc4d1b512020-07-31 11:26:37 +020060 * key slot containing the description of the key
61 * identified by \p key.
Gilles Peskine09829032018-12-10 17:00:38 +010062 *
Ronald Cronc4d1b512020-07-31 11:26:37 +020063 * \retval #PSA_SUCCESS
64 * The pointer to the key slot containing the description of the key
65 * identified by \p key was returned.
66 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine09829032018-12-10 17:00:38 +010067 * The library has not been initialized.
Ronald Cronc4d1b512020-07-31 11:26:37 +020068 * \retval #PSA_ERROR_INVALID_HANDLE
69 * \p key is not a valid key identifier.
70 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
71 * \p key is a persistent key identifier. The implementation does not
72 * have sufficient resources to load the persistent key. This can be
73 * due to a lack of empty key slot, or available memory.
74 * \retval #PSA_ERROR_DOES_NOT_EXIST
75 * There is no key with key identifier \p key.
76 * \retval #PSA_ERROR_CORRUPTION_DETECTED
77 * \retval #PSA_ERROR_STORAGE_FAILURE
78 * \retval #PSA_ERROR_DATA_CORRUPT
Gilles Peskine09829032018-12-10 17:00:38 +010079 */
Ronald Cronc4d1b512020-07-31 11:26:37 +020080psa_status_t psa_get_key_slot( mbedtls_svc_key_id_t key,
Gilles Peskine66fb1262018-12-10 16:29:04 +010081 psa_key_slot_t **p_slot );
82
Gilles Peskine09829032018-12-10 17:00:38 +010083/** Initialize the key slot structures.
84 *
85 * \retval PSA_SUCCESS
86 * Currently this function always succeeds.
87 */
Gilles Peskine66fb1262018-12-10 16:29:04 +010088psa_status_t psa_initialize_key_slots( void );
89
Gilles Peskine09829032018-12-10 17:00:38 +010090/** Delete all data from key slots in memory.
91 *
92 * This does not affect persistent storage. */
Gilles Peskine66fb1262018-12-10 16:29:04 +010093void psa_wipe_all_key_slots( void );
94
Gilles Peskine41e50d22019-07-31 15:01:55 +020095/** Find a free key slot.
96 *
97 * This function returns a key slot that is available for use and is in its
98 * ground state (all-bits-zero).
Gilles Peskinef46f81c2019-05-27 14:53:10 +020099 *
Ronald Cron2a993152020-07-17 14:13:26 +0200100 * \param[out] volatile_key_id On success, volatile key identifier
101 * associated to the returned slot.
102 * \param[out] p_slot On success, a pointer to the slot.
Gilles Peskinef46f81c2019-05-27 14:53:10 +0200103 *
104 * \retval #PSA_SUCCESS
105 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine267c6562019-05-27 19:01:54 +0200106 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef46f81c2019-05-27 14:53:10 +0200107 */
Ronald Cronc4d1b512020-07-31 11:26:37 +0200108psa_status_t psa_get_empty_key_slot( psa_key_id_t *volatile_key_id,
Gilles Peskineedbed562019-08-07 18:19:59 +0200109 psa_key_slot_t **p_slot );
Gilles Peskinef46f81c2019-05-27 14:53:10 +0200110
Gilles Peskine011e4282019-06-26 18:34:38 +0200111/** Test whether a lifetime designates a key in an external cryptoprocessor.
112 *
113 * \param lifetime The lifetime to test.
114 *
115 * \retval 1
116 * The lifetime designates an external key. There should be a
117 * registered driver for this lifetime, otherwise the key cannot
118 * be created or manipulated.
119 * \retval 0
120 * The lifetime designates a key that is volatile or in internal
121 * storage.
122 */
123static inline int psa_key_lifetime_is_external( psa_key_lifetime_t lifetime )
124{
Steven Cooremanc59de6a2020-06-08 18:28:25 +0200125 return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime )
126 != PSA_KEY_LOCATION_LOCAL_STORAGE );
Gilles Peskine011e4282019-06-26 18:34:38 +0200127}
128
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200129/** Validate a key's location.
Gilles Peskined167b942019-04-19 18:19:40 +0200130 *
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200131 * This function checks whether the key's attributes point to a location that
132 * is known to the PSA Core, and returns the driver function table if the key
133 * is to be found in an external location.
Gilles Peskined167b942019-04-19 18:19:40 +0200134 *
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200135 * \param[in] lifetime The key lifetime attribute.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200136 * \param[out] p_drv On success, when a key is located in external
137 * storage, returns a pointer to the driver table
138 * associated with the key's storage location.
Gilles Peskine011e4282019-06-26 18:34:38 +0200139 *
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200140 * \retval #PSA_SUCCESS
141 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskined167b942019-04-19 18:19:40 +0200142 */
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200143psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime,
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200144 psa_se_drv_table_entry_t **p_drv );
145
Ronald Crond2ed4812020-07-17 16:11:30 +0200146/** Validate the persistence of a key.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200147 *
Ronald Cron27238fc2020-07-23 12:30:41 +0200148 * \param[in] lifetime The key lifetime attribute.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200149 *
150 * \retval #PSA_SUCCESS
Ronald Crond2ed4812020-07-17 16:11:30 +0200151 * \retval #PSA_ERROR_INVALID_ARGUMENT The key is persistent but persistent
152 * keys are not supported.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200153 */
Ronald Crond2ed4812020-07-17 16:11:30 +0200154psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime );
155
156/** Validate a key identifier.
157 *
Ronald Cronfc9c5562020-10-15 19:24:49 +0200158 * \param[in] key The key identifier.
159 * \param[in] vendor_ok Non-zero to indicate that key identifiers in the
160 * vendor range are allowed, volatile key identifiers
161 * excepted \c 0 otherwise.
162 * \param[in] volatile_ok Non-zero to indicate that volatile key identifiers
163 * are allowed \c 0 otherwise.
Ronald Crond2ed4812020-07-17 16:11:30 +0200164 *
165 * \retval #PSA_SUCCESS The identifier is valid.
166 * \retval #PSA_ERROR_INVALID_ARGUMENT The key identifier is not valid.
167 */
Ronald Cronfc9c5562020-10-15 19:24:49 +0200168psa_status_t psa_validate_key_id(
169 mbedtls_svc_key_id_t key, int vendor_ok, int volatile_ok );
Gilles Peskined167b942019-04-19 18:19:40 +0200170
Gilles Peskine961849f2018-11-30 18:54:54 +0100171#endif /* PSA_CRYPTO_SLOT_MANAGEMENT_H */