blob: d649f53a7ce2bfeb5147c5f5b6ced2540885891f [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"
25#include "psa_crypto_se.h"
26
Gilles Peskine961849f2018-11-30 18:54:54 +010027/* Number of key slots (plus one because 0 is not used).
28 * The value is a compile-time constant for now, for simplicity. */
29#define PSA_KEY_SLOT_COUNT 32
30
Ronald Cron2a993152020-07-17 14:13:26 +020031/** Range of volatile key identifiers.
32 *
33 * The last PSA_KEY_SLOT_COUNT identifiers of the implementation range
34 * of key identifiers are reserved for volatile key identifiers.
35 * A volatile key identifier is equal to PSA_KEY_ID_VOLATILE_MIN plus the
36 * index of the key slot containing the volatile key definition.
37 */
38
39/** The minimum value for a volatile key identifier.
40 */
41#define PSA_KEY_ID_VOLATILE_MIN ( PSA_KEY_ID_VENDOR_MAX - \
42 PSA_KEY_SLOT_COUNT + 1 )
43
44/** The maximum value for a volatile key identifier.
45 */
46#define PSA_KEY_ID_VOLATILE_MAX PSA_KEY_ID_VENDOR_MAX
47
Gilles Peskine09829032018-12-10 17:00:38 +010048/** Access a key slot at the given handle.
49 *
50 * \param handle Key handle to query.
51 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
52 * key slot in memory designated by \p handle.
53 *
54 * \retval PSA_SUCCESS
55 * Success: \p handle is a handle to `*p_slot`. Note that `*p_slot`
56 * may be empty or occupied.
57 * \retval PSA_ERROR_INVALID_HANDLE
58 * \p handle is out of range or is not in use.
59 * \retval PSA_ERROR_BAD_STATE
60 * The library has not been initialized.
61 */
Gilles Peskine66fb1262018-12-10 16:29:04 +010062psa_status_t psa_get_key_slot( psa_key_handle_t handle,
63 psa_key_slot_t **p_slot );
64
Gilles Peskine09829032018-12-10 17:00:38 +010065/** Initialize the key slot structures.
66 *
67 * \retval PSA_SUCCESS
68 * Currently this function always succeeds.
69 */
Gilles Peskine66fb1262018-12-10 16:29:04 +010070psa_status_t psa_initialize_key_slots( void );
71
Gilles Peskine09829032018-12-10 17:00:38 +010072/** Delete all data from key slots in memory.
73 *
74 * This does not affect persistent storage. */
Gilles Peskine66fb1262018-12-10 16:29:04 +010075void psa_wipe_all_key_slots( void );
76
Gilles Peskine41e50d22019-07-31 15:01:55 +020077/** Find a free key slot.
78 *
79 * This function returns a key slot that is available for use and is in its
80 * ground state (all-bits-zero).
Gilles Peskinef46f81c2019-05-27 14:53:10 +020081 *
Ronald Cron2a993152020-07-17 14:13:26 +020082 * \param[out] handle On success, a slot number that can be used
83 * as a handle to the slot.
84 * \param[out] volatile_key_id On success, volatile key identifier
85 * associated to the returned slot.
86 * \param[out] p_slot On success, a pointer to the slot.
Gilles Peskinef46f81c2019-05-27 14:53:10 +020087 *
88 * \retval #PSA_SUCCESS
89 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine267c6562019-05-27 19:01:54 +020090 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef46f81c2019-05-27 14:53:10 +020091 */
Gilles Peskineedbed562019-08-07 18:19:59 +020092psa_status_t psa_get_empty_key_slot( psa_key_handle_t *handle,
Ronald Cron2a993152020-07-17 14:13:26 +020093 psa_key_id_t *volatile_key_id,
Gilles Peskineedbed562019-08-07 18:19:59 +020094 psa_key_slot_t **p_slot );
Gilles Peskinef46f81c2019-05-27 14:53:10 +020095
Gilles Peskine011e4282019-06-26 18:34:38 +020096/** Test whether a lifetime designates a key in an external cryptoprocessor.
97 *
98 * \param lifetime The lifetime to test.
99 *
100 * \retval 1
101 * The lifetime designates an external key. There should be a
102 * registered driver for this lifetime, otherwise the key cannot
103 * be created or manipulated.
104 * \retval 0
105 * The lifetime designates a key that is volatile or in internal
106 * storage.
107 */
108static inline int psa_key_lifetime_is_external( psa_key_lifetime_t lifetime )
109{
Steven Cooremanc59de6a2020-06-08 18:28:25 +0200110 return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime )
111 != PSA_KEY_LOCATION_LOCAL_STORAGE );
Gilles Peskine011e4282019-06-26 18:34:38 +0200112}
113
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200114/** Validate a key's location.
Gilles Peskined167b942019-04-19 18:19:40 +0200115 *
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200116 * This function checks whether the key's attributes point to a location that
117 * is known to the PSA Core, and returns the driver function table if the key
118 * is to be found in an external location.
Gilles Peskined167b942019-04-19 18:19:40 +0200119 *
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200120 * \param[in] lifetime The key lifetime attribute.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200121 * \param[out] p_drv On success, when a key is located in external
122 * storage, returns a pointer to the driver table
123 * associated with the key's storage location.
Gilles Peskine011e4282019-06-26 18:34:38 +0200124 *
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200125 * \retval #PSA_SUCCESS
126 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskined167b942019-04-19 18:19:40 +0200127 */
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200128psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime,
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200129 psa_se_drv_table_entry_t **p_drv );
130
Ronald Crond2ed4812020-07-17 16:11:30 +0200131/** Validate the persistence of a key.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200132 *
Ronald Cron27238fc2020-07-23 12:30:41 +0200133 * \param[in] lifetime The key lifetime attribute.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200134 *
135 * \retval #PSA_SUCCESS
Ronald Crond2ed4812020-07-17 16:11:30 +0200136 * \retval #PSA_ERROR_INVALID_ARGUMENT The key is persistent but persistent
137 * keys are not supported.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200138 */
Ronald Crond2ed4812020-07-17 16:11:30 +0200139psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime );
140
141/** Validate a key identifier.
142 *
143 * \param[in] key The key identifier.
144 * \param[in] vendor_ok Non-zero to indicate that key identifiers in the
145 * vendor range are allowed, \c 0 otherwise.
146 *
147 * \retval #PSA_SUCCESS The identifier is valid.
148 * \retval #PSA_ERROR_INVALID_ARGUMENT The key identifier is not valid.
149 */
150psa_status_t psa_validate_key_id( mbedtls_svc_key_id_t key, int vendor_ok );
Gilles Peskined167b942019-04-19 18:19:40 +0200151
Gilles Peskine961849f2018-11-30 18:54:54 +0100152#endif /* PSA_CRYPTO_SLOT_MANAGEMENT_H */