Mbed OS Reference
Loading...
Searching...
No Matches
psa_crypto_slot_management.h
Go to the documentation of this file.
1/**
2 * \file
3 *
4 * \brief PSA crypto layer on top of Mbed TLS crypto
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#ifndef PSA_CRYPTO_SLOT_MANAGEMENT_H
24#define PSA_CRYPTO_SLOT_MANAGEMENT_H
25
26#include "psa/crypto.h"
27#include "psa_crypto_core.h"
28#include "psa_crypto_se.h"
29
30/** Number of key slots (plus one because 0 is not used).
31 * The value is a compile-time constant for now, for simplicity. */
32#define PSA_KEY_SLOT_COUNT 32
33
34/** Range of volatile key identifiers.
35 *
36 * The last PSA_KEY_SLOT_COUNT identifiers of the implementation range
37 * of key identifiers are reserved for volatile key identifiers.
38 * A volatile key identifier is equal to #PSA_KEY_ID_VOLATILE_MIN plus the
39 * index of the key slot containing the volatile key definition.
40 */
41
42/** The minimum value for a volatile key identifier.
43 */
44#define PSA_KEY_ID_VOLATILE_MIN ( PSA_KEY_ID_VENDOR_MAX - \
45 PSA_KEY_SLOT_COUNT + 1 )
46
47/** The maximum value for a volatile key identifier.
48 */
49#define PSA_KEY_ID_VOLATILE_MAX PSA_KEY_ID_VENDOR_MAX
50
51/** Test whether a key identifier is a volatile key identifier.
52 *
53 * \param key_id Key identifier to test.
54 *
55 * \retval 1
56 * The key identifier is a volatile key identifier.
57 * \retval 0
58 * The key identifier is not a volatile key identifier.
59 */
60static inline int psa_key_id_is_volatile( psa_key_id_t key_id )
61{
62 return( ( key_id >= PSA_KEY_ID_VOLATILE_MIN ) &&
63 ( key_id <= PSA_KEY_ID_VOLATILE_MAX ) );
64}
65
66/** Get the description of a key given its identifier and lock it.
67 *
68 * The descriptions of volatile keys and loaded persistent keys are stored in
69 * key slots. This function returns a pointer to the key slot containing the
70 * description of a key given its identifier.
71 *
72 * In case of a persistent key, the function loads the description of the key
73 * into a key slot if not already done.
74 *
75 * On success, the returned key slot is locked. It is the responsibility of
76 * the caller to unlock the key slot when it does not access it anymore.
77 *
78 * \param key Key identifier to query.
79 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
80 * key slot containing the description of the key
81 * identified by \p key.
82 *
83 * \retval #PSA_SUCCESS
84 * \p *p_slot contains a pointer to the key slot containing the
85 * description of the key identified by \p key.
86 * The key slot counter has been incremented.
87 * \retval #PSA_ERROR_BAD_STATE
88 * The library has not been initialized.
89 * \retval #PSA_ERROR_INVALID_HANDLE
90 * \p key is not a valid key identifier.
91 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
92 * \p key is a persistent key identifier. The implementation does not
93 * have sufficient resources to load the persistent key. This can be
94 * due to a lack of empty key slot, or available memory.
95 * \retval #PSA_ERROR_DOES_NOT_EXIST
96 * There is no key with key identifier \p key.
97 * \retval #PSA_ERROR_CORRUPTION_DETECTED
98 * \retval #PSA_ERROR_STORAGE_FAILURE
99 * \retval #PSA_ERROR_DATA_CORRUPT
100 */
102 psa_key_slot_t **p_slot );
103
104/** Initialize the key slot structures.
105 *
106 * \retval #PSA_SUCCESS
107 * Currently this function always succeeds.
108 */
110
111/** Delete all data from key slots in memory.
112 *
113 * This does not affect persistent storage. */
115
116/** Find a free key slot.
117 *
118 * This function returns a key slot that is available for use and is in its
119 * ground state (all-bits-zero). On success, the key slot is locked. It is
120 * the responsibility of the caller to unlock the key slot when it does not
121 * access it anymore.
122 *
123 * \param[out] volatile_key_id On success, volatile key identifier
124 * associated to the returned slot.
125 * \param[out] p_slot On success, a pointer to the slot.
126 *
127 * \retval #PSA_SUCCESS
128 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
129 * \retval #PSA_ERROR_BAD_STATE
130 */
132 psa_key_slot_t **p_slot );
133
134/** Lock a key slot.
135 *
136 * This function increments the key slot lock counter by one.
137 *
138 * \param[in] slot The key slot.
139 *
140 * \retval #PSA_SUCCESS
141 The key slot lock counter was incremented.
142 * \retval #PSA_ERROR_CORRUPTION_DETECTED
143 * The lock counter already reached its maximum value and was not
144 * increased.
145 */
146static inline psa_status_t psa_lock_key_slot( psa_key_slot_t *slot )
147{
148 if( slot->lock_count >= SIZE_MAX )
150
151 slot->lock_count++;
152
153 return( PSA_SUCCESS );
154}
155
156/** Unlock a key slot.
157 *
158 * This function decrements the key slot lock counter by one.
159 *
160 * \note To ease the handling of errors in retrieving a key slot
161 * a NULL input pointer is valid, and the function returns
162 * successfully without doing anything in that case.
163 *
164 * \param[in] slot The key slot.
165 * \retval #PSA_SUCCESS
166 * \p slot is NULL or the key slot lock counter has been
167 * decremented successfully.
168 * \retval #PSA_ERROR_CORRUPTION_DETECTED
169 * The lock counter was equal to 0.
170 *
171 */
173
174/** Test whether a lifetime designates a key in an external cryptoprocessor.
175 *
176 * \param lifetime The lifetime to test.
177 *
178 * \retval 1
179 * The lifetime designates an external key. There should be a
180 * registered driver for this lifetime, otherwise the key cannot
181 * be created or manipulated.
182 * \retval 0
183 * The lifetime designates a key that is volatile or in internal
184 * storage.
185 */
186static inline int psa_key_lifetime_is_external( psa_key_lifetime_t lifetime )
187{
188 return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime )
190}
191
192/** Validate a key's location.
193 *
194 * This function checks whether the key's attributes point to a location that
195 * is known to the PSA Core, and returns the driver function table if the key
196 * is to be found in an external location.
197 *
198 * \param[in] lifetime The key lifetime attribute.
199 * \param[out] p_drv On success, when a key is located in external
200 * storage, returns a pointer to the driver table
201 * associated with the key's storage location.
202 *
203 * \retval #PSA_SUCCESS
204 * \retval #PSA_ERROR_INVALID_ARGUMENT
205 */
207 psa_se_drv_table_entry_t **p_drv );
208
209/** Validate the persistence of a key.
210 *
211 * \param[in] lifetime The key lifetime attribute.
212 *
213 * \retval #PSA_SUCCESS
214 * \retval #PSA_ERROR_INVALID_ARGUMENT The key is persistent but persistent
215 * keys are not supported.
216 */
218
219/** Validate a key identifier.
220 *
221 * \param[in] key The key identifier.
222 * \param[in] vendor_ok Non-zero to indicate that key identifiers in the
223 * vendor range are allowed, volatile key identifiers
224 * excepted \c 0 otherwise.
225 *
226 * \retval #PSA_SUCCESS The identifier is valid.
227 * \retval #PSA_ERROR_INVALID_ARGUMENT The key identifier is not valid.
228 */
230
231#endif /* PSA_CRYPTO_SLOT_MANAGEMENT_H */
Platform Security Architecture cryptography module.
int32_t psa_status_t
Function return status.
Definition: crypto_types.h:55
#define PSA_ERROR_CORRUPTION_DETECTED
A tampering attempt was detected.
#define PSA_SUCCESS
The action was completed successfully.
Definition: crypto_values.h:45
uint32_t psa_key_id_t
Encoding of identifiers of persistent keys.
Definition: crypto_types.h:231
#define PSA_KEY_LOCATION_LOCAL_STORAGE
The local storage area for persistent keys.
#define PSA_KEY_LIFETIME_GET_LOCATION(lifetime)
Get the location (see psa_key_location_t) from a psa_key_lifetime_t.
uint32_t psa_key_lifetime_t
Encoding of key lifetimes.
Definition: crypto_types.h:141
psa_key_id_t mbedtls_svc_key_id_t
Identifier for persistent keys.
Definition: crypto_types.h:237
#define PSA_KEY_ID_VOLATILE_MAX
The maximum value for a volatile key identifier.
psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot)
Get the description of a key given its identifier and lock it.
psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime)
Validate the persistence of a key.
#define PSA_KEY_ID_VOLATILE_MIN
Range of volatile key identifiers.
psa_status_t psa_unlock_key_slot(psa_key_slot_t *slot)
Unlock a key slot.
psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime, psa_se_drv_table_entry_t **p_drv)
Validate a key's location.
psa_status_t psa_validate_key_id(mbedtls_svc_key_id_t key, int vendor_ok)
Validate a key identifier.
void psa_wipe_all_key_slots(void)
Delete all data from key slots in memory.
psa_status_t psa_initialize_key_slots(void)
Initialize the key slot structures.
psa_status_t psa_get_empty_key_slot(psa_key_id_t *volatile_key_id, psa_key_slot_t **p_slot)
Find a free key slot.
The data structure representing a key slot, containing key material and metadata for one key.