blob: 8841284cdf6c6b3c1b6ac7f551f21154c836d230 [file] [log] [blame]
Gilles Peskine961849f2018-11-30 18:54:54 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#ifndef PSA_CRYPTO_SLOT_MANAGEMENT_H
23#define PSA_CRYPTO_SLOT_MANAGEMENT_H
24
Gilles Peskine011e4282019-06-26 18:34:38 +020025#include "psa/crypto.h"
26#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
Gilles Peskine09829032018-12-10 17:00:38 +010032/** Access a key slot at the given handle.
33 *
34 * \param handle Key handle to query.
35 * \param[out] p_slot On success, `*p_slot` contains a pointer to the
36 * key slot in memory designated by \p handle.
37 *
38 * \retval PSA_SUCCESS
39 * Success: \p handle is a handle to `*p_slot`. Note that `*p_slot`
40 * may be empty or occupied.
41 * \retval PSA_ERROR_INVALID_HANDLE
42 * \p handle is out of range or is not in use.
43 * \retval PSA_ERROR_BAD_STATE
44 * The library has not been initialized.
45 */
Gilles Peskine66fb1262018-12-10 16:29:04 +010046psa_status_t psa_get_key_slot( psa_key_handle_t handle,
47 psa_key_slot_t **p_slot );
48
Gilles Peskine09829032018-12-10 17:00:38 +010049/** Initialize the key slot structures.
50 *
51 * \retval PSA_SUCCESS
52 * Currently this function always succeeds.
53 */
Gilles Peskine66fb1262018-12-10 16:29:04 +010054psa_status_t psa_initialize_key_slots( void );
55
Gilles Peskine09829032018-12-10 17:00:38 +010056/** Delete all data from key slots in memory.
57 *
58 * This does not affect persistent storage. */
Gilles Peskine66fb1262018-12-10 16:29:04 +010059void psa_wipe_all_key_slots( void );
60
Gilles Peskine41e50d22019-07-31 15:01:55 +020061/** Find a free key slot.
62 *
63 * This function returns a key slot that is available for use and is in its
64 * ground state (all-bits-zero).
Gilles Peskinef46f81c2019-05-27 14:53:10 +020065 *
Gilles Peskinebfcae2e2019-06-05 11:39:57 +020066 * \param[out] handle On success, a slot number that can be used as a
Gilles Peskine41e50d22019-07-31 15:01:55 +020067 * handle to the slot.
Gilles Peskine267c6562019-05-27 19:01:54 +020068 * \param[out] p_slot On success, a pointer to the slot.
Gilles Peskinef46f81c2019-05-27 14:53:10 +020069 *
70 * \retval #PSA_SUCCESS
71 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine267c6562019-05-27 19:01:54 +020072 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef46f81c2019-05-27 14:53:10 +020073 */
Gilles Peskineedbed562019-08-07 18:19:59 +020074psa_status_t psa_get_empty_key_slot( psa_key_handle_t *handle,
75 psa_key_slot_t **p_slot );
Gilles Peskinef46f81c2019-05-27 14:53:10 +020076
Gilles Peskine011e4282019-06-26 18:34:38 +020077/** Test whether a lifetime designates a key in an external cryptoprocessor.
78 *
79 * \param lifetime The lifetime to test.
80 *
81 * \retval 1
82 * The lifetime designates an external key. There should be a
83 * registered driver for this lifetime, otherwise the key cannot
84 * be created or manipulated.
85 * \retval 0
86 * The lifetime designates a key that is volatile or in internal
87 * storage.
88 */
89static inline int psa_key_lifetime_is_external( psa_key_lifetime_t lifetime )
90{
Steven Cooremanc59de6a2020-06-08 18:28:25 +020091 return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime )
92 != PSA_KEY_LOCATION_LOCAL_STORAGE );
Gilles Peskine011e4282019-06-26 18:34:38 +020093}
94
Steven Cooreman81fe7c32020-06-08 18:37:19 +020095/** Validate that a key's attributes point to a known location.
Gilles Peskined167b942019-04-19 18:19:40 +020096 *
Steven Cooreman81fe7c32020-06-08 18:37:19 +020097 * This function checks whether the key's attributes point to a location that
98 * is known to the PSA Core, and returns the driver function table if the key
99 * is to be found in an external location.
Gilles Peskined167b942019-04-19 18:19:40 +0200100 *
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200101 * \param[in] attributes The key attributes.
102 * \param[out] p_drv On success, when a key is located in external
103 * storage, returns a pointer to the driver table
104 * associated with the key's storage location.
Gilles Peskine011e4282019-06-26 18:34:38 +0200105 *
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200106 * \retval #PSA_SUCCESS
107 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskined167b942019-04-19 18:19:40 +0200108 */
Steven Cooreman81fe7c32020-06-08 18:37:19 +0200109psa_status_t psa_validate_key_location( const psa_key_attributes_t *attributes,
110 psa_se_drv_table_entry_t **p_drv );
111
112/** Validate that a key's persistence is consistent.
113 *
114 * This function checks whether a key's persistence attribute is consistent.
115 *
116 * \param[in] attributes The key attributes.
117 *
118 * \retval #PSA_SUCCESS
119 * \retval #PSA_ERROR_INVALID_ARGUMENT
120 */
121psa_status_t psa_validate_key_persistence( const psa_key_attributes_t *attributes );
Gilles Peskined167b942019-04-19 18:19:40 +0200122
123
Gilles Peskine961849f2018-11-30 18:54:54 +0100124#endif /* PSA_CRYPTO_SLOT_MANAGEMENT_H */