Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 1 | /* |
| 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 Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 25 | #include "psa/crypto.h" |
| 26 | #include "psa_crypto_se.h" |
| 27 | |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 28 | /* 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 Peskine | 0982903 | 2018-12-10 17:00:38 +0100 | [diff] [blame] | 32 | /** 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 Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 46 | psa_status_t psa_get_key_slot( psa_key_handle_t handle, |
| 47 | psa_key_slot_t **p_slot ); |
| 48 | |
Gilles Peskine | 0982903 | 2018-12-10 17:00:38 +0100 | [diff] [blame] | 49 | /** Initialize the key slot structures. |
| 50 | * |
| 51 | * \retval PSA_SUCCESS |
| 52 | * Currently this function always succeeds. |
| 53 | */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 54 | psa_status_t psa_initialize_key_slots( void ); |
| 55 | |
Gilles Peskine | 0982903 | 2018-12-10 17:00:38 +0100 | [diff] [blame] | 56 | /** Delete all data from key slots in memory. |
| 57 | * |
| 58 | * This does not affect persistent storage. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 59 | void psa_wipe_all_key_slots( void ); |
| 60 | |
Gilles Peskine | 41e50d2 | 2019-07-31 15:01:55 +0200 | [diff] [blame] | 61 | /** 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 Peskine | f46f81c | 2019-05-27 14:53:10 +0200 | [diff] [blame] | 65 | * |
Gilles Peskine | bfcae2e | 2019-06-05 11:39:57 +0200 | [diff] [blame] | 66 | * \param[out] handle On success, a slot number that can be used as a |
Gilles Peskine | 41e50d2 | 2019-07-31 15:01:55 +0200 | [diff] [blame] | 67 | * handle to the slot. |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 68 | * \param[out] p_slot On success, a pointer to the slot. |
Gilles Peskine | f46f81c | 2019-05-27 14:53:10 +0200 | [diff] [blame] | 69 | * |
| 70 | * \retval #PSA_SUCCESS |
| 71 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 72 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | f46f81c | 2019-05-27 14:53:10 +0200 | [diff] [blame] | 73 | */ |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 74 | psa_status_t psa_get_empty_key_slot( psa_key_handle_t *handle, |
| 75 | psa_key_slot_t **p_slot ); |
Gilles Peskine | f46f81c | 2019-05-27 14:53:10 +0200 | [diff] [blame] | 76 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 77 | /** 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 | */ |
| 89 | static inline int psa_key_lifetime_is_external( psa_key_lifetime_t lifetime ) |
| 90 | { |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 91 | return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) |
| 92 | != PSA_KEY_LOCATION_LOCAL_STORAGE ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame^] | 95 | /** Validate that a key's attributes point to a known location. |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 96 | * |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame^] | 97 | * 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 Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 100 | * |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame^] | 101 | * \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 Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 105 | * |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame^] | 106 | * \retval #PSA_SUCCESS |
| 107 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 108 | */ |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame^] | 109 | psa_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 | */ |
| 121 | psa_status_t psa_validate_key_persistence( const psa_key_attributes_t *attributes ); |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 122 | |
| 123 | |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 124 | #endif /* PSA_CRYPTO_SLOT_MANAGEMENT_H */ |