Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 4 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 5 | * Copyright The Mbed TLS Contributors |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 6 | * 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 Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #ifndef PSA_CRYPTO_SLOT_MANAGEMENT_H |
| 22 | #define PSA_CRYPTO_SLOT_MANAGEMENT_H |
| 23 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 24 | #include "psa/crypto.h" |
| 25 | #include "psa_crypto_se.h" |
| 26 | |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 27 | /* 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 Peskine | 0982903 | 2018-12-10 17:00:38 +0100 | [diff] [blame] | 31 | /** 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 Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 45 | psa_status_t psa_get_key_slot( psa_key_handle_t handle, |
| 46 | psa_key_slot_t **p_slot ); |
| 47 | |
Gilles Peskine | 0982903 | 2018-12-10 17:00:38 +0100 | [diff] [blame] | 48 | /** Initialize the key slot structures. |
| 49 | * |
| 50 | * \retval PSA_SUCCESS |
| 51 | * Currently this function always succeeds. |
| 52 | */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 53 | psa_status_t psa_initialize_key_slots( void ); |
| 54 | |
Gilles Peskine | 0982903 | 2018-12-10 17:00:38 +0100 | [diff] [blame] | 55 | /** Delete all data from key slots in memory. |
| 56 | * |
| 57 | * This does not affect persistent storage. */ |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 58 | void psa_wipe_all_key_slots( void ); |
| 59 | |
Gilles Peskine | 41e50d2 | 2019-07-31 15:01:55 +0200 | [diff] [blame] | 60 | /** 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 Peskine | f46f81c | 2019-05-27 14:53:10 +0200 | [diff] [blame] | 64 | * |
Gilles Peskine | bfcae2e | 2019-06-05 11:39:57 +0200 | [diff] [blame] | 65 | * \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] | 66 | * handle to the slot. |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 67 | * \param[out] p_slot On success, a pointer to the slot. |
Gilles Peskine | f46f81c | 2019-05-27 14:53:10 +0200 | [diff] [blame] | 68 | * |
| 69 | * \retval #PSA_SUCCESS |
| 70 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
Gilles Peskine | 267c656 | 2019-05-27 19:01:54 +0200 | [diff] [blame] | 71 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | f46f81c | 2019-05-27 14:53:10 +0200 | [diff] [blame] | 72 | */ |
Gilles Peskine | edbed56 | 2019-08-07 18:19:59 +0200 | [diff] [blame] | 73 | psa_status_t psa_get_empty_key_slot( psa_key_handle_t *handle, |
| 74 | psa_key_slot_t **p_slot ); |
Gilles Peskine | f46f81c | 2019-05-27 14:53:10 +0200 | [diff] [blame] | 75 | |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 76 | /** 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 | */ |
| 88 | static inline int psa_key_lifetime_is_external( psa_key_lifetime_t lifetime ) |
| 89 | { |
Steven Cooreman | c59de6a | 2020-06-08 18:28:25 +0200 | [diff] [blame] | 90 | return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) |
| 91 | != PSA_KEY_LOCATION_LOCAL_STORAGE ); |
Gilles Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 94 | /** Validate a key's location. |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 95 | * |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 96 | * 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 Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 99 | * |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 100 | * \param[in] lifetime The key lifetime attribute. |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 101 | * \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 Peskine | 011e428 | 2019-06-26 18:34:38 +0200 | [diff] [blame] | 104 | * |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 105 | * \retval #PSA_SUCCESS |
| 106 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 107 | */ |
Steven Cooreman | 8c1e759 | 2020-06-17 14:52:05 +0200 | [diff] [blame] | 108 | psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime, |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 109 | psa_se_drv_table_entry_t **p_drv ); |
| 110 | |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame^] | 111 | /** Validate the persistence of a key. |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 112 | * |
Ronald Cron | 27238fc | 2020-07-23 12:30:41 +0200 | [diff] [blame] | 113 | * \param[in] lifetime The key lifetime attribute. |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 114 | * |
| 115 | * \retval #PSA_SUCCESS |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame^] | 116 | * \retval #PSA_ERROR_INVALID_ARGUMENT The key is persistent but persistent |
| 117 | * keys are not supported. |
Steven Cooreman | 81fe7c3 | 2020-06-08 18:37:19 +0200 | [diff] [blame] | 118 | */ |
Ronald Cron | d2ed481 | 2020-07-17 16:11:30 +0200 | [diff] [blame^] | 119 | psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime ); |
| 120 | |
| 121 | /** Validate a key identifier. |
| 122 | * |
| 123 | * \param[in] key The key identifier. |
| 124 | * \param[in] vendor_ok Non-zero to indicate that key identifiers in the |
| 125 | * vendor range are allowed, \c 0 otherwise. |
| 126 | * |
| 127 | * \retval #PSA_SUCCESS The identifier is valid. |
| 128 | * \retval #PSA_ERROR_INVALID_ARGUMENT The key identifier is not valid. |
| 129 | */ |
| 130 | psa_status_t psa_validate_key_id( mbedtls_svc_key_id_t key, int vendor_ok ); |
Gilles Peskine | d167b94 | 2019-04-19 18:19:40 +0200 | [diff] [blame] | 131 | |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 132 | #endif /* PSA_CRYPTO_SLOT_MANAGEMENT_H */ |