blob: 5bf0c0e15c09a8b2915051ebc2059f33f645b0cb [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/*
5 * Copyright (C) 2018, ARM Limited, All Rights Reserved
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.
19 *
20 * This file is part of mbed TLS (https://tls.mbed.org)
21 */
22
23#ifndef PSA_CRYPTO_SLOT_MANAGEMENT_H
24#define PSA_CRYPTO_SLOT_MANAGEMENT_H
25
Gilles Peskine011e4282019-06-26 18:34:38 +020026#include "psa/crypto.h"
27#include "psa_crypto_se.h"
28
Gilles Peskine961849f2018-11-30 18:54:54 +010029/* Number of key slots (plus one because 0 is not used).
30 * The value is a compile-time constant for now, for simplicity. */
31#define PSA_KEY_SLOT_COUNT 32
32
Gilles Peskine09829032018-12-10 17:00:38 +010033/** Access a key slot at the given handle.
34 *
35 * \param handle Key handle to query.
36 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
37 * key slot in memory designated by \p handle.
38 *
39 * \retval PSA_SUCCESS
40 * Success: \p handle is a handle to `*p_slot`. Note that `*p_slot`
41 * may be empty or occupied.
42 * \retval PSA_ERROR_INVALID_HANDLE
43 * \p handle is out of range or is not in use.
44 * \retval PSA_ERROR_BAD_STATE
45 * The library has not been initialized.
46 */
Gilles Peskine66fb1262018-12-10 16:29:04 +010047psa_status_t psa_get_key_slot( psa_key_handle_t handle,
48 psa_key_slot_t **p_slot );
49
Gilles Peskine09829032018-12-10 17:00:38 +010050/** Initialize the key slot structures.
51 *
52 * \retval PSA_SUCCESS
53 * Currently this function always succeeds.
54 */
Gilles Peskine66fb1262018-12-10 16:29:04 +010055psa_status_t psa_initialize_key_slots( void );
56
Gilles Peskine09829032018-12-10 17:00:38 +010057/** Delete all data from key slots in memory.
58 *
59 * This does not affect persistent storage. */
Gilles Peskine66fb1262018-12-10 16:29:04 +010060void psa_wipe_all_key_slots( void );
61
Gilles Peskine41e50d22019-07-31 15:01:55 +020062/** Find a free key slot.
63 *
64 * This function returns a key slot that is available for use and is in its
65 * ground state (all-bits-zero).
Gilles Peskinef46f81c2019-05-27 14:53:10 +020066 *
Gilles Peskinebfcae2e2019-06-05 11:39:57 +020067 * \param[out] handle On success, a slot number that can be used as a
Gilles Peskine41e50d22019-07-31 15:01:55 +020068 * handle to the slot.
Gilles Peskine267c6562019-05-27 19:01:54 +020069 * \param[out] p_slot On success, a pointer to the slot.
Gilles Peskinef46f81c2019-05-27 14:53:10 +020070 *
71 * \retval #PSA_SUCCESS
72 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine267c6562019-05-27 19:01:54 +020073 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef46f81c2019-05-27 14:53:10 +020074 */
Gilles Peskineedbed562019-08-07 18:19:59 +020075psa_status_t psa_get_empty_key_slot( psa_key_handle_t *handle,
76 psa_key_slot_t **p_slot );
Gilles Peskinef46f81c2019-05-27 14:53:10 +020077
Gilles Peskine011e4282019-06-26 18:34:38 +020078/** Test whether a lifetime designates a key in an external cryptoprocessor.
79 *
80 * \param lifetime The lifetime to test.
81 *
82 * \retval 1
83 * The lifetime designates an external key. There should be a
84 * registered driver for this lifetime, otherwise the key cannot
85 * be created or manipulated.
86 * \retval 0
87 * The lifetime designates a key that is volatile or in internal
88 * storage.
89 */
90static inline int psa_key_lifetime_is_external( psa_key_lifetime_t lifetime )
91{
Steven Cooremanc59de6a2020-06-08 18:28:25 +020092 return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime )
93 != PSA_KEY_LOCATION_LOCAL_STORAGE );
Gilles Peskine011e4282019-06-26 18:34:38 +020094}
95
Steven Cooreman8c1e7592020-06-17 14:52:05 +020096/** Validate a key's location.
Gilles Peskined167b942019-04-19 18:19:40 +020097 *
Steven Cooreman81fe7c32020-06-08 18:37:19 +020098 * This function checks whether the key's attributes point to a location that
99 * is known to the PSA Core, and returns the driver function table if the key
100 * is to be found in an external location.
Gilles Peskined167b942019-04-19 18:19:40 +0200101 *
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200102 * \param[in] lifetime The key lifetime attribute.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200103 * \param[out] p_drv On success, when a key is located in external
104 * storage, returns a pointer to the driver table
105 * associated with the key's storage location.
Gilles Peskine011e4282019-06-26 18:34:38 +0200106 *
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200107 * \retval #PSA_SUCCESS
108 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskined167b942019-04-19 18:19:40 +0200109 */
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200110psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime,
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200111 psa_se_drv_table_entry_t **p_drv );
112
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200113/** Validate that a key's persistence attributes are valid.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200114 *
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200115 * This function checks whether a key's declared persistence level and key ID
116 * attributes are valid and known to the PSA Core in its actual configuration.
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200117 *
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200118 * \param[in] lifetime The key lifetime attribute.
119 * \param[in] key_id The key ID attribute
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200120 *
121 * \retval #PSA_SUCCESS
122 * \retval #PSA_ERROR_INVALID_ARGUMENT
123 */
Steven Cooreman8c1e7592020-06-17 14:52:05 +0200124psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime,
125 psa_key_id_t key_id );
Gilles Peskined167b942019-04-19 18:19:40 +0200126
127
Gilles Peskine961849f2018-11-30 18:54:54 +0100128#endif /* PSA_CRYPTO_SLOT_MANAGEMENT_H */