blob: c6fecbb7aeefdf67f6af7b06b716c9827427b695 [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
Gilles Peskine09829032018-12-10 17:00:38 +010031/** Access a key slot at the given handle.
32 *
33 * \param handle Key handle to query.
34 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
35 * key slot in memory designated by \p handle.
36 *
37 * \retval PSA_SUCCESS
38 * Success: \p handle is a handle to `*p_slot`. Note that `*p_slot`
39 * may be empty or occupied.
40 * \retval PSA_ERROR_INVALID_HANDLE
41 * \p handle is out of range or is not in use.
42 * \retval PSA_ERROR_BAD_STATE
43 * The library has not been initialized.
44 */
Gilles Peskine66fb1262018-12-10 16:29:04 +010045psa_status_t psa_get_key_slot( psa_key_handle_t handle,
46 psa_key_slot_t **p_slot );
47
Gilles Peskine09829032018-12-10 17:00:38 +010048/** Initialize the key slot structures.
49 *
50 * \retval PSA_SUCCESS
51 * Currently this function always succeeds.
52 */
Gilles Peskine66fb1262018-12-10 16:29:04 +010053psa_status_t psa_initialize_key_slots( void );
54
Gilles Peskine09829032018-12-10 17:00:38 +010055/** Delete all data from key slots in memory.
56 *
57 * This does not affect persistent storage. */
Gilles Peskine66fb1262018-12-10 16:29:04 +010058void psa_wipe_all_key_slots( void );
59
Gilles Peskine41e50d22019-07-31 15:01:55 +020060/** Find a free key slot.
61 *
62 * This function returns a key slot that is available for use and is in its
63 * ground state (all-bits-zero).
Gilles Peskinef46f81c2019-05-27 14:53:10 +020064 *
Gilles Peskinebfcae2e2019-06-05 11:39:57 +020065 * \param[out] handle On success, a slot number that can be used as a
Gilles Peskine41e50d22019-07-31 15:01:55 +020066 * handle to the slot.
Gilles Peskine267c6562019-05-27 19:01:54 +020067 * \param[out] p_slot On success, a pointer to the slot.
Gilles Peskinef46f81c2019-05-27 14:53:10 +020068 *
69 * \retval #PSA_SUCCESS
70 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine267c6562019-05-27 19:01:54 +020071 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef46f81c2019-05-27 14:53:10 +020072 */
Gilles Peskineedbed562019-08-07 18:19:59 +020073psa_status_t psa_get_empty_key_slot( psa_key_handle_t *handle,
74 psa_key_slot_t **p_slot );
Gilles Peskinef46f81c2019-05-27 14:53:10 +020075
Gilles Peskine011e4282019-06-26 18:34:38 +020076/** Test whether a lifetime designates a key in an external cryptoprocessor.
77 *
78 * \param lifetime The lifetime to test.
79 *
80 * \retval 1
81 * The lifetime designates an external key. There should be a
82 * registered driver for this lifetime, otherwise the key cannot
83 * be created or manipulated.
84 * \retval 0
85 * The lifetime designates a key that is volatile or in internal
86 * storage.
87 */
88static inline int psa_key_lifetime_is_external( psa_key_lifetime_t lifetime )
89{
Steven Cooremanc59de6a2020-06-08 18:28:25 +020090 return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime )
91 != PSA_KEY_LOCATION_LOCAL_STORAGE );
Gilles Peskine011e4282019-06-26 18:34:38 +020092}
93
Steven Cooreman8c1e7592020-06-17 14:52:05 +020094/** Validate a key's location.
Gilles Peskined167b942019-04-19 18:19:40 +020095 *
Steven Cooreman81fe7c32020-06-08 18:37:19 +020096 * This function checks whether the key's attributes point to a location that
97 * is known to the PSA Core, and returns the driver function table if the key
98 * is to be found in an external location.
Gilles Peskined167b942019-04-19 18:19:40 +020099 *
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200100 * \param[in] lifetime The key lifetime attribute.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200101 * \param[out] p_drv On success, when a key is located in external
102 * storage, returns a pointer to the driver table
103 * associated with the key's storage location.
Gilles Peskine011e4282019-06-26 18:34:38 +0200104 *
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200105 * \retval #PSA_SUCCESS
106 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskined167b942019-04-19 18:19:40 +0200107 */
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200108psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime,
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200109 psa_se_drv_table_entry_t **p_drv );
110
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200111/** Validate that a key's persistence attributes are valid.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200112 *
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200113 * This function checks whether a key's declared persistence level and key ID
114 * attributes are valid and known to the PSA Core in its actual configuration.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200115 *
Ronald Cron27238fc2020-07-23 12:30:41 +0200116 * \param[in] lifetime The key lifetime attribute.
117 * \param[in] key The key identifier.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200118 *
119 * \retval #PSA_SUCCESS
120 * \retval #PSA_ERROR_INVALID_ARGUMENT
121 */
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200122psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime,
Ronald Cron71016a92020-08-28 19:01:50 +0200123 mbedtls_svc_key_id_t key );
Gilles Peskined167b942019-04-19 18:19:40 +0200124
Gilles Peskine961849f2018-11-30 18:54:54 +0100125#endif /* PSA_CRYPTO_SLOT_MANAGEMENT_H */