blob: 472253dd9b07e43f9de74df74b94ff715d03f074 [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{
91 return( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
92 lifetime != PSA_KEY_LIFETIME_PERSISTENT );
93}
94
Gilles Peskined167b942019-04-19 18:19:40 +020095/** Test whether the given parameters are acceptable for a persistent key.
96 *
97 * This function does not access the storage in any way. It only tests
98 * whether the parameters are meaningful and permitted by general policy.
99 * It does not test whether the a file by the given id exists or could be
100 * created.
101 *
Gilles Peskine011e4282019-06-26 18:34:38 +0200102 * If the key is in external storage, this function returns the corresponding
103 * driver.
104 *
Gilles Peskined167b942019-04-19 18:19:40 +0200105 * \param lifetime The lifetime to test.
106 * \param id The key id to test.
Gilles Peskine011e4282019-06-26 18:34:38 +0200107 * \param[out] p_drv On output, if \p lifetime designates a key
108 * in an external processor, \c *p_drv is a pointer
109 * to the driver table entry fot this lifetime.
110 * If \p lifetime designates a transparent key,
111 * \c *p_drv is \c NULL.
Gilles Peskinef9666592019-05-06 18:56:30 +0200112 * \param creating 0 if attempting to open an existing key.
113 * Nonzero if attempting to create a key.
Gilles Peskined167b942019-04-19 18:19:40 +0200114 *
115 * \retval PSA_SUCCESS
116 * The given parameters are valid.
117 * \retval PSA_ERROR_INVALID_ARGUMENT
118 * \p lifetime is volatile or is invalid.
119 * \retval PSA_ERROR_INVALID_ARGUMENT
120 * \p id is invalid.
121 */
122psa_status_t psa_validate_persistent_key_parameters(
123 psa_key_lifetime_t lifetime,
Gilles Peskinef9666592019-05-06 18:56:30 +0200124 psa_key_file_id_t id,
Gilles Peskine8abe6a22019-07-12 23:40:35 +0200125 psa_se_drv_table_entry_t **p_drv,
Gilles Peskinef9666592019-05-06 18:56:30 +0200126 int creating );
Gilles Peskined167b942019-04-19 18:19:40 +0200127
128
Gilles Peskine961849f2018-11-30 18:54:54 +0100129#endif /* PSA_CRYPTO_SLOT_MANAGEMENT_H */